blob: 424836e98a9832383fdcc0572dfc757b81451ffd [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
Liam Girdwood13e13a32011-01-31 21:30:52 +000032#undef DEBUG
Liam Girdwood7987a112011-01-31 19:52:42 +000033
Richard Purdie2b97eab2006-10-06 18:32:18 +020034#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080037#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020038#include <linux/delay.h>
39#include <linux/pm.h>
40#include <linux/bitops.h>
41#include <linux/platform_device.h>
42#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020043#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020045#include <sound/core.h>
46#include <sound/pcm.h>
47#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020048#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020049#include <sound/initval.h>
50
Mark Brown84e90932010-11-04 00:07:02 -040051#include <trace/events/asoc.h>
52
Liam Girdwoodab1058a2011-01-31 20:33:07 +000053#define PATH_MAX_HOPS 16
54
Liam Girdwoode002c982011-02-08 11:45:20 +000055int soc_dsp_runtime_update(struct snd_soc_dapm_widget *);
56
Richard Purdie2b97eab2006-10-06 18:32:18 +020057/* dapm power sequences - make this per codec in the future */
58static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010059 [snd_soc_dapm_pre] = 0,
60 [snd_soc_dapm_supply] = 1,
61 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010062 [snd_soc_dapm_aif_in] = 3,
63 [snd_soc_dapm_aif_out] = 3,
64 [snd_soc_dapm_mic] = 4,
65 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000066 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010067 [snd_soc_dapm_value_mux] = 5,
68 [snd_soc_dapm_dac] = 6,
69 [snd_soc_dapm_mixer] = 7,
70 [snd_soc_dapm_mixer_named_ctl] = 7,
71 [snd_soc_dapm_pga] = 8,
72 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060073 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010074 [snd_soc_dapm_hp] = 10,
75 [snd_soc_dapm_spk] = 10,
76 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020077};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000078
Richard Purdie2b97eab2006-10-06 18:32:18 +020079static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010080 [snd_soc_dapm_pre] = 0,
81 [snd_soc_dapm_adc] = 1,
82 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010083 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060084 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010085 [snd_soc_dapm_pga] = 4,
86 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010087 [snd_soc_dapm_mixer] = 5,
88 [snd_soc_dapm_dac] = 6,
89 [snd_soc_dapm_mic] = 7,
90 [snd_soc_dapm_micbias] = 8,
91 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000092 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010093 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010094 [snd_soc_dapm_aif_in] = 10,
95 [snd_soc_dapm_aif_out] = 10,
96 [snd_soc_dapm_supply] = 11,
97 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020098};
99
Troy Kisky12ef1932008-10-13 17:42:14 -0700100static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100101{
102 if (pop_time)
103 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
104}
105
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200106static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100107{
108 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200109 char *buf;
110
111 if (!pop_time)
112 return;
113
114 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
115 if (buf == NULL)
116 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100117
118 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200119 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100120 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100121 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200122
123 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100124}
125
Richard Purdie2b97eab2006-10-06 18:32:18 +0200126/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100127static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200128 const struct snd_soc_dapm_widget *_widget)
129{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100130 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200131}
132
Liam Girdwood7987a112011-01-31 19:52:42 +0000133static inline struct snd_card *dapm_get_card(struct snd_soc_dapm_context *dapm)
134{
135 if (dapm->codec)
136 return dapm->codec->card->snd_card;
137 else if (dapm->platform)
138 return dapm->platform->card->snd_card;
139 else
140 BUG();
141}
142
143static inline struct snd_soc_card *dapm_get_soc_card(
144 struct snd_soc_dapm_context *dapm)
145{
146 if (dapm->codec)
147 return dapm->codec->card;
148 else if (dapm->platform)
149 return dapm->platform->card;
150 else
151 BUG();
152}
153
Liam Girdwood35ca6602011-01-28 17:45:35 +0000154static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
155{
156 if (w->codec)
157 return snd_soc_read(w->codec, reg);
Liam Girdwood7987a112011-01-31 19:52:42 +0000158 else if (w->platform)
159 return snd_soc_platform_read(w->platform, reg);
Liam Girdwood35ca6602011-01-28 17:45:35 +0000160 return 0;
161}
162
163static int soc_widget_write(struct snd_soc_dapm_widget *w,int reg, int val)
164{
165 if (w->codec)
166 return snd_soc_write(w->codec, reg, val);
Liam Girdwood7987a112011-01-31 19:52:42 +0000167 else if (w->platform)
168 return snd_soc_platform_write(w->platform, reg, val);
Liam Girdwood35ca6602011-01-28 17:45:35 +0000169 return 0;
170}
171
172int soc_widget_update_bits(struct snd_soc_dapm_widget *w, unsigned short reg,
173 unsigned int mask, unsigned int value)
174{
175 int change;
176 unsigned int old, new;
177
178 old = soc_widget_read(w, reg);
179 new = (old & ~mask) | value;
180 change = old != new;
181 if (change)
182 soc_widget_write(w, reg, new);
183
184 return change;
185}
186
187int soc_widget_test_bits(struct snd_soc_dapm_widget *w, unsigned short reg,
188 unsigned int mask, unsigned int value)
189{
190 int change;
191 unsigned int old, new;
192
193 old = soc_widget_read(w, reg);
194 new = (old & ~mask) | value;
195 change = old != new;
196
197 return change;
198}
199
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000200/* reset 'walked' bit for each dapm path */
201static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
202{
203 struct snd_soc_dapm_path *p;
204
205 list_for_each_entry(p, &dapm->card->paths, list)
206 p->walked = 0;
207}
208
209static void dapm_clear_paths(struct snd_soc_dapm_context *dapm)
210{
211 struct snd_soc_dapm_path *p;
212 struct snd_soc_dapm_widget *w;
213 struct list_head *l;
214
Liam Girdwoode002c982011-02-08 11:45:20 +0000215 list_for_each(l, &dapm->card->paths) {
Liam Girdwoodab1058a2011-01-31 20:33:07 +0000216 p = list_entry(l, struct snd_soc_dapm_path, list);
217 p->length = 0;
218 }
219 list_for_each(l, &dapm->card->widgets) {
220 w = list_entry(l, struct snd_soc_dapm_widget, list);
221 w->hops = 0;
222 }
223 dapm_clear_walk(dapm);
224}
225
226/*
227 * find all the paths between source and sink
228 */
229static int dapm_find_playback_paths (struct snd_soc_dapm_context *dapm,
230 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
231 int hops)
232{
233 struct list_head *lp;
234 struct snd_soc_dapm_path *path;
235 int dist = 0;
236
237 if (hops > PATH_MAX_HOPS)
238 return 0;
239
240 if (source == sink) {
241 dev_dbg(dapm->dev," ! %d: valid route found\n", hops);
242 dapm->num_valid_paths++;
243 return 1;
244 }
245
246 if (source->hops && source->hops <= hops)
247 return 0;
248 source->hops = hops;
249
250 /* check all the output paths on this source widget by walking
251 * from source to sink */
252 list_for_each(lp, &source->sinks) {
253 path = list_entry(lp, struct snd_soc_dapm_path, list_source);
254
255 dev_dbg(dapm->dev," %c %d: %s -> %s -> %s\n",
256 path->connect ? '*' : ' ', hops,
257 source->name, path->name, path->sink->name);
258
259 /* been here before ? */
260 if (path->length && path->length <= hops)
261 continue;
262
263 /* check down the next path if connected */
264 if (path->sink && path->connect &&
265 dapm_find_playback_paths(dapm, path->sink, sink, hops + 1)) {
266 path->length = hops;
267 if (!dist || dist > path->length)
268 dist = path->length;
269 }
270 }
271
272 return dist;
273}
274
275static int dapm_find_capture_paths (struct snd_soc_dapm_context *dapm,
276 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
277 int hops)
278{
279 struct list_head *lp;
280 struct snd_soc_dapm_path *path;
281 int dist = 0;
282
283 if (hops > PATH_MAX_HOPS)
284 return 0;
285
286 if (source == sink) {
287 dev_dbg(dapm->dev," ! %d: valid route found\n", hops);
288 dapm->num_valid_paths++;
289 return 1;
290 }
291
292 if (sink->hops && sink->hops <= hops)
293 return 0;
294 sink->hops = hops;
295
296 /* check all the output paths on this source widget by walking from
297 * sink to source */
298 list_for_each(lp, &sink->sources) {
299 path = list_entry(lp, struct snd_soc_dapm_path, list_sink);
300
301 dev_dbg(dapm->dev," %c %d: %s <- %s <- %s\n",
302 path->connect ? '*' : ' ', hops,
303 sink->name, path->name, path->source->name);
304
305 /* been here before ? */
306 if (path->length && path->length <= hops)
307 continue;
308
309 /* check down the next path if connected */
310 if (path->source && path->connect &&
311 dapm_find_capture_paths(dapm, source, path->source, hops + 1)) {
312 path->length = hops;
313 if (!dist || dist > path->length)
314 dist = path->length;
315 }
316 }
317
318 return dist;
319}
320
321/*
322 * traverse the tree from sink to source via the shortest path
323 */
324static int dapm_get_playback_paths(struct snd_soc_dapm_context *dapm,
325 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink)
326{
327 dev_dbg(dapm->dev, "Playback: checking path from %s to %s\n",
328 source->name, sink->name);
329
330 dapm->num_valid_paths = 0;
331 dapm_find_playback_paths(dapm, source, sink, 1);
332 return dapm->num_valid_paths;
333}
334
335static int dapm_get_capture_paths(struct snd_soc_dapm_context *dapm,
336 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink)
337{
338 dev_dbg(dapm->dev, "check capture path to %s from %s\n",
339 source->name, sink->name);
340
341 dapm->num_valid_paths = 0;
342 dapm_find_capture_paths(dapm, sink, source, 1);
343 return dapm->num_valid_paths;
344}
345
346/**
347 * snd_soc_dapm_query_path - query audio path
348 * @dapm: the dapm context
349 * @source_name: source widget name
350 * @sink_name: sink widget name
351 * @stream: stream direction
352 *
353 * Queries DAPM graph as to whether an valid audio stream path exists between
354 * the source and sink widgets specified. This takes into account current
355 * mixer and mux kcontrol settings.
356 *
357 * Returns the number of valid paths or negative error.
358 */
359int snd_soc_dapm_query_path(struct snd_soc_dapm_context *dapm,
360 const char *source_name, const char *sink_name, int stream)
361{
362 struct snd_soc_dapm_widget *sink = NULL, *source = NULL;
363 struct list_head *l = NULL;
364 int routes;
365
366 /* find source and sink widgets */
367 list_for_each(l, &dapm->card->widgets) {
368 struct snd_soc_dapm_widget *w =
369 list_entry(l, struct snd_soc_dapm_widget, list);
370
371 if (!source && !strcmp(w->name, source_name)) {
372 source = w;
373 continue;
374 }
375
376 if (!sink && !strcmp(w->name, sink_name))
377 sink = w;
378 }
379
380 if (!source) {
381 dev_err(dapm->dev, "can't find source widget: %s\n", source_name);
382 return -EINVAL;
383 }
384 if (!sink) {
385 dev_err(dapm->dev, "can't find sink widget: %s\n", sink_name);
386 return -EINVAL;
387 }
388
389 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
390 routes = dapm_get_playback_paths(dapm, source, sink);
391 else
392 routes = dapm_get_capture_paths(dapm, source, sink);
393
394 dapm_clear_paths(dapm);
395 return routes;
396}
397EXPORT_SYMBOL_GPL(snd_soc_dapm_query_path);
Liam Girdwood35ca6602011-01-28 17:45:35 +0000398
Mark Brown452c5ea2009-05-17 21:41:23 +0100399/**
400 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800401 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100402 * @level: level to configure
403 *
404 * Configure the bias (power) levels for the SoC audio device.
405 *
406 * Returns 0 for success else error.
407 */
Mark Browned5a4c42011-02-18 11:12:42 -0800408static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200409 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100410{
Mark Browned5a4c42011-02-18 11:12:42 -0800411 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100412 int ret = 0;
413
Mark Brownf83fba82009-05-18 15:44:43 +0100414 switch (level) {
415 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200416 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100417 break;
418 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200419 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100420 break;
421 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200422 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100423 break;
424 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200425 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100426 break;
427 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200428 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100429 return -EINVAL;
430 }
431
Mark Brown84e90932010-11-04 00:07:02 -0400432 trace_snd_soc_bias_level_start(card, level);
433
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000434 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100435 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100436 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200437 if (dapm->codec && dapm->codec->driver->set_bias_level)
438 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100439 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200440 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100441 }
Mark Brown1badabd2010-12-04 12:41:04 +0000442 if (ret == 0) {
443 if (card && card->set_bias_level_post)
444 ret = card->set_bias_level_post(card, level);
445 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100446
Mark Brown84e90932010-11-04 00:07:02 -0400447 trace_snd_soc_bias_level_done(card, level);
448
Mark Brown452c5ea2009-05-17 21:41:23 +0100449 return ret;
450}
451
Richard Purdie2b97eab2006-10-06 18:32:18 +0200452/* set up initial codec paths */
453static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
454 struct snd_soc_dapm_path *p, int i)
455{
456 switch (w->id) {
457 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000458 case snd_soc_dapm_mixer:
459 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200460 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100461 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600462 w->kcontrol_news[i].private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -0400463 unsigned int reg = mc->reg;
464 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100465 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -0400466 unsigned int mask = (1 << fls(max)) - 1;
467 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200468
Liam Girdwood35ca6602011-01-28 17:45:35 +0000469 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200470 val = (val >> shift) & mask;
471
472 if ((invert && !val) || (!invert && val))
473 p->connect = 1;
474 else
475 p->connect = 0;
476 }
477 break;
478 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600479 struct soc_enum *e = (struct soc_enum *)
480 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200481 int val, item, bitmask;
482
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100483 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200484 ;
Liam Girdwood35ca6602011-01-28 17:45:35 +0000485 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200486 item = (val >> e->shift_l) & (bitmask - 1);
487
488 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100489 for (i = 0; i < e->max; i++) {
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100490 if (!(strcmp(p->name, snd_soc_get_enum_text(e, i))) && item == i)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200491 p->connect = 1;
492 }
493 }
494 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000495 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600496 struct soc_enum *e = (struct soc_enum *)
497 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000498
499 p->connect = 0;
500 /* since a virtual mux has no backing registers to
501 * decide which path to connect, it will try to match
502 * with the first enumeration. This is to ensure
503 * that the default mux choice (the first) will be
504 * correctly powered up during initialization.
505 */
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100506 if (!strcmp(p->name, snd_soc_get_enum_text(e, 0)))
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000507 p->connect = 1;
508 }
509 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200510 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200511 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600512 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200513 int val, item;
514
Liam Girdwood35ca6602011-01-28 17:45:35 +0000515 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200516 val = (val >> e->shift_l) & e->mask;
517 for (item = 0; item < e->max; item++) {
518 if (val == e->values[item])
519 break;
520 }
521
522 p->connect = 0;
523 for (i = 0; i < e->max; i++) {
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100524 if (!(strcmp(p->name, snd_soc_get_enum_text(e, i))) && item == i)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200525 p->connect = 1;
526 }
527 }
528 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200529 /* does not effect routing - always connected */
530 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600531 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200532 case snd_soc_dapm_output:
533 case snd_soc_dapm_adc:
534 case snd_soc_dapm_input:
535 case snd_soc_dapm_dac:
536 case snd_soc_dapm_micbias:
537 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100538 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100539 case snd_soc_dapm_aif_in:
540 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200541 p->connect = 1;
542 break;
543 /* does effect routing - dynamically connected */
544 case snd_soc_dapm_hp:
545 case snd_soc_dapm_mic:
546 case snd_soc_dapm_spk:
547 case snd_soc_dapm_line:
548 case snd_soc_dapm_pre:
549 case snd_soc_dapm_post:
550 p->connect = 0;
551 break;
552 }
553}
554
Mark Brown74b8f952009-06-06 11:26:15 +0100555/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200556static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200557 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
558 struct snd_soc_dapm_path *path, const char *control_name,
559 const struct snd_kcontrol_new *kcontrol)
560{
561 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
562 int i;
563
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100564 for (i = 0; i < e->max; i++) {
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100565 if (!(strcmp(control_name, snd_soc_get_enum_text(e, i)))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200566 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200567 list_add(&path->list_sink, &dest->sources);
568 list_add(&path->list_source, &src->sinks);
Liam Girdwooda8f13d82011-05-18 18:39:21 +0100569 path->name = (char*)snd_soc_get_enum_text(e, i);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200570 dapm_set_path_status(dest, path, 0);
571 return 0;
572 }
573 }
574
575 return -ENODEV;
576}
577
Mark Brown74b8f952009-06-06 11:26:15 +0100578/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200579static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200580 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
581 struct snd_soc_dapm_path *path, const char *control_name)
582{
583 int i;
584
585 /* search for mixer kcontrol */
586 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600587 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200588 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200589 list_add(&path->list_sink, &dest->sources);
590 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600591 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200592 dapm_set_path_status(dest, path, i);
593 return 0;
594 }
595 }
596 return -ENODEV;
597}
598
Stephen Warrenaf468002011-04-28 17:38:01 -0600599static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600600 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600601 const struct snd_kcontrol_new *kcontrol_new,
602 struct snd_kcontrol **kcontrol)
603{
604 struct snd_soc_dapm_widget *w;
605 int i;
606
607 *kcontrol = NULL;
608
609 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600610 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
611 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600612 for (i = 0; i < w->num_kcontrols; i++) {
613 if (&w->kcontrol_news[i] == kcontrol_new) {
614 if (w->kcontrols)
615 *kcontrol = w->kcontrols[i];
616 return 1;
617 }
618 }
619 }
620
621 return 0;
622}
623
Richard Purdie2b97eab2006-10-06 18:32:18 +0200624/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200625static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200626{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200627 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200628 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000629 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200630 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000631 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000632 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600633 struct snd_soc_dapm_widget_list *wlist;
634 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000635
636 if (dapm->codec)
637 prefix = dapm->codec->name_prefix;
638 else
639 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200640
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000641 if (prefix)
642 prefix_len = strlen(prefix) + 1;
643 else
644 prefix_len = 0;
645
Richard Purdie2b97eab2006-10-06 18:32:18 +0200646 /* add kcontrol */
647 for (i = 0; i < w->num_kcontrols; i++) {
648
649 /* match name */
650 list_for_each_entry(path, &w->sources, list_sink) {
651
652 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600653 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200654 continue;
655
Stephen Warrenfafd2172011-04-28 17:38:00 -0600656 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
657 sizeof(struct snd_soc_dapm_widget *),
658 wlist = kzalloc(wlistsize, GFP_KERNEL);
659 if (wlist == NULL) {
660 dev_err(dapm->dev,
661 "asoc: can't allocate widget list for %s\n",
662 w->name);
663 return -ENOMEM;
664 }
665 wlist->num_widgets = 1;
666 wlist->widgets[0] = w;
667
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000668 /* add dapm control with long name.
669 * for dapm_mixer this is the concatenation of the
670 * mixer and kcontrol name.
671 * for dapm_mixer_named_ctl this is simply the
672 * kcontrol name.
673 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600674 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000675 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000676 name_len += 1 + strlen(w->name);
677
Mark Brown219b93f2008-10-28 13:02:31 +0000678 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000679
Stephen Warrenfafd2172011-04-28 17:38:00 -0600680 if (path->long_name == NULL) {
681 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200682 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600683 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200684
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000685 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000686 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000687 /* The control will get a prefix from
688 * the control creation process but
689 * we're also using the same prefix
690 * for widgets so cut the prefix off
691 * the front of the widget name.
692 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000693 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000694 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600695 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000696 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000697 case snd_soc_dapm_mixer_named_ctl:
698 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600699 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000700 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000701 }
702
Mark Brown219b93f2008-10-28 13:02:31 +0000703 path->long_name[name_len - 1] = '\0';
704
Stephen Warrenfafd2172011-04-28 17:38:00 -0600705 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
706 wlist, path->long_name,
707 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200708 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200709 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200710 dev_err(dapm->dev,
711 "asoc: failed to add dapm kcontrol %s: %d\n",
712 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600713 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200714 kfree(path->long_name);
715 path->long_name = NULL;
716 return ret;
717 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600718 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200719 }
720 }
721 return ret;
722}
723
724/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200725static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200726{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200727 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200728 struct snd_soc_dapm_path *path = NULL;
729 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000730 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000731 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000732 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600733 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600734 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600735 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600736 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600737 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200738
Stephen Warrenaf468002011-04-28 17:38:01 -0600739 if (w->num_kcontrols != 1) {
740 dev_err(dapm->dev,
741 "asoc: mux %s has incorrect number of controls\n",
742 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200743 return -EINVAL;
744 }
745
Stephen Warren1007da02011-05-26 09:57:33 -0600746 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600747 &kcontrol);
748 if (kcontrol) {
749 wlist = kcontrol->private_data;
750 wlistentries = wlist->num_widgets + 1;
751 } else {
752 wlist = NULL;
753 wlistentries = 1;
754 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600755 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600756 wlistentries * sizeof(struct snd_soc_dapm_widget *),
757 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600758 if (wlist == NULL) {
759 dev_err(dapm->dev,
760 "asoc: can't allocate widget list for %s\n", w->name);
761 return -ENOMEM;
762 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600763 wlist->num_widgets = wlistentries;
764 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600765
Stephen Warrenaf468002011-04-28 17:38:01 -0600766 if (!kcontrol) {
Liam Girdwood4d74b362011-05-18 17:50:07 +0100767 if (dapm->codec && dapm->codec->name_prefix)
Stephen Warrenaf468002011-04-28 17:38:01 -0600768 prefix = dapm->codec->name_prefix;
769 else
Liam Girdwood4d74b362011-05-18 17:50:07 +0100770 prefix = w->name;
Mark Brownefb7ac32011-03-08 17:23:24 +0000771
Stephen Warrenaf468002011-04-28 17:38:01 -0600772 if (shared) {
773 name = w->kcontrol_news[0].name;
774 prefix_len = 0;
775 } else {
776 name = w->name;
777 if (prefix)
778 prefix_len = strlen(prefix) + 1;
779 else
780 prefix_len = 0;
781 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000782
Stephen Warrenaf468002011-04-28 17:38:01 -0600783 /*
784 * The control will get a prefix from the control creation
785 * process but we're also using the same prefix for widgets so
786 * cut the prefix off the front of the widget name.
787 */
788 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
789 name + prefix_len, prefix);
790 ret = snd_ctl_add(card, kcontrol);
791 if (ret < 0) {
792 dev_err(dapm->dev,
793 "asoc: failed to add kcontrol %s\n", w->name);
794 kfree(wlist);
795 return ret;
796 }
797 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200798
Stephen Warrenaf468002011-04-28 17:38:01 -0600799 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200800
Stephen Warrenfad59882011-04-28 17:37:59 -0600801 w->kcontrols[0] = kcontrol;
802
Richard Purdie2b97eab2006-10-06 18:32:18 +0200803 list_for_each_entry(path, &w->sources, list_sink)
804 path->kcontrol = kcontrol;
805
Stephen Warrenaf468002011-04-28 17:38:01 -0600806 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200807}
808
809/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200810static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200811{
Mark Browna6c65732010-03-03 17:45:21 +0000812 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200813 dev_err(w->dapm->dev,
814 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200815
Mark Browna6c65732010-03-03 17:45:21 +0000816 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200817}
818
Mark Brown99497882010-05-07 20:24:05 +0100819/* We implement power down on suspend by checking the power state of
820 * the ALSA card - when we are suspending the ALSA state for the card
821 * is set to D3.
822 */
823static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
824{
Mark Brown12ea2c72011-03-02 18:17:32 +0000825 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100826
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000827 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100828 case SNDRV_CTL_POWER_D3hot:
829 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100830 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200831 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
832 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100833 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100834 default:
835 return 1;
836 }
837}
838
Richard Purdie2b97eab2006-10-06 18:32:18 +0200839/*
840 * Recursively check for a completed path to an active or physically connected
841 * output widget. Returns number of complete paths.
842 */
843static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
844{
845 struct snd_soc_dapm_path *path;
846 int con = 0;
847
Mark Brown246d0a12009-04-22 18:24:55 +0100848 if (widget->id == snd_soc_dapm_supply)
849 return 0;
850
Mark Brown010ff262009-08-17 17:39:22 +0100851 switch (widget->id) {
852 case snd_soc_dapm_adc:
853 case snd_soc_dapm_aif_out:
854 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100855 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100856 default:
857 break;
858 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200859
860 if (widget->connected) {
861 /* connected pin ? */
862 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100863 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200864
865 /* connected jack or spk ? */
866 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300867 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100868 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200869 }
870
871 list_for_each_entry(path, &widget->sinks, list_source) {
872 if (path->walked)
873 continue;
874
875 if (path->sink && path->connect) {
876 path->walked = 1;
877 con += is_connected_output_ep(path->sink);
878 }
879 }
880
881 return con;
882}
883
884/*
885 * Recursively check for a completed path to an active or physically connected
886 * input widget. Returns number of complete paths.
887 */
888static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
889{
890 struct snd_soc_dapm_path *path;
891 int con = 0;
892
Mark Brown246d0a12009-04-22 18:24:55 +0100893 if (widget->id == snd_soc_dapm_supply)
894 return 0;
895
Richard Purdie2b97eab2006-10-06 18:32:18 +0200896 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100897 switch (widget->id) {
898 case snd_soc_dapm_dac:
899 case snd_soc_dapm_aif_in:
900 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100901 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100902 default:
903 break;
904 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200905
906 if (widget->connected) {
907 /* connected pin ? */
908 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100909 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200910
911 /* connected VMID/Bias for lower pops */
912 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100913 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200914
915 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300916 if (widget->id == snd_soc_dapm_mic ||
917 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100918 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200919 }
920
921 list_for_each_entry(path, &widget->sources, list_sink) {
922 if (path->walked)
923 continue;
924
925 if (path->source && path->connect) {
926 path->walked = 1;
927 con += is_connected_input_ep(path->source);
928 }
929 }
930
931 return con;
932}
933
934/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300935 * Handler for generic register modifier widget.
936 */
937int dapm_reg_event(struct snd_soc_dapm_widget *w,
938 struct snd_kcontrol *kcontrol, int event)
939{
940 unsigned int val;
941
942 if (SND_SOC_DAPM_EVENT_ON(event))
943 val = w->on_val;
944 else
945 val = w->off_val;
946
Liam Girdwood35ca6602011-01-28 17:45:35 +0000947 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300948 w->mask << w->shift, val << w->shift);
949
950 return 0;
951}
Mark Brown11589412008-07-29 11:42:23 +0100952EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300953
Mark Browncd0f2d42009-04-20 16:56:59 +0100954/* Generic check to see if a widget should be powered.
955 */
956static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
957{
958 int in, out;
959
960 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200961 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100962 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200963 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100964 return out != 0 && in != 0;
965}
966
Mark Brown6ea31b92009-04-20 17:15:41 +0100967/* Check to see if an ADC has power */
968static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
969{
970 int in;
971
972 if (w->active) {
973 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200974 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100975 return in != 0;
976 } else {
977 return dapm_generic_check_power(w);
978 }
979}
980
981/* Check to see if a DAC has power */
982static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
983{
984 int out;
985
986 if (w->active) {
987 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200988 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100989 return out != 0;
990 } else {
991 return dapm_generic_check_power(w);
992 }
993}
994
Mark Brown246d0a12009-04-22 18:24:55 +0100995/* Check to see if a power supply is needed */
996static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
997{
998 struct snd_soc_dapm_path *path;
999 int power = 0;
1000
1001 /* Check if one of our outputs is connected */
1002 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001003 if (path->connected &&
1004 !path->connected(path->source, path->sink))
1005 continue;
1006
Mark Brown30173582011-02-11 11:42:19 +00001007 if (!path->sink)
1008 continue;
1009
1010 if (path->sink->force) {
1011 power = 1;
1012 break;
1013 }
1014
1015 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +01001016 path->sink->power_check(path->sink)) {
1017 power = 1;
1018 break;
1019 }
1020 }
1021
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001022 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +01001023
1024 return power;
1025}
1026
Mark Brown38357ab2009-06-06 19:03:23 +01001027static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1028 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001029 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001030{
Mark Brown828a8422011-01-15 13:14:30 +00001031 int *sort;
1032
1033 if (power_up)
1034 sort = dapm_up_seq;
1035 else
1036 sort = dapm_down_seq;
1037
Mark Brown38357ab2009-06-06 19:03:23 +01001038 if (sort[a->id] != sort[b->id])
1039 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001040 if (a->subseq != b->subseq) {
1041 if (power_up)
1042 return a->subseq - b->subseq;
1043 else
1044 return b->subseq - a->subseq;
1045 }
Mark Brownb22ead22009-06-07 12:51:26 +01001046 if (a->reg != b->reg)
1047 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001048 if (a->dapm != b->dapm)
1049 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001050
Mark Brown38357ab2009-06-06 19:03:23 +01001051 return 0;
1052}
Mark Brown42aa3412009-03-01 19:21:10 +00001053
Mark Brown38357ab2009-06-06 19:03:23 +01001054/* Insert a widget in order into a DAPM power sequence. */
1055static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1056 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001057 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001058{
1059 struct snd_soc_dapm_widget *w;
1060
1061 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001062 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001063 list_add_tail(&new_widget->power_list, &w->power_list);
1064 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001065 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001066
Mark Brown38357ab2009-06-06 19:03:23 +01001067 list_add_tail(&new_widget->power_list, list);
1068}
Mark Brown42aa3412009-03-01 19:21:10 +00001069
Mark Brown68f89ad2010-11-03 23:51:49 -04001070static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1071 struct snd_soc_dapm_widget *w, int event)
1072{
1073 struct snd_soc_card *card = dapm->card;
1074 const char *ev_name;
1075 int power, ret;
1076
1077 switch (event) {
1078 case SND_SOC_DAPM_PRE_PMU:
1079 ev_name = "PRE_PMU";
1080 power = 1;
1081 break;
1082 case SND_SOC_DAPM_POST_PMU:
1083 ev_name = "POST_PMU";
1084 power = 1;
1085 break;
1086 case SND_SOC_DAPM_PRE_PMD:
1087 ev_name = "PRE_PMD";
1088 power = 0;
1089 break;
1090 case SND_SOC_DAPM_POST_PMD:
1091 ev_name = "POST_PMD";
1092 power = 0;
1093 break;
1094 default:
1095 BUG();
1096 return;
1097 }
1098
1099 if (w->power != power)
1100 return;
1101
1102 if (w->event && (w->event_flags & event)) {
1103 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1104 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001105 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001106 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001107 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001108 if (ret < 0)
1109 pr_err("%s: %s event failed: %d\n",
1110 ev_name, w->name, ret);
1111 }
1112}
1113
Mark Brownb22ead22009-06-07 12:51:26 +01001114/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001115static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001116 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001117{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001118 struct snd_soc_card *card = dapm->card;
Liam Girdwoodecc1a0d2011-02-16 16:24:17 +00001119 struct snd_soc_dapm_widget *w, *_w;
Mark Brown68f89ad2010-11-03 23:51:49 -04001120 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001121 unsigned int value = 0;
1122 unsigned int mask = 0;
1123 unsigned int cur_mask;
1124
Liam Girdwoodecc1a0d2011-02-16 16:24:17 +00001125 _w = list_first_entry(pending, struct snd_soc_dapm_widget,
1126 power_list);
1127 reg = _w->reg;
Mark Brownb22ead22009-06-07 12:51:26 +01001128
1129 list_for_each_entry(w, pending, power_list) {
1130 cur_mask = 1 << w->shift;
1131 BUG_ON(reg != w->reg);
1132
1133 if (w->invert)
1134 power = !w->power;
1135 else
1136 power = w->power;
1137
1138 mask |= cur_mask;
1139 if (power)
1140 value |= cur_mask;
1141
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001142 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001143 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1144 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001145
Mark Brown68f89ad2010-11-03 23:51:49 -04001146 /* Check for events */
1147 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1148 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001149 }
1150
Mark Brown81628102009-06-07 13:21:24 +01001151 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001152 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001153 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001154 value, mask, reg, card->pop_time);
1155 pop_wait(card->pop_time);
Liam Girdwoodecc1a0d2011-02-16 16:24:17 +00001156 soc_widget_update_bits(_w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001157 }
1158
1159 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001160 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1161 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001162 }
Mark Brown42aa3412009-03-01 19:21:10 +00001163}
1164
Mark Brownb22ead22009-06-07 12:51:26 +01001165/* Apply a DAPM power sequence.
1166 *
1167 * We walk over a pre-sorted list of widgets to apply power to. In
1168 * order to minimise the number of writes to the device required
1169 * multiple widgets will be updated in a single write where possible.
1170 * Currently anything that requires more than a single write is not
1171 * handled.
1172 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001173static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001174 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001175{
1176 struct snd_soc_dapm_widget *w, *n;
1177 LIST_HEAD(pending);
1178 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001179 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001180 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001181 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001182 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001183 int *sort;
1184
1185 if (power_up)
1186 sort = dapm_up_seq;
1187 else
1188 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001189
Mark Brownb22ead22009-06-07 12:51:26 +01001190 list_for_each_entry_safe(w, n, list, power_list) {
1191 ret = 0;
1192
1193 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001194 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001195 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001196 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001197 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001198
Mark Brown474b62d2011-01-18 16:14:44 +00001199 if (cur_dapm && cur_dapm->seq_notifier) {
1200 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1201 if (sort[i] == cur_sort)
1202 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001203 i,
1204 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001205 }
1206
Mark Brownb22ead22009-06-07 12:51:26 +01001207 INIT_LIST_HEAD(&pending);
1208 cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001209 cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001210 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001211 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001212 }
1213
Mark Brown163cac02009-06-07 10:12:52 +01001214 switch (w->id) {
1215 case snd_soc_dapm_pre:
1216 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001217 list_for_each_entry_safe_continue(w, n, list,
1218 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001219
Mark Brownb22ead22009-06-07 12:51:26 +01001220 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001221 ret = w->event(w,
1222 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001223 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001224 ret = w->event(w,
1225 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001226 break;
1227
1228 case snd_soc_dapm_post:
1229 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001230 list_for_each_entry_safe_continue(w, n, list,
1231 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001232
Mark Brownb22ead22009-06-07 12:51:26 +01001233 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001234 ret = w->event(w,
1235 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001236 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001237 ret = w->event(w,
1238 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001239 break;
1240
Mark Brown163cac02009-06-07 10:12:52 +01001241 default:
Mark Brown81628102009-06-07 13:21:24 +01001242 /* Queue it up for application */
1243 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001244 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001245 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001246 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001247 list_move(&w->power_list, &pending);
1248 break;
Mark Brown163cac02009-06-07 10:12:52 +01001249 }
Mark Brownb22ead22009-06-07 12:51:26 +01001250
1251 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001252 dev_err(w->dapm->dev,
1253 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001254 }
Mark Brownb22ead22009-06-07 12:51:26 +01001255
1256 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001257 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001258
1259 if (cur_dapm && cur_dapm->seq_notifier) {
1260 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1261 if (sort[i] == cur_sort)
1262 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001263 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001264 }
Mark Brown163cac02009-06-07 10:12:52 +01001265}
1266
Mark Brown97404f22010-12-14 16:13:57 +00001267static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1268{
1269 struct snd_soc_dapm_update *update = dapm->update;
1270 struct snd_soc_dapm_widget *w;
1271 int ret;
1272
1273 if (!update)
1274 return;
1275
1276 w = update->widget;
1277
1278 if (w->event &&
1279 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1280 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1281 if (ret != 0)
1282 pr_err("%s DAPM pre-event failed: %d\n",
1283 w->name, ret);
1284 }
1285
1286 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1287 update->val);
1288 if (ret < 0)
1289 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1290
1291 if (w->event &&
1292 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1293 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1294 if (ret != 0)
1295 pr_err("%s DAPM post-event failed: %d\n",
1296 w->name, ret);
1297 }
1298}
1299
Mark Brown9d0624a2011-02-18 11:49:43 -08001300/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1301 * they're changing state.
1302 */
1303static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1304{
1305 struct snd_soc_dapm_context *d = data;
1306 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001307
Mark Brown9d0624a2011-02-18 11:49:43 -08001308 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1309 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1310 if (ret != 0)
1311 dev_err(d->dev,
1312 "Failed to turn on bias: %d\n", ret);
1313 }
1314
1315 /* If we're changing to all on or all off then prepare */
1316 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1317 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1318 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1319 if (ret != 0)
1320 dev_err(d->dev,
1321 "Failed to prepare bias: %d\n", ret);
1322 }
1323}
1324
1325/* Async callback run prior to DAPM sequences - brings to their final
1326 * state.
1327 */
1328static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1329{
1330 struct snd_soc_dapm_context *d = data;
1331 int ret;
1332
1333 /* If we just powered the last thing off drop to standby bias */
1334 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1335 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1336 if (ret != 0)
1337 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1338 ret);
1339 }
1340
1341 /* If we're in standby and can support bias off then do that */
1342 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
1343 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1344 if (ret != 0)
1345 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1346 }
1347
1348 /* If we just powered up then move to active bias */
1349 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1350 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1351 if (ret != 0)
1352 dev_err(d->dev, "Failed to apply active bias: %d\n",
1353 ret);
1354 }
1355}
Mark Brown97404f22010-12-14 16:13:57 +00001356
Mark Brown42aa3412009-03-01 19:21:10 +00001357/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001358 * Scan each dapm widget for complete audio path.
1359 * A complete path is a route that has valid endpoints i.e.:-
1360 *
1361 * o DAC to output pin.
1362 * o Input Pin to ADC.
1363 * o Input pin to Output pin (bypass, sidetone)
1364 * o DAC to ADC (loopback).
1365 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001366static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001367{
Mark Brown12ea2c72011-03-02 18:17:32 +00001368 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001369 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001370 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001371 LIST_HEAD(up_list);
1372 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001373 LIST_HEAD(async_domain);
Mark Brown38357ab2009-06-06 19:03:23 +01001374 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001375
Mark Brown84e90932010-11-04 00:07:02 -04001376 trace_snd_soc_dapm_start(card);
1377
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001378 list_for_each_entry(d, &card->dapm_list, list)
Jarkko Nikulaea77b942011-05-26 16:32:18 +03001379 if (d->n_widgets || d->codec == NULL)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001380 d->dev_power = 0;
1381
Mark Brown6d3ddc82009-05-16 17:47:29 +01001382 /* Check which widgets we need to power and store them in
1383 * lists indicating if they should be powered up or down.
1384 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001385 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001386 switch (w->id) {
1387 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001388 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001389 break;
1390 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001391 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001392 break;
1393
1394 default:
1395 if (!w->power_check)
1396 continue;
1397
Mark Brown99497882010-05-07 20:24:05 +01001398 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001399 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001400 else
1401 power = 1;
1402 if (power)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001403 w->dapm->dev_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +01001404
Mark Brown6d3ddc82009-05-16 17:47:29 +01001405 if (w->power == power)
1406 continue;
1407
Mark Brown84e90932010-11-04 00:07:02 -04001408 trace_snd_soc_dapm_widget_power(w, power);
1409
Mark Brown6d3ddc82009-05-16 17:47:29 +01001410 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001411 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001412 else
Mark Brown828a8422011-01-15 13:14:30 +00001413 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001414
1415 w->power = power;
1416 break;
1417 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001418 }
1419
Mark Brownb14b76a2009-08-17 11:55:38 +01001420 /* If there are no DAPM widgets then try to figure out power from the
1421 * event type.
1422 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001423 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001424 switch (event) {
1425 case SND_SOC_DAPM_STREAM_START:
1426 case SND_SOC_DAPM_STREAM_RESUME:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001427 dapm->dev_power = 1;
Mark Brownb14b76a2009-08-17 11:55:38 +01001428 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001429 case SND_SOC_DAPM_STREAM_STOP:
Liam Girdwood7987a112011-01-31 19:52:42 +00001430#warning need re-work
1431 if (dapm->codec)
1432 dapm->dev_power = !!dapm->codec->active;
1433 else
1434 dapm->dev_power = 0;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001435 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001436 case SND_SOC_DAPM_STREAM_SUSPEND:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001437 dapm->dev_power = 0;
Mark Brown50b6bce2009-11-23 13:11:53 +00001438 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001439 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001440 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +00001441 case SND_SOC_BIAS_STANDBY:
1442 case SND_SOC_BIAS_OFF:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001443 dapm->dev_power = 0;
Mark Browna96ca332010-01-19 22:49:43 +00001444 break;
1445 default:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001446 dapm->dev_power = 1;
Mark Browna96ca332010-01-19 22:49:43 +00001447 break;
1448 }
Mark Brown50b6bce2009-11-23 13:11:53 +00001449 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001450 default:
1451 break;
1452 }
1453 }
1454
Mark Brown52ba67b2011-04-04 21:05:11 +09001455 /* Force all contexts in the card to the same bias state */
1456 power = 0;
1457 list_for_each_entry(d, &card->dapm_list, list)
1458 if (d->dev_power)
1459 power = 1;
1460 list_for_each_entry(d, &card->dapm_list, list)
1461 d->dev_power = power;
1462
1463
Mark Brown9d0624a2011-02-18 11:49:43 -08001464 /* Run all the bias changes in parallel */
1465 list_for_each_entry(d, &dapm->card->dapm_list, list)
1466 async_schedule_domain(dapm_pre_sequence_async, d,
1467 &async_domain);
1468 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001469
Mark Brown6d3ddc82009-05-16 17:47:29 +01001470 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001471 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001472
Mark Brown97404f22010-12-14 16:13:57 +00001473 dapm_widget_update(dapm);
1474
Mark Brown6d3ddc82009-05-16 17:47:29 +01001475 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001476 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001477
Mark Brown9d0624a2011-02-18 11:49:43 -08001478 /* Run all the bias changes in parallel */
1479 list_for_each_entry(d, &dapm->card->dapm_list, list)
1480 async_schedule_domain(dapm_post_sequence_async, d,
1481 &async_domain);
1482 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001483
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001484 pop_dbg(dapm->dev, card->pop_time,
1485 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001486 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001487
Mark Brown84e90932010-11-04 00:07:02 -04001488 trace_snd_soc_dapm_done(card);
1489
Mark Brown42aa3412009-03-01 19:21:10 +00001490 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001491}
1492
Mark Brown79fb9382009-08-21 16:38:13 +01001493#ifdef CONFIG_DEBUG_FS
1494static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1495{
1496 file->private_data = inode->i_private;
1497 return 0;
1498}
1499
1500static ssize_t dapm_widget_power_read_file(struct file *file,
1501 char __user *user_buf,
1502 size_t count, loff_t *ppos)
1503{
1504 struct snd_soc_dapm_widget *w = file->private_data;
1505 char *buf;
1506 int in, out;
1507 ssize_t ret;
1508 struct snd_soc_dapm_path *p = NULL;
1509
1510 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1511 if (!buf)
1512 return -ENOMEM;
1513
1514 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001515 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001516 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001517 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001518
Mark Brownd033c362009-12-04 15:25:56 +00001519 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001520 w->name, w->power ? "On" : "Off", in, out);
1521
Mark Brownd033c362009-12-04 15:25:56 +00001522 if (w->reg >= 0)
1523 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1524 " - R%d(0x%x) bit %d",
1525 w->reg, w->reg, w->shift);
1526
1527 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1528
Mark Brown3eef08b2009-09-14 16:49:00 +01001529 if (w->sname)
1530 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1531 w->sname,
1532 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001533
1534 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001535 if (p->connected && !p->connected(w, p->sink))
1536 continue;
1537
Mark Brown79fb9382009-08-21 16:38:13 +01001538 if (p->connect)
1539 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001540 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001541 p->name ? p->name : "static",
1542 p->source->name);
1543 }
1544 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001545 if (p->connected && !p->connected(w, p->sink))
1546 continue;
1547
Mark Brown79fb9382009-08-21 16:38:13 +01001548 if (p->connect)
1549 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001550 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001551 p->name ? p->name : "static",
1552 p->sink->name);
1553 }
1554
1555 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1556
1557 kfree(buf);
1558 return ret;
1559}
1560
1561static const struct file_operations dapm_widget_power_fops = {
1562 .open = dapm_widget_power_open_file,
1563 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001564 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001565};
1566
Mark Brownef49e4f2011-04-04 20:48:13 +09001567static int dapm_bias_open_file(struct inode *inode, struct file *file)
1568{
1569 file->private_data = inode->i_private;
1570 return 0;
1571}
1572
1573static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1574 size_t count, loff_t *ppos)
1575{
1576 struct snd_soc_dapm_context *dapm = file->private_data;
1577 char *level;
1578
1579 switch (dapm->bias_level) {
1580 case SND_SOC_BIAS_ON:
1581 level = "On\n";
1582 break;
1583 case SND_SOC_BIAS_PREPARE:
1584 level = "Prepare\n";
1585 break;
1586 case SND_SOC_BIAS_STANDBY:
1587 level = "Standby\n";
1588 break;
1589 case SND_SOC_BIAS_OFF:
1590 level = "Off\n";
1591 break;
1592 default:
1593 BUG();
1594 level = "Unknown\n";
1595 break;
1596 }
1597
1598 return simple_read_from_buffer(user_buf, count, ppos, level,
1599 strlen(level));
1600}
1601
1602static const struct file_operations dapm_bias_fops = {
1603 .open = dapm_bias_open_file,
1604 .read = dapm_bias_read_file,
1605 .llseek = default_llseek,
1606};
1607
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001608void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1609 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001610{
Mark Brown79fb9382009-08-21 16:38:13 +01001611 struct dentry *d;
1612
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001613 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1614
1615 if (!dapm->debugfs_dapm) {
1616 printk(KERN_WARNING
1617 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001618 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001619 }
Mark Brown79fb9382009-08-21 16:38:13 +01001620
Mark Brownef49e4f2011-04-04 20:48:13 +09001621 d = debugfs_create_file("bias_level", 0444,
1622 dapm->debugfs_dapm, dapm,
1623 &dapm_bias_fops);
1624 if (!d)
1625 dev_warn(dapm->dev,
1626 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001627}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001628
1629static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1630{
1631 struct snd_soc_dapm_context *dapm = w->dapm;
1632 struct dentry *d;
1633
1634 if (!dapm->debugfs_dapm || !w->name)
1635 return;
1636
1637 d = debugfs_create_file(w->name, 0444,
1638 dapm->debugfs_dapm, w,
1639 &dapm_widget_power_fops);
1640 if (!d)
1641 dev_warn(w->dapm->dev,
1642 "ASoC: Failed to create %s debugfs file\n",
1643 w->name);
1644}
1645
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001646static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1647{
1648 debugfs_remove_recursive(dapm->debugfs_dapm);
1649}
1650
Mark Brown79fb9382009-08-21 16:38:13 +01001651#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001652void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1653 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001654{
1655}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001656
1657static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1658{
1659}
1660
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001661static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1662{
1663}
1664
Mark Brown79fb9382009-08-21 16:38:13 +01001665#endif
1666
Richard Purdie2b97eab2006-10-06 18:32:18 +02001667/* test and update the power status of a mux widget */
Liam Girdwoode002c982011-02-08 11:45:20 +00001668int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001669 struct snd_kcontrol *kcontrol, int change,
1670 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001671{
1672 struct snd_soc_dapm_path *path;
1673 int found = 0;
1674
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001675 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001676 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001677 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001678 return -ENODEV;
1679
Mark Brown3a655772009-10-05 17:23:30 +01001680 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001681 return 0;
1682
1683 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001684 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Liam Girdwoode002c982011-02-08 11:45:20 +00001685
Richard Purdie2b97eab2006-10-06 18:32:18 +02001686 if (path->kcontrol != kcontrol)
1687 continue;
1688
Liam Girdwooda8f13d82011-05-18 18:39:21 +01001689 if (!path->name || !snd_soc_get_enum_text(e, mux))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001690 continue;
1691
1692 found = 1;
1693 /* we now need to match the string in the enum to the path */
Liam Girdwooda8f13d82011-05-18 18:39:21 +01001694 if (!(strcmp(path->name, snd_soc_get_enum_text(e, mux))))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001695 path->connect = 1; /* new connection */
1696 else
1697 path->connect = 0; /* old connection must be powered down */
1698 }
1699
Liam Girdwoode002c982011-02-08 11:45:20 +00001700 if (found) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001701 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwoode002c982011-02-08 11:45:20 +00001702 soc_dsp_runtime_update(widget);
1703 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001704
1705 return 0;
1706}
Liam Girdwoode002c982011-02-08 11:45:20 +00001707EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001708
Milan plzik1b075e32008-01-10 14:39:46 +01001709/* test and update the power status of a mixer or switch widget */
Liam Girdwoode002c982011-02-08 11:45:20 +00001710int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1711 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001712{
1713 struct snd_soc_dapm_path *path;
1714 int found = 0;
1715
Milan plzik1b075e32008-01-10 14:39:46 +01001716 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001717 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001718 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001719 return -ENODEV;
1720
Richard Purdie2b97eab2006-10-06 18:32:18 +02001721 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001722 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001723 if (path->kcontrol != kcontrol)
1724 continue;
1725
1726 /* found, now check type */
1727 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001728 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001729 break;
1730 }
1731
Liam Girdwoode002c982011-02-08 11:45:20 +00001732 if (found) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001733 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwoode002c982011-02-08 11:45:20 +00001734 soc_dsp_runtime_update(widget);
1735 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001736
1737 return 0;
1738}
Liam Girdwoode002c982011-02-08 11:45:20 +00001739EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001740
1741/* show dapm widget status in sys fs */
Liam Girdwood7987a112011-01-31 19:52:42 +00001742static ssize_t widget_show(struct snd_soc_dapm_context *dapm,
1743 const char *name, char *buf, ssize_t count)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001744{
Richard Purdie2b97eab2006-10-06 18:32:18 +02001745 struct snd_soc_dapm_widget *w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001746 char *state = "not set";
1747
Liam Girdwood7987a112011-01-31 19:52:42 +00001748 count += sprintf(buf + count, "\n%s\n", name);
1749
Liam Girdwoodab1058a2011-01-31 20:33:07 +00001750 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001751
1752 /* only display widgets that burnm power */
1753 switch (w->id) {
1754 case snd_soc_dapm_hp:
1755 case snd_soc_dapm_mic:
1756 case snd_soc_dapm_spk:
1757 case snd_soc_dapm_line:
1758 case snd_soc_dapm_micbias:
1759 case snd_soc_dapm_dac:
1760 case snd_soc_dapm_adc:
1761 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001762 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001763 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001764 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001765 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001766 if (w->name)
1767 count += sprintf(buf + count, "%s: %s\n",
1768 w->name, w->power ? "On":"Off");
1769 break;
1770 default:
1771 break;
1772 }
1773 }
1774
Liam Girdwood7987a112011-01-31 19:52:42 +00001775 switch (dapm->bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001776 case SND_SOC_BIAS_ON:
1777 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001778 break;
Mark Brown0be98982008-05-19 12:31:28 +02001779 case SND_SOC_BIAS_PREPARE:
1780 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001781 break;
Mark Brown0be98982008-05-19 12:31:28 +02001782 case SND_SOC_BIAS_STANDBY:
1783 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001784 break;
Mark Brown0be98982008-05-19 12:31:28 +02001785 case SND_SOC_BIAS_OFF:
1786 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001787 break;
1788 }
1789 count += sprintf(buf + count, "PM State: %s\n", state);
1790
1791 return count;
1792}
1793
Liam Girdwood7987a112011-01-31 19:52:42 +00001794/* show dapm widget status in sys fs */
1795static ssize_t dapm_widget_show(struct device *dev,
1796 struct device_attribute *attr, char *buf)
1797{
1798 struct snd_soc_pcm_runtime *rtd =
1799 container_of(dev, struct snd_soc_pcm_runtime, dev);
1800 struct snd_soc_codec *codec = rtd->codec;
1801 struct snd_soc_platform *platform = rtd->platform;
1802 ssize_t count = 0;
1803
1804 count += widget_show(&codec->dapm, codec->name, buf, count);
1805 count += widget_show(&platform->dapm, platform->name, buf, count);
1806 return count;
1807}
1808
Richard Purdie2b97eab2006-10-06 18:32:18 +02001809static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1810
1811int snd_soc_dapm_sys_add(struct device *dev)
1812{
Troy Kisky12ef1932008-10-13 17:42:14 -07001813 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001814}
1815
1816static void snd_soc_dapm_sys_remove(struct device *dev)
1817{
Mark Brownaef90842009-05-16 17:53:16 +01001818 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001819}
1820
1821/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001822static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001823{
1824 struct snd_soc_dapm_widget *w, *next_w;
1825 struct snd_soc_dapm_path *p, *next_p;
1826
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001827 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1828 if (w->dapm != dapm)
1829 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001830 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001831 /*
1832 * remove source and sink paths associated to this widget.
1833 * While removing the path, remove reference to it from both
1834 * source and sink widgets so that path is removed only once.
1835 */
1836 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1837 list_del(&p->list_sink);
1838 list_del(&p->list_source);
1839 list_del(&p->list);
1840 kfree(p->long_name);
1841 kfree(p);
1842 }
1843 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1844 list_del(&p->list_sink);
1845 list_del(&p->list_source);
1846 list_del(&p->list);
1847 kfree(p->long_name);
1848 kfree(p);
1849 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001850 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001851 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001852 kfree(w);
1853 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001854}
1855
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001856static struct snd_soc_dapm_widget *dapm_find_widget(
1857 struct snd_soc_dapm_context *dapm, const char *pin,
1858 bool search_other_contexts)
1859{
1860 struct snd_soc_dapm_widget *w;
1861 struct snd_soc_dapm_widget *fallback = NULL;
1862
1863 list_for_each_entry(w, &dapm->card->widgets, list) {
1864 if (!strcmp(w->name, pin)) {
1865 if (w->dapm == dapm)
1866 return w;
1867 else
1868 fallback = w;
1869 }
1870 }
1871
1872 if (search_other_contexts)
1873 return fallback;
1874
1875 return NULL;
1876}
1877
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001878static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001879 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001880{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001881 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001882
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001883 if (!w) {
1884 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1885 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001886 }
1887
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001888 w->connected = status;
1889 if (status == 0)
1890 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001891
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001892 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001893}
1894
Richard Purdie2b97eab2006-10-06 18:32:18 +02001895/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001896 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001897 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001898 *
1899 * Walks all dapm audio paths and powers widgets according to their
1900 * stream or path usage.
1901 *
1902 * Returns 0 for success.
1903 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001904int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001905{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001906 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001907}
Liam Girdwooda5302182008-07-07 13:35:17 +01001908EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001909
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001910static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001911 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001912{
1913 struct snd_soc_dapm_path *path;
1914 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001915 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001916 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001917 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001918 const char *source;
1919 char prefixed_sink[80];
1920 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001921 int ret = 0;
1922
Mark Brown88e8b9a2011-03-02 18:18:24 +00001923 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001924 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1925 dapm->codec->name_prefix, route->sink);
1926 sink = prefixed_sink;
1927 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1928 dapm->codec->name_prefix, route->source);
1929 source = prefixed_source;
1930 } else {
1931 sink = route->sink;
1932 source = route->source;
1933 }
1934
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001935 /*
1936 * find src and dest widgets over all widgets but favor a widget from
1937 * current DAPM context
1938 */
1939 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001940 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001941 wtsink = w;
1942 if (w->dapm == dapm)
1943 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001944 continue;
1945 }
1946 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001947 wtsource = w;
1948 if (w->dapm == dapm)
1949 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001950 }
1951 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001952 /* use widget from another DAPM context if not found from this */
1953 if (!wsink)
1954 wsink = wtsink;
1955 if (!wsource)
1956 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001957
1958 if (wsource == NULL || wsink == NULL)
1959 return -ENODEV;
1960
1961 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1962 if (!path)
1963 return -ENOMEM;
1964
1965 path->source = wsource;
1966 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001967 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001968 INIT_LIST_HEAD(&path->list);
1969 INIT_LIST_HEAD(&path->list_source);
1970 INIT_LIST_HEAD(&path->list_sink);
1971
1972 /* check for external widgets */
1973 if (wsink->id == snd_soc_dapm_input) {
1974 if (wsource->id == snd_soc_dapm_micbias ||
1975 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001976 wsource->id == snd_soc_dapm_line ||
1977 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001978 wsink->ext = 1;
1979 }
1980 if (wsource->id == snd_soc_dapm_output) {
1981 if (wsink->id == snd_soc_dapm_spk ||
1982 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001983 wsink->id == snd_soc_dapm_line ||
1984 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001985 wsource->ext = 1;
1986 }
1987
1988 /* connect static paths */
1989 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001990 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001991 list_add(&path->list_sink, &wsink->sources);
1992 list_add(&path->list_source, &wsource->sinks);
1993 path->connect = 1;
1994 return 0;
1995 }
1996
1997 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001998 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001999 case snd_soc_dapm_adc:
2000 case snd_soc_dapm_dac:
2001 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002002 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002003 case snd_soc_dapm_input:
2004 case snd_soc_dapm_output:
2005 case snd_soc_dapm_micbias:
2006 case snd_soc_dapm_vmid:
2007 case snd_soc_dapm_pre:
2008 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002009 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002010 case snd_soc_dapm_aif_in:
2011 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002012 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002013 list_add(&path->list_sink, &wsink->sources);
2014 list_add(&path->list_source, &wsource->sinks);
2015 path->connect = 1;
2016 return 0;
2017 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002018 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002019 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002020 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002021 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002022 if (ret != 0)
2023 goto err;
2024 break;
2025 case snd_soc_dapm_switch:
2026 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002027 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002028 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002029 if (ret != 0)
2030 goto err;
2031 break;
2032 case snd_soc_dapm_hp:
2033 case snd_soc_dapm_mic:
2034 case snd_soc_dapm_line:
2035 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002036 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002037 list_add(&path->list_sink, &wsink->sources);
2038 list_add(&path->list_source, &wsource->sinks);
2039 path->connect = 0;
2040 return 0;
2041 }
2042 return 0;
2043
2044err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002045 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2046 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002047 kfree(path);
2048 return ret;
2049}
Mark Brown105f1c22008-05-13 14:52:19 +02002050
2051/**
Mark Brown105f1c22008-05-13 14:52:19 +02002052 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002053 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002054 * @route: audio routes
2055 * @num: number of routes
2056 *
2057 * Connects 2 dapm widgets together via a named audio path. The sink is
2058 * the widget receiving the audio signal, whilst the source is the sender
2059 * of the audio signal.
2060 *
2061 * Returns 0 for success else error. On error all resources can be freed
2062 * with a call to snd_soc_card_free().
2063 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002064int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002065 const struct snd_soc_dapm_route *route, int num)
2066{
2067 int i, ret;
2068
2069 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002070 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02002071 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002072 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2073 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02002074 return ret;
2075 }
2076 route++;
2077 }
2078
2079 return 0;
2080}
2081EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2082
2083/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002084 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002085 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002086 *
2087 * Checks the codec for any new dapm widgets and creates them if found.
2088 *
2089 * Returns 0 for success.
2090 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002091int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002092{
2093 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002094 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002095
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002096 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002097 {
2098 if (w->new)
2099 continue;
2100
Stephen Warrenfad59882011-04-28 17:37:59 -06002101 if (w->num_kcontrols) {
2102 w->kcontrols = kzalloc(w->num_kcontrols *
2103 sizeof(struct snd_kcontrol *),
2104 GFP_KERNEL);
2105 if (!w->kcontrols)
2106 return -ENOMEM;
2107 }
2108
Richard Purdie2b97eab2006-10-06 18:32:18 +02002109 switch(w->id) {
2110 case snd_soc_dapm_switch:
2111 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002112 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01002113 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002114 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002115 break;
2116 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002117 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002118 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01002119 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002120 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002121 break;
2122 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01002123 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01002124 w->power_check = dapm_adc_check_power;
2125 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002126 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01002127 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01002128 w->power_check = dapm_dac_check_power;
2129 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002130 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002131 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01002132 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002133 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002134 break;
2135 case snd_soc_dapm_input:
2136 case snd_soc_dapm_output:
2137 case snd_soc_dapm_micbias:
2138 case snd_soc_dapm_spk:
2139 case snd_soc_dapm_hp:
2140 case snd_soc_dapm_mic:
2141 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01002142 w->power_check = dapm_generic_check_power;
2143 break;
Mark Brown246d0a12009-04-22 18:24:55 +01002144 case snd_soc_dapm_supply:
2145 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002146 case snd_soc_dapm_vmid:
2147 case snd_soc_dapm_pre:
2148 case snd_soc_dapm_post:
2149 break;
2150 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002151
2152 /* Read the initial power state from the device */
2153 if (w->reg >= 0) {
Liam Girdwood35ca6602011-01-28 17:45:35 +00002154 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002155 val &= 1 << w->shift;
2156 if (w->invert)
2157 val = !val;
2158
2159 if (val)
2160 w->power = 1;
2161 }
2162
Richard Purdie2b97eab2006-10-06 18:32:18 +02002163 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002164
2165 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002166 }
2167
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002168 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002169 return 0;
2170}
2171EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2172
Liam Girdwood7038dfd92011-02-01 20:58:15 +00002173const char *snd_soc_dapm_get_aif(struct snd_soc_dapm_context *dapm,
2174 const char *stream_name, enum snd_soc_dapm_type type)
2175{
2176 struct snd_soc_dapm_widget *w;
2177
Liam Girdwoodab1058a2011-01-31 20:33:07 +00002178 list_for_each_entry(w, &dapm->card->widgets, list) {
Liam Girdwood7038dfd92011-02-01 20:58:15 +00002179
2180 if (!w->sname)
2181 continue;
2182
2183 if (w->id == type && strstr(w->sname, stream_name))
2184 return w->name;
Liam Girdwoodab1058a2011-01-31 20:33:07 +00002185
Liam Girdwood7038dfd92011-02-01 20:58:15 +00002186 }
2187 return NULL;
2188}
2189EXPORT_SYMBOL_GPL(snd_soc_dapm_get_aif);
2190
Richard Purdie2b97eab2006-10-06 18:32:18 +02002191/**
2192 * snd_soc_dapm_get_volsw - dapm mixer get callback
2193 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002194 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002195 *
2196 * Callback to get the value of a dapm mixer control.
2197 *
2198 * Returns 0 for success.
2199 */
2200int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2201 struct snd_ctl_elem_value *ucontrol)
2202{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002203 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2204 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002205 struct soc_mixer_control *mc =
2206 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002207 unsigned int reg = mc->reg;
2208 unsigned int shift = mc->shift;
2209 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002210 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002211 unsigned int invert = mc->invert;
2212 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002213
Richard Purdie2b97eab2006-10-06 18:32:18 +02002214 ucontrol->value.integer.value[0] =
2215 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2216 if (shift != rshift)
2217 ucontrol->value.integer.value[1] =
2218 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2219 if (invert) {
2220 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002221 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002222 if (shift != rshift)
2223 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002224 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002225 }
2226
2227 return 0;
2228}
2229EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2230
2231/**
2232 * snd_soc_dapm_put_volsw - dapm mixer set callback
2233 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002234 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002235 *
2236 * Callback to set the value of a dapm mixer control.
2237 *
2238 * Returns 0 for success.
2239 */
2240int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
2242{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002243 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2244 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2245 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002246 struct soc_mixer_control *mc =
2247 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002248 unsigned int reg = mc->reg;
2249 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002250 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002251 unsigned int mask = (1 << fls(max)) - 1;
2252 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002253 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002254 int connect, change;
2255 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002256 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002257
2258 val = (ucontrol->value.integer.value[0] & mask);
2259
2260 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002261 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002262 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002263 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002264
Stephen Warrenfafd2172011-04-28 17:38:00 -06002265 if (val)
2266 /* new connection */
2267 connect = invert ? 0 : 1;
2268 else
2269 /* old connection must be powered down */
2270 connect = invert ? 1 : 0;
2271
2272 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002273
Stephen Warrene9cf7042011-01-27 14:54:05 -07002274 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002275 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002276 for (wi = 0; wi < wlist->num_widgets; wi++) {
2277 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002278
Stephen Warrenfafd2172011-04-28 17:38:00 -06002279 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002280
Stephen Warrenfafd2172011-04-28 17:38:00 -06002281 update.kcontrol = kcontrol;
2282 update.widget = widget;
2283 update.reg = reg;
2284 update.mask = mask;
2285 update.val = val;
2286 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002287
Liam Girdwoode002c982011-02-08 11:45:20 +00002288 snd_soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002289
2290 widget->dapm->update = NULL;
2291 }
Mark Brown283375c2009-12-07 18:09:03 +00002292 }
2293
Stephen Warrenfafd2172011-04-28 17:38:00 -06002294 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002295 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296}
2297EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2298
2299/**
2300 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2301 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002302 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002303 *
2304 * Callback to get the value of a dapm enumerated double mixer control.
2305 *
2306 * Returns 0 for success.
2307 */
2308int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2309 struct snd_ctl_elem_value *ucontrol)
2310{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002311 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2312 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002313 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002314 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002315
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002316 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002317 ;
2318 val = snd_soc_read(widget->codec, e->reg);
2319 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2320 if (e->shift_l != e->shift_r)
2321 ucontrol->value.enumerated.item[1] =
2322 (val >> e->shift_r) & (bitmask - 1);
2323
2324 return 0;
2325}
2326EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2327
2328/**
2329 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2330 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002331 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002332 *
2333 * Callback to set the value of a dapm enumerated double mixer control.
2334 *
2335 * Returns 0 for success.
2336 */
2337int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2338 struct snd_ctl_elem_value *ucontrol)
2339{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002340 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2341 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2342 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002343 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002344 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002345 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002346 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002347 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002348
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002349 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002350 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002351 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002352 return -EINVAL;
2353 mux = ucontrol->value.enumerated.item[0];
2354 val = mux << e->shift_l;
2355 mask = (bitmask - 1) << e->shift_l;
2356 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002357 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002358 return -EINVAL;
2359 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2360 mask |= (bitmask - 1) << e->shift_r;
2361 }
2362
Stephen Warrenfafd2172011-04-28 17:38:00 -06002363 mutex_lock(&codec->mutex);
2364
Mark Brown3a655772009-10-05 17:23:30 +01002365 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002366 if (change) {
2367 for (wi = 0; wi < wlist->num_widgets; wi++) {
2368 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002369
Stephen Warrenfafd2172011-04-28 17:38:00 -06002370 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002371
Stephen Warrenfafd2172011-04-28 17:38:00 -06002372 update.kcontrol = kcontrol;
2373 update.widget = widget;
2374 update.reg = e->reg;
2375 update.mask = mask;
2376 update.val = val;
2377 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002378
Liam Girdwoode002c982011-02-08 11:45:20 +00002379 snd_soc_dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002380
Stephen Warrenfafd2172011-04-28 17:38:00 -06002381 widget->dapm->update = NULL;
2382 }
2383 }
2384
2385 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002386 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002387}
2388EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2389
2390/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002391 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2392 * @kcontrol: mixer control
2393 * @ucontrol: control element information
2394 *
2395 * Returns 0 for success.
2396 */
2397int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2398 struct snd_ctl_elem_value *ucontrol)
2399{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002400 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2401 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002402
2403 ucontrol->value.enumerated.item[0] = widget->value;
2404
2405 return 0;
2406}
2407EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2408
2409/**
2410 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2411 * @kcontrol: mixer control
2412 * @ucontrol: control element information
2413 *
2414 * Returns 0 for success.
2415 */
2416int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2417 struct snd_ctl_elem_value *ucontrol)
2418{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002419 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2420 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2421 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002422 struct soc_enum *e =
2423 (struct soc_enum *)kcontrol->private_value;
2424 int change;
2425 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002426 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002427
2428 if (ucontrol->value.enumerated.item[0] >= e->max)
2429 return -EINVAL;
2430
Stephen Warrenfafd2172011-04-28 17:38:00 -06002431 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002432
2433 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002434 if (change) {
2435 for (wi = 0; wi < wlist->num_widgets; wi++) {
2436 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002437
Stephen Warrenfafd2172011-04-28 17:38:00 -06002438 widget->value = ucontrol->value.enumerated.item[0];
2439
Liam Girdwoode002c982011-02-08 11:45:20 +00002440 snd_soc_dapm_mux_update_power(widget, kcontrol, change,
2441 widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002442 }
2443 }
2444
2445 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002446 return ret;
2447}
2448EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2449
2450/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002451 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2452 * callback
2453 * @kcontrol: mixer control
2454 * @ucontrol: control element information
2455 *
2456 * Callback to get the value of a dapm semi enumerated double mixer control.
2457 *
2458 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2459 * used for handling bitfield coded enumeration for example.
2460 *
2461 * Returns 0 for success.
2462 */
2463int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2464 struct snd_ctl_elem_value *ucontrol)
2465{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002466 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2467 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002468 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002469 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002470
2471 reg_val = snd_soc_read(widget->codec, e->reg);
2472 val = (reg_val >> e->shift_l) & e->mask;
2473 for (mux = 0; mux < e->max; mux++) {
2474 if (val == e->values[mux])
2475 break;
2476 }
2477 ucontrol->value.enumerated.item[0] = mux;
2478 if (e->shift_l != e->shift_r) {
2479 val = (reg_val >> e->shift_r) & e->mask;
2480 for (mux = 0; mux < e->max; mux++) {
2481 if (val == e->values[mux])
2482 break;
2483 }
2484 ucontrol->value.enumerated.item[1] = mux;
2485 }
2486
2487 return 0;
2488}
2489EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2490
2491/**
2492 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2493 * callback
2494 * @kcontrol: mixer control
2495 * @ucontrol: control element information
2496 *
2497 * Callback to set the value of a dapm semi enumerated double mixer control.
2498 *
2499 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2500 * used for handling bitfield coded enumeration for example.
2501 *
2502 * Returns 0 for success.
2503 */
2504int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2505 struct snd_ctl_elem_value *ucontrol)
2506{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002507 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2508 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2509 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002510 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002511 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002512 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002513 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002514 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002515
2516 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2517 return -EINVAL;
2518 mux = ucontrol->value.enumerated.item[0];
2519 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2520 mask = e->mask << e->shift_l;
2521 if (e->shift_l != e->shift_r) {
2522 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2523 return -EINVAL;
2524 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2525 mask |= e->mask << e->shift_r;
2526 }
2527
Stephen Warrenfafd2172011-04-28 17:38:00 -06002528 mutex_lock(&codec->mutex);
2529
Mark Brown3a655772009-10-05 17:23:30 +01002530 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002531 if (change) {
2532 for (wi = 0; wi < wlist->num_widgets; wi++) {
2533 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002534
Stephen Warrenfafd2172011-04-28 17:38:00 -06002535 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002536
Stephen Warrenfafd2172011-04-28 17:38:00 -06002537 update.kcontrol = kcontrol;
2538 update.widget = widget;
2539 update.reg = e->reg;
2540 update.mask = mask;
2541 update.val = val;
2542 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002543
Liam Girdwoode002c982011-02-08 11:45:20 +00002544 snd_soc_dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002545
Stephen Warrenfafd2172011-04-28 17:38:00 -06002546 widget->dapm->update = NULL;
2547 }
2548 }
2549
2550 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002551 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002552}
2553EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2554
2555/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002556 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2557 *
2558 * @kcontrol: mixer control
2559 * @uinfo: control element information
2560 *
2561 * Callback to provide information about a pin switch control.
2562 */
2563int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2564 struct snd_ctl_elem_info *uinfo)
2565{
2566 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2567 uinfo->count = 1;
2568 uinfo->value.integer.min = 0;
2569 uinfo->value.integer.max = 1;
2570
2571 return 0;
2572}
2573EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2574
2575/**
2576 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2577 *
2578 * @kcontrol: mixer control
2579 * @ucontrol: Value
2580 */
2581int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2582 struct snd_ctl_elem_value *ucontrol)
2583{
2584 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2585 const char *pin = (const char *)kcontrol->private_value;
2586
2587 mutex_lock(&codec->mutex);
2588
2589 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002590 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002591
2592 mutex_unlock(&codec->mutex);
2593
2594 return 0;
2595}
2596EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2597
2598/**
2599 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2600 *
2601 * @kcontrol: mixer control
2602 * @ucontrol: Value
2603 */
2604int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2605 struct snd_ctl_elem_value *ucontrol)
2606{
2607 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2608 const char *pin = (const char *)kcontrol->private_value;
2609
2610 mutex_lock(&codec->mutex);
2611
2612 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002613 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002614 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002615 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002616
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002617 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002618
2619 mutex_unlock(&codec->mutex);
2620
2621 return 0;
2622}
2623EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2624
2625/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002626 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002627 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002628 * @widget: widget template
2629 *
2630 * Creates a new dapm control based upon the template.
2631 *
2632 * Returns 0 for success else error.
2633 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002634int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002635 const struct snd_soc_dapm_widget *widget)
2636{
2637 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002638 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002639
2640 if ((w = dapm_cnew_widget(widget)) == NULL)
2641 return -ENOMEM;
2642
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002643 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002644 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002645 name_len += 1 + strlen(dapm->codec->name_prefix);
2646 w->name = kmalloc(name_len, GFP_KERNEL);
2647 if (w->name == NULL) {
2648 kfree(w);
2649 return -ENOMEM;
2650 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002651 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002652 snprintf(w->name, name_len, "%s %s",
2653 dapm->codec->name_prefix, widget->name);
2654 else
2655 snprintf(w->name, name_len, "%s", widget->name);
2656
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002657 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002658 w->dapm = dapm;
2659 w->codec = dapm->codec;
Liam Girdwood7987a112011-01-31 19:52:42 +00002660 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002661 INIT_LIST_HEAD(&w->sources);
2662 INIT_LIST_HEAD(&w->sinks);
2663 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002664 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002665
2666 /* machine layer set ups unconnected pins and insertions */
2667 w->connected = 1;
2668 return 0;
2669}
2670EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2671
2672/**
Mark Brown4ba13272008-05-13 14:51:19 +02002673 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002674 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002675 * @widget: widget array
2676 * @num: number of widgets
2677 *
2678 * Creates new DAPM controls based upon the templates.
2679 *
2680 * Returns 0 for success else error.
2681 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002682int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002683 const struct snd_soc_dapm_widget *widget,
2684 int num)
2685{
2686 int i, ret;
2687
2688 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002689 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002690 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002691 dev_err(dapm->dev,
2692 "ASoC: Failed to create DAPM control %s: %d\n",
2693 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002694 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002695 }
Mark Brown4ba13272008-05-13 14:51:19 +02002696 widget++;
2697 }
2698 return 0;
2699}
2700EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2701
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002702static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002703 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002704{
2705 struct snd_soc_dapm_widget *w;
2706
Liam Girdwood13e13a32011-01-31 21:30:52 +00002707 if (!dapm)
2708 return;
2709
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002710 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002711 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002712 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002713 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002714 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2715 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002716 if (strstr(w->sname, stream)) {
2717 switch(event) {
2718 case SND_SOC_DAPM_STREAM_START:
2719 w->active = 1;
2720 break;
2721 case SND_SOC_DAPM_STREAM_STOP:
2722 w->active = 0;
2723 break;
2724 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002725 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002726 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002727 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2728 break;
2729 }
2730 }
2731 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002732
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002733 dapm_power_widgets(dapm, event);
Liam Girdwood670ff442011-03-30 23:27:27 +01002734 /* do we need to notify any clients that DAPM stream is complete */
2735 if (dapm->stream_event)
2736 dapm->stream_event(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002737}
2738
2739/**
2740 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2741 * @rtd: PCM runtime data
2742 * @stream: stream name
2743 * @event: stream event
2744 *
2745 * Sends a stream event to the dapm core. The core then makes any
2746 * necessary widget power changes.
2747 *
2748 * Returns 0 for success else error.
2749 */
2750int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2751 const char *stream, int event)
2752{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002753 if (stream == NULL)
2754 return 0;
Liam Girdwoodecc1a0d2011-02-16 16:24:17 +00002755
Liam Girdwood670ff442011-03-30 23:27:27 +01002756 mutex_lock(&rtd->card->dapm_mutex);
2757
2758 soc_dapm_stream_event(&rtd->platform->dapm, stream, event);
2759 soc_dapm_stream_event(&rtd->codec->dapm, stream, event);
2760
2761 mutex_unlock(&rtd->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002762 return 0;
2763}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002764
2765/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002766 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002767 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002768 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002769 *
Mark Brown74b8f952009-06-06 11:26:15 +01002770 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002771 * a valid audio route and active audio stream.
2772 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2773 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002774 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002775int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002776{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002777 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002778}
Liam Girdwooda5302182008-07-07 13:35:17 +01002779EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002780
2781/**
Mark Brownda341832010-03-15 19:23:37 +00002782 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002783 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002784 * @pin: pin name
2785 *
2786 * Enables input/output pin regardless of any other state. This is
2787 * intended for use with microphone bias supplies used in microphone
2788 * jack detection.
2789 *
2790 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2791 * do any widget power switching.
2792 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002793int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2794 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002795{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002796 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002797
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002798 if (!w) {
2799 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2800 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002801 }
2802
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002803 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2804 w->connected = 1;
2805 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002806
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002807 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002808}
2809EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2810
2811/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002812 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002813 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002814 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002815 *
Mark Brown74b8f952009-06-06 11:26:15 +01002816 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002817 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2818 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002819 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002820int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2821 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002822{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002823 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002824}
2825EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2826
2827/**
Mark Brown5817b522008-09-24 11:23:11 +01002828 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002829 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002830 * @pin: pin name
2831 *
2832 * Marks the specified pin as being not connected, disabling it along
2833 * any parent or child widgets. At present this is identical to
2834 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2835 * additional things such as disabling controls which only affect
2836 * paths through the pin.
2837 *
2838 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2839 * do any widget power switching.
2840 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002841int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002842{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002843 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002844}
2845EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2846
2847/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002848 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002849 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002850 * @pin: audio signal pin endpoint (or start point)
2851 *
2852 * Get audio pin status - connected or disconnected.
2853 *
2854 * Returns 1 for connected otherwise 0.
2855 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002856int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2857 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002858{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002859 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002860
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002861 if (w)
2862 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002863
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002864 return 0;
2865}
Liam Girdwooda5302182008-07-07 13:35:17 +01002866EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002867
2868/**
Mark Brown1547aba2010-05-07 21:11:40 +01002869 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002870 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002871 * @pin: audio signal pin endpoint (or start point)
2872 *
2873 * Mark the given endpoint or pin as ignoring suspend. When the
2874 * system is disabled a path between two endpoints flagged as ignoring
2875 * suspend will not be disabled. The path must already be enabled via
2876 * normal means at suspend time, it will not be turned on if it was not
2877 * already enabled.
2878 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002879int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2880 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002881{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002882 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002883
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002884 if (!w) {
2885 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2886 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002887 }
2888
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002889 w->ignore_suspend = 1;
2890
2891 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002892}
2893EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2894
2895/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002896 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002897 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002898 *
2899 * Free all dapm widgets and resources.
2900 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002901void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002902{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002903 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002904 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002905 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002906 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002907}
2908EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2909
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002910static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002911{
Mark Brown51737472009-06-22 13:16:51 +01002912 struct snd_soc_dapm_widget *w;
2913 LIST_HEAD(down_list);
2914 int powerdown = 0;
2915
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002916 list_for_each_entry(w, &dapm->card->widgets, list) {
2917 if (w->dapm != dapm)
2918 continue;
Mark Brown51737472009-06-22 13:16:51 +01002919 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002920 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002921 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002922 powerdown = 1;
2923 }
2924 }
2925
2926 /* If there were no widgets to power down we're already in
2927 * standby.
2928 */
2929 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002930 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002931 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002932 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002933 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002934}
Mark Brown51737472009-06-22 13:16:51 +01002935
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002936/*
2937 * snd_soc_dapm_shutdown - callback for system shutdown
2938 */
2939void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2940{
2941 struct snd_soc_codec *codec;
Liam Girdwood7987a112011-01-31 19:52:42 +00002942 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002943
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002944 list_for_each_entry(codec, &card->codec_dev_list, list) {
2945 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002946 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002947 }
Liam Girdwood7987a112011-01-31 19:52:42 +00002948
2949 list_for_each_entry(platform, &card->platform_dev_list, list) {
2950 soc_dapm_shutdown_codec(&platform->dapm);
2951 snd_soc_dapm_set_bias_level(&platform->dapm, SND_SOC_BIAS_OFF);
2952 }
Mark Brown51737472009-06-22 13:16:51 +01002953}
2954
Richard Purdie2b97eab2006-10-06 18:32:18 +02002955/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002956MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002957MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2958MODULE_LICENSE("GPL");