blob: 6a1e13ea996d5271be13952e55f5d85c2ba1a562 [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 Smirl815ecf82008-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 Smirl815ecf82008-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 Smirlf8ba0b7b2008-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 Smirlf8ba0b7b2008-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 Smirlf8ba0b7b2008-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 Brownbf3a9e12011-06-13 16:42:29 +0100680 if (path->weak)
681 continue;
682
Richard Purdie2b97eab2006-10-06 18:32:18 +0200683 if (path->walked)
684 continue;
685
686 if (path->sink && path->connect) {
687 path->walked = 1;
688 con += is_connected_output_ep(path->sink);
689 }
690 }
691
692 return con;
693}
694
695/*
696 * Recursively check for a completed path to an active or physically connected
697 * input widget. Returns number of complete paths.
698 */
699static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
700{
701 struct snd_soc_dapm_path *path;
702 int con = 0;
703
Mark Brownde02d072011-09-20 21:43:24 +0100704 DAPM_UPDATE_STAT(widget, path_checks);
705
Mark Brown246d0a12009-04-22 18:24:55 +0100706 if (widget->id == snd_soc_dapm_supply)
707 return 0;
708
Richard Purdie2b97eab2006-10-06 18:32:18 +0200709 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100710 switch (widget->id) {
711 case snd_soc_dapm_dac:
712 case snd_soc_dapm_aif_in:
713 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100714 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100715 default:
716 break;
717 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200718
719 if (widget->connected) {
720 /* connected pin ? */
721 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100722 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200723
724 /* connected VMID/Bias for lower pops */
725 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100726 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200727
728 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300729 if (widget->id == snd_soc_dapm_mic ||
730 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100731 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200732 }
733
734 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Brownbf3a9e12011-06-13 16:42:29 +0100735 if (path->weak)
736 continue;
737
Richard Purdie2b97eab2006-10-06 18:32:18 +0200738 if (path->walked)
739 continue;
740
741 if (path->source && path->connect) {
742 path->walked = 1;
743 con += is_connected_input_ep(path->source);
744 }
745 }
746
747 return con;
748}
749
750/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300751 * Handler for generic register modifier widget.
752 */
753int dapm_reg_event(struct snd_soc_dapm_widget *w,
754 struct snd_kcontrol *kcontrol, int event)
755{
756 unsigned int val;
757
758 if (SND_SOC_DAPM_EVENT_ON(event))
759 val = w->on_val;
760 else
761 val = w->off_val;
762
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100763 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300764 w->mask << w->shift, val << w->shift);
765
766 return 0;
767}
Mark Brown11589412008-07-29 11:42:23 +0100768EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300769
Mark Browncd0f2d42009-04-20 16:56:59 +0100770/* Generic check to see if a widget should be powered.
771 */
772static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
773{
774 int in, out;
775
Mark Brownde02d072011-09-20 21:43:24 +0100776 DAPM_UPDATE_STAT(w, power_checks);
777
Mark Browncd0f2d42009-04-20 16:56:59 +0100778 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200779 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100780 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200781 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100782 return out != 0 && in != 0;
783}
784
Mark Brown6ea31b92009-04-20 17:15:41 +0100785/* Check to see if an ADC has power */
786static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
787{
788 int in;
789
Mark Brownde02d072011-09-20 21:43:24 +0100790 DAPM_UPDATE_STAT(w, power_checks);
791
Mark Brown6ea31b92009-04-20 17:15:41 +0100792 if (w->active) {
793 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200794 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100795 return in != 0;
796 } else {
797 return dapm_generic_check_power(w);
798 }
799}
800
801/* Check to see if a DAC has power */
802static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
803{
804 int out;
805
Mark Brownde02d072011-09-20 21:43:24 +0100806 DAPM_UPDATE_STAT(w, power_checks);
807
Mark Brown6ea31b92009-04-20 17:15:41 +0100808 if (w->active) {
809 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200810 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100811 return out != 0;
812 } else {
813 return dapm_generic_check_power(w);
814 }
815}
816
Mark Brown246d0a12009-04-22 18:24:55 +0100817/* Check to see if a power supply is needed */
818static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
819{
820 struct snd_soc_dapm_path *path;
821 int power = 0;
822
Mark Brownde02d072011-09-20 21:43:24 +0100823 DAPM_UPDATE_STAT(w, power_checks);
824
Mark Brown246d0a12009-04-22 18:24:55 +0100825 /* Check if one of our outputs is connected */
826 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brownbf3a9e12011-06-13 16:42:29 +0100827 if (path->weak)
828 continue;
829
Mark Brown215edda2009-09-08 18:59:05 +0100830 if (path->connected &&
831 !path->connected(path->source, path->sink))
832 continue;
833
Mark Brown30173582011-02-11 11:42:19 +0000834 if (!path->sink)
835 continue;
836
837 if (path->sink->force) {
838 power = 1;
839 break;
840 }
841
842 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100843 path->sink->power_check(path->sink)) {
844 power = 1;
845 break;
846 }
847 }
848
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200849 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100850
851 return power;
852}
853
Mark Brown38357ab2009-06-06 19:03:23 +0100854static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
855 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000856 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000857{
Mark Brown828a8422011-01-15 13:14:30 +0000858 int *sort;
859
860 if (power_up)
861 sort = dapm_up_seq;
862 else
863 sort = dapm_down_seq;
864
Mark Brown38357ab2009-06-06 19:03:23 +0100865 if (sort[a->id] != sort[b->id])
866 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000867 if (a->subseq != b->subseq) {
868 if (power_up)
869 return a->subseq - b->subseq;
870 else
871 return b->subseq - a->subseq;
872 }
Mark Brownb22ead22009-06-07 12:51:26 +0100873 if (a->reg != b->reg)
874 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000875 if (a->dapm != b->dapm)
876 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000877
Mark Brown38357ab2009-06-06 19:03:23 +0100878 return 0;
879}
Mark Brown42aa3412009-03-01 19:21:10 +0000880
Mark Brown38357ab2009-06-06 19:03:23 +0100881/* Insert a widget in order into a DAPM power sequence. */
882static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
883 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000884 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100885{
886 struct snd_soc_dapm_widget *w;
887
888 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000889 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100890 list_add_tail(&new_widget->power_list, &w->power_list);
891 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000892 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100893
Mark Brown38357ab2009-06-06 19:03:23 +0100894 list_add_tail(&new_widget->power_list, list);
895}
Mark Brown42aa3412009-03-01 19:21:10 +0000896
Mark Brown68f89ad2010-11-03 23:51:49 -0400897static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
898 struct snd_soc_dapm_widget *w, int event)
899{
900 struct snd_soc_card *card = dapm->card;
901 const char *ev_name;
902 int power, ret;
903
904 switch (event) {
905 case SND_SOC_DAPM_PRE_PMU:
906 ev_name = "PRE_PMU";
907 power = 1;
908 break;
909 case SND_SOC_DAPM_POST_PMU:
910 ev_name = "POST_PMU";
911 power = 1;
912 break;
913 case SND_SOC_DAPM_PRE_PMD:
914 ev_name = "PRE_PMD";
915 power = 0;
916 break;
917 case SND_SOC_DAPM_POST_PMD:
918 ev_name = "POST_PMD";
919 power = 0;
920 break;
921 default:
922 BUG();
923 return;
924 }
925
926 if (w->power != power)
927 return;
928
929 if (w->event && (w->event_flags & event)) {
930 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
931 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400932 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400933 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400934 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400935 if (ret < 0)
936 pr_err("%s: %s event failed: %d\n",
937 ev_name, w->name, ret);
938 }
939}
940
Mark Brownb22ead22009-06-07 12:51:26 +0100941/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200942static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100943 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100944{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200945 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400946 struct snd_soc_dapm_widget *w;
947 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100948 unsigned int value = 0;
949 unsigned int mask = 0;
950 unsigned int cur_mask;
951
952 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
953 power_list)->reg;
954
955 list_for_each_entry(w, pending, power_list) {
956 cur_mask = 1 << w->shift;
957 BUG_ON(reg != w->reg);
958
959 if (w->invert)
960 power = !w->power;
961 else
962 power = w->power;
963
964 mask |= cur_mask;
965 if (power)
966 value |= cur_mask;
967
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200968 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100969 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
970 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100971
Mark Brown68f89ad2010-11-03 23:51:49 -0400972 /* Check for events */
973 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
974 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100975 }
976
Mark Brown81628102009-06-07 13:21:24 +0100977 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +0100978 /* Any widget will do, they should all be updating the
979 * same register.
980 */
981 w = list_first_entry(pending, struct snd_soc_dapm_widget,
982 power_list);
983
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200984 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100985 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200986 value, mask, reg, card->pop_time);
987 pop_wait(card->pop_time);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100988 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100989 }
990
991 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400992 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
993 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000994 }
Mark Brown42aa3412009-03-01 19:21:10 +0000995}
996
Mark Brownb22ead22009-06-07 12:51:26 +0100997/* Apply a DAPM power sequence.
998 *
999 * We walk over a pre-sorted list of widgets to apply power to. In
1000 * order to minimise the number of writes to the device required
1001 * multiple widgets will be updated in a single write where possible.
1002 * Currently anything that requires more than a single write is not
1003 * handled.
1004 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001005static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001006 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001007{
1008 struct snd_soc_dapm_widget *w, *n;
1009 LIST_HEAD(pending);
1010 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001011 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001012 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001013 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001014 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001015 int *sort;
1016
1017 if (power_up)
1018 sort = dapm_up_seq;
1019 else
1020 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001021
Mark Brownb22ead22009-06-07 12:51:26 +01001022 list_for_each_entry_safe(w, n, list, power_list) {
1023 ret = 0;
1024
1025 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001026 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001027 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001028 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001029 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001030
Mark Brown474b62d2011-01-18 16:14:44 +00001031 if (cur_dapm && cur_dapm->seq_notifier) {
1032 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1033 if (sort[i] == cur_sort)
1034 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001035 i,
1036 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001037 }
1038
Mark Brownb22ead22009-06-07 12:51:26 +01001039 INIT_LIST_HEAD(&pending);
1040 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001041 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001042 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001043 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001044 }
1045
Mark Brown163cac02009-06-07 10:12:52 +01001046 switch (w->id) {
1047 case snd_soc_dapm_pre:
1048 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001049 list_for_each_entry_safe_continue(w, n, list,
1050 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001051
Mark Brownb22ead22009-06-07 12:51:26 +01001052 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001053 ret = w->event(w,
1054 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001055 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001056 ret = w->event(w,
1057 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001058 break;
1059
1060 case snd_soc_dapm_post:
1061 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001062 list_for_each_entry_safe_continue(w, n, list,
1063 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001064
Mark Brownb22ead22009-06-07 12:51:26 +01001065 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001066 ret = w->event(w,
1067 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001068 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001069 ret = w->event(w,
1070 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001071 break;
1072
Mark Brown163cac02009-06-07 10:12:52 +01001073 default:
Mark Brown81628102009-06-07 13:21:24 +01001074 /* Queue it up for application */
1075 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001076 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001077 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001078 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001079 list_move(&w->power_list, &pending);
1080 break;
Mark Brown163cac02009-06-07 10:12:52 +01001081 }
Mark Brownb22ead22009-06-07 12:51:26 +01001082
1083 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001084 dev_err(w->dapm->dev,
1085 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001086 }
Mark Brownb22ead22009-06-07 12:51:26 +01001087
1088 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001089 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001090
1091 if (cur_dapm && cur_dapm->seq_notifier) {
1092 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1093 if (sort[i] == cur_sort)
1094 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001095 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001096 }
Mark Brown163cac02009-06-07 10:12:52 +01001097}
1098
Mark Brown97404f22010-12-14 16:13:57 +00001099static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1100{
1101 struct snd_soc_dapm_update *update = dapm->update;
1102 struct snd_soc_dapm_widget *w;
1103 int ret;
1104
1105 if (!update)
1106 return;
1107
1108 w = update->widget;
1109
1110 if (w->event &&
1111 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1112 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1113 if (ret != 0)
1114 pr_err("%s DAPM pre-event failed: %d\n",
1115 w->name, ret);
1116 }
1117
1118 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1119 update->val);
1120 if (ret < 0)
1121 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1122
1123 if (w->event &&
1124 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1125 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1126 if (ret != 0)
1127 pr_err("%s DAPM post-event failed: %d\n",
1128 w->name, ret);
1129 }
1130}
1131
Mark Brown9d0624a2011-02-18 11:49:43 -08001132/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1133 * they're changing state.
1134 */
1135static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1136{
1137 struct snd_soc_dapm_context *d = data;
1138 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001139
Mark Brown56fba412011-06-04 11:25:10 +01001140 /* If we're off and we're not supposed to be go into STANDBY */
1141 if (d->bias_level == SND_SOC_BIAS_OFF &&
1142 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001143 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1144 if (ret != 0)
1145 dev_err(d->dev,
1146 "Failed to turn on bias: %d\n", ret);
1147 }
1148
Mark Brown56fba412011-06-04 11:25:10 +01001149 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1150 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001151 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1152 if (ret != 0)
1153 dev_err(d->dev,
1154 "Failed to prepare bias: %d\n", ret);
1155 }
1156}
1157
1158/* Async callback run prior to DAPM sequences - brings to their final
1159 * state.
1160 */
1161static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1162{
1163 struct snd_soc_dapm_context *d = data;
1164 int ret;
1165
1166 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001167 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1168 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1169 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001170 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1171 if (ret != 0)
1172 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1173 ret);
1174 }
1175
1176 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001177 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1178 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001179 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1180 if (ret != 0)
1181 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1182 }
1183
1184 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001185 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1186 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001187 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1188 if (ret != 0)
1189 dev_err(d->dev, "Failed to apply active bias: %d\n",
1190 ret);
1191 }
1192}
Mark Brown97404f22010-12-14 16:13:57 +00001193
Mark Brown42aa3412009-03-01 19:21:10 +00001194/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001195 * Scan each dapm widget for complete audio path.
1196 * A complete path is a route that has valid endpoints i.e.:-
1197 *
1198 * o DAC to output pin.
1199 * o Input Pin to ADC.
1200 * o Input pin to Output pin (bypass, sidetone)
1201 * o DAC to ADC (loopback).
1202 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001203static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001204{
Mark Brown12ea2c72011-03-02 18:17:32 +00001205 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001206 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001207 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001208 LIST_HEAD(up_list);
1209 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001210 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001211 enum snd_soc_bias_level bias;
Mark Brown38357ab2009-06-06 19:03:23 +01001212 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001213
Mark Brown84e90932010-11-04 00:07:02 -04001214 trace_snd_soc_dapm_start(card);
1215
Mark Brown56fba412011-06-04 11:25:10 +01001216 list_for_each_entry(d, &card->dapm_list, list) {
1217 if (d->n_widgets || d->codec == NULL) {
1218 if (d->idle_bias_off)
1219 d->target_bias_level = SND_SOC_BIAS_OFF;
1220 else
1221 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1222 }
1223 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001224
Mark Brownde02d072011-09-20 21:43:24 +01001225 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1226
Mark Brown6d3ddc82009-05-16 17:47:29 +01001227 /* Check which widgets we need to power and store them in
1228 * lists indicating if they should be powered up or down.
1229 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001230 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001231 switch (w->id) {
1232 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001233 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001234 break;
1235 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001236 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001237 break;
1238
1239 default:
1240 if (!w->power_check)
1241 continue;
1242
Mark Brown99497882010-05-07 20:24:05 +01001243 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001244 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001245 else
1246 power = 1;
Mark Browndfcc9042011-06-04 11:34:43 +01001247
1248 if (power) {
1249 d = w->dapm;
1250
1251 /* Supplies and micbiases only bring
1252 * the context up to STANDBY as unless
1253 * something else is active and
1254 * passing audio they generally don't
1255 * require full power.
1256 */
1257 switch (w->id) {
1258 case snd_soc_dapm_supply:
1259 case snd_soc_dapm_micbias:
1260 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1261 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1262 break;
1263 default:
1264 d->target_bias_level = SND_SOC_BIAS_ON;
1265 break;
1266 }
1267 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001268
Mark Brown6d3ddc82009-05-16 17:47:29 +01001269 if (w->power == power)
1270 continue;
1271
Mark Brown84e90932010-11-04 00:07:02 -04001272 trace_snd_soc_dapm_widget_power(w, power);
1273
Mark Brown6d3ddc82009-05-16 17:47:29 +01001274 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001275 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001276 else
Mark Brown828a8422011-01-15 13:14:30 +00001277 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001278
1279 w->power = power;
1280 break;
1281 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001282 }
1283
Mark Brownb14b76a2009-08-17 11:55:38 +01001284 /* If there are no DAPM widgets then try to figure out power from the
1285 * event type.
1286 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001287 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001288 switch (event) {
1289 case SND_SOC_DAPM_STREAM_START:
1290 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001291 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a2009-08-17 11:55:38 +01001292 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001293 case SND_SOC_DAPM_STREAM_STOP:
Mark Brown56fba412011-06-04 11:25:10 +01001294 if (dapm->codec->active)
1295 dapm->target_bias_level = SND_SOC_BIAS_ON;
1296 else
1297 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001298 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001299 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001300 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001301 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001302 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001303 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001304 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001305 default:
1306 break;
1307 }
1308 }
1309
Mark Brown52ba67b2011-04-04 21:05:11 +09001310 /* Force all contexts in the card to the same bias state */
Mark Brown56fba412011-06-04 11:25:10 +01001311 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001312 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001313 if (d->target_bias_level > bias)
1314 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001315 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001316 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001317
Mark Brownde02d072011-09-20 21:43:24 +01001318 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001319
Mark Brown9d0624a2011-02-18 11:49:43 -08001320 /* Run all the bias changes in parallel */
1321 list_for_each_entry(d, &dapm->card->dapm_list, list)
1322 async_schedule_domain(dapm_pre_sequence_async, d,
1323 &async_domain);
1324 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001325
Mark Brown6d3ddc82009-05-16 17:47:29 +01001326 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001327 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001328
Mark Brown97404f22010-12-14 16:13:57 +00001329 dapm_widget_update(dapm);
1330
Mark Brown6d3ddc82009-05-16 17:47:29 +01001331 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001332 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001333
Mark Brown9d0624a2011-02-18 11:49:43 -08001334 /* Run all the bias changes in parallel */
1335 list_for_each_entry(d, &dapm->card->dapm_list, list)
1336 async_schedule_domain(dapm_post_sequence_async, d,
1337 &async_domain);
1338 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001339
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001340 pop_dbg(dapm->dev, card->pop_time,
1341 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001342 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001343
Mark Brown84e90932010-11-04 00:07:02 -04001344 trace_snd_soc_dapm_done(card);
1345
Mark Brown42aa3412009-03-01 19:21:10 +00001346 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001347}
1348
Mark Brown79fb9382009-08-21 16:38:13 +01001349#ifdef CONFIG_DEBUG_FS
1350static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1351{
1352 file->private_data = inode->i_private;
1353 return 0;
1354}
1355
1356static ssize_t dapm_widget_power_read_file(struct file *file,
1357 char __user *user_buf,
1358 size_t count, loff_t *ppos)
1359{
1360 struct snd_soc_dapm_widget *w = file->private_data;
1361 char *buf;
1362 int in, out;
1363 ssize_t ret;
1364 struct snd_soc_dapm_path *p = NULL;
1365
1366 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1367 if (!buf)
1368 return -ENOMEM;
1369
1370 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001371 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001372 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001373 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001374
Mark Brownd033c362009-12-04 15:25:56 +00001375 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001376 w->name, w->power ? "On" : "Off", in, out);
1377
Mark Brownd033c362009-12-04 15:25:56 +00001378 if (w->reg >= 0)
1379 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1380 " - R%d(0x%x) bit %d",
1381 w->reg, w->reg, w->shift);
1382
1383 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1384
Mark Brown3eef08b2009-09-14 16:49:00 +01001385 if (w->sname)
1386 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1387 w->sname,
1388 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001389
1390 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001391 if (p->connected && !p->connected(w, p->sink))
1392 continue;
1393
Mark Brown79fb9382009-08-21 16:38:13 +01001394 if (p->connect)
1395 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001396 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001397 p->name ? p->name : "static",
1398 p->source->name);
1399 }
1400 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001401 if (p->connected && !p->connected(w, p->sink))
1402 continue;
1403
Mark Brown79fb9382009-08-21 16:38:13 +01001404 if (p->connect)
1405 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001406 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001407 p->name ? p->name : "static",
1408 p->sink->name);
1409 }
1410
1411 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1412
1413 kfree(buf);
1414 return ret;
1415}
1416
1417static const struct file_operations dapm_widget_power_fops = {
1418 .open = dapm_widget_power_open_file,
1419 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001420 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001421};
1422
Mark Brownef49e4f2011-04-04 20:48:13 +09001423static int dapm_bias_open_file(struct inode *inode, struct file *file)
1424{
1425 file->private_data = inode->i_private;
1426 return 0;
1427}
1428
1429static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1430 size_t count, loff_t *ppos)
1431{
1432 struct snd_soc_dapm_context *dapm = file->private_data;
1433 char *level;
1434
1435 switch (dapm->bias_level) {
1436 case SND_SOC_BIAS_ON:
1437 level = "On\n";
1438 break;
1439 case SND_SOC_BIAS_PREPARE:
1440 level = "Prepare\n";
1441 break;
1442 case SND_SOC_BIAS_STANDBY:
1443 level = "Standby\n";
1444 break;
1445 case SND_SOC_BIAS_OFF:
1446 level = "Off\n";
1447 break;
1448 default:
1449 BUG();
1450 level = "Unknown\n";
1451 break;
1452 }
1453
1454 return simple_read_from_buffer(user_buf, count, ppos, level,
1455 strlen(level));
1456}
1457
1458static const struct file_operations dapm_bias_fops = {
1459 .open = dapm_bias_open_file,
1460 .read = dapm_bias_read_file,
1461 .llseek = default_llseek,
1462};
1463
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001464void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1465 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001466{
Mark Brown79fb9382009-08-21 16:38:13 +01001467 struct dentry *d;
1468
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001469 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1470
1471 if (!dapm->debugfs_dapm) {
1472 printk(KERN_WARNING
1473 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001474 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001475 }
Mark Brown79fb9382009-08-21 16:38:13 +01001476
Mark Brownef49e4f2011-04-04 20:48:13 +09001477 d = debugfs_create_file("bias_level", 0444,
1478 dapm->debugfs_dapm, dapm,
1479 &dapm_bias_fops);
1480 if (!d)
1481 dev_warn(dapm->dev,
1482 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001483}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001484
1485static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1486{
1487 struct snd_soc_dapm_context *dapm = w->dapm;
1488 struct dentry *d;
1489
1490 if (!dapm->debugfs_dapm || !w->name)
1491 return;
1492
1493 d = debugfs_create_file(w->name, 0444,
1494 dapm->debugfs_dapm, w,
1495 &dapm_widget_power_fops);
1496 if (!d)
1497 dev_warn(w->dapm->dev,
1498 "ASoC: Failed to create %s debugfs file\n",
1499 w->name);
1500}
1501
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001502static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1503{
1504 debugfs_remove_recursive(dapm->debugfs_dapm);
1505}
1506
Mark Brown79fb9382009-08-21 16:38:13 +01001507#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001508void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1509 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001510{
1511}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001512
1513static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1514{
1515}
1516
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001517static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1518{
1519}
1520
Mark Brown79fb9382009-08-21 16:38:13 +01001521#endif
1522
Richard Purdie2b97eab2006-10-06 18:32:18 +02001523/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001524static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001525 struct snd_kcontrol *kcontrol, int change,
1526 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001527{
1528 struct snd_soc_dapm_path *path;
1529 int found = 0;
1530
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001531 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001532 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001533 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001534 return -ENODEV;
1535
Mark Brown3a655772009-10-05 17:23:30 +01001536 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001537 return 0;
1538
1539 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001540 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001541 if (path->kcontrol != kcontrol)
1542 continue;
1543
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001544 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001545 continue;
1546
1547 found = 1;
1548 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001549 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001550 path->connect = 1; /* new connection */
1551 else
1552 path->connect = 0; /* old connection must be powered down */
1553 }
1554
Mark Brownb91b8fa2010-01-20 18:18:35 +00001555 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001556 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001557
1558 return 0;
1559}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001560
Milan plzik1b075e32008-01-10 14:39:46 +01001561/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001562static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001563 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001564{
1565 struct snd_soc_dapm_path *path;
1566 int found = 0;
1567
Milan plzik1b075e32008-01-10 14:39:46 +01001568 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001569 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001570 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001571 return -ENODEV;
1572
Richard Purdie2b97eab2006-10-06 18:32:18 +02001573 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001574 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001575 if (path->kcontrol != kcontrol)
1576 continue;
1577
1578 /* found, now check type */
1579 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001580 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001581 }
1582
Mark Brownb91b8fa2010-01-20 18:18:35 +00001583 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001584 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001585
1586 return 0;
1587}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001588
1589/* show dapm widget status in sys fs */
1590static ssize_t dapm_widget_show(struct device *dev,
1591 struct device_attribute *attr, char *buf)
1592{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001593 struct snd_soc_pcm_runtime *rtd =
1594 container_of(dev, struct snd_soc_pcm_runtime, dev);
1595 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001596 struct snd_soc_dapm_widget *w;
1597 int count = 0;
1598 char *state = "not set";
1599
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001600 list_for_each_entry(w, &codec->card->widgets, list) {
1601 if (w->dapm != &codec->dapm)
1602 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001603
1604 /* only display widgets that burnm power */
1605 switch (w->id) {
1606 case snd_soc_dapm_hp:
1607 case snd_soc_dapm_mic:
1608 case snd_soc_dapm_spk:
1609 case snd_soc_dapm_line:
1610 case snd_soc_dapm_micbias:
1611 case snd_soc_dapm_dac:
1612 case snd_soc_dapm_adc:
1613 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001614 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001615 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001616 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001617 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001618 if (w->name)
1619 count += sprintf(buf + count, "%s: %s\n",
1620 w->name, w->power ? "On":"Off");
1621 break;
1622 default:
1623 break;
1624 }
1625 }
1626
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001627 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001628 case SND_SOC_BIAS_ON:
1629 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001630 break;
Mark Brown0be98982008-05-19 12:31:28 +02001631 case SND_SOC_BIAS_PREPARE:
1632 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001633 break;
Mark Brown0be98982008-05-19 12:31:28 +02001634 case SND_SOC_BIAS_STANDBY:
1635 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001636 break;
Mark Brown0be98982008-05-19 12:31:28 +02001637 case SND_SOC_BIAS_OFF:
1638 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001639 break;
1640 }
1641 count += sprintf(buf + count, "PM State: %s\n", state);
1642
1643 return count;
1644}
1645
1646static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1647
1648int snd_soc_dapm_sys_add(struct device *dev)
1649{
Troy Kisky12ef1932008-10-13 17:42:14 -07001650 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001651}
1652
1653static void snd_soc_dapm_sys_remove(struct device *dev)
1654{
Mark Brownaef90842009-05-16 17:53:16 +01001655 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001656}
1657
1658/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001659static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001660{
1661 struct snd_soc_dapm_widget *w, *next_w;
1662 struct snd_soc_dapm_path *p, *next_p;
1663
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001664 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1665 if (w->dapm != dapm)
1666 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001667 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001668 /*
1669 * remove source and sink paths associated to this widget.
1670 * While removing the path, remove reference to it from both
1671 * source and sink widgets so that path is removed only once.
1672 */
1673 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1674 list_del(&p->list_sink);
1675 list_del(&p->list_source);
1676 list_del(&p->list);
1677 kfree(p->long_name);
1678 kfree(p);
1679 }
1680 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1681 list_del(&p->list_sink);
1682 list_del(&p->list_source);
1683 list_del(&p->list);
1684 kfree(p->long_name);
1685 kfree(p);
1686 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001687 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001688 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001689 kfree(w);
1690 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001691}
1692
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001693static struct snd_soc_dapm_widget *dapm_find_widget(
1694 struct snd_soc_dapm_context *dapm, const char *pin,
1695 bool search_other_contexts)
1696{
1697 struct snd_soc_dapm_widget *w;
1698 struct snd_soc_dapm_widget *fallback = NULL;
1699
1700 list_for_each_entry(w, &dapm->card->widgets, list) {
1701 if (!strcmp(w->name, pin)) {
1702 if (w->dapm == dapm)
1703 return w;
1704 else
1705 fallback = w;
1706 }
1707 }
1708
1709 if (search_other_contexts)
1710 return fallback;
1711
1712 return NULL;
1713}
1714
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001715static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001716 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001717{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001718 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001719
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001720 if (!w) {
1721 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1722 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001723 }
1724
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001725 w->connected = status;
1726 if (status == 0)
1727 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001728
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001729 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001730}
1731
Richard Purdie2b97eab2006-10-06 18:32:18 +02001732/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001733 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001734 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001735 *
1736 * Walks all dapm audio paths and powers widgets according to their
1737 * stream or path usage.
1738 *
1739 * Returns 0 for success.
1740 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001741int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001742{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001743 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001744}
Liam Girdwooda5302182008-07-07 13:35:17 +01001745EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001746
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001747static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001748 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001749{
1750 struct snd_soc_dapm_path *path;
1751 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001752 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001753 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001754 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001755 const char *source;
1756 char prefixed_sink[80];
1757 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001758 int ret = 0;
1759
Mark Brown88e8b9a2011-03-02 18:18:24 +00001760 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001761 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1762 dapm->codec->name_prefix, route->sink);
1763 sink = prefixed_sink;
1764 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1765 dapm->codec->name_prefix, route->source);
1766 source = prefixed_source;
1767 } else {
1768 sink = route->sink;
1769 source = route->source;
1770 }
1771
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001772 /*
1773 * find src and dest widgets over all widgets but favor a widget from
1774 * current DAPM context
1775 */
1776 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001777 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001778 wtsink = w;
1779 if (w->dapm == dapm)
1780 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001781 continue;
1782 }
1783 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001784 wtsource = w;
1785 if (w->dapm == dapm)
1786 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001787 }
1788 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001789 /* use widget from another DAPM context if not found from this */
1790 if (!wsink)
1791 wsink = wtsink;
1792 if (!wsource)
1793 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001794
1795 if (wsource == NULL || wsink == NULL)
1796 return -ENODEV;
1797
1798 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1799 if (!path)
1800 return -ENOMEM;
1801
1802 path->source = wsource;
1803 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001804 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001805 INIT_LIST_HEAD(&path->list);
1806 INIT_LIST_HEAD(&path->list_source);
1807 INIT_LIST_HEAD(&path->list_sink);
1808
1809 /* check for external widgets */
1810 if (wsink->id == snd_soc_dapm_input) {
1811 if (wsource->id == snd_soc_dapm_micbias ||
1812 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001813 wsource->id == snd_soc_dapm_line ||
1814 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001815 wsink->ext = 1;
1816 }
1817 if (wsource->id == snd_soc_dapm_output) {
1818 if (wsink->id == snd_soc_dapm_spk ||
1819 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001820 wsink->id == snd_soc_dapm_line ||
1821 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001822 wsource->ext = 1;
1823 }
1824
1825 /* connect static paths */
1826 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001827 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001828 list_add(&path->list_sink, &wsink->sources);
1829 list_add(&path->list_source, &wsource->sinks);
1830 path->connect = 1;
1831 return 0;
1832 }
1833
1834 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001835 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001836 case snd_soc_dapm_adc:
1837 case snd_soc_dapm_dac:
1838 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001839 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001840 case snd_soc_dapm_input:
1841 case snd_soc_dapm_output:
1842 case snd_soc_dapm_micbias:
1843 case snd_soc_dapm_vmid:
1844 case snd_soc_dapm_pre:
1845 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001846 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001847 case snd_soc_dapm_aif_in:
1848 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001849 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001850 list_add(&path->list_sink, &wsink->sources);
1851 list_add(&path->list_source, &wsource->sinks);
1852 path->connect = 1;
1853 return 0;
1854 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001855 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001856 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001857 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06001858 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001859 if (ret != 0)
1860 goto err;
1861 break;
1862 case snd_soc_dapm_switch:
1863 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001864 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001865 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001866 if (ret != 0)
1867 goto err;
1868 break;
1869 case snd_soc_dapm_hp:
1870 case snd_soc_dapm_mic:
1871 case snd_soc_dapm_line:
1872 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001873 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001874 list_add(&path->list_sink, &wsink->sources);
1875 list_add(&path->list_source, &wsource->sinks);
1876 path->connect = 0;
1877 return 0;
1878 }
1879 return 0;
1880
1881err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001882 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1883 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001884 kfree(path);
1885 return ret;
1886}
Mark Brown105f1c22008-05-13 14:52:19 +02001887
1888/**
Mark Brown105f1c22008-05-13 14:52:19 +02001889 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001890 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001891 * @route: audio routes
1892 * @num: number of routes
1893 *
1894 * Connects 2 dapm widgets together via a named audio path. The sink is
1895 * the widget receiving the audio signal, whilst the source is the sender
1896 * of the audio signal.
1897 *
1898 * Returns 0 for success else error. On error all resources can be freed
1899 * with a call to snd_soc_card_free().
1900 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001901int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001902 const struct snd_soc_dapm_route *route, int num)
1903{
1904 int i, ret;
1905
1906 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001907 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001908 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001909 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1910 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001911 return ret;
1912 }
1913 route++;
1914 }
1915
1916 return 0;
1917}
1918EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1919
Mark Brownbf3a9e12011-06-13 16:42:29 +01001920static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
1921 const struct snd_soc_dapm_route *route)
1922{
1923 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
1924 route->source,
1925 true);
1926 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
1927 route->sink,
1928 true);
1929 struct snd_soc_dapm_path *path;
1930 int count = 0;
1931
1932 if (!source) {
1933 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
1934 route->source);
1935 return -ENODEV;
1936 }
1937
1938 if (!sink) {
1939 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
1940 route->sink);
1941 return -ENODEV;
1942 }
1943
1944 if (route->control || route->connected)
1945 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
1946 route->source, route->sink);
1947
1948 list_for_each_entry(path, &source->sinks, list_source) {
1949 if (path->sink == sink) {
1950 path->weak = 1;
1951 count++;
1952 }
1953 }
1954
1955 if (count == 0)
1956 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
1957 route->source, route->sink);
1958 if (count > 1)
1959 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
1960 count, route->source, route->sink);
1961
1962 return 0;
1963}
1964
1965/**
1966 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
1967 * @dapm: DAPM context
1968 * @route: audio routes
1969 * @num: number of routes
1970 *
1971 * Mark existing routes matching those specified in the passed array
1972 * as being weak, meaning that they are ignored for the purpose of
1973 * power decisions. The main intended use case is for sidetone paths
1974 * which couple audio between other independent paths if they are both
1975 * active in order to make the combination work better at the user
1976 * level but which aren't intended to be "used".
1977 *
1978 * Note that CODEC drivers should not use this as sidetone type paths
1979 * can frequently also be used as bypass paths.
1980 */
1981int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
1982 const struct snd_soc_dapm_route *route, int num)
1983{
1984 int i, err;
1985 int ret = 0;
1986
1987 for (i = 0; i < num; i++) {
1988 err = snd_soc_dapm_weak_route(dapm, route);
1989 if (err)
1990 ret = err;
1991 route++;
1992 }
1993
1994 return ret;
1995}
1996EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
1997
Mark Brown105f1c22008-05-13 14:52:19 +02001998/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001999 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002000 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002001 *
2002 * Checks the codec for any new dapm widgets and creates them if found.
2003 *
2004 * Returns 0 for success.
2005 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002006int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002007{
2008 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002009 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002010
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002011 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002012 {
2013 if (w->new)
2014 continue;
2015
Stephen Warrenfad59882011-04-28 17:37:59 -06002016 if (w->num_kcontrols) {
2017 w->kcontrols = kzalloc(w->num_kcontrols *
2018 sizeof(struct snd_kcontrol *),
2019 GFP_KERNEL);
2020 if (!w->kcontrols)
2021 return -ENOMEM;
2022 }
2023
Richard Purdie2b97eab2006-10-06 18:32:18 +02002024 switch(w->id) {
2025 case snd_soc_dapm_switch:
2026 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002027 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01002028 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002029 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002030 break;
2031 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002032 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002033 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01002034 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002035 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002036 break;
2037 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01002038 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01002039 w->power_check = dapm_adc_check_power;
2040 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002041 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01002042 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01002043 w->power_check = dapm_dac_check_power;
2044 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002045 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002046 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01002047 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002048 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002049 break;
2050 case snd_soc_dapm_input:
2051 case snd_soc_dapm_output:
2052 case snd_soc_dapm_micbias:
2053 case snd_soc_dapm_spk:
2054 case snd_soc_dapm_hp:
2055 case snd_soc_dapm_mic:
2056 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01002057 w->power_check = dapm_generic_check_power;
2058 break;
Mark Brown246d0a12009-04-22 18:24:55 +01002059 case snd_soc_dapm_supply:
2060 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002061 case snd_soc_dapm_vmid:
2062 case snd_soc_dapm_pre:
2063 case snd_soc_dapm_post:
2064 break;
2065 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002066
2067 /* Read the initial power state from the device */
2068 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002069 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002070 val &= 1 << w->shift;
2071 if (w->invert)
2072 val = !val;
2073
2074 if (val)
2075 w->power = 1;
2076 }
2077
Richard Purdie2b97eab2006-10-06 18:32:18 +02002078 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002079
2080 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002081 }
2082
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002083 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002084 return 0;
2085}
2086EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2087
2088/**
2089 * snd_soc_dapm_get_volsw - dapm mixer get callback
2090 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002091 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002092 *
2093 * Callback to get the value of a dapm mixer control.
2094 *
2095 * Returns 0 for success.
2096 */
2097int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2098 struct snd_ctl_elem_value *ucontrol)
2099{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002100 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2101 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002102 struct soc_mixer_control *mc =
2103 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002104 unsigned int reg = mc->reg;
2105 unsigned int shift = mc->shift;
2106 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002107 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002108 unsigned int invert = mc->invert;
2109 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002110
Richard Purdie2b97eab2006-10-06 18:32:18 +02002111 ucontrol->value.integer.value[0] =
2112 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2113 if (shift != rshift)
2114 ucontrol->value.integer.value[1] =
2115 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2116 if (invert) {
2117 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002118 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002119 if (shift != rshift)
2120 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002121 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002122 }
2123
2124 return 0;
2125}
2126EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2127
2128/**
2129 * snd_soc_dapm_put_volsw - dapm mixer set callback
2130 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002131 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002132 *
2133 * Callback to set the value of a dapm mixer control.
2134 *
2135 * Returns 0 for success.
2136 */
2137int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2138 struct snd_ctl_elem_value *ucontrol)
2139{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002140 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2141 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2142 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002143 struct soc_mixer_control *mc =
2144 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002145 unsigned int reg = mc->reg;
2146 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002147 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002148 unsigned int mask = (1 << fls(max)) - 1;
2149 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002150 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002151 int connect, change;
2152 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002153 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002154
2155 val = (ucontrol->value.integer.value[0] & mask);
2156
2157 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002158 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002159 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002160 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002161
Stephen Warrenfafd2172011-04-28 17:38:00 -06002162 if (val)
2163 /* new connection */
2164 connect = invert ? 0 : 1;
2165 else
2166 /* old connection must be powered down */
2167 connect = invert ? 1 : 0;
2168
2169 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002170
Stephen Warrene9cf7042011-01-27 14:54:05 -07002171 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002172 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002173 for (wi = 0; wi < wlist->num_widgets; wi++) {
2174 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002175
Stephen Warrenfafd2172011-04-28 17:38:00 -06002176 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002177
Stephen Warrenfafd2172011-04-28 17:38:00 -06002178 update.kcontrol = kcontrol;
2179 update.widget = widget;
2180 update.reg = reg;
2181 update.mask = mask;
2182 update.val = val;
2183 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002184
Stephen Warrenfafd2172011-04-28 17:38:00 -06002185 dapm_mixer_update_power(widget, kcontrol, connect);
2186
2187 widget->dapm->update = NULL;
2188 }
Mark Brown283375c2009-12-07 18:09:03 +00002189 }
2190
Stephen Warrenfafd2172011-04-28 17:38:00 -06002191 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002192 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002193}
2194EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2195
2196/**
2197 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2198 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002199 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002200 *
2201 * Callback to get the value of a dapm enumerated double mixer control.
2202 *
2203 * Returns 0 for success.
2204 */
2205int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2206 struct snd_ctl_elem_value *ucontrol)
2207{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002208 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2209 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002210 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002211 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002212
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002213 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002214 ;
2215 val = snd_soc_read(widget->codec, e->reg);
2216 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2217 if (e->shift_l != e->shift_r)
2218 ucontrol->value.enumerated.item[1] =
2219 (val >> e->shift_r) & (bitmask - 1);
2220
2221 return 0;
2222}
2223EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2224
2225/**
2226 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2227 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002228 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002229 *
2230 * Callback to set the value of a dapm enumerated double mixer control.
2231 *
2232 * Returns 0 for success.
2233 */
2234int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2235 struct snd_ctl_elem_value *ucontrol)
2236{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002237 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2238 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2239 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002240 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002241 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002242 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002243 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002244 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002245
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002246 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002247 ;
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002248 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002249 return -EINVAL;
2250 mux = ucontrol->value.enumerated.item[0];
2251 val = mux << e->shift_l;
2252 mask = (bitmask - 1) << e->shift_l;
2253 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002254 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002255 return -EINVAL;
2256 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2257 mask |= (bitmask - 1) << e->shift_r;
2258 }
2259
Stephen Warrenfafd2172011-04-28 17:38:00 -06002260 mutex_lock(&codec->mutex);
2261
Mark Brown3a655772009-10-05 17:23:30 +01002262 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002263 if (change) {
2264 for (wi = 0; wi < wlist->num_widgets; wi++) {
2265 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002266
Stephen Warrenfafd2172011-04-28 17:38:00 -06002267 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002268
Stephen Warrenfafd2172011-04-28 17:38:00 -06002269 update.kcontrol = kcontrol;
2270 update.widget = widget;
2271 update.reg = e->reg;
2272 update.mask = mask;
2273 update.val = val;
2274 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002275
Stephen Warrenfafd2172011-04-28 17:38:00 -06002276 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002277
Stephen Warrenfafd2172011-04-28 17:38:00 -06002278 widget->dapm->update = NULL;
2279 }
2280 }
2281
2282 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002283 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002284}
2285EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2286
2287/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002288 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2289 * @kcontrol: mixer control
2290 * @ucontrol: control element information
2291 *
2292 * Returns 0 for success.
2293 */
2294int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2295 struct snd_ctl_elem_value *ucontrol)
2296{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002297 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2298 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002299
2300 ucontrol->value.enumerated.item[0] = widget->value;
2301
2302 return 0;
2303}
2304EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2305
2306/**
2307 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2308 * @kcontrol: mixer control
2309 * @ucontrol: control element information
2310 *
2311 * Returns 0 for success.
2312 */
2313int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2314 struct snd_ctl_elem_value *ucontrol)
2315{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002316 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2317 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2318 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002319 struct soc_enum *e =
2320 (struct soc_enum *)kcontrol->private_value;
2321 int change;
2322 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002323 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002324
2325 if (ucontrol->value.enumerated.item[0] >= e->max)
2326 return -EINVAL;
2327
Stephen Warrenfafd2172011-04-28 17:38:00 -06002328 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002329
2330 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002331 if (change) {
2332 for (wi = 0; wi < wlist->num_widgets; wi++) {
2333 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002334
Stephen Warrenfafd2172011-04-28 17:38:00 -06002335 widget->value = ucontrol->value.enumerated.item[0];
2336
2337 dapm_mux_update_power(widget, kcontrol, change,
2338 widget->value, e);
2339 }
2340 }
2341
2342 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002343 return ret;
2344}
2345EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2346
2347/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002348 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2349 * callback
2350 * @kcontrol: mixer control
2351 * @ucontrol: control element information
2352 *
2353 * Callback to get the value of a dapm semi enumerated double mixer control.
2354 *
2355 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2356 * used for handling bitfield coded enumeration for example.
2357 *
2358 * Returns 0 for success.
2359 */
2360int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2361 struct snd_ctl_elem_value *ucontrol)
2362{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002363 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2364 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002365 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002366 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002367
2368 reg_val = snd_soc_read(widget->codec, e->reg);
2369 val = (reg_val >> e->shift_l) & e->mask;
2370 for (mux = 0; mux < e->max; mux++) {
2371 if (val == e->values[mux])
2372 break;
2373 }
2374 ucontrol->value.enumerated.item[0] = mux;
2375 if (e->shift_l != e->shift_r) {
2376 val = (reg_val >> e->shift_r) & e->mask;
2377 for (mux = 0; mux < e->max; mux++) {
2378 if (val == e->values[mux])
2379 break;
2380 }
2381 ucontrol->value.enumerated.item[1] = mux;
2382 }
2383
2384 return 0;
2385}
2386EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2387
2388/**
2389 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2390 * callback
2391 * @kcontrol: mixer control
2392 * @ucontrol: control element information
2393 *
2394 * Callback to set the value of a dapm semi enumerated double mixer control.
2395 *
2396 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2397 * used for handling bitfield coded enumeration for example.
2398 *
2399 * Returns 0 for success.
2400 */
2401int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_value *ucontrol)
2403{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002404 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2405 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2406 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002407 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002408 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002409 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002410 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002411 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002412
2413 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2414 return -EINVAL;
2415 mux = ucontrol->value.enumerated.item[0];
2416 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2417 mask = e->mask << e->shift_l;
2418 if (e->shift_l != e->shift_r) {
2419 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2420 return -EINVAL;
2421 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2422 mask |= e->mask << e->shift_r;
2423 }
2424
Stephen Warrenfafd2172011-04-28 17:38:00 -06002425 mutex_lock(&codec->mutex);
2426
Mark Brown3a655772009-10-05 17:23:30 +01002427 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002428 if (change) {
2429 for (wi = 0; wi < wlist->num_widgets; wi++) {
2430 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002431
Stephen Warrenfafd2172011-04-28 17:38:00 -06002432 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002433
Stephen Warrenfafd2172011-04-28 17:38:00 -06002434 update.kcontrol = kcontrol;
2435 update.widget = widget;
2436 update.reg = e->reg;
2437 update.mask = mask;
2438 update.val = val;
2439 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002440
Stephen Warrenfafd2172011-04-28 17:38:00 -06002441 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002442
Stephen Warrenfafd2172011-04-28 17:38:00 -06002443 widget->dapm->update = NULL;
2444 }
2445 }
2446
2447 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002448 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002449}
2450EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2451
2452/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002453 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2454 *
2455 * @kcontrol: mixer control
2456 * @uinfo: control element information
2457 *
2458 * Callback to provide information about a pin switch control.
2459 */
2460int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2461 struct snd_ctl_elem_info *uinfo)
2462{
2463 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2464 uinfo->count = 1;
2465 uinfo->value.integer.min = 0;
2466 uinfo->value.integer.max = 1;
2467
2468 return 0;
2469}
2470EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2471
2472/**
2473 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2474 *
2475 * @kcontrol: mixer control
2476 * @ucontrol: Value
2477 */
2478int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2479 struct snd_ctl_elem_value *ucontrol)
2480{
2481 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2482 const char *pin = (const char *)kcontrol->private_value;
2483
2484 mutex_lock(&codec->mutex);
2485
2486 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002487 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002488
2489 mutex_unlock(&codec->mutex);
2490
2491 return 0;
2492}
2493EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2494
2495/**
2496 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2497 *
2498 * @kcontrol: mixer control
2499 * @ucontrol: Value
2500 */
2501int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2502 struct snd_ctl_elem_value *ucontrol)
2503{
2504 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2505 const char *pin = (const char *)kcontrol->private_value;
2506
2507 mutex_lock(&codec->mutex);
2508
2509 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002510 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002511 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002512 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002513
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002514 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002515
2516 mutex_unlock(&codec->mutex);
2517
2518 return 0;
2519}
2520EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2521
2522/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002523 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002524 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002525 * @widget: widget template
2526 *
2527 * Creates a new dapm control based upon the template.
2528 *
2529 * Returns 0 for success else error.
2530 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002531int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002532 const struct snd_soc_dapm_widget *widget)
2533{
2534 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002535 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002536
2537 if ((w = dapm_cnew_widget(widget)) == NULL)
2538 return -ENOMEM;
2539
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002540 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002541 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002542 name_len += 1 + strlen(dapm->codec->name_prefix);
2543 w->name = kmalloc(name_len, GFP_KERNEL);
2544 if (w->name == NULL) {
2545 kfree(w);
2546 return -ENOMEM;
2547 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002548 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002549 snprintf(w->name, name_len, "%s %s",
2550 dapm->codec->name_prefix, widget->name);
2551 else
2552 snprintf(w->name, name_len, "%s", widget->name);
2553
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002554 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002555 w->dapm = dapm;
2556 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002557 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002558 INIT_LIST_HEAD(&w->sources);
2559 INIT_LIST_HEAD(&w->sinks);
2560 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002561 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002562
2563 /* machine layer set ups unconnected pins and insertions */
2564 w->connected = 1;
2565 return 0;
2566}
2567EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2568
2569/**
Mark Brown4ba13272008-05-13 14:51:19 +02002570 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002571 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002572 * @widget: widget array
2573 * @num: number of widgets
2574 *
2575 * Creates new DAPM controls based upon the templates.
2576 *
2577 * Returns 0 for success else error.
2578 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002579int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002580 const struct snd_soc_dapm_widget *widget,
2581 int num)
2582{
2583 int i, ret;
2584
2585 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002586 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002587 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002588 dev_err(dapm->dev,
2589 "ASoC: Failed to create DAPM control %s: %d\n",
2590 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002591 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002592 }
Mark Brown4ba13272008-05-13 14:51:19 +02002593 widget++;
2594 }
2595 return 0;
2596}
2597EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2598
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002599static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002600 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002601{
2602 struct snd_soc_dapm_widget *w;
2603
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002604 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002605 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002606 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002607 continue;
Liam Girdwoodee47b362011-07-25 11:15:50 +01002608 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002609 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002610 if (strstr(w->sname, stream)) {
2611 switch(event) {
2612 case SND_SOC_DAPM_STREAM_START:
2613 w->active = 1;
2614 break;
2615 case SND_SOC_DAPM_STREAM_STOP:
2616 w->active = 0;
2617 break;
2618 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002619 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002620 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002621 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2622 break;
2623 }
2624 }
2625 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002626
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002627 dapm_power_widgets(dapm, event);
Liam Girdwood64a648c222011-07-25 11:15:15 +01002628
2629 /* do we need to notify any clients that DAPM stream is complete */
2630 if (dapm->stream_event)
2631 dapm->stream_event(dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002632}
2633
2634/**
2635 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2636 * @rtd: PCM runtime data
2637 * @stream: stream name
2638 * @event: stream event
2639 *
2640 * Sends a stream event to the dapm core. The core then makes any
2641 * necessary widget power changes.
2642 *
2643 * Returns 0 for success else error.
2644 */
2645int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2646 const char *stream, int event)
2647{
2648 struct snd_soc_codec *codec = rtd->codec;
2649
2650 if (stream == NULL)
2651 return 0;
2652
2653 mutex_lock(&codec->mutex);
2654 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002655 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002656 return 0;
2657}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002658
2659/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002660 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002661 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002662 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002663 *
Mark Brown74b8f952009-06-06 11:26:15 +01002664 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002665 * a valid audio route and active audio stream.
2666 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2667 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002668 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002669int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002670{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002671 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002672}
Liam Girdwooda5302182008-07-07 13:35:17 +01002673EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002674
2675/**
Mark Brownda341832010-03-15 19:23:37 +00002676 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002677 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002678 * @pin: pin name
2679 *
2680 * Enables input/output pin regardless of any other state. This is
2681 * intended for use with microphone bias supplies used in microphone
2682 * jack detection.
2683 *
2684 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2685 * do any widget power switching.
2686 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002687int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2688 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002689{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002690 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002691
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002692 if (!w) {
2693 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2694 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002695 }
2696
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002697 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2698 w->connected = 1;
2699 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002700
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002701 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002702}
2703EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2704
2705/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002706 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002707 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002708 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002709 *
Mark Brown74b8f952009-06-06 11:26:15 +01002710 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002711 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2712 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002713 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002714int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2715 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002716{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002717 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002718}
2719EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2720
2721/**
Mark Brown5817b522008-09-24 11:23:11 +01002722 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002723 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002724 * @pin: pin name
2725 *
2726 * Marks the specified pin as being not connected, disabling it along
2727 * any parent or child widgets. At present this is identical to
2728 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2729 * additional things such as disabling controls which only affect
2730 * paths through the pin.
2731 *
2732 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2733 * do any widget power switching.
2734 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002735int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002736{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002737 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002738}
2739EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2740
2741/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002742 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002743 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002744 * @pin: audio signal pin endpoint (or start point)
2745 *
2746 * Get audio pin status - connected or disconnected.
2747 *
2748 * Returns 1 for connected otherwise 0.
2749 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002750int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2751 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002752{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002753 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002754
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002755 if (w)
2756 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002757
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002758 return 0;
2759}
Liam Girdwooda5302182008-07-07 13:35:17 +01002760EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002761
2762/**
Mark Brown1547aba2010-05-07 21:11:40 +01002763 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002764 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002765 * @pin: audio signal pin endpoint (or start point)
2766 *
2767 * Mark the given endpoint or pin as ignoring suspend. When the
2768 * system is disabled a path between two endpoints flagged as ignoring
2769 * suspend will not be disabled. The path must already be enabled via
2770 * normal means at suspend time, it will not be turned on if it was not
2771 * already enabled.
2772 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002773int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2774 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002775{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002776 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002777
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002778 if (!w) {
2779 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2780 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002781 }
2782
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002783 w->ignore_suspend = 1;
2784
2785 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002786}
2787EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2788
2789/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002790 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03002791 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002792 *
2793 * Free all dapm widgets and resources.
2794 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002795void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002796{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002797 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002798 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002799 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002800 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002801}
2802EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2803
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002804static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002805{
Mark Brown51737472009-06-22 13:16:51 +01002806 struct snd_soc_dapm_widget *w;
2807 LIST_HEAD(down_list);
2808 int powerdown = 0;
2809
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002810 list_for_each_entry(w, &dapm->card->widgets, list) {
2811 if (w->dapm != dapm)
2812 continue;
Mark Brown51737472009-06-22 13:16:51 +01002813 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002814 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002815 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002816 powerdown = 1;
2817 }
2818 }
2819
2820 /* If there were no widgets to power down we're already in
2821 * standby.
2822 */
2823 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002824 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002825 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002826 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002827 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002828}
Mark Brown51737472009-06-22 13:16:51 +01002829
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002830/*
2831 * snd_soc_dapm_shutdown - callback for system shutdown
2832 */
2833void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2834{
2835 struct snd_soc_codec *codec;
2836
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002837 list_for_each_entry(codec, &card->codec_dev_list, list) {
2838 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002839 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002840 }
Mark Brown51737472009-06-22 13:16:51 +01002841}
2842
Richard Purdie2b97eab2006-10-06 18:32:18 +02002843/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002844MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002845MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2846MODULE_LICENSE("GPL");