blob: 605c225c709a9b2c5a2f62f43aae88bf1ee65704 [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 Brown15e4c722008-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 Brown15e4c722008-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 Brown15e4c722008-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 Brown15e4c722008-07-02 11:51:20 +0100115 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200116
117 kfree(buf);
Mark Brown15e4c722008-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);
131 return 0;
132}
133
134static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
135{
136 if (w->codec)
137 return snd_soc_write(w->codec, reg, val);
138 return 0;
139}
140
141static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
142 unsigned short reg, unsigned int mask, unsigned int value)
143{
144 int change;
145 unsigned int old, new;
146 int ret;
147
148 ret = soc_widget_read(w, reg);
149 if (ret < 0)
150 return ret;
151
152 old = ret;
153 new = (old & ~mask) | (value & mask);
154 change = old != new;
155 if (change) {
156 ret = soc_widget_write(w, reg, new);
157 if (ret < 0)
158 return ret;
159 }
160
161 return change;
162}
163
Mark Brown452c5ea2009-05-17 21:41:23 +0100164/**
165 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800166 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100167 * @level: level to configure
168 *
169 * Configure the bias (power) levels for the SoC audio device.
170 *
171 * Returns 0 for success else error.
172 */
Mark Browned5a4c42011-02-18 11:12:42 -0800173static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200174 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100175{
Mark Browned5a4c42011-02-18 11:12:42 -0800176 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100177 int ret = 0;
178
Mark Brown84e90932010-11-04 00:07:02 -0400179 trace_snd_soc_bias_level_start(card, level);
180
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000181 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100182 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100183 if (ret != 0)
184 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100185
Mark Browncc4c6702011-06-06 19:03:34 +0100186 if (dapm->codec) {
187 if (dapm->codec->driver->set_bias_level)
188 ret = dapm->codec->driver->set_bias_level(dapm->codec,
189 level);
190 else
191 dapm->bias_level = level;
192 }
Mark Brown171ec6b2011-06-06 18:15:19 +0100193 if (ret != 0)
194 goto out;
195
196 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100197 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100198out:
Mark Brown84e90932010-11-04 00:07:02 -0400199 trace_snd_soc_bias_level_done(card, level);
200
Mark Brown452c5ea2009-05-17 21:41:23 +0100201 return ret;
202}
203
Richard Purdie2b97eab2006-10-06 18:32:18 +0200204/* set up initial codec paths */
205static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
206 struct snd_soc_dapm_path *p, int i)
207{
208 switch (w->id) {
209 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000210 case snd_soc_dapm_mixer:
211 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200212 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100213 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600214 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400215 unsigned int reg = mc->reg;
216 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100217 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400218 unsigned int mask = (1 << fls(max)) - 1;
219 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200220
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100221 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200222 val = (val >> shift) & mask;
223
224 if ((invert && !val) || (!invert && val))
225 p->connect = 1;
226 else
227 p->connect = 0;
228 }
229 break;
230 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600231 struct soc_enum *e = (struct soc_enum *)
232 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200233 int val, item, bitmask;
234
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100235 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100236 ;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100237 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200238 item = (val >> e->shift_l) & (bitmask - 1);
239
240 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100241 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200242 if (!(strcmp(p->name, e->texts[i])) && item == i)
243 p->connect = 1;
244 }
245 }
246 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000247 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600248 struct soc_enum *e = (struct soc_enum *)
249 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000250
251 p->connect = 0;
252 /* since a virtual mux has no backing registers to
253 * decide which path to connect, it will try to match
254 * with the first enumeration. This is to ensure
255 * that the default mux choice (the first) will be
256 * correctly powered up during initialization.
257 */
258 if (!strcmp(p->name, e->texts[0]))
259 p->connect = 1;
260 }
261 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200262 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200263 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600264 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200265 int val, item;
266
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100267 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200268 val = (val >> e->shift_l) & e->mask;
269 for (item = 0; item < e->max; item++) {
270 if (val == e->values[item])
271 break;
272 }
273
274 p->connect = 0;
275 for (i = 0; i < e->max; i++) {
276 if (!(strcmp(p->name, e->texts[i])) && item == i)
277 p->connect = 1;
278 }
279 }
280 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200281 /* does not effect routing - always connected */
282 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600283 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200284 case snd_soc_dapm_output:
285 case snd_soc_dapm_adc:
286 case snd_soc_dapm_input:
287 case snd_soc_dapm_dac:
288 case snd_soc_dapm_micbias:
289 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100290 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100291 case snd_soc_dapm_aif_in:
292 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200293 p->connect = 1;
294 break;
295 /* does effect routing - dynamically connected */
296 case snd_soc_dapm_hp:
297 case snd_soc_dapm_mic:
298 case snd_soc_dapm_spk:
299 case snd_soc_dapm_line:
300 case snd_soc_dapm_pre:
301 case snd_soc_dapm_post:
302 p->connect = 0;
303 break;
304 }
305}
306
Mark Brown74b8f952009-06-06 11:26:15 +0100307/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200308static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200309 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
310 struct snd_soc_dapm_path *path, const char *control_name,
311 const struct snd_kcontrol_new *kcontrol)
312{
313 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
314 int i;
315
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100316 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200317 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200318 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200319 list_add(&path->list_sink, &dest->sources);
320 list_add(&path->list_source, &src->sinks);
321 path->name = (char*)e->texts[i];
322 dapm_set_path_status(dest, path, 0);
323 return 0;
324 }
325 }
326
327 return -ENODEV;
328}
329
Mark Brown74b8f952009-06-06 11:26:15 +0100330/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200331static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200332 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
333 struct snd_soc_dapm_path *path, const char *control_name)
334{
335 int i;
336
337 /* search for mixer kcontrol */
338 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600339 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200340 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200341 list_add(&path->list_sink, &dest->sources);
342 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600343 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200344 dapm_set_path_status(dest, path, i);
345 return 0;
346 }
347 }
348 return -ENODEV;
349}
350
Stephen Warrenaf468002011-04-28 17:38:01 -0600351static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600352 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600353 const struct snd_kcontrol_new *kcontrol_new,
354 struct snd_kcontrol **kcontrol)
355{
356 struct snd_soc_dapm_widget *w;
357 int i;
358
359 *kcontrol = NULL;
360
361 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600362 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
363 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600364 for (i = 0; i < w->num_kcontrols; i++) {
365 if (&w->kcontrol_news[i] == kcontrol_new) {
366 if (w->kcontrols)
367 *kcontrol = w->kcontrols[i];
368 return 1;
369 }
370 }
371 }
372
373 return 0;
374}
375
Richard Purdie2b97eab2006-10-06 18:32:18 +0200376/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200377static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200378{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200379 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200380 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000381 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200382 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000383 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000384 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600385 struct snd_soc_dapm_widget_list *wlist;
386 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000387
388 if (dapm->codec)
389 prefix = dapm->codec->name_prefix;
390 else
391 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200392
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000393 if (prefix)
394 prefix_len = strlen(prefix) + 1;
395 else
396 prefix_len = 0;
397
Richard Purdie2b97eab2006-10-06 18:32:18 +0200398 /* add kcontrol */
399 for (i = 0; i < w->num_kcontrols; i++) {
400
401 /* match name */
402 list_for_each_entry(path, &w->sources, list_sink) {
403
404 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600405 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200406 continue;
407
Stephen Warrenfafd2172011-04-28 17:38:00 -0600408 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
409 sizeof(struct snd_soc_dapm_widget *),
410 wlist = kzalloc(wlistsize, GFP_KERNEL);
411 if (wlist == NULL) {
412 dev_err(dapm->dev,
413 "asoc: can't allocate widget list for %s\n",
414 w->name);
415 return -ENOMEM;
416 }
417 wlist->num_widgets = 1;
418 wlist->widgets[0] = w;
419
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000420 /* add dapm control with long name.
421 * for dapm_mixer this is the concatenation of the
422 * mixer and kcontrol name.
423 * for dapm_mixer_named_ctl this is simply the
424 * kcontrol name.
425 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600426 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000427 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000428 name_len += 1 + strlen(w->name);
429
Mark Brown219b93f2008-10-28 13:02:31 +0000430 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000431
Stephen Warrenfafd2172011-04-28 17:38:00 -0600432 if (path->long_name == NULL) {
433 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200434 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600435 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200436
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000437 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000438 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000439 /* The control will get a prefix from
440 * the control creation process but
441 * we're also using the same prefix
442 * for widgets so cut the prefix off
443 * the front of the widget name.
444 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000445 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000446 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600447 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000448 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000449 case snd_soc_dapm_mixer_named_ctl:
450 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600451 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000452 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000453 }
454
Mark Brown219b93f2008-10-28 13:02:31 +0000455 path->long_name[name_len - 1] = '\0';
456
Stephen Warrenfafd2172011-04-28 17:38:00 -0600457 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
458 wlist, path->long_name,
459 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200460 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200461 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200462 dev_err(dapm->dev,
463 "asoc: failed to add dapm kcontrol %s: %d\n",
464 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600465 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200466 kfree(path->long_name);
467 path->long_name = NULL;
468 return ret;
469 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600470 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200471 }
472 }
473 return ret;
474}
475
476/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200477static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200478{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200479 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200480 struct snd_soc_dapm_path *path = NULL;
481 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000482 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000483 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000484 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600485 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600486 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600487 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600488 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600489 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200490
Stephen Warrenaf468002011-04-28 17:38:01 -0600491 if (w->num_kcontrols != 1) {
492 dev_err(dapm->dev,
493 "asoc: mux %s has incorrect number of controls\n",
494 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200495 return -EINVAL;
496 }
497
Stephen Warren1007da02011-05-26 09:57:33 -0600498 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600499 &kcontrol);
500 if (kcontrol) {
501 wlist = kcontrol->private_data;
502 wlistentries = wlist->num_widgets + 1;
503 } else {
504 wlist = NULL;
505 wlistentries = 1;
506 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600507 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600508 wlistentries * sizeof(struct snd_soc_dapm_widget *),
509 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600510 if (wlist == NULL) {
511 dev_err(dapm->dev,
512 "asoc: can't allocate widget list for %s\n", w->name);
513 return -ENOMEM;
514 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600515 wlist->num_widgets = wlistentries;
516 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600517
Stephen Warrenaf468002011-04-28 17:38:01 -0600518 if (!kcontrol) {
519 if (dapm->codec)
520 prefix = dapm->codec->name_prefix;
521 else
522 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000523
Stephen Warrenaf468002011-04-28 17:38:01 -0600524 if (shared) {
525 name = w->kcontrol_news[0].name;
526 prefix_len = 0;
527 } else {
528 name = w->name;
529 if (prefix)
530 prefix_len = strlen(prefix) + 1;
531 else
532 prefix_len = 0;
533 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000534
Stephen Warrenaf468002011-04-28 17:38:01 -0600535 /*
536 * The control will get a prefix from the control creation
537 * process but we're also using the same prefix for widgets so
538 * cut the prefix off the front of the widget name.
539 */
540 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
541 name + prefix_len, prefix);
542 ret = snd_ctl_add(card, kcontrol);
543 if (ret < 0) {
544 dev_err(dapm->dev,
545 "asoc: failed to add kcontrol %s\n", w->name);
546 kfree(wlist);
547 return ret;
548 }
549 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200550
Stephen Warrenaf468002011-04-28 17:38:01 -0600551 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200552
Stephen Warrenfad59882011-04-28 17:37:59 -0600553 w->kcontrols[0] = kcontrol;
554
Richard Purdie2b97eab2006-10-06 18:32:18 +0200555 list_for_each_entry(path, &w->sources, list_sink)
556 path->kcontrol = kcontrol;
557
Stephen Warrenaf468002011-04-28 17:38:01 -0600558 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200559}
560
561/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200562static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200563{
Mark Browna6c65732010-03-03 17:45:21 +0000564 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200565 dev_err(w->dapm->dev,
566 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200567
Mark Browna6c65732010-03-03 17:45:21 +0000568 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200569}
570
571/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200572static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200573{
574 struct snd_soc_dapm_path *p;
575
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200576 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200577 p->walked = 0;
578}
579
Mark Brown99497882010-05-07 20:24:05 +0100580/* We implement power down on suspend by checking the power state of
581 * the ALSA card - when we are suspending the ALSA state for the card
582 * is set to D3.
583 */
584static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
585{
Mark Brown12ea2c72011-03-02 18:17:32 +0000586 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100587
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100589 case SNDRV_CTL_POWER_D3hot:
590 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100591 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200592 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
593 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100594 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100595 default:
596 return 1;
597 }
598}
599
Richard Purdie2b97eab2006-10-06 18:32:18 +0200600/*
601 * Recursively check for a completed path to an active or physically connected
602 * output widget. Returns number of complete paths.
603 */
604static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
605{
606 struct snd_soc_dapm_path *path;
607 int con = 0;
608
Mark Brown246d0a12009-04-22 18:24:55 +0100609 if (widget->id == snd_soc_dapm_supply)
610 return 0;
611
Mark Brown010ff262009-08-17 17:39:22 +0100612 switch (widget->id) {
613 case snd_soc_dapm_adc:
614 case snd_soc_dapm_aif_out:
615 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100616 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100617 default:
618 break;
619 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200620
621 if (widget->connected) {
622 /* connected pin ? */
623 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100624 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200625
626 /* connected jack or spk ? */
627 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300628 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100629 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200630 }
631
632 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Brownbf3a9e12011-06-13 16:42:29 +0100633 if (path->weak)
634 continue;
635
Richard Purdie2b97eab2006-10-06 18:32:18 +0200636 if (path->walked)
637 continue;
638
639 if (path->sink && path->connect) {
640 path->walked = 1;
641 con += is_connected_output_ep(path->sink);
642 }
643 }
644
645 return con;
646}
647
648/*
649 * Recursively check for a completed path to an active or physically connected
650 * input widget. Returns number of complete paths.
651 */
652static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
653{
654 struct snd_soc_dapm_path *path;
655 int con = 0;
656
Mark Brown246d0a12009-04-22 18:24:55 +0100657 if (widget->id == snd_soc_dapm_supply)
658 return 0;
659
Richard Purdie2b97eab2006-10-06 18:32:18 +0200660 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100661 switch (widget->id) {
662 case snd_soc_dapm_dac:
663 case snd_soc_dapm_aif_in:
664 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100665 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100666 default:
667 break;
668 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200669
670 if (widget->connected) {
671 /* connected pin ? */
672 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100673 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200674
675 /* connected VMID/Bias for lower pops */
676 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100677 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200678
679 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300680 if (widget->id == snd_soc_dapm_mic ||
681 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100682 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200683 }
684
685 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Brownbf3a9e12011-06-13 16:42:29 +0100686 if (path->weak)
687 continue;
688
Richard Purdie2b97eab2006-10-06 18:32:18 +0200689 if (path->walked)
690 continue;
691
692 if (path->source && path->connect) {
693 path->walked = 1;
694 con += is_connected_input_ep(path->source);
695 }
696 }
697
698 return con;
699}
700
701/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300702 * Handler for generic register modifier widget.
703 */
704int dapm_reg_event(struct snd_soc_dapm_widget *w,
705 struct snd_kcontrol *kcontrol, int event)
706{
707 unsigned int val;
708
709 if (SND_SOC_DAPM_EVENT_ON(event))
710 val = w->on_val;
711 else
712 val = w->off_val;
713
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100714 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300715 w->mask << w->shift, val << w->shift);
716
717 return 0;
718}
Mark Brown11589412008-07-29 11:42:23 +0100719EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300720
Mark Browncd0f2d42009-04-20 16:56:59 +0100721/* Generic check to see if a widget should be powered.
722 */
723static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
724{
725 int in, out;
726
727 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200728 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100729 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200730 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100731 return out != 0 && in != 0;
732}
733
Mark Brown6ea31b92009-04-20 17:15:41 +0100734/* Check to see if an ADC has power */
735static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
736{
737 int in;
738
739 if (w->active) {
740 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200741 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100742 return in != 0;
743 } else {
744 return dapm_generic_check_power(w);
745 }
746}
747
748/* Check to see if a DAC has power */
749static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
750{
751 int out;
752
753 if (w->active) {
754 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200755 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100756 return out != 0;
757 } else {
758 return dapm_generic_check_power(w);
759 }
760}
761
Mark Brown246d0a12009-04-22 18:24:55 +0100762/* Check to see if a power supply is needed */
763static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
764{
765 struct snd_soc_dapm_path *path;
766 int power = 0;
767
768 /* Check if one of our outputs is connected */
769 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brownbf3a9e12011-06-13 16:42:29 +0100770 if (path->weak)
771 continue;
772
Mark Brown215edda2009-09-08 18:59:05 +0100773 if (path->connected &&
774 !path->connected(path->source, path->sink))
775 continue;
776
Mark Brown30173582011-02-11 11:42:19 +0000777 if (!path->sink)
778 continue;
779
780 if (path->sink->force) {
781 power = 1;
782 break;
783 }
784
785 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100786 path->sink->power_check(path->sink)) {
787 power = 1;
788 break;
789 }
790 }
791
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200792 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100793
794 return power;
795}
796
Mark Brown38357ab2009-06-06 19:03:23 +0100797static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
798 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000799 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000800{
Mark Brown828a8422011-01-15 13:14:30 +0000801 int *sort;
802
803 if (power_up)
804 sort = dapm_up_seq;
805 else
806 sort = dapm_down_seq;
807
Mark Brown38357ab2009-06-06 19:03:23 +0100808 if (sort[a->id] != sort[b->id])
809 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000810 if (a->subseq != b->subseq) {
811 if (power_up)
812 return a->subseq - b->subseq;
813 else
814 return b->subseq - a->subseq;
815 }
Mark Brownb22ead22009-06-07 12:51:26 +0100816 if (a->reg != b->reg)
817 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000818 if (a->dapm != b->dapm)
819 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000820
Mark Brown38357ab2009-06-06 19:03:23 +0100821 return 0;
822}
Mark Brown42aa3412009-03-01 19:21:10 +0000823
Mark Brown38357ab2009-06-06 19:03:23 +0100824/* Insert a widget in order into a DAPM power sequence. */
825static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
826 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000827 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100828{
829 struct snd_soc_dapm_widget *w;
830
831 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000832 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100833 list_add_tail(&new_widget->power_list, &w->power_list);
834 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000835 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100836
Mark Brown38357ab2009-06-06 19:03:23 +0100837 list_add_tail(&new_widget->power_list, list);
838}
Mark Brown42aa3412009-03-01 19:21:10 +0000839
Mark Brown68f89ad2010-11-03 23:51:49 -0400840static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
841 struct snd_soc_dapm_widget *w, int event)
842{
843 struct snd_soc_card *card = dapm->card;
844 const char *ev_name;
845 int power, ret;
846
847 switch (event) {
848 case SND_SOC_DAPM_PRE_PMU:
849 ev_name = "PRE_PMU";
850 power = 1;
851 break;
852 case SND_SOC_DAPM_POST_PMU:
853 ev_name = "POST_PMU";
854 power = 1;
855 break;
856 case SND_SOC_DAPM_PRE_PMD:
857 ev_name = "PRE_PMD";
858 power = 0;
859 break;
860 case SND_SOC_DAPM_POST_PMD:
861 ev_name = "POST_PMD";
862 power = 0;
863 break;
864 default:
865 BUG();
866 return;
867 }
868
869 if (w->power != power)
870 return;
871
872 if (w->event && (w->event_flags & event)) {
873 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
874 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400875 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400876 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400877 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400878 if (ret < 0)
879 pr_err("%s: %s event failed: %d\n",
880 ev_name, w->name, ret);
881 }
882}
883
Mark Brownb22ead22009-06-07 12:51:26 +0100884/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200885static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100886 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100887{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200888 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400889 struct snd_soc_dapm_widget *w;
890 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100891 unsigned int value = 0;
892 unsigned int mask = 0;
893 unsigned int cur_mask;
894
895 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
896 power_list)->reg;
897
898 list_for_each_entry(w, pending, power_list) {
899 cur_mask = 1 << w->shift;
900 BUG_ON(reg != w->reg);
901
902 if (w->invert)
903 power = !w->power;
904 else
905 power = w->power;
906
907 mask |= cur_mask;
908 if (power)
909 value |= cur_mask;
910
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200911 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100912 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
913 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100914
Mark Brown68f89ad2010-11-03 23:51:49 -0400915 /* Check for events */
916 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
917 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100918 }
919
Mark Brown81628102009-06-07 13:21:24 +0100920 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200921 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100922 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200923 value, mask, reg, card->pop_time);
924 pop_wait(card->pop_time);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100925 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100926 }
927
928 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400929 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
930 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000931 }
Mark Brown42aa3412009-03-01 19:21:10 +0000932}
933
Mark Brownb22ead22009-06-07 12:51:26 +0100934/* Apply a DAPM power sequence.
935 *
936 * We walk over a pre-sorted list of widgets to apply power to. In
937 * order to minimise the number of writes to the device required
938 * multiple widgets will be updated in a single write where possible.
939 * Currently anything that requires more than a single write is not
940 * handled.
941 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200942static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +0000943 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +0100944{
945 struct snd_soc_dapm_widget *w, *n;
946 LIST_HEAD(pending);
947 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000948 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100949 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200950 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +0000951 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +0000952 int *sort;
953
954 if (power_up)
955 sort = dapm_up_seq;
956 else
957 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +0100958
Mark Brownb22ead22009-06-07 12:51:26 +0100959 list_for_each_entry_safe(w, n, list, power_list) {
960 ret = 0;
961
962 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200963 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +0000964 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +0100965 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200966 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +0100967
Mark Brown474b62d2011-01-18 16:14:44 +0000968 if (cur_dapm && cur_dapm->seq_notifier) {
969 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
970 if (sort[i] == cur_sort)
971 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +0000972 i,
973 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +0000974 }
975
Mark Brownb22ead22009-06-07 12:51:26 +0100976 INIT_LIST_HEAD(&pending);
977 cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000978 cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100979 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200980 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +0100981 }
982
Mark Brown163cac02009-06-07 10:12:52 +0100983 switch (w->id) {
984 case snd_soc_dapm_pre:
985 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100986 list_for_each_entry_safe_continue(w, n, list,
987 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100988
Mark Brownb22ead22009-06-07 12:51:26 +0100989 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100990 ret = w->event(w,
991 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100992 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100993 ret = w->event(w,
994 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100995 break;
996
997 case snd_soc_dapm_post:
998 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100999 list_for_each_entry_safe_continue(w, n, list,
1000 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001001
Mark Brownb22ead22009-06-07 12:51:26 +01001002 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001003 ret = w->event(w,
1004 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001005 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001006 ret = w->event(w,
1007 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001008 break;
1009
Mark Brown163cac02009-06-07 10:12:52 +01001010 default:
Mark Brown81628102009-06-07 13:21:24 +01001011 /* Queue it up for application */
1012 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001013 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001014 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001015 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001016 list_move(&w->power_list, &pending);
1017 break;
Mark Brown163cac02009-06-07 10:12:52 +01001018 }
Mark Brownb22ead22009-06-07 12:51:26 +01001019
1020 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001021 dev_err(w->dapm->dev,
1022 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001023 }
Mark Brownb22ead22009-06-07 12:51:26 +01001024
1025 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001026 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001027
1028 if (cur_dapm && cur_dapm->seq_notifier) {
1029 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1030 if (sort[i] == cur_sort)
1031 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001032 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001033 }
Mark Brown163cac02009-06-07 10:12:52 +01001034}
1035
Mark Brown97404f22010-12-14 16:13:57 +00001036static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1037{
1038 struct snd_soc_dapm_update *update = dapm->update;
1039 struct snd_soc_dapm_widget *w;
1040 int ret;
1041
1042 if (!update)
1043 return;
1044
1045 w = update->widget;
1046
1047 if (w->event &&
1048 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1049 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1050 if (ret != 0)
1051 pr_err("%s DAPM pre-event failed: %d\n",
1052 w->name, ret);
1053 }
1054
1055 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1056 update->val);
1057 if (ret < 0)
1058 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1059
1060 if (w->event &&
1061 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1062 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1063 if (ret != 0)
1064 pr_err("%s DAPM post-event failed: %d\n",
1065 w->name, ret);
1066 }
1067}
1068
Mark Brown9d0624a2011-02-18 11:49:43 -08001069/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1070 * they're changing state.
1071 */
1072static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1073{
1074 struct snd_soc_dapm_context *d = data;
1075 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001076
Mark Brown56fba412011-06-04 11:25:10 +01001077 /* If we're off and we're not supposed to be go into STANDBY */
1078 if (d->bias_level == SND_SOC_BIAS_OFF &&
1079 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001080 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1081 if (ret != 0)
1082 dev_err(d->dev,
1083 "Failed to turn on bias: %d\n", ret);
1084 }
1085
Mark Brown56fba412011-06-04 11:25:10 +01001086 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1087 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001088 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1089 if (ret != 0)
1090 dev_err(d->dev,
1091 "Failed to prepare bias: %d\n", ret);
1092 }
1093}
1094
1095/* Async callback run prior to DAPM sequences - brings to their final
1096 * state.
1097 */
1098static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1099{
1100 struct snd_soc_dapm_context *d = data;
1101 int ret;
1102
1103 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001104 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1105 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1106 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001107 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1108 if (ret != 0)
1109 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1110 ret);
1111 }
1112
1113 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001114 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1115 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001116 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1117 if (ret != 0)
1118 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1119 }
1120
1121 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001122 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1123 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001124 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1125 if (ret != 0)
1126 dev_err(d->dev, "Failed to apply active bias: %d\n",
1127 ret);
1128 }
1129}
Mark Brown97404f22010-12-14 16:13:57 +00001130
Mark Brown42aa3412009-03-01 19:21:10 +00001131/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001132 * Scan each dapm widget for complete audio path.
1133 * A complete path is a route that has valid endpoints i.e.:-
1134 *
1135 * o DAC to output pin.
1136 * o Input Pin to ADC.
1137 * o Input pin to Output pin (bypass, sidetone)
1138 * o DAC to ADC (loopback).
1139 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001140static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001141{
Mark Brown12ea2c72011-03-02 18:17:32 +00001142 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001143 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001144 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001145 LIST_HEAD(up_list);
1146 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001147 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001148 enum snd_soc_bias_level bias;
Mark Brown38357ab2009-06-06 19:03:23 +01001149 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001150
Mark Brown84e90932010-11-04 00:07:02 -04001151 trace_snd_soc_dapm_start(card);
1152
Mark Brown56fba412011-06-04 11:25:10 +01001153 list_for_each_entry(d, &card->dapm_list, list) {
1154 if (d->n_widgets || d->codec == NULL) {
1155 if (d->idle_bias_off)
1156 d->target_bias_level = SND_SOC_BIAS_OFF;
1157 else
1158 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1159 }
1160 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001161
Mark Brown6d3ddc82009-05-16 17:47:29 +01001162 /* Check which widgets we need to power and store them in
1163 * lists indicating if they should be powered up or down.
1164 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001165 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001166 switch (w->id) {
1167 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001168 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001169 break;
1170 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001171 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001172 break;
1173
1174 default:
1175 if (!w->power_check)
1176 continue;
1177
Mark Brown99497882010-05-07 20:24:05 +01001178 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001179 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001180 else
1181 power = 1;
Mark Browndfcc9042011-06-04 11:34:43 +01001182
1183 if (power) {
1184 d = w->dapm;
1185
1186 /* Supplies and micbiases only bring
1187 * the context up to STANDBY as unless
1188 * something else is active and
1189 * passing audio they generally don't
1190 * require full power.
1191 */
1192 switch (w->id) {
1193 case snd_soc_dapm_supply:
1194 case snd_soc_dapm_micbias:
1195 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1196 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1197 break;
1198 default:
1199 d->target_bias_level = SND_SOC_BIAS_ON;
1200 break;
1201 }
1202 }
Mark Brown452c5ea2009-05-17 21:41:23 +01001203
Mark Brown6d3ddc82009-05-16 17:47:29 +01001204 if (w->power == power)
1205 continue;
1206
Mark Brown84e90932010-11-04 00:07:02 -04001207 trace_snd_soc_dapm_widget_power(w, power);
1208
Mark Brown6d3ddc82009-05-16 17:47:29 +01001209 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001210 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001211 else
Mark Brown828a8422011-01-15 13:14:30 +00001212 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001213
1214 w->power = power;
1215 break;
1216 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001217 }
1218
Mark Brownb14b76a2009-08-17 11:55:38 +01001219 /* If there are no DAPM widgets then try to figure out power from the
1220 * event type.
1221 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001222 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001223 switch (event) {
1224 case SND_SOC_DAPM_STREAM_START:
1225 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001226 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a2009-08-17 11:55:38 +01001227 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001228 case SND_SOC_DAPM_STREAM_STOP:
Mark Brown56fba412011-06-04 11:25:10 +01001229 if (dapm->codec->active)
1230 dapm->target_bias_level = SND_SOC_BIAS_ON;
1231 else
1232 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001233 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001234 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001235 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001236 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001237 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001238 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001239 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001240 default:
1241 break;
1242 }
1243 }
1244
Mark Brown52ba67b2011-04-04 21:05:11 +09001245 /* Force all contexts in the card to the same bias state */
Mark Brown56fba412011-06-04 11:25:10 +01001246 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001247 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001248 if (d->target_bias_level > bias)
1249 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001250 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001251 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001252
1253
Mark Brown9d0624a2011-02-18 11:49:43 -08001254 /* Run all the bias changes in parallel */
1255 list_for_each_entry(d, &dapm->card->dapm_list, list)
1256 async_schedule_domain(dapm_pre_sequence_async, d,
1257 &async_domain);
1258 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001259
Mark Brown6d3ddc82009-05-16 17:47:29 +01001260 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001261 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001262
Mark Brown97404f22010-12-14 16:13:57 +00001263 dapm_widget_update(dapm);
1264
Mark Brown6d3ddc82009-05-16 17:47:29 +01001265 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001266 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001267
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_post_sequence_async, d,
1271 &async_domain);
1272 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001273
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001274 pop_dbg(dapm->dev, card->pop_time,
1275 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001276 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001277
Mark Brown84e90932010-11-04 00:07:02 -04001278 trace_snd_soc_dapm_done(card);
1279
Mark Brown42aa3412009-03-01 19:21:10 +00001280 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001281}
1282
Mark Brown79fb9382009-08-21 16:38:13 +01001283#ifdef CONFIG_DEBUG_FS
1284static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1285{
1286 file->private_data = inode->i_private;
1287 return 0;
1288}
1289
1290static ssize_t dapm_widget_power_read_file(struct file *file,
1291 char __user *user_buf,
1292 size_t count, loff_t *ppos)
1293{
1294 struct snd_soc_dapm_widget *w = file->private_data;
1295 char *buf;
1296 int in, out;
1297 ssize_t ret;
1298 struct snd_soc_dapm_path *p = NULL;
1299
1300 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1301 if (!buf)
1302 return -ENOMEM;
1303
1304 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001305 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001306 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001307 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001308
Mark Brownd033c362009-12-04 15:25:56 +00001309 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001310 w->name, w->power ? "On" : "Off", in, out);
1311
Mark Brownd033c362009-12-04 15:25:56 +00001312 if (w->reg >= 0)
1313 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1314 " - R%d(0x%x) bit %d",
1315 w->reg, w->reg, w->shift);
1316
1317 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1318
Mark Brown3eef08b2009-09-14 16:49:00 +01001319 if (w->sname)
1320 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1321 w->sname,
1322 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001323
1324 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001325 if (p->connected && !p->connected(w, p->sink))
1326 continue;
1327
Mark Brown79fb9382009-08-21 16:38:13 +01001328 if (p->connect)
1329 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001330 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001331 p->name ? p->name : "static",
1332 p->source->name);
1333 }
1334 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001335 if (p->connected && !p->connected(w, p->sink))
1336 continue;
1337
Mark Brown79fb9382009-08-21 16:38:13 +01001338 if (p->connect)
1339 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001340 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001341 p->name ? p->name : "static",
1342 p->sink->name);
1343 }
1344
1345 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1346
1347 kfree(buf);
1348 return ret;
1349}
1350
1351static const struct file_operations dapm_widget_power_fops = {
1352 .open = dapm_widget_power_open_file,
1353 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001354 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001355};
1356
Mark Brownef49e4f2011-04-04 20:48:13 +09001357static int dapm_bias_open_file(struct inode *inode, struct file *file)
1358{
1359 file->private_data = inode->i_private;
1360 return 0;
1361}
1362
1363static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1364 size_t count, loff_t *ppos)
1365{
1366 struct snd_soc_dapm_context *dapm = file->private_data;
1367 char *level;
1368
1369 switch (dapm->bias_level) {
1370 case SND_SOC_BIAS_ON:
1371 level = "On\n";
1372 break;
1373 case SND_SOC_BIAS_PREPARE:
1374 level = "Prepare\n";
1375 break;
1376 case SND_SOC_BIAS_STANDBY:
1377 level = "Standby\n";
1378 break;
1379 case SND_SOC_BIAS_OFF:
1380 level = "Off\n";
1381 break;
1382 default:
1383 BUG();
1384 level = "Unknown\n";
1385 break;
1386 }
1387
1388 return simple_read_from_buffer(user_buf, count, ppos, level,
1389 strlen(level));
1390}
1391
1392static const struct file_operations dapm_bias_fops = {
1393 .open = dapm_bias_open_file,
1394 .read = dapm_bias_read_file,
1395 .llseek = default_llseek,
1396};
1397
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001398void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1399 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001400{
Mark Brown79fb9382009-08-21 16:38:13 +01001401 struct dentry *d;
1402
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001403 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1404
1405 if (!dapm->debugfs_dapm) {
1406 printk(KERN_WARNING
1407 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001408 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001409 }
Mark Brown79fb9382009-08-21 16:38:13 +01001410
Mark Brownef49e4f2011-04-04 20:48:13 +09001411 d = debugfs_create_file("bias_level", 0444,
1412 dapm->debugfs_dapm, dapm,
1413 &dapm_bias_fops);
1414 if (!d)
1415 dev_warn(dapm->dev,
1416 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001417}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001418
1419static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1420{
1421 struct snd_soc_dapm_context *dapm = w->dapm;
1422 struct dentry *d;
1423
1424 if (!dapm->debugfs_dapm || !w->name)
1425 return;
1426
1427 d = debugfs_create_file(w->name, 0444,
1428 dapm->debugfs_dapm, w,
1429 &dapm_widget_power_fops);
1430 if (!d)
1431 dev_warn(w->dapm->dev,
1432 "ASoC: Failed to create %s debugfs file\n",
1433 w->name);
1434}
1435
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001436static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1437{
1438 debugfs_remove_recursive(dapm->debugfs_dapm);
1439}
1440
Mark Brown79fb9382009-08-21 16:38:13 +01001441#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001442void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1443 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001444{
1445}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001446
1447static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1448{
1449}
1450
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001451static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1452{
1453}
1454
Mark Brown79fb9382009-08-21 16:38:13 +01001455#endif
1456
Richard Purdie2b97eab2006-10-06 18:32:18 +02001457/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001458static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001459 struct snd_kcontrol *kcontrol, int change,
1460 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001461{
1462 struct snd_soc_dapm_path *path;
1463 int found = 0;
1464
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001465 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001466 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001467 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001468 return -ENODEV;
1469
Mark Brown3a655772009-10-05 17:23:30 +01001470 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001471 return 0;
1472
1473 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001474 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001475 if (path->kcontrol != kcontrol)
1476 continue;
1477
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001478 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001479 continue;
1480
1481 found = 1;
1482 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001483 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001484 path->connect = 1; /* new connection */
1485 else
1486 path->connect = 0; /* old connection must be powered down */
1487 }
1488
Mark Brownb91b8fa2010-01-20 18:18:35 +00001489 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001490 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001491
1492 return 0;
1493}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001494
Milan plzik1b075e32008-01-10 14:39:46 +01001495/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001496static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001497 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001498{
1499 struct snd_soc_dapm_path *path;
1500 int found = 0;
1501
Milan plzik1b075e32008-01-10 14:39:46 +01001502 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001503 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001504 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001505 return -ENODEV;
1506
Richard Purdie2b97eab2006-10-06 18:32:18 +02001507 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001508 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001509 if (path->kcontrol != kcontrol)
1510 continue;
1511
1512 /* found, now check type */
1513 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001514 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001515 break;
1516 }
1517
Mark Brownb91b8fa2010-01-20 18:18:35 +00001518 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001519 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001520
1521 return 0;
1522}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001523
1524/* show dapm widget status in sys fs */
1525static ssize_t dapm_widget_show(struct device *dev,
1526 struct device_attribute *attr, char *buf)
1527{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001528 struct snd_soc_pcm_runtime *rtd =
1529 container_of(dev, struct snd_soc_pcm_runtime, dev);
1530 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001531 struct snd_soc_dapm_widget *w;
1532 int count = 0;
1533 char *state = "not set";
1534
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001535 list_for_each_entry(w, &codec->card->widgets, list) {
1536 if (w->dapm != &codec->dapm)
1537 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001538
1539 /* only display widgets that burnm power */
1540 switch (w->id) {
1541 case snd_soc_dapm_hp:
1542 case snd_soc_dapm_mic:
1543 case snd_soc_dapm_spk:
1544 case snd_soc_dapm_line:
1545 case snd_soc_dapm_micbias:
1546 case snd_soc_dapm_dac:
1547 case snd_soc_dapm_adc:
1548 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001549 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001550 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001551 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001552 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001553 if (w->name)
1554 count += sprintf(buf + count, "%s: %s\n",
1555 w->name, w->power ? "On":"Off");
1556 break;
1557 default:
1558 break;
1559 }
1560 }
1561
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001562 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001563 case SND_SOC_BIAS_ON:
1564 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001565 break;
Mark Brown0be98982008-05-19 12:31:28 +02001566 case SND_SOC_BIAS_PREPARE:
1567 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001568 break;
Mark Brown0be98982008-05-19 12:31:28 +02001569 case SND_SOC_BIAS_STANDBY:
1570 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001571 break;
Mark Brown0be98982008-05-19 12:31:28 +02001572 case SND_SOC_BIAS_OFF:
1573 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001574 break;
1575 }
1576 count += sprintf(buf + count, "PM State: %s\n", state);
1577
1578 return count;
1579}
1580
1581static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1582
1583int snd_soc_dapm_sys_add(struct device *dev)
1584{
Troy Kisky12ef1932008-10-13 17:42:14 -07001585 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001586}
1587
1588static void snd_soc_dapm_sys_remove(struct device *dev)
1589{
Mark Brownaef90842009-05-16 17:53:16 +01001590 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001591}
1592
1593/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001594static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001595{
1596 struct snd_soc_dapm_widget *w, *next_w;
1597 struct snd_soc_dapm_path *p, *next_p;
1598
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001599 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1600 if (w->dapm != dapm)
1601 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001602 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001603 /*
1604 * remove source and sink paths associated to this widget.
1605 * While removing the path, remove reference to it from both
1606 * source and sink widgets so that path is removed only once.
1607 */
1608 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1609 list_del(&p->list_sink);
1610 list_del(&p->list_source);
1611 list_del(&p->list);
1612 kfree(p->long_name);
1613 kfree(p);
1614 }
1615 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1616 list_del(&p->list_sink);
1617 list_del(&p->list_source);
1618 list_del(&p->list);
1619 kfree(p->long_name);
1620 kfree(p);
1621 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001622 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001623 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001624 kfree(w);
1625 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001626}
1627
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001628static struct snd_soc_dapm_widget *dapm_find_widget(
1629 struct snd_soc_dapm_context *dapm, const char *pin,
1630 bool search_other_contexts)
1631{
1632 struct snd_soc_dapm_widget *w;
1633 struct snd_soc_dapm_widget *fallback = NULL;
1634
1635 list_for_each_entry(w, &dapm->card->widgets, list) {
1636 if (!strcmp(w->name, pin)) {
1637 if (w->dapm == dapm)
1638 return w;
1639 else
1640 fallback = w;
1641 }
1642 }
1643
1644 if (search_other_contexts)
1645 return fallback;
1646
1647 return NULL;
1648}
1649
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001650static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001651 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001652{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001653 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001654
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001655 if (!w) {
1656 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1657 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001658 }
1659
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001660 w->connected = status;
1661 if (status == 0)
1662 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001663
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001664 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001665}
1666
Richard Purdie2b97eab2006-10-06 18:32:18 +02001667/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001668 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001669 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001670 *
1671 * Walks all dapm audio paths and powers widgets according to their
1672 * stream or path usage.
1673 *
1674 * Returns 0 for success.
1675 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001676int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001677{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001678 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001679}
Liam Girdwooda5302182008-07-07 13:35:17 +01001680EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001681
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001682static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001683 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001684{
1685 struct snd_soc_dapm_path *path;
1686 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001687 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001688 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001689 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001690 const char *source;
1691 char prefixed_sink[80];
1692 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001693 int ret = 0;
1694
Mark Brown88e8b9a2011-03-02 18:18:24 +00001695 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001696 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1697 dapm->codec->name_prefix, route->sink);
1698 sink = prefixed_sink;
1699 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1700 dapm->codec->name_prefix, route->source);
1701 source = prefixed_source;
1702 } else {
1703 sink = route->sink;
1704 source = route->source;
1705 }
1706
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001707 /*
1708 * find src and dest widgets over all widgets but favor a widget from
1709 * current DAPM context
1710 */
1711 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001712 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001713 wtsink = w;
1714 if (w->dapm == dapm)
1715 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001716 continue;
1717 }
1718 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001719 wtsource = w;
1720 if (w->dapm == dapm)
1721 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001722 }
1723 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001724 /* use widget from another DAPM context if not found from this */
1725 if (!wsink)
1726 wsink = wtsink;
1727 if (!wsource)
1728 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001729
1730 if (wsource == NULL || wsink == NULL)
1731 return -ENODEV;
1732
1733 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1734 if (!path)
1735 return -ENOMEM;
1736
1737 path->source = wsource;
1738 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001739 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001740 INIT_LIST_HEAD(&path->list);
1741 INIT_LIST_HEAD(&path->list_source);
1742 INIT_LIST_HEAD(&path->list_sink);
1743
1744 /* check for external widgets */
1745 if (wsink->id == snd_soc_dapm_input) {
1746 if (wsource->id == snd_soc_dapm_micbias ||
1747 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001748 wsource->id == snd_soc_dapm_line ||
1749 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001750 wsink->ext = 1;
1751 }
1752 if (wsource->id == snd_soc_dapm_output) {
1753 if (wsink->id == snd_soc_dapm_spk ||
1754 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001755 wsink->id == snd_soc_dapm_line ||
1756 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001757 wsource->ext = 1;
1758 }
1759
1760 /* connect static paths */
1761 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001762 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001763 list_add(&path->list_sink, &wsink->sources);
1764 list_add(&path->list_source, &wsource->sinks);
1765 path->connect = 1;
1766 return 0;
1767 }
1768
1769 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001770 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001771 case snd_soc_dapm_adc:
1772 case snd_soc_dapm_dac:
1773 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001774 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001775 case snd_soc_dapm_input:
1776 case snd_soc_dapm_output:
1777 case snd_soc_dapm_micbias:
1778 case snd_soc_dapm_vmid:
1779 case snd_soc_dapm_pre:
1780 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001781 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001782 case snd_soc_dapm_aif_in:
1783 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001784 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001785 list_add(&path->list_sink, &wsink->sources);
1786 list_add(&path->list_source, &wsource->sinks);
1787 path->connect = 1;
1788 return 0;
1789 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001790 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001791 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001792 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06001793 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001794 if (ret != 0)
1795 goto err;
1796 break;
1797 case snd_soc_dapm_switch:
1798 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001799 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001800 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001801 if (ret != 0)
1802 goto err;
1803 break;
1804 case snd_soc_dapm_hp:
1805 case snd_soc_dapm_mic:
1806 case snd_soc_dapm_line:
1807 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001808 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001809 list_add(&path->list_sink, &wsink->sources);
1810 list_add(&path->list_source, &wsource->sinks);
1811 path->connect = 0;
1812 return 0;
1813 }
1814 return 0;
1815
1816err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001817 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1818 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001819 kfree(path);
1820 return ret;
1821}
Mark Brown105f1c22008-05-13 14:52:19 +02001822
1823/**
Mark Brown105f1c22008-05-13 14:52:19 +02001824 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001825 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001826 * @route: audio routes
1827 * @num: number of routes
1828 *
1829 * Connects 2 dapm widgets together via a named audio path. The sink is
1830 * the widget receiving the audio signal, whilst the source is the sender
1831 * of the audio signal.
1832 *
1833 * Returns 0 for success else error. On error all resources can be freed
1834 * with a call to snd_soc_card_free().
1835 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001836int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001837 const struct snd_soc_dapm_route *route, int num)
1838{
1839 int i, ret;
1840
1841 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001842 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001843 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001844 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1845 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001846 return ret;
1847 }
1848 route++;
1849 }
1850
1851 return 0;
1852}
1853EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1854
Mark Brownbf3a9e12011-06-13 16:42:29 +01001855static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
1856 const struct snd_soc_dapm_route *route)
1857{
1858 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
1859 route->source,
1860 true);
1861 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
1862 route->sink,
1863 true);
1864 struct snd_soc_dapm_path *path;
1865 int count = 0;
1866
1867 if (!source) {
1868 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
1869 route->source);
1870 return -ENODEV;
1871 }
1872
1873 if (!sink) {
1874 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
1875 route->sink);
1876 return -ENODEV;
1877 }
1878
1879 if (route->control || route->connected)
1880 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
1881 route->source, route->sink);
1882
1883 list_for_each_entry(path, &source->sinks, list_source) {
1884 if (path->sink == sink) {
1885 path->weak = 1;
1886 count++;
1887 }
1888 }
1889
1890 if (count == 0)
1891 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
1892 route->source, route->sink);
1893 if (count > 1)
1894 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
1895 count, route->source, route->sink);
1896
1897 return 0;
1898}
1899
1900/**
1901 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
1902 * @dapm: DAPM context
1903 * @route: audio routes
1904 * @num: number of routes
1905 *
1906 * Mark existing routes matching those specified in the passed array
1907 * as being weak, meaning that they are ignored for the purpose of
1908 * power decisions. The main intended use case is for sidetone paths
1909 * which couple audio between other independent paths if they are both
1910 * active in order to make the combination work better at the user
1911 * level but which aren't intended to be "used".
1912 *
1913 * Note that CODEC drivers should not use this as sidetone type paths
1914 * can frequently also be used as bypass paths.
1915 */
1916int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
1917 const struct snd_soc_dapm_route *route, int num)
1918{
1919 int i, err;
1920 int ret = 0;
1921
1922 for (i = 0; i < num; i++) {
1923 err = snd_soc_dapm_weak_route(dapm, route);
1924 if (err)
1925 ret = err;
1926 route++;
1927 }
1928
1929 return ret;
1930}
1931EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
1932
Mark Brown105f1c22008-05-13 14:52:19 +02001933/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001934 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001935 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001936 *
1937 * Checks the codec for any new dapm widgets and creates them if found.
1938 *
1939 * Returns 0 for success.
1940 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001941int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001942{
1943 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00001944 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001945
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001946 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001947 {
1948 if (w->new)
1949 continue;
1950
Stephen Warrenfad59882011-04-28 17:37:59 -06001951 if (w->num_kcontrols) {
1952 w->kcontrols = kzalloc(w->num_kcontrols *
1953 sizeof(struct snd_kcontrol *),
1954 GFP_KERNEL);
1955 if (!w->kcontrols)
1956 return -ENOMEM;
1957 }
1958
Richard Purdie2b97eab2006-10-06 18:32:18 +02001959 switch(w->id) {
1960 case snd_soc_dapm_switch:
1961 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001962 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001963 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001964 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001965 break;
1966 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001967 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001968 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001969 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001970 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001971 break;
1972 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001973 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001974 w->power_check = dapm_adc_check_power;
1975 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001976 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001977 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001978 w->power_check = dapm_dac_check_power;
1979 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001980 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001981 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01001982 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001983 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001984 break;
1985 case snd_soc_dapm_input:
1986 case snd_soc_dapm_output:
1987 case snd_soc_dapm_micbias:
1988 case snd_soc_dapm_spk:
1989 case snd_soc_dapm_hp:
1990 case snd_soc_dapm_mic:
1991 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001992 w->power_check = dapm_generic_check_power;
1993 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001994 case snd_soc_dapm_supply:
1995 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001996 case snd_soc_dapm_vmid:
1997 case snd_soc_dapm_pre:
1998 case snd_soc_dapm_post:
1999 break;
2000 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002001
2002 /* Read the initial power state from the device */
2003 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002004 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002005 val &= 1 << w->shift;
2006 if (w->invert)
2007 val = !val;
2008
2009 if (val)
2010 w->power = 1;
2011 }
2012
Richard Purdie2b97eab2006-10-06 18:32:18 +02002013 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002014
2015 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002016 }
2017
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002018 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002019 return 0;
2020}
2021EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2022
2023/**
2024 * snd_soc_dapm_get_volsw - dapm mixer get callback
2025 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002026 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002027 *
2028 * Callback to get the value of a dapm mixer control.
2029 *
2030 * Returns 0 for success.
2031 */
2032int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2033 struct snd_ctl_elem_value *ucontrol)
2034{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002035 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2036 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002037 struct soc_mixer_control *mc =
2038 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002039 unsigned int reg = mc->reg;
2040 unsigned int shift = mc->shift;
2041 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002042 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002043 unsigned int invert = mc->invert;
2044 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002045
Richard Purdie2b97eab2006-10-06 18:32:18 +02002046 ucontrol->value.integer.value[0] =
2047 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2048 if (shift != rshift)
2049 ucontrol->value.integer.value[1] =
2050 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2051 if (invert) {
2052 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002053 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002054 if (shift != rshift)
2055 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002056 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002057 }
2058
2059 return 0;
2060}
2061EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2062
2063/**
2064 * snd_soc_dapm_put_volsw - dapm mixer set callback
2065 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002066 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002067 *
2068 * Callback to set the value of a dapm mixer control.
2069 *
2070 * Returns 0 for success.
2071 */
2072int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2073 struct snd_ctl_elem_value *ucontrol)
2074{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002075 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2076 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2077 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002078 struct soc_mixer_control *mc =
2079 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002080 unsigned int reg = mc->reg;
2081 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002082 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002083 unsigned int mask = (1 << fls(max)) - 1;
2084 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002085 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002086 int connect, change;
2087 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002088 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002089
2090 val = (ucontrol->value.integer.value[0] & mask);
2091
2092 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002093 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002094 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002095 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002096
Stephen Warrenfafd2172011-04-28 17:38:00 -06002097 if (val)
2098 /* new connection */
2099 connect = invert ? 0 : 1;
2100 else
2101 /* old connection must be powered down */
2102 connect = invert ? 1 : 0;
2103
2104 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002105
Stephen Warrene9cf7042011-01-27 14:54:05 -07002106 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002107 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002108 for (wi = 0; wi < wlist->num_widgets; wi++) {
2109 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002110
Stephen Warrenfafd2172011-04-28 17:38:00 -06002111 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002112
Stephen Warrenfafd2172011-04-28 17:38:00 -06002113 update.kcontrol = kcontrol;
2114 update.widget = widget;
2115 update.reg = reg;
2116 update.mask = mask;
2117 update.val = val;
2118 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002119
Stephen Warrenfafd2172011-04-28 17:38:00 -06002120 dapm_mixer_update_power(widget, kcontrol, connect);
2121
2122 widget->dapm->update = NULL;
2123 }
Mark Brown283375c2009-12-07 18:09:03 +00002124 }
2125
Stephen Warrenfafd2172011-04-28 17:38:00 -06002126 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002127 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002128}
2129EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2130
2131/**
2132 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2133 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002134 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002135 *
2136 * Callback to get the value of a dapm enumerated double mixer control.
2137 *
2138 * Returns 0 for success.
2139 */
2140int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2141 struct snd_ctl_elem_value *ucontrol)
2142{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002143 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2144 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002145 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002146 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002147
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002148 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002149 ;
2150 val = snd_soc_read(widget->codec, e->reg);
2151 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2152 if (e->shift_l != e->shift_r)
2153 ucontrol->value.enumerated.item[1] =
2154 (val >> e->shift_r) & (bitmask - 1);
2155
2156 return 0;
2157}
2158EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2159
2160/**
2161 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2162 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002163 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002164 *
2165 * Callback to set the value of a dapm enumerated double mixer control.
2166 *
2167 * Returns 0 for success.
2168 */
2169int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2170 struct snd_ctl_elem_value *ucontrol)
2171{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002172 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2173 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2174 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002175 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002176 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002177 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002178 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002179 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002180
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002181 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002182 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002183 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002184 return -EINVAL;
2185 mux = ucontrol->value.enumerated.item[0];
2186 val = mux << e->shift_l;
2187 mask = (bitmask - 1) << e->shift_l;
2188 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002189 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002190 return -EINVAL;
2191 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2192 mask |= (bitmask - 1) << e->shift_r;
2193 }
2194
Stephen Warrenfafd2172011-04-28 17:38:00 -06002195 mutex_lock(&codec->mutex);
2196
Mark Brown3a655772009-10-05 17:23:30 +01002197 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002198 if (change) {
2199 for (wi = 0; wi < wlist->num_widgets; wi++) {
2200 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002201
Stephen Warrenfafd2172011-04-28 17:38:00 -06002202 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002203
Stephen Warrenfafd2172011-04-28 17:38:00 -06002204 update.kcontrol = kcontrol;
2205 update.widget = widget;
2206 update.reg = e->reg;
2207 update.mask = mask;
2208 update.val = val;
2209 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002210
Stephen Warrenfafd2172011-04-28 17:38:00 -06002211 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002212
Stephen Warrenfafd2172011-04-28 17:38:00 -06002213 widget->dapm->update = NULL;
2214 }
2215 }
2216
2217 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002218 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002219}
2220EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2221
2222/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002223 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2224 * @kcontrol: mixer control
2225 * @ucontrol: control element information
2226 *
2227 * Returns 0 for success.
2228 */
2229int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2230 struct snd_ctl_elem_value *ucontrol)
2231{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002232 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2233 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002234
2235 ucontrol->value.enumerated.item[0] = widget->value;
2236
2237 return 0;
2238}
2239EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2240
2241/**
2242 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2243 * @kcontrol: mixer control
2244 * @ucontrol: control element information
2245 *
2246 * Returns 0 for success.
2247 */
2248int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2249 struct snd_ctl_elem_value *ucontrol)
2250{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002251 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2252 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2253 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002254 struct soc_enum *e =
2255 (struct soc_enum *)kcontrol->private_value;
2256 int change;
2257 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002258 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002259
2260 if (ucontrol->value.enumerated.item[0] >= e->max)
2261 return -EINVAL;
2262
Stephen Warrenfafd2172011-04-28 17:38:00 -06002263 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002264
2265 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002266 if (change) {
2267 for (wi = 0; wi < wlist->num_widgets; wi++) {
2268 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002269
Stephen Warrenfafd2172011-04-28 17:38:00 -06002270 widget->value = ucontrol->value.enumerated.item[0];
2271
2272 dapm_mux_update_power(widget, kcontrol, change,
2273 widget->value, e);
2274 }
2275 }
2276
2277 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002278 return ret;
2279}
2280EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2281
2282/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002283 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2284 * callback
2285 * @kcontrol: mixer control
2286 * @ucontrol: control element information
2287 *
2288 * Callback to get the value of a dapm semi enumerated double mixer control.
2289 *
2290 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2291 * used for handling bitfield coded enumeration for example.
2292 *
2293 * Returns 0 for success.
2294 */
2295int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2296 struct snd_ctl_elem_value *ucontrol)
2297{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002298 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2299 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002300 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002301 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002302
2303 reg_val = snd_soc_read(widget->codec, e->reg);
2304 val = (reg_val >> e->shift_l) & e->mask;
2305 for (mux = 0; mux < e->max; mux++) {
2306 if (val == e->values[mux])
2307 break;
2308 }
2309 ucontrol->value.enumerated.item[0] = mux;
2310 if (e->shift_l != e->shift_r) {
2311 val = (reg_val >> e->shift_r) & e->mask;
2312 for (mux = 0; mux < e->max; mux++) {
2313 if (val == e->values[mux])
2314 break;
2315 }
2316 ucontrol->value.enumerated.item[1] = mux;
2317 }
2318
2319 return 0;
2320}
2321EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2322
2323/**
2324 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2325 * callback
2326 * @kcontrol: mixer control
2327 * @ucontrol: control element information
2328 *
2329 * Callback to set the value of a dapm semi enumerated double mixer control.
2330 *
2331 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2332 * used for handling bitfield coded enumeration for example.
2333 *
2334 * Returns 0 for success.
2335 */
2336int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2337 struct snd_ctl_elem_value *ucontrol)
2338{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002339 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2340 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2341 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002342 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002343 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002344 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002345 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002346 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002347
2348 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2349 return -EINVAL;
2350 mux = ucontrol->value.enumerated.item[0];
2351 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2352 mask = e->mask << e->shift_l;
2353 if (e->shift_l != e->shift_r) {
2354 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2355 return -EINVAL;
2356 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2357 mask |= e->mask << e->shift_r;
2358 }
2359
Stephen Warrenfafd2172011-04-28 17:38:00 -06002360 mutex_lock(&codec->mutex);
2361
Mark Brown3a655772009-10-05 17:23:30 +01002362 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002363 if (change) {
2364 for (wi = 0; wi < wlist->num_widgets; wi++) {
2365 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002366
Stephen Warrenfafd2172011-04-28 17:38:00 -06002367 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002368
Stephen Warrenfafd2172011-04-28 17:38:00 -06002369 update.kcontrol = kcontrol;
2370 update.widget = widget;
2371 update.reg = e->reg;
2372 update.mask = mask;
2373 update.val = val;
2374 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002375
Stephen Warrenfafd2172011-04-28 17:38:00 -06002376 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002377
Stephen Warrenfafd2172011-04-28 17:38:00 -06002378 widget->dapm->update = NULL;
2379 }
2380 }
2381
2382 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002383 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002384}
2385EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2386
2387/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002388 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2389 *
2390 * @kcontrol: mixer control
2391 * @uinfo: control element information
2392 *
2393 * Callback to provide information about a pin switch control.
2394 */
2395int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2396 struct snd_ctl_elem_info *uinfo)
2397{
2398 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2399 uinfo->count = 1;
2400 uinfo->value.integer.min = 0;
2401 uinfo->value.integer.max = 1;
2402
2403 return 0;
2404}
2405EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2406
2407/**
2408 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2409 *
2410 * @kcontrol: mixer control
2411 * @ucontrol: Value
2412 */
2413int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2414 struct snd_ctl_elem_value *ucontrol)
2415{
2416 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2417 const char *pin = (const char *)kcontrol->private_value;
2418
2419 mutex_lock(&codec->mutex);
2420
2421 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002422 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002423
2424 mutex_unlock(&codec->mutex);
2425
2426 return 0;
2427}
2428EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2429
2430/**
2431 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2432 *
2433 * @kcontrol: mixer control
2434 * @ucontrol: Value
2435 */
2436int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2437 struct snd_ctl_elem_value *ucontrol)
2438{
2439 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2440 const char *pin = (const char *)kcontrol->private_value;
2441
2442 mutex_lock(&codec->mutex);
2443
2444 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002445 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002446 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002447 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002448
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002449 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002450
2451 mutex_unlock(&codec->mutex);
2452
2453 return 0;
2454}
2455EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2456
2457/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002458 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002459 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002460 * @widget: widget template
2461 *
2462 * Creates a new dapm control based upon the template.
2463 *
2464 * Returns 0 for success else error.
2465 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002466int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002467 const struct snd_soc_dapm_widget *widget)
2468{
2469 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002470 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002471
2472 if ((w = dapm_cnew_widget(widget)) == NULL)
2473 return -ENOMEM;
2474
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002475 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002476 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002477 name_len += 1 + strlen(dapm->codec->name_prefix);
2478 w->name = kmalloc(name_len, GFP_KERNEL);
2479 if (w->name == NULL) {
2480 kfree(w);
2481 return -ENOMEM;
2482 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002483 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002484 snprintf(w->name, name_len, "%s %s",
2485 dapm->codec->name_prefix, widget->name);
2486 else
2487 snprintf(w->name, name_len, "%s", widget->name);
2488
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002489 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002490 w->dapm = dapm;
2491 w->codec = dapm->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002492 INIT_LIST_HEAD(&w->sources);
2493 INIT_LIST_HEAD(&w->sinks);
2494 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002495 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002496
2497 /* machine layer set ups unconnected pins and insertions */
2498 w->connected = 1;
2499 return 0;
2500}
2501EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2502
2503/**
Mark Brown4ba13272008-05-13 14:51:19 +02002504 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002505 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002506 * @widget: widget array
2507 * @num: number of widgets
2508 *
2509 * Creates new DAPM controls based upon the templates.
2510 *
2511 * Returns 0 for success else error.
2512 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002513int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002514 const struct snd_soc_dapm_widget *widget,
2515 int num)
2516{
2517 int i, ret;
2518
2519 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002520 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002521 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002522 dev_err(dapm->dev,
2523 "ASoC: Failed to create DAPM control %s: %d\n",
2524 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002525 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002526 }
Mark Brown4ba13272008-05-13 14:51:19 +02002527 widget++;
2528 }
2529 return 0;
2530}
2531EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2532
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002533static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002534 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002535{
2536 struct snd_soc_dapm_widget *w;
2537
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002538 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002539 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002540 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002541 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002542 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2543 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002544 if (strstr(w->sname, stream)) {
2545 switch(event) {
2546 case SND_SOC_DAPM_STREAM_START:
2547 w->active = 1;
2548 break;
2549 case SND_SOC_DAPM_STREAM_STOP:
2550 w->active = 0;
2551 break;
2552 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002553 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002554 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002555 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2556 break;
2557 }
2558 }
2559 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002560
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002561 dapm_power_widgets(dapm, event);
2562}
2563
2564/**
2565 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2566 * @rtd: PCM runtime data
2567 * @stream: stream name
2568 * @event: stream event
2569 *
2570 * Sends a stream event to the dapm core. The core then makes any
2571 * necessary widget power changes.
2572 *
2573 * Returns 0 for success else error.
2574 */
2575int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2576 const char *stream, int event)
2577{
2578 struct snd_soc_codec *codec = rtd->codec;
2579
2580 if (stream == NULL)
2581 return 0;
2582
2583 mutex_lock(&codec->mutex);
2584 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002585 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002586 return 0;
2587}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002588
2589/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002590 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002591 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002592 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002593 *
Mark Brown74b8f952009-06-06 11:26:15 +01002594 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002595 * a valid audio route and active audio stream.
2596 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2597 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002598 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002599int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002600{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002601 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002602}
Liam Girdwooda5302182008-07-07 13:35:17 +01002603EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002604
2605/**
Mark Brownda341832010-03-15 19:23:37 +00002606 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002607 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002608 * @pin: pin name
2609 *
2610 * Enables input/output pin regardless of any other state. This is
2611 * intended for use with microphone bias supplies used in microphone
2612 * jack detection.
2613 *
2614 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2615 * do any widget power switching.
2616 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002617int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2618 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002619{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002620 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002621
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002622 if (!w) {
2623 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2624 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002625 }
2626
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002627 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2628 w->connected = 1;
2629 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002630
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002631 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002632}
2633EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2634
2635/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002636 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002637 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002638 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002639 *
Mark Brown74b8f952009-06-06 11:26:15 +01002640 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002641 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2642 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002643 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002644int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2645 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002646{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002647 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002648}
2649EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2650
2651/**
Mark Brown5817b522008-09-24 11:23:11 +01002652 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002653 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002654 * @pin: pin name
2655 *
2656 * Marks the specified pin as being not connected, disabling it along
2657 * any parent or child widgets. At present this is identical to
2658 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2659 * additional things such as disabling controls which only affect
2660 * paths through the pin.
2661 *
2662 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2663 * do any widget power switching.
2664 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002665int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002666{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002667 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002668}
2669EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2670
2671/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002672 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002673 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002674 * @pin: audio signal pin endpoint (or start point)
2675 *
2676 * Get audio pin status - connected or disconnected.
2677 *
2678 * Returns 1 for connected otherwise 0.
2679 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002680int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2681 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002682{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002683 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002684
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002685 if (w)
2686 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002687
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002688 return 0;
2689}
Liam Girdwooda5302182008-07-07 13:35:17 +01002690EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002691
2692/**
Mark Brown1547aba2010-05-07 21:11:40 +01002693 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002694 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002695 * @pin: audio signal pin endpoint (or start point)
2696 *
2697 * Mark the given endpoint or pin as ignoring suspend. When the
2698 * system is disabled a path between two endpoints flagged as ignoring
2699 * suspend will not be disabled. The path must already be enabled via
2700 * normal means at suspend time, it will not be turned on if it was not
2701 * already enabled.
2702 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002703int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2704 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002705{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002706 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002707
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002708 if (!w) {
2709 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2710 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002711 }
2712
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002713 w->ignore_suspend = 1;
2714
2715 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002716}
2717EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2718
2719/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002720 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002721 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002722 *
2723 * Free all dapm widgets and resources.
2724 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002725void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002726{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002727 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002728 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002729 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002730 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002731}
2732EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2733
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002734static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002735{
Mark Brown51737472009-06-22 13:16:51 +01002736 struct snd_soc_dapm_widget *w;
2737 LIST_HEAD(down_list);
2738 int powerdown = 0;
2739
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002740 list_for_each_entry(w, &dapm->card->widgets, list) {
2741 if (w->dapm != dapm)
2742 continue;
Mark Brown51737472009-06-22 13:16:51 +01002743 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002744 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002745 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002746 powerdown = 1;
2747 }
2748 }
2749
2750 /* If there were no widgets to power down we're already in
2751 * standby.
2752 */
2753 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002754 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002755 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002756 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002757 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002758}
Mark Brown51737472009-06-22 13:16:51 +01002759
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002760/*
2761 * snd_soc_dapm_shutdown - callback for system shutdown
2762 */
2763void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2764{
2765 struct snd_soc_codec *codec;
2766
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002767 list_for_each_entry(codec, &card->codec_dev_list, list) {
2768 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002769 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002770 }
Mark Brown51737472009-06-22 13:16:51 +01002771}
2772
Richard Purdie2b97eab2006-10-06 18:32:18 +02002773/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002774MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002775MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2776MODULE_LICENSE("GPL");