blob: 54fa2e5e307877528113bf4da2e3b4e60365a606 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080035#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020036#include <linux/delay.h>
37#include <linux/pm.h>
38#include <linux/bitops.h>
39#include <linux/platform_device.h>
40#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020041#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020043#include <sound/core.h>
44#include <sound/pcm.h>
45#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020046#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020047#include <sound/initval.h>
48
Mark Brown84e90932010-11-04 00:07:02 -040049#include <trace/events/asoc.h>
50
Richard Purdie2b97eab2006-10-06 18:32:18 +020051/* dapm power sequences - make this per codec in the future */
52static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010053 [snd_soc_dapm_pre] = 0,
54 [snd_soc_dapm_supply] = 1,
55 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010056 [snd_soc_dapm_aif_in] = 3,
57 [snd_soc_dapm_aif_out] = 3,
58 [snd_soc_dapm_mic] = 4,
59 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000060 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010061 [snd_soc_dapm_value_mux] = 5,
62 [snd_soc_dapm_dac] = 6,
63 [snd_soc_dapm_mixer] = 7,
64 [snd_soc_dapm_mixer_named_ctl] = 7,
65 [snd_soc_dapm_pga] = 8,
66 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060067 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010068 [snd_soc_dapm_hp] = 10,
69 [snd_soc_dapm_spk] = 10,
70 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020071};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000072
Richard Purdie2b97eab2006-10-06 18:32:18 +020073static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010074 [snd_soc_dapm_pre] = 0,
75 [snd_soc_dapm_adc] = 1,
76 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010077 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060078 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010079 [snd_soc_dapm_pga] = 4,
80 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010081 [snd_soc_dapm_mixer] = 5,
82 [snd_soc_dapm_dac] = 6,
83 [snd_soc_dapm_mic] = 7,
84 [snd_soc_dapm_micbias] = 8,
85 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000086 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010087 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010088 [snd_soc_dapm_aif_in] = 10,
89 [snd_soc_dapm_aif_out] = 10,
90 [snd_soc_dapm_supply] = 11,
91 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020092};
93
Troy Kisky12ef1932008-10-13 17:42:14 -070094static void pop_wait(u32 pop_time)
Mark Brown15e4c72f2008-07-02 11:51:20 +010095{
96 if (pop_time)
97 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
98}
99
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200100static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c72f2008-07-02 11:51:20 +0100101{
102 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200103 char *buf;
104
105 if (!pop_time)
106 return;
107
108 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
109 if (buf == NULL)
110 return;
Mark Brown15e4c72f2008-07-02 11:51:20 +0100111
112 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200113 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100114 dev_info(dev, "%s", buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100115 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200116
117 kfree(buf);
Mark Brown15e4c72f2008-07-02 11:51:20 +0100118}
119
Richard Purdie2b97eab2006-10-06 18:32:18 +0200120/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100121static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200122 const struct snd_soc_dapm_widget *_widget)
123{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100124 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200125}
126
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100127static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
128{
129 if (w->codec)
130 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100131 else if (w->platform)
132 return snd_soc_platform_read(w->platform, reg);
133
134 dev_err(w->dapm->dev, "no valid widget read method\n");
135 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100136}
137
138static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
139{
140 if (w->codec)
141 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100142 else if (w->platform)
143 return snd_soc_platform_write(w->platform, reg, val);
144
145 dev_err(w->dapm->dev, "no valid widget write method\n");
146 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100147}
148
149static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
150 unsigned short reg, unsigned int mask, unsigned int value)
151{
152 int change;
153 unsigned int old, new;
154 int ret;
155
156 ret = soc_widget_read(w, reg);
157 if (ret < 0)
158 return ret;
159
160 old = ret;
161 new = (old & ~mask) | (value & mask);
162 change = old != new;
163 if (change) {
164 ret = soc_widget_write(w, reg, new);
165 if (ret < 0)
166 return ret;
167 }
168
169 return change;
170}
171
Mark Brown452c5ea2009-05-17 21:41:23 +0100172/**
173 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800174 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100175 * @level: level to configure
176 *
177 * Configure the bias (power) levels for the SoC audio device.
178 *
179 * Returns 0 for success else error.
180 */
Mark Browned5a4c42011-02-18 11:12:42 -0800181static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200182 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100183{
Mark Browned5a4c42011-02-18 11:12:42 -0800184 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100185 int ret = 0;
186
Mark Brown84e90932010-11-04 00:07:02 -0400187 trace_snd_soc_bias_level_start(card, level);
188
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000189 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100190 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100191 if (ret != 0)
192 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100193
Mark Browncc4c6702011-06-06 19:03:34 +0100194 if (dapm->codec) {
195 if (dapm->codec->driver->set_bias_level)
196 ret = dapm->codec->driver->set_bias_level(dapm->codec,
197 level);
198 else
199 dapm->bias_level = level;
200 }
Mark Brown171ec6b2011-06-06 18:15:19 +0100201 if (ret != 0)
202 goto out;
203
204 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100205 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100206out:
Mark Brown84e90932010-11-04 00:07:02 -0400207 trace_snd_soc_bias_level_done(card, level);
208
Mark Brown452c5ea2009-05-17 21:41:23 +0100209 return ret;
210}
211
Richard Purdie2b97eab2006-10-06 18:32:18 +0200212/* set up initial codec paths */
213static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
214 struct snd_soc_dapm_path *p, int i)
215{
216 switch (w->id) {
217 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000218 case snd_soc_dapm_mixer:
219 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200220 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100221 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600222 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400223 unsigned int reg = mc->reg;
224 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100225 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400226 unsigned int mask = (1 << fls(max)) - 1;
227 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200228
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100229 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200230 val = (val >> shift) & mask;
231
232 if ((invert && !val) || (!invert && val))
233 p->connect = 1;
234 else
235 p->connect = 0;
236 }
237 break;
238 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600239 struct soc_enum *e = (struct soc_enum *)
240 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200241 int val, item, bitmask;
242
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100243 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100244 ;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100245 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200246 item = (val >> e->shift_l) & (bitmask - 1);
247
248 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100249 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200250 if (!(strcmp(p->name, e->texts[i])) && item == i)
251 p->connect = 1;
252 }
253 }
254 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000255 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600256 struct soc_enum *e = (struct soc_enum *)
257 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000258
259 p->connect = 0;
260 /* since a virtual mux has no backing registers to
261 * decide which path to connect, it will try to match
262 * with the first enumeration. This is to ensure
263 * that the default mux choice (the first) will be
264 * correctly powered up during initialization.
265 */
266 if (!strcmp(p->name, e->texts[0]))
267 p->connect = 1;
268 }
269 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200270 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200271 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600272 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200273 int val, item;
274
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100275 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200276 val = (val >> e->shift_l) & e->mask;
277 for (item = 0; item < e->max; item++) {
278 if (val == e->values[item])
279 break;
280 }
281
282 p->connect = 0;
283 for (i = 0; i < e->max; i++) {
284 if (!(strcmp(p->name, e->texts[i])) && item == i)
285 p->connect = 1;
286 }
287 }
288 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200289 /* does not effect routing - always connected */
290 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600291 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200292 case snd_soc_dapm_output:
293 case snd_soc_dapm_adc:
294 case snd_soc_dapm_input:
295 case snd_soc_dapm_dac:
296 case snd_soc_dapm_micbias:
297 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100298 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100299 case snd_soc_dapm_aif_in:
300 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200301 p->connect = 1;
302 break;
303 /* does effect routing - dynamically connected */
304 case snd_soc_dapm_hp:
305 case snd_soc_dapm_mic:
306 case snd_soc_dapm_spk:
307 case snd_soc_dapm_line:
308 case snd_soc_dapm_pre:
309 case snd_soc_dapm_post:
310 p->connect = 0;
311 break;
312 }
313}
314
Mark Brown74b8f952009-06-06 11:26:15 +0100315/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200316static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200317 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
318 struct snd_soc_dapm_path *path, const char *control_name,
319 const struct snd_kcontrol_new *kcontrol)
320{
321 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
322 int i;
323
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100324 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200325 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200326 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200327 list_add(&path->list_sink, &dest->sources);
328 list_add(&path->list_source, &src->sinks);
329 path->name = (char*)e->texts[i];
330 dapm_set_path_status(dest, path, 0);
331 return 0;
332 }
333 }
334
335 return -ENODEV;
336}
337
Mark Brown74b8f952009-06-06 11:26:15 +0100338/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200339static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200340 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
341 struct snd_soc_dapm_path *path, const char *control_name)
342{
343 int i;
344
345 /* search for mixer kcontrol */
346 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600347 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200348 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200349 list_add(&path->list_sink, &dest->sources);
350 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600351 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200352 dapm_set_path_status(dest, path, i);
353 return 0;
354 }
355 }
356 return -ENODEV;
357}
358
Stephen Warrenaf468002011-04-28 17:38:01 -0600359static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600360 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600361 const struct snd_kcontrol_new *kcontrol_new,
362 struct snd_kcontrol **kcontrol)
363{
364 struct snd_soc_dapm_widget *w;
365 int i;
366
367 *kcontrol = NULL;
368
369 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600370 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
371 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600372 for (i = 0; i < w->num_kcontrols; i++) {
373 if (&w->kcontrol_news[i] == kcontrol_new) {
374 if (w->kcontrols)
375 *kcontrol = w->kcontrols[i];
376 return 1;
377 }
378 }
379 }
380
381 return 0;
382}
383
Richard Purdie2b97eab2006-10-06 18:32:18 +0200384/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200385static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200386{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200387 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200388 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000389 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200390 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000391 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000392 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600393 struct snd_soc_dapm_widget_list *wlist;
394 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000395
396 if (dapm->codec)
397 prefix = dapm->codec->name_prefix;
398 else
399 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200400
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000401 if (prefix)
402 prefix_len = strlen(prefix) + 1;
403 else
404 prefix_len = 0;
405
Richard Purdie2b97eab2006-10-06 18:32:18 +0200406 /* add kcontrol */
407 for (i = 0; i < w->num_kcontrols; i++) {
408
409 /* match name */
410 list_for_each_entry(path, &w->sources, list_sink) {
411
412 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600413 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200414 continue;
415
Stephen Warrenfafd2172011-04-28 17:38:00 -0600416 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
417 sizeof(struct snd_soc_dapm_widget *),
418 wlist = kzalloc(wlistsize, GFP_KERNEL);
419 if (wlist == NULL) {
420 dev_err(dapm->dev,
421 "asoc: can't allocate widget list for %s\n",
422 w->name);
423 return -ENOMEM;
424 }
425 wlist->num_widgets = 1;
426 wlist->widgets[0] = w;
427
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000428 /* add dapm control with long name.
429 * for dapm_mixer this is the concatenation of the
430 * mixer and kcontrol name.
431 * for dapm_mixer_named_ctl this is simply the
432 * kcontrol name.
433 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600434 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000435 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000436 name_len += 1 + strlen(w->name);
437
Mark Brown219b93f2008-10-28 13:02:31 +0000438 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000439
Stephen Warrenfafd2172011-04-28 17:38:00 -0600440 if (path->long_name == NULL) {
441 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200442 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600443 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200444
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000445 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000446 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000447 /* The control will get a prefix from
448 * the control creation process but
449 * we're also using the same prefix
450 * for widgets so cut the prefix off
451 * the front of the widget name.
452 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000453 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000454 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600455 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000456 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000457 case snd_soc_dapm_mixer_named_ctl:
458 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600459 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000460 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000461 }
462
Mark Brown219b93f2008-10-28 13:02:31 +0000463 path->long_name[name_len - 1] = '\0';
464
Stephen Warrenfafd2172011-04-28 17:38:00 -0600465 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
466 wlist, path->long_name,
467 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200468 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200469 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200470 dev_err(dapm->dev,
471 "asoc: failed to add dapm kcontrol %s: %d\n",
472 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600473 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200474 kfree(path->long_name);
475 path->long_name = NULL;
476 return ret;
477 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600478 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200479 }
480 }
481 return ret;
482}
483
484/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200485static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200486{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200487 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200488 struct snd_soc_dapm_path *path = NULL;
489 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000490 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000491 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000492 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600493 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600494 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600495 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600496 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600497 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200498
Stephen Warrenaf468002011-04-28 17:38:01 -0600499 if (w->num_kcontrols != 1) {
500 dev_err(dapm->dev,
501 "asoc: mux %s has incorrect number of controls\n",
502 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200503 return -EINVAL;
504 }
505
Stephen Warren1007da02011-05-26 09:57:33 -0600506 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600507 &kcontrol);
508 if (kcontrol) {
509 wlist = kcontrol->private_data;
510 wlistentries = wlist->num_widgets + 1;
511 } else {
512 wlist = NULL;
513 wlistentries = 1;
514 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600515 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600516 wlistentries * sizeof(struct snd_soc_dapm_widget *),
517 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600518 if (wlist == NULL) {
519 dev_err(dapm->dev,
520 "asoc: can't allocate widget list for %s\n", w->name);
521 return -ENOMEM;
522 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600523 wlist->num_widgets = wlistentries;
524 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600525
Stephen Warrenaf468002011-04-28 17:38:01 -0600526 if (!kcontrol) {
527 if (dapm->codec)
528 prefix = dapm->codec->name_prefix;
529 else
530 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000531
Stephen Warrenaf468002011-04-28 17:38:01 -0600532 if (shared) {
533 name = w->kcontrol_news[0].name;
534 prefix_len = 0;
535 } else {
536 name = w->name;
537 if (prefix)
538 prefix_len = strlen(prefix) + 1;
539 else
540 prefix_len = 0;
541 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000542
Stephen Warrenaf468002011-04-28 17:38:01 -0600543 /*
544 * The control will get a prefix from the control creation
545 * process but we're also using the same prefix for widgets so
546 * cut the prefix off the front of the widget name.
547 */
548 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
549 name + prefix_len, prefix);
550 ret = snd_ctl_add(card, kcontrol);
551 if (ret < 0) {
552 dev_err(dapm->dev,
553 "asoc: failed to add kcontrol %s\n", w->name);
554 kfree(wlist);
555 return ret;
556 }
557 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200558
Stephen Warrenaf468002011-04-28 17:38:01 -0600559 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200560
Stephen Warrenfad59882011-04-28 17:37:59 -0600561 w->kcontrols[0] = kcontrol;
562
Richard Purdie2b97eab2006-10-06 18:32:18 +0200563 list_for_each_entry(path, &w->sources, list_sink)
564 path->kcontrol = kcontrol;
565
Stephen Warrenaf468002011-04-28 17:38:01 -0600566 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200567}
568
569/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200570static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200571{
Mark Browna6c65732010-03-03 17:45:21 +0000572 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200573 dev_err(w->dapm->dev,
574 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200575
Mark Browna6c65732010-03-03 17:45:21 +0000576 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200577}
578
579/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200580static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200581{
582 struct snd_soc_dapm_path *p;
583
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200584 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200585 p->walked = 0;
586}
587
Mark Brown9949788b2010-05-07 20:24:05 +0100588/* We implement power down on suspend by checking the power state of
589 * the ALSA card - when we are suspending the ALSA state for the card
590 * is set to D3.
591 */
592static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
593{
Mark Brown12ea2c72011-03-02 18:17:32 +0000594 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100595
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100597 case SNDRV_CTL_POWER_D3hot:
598 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100599 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200600 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
601 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100602 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100603 default:
604 return 1;
605 }
606}
607
Richard Purdie2b97eab2006-10-06 18:32:18 +0200608/*
609 * Recursively check for a completed path to an active or physically connected
610 * output widget. Returns number of complete paths.
611 */
612static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
613{
614 struct snd_soc_dapm_path *path;
615 int con = 0;
616
Mark Brown246d0a12009-04-22 18:24:55 +0100617 if (widget->id == snd_soc_dapm_supply)
618 return 0;
619
Mark Brown010ff262009-08-17 17:39:22 +0100620 switch (widget->id) {
621 case snd_soc_dapm_adc:
622 case snd_soc_dapm_aif_out:
623 if (widget->active)
Mark Brown9949788b2010-05-07 20:24:05 +0100624 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100625 default:
626 break;
627 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200628
629 if (widget->connected) {
630 /* connected pin ? */
631 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown9949788b2010-05-07 20:24:05 +0100632 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200633
634 /* connected jack or spk ? */
635 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300636 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown9949788b2010-05-07 20:24:05 +0100637 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200638 }
639
640 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Brownbf3a9e12011-06-13 16:42:29 +0100641 if (path->weak)
642 continue;
643
Richard Purdie2b97eab2006-10-06 18:32:18 +0200644 if (path->walked)
645 continue;
646
647 if (path->sink && path->connect) {
648 path->walked = 1;
649 con += is_connected_output_ep(path->sink);
650 }
651 }
652
653 return con;
654}
655
656/*
657 * Recursively check for a completed path to an active or physically connected
658 * input widget. Returns number of complete paths.
659 */
660static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
661{
662 struct snd_soc_dapm_path *path;
663 int con = 0;
664
Mark Brown246d0a12009-04-22 18:24:55 +0100665 if (widget->id == snd_soc_dapm_supply)
666 return 0;
667
Richard Purdie2b97eab2006-10-06 18:32:18 +0200668 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100669 switch (widget->id) {
670 case snd_soc_dapm_dac:
671 case snd_soc_dapm_aif_in:
672 if (widget->active)
Mark Brown9949788b2010-05-07 20:24:05 +0100673 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100674 default:
675 break;
676 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200677
678 if (widget->connected) {
679 /* connected pin ? */
680 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown9949788b2010-05-07 20:24:05 +0100681 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200682
683 /* connected VMID/Bias for lower pops */
684 if (widget->id == snd_soc_dapm_vmid)
Mark Brown9949788b2010-05-07 20:24:05 +0100685 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200686
687 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300688 if (widget->id == snd_soc_dapm_mic ||
689 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown9949788b2010-05-07 20:24:05 +0100690 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200691 }
692
693 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Brownbf3a9e12011-06-13 16:42:29 +0100694 if (path->weak)
695 continue;
696
Richard Purdie2b97eab2006-10-06 18:32:18 +0200697 if (path->walked)
698 continue;
699
700 if (path->source && path->connect) {
701 path->walked = 1;
702 con += is_connected_input_ep(path->source);
703 }
704 }
705
706 return con;
707}
708
709/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300710 * Handler for generic register modifier widget.
711 */
712int dapm_reg_event(struct snd_soc_dapm_widget *w,
713 struct snd_kcontrol *kcontrol, int event)
714{
715 unsigned int val;
716
717 if (SND_SOC_DAPM_EVENT_ON(event))
718 val = w->on_val;
719 else
720 val = w->off_val;
721
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100722 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300723 w->mask << w->shift, val << w->shift);
724
725 return 0;
726}
Mark Brown11589412008-07-29 11:42:23 +0100727EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300728
Mark Browncd0f2d42009-04-20 16:56:59 +0100729/* Generic check to see if a widget should be powered.
730 */
731static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
732{
733 int in, out;
734
735 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200736 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100737 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200738 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100739 return out != 0 && in != 0;
740}
741
Mark Brown6ea31b92009-04-20 17:15:41 +0100742/* Check to see if an ADC has power */
743static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
744{
745 int in;
746
747 if (w->active) {
748 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200749 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100750 return in != 0;
751 } else {
752 return dapm_generic_check_power(w);
753 }
754}
755
756/* Check to see if a DAC has power */
757static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
758{
759 int out;
760
761 if (w->active) {
762 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200763 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100764 return out != 0;
765 } else {
766 return dapm_generic_check_power(w);
767 }
768}
769
Mark Brown246d0a12009-04-22 18:24:55 +0100770/* Check to see if a power supply is needed */
771static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
772{
773 struct snd_soc_dapm_path *path;
774 int power = 0;
775
776 /* Check if one of our outputs is connected */
777 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brownbf3a9e12011-06-13 16:42:29 +0100778 if (path->weak)
779 continue;
780
Mark Brown215edda2009-09-08 18:59:05 +0100781 if (path->connected &&
782 !path->connected(path->source, path->sink))
783 continue;
784
Mark Brown30173582011-02-11 11:42:19 +0000785 if (!path->sink)
786 continue;
787
788 if (path->sink->force) {
789 power = 1;
790 break;
791 }
792
793 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100794 path->sink->power_check(path->sink)) {
795 power = 1;
796 break;
797 }
798 }
799
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200800 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100801
802 return power;
803}
804
Mark Brown38357ab2009-06-06 19:03:23 +0100805static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
806 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000807 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000808{
Mark Brown828a8422011-01-15 13:14:30 +0000809 int *sort;
810
811 if (power_up)
812 sort = dapm_up_seq;
813 else
814 sort = dapm_down_seq;
815
Mark Brown38357ab2009-06-06 19:03:23 +0100816 if (sort[a->id] != sort[b->id])
817 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000818 if (a->subseq != b->subseq) {
819 if (power_up)
820 return a->subseq - b->subseq;
821 else
822 return b->subseq - a->subseq;
823 }
Mark Brownb22ead22009-06-07 12:51:26 +0100824 if (a->reg != b->reg)
825 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000826 if (a->dapm != b->dapm)
827 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000828
Mark Brown38357ab2009-06-06 19:03:23 +0100829 return 0;
830}
Mark Brown42aa3412009-03-01 19:21:10 +0000831
Mark Brown38357ab2009-06-06 19:03:23 +0100832/* Insert a widget in order into a DAPM power sequence. */
833static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
834 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000835 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100836{
837 struct snd_soc_dapm_widget *w;
838
839 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000840 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100841 list_add_tail(&new_widget->power_list, &w->power_list);
842 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000843 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100844
Mark Brown38357ab2009-06-06 19:03:23 +0100845 list_add_tail(&new_widget->power_list, list);
846}
Mark Brown42aa3412009-03-01 19:21:10 +0000847
Mark Brown68f89ad2010-11-03 23:51:49 -0400848static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
849 struct snd_soc_dapm_widget *w, int event)
850{
851 struct snd_soc_card *card = dapm->card;
852 const char *ev_name;
853 int power, ret;
854
855 switch (event) {
856 case SND_SOC_DAPM_PRE_PMU:
857 ev_name = "PRE_PMU";
858 power = 1;
859 break;
860 case SND_SOC_DAPM_POST_PMU:
861 ev_name = "POST_PMU";
862 power = 1;
863 break;
864 case SND_SOC_DAPM_PRE_PMD:
865 ev_name = "PRE_PMD";
866 power = 0;
867 break;
868 case SND_SOC_DAPM_POST_PMD:
869 ev_name = "POST_PMD";
870 power = 0;
871 break;
872 default:
873 BUG();
874 return;
875 }
876
877 if (w->power != power)
878 return;
879
880 if (w->event && (w->event_flags & event)) {
881 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
882 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400883 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400884 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400885 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400886 if (ret < 0)
887 pr_err("%s: %s event failed: %d\n",
888 ev_name, w->name, ret);
889 }
890}
891
Mark Brownb22ead22009-06-07 12:51:26 +0100892/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200893static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100894 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100895{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200896 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400897 struct snd_soc_dapm_widget *w;
898 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100899 unsigned int value = 0;
900 unsigned int mask = 0;
901 unsigned int cur_mask;
902
903 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
904 power_list)->reg;
905
906 list_for_each_entry(w, pending, power_list) {
907 cur_mask = 1 << w->shift;
908 BUG_ON(reg != w->reg);
909
910 if (w->invert)
911 power = !w->power;
912 else
913 power = w->power;
914
915 mask |= cur_mask;
916 if (power)
917 value |= cur_mask;
918
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200919 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100920 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
921 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100922
Mark Brown68f89ad2010-11-03 23:51:49 -0400923 /* Check for events */
924 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
925 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100926 }
927
Mark Brown81628102009-06-07 13:21:24 +0100928 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +0100929 /* Any widget will do, they should all be updating the
930 * same register.
931 */
932 w = list_first_entry(pending, struct snd_soc_dapm_widget,
933 power_list);
934
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200935 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100936 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200937 value, mask, reg, card->pop_time);
938 pop_wait(card->pop_time);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100939 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100940 }
941
942 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400943 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
944 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000945 }
Mark Brown42aa3412009-03-01 19:21:10 +0000946}
947
Mark Brownb22ead22009-06-07 12:51:26 +0100948/* Apply a DAPM power sequence.
949 *
950 * We walk over a pre-sorted list of widgets to apply power to. In
951 * order to minimise the number of writes to the device required
952 * multiple widgets will be updated in a single write where possible.
953 * Currently anything that requires more than a single write is not
954 * handled.
955 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200956static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +0000957 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +0100958{
959 struct snd_soc_dapm_widget *w, *n;
960 LIST_HEAD(pending);
961 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000962 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100963 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200964 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +0000965 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +0000966 int *sort;
967
968 if (power_up)
969 sort = dapm_up_seq;
970 else
971 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +0100972
Mark Brownb22ead22009-06-07 12:51:26 +0100973 list_for_each_entry_safe(w, n, list, power_list) {
974 ret = 0;
975
976 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200977 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +0000978 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +0100979 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200980 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100981
Mark Brown474b62d2011-01-18 16:14:44 +0000982 if (cur_dapm && cur_dapm->seq_notifier) {
983 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
984 if (sort[i] == cur_sort)
985 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000986 i,
987 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000988 }
989
Mark Brownb22ead22009-06-07 12:51:26 +0100990 INIT_LIST_HEAD(&pending);
991 cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000992 cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100993 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200994 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +0100995 }
996
Mark Brown163cac02009-06-07 10:12:52 +0100997 switch (w->id) {
998 case snd_soc_dapm_pre:
999 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001000 list_for_each_entry_safe_continue(w, n, list,
1001 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001002
Mark Brownb22ead22009-06-07 12:51:26 +01001003 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001004 ret = w->event(w,
1005 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001006 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001007 ret = w->event(w,
1008 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001009 break;
1010
1011 case snd_soc_dapm_post:
1012 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001013 list_for_each_entry_safe_continue(w, n, list,
1014 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001015
Mark Brownb22ead22009-06-07 12:51:26 +01001016 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001017 ret = w->event(w,
1018 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001019 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001020 ret = w->event(w,
1021 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001022 break;
1023
Mark Brown163cac02009-06-07 10:12:52 +01001024 default:
Mark Brown81628102009-06-07 13:21:24 +01001025 /* Queue it up for application */
1026 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001027 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001028 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001029 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001030 list_move(&w->power_list, &pending);
1031 break;
Mark Brown163cac02009-06-07 10:12:52 +01001032 }
Mark Brownb22ead22009-06-07 12:51:26 +01001033
1034 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001035 dev_err(w->dapm->dev,
1036 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001037 }
Mark Brownb22ead22009-06-07 12:51:26 +01001038
1039 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001040 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001041
1042 if (cur_dapm && cur_dapm->seq_notifier) {
1043 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1044 if (sort[i] == cur_sort)
1045 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001046 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001047 }
Mark Brown163cac02009-06-07 10:12:52 +01001048}
1049
Mark Brown97404f22010-12-14 16:13:57 +00001050static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1051{
1052 struct snd_soc_dapm_update *update = dapm->update;
1053 struct snd_soc_dapm_widget *w;
1054 int ret;
1055
1056 if (!update)
1057 return;
1058
1059 w = update->widget;
1060
1061 if (w->event &&
1062 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1063 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1064 if (ret != 0)
1065 pr_err("%s DAPM pre-event failed: %d\n",
1066 w->name, ret);
1067 }
1068
1069 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1070 update->val);
1071 if (ret < 0)
1072 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1073
1074 if (w->event &&
1075 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1076 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1077 if (ret != 0)
1078 pr_err("%s DAPM post-event failed: %d\n",
1079 w->name, ret);
1080 }
1081}
1082
Mark Brown9d0624a2011-02-18 11:49:43 -08001083/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1084 * they're changing state.
1085 */
1086static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1087{
1088 struct snd_soc_dapm_context *d = data;
1089 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001090
Mark Brown56fba412011-06-04 11:25:10 +01001091 /* If we're off and we're not supposed to be go into STANDBY */
1092 if (d->bias_level == SND_SOC_BIAS_OFF &&
1093 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001094 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1095 if (ret != 0)
1096 dev_err(d->dev,
1097 "Failed to turn on bias: %d\n", ret);
1098 }
1099
Mark Brown56fba412011-06-04 11:25:10 +01001100 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1101 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001102 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1103 if (ret != 0)
1104 dev_err(d->dev,
1105 "Failed to prepare bias: %d\n", ret);
1106 }
1107}
1108
1109/* Async callback run prior to DAPM sequences - brings to their final
1110 * state.
1111 */
1112static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1113{
1114 struct snd_soc_dapm_context *d = data;
1115 int ret;
1116
1117 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001118 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1119 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1120 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001121 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1122 if (ret != 0)
1123 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1124 ret);
1125 }
1126
1127 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001128 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1129 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001130 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1131 if (ret != 0)
1132 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1133 }
1134
1135 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001136 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1137 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001138 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1139 if (ret != 0)
1140 dev_err(d->dev, "Failed to apply active bias: %d\n",
1141 ret);
1142 }
1143}
Mark Brown97404f22010-12-14 16:13:57 +00001144
Mark Brown42aa3412009-03-01 19:21:10 +00001145/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001146 * Scan each dapm widget for complete audio path.
1147 * A complete path is a route that has valid endpoints i.e.:-
1148 *
1149 * o DAC to output pin.
1150 * o Input Pin to ADC.
1151 * o Input pin to Output pin (bypass, sidetone)
1152 * o DAC to ADC (loopback).
1153 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001154static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001155{
Mark Brown12ea2c72011-03-02 18:17:32 +00001156 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001157 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001158 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001159 LIST_HEAD(up_list);
1160 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001161 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001162 enum snd_soc_bias_level bias;
Mark Brown38357ab2009-06-06 19:03:23 +01001163 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001164
Mark Brown84e90932010-11-04 00:07:02 -04001165 trace_snd_soc_dapm_start(card);
1166
Mark Brown56fba412011-06-04 11:25:10 +01001167 list_for_each_entry(d, &card->dapm_list, list) {
1168 if (d->n_widgets || d->codec == NULL) {
1169 if (d->idle_bias_off)
1170 d->target_bias_level = SND_SOC_BIAS_OFF;
1171 else
1172 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1173 }
1174 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001175
Mark Brown6d3ddc82009-05-16 17:47:29 +01001176 /* Check which widgets we need to power and store them in
1177 * lists indicating if they should be powered up or down.
1178 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001179 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001180 switch (w->id) {
1181 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001182 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001183 break;
1184 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001185 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001186 break;
1187
1188 default:
1189 if (!w->power_check)
1190 continue;
1191
Mark Brown9949788b2010-05-07 20:24:05 +01001192 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001193 power = w->power_check(w);
Mark Brown9949788b2010-05-07 20:24:05 +01001194 else
1195 power = 1;
Mark Browndfcc9042011-06-04 11:34:43 +01001196
1197 if (power) {
1198 d = w->dapm;
1199
1200 /* Supplies and micbiases only bring
1201 * the context up to STANDBY as unless
1202 * something else is active and
1203 * passing audio they generally don't
1204 * require full power.
1205 */
1206 switch (w->id) {
1207 case snd_soc_dapm_supply:
1208 case snd_soc_dapm_micbias:
1209 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1210 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1211 break;
1212 default:
1213 d->target_bias_level = SND_SOC_BIAS_ON;
1214 break;
1215 }
1216 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001217
Mark Brown6d3ddc82009-05-16 17:47:29 +01001218 if (w->power == power)
1219 continue;
1220
Mark Brown84e90932010-11-04 00:07:02 -04001221 trace_snd_soc_dapm_widget_power(w, power);
1222
Mark Brown6d3ddc82009-05-16 17:47:29 +01001223 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001224 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001225 else
Mark Brown828a8422011-01-15 13:14:30 +00001226 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001227
1228 w->power = power;
1229 break;
1230 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001231 }
1232
Mark Brownb14b76a52009-08-17 11:55:38 +01001233 /* If there are no DAPM widgets then try to figure out power from the
1234 * event type.
1235 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001236 if (!dapm->n_widgets) {
Mark Brownb14b76a52009-08-17 11:55:38 +01001237 switch (event) {
1238 case SND_SOC_DAPM_STREAM_START:
1239 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001240 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a52009-08-17 11:55:38 +01001241 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001242 case SND_SOC_DAPM_STREAM_STOP:
Mark Brown56fba412011-06-04 11:25:10 +01001243 if (dapm->codec->active)
1244 dapm->target_bias_level = SND_SOC_BIAS_ON;
1245 else
1246 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001247 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001248 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001249 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001250 break;
Mark Brownb14b76a52009-08-17 11:55:38 +01001251 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001252 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001253 break;
Mark Brownb14b76a52009-08-17 11:55:38 +01001254 default:
1255 break;
1256 }
1257 }
1258
Mark Brown52ba67b2011-04-04 21:05:11 +09001259 /* Force all contexts in the card to the same bias state */
Mark Brown56fba412011-06-04 11:25:10 +01001260 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001261 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001262 if (d->target_bias_level > bias)
1263 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001264 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001265 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001266
1267
Mark Brown9d0624a2011-02-18 11:49:43 -08001268 /* Run all the bias changes in parallel */
1269 list_for_each_entry(d, &dapm->card->dapm_list, list)
1270 async_schedule_domain(dapm_pre_sequence_async, d,
1271 &async_domain);
1272 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001273
Mark Brown6d3ddc82009-05-16 17:47:29 +01001274 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001275 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001276
Mark Brown97404f22010-12-14 16:13:57 +00001277 dapm_widget_update(dapm);
1278
Mark Brown6d3ddc82009-05-16 17:47:29 +01001279 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001280 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001281
Mark Brown9d0624a2011-02-18 11:49:43 -08001282 /* Run all the bias changes in parallel */
1283 list_for_each_entry(d, &dapm->card->dapm_list, list)
1284 async_schedule_domain(dapm_post_sequence_async, d,
1285 &async_domain);
1286 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001287
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001288 pop_dbg(dapm->dev, card->pop_time,
1289 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001290 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001291
Mark Brown84e90932010-11-04 00:07:02 -04001292 trace_snd_soc_dapm_done(card);
1293
Mark Brown42aa3412009-03-01 19:21:10 +00001294 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001295}
1296
Mark Brown79fb9382009-08-21 16:38:13 +01001297#ifdef CONFIG_DEBUG_FS
1298static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1299{
1300 file->private_data = inode->i_private;
1301 return 0;
1302}
1303
1304static ssize_t dapm_widget_power_read_file(struct file *file,
1305 char __user *user_buf,
1306 size_t count, loff_t *ppos)
1307{
1308 struct snd_soc_dapm_widget *w = file->private_data;
1309 char *buf;
1310 int in, out;
1311 ssize_t ret;
1312 struct snd_soc_dapm_path *p = NULL;
1313
1314 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1315 if (!buf)
1316 return -ENOMEM;
1317
1318 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001319 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001320 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001321 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001322
Mark Brownd033c362009-12-04 15:25:56 +00001323 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001324 w->name, w->power ? "On" : "Off", in, out);
1325
Mark Brownd033c362009-12-04 15:25:56 +00001326 if (w->reg >= 0)
1327 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1328 " - R%d(0x%x) bit %d",
1329 w->reg, w->reg, w->shift);
1330
1331 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1332
Mark Brown3eef08b2009-09-14 16:49:00 +01001333 if (w->sname)
1334 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1335 w->sname,
1336 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001337
1338 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001339 if (p->connected && !p->connected(w, p->sink))
1340 continue;
1341
Mark Brown79fb9382009-08-21 16:38:13 +01001342 if (p->connect)
1343 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001344 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001345 p->name ? p->name : "static",
1346 p->source->name);
1347 }
1348 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001349 if (p->connected && !p->connected(w, p->sink))
1350 continue;
1351
Mark Brown79fb9382009-08-21 16:38:13 +01001352 if (p->connect)
1353 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001354 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001355 p->name ? p->name : "static",
1356 p->sink->name);
1357 }
1358
1359 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1360
1361 kfree(buf);
1362 return ret;
1363}
1364
1365static const struct file_operations dapm_widget_power_fops = {
1366 .open = dapm_widget_power_open_file,
1367 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001368 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001369};
1370
Mark Brownef49e4f2011-04-04 20:48:13 +09001371static int dapm_bias_open_file(struct inode *inode, struct file *file)
1372{
1373 file->private_data = inode->i_private;
1374 return 0;
1375}
1376
1377static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1378 size_t count, loff_t *ppos)
1379{
1380 struct snd_soc_dapm_context *dapm = file->private_data;
1381 char *level;
1382
1383 switch (dapm->bias_level) {
1384 case SND_SOC_BIAS_ON:
1385 level = "On\n";
1386 break;
1387 case SND_SOC_BIAS_PREPARE:
1388 level = "Prepare\n";
1389 break;
1390 case SND_SOC_BIAS_STANDBY:
1391 level = "Standby\n";
1392 break;
1393 case SND_SOC_BIAS_OFF:
1394 level = "Off\n";
1395 break;
1396 default:
1397 BUG();
1398 level = "Unknown\n";
1399 break;
1400 }
1401
1402 return simple_read_from_buffer(user_buf, count, ppos, level,
1403 strlen(level));
1404}
1405
1406static const struct file_operations dapm_bias_fops = {
1407 .open = dapm_bias_open_file,
1408 .read = dapm_bias_read_file,
1409 .llseek = default_llseek,
1410};
1411
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001412void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1413 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001414{
Mark Brown79fb9382009-08-21 16:38:13 +01001415 struct dentry *d;
1416
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001417 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1418
1419 if (!dapm->debugfs_dapm) {
1420 printk(KERN_WARNING
1421 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001422 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001423 }
Mark Brown79fb9382009-08-21 16:38:13 +01001424
Mark Brownef49e4f2011-04-04 20:48:13 +09001425 d = debugfs_create_file("bias_level", 0444,
1426 dapm->debugfs_dapm, dapm,
1427 &dapm_bias_fops);
1428 if (!d)
1429 dev_warn(dapm->dev,
1430 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001431}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001432
1433static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1434{
1435 struct snd_soc_dapm_context *dapm = w->dapm;
1436 struct dentry *d;
1437
1438 if (!dapm->debugfs_dapm || !w->name)
1439 return;
1440
1441 d = debugfs_create_file(w->name, 0444,
1442 dapm->debugfs_dapm, w,
1443 &dapm_widget_power_fops);
1444 if (!d)
1445 dev_warn(w->dapm->dev,
1446 "ASoC: Failed to create %s debugfs file\n",
1447 w->name);
1448}
1449
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001450static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1451{
1452 debugfs_remove_recursive(dapm->debugfs_dapm);
1453}
1454
Mark Brown79fb9382009-08-21 16:38:13 +01001455#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001456void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1457 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001458{
1459}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001460
1461static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1462{
1463}
1464
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001465static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1466{
1467}
1468
Mark Brown79fb9382009-08-21 16:38:13 +01001469#endif
1470
Richard Purdie2b97eab2006-10-06 18:32:18 +02001471/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001472static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001473 struct snd_kcontrol *kcontrol, int change,
1474 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001475{
1476 struct snd_soc_dapm_path *path;
1477 int found = 0;
1478
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001479 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001480 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001481 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001482 return -ENODEV;
1483
Mark Brown3a655772009-10-05 17:23:30 +01001484 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001485 return 0;
1486
1487 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001488 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001489 if (path->kcontrol != kcontrol)
1490 continue;
1491
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001492 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001493 continue;
1494
1495 found = 1;
1496 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001497 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001498 path->connect = 1; /* new connection */
1499 else
1500 path->connect = 0; /* old connection must be powered down */
1501 }
1502
Mark Brownb91b8fa2010-01-20 18:18:35 +00001503 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001504 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001505
1506 return 0;
1507}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001508
Milan plzik1b075e32008-01-10 14:39:46 +01001509/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001510static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001511 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001512{
1513 struct snd_soc_dapm_path *path;
1514 int found = 0;
1515
Milan plzik1b075e32008-01-10 14:39:46 +01001516 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001517 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001518 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001519 return -ENODEV;
1520
Richard Purdie2b97eab2006-10-06 18:32:18 +02001521 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001522 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001523 if (path->kcontrol != kcontrol)
1524 continue;
1525
1526 /* found, now check type */
1527 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001528 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001529 break;
1530 }
1531
Mark Brownb91b8fa2010-01-20 18:18:35 +00001532 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001533 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001534
1535 return 0;
1536}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001537
1538/* show dapm widget status in sys fs */
1539static ssize_t dapm_widget_show(struct device *dev,
1540 struct device_attribute *attr, char *buf)
1541{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001542 struct snd_soc_pcm_runtime *rtd =
1543 container_of(dev, struct snd_soc_pcm_runtime, dev);
1544 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001545 struct snd_soc_dapm_widget *w;
1546 int count = 0;
1547 char *state = "not set";
1548
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001549 list_for_each_entry(w, &codec->card->widgets, list) {
1550 if (w->dapm != &codec->dapm)
1551 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001552
1553 /* only display widgets that burnm power */
1554 switch (w->id) {
1555 case snd_soc_dapm_hp:
1556 case snd_soc_dapm_mic:
1557 case snd_soc_dapm_spk:
1558 case snd_soc_dapm_line:
1559 case snd_soc_dapm_micbias:
1560 case snd_soc_dapm_dac:
1561 case snd_soc_dapm_adc:
1562 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001563 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001564 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001565 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001566 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001567 if (w->name)
1568 count += sprintf(buf + count, "%s: %s\n",
1569 w->name, w->power ? "On":"Off");
1570 break;
1571 default:
1572 break;
1573 }
1574 }
1575
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001576 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001577 case SND_SOC_BIAS_ON:
1578 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001579 break;
Mark Brown0be98982008-05-19 12:31:28 +02001580 case SND_SOC_BIAS_PREPARE:
1581 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001582 break;
Mark Brown0be98982008-05-19 12:31:28 +02001583 case SND_SOC_BIAS_STANDBY:
1584 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001585 break;
Mark Brown0be98982008-05-19 12:31:28 +02001586 case SND_SOC_BIAS_OFF:
1587 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001588 break;
1589 }
1590 count += sprintf(buf + count, "PM State: %s\n", state);
1591
1592 return count;
1593}
1594
1595static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1596
1597int snd_soc_dapm_sys_add(struct device *dev)
1598{
Troy Kisky12ef1932008-10-13 17:42:14 -07001599 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001600}
1601
1602static void snd_soc_dapm_sys_remove(struct device *dev)
1603{
Mark Brownaef90842009-05-16 17:53:16 +01001604 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001605}
1606
1607/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001608static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001609{
1610 struct snd_soc_dapm_widget *w, *next_w;
1611 struct snd_soc_dapm_path *p, *next_p;
1612
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001613 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1614 if (w->dapm != dapm)
1615 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001616 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001617 /*
1618 * remove source and sink paths associated to this widget.
1619 * While removing the path, remove reference to it from both
1620 * source and sink widgets so that path is removed only once.
1621 */
1622 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1623 list_del(&p->list_sink);
1624 list_del(&p->list_source);
1625 list_del(&p->list);
1626 kfree(p->long_name);
1627 kfree(p);
1628 }
1629 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1630 list_del(&p->list_sink);
1631 list_del(&p->list_source);
1632 list_del(&p->list);
1633 kfree(p->long_name);
1634 kfree(p);
1635 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001636 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001637 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001638 kfree(w);
1639 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001640}
1641
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001642static struct snd_soc_dapm_widget *dapm_find_widget(
1643 struct snd_soc_dapm_context *dapm, const char *pin,
1644 bool search_other_contexts)
1645{
1646 struct snd_soc_dapm_widget *w;
1647 struct snd_soc_dapm_widget *fallback = NULL;
1648
1649 list_for_each_entry(w, &dapm->card->widgets, list) {
1650 if (!strcmp(w->name, pin)) {
1651 if (w->dapm == dapm)
1652 return w;
1653 else
1654 fallback = w;
1655 }
1656 }
1657
1658 if (search_other_contexts)
1659 return fallback;
1660
1661 return NULL;
1662}
1663
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001664static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001665 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001666{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001667 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001668
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001669 if (!w) {
1670 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1671 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001672 }
1673
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001674 w->connected = status;
1675 if (status == 0)
1676 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001677
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001678 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001679}
1680
Richard Purdie2b97eab2006-10-06 18:32:18 +02001681/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001682 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001683 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001684 *
1685 * Walks all dapm audio paths and powers widgets according to their
1686 * stream or path usage.
1687 *
1688 * Returns 0 for success.
1689 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001690int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001691{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001692 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001693}
Liam Girdwooda5302182008-07-07 13:35:17 +01001694EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001695
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001696static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001697 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001698{
1699 struct snd_soc_dapm_path *path;
1700 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001701 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001702 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001703 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001704 const char *source;
1705 char prefixed_sink[80];
1706 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001707 int ret = 0;
1708
Mark Brown88e8b9a2011-03-02 18:18:24 +00001709 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001710 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1711 dapm->codec->name_prefix, route->sink);
1712 sink = prefixed_sink;
1713 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1714 dapm->codec->name_prefix, route->source);
1715 source = prefixed_source;
1716 } else {
1717 sink = route->sink;
1718 source = route->source;
1719 }
1720
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001721 /*
1722 * find src and dest widgets over all widgets but favor a widget from
1723 * current DAPM context
1724 */
1725 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001726 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001727 wtsink = w;
1728 if (w->dapm == dapm)
1729 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001730 continue;
1731 }
1732 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001733 wtsource = w;
1734 if (w->dapm == dapm)
1735 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001736 }
1737 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001738 /* use widget from another DAPM context if not found from this */
1739 if (!wsink)
1740 wsink = wtsink;
1741 if (!wsource)
1742 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001743
1744 if (wsource == NULL || wsink == NULL)
1745 return -ENODEV;
1746
1747 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1748 if (!path)
1749 return -ENOMEM;
1750
1751 path->source = wsource;
1752 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001753 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001754 INIT_LIST_HEAD(&path->list);
1755 INIT_LIST_HEAD(&path->list_source);
1756 INIT_LIST_HEAD(&path->list_sink);
1757
1758 /* check for external widgets */
1759 if (wsink->id == snd_soc_dapm_input) {
1760 if (wsource->id == snd_soc_dapm_micbias ||
1761 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001762 wsource->id == snd_soc_dapm_line ||
1763 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001764 wsink->ext = 1;
1765 }
1766 if (wsource->id == snd_soc_dapm_output) {
1767 if (wsink->id == snd_soc_dapm_spk ||
1768 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001769 wsink->id == snd_soc_dapm_line ||
1770 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001771 wsource->ext = 1;
1772 }
1773
1774 /* connect static paths */
1775 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001776 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001777 list_add(&path->list_sink, &wsink->sources);
1778 list_add(&path->list_source, &wsource->sinks);
1779 path->connect = 1;
1780 return 0;
1781 }
1782
1783 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001784 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001785 case snd_soc_dapm_adc:
1786 case snd_soc_dapm_dac:
1787 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001788 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001789 case snd_soc_dapm_input:
1790 case snd_soc_dapm_output:
1791 case snd_soc_dapm_micbias:
1792 case snd_soc_dapm_vmid:
1793 case snd_soc_dapm_pre:
1794 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001795 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001796 case snd_soc_dapm_aif_in:
1797 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001798 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001799 list_add(&path->list_sink, &wsink->sources);
1800 list_add(&path->list_source, &wsource->sinks);
1801 path->connect = 1;
1802 return 0;
1803 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001804 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001805 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001806 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06001807 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001808 if (ret != 0)
1809 goto err;
1810 break;
1811 case snd_soc_dapm_switch:
1812 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001813 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001814 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001815 if (ret != 0)
1816 goto err;
1817 break;
1818 case snd_soc_dapm_hp:
1819 case snd_soc_dapm_mic:
1820 case snd_soc_dapm_line:
1821 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001822 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001823 list_add(&path->list_sink, &wsink->sources);
1824 list_add(&path->list_source, &wsource->sinks);
1825 path->connect = 0;
1826 return 0;
1827 }
1828 return 0;
1829
1830err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001831 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1832 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001833 kfree(path);
1834 return ret;
1835}
Mark Brown105f1c22008-05-13 14:52:19 +02001836
1837/**
Mark Brown105f1c22008-05-13 14:52:19 +02001838 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001839 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001840 * @route: audio routes
1841 * @num: number of routes
1842 *
1843 * Connects 2 dapm widgets together via a named audio path. The sink is
1844 * the widget receiving the audio signal, whilst the source is the sender
1845 * of the audio signal.
1846 *
1847 * Returns 0 for success else error. On error all resources can be freed
1848 * with a call to snd_soc_card_free().
1849 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001850int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001851 const struct snd_soc_dapm_route *route, int num)
1852{
1853 int i, ret;
1854
1855 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001856 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001857 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001858 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1859 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001860 return ret;
1861 }
1862 route++;
1863 }
1864
1865 return 0;
1866}
1867EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1868
Mark Brownbf3a9e12011-06-13 16:42:29 +01001869static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
1870 const struct snd_soc_dapm_route *route)
1871{
1872 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
1873 route->source,
1874 true);
1875 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
1876 route->sink,
1877 true);
1878 struct snd_soc_dapm_path *path;
1879 int count = 0;
1880
1881 if (!source) {
1882 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
1883 route->source);
1884 return -ENODEV;
1885 }
1886
1887 if (!sink) {
1888 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
1889 route->sink);
1890 return -ENODEV;
1891 }
1892
1893 if (route->control || route->connected)
1894 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
1895 route->source, route->sink);
1896
1897 list_for_each_entry(path, &source->sinks, list_source) {
1898 if (path->sink == sink) {
1899 path->weak = 1;
1900 count++;
1901 }
1902 }
1903
1904 if (count == 0)
1905 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
1906 route->source, route->sink);
1907 if (count > 1)
1908 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
1909 count, route->source, route->sink);
1910
1911 return 0;
1912}
1913
1914/**
1915 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
1916 * @dapm: DAPM context
1917 * @route: audio routes
1918 * @num: number of routes
1919 *
1920 * Mark existing routes matching those specified in the passed array
1921 * as being weak, meaning that they are ignored for the purpose of
1922 * power decisions. The main intended use case is for sidetone paths
1923 * which couple audio between other independent paths if they are both
1924 * active in order to make the combination work better at the user
1925 * level but which aren't intended to be "used".
1926 *
1927 * Note that CODEC drivers should not use this as sidetone type paths
1928 * can frequently also be used as bypass paths.
1929 */
1930int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
1931 const struct snd_soc_dapm_route *route, int num)
1932{
1933 int i, err;
1934 int ret = 0;
1935
1936 for (i = 0; i < num; i++) {
1937 err = snd_soc_dapm_weak_route(dapm, route);
1938 if (err)
1939 ret = err;
1940 route++;
1941 }
1942
1943 return ret;
1944}
1945EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
1946
Mark Brown105f1c22008-05-13 14:52:19 +02001947/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001948 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001949 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001950 *
1951 * Checks the codec for any new dapm widgets and creates them if found.
1952 *
1953 * Returns 0 for success.
1954 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001955int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001956{
1957 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00001958 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001959
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001960 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001961 {
1962 if (w->new)
1963 continue;
1964
Stephen Warrenfad59882011-04-28 17:37:59 -06001965 if (w->num_kcontrols) {
1966 w->kcontrols = kzalloc(w->num_kcontrols *
1967 sizeof(struct snd_kcontrol *),
1968 GFP_KERNEL);
1969 if (!w->kcontrols)
1970 return -ENOMEM;
1971 }
1972
Richard Purdie2b97eab2006-10-06 18:32:18 +02001973 switch(w->id) {
1974 case snd_soc_dapm_switch:
1975 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001976 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001977 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001978 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001979 break;
1980 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001981 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001982 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001983 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001984 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001985 break;
1986 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001987 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001988 w->power_check = dapm_adc_check_power;
1989 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001990 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001991 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001992 w->power_check = dapm_dac_check_power;
1993 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001994 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001995 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01001996 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001997 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001998 break;
1999 case snd_soc_dapm_input:
2000 case snd_soc_dapm_output:
2001 case snd_soc_dapm_micbias:
2002 case snd_soc_dapm_spk:
2003 case snd_soc_dapm_hp:
2004 case snd_soc_dapm_mic:
2005 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01002006 w->power_check = dapm_generic_check_power;
2007 break;
Mark Brown246d0a12009-04-22 18:24:55 +01002008 case snd_soc_dapm_supply:
2009 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002010 case snd_soc_dapm_vmid:
2011 case snd_soc_dapm_pre:
2012 case snd_soc_dapm_post:
2013 break;
2014 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002015
2016 /* Read the initial power state from the device */
2017 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002018 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002019 val &= 1 << w->shift;
2020 if (w->invert)
2021 val = !val;
2022
2023 if (val)
2024 w->power = 1;
2025 }
2026
Richard Purdie2b97eab2006-10-06 18:32:18 +02002027 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002028
2029 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002030 }
2031
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002032 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002033 return 0;
2034}
2035EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2036
2037/**
2038 * snd_soc_dapm_get_volsw - dapm mixer get callback
2039 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002040 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002041 *
2042 * Callback to get the value of a dapm mixer control.
2043 *
2044 * Returns 0 for success.
2045 */
2046int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2047 struct snd_ctl_elem_value *ucontrol)
2048{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002049 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2050 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002051 struct soc_mixer_control *mc =
2052 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002053 unsigned int reg = mc->reg;
2054 unsigned int shift = mc->shift;
2055 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002056 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002057 unsigned int invert = mc->invert;
2058 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002059
Richard Purdie2b97eab2006-10-06 18:32:18 +02002060 ucontrol->value.integer.value[0] =
2061 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2062 if (shift != rshift)
2063 ucontrol->value.integer.value[1] =
2064 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2065 if (invert) {
2066 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002067 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002068 if (shift != rshift)
2069 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002070 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002071 }
2072
2073 return 0;
2074}
2075EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2076
2077/**
2078 * snd_soc_dapm_put_volsw - dapm mixer set callback
2079 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002080 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002081 *
2082 * Callback to set the value of a dapm mixer control.
2083 *
2084 * Returns 0 for success.
2085 */
2086int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2087 struct snd_ctl_elem_value *ucontrol)
2088{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002089 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2090 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2091 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002092 struct soc_mixer_control *mc =
2093 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002094 unsigned int reg = mc->reg;
2095 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002096 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002097 unsigned int mask = (1 << fls(max)) - 1;
2098 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002099 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002100 int connect, change;
2101 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002102 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002103
2104 val = (ucontrol->value.integer.value[0] & mask);
2105
2106 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002107 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002108 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002109 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002110
Stephen Warrenfafd2172011-04-28 17:38:00 -06002111 if (val)
2112 /* new connection */
2113 connect = invert ? 0 : 1;
2114 else
2115 /* old connection must be powered down */
2116 connect = invert ? 1 : 0;
2117
2118 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002119
Stephen Warrene9cf7042011-01-27 14:54:05 -07002120 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002121 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002122 for (wi = 0; wi < wlist->num_widgets; wi++) {
2123 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002124
Stephen Warrenfafd2172011-04-28 17:38:00 -06002125 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002126
Stephen Warrenfafd2172011-04-28 17:38:00 -06002127 update.kcontrol = kcontrol;
2128 update.widget = widget;
2129 update.reg = reg;
2130 update.mask = mask;
2131 update.val = val;
2132 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002133
Stephen Warrenfafd2172011-04-28 17:38:00 -06002134 dapm_mixer_update_power(widget, kcontrol, connect);
2135
2136 widget->dapm->update = NULL;
2137 }
Mark Brown283375c2009-12-07 18:09:03 +00002138 }
2139
Stephen Warrenfafd2172011-04-28 17:38:00 -06002140 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002141 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002142}
2143EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2144
2145/**
2146 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2147 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002148 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002149 *
2150 * Callback to get the value of a dapm enumerated double mixer control.
2151 *
2152 * Returns 0 for success.
2153 */
2154int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_value *ucontrol)
2156{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002157 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2158 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002159 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002160 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002161
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002162 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002163 ;
2164 val = snd_soc_read(widget->codec, e->reg);
2165 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2166 if (e->shift_l != e->shift_r)
2167 ucontrol->value.enumerated.item[1] =
2168 (val >> e->shift_r) & (bitmask - 1);
2169
2170 return 0;
2171}
2172EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2173
2174/**
2175 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2176 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002177 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002178 *
2179 * Callback to set the value of a dapm enumerated double mixer control.
2180 *
2181 * Returns 0 for success.
2182 */
2183int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2184 struct snd_ctl_elem_value *ucontrol)
2185{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002186 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2187 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2188 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002189 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002190 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002191 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002192 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002193 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002194
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002195 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002196 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002197 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002198 return -EINVAL;
2199 mux = ucontrol->value.enumerated.item[0];
2200 val = mux << e->shift_l;
2201 mask = (bitmask - 1) << e->shift_l;
2202 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002203 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002204 return -EINVAL;
2205 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2206 mask |= (bitmask - 1) << e->shift_r;
2207 }
2208
Stephen Warrenfafd2172011-04-28 17:38:00 -06002209 mutex_lock(&codec->mutex);
2210
Mark Brown3a655772009-10-05 17:23:30 +01002211 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002212 if (change) {
2213 for (wi = 0; wi < wlist->num_widgets; wi++) {
2214 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002215
Stephen Warrenfafd2172011-04-28 17:38:00 -06002216 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002217
Stephen Warrenfafd2172011-04-28 17:38:00 -06002218 update.kcontrol = kcontrol;
2219 update.widget = widget;
2220 update.reg = e->reg;
2221 update.mask = mask;
2222 update.val = val;
2223 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002224
Stephen Warrenfafd2172011-04-28 17:38:00 -06002225 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002226
Stephen Warrenfafd2172011-04-28 17:38:00 -06002227 widget->dapm->update = NULL;
2228 }
2229 }
2230
2231 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002232 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002233}
2234EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2235
2236/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002237 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2238 * @kcontrol: mixer control
2239 * @ucontrol: control element information
2240 *
2241 * Returns 0 for success.
2242 */
2243int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2244 struct snd_ctl_elem_value *ucontrol)
2245{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002246 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2247 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002248
2249 ucontrol->value.enumerated.item[0] = widget->value;
2250
2251 return 0;
2252}
2253EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2254
2255/**
2256 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2257 * @kcontrol: mixer control
2258 * @ucontrol: control element information
2259 *
2260 * Returns 0 for success.
2261 */
2262int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2263 struct snd_ctl_elem_value *ucontrol)
2264{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002265 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2266 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2267 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002268 struct soc_enum *e =
2269 (struct soc_enum *)kcontrol->private_value;
2270 int change;
2271 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002272 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002273
2274 if (ucontrol->value.enumerated.item[0] >= e->max)
2275 return -EINVAL;
2276
Stephen Warrenfafd2172011-04-28 17:38:00 -06002277 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002278
2279 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002280 if (change) {
2281 for (wi = 0; wi < wlist->num_widgets; wi++) {
2282 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002283
Stephen Warrenfafd2172011-04-28 17:38:00 -06002284 widget->value = ucontrol->value.enumerated.item[0];
2285
2286 dapm_mux_update_power(widget, kcontrol, change,
2287 widget->value, e);
2288 }
2289 }
2290
2291 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002292 return ret;
2293}
2294EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2295
2296/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002297 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2298 * callback
2299 * @kcontrol: mixer control
2300 * @ucontrol: control element information
2301 *
2302 * Callback to get the value of a dapm semi enumerated double mixer control.
2303 *
2304 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2305 * used for handling bitfield coded enumeration for example.
2306 *
2307 * Returns 0 for success.
2308 */
2309int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2310 struct snd_ctl_elem_value *ucontrol)
2311{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002312 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2313 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002314 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002315 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002316
2317 reg_val = snd_soc_read(widget->codec, e->reg);
2318 val = (reg_val >> e->shift_l) & e->mask;
2319 for (mux = 0; mux < e->max; mux++) {
2320 if (val == e->values[mux])
2321 break;
2322 }
2323 ucontrol->value.enumerated.item[0] = mux;
2324 if (e->shift_l != e->shift_r) {
2325 val = (reg_val >> e->shift_r) & e->mask;
2326 for (mux = 0; mux < e->max; mux++) {
2327 if (val == e->values[mux])
2328 break;
2329 }
2330 ucontrol->value.enumerated.item[1] = mux;
2331 }
2332
2333 return 0;
2334}
2335EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2336
2337/**
2338 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2339 * callback
2340 * @kcontrol: mixer control
2341 * @ucontrol: control element information
2342 *
2343 * Callback to set the value of a dapm semi enumerated double mixer control.
2344 *
2345 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2346 * used for handling bitfield coded enumeration for example.
2347 *
2348 * Returns 0 for success.
2349 */
2350int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2351 struct snd_ctl_elem_value *ucontrol)
2352{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002353 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2354 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2355 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002356 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002357 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002358 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002359 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002360 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002361
2362 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2363 return -EINVAL;
2364 mux = ucontrol->value.enumerated.item[0];
2365 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2366 mask = e->mask << e->shift_l;
2367 if (e->shift_l != e->shift_r) {
2368 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2369 return -EINVAL;
2370 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2371 mask |= e->mask << e->shift_r;
2372 }
2373
Stephen Warrenfafd2172011-04-28 17:38:00 -06002374 mutex_lock(&codec->mutex);
2375
Mark Brown3a655772009-10-05 17:23:30 +01002376 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002377 if (change) {
2378 for (wi = 0; wi < wlist->num_widgets; wi++) {
2379 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002380
Stephen Warrenfafd2172011-04-28 17:38:00 -06002381 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002382
Stephen Warrenfafd2172011-04-28 17:38:00 -06002383 update.kcontrol = kcontrol;
2384 update.widget = widget;
2385 update.reg = e->reg;
2386 update.mask = mask;
2387 update.val = val;
2388 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002389
Stephen Warrenfafd2172011-04-28 17:38:00 -06002390 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002391
Stephen Warrenfafd2172011-04-28 17:38:00 -06002392 widget->dapm->update = NULL;
2393 }
2394 }
2395
2396 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002397 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002398}
2399EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2400
2401/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002402 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2403 *
2404 * @kcontrol: mixer control
2405 * @uinfo: control element information
2406 *
2407 * Callback to provide information about a pin switch control.
2408 */
2409int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2410 struct snd_ctl_elem_info *uinfo)
2411{
2412 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2413 uinfo->count = 1;
2414 uinfo->value.integer.min = 0;
2415 uinfo->value.integer.max = 1;
2416
2417 return 0;
2418}
2419EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2420
2421/**
2422 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2423 *
2424 * @kcontrol: mixer control
2425 * @ucontrol: Value
2426 */
2427int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2428 struct snd_ctl_elem_value *ucontrol)
2429{
2430 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2431 const char *pin = (const char *)kcontrol->private_value;
2432
2433 mutex_lock(&codec->mutex);
2434
2435 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002436 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002437
2438 mutex_unlock(&codec->mutex);
2439
2440 return 0;
2441}
2442EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2443
2444/**
2445 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2446 *
2447 * @kcontrol: mixer control
2448 * @ucontrol: Value
2449 */
2450int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2451 struct snd_ctl_elem_value *ucontrol)
2452{
2453 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2454 const char *pin = (const char *)kcontrol->private_value;
2455
2456 mutex_lock(&codec->mutex);
2457
2458 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002459 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002460 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002461 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002462
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002463 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002464
2465 mutex_unlock(&codec->mutex);
2466
2467 return 0;
2468}
2469EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2470
2471/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002472 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002473 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002474 * @widget: widget template
2475 *
2476 * Creates a new dapm control based upon the template.
2477 *
2478 * Returns 0 for success else error.
2479 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002480int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002481 const struct snd_soc_dapm_widget *widget)
2482{
2483 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002484 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002485
2486 if ((w = dapm_cnew_widget(widget)) == NULL)
2487 return -ENOMEM;
2488
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002489 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002490 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002491 name_len += 1 + strlen(dapm->codec->name_prefix);
2492 w->name = kmalloc(name_len, GFP_KERNEL);
2493 if (w->name == NULL) {
2494 kfree(w);
2495 return -ENOMEM;
2496 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002497 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002498 snprintf(w->name, name_len, "%s %s",
2499 dapm->codec->name_prefix, widget->name);
2500 else
2501 snprintf(w->name, name_len, "%s", widget->name);
2502
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002503 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002504 w->dapm = dapm;
2505 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002506 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002507 INIT_LIST_HEAD(&w->sources);
2508 INIT_LIST_HEAD(&w->sinks);
2509 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002510 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002511
2512 /* machine layer set ups unconnected pins and insertions */
2513 w->connected = 1;
2514 return 0;
2515}
2516EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2517
2518/**
Mark Brown4ba13272008-05-13 14:51:19 +02002519 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002520 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002521 * @widget: widget array
2522 * @num: number of widgets
2523 *
2524 * Creates new DAPM controls based upon the templates.
2525 *
2526 * Returns 0 for success else error.
2527 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002528int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002529 const struct snd_soc_dapm_widget *widget,
2530 int num)
2531{
2532 int i, ret;
2533
2534 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002535 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002536 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002537 dev_err(dapm->dev,
2538 "ASoC: Failed to create DAPM control %s: %d\n",
2539 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002540 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002541 }
Mark Brown4ba13272008-05-13 14:51:19 +02002542 widget++;
2543 }
2544 return 0;
2545}
2546EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2547
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002548static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002549 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002550{
2551 struct snd_soc_dapm_widget *w;
2552
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002553 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002554 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002555 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002556 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002557 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2558 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002559 if (strstr(w->sname, stream)) {
2560 switch(event) {
2561 case SND_SOC_DAPM_STREAM_START:
2562 w->active = 1;
2563 break;
2564 case SND_SOC_DAPM_STREAM_STOP:
2565 w->active = 0;
2566 break;
2567 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002568 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002569 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002570 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2571 break;
2572 }
2573 }
2574 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002575
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002576 dapm_power_widgets(dapm, event);
2577}
2578
2579/**
2580 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2581 * @rtd: PCM runtime data
2582 * @stream: stream name
2583 * @event: stream event
2584 *
2585 * Sends a stream event to the dapm core. The core then makes any
2586 * necessary widget power changes.
2587 *
2588 * Returns 0 for success else error.
2589 */
2590int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2591 const char *stream, int event)
2592{
2593 struct snd_soc_codec *codec = rtd->codec;
2594
2595 if (stream == NULL)
2596 return 0;
2597
2598 mutex_lock(&codec->mutex);
2599 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002600 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002601 return 0;
2602}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002603
2604/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002605 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002606 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002607 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002608 *
Mark Brown74b8f952009-06-06 11:26:15 +01002609 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002610 * a valid audio route and active audio stream.
2611 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2612 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002613 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002614int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002615{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002616 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002617}
Liam Girdwooda5302182008-07-07 13:35:17 +01002618EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002619
2620/**
Mark Brownda341832010-03-15 19:23:37 +00002621 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002622 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002623 * @pin: pin name
2624 *
2625 * Enables input/output pin regardless of any other state. This is
2626 * intended for use with microphone bias supplies used in microphone
2627 * jack detection.
2628 *
2629 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2630 * do any widget power switching.
2631 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002632int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2633 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002634{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002635 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002636
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002637 if (!w) {
2638 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2639 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002640 }
2641
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002642 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2643 w->connected = 1;
2644 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002645
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002646 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002647}
2648EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2649
2650/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002651 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002652 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002653 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002654 *
Mark Brown74b8f952009-06-06 11:26:15 +01002655 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002656 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2657 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002658 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002659int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2660 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002661{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002662 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002663}
2664EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2665
2666/**
Mark Brown5817b522008-09-24 11:23:11 +01002667 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002668 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002669 * @pin: pin name
2670 *
2671 * Marks the specified pin as being not connected, disabling it along
2672 * any parent or child widgets. At present this is identical to
2673 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2674 * additional things such as disabling controls which only affect
2675 * paths through the pin.
2676 *
2677 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2678 * do any widget power switching.
2679 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002680int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002681{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002682 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002683}
2684EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2685
2686/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002687 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002688 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002689 * @pin: audio signal pin endpoint (or start point)
2690 *
2691 * Get audio pin status - connected or disconnected.
2692 *
2693 * Returns 1 for connected otherwise 0.
2694 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002695int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2696 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002697{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002698 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002699
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002700 if (w)
2701 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002702
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002703 return 0;
2704}
Liam Girdwooda5302182008-07-07 13:35:17 +01002705EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002706
2707/**
Mark Brown1547aba2010-05-07 21:11:40 +01002708 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002709 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002710 * @pin: audio signal pin endpoint (or start point)
2711 *
2712 * Mark the given endpoint or pin as ignoring suspend. When the
2713 * system is disabled a path between two endpoints flagged as ignoring
2714 * suspend will not be disabled. The path must already be enabled via
2715 * normal means at suspend time, it will not be turned on if it was not
2716 * already enabled.
2717 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002718int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2719 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002720{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002721 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002722
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002723 if (!w) {
2724 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2725 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002726 }
2727
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002728 w->ignore_suspend = 1;
2729
2730 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002731}
2732EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2733
2734/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002735 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002736 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002737 *
2738 * Free all dapm widgets and resources.
2739 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002740void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002741{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002742 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002743 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002744 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002745 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002746}
2747EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2748
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002749static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002750{
Mark Brown51737472009-06-22 13:16:51 +01002751 struct snd_soc_dapm_widget *w;
2752 LIST_HEAD(down_list);
2753 int powerdown = 0;
2754
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002755 list_for_each_entry(w, &dapm->card->widgets, list) {
2756 if (w->dapm != dapm)
2757 continue;
Mark Brown51737472009-06-22 13:16:51 +01002758 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002759 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002760 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002761 powerdown = 1;
2762 }
2763 }
2764
2765 /* If there were no widgets to power down we're already in
2766 * standby.
2767 */
2768 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002769 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002770 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002771 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002772 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002773}
Mark Brown51737472009-06-22 13:16:51 +01002774
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002775/*
2776 * snd_soc_dapm_shutdown - callback for system shutdown
2777 */
2778void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2779{
2780 struct snd_soc_codec *codec;
2781
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002782 list_for_each_entry(codec, &card->codec_dev_list, list) {
2783 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002784 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002785 }
Mark Brown51737472009-06-22 13:16:51 +01002786}
2787
Richard Purdie2b97eab2006-10-06 18:32:18 +02002788/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002789MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002790MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2791MODULE_LICENSE("GPL");