blob: b462639f7c003cbc89e4c3a852d5e1cede4393ff [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.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07005 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01006 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02007 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070010 * Free Software Foundation; only version 2 of the License.
Richard Purdie2b97eab2006-10-06 18:32:18 +020011 *
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
Liam Girdwood13e13a32011-01-31 21:30:52 +000032#undef DEBUG
Liam Girdwood7987a112011-01-31 19:52:42 +000033
Richard Purdie2b97eab2006-10-06 18:32:18 +020034#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080037#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020038#include <linux/delay.h>
39#include <linux/pm.h>
40#include <linux/bitops.h>
41#include <linux/platform_device.h>
42#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020043#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020045#include <sound/core.h>
46#include <sound/pcm.h>
47#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020048#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020049#include <sound/initval.h>
50
Mark Brown84e90932010-11-04 00:07:02 -040051#include <trace/events/asoc.h>
52
Liam Girdwoodab1058a2011-01-31 20:33:07 +000053#define PATH_MAX_HOPS 16
54
Liam Girdwoode002c982011-02-08 11:45:20 +000055int soc_dsp_runtime_update(struct snd_soc_dapm_widget *);
56
Richard Purdie2b97eab2006-10-06 18:32:18 +020057/* dapm power sequences - make this per codec in the future */
58static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010059 [snd_soc_dapm_pre] = 0,
60 [snd_soc_dapm_supply] = 1,
61 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010062 [snd_soc_dapm_aif_in] = 3,
63 [snd_soc_dapm_aif_out] = 3,
64 [snd_soc_dapm_mic] = 4,
65 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000066 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010067 [snd_soc_dapm_value_mux] = 5,
68 [snd_soc_dapm_dac] = 6,
69 [snd_soc_dapm_mixer] = 7,
70 [snd_soc_dapm_mixer_named_ctl] = 7,
71 [snd_soc_dapm_pga] = 8,
72 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060073 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010074 [snd_soc_dapm_hp] = 10,
75 [snd_soc_dapm_spk] = 10,
76 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020077};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000078
Richard Purdie2b97eab2006-10-06 18:32:18 +020079static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010080 [snd_soc_dapm_pre] = 0,
81 [snd_soc_dapm_adc] = 1,
82 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010083 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060084 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010085 [snd_soc_dapm_pga] = 4,
86 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010087 [snd_soc_dapm_mixer] = 5,
88 [snd_soc_dapm_dac] = 6,
89 [snd_soc_dapm_mic] = 7,
90 [snd_soc_dapm_micbias] = 8,
91 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000092 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010093 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010094 [snd_soc_dapm_aif_in] = 10,
95 [snd_soc_dapm_aif_out] = 10,
96 [snd_soc_dapm_supply] = 11,
97 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020098};
99
Troy Kisky12ef1932008-10-13 17:42:14 -0700100static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100101{
102 if (pop_time)
103 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
104}
105
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200106static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100107{
108 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200109 char *buf;
110
111 if (!pop_time)
112 return;
113
114 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
115 if (buf == NULL)
116 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100117
118 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200119 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100120 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100121 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200122
123 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100124}
125
Richard Purdie2b97eab2006-10-06 18:32:18 +0200126/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100127static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200128 const struct snd_soc_dapm_widget *_widget)
129{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100130 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200131}
132
Liam Girdwood7987a112011-01-31 19:52:42 +0000133static inline struct snd_card *dapm_get_card(struct snd_soc_dapm_context *dapm)
134{
135 if (dapm->codec)
136 return dapm->codec->card->snd_card;
137 else if (dapm->platform)
138 return dapm->platform->card->snd_card;
139 else
140 BUG();
141}
142
143static inline struct snd_soc_card *dapm_get_soc_card(
144 struct snd_soc_dapm_context *dapm)
145{
146 if (dapm->codec)
147 return dapm->codec->card;
148 else if (dapm->platform)
149 return dapm->platform->card;
150 else
151 BUG();
152}
153
Liam Girdwood35ca6602011-01-28 17:45:35 +0000154static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
155{
156 if (w->codec)
157 return snd_soc_read(w->codec, reg);
Liam Girdwood7987a112011-01-31 19:52:42 +0000158 else if (w->platform)
159 return snd_soc_platform_read(w->platform, reg);
Liam Girdwood35ca6602011-01-28 17:45:35 +0000160 return 0;
161}
162
163static int soc_widget_write(struct snd_soc_dapm_widget *w,int reg, int val)
164{
165 if (w->codec)
166 return snd_soc_write(w->codec, reg, val);
Liam Girdwood7987a112011-01-31 19:52:42 +0000167 else if (w->platform)
168 return snd_soc_platform_write(w->platform, reg, val);
Liam Girdwood35ca6602011-01-28 17:45:35 +0000169 return 0;
170}
171
172int soc_widget_update_bits(struct snd_soc_dapm_widget *w, unsigned short reg,
173 unsigned int mask, unsigned int value)
174{
175 int change;
176 unsigned int old, new;
177
178 old = soc_widget_read(w, reg);
179 new = (old & ~mask) | value;
180 change = old != new;
181 if (change)
182 soc_widget_write(w, reg, new);
183
184 return change;
185}
186
187int soc_widget_test_bits(struct snd_soc_dapm_widget *w, unsigned short reg,
188 unsigned int mask, unsigned int value)
189{
190 int change;
191 unsigned int old, new;
192
193 old = soc_widget_read(w, reg);
194 new = (old & ~mask) | value;
195 change = old != new;
196
197 return change;
198}
199
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000200/* reset 'walked' bit for each dapm path */
201static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
202{
203 struct snd_soc_dapm_path *p;
204
205 list_for_each_entry(p, &dapm->card->paths, list)
206 p->walked = 0;
207}
208
209static void dapm_clear_paths(struct snd_soc_dapm_context *dapm)
210{
211 struct snd_soc_dapm_path *p;
212 struct snd_soc_dapm_widget *w;
213 struct list_head *l;
214
Liam Girdwoode002c982011-02-08 11:45:20 +0000215 list_for_each(l, &dapm->card->paths) {
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000216 p = list_entry(l, struct snd_soc_dapm_path, list);
217 p->length = 0;
218 }
219 list_for_each(l, &dapm->card->widgets) {
220 w = list_entry(l, struct snd_soc_dapm_widget, list);
221 w->hops = 0;
222 }
223 dapm_clear_walk(dapm);
224}
225
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100226static int dapm_add_unique_widget(struct snd_soc_dapm_context *dapm,
227 struct snd_soc_dapm_widget_list **list, struct snd_soc_dapm_widget *w)
228{
229 struct snd_soc_dapm_widget_list *wlist;
230 int wlistsize, wlistentries, i;
231
232 /* is the list empty ? */
233 if (*list == NULL) {
234
235 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
236 sizeof(struct snd_soc_dapm_widget *);
237 *list = kzalloc(wlistsize, GFP_KERNEL);
238 if (*list == NULL) {
239 dev_err(dapm->dev, "can't allocate widget list for %s\n", w->name);
240 return -ENOMEM;
241 }
242 } else {
243
244 wlist = *list;
245 /* is this widget already in the list */
246 for (i = 0; i < wlist->num_widgets; i++) {
247 if (wlist->widgets[i] == w)
248 return 0;
249 }
250
251 wlistentries = wlist->num_widgets + 1;
252 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
253 wlistentries * sizeof(struct snd_soc_dapm_widget *);
254 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
255 if (*list == NULL) {
256 dev_err(dapm->dev, "can't allocate widget list for %s\n", w->name);
257 return -ENOMEM;
258 }
259 }
260 wlist = *list;
261
262 /* insert the widget */
263 dev_dbg(dapm->dev, "added %s in widget list pos %d\n",
264 w->name, wlist->num_widgets);
265 wlist->widgets[wlist->num_widgets] = w;
266 wlist->num_widgets++;
267 return 1;
268}
269
270static int is_output_widget_ep(struct snd_soc_dapm_widget *widget)
271{
272 switch (widget->id) {
273 case snd_soc_dapm_adc:
274 case snd_soc_dapm_aif_out:
275 return 1;
276 case snd_soc_dapm_output:
277 if (widget->connected && !widget->ext)
278 return 1;
279 else
280 return 0;
281 case snd_soc_dapm_hp:
282 case snd_soc_dapm_spk:
283 case snd_soc_dapm_line:
284 return !list_empty(&widget->sources);
285 default:
286 return 0;
287 }
288}
289
290static int is_input_widget_ep(struct snd_soc_dapm_widget *widget)
291{
292 switch (widget->id) {
293 case snd_soc_dapm_dac:
294 case snd_soc_dapm_aif_in:
295 return 1;
296 case snd_soc_dapm_input:
297 if (widget->connected && !widget->ext)
298 return 1;
299 else
300 return 0;
301 case snd_soc_dapm_mic:
302 return !list_empty(&widget->sources);
303 default:
304 return 0;
305 }
306}
307
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000308/*
309 * find all the paths between source and sink
310 */
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100311static int dapm_find_playback_paths(struct snd_soc_dapm_context *dapm,
312 struct snd_soc_dapm_widget *root,
313 struct snd_soc_dapm_widget_list **list, int hops)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000314{
315 struct list_head *lp;
316 struct snd_soc_dapm_path *path;
317 int dist = 0;
318
319 if (hops > PATH_MAX_HOPS)
320 return 0;
321
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100322 if (is_output_widget_ep(root) && hops != 1) {
323 dev_dbg(dapm->dev," ! %d: valid playback route found\n", hops);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000324 dapm->num_valid_paths++;
325 return 1;
326 }
327
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100328 if (root->hops && root->hops <= hops)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000329 return 0;
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100330 root->hops = hops;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000331
332 /* check all the output paths on this source widget by walking
333 * from source to sink */
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100334 list_for_each(lp, &root->sinks) {
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000335 path = list_entry(lp, struct snd_soc_dapm_path, list_source);
336
337 dev_dbg(dapm->dev," %c %d: %s -> %s -> %s\n",
338 path->connect ? '*' : ' ', hops,
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100339 root->name, path->name, path->sink->name);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000340
341 /* been here before ? */
342 if (path->length && path->length <= hops)
343 continue;
344
345 /* check down the next path if connected */
346 if (path->sink && path->connect &&
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100347 dapm_find_playback_paths(dapm, path->sink, list, hops + 1)) {
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000348 path->length = hops;
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100349
350 /* add widget to list */
351 dapm_add_unique_widget(dapm, list, path->sink);
352
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000353 if (!dist || dist > path->length)
354 dist = path->length;
355 }
356 }
357
358 return dist;
359}
360
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100361static int dapm_find_capture_paths(struct snd_soc_dapm_context *dapm,
362 struct snd_soc_dapm_widget *root,
363 struct snd_soc_dapm_widget_list **list, int hops)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000364{
365 struct list_head *lp;
366 struct snd_soc_dapm_path *path;
367 int dist = 0;
368
369 if (hops > PATH_MAX_HOPS)
370 return 0;
371
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100372 if (is_input_widget_ep(root) && hops != 1) {
373 dev_dbg(dapm->dev," ! %d: valid capture route found\n", hops);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000374 dapm->num_valid_paths++;
375 return 1;
376 }
377
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100378 if (root->hops && root->hops <= hops)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000379 return 0;
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100380 root->hops = hops;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000381
382 /* check all the output paths on this source widget by walking from
383 * sink to source */
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100384 list_for_each(lp, &root->sources) {
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000385 path = list_entry(lp, struct snd_soc_dapm_path, list_sink);
386
387 dev_dbg(dapm->dev," %c %d: %s <- %s <- %s\n",
388 path->connect ? '*' : ' ', hops,
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100389 root->name, path->name, path->source->name);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000390
391 /* been here before ? */
392 if (path->length && path->length <= hops)
393 continue;
394
395 /* check down the next path if connected */
396 if (path->source && path->connect &&
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100397 dapm_find_capture_paths(dapm, path->source, list, hops + 1)) {
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000398 path->length = hops;
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100399
400 /* add widget to list */
401 dapm_add_unique_widget(dapm, list, path->source);
402
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000403 if (!dist || dist > path->length)
404 dist = path->length;
405 }
406 }
407
408 return dist;
409}
410
411/*
412 * traverse the tree from sink to source via the shortest path
413 */
414static int dapm_get_playback_paths(struct snd_soc_dapm_context *dapm,
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100415 struct snd_soc_dapm_widget *root,
416 struct snd_soc_dapm_widget_list **list)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000417{
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100418 dev_dbg(dapm->dev, "Playback: checking paths from %s\n",root->name);
419 dapm_find_playback_paths(dapm, root, list, 1);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000420 return dapm->num_valid_paths;
421}
422
423static int dapm_get_capture_paths(struct snd_soc_dapm_context *dapm,
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100424 struct snd_soc_dapm_widget *root,
425 struct snd_soc_dapm_widget_list **list)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000426{
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100427 dev_dbg(dapm->dev, "Capture: checking paths to %s\n", root->name);
428 dapm_find_capture_paths(dapm, root, list, 1);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000429 return dapm->num_valid_paths;
430}
431
432/**
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100433 * snd_soc_dapm_get_connected_widgets_type - query audio path and it's widgets.
434 * @dapm: the dapm context.
435 * @stream_name: stream name.
436 * @list: list of active widgets for this stream.
437 * @stream: stream direction.
438 * @type: Initial widget type.
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000439 *
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100440 * Queries DAPM graph as to whether an valid audio stream path exists for
441 * the DAPM stream and initial widget type specified. This takes into account
442 * current mixer and mux kcontrol settings. Creates list of valid widgets.
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000443 *
444 * Returns the number of valid paths or negative error.
445 */
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100446int snd_soc_dapm_get_connected_widgets_type(struct snd_soc_dapm_context *dapm,
447 const char *stream_name, struct snd_soc_dapm_widget_list **list,
448 int stream, enum snd_soc_dapm_type type)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000449{
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100450 struct snd_soc_dapm_widget *w;
451 int paths;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000452
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100453 /* get stream root widget AIF, DAC or ADC from stream string and direction */
454 list_for_each_entry(w, &dapm->card->widgets, list) {
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000455
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100456 if (!w->sname)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000457 continue;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000458
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100459 if (w->id != type)
460 continue;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000461
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100462 if (strstr(w->sname, stream_name))
463 goto found;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000464 }
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100465 dev_err(dapm->dev, "root widget not found\n");
466 return 0;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000467
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100468found:
469 dapm->num_valid_paths = 0;
470 *list = NULL;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000471 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100472 paths = dapm_get_playback_paths(dapm, w, list);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000473 else
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100474 paths = dapm_get_capture_paths(dapm, w, list);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000475
476 dapm_clear_paths(dapm);
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100477 return paths;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000478}
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100479/**
480 * snd_soc_dapm_get_connected_widgets_name - query audio path and it's widgets.
481 * @dapm: the dapm context.
482 * @name: initial widget name.
483 * @list: list of active widgets for this stream.
484 * @stream: stream direction.
485 *
486 * Queries DAPM graph as to whether an valid audio stream path exists for
487 * the initial widget specified by name. This takes into account
488 * current mixer and mux kcontrol settings. Creates list of valid widgets.
489 *
490 * Returns the number of valid paths or negative error.
491 */
492int snd_soc_dapm_get_connected_widgets_name(struct snd_soc_dapm_context *dapm,
493 const char *name, struct snd_soc_dapm_widget_list **list, int stream)
494{
495 struct snd_soc_dapm_widget *w;
496 int paths;
497
498 /* get stream root widget AIF, DAC or ADC from stream string and direction */
499 list_for_each_entry(w, &dapm->card->widgets, list) {
500
501 if (strstr(w->name, name))
502 goto found;
503 }
504 dev_err(dapm->dev, "root widget %s not found\n", name);
505 return 0;
506
507found:
508 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
509 paths = dapm_get_playback_paths(dapm, w, list);
510 else
511 paths = dapm_get_capture_paths(dapm, w, list);
512
513 dapm_clear_paths(dapm);
514 return paths;
515}
Liam Girdwood35ca6602011-01-28 17:45:35 +0000516
Mark Brown452c5ea2009-05-17 21:41:23 +0100517/**
518 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800519 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100520 * @level: level to configure
521 *
522 * Configure the bias (power) levels for the SoC audio device.
523 *
524 * Returns 0 for success else error.
525 */
Mark Browned5a4c42011-02-18 11:12:42 -0800526static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200527 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100528{
Mark Browned5a4c42011-02-18 11:12:42 -0800529 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100530 int ret = 0;
531
Mark Brownf83fba82009-05-18 15:44:43 +0100532 switch (level) {
533 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200534 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100535 break;
536 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200537 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100538 break;
539 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200540 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100541 break;
542 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200543 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100544 break;
545 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200546 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100547 return -EINVAL;
548 }
549
Mark Brown84e90932010-11-04 00:07:02 -0400550 trace_snd_soc_bias_level_start(card, level);
551
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100553 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100554 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200555 if (dapm->codec && dapm->codec->driver->set_bias_level)
556 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100557 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200558 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100559 }
Mark Brown1badabd2010-12-04 12:41:04 +0000560 if (ret == 0) {
561 if (card && card->set_bias_level_post)
562 ret = card->set_bias_level_post(card, level);
563 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100564
Mark Brown84e90932010-11-04 00:07:02 -0400565 trace_snd_soc_bias_level_done(card, level);
566
Mark Brown452c5ea2009-05-17 21:41:23 +0100567 return ret;
568}
569
Richard Purdie2b97eab2006-10-06 18:32:18 +0200570/* set up initial codec paths */
571static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
572 struct snd_soc_dapm_path *p, int i)
573{
574 switch (w->id) {
575 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000576 case snd_soc_dapm_mixer:
577 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200578 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100579 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600580 w->kcontrol_news[i].private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -0400581 unsigned int reg = mc->reg;
582 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100583 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -0400584 unsigned int mask = (1 << fls(max)) - 1;
585 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200586
Liam Girdwood35ca6602011-01-28 17:45:35 +0000587 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200588 val = (val >> shift) & mask;
589
590 if ((invert && !val) || (!invert && val))
591 p->connect = 1;
592 else
593 p->connect = 0;
594 }
595 break;
596 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600597 struct soc_enum *e = (struct soc_enum *)
598 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200599 int val, item, bitmask;
600
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100601 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200602 ;
Liam Girdwood35ca6602011-01-28 17:45:35 +0000603 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200604 item = (val >> e->shift_l) & (bitmask - 1);
605
606 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100607 for (i = 0; i < e->max; i++) {
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100608 if (!(strcmp(p->name, snd_soc_get_enum_text(e, i))) && item == i)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200609 p->connect = 1;
610 }
611 }
612 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000613 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600614 struct soc_enum *e = (struct soc_enum *)
615 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000616
617 p->connect = 0;
618 /* since a virtual mux has no backing registers to
619 * decide which path to connect, it will try to match
620 * with the first enumeration. This is to ensure
621 * that the default mux choice (the first) will be
622 * correctly powered up during initialization.
623 */
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100624 if (!strcmp(p->name, snd_soc_get_enum_text(e, 0)))
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000625 p->connect = 1;
626 }
627 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200628 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200629 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600630 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200631 int val, item;
632
Liam Girdwood35ca6602011-01-28 17:45:35 +0000633 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200634 val = (val >> e->shift_l) & e->mask;
635 for (item = 0; item < e->max; item++) {
636 if (val == e->values[item])
637 break;
638 }
639
640 p->connect = 0;
641 for (i = 0; i < e->max; i++) {
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100642 if (!(strcmp(p->name, snd_soc_get_enum_text(e, i))) && item == i)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200643 p->connect = 1;
644 }
645 }
646 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200647 /* does not effect routing - always connected */
648 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600649 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200650 case snd_soc_dapm_output:
651 case snd_soc_dapm_adc:
652 case snd_soc_dapm_input:
653 case snd_soc_dapm_dac:
654 case snd_soc_dapm_micbias:
655 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100656 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100657 case snd_soc_dapm_aif_in:
658 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200659 p->connect = 1;
660 break;
661 /* does effect routing - dynamically connected */
662 case snd_soc_dapm_hp:
663 case snd_soc_dapm_mic:
664 case snd_soc_dapm_spk:
665 case snd_soc_dapm_line:
666 case snd_soc_dapm_pre:
667 case snd_soc_dapm_post:
668 p->connect = 0;
669 break;
670 }
671}
672
Mark Brown74b8f952009-06-06 11:26:15 +0100673/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200674static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200675 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
676 struct snd_soc_dapm_path *path, const char *control_name,
677 const struct snd_kcontrol_new *kcontrol)
678{
679 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
680 int i;
681
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100682 for (i = 0; i < e->max; i++) {
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100683 if (!(strcmp(control_name, snd_soc_get_enum_text(e, i)))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200684 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200685 list_add(&path->list_sink, &dest->sources);
686 list_add(&path->list_source, &src->sinks);
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100687 path->name = (char*)snd_soc_get_enum_text(e, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200688 dapm_set_path_status(dest, path, 0);
689 return 0;
690 }
691 }
692
693 return -ENODEV;
694}
695
Mark Brown74b8f952009-06-06 11:26:15 +0100696/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200697static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200698 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
699 struct snd_soc_dapm_path *path, const char *control_name)
700{
701 int i;
702
703 /* search for mixer kcontrol */
704 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600705 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200706 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200707 list_add(&path->list_sink, &dest->sources);
708 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600709 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200710 dapm_set_path_status(dest, path, i);
711 return 0;
712 }
713 }
714 return -ENODEV;
715}
716
Stephen Warrenaf468002011-04-28 17:38:01 -0600717static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600718 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600719 const struct snd_kcontrol_new *kcontrol_new,
720 struct snd_kcontrol **kcontrol)
721{
722 struct snd_soc_dapm_widget *w;
723 int i;
724
725 *kcontrol = NULL;
726
727 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600728 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
729 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600730 for (i = 0; i < w->num_kcontrols; i++) {
731 if (&w->kcontrol_news[i] == kcontrol_new) {
732 if (w->kcontrols)
733 *kcontrol = w->kcontrols[i];
734 return 1;
735 }
736 }
737 }
738
739 return 0;
740}
741
Richard Purdie2b97eab2006-10-06 18:32:18 +0200742/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200743static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200744{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200745 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200746 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000747 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200748 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000749 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000750 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600751 struct snd_soc_dapm_widget_list *wlist;
752 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000753
754 if (dapm->codec)
755 prefix = dapm->codec->name_prefix;
756 else
757 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200758
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000759 if (prefix)
760 prefix_len = strlen(prefix) + 1;
761 else
762 prefix_len = 0;
763
Richard Purdie2b97eab2006-10-06 18:32:18 +0200764 /* add kcontrol */
765 for (i = 0; i < w->num_kcontrols; i++) {
766
767 /* match name */
768 list_for_each_entry(path, &w->sources, list_sink) {
769
770 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600771 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200772 continue;
773
Stephen Warrenfafd2172011-04-28 17:38:00 -0600774 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
775 sizeof(struct snd_soc_dapm_widget *),
776 wlist = kzalloc(wlistsize, GFP_KERNEL);
777 if (wlist == NULL) {
778 dev_err(dapm->dev,
779 "asoc: can't allocate widget list for %s\n",
780 w->name);
781 return -ENOMEM;
782 }
783 wlist->num_widgets = 1;
784 wlist->widgets[0] = w;
785
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000786 /* add dapm control with long name.
787 * for dapm_mixer this is the concatenation of the
788 * mixer and kcontrol name.
789 * for dapm_mixer_named_ctl this is simply the
790 * kcontrol name.
791 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600792 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000793 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000794 name_len += 1 + strlen(w->name);
795
Mark Brown219b93f2008-10-28 13:02:31 +0000796 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000797
Stephen Warrenfafd2172011-04-28 17:38:00 -0600798 if (path->long_name == NULL) {
799 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200800 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600801 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200802
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000803 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000804 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000805 /* The control will get a prefix from
806 * the control creation process but
807 * we're also using the same prefix
808 * for widgets so cut the prefix off
809 * the front of the widget name.
810 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000811 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000812 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600813 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000814 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000815 case snd_soc_dapm_mixer_named_ctl:
816 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600817 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000818 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000819 }
820
Mark Brown219b93f2008-10-28 13:02:31 +0000821 path->long_name[name_len - 1] = '\0';
822
Stephen Warrenfafd2172011-04-28 17:38:00 -0600823 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
824 wlist, path->long_name,
825 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200826 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200827 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200828 dev_err(dapm->dev,
829 "asoc: failed to add dapm kcontrol %s: %d\n",
830 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600831 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200832 kfree(path->long_name);
833 path->long_name = NULL;
834 return ret;
835 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600836 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200837 }
838 }
839 return ret;
840}
841
842/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200843static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200844{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200845 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200846 struct snd_soc_dapm_path *path = NULL;
847 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000848 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000849 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000850 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600851 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600852 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600853 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600854 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600855 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200856
Stephen Warrenaf468002011-04-28 17:38:01 -0600857 if (w->num_kcontrols != 1) {
858 dev_err(dapm->dev,
859 "asoc: mux %s has incorrect number of controls\n",
860 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200861 return -EINVAL;
862 }
863
Stephen Warren1007da02011-05-26 09:57:33 -0600864 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600865 &kcontrol);
866 if (kcontrol) {
867 wlist = kcontrol->private_data;
868 wlistentries = wlist->num_widgets + 1;
869 } else {
870 wlist = NULL;
871 wlistentries = 1;
872 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600873 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600874 wlistentries * sizeof(struct snd_soc_dapm_widget *),
875 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600876 if (wlist == NULL) {
877 dev_err(dapm->dev,
878 "asoc: can't allocate widget list for %s\n", w->name);
879 return -ENOMEM;
880 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600881 wlist->num_widgets = wlistentries;
882 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600883
Stephen Warrenaf468002011-04-28 17:38:01 -0600884 if (!kcontrol) {
Liam Girdwood4d74b362011-05-18 17:50:07 +0100885 if (dapm->codec && dapm->codec->name_prefix)
Stephen Warrenaf468002011-04-28 17:38:01 -0600886 prefix = dapm->codec->name_prefix;
887 else
Misael Lopez Cruzbd5e5632011-06-17 13:35:36 -0700888 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000889
Stephen Warrenaf468002011-04-28 17:38:01 -0600890 if (shared) {
891 name = w->kcontrol_news[0].name;
892 prefix_len = 0;
893 } else {
894 name = w->name;
895 if (prefix)
896 prefix_len = strlen(prefix) + 1;
897 else
898 prefix_len = 0;
899 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000900
Stephen Warrenaf468002011-04-28 17:38:01 -0600901 /*
902 * The control will get a prefix from the control creation
903 * process but we're also using the same prefix for widgets so
904 * cut the prefix off the front of the widget name.
905 */
906 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
Misael Lopez Cruzbd5e5632011-06-17 13:35:36 -0700907 name, prefix);
Stephen Warrenaf468002011-04-28 17:38:01 -0600908 ret = snd_ctl_add(card, kcontrol);
909 if (ret < 0) {
910 dev_err(dapm->dev,
911 "asoc: failed to add kcontrol %s\n", w->name);
912 kfree(wlist);
913 return ret;
914 }
915 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200916
Stephen Warrenaf468002011-04-28 17:38:01 -0600917 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200918
Stephen Warrenfad59882011-04-28 17:37:59 -0600919 w->kcontrols[0] = kcontrol;
920
Richard Purdie2b97eab2006-10-06 18:32:18 +0200921 list_for_each_entry(path, &w->sources, list_sink)
922 path->kcontrol = kcontrol;
923
Stephen Warrenaf468002011-04-28 17:38:01 -0600924 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200925}
926
927/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200928static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200929{
Mark Browna6c65732010-03-03 17:45:21 +0000930 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200931 dev_err(w->dapm->dev,
932 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200933
Mark Browna6c65732010-03-03 17:45:21 +0000934 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200935}
936
Mark Brown99497882010-05-07 20:24:05 +0100937/* We implement power down on suspend by checking the power state of
938 * the ALSA card - when we are suspending the ALSA state for the card
939 * is set to D3.
940 */
941static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
942{
Mark Brown12ea2c72011-03-02 18:17:32 +0000943 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100944
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000945 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100946 case SNDRV_CTL_POWER_D3hot:
947 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100948 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200949 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
950 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100951 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100952 default:
953 return 1;
954 }
955}
956
Richard Purdie2b97eab2006-10-06 18:32:18 +0200957/*
958 * Recursively check for a completed path to an active or physically connected
959 * output widget. Returns number of complete paths.
960 */
961static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
962{
963 struct snd_soc_dapm_path *path;
964 int con = 0;
965
Mark Brown246d0a12009-04-22 18:24:55 +0100966 if (widget->id == snd_soc_dapm_supply)
967 return 0;
968
Mark Brown010ff262009-08-17 17:39:22 +0100969 switch (widget->id) {
970 case snd_soc_dapm_adc:
971 case snd_soc_dapm_aif_out:
972 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100973 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100974 default:
975 break;
976 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200977
978 if (widget->connected) {
979 /* connected pin ? */
980 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100981 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200982
983 /* connected jack or spk ? */
984 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300985 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100986 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200987 }
988
989 list_for_each_entry(path, &widget->sinks, list_source) {
990 if (path->walked)
991 continue;
992
993 if (path->sink && path->connect) {
994 path->walked = 1;
995 con += is_connected_output_ep(path->sink);
996 }
997 }
998
999 return con;
1000}
1001
1002/*
1003 * Recursively check for a completed path to an active or physically connected
1004 * input widget. Returns number of complete paths.
1005 */
1006static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
1007{
1008 struct snd_soc_dapm_path *path;
1009 int con = 0;
1010
Mark Brown246d0a12009-04-22 18:24:55 +01001011 if (widget->id == snd_soc_dapm_supply)
1012 return 0;
1013
Richard Purdie2b97eab2006-10-06 18:32:18 +02001014 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +01001015 switch (widget->id) {
1016 case snd_soc_dapm_dac:
1017 case snd_soc_dapm_aif_in:
1018 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +01001019 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +01001020 default:
1021 break;
1022 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001023
1024 if (widget->connected) {
1025 /* connected pin ? */
1026 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +01001027 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001028
1029 /* connected VMID/Bias for lower pops */
1030 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +01001031 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001032
1033 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001034 if (widget->id == snd_soc_dapm_mic ||
1035 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +01001036 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001037 }
1038
1039 list_for_each_entry(path, &widget->sources, list_sink) {
1040 if (path->walked)
1041 continue;
1042
1043 if (path->source && path->connect) {
1044 path->walked = 1;
1045 con += is_connected_input_ep(path->source);
1046 }
1047 }
1048
1049 return con;
1050}
1051
1052/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001053 * Handler for generic register modifier widget.
1054 */
1055int dapm_reg_event(struct snd_soc_dapm_widget *w,
1056 struct snd_kcontrol *kcontrol, int event)
1057{
1058 unsigned int val;
1059
1060 if (SND_SOC_DAPM_EVENT_ON(event))
1061 val = w->on_val;
1062 else
1063 val = w->off_val;
1064
Liam Girdwood35ca6602011-01-28 17:45:35 +00001065 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001066 w->mask << w->shift, val << w->shift);
1067
1068 return 0;
1069}
Mark Brown11589412008-07-29 11:42:23 +01001070EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001071
Mark Browncd0f2d42009-04-20 16:56:59 +01001072/* Generic check to see if a widget should be powered.
1073 */
1074static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1075{
1076 int in, out;
1077
1078 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001079 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +01001080 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001081 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +01001082 return out != 0 && in != 0;
1083}
1084
Mark Brown6ea31b92009-04-20 17:15:41 +01001085/* Check to see if an ADC has power */
1086static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1087{
1088 int in;
1089
1090 if (w->active) {
1091 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001092 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001093 return in != 0;
1094 } else {
1095 return dapm_generic_check_power(w);
1096 }
1097}
1098
1099/* Check to see if a DAC has power */
1100static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1101{
1102 int out;
1103
1104 if (w->active) {
1105 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001106 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001107 return out != 0;
1108 } else {
1109 return dapm_generic_check_power(w);
1110 }
1111}
1112
Mark Brown246d0a12009-04-22 18:24:55 +01001113/* Check to see if a power supply is needed */
1114static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1115{
1116 struct snd_soc_dapm_path *path;
1117 int power = 0;
1118
1119 /* Check if one of our outputs is connected */
1120 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001121 if (path->connected &&
1122 !path->connected(path->source, path->sink))
1123 continue;
1124
Mark Brown30173582011-02-11 11:42:19 +00001125 if (!path->sink)
1126 continue;
1127
1128 if (path->sink->force) {
1129 power = 1;
1130 break;
1131 }
1132
1133 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +01001134 path->sink->power_check(path->sink)) {
1135 power = 1;
1136 break;
1137 }
1138 }
1139
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001140 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +01001141
1142 return power;
1143}
1144
Mark Brown38357ab2009-06-06 19:03:23 +01001145static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1146 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001147 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001148{
Mark Brown828a8422011-01-15 13:14:30 +00001149 int *sort;
1150
1151 if (power_up)
1152 sort = dapm_up_seq;
1153 else
1154 sort = dapm_down_seq;
1155
Mark Brown38357ab2009-06-06 19:03:23 +01001156 if (sort[a->id] != sort[b->id])
1157 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001158 if (a->subseq != b->subseq) {
1159 if (power_up)
1160 return a->subseq - b->subseq;
1161 else
1162 return b->subseq - a->subseq;
1163 }
Mark Brownb22ead22009-06-07 12:51:26 +01001164 if (a->reg != b->reg)
1165 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001166 if (a->dapm != b->dapm)
1167 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001168
Mark Brown38357ab2009-06-06 19:03:23 +01001169 return 0;
1170}
Mark Brown42aa3412009-03-01 19:21:10 +00001171
Mark Brown38357ab2009-06-06 19:03:23 +01001172/* Insert a widget in order into a DAPM power sequence. */
1173static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1174 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001175 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001176{
1177 struct snd_soc_dapm_widget *w;
1178
1179 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001180 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001181 list_add_tail(&new_widget->power_list, &w->power_list);
1182 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001183 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001184
Mark Brown38357ab2009-06-06 19:03:23 +01001185 list_add_tail(&new_widget->power_list, list);
1186}
Mark Brown42aa3412009-03-01 19:21:10 +00001187
Mark Brown68f89ad2010-11-03 23:51:49 -04001188static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1189 struct snd_soc_dapm_widget *w, int event)
1190{
1191 struct snd_soc_card *card = dapm->card;
1192 const char *ev_name;
1193 int power, ret;
1194
1195 switch (event) {
1196 case SND_SOC_DAPM_PRE_PMU:
1197 ev_name = "PRE_PMU";
1198 power = 1;
1199 break;
1200 case SND_SOC_DAPM_POST_PMU:
1201 ev_name = "POST_PMU";
1202 power = 1;
1203 break;
1204 case SND_SOC_DAPM_PRE_PMD:
1205 ev_name = "PRE_PMD";
1206 power = 0;
1207 break;
1208 case SND_SOC_DAPM_POST_PMD:
1209 ev_name = "POST_PMD";
1210 power = 0;
1211 break;
1212 default:
1213 BUG();
1214 return;
1215 }
1216
1217 if (w->power != power)
1218 return;
1219
1220 if (w->event && (w->event_flags & event)) {
1221 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1222 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001223 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001224 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001225 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001226 if (ret < 0)
1227 pr_err("%s: %s event failed: %d\n",
1228 ev_name, w->name, ret);
1229 }
1230}
1231
Mark Brownb22ead22009-06-07 12:51:26 +01001232/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001233static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001234 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001235{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001236 struct snd_soc_card *card = dapm->card;
Liam Girdwoodecc1a0d2011-02-16 16:24:17 +00001237 struct snd_soc_dapm_widget *w, *_w;
Mark Brown68f89ad2010-11-03 23:51:49 -04001238 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001239 unsigned int value = 0;
1240 unsigned int mask = 0;
1241 unsigned int cur_mask;
1242
Liam Girdwoodecc1a0d2011-02-16 16:24:17 +00001243 _w = list_first_entry(pending, struct snd_soc_dapm_widget,
1244 power_list);
1245 reg = _w->reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001246
1247 list_for_each_entry(w, pending, power_list) {
1248 cur_mask = 1 << w->shift;
1249 BUG_ON(reg != w->reg);
1250
1251 if (w->invert)
1252 power = !w->power;
1253 else
1254 power = w->power;
1255
1256 mask |= cur_mask;
1257 if (power)
1258 value |= cur_mask;
1259
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001260 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001261 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1262 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001263
Mark Brown68f89ad2010-11-03 23:51:49 -04001264 /* Check for events */
1265 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1266 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001267 }
1268
Mark Brown81628102009-06-07 13:21:24 +01001269 if (reg >= 0) {
Mark Brown9a014432011-06-19 13:49:28 +01001270 /* Any widget will do, they should all be updating the
1271 * same register.
1272 */
1273 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1274 power_list);
1275
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001276 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001277 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001278 value, mask, reg, card->pop_time);
1279 pop_wait(card->pop_time);
Liam Girdwoodecc1a0d2011-02-16 16:24:17 +00001280 soc_widget_update_bits(_w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001281 }
1282
1283 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001284 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1285 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001286 }
Mark Brown42aa3412009-03-01 19:21:10 +00001287}
1288
Mark Brownb22ead22009-06-07 12:51:26 +01001289/* Apply a DAPM power sequence.
1290 *
1291 * We walk over a pre-sorted list of widgets to apply power to. In
1292 * order to minimise the number of writes to the device required
1293 * multiple widgets will be updated in a single write where possible.
1294 * Currently anything that requires more than a single write is not
1295 * handled.
1296 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001297static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001298 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001299{
1300 struct snd_soc_dapm_widget *w, *n;
1301 LIST_HEAD(pending);
1302 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001303 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001304 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001305 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001306 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001307 int *sort;
1308
1309 if (power_up)
1310 sort = dapm_up_seq;
1311 else
1312 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001313
Mark Brownb22ead22009-06-07 12:51:26 +01001314 list_for_each_entry_safe(w, n, list, power_list) {
1315 ret = 0;
1316
1317 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001318 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001319 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001320 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001321 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001322
Mark Brown474b62d2011-01-18 16:14:44 +00001323 if (cur_dapm && cur_dapm->seq_notifier) {
1324 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1325 if (sort[i] == cur_sort)
1326 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001327 i,
1328 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001329 }
1330
Mark Brownb22ead22009-06-07 12:51:26 +01001331 INIT_LIST_HEAD(&pending);
1332 cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001333 cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001334 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001335 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001336 }
1337
Mark Brown163cac02009-06-07 10:12:52 +01001338 switch (w->id) {
1339 case snd_soc_dapm_pre:
1340 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001341 list_for_each_entry_safe_continue(w, n, list,
1342 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001343
Mark Brownb22ead22009-06-07 12:51:26 +01001344 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001345 ret = w->event(w,
1346 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001347 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001348 ret = w->event(w,
1349 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001350 break;
1351
1352 case snd_soc_dapm_post:
1353 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001354 list_for_each_entry_safe_continue(w, n, list,
1355 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001356
Mark Brownb22ead22009-06-07 12:51:26 +01001357 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001358 ret = w->event(w,
1359 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001360 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001361 ret = w->event(w,
1362 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001363 break;
1364
Mark Brown163cac02009-06-07 10:12:52 +01001365 default:
Mark Brown81628102009-06-07 13:21:24 +01001366 /* Queue it up for application */
1367 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001368 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001369 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001370 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001371 list_move(&w->power_list, &pending);
1372 break;
Mark Brown163cac02009-06-07 10:12:52 +01001373 }
Mark Brownb22ead22009-06-07 12:51:26 +01001374
1375 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001376 dev_err(w->dapm->dev,
1377 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001378 }
Mark Brownb22ead22009-06-07 12:51:26 +01001379
1380 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001381 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001382
1383 if (cur_dapm && cur_dapm->seq_notifier) {
1384 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1385 if (sort[i] == cur_sort)
1386 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001387 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001388 }
Mark Brown163cac02009-06-07 10:12:52 +01001389}
1390
Mark Brown97404f22010-12-14 16:13:57 +00001391static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1392{
1393 struct snd_soc_dapm_update *update = dapm->update;
1394 struct snd_soc_dapm_widget *w;
1395 int ret;
1396
1397 if (!update)
1398 return;
1399
1400 w = update->widget;
1401
1402 if (w->event &&
1403 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1404 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1405 if (ret != 0)
1406 pr_err("%s DAPM pre-event failed: %d\n",
1407 w->name, ret);
1408 }
1409
1410 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1411 update->val);
1412 if (ret < 0)
1413 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1414
1415 if (w->event &&
1416 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1417 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1418 if (ret != 0)
1419 pr_err("%s DAPM post-event failed: %d\n",
1420 w->name, ret);
1421 }
1422}
1423
Mark Brown9d0624a2011-02-18 11:49:43 -08001424/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1425 * they're changing state.
1426 */
1427static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1428{
1429 struct snd_soc_dapm_context *d = data;
1430 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001431
Mark Brown9d0624a2011-02-18 11:49:43 -08001432 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1433 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1434 if (ret != 0)
1435 dev_err(d->dev,
1436 "Failed to turn on bias: %d\n", ret);
1437 }
1438
1439 /* If we're changing to all on or all off then prepare */
1440 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1441 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1442 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1443 if (ret != 0)
1444 dev_err(d->dev,
1445 "Failed to prepare bias: %d\n", ret);
1446 }
1447}
1448
1449/* Async callback run prior to DAPM sequences - brings to their final
1450 * state.
1451 */
1452static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1453{
1454 struct snd_soc_dapm_context *d = data;
1455 int ret;
1456
1457 /* If we just powered the last thing off drop to standby bias */
1458 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1459 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1460 if (ret != 0)
1461 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1462 ret);
1463 }
1464
1465 /* If we're in standby and can support bias off then do that */
1466 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
1467 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1468 if (ret != 0)
1469 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1470 }
1471
1472 /* If we just powered up then move to active bias */
1473 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1474 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1475 if (ret != 0)
1476 dev_err(d->dev, "Failed to apply active bias: %d\n",
1477 ret);
1478 }
1479}
Mark Brown97404f22010-12-14 16:13:57 +00001480
Mark Brown42aa3412009-03-01 19:21:10 +00001481/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001482 * Scan each dapm widget for complete audio path.
1483 * A complete path is a route that has valid endpoints i.e.:-
1484 *
1485 * o DAC to output pin.
1486 * o Input Pin to ADC.
1487 * o Input pin to Output pin (bypass, sidetone)
1488 * o DAC to ADC (loopback).
1489 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001490static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001491{
Mark Brown12ea2c72011-03-02 18:17:32 +00001492 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001493 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001494 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001495 LIST_HEAD(up_list);
1496 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001497 LIST_HEAD(async_domain);
Mark Brown38357ab2009-06-06 19:03:23 +01001498 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001499
Mark Brown84e90932010-11-04 00:07:02 -04001500 trace_snd_soc_dapm_start(card);
1501
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001502 list_for_each_entry(d, &card->dapm_list, list)
Jarkko Nikulaea77b942011-05-26 16:32:18 +03001503 if (d->n_widgets || d->codec == NULL)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001504 d->dev_power = 0;
1505
Mark Brown6d3ddc82009-05-16 17:47:29 +01001506 /* Check which widgets we need to power and store them in
1507 * lists indicating if they should be powered up or down.
1508 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001509 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001510 switch (w->id) {
1511 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001512 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001513 break;
1514 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001515 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001516 break;
1517
1518 default:
1519 if (!w->power_check)
1520 continue;
1521
Mark Brown99497882010-05-07 20:24:05 +01001522 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001523 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001524 else
1525 power = 1;
1526 if (power)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001527 w->dapm->dev_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +01001528
Mark Brown6d3ddc82009-05-16 17:47:29 +01001529 if (w->power == power)
1530 continue;
1531
Mark Brown84e90932010-11-04 00:07:02 -04001532 trace_snd_soc_dapm_widget_power(w, power);
1533
Mark Brown6d3ddc82009-05-16 17:47:29 +01001534 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001535 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001536 else
Mark Brown828a8422011-01-15 13:14:30 +00001537 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001538
1539 w->power = power;
1540 break;
1541 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001542 }
1543
Mark Brownb14b76a2009-08-17 11:55:38 +01001544 /* If there are no DAPM widgets then try to figure out power from the
1545 * event type.
1546 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001547 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001548 switch (event) {
1549 case SND_SOC_DAPM_STREAM_START:
1550 case SND_SOC_DAPM_STREAM_RESUME:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001551 dapm->dev_power = 1;
Mark Brownb14b76a2009-08-17 11:55:38 +01001552 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001553 case SND_SOC_DAPM_STREAM_STOP:
Liam Girdwood7987a112011-01-31 19:52:42 +00001554 if (dapm->codec)
1555 dapm->dev_power = !!dapm->codec->active;
1556 else
1557 dapm->dev_power = 0;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001558 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001559 case SND_SOC_DAPM_STREAM_SUSPEND:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001560 dapm->dev_power = 0;
Mark Brown50b6bce2009-11-23 13:11:53 +00001561 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001562 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001563 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +00001564 case SND_SOC_BIAS_STANDBY:
1565 case SND_SOC_BIAS_OFF:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001566 dapm->dev_power = 0;
Mark Browna96ca332010-01-19 22:49:43 +00001567 break;
1568 default:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001569 dapm->dev_power = 1;
Mark Browna96ca332010-01-19 22:49:43 +00001570 break;
1571 }
Mark Brown50b6bce2009-11-23 13:11:53 +00001572 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001573 default:
1574 break;
1575 }
1576 }
1577
Mark Brown52ba67b2011-04-04 21:05:11 +09001578 /* Force all contexts in the card to the same bias state */
1579 power = 0;
1580 list_for_each_entry(d, &card->dapm_list, list)
1581 if (d->dev_power)
1582 power = 1;
1583 list_for_each_entry(d, &card->dapm_list, list)
1584 d->dev_power = power;
1585
1586
Mark Brown9d0624a2011-02-18 11:49:43 -08001587 /* Run all the bias changes in parallel */
1588 list_for_each_entry(d, &dapm->card->dapm_list, list)
1589 async_schedule_domain(dapm_pre_sequence_async, d,
1590 &async_domain);
1591 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001592
Mark Brown6d3ddc82009-05-16 17:47:29 +01001593 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001594 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001595
Mark Brown97404f22010-12-14 16:13:57 +00001596 dapm_widget_update(dapm);
1597
Mark Brown6d3ddc82009-05-16 17:47:29 +01001598 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001599 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001600
Mark Brown9d0624a2011-02-18 11:49:43 -08001601 /* Run all the bias changes in parallel */
1602 list_for_each_entry(d, &dapm->card->dapm_list, list)
1603 async_schedule_domain(dapm_post_sequence_async, d,
1604 &async_domain);
1605 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001606
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001607 pop_dbg(dapm->dev, card->pop_time,
1608 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001609 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001610
Mark Brown84e90932010-11-04 00:07:02 -04001611 trace_snd_soc_dapm_done(card);
1612
Mark Brown42aa3412009-03-01 19:21:10 +00001613 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001614}
1615
Mark Brown79fb9382009-08-21 16:38:13 +01001616#ifdef CONFIG_DEBUG_FS
1617static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1618{
1619 file->private_data = inode->i_private;
1620 return 0;
1621}
1622
1623static ssize_t dapm_widget_power_read_file(struct file *file,
1624 char __user *user_buf,
1625 size_t count, loff_t *ppos)
1626{
1627 struct snd_soc_dapm_widget *w = file->private_data;
1628 char *buf;
1629 int in, out;
1630 ssize_t ret;
1631 struct snd_soc_dapm_path *p = NULL;
1632
1633 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1634 if (!buf)
1635 return -ENOMEM;
1636
1637 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001638 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001639 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001640 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001641
Mark Brownd033c362009-12-04 15:25:56 +00001642 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001643 w->name, w->power ? "On" : "Off", in, out);
1644
Mark Brownd033c362009-12-04 15:25:56 +00001645 if (w->reg >= 0)
1646 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1647 " - R%d(0x%x) bit %d",
1648 w->reg, w->reg, w->shift);
1649
1650 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1651
Mark Brown3eef08b2009-09-14 16:49:00 +01001652 if (w->sname)
1653 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1654 w->sname,
1655 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001656
1657 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001658 if (p->connected && !p->connected(w, p->sink))
1659 continue;
1660
Mark Brown79fb9382009-08-21 16:38:13 +01001661 if (p->connect)
1662 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001663 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001664 p->name ? p->name : "static",
1665 p->source->name);
1666 }
1667 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001668 if (p->connected && !p->connected(w, p->sink))
1669 continue;
1670
Mark Brown79fb9382009-08-21 16:38:13 +01001671 if (p->connect)
1672 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001673 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001674 p->name ? p->name : "static",
1675 p->sink->name);
1676 }
1677
1678 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1679
1680 kfree(buf);
1681 return ret;
1682}
1683
1684static const struct file_operations dapm_widget_power_fops = {
1685 .open = dapm_widget_power_open_file,
1686 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001687 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001688};
1689
Mark Brownef49e4f2011-04-04 20:48:13 +09001690static int dapm_bias_open_file(struct inode *inode, struct file *file)
1691{
1692 file->private_data = inode->i_private;
1693 return 0;
1694}
1695
1696static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1697 size_t count, loff_t *ppos)
1698{
1699 struct snd_soc_dapm_context *dapm = file->private_data;
1700 char *level;
1701
1702 switch (dapm->bias_level) {
1703 case SND_SOC_BIAS_ON:
1704 level = "On\n";
1705 break;
1706 case SND_SOC_BIAS_PREPARE:
1707 level = "Prepare\n";
1708 break;
1709 case SND_SOC_BIAS_STANDBY:
1710 level = "Standby\n";
1711 break;
1712 case SND_SOC_BIAS_OFF:
1713 level = "Off\n";
1714 break;
1715 default:
1716 BUG();
1717 level = "Unknown\n";
1718 break;
1719 }
1720
1721 return simple_read_from_buffer(user_buf, count, ppos, level,
1722 strlen(level));
1723}
1724
1725static const struct file_operations dapm_bias_fops = {
1726 .open = dapm_bias_open_file,
1727 .read = dapm_bias_read_file,
1728 .llseek = default_llseek,
1729};
1730
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001731void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1732 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001733{
Mark Brown79fb9382009-08-21 16:38:13 +01001734 struct dentry *d;
1735
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001736 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1737
1738 if (!dapm->debugfs_dapm) {
1739 printk(KERN_WARNING
1740 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001741 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001742 }
Mark Brown79fb9382009-08-21 16:38:13 +01001743
Mark Brownef49e4f2011-04-04 20:48:13 +09001744 d = debugfs_create_file("bias_level", 0444,
1745 dapm->debugfs_dapm, dapm,
1746 &dapm_bias_fops);
1747 if (!d)
1748 dev_warn(dapm->dev,
1749 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001750}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001751
1752static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1753{
1754 struct snd_soc_dapm_context *dapm = w->dapm;
1755 struct dentry *d;
1756
1757 if (!dapm->debugfs_dapm || !w->name)
1758 return;
1759
1760 d = debugfs_create_file(w->name, 0444,
1761 dapm->debugfs_dapm, w,
1762 &dapm_widget_power_fops);
1763 if (!d)
1764 dev_warn(w->dapm->dev,
1765 "ASoC: Failed to create %s debugfs file\n",
1766 w->name);
1767}
1768
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001769static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1770{
1771 debugfs_remove_recursive(dapm->debugfs_dapm);
1772}
1773
Mark Brown79fb9382009-08-21 16:38:13 +01001774#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001775void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1776 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001777{
1778}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001779
1780static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1781{
1782}
1783
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001784static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1785{
1786}
1787
Mark Brown79fb9382009-08-21 16:38:13 +01001788#endif
1789
Richard Purdie2b97eab2006-10-06 18:32:18 +02001790/* test and update the power status of a mux widget */
Liam Girdwoode002c982011-02-08 11:45:20 +00001791int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001792 struct snd_kcontrol *kcontrol, int change,
1793 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001794{
1795 struct snd_soc_dapm_path *path;
1796 int found = 0;
1797
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001798 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001799 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001800 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001801 return -ENODEV;
1802
Mark Brown3a655772009-10-05 17:23:30 +01001803 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001804 return 0;
1805
1806 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001807 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Liam Girdwoode002c982011-02-08 11:45:20 +00001808
Richard Purdie2b97eab2006-10-06 18:32:18 +02001809 if (path->kcontrol != kcontrol)
1810 continue;
1811
Liam Girdwooda8f13d82011-05-18 18:39:21 +01001812 if (!path->name || !snd_soc_get_enum_text(e, mux))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001813 continue;
1814
1815 found = 1;
1816 /* we now need to match the string in the enum to the path */
Liam Girdwooda8f13d82011-05-18 18:39:21 +01001817 if (!(strcmp(path->name, snd_soc_get_enum_text(e, mux))))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001818 path->connect = 1; /* new connection */
1819 else
1820 path->connect = 0; /* old connection must be powered down */
1821 }
1822
Liam Girdwoode002c982011-02-08 11:45:20 +00001823 if (found) {
Patrick Lai88f4c8f2011-08-25 16:46:19 -07001824 if (widget->platform)
1825 soc_dsp_runtime_update(widget);
Patrick Laia8d41ee2011-09-12 12:42:04 -07001826 else
1827 dapm_power_widgets(widget->dapm,
1828 SND_SOC_DAPM_STREAM_NOP);
Liam Girdwoode002c982011-02-08 11:45:20 +00001829 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001830
1831 return 0;
1832}
Liam Girdwoode002c982011-02-08 11:45:20 +00001833EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001834
Milan plzik1b075e32008-01-10 14:39:46 +01001835/* test and update the power status of a mixer or switch widget */
Liam Girdwoode002c982011-02-08 11:45:20 +00001836int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1837 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001838{
1839 struct snd_soc_dapm_path *path;
1840 int found = 0;
1841
Milan plzik1b075e32008-01-10 14:39:46 +01001842 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001843 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001844 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001845 return -ENODEV;
1846
Richard Purdie2b97eab2006-10-06 18:32:18 +02001847 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001848 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001849 if (path->kcontrol != kcontrol)
1850 continue;
1851
1852 /* found, now check type */
1853 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001854 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001855 break;
1856 }
1857
Liam Girdwoode002c982011-02-08 11:45:20 +00001858 if (found) {
Patrick Lai88f4c8f2011-08-25 16:46:19 -07001859 if (widget->platform)
1860 soc_dsp_runtime_update(widget);
Patrick Laia8d41ee2011-09-12 12:42:04 -07001861 else
1862 dapm_power_widgets(widget->dapm,
1863 SND_SOC_DAPM_STREAM_NOP);
Liam Girdwoode002c982011-02-08 11:45:20 +00001864 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001865
1866 return 0;
1867}
Liam Girdwoode002c982011-02-08 11:45:20 +00001868EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001869
1870/* show dapm widget status in sys fs */
Liam Girdwood7987a112011-01-31 19:52:42 +00001871static ssize_t widget_show(struct snd_soc_dapm_context *dapm,
1872 const char *name, char *buf, ssize_t count)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001873{
Richard Purdie2b97eab2006-10-06 18:32:18 +02001874 struct snd_soc_dapm_widget *w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001875 char *state = "not set";
1876
Liam Girdwood7987a112011-01-31 19:52:42 +00001877 count += sprintf(buf + count, "\n%s\n", name);
1878
Liam Girdwoodab1058a2011-01-31 20:33:07 +00001879 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001880
1881 /* only display widgets that burnm power */
1882 switch (w->id) {
1883 case snd_soc_dapm_hp:
1884 case snd_soc_dapm_mic:
1885 case snd_soc_dapm_spk:
1886 case snd_soc_dapm_line:
1887 case snd_soc_dapm_micbias:
1888 case snd_soc_dapm_dac:
1889 case snd_soc_dapm_adc:
1890 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001891 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001892 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001893 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001894 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001895 if (w->name)
1896 count += sprintf(buf + count, "%s: %s\n",
1897 w->name, w->power ? "On":"Off");
1898 break;
1899 default:
1900 break;
1901 }
1902 }
1903
Liam Girdwood7987a112011-01-31 19:52:42 +00001904 switch (dapm->bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001905 case SND_SOC_BIAS_ON:
1906 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001907 break;
Mark Brown0be98982008-05-19 12:31:28 +02001908 case SND_SOC_BIAS_PREPARE:
1909 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001910 break;
Mark Brown0be98982008-05-19 12:31:28 +02001911 case SND_SOC_BIAS_STANDBY:
1912 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001913 break;
Mark Brown0be98982008-05-19 12:31:28 +02001914 case SND_SOC_BIAS_OFF:
1915 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001916 break;
1917 }
1918 count += sprintf(buf + count, "PM State: %s\n", state);
1919
1920 return count;
1921}
1922
Liam Girdwood7987a112011-01-31 19:52:42 +00001923/* show dapm widget status in sys fs */
1924static ssize_t dapm_widget_show(struct device *dev,
1925 struct device_attribute *attr, char *buf)
1926{
1927 struct snd_soc_pcm_runtime *rtd =
1928 container_of(dev, struct snd_soc_pcm_runtime, dev);
1929 struct snd_soc_codec *codec = rtd->codec;
1930 struct snd_soc_platform *platform = rtd->platform;
1931 ssize_t count = 0;
1932
1933 count += widget_show(&codec->dapm, codec->name, buf, count);
1934 count += widget_show(&platform->dapm, platform->name, buf, count);
1935 return count;
1936}
1937
Richard Purdie2b97eab2006-10-06 18:32:18 +02001938static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1939
1940int snd_soc_dapm_sys_add(struct device *dev)
1941{
Troy Kisky12ef1932008-10-13 17:42:14 -07001942 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001943}
1944
1945static void snd_soc_dapm_sys_remove(struct device *dev)
1946{
Mark Brownaef90842009-05-16 17:53:16 +01001947 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001948}
1949
1950/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001951static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001952{
1953 struct snd_soc_dapm_widget *w, *next_w;
1954 struct snd_soc_dapm_path *p, *next_p;
1955
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001956 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1957 if (w->dapm != dapm)
1958 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001959 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001960 /*
1961 * remove source and sink paths associated to this widget.
1962 * While removing the path, remove reference to it from both
1963 * source and sink widgets so that path is removed only once.
1964 */
1965 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1966 list_del(&p->list_sink);
1967 list_del(&p->list_source);
1968 list_del(&p->list);
1969 kfree(p->long_name);
1970 kfree(p);
1971 }
1972 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1973 list_del(&p->list_sink);
1974 list_del(&p->list_source);
1975 list_del(&p->list);
1976 kfree(p->long_name);
1977 kfree(p);
1978 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001979 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001980 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001981 kfree(w);
1982 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001983}
1984
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001985static struct snd_soc_dapm_widget *dapm_find_widget(
1986 struct snd_soc_dapm_context *dapm, const char *pin,
1987 bool search_other_contexts)
1988{
1989 struct snd_soc_dapm_widget *w;
1990 struct snd_soc_dapm_widget *fallback = NULL;
1991
1992 list_for_each_entry(w, &dapm->card->widgets, list) {
1993 if (!strcmp(w->name, pin)) {
1994 if (w->dapm == dapm)
1995 return w;
1996 else
1997 fallback = w;
1998 }
1999 }
2000
2001 if (search_other_contexts)
2002 return fallback;
2003
2004 return NULL;
2005}
2006
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002007static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002008 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002009{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002010 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002011
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002012 if (!w) {
2013 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2014 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002015 }
2016
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002017 w->connected = status;
2018 if (status == 0)
2019 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002020
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002021 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002022}
2023
Richard Purdie2b97eab2006-10-06 18:32:18 +02002024/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002025 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002026 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002027 *
2028 * Walks all dapm audio paths and powers widgets according to their
2029 * stream or path usage.
2030 *
2031 * Returns 0 for success.
2032 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002033int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002034{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002035 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002036}
Liam Girdwooda5302182008-07-07 13:35:17 +01002037EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002038
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002039static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002040 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002041{
2042 struct snd_soc_dapm_path *path;
2043 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002044 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002045 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002046 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002047 const char *source;
2048 char prefixed_sink[80];
2049 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002050 int ret = 0;
2051
Mark Brown88e8b9a2011-03-02 18:18:24 +00002052 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002053 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2054 dapm->codec->name_prefix, route->sink);
2055 sink = prefixed_sink;
2056 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2057 dapm->codec->name_prefix, route->source);
2058 source = prefixed_source;
2059 } else {
2060 sink = route->sink;
2061 source = route->source;
2062 }
2063
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002064 /*
2065 * find src and dest widgets over all widgets but favor a widget from
2066 * current DAPM context
2067 */
2068 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002069 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002070 wtsink = w;
2071 if (w->dapm == dapm)
2072 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002073 continue;
2074 }
2075 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002076 wtsource = w;
2077 if (w->dapm == dapm)
2078 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002079 }
2080 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002081 /* use widget from another DAPM context if not found from this */
2082 if (!wsink)
2083 wsink = wtsink;
2084 if (!wsource)
2085 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002086
2087 if (wsource == NULL || wsink == NULL)
2088 return -ENODEV;
2089
2090 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2091 if (!path)
2092 return -ENOMEM;
2093
2094 path->source = wsource;
2095 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002096 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002097 INIT_LIST_HEAD(&path->list);
2098 INIT_LIST_HEAD(&path->list_source);
2099 INIT_LIST_HEAD(&path->list_sink);
2100
2101 /* check for external widgets */
2102 if (wsink->id == snd_soc_dapm_input) {
2103 if (wsource->id == snd_soc_dapm_micbias ||
2104 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002105 wsource->id == snd_soc_dapm_line ||
2106 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002107 wsink->ext = 1;
2108 }
2109 if (wsource->id == snd_soc_dapm_output) {
2110 if (wsink->id == snd_soc_dapm_spk ||
2111 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002112 wsink->id == snd_soc_dapm_line ||
2113 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002114 wsource->ext = 1;
2115 }
2116
2117 /* connect static paths */
2118 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002119 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002120 list_add(&path->list_sink, &wsink->sources);
2121 list_add(&path->list_source, &wsource->sinks);
2122 path->connect = 1;
2123 return 0;
2124 }
2125
2126 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002127 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002128 case snd_soc_dapm_adc:
2129 case snd_soc_dapm_dac:
2130 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002131 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002132 case snd_soc_dapm_input:
2133 case snd_soc_dapm_output:
2134 case snd_soc_dapm_micbias:
2135 case snd_soc_dapm_vmid:
2136 case snd_soc_dapm_pre:
2137 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002138 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002139 case snd_soc_dapm_aif_in:
2140 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002141 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002142 list_add(&path->list_sink, &wsink->sources);
2143 list_add(&path->list_source, &wsource->sinks);
2144 path->connect = 1;
2145 return 0;
2146 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002147 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002148 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002149 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002150 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002151 if (ret != 0)
2152 goto err;
2153 break;
2154 case snd_soc_dapm_switch:
2155 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002156 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002157 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002158 if (ret != 0)
2159 goto err;
2160 break;
2161 case snd_soc_dapm_hp:
2162 case snd_soc_dapm_mic:
2163 case snd_soc_dapm_line:
2164 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002165 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002166 list_add(&path->list_sink, &wsink->sources);
2167 list_add(&path->list_source, &wsource->sinks);
2168 path->connect = 0;
2169 return 0;
2170 }
2171 return 0;
2172
2173err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002174 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2175 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002176 kfree(path);
2177 return ret;
2178}
Mark Brown105f1c22008-05-13 14:52:19 +02002179
2180/**
Mark Brown105f1c22008-05-13 14:52:19 +02002181 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002182 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002183 * @route: audio routes
2184 * @num: number of routes
2185 *
2186 * Connects 2 dapm widgets together via a named audio path. The sink is
2187 * the widget receiving the audio signal, whilst the source is the sender
2188 * of the audio signal.
2189 *
2190 * Returns 0 for success else error. On error all resources can be freed
2191 * with a call to snd_soc_card_free().
2192 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002193int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002194 const struct snd_soc_dapm_route *route, int num)
2195{
2196 int i, ret;
2197
2198 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002199 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02002200 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002201 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2202 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02002203 return ret;
2204 }
2205 route++;
2206 }
2207
2208 return 0;
2209}
2210EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2211
2212/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002213 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002214 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002215 *
2216 * Checks the codec for any new dapm widgets and creates them if found.
2217 *
2218 * Returns 0 for success.
2219 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002220int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002221{
2222 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002223 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002224
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002225 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002226 {
2227 if (w->new)
2228 continue;
2229
Stephen Warrenfad59882011-04-28 17:37:59 -06002230 if (w->num_kcontrols) {
2231 w->kcontrols = kzalloc(w->num_kcontrols *
2232 sizeof(struct snd_kcontrol *),
2233 GFP_KERNEL);
2234 if (!w->kcontrols)
2235 return -ENOMEM;
2236 }
2237
Richard Purdie2b97eab2006-10-06 18:32:18 +02002238 switch(w->id) {
2239 case snd_soc_dapm_switch:
2240 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002241 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01002242 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002243 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244 break;
2245 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002246 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002247 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01002248 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002249 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002250 break;
2251 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01002252 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01002253 w->power_check = dapm_adc_check_power;
2254 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002255 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01002256 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01002257 w->power_check = dapm_dac_check_power;
2258 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002259 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002260 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01002261 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002262 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002263 break;
2264 case snd_soc_dapm_input:
2265 case snd_soc_dapm_output:
2266 case snd_soc_dapm_micbias:
2267 case snd_soc_dapm_spk:
2268 case snd_soc_dapm_hp:
2269 case snd_soc_dapm_mic:
2270 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01002271 w->power_check = dapm_generic_check_power;
2272 break;
Mark Brown246d0a12009-04-22 18:24:55 +01002273 case snd_soc_dapm_supply:
2274 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275 case snd_soc_dapm_vmid:
2276 case snd_soc_dapm_pre:
2277 case snd_soc_dapm_post:
2278 break;
2279 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002280
2281 /* Read the initial power state from the device */
2282 if (w->reg >= 0) {
Liam Girdwood35ca6602011-01-28 17:45:35 +00002283 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002284 val &= 1 << w->shift;
2285 if (w->invert)
2286 val = !val;
2287
2288 if (val)
2289 w->power = 1;
2290 }
2291
Richard Purdie2b97eab2006-10-06 18:32:18 +02002292 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002293
2294 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002295 }
2296
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002297 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002298 return 0;
2299}
2300EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2301
Liam Girdwood7038dfd92011-02-01 20:58:15 +00002302const char *snd_soc_dapm_get_aif(struct snd_soc_dapm_context *dapm,
2303 const char *stream_name, enum snd_soc_dapm_type type)
2304{
2305 struct snd_soc_dapm_widget *w;
2306
Liam Girdwoodab1058a2011-01-31 20:33:07 +00002307 list_for_each_entry(w, &dapm->card->widgets, list) {
Liam Girdwood7038dfd92011-02-01 20:58:15 +00002308
2309 if (!w->sname)
2310 continue;
2311
2312 if (w->id == type && strstr(w->sname, stream_name))
2313 return w->name;
Liam Girdwoodab1058a2011-01-31 20:33:07 +00002314
Liam Girdwood7038dfd92011-02-01 20:58:15 +00002315 }
2316 return NULL;
2317}
2318EXPORT_SYMBOL_GPL(snd_soc_dapm_get_aif);
2319
Richard Purdie2b97eab2006-10-06 18:32:18 +02002320/**
2321 * snd_soc_dapm_get_volsw - dapm mixer get callback
2322 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002323 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002324 *
2325 * Callback to get the value of a dapm mixer control.
2326 *
2327 * Returns 0 for success.
2328 */
2329int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2330 struct snd_ctl_elem_value *ucontrol)
2331{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002332 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2333 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002334 struct soc_mixer_control *mc =
2335 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002336 unsigned int reg = mc->reg;
2337 unsigned int shift = mc->shift;
2338 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002339 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002340 unsigned int invert = mc->invert;
2341 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002342
Richard Purdie2b97eab2006-10-06 18:32:18 +02002343 ucontrol->value.integer.value[0] =
2344 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2345 if (shift != rshift)
2346 ucontrol->value.integer.value[1] =
2347 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2348 if (invert) {
2349 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002350 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002351 if (shift != rshift)
2352 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002353 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002354 }
2355
2356 return 0;
2357}
2358EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2359
2360/**
2361 * snd_soc_dapm_put_volsw - dapm mixer set callback
2362 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002363 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002364 *
2365 * Callback to set the value of a dapm mixer control.
2366 *
2367 * Returns 0 for success.
2368 */
2369int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2370 struct snd_ctl_elem_value *ucontrol)
2371{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002372 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2373 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2374 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002375 struct soc_mixer_control *mc =
2376 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002377 unsigned int reg = mc->reg;
2378 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002379 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002380 unsigned int mask = (1 << fls(max)) - 1;
2381 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002382 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002383 int connect, change;
2384 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002385 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002386
2387 val = (ucontrol->value.integer.value[0] & mask);
2388
2389 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002390 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002391 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002392 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002393
Stephen Warrenfafd2172011-04-28 17:38:00 -06002394 if (val)
2395 /* new connection */
2396 connect = invert ? 0 : 1;
2397 else
2398 /* old connection must be powered down */
2399 connect = invert ? 1 : 0;
2400
2401 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002402
Stephen Warrene9cf7042011-01-27 14:54:05 -07002403 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002404 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002405 for (wi = 0; wi < wlist->num_widgets; wi++) {
2406 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002407
Stephen Warrenfafd2172011-04-28 17:38:00 -06002408 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002409
Stephen Warrenfafd2172011-04-28 17:38:00 -06002410 update.kcontrol = kcontrol;
2411 update.widget = widget;
2412 update.reg = reg;
2413 update.mask = mask;
2414 update.val = val;
2415 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002416
Liam Girdwoode002c982011-02-08 11:45:20 +00002417 snd_soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002418
2419 widget->dapm->update = NULL;
2420 }
Mark Brown283375c2009-12-07 18:09:03 +00002421 }
2422
Stephen Warrenfafd2172011-04-28 17:38:00 -06002423 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002424 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002425}
2426EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2427
2428/**
2429 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2430 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002431 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002432 *
2433 * Callback to get the value of a dapm enumerated double mixer control.
2434 *
2435 * Returns 0 for success.
2436 */
2437int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2438 struct snd_ctl_elem_value *ucontrol)
2439{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002440 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2441 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002442 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002443 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002444
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002445 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002446 ;
2447 val = snd_soc_read(widget->codec, e->reg);
2448 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2449 if (e->shift_l != e->shift_r)
2450 ucontrol->value.enumerated.item[1] =
2451 (val >> e->shift_r) & (bitmask - 1);
2452
2453 return 0;
2454}
2455EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2456
2457/**
2458 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2459 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002460 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002461 *
2462 * Callback to set the value of a dapm enumerated double mixer control.
2463 *
2464 * Returns 0 for success.
2465 */
2466int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2467 struct snd_ctl_elem_value *ucontrol)
2468{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002469 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2470 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2471 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002472 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002473 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002474 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002475 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002476 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002477
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002478 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002479 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002480 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002481 return -EINVAL;
2482 mux = ucontrol->value.enumerated.item[0];
2483 val = mux << e->shift_l;
2484 mask = (bitmask - 1) << e->shift_l;
2485 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002486 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002487 return -EINVAL;
2488 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2489 mask |= (bitmask - 1) << e->shift_r;
2490 }
2491
Stephen Warrenfafd2172011-04-28 17:38:00 -06002492 mutex_lock(&codec->mutex);
2493
Mark Brown3a655772009-10-05 17:23:30 +01002494 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002495 if (change) {
2496 for (wi = 0; wi < wlist->num_widgets; wi++) {
2497 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002498
Stephen Warrenfafd2172011-04-28 17:38:00 -06002499 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002500
Stephen Warrenfafd2172011-04-28 17:38:00 -06002501 update.kcontrol = kcontrol;
2502 update.widget = widget;
2503 update.reg = e->reg;
2504 update.mask = mask;
2505 update.val = val;
2506 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002507
Liam Girdwoode002c982011-02-08 11:45:20 +00002508 snd_soc_dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002509
Stephen Warrenfafd2172011-04-28 17:38:00 -06002510 widget->dapm->update = NULL;
2511 }
2512 }
2513
2514 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002515 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002516}
2517EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2518
2519/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002520 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2521 * @kcontrol: mixer control
2522 * @ucontrol: control element information
2523 *
2524 * Returns 0 for success.
2525 */
2526int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2527 struct snd_ctl_elem_value *ucontrol)
2528{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002529 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2530 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002531
2532 ucontrol->value.enumerated.item[0] = widget->value;
2533
2534 return 0;
2535}
2536EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2537
2538/**
2539 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2540 * @kcontrol: mixer control
2541 * @ucontrol: control element information
2542 *
2543 * Returns 0 for success.
2544 */
2545int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2546 struct snd_ctl_elem_value *ucontrol)
2547{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002548 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2549 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2550 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002551 struct soc_enum *e =
2552 (struct soc_enum *)kcontrol->private_value;
2553 int change;
2554 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002555 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002556
2557 if (ucontrol->value.enumerated.item[0] >= e->max)
2558 return -EINVAL;
2559
Stephen Warrenfafd2172011-04-28 17:38:00 -06002560 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002561
2562 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002563 if (change) {
2564 for (wi = 0; wi < wlist->num_widgets; wi++) {
2565 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002566
Stephen Warrenfafd2172011-04-28 17:38:00 -06002567 widget->value = ucontrol->value.enumerated.item[0];
2568
Liam Girdwoode002c982011-02-08 11:45:20 +00002569 snd_soc_dapm_mux_update_power(widget, kcontrol, change,
2570 widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002571 }
2572 }
2573
2574 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002575 return ret;
2576}
2577EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2578
2579/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002580 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2581 * callback
2582 * @kcontrol: mixer control
2583 * @ucontrol: control element information
2584 *
2585 * Callback to get the value of a dapm semi enumerated double mixer control.
2586 *
2587 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2588 * used for handling bitfield coded enumeration for example.
2589 *
2590 * Returns 0 for success.
2591 */
2592int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2593 struct snd_ctl_elem_value *ucontrol)
2594{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002595 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2596 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002597 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002598 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002599
2600 reg_val = snd_soc_read(widget->codec, e->reg);
2601 val = (reg_val >> e->shift_l) & e->mask;
2602 for (mux = 0; mux < e->max; mux++) {
2603 if (val == e->values[mux])
2604 break;
2605 }
2606 ucontrol->value.enumerated.item[0] = mux;
2607 if (e->shift_l != e->shift_r) {
2608 val = (reg_val >> e->shift_r) & e->mask;
2609 for (mux = 0; mux < e->max; mux++) {
2610 if (val == e->values[mux])
2611 break;
2612 }
2613 ucontrol->value.enumerated.item[1] = mux;
2614 }
2615
2616 return 0;
2617}
2618EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2619
2620/**
2621 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2622 * callback
2623 * @kcontrol: mixer control
2624 * @ucontrol: control element information
2625 *
2626 * Callback to set the value of a dapm semi enumerated double mixer control.
2627 *
2628 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2629 * used for handling bitfield coded enumeration for example.
2630 *
2631 * Returns 0 for success.
2632 */
2633int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2634 struct snd_ctl_elem_value *ucontrol)
2635{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002636 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2637 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2638 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002639 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002640 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002641 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002642 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002643 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002644
2645 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2646 return -EINVAL;
2647 mux = ucontrol->value.enumerated.item[0];
2648 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2649 mask = e->mask << e->shift_l;
2650 if (e->shift_l != e->shift_r) {
2651 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2652 return -EINVAL;
2653 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2654 mask |= e->mask << e->shift_r;
2655 }
2656
Stephen Warrenfafd2172011-04-28 17:38:00 -06002657 mutex_lock(&codec->mutex);
2658
Mark Brown3a655772009-10-05 17:23:30 +01002659 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002660 if (change) {
2661 for (wi = 0; wi < wlist->num_widgets; wi++) {
2662 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002663
Stephen Warrenfafd2172011-04-28 17:38:00 -06002664 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002665
Stephen Warrenfafd2172011-04-28 17:38:00 -06002666 update.kcontrol = kcontrol;
2667 update.widget = widget;
2668 update.reg = e->reg;
2669 update.mask = mask;
2670 update.val = val;
2671 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002672
Liam Girdwoode002c982011-02-08 11:45:20 +00002673 snd_soc_dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002674
Stephen Warrenfafd2172011-04-28 17:38:00 -06002675 widget->dapm->update = NULL;
2676 }
2677 }
2678
2679 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002680 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002681}
2682EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2683
2684/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002685 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2686 *
2687 * @kcontrol: mixer control
2688 * @uinfo: control element information
2689 *
2690 * Callback to provide information about a pin switch control.
2691 */
2692int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2693 struct snd_ctl_elem_info *uinfo)
2694{
2695 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2696 uinfo->count = 1;
2697 uinfo->value.integer.min = 0;
2698 uinfo->value.integer.max = 1;
2699
2700 return 0;
2701}
2702EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2703
2704/**
2705 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2706 *
2707 * @kcontrol: mixer control
2708 * @ucontrol: Value
2709 */
2710int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2711 struct snd_ctl_elem_value *ucontrol)
2712{
2713 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2714 const char *pin = (const char *)kcontrol->private_value;
2715
2716 mutex_lock(&codec->mutex);
2717
2718 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002719 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002720
2721 mutex_unlock(&codec->mutex);
2722
2723 return 0;
2724}
2725EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2726
2727/**
2728 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2729 *
2730 * @kcontrol: mixer control
2731 * @ucontrol: Value
2732 */
2733int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2734 struct snd_ctl_elem_value *ucontrol)
2735{
2736 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2737 const char *pin = (const char *)kcontrol->private_value;
2738
2739 mutex_lock(&codec->mutex);
2740
2741 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002742 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002743 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002744 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002745
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002746 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002747
2748 mutex_unlock(&codec->mutex);
2749
2750 return 0;
2751}
2752EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2753
2754/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002755 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002756 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002757 * @widget: widget template
2758 *
2759 * Creates a new dapm control based upon the template.
2760 *
2761 * Returns 0 for success else error.
2762 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002763int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002764 const struct snd_soc_dapm_widget *widget)
2765{
2766 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002767 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002768
2769 if ((w = dapm_cnew_widget(widget)) == NULL)
2770 return -ENOMEM;
2771
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002772 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002773 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002774 name_len += 1 + strlen(dapm->codec->name_prefix);
2775 w->name = kmalloc(name_len, GFP_KERNEL);
2776 if (w->name == NULL) {
2777 kfree(w);
2778 return -ENOMEM;
2779 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002780 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002781 snprintf(w->name, name_len, "%s %s",
2782 dapm->codec->name_prefix, widget->name);
2783 else
2784 snprintf(w->name, name_len, "%s", widget->name);
2785
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002786 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002787 w->dapm = dapm;
2788 w->codec = dapm->codec;
Liam Girdwood7987a112011-01-31 19:52:42 +00002789 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002790 INIT_LIST_HEAD(&w->sources);
2791 INIT_LIST_HEAD(&w->sinks);
2792 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002793 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002794
2795 /* machine layer set ups unconnected pins and insertions */
2796 w->connected = 1;
2797 return 0;
2798}
2799EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2800
2801/**
Mark Brown4ba13272008-05-13 14:51:19 +02002802 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002803 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002804 * @widget: widget array
2805 * @num: number of widgets
2806 *
2807 * Creates new DAPM controls based upon the templates.
2808 *
2809 * Returns 0 for success else error.
2810 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002811int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002812 const struct snd_soc_dapm_widget *widget,
2813 int num)
2814{
2815 int i, ret;
2816
2817 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002818 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002819 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002820 dev_err(dapm->dev,
2821 "ASoC: Failed to create DAPM control %s: %d\n",
2822 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002823 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002824 }
Mark Brown4ba13272008-05-13 14:51:19 +02002825 widget++;
2826 }
2827 return 0;
2828}
2829EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2830
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002831static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002832 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002833{
2834 struct snd_soc_dapm_widget *w;
2835
Liam Girdwood13e13a32011-01-31 21:30:52 +00002836 if (!dapm)
2837 return;
2838
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002839 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002840 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002841 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002842 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002843 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2844 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002845 if (strstr(w->sname, stream)) {
2846 switch(event) {
2847 case SND_SOC_DAPM_STREAM_START:
2848 w->active = 1;
2849 break;
2850 case SND_SOC_DAPM_STREAM_STOP:
2851 w->active = 0;
2852 break;
2853 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002854 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002855 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002856 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2857 break;
2858 }
2859 }
2860 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002861
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002862 dapm_power_widgets(dapm, event);
Liam Girdwood670ff442011-03-30 23:27:27 +01002863 /* do we need to notify any clients that DAPM stream is complete */
2864 if (dapm->stream_event)
2865 dapm->stream_event(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002866}
2867
2868/**
2869 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2870 * @rtd: PCM runtime data
2871 * @stream: stream name
2872 * @event: stream event
2873 *
2874 * Sends a stream event to the dapm core. The core then makes any
2875 * necessary widget power changes.
2876 *
2877 * Returns 0 for success else error.
2878 */
2879int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2880 const char *stream, int event)
2881{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002882 if (stream == NULL)
2883 return 0;
Liam Girdwoodecc1a0d2011-02-16 16:24:17 +00002884
Liam Girdwood670ff442011-03-30 23:27:27 +01002885 mutex_lock(&rtd->card->dapm_mutex);
Patrick Lai7f100832011-09-02 11:41:45 -07002886 mutex_lock(&rtd->codec->mutex);
Liam Girdwood670ff442011-03-30 23:27:27 +01002887 soc_dapm_stream_event(&rtd->platform->dapm, stream, event);
2888 soc_dapm_stream_event(&rtd->codec->dapm, stream, event);
Patrick Lai7f100832011-09-02 11:41:45 -07002889 mutex_unlock(&rtd->codec->mutex);
Liam Girdwood670ff442011-03-30 23:27:27 +01002890 mutex_unlock(&rtd->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002891 return 0;
2892}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002893
2894/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002895 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002896 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002897 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002898 *
Mark Brown74b8f952009-06-06 11:26:15 +01002899 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002900 * a valid audio route and active audio stream.
2901 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2902 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002903 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002904int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002905{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002906 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002907}
Liam Girdwooda5302182008-07-07 13:35:17 +01002908EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002909
2910/**
Mark Brownda341832010-03-15 19:23:37 +00002911 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002912 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002913 * @pin: pin name
2914 *
2915 * Enables input/output pin regardless of any other state. This is
2916 * intended for use with microphone bias supplies used in microphone
2917 * jack detection.
2918 *
2919 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2920 * do any widget power switching.
2921 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002922int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2923 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002924{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002925 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002926
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002927 if (!w) {
2928 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2929 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002930 }
2931
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002932 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2933 w->connected = 1;
2934 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002935
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002936 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002937}
2938EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2939
2940/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002941 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002942 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002943 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002944 *
Mark Brown74b8f952009-06-06 11:26:15 +01002945 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002946 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2947 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002948 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002949int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2950 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002951{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002952 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002953}
2954EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2955
2956/**
Mark Brown5817b522008-09-24 11:23:11 +01002957 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002958 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002959 * @pin: pin name
2960 *
2961 * Marks the specified pin as being not connected, disabling it along
2962 * any parent or child widgets. At present this is identical to
2963 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2964 * additional things such as disabling controls which only affect
2965 * paths through the pin.
2966 *
2967 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2968 * do any widget power switching.
2969 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002970int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002971{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002972 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002973}
2974EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2975
2976/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002977 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002978 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002979 * @pin: audio signal pin endpoint (or start point)
2980 *
2981 * Get audio pin status - connected or disconnected.
2982 *
2983 * Returns 1 for connected otherwise 0.
2984 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002985int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2986 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002987{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002988 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002989
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002990 if (w)
2991 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002992
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002993 return 0;
2994}
Liam Girdwooda5302182008-07-07 13:35:17 +01002995EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002996
2997/**
Mark Brown1547aba2010-05-07 21:11:40 +01002998 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002999 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003000 * @pin: audio signal pin endpoint (or start point)
3001 *
3002 * Mark the given endpoint or pin as ignoring suspend. When the
3003 * system is disabled a path between two endpoints flagged as ignoring
3004 * suspend will not be disabled. The path must already be enabled via
3005 * normal means at suspend time, it will not be turned on if it was not
3006 * already enabled.
3007 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003008int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3009 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003010{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003011 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003012
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003013 if (!w) {
3014 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3015 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003016 }
3017
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003018 w->ignore_suspend = 1;
3019
3020 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003021}
3022EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3023
3024/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003025 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003026 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02003027 *
3028 * Free all dapm widgets and resources.
3029 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003030void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003031{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003032 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003033 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003034 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003035 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003036}
3037EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3038
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003039static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003040{
Mark Brown51737472009-06-22 13:16:51 +01003041 struct snd_soc_dapm_widget *w;
3042 LIST_HEAD(down_list);
3043 int powerdown = 0;
3044
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003045 list_for_each_entry(w, &dapm->card->widgets, list) {
3046 if (w->dapm != dapm)
3047 continue;
Mark Brown51737472009-06-22 13:16:51 +01003048 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003049 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003050 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003051 powerdown = 1;
3052 }
3053 }
3054
3055 /* If there were no widgets to power down we're already in
3056 * standby.
3057 */
3058 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08003059 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003060 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08003061 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003062 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003063}
Mark Brown51737472009-06-22 13:16:51 +01003064
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003065/*
3066 * snd_soc_dapm_shutdown - callback for system shutdown
3067 */
3068void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3069{
3070 struct snd_soc_codec *codec;
Liam Girdwood7987a112011-01-31 19:52:42 +00003071 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003072
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003073 list_for_each_entry(codec, &card->codec_dev_list, list) {
3074 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08003075 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003076 }
Liam Girdwood7987a112011-01-31 19:52:42 +00003077
3078 list_for_each_entry(platform, &card->platform_dev_list, list) {
3079 soc_dapm_shutdown_codec(&platform->dapm);
3080 snd_soc_dapm_set_bias_level(&platform->dapm, SND_SOC_BIAS_OFF);
3081 }
Mark Brown51737472009-06-22 13:16:51 +01003082}
3083
Richard Purdie2b97eab2006-10-06 18:32:18 +02003084/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003085MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003086MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003087MODULE_LICENSE("GPL v2");