blob: 214a709128d219785d4e2408c14c3851655ac311 [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
Mark Brownde02d072011-09-20 21:43:24 +010051#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
52
Richard Purdie2b97eab2006-10-06 18:32:18 +020053/* dapm power sequences - make this per codec in the future */
54static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010055 [snd_soc_dapm_pre] = 0,
56 [snd_soc_dapm_supply] = 1,
57 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010058 [snd_soc_dapm_aif_in] = 3,
59 [snd_soc_dapm_aif_out] = 3,
60 [snd_soc_dapm_mic] = 4,
61 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000062 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010063 [snd_soc_dapm_value_mux] = 5,
64 [snd_soc_dapm_dac] = 6,
65 [snd_soc_dapm_mixer] = 7,
66 [snd_soc_dapm_mixer_named_ctl] = 7,
67 [snd_soc_dapm_pga] = 8,
68 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060069 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010070 [snd_soc_dapm_hp] = 10,
71 [snd_soc_dapm_spk] = 10,
72 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020073};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000074
Richard Purdie2b97eab2006-10-06 18:32:18 +020075static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010076 [snd_soc_dapm_pre] = 0,
77 [snd_soc_dapm_adc] = 1,
78 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010079 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060080 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010081 [snd_soc_dapm_pga] = 4,
82 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010083 [snd_soc_dapm_mixer] = 5,
84 [snd_soc_dapm_dac] = 6,
85 [snd_soc_dapm_mic] = 7,
86 [snd_soc_dapm_micbias] = 8,
87 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000088 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010089 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010090 [snd_soc_dapm_aif_in] = 10,
91 [snd_soc_dapm_aif_out] = 10,
92 [snd_soc_dapm_supply] = 11,
93 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020094};
95
Troy Kisky12ef1932008-10-13 17:42:14 -070096static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010097{
98 if (pop_time)
99 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
100}
101
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200102static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100103{
104 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200105 char *buf;
106
107 if (!pop_time)
108 return;
109
110 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
111 if (buf == NULL)
112 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100113
114 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200115 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100116 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100117 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200118
119 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100120}
121
Mark Browndb432b42011-10-03 21:06:40 +0100122static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
123{
124 return !list_empty(&w->dirty);
125}
126
127static void dapm_mark_dirty(struct snd_soc_dapm_widget *w)
128{
129 if (!dapm_dirty_widget(w))
130 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
131}
132
Richard Purdie2b97eab2006-10-06 18:32:18 +0200133/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100134static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200135 const struct snd_soc_dapm_widget *_widget)
136{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100137 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200138}
139
Liam Girdwood48056082011-07-20 12:23:33 +0100140/* get snd_card from DAPM context */
141static inline struct snd_card *dapm_get_snd_card(
142 struct snd_soc_dapm_context *dapm)
143{
144 if (dapm->codec)
145 return dapm->codec->card->snd_card;
146 else if (dapm->platform)
147 return dapm->platform->card->snd_card;
148 else
149 BUG();
150
151 /* unreachable */
152 return NULL;
153}
154
155/* get soc_card from DAPM context */
156static inline struct snd_soc_card *dapm_get_soc_card(
157 struct snd_soc_dapm_context *dapm)
158{
159 if (dapm->codec)
160 return dapm->codec->card;
161 else if (dapm->platform)
162 return dapm->platform->card;
163 else
164 BUG();
165
166 /* unreachable */
167 return NULL;
168}
169
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100170static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
171{
172 if (w->codec)
173 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100174 else if (w->platform)
175 return snd_soc_platform_read(w->platform, reg);
176
177 dev_err(w->dapm->dev, "no valid widget read method\n");
178 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100179}
180
181static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
182{
183 if (w->codec)
184 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100185 else if (w->platform)
186 return snd_soc_platform_write(w->platform, reg, val);
187
188 dev_err(w->dapm->dev, "no valid widget write method\n");
189 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100190}
191
192static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
193 unsigned short reg, unsigned int mask, unsigned int value)
194{
195 int change;
196 unsigned int old, new;
197 int ret;
198
199 ret = soc_widget_read(w, reg);
200 if (ret < 0)
201 return ret;
202
203 old = ret;
204 new = (old & ~mask) | (value & mask);
205 change = old != new;
206 if (change) {
207 ret = soc_widget_write(w, reg, new);
208 if (ret < 0)
209 return ret;
210 }
211
212 return change;
213}
214
Mark Brown452c5ea2009-05-17 21:41:23 +0100215/**
216 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800217 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100218 * @level: level to configure
219 *
220 * Configure the bias (power) levels for the SoC audio device.
221 *
222 * Returns 0 for success else error.
223 */
Mark Browned5a4c42011-02-18 11:12:42 -0800224static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200225 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100226{
Mark Browned5a4c42011-02-18 11:12:42 -0800227 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100228 int ret = 0;
229
Mark Brown84e90932010-11-04 00:07:02 -0400230 trace_snd_soc_bias_level_start(card, level);
231
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000232 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100233 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100234 if (ret != 0)
235 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100236
Mark Browncc4c6702011-06-06 19:03:34 +0100237 if (dapm->codec) {
238 if (dapm->codec->driver->set_bias_level)
239 ret = dapm->codec->driver->set_bias_level(dapm->codec,
240 level);
241 else
242 dapm->bias_level = level;
243 }
Mark Brown171ec6b2011-06-06 18:15:19 +0100244 if (ret != 0)
245 goto out;
246
247 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100248 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100249out:
Mark Brown84e90932010-11-04 00:07:02 -0400250 trace_snd_soc_bias_level_done(card, level);
251
Mark Brown452c5ea2009-05-17 21:41:23 +0100252 return ret;
253}
254
Richard Purdie2b97eab2006-10-06 18:32:18 +0200255/* set up initial codec paths */
256static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
257 struct snd_soc_dapm_path *p, int i)
258{
259 switch (w->id) {
260 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000261 case snd_soc_dapm_mixer:
262 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200263 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100264 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600265 w->kcontrol_news[i].private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -0400266 unsigned int reg = mc->reg;
267 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100268 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -0400269 unsigned int mask = (1 << fls(max)) - 1;
270 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200271
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100272 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200273 val = (val >> shift) & mask;
274
275 if ((invert && !val) || (!invert && val))
276 p->connect = 1;
277 else
278 p->connect = 0;
279 }
280 break;
281 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600282 struct soc_enum *e = (struct soc_enum *)
283 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200284 int val, item, bitmask;
285
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +0100286 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100287 ;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100288 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200289 item = (val >> e->shift_l) & (bitmask - 1);
290
291 p->connect = 0;
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +0100292 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200293 if (!(strcmp(p->name, e->texts[i])) && item == i)
294 p->connect = 1;
295 }
296 }
297 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000298 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600299 struct soc_enum *e = (struct soc_enum *)
300 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000301
302 p->connect = 0;
303 /* since a virtual mux has no backing registers to
304 * decide which path to connect, it will try to match
305 * with the first enumeration. This is to ensure
306 * that the default mux choice (the first) will be
307 * correctly powered up during initialization.
308 */
309 if (!strcmp(p->name, e->texts[0]))
310 p->connect = 1;
311 }
312 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200313 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200314 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600315 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200316 int val, item;
317
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100318 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200319 val = (val >> e->shift_l) & e->mask;
320 for (item = 0; item < e->max; item++) {
321 if (val == e->values[item])
322 break;
323 }
324
325 p->connect = 0;
326 for (i = 0; i < e->max; i++) {
327 if (!(strcmp(p->name, e->texts[i])) && item == i)
328 p->connect = 1;
329 }
330 }
331 break;
Mark Brown56563102011-10-03 22:41:09 +0100332 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200333 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600334 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200335 case snd_soc_dapm_output:
336 case snd_soc_dapm_adc:
337 case snd_soc_dapm_input:
338 case snd_soc_dapm_dac:
339 case snd_soc_dapm_micbias:
340 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100341 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100342 case snd_soc_dapm_aif_in:
343 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200344 case snd_soc_dapm_hp:
345 case snd_soc_dapm_mic:
346 case snd_soc_dapm_spk:
347 case snd_soc_dapm_line:
Mark Brown56563102011-10-03 22:41:09 +0100348 p->connect = 1;
349 break;
350 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200351 case snd_soc_dapm_pre:
352 case snd_soc_dapm_post:
353 p->connect = 0;
354 break;
355 }
356}
357
Mark Brown74b8f952009-06-06 11:26:15 +0100358/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200359static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200360 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
361 struct snd_soc_dapm_path *path, const char *control_name,
362 const struct snd_kcontrol_new *kcontrol)
363{
364 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
365 int i;
366
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +0100367 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200368 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200369 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200370 list_add(&path->list_sink, &dest->sources);
371 list_add(&path->list_source, &src->sinks);
372 path->name = (char*)e->texts[i];
373 dapm_set_path_status(dest, path, 0);
374 return 0;
375 }
376 }
377
378 return -ENODEV;
379}
380
Mark Brown74b8f952009-06-06 11:26:15 +0100381/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200382static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200383 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
384 struct snd_soc_dapm_path *path, const char *control_name)
385{
386 int i;
387
388 /* search for mixer kcontrol */
389 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600390 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200391 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200392 list_add(&path->list_sink, &dest->sources);
393 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600394 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200395 dapm_set_path_status(dest, path, i);
396 return 0;
397 }
398 }
399 return -ENODEV;
400}
401
Stephen Warrenaf468002011-04-28 17:38:01 -0600402static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600403 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600404 const struct snd_kcontrol_new *kcontrol_new,
405 struct snd_kcontrol **kcontrol)
406{
407 struct snd_soc_dapm_widget *w;
408 int i;
409
410 *kcontrol = NULL;
411
412 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600413 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
414 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600415 for (i = 0; i < w->num_kcontrols; i++) {
416 if (&w->kcontrol_news[i] == kcontrol_new) {
417 if (w->kcontrols)
418 *kcontrol = w->kcontrols[i];
419 return 1;
420 }
421 }
422 }
423
424 return 0;
425}
426
Richard Purdie2b97eab2006-10-06 18:32:18 +0200427/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200428static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200429{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200430 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200431 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000432 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200433 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000434 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000435 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600436 struct snd_soc_dapm_widget_list *wlist;
437 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000438
439 if (dapm->codec)
440 prefix = dapm->codec->name_prefix;
441 else
442 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200443
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000444 if (prefix)
445 prefix_len = strlen(prefix) + 1;
446 else
447 prefix_len = 0;
448
Richard Purdie2b97eab2006-10-06 18:32:18 +0200449 /* add kcontrol */
450 for (i = 0; i < w->num_kcontrols; i++) {
451
452 /* match name */
453 list_for_each_entry(path, &w->sources, list_sink) {
454
455 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600456 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200457 continue;
458
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200459 if (w->kcontrols[i]) {
460 path->kcontrol = w->kcontrols[i];
461 continue;
462 }
463
Stephen Warrenfafd2172011-04-28 17:38:00 -0600464 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
465 sizeof(struct snd_soc_dapm_widget *),
466 wlist = kzalloc(wlistsize, GFP_KERNEL);
467 if (wlist == NULL) {
468 dev_err(dapm->dev,
469 "asoc: can't allocate widget list for %s\n",
470 w->name);
471 return -ENOMEM;
472 }
473 wlist->num_widgets = 1;
474 wlist->widgets[0] = w;
475
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000476 /* add dapm control with long name.
477 * for dapm_mixer this is the concatenation of the
478 * mixer and kcontrol name.
479 * for dapm_mixer_named_ctl this is simply the
480 * kcontrol name.
481 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600482 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000483 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000484 name_len += 1 + strlen(w->name);
485
Mark Brown219b93f2008-10-28 13:02:31 +0000486 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000487
Stephen Warrenfafd2172011-04-28 17:38:00 -0600488 if (path->long_name == NULL) {
489 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200490 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600491 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200492
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000493 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000494 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000495 /* The control will get a prefix from
496 * the control creation process but
497 * we're also using the same prefix
498 * for widgets so cut the prefix off
499 * the front of the widget name.
500 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000501 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000502 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600503 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000504 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000505 case snd_soc_dapm_mixer_named_ctl:
506 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600507 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000508 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000509 }
510
Mark Brown219b93f2008-10-28 13:02:31 +0000511 path->long_name[name_len - 1] = '\0';
512
Stephen Warrenfafd2172011-04-28 17:38:00 -0600513 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
514 wlist, path->long_name,
515 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200516 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200517 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200518 dev_err(dapm->dev,
519 "asoc: failed to add dapm kcontrol %s: %d\n",
520 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600521 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200522 kfree(path->long_name);
523 path->long_name = NULL;
524 return ret;
525 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600526 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200527 }
528 }
529 return ret;
530}
531
532/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200533static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200534{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200535 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200536 struct snd_soc_dapm_path *path = NULL;
537 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000538 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000539 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000540 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600541 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600542 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600543 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600544 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600545 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200546
Stephen Warrenaf468002011-04-28 17:38:01 -0600547 if (w->num_kcontrols != 1) {
548 dev_err(dapm->dev,
549 "asoc: mux %s has incorrect number of controls\n",
550 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200551 return -EINVAL;
552 }
553
Stephen Warren1007da02011-05-26 09:57:33 -0600554 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600555 &kcontrol);
556 if (kcontrol) {
557 wlist = kcontrol->private_data;
558 wlistentries = wlist->num_widgets + 1;
559 } else {
560 wlist = NULL;
561 wlistentries = 1;
562 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600563 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600564 wlistentries * sizeof(struct snd_soc_dapm_widget *),
565 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600566 if (wlist == NULL) {
567 dev_err(dapm->dev,
568 "asoc: can't allocate widget list for %s\n", w->name);
569 return -ENOMEM;
570 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600571 wlist->num_widgets = wlistentries;
572 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600573
Stephen Warrenaf468002011-04-28 17:38:01 -0600574 if (!kcontrol) {
575 if (dapm->codec)
576 prefix = dapm->codec->name_prefix;
577 else
578 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000579
Stephen Warrenaf468002011-04-28 17:38:01 -0600580 if (shared) {
581 name = w->kcontrol_news[0].name;
582 prefix_len = 0;
583 } else {
584 name = w->name;
585 if (prefix)
586 prefix_len = strlen(prefix) + 1;
587 else
588 prefix_len = 0;
589 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000590
Stephen Warrenaf468002011-04-28 17:38:01 -0600591 /*
592 * The control will get a prefix from the control creation
593 * process but we're also using the same prefix for widgets so
594 * cut the prefix off the front of the widget name.
595 */
596 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
597 name + prefix_len, prefix);
598 ret = snd_ctl_add(card, kcontrol);
599 if (ret < 0) {
Mark Brown53daf202011-09-05 10:51:05 -0700600 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
601 w->name, ret);
Stephen Warrenaf468002011-04-28 17:38:01 -0600602 kfree(wlist);
603 return ret;
604 }
605 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200606
Stephen Warrenaf468002011-04-28 17:38:01 -0600607 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200608
Stephen Warrenfad59882011-04-28 17:37:59 -0600609 w->kcontrols[0] = kcontrol;
610
Richard Purdie2b97eab2006-10-06 18:32:18 +0200611 list_for_each_entry(path, &w->sources, list_sink)
612 path->kcontrol = kcontrol;
613
Stephen Warrenaf468002011-04-28 17:38:01 -0600614 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200615}
616
617/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200618static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200619{
Mark Browna6c65732010-03-03 17:45:21 +0000620 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200621 dev_err(w->dapm->dev,
622 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200623
Mark Browna6c65732010-03-03 17:45:21 +0000624 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200625}
626
627/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200628static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200629{
630 struct snd_soc_dapm_path *p;
631
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200632 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200633 p->walked = 0;
634}
635
Mark Brown99497882010-05-07 20:24:05 +0100636/* We implement power down on suspend by checking the power state of
637 * the ALSA card - when we are suspending the ALSA state for the card
638 * is set to D3.
639 */
640static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
641{
Mark Brown12ea2c72011-03-02 18:17:32 +0000642 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100643
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100645 case SNDRV_CTL_POWER_D3hot:
646 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100647 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200648 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
649 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100650 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100651 default:
652 return 1;
653 }
654}
655
Richard Purdie2b97eab2006-10-06 18:32:18 +0200656/*
657 * Recursively check for a completed path to an active or physically connected
658 * output widget. Returns number of complete paths.
659 */
660static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
661{
662 struct snd_soc_dapm_path *path;
663 int con = 0;
664
Mark Brownde02d072011-09-20 21:43:24 +0100665 DAPM_UPDATE_STAT(widget, path_checks);
666
Mark Brown246d0a12009-04-22 18:24:55 +0100667 if (widget->id == snd_soc_dapm_supply)
668 return 0;
669
Mark Brown010ff262009-08-17 17:39:22 +0100670 switch (widget->id) {
671 case snd_soc_dapm_adc:
672 case snd_soc_dapm_aif_out:
673 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100674 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100675 default:
676 break;
677 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200678
679 if (widget->connected) {
680 /* connected pin ? */
681 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100682 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200683
684 /* connected jack or spk ? */
685 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300686 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100687 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200688 }
689
690 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100691 DAPM_UPDATE_STAT(widget, neighbour_checks);
692
Mark Brownbf3a9e12011-06-13 16:42:29 +0100693 if (path->weak)
694 continue;
695
Richard Purdie2b97eab2006-10-06 18:32:18 +0200696 if (path->walked)
697 continue;
698
699 if (path->sink && path->connect) {
700 path->walked = 1;
701 con += is_connected_output_ep(path->sink);
702 }
703 }
704
705 return con;
706}
707
708/*
709 * Recursively check for a completed path to an active or physically connected
710 * input widget. Returns number of complete paths.
711 */
712static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
713{
714 struct snd_soc_dapm_path *path;
715 int con = 0;
716
Mark Brownde02d072011-09-20 21:43:24 +0100717 DAPM_UPDATE_STAT(widget, path_checks);
718
Mark Brown246d0a12009-04-22 18:24:55 +0100719 if (widget->id == snd_soc_dapm_supply)
720 return 0;
721
Richard Purdie2b97eab2006-10-06 18:32:18 +0200722 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100723 switch (widget->id) {
724 case snd_soc_dapm_dac:
725 case snd_soc_dapm_aif_in:
726 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100727 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100728 default:
729 break;
730 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200731
732 if (widget->connected) {
733 /* connected pin ? */
734 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100735 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200736
737 /* connected VMID/Bias for lower pops */
738 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100739 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200740
741 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300742 if (widget->id == snd_soc_dapm_mic ||
743 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100744 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200745 }
746
747 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100748 DAPM_UPDATE_STAT(widget, neighbour_checks);
749
Mark Brownbf3a9e12011-06-13 16:42:29 +0100750 if (path->weak)
751 continue;
752
Richard Purdie2b97eab2006-10-06 18:32:18 +0200753 if (path->walked)
754 continue;
755
756 if (path->source && path->connect) {
757 path->walked = 1;
758 con += is_connected_input_ep(path->source);
759 }
760 }
761
762 return con;
763}
764
765/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300766 * Handler for generic register modifier widget.
767 */
768int dapm_reg_event(struct snd_soc_dapm_widget *w,
769 struct snd_kcontrol *kcontrol, int event)
770{
771 unsigned int val;
772
773 if (SND_SOC_DAPM_EVENT_ON(event))
774 val = w->on_val;
775 else
776 val = w->off_val;
777
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100778 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300779 w->mask << w->shift, val << w->shift);
780
781 return 0;
782}
Mark Brown11589412008-07-29 11:42:23 +0100783EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300784
Mark Brownd8050022011-09-28 18:28:23 +0100785static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
786{
787 if (w->force)
788 return 1;
789 else
790 return w->power_check(w);
791}
792
Mark Browncd0f2d42009-04-20 16:56:59 +0100793/* Generic check to see if a widget should be powered.
794 */
795static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
796{
797 int in, out;
798
Mark Brownde02d072011-09-20 21:43:24 +0100799 DAPM_UPDATE_STAT(w, power_checks);
800
Mark Browncd0f2d42009-04-20 16:56:59 +0100801 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200802 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100803 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200804 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100805 return out != 0 && in != 0;
806}
807
Mark Brown6ea31b92009-04-20 17:15:41 +0100808/* Check to see if an ADC has power */
809static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
810{
811 int in;
812
Mark Brownde02d072011-09-20 21:43:24 +0100813 DAPM_UPDATE_STAT(w, power_checks);
814
Mark Brown6ea31b92009-04-20 17:15:41 +0100815 if (w->active) {
816 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200817 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100818 return in != 0;
819 } else {
820 return dapm_generic_check_power(w);
821 }
822}
823
824/* Check to see if a DAC has power */
825static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
826{
827 int out;
828
Mark Brownde02d072011-09-20 21:43:24 +0100829 DAPM_UPDATE_STAT(w, power_checks);
830
Mark Brown6ea31b92009-04-20 17:15:41 +0100831 if (w->active) {
832 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200833 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100834 return out != 0;
835 } else {
836 return dapm_generic_check_power(w);
837 }
838}
839
Mark Brown246d0a12009-04-22 18:24:55 +0100840/* Check to see if a power supply is needed */
841static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
842{
843 struct snd_soc_dapm_path *path;
844 int power = 0;
845
Mark Brownde02d072011-09-20 21:43:24 +0100846 DAPM_UPDATE_STAT(w, power_checks);
847
Mark Brown246d0a12009-04-22 18:24:55 +0100848 /* Check if one of our outputs is connected */
849 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +0100850 DAPM_UPDATE_STAT(w, neighbour_checks);
851
Mark Brownbf3a9e12011-06-13 16:42:29 +0100852 if (path->weak)
853 continue;
854
Mark Brown215edda2009-09-08 18:59:05 +0100855 if (path->connected &&
856 !path->connected(path->source, path->sink))
857 continue;
858
Mark Brown30173582011-02-11 11:42:19 +0000859 if (!path->sink)
860 continue;
861
Mark Brownd8050022011-09-28 18:28:23 +0100862 if (dapm_widget_power_check(path->sink)) {
Mark Brown246d0a12009-04-22 18:24:55 +0100863 power = 1;
864 break;
865 }
866 }
867
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200868 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100869
870 return power;
871}
872
Mark Brown35c64bca2011-09-28 18:23:53 +0100873static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
874{
875 return 1;
876}
877
Mark Brown38357ab2009-06-06 19:03:23 +0100878static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
879 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000880 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000881{
Mark Brown828a8422011-01-15 13:14:30 +0000882 int *sort;
883
884 if (power_up)
885 sort = dapm_up_seq;
886 else
887 sort = dapm_down_seq;
888
Mark Brown38357ab2009-06-06 19:03:23 +0100889 if (sort[a->id] != sort[b->id])
890 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000891 if (a->subseq != b->subseq) {
892 if (power_up)
893 return a->subseq - b->subseq;
894 else
895 return b->subseq - a->subseq;
896 }
Mark Brownb22ead22009-06-07 12:51:26 +0100897 if (a->reg != b->reg)
898 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000899 if (a->dapm != b->dapm)
900 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000901
Mark Brown38357ab2009-06-06 19:03:23 +0100902 return 0;
903}
Mark Brown42aa3412009-03-01 19:21:10 +0000904
Mark Brown38357ab2009-06-06 19:03:23 +0100905/* Insert a widget in order into a DAPM power sequence. */
906static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
907 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000908 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100909{
910 struct snd_soc_dapm_widget *w;
911
912 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000913 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100914 list_add_tail(&new_widget->power_list, &w->power_list);
915 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000916 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100917
Mark Brown38357ab2009-06-06 19:03:23 +0100918 list_add_tail(&new_widget->power_list, list);
919}
Mark Brown42aa3412009-03-01 19:21:10 +0000920
Mark Brown68f89ad2010-11-03 23:51:49 -0400921static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
922 struct snd_soc_dapm_widget *w, int event)
923{
924 struct snd_soc_card *card = dapm->card;
925 const char *ev_name;
926 int power, ret;
927
928 switch (event) {
929 case SND_SOC_DAPM_PRE_PMU:
930 ev_name = "PRE_PMU";
931 power = 1;
932 break;
933 case SND_SOC_DAPM_POST_PMU:
934 ev_name = "POST_PMU";
935 power = 1;
936 break;
937 case SND_SOC_DAPM_PRE_PMD:
938 ev_name = "PRE_PMD";
939 power = 0;
940 break;
941 case SND_SOC_DAPM_POST_PMD:
942 ev_name = "POST_PMD";
943 power = 0;
944 break;
945 default:
946 BUG();
947 return;
948 }
949
950 if (w->power != power)
951 return;
952
953 if (w->event && (w->event_flags & event)) {
954 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
955 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400956 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400957 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400958 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400959 if (ret < 0)
960 pr_err("%s: %s event failed: %d\n",
961 ev_name, w->name, ret);
962 }
963}
964
Mark Brownb22ead22009-06-07 12:51:26 +0100965/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200966static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100967 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100968{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200969 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400970 struct snd_soc_dapm_widget *w;
971 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100972 unsigned int value = 0;
973 unsigned int mask = 0;
974 unsigned int cur_mask;
975
976 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
977 power_list)->reg;
978
979 list_for_each_entry(w, pending, power_list) {
980 cur_mask = 1 << w->shift;
981 BUG_ON(reg != w->reg);
982
983 if (w->invert)
984 power = !w->power;
985 else
986 power = w->power;
987
988 mask |= cur_mask;
989 if (power)
990 value |= cur_mask;
991
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200992 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100993 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
994 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100995
Mark Brown68f89ad2010-11-03 23:51:49 -0400996 /* Check for events */
997 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
998 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100999 }
1000
Mark Brown81628102009-06-07 13:21:24 +01001001 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001002 /* Any widget will do, they should all be updating the
1003 * same register.
1004 */
1005 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1006 power_list);
1007
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001008 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001009 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001010 value, mask, reg, card->pop_time);
1011 pop_wait(card->pop_time);
Liam Girdwood0445bdf2011-06-13 19:37:36 +01001012 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001013 }
1014
1015 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001016 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1017 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001018 }
Mark Brown42aa3412009-03-01 19:21:10 +00001019}
1020
Mark Brownb22ead22009-06-07 12:51:26 +01001021/* Apply a DAPM power sequence.
1022 *
1023 * We walk over a pre-sorted list of widgets to apply power to. In
1024 * order to minimise the number of writes to the device required
1025 * multiple widgets will be updated in a single write where possible.
1026 * Currently anything that requires more than a single write is not
1027 * handled.
1028 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001029static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001030 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001031{
1032 struct snd_soc_dapm_widget *w, *n;
1033 LIST_HEAD(pending);
1034 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001035 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001036 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001037 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001038 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001039 int *sort;
1040
1041 if (power_up)
1042 sort = dapm_up_seq;
1043 else
1044 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001045
Mark Brownb22ead22009-06-07 12:51:26 +01001046 list_for_each_entry_safe(w, n, list, power_list) {
1047 ret = 0;
1048
1049 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001050 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001051 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001052 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001053 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001054
Mark Brown474b62d2011-01-18 16:14:44 +00001055 if (cur_dapm && cur_dapm->seq_notifier) {
1056 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1057 if (sort[i] == cur_sort)
1058 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001059 i,
1060 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001061 }
1062
Mark Brownb22ead22009-06-07 12:51:26 +01001063 INIT_LIST_HEAD(&pending);
1064 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001065 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001066 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001067 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001068 }
1069
Mark Brown163cac02009-06-07 10:12:52 +01001070 switch (w->id) {
1071 case snd_soc_dapm_pre:
1072 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001073 list_for_each_entry_safe_continue(w, n, list,
1074 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001075
Mark Brownb22ead22009-06-07 12:51:26 +01001076 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001077 ret = w->event(w,
1078 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001079 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001080 ret = w->event(w,
1081 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001082 break;
1083
1084 case snd_soc_dapm_post:
1085 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001086 list_for_each_entry_safe_continue(w, n, list,
1087 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001088
Mark Brownb22ead22009-06-07 12:51:26 +01001089 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001090 ret = w->event(w,
1091 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001092 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001093 ret = w->event(w,
1094 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001095 break;
1096
Mark Brown163cac02009-06-07 10:12:52 +01001097 default:
Mark Brown81628102009-06-07 13:21:24 +01001098 /* Queue it up for application */
1099 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001100 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001101 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001102 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001103 list_move(&w->power_list, &pending);
1104 break;
Mark Brown163cac02009-06-07 10:12:52 +01001105 }
Mark Brownb22ead22009-06-07 12:51:26 +01001106
1107 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001108 dev_err(w->dapm->dev,
1109 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001110 }
Mark Brownb22ead22009-06-07 12:51:26 +01001111
1112 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001113 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001114
1115 if (cur_dapm && cur_dapm->seq_notifier) {
1116 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1117 if (sort[i] == cur_sort)
1118 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001119 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001120 }
Mark Brown163cac02009-06-07 10:12:52 +01001121}
1122
Mark Brown97404f22010-12-14 16:13:57 +00001123static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1124{
1125 struct snd_soc_dapm_update *update = dapm->update;
1126 struct snd_soc_dapm_widget *w;
1127 int ret;
1128
1129 if (!update)
1130 return;
1131
1132 w = update->widget;
1133
1134 if (w->event &&
1135 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1136 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1137 if (ret != 0)
1138 pr_err("%s DAPM pre-event failed: %d\n",
1139 w->name, ret);
1140 }
1141
1142 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1143 update->val);
1144 if (ret < 0)
1145 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1146
1147 if (w->event &&
1148 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1149 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1150 if (ret != 0)
1151 pr_err("%s DAPM post-event failed: %d\n",
1152 w->name, ret);
1153 }
1154}
1155
Mark Brown9d0624a2011-02-18 11:49:43 -08001156/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1157 * they're changing state.
1158 */
1159static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1160{
1161 struct snd_soc_dapm_context *d = data;
1162 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001163
Mark Brown56fba412011-06-04 11:25:10 +01001164 /* If we're off and we're not supposed to be go into STANDBY */
1165 if (d->bias_level == SND_SOC_BIAS_OFF &&
1166 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001167 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1168 if (ret != 0)
1169 dev_err(d->dev,
1170 "Failed to turn on bias: %d\n", ret);
1171 }
1172
Mark Brown56fba412011-06-04 11:25:10 +01001173 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1174 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001175 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1176 if (ret != 0)
1177 dev_err(d->dev,
1178 "Failed to prepare bias: %d\n", ret);
1179 }
1180}
1181
1182/* Async callback run prior to DAPM sequences - brings to their final
1183 * state.
1184 */
1185static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1186{
1187 struct snd_soc_dapm_context *d = data;
1188 int ret;
1189
1190 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001191 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1192 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1193 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001194 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1195 if (ret != 0)
1196 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1197 ret);
1198 }
1199
1200 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001201 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1202 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001203 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1204 if (ret != 0)
1205 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1206 }
1207
1208 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001209 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1210 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001211 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1212 if (ret != 0)
1213 dev_err(d->dev, "Failed to apply active bias: %d\n",
1214 ret);
1215 }
1216}
Mark Brown97404f22010-12-14 16:13:57 +00001217
Mark Brownfe4fda52011-10-03 22:36:57 +01001218static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1219 bool power, bool connect)
1220{
1221 /* If a connection is being made or broken then that update
1222 * will have marked the peer dirty, otherwise the widgets are
1223 * not connected and this update has no impact. */
1224 if (!connect)
1225 return;
1226
1227 /* If the peer is already in the state we're moving to then we
1228 * won't have an impact on it. */
1229 if (power != peer->power)
1230 dapm_mark_dirty(peer);
1231}
1232
Mark Brown05623c42011-09-28 17:02:31 +01001233static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1234 struct list_head *up_list,
1235 struct list_head *down_list)
1236{
Mark Browndb432b42011-10-03 21:06:40 +01001237 struct snd_soc_dapm_path *path;
1238
Mark Brown05623c42011-09-28 17:02:31 +01001239 if (w->power == power)
1240 return;
1241
1242 trace_snd_soc_dapm_widget_power(w, power);
1243
Mark Browndb432b42011-10-03 21:06:40 +01001244 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001245 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001246 */
1247 list_for_each_entry(path, &w->sources, list_sink) {
1248 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001249 dapm_widget_set_peer_power(path->source, power,
1250 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001251 }
1252 }
1253 list_for_each_entry(path, &w->sinks, list_source) {
1254 if (path->sink) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001255 dapm_widget_set_peer_power(path->sink, power,
1256 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001257 }
1258 }
1259
Mark Brown05623c42011-09-28 17:02:31 +01001260 if (power)
1261 dapm_seq_insert(w, up_list, true);
1262 else
1263 dapm_seq_insert(w, down_list, false);
1264
1265 w->power = power;
1266}
1267
Mark Brown7c81beb2011-09-20 22:22:32 +01001268static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1269 struct list_head *up_list,
1270 struct list_head *down_list)
1271{
Mark Brown7c81beb2011-09-20 22:22:32 +01001272 int power;
1273
1274 switch (w->id) {
1275 case snd_soc_dapm_pre:
1276 dapm_seq_insert(w, down_list, false);
1277 break;
1278 case snd_soc_dapm_post:
1279 dapm_seq_insert(w, up_list, true);
1280 break;
1281
1282 default:
Mark Brownd8050022011-09-28 18:28:23 +01001283 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001284
Mark Brown05623c42011-09-28 17:02:31 +01001285 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001286 break;
1287 }
1288}
1289
Mark Brown42aa3412009-03-01 19:21:10 +00001290/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001291 * Scan each dapm widget for complete audio path.
1292 * A complete path is a route that has valid endpoints i.e.:-
1293 *
1294 * o DAC to output pin.
1295 * o Input Pin to ADC.
1296 * o Input pin to Output pin (bypass, sidetone)
1297 * o DAC to ADC (loopback).
1298 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001299static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001300{
Mark Brown12ea2c72011-03-02 18:17:32 +00001301 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001302 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001303 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001304 LIST_HEAD(up_list);
1305 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001306 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001307 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001308
Mark Brown84e90932010-11-04 00:07:02 -04001309 trace_snd_soc_dapm_start(card);
1310
Mark Brown56fba412011-06-04 11:25:10 +01001311 list_for_each_entry(d, &card->dapm_list, list) {
1312 if (d->n_widgets || d->codec == NULL) {
1313 if (d->idle_bias_off)
1314 d->target_bias_level = SND_SOC_BIAS_OFF;
1315 else
1316 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1317 }
1318 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001319
Mark Brownde02d072011-09-20 21:43:24 +01001320 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1321
Mark Brown6d3ddc82009-05-16 17:47:29 +01001322 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001323 * lists indicating if they should be powered up or down. We
1324 * only check widgets that have been flagged as dirty but note
1325 * that new widgets may be added to the dirty list while we
1326 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001327 */
Mark Browndb432b42011-10-03 21:06:40 +01001328 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001329 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001330 }
1331
Mark Brownf9de6d72011-09-28 17:19:47 +01001332 list_for_each_entry(w, &card->widgets, list) {
Mark Browndb432b42011-10-03 21:06:40 +01001333 list_del_init(&w->dirty);
1334
Mark Brownf9de6d72011-09-28 17:19:47 +01001335 if (w->power) {
1336 d = w->dapm;
1337
1338 /* Supplies and micbiases only bring the
1339 * context up to STANDBY as unless something
1340 * else is active and passing audio they
1341 * generally don't require full power.
1342 */
1343 switch (w->id) {
1344 case snd_soc_dapm_supply:
1345 case snd_soc_dapm_micbias:
1346 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1347 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1348 break;
1349 default:
1350 d->target_bias_level = SND_SOC_BIAS_ON;
1351 break;
1352 }
1353 }
1354
1355 }
1356
Mark Brownb14b76a2009-08-17 11:55:38 +01001357 /* If there are no DAPM widgets then try to figure out power from the
1358 * event type.
1359 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001360 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001361 switch (event) {
1362 case SND_SOC_DAPM_STREAM_START:
1363 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001364 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a2009-08-17 11:55:38 +01001365 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001366 case SND_SOC_DAPM_STREAM_STOP:
Mark Brown56fba412011-06-04 11:25:10 +01001367 if (dapm->codec->active)
1368 dapm->target_bias_level = SND_SOC_BIAS_ON;
1369 else
1370 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001371 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001372 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001373 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001374 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001375 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001376 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001377 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001378 default:
1379 break;
1380 }
1381 }
1382
Mark Brown85a843c2011-09-21 21:29:47 +01001383 /* Force all contexts in the card to the same bias state if
1384 * they're not ground referenced.
1385 */
Mark Brown56fba412011-06-04 11:25:10 +01001386 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001387 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001388 if (d->target_bias_level > bias)
1389 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001390 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001391 if (!d->idle_bias_off)
1392 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001393
Mark Brownde02d072011-09-20 21:43:24 +01001394 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001395
Mark Brown9d0624a2011-02-18 11:49:43 -08001396 /* Run all the bias changes in parallel */
1397 list_for_each_entry(d, &dapm->card->dapm_list, list)
1398 async_schedule_domain(dapm_pre_sequence_async, d,
1399 &async_domain);
1400 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001401
Mark Brown6d3ddc82009-05-16 17:47:29 +01001402 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001403 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001404
Mark Brown97404f22010-12-14 16:13:57 +00001405 dapm_widget_update(dapm);
1406
Mark Brown6d3ddc82009-05-16 17:47:29 +01001407 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001408 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001409
Mark Brown9d0624a2011-02-18 11:49:43 -08001410 /* Run all the bias changes in parallel */
1411 list_for_each_entry(d, &dapm->card->dapm_list, list)
1412 async_schedule_domain(dapm_post_sequence_async, d,
1413 &async_domain);
1414 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001415
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001416 pop_dbg(dapm->dev, card->pop_time,
1417 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001418 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001419
Mark Brown84e90932010-11-04 00:07:02 -04001420 trace_snd_soc_dapm_done(card);
1421
Mark Brown42aa3412009-03-01 19:21:10 +00001422 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001423}
1424
Mark Brown79fb9382009-08-21 16:38:13 +01001425#ifdef CONFIG_DEBUG_FS
1426static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1427{
1428 file->private_data = inode->i_private;
1429 return 0;
1430}
1431
1432static ssize_t dapm_widget_power_read_file(struct file *file,
1433 char __user *user_buf,
1434 size_t count, loff_t *ppos)
1435{
1436 struct snd_soc_dapm_widget *w = file->private_data;
1437 char *buf;
1438 int in, out;
1439 ssize_t ret;
1440 struct snd_soc_dapm_path *p = NULL;
1441
1442 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1443 if (!buf)
1444 return -ENOMEM;
1445
1446 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001447 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001448 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001449 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001450
Mark Brownd033c362009-12-04 15:25:56 +00001451 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001452 w->name, w->power ? "On" : "Off", in, out);
1453
Mark Brownd033c362009-12-04 15:25:56 +00001454 if (w->reg >= 0)
1455 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1456 " - R%d(0x%x) bit %d",
1457 w->reg, w->reg, w->shift);
1458
1459 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1460
Mark Brown3eef08b2009-09-14 16:49:00 +01001461 if (w->sname)
1462 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1463 w->sname,
1464 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001465
1466 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001467 if (p->connected && !p->connected(w, p->sink))
1468 continue;
1469
Mark Brown79fb9382009-08-21 16:38:13 +01001470 if (p->connect)
1471 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001472 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001473 p->name ? p->name : "static",
1474 p->source->name);
1475 }
1476 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001477 if (p->connected && !p->connected(w, p->sink))
1478 continue;
1479
Mark Brown79fb9382009-08-21 16:38:13 +01001480 if (p->connect)
1481 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001482 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001483 p->name ? p->name : "static",
1484 p->sink->name);
1485 }
1486
1487 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1488
1489 kfree(buf);
1490 return ret;
1491}
1492
1493static const struct file_operations dapm_widget_power_fops = {
1494 .open = dapm_widget_power_open_file,
1495 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001496 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001497};
1498
Mark Brownef49e4f2011-04-04 20:48:13 +09001499static int dapm_bias_open_file(struct inode *inode, struct file *file)
1500{
1501 file->private_data = inode->i_private;
1502 return 0;
1503}
1504
1505static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1506 size_t count, loff_t *ppos)
1507{
1508 struct snd_soc_dapm_context *dapm = file->private_data;
1509 char *level;
1510
1511 switch (dapm->bias_level) {
1512 case SND_SOC_BIAS_ON:
1513 level = "On\n";
1514 break;
1515 case SND_SOC_BIAS_PREPARE:
1516 level = "Prepare\n";
1517 break;
1518 case SND_SOC_BIAS_STANDBY:
1519 level = "Standby\n";
1520 break;
1521 case SND_SOC_BIAS_OFF:
1522 level = "Off\n";
1523 break;
1524 default:
1525 BUG();
1526 level = "Unknown\n";
1527 break;
1528 }
1529
1530 return simple_read_from_buffer(user_buf, count, ppos, level,
1531 strlen(level));
1532}
1533
1534static const struct file_operations dapm_bias_fops = {
1535 .open = dapm_bias_open_file,
1536 .read = dapm_bias_read_file,
1537 .llseek = default_llseek,
1538};
1539
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001540void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1541 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001542{
Mark Brown79fb9382009-08-21 16:38:13 +01001543 struct dentry *d;
1544
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001545 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1546
1547 if (!dapm->debugfs_dapm) {
1548 printk(KERN_WARNING
1549 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001550 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001551 }
Mark Brown79fb9382009-08-21 16:38:13 +01001552
Mark Brownef49e4f2011-04-04 20:48:13 +09001553 d = debugfs_create_file("bias_level", 0444,
1554 dapm->debugfs_dapm, dapm,
1555 &dapm_bias_fops);
1556 if (!d)
1557 dev_warn(dapm->dev,
1558 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001559}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001560
1561static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1562{
1563 struct snd_soc_dapm_context *dapm = w->dapm;
1564 struct dentry *d;
1565
1566 if (!dapm->debugfs_dapm || !w->name)
1567 return;
1568
1569 d = debugfs_create_file(w->name, 0444,
1570 dapm->debugfs_dapm, w,
1571 &dapm_widget_power_fops);
1572 if (!d)
1573 dev_warn(w->dapm->dev,
1574 "ASoC: Failed to create %s debugfs file\n",
1575 w->name);
1576}
1577
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001578static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1579{
1580 debugfs_remove_recursive(dapm->debugfs_dapm);
1581}
1582
Mark Brown79fb9382009-08-21 16:38:13 +01001583#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001584void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1585 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001586{
1587}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001588
1589static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1590{
1591}
1592
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001593static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1594{
1595}
1596
Mark Brown79fb9382009-08-21 16:38:13 +01001597#endif
1598
Richard Purdie2b97eab2006-10-06 18:32:18 +02001599/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001600static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001601 struct snd_kcontrol *kcontrol, int change,
1602 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001603{
1604 struct snd_soc_dapm_path *path;
1605 int found = 0;
1606
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001607 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001608 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001609 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001610 return -ENODEV;
1611
Mark Brown3a655772009-10-05 17:23:30 +01001612 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001613 return 0;
1614
1615 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001616 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001617 if (path->kcontrol != kcontrol)
1618 continue;
1619
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001620 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001621 continue;
1622
1623 found = 1;
1624 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001625 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001626 path->connect = 1; /* new connection */
Mark Browndb432b42011-10-03 21:06:40 +01001627 dapm_mark_dirty(path->source);
1628 } else {
1629 if (path->connect)
1630 dapm_mark_dirty(path->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001631 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001632 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001633 }
1634
Mark Browndb432b42011-10-03 21:06:40 +01001635 if (found) {
1636 dapm_mark_dirty(widget);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001637 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001638 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001639
1640 return 0;
1641}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001642
Milan plzik1b075e32008-01-10 14:39:46 +01001643/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001644static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001645 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001646{
1647 struct snd_soc_dapm_path *path;
1648 int found = 0;
1649
Milan plzik1b075e32008-01-10 14:39:46 +01001650 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001651 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001652 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001653 return -ENODEV;
1654
Richard Purdie2b97eab2006-10-06 18:32:18 +02001655 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001656 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001657 if (path->kcontrol != kcontrol)
1658 continue;
1659
1660 /* found, now check type */
1661 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001662 path->connect = connect;
Mark Browndb432b42011-10-03 21:06:40 +01001663 dapm_mark_dirty(path->source);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001664 }
1665
Mark Browndb432b42011-10-03 21:06:40 +01001666 if (found) {
1667 dapm_mark_dirty(widget);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001668 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001669 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001670
1671 return 0;
1672}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001673
1674/* show dapm widget status in sys fs */
1675static ssize_t dapm_widget_show(struct device *dev,
1676 struct device_attribute *attr, char *buf)
1677{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001678 struct snd_soc_pcm_runtime *rtd =
1679 container_of(dev, struct snd_soc_pcm_runtime, dev);
1680 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001681 struct snd_soc_dapm_widget *w;
1682 int count = 0;
1683 char *state = "not set";
1684
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001685 list_for_each_entry(w, &codec->card->widgets, list) {
1686 if (w->dapm != &codec->dapm)
1687 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001688
1689 /* only display widgets that burnm power */
1690 switch (w->id) {
1691 case snd_soc_dapm_hp:
1692 case snd_soc_dapm_mic:
1693 case snd_soc_dapm_spk:
1694 case snd_soc_dapm_line:
1695 case snd_soc_dapm_micbias:
1696 case snd_soc_dapm_dac:
1697 case snd_soc_dapm_adc:
1698 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001699 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001700 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001701 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001702 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001703 if (w->name)
1704 count += sprintf(buf + count, "%s: %s\n",
1705 w->name, w->power ? "On":"Off");
1706 break;
1707 default:
1708 break;
1709 }
1710 }
1711
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001712 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001713 case SND_SOC_BIAS_ON:
1714 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001715 break;
Mark Brown0be98982008-05-19 12:31:28 +02001716 case SND_SOC_BIAS_PREPARE:
1717 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001718 break;
Mark Brown0be98982008-05-19 12:31:28 +02001719 case SND_SOC_BIAS_STANDBY:
1720 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001721 break;
Mark Brown0be98982008-05-19 12:31:28 +02001722 case SND_SOC_BIAS_OFF:
1723 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001724 break;
1725 }
1726 count += sprintf(buf + count, "PM State: %s\n", state);
1727
1728 return count;
1729}
1730
1731static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1732
1733int snd_soc_dapm_sys_add(struct device *dev)
1734{
Troy Kisky12ef1932008-10-13 17:42:14 -07001735 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001736}
1737
1738static void snd_soc_dapm_sys_remove(struct device *dev)
1739{
Mark Brownaef90842009-05-16 17:53:16 +01001740 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001741}
1742
1743/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001744static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001745{
1746 struct snd_soc_dapm_widget *w, *next_w;
1747 struct snd_soc_dapm_path *p, *next_p;
1748
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001749 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1750 if (w->dapm != dapm)
1751 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001752 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001753 /*
1754 * remove source and sink paths associated to this widget.
1755 * While removing the path, remove reference to it from both
1756 * source and sink widgets so that path is removed only once.
1757 */
1758 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1759 list_del(&p->list_sink);
1760 list_del(&p->list_source);
1761 list_del(&p->list);
1762 kfree(p->long_name);
1763 kfree(p);
1764 }
1765 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1766 list_del(&p->list_sink);
1767 list_del(&p->list_source);
1768 list_del(&p->list);
1769 kfree(p->long_name);
1770 kfree(p);
1771 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001772 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001773 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001774 kfree(w);
1775 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001776}
1777
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001778static struct snd_soc_dapm_widget *dapm_find_widget(
1779 struct snd_soc_dapm_context *dapm, const char *pin,
1780 bool search_other_contexts)
1781{
1782 struct snd_soc_dapm_widget *w;
1783 struct snd_soc_dapm_widget *fallback = NULL;
1784
1785 list_for_each_entry(w, &dapm->card->widgets, list) {
1786 if (!strcmp(w->name, pin)) {
1787 if (w->dapm == dapm)
1788 return w;
1789 else
1790 fallback = w;
1791 }
1792 }
1793
1794 if (search_other_contexts)
1795 return fallback;
1796
1797 return NULL;
1798}
1799
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001800static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001801 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001802{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001803 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001804
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001805 if (!w) {
1806 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1807 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001808 }
1809
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001810 w->connected = status;
1811 if (status == 0)
1812 w->force = 0;
Mark Browndb432b42011-10-03 21:06:40 +01001813 dapm_mark_dirty(w);
Mark Brown0d867332011-04-06 11:38:14 +09001814
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001815 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001816}
1817
Richard Purdie2b97eab2006-10-06 18:32:18 +02001818/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001819 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001820 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001821 *
1822 * Walks all dapm audio paths and powers widgets according to their
1823 * stream or path usage.
1824 *
1825 * Returns 0 for success.
1826 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001827int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001828{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001829 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001830}
Liam Girdwooda5302182008-07-07 13:35:17 +01001831EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001832
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001833static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001834 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001835{
1836 struct snd_soc_dapm_path *path;
1837 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001838 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001839 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001840 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001841 const char *source;
1842 char prefixed_sink[80];
1843 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001844 int ret = 0;
1845
Mark Brown88e8b9a2011-03-02 18:18:24 +00001846 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001847 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1848 dapm->codec->name_prefix, route->sink);
1849 sink = prefixed_sink;
1850 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1851 dapm->codec->name_prefix, route->source);
1852 source = prefixed_source;
1853 } else {
1854 sink = route->sink;
1855 source = route->source;
1856 }
1857
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001858 /*
1859 * find src and dest widgets over all widgets but favor a widget from
1860 * current DAPM context
1861 */
1862 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001863 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001864 wtsink = w;
1865 if (w->dapm == dapm)
1866 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001867 continue;
1868 }
1869 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001870 wtsource = w;
1871 if (w->dapm == dapm)
1872 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001873 }
1874 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001875 /* use widget from another DAPM context if not found from this */
1876 if (!wsink)
1877 wsink = wtsink;
1878 if (!wsource)
1879 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001880
1881 if (wsource == NULL || wsink == NULL)
1882 return -ENODEV;
1883
1884 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1885 if (!path)
1886 return -ENOMEM;
1887
1888 path->source = wsource;
1889 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001890 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001891 INIT_LIST_HEAD(&path->list);
1892 INIT_LIST_HEAD(&path->list_source);
1893 INIT_LIST_HEAD(&path->list_sink);
1894
1895 /* check for external widgets */
1896 if (wsink->id == snd_soc_dapm_input) {
1897 if (wsource->id == snd_soc_dapm_micbias ||
1898 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001899 wsource->id == snd_soc_dapm_line ||
1900 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001901 wsink->ext = 1;
1902 }
1903 if (wsource->id == snd_soc_dapm_output) {
1904 if (wsink->id == snd_soc_dapm_spk ||
1905 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001906 wsink->id == snd_soc_dapm_line ||
1907 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001908 wsource->ext = 1;
1909 }
1910
1911 /* connect static paths */
1912 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001913 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001914 list_add(&path->list_sink, &wsink->sources);
1915 list_add(&path->list_source, &wsource->sinks);
1916 path->connect = 1;
1917 return 0;
1918 }
1919
1920 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001921 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001922 case snd_soc_dapm_adc:
1923 case snd_soc_dapm_dac:
1924 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001925 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001926 case snd_soc_dapm_input:
1927 case snd_soc_dapm_output:
1928 case snd_soc_dapm_micbias:
1929 case snd_soc_dapm_vmid:
1930 case snd_soc_dapm_pre:
1931 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001932 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001933 case snd_soc_dapm_aif_in:
1934 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001935 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001936 list_add(&path->list_sink, &wsink->sources);
1937 list_add(&path->list_source, &wsource->sinks);
1938 path->connect = 1;
1939 return 0;
1940 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001941 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001942 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001943 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06001944 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001945 if (ret != 0)
1946 goto err;
1947 break;
1948 case snd_soc_dapm_switch:
1949 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001950 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001951 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001952 if (ret != 0)
1953 goto err;
1954 break;
1955 case snd_soc_dapm_hp:
1956 case snd_soc_dapm_mic:
1957 case snd_soc_dapm_line:
1958 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001959 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001960 list_add(&path->list_sink, &wsink->sources);
1961 list_add(&path->list_source, &wsource->sinks);
1962 path->connect = 0;
1963 return 0;
1964 }
1965 return 0;
1966
1967err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001968 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1969 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001970 kfree(path);
1971 return ret;
1972}
Mark Brown105f1c22008-05-13 14:52:19 +02001973
1974/**
Mark Brown105f1c22008-05-13 14:52:19 +02001975 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001976 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001977 * @route: audio routes
1978 * @num: number of routes
1979 *
1980 * Connects 2 dapm widgets together via a named audio path. The sink is
1981 * the widget receiving the audio signal, whilst the source is the sender
1982 * of the audio signal.
1983 *
1984 * Returns 0 for success else error. On error all resources can be freed
1985 * with a call to snd_soc_card_free().
1986 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001987int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001988 const struct snd_soc_dapm_route *route, int num)
1989{
1990 int i, ret;
1991
1992 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001993 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001994 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001995 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1996 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001997 return ret;
1998 }
1999 route++;
2000 }
2001
2002 return 0;
2003}
2004EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2005
Mark Brownbf3a9e12011-06-13 16:42:29 +01002006static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2007 const struct snd_soc_dapm_route *route)
2008{
2009 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2010 route->source,
2011 true);
2012 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2013 route->sink,
2014 true);
2015 struct snd_soc_dapm_path *path;
2016 int count = 0;
2017
2018 if (!source) {
2019 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2020 route->source);
2021 return -ENODEV;
2022 }
2023
2024 if (!sink) {
2025 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2026 route->sink);
2027 return -ENODEV;
2028 }
2029
2030 if (route->control || route->connected)
2031 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2032 route->source, route->sink);
2033
2034 list_for_each_entry(path, &source->sinks, list_source) {
2035 if (path->sink == sink) {
2036 path->weak = 1;
2037 count++;
2038 }
2039 }
2040
2041 if (count == 0)
2042 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2043 route->source, route->sink);
2044 if (count > 1)
2045 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2046 count, route->source, route->sink);
2047
2048 return 0;
2049}
2050
2051/**
2052 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2053 * @dapm: DAPM context
2054 * @route: audio routes
2055 * @num: number of routes
2056 *
2057 * Mark existing routes matching those specified in the passed array
2058 * as being weak, meaning that they are ignored for the purpose of
2059 * power decisions. The main intended use case is for sidetone paths
2060 * which couple audio between other independent paths if they are both
2061 * active in order to make the combination work better at the user
2062 * level but which aren't intended to be "used".
2063 *
2064 * Note that CODEC drivers should not use this as sidetone type paths
2065 * can frequently also be used as bypass paths.
2066 */
2067int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2068 const struct snd_soc_dapm_route *route, int num)
2069{
2070 int i, err;
2071 int ret = 0;
2072
2073 for (i = 0; i < num; i++) {
2074 err = snd_soc_dapm_weak_route(dapm, route);
2075 if (err)
2076 ret = err;
2077 route++;
2078 }
2079
2080 return ret;
2081}
2082EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2083
Mark Brown105f1c22008-05-13 14:52:19 +02002084/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002085 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002086 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002087 *
2088 * Checks the codec for any new dapm widgets and creates them if found.
2089 *
2090 * Returns 0 for success.
2091 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002092int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002093{
2094 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002095 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002096
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002097 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002098 {
2099 if (w->new)
2100 continue;
2101
Stephen Warrenfad59882011-04-28 17:37:59 -06002102 if (w->num_kcontrols) {
2103 w->kcontrols = kzalloc(w->num_kcontrols *
2104 sizeof(struct snd_kcontrol *),
2105 GFP_KERNEL);
2106 if (!w->kcontrols)
2107 return -ENOMEM;
2108 }
2109
Richard Purdie2b97eab2006-10-06 18:32:18 +02002110 switch(w->id) {
2111 case snd_soc_dapm_switch:
2112 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002113 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01002114 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002115 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002116 break;
2117 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002118 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002119 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01002120 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002121 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002122 break;
2123 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01002124 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01002125 w->power_check = dapm_adc_check_power;
2126 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002127 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01002128 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01002129 w->power_check = dapm_dac_check_power;
2130 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002131 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002132 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01002133 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002134 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002135 break;
2136 case snd_soc_dapm_input:
2137 case snd_soc_dapm_output:
2138 case snd_soc_dapm_micbias:
2139 case snd_soc_dapm_spk:
2140 case snd_soc_dapm_hp:
2141 case snd_soc_dapm_mic:
2142 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01002143 w->power_check = dapm_generic_check_power;
2144 break;
Mark Brown246d0a12009-04-22 18:24:55 +01002145 case snd_soc_dapm_supply:
2146 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002147 case snd_soc_dapm_vmid:
2148 case snd_soc_dapm_pre:
2149 case snd_soc_dapm_post:
2150 break;
2151 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002152
Mark Brown35c64bca2011-09-28 18:23:53 +01002153 if (!w->power_check)
2154 w->power_check = dapm_always_on_check_power;
2155
Mark Brownb66a70d2011-02-09 18:04:11 +00002156 /* Read the initial power state from the device */
2157 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002158 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002159 val &= 1 << w->shift;
2160 if (w->invert)
2161 val = !val;
2162
2163 if (val)
2164 w->power = 1;
2165 }
2166
Richard Purdie2b97eab2006-10-06 18:32:18 +02002167 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002168
Mark Browndb432b42011-10-03 21:06:40 +01002169 list_add(&w->dirty, &(w->dapm->card->dapm_dirty));
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002170 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002171 }
2172
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002173 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002174 return 0;
2175}
2176EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2177
2178/**
2179 * snd_soc_dapm_get_volsw - dapm mixer get callback
2180 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002181 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002182 *
2183 * Callback to get the value of a dapm mixer control.
2184 *
2185 * Returns 0 for success.
2186 */
2187int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2188 struct snd_ctl_elem_value *ucontrol)
2189{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002190 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2191 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002192 struct soc_mixer_control *mc =
2193 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002194 unsigned int reg = mc->reg;
2195 unsigned int shift = mc->shift;
2196 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002197 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002198 unsigned int invert = mc->invert;
2199 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002200
Richard Purdie2b97eab2006-10-06 18:32:18 +02002201 ucontrol->value.integer.value[0] =
2202 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2203 if (shift != rshift)
2204 ucontrol->value.integer.value[1] =
2205 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2206 if (invert) {
2207 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002208 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002209 if (shift != rshift)
2210 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002211 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002212 }
2213
2214 return 0;
2215}
2216EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2217
2218/**
2219 * snd_soc_dapm_put_volsw - dapm mixer set callback
2220 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002221 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002222 *
2223 * Callback to set the value of a dapm mixer control.
2224 *
2225 * Returns 0 for success.
2226 */
2227int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2228 struct snd_ctl_elem_value *ucontrol)
2229{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002230 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2231 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2232 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002233 struct soc_mixer_control *mc =
2234 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002235 unsigned int reg = mc->reg;
2236 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002237 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002238 unsigned int mask = (1 << fls(max)) - 1;
2239 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002240 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002241 int connect, change;
2242 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002243 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244
2245 val = (ucontrol->value.integer.value[0] & mask);
2246
2247 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002248 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002249 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002250 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002251
Stephen Warrenfafd2172011-04-28 17:38:00 -06002252 if (val)
2253 /* new connection */
2254 connect = invert ? 0 : 1;
2255 else
2256 /* old connection must be powered down */
2257 connect = invert ? 1 : 0;
2258
2259 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260
Stephen Warrene9cf7042011-01-27 14:54:05 -07002261 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002262 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002263 for (wi = 0; wi < wlist->num_widgets; wi++) {
2264 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002265
Stephen Warrenfafd2172011-04-28 17:38:00 -06002266 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002267
Stephen Warrenfafd2172011-04-28 17:38:00 -06002268 update.kcontrol = kcontrol;
2269 update.widget = widget;
2270 update.reg = reg;
2271 update.mask = mask;
2272 update.val = val;
2273 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002274
Stephen Warrenfafd2172011-04-28 17:38:00 -06002275 dapm_mixer_update_power(widget, kcontrol, connect);
2276
2277 widget->dapm->update = NULL;
2278 }
Mark Brown283375c2009-12-07 18:09:03 +00002279 }
2280
Stephen Warrenfafd2172011-04-28 17:38:00 -06002281 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002282 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002283}
2284EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2285
2286/**
2287 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2288 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002289 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002290 *
2291 * Callback to get the value of a dapm enumerated double mixer control.
2292 *
2293 * Returns 0 for success.
2294 */
2295int snd_soc_dapm_get_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];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002300 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002301 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002302
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002303 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002304 ;
2305 val = snd_soc_read(widget->codec, e->reg);
2306 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2307 if (e->shift_l != e->shift_r)
2308 ucontrol->value.enumerated.item[1] =
2309 (val >> e->shift_r) & (bitmask - 1);
2310
2311 return 0;
2312}
2313EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2314
2315/**
2316 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2317 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002318 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002319 *
2320 * Callback to set the value of a dapm enumerated double mixer control.
2321 *
2322 * Returns 0 for success.
2323 */
2324int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2325 struct snd_ctl_elem_value *ucontrol)
2326{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002327 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2328 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2329 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002330 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002331 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002332 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002333 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002334 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002335
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002336 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002337 ;
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002338 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002339 return -EINVAL;
2340 mux = ucontrol->value.enumerated.item[0];
2341 val = mux << e->shift_l;
2342 mask = (bitmask - 1) << e->shift_l;
2343 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002344 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002345 return -EINVAL;
2346 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2347 mask |= (bitmask - 1) << e->shift_r;
2348 }
2349
Stephen Warrenfafd2172011-04-28 17:38:00 -06002350 mutex_lock(&codec->mutex);
2351
Mark Brown3a655772009-10-05 17:23:30 +01002352 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002353 if (change) {
2354 for (wi = 0; wi < wlist->num_widgets; wi++) {
2355 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002356
Stephen Warrenfafd2172011-04-28 17:38:00 -06002357 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002358
Stephen Warrenfafd2172011-04-28 17:38:00 -06002359 update.kcontrol = kcontrol;
2360 update.widget = widget;
2361 update.reg = e->reg;
2362 update.mask = mask;
2363 update.val = val;
2364 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002365
Stephen Warrenfafd2172011-04-28 17:38:00 -06002366 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002367
Stephen Warrenfafd2172011-04-28 17:38:00 -06002368 widget->dapm->update = NULL;
2369 }
2370 }
2371
2372 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002373 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002374}
2375EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2376
2377/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002378 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2379 * @kcontrol: mixer control
2380 * @ucontrol: control element information
2381 *
2382 * Returns 0 for success.
2383 */
2384int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2385 struct snd_ctl_elem_value *ucontrol)
2386{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002387 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2388 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002389
2390 ucontrol->value.enumerated.item[0] = widget->value;
2391
2392 return 0;
2393}
2394EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2395
2396/**
2397 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2398 * @kcontrol: mixer control
2399 * @ucontrol: control element information
2400 *
2401 * Returns 0 for success.
2402 */
2403int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_value *ucontrol)
2405{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002406 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2407 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2408 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002409 struct soc_enum *e =
2410 (struct soc_enum *)kcontrol->private_value;
2411 int change;
2412 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002413 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002414
2415 if (ucontrol->value.enumerated.item[0] >= e->max)
2416 return -EINVAL;
2417
Stephen Warrenfafd2172011-04-28 17:38:00 -06002418 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002419
2420 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002421 if (change) {
2422 for (wi = 0; wi < wlist->num_widgets; wi++) {
2423 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002424
Stephen Warrenfafd2172011-04-28 17:38:00 -06002425 widget->value = ucontrol->value.enumerated.item[0];
2426
2427 dapm_mux_update_power(widget, kcontrol, change,
2428 widget->value, e);
2429 }
2430 }
2431
2432 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002433 return ret;
2434}
2435EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2436
2437/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002438 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2439 * callback
2440 * @kcontrol: mixer control
2441 * @ucontrol: control element information
2442 *
2443 * Callback to get the value of a dapm semi enumerated double mixer control.
2444 *
2445 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2446 * used for handling bitfield coded enumeration for example.
2447 *
2448 * Returns 0 for success.
2449 */
2450int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2451 struct snd_ctl_elem_value *ucontrol)
2452{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002453 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2454 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002455 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002456 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002457
2458 reg_val = snd_soc_read(widget->codec, e->reg);
2459 val = (reg_val >> e->shift_l) & e->mask;
2460 for (mux = 0; mux < e->max; mux++) {
2461 if (val == e->values[mux])
2462 break;
2463 }
2464 ucontrol->value.enumerated.item[0] = mux;
2465 if (e->shift_l != e->shift_r) {
2466 val = (reg_val >> e->shift_r) & e->mask;
2467 for (mux = 0; mux < e->max; mux++) {
2468 if (val == e->values[mux])
2469 break;
2470 }
2471 ucontrol->value.enumerated.item[1] = mux;
2472 }
2473
2474 return 0;
2475}
2476EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2477
2478/**
2479 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2480 * callback
2481 * @kcontrol: mixer control
2482 * @ucontrol: control element information
2483 *
2484 * Callback to set the value of a dapm semi enumerated double mixer control.
2485 *
2486 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2487 * used for handling bitfield coded enumeration for example.
2488 *
2489 * Returns 0 for success.
2490 */
2491int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2492 struct snd_ctl_elem_value *ucontrol)
2493{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002494 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2495 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2496 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002497 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002498 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002499 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002500 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002501 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002502
2503 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2504 return -EINVAL;
2505 mux = ucontrol->value.enumerated.item[0];
2506 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2507 mask = e->mask << e->shift_l;
2508 if (e->shift_l != e->shift_r) {
2509 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2510 return -EINVAL;
2511 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2512 mask |= e->mask << e->shift_r;
2513 }
2514
Stephen Warrenfafd2172011-04-28 17:38:00 -06002515 mutex_lock(&codec->mutex);
2516
Mark Brown3a655772009-10-05 17:23:30 +01002517 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002518 if (change) {
2519 for (wi = 0; wi < wlist->num_widgets; wi++) {
2520 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002521
Stephen Warrenfafd2172011-04-28 17:38:00 -06002522 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002523
Stephen Warrenfafd2172011-04-28 17:38:00 -06002524 update.kcontrol = kcontrol;
2525 update.widget = widget;
2526 update.reg = e->reg;
2527 update.mask = mask;
2528 update.val = val;
2529 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002530
Stephen Warrenfafd2172011-04-28 17:38:00 -06002531 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002532
Stephen Warrenfafd2172011-04-28 17:38:00 -06002533 widget->dapm->update = NULL;
2534 }
2535 }
2536
2537 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002538 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002539}
2540EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2541
2542/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002543 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2544 *
2545 * @kcontrol: mixer control
2546 * @uinfo: control element information
2547 *
2548 * Callback to provide information about a pin switch control.
2549 */
2550int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2551 struct snd_ctl_elem_info *uinfo)
2552{
2553 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2554 uinfo->count = 1;
2555 uinfo->value.integer.min = 0;
2556 uinfo->value.integer.max = 1;
2557
2558 return 0;
2559}
2560EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2561
2562/**
2563 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2564 *
2565 * @kcontrol: mixer control
2566 * @ucontrol: Value
2567 */
2568int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2569 struct snd_ctl_elem_value *ucontrol)
2570{
2571 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2572 const char *pin = (const char *)kcontrol->private_value;
2573
2574 mutex_lock(&codec->mutex);
2575
2576 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002577 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002578
2579 mutex_unlock(&codec->mutex);
2580
2581 return 0;
2582}
2583EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2584
2585/**
2586 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2587 *
2588 * @kcontrol: mixer control
2589 * @ucontrol: Value
2590 */
2591int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2592 struct snd_ctl_elem_value *ucontrol)
2593{
2594 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2595 const char *pin = (const char *)kcontrol->private_value;
2596
2597 mutex_lock(&codec->mutex);
2598
2599 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002600 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002601 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002602 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002603
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002604 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002605
2606 mutex_unlock(&codec->mutex);
2607
2608 return 0;
2609}
2610EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2611
2612/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002613 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002614 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002615 * @widget: widget template
2616 *
2617 * Creates a new dapm control based upon the template.
2618 *
2619 * Returns 0 for success else error.
2620 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002621int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002622 const struct snd_soc_dapm_widget *widget)
2623{
2624 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002625 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002626
2627 if ((w = dapm_cnew_widget(widget)) == NULL)
2628 return -ENOMEM;
2629
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002630 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002631 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002632 name_len += 1 + strlen(dapm->codec->name_prefix);
2633 w->name = kmalloc(name_len, GFP_KERNEL);
2634 if (w->name == NULL) {
2635 kfree(w);
2636 return -ENOMEM;
2637 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002638 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002639 snprintf(w->name, name_len, "%s %s",
2640 dapm->codec->name_prefix, widget->name);
2641 else
2642 snprintf(w->name, name_len, "%s", widget->name);
2643
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002644 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002645 w->dapm = dapm;
2646 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002647 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002648 INIT_LIST_HEAD(&w->sources);
2649 INIT_LIST_HEAD(&w->sinks);
2650 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01002651 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002652 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002653
2654 /* machine layer set ups unconnected pins and insertions */
2655 w->connected = 1;
2656 return 0;
2657}
2658EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2659
2660/**
Mark Brown4ba13272008-05-13 14:51:19 +02002661 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002662 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002663 * @widget: widget array
2664 * @num: number of widgets
2665 *
2666 * Creates new DAPM controls based upon the templates.
2667 *
2668 * Returns 0 for success else error.
2669 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002670int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002671 const struct snd_soc_dapm_widget *widget,
2672 int num)
2673{
2674 int i, ret;
2675
2676 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002677 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002678 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002679 dev_err(dapm->dev,
2680 "ASoC: Failed to create DAPM control %s: %d\n",
2681 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002682 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002683 }
Mark Brown4ba13272008-05-13 14:51:19 +02002684 widget++;
2685 }
2686 return 0;
2687}
2688EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2689
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002690static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002691 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002692{
2693 struct snd_soc_dapm_widget *w;
2694
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002695 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002696 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002697 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002698 continue;
Liam Girdwoodee47b362011-07-25 11:15:50 +01002699 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002700 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002701 if (strstr(w->sname, stream)) {
Mark Browndb432b42011-10-03 21:06:40 +01002702 dapm_mark_dirty(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002703 switch(event) {
2704 case SND_SOC_DAPM_STREAM_START:
2705 w->active = 1;
2706 break;
2707 case SND_SOC_DAPM_STREAM_STOP:
2708 w->active = 0;
2709 break;
2710 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002711 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002712 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002713 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2714 break;
2715 }
2716 }
2717 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002718
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002719 dapm_power_widgets(dapm, event);
Liam Girdwood64a648c222011-07-25 11:15:15 +01002720
2721 /* do we need to notify any clients that DAPM stream is complete */
2722 if (dapm->stream_event)
2723 dapm->stream_event(dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002724}
2725
2726/**
2727 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2728 * @rtd: PCM runtime data
2729 * @stream: stream name
2730 * @event: stream event
2731 *
2732 * Sends a stream event to the dapm core. The core then makes any
2733 * necessary widget power changes.
2734 *
2735 * Returns 0 for success else error.
2736 */
2737int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2738 const char *stream, int event)
2739{
2740 struct snd_soc_codec *codec = rtd->codec;
2741
2742 if (stream == NULL)
2743 return 0;
2744
2745 mutex_lock(&codec->mutex);
2746 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002747 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002748 return 0;
2749}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002750
2751/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002752 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002753 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002754 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002755 *
Mark Brown74b8f952009-06-06 11:26:15 +01002756 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002757 * a valid audio route and active audio stream.
2758 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2759 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002760 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002761int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002762{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002763 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002764}
Liam Girdwooda5302182008-07-07 13:35:17 +01002765EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002766
2767/**
Mark Brownda341832010-03-15 19:23:37 +00002768 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002769 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002770 * @pin: pin name
2771 *
2772 * Enables input/output pin regardless of any other state. This is
2773 * intended for use with microphone bias supplies used in microphone
2774 * jack detection.
2775 *
2776 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2777 * do any widget power switching.
2778 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002779int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2780 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002781{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002782 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002783
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002784 if (!w) {
2785 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2786 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002787 }
2788
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002789 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2790 w->connected = 1;
2791 w->force = 1;
Mark Browndb432b42011-10-03 21:06:40 +01002792 dapm_mark_dirty(w);
Mark Brown0d867332011-04-06 11:38:14 +09002793
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002794 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002795}
2796EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2797
2798/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002799 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002800 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002801 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002802 *
Mark Brown74b8f952009-06-06 11:26:15 +01002803 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002804 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2805 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002806 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002807int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2808 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002809{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002810 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002811}
2812EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2813
2814/**
Mark Brown5817b522008-09-24 11:23:11 +01002815 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002816 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002817 * @pin: pin name
2818 *
2819 * Marks the specified pin as being not connected, disabling it along
2820 * any parent or child widgets. At present this is identical to
2821 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2822 * additional things such as disabling controls which only affect
2823 * paths through the pin.
2824 *
2825 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2826 * do any widget power switching.
2827 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002828int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002829{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002830 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002831}
2832EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2833
2834/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002835 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002836 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002837 * @pin: audio signal pin endpoint (or start point)
2838 *
2839 * Get audio pin status - connected or disconnected.
2840 *
2841 * Returns 1 for connected otherwise 0.
2842 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002843int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2844 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002845{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002846 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002847
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002848 if (w)
2849 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002850
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002851 return 0;
2852}
Liam Girdwooda5302182008-07-07 13:35:17 +01002853EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002854
2855/**
Mark Brown1547aba2010-05-07 21:11:40 +01002856 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002857 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002858 * @pin: audio signal pin endpoint (or start point)
2859 *
2860 * Mark the given endpoint or pin as ignoring suspend. When the
2861 * system is disabled a path between two endpoints flagged as ignoring
2862 * suspend will not be disabled. The path must already be enabled via
2863 * normal means at suspend time, it will not be turned on if it was not
2864 * already enabled.
2865 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002866int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2867 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002868{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002869 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002870
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002871 if (!w) {
2872 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2873 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002874 }
2875
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002876 w->ignore_suspend = 1;
2877
2878 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002879}
2880EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2881
2882/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002883 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03002884 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002885 *
2886 * Free all dapm widgets and resources.
2887 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002888void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002889{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002890 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002891 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002892 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002893 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002894}
2895EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2896
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002897static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002898{
Mark Brown51737472009-06-22 13:16:51 +01002899 struct snd_soc_dapm_widget *w;
2900 LIST_HEAD(down_list);
2901 int powerdown = 0;
2902
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002903 list_for_each_entry(w, &dapm->card->widgets, list) {
2904 if (w->dapm != dapm)
2905 continue;
Mark Brown51737472009-06-22 13:16:51 +01002906 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002907 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002908 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002909 powerdown = 1;
2910 }
2911 }
2912
2913 /* If there were no widgets to power down we're already in
2914 * standby.
2915 */
2916 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002917 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002918 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002919 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002920 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002921}
Mark Brown51737472009-06-22 13:16:51 +01002922
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002923/*
2924 * snd_soc_dapm_shutdown - callback for system shutdown
2925 */
2926void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2927{
2928 struct snd_soc_codec *codec;
2929
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002930 list_for_each_entry(codec, &card->codec_dev_list, list) {
2931 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002932 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002933 }
Mark Brown51737472009-06-22 13:16:51 +01002934}
2935
Richard Purdie2b97eab2006-10-06 18:32:18 +02002936/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002937MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002938MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2939MODULE_LICENSE("GPL");