blob: 265df9ebe4ce6dfb7e05d5b8eac163a203bfcd9f [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.
Bharath Ramachandramurthy9c79f132011-11-28 11:18:57 -08005 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07006 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070011 * Free Software Foundation; only version 2 of the License.
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020013 * Features:
14 * o Changes power status of internal codec blocks depending on the
15 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010016 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020017 * o Platform power domain - can support external components i.e. amps and
18 * mic/meadphone insertion events.
19 * o Automatic Mic Bias support
20 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020022 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020023 * device reopen.
24 *
25 * Todo:
26 * o DAPM power change sequencing - allow for configurable per
27 * codec sequences.
28 * o Support for analogue bias optimisation.
29 * o Support for reduced codec oversampling rates.
30 * o Support for reduced codec bias currents.
31 */
32
Liam Girdwood13e13a32011-01-31 21:30:52 +000033#undef DEBUG
Liam Girdwood7987a112011-01-31 19:52:42 +000034
Richard Purdie2b97eab2006-10-06 18:32:18 +020035#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080038#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020039#include <linux/delay.h>
40#include <linux/pm.h>
41#include <linux/bitops.h>
42#include <linux/platform_device.h>
43#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020044#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020046#include <sound/core.h>
47#include <sound/pcm.h>
48#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020049#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020050#include <sound/initval.h>
51
Mark Brown84e90932010-11-04 00:07:02 -040052#include <trace/events/asoc.h>
53
Liam Girdwoodab1058a2011-01-31 20:33:07 +000054#define PATH_MAX_HOPS 16
55
Liam Girdwoode002c982011-02-08 11:45:20 +000056int soc_dsp_runtime_update(struct snd_soc_dapm_widget *);
57
Richard Purdie2b97eab2006-10-06 18:32:18 +020058/* dapm power sequences - make this per codec in the future */
59static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010060 [snd_soc_dapm_pre] = 0,
61 [snd_soc_dapm_supply] = 1,
62 [snd_soc_dapm_micbias] = 2,
Kiran Kandid8cf5212012-03-02 15:34:53 -080063 [snd_soc_dapm_adc] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010064 [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,
Bharath Ramachandramurthy9c79f132011-11-28 11:18:57 -080072 [snd_soc_dapm_aif_in] = 8,
73 [snd_soc_dapm_aif_out] = 8,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060074 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010075 [snd_soc_dapm_hp] = 10,
76 [snd_soc_dapm_spk] = 10,
77 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020078};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000079
Richard Purdie2b97eab2006-10-06 18:32:18 +020080static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010081 [snd_soc_dapm_pre] = 0,
Bharath Ramachandramurthy9c79f132011-11-28 11:18:57 -080082 [snd_soc_dapm_aif_in] = 1,
83 [snd_soc_dapm_aif_out] = 1,
Mark Brown38357ab2009-06-06 19:03:23 +010084 [snd_soc_dapm_adc] = 1,
85 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010086 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060087 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010088 [snd_soc_dapm_pga] = 4,
89 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010090 [snd_soc_dapm_mixer] = 5,
91 [snd_soc_dapm_dac] = 6,
92 [snd_soc_dapm_mic] = 7,
93 [snd_soc_dapm_micbias] = 8,
94 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000095 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010096 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010097 [snd_soc_dapm_supply] = 11,
98 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020099};
100
Troy Kisky12ef1932008-10-13 17:42:14 -0700101static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100102{
103 if (pop_time)
104 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
105}
106
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200107static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100108{
109 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200110 char *buf;
111
112 if (!pop_time)
113 return;
114
115 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
116 if (buf == NULL)
117 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100118
119 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200120 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100121 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100122 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200123
124 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100125}
126
Richard Purdie2b97eab2006-10-06 18:32:18 +0200127/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100128static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200129 const struct snd_soc_dapm_widget *_widget)
130{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100131 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200132}
133
Liam Girdwood7987a112011-01-31 19:52:42 +0000134static inline struct snd_card *dapm_get_card(struct snd_soc_dapm_context *dapm)
135{
136 if (dapm->codec)
137 return dapm->codec->card->snd_card;
138 else if (dapm->platform)
139 return dapm->platform->card->snd_card;
140 else
141 BUG();
142}
143
144static inline struct snd_soc_card *dapm_get_soc_card(
145 struct snd_soc_dapm_context *dapm)
146{
147 if (dapm->codec)
148 return dapm->codec->card;
149 else if (dapm->platform)
150 return dapm->platform->card;
151 else
152 BUG();
153}
154
Liam Girdwood35ca6602011-01-28 17:45:35 +0000155static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
156{
157 if (w->codec)
158 return snd_soc_read(w->codec, reg);
Liam Girdwood7987a112011-01-31 19:52:42 +0000159 else if (w->platform)
160 return snd_soc_platform_read(w->platform, reg);
Liam Girdwood35ca6602011-01-28 17:45:35 +0000161 return 0;
162}
163
164static int soc_widget_write(struct snd_soc_dapm_widget *w,int reg, int val)
165{
166 if (w->codec)
167 return snd_soc_write(w->codec, reg, val);
Liam Girdwood7987a112011-01-31 19:52:42 +0000168 else if (w->platform)
169 return snd_soc_platform_write(w->platform, reg, val);
Liam Girdwood35ca6602011-01-28 17:45:35 +0000170 return 0;
171}
172
173int soc_widget_update_bits(struct snd_soc_dapm_widget *w, unsigned short reg,
174 unsigned int mask, unsigned int value)
175{
176 int change;
177 unsigned int old, new;
178
179 old = soc_widget_read(w, reg);
180 new = (old & ~mask) | value;
181 change = old != new;
182 if (change)
183 soc_widget_write(w, reg, new);
184
185 return change;
186}
187
188int soc_widget_test_bits(struct snd_soc_dapm_widget *w, unsigned short reg,
189 unsigned int mask, unsigned int value)
190{
191 int change;
192 unsigned int old, new;
193
194 old = soc_widget_read(w, reg);
195 new = (old & ~mask) | value;
196 change = old != new;
197
198 return change;
199}
200
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000201/* reset 'walked' bit for each dapm path */
202static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
203{
204 struct snd_soc_dapm_path *p;
205
206 list_for_each_entry(p, &dapm->card->paths, list)
207 p->walked = 0;
208}
209
210static void dapm_clear_paths(struct snd_soc_dapm_context *dapm)
211{
212 struct snd_soc_dapm_path *p;
213 struct snd_soc_dapm_widget *w;
214 struct list_head *l;
215
Liam Girdwoode002c982011-02-08 11:45:20 +0000216 list_for_each(l, &dapm->card->paths) {
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000217 p = list_entry(l, struct snd_soc_dapm_path, list);
218 p->length = 0;
219 }
220 list_for_each(l, &dapm->card->widgets) {
221 w = list_entry(l, struct snd_soc_dapm_widget, list);
222 w->hops = 0;
223 }
224 dapm_clear_walk(dapm);
225}
226
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100227static int dapm_add_unique_widget(struct snd_soc_dapm_context *dapm,
228 struct snd_soc_dapm_widget_list **list, struct snd_soc_dapm_widget *w)
229{
230 struct snd_soc_dapm_widget_list *wlist;
231 int wlistsize, wlistentries, i;
232
233 /* is the list empty ? */
234 if (*list == NULL) {
235
236 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
237 sizeof(struct snd_soc_dapm_widget *);
238 *list = kzalloc(wlistsize, GFP_KERNEL);
239 if (*list == NULL) {
240 dev_err(dapm->dev, "can't allocate widget list for %s\n", w->name);
241 return -ENOMEM;
242 }
243 } else {
244
245 wlist = *list;
246 /* is this widget already in the list */
247 for (i = 0; i < wlist->num_widgets; i++) {
248 if (wlist->widgets[i] == w)
249 return 0;
250 }
251
252 wlistentries = wlist->num_widgets + 1;
253 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
254 wlistentries * sizeof(struct snd_soc_dapm_widget *);
255 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
256 if (*list == NULL) {
257 dev_err(dapm->dev, "can't allocate widget list for %s\n", w->name);
258 return -ENOMEM;
259 }
260 }
261 wlist = *list;
262
263 /* insert the widget */
264 dev_dbg(dapm->dev, "added %s in widget list pos %d\n",
265 w->name, wlist->num_widgets);
266 wlist->widgets[wlist->num_widgets] = w;
267 wlist->num_widgets++;
268 return 1;
269}
270
271static int is_output_widget_ep(struct snd_soc_dapm_widget *widget)
272{
273 switch (widget->id) {
274 case snd_soc_dapm_adc:
275 case snd_soc_dapm_aif_out:
276 return 1;
277 case snd_soc_dapm_output:
278 if (widget->connected && !widget->ext)
279 return 1;
280 else
281 return 0;
282 case snd_soc_dapm_hp:
283 case snd_soc_dapm_spk:
284 case snd_soc_dapm_line:
285 return !list_empty(&widget->sources);
286 default:
287 return 0;
288 }
289}
290
291static int is_input_widget_ep(struct snd_soc_dapm_widget *widget)
292{
293 switch (widget->id) {
294 case snd_soc_dapm_dac:
295 case snd_soc_dapm_aif_in:
296 return 1;
297 case snd_soc_dapm_input:
298 if (widget->connected && !widget->ext)
299 return 1;
300 else
301 return 0;
302 case snd_soc_dapm_mic:
303 return !list_empty(&widget->sources);
304 default:
305 return 0;
306 }
307}
308
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000309/*
310 * find all the paths between source and sink
311 */
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100312static int dapm_find_playback_paths(struct snd_soc_dapm_context *dapm,
313 struct snd_soc_dapm_widget *root,
314 struct snd_soc_dapm_widget_list **list, int hops)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000315{
316 struct list_head *lp;
317 struct snd_soc_dapm_path *path;
318 int dist = 0;
319
320 if (hops > PATH_MAX_HOPS)
321 return 0;
322
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100323 if (is_output_widget_ep(root) && hops != 1) {
324 dev_dbg(dapm->dev," ! %d: valid playback route found\n", hops);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000325 dapm->num_valid_paths++;
326 return 1;
327 }
328
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100329 if (root->hops && root->hops <= hops)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000330 return 0;
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100331 root->hops = hops;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000332
333 /* check all the output paths on this source widget by walking
334 * from source to sink */
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100335 list_for_each(lp, &root->sinks) {
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000336 path = list_entry(lp, struct snd_soc_dapm_path, list_source);
337
338 dev_dbg(dapm->dev," %c %d: %s -> %s -> %s\n",
339 path->connect ? '*' : ' ', hops,
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100340 root->name, path->name, path->sink->name);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000341
342 /* been here before ? */
343 if (path->length && path->length <= hops)
344 continue;
345
346 /* check down the next path if connected */
347 if (path->sink && path->connect &&
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100348 dapm_find_playback_paths(dapm, path->sink, list, hops + 1)) {
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000349 path->length = hops;
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100350
351 /* add widget to list */
352 dapm_add_unique_widget(dapm, list, path->sink);
353
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000354 if (!dist || dist > path->length)
355 dist = path->length;
356 }
357 }
358
359 return dist;
360}
361
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100362static int dapm_find_capture_paths(struct snd_soc_dapm_context *dapm,
363 struct snd_soc_dapm_widget *root,
364 struct snd_soc_dapm_widget_list **list, int hops)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000365{
366 struct list_head *lp;
367 struct snd_soc_dapm_path *path;
368 int dist = 0;
369
370 if (hops > PATH_MAX_HOPS)
371 return 0;
372
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100373 if (is_input_widget_ep(root) && hops != 1) {
374 dev_dbg(dapm->dev," ! %d: valid capture route found\n", hops);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000375 dapm->num_valid_paths++;
376 return 1;
377 }
378
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100379 if (root->hops && root->hops <= hops)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000380 return 0;
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100381 root->hops = hops;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000382
383 /* check all the output paths on this source widget by walking from
384 * sink to source */
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100385 list_for_each(lp, &root->sources) {
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000386 path = list_entry(lp, struct snd_soc_dapm_path, list_sink);
387
388 dev_dbg(dapm->dev," %c %d: %s <- %s <- %s\n",
389 path->connect ? '*' : ' ', hops,
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100390 root->name, path->name, path->source->name);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000391
392 /* been here before ? */
393 if (path->length && path->length <= hops)
394 continue;
395
396 /* check down the next path if connected */
397 if (path->source && path->connect &&
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100398 dapm_find_capture_paths(dapm, path->source, list, hops + 1)) {
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000399 path->length = hops;
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100400
401 /* add widget to list */
402 dapm_add_unique_widget(dapm, list, path->source);
403
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000404 if (!dist || dist > path->length)
405 dist = path->length;
406 }
407 }
408
409 return dist;
410}
411
412/*
413 * traverse the tree from sink to source via the shortest path
414 */
415static int dapm_get_playback_paths(struct snd_soc_dapm_context *dapm,
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100416 struct snd_soc_dapm_widget *root,
417 struct snd_soc_dapm_widget_list **list)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000418{
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100419 dev_dbg(dapm->dev, "Playback: checking paths from %s\n",root->name);
420 dapm_find_playback_paths(dapm, root, list, 1);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000421 return dapm->num_valid_paths;
422}
423
424static int dapm_get_capture_paths(struct snd_soc_dapm_context *dapm,
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100425 struct snd_soc_dapm_widget *root,
426 struct snd_soc_dapm_widget_list **list)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000427{
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100428 dev_dbg(dapm->dev, "Capture: checking paths to %s\n", root->name);
429 dapm_find_capture_paths(dapm, root, list, 1);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000430 return dapm->num_valid_paths;
431}
432
433/**
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100434 * snd_soc_dapm_get_connected_widgets_type - query audio path and it's widgets.
435 * @dapm: the dapm context.
436 * @stream_name: stream name.
437 * @list: list of active widgets for this stream.
438 * @stream: stream direction.
439 * @type: Initial widget type.
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000440 *
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100441 * Queries DAPM graph as to whether an valid audio stream path exists for
442 * the DAPM stream and initial widget type specified. This takes into account
443 * current mixer and mux kcontrol settings. Creates list of valid widgets.
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000444 *
445 * Returns the number of valid paths or negative error.
446 */
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100447int snd_soc_dapm_get_connected_widgets_type(struct snd_soc_dapm_context *dapm,
448 const char *stream_name, struct snd_soc_dapm_widget_list **list,
449 int stream, enum snd_soc_dapm_type type)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000450{
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100451 struct snd_soc_dapm_widget *w;
452 int paths;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000453
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100454 /* get stream root widget AIF, DAC or ADC from stream string and direction */
455 list_for_each_entry(w, &dapm->card->widgets, list) {
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000456
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100457 if (!w->sname)
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000458 continue;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000459
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100460 if (w->id != type)
461 continue;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000462
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100463 if (strstr(w->sname, stream_name))
464 goto found;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000465 }
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100466 dev_err(dapm->dev, "root widget not found\n");
467 return 0;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000468
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100469found:
470 dapm->num_valid_paths = 0;
471 *list = NULL;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000472 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100473 paths = dapm_get_playback_paths(dapm, w, list);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000474 else
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100475 paths = dapm_get_capture_paths(dapm, w, list);
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000476
477 dapm_clear_paths(dapm);
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100478 return paths;
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000479}
Liam Girdwood3f5dc292011-06-01 20:02:32 +0100480/**
481 * snd_soc_dapm_get_connected_widgets_name - query audio path and it's widgets.
482 * @dapm: the dapm context.
483 * @name: initial widget name.
484 * @list: list of active widgets for this stream.
485 * @stream: stream direction.
486 *
487 * Queries DAPM graph as to whether an valid audio stream path exists for
488 * the initial widget specified by name. This takes into account
489 * current mixer and mux kcontrol settings. Creates list of valid widgets.
490 *
491 * Returns the number of valid paths or negative error.
492 */
493int snd_soc_dapm_get_connected_widgets_name(struct snd_soc_dapm_context *dapm,
494 const char *name, struct snd_soc_dapm_widget_list **list, int stream)
495{
496 struct snd_soc_dapm_widget *w;
497 int paths;
498
499 /* get stream root widget AIF, DAC or ADC from stream string and direction */
500 list_for_each_entry(w, &dapm->card->widgets, list) {
501
502 if (strstr(w->name, name))
503 goto found;
504 }
505 dev_err(dapm->dev, "root widget %s not found\n", name);
506 return 0;
507
508found:
509 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
510 paths = dapm_get_playback_paths(dapm, w, list);
511 else
512 paths = dapm_get_capture_paths(dapm, w, list);
513
514 dapm_clear_paths(dapm);
515 return paths;
516}
Liam Girdwood35ca6602011-01-28 17:45:35 +0000517
Mark Brown452c5ea2009-05-17 21:41:23 +0100518/**
519 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800520 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100521 * @level: level to configure
522 *
523 * Configure the bias (power) levels for the SoC audio device.
524 *
525 * Returns 0 for success else error.
526 */
Mark Browned5a4c42011-02-18 11:12:42 -0800527static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200528 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100529{
Mark Browned5a4c42011-02-18 11:12:42 -0800530 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100531 int ret = 0;
532
Mark Brownf83fba82009-05-18 15:44:43 +0100533 switch (level) {
534 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200535 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100536 break;
537 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200538 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100539 break;
540 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200541 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100542 break;
543 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200544 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100545 break;
546 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200547 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100548 return -EINVAL;
549 }
550
Mark Brown84e90932010-11-04 00:07:02 -0400551 trace_snd_soc_bias_level_start(card, level);
552
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100554 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100555 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200556 if (dapm->codec && dapm->codec->driver->set_bias_level)
557 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100558 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200559 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100560 }
Mark Brown1badabd2010-12-04 12:41:04 +0000561 if (ret == 0) {
562 if (card && card->set_bias_level_post)
563 ret = card->set_bias_level_post(card, level);
564 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100565
Mark Brown84e90932010-11-04 00:07:02 -0400566 trace_snd_soc_bias_level_done(card, level);
567
Mark Brown452c5ea2009-05-17 21:41:23 +0100568 return ret;
569}
570
Richard Purdie2b97eab2006-10-06 18:32:18 +0200571/* set up initial codec paths */
572static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
573 struct snd_soc_dapm_path *p, int i)
574{
575 switch (w->id) {
576 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000577 case snd_soc_dapm_mixer:
578 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200579 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100580 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600581 w->kcontrol_news[i].private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -0400582 unsigned int reg = mc->reg;
583 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100584 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -0400585 unsigned int mask = (1 << fls(max)) - 1;
586 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200587
Liam Girdwood35ca6602011-01-28 17:45:35 +0000588 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200589 val = (val >> shift) & mask;
590
591 if ((invert && !val) || (!invert && val))
592 p->connect = 1;
593 else
594 p->connect = 0;
595 }
596 break;
597 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600598 struct soc_enum *e = (struct soc_enum *)
599 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200600 int val, item, bitmask;
601
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100602 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200603 ;
Liam Girdwood35ca6602011-01-28 17:45:35 +0000604 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200605 item = (val >> e->shift_l) & (bitmask - 1);
606
607 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100608 for (i = 0; i < e->max; i++) {
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100609 if (!(strcmp(p->name, snd_soc_get_enum_text(e, i))) && item == i)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200610 p->connect = 1;
611 }
612 }
613 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000614 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600615 struct soc_enum *e = (struct soc_enum *)
616 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000617
618 p->connect = 0;
619 /* since a virtual mux has no backing registers to
620 * decide which path to connect, it will try to match
621 * with the first enumeration. This is to ensure
622 * that the default mux choice (the first) will be
623 * correctly powered up during initialization.
624 */
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100625 if (!strcmp(p->name, snd_soc_get_enum_text(e, 0)))
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000626 p->connect = 1;
627 }
628 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200629 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200630 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600631 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200632 int val, item;
633
Liam Girdwood35ca6602011-01-28 17:45:35 +0000634 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200635 val = (val >> e->shift_l) & e->mask;
636 for (item = 0; item < e->max; item++) {
637 if (val == e->values[item])
638 break;
639 }
640
641 p->connect = 0;
642 for (i = 0; i < e->max; i++) {
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100643 if (!(strcmp(p->name, snd_soc_get_enum_text(e, i))) && item == i)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200644 p->connect = 1;
645 }
646 }
647 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200648 /* does not effect routing - always connected */
649 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600650 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200651 case snd_soc_dapm_output:
652 case snd_soc_dapm_adc:
653 case snd_soc_dapm_input:
654 case snd_soc_dapm_dac:
655 case snd_soc_dapm_micbias:
656 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100657 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100658 case snd_soc_dapm_aif_in:
659 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200660 p->connect = 1;
661 break;
662 /* does effect routing - dynamically connected */
663 case snd_soc_dapm_hp:
664 case snd_soc_dapm_mic:
665 case snd_soc_dapm_spk:
666 case snd_soc_dapm_line:
667 case snd_soc_dapm_pre:
668 case snd_soc_dapm_post:
669 p->connect = 0;
670 break;
671 }
672}
673
Mark Brown74b8f952009-06-06 11:26:15 +0100674/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200675static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200676 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
677 struct snd_soc_dapm_path *path, const char *control_name,
678 const struct snd_kcontrol_new *kcontrol)
679{
680 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
681 int i;
682
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100683 for (i = 0; i < e->max; i++) {
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100684 if (!(strcmp(control_name, snd_soc_get_enum_text(e, i)))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200685 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200686 list_add(&path->list_sink, &dest->sources);
687 list_add(&path->list_source, &src->sinks);
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100688 path->name = (char*)snd_soc_get_enum_text(e, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200689 dapm_set_path_status(dest, path, 0);
690 return 0;
691 }
692 }
693
694 return -ENODEV;
695}
696
Mark Brown74b8f952009-06-06 11:26:15 +0100697/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200698static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200699 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
700 struct snd_soc_dapm_path *path, const char *control_name)
701{
702 int i;
703
704 /* search for mixer kcontrol */
705 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600706 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200707 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200708 list_add(&path->list_sink, &dest->sources);
709 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600710 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200711 dapm_set_path_status(dest, path, i);
712 return 0;
713 }
714 }
715 return -ENODEV;
716}
717
Stephen Warrenaf468002011-04-28 17:38:01 -0600718static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600719 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600720 const struct snd_kcontrol_new *kcontrol_new,
721 struct snd_kcontrol **kcontrol)
722{
723 struct snd_soc_dapm_widget *w;
724 int i;
725
726 *kcontrol = NULL;
727
728 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600729 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
730 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600731 for (i = 0; i < w->num_kcontrols; i++) {
732 if (&w->kcontrol_news[i] == kcontrol_new) {
733 if (w->kcontrols)
734 *kcontrol = w->kcontrols[i];
735 return 1;
736 }
737 }
738 }
739
740 return 0;
741}
742
Richard Purdie2b97eab2006-10-06 18:32:18 +0200743/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200744static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200745{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200746 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200747 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000748 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200749 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000750 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000751 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600752 struct snd_soc_dapm_widget_list *wlist;
753 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000754
755 if (dapm->codec)
756 prefix = dapm->codec->name_prefix;
757 else
758 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200759
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000760 if (prefix)
761 prefix_len = strlen(prefix) + 1;
762 else
763 prefix_len = 0;
764
Richard Purdie2b97eab2006-10-06 18:32:18 +0200765 /* add kcontrol */
766 for (i = 0; i < w->num_kcontrols; i++) {
767
768 /* match name */
769 list_for_each_entry(path, &w->sources, list_sink) {
770
771 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600772 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200773 continue;
774
Stephen Warrenfafd2172011-04-28 17:38:00 -0600775 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
776 sizeof(struct snd_soc_dapm_widget *),
777 wlist = kzalloc(wlistsize, GFP_KERNEL);
778 if (wlist == NULL) {
779 dev_err(dapm->dev,
780 "asoc: can't allocate widget list for %s\n",
781 w->name);
782 return -ENOMEM;
783 }
784 wlist->num_widgets = 1;
785 wlist->widgets[0] = w;
786
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000787 /* add dapm control with long name.
788 * for dapm_mixer this is the concatenation of the
789 * mixer and kcontrol name.
790 * for dapm_mixer_named_ctl this is simply the
791 * kcontrol name.
792 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600793 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000794 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000795 name_len += 1 + strlen(w->name);
796
Mark Brown219b93f2008-10-28 13:02:31 +0000797 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000798
Stephen Warrenfafd2172011-04-28 17:38:00 -0600799 if (path->long_name == NULL) {
800 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200801 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600802 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200803
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000804 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000805 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000806 /* The control will get a prefix from
807 * the control creation process but
808 * we're also using the same prefix
809 * for widgets so cut the prefix off
810 * the front of the widget name.
811 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000812 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000813 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600814 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000815 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000816 case snd_soc_dapm_mixer_named_ctl:
817 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600818 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000819 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000820 }
821
Mark Brown219b93f2008-10-28 13:02:31 +0000822 path->long_name[name_len - 1] = '\0';
823
Stephen Warrenfafd2172011-04-28 17:38:00 -0600824 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
825 wlist, path->long_name,
826 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200827 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200828 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200829 dev_err(dapm->dev,
830 "asoc: failed to add dapm kcontrol %s: %d\n",
831 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600832 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200833 kfree(path->long_name);
834 path->long_name = NULL;
835 return ret;
836 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600837 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200838 }
839 }
840 return ret;
841}
842
843/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200844static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200845{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200846 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200847 struct snd_soc_dapm_path *path = NULL;
848 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000849 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000850 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000851 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600852 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600853 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600854 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600855 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600856 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200857
Stephen Warrenaf468002011-04-28 17:38:01 -0600858 if (w->num_kcontrols != 1) {
859 dev_err(dapm->dev,
860 "asoc: mux %s has incorrect number of controls\n",
861 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200862 return -EINVAL;
863 }
864
Stephen Warren1007da02011-05-26 09:57:33 -0600865 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600866 &kcontrol);
867 if (kcontrol) {
868 wlist = kcontrol->private_data;
869 wlistentries = wlist->num_widgets + 1;
870 } else {
871 wlist = NULL;
872 wlistentries = 1;
873 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600874 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600875 wlistentries * sizeof(struct snd_soc_dapm_widget *),
876 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600877 if (wlist == NULL) {
878 dev_err(dapm->dev,
879 "asoc: can't allocate widget list for %s\n", w->name);
880 return -ENOMEM;
881 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600882 wlist->num_widgets = wlistentries;
883 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600884
Stephen Warrenaf468002011-04-28 17:38:01 -0600885 if (!kcontrol) {
Liam Girdwood4d74b362011-05-18 17:50:07 +0100886 if (dapm->codec && dapm->codec->name_prefix)
Stephen Warrenaf468002011-04-28 17:38:01 -0600887 prefix = dapm->codec->name_prefix;
888 else
Misael Lopez Cruzbd5e5632011-06-17 13:35:36 -0700889 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000890
Stephen Warrenaf468002011-04-28 17:38:01 -0600891 if (shared) {
892 name = w->kcontrol_news[0].name;
893 prefix_len = 0;
894 } else {
895 name = w->name;
896 if (prefix)
897 prefix_len = strlen(prefix) + 1;
898 else
899 prefix_len = 0;
900 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000901
Stephen Warrenaf468002011-04-28 17:38:01 -0600902 /*
903 * The control will get a prefix from the control creation
904 * process but we're also using the same prefix for widgets so
905 * cut the prefix off the front of the widget name.
906 */
907 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
Misael Lopez Cruzbd5e5632011-06-17 13:35:36 -0700908 name, prefix);
Stephen Warrenaf468002011-04-28 17:38:01 -0600909 ret = snd_ctl_add(card, kcontrol);
910 if (ret < 0) {
911 dev_err(dapm->dev,
912 "asoc: failed to add kcontrol %s\n", w->name);
913 kfree(wlist);
914 return ret;
915 }
916 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200917
Stephen Warrenaf468002011-04-28 17:38:01 -0600918 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200919
Stephen Warrenfad59882011-04-28 17:37:59 -0600920 w->kcontrols[0] = kcontrol;
921
Richard Purdie2b97eab2006-10-06 18:32:18 +0200922 list_for_each_entry(path, &w->sources, list_sink)
923 path->kcontrol = kcontrol;
924
Stephen Warrenaf468002011-04-28 17:38:01 -0600925 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200926}
927
928/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200929static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200930{
Mark Browna6c65732010-03-03 17:45:21 +0000931 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200932 dev_err(w->dapm->dev,
933 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200934
Mark Browna6c65732010-03-03 17:45:21 +0000935 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200936}
937
Mark Brown99497882010-05-07 20:24:05 +0100938/* We implement power down on suspend by checking the power state of
939 * the ALSA card - when we are suspending the ALSA state for the card
940 * is set to D3.
941 */
942static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
943{
Mark Brown12ea2c72011-03-02 18:17:32 +0000944 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100945
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000946 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100947 case SNDRV_CTL_POWER_D3hot:
948 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100949 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200950 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
951 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100952 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100953 default:
954 return 1;
955 }
956}
957
Richard Purdie2b97eab2006-10-06 18:32:18 +0200958/*
959 * Recursively check for a completed path to an active or physically connected
960 * output widget. Returns number of complete paths.
961 */
962static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
963{
964 struct snd_soc_dapm_path *path;
965 int con = 0;
966
Mark Brown246d0a12009-04-22 18:24:55 +0100967 if (widget->id == snd_soc_dapm_supply)
968 return 0;
969
Mark Brown010ff262009-08-17 17:39:22 +0100970 switch (widget->id) {
971 case snd_soc_dapm_adc:
972 case snd_soc_dapm_aif_out:
973 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100974 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100975 default:
976 break;
977 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200978
979 if (widget->connected) {
980 /* connected pin ? */
981 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100982 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200983
984 /* connected jack or spk ? */
985 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300986 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100987 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200988 }
989
990 list_for_each_entry(path, &widget->sinks, list_source) {
991 if (path->walked)
992 continue;
993
994 if (path->sink && path->connect) {
995 path->walked = 1;
996 con += is_connected_output_ep(path->sink);
997 }
998 }
999
1000 return con;
1001}
1002
1003/*
1004 * Recursively check for a completed path to an active or physically connected
1005 * input widget. Returns number of complete paths.
1006 */
1007static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
1008{
1009 struct snd_soc_dapm_path *path;
1010 int con = 0;
1011
Mark Brown246d0a12009-04-22 18:24:55 +01001012 if (widget->id == snd_soc_dapm_supply)
1013 return 0;
1014
Richard Purdie2b97eab2006-10-06 18:32:18 +02001015 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +01001016 switch (widget->id) {
1017 case snd_soc_dapm_dac:
1018 case snd_soc_dapm_aif_in:
1019 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +01001020 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +01001021 default:
1022 break;
1023 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001024
1025 if (widget->connected) {
1026 /* connected pin ? */
1027 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +01001028 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001029
1030 /* connected VMID/Bias for lower pops */
1031 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +01001032 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001033
1034 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +03001035 if (widget->id == snd_soc_dapm_mic ||
1036 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +01001037 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001038 }
1039
1040 list_for_each_entry(path, &widget->sources, list_sink) {
1041 if (path->walked)
1042 continue;
1043
1044 if (path->source && path->connect) {
1045 path->walked = 1;
1046 con += is_connected_input_ep(path->source);
1047 }
1048 }
1049
1050 return con;
1051}
1052
1053/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001054 * Handler for generic register modifier widget.
1055 */
1056int dapm_reg_event(struct snd_soc_dapm_widget *w,
1057 struct snd_kcontrol *kcontrol, int event)
1058{
1059 unsigned int val;
1060
1061 if (SND_SOC_DAPM_EVENT_ON(event))
1062 val = w->on_val;
1063 else
1064 val = w->off_val;
1065
Liam Girdwood35ca6602011-01-28 17:45:35 +00001066 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001067 w->mask << w->shift, val << w->shift);
1068
1069 return 0;
1070}
Mark Brown11589412008-07-29 11:42:23 +01001071EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001072
Mark Browncd0f2d42009-04-20 16:56:59 +01001073/* Generic check to see if a widget should be powered.
1074 */
1075static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1076{
1077 int in, out;
1078
1079 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001080 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +01001081 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001082 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +01001083 return out != 0 && in != 0;
1084}
1085
Mark Brown6ea31b92009-04-20 17:15:41 +01001086/* Check to see if an ADC has power */
1087static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1088{
1089 int in;
1090
1091 if (w->active) {
1092 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001093 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001094 return in != 0;
1095 } else {
1096 return dapm_generic_check_power(w);
1097 }
1098}
1099
1100/* Check to see if a DAC has power */
1101static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1102{
1103 int out;
1104
1105 if (w->active) {
1106 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001107 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001108 return out != 0;
1109 } else {
1110 return dapm_generic_check_power(w);
1111 }
1112}
1113
Mark Brown246d0a12009-04-22 18:24:55 +01001114/* Check to see if a power supply is needed */
1115static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1116{
1117 struct snd_soc_dapm_path *path;
1118 int power = 0;
1119
1120 /* Check if one of our outputs is connected */
1121 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001122 if (path->connected &&
1123 !path->connected(path->source, path->sink))
1124 continue;
1125
Mark Brown30173582011-02-11 11:42:19 +00001126 if (!path->sink)
1127 continue;
1128
1129 if (path->sink->force) {
1130 power = 1;
1131 break;
1132 }
1133
1134 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +01001135 path->sink->power_check(path->sink)) {
1136 power = 1;
1137 break;
1138 }
1139 }
1140
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001141 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +01001142
1143 return power;
1144}
1145
Mark Brown38357ab2009-06-06 19:03:23 +01001146static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1147 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001148 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001149{
Mark Brown828a8422011-01-15 13:14:30 +00001150 int *sort;
1151
1152 if (power_up)
1153 sort = dapm_up_seq;
1154 else
1155 sort = dapm_down_seq;
1156
Mark Brown38357ab2009-06-06 19:03:23 +01001157 if (sort[a->id] != sort[b->id])
1158 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001159 if (a->subseq != b->subseq) {
1160 if (power_up)
1161 return a->subseq - b->subseq;
1162 else
1163 return b->subseq - a->subseq;
1164 }
Mark Brownb22ead22009-06-07 12:51:26 +01001165 if (a->reg != b->reg)
1166 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001167 if (a->dapm != b->dapm)
1168 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001169
Mark Brown38357ab2009-06-06 19:03:23 +01001170 return 0;
1171}
Mark Brown42aa3412009-03-01 19:21:10 +00001172
Mark Brown38357ab2009-06-06 19:03:23 +01001173/* Insert a widget in order into a DAPM power sequence. */
1174static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1175 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001176 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001177{
1178 struct snd_soc_dapm_widget *w;
1179
1180 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001181 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001182 list_add_tail(&new_widget->power_list, &w->power_list);
1183 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001184 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001185
Mark Brown38357ab2009-06-06 19:03:23 +01001186 list_add_tail(&new_widget->power_list, list);
1187}
Mark Brown42aa3412009-03-01 19:21:10 +00001188
Mark Brown68f89ad2010-11-03 23:51:49 -04001189static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1190 struct snd_soc_dapm_widget *w, int event)
1191{
1192 struct snd_soc_card *card = dapm->card;
1193 const char *ev_name;
1194 int power, ret;
1195
1196 switch (event) {
1197 case SND_SOC_DAPM_PRE_PMU:
1198 ev_name = "PRE_PMU";
1199 power = 1;
1200 break;
1201 case SND_SOC_DAPM_POST_PMU:
1202 ev_name = "POST_PMU";
1203 power = 1;
1204 break;
1205 case SND_SOC_DAPM_PRE_PMD:
1206 ev_name = "PRE_PMD";
1207 power = 0;
1208 break;
1209 case SND_SOC_DAPM_POST_PMD:
1210 ev_name = "POST_PMD";
1211 power = 0;
1212 break;
1213 default:
1214 BUG();
1215 return;
1216 }
1217
1218 if (w->power != power)
1219 return;
1220
1221 if (w->event && (w->event_flags & event)) {
1222 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1223 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001224 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001225 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001226 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001227 if (ret < 0)
1228 pr_err("%s: %s event failed: %d\n",
1229 ev_name, w->name, ret);
1230 }
1231}
1232
Mark Brownb22ead22009-06-07 12:51:26 +01001233/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001234static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001235 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001236{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001237 struct snd_soc_card *card = dapm->card;
Liam Girdwoodecc1a0d2011-02-16 16:24:17 +00001238 struct snd_soc_dapm_widget *w, *_w;
Mark Brown68f89ad2010-11-03 23:51:49 -04001239 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001240 unsigned int value = 0;
1241 unsigned int mask = 0;
1242 unsigned int cur_mask;
1243
Liam Girdwoodecc1a0d2011-02-16 16:24:17 +00001244 _w = list_first_entry(pending, struct snd_soc_dapm_widget,
1245 power_list);
1246 reg = _w->reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001247
1248 list_for_each_entry(w, pending, power_list) {
1249 cur_mask = 1 << w->shift;
1250 BUG_ON(reg != w->reg);
1251
1252 if (w->invert)
1253 power = !w->power;
1254 else
1255 power = w->power;
1256
1257 mask |= cur_mask;
1258 if (power)
1259 value |= cur_mask;
1260
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001261 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001262 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1263 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001264
Mark Brown68f89ad2010-11-03 23:51:49 -04001265 /* Check for events */
1266 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1267 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001268 }
1269
Mark Brown81628102009-06-07 13:21:24 +01001270 if (reg >= 0) {
Mark Brown9a014432011-06-19 13:49:28 +01001271 /* Any widget will do, they should all be updating the
1272 * same register.
1273 */
1274 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1275 power_list);
1276
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001277 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001278 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001279 value, mask, reg, card->pop_time);
1280 pop_wait(card->pop_time);
Liam Girdwoodecc1a0d2011-02-16 16:24:17 +00001281 soc_widget_update_bits(_w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001282 }
1283
1284 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001285 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1286 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001287 }
Mark Brown42aa3412009-03-01 19:21:10 +00001288}
1289
Mark Brownb22ead22009-06-07 12:51:26 +01001290/* Apply a DAPM power sequence.
1291 *
1292 * We walk over a pre-sorted list of widgets to apply power to. In
1293 * order to minimise the number of writes to the device required
1294 * multiple widgets will be updated in a single write where possible.
1295 * Currently anything that requires more than a single write is not
1296 * handled.
1297 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001298static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001299 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001300{
1301 struct snd_soc_dapm_widget *w, *n;
1302 LIST_HEAD(pending);
1303 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001304 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001305 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001306 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001307 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001308 int *sort;
1309
1310 if (power_up)
1311 sort = dapm_up_seq;
1312 else
1313 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001314
Mark Brownb22ead22009-06-07 12:51:26 +01001315 list_for_each_entry_safe(w, n, list, power_list) {
1316 ret = 0;
1317
1318 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001319 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001320 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Jayasena Sangaraboinab337c2a2012-02-17 12:18:59 -08001321 if (cur_dapm && !list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001322 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001323
Mark Brown474b62d2011-01-18 16:14:44 +00001324 if (cur_dapm && cur_dapm->seq_notifier) {
1325 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1326 if (sort[i] == cur_sort)
1327 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001328 i,
1329 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001330 }
1331
Mark Brownb22ead22009-06-07 12:51:26 +01001332 INIT_LIST_HEAD(&pending);
1333 cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001334 cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001335 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001336 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001337 }
1338
Mark Brown163cac02009-06-07 10:12:52 +01001339 switch (w->id) {
1340 case snd_soc_dapm_pre:
1341 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001342 list_for_each_entry_safe_continue(w, n, list,
1343 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001344
Mark Brownb22ead22009-06-07 12:51:26 +01001345 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001346 ret = w->event(w,
1347 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001348 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001349 ret = w->event(w,
1350 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001351 break;
1352
1353 case snd_soc_dapm_post:
1354 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001355 list_for_each_entry_safe_continue(w, n, list,
1356 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001357
Mark Brownb22ead22009-06-07 12:51:26 +01001358 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001359 ret = w->event(w,
1360 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001361 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001362 ret = w->event(w,
1363 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001364 break;
1365
Mark Brown163cac02009-06-07 10:12:52 +01001366 default:
Mark Brown81628102009-06-07 13:21:24 +01001367 /* Queue it up for application */
1368 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001369 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001370 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001371 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001372 list_move(&w->power_list, &pending);
1373 break;
Mark Brown163cac02009-06-07 10:12:52 +01001374 }
Mark Brownb22ead22009-06-07 12:51:26 +01001375
1376 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001377 dev_err(w->dapm->dev,
1378 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001379 }
Mark Brownb22ead22009-06-07 12:51:26 +01001380
Jayasena Sangaraboinab337c2a2012-02-17 12:18:59 -08001381 if (cur_dapm && !list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001382 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001383
1384 if (cur_dapm && cur_dapm->seq_notifier) {
1385 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1386 if (sort[i] == cur_sort)
1387 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001388 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001389 }
Mark Brown163cac02009-06-07 10:12:52 +01001390}
1391
Mark Brown97404f22010-12-14 16:13:57 +00001392static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1393{
1394 struct snd_soc_dapm_update *update = dapm->update;
1395 struct snd_soc_dapm_widget *w;
1396 int ret;
1397
1398 if (!update)
1399 return;
1400
1401 w = update->widget;
1402
1403 if (w->event &&
1404 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1405 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1406 if (ret != 0)
1407 pr_err("%s DAPM pre-event failed: %d\n",
1408 w->name, ret);
1409 }
1410
1411 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1412 update->val);
1413 if (ret < 0)
1414 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1415
1416 if (w->event &&
1417 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1418 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1419 if (ret != 0)
1420 pr_err("%s DAPM post-event failed: %d\n",
1421 w->name, ret);
1422 }
1423}
1424
Mark Brown9d0624a2011-02-18 11:49:43 -08001425/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1426 * they're changing state.
1427 */
1428static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1429{
1430 struct snd_soc_dapm_context *d = data;
1431 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001432
Mark Brown9d0624a2011-02-18 11:49:43 -08001433 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1434 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1435 if (ret != 0)
1436 dev_err(d->dev,
1437 "Failed to turn on bias: %d\n", ret);
1438 }
1439
1440 /* If we're changing to all on or all off then prepare */
1441 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1442 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1443 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1444 if (ret != 0)
1445 dev_err(d->dev,
1446 "Failed to prepare bias: %d\n", ret);
1447 }
1448}
1449
1450/* Async callback run prior to DAPM sequences - brings to their final
1451 * state.
1452 */
1453static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1454{
1455 struct snd_soc_dapm_context *d = data;
1456 int ret;
1457
1458 /* If we just powered the last thing off drop to standby bias */
1459 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1460 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1461 if (ret != 0)
1462 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1463 ret);
1464 }
1465
1466 /* If we're in standby and can support bias off then do that */
1467 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
1468 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1469 if (ret != 0)
1470 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1471 }
1472
1473 /* If we just powered up then move to active bias */
1474 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1475 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1476 if (ret != 0)
1477 dev_err(d->dev, "Failed to apply active bias: %d\n",
1478 ret);
1479 }
1480}
Mark Brown97404f22010-12-14 16:13:57 +00001481
Mark Brown42aa3412009-03-01 19:21:10 +00001482/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001483 * Scan each dapm widget for complete audio path.
1484 * A complete path is a route that has valid endpoints i.e.:-
1485 *
1486 * o DAC to output pin.
1487 * o Input Pin to ADC.
1488 * o Input pin to Output pin (bypass, sidetone)
1489 * o DAC to ADC (loopback).
1490 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001491static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001492{
Mark Brown12ea2c72011-03-02 18:17:32 +00001493 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001494 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001495 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001496 LIST_HEAD(up_list);
1497 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001498 LIST_HEAD(async_domain);
Mark Brown38357ab2009-06-06 19:03:23 +01001499 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001500
Mark Brown84e90932010-11-04 00:07:02 -04001501 trace_snd_soc_dapm_start(card);
1502
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001503 list_for_each_entry(d, &card->dapm_list, list)
Jarkko Nikulaea77b942011-05-26 16:32:18 +03001504 if (d->n_widgets || d->codec == NULL)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001505 d->dev_power = 0;
1506
Mark Brown6d3ddc82009-05-16 17:47:29 +01001507 /* Check which widgets we need to power and store them in
1508 * lists indicating if they should be powered up or down.
1509 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001510 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001511 switch (w->id) {
1512 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001513 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001514 break;
1515 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001516 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001517 break;
1518
1519 default:
1520 if (!w->power_check)
1521 continue;
1522
Mark Brown99497882010-05-07 20:24:05 +01001523 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001524 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001525 else
1526 power = 1;
1527 if (power)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001528 w->dapm->dev_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +01001529
Mark Brown6d3ddc82009-05-16 17:47:29 +01001530 if (w->power == power)
1531 continue;
1532
Mark Brown84e90932010-11-04 00:07:02 -04001533 trace_snd_soc_dapm_widget_power(w, power);
1534
Mark Brown6d3ddc82009-05-16 17:47:29 +01001535 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001536 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001537 else
Mark Brown828a8422011-01-15 13:14:30 +00001538 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001539
1540 w->power = power;
1541 break;
1542 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001543 }
1544
Mark Brownb14b76a2009-08-17 11:55:38 +01001545 /* If there are no DAPM widgets then try to figure out power from the
1546 * event type.
1547 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001548 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001549 switch (event) {
1550 case SND_SOC_DAPM_STREAM_START:
1551 case SND_SOC_DAPM_STREAM_RESUME:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001552 dapm->dev_power = 1;
Mark Brownb14b76a2009-08-17 11:55:38 +01001553 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001554 case SND_SOC_DAPM_STREAM_STOP:
Liam Girdwood7987a112011-01-31 19:52:42 +00001555 if (dapm->codec)
1556 dapm->dev_power = !!dapm->codec->active;
1557 else
1558 dapm->dev_power = 0;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001559 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001560 case SND_SOC_DAPM_STREAM_SUSPEND:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001561 dapm->dev_power = 0;
Mark Brown50b6bce2009-11-23 13:11:53 +00001562 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001563 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001564 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +00001565 case SND_SOC_BIAS_STANDBY:
1566 case SND_SOC_BIAS_OFF:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001567 dapm->dev_power = 0;
Mark Browna96ca332010-01-19 22:49:43 +00001568 break;
1569 default:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001570 dapm->dev_power = 1;
Mark Browna96ca332010-01-19 22:49:43 +00001571 break;
1572 }
Mark Brown50b6bce2009-11-23 13:11:53 +00001573 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001574 default:
1575 break;
1576 }
1577 }
1578
Mark Brown52ba67b2011-04-04 21:05:11 +09001579 /* Force all contexts in the card to the same bias state */
1580 power = 0;
1581 list_for_each_entry(d, &card->dapm_list, list)
1582 if (d->dev_power)
1583 power = 1;
1584 list_for_each_entry(d, &card->dapm_list, list)
1585 d->dev_power = power;
1586
1587
Mark Brown9d0624a2011-02-18 11:49:43 -08001588 /* Run all the bias changes in parallel */
1589 list_for_each_entry(d, &dapm->card->dapm_list, list)
1590 async_schedule_domain(dapm_pre_sequence_async, d,
1591 &async_domain);
1592 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001593
Mark Brown6d3ddc82009-05-16 17:47:29 +01001594 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001595 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001596
Mark Brown97404f22010-12-14 16:13:57 +00001597 dapm_widget_update(dapm);
1598
Mark Brown6d3ddc82009-05-16 17:47:29 +01001599 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001600 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001601
Mark Brown9d0624a2011-02-18 11:49:43 -08001602 /* Run all the bias changes in parallel */
1603 list_for_each_entry(d, &dapm->card->dapm_list, list)
1604 async_schedule_domain(dapm_post_sequence_async, d,
1605 &async_domain);
1606 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001607
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001608 pop_dbg(dapm->dev, card->pop_time,
1609 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001610 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001611
Mark Brown84e90932010-11-04 00:07:02 -04001612 trace_snd_soc_dapm_done(card);
1613
Mark Brown42aa3412009-03-01 19:21:10 +00001614 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001615}
1616
Mark Brown79fb9382009-08-21 16:38:13 +01001617#ifdef CONFIG_DEBUG_FS
1618static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1619{
1620 file->private_data = inode->i_private;
1621 return 0;
1622}
1623
1624static ssize_t dapm_widget_power_read_file(struct file *file,
1625 char __user *user_buf,
1626 size_t count, loff_t *ppos)
1627{
1628 struct snd_soc_dapm_widget *w = file->private_data;
1629 char *buf;
1630 int in, out;
1631 ssize_t ret;
1632 struct snd_soc_dapm_path *p = NULL;
1633
1634 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1635 if (!buf)
1636 return -ENOMEM;
1637
1638 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001639 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001640 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001641 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001642
Mark Brownd033c362009-12-04 15:25:56 +00001643 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001644 w->name, w->power ? "On" : "Off", in, out);
1645
Mark Brownd033c362009-12-04 15:25:56 +00001646 if (w->reg >= 0)
1647 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1648 " - R%d(0x%x) bit %d",
1649 w->reg, w->reg, w->shift);
1650
1651 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1652
Mark Brown3eef08b2009-09-14 16:49:00 +01001653 if (w->sname)
1654 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1655 w->sname,
1656 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001657
1658 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001659 if (p->connected && !p->connected(w, p->sink))
1660 continue;
1661
Mark Brown79fb9382009-08-21 16:38:13 +01001662 if (p->connect)
1663 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001664 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001665 p->name ? p->name : "static",
1666 p->source->name);
1667 }
1668 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001669 if (p->connected && !p->connected(w, p->sink))
1670 continue;
1671
Mark Brown79fb9382009-08-21 16:38:13 +01001672 if (p->connect)
1673 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001674 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001675 p->name ? p->name : "static",
1676 p->sink->name);
1677 }
1678
1679 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1680
1681 kfree(buf);
1682 return ret;
1683}
1684
1685static const struct file_operations dapm_widget_power_fops = {
1686 .open = dapm_widget_power_open_file,
1687 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001688 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001689};
1690
Mark Brownef49e4f2011-04-04 20:48:13 +09001691static int dapm_bias_open_file(struct inode *inode, struct file *file)
1692{
1693 file->private_data = inode->i_private;
1694 return 0;
1695}
1696
1697static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1698 size_t count, loff_t *ppos)
1699{
1700 struct snd_soc_dapm_context *dapm = file->private_data;
1701 char *level;
1702
1703 switch (dapm->bias_level) {
1704 case SND_SOC_BIAS_ON:
1705 level = "On\n";
1706 break;
1707 case SND_SOC_BIAS_PREPARE:
1708 level = "Prepare\n";
1709 break;
1710 case SND_SOC_BIAS_STANDBY:
1711 level = "Standby\n";
1712 break;
1713 case SND_SOC_BIAS_OFF:
1714 level = "Off\n";
1715 break;
1716 default:
1717 BUG();
1718 level = "Unknown\n";
1719 break;
1720 }
1721
1722 return simple_read_from_buffer(user_buf, count, ppos, level,
1723 strlen(level));
1724}
1725
1726static const struct file_operations dapm_bias_fops = {
1727 .open = dapm_bias_open_file,
1728 .read = dapm_bias_read_file,
1729 .llseek = default_llseek,
1730};
1731
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001732void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1733 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001734{
Mark Brown79fb9382009-08-21 16:38:13 +01001735 struct dentry *d;
1736
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001737 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1738
1739 if (!dapm->debugfs_dapm) {
1740 printk(KERN_WARNING
1741 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001742 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001743 }
Mark Brown79fb9382009-08-21 16:38:13 +01001744
Mark Brownef49e4f2011-04-04 20:48:13 +09001745 d = debugfs_create_file("bias_level", 0444,
1746 dapm->debugfs_dapm, dapm,
1747 &dapm_bias_fops);
1748 if (!d)
1749 dev_warn(dapm->dev,
1750 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001751}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001752
1753static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1754{
1755 struct snd_soc_dapm_context *dapm = w->dapm;
1756 struct dentry *d;
1757
1758 if (!dapm->debugfs_dapm || !w->name)
1759 return;
1760
1761 d = debugfs_create_file(w->name, 0444,
1762 dapm->debugfs_dapm, w,
1763 &dapm_widget_power_fops);
1764 if (!d)
1765 dev_warn(w->dapm->dev,
1766 "ASoC: Failed to create %s debugfs file\n",
1767 w->name);
1768}
1769
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001770static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1771{
1772 debugfs_remove_recursive(dapm->debugfs_dapm);
1773}
1774
Mark Brown79fb9382009-08-21 16:38:13 +01001775#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001776void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1777 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001778{
1779}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001780
1781static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1782{
1783}
1784
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001785static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1786{
1787}
1788
Mark Brown79fb9382009-08-21 16:38:13 +01001789#endif
1790
Richard Purdie2b97eab2006-10-06 18:32:18 +02001791/* test and update the power status of a mux widget */
Liam Girdwoode002c982011-02-08 11:45:20 +00001792int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001793 struct snd_kcontrol *kcontrol, int change,
1794 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001795{
1796 struct snd_soc_dapm_path *path;
1797 int found = 0;
1798
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001799 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001800 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001801 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001802 return -ENODEV;
1803
Mark Brown3a655772009-10-05 17:23:30 +01001804 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001805 return 0;
1806
1807 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001808 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Liam Girdwoode002c982011-02-08 11:45:20 +00001809
Richard Purdie2b97eab2006-10-06 18:32:18 +02001810 if (path->kcontrol != kcontrol)
1811 continue;
1812
Liam Girdwooda8f13d82011-05-18 18:39:21 +01001813 if (!path->name || !snd_soc_get_enum_text(e, mux))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001814 continue;
1815
1816 found = 1;
1817 /* we now need to match the string in the enum to the path */
Liam Girdwooda8f13d82011-05-18 18:39:21 +01001818 if (!(strcmp(path->name, snd_soc_get_enum_text(e, mux))))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001819 path->connect = 1; /* new connection */
1820 else
1821 path->connect = 0; /* old connection must be powered down */
1822 }
1823
Liam Girdwoode002c982011-02-08 11:45:20 +00001824 if (found) {
Patrick Lai88f4c8f2011-08-25 16:46:19 -07001825 if (widget->platform)
1826 soc_dsp_runtime_update(widget);
Patrick Laia8d41ee2011-09-12 12:42:04 -07001827 else
1828 dapm_power_widgets(widget->dapm,
1829 SND_SOC_DAPM_STREAM_NOP);
Liam Girdwoode002c982011-02-08 11:45:20 +00001830 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001831
1832 return 0;
1833}
Liam Girdwoode002c982011-02-08 11:45:20 +00001834EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001835
Milan plzik1b075e32008-01-10 14:39:46 +01001836/* test and update the power status of a mixer or switch widget */
Liam Girdwoode002c982011-02-08 11:45:20 +00001837int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1838 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001839{
1840 struct snd_soc_dapm_path *path;
1841 int found = 0;
1842
Milan plzik1b075e32008-01-10 14:39:46 +01001843 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001844 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001845 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001846 return -ENODEV;
1847
Richard Purdie2b97eab2006-10-06 18:32:18 +02001848 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001849 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001850 if (path->kcontrol != kcontrol)
1851 continue;
1852
1853 /* found, now check type */
1854 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001855 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001856 break;
1857 }
1858
Liam Girdwoode002c982011-02-08 11:45:20 +00001859 if (found) {
Patrick Lai88f4c8f2011-08-25 16:46:19 -07001860 if (widget->platform)
1861 soc_dsp_runtime_update(widget);
Patrick Laia8d41ee2011-09-12 12:42:04 -07001862 else
1863 dapm_power_widgets(widget->dapm,
1864 SND_SOC_DAPM_STREAM_NOP);
Liam Girdwoode002c982011-02-08 11:45:20 +00001865 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001866
1867 return 0;
1868}
Liam Girdwoode002c982011-02-08 11:45:20 +00001869EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001870
1871/* show dapm widget status in sys fs */
Liam Girdwood7987a112011-01-31 19:52:42 +00001872static ssize_t widget_show(struct snd_soc_dapm_context *dapm,
1873 const char *name, char *buf, ssize_t count)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001874{
Richard Purdie2b97eab2006-10-06 18:32:18 +02001875 struct snd_soc_dapm_widget *w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001876 char *state = "not set";
1877
Liam Girdwood7987a112011-01-31 19:52:42 +00001878 count += sprintf(buf + count, "\n%s\n", name);
1879
Liam Girdwoodab1058a2011-01-31 20:33:07 +00001880 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001881
1882 /* only display widgets that burnm power */
1883 switch (w->id) {
1884 case snd_soc_dapm_hp:
1885 case snd_soc_dapm_mic:
1886 case snd_soc_dapm_spk:
1887 case snd_soc_dapm_line:
1888 case snd_soc_dapm_micbias:
1889 case snd_soc_dapm_dac:
1890 case snd_soc_dapm_adc:
1891 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001892 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001893 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001894 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001895 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001896 if (w->name)
1897 count += sprintf(buf + count, "%s: %s\n",
1898 w->name, w->power ? "On":"Off");
1899 break;
1900 default:
1901 break;
1902 }
1903 }
1904
Liam Girdwood7987a112011-01-31 19:52:42 +00001905 switch (dapm->bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001906 case SND_SOC_BIAS_ON:
1907 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001908 break;
Mark Brown0be98982008-05-19 12:31:28 +02001909 case SND_SOC_BIAS_PREPARE:
1910 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001911 break;
Mark Brown0be98982008-05-19 12:31:28 +02001912 case SND_SOC_BIAS_STANDBY:
1913 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001914 break;
Mark Brown0be98982008-05-19 12:31:28 +02001915 case SND_SOC_BIAS_OFF:
1916 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001917 break;
1918 }
1919 count += sprintf(buf + count, "PM State: %s\n", state);
1920
1921 return count;
1922}
1923
Liam Girdwood7987a112011-01-31 19:52:42 +00001924/* show dapm widget status in sys fs */
1925static ssize_t dapm_widget_show(struct device *dev,
1926 struct device_attribute *attr, char *buf)
1927{
1928 struct snd_soc_pcm_runtime *rtd =
1929 container_of(dev, struct snd_soc_pcm_runtime, dev);
1930 struct snd_soc_codec *codec = rtd->codec;
1931 struct snd_soc_platform *platform = rtd->platform;
1932 ssize_t count = 0;
1933
1934 count += widget_show(&codec->dapm, codec->name, buf, count);
1935 count += widget_show(&platform->dapm, platform->name, buf, count);
1936 return count;
1937}
1938
Richard Purdie2b97eab2006-10-06 18:32:18 +02001939static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1940
1941int snd_soc_dapm_sys_add(struct device *dev)
1942{
Troy Kisky12ef1932008-10-13 17:42:14 -07001943 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001944}
1945
1946static void snd_soc_dapm_sys_remove(struct device *dev)
1947{
Mark Brownaef90842009-05-16 17:53:16 +01001948 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001949}
1950
1951/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001952static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001953{
1954 struct snd_soc_dapm_widget *w, *next_w;
1955 struct snd_soc_dapm_path *p, *next_p;
1956
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001957 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1958 if (w->dapm != dapm)
1959 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001960 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001961 /*
1962 * remove source and sink paths associated to this widget.
1963 * While removing the path, remove reference to it from both
1964 * source and sink widgets so that path is removed only once.
1965 */
1966 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1967 list_del(&p->list_sink);
1968 list_del(&p->list_source);
1969 list_del(&p->list);
1970 kfree(p->long_name);
1971 kfree(p);
1972 }
1973 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1974 list_del(&p->list_sink);
1975 list_del(&p->list_source);
1976 list_del(&p->list);
1977 kfree(p->long_name);
1978 kfree(p);
1979 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001980 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001981 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001982 kfree(w);
1983 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001984}
1985
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001986static struct snd_soc_dapm_widget *dapm_find_widget(
1987 struct snd_soc_dapm_context *dapm, const char *pin,
1988 bool search_other_contexts)
1989{
1990 struct snd_soc_dapm_widget *w;
1991 struct snd_soc_dapm_widget *fallback = NULL;
1992
1993 list_for_each_entry(w, &dapm->card->widgets, list) {
1994 if (!strcmp(w->name, pin)) {
1995 if (w->dapm == dapm)
1996 return w;
1997 else
1998 fallback = w;
1999 }
2000 }
2001
2002 if (search_other_contexts)
2003 return fallback;
2004
2005 return NULL;
2006}
2007
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002008static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002009 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002010{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002011 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002012
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002013 if (!w) {
2014 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2015 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002016 }
2017
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002018 w->connected = status;
2019 if (status == 0)
2020 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002021
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002022 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002023}
2024
Richard Purdie2b97eab2006-10-06 18:32:18 +02002025/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002026 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002027 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002028 *
2029 * Walks all dapm audio paths and powers widgets according to their
2030 * stream or path usage.
2031 *
2032 * Returns 0 for success.
2033 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002034int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002035{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002036 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002037}
Liam Girdwooda5302182008-07-07 13:35:17 +01002038EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002039
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002040static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002041 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002042{
2043 struct snd_soc_dapm_path *path;
2044 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002045 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002046 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002047 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002048 const char *source;
2049 char prefixed_sink[80];
2050 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002051 int ret = 0;
2052
Mark Brown88e8b9a2011-03-02 18:18:24 +00002053 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002054 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2055 dapm->codec->name_prefix, route->sink);
2056 sink = prefixed_sink;
2057 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2058 dapm->codec->name_prefix, route->source);
2059 source = prefixed_source;
2060 } else {
2061 sink = route->sink;
2062 source = route->source;
2063 }
2064
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002065 /*
2066 * find src and dest widgets over all widgets but favor a widget from
2067 * current DAPM context
2068 */
2069 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002070 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002071 wtsink = w;
2072 if (w->dapm == dapm)
2073 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002074 continue;
2075 }
2076 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002077 wtsource = w;
2078 if (w->dapm == dapm)
2079 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002080 }
2081 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002082 /* use widget from another DAPM context if not found from this */
2083 if (!wsink)
2084 wsink = wtsink;
2085 if (!wsource)
2086 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002087
2088 if (wsource == NULL || wsink == NULL)
2089 return -ENODEV;
2090
2091 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2092 if (!path)
2093 return -ENOMEM;
2094
2095 path->source = wsource;
2096 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002097 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002098 INIT_LIST_HEAD(&path->list);
2099 INIT_LIST_HEAD(&path->list_source);
2100 INIT_LIST_HEAD(&path->list_sink);
2101
2102 /* check for external widgets */
2103 if (wsink->id == snd_soc_dapm_input) {
2104 if (wsource->id == snd_soc_dapm_micbias ||
2105 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002106 wsource->id == snd_soc_dapm_line ||
2107 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002108 wsink->ext = 1;
2109 }
2110 if (wsource->id == snd_soc_dapm_output) {
2111 if (wsink->id == snd_soc_dapm_spk ||
2112 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002113 wsink->id == snd_soc_dapm_line ||
2114 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002115 wsource->ext = 1;
2116 }
2117
2118 /* connect static paths */
2119 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002120 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002121 list_add(&path->list_sink, &wsink->sources);
2122 list_add(&path->list_source, &wsource->sinks);
2123 path->connect = 1;
2124 return 0;
2125 }
2126
2127 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002128 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002129 case snd_soc_dapm_adc:
2130 case snd_soc_dapm_dac:
2131 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002132 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002133 case snd_soc_dapm_input:
2134 case snd_soc_dapm_output:
2135 case snd_soc_dapm_micbias:
2136 case snd_soc_dapm_vmid:
2137 case snd_soc_dapm_pre:
2138 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002139 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002140 case snd_soc_dapm_aif_in:
2141 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002142 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002143 list_add(&path->list_sink, &wsink->sources);
2144 list_add(&path->list_source, &wsource->sinks);
2145 path->connect = 1;
2146 return 0;
2147 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002148 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002149 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002150 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002151 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002152 if (ret != 0)
2153 goto err;
2154 break;
2155 case snd_soc_dapm_switch:
2156 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002157 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002158 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002159 if (ret != 0)
2160 goto err;
2161 break;
2162 case snd_soc_dapm_hp:
2163 case snd_soc_dapm_mic:
2164 case snd_soc_dapm_line:
2165 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002166 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002167 list_add(&path->list_sink, &wsink->sources);
2168 list_add(&path->list_source, &wsource->sinks);
2169 path->connect = 0;
2170 return 0;
2171 }
2172 return 0;
2173
2174err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002175 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2176 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002177 kfree(path);
2178 return ret;
2179}
Mark Brown105f1c22008-05-13 14:52:19 +02002180
2181/**
Mark Brown105f1c22008-05-13 14:52:19 +02002182 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002183 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002184 * @route: audio routes
2185 * @num: number of routes
2186 *
2187 * Connects 2 dapm widgets together via a named audio path. The sink is
2188 * the widget receiving the audio signal, whilst the source is the sender
2189 * of the audio signal.
2190 *
2191 * Returns 0 for success else error. On error all resources can be freed
2192 * with a call to snd_soc_card_free().
2193 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002194int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002195 const struct snd_soc_dapm_route *route, int num)
2196{
2197 int i, ret;
2198
2199 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002200 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02002201 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002202 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2203 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02002204 return ret;
2205 }
2206 route++;
2207 }
2208
2209 return 0;
2210}
2211EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2212
2213/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002214 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002215 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002216 *
2217 * Checks the codec for any new dapm widgets and creates them if found.
2218 *
2219 * Returns 0 for success.
2220 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002221int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002222{
2223 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002224 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002225
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002226 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227 {
2228 if (w->new)
2229 continue;
2230
Stephen Warrenfad59882011-04-28 17:37:59 -06002231 if (w->num_kcontrols) {
2232 w->kcontrols = kzalloc(w->num_kcontrols *
2233 sizeof(struct snd_kcontrol *),
2234 GFP_KERNEL);
2235 if (!w->kcontrols)
2236 return -ENOMEM;
2237 }
2238
Richard Purdie2b97eab2006-10-06 18:32:18 +02002239 switch(w->id) {
2240 case snd_soc_dapm_switch:
2241 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002242 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01002243 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002244 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002245 break;
2246 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002247 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002248 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01002249 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002250 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002251 break;
2252 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01002253 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01002254 w->power_check = dapm_adc_check_power;
2255 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002256 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01002257 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01002258 w->power_check = dapm_dac_check_power;
2259 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002261 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01002262 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002263 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264 break;
2265 case snd_soc_dapm_input:
2266 case snd_soc_dapm_output:
2267 case snd_soc_dapm_micbias:
2268 case snd_soc_dapm_spk:
2269 case snd_soc_dapm_hp:
2270 case snd_soc_dapm_mic:
2271 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01002272 w->power_check = dapm_generic_check_power;
2273 break;
Mark Brown246d0a12009-04-22 18:24:55 +01002274 case snd_soc_dapm_supply:
2275 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002276 case snd_soc_dapm_vmid:
2277 case snd_soc_dapm_pre:
2278 case snd_soc_dapm_post:
2279 break;
2280 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002281
2282 /* Read the initial power state from the device */
2283 if (w->reg >= 0) {
Liam Girdwood35ca6602011-01-28 17:45:35 +00002284 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002285 val &= 1 << w->shift;
2286 if (w->invert)
2287 val = !val;
2288
2289 if (val)
2290 w->power = 1;
2291 }
2292
Richard Purdie2b97eab2006-10-06 18:32:18 +02002293 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002294
2295 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296 }
2297
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002298 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002299 return 0;
2300}
2301EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2302
Liam Girdwood7038dfd92011-02-01 20:58:15 +00002303const char *snd_soc_dapm_get_aif(struct snd_soc_dapm_context *dapm,
2304 const char *stream_name, enum snd_soc_dapm_type type)
2305{
2306 struct snd_soc_dapm_widget *w;
2307
Liam Girdwoodab1058a2011-01-31 20:33:07 +00002308 list_for_each_entry(w, &dapm->card->widgets, list) {
Liam Girdwood7038dfd92011-02-01 20:58:15 +00002309
2310 if (!w->sname)
2311 continue;
2312
2313 if (w->id == type && strstr(w->sname, stream_name))
2314 return w->name;
Liam Girdwoodab1058a2011-01-31 20:33:07 +00002315
Liam Girdwood7038dfd92011-02-01 20:58:15 +00002316 }
2317 return NULL;
2318}
2319EXPORT_SYMBOL_GPL(snd_soc_dapm_get_aif);
2320
Richard Purdie2b97eab2006-10-06 18:32:18 +02002321/**
2322 * snd_soc_dapm_get_volsw - dapm mixer get callback
2323 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002324 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002325 *
2326 * Callback to get the value of a dapm mixer control.
2327 *
2328 * Returns 0 for success.
2329 */
2330int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_value *ucontrol)
2332{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002333 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2334 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002335 struct soc_mixer_control *mc =
2336 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002337 unsigned int reg = mc->reg;
2338 unsigned int shift = mc->shift;
2339 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002340 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002341 unsigned int invert = mc->invert;
2342 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002343
Richard Purdie2b97eab2006-10-06 18:32:18 +02002344 ucontrol->value.integer.value[0] =
2345 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2346 if (shift != rshift)
2347 ucontrol->value.integer.value[1] =
2348 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2349 if (invert) {
2350 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002351 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002352 if (shift != rshift)
2353 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002354 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002355 }
2356
2357 return 0;
2358}
2359EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2360
2361/**
2362 * snd_soc_dapm_put_volsw - dapm mixer set callback
2363 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002364 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002365 *
2366 * Callback to set the value of a dapm mixer control.
2367 *
2368 * Returns 0 for success.
2369 */
2370int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2371 struct snd_ctl_elem_value *ucontrol)
2372{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002373 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2374 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2375 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002376 struct soc_mixer_control *mc =
2377 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002378 unsigned int reg = mc->reg;
2379 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002380 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002381 unsigned int mask = (1 << fls(max)) - 1;
2382 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002383 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002384 int connect, change;
2385 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002386 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002387
2388 val = (ucontrol->value.integer.value[0] & mask);
2389
2390 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002391 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002392 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002393 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002394
Stephen Warrenfafd2172011-04-28 17:38:00 -06002395 if (val)
2396 /* new connection */
2397 connect = invert ? 0 : 1;
2398 else
2399 /* old connection must be powered down */
2400 connect = invert ? 1 : 0;
2401
2402 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002403
Stephen Warrene9cf7042011-01-27 14:54:05 -07002404 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002405 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002406 for (wi = 0; wi < wlist->num_widgets; wi++) {
2407 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002408
Stephen Warrenfafd2172011-04-28 17:38:00 -06002409 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002410
Stephen Warrenfafd2172011-04-28 17:38:00 -06002411 update.kcontrol = kcontrol;
2412 update.widget = widget;
2413 update.reg = reg;
2414 update.mask = mask;
2415 update.val = val;
2416 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002417
Liam Girdwoode002c982011-02-08 11:45:20 +00002418 snd_soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002419
2420 widget->dapm->update = NULL;
2421 }
Mark Brown283375c2009-12-07 18:09:03 +00002422 }
2423
Stephen Warrenfafd2172011-04-28 17:38:00 -06002424 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002425 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002426}
2427EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2428
2429/**
2430 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2431 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002432 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002433 *
2434 * Callback to get the value of a dapm enumerated double mixer control.
2435 *
2436 * Returns 0 for success.
2437 */
2438int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2439 struct snd_ctl_elem_value *ucontrol)
2440{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002441 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2442 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002443 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002444 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002445
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002446 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002447 ;
2448 val = snd_soc_read(widget->codec, e->reg);
2449 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2450 if (e->shift_l != e->shift_r)
2451 ucontrol->value.enumerated.item[1] =
2452 (val >> e->shift_r) & (bitmask - 1);
2453
2454 return 0;
2455}
2456EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2457
2458/**
2459 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2460 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002461 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002462 *
2463 * Callback to set the value of a dapm enumerated double mixer control.
2464 *
2465 * Returns 0 for success.
2466 */
2467int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2468 struct snd_ctl_elem_value *ucontrol)
2469{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002470 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2471 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2472 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002473 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002474 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002475 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002476 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002477 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002478
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002479 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002480 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002481 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002482 return -EINVAL;
2483 mux = ucontrol->value.enumerated.item[0];
2484 val = mux << e->shift_l;
2485 mask = (bitmask - 1) << e->shift_l;
2486 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002487 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002488 return -EINVAL;
2489 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2490 mask |= (bitmask - 1) << e->shift_r;
2491 }
2492
Stephen Warrenfafd2172011-04-28 17:38:00 -06002493 mutex_lock(&codec->mutex);
2494
Mark Brown3a655772009-10-05 17:23:30 +01002495 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002496 if (change) {
2497 for (wi = 0; wi < wlist->num_widgets; wi++) {
2498 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002499
Stephen Warrenfafd2172011-04-28 17:38:00 -06002500 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002501
Stephen Warrenfafd2172011-04-28 17:38:00 -06002502 update.kcontrol = kcontrol;
2503 update.widget = widget;
2504 update.reg = e->reg;
2505 update.mask = mask;
2506 update.val = val;
2507 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002508
Liam Girdwoode002c982011-02-08 11:45:20 +00002509 snd_soc_dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002510
Stephen Warrenfafd2172011-04-28 17:38:00 -06002511 widget->dapm->update = NULL;
2512 }
2513 }
2514
2515 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002516 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002517}
2518EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2519
2520/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002521 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2522 * @kcontrol: mixer control
2523 * @ucontrol: control element information
2524 *
2525 * Returns 0 for success.
2526 */
2527int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2528 struct snd_ctl_elem_value *ucontrol)
2529{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002530 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2531 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002532
2533 ucontrol->value.enumerated.item[0] = widget->value;
2534
2535 return 0;
2536}
2537EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2538
2539/**
2540 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2541 * @kcontrol: mixer control
2542 * @ucontrol: control element information
2543 *
2544 * Returns 0 for success.
2545 */
2546int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2547 struct snd_ctl_elem_value *ucontrol)
2548{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002549 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2550 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2551 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002552 struct soc_enum *e =
2553 (struct soc_enum *)kcontrol->private_value;
2554 int change;
2555 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002556 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002557
2558 if (ucontrol->value.enumerated.item[0] >= e->max)
2559 return -EINVAL;
2560
Stephen Warrenfafd2172011-04-28 17:38:00 -06002561 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002562
2563 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002564 if (change) {
2565 for (wi = 0; wi < wlist->num_widgets; wi++) {
2566 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002567
Stephen Warrenfafd2172011-04-28 17:38:00 -06002568 widget->value = ucontrol->value.enumerated.item[0];
2569
Liam Girdwoode002c982011-02-08 11:45:20 +00002570 snd_soc_dapm_mux_update_power(widget, kcontrol, change,
2571 widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002572 }
2573 }
2574
2575 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002576 return ret;
2577}
2578EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2579
2580/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002581 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2582 * callback
2583 * @kcontrol: mixer control
2584 * @ucontrol: control element information
2585 *
2586 * Callback to get the value of a dapm semi enumerated double mixer control.
2587 *
2588 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2589 * used for handling bitfield coded enumeration for example.
2590 *
2591 * Returns 0 for success.
2592 */
2593int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_value *ucontrol)
2595{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002596 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2597 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002598 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002599 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002600
2601 reg_val = snd_soc_read(widget->codec, e->reg);
2602 val = (reg_val >> e->shift_l) & e->mask;
2603 for (mux = 0; mux < e->max; mux++) {
2604 if (val == e->values[mux])
2605 break;
2606 }
2607 ucontrol->value.enumerated.item[0] = mux;
2608 if (e->shift_l != e->shift_r) {
2609 val = (reg_val >> e->shift_r) & e->mask;
2610 for (mux = 0; mux < e->max; mux++) {
2611 if (val == e->values[mux])
2612 break;
2613 }
2614 ucontrol->value.enumerated.item[1] = mux;
2615 }
2616
2617 return 0;
2618}
2619EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2620
2621/**
2622 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2623 * callback
2624 * @kcontrol: mixer control
2625 * @ucontrol: control element information
2626 *
2627 * Callback to set the value of a dapm semi enumerated double mixer control.
2628 *
2629 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2630 * used for handling bitfield coded enumeration for example.
2631 *
2632 * Returns 0 for success.
2633 */
2634int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2635 struct snd_ctl_elem_value *ucontrol)
2636{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002637 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2638 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2639 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002640 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002641 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002642 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002643 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002644 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002645
2646 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2647 return -EINVAL;
2648 mux = ucontrol->value.enumerated.item[0];
2649 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2650 mask = e->mask << e->shift_l;
2651 if (e->shift_l != e->shift_r) {
2652 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2653 return -EINVAL;
2654 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2655 mask |= e->mask << e->shift_r;
2656 }
2657
Stephen Warrenfafd2172011-04-28 17:38:00 -06002658 mutex_lock(&codec->mutex);
2659
Mark Brown3a655772009-10-05 17:23:30 +01002660 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002661 if (change) {
2662 for (wi = 0; wi < wlist->num_widgets; wi++) {
2663 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002664
Stephen Warrenfafd2172011-04-28 17:38:00 -06002665 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002666
Stephen Warrenfafd2172011-04-28 17:38:00 -06002667 update.kcontrol = kcontrol;
2668 update.widget = widget;
2669 update.reg = e->reg;
2670 update.mask = mask;
2671 update.val = val;
2672 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002673
Liam Girdwoode002c982011-02-08 11:45:20 +00002674 snd_soc_dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002675
Stephen Warrenfafd2172011-04-28 17:38:00 -06002676 widget->dapm->update = NULL;
2677 }
2678 }
2679
2680 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002681 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002682}
2683EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2684
2685/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002686 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2687 *
2688 * @kcontrol: mixer control
2689 * @uinfo: control element information
2690 *
2691 * Callback to provide information about a pin switch control.
2692 */
2693int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2694 struct snd_ctl_elem_info *uinfo)
2695{
2696 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2697 uinfo->count = 1;
2698 uinfo->value.integer.min = 0;
2699 uinfo->value.integer.max = 1;
2700
2701 return 0;
2702}
2703EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2704
2705/**
2706 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2707 *
2708 * @kcontrol: mixer control
2709 * @ucontrol: Value
2710 */
2711int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2712 struct snd_ctl_elem_value *ucontrol)
2713{
2714 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2715 const char *pin = (const char *)kcontrol->private_value;
2716
2717 mutex_lock(&codec->mutex);
2718
2719 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002720 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002721
2722 mutex_unlock(&codec->mutex);
2723
2724 return 0;
2725}
2726EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2727
2728/**
2729 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2730 *
2731 * @kcontrol: mixer control
2732 * @ucontrol: Value
2733 */
2734int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2735 struct snd_ctl_elem_value *ucontrol)
2736{
2737 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2738 const char *pin = (const char *)kcontrol->private_value;
2739
2740 mutex_lock(&codec->mutex);
2741
2742 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002743 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002744 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002745 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002746
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002747 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002748
2749 mutex_unlock(&codec->mutex);
2750
2751 return 0;
2752}
2753EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2754
2755/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002756 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002757 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002758 * @widget: widget template
2759 *
2760 * Creates a new dapm control based upon the template.
2761 *
2762 * Returns 0 for success else error.
2763 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002764int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002765 const struct snd_soc_dapm_widget *widget)
2766{
2767 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002768 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002769
2770 if ((w = dapm_cnew_widget(widget)) == NULL)
2771 return -ENOMEM;
2772
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002773 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002774 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002775 name_len += 1 + strlen(dapm->codec->name_prefix);
2776 w->name = kmalloc(name_len, GFP_KERNEL);
2777 if (w->name == NULL) {
2778 kfree(w);
2779 return -ENOMEM;
2780 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002781 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002782 snprintf(w->name, name_len, "%s %s",
2783 dapm->codec->name_prefix, widget->name);
2784 else
2785 snprintf(w->name, name_len, "%s", widget->name);
2786
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002787 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002788 w->dapm = dapm;
2789 w->codec = dapm->codec;
Liam Girdwood7987a112011-01-31 19:52:42 +00002790 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002791 INIT_LIST_HEAD(&w->sources);
2792 INIT_LIST_HEAD(&w->sinks);
2793 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002794 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002795
2796 /* machine layer set ups unconnected pins and insertions */
2797 w->connected = 1;
2798 return 0;
2799}
2800EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2801
2802/**
Mark Brown4ba13272008-05-13 14:51:19 +02002803 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002804 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002805 * @widget: widget array
2806 * @num: number of widgets
2807 *
2808 * Creates new DAPM controls based upon the templates.
2809 *
2810 * Returns 0 for success else error.
2811 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002812int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002813 const struct snd_soc_dapm_widget *widget,
2814 int num)
2815{
2816 int i, ret;
2817
2818 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002819 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002820 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002821 dev_err(dapm->dev,
2822 "ASoC: Failed to create DAPM control %s: %d\n",
2823 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002824 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002825 }
Mark Brown4ba13272008-05-13 14:51:19 +02002826 widget++;
2827 }
2828 return 0;
2829}
2830EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2831
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002832static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002833 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002834{
2835 struct snd_soc_dapm_widget *w;
2836
Liam Girdwood13e13a32011-01-31 21:30:52 +00002837 if (!dapm)
2838 return;
2839
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002840 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002841 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002842 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002843 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002844 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2845 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002846 if (strstr(w->sname, stream)) {
2847 switch(event) {
2848 case SND_SOC_DAPM_STREAM_START:
2849 w->active = 1;
2850 break;
2851 case SND_SOC_DAPM_STREAM_STOP:
2852 w->active = 0;
2853 break;
2854 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002855 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002856 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002857 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2858 break;
2859 }
2860 }
2861 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002862
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002863 dapm_power_widgets(dapm, event);
Liam Girdwood670ff442011-03-30 23:27:27 +01002864 /* do we need to notify any clients that DAPM stream is complete */
2865 if (dapm->stream_event)
2866 dapm->stream_event(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002867}
2868
2869/**
2870 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2871 * @rtd: PCM runtime data
2872 * @stream: stream name
2873 * @event: stream event
2874 *
2875 * Sends a stream event to the dapm core. The core then makes any
2876 * necessary widget power changes.
2877 *
2878 * Returns 0 for success else error.
2879 */
2880int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2881 const char *stream, int event)
2882{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002883 if (stream == NULL)
2884 return 0;
Liam Girdwoodecc1a0d2011-02-16 16:24:17 +00002885
Liam Girdwood670ff442011-03-30 23:27:27 +01002886 mutex_lock(&rtd->card->dapm_mutex);
Patrick Lai7f100832011-09-02 11:41:45 -07002887 mutex_lock(&rtd->codec->mutex);
Liam Girdwood670ff442011-03-30 23:27:27 +01002888 soc_dapm_stream_event(&rtd->platform->dapm, stream, event);
2889 soc_dapm_stream_event(&rtd->codec->dapm, stream, event);
Patrick Lai7f100832011-09-02 11:41:45 -07002890 mutex_unlock(&rtd->codec->mutex);
Liam Girdwood670ff442011-03-30 23:27:27 +01002891 mutex_unlock(&rtd->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002892 return 0;
2893}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002894
2895/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002896 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002897 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002898 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002899 *
Mark Brown74b8f952009-06-06 11:26:15 +01002900 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002901 * a valid audio route and active audio stream.
2902 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2903 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002904 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002905int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002906{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002907 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002908}
Liam Girdwooda5302182008-07-07 13:35:17 +01002909EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002910
2911/**
Mark Brownda341832010-03-15 19:23:37 +00002912 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002913 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002914 * @pin: pin name
2915 *
2916 * Enables input/output pin regardless of any other state. This is
2917 * intended for use with microphone bias supplies used in microphone
2918 * jack detection.
2919 *
2920 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2921 * do any widget power switching.
2922 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002923int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2924 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002925{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002926 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002927
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002928 if (!w) {
2929 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2930 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002931 }
2932
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002933 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2934 w->connected = 1;
2935 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002936
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002937 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002938}
2939EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2940
2941/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002942 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002943 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002944 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002945 *
Mark Brown74b8f952009-06-06 11:26:15 +01002946 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002947 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2948 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002949 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002950int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2951 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002952{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002953 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002954}
2955EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2956
2957/**
Mark Brown5817b522008-09-24 11:23:11 +01002958 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002959 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002960 * @pin: pin name
2961 *
2962 * Marks the specified pin as being not connected, disabling it along
2963 * any parent or child widgets. At present this is identical to
2964 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2965 * additional things such as disabling controls which only affect
2966 * paths through the pin.
2967 *
2968 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2969 * do any widget power switching.
2970 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002971int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002972{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002973 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002974}
2975EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2976
2977/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002978 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002979 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002980 * @pin: audio signal pin endpoint (or start point)
2981 *
2982 * Get audio pin status - connected or disconnected.
2983 *
2984 * Returns 1 for connected otherwise 0.
2985 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002986int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2987 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002988{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002989 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002990
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002991 if (w)
2992 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002993
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002994 return 0;
2995}
Liam Girdwooda5302182008-07-07 13:35:17 +01002996EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002997
2998/**
Mark Brown1547aba2010-05-07 21:11:40 +01002999 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003000 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003001 * @pin: audio signal pin endpoint (or start point)
3002 *
3003 * Mark the given endpoint or pin as ignoring suspend. When the
3004 * system is disabled a path between two endpoints flagged as ignoring
3005 * suspend will not be disabled. The path must already be enabled via
3006 * normal means at suspend time, it will not be turned on if it was not
3007 * already enabled.
3008 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003009int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3010 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003011{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003012 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003013
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003014 if (!w) {
3015 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3016 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003017 }
3018
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003019 w->ignore_suspend = 1;
3020
3021 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003022}
3023EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3024
3025/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003026 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003027 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02003028 *
3029 * Free all dapm widgets and resources.
3030 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003031void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003032{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003033 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003034 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003035 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003036 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003037}
3038EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3039
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003040static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003041{
Mark Brown51737472009-06-22 13:16:51 +01003042 struct snd_soc_dapm_widget *w;
3043 LIST_HEAD(down_list);
3044 int powerdown = 0;
3045
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003046 list_for_each_entry(w, &dapm->card->widgets, list) {
3047 if (w->dapm != dapm)
3048 continue;
Mark Brown51737472009-06-22 13:16:51 +01003049 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003050 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003051 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003052 powerdown = 1;
3053 }
3054 }
3055
3056 /* If there were no widgets to power down we're already in
3057 * standby.
3058 */
3059 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08003060 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003061 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08003062 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003063 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003064}
Mark Brown51737472009-06-22 13:16:51 +01003065
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003066/*
3067 * snd_soc_dapm_shutdown - callback for system shutdown
3068 */
3069void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3070{
3071 struct snd_soc_codec *codec;
Liam Girdwood7987a112011-01-31 19:52:42 +00003072 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003073
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003074 list_for_each_entry(codec, &card->codec_dev_list, list) {
3075 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08003076 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003077 }
Liam Girdwood7987a112011-01-31 19:52:42 +00003078
3079 list_for_each_entry(platform, &card->platform_dev_list, list) {
3080 soc_dapm_shutdown_codec(&platform->dapm);
3081 snd_soc_dapm_set_bias_level(&platform->dapm, SND_SOC_BIAS_OFF);
3082 }
Mark Brown51737472009-06-22 13:16:51 +01003083}
3084
Richard Purdie2b97eab2006-10-06 18:32:18 +02003085/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003086MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003087MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003088MODULE_LICENSE("GPL v2");