blob: 1bb6d4a63cd8630854ae2b82f47a70057cdd753a [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
Liam Girdwood612a3fe2012-02-06 16:05:29 +000017 * mic/headphone insertion events.
Richard Purdie2b97eab2006-10-06 18:32:18 +020018 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Liam Girdwood612a3fe2012-02-06 16:05:29 +000021 * o Delayed power down of audio subsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020024 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080029#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020030#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020035#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000036#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000037#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020039#include <sound/core.h>
40#include <sound/pcm.h>
41#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020042#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020043#include <sound/initval.h>
44
Mark Brown84e90932010-11-04 00:07:02 -040045#include <trace/events/asoc.h>
46
Mark Brownde02d072011-09-20 21:43:24 +010047#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
48
Richard Purdie2b97eab2006-10-06 18:32:18 +020049/* dapm power sequences - make this per codec in the future */
50static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010051 [snd_soc_dapm_pre] = 0,
52 [snd_soc_dapm_supply] = 1,
Mark Brown62ea8742012-01-21 21:14:48 +000053 [snd_soc_dapm_regulator_supply] = 1,
Mark Brown38357ab2009-06-06 19:03:23 +010054 [snd_soc_dapm_micbias] = 2,
Mark Brown888df392012-02-16 19:37:51 -080055 [snd_soc_dapm_dai] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010056 [snd_soc_dapm_aif_in] = 3,
57 [snd_soc_dapm_aif_out] = 3,
58 [snd_soc_dapm_mic] = 4,
59 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000060 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010061 [snd_soc_dapm_value_mux] = 5,
62 [snd_soc_dapm_dac] = 6,
63 [snd_soc_dapm_mixer] = 7,
64 [snd_soc_dapm_mixer_named_ctl] = 7,
65 [snd_soc_dapm_pga] = 8,
66 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060067 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010068 [snd_soc_dapm_hp] = 10,
69 [snd_soc_dapm_spk] = 10,
Mark Brown7e1f7c82012-04-12 17:29:36 +010070 [snd_soc_dapm_line] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010071 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020072};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000073
Richard Purdie2b97eab2006-10-06 18:32:18 +020074static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010075 [snd_soc_dapm_pre] = 0,
76 [snd_soc_dapm_adc] = 1,
77 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010078 [snd_soc_dapm_spk] = 2,
Mark Brown7e1f7c82012-04-12 17:29:36 +010079 [snd_soc_dapm_line] = 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,
Mark Brown888df392012-02-16 19:37:51 -080092 [snd_soc_dapm_dai] = 10,
Mark Brown62ea8742012-01-21 21:14:48 +000093 [snd_soc_dapm_regulator_supply] = 11,
Mark Brown010ff262009-08-17 17:39:22 +010094 [snd_soc_dapm_supply] = 11,
95 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020096};
97
Troy Kisky12ef1932008-10-13 17:42:14 -070098static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010099{
100 if (pop_time)
101 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
102}
103
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200104static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100105{
106 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200107 char *buf;
108
109 if (!pop_time)
110 return;
111
112 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
113 if (buf == NULL)
114 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100115
116 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200117 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100118 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100119 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200120
121 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100122}
123
Mark Browndb432b42011-10-03 21:06:40 +0100124static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
125{
126 return !list_empty(&w->dirty);
127}
128
Mark Brown25c77c52011-10-08 13:36:03 +0100129void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100130{
Mark Brown75c1f892011-10-04 22:28:08 +0100131 if (!dapm_dirty_widget(w)) {
132 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
133 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100134 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100135 }
Mark Browndb432b42011-10-03 21:06:40 +0100136}
Mark Brown25c77c52011-10-08 13:36:03 +0100137EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100138
Richard Purdie2b97eab2006-10-06 18:32:18 +0200139/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100140static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200141 const struct snd_soc_dapm_widget *_widget)
142{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100143 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200144}
145
Liam Girdwood48056082011-07-20 12:23:33 +0100146/* get snd_card from DAPM context */
147static inline struct snd_card *dapm_get_snd_card(
148 struct snd_soc_dapm_context *dapm)
149{
150 if (dapm->codec)
151 return dapm->codec->card->snd_card;
152 else if (dapm->platform)
153 return dapm->platform->card->snd_card;
154 else
155 BUG();
156
157 /* unreachable */
158 return NULL;
159}
160
161/* get soc_card from DAPM context */
162static inline struct snd_soc_card *dapm_get_soc_card(
163 struct snd_soc_dapm_context *dapm)
164{
165 if (dapm->codec)
166 return dapm->codec->card;
167 else if (dapm->platform)
168 return dapm->platform->card;
169 else
170 BUG();
171
172 /* unreachable */
173 return NULL;
174}
175
Liam Girdwood6c120e12012-02-15 15:15:34 +0000176static void dapm_reset(struct snd_soc_card *card)
177{
178 struct snd_soc_dapm_widget *w;
179
180 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
181
182 list_for_each_entry(w, &card->widgets, list) {
183 w->power_checked = false;
184 w->inputs = -1;
185 w->outputs = -1;
186 }
187}
188
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100189static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
190{
191 if (w->codec)
192 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100193 else if (w->platform)
194 return snd_soc_platform_read(w->platform, reg);
195
196 dev_err(w->dapm->dev, "no valid widget read method\n");
197 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100198}
199
200static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
201{
202 if (w->codec)
203 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100204 else if (w->platform)
205 return snd_soc_platform_write(w->platform, reg, val);
206
207 dev_err(w->dapm->dev, "no valid widget write method\n");
208 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100209}
210
211static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
212 unsigned short reg, unsigned int mask, unsigned int value)
213{
Mark Brown8a713da2011-12-03 12:33:55 +0000214 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100215 unsigned int old, new;
216 int ret;
217
Mark Brown8a713da2011-12-03 12:33:55 +0000218 if (w->codec && w->codec->using_regmap) {
219 ret = regmap_update_bits_check(w->codec->control_data,
220 reg, mask, value, &change);
221 if (ret != 0)
222 return ret;
223 } else {
224 ret = soc_widget_read(w, reg);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100225 if (ret < 0)
226 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +0000227
228 old = ret;
229 new = (old & ~mask) | (value & mask);
230 change = old != new;
231 if (change) {
232 ret = soc_widget_write(w, reg, new);
Mark Brown80f48142012-03-12 10:33:34 +0000233 if (ret < 0)
Mark Brown8a713da2011-12-03 12:33:55 +0000234 return ret;
235 }
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100236 }
237
238 return change;
239}
240
Mark Brown452c5ea2009-05-17 21:41:23 +0100241/**
242 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800243 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100244 * @level: level to configure
245 *
246 * Configure the bias (power) levels for the SoC audio device.
247 *
248 * Returns 0 for success else error.
249 */
Mark Browned5a4c42011-02-18 11:12:42 -0800250static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200251 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100252{
Mark Browned5a4c42011-02-18 11:12:42 -0800253 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100254 int ret = 0;
255
Mark Brown84e90932010-11-04 00:07:02 -0400256 trace_snd_soc_bias_level_start(card, level);
257
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000258 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100259 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100260 if (ret != 0)
261 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100262
Mark Browncc4c6702011-06-06 19:03:34 +0100263 if (dapm->codec) {
264 if (dapm->codec->driver->set_bias_level)
265 ret = dapm->codec->driver->set_bias_level(dapm->codec,
266 level);
267 else
268 dapm->bias_level = level;
269 }
Mark Brown171ec6b2011-06-06 18:15:19 +0100270 if (ret != 0)
271 goto out;
272
273 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100274 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100275out:
Mark Brown84e90932010-11-04 00:07:02 -0400276 trace_snd_soc_bias_level_done(card, level);
277
Mark Brown452c5ea2009-05-17 21:41:23 +0100278 return ret;
279}
280
Richard Purdie2b97eab2006-10-06 18:32:18 +0200281/* set up initial codec paths */
282static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
283 struct snd_soc_dapm_path *p, int i)
284{
285 switch (w->id) {
286 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000287 case snd_soc_dapm_mixer:
288 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200289 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100290 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600291 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400292 unsigned int reg = mc->reg;
293 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100294 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400295 unsigned int mask = (1 << fls(max)) - 1;
296 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200297
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100298 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200299 val = (val >> shift) & mask;
300
301 if ((invert && !val) || (!invert && val))
302 p->connect = 1;
303 else
304 p->connect = 0;
305 }
306 break;
307 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600308 struct soc_enum *e = (struct soc_enum *)
309 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200310 int val, item, bitmask;
311
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100312 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100313 ;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100314 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200315 item = (val >> e->shift_l) & (bitmask - 1);
316
317 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100318 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200319 if (!(strcmp(p->name, e->texts[i])) && item == i)
320 p->connect = 1;
321 }
322 }
323 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000324 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600325 struct soc_enum *e = (struct soc_enum *)
326 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000327
328 p->connect = 0;
329 /* since a virtual mux has no backing registers to
330 * decide which path to connect, it will try to match
331 * with the first enumeration. This is to ensure
332 * that the default mux choice (the first) will be
333 * correctly powered up during initialization.
334 */
335 if (!strcmp(p->name, e->texts[0]))
336 p->connect = 1;
337 }
338 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200339 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200340 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600341 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200342 int val, item;
343
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100344 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200345 val = (val >> e->shift_l) & e->mask;
346 for (item = 0; item < e->max; item++) {
347 if (val == e->values[item])
348 break;
349 }
350
351 p->connect = 0;
352 for (i = 0; i < e->max; i++) {
353 if (!(strcmp(p->name, e->texts[i])) && item == i)
354 p->connect = 1;
355 }
356 }
357 break;
Mark Brown56563102011-10-03 22:41:09 +0100358 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200359 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600360 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200361 case snd_soc_dapm_output:
362 case snd_soc_dapm_adc:
363 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000364 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200365 case snd_soc_dapm_dac:
366 case snd_soc_dapm_micbias:
367 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100368 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000369 case snd_soc_dapm_regulator_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100370 case snd_soc_dapm_aif_in:
371 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800372 case snd_soc_dapm_dai:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200373 case snd_soc_dapm_hp:
374 case snd_soc_dapm_mic:
375 case snd_soc_dapm_spk:
376 case snd_soc_dapm_line:
Mark Brown56563102011-10-03 22:41:09 +0100377 p->connect = 1;
378 break;
379 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200380 case snd_soc_dapm_pre:
381 case snd_soc_dapm_post:
382 p->connect = 0;
383 break;
384 }
385}
386
Mark Brown74b8f952009-06-06 11:26:15 +0100387/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200388static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200389 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
390 struct snd_soc_dapm_path *path, const char *control_name,
391 const struct snd_kcontrol_new *kcontrol)
392{
393 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
394 int i;
395
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100396 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200397 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200398 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200399 list_add(&path->list_sink, &dest->sources);
400 list_add(&path->list_source, &src->sinks);
401 path->name = (char*)e->texts[i];
402 dapm_set_path_status(dest, path, 0);
403 return 0;
404 }
405 }
406
407 return -ENODEV;
408}
409
Mark Brown74b8f952009-06-06 11:26:15 +0100410/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200411static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200412 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
413 struct snd_soc_dapm_path *path, const char *control_name)
414{
415 int i;
416
417 /* search for mixer kcontrol */
418 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600419 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200420 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200421 list_add(&path->list_sink, &dest->sources);
422 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600423 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200424 dapm_set_path_status(dest, path, i);
425 return 0;
426 }
427 }
428 return -ENODEV;
429}
430
Stephen Warrenaf468002011-04-28 17:38:01 -0600431static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600432 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600433 const struct snd_kcontrol_new *kcontrol_new,
434 struct snd_kcontrol **kcontrol)
435{
436 struct snd_soc_dapm_widget *w;
437 int i;
438
439 *kcontrol = NULL;
440
441 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600442 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
443 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600444 for (i = 0; i < w->num_kcontrols; i++) {
445 if (&w->kcontrol_news[i] == kcontrol_new) {
446 if (w->kcontrols)
447 *kcontrol = w->kcontrols[i];
448 return 1;
449 }
450 }
451 }
452
453 return 0;
454}
455
Richard Purdie2b97eab2006-10-06 18:32:18 +0200456/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200457static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200458{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200459 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200460 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000461 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200462 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000463 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000464 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600465 struct snd_soc_dapm_widget_list *wlist;
466 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000467
468 if (dapm->codec)
469 prefix = dapm->codec->name_prefix;
470 else
471 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200472
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000473 if (prefix)
474 prefix_len = strlen(prefix) + 1;
475 else
476 prefix_len = 0;
477
Richard Purdie2b97eab2006-10-06 18:32:18 +0200478 /* add kcontrol */
479 for (i = 0; i < w->num_kcontrols; i++) {
480
481 /* match name */
482 list_for_each_entry(path, &w->sources, list_sink) {
483
484 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600485 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200486 continue;
487
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200488 if (w->kcontrols[i]) {
489 path->kcontrol = w->kcontrols[i];
490 continue;
491 }
492
Stephen Warrenfafd2172011-04-28 17:38:00 -0600493 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
494 sizeof(struct snd_soc_dapm_widget *),
495 wlist = kzalloc(wlistsize, GFP_KERNEL);
496 if (wlist == NULL) {
497 dev_err(dapm->dev,
498 "asoc: can't allocate widget list for %s\n",
499 w->name);
500 return -ENOMEM;
501 }
502 wlist->num_widgets = 1;
503 wlist->widgets[0] = w;
504
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000505 /* add dapm control with long name.
506 * for dapm_mixer this is the concatenation of the
507 * mixer and kcontrol name.
508 * for dapm_mixer_named_ctl this is simply the
509 * kcontrol name.
510 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600511 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000512 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000513 name_len += 1 + strlen(w->name);
514
Mark Brown219b93f2008-10-28 13:02:31 +0000515 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000516
Stephen Warrenfafd2172011-04-28 17:38:00 -0600517 if (path->long_name == NULL) {
518 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200519 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600520 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200521
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000522 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000523 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000524 /* The control will get a prefix from
525 * the control creation process but
526 * we're also using the same prefix
527 * for widgets so cut the prefix off
528 * the front of the widget name.
529 */
Mark Brown888df392012-02-16 19:37:51 -0800530 snprintf((char *)path->long_name, name_len,
531 "%s %s", w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600532 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000533 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000534 case snd_soc_dapm_mixer_named_ctl:
Mark Brown888df392012-02-16 19:37:51 -0800535 snprintf((char *)path->long_name, name_len,
536 "%s", w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000537 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000538 }
539
Mark Brown888df392012-02-16 19:37:51 -0800540 ((char *)path->long_name)[name_len - 1] = '\0';
Mark Brown219b93f2008-10-28 13:02:31 +0000541
Stephen Warrenfafd2172011-04-28 17:38:00 -0600542 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
543 wlist, path->long_name,
544 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200545 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200546 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200547 dev_err(dapm->dev,
548 "asoc: failed to add dapm kcontrol %s: %d\n",
549 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600550 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200551 kfree(path->long_name);
552 path->long_name = NULL;
553 return ret;
554 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600555 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200556 }
557 }
558 return ret;
559}
560
561/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200562static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200563{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200564 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200565 struct snd_soc_dapm_path *path = NULL;
566 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000567 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000568 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000569 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600570 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600571 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600572 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600573 size_t wlistsize;
Mark Brown888df392012-02-16 19:37:51 -0800574 const char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200575
Stephen Warrenaf468002011-04-28 17:38:01 -0600576 if (w->num_kcontrols != 1) {
577 dev_err(dapm->dev,
578 "asoc: mux %s has incorrect number of controls\n",
579 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200580 return -EINVAL;
581 }
582
Stephen Warren1007da02011-05-26 09:57:33 -0600583 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600584 &kcontrol);
585 if (kcontrol) {
586 wlist = kcontrol->private_data;
587 wlistentries = wlist->num_widgets + 1;
588 } else {
589 wlist = NULL;
590 wlistentries = 1;
591 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600592 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600593 wlistentries * sizeof(struct snd_soc_dapm_widget *),
594 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600595 if (wlist == NULL) {
596 dev_err(dapm->dev,
597 "asoc: can't allocate widget list for %s\n", w->name);
598 return -ENOMEM;
599 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600600 wlist->num_widgets = wlistentries;
601 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600602
Stephen Warrenaf468002011-04-28 17:38:01 -0600603 if (!kcontrol) {
604 if (dapm->codec)
605 prefix = dapm->codec->name_prefix;
606 else
607 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000608
Stephen Warrenaf468002011-04-28 17:38:01 -0600609 if (shared) {
610 name = w->kcontrol_news[0].name;
611 prefix_len = 0;
612 } else {
613 name = w->name;
614 if (prefix)
615 prefix_len = strlen(prefix) + 1;
616 else
617 prefix_len = 0;
618 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000619
Stephen Warrenaf468002011-04-28 17:38:01 -0600620 /*
621 * The control will get a prefix from the control creation
622 * process but we're also using the same prefix for widgets so
623 * cut the prefix off the front of the widget name.
624 */
625 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
626 name + prefix_len, prefix);
627 ret = snd_ctl_add(card, kcontrol);
628 if (ret < 0) {
Mark Brown53daf202011-09-05 10:51:05 -0700629 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
630 w->name, ret);
Stephen Warrenaf468002011-04-28 17:38:01 -0600631 kfree(wlist);
632 return ret;
633 }
634 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200635
Stephen Warrenaf468002011-04-28 17:38:01 -0600636 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200637
Stephen Warrenfad59882011-04-28 17:37:59 -0600638 w->kcontrols[0] = kcontrol;
639
Richard Purdie2b97eab2006-10-06 18:32:18 +0200640 list_for_each_entry(path, &w->sources, list_sink)
641 path->kcontrol = kcontrol;
642
Stephen Warrenaf468002011-04-28 17:38:01 -0600643 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200644}
645
646/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200647static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200648{
Mark Browna6c65732010-03-03 17:45:21 +0000649 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200650 dev_err(w->dapm->dev,
651 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200652
Mark Browna6c65732010-03-03 17:45:21 +0000653 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200654}
655
656/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200657static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200658{
659 struct snd_soc_dapm_path *p;
660
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200661 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200662 p->walked = 0;
663}
664
Mark Brown99497882010-05-07 20:24:05 +0100665/* We implement power down on suspend by checking the power state of
666 * the ALSA card - when we are suspending the ALSA state for the card
667 * is set to D3.
668 */
669static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
670{
Mark Brown12ea2c72011-03-02 18:17:32 +0000671 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100672
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100674 case SNDRV_CTL_POWER_D3hot:
675 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100676 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200677 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
678 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100679 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100680 default:
681 return 1;
682 }
683}
684
Richard Purdie2b97eab2006-10-06 18:32:18 +0200685/*
686 * Recursively check for a completed path to an active or physically connected
687 * output widget. Returns number of complete paths.
688 */
689static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
690{
691 struct snd_soc_dapm_path *path;
692 int con = 0;
693
Mark Brown024dc072011-10-09 11:52:05 +0100694 if (widget->outputs >= 0)
695 return widget->outputs;
696
Mark Brownde02d072011-09-20 21:43:24 +0100697 DAPM_UPDATE_STAT(widget, path_checks);
698
Mark Brown62ea8742012-01-21 21:14:48 +0000699 switch (widget->id) {
700 case snd_soc_dapm_supply:
701 case snd_soc_dapm_regulator_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100702 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000703 default:
704 break;
705 }
Mark Brown246d0a12009-04-22 18:24:55 +0100706
Mark Brown010ff262009-08-17 17:39:22 +0100707 switch (widget->id) {
708 case snd_soc_dapm_adc:
709 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800710 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100711 if (widget->active) {
712 widget->outputs = snd_soc_dapm_suspend_check(widget);
713 return widget->outputs;
714 }
Mark Brown010ff262009-08-17 17:39:22 +0100715 default:
716 break;
717 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200718
719 if (widget->connected) {
720 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100721 if (widget->id == snd_soc_dapm_output && !widget->ext) {
722 widget->outputs = snd_soc_dapm_suspend_check(widget);
723 return widget->outputs;
724 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200725
726 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100727 if (widget->id == snd_soc_dapm_hp ||
728 widget->id == snd_soc_dapm_spk ||
729 (widget->id == snd_soc_dapm_line &&
730 !list_empty(&widget->sources))) {
731 widget->outputs = snd_soc_dapm_suspend_check(widget);
732 return widget->outputs;
733 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200734 }
735
736 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100737 DAPM_UPDATE_STAT(widget, neighbour_checks);
738
Mark Brownbf3a9e12011-06-13 16:42:29 +0100739 if (path->weak)
740 continue;
741
Richard Purdie2b97eab2006-10-06 18:32:18 +0200742 if (path->walked)
743 continue;
744
745 if (path->sink && path->connect) {
746 path->walked = 1;
747 con += is_connected_output_ep(path->sink);
748 }
749 }
750
Mark Brown024dc072011-10-09 11:52:05 +0100751 widget->outputs = con;
752
Richard Purdie2b97eab2006-10-06 18:32:18 +0200753 return con;
754}
755
756/*
757 * Recursively check for a completed path to an active or physically connected
758 * input widget. Returns number of complete paths.
759 */
760static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
761{
762 struct snd_soc_dapm_path *path;
763 int con = 0;
764
Mark Brown024dc072011-10-09 11:52:05 +0100765 if (widget->inputs >= 0)
766 return widget->inputs;
767
Mark Brownde02d072011-09-20 21:43:24 +0100768 DAPM_UPDATE_STAT(widget, path_checks);
769
Mark Brown62ea8742012-01-21 21:14:48 +0000770 switch (widget->id) {
771 case snd_soc_dapm_supply:
772 case snd_soc_dapm_regulator_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100773 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000774 default:
775 break;
776 }
Mark Brown246d0a12009-04-22 18:24:55 +0100777
Richard Purdie2b97eab2006-10-06 18:32:18 +0200778 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100779 switch (widget->id) {
780 case snd_soc_dapm_dac:
781 case snd_soc_dapm_aif_in:
Mark Brown888df392012-02-16 19:37:51 -0800782 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100783 if (widget->active) {
784 widget->inputs = snd_soc_dapm_suspend_check(widget);
785 return widget->inputs;
786 }
Mark Brown010ff262009-08-17 17:39:22 +0100787 default:
788 break;
789 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200790
791 if (widget->connected) {
792 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100793 if (widget->id == snd_soc_dapm_input && !widget->ext) {
794 widget->inputs = snd_soc_dapm_suspend_check(widget);
795 return widget->inputs;
796 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200797
798 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100799 if (widget->id == snd_soc_dapm_vmid) {
800 widget->inputs = snd_soc_dapm_suspend_check(widget);
801 return widget->inputs;
802 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200803
804 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300805 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100806 (widget->id == snd_soc_dapm_line &&
807 !list_empty(&widget->sinks))) {
808 widget->inputs = snd_soc_dapm_suspend_check(widget);
809 return widget->inputs;
810 }
811
Mark Brown1ab97c82011-11-27 16:21:51 +0000812 /* signal generator */
813 if (widget->id == snd_soc_dapm_siggen) {
814 widget->inputs = snd_soc_dapm_suspend_check(widget);
815 return widget->inputs;
816 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200817 }
818
819 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100820 DAPM_UPDATE_STAT(widget, neighbour_checks);
821
Mark Brownbf3a9e12011-06-13 16:42:29 +0100822 if (path->weak)
823 continue;
824
Richard Purdie2b97eab2006-10-06 18:32:18 +0200825 if (path->walked)
826 continue;
827
828 if (path->source && path->connect) {
829 path->walked = 1;
830 con += is_connected_input_ep(path->source);
831 }
832 }
833
Mark Brown024dc072011-10-09 11:52:05 +0100834 widget->inputs = con;
835
Richard Purdie2b97eab2006-10-06 18:32:18 +0200836 return con;
837}
838
839/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300840 * Handler for generic register modifier widget.
841 */
842int dapm_reg_event(struct snd_soc_dapm_widget *w,
843 struct snd_kcontrol *kcontrol, int event)
844{
845 unsigned int val;
846
847 if (SND_SOC_DAPM_EVENT_ON(event))
848 val = w->on_val;
849 else
850 val = w->off_val;
851
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100852 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300853 w->mask << w->shift, val << w->shift);
854
855 return 0;
856}
Mark Brown11589412008-07-29 11:42:23 +0100857EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300858
Mark Brown62ea8742012-01-21 21:14:48 +0000859/*
860 * Handler for regulator supply widget.
861 */
862int dapm_regulator_event(struct snd_soc_dapm_widget *w,
863 struct snd_kcontrol *kcontrol, int event)
864{
865 if (SND_SOC_DAPM_EVENT_ON(event))
866 return regulator_enable(w->priv);
867 else
868 return regulator_disable_deferred(w->priv, w->shift);
869}
870EXPORT_SYMBOL_GPL(dapm_regulator_event);
871
Mark Brownd8050022011-09-28 18:28:23 +0100872static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
873{
Mark Brown9b8a83b2011-10-04 22:15:59 +0100874 if (w->power_checked)
875 return w->new_power;
876
Mark Brownd8050022011-09-28 18:28:23 +0100877 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +0100878 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +0100879 else
Mark Brown9b8a83b2011-10-04 22:15:59 +0100880 w->new_power = w->power_check(w);
881
882 w->power_checked = true;
883
884 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +0100885}
886
Mark Browncd0f2d42009-04-20 16:56:59 +0100887/* Generic check to see if a widget should be powered.
888 */
889static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
890{
891 int in, out;
892
Mark Brownde02d072011-09-20 21:43:24 +0100893 DAPM_UPDATE_STAT(w, power_checks);
894
Mark Browncd0f2d42009-04-20 16:56:59 +0100895 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200896 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100897 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200898 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100899 return out != 0 && in != 0;
900}
901
Mark Brown888df392012-02-16 19:37:51 -0800902static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
903{
904 DAPM_UPDATE_STAT(w, power_checks);
905
906 return w->active;
907}
908
Mark Brown6ea31b92009-04-20 17:15:41 +0100909/* Check to see if an ADC has power */
910static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
911{
912 int in;
913
Mark Brownde02d072011-09-20 21:43:24 +0100914 DAPM_UPDATE_STAT(w, power_checks);
915
Mark Brown6ea31b92009-04-20 17:15:41 +0100916 if (w->active) {
917 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200918 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100919 return in != 0;
920 } else {
921 return dapm_generic_check_power(w);
922 }
923}
924
925/* Check to see if a DAC has power */
926static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
927{
928 int out;
929
Mark Brownde02d072011-09-20 21:43:24 +0100930 DAPM_UPDATE_STAT(w, power_checks);
931
Mark Brown6ea31b92009-04-20 17:15:41 +0100932 if (w->active) {
933 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200934 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100935 return out != 0;
936 } else {
937 return dapm_generic_check_power(w);
938 }
939}
940
Mark Brown246d0a12009-04-22 18:24:55 +0100941/* Check to see if a power supply is needed */
942static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
943{
944 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +0100945
Mark Brownde02d072011-09-20 21:43:24 +0100946 DAPM_UPDATE_STAT(w, power_checks);
947
Mark Brown246d0a12009-04-22 18:24:55 +0100948 /* Check if one of our outputs is connected */
949 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +0100950 DAPM_UPDATE_STAT(w, neighbour_checks);
951
Mark Brownbf3a9e12011-06-13 16:42:29 +0100952 if (path->weak)
953 continue;
954
Mark Brown215edda2009-09-08 18:59:05 +0100955 if (path->connected &&
956 !path->connected(path->source, path->sink))
957 continue;
958
Mark Brown30173582011-02-11 11:42:19 +0000959 if (!path->sink)
960 continue;
961
Mark Brownf68d7e12011-10-04 22:57:50 +0100962 if (dapm_widget_power_check(path->sink))
963 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +0100964 }
965
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200966 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100967
Mark Brownf68d7e12011-10-04 22:57:50 +0100968 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +0100969}
970
Mark Brown35c64bc2011-09-28 18:23:53 +0100971static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
972{
973 return 1;
974}
975
Mark Brown38357ab2009-06-06 19:03:23 +0100976static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
977 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000978 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000979{
Mark Brown828a8422011-01-15 13:14:30 +0000980 int *sort;
981
982 if (power_up)
983 sort = dapm_up_seq;
984 else
985 sort = dapm_down_seq;
986
Mark Brown38357ab2009-06-06 19:03:23 +0100987 if (sort[a->id] != sort[b->id])
988 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000989 if (a->subseq != b->subseq) {
990 if (power_up)
991 return a->subseq - b->subseq;
992 else
993 return b->subseq - a->subseq;
994 }
Mark Brownb22ead22009-06-07 12:51:26 +0100995 if (a->reg != b->reg)
996 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000997 if (a->dapm != b->dapm)
998 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000999
Mark Brown38357ab2009-06-06 19:03:23 +01001000 return 0;
1001}
Mark Brown42aa3412009-03-01 19:21:10 +00001002
Mark Brown38357ab2009-06-06 19:03:23 +01001003/* Insert a widget in order into a DAPM power sequence. */
1004static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1005 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001006 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001007{
1008 struct snd_soc_dapm_widget *w;
1009
1010 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001011 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001012 list_add_tail(&new_widget->power_list, &w->power_list);
1013 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001014 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001015
Mark Brown38357ab2009-06-06 19:03:23 +01001016 list_add_tail(&new_widget->power_list, list);
1017}
Mark Brown42aa3412009-03-01 19:21:10 +00001018
Mark Brown68f89ad2010-11-03 23:51:49 -04001019static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1020 struct snd_soc_dapm_widget *w, int event)
1021{
1022 struct snd_soc_card *card = dapm->card;
1023 const char *ev_name;
1024 int power, ret;
1025
1026 switch (event) {
1027 case SND_SOC_DAPM_PRE_PMU:
1028 ev_name = "PRE_PMU";
1029 power = 1;
1030 break;
1031 case SND_SOC_DAPM_POST_PMU:
1032 ev_name = "POST_PMU";
1033 power = 1;
1034 break;
1035 case SND_SOC_DAPM_PRE_PMD:
1036 ev_name = "PRE_PMD";
1037 power = 0;
1038 break;
1039 case SND_SOC_DAPM_POST_PMD:
1040 ev_name = "POST_PMD";
1041 power = 0;
1042 break;
1043 default:
1044 BUG();
1045 return;
1046 }
1047
1048 if (w->power != power)
1049 return;
1050
1051 if (w->event && (w->event_flags & event)) {
1052 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1053 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001054 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001055 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001056 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001057 if (ret < 0)
1058 pr_err("%s: %s event failed: %d\n",
1059 ev_name, w->name, ret);
1060 }
1061}
1062
Mark Brownb22ead22009-06-07 12:51:26 +01001063/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001064static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001065 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001066{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001067 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001068 struct snd_soc_dapm_widget *w;
1069 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001070 unsigned int value = 0;
1071 unsigned int mask = 0;
1072 unsigned int cur_mask;
1073
1074 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1075 power_list)->reg;
1076
1077 list_for_each_entry(w, pending, power_list) {
1078 cur_mask = 1 << w->shift;
1079 BUG_ON(reg != w->reg);
1080
1081 if (w->invert)
1082 power = !w->power;
1083 else
1084 power = w->power;
1085
1086 mask |= cur_mask;
1087 if (power)
1088 value |= cur_mask;
1089
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001090 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001091 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1092 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001093
Mark Brown68f89ad2010-11-03 23:51:49 -04001094 /* Check for events */
1095 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1096 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001097 }
1098
Mark Brown81628102009-06-07 13:21:24 +01001099 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001100 /* Any widget will do, they should all be updating the
1101 * same register.
1102 */
1103 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1104 power_list);
1105
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001106 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001107 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001108 value, mask, reg, card->pop_time);
1109 pop_wait(card->pop_time);
Liam Girdwood0445bdf2011-06-13 19:37:36 +01001110 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001111 }
1112
1113 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001114 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1115 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001116 }
Mark Brown42aa3412009-03-01 19:21:10 +00001117}
1118
Mark Brownb22ead22009-06-07 12:51:26 +01001119/* Apply a DAPM power sequence.
1120 *
1121 * We walk over a pre-sorted list of widgets to apply power to. In
1122 * order to minimise the number of writes to the device required
1123 * multiple widgets will be updated in a single write where possible.
1124 * Currently anything that requires more than a single write is not
1125 * handled.
1126 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001127static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001128 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001129{
1130 struct snd_soc_dapm_widget *w, *n;
1131 LIST_HEAD(pending);
1132 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001133 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001134 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001135 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001136 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001137 int *sort;
1138
1139 if (power_up)
1140 sort = dapm_up_seq;
1141 else
1142 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001143
Mark Brownb22ead22009-06-07 12:51:26 +01001144 list_for_each_entry_safe(w, n, list, power_list) {
1145 ret = 0;
1146
1147 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001148 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001149 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001150 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001151 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001152
Mark Brown474b62d2011-01-18 16:14:44 +00001153 if (cur_dapm && cur_dapm->seq_notifier) {
1154 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1155 if (sort[i] == cur_sort)
1156 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001157 i,
1158 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001159 }
1160
Mark Brownb22ead22009-06-07 12:51:26 +01001161 INIT_LIST_HEAD(&pending);
1162 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001163 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001164 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001165 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001166 }
1167
Mark Brown163cac02009-06-07 10:12:52 +01001168 switch (w->id) {
1169 case snd_soc_dapm_pre:
1170 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001171 list_for_each_entry_safe_continue(w, n, list,
1172 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001173
Mark Brownb22ead22009-06-07 12:51:26 +01001174 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001175 ret = w->event(w,
1176 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001177 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001178 ret = w->event(w,
1179 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001180 break;
1181
1182 case snd_soc_dapm_post:
1183 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001184 list_for_each_entry_safe_continue(w, n, list,
1185 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001186
Mark Brownb22ead22009-06-07 12:51:26 +01001187 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001188 ret = w->event(w,
1189 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001190 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001191 ret = w->event(w,
1192 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001193 break;
1194
Mark Brown163cac02009-06-07 10:12:52 +01001195 default:
Mark Brown81628102009-06-07 13:21:24 +01001196 /* Queue it up for application */
1197 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001198 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001199 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001200 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001201 list_move(&w->power_list, &pending);
1202 break;
Mark Brown163cac02009-06-07 10:12:52 +01001203 }
Mark Brownb22ead22009-06-07 12:51:26 +01001204
1205 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001206 dev_err(w->dapm->dev,
1207 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001208 }
Mark Brownb22ead22009-06-07 12:51:26 +01001209
1210 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001211 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001212
1213 if (cur_dapm && cur_dapm->seq_notifier) {
1214 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1215 if (sort[i] == cur_sort)
1216 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001217 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001218 }
Mark Brown163cac02009-06-07 10:12:52 +01001219}
1220
Mark Brown97404f22010-12-14 16:13:57 +00001221static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1222{
1223 struct snd_soc_dapm_update *update = dapm->update;
1224 struct snd_soc_dapm_widget *w;
1225 int ret;
1226
1227 if (!update)
1228 return;
1229
1230 w = update->widget;
1231
1232 if (w->event &&
1233 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1234 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1235 if (ret != 0)
1236 pr_err("%s DAPM pre-event failed: %d\n",
1237 w->name, ret);
1238 }
1239
1240 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1241 update->val);
1242 if (ret < 0)
1243 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1244
1245 if (w->event &&
1246 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1247 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1248 if (ret != 0)
1249 pr_err("%s DAPM post-event failed: %d\n",
1250 w->name, ret);
1251 }
1252}
1253
Mark Brown9d0624a2011-02-18 11:49:43 -08001254/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1255 * they're changing state.
1256 */
1257static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1258{
1259 struct snd_soc_dapm_context *d = data;
1260 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001261
Mark Brown56fba412011-06-04 11:25:10 +01001262 /* If we're off and we're not supposed to be go into STANDBY */
1263 if (d->bias_level == SND_SOC_BIAS_OFF &&
1264 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001265 if (d->dev)
1266 pm_runtime_get_sync(d->dev);
1267
Mark Brown9d0624a2011-02-18 11:49:43 -08001268 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1269 if (ret != 0)
1270 dev_err(d->dev,
1271 "Failed to turn on bias: %d\n", ret);
1272 }
1273
Mark Brown56fba412011-06-04 11:25:10 +01001274 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1275 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001276 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1277 if (ret != 0)
1278 dev_err(d->dev,
1279 "Failed to prepare bias: %d\n", ret);
1280 }
1281}
1282
1283/* Async callback run prior to DAPM sequences - brings to their final
1284 * state.
1285 */
1286static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1287{
1288 struct snd_soc_dapm_context *d = data;
1289 int ret;
1290
1291 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001292 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1293 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1294 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001295 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1296 if (ret != 0)
1297 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1298 ret);
1299 }
1300
1301 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001302 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1303 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001304 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1305 if (ret != 0)
1306 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001307
1308 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001309 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001310 }
1311
1312 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001313 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1314 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001315 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1316 if (ret != 0)
1317 dev_err(d->dev, "Failed to apply active bias: %d\n",
1318 ret);
1319 }
1320}
Mark Brown97404f22010-12-14 16:13:57 +00001321
Mark Brownfe4fda52011-10-03 22:36:57 +01001322static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1323 bool power, bool connect)
1324{
1325 /* If a connection is being made or broken then that update
1326 * will have marked the peer dirty, otherwise the widgets are
1327 * not connected and this update has no impact. */
1328 if (!connect)
1329 return;
1330
1331 /* If the peer is already in the state we're moving to then we
1332 * won't have an impact on it. */
1333 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001334 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001335}
1336
Mark Brown05623c42011-09-28 17:02:31 +01001337static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1338 struct list_head *up_list,
1339 struct list_head *down_list)
1340{
Mark Browndb432b42011-10-03 21:06:40 +01001341 struct snd_soc_dapm_path *path;
1342
Mark Brown05623c42011-09-28 17:02:31 +01001343 if (w->power == power)
1344 return;
1345
1346 trace_snd_soc_dapm_widget_power(w, power);
1347
Mark Browndb432b42011-10-03 21:06:40 +01001348 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001349 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001350 */
1351 list_for_each_entry(path, &w->sources, list_sink) {
1352 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001353 dapm_widget_set_peer_power(path->source, power,
1354 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001355 }
1356 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001357 switch (w->id) {
1358 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001359 case snd_soc_dapm_regulator_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001360 /* Supplies can't affect their outputs, only their inputs */
1361 break;
1362 default:
1363 list_for_each_entry(path, &w->sinks, list_source) {
1364 if (path->sink) {
1365 dapm_widget_set_peer_power(path->sink, power,
1366 path->connect);
1367 }
Mark Browndb432b42011-10-03 21:06:40 +01001368 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001369 break;
Mark Browndb432b42011-10-03 21:06:40 +01001370 }
1371
Mark Brown05623c42011-09-28 17:02:31 +01001372 if (power)
1373 dapm_seq_insert(w, up_list, true);
1374 else
1375 dapm_seq_insert(w, down_list, false);
1376
1377 w->power = power;
1378}
1379
Mark Brown7c81beb2011-09-20 22:22:32 +01001380static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1381 struct list_head *up_list,
1382 struct list_head *down_list)
1383{
Mark Brown7c81beb2011-09-20 22:22:32 +01001384 int power;
1385
1386 switch (w->id) {
1387 case snd_soc_dapm_pre:
1388 dapm_seq_insert(w, down_list, false);
1389 break;
1390 case snd_soc_dapm_post:
1391 dapm_seq_insert(w, up_list, true);
1392 break;
1393
1394 default:
Mark Brownd8050022011-09-28 18:28:23 +01001395 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001396
Mark Brown05623c42011-09-28 17:02:31 +01001397 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001398 break;
1399 }
1400}
1401
Mark Brown42aa3412009-03-01 19:21:10 +00001402/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001403 * Scan each dapm widget for complete audio path.
1404 * A complete path is a route that has valid endpoints i.e.:-
1405 *
1406 * o DAC to output pin.
1407 * o Input Pin to ADC.
1408 * o Input pin to Output pin (bypass, sidetone)
1409 * o DAC to ADC (loopback).
1410 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001411static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001412{
Mark Brown12ea2c72011-03-02 18:17:32 +00001413 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001414 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001415 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001416 LIST_HEAD(up_list);
1417 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001418 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001419 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001420
Mark Brown84e90932010-11-04 00:07:02 -04001421 trace_snd_soc_dapm_start(card);
1422
Mark Brown56fba412011-06-04 11:25:10 +01001423 list_for_each_entry(d, &card->dapm_list, list) {
1424 if (d->n_widgets || d->codec == NULL) {
1425 if (d->idle_bias_off)
1426 d->target_bias_level = SND_SOC_BIAS_OFF;
1427 else
1428 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1429 }
1430 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001431
Liam Girdwood6c120e12012-02-15 15:15:34 +00001432 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001433
Mark Brown6d3ddc82009-05-16 17:47:29 +01001434 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001435 * lists indicating if they should be powered up or down. We
1436 * only check widgets that have been flagged as dirty but note
1437 * that new widgets may be added to the dirty list while we
1438 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001439 */
Mark Browndb432b42011-10-03 21:06:40 +01001440 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001441 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001442 }
1443
Mark Brownf9de6d72011-09-28 17:19:47 +01001444 list_for_each_entry(w, &card->widgets, list) {
Mark Browndb432b42011-10-03 21:06:40 +01001445 list_del_init(&w->dirty);
1446
Mark Brownf9de6d72011-09-28 17:19:47 +01001447 if (w->power) {
1448 d = w->dapm;
1449
1450 /* Supplies and micbiases only bring the
1451 * context up to STANDBY as unless something
1452 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001453 * generally don't require full power. Signal
1454 * generators are virtual pins and have no
1455 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001456 */
1457 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001458 case snd_soc_dapm_siggen:
1459 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001460 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001461 case snd_soc_dapm_regulator_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001462 case snd_soc_dapm_micbias:
1463 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1464 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1465 break;
1466 default:
1467 d->target_bias_level = SND_SOC_BIAS_ON;
1468 break;
1469 }
1470 }
1471
1472 }
1473
Mark Brownb14b76a2009-08-17 11:55:38 +01001474 /* If there are no DAPM widgets then try to figure out power from the
1475 * event type.
1476 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001477 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001478 switch (event) {
1479 case SND_SOC_DAPM_STREAM_START:
1480 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001481 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a2009-08-17 11:55:38 +01001482 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001483 case SND_SOC_DAPM_STREAM_STOP:
Liam Girdwoode7c80e22012-01-16 15:23:31 +00001484 if (dapm->codec && dapm->codec->active)
Mark Brown56fba412011-06-04 11:25:10 +01001485 dapm->target_bias_level = SND_SOC_BIAS_ON;
1486 else
1487 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001488 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001489 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001490 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001491 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001492 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001493 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001494 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001495 default:
1496 break;
1497 }
1498 }
1499
Mark Brown85a843c2011-09-21 21:29:47 +01001500 /* Force all contexts in the card to the same bias state if
1501 * they're not ground referenced.
1502 */
Mark Brown56fba412011-06-04 11:25:10 +01001503 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001504 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001505 if (d->target_bias_level > bias)
1506 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001507 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001508 if (!d->idle_bias_off)
1509 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001510
Mark Brownde02d072011-09-20 21:43:24 +01001511 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001512
Mark Brown9d0624a2011-02-18 11:49:43 -08001513 /* Run all the bias changes in parallel */
1514 list_for_each_entry(d, &dapm->card->dapm_list, list)
1515 async_schedule_domain(dapm_pre_sequence_async, d,
1516 &async_domain);
1517 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001518
Mark Brown6d3ddc82009-05-16 17:47:29 +01001519 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001520 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001521
Mark Brown97404f22010-12-14 16:13:57 +00001522 dapm_widget_update(dapm);
1523
Mark Brown6d3ddc82009-05-16 17:47:29 +01001524 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001525 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001526
Mark Brown9d0624a2011-02-18 11:49:43 -08001527 /* Run all the bias changes in parallel */
1528 list_for_each_entry(d, &dapm->card->dapm_list, list)
1529 async_schedule_domain(dapm_post_sequence_async, d,
1530 &async_domain);
1531 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001532
Liam Girdwood8078d872012-02-15 15:15:35 +00001533 /* do we need to notify any clients that DAPM event is complete */
1534 list_for_each_entry(d, &card->dapm_list, list) {
1535 if (d->stream_event)
1536 d->stream_event(d, event);
1537 }
1538
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001539 pop_dbg(dapm->dev, card->pop_time,
1540 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001541 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001542
Mark Brown84e90932010-11-04 00:07:02 -04001543 trace_snd_soc_dapm_done(card);
1544
Mark Brown42aa3412009-03-01 19:21:10 +00001545 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001546}
1547
Mark Brown79fb9382009-08-21 16:38:13 +01001548#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001549static ssize_t dapm_widget_power_read_file(struct file *file,
1550 char __user *user_buf,
1551 size_t count, loff_t *ppos)
1552{
1553 struct snd_soc_dapm_widget *w = file->private_data;
1554 char *buf;
1555 int in, out;
1556 ssize_t ret;
1557 struct snd_soc_dapm_path *p = NULL;
1558
1559 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1560 if (!buf)
1561 return -ENOMEM;
1562
1563 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001564 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001565 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001566 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001567
Mark Brownf13ebad2012-03-03 18:01:01 +00001568 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1569 w->name, w->power ? "On" : "Off",
1570 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001571
Mark Brownd033c362009-12-04 15:25:56 +00001572 if (w->reg >= 0)
1573 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1574 " - R%d(0x%x) bit %d",
1575 w->reg, w->reg, w->shift);
1576
1577 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1578
Mark Brown3eef08b2009-09-14 16:49:00 +01001579 if (w->sname)
1580 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1581 w->sname,
1582 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001583
1584 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001585 if (p->connected && !p->connected(w, p->sink))
1586 continue;
1587
Mark Brown79fb9382009-08-21 16:38:13 +01001588 if (p->connect)
1589 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001590 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001591 p->name ? p->name : "static",
1592 p->source->name);
1593 }
1594 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001595 if (p->connected && !p->connected(w, p->sink))
1596 continue;
1597
Mark Brown79fb9382009-08-21 16:38:13 +01001598 if (p->connect)
1599 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001600 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001601 p->name ? p->name : "static",
1602 p->sink->name);
1603 }
1604
1605 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1606
1607 kfree(buf);
1608 return ret;
1609}
1610
1611static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001612 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001613 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001614 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001615};
1616
Mark Brownef49e4f2011-04-04 20:48:13 +09001617static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1618 size_t count, loff_t *ppos)
1619{
1620 struct snd_soc_dapm_context *dapm = file->private_data;
1621 char *level;
1622
1623 switch (dapm->bias_level) {
1624 case SND_SOC_BIAS_ON:
1625 level = "On\n";
1626 break;
1627 case SND_SOC_BIAS_PREPARE:
1628 level = "Prepare\n";
1629 break;
1630 case SND_SOC_BIAS_STANDBY:
1631 level = "Standby\n";
1632 break;
1633 case SND_SOC_BIAS_OFF:
1634 level = "Off\n";
1635 break;
1636 default:
1637 BUG();
1638 level = "Unknown\n";
1639 break;
1640 }
1641
1642 return simple_read_from_buffer(user_buf, count, ppos, level,
1643 strlen(level));
1644}
1645
1646static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001647 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001648 .read = dapm_bias_read_file,
1649 .llseek = default_llseek,
1650};
1651
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001652void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1653 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001654{
Mark Brown79fb9382009-08-21 16:38:13 +01001655 struct dentry *d;
1656
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001657 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1658
1659 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001660 dev_warn(dapm->dev,
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001661 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001662 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001663 }
Mark Brown79fb9382009-08-21 16:38:13 +01001664
Mark Brownef49e4f2011-04-04 20:48:13 +09001665 d = debugfs_create_file("bias_level", 0444,
1666 dapm->debugfs_dapm, dapm,
1667 &dapm_bias_fops);
1668 if (!d)
1669 dev_warn(dapm->dev,
1670 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001671}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001672
1673static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1674{
1675 struct snd_soc_dapm_context *dapm = w->dapm;
1676 struct dentry *d;
1677
1678 if (!dapm->debugfs_dapm || !w->name)
1679 return;
1680
1681 d = debugfs_create_file(w->name, 0444,
1682 dapm->debugfs_dapm, w,
1683 &dapm_widget_power_fops);
1684 if (!d)
1685 dev_warn(w->dapm->dev,
1686 "ASoC: Failed to create %s debugfs file\n",
1687 w->name);
1688}
1689
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001690static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1691{
1692 debugfs_remove_recursive(dapm->debugfs_dapm);
1693}
1694
Mark Brown79fb9382009-08-21 16:38:13 +01001695#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001696void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1697 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001698{
1699}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001700
1701static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1702{
1703}
1704
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001705static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1706{
1707}
1708
Mark Brown79fb9382009-08-21 16:38:13 +01001709#endif
1710
Richard Purdie2b97eab2006-10-06 18:32:18 +02001711/* test and update the power status of a mux widget */
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001712int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1713 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001714{
1715 struct snd_soc_dapm_path *path;
1716 int found = 0;
1717
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001718 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001719 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001720 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001721 return -ENODEV;
1722
Richard Purdie2b97eab2006-10-06 18:32:18 +02001723 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001724 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001725 if (path->kcontrol != kcontrol)
1726 continue;
1727
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001728 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001729 continue;
1730
1731 found = 1;
1732 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001733 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001734 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001735 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001736 } else {
1737 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001738 dapm_mark_dirty(path->source,
1739 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001740 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001741 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001742 }
1743
Mark Browndb432b42011-10-03 21:06:40 +01001744 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001745 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001746 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001747 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001748
1749 return 0;
1750}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001751EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001752
Milan plzik1b075e32008-01-10 14:39:46 +01001753/* test and update the power status of a mixer or switch widget */
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001754int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001755 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001756{
1757 struct snd_soc_dapm_path *path;
1758 int found = 0;
1759
Milan plzik1b075e32008-01-10 14:39:46 +01001760 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001761 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001762 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001763 return -ENODEV;
1764
Richard Purdie2b97eab2006-10-06 18:32:18 +02001765 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001766 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001767 if (path->kcontrol != kcontrol)
1768 continue;
1769
1770 /* found, now check type */
1771 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001772 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001773 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001774 }
1775
Mark Browndb432b42011-10-03 21:06:40 +01001776 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001777 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001778 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001779 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001780
1781 return 0;
1782}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001783EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001784
1785/* show dapm widget status in sys fs */
1786static ssize_t dapm_widget_show(struct device *dev,
1787 struct device_attribute *attr, char *buf)
1788{
Mark Brown36ae1a92012-01-06 17:12:45 -08001789 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001790 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001791 struct snd_soc_dapm_widget *w;
1792 int count = 0;
1793 char *state = "not set";
1794
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001795 list_for_each_entry(w, &codec->card->widgets, list) {
1796 if (w->dapm != &codec->dapm)
1797 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001798
1799 /* only display widgets that burnm power */
1800 switch (w->id) {
1801 case snd_soc_dapm_hp:
1802 case snd_soc_dapm_mic:
1803 case snd_soc_dapm_spk:
1804 case snd_soc_dapm_line:
1805 case snd_soc_dapm_micbias:
1806 case snd_soc_dapm_dac:
1807 case snd_soc_dapm_adc:
1808 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001809 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001810 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001811 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001812 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001813 case snd_soc_dapm_regulator_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001814 if (w->name)
1815 count += sprintf(buf + count, "%s: %s\n",
1816 w->name, w->power ? "On":"Off");
1817 break;
1818 default:
1819 break;
1820 }
1821 }
1822
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001823 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001824 case SND_SOC_BIAS_ON:
1825 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001826 break;
Mark Brown0be98982008-05-19 12:31:28 +02001827 case SND_SOC_BIAS_PREPARE:
1828 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001829 break;
Mark Brown0be98982008-05-19 12:31:28 +02001830 case SND_SOC_BIAS_STANDBY:
1831 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001832 break;
Mark Brown0be98982008-05-19 12:31:28 +02001833 case SND_SOC_BIAS_OFF:
1834 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001835 break;
1836 }
1837 count += sprintf(buf + count, "PM State: %s\n", state);
1838
1839 return count;
1840}
1841
1842static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1843
1844int snd_soc_dapm_sys_add(struct device *dev)
1845{
Troy Kisky12ef1932008-10-13 17:42:14 -07001846 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001847}
1848
1849static void snd_soc_dapm_sys_remove(struct device *dev)
1850{
Mark Brownaef90842009-05-16 17:53:16 +01001851 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001852}
1853
1854/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001855static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001856{
1857 struct snd_soc_dapm_widget *w, *next_w;
1858 struct snd_soc_dapm_path *p, *next_p;
1859
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001860 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1861 if (w->dapm != dapm)
1862 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001863 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001864 /*
1865 * remove source and sink paths associated to this widget.
1866 * While removing the path, remove reference to it from both
1867 * source and sink widgets so that path is removed only once.
1868 */
1869 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1870 list_del(&p->list_sink);
1871 list_del(&p->list_source);
1872 list_del(&p->list);
1873 kfree(p->long_name);
1874 kfree(p);
1875 }
1876 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1877 list_del(&p->list_sink);
1878 list_del(&p->list_source);
1879 list_del(&p->list);
1880 kfree(p->long_name);
1881 kfree(p);
1882 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001883 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001884 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001885 kfree(w);
1886 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001887}
1888
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001889static struct snd_soc_dapm_widget *dapm_find_widget(
1890 struct snd_soc_dapm_context *dapm, const char *pin,
1891 bool search_other_contexts)
1892{
1893 struct snd_soc_dapm_widget *w;
1894 struct snd_soc_dapm_widget *fallback = NULL;
1895
1896 list_for_each_entry(w, &dapm->card->widgets, list) {
1897 if (!strcmp(w->name, pin)) {
1898 if (w->dapm == dapm)
1899 return w;
1900 else
1901 fallback = w;
1902 }
1903 }
1904
1905 if (search_other_contexts)
1906 return fallback;
1907
1908 return NULL;
1909}
1910
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001911static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001912 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001913{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001914 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001915
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001916 if (!w) {
1917 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1918 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001919 }
1920
Mark Brown1a8b2d92012-02-16 11:50:07 -08001921 if (w->connected != status)
1922 dapm_mark_dirty(w, "pin configuration");
1923
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001924 w->connected = status;
1925 if (status == 0)
1926 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001927
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001928 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001929}
1930
Richard Purdie2b97eab2006-10-06 18:32:18 +02001931/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001932 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001933 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001934 *
1935 * Walks all dapm audio paths and powers widgets according to their
1936 * stream or path usage.
1937 *
1938 * Returns 0 for success.
1939 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001940int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001941{
Mark Brown4f4c0072011-10-07 14:29:19 +01001942 /*
1943 * Suppress early reports (eg, jacks syncing their state) to avoid
1944 * silly DAPM runs during card startup.
1945 */
1946 if (!dapm->card || !dapm->card->instantiated)
1947 return 0;
1948
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001949 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001950}
Liam Girdwooda5302182008-07-07 13:35:17 +01001951EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001952
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001953static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001954 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001955{
1956 struct snd_soc_dapm_path *path;
1957 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001958 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001959 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001960 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001961 const char *source;
1962 char prefixed_sink[80];
1963 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001964 int ret = 0;
1965
Mark Brown88e8b9a2011-03-02 18:18:24 +00001966 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001967 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1968 dapm->codec->name_prefix, route->sink);
1969 sink = prefixed_sink;
1970 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1971 dapm->codec->name_prefix, route->source);
1972 source = prefixed_source;
1973 } else {
1974 sink = route->sink;
1975 source = route->source;
1976 }
1977
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001978 /*
1979 * find src and dest widgets over all widgets but favor a widget from
1980 * current DAPM context
1981 */
1982 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001983 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001984 wtsink = w;
1985 if (w->dapm == dapm)
1986 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001987 continue;
1988 }
1989 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001990 wtsource = w;
1991 if (w->dapm == dapm)
1992 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001993 }
1994 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001995 /* use widget from another DAPM context if not found from this */
1996 if (!wsink)
1997 wsink = wtsink;
1998 if (!wsource)
1999 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002000
2001 if (wsource == NULL || wsink == NULL)
2002 return -ENODEV;
2003
2004 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2005 if (!path)
2006 return -ENOMEM;
2007
2008 path->source = wsource;
2009 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002010 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002011 INIT_LIST_HEAD(&path->list);
2012 INIT_LIST_HEAD(&path->list_source);
2013 INIT_LIST_HEAD(&path->list_sink);
2014
2015 /* check for external widgets */
2016 if (wsink->id == snd_soc_dapm_input) {
2017 if (wsource->id == snd_soc_dapm_micbias ||
2018 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002019 wsource->id == snd_soc_dapm_line ||
2020 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002021 wsink->ext = 1;
2022 }
2023 if (wsource->id == snd_soc_dapm_output) {
2024 if (wsink->id == snd_soc_dapm_spk ||
2025 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002026 wsink->id == snd_soc_dapm_line ||
2027 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002028 wsource->ext = 1;
2029 }
2030
2031 /* connect static paths */
2032 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002033 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002034 list_add(&path->list_sink, &wsink->sources);
2035 list_add(&path->list_source, &wsource->sinks);
2036 path->connect = 1;
2037 return 0;
2038 }
2039
2040 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002041 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002042 case snd_soc_dapm_adc:
2043 case snd_soc_dapm_dac:
2044 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002045 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002046 case snd_soc_dapm_input:
2047 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002048 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002049 case snd_soc_dapm_micbias:
2050 case snd_soc_dapm_vmid:
2051 case snd_soc_dapm_pre:
2052 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002053 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002054 case snd_soc_dapm_regulator_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002055 case snd_soc_dapm_aif_in:
2056 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -08002057 case snd_soc_dapm_dai:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002058 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002059 list_add(&path->list_sink, &wsink->sources);
2060 list_add(&path->list_source, &wsource->sinks);
2061 path->connect = 1;
2062 return 0;
2063 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002064 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002065 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002066 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002067 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002068 if (ret != 0)
2069 goto err;
2070 break;
2071 case snd_soc_dapm_switch:
2072 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002073 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002074 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002075 if (ret != 0)
2076 goto err;
2077 break;
2078 case snd_soc_dapm_hp:
2079 case snd_soc_dapm_mic:
2080 case snd_soc_dapm_line:
2081 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002082 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002083 list_add(&path->list_sink, &wsink->sources);
2084 list_add(&path->list_source, &wsource->sinks);
2085 path->connect = 0;
2086 return 0;
2087 }
2088 return 0;
2089
2090err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002091 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2092 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002093 kfree(path);
2094 return ret;
2095}
Mark Brown105f1c22008-05-13 14:52:19 +02002096
2097/**
Mark Brown105f1c22008-05-13 14:52:19 +02002098 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002099 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002100 * @route: audio routes
2101 * @num: number of routes
2102 *
2103 * Connects 2 dapm widgets together via a named audio path. The sink is
2104 * the widget receiving the audio signal, whilst the source is the sender
2105 * of the audio signal.
2106 *
2107 * Returns 0 for success else error. On error all resources can be freed
2108 * with a call to snd_soc_card_free().
2109 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002110int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002111 const struct snd_soc_dapm_route *route, int num)
2112{
2113 int i, ret;
2114
2115 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002116 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02002117 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002118 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2119 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02002120 return ret;
2121 }
2122 route++;
2123 }
2124
2125 return 0;
2126}
2127EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2128
Mark Brownbf3a9e12011-06-13 16:42:29 +01002129static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2130 const struct snd_soc_dapm_route *route)
2131{
2132 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2133 route->source,
2134 true);
2135 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2136 route->sink,
2137 true);
2138 struct snd_soc_dapm_path *path;
2139 int count = 0;
2140
2141 if (!source) {
2142 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2143 route->source);
2144 return -ENODEV;
2145 }
2146
2147 if (!sink) {
2148 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2149 route->sink);
2150 return -ENODEV;
2151 }
2152
2153 if (route->control || route->connected)
2154 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2155 route->source, route->sink);
2156
2157 list_for_each_entry(path, &source->sinks, list_source) {
2158 if (path->sink == sink) {
2159 path->weak = 1;
2160 count++;
2161 }
2162 }
2163
2164 if (count == 0)
2165 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2166 route->source, route->sink);
2167 if (count > 1)
2168 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2169 count, route->source, route->sink);
2170
2171 return 0;
2172}
2173
2174/**
2175 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2176 * @dapm: DAPM context
2177 * @route: audio routes
2178 * @num: number of routes
2179 *
2180 * Mark existing routes matching those specified in the passed array
2181 * as being weak, meaning that they are ignored for the purpose of
2182 * power decisions. The main intended use case is for sidetone paths
2183 * which couple audio between other independent paths if they are both
2184 * active in order to make the combination work better at the user
2185 * level but which aren't intended to be "used".
2186 *
2187 * Note that CODEC drivers should not use this as sidetone type paths
2188 * can frequently also be used as bypass paths.
2189 */
2190int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2191 const struct snd_soc_dapm_route *route, int num)
2192{
2193 int i, err;
2194 int ret = 0;
2195
2196 for (i = 0; i < num; i++) {
2197 err = snd_soc_dapm_weak_route(dapm, route);
2198 if (err)
2199 ret = err;
2200 route++;
2201 }
2202
2203 return ret;
2204}
2205EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2206
Mark Brown105f1c22008-05-13 14:52:19 +02002207/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002208 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002209 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002210 *
2211 * Checks the codec for any new dapm widgets and creates them if found.
2212 *
2213 * Returns 0 for success.
2214 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002215int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002216{
2217 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002218 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002219
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002220 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002221 {
2222 if (w->new)
2223 continue;
2224
Stephen Warrenfad59882011-04-28 17:37:59 -06002225 if (w->num_kcontrols) {
2226 w->kcontrols = kzalloc(w->num_kcontrols *
2227 sizeof(struct snd_kcontrol *),
2228 GFP_KERNEL);
2229 if (!w->kcontrols)
2230 return -ENOMEM;
2231 }
2232
Richard Purdie2b97eab2006-10-06 18:32:18 +02002233 switch(w->id) {
2234 case snd_soc_dapm_switch:
2235 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002236 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002237 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002238 break;
2239 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002240 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002241 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002242 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002243 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002245 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002246 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002247 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002248 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002249 break;
2250 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002251
2252 /* Read the initial power state from the device */
2253 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002254 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002255 val &= 1 << w->shift;
2256 if (w->invert)
2257 val = !val;
2258
2259 if (val)
2260 w->power = 1;
2261 }
2262
Richard Purdie2b97eab2006-10-06 18:32:18 +02002263 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002264
Mark Brown7508b122011-10-05 12:09:12 +01002265 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002266 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002267 }
2268
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002269 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002270 return 0;
2271}
2272EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2273
2274/**
2275 * snd_soc_dapm_get_volsw - dapm mixer get callback
2276 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002277 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002278 *
2279 * Callback to get the value of a dapm mixer control.
2280 *
2281 * Returns 0 for success.
2282 */
2283int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2284 struct snd_ctl_elem_value *ucontrol)
2285{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002286 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2287 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002288 struct soc_mixer_control *mc =
2289 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002290 unsigned int reg = mc->reg;
2291 unsigned int shift = mc->shift;
2292 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002293 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002294 unsigned int invert = mc->invert;
2295 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002296
Richard Purdie2b97eab2006-10-06 18:32:18 +02002297 ucontrol->value.integer.value[0] =
2298 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2299 if (shift != rshift)
2300 ucontrol->value.integer.value[1] =
2301 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2302 if (invert) {
2303 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002304 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002305 if (shift != rshift)
2306 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002307 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002308 }
2309
2310 return 0;
2311}
2312EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2313
2314/**
2315 * snd_soc_dapm_put_volsw - dapm mixer set callback
2316 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002317 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002318 *
2319 * Callback to set the value of a dapm mixer control.
2320 *
2321 * Returns 0 for success.
2322 */
2323int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2324 struct snd_ctl_elem_value *ucontrol)
2325{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002326 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2327 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2328 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002329 struct soc_mixer_control *mc =
2330 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002331 unsigned int reg = mc->reg;
2332 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002333 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002334 unsigned int mask = (1 << fls(max)) - 1;
2335 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002336 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002337 int connect, change;
2338 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002339 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002340
2341 val = (ucontrol->value.integer.value[0] & mask);
2342
2343 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002344 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002345 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002346 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002347
Stephen Warrenfafd2172011-04-28 17:38:00 -06002348 if (val)
2349 /* new connection */
2350 connect = invert ? 0 : 1;
2351 else
2352 /* old connection must be powered down */
2353 connect = invert ? 1 : 0;
2354
2355 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002356
Stephen Warrene9cf7042011-01-27 14:54:05 -07002357 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002358 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002359 for (wi = 0; wi < wlist->num_widgets; wi++) {
2360 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002361
Stephen Warrenfafd2172011-04-28 17:38:00 -06002362 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002363
Stephen Warrenfafd2172011-04-28 17:38:00 -06002364 update.kcontrol = kcontrol;
2365 update.widget = widget;
2366 update.reg = reg;
2367 update.mask = mask;
2368 update.val = val;
2369 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002370
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002371 snd_soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002372
2373 widget->dapm->update = NULL;
2374 }
Mark Brown283375c2009-12-07 18:09:03 +00002375 }
2376
Stephen Warrenfafd2172011-04-28 17:38:00 -06002377 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002378 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002379}
2380EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2381
2382/**
2383 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2384 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002385 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002386 *
2387 * Callback to get the value of a dapm enumerated double mixer control.
2388 *
2389 * Returns 0 for success.
2390 */
2391int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2392 struct snd_ctl_elem_value *ucontrol)
2393{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002394 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2395 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002396 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002397 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002398
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002399 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002400 ;
2401 val = snd_soc_read(widget->codec, e->reg);
2402 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2403 if (e->shift_l != e->shift_r)
2404 ucontrol->value.enumerated.item[1] =
2405 (val >> e->shift_r) & (bitmask - 1);
2406
2407 return 0;
2408}
2409EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2410
2411/**
2412 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2413 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002414 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002415 *
2416 * Callback to set the value of a dapm enumerated double mixer control.
2417 *
2418 * Returns 0 for success.
2419 */
2420int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2421 struct snd_ctl_elem_value *ucontrol)
2422{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002423 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2424 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2425 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002426 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002427 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002428 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002429 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002430 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002431
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002432 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002433 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002434 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002435 return -EINVAL;
2436 mux = ucontrol->value.enumerated.item[0];
2437 val = mux << e->shift_l;
2438 mask = (bitmask - 1) << e->shift_l;
2439 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002440 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002441 return -EINVAL;
2442 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2443 mask |= (bitmask - 1) << e->shift_r;
2444 }
2445
Stephen Warrenfafd2172011-04-28 17:38:00 -06002446 mutex_lock(&codec->mutex);
2447
Mark Brown3a655772009-10-05 17:23:30 +01002448 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002449 if (change) {
2450 for (wi = 0; wi < wlist->num_widgets; wi++) {
2451 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002452
Stephen Warrenfafd2172011-04-28 17:38:00 -06002453 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002454
Stephen Warrenfafd2172011-04-28 17:38:00 -06002455 update.kcontrol = kcontrol;
2456 update.widget = widget;
2457 update.reg = e->reg;
2458 update.mask = mask;
2459 update.val = val;
2460 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002461
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002462 snd_soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002463
Stephen Warrenfafd2172011-04-28 17:38:00 -06002464 widget->dapm->update = NULL;
2465 }
2466 }
2467
2468 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002469 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002470}
2471EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2472
2473/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002474 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2475 * @kcontrol: mixer control
2476 * @ucontrol: control element information
2477 *
2478 * Returns 0 for success.
2479 */
2480int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2481 struct snd_ctl_elem_value *ucontrol)
2482{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002483 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2484 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002485
2486 ucontrol->value.enumerated.item[0] = widget->value;
2487
2488 return 0;
2489}
2490EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2491
2492/**
2493 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2494 * @kcontrol: mixer control
2495 * @ucontrol: control element information
2496 *
2497 * Returns 0 for success.
2498 */
2499int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2500 struct snd_ctl_elem_value *ucontrol)
2501{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002502 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2503 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2504 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002505 struct soc_enum *e =
2506 (struct soc_enum *)kcontrol->private_value;
2507 int change;
2508 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002509 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002510
2511 if (ucontrol->value.enumerated.item[0] >= e->max)
2512 return -EINVAL;
2513
Stephen Warrenfafd2172011-04-28 17:38:00 -06002514 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002515
2516 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002517 if (change) {
2518 for (wi = 0; wi < wlist->num_widgets; wi++) {
2519 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002520
Stephen Warrenfafd2172011-04-28 17:38:00 -06002521 widget->value = ucontrol->value.enumerated.item[0];
2522
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002523 snd_soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002524 }
2525 }
2526
2527 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002528 return ret;
2529}
2530EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2531
2532/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002533 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2534 * callback
2535 * @kcontrol: mixer control
2536 * @ucontrol: control element information
2537 *
2538 * Callback to get the value of a dapm semi enumerated double mixer control.
2539 *
2540 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2541 * used for handling bitfield coded enumeration for example.
2542 *
2543 * Returns 0 for success.
2544 */
2545int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2546 struct snd_ctl_elem_value *ucontrol)
2547{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002548 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2549 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002550 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002551 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002552
2553 reg_val = snd_soc_read(widget->codec, e->reg);
2554 val = (reg_val >> e->shift_l) & e->mask;
2555 for (mux = 0; mux < e->max; mux++) {
2556 if (val == e->values[mux])
2557 break;
2558 }
2559 ucontrol->value.enumerated.item[0] = mux;
2560 if (e->shift_l != e->shift_r) {
2561 val = (reg_val >> e->shift_r) & e->mask;
2562 for (mux = 0; mux < e->max; mux++) {
2563 if (val == e->values[mux])
2564 break;
2565 }
2566 ucontrol->value.enumerated.item[1] = mux;
2567 }
2568
2569 return 0;
2570}
2571EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2572
2573/**
2574 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2575 * callback
2576 * @kcontrol: mixer control
2577 * @ucontrol: control element information
2578 *
2579 * Callback to set the value of a dapm semi enumerated double mixer control.
2580 *
2581 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2582 * used for handling bitfield coded enumeration for example.
2583 *
2584 * Returns 0 for success.
2585 */
2586int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2587 struct snd_ctl_elem_value *ucontrol)
2588{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002589 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2590 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2591 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002592 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002593 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002594 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002595 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002596 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002597
2598 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2599 return -EINVAL;
2600 mux = ucontrol->value.enumerated.item[0];
2601 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2602 mask = e->mask << e->shift_l;
2603 if (e->shift_l != e->shift_r) {
2604 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2605 return -EINVAL;
2606 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2607 mask |= e->mask << e->shift_r;
2608 }
2609
Stephen Warrenfafd2172011-04-28 17:38:00 -06002610 mutex_lock(&codec->mutex);
2611
Mark Brown3a655772009-10-05 17:23:30 +01002612 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002613 if (change) {
2614 for (wi = 0; wi < wlist->num_widgets; wi++) {
2615 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002616
Stephen Warrenfafd2172011-04-28 17:38:00 -06002617 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002618
Stephen Warrenfafd2172011-04-28 17:38:00 -06002619 update.kcontrol = kcontrol;
2620 update.widget = widget;
2621 update.reg = e->reg;
2622 update.mask = mask;
2623 update.val = val;
2624 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002625
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002626 snd_soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002627
Stephen Warrenfafd2172011-04-28 17:38:00 -06002628 widget->dapm->update = NULL;
2629 }
2630 }
2631
2632 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002633 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002634}
2635EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2636
2637/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002638 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2639 *
2640 * @kcontrol: mixer control
2641 * @uinfo: control element information
2642 *
2643 * Callback to provide information about a pin switch control.
2644 */
2645int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2646 struct snd_ctl_elem_info *uinfo)
2647{
2648 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2649 uinfo->count = 1;
2650 uinfo->value.integer.min = 0;
2651 uinfo->value.integer.max = 1;
2652
2653 return 0;
2654}
2655EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2656
2657/**
2658 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2659 *
2660 * @kcontrol: mixer control
2661 * @ucontrol: Value
2662 */
2663int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2664 struct snd_ctl_elem_value *ucontrol)
2665{
Mark Brown48a8c392012-02-14 17:11:15 -08002666 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002667 const char *pin = (const char *)kcontrol->private_value;
2668
Mark Brown48a8c392012-02-14 17:11:15 -08002669 mutex_lock(&card->mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002670
2671 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002672 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002673
Mark Brown48a8c392012-02-14 17:11:15 -08002674 mutex_unlock(&card->mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002675
2676 return 0;
2677}
2678EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2679
2680/**
2681 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2682 *
2683 * @kcontrol: mixer control
2684 * @ucontrol: Value
2685 */
2686int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2687 struct snd_ctl_elem_value *ucontrol)
2688{
Mark Brown48a8c392012-02-14 17:11:15 -08002689 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002690 const char *pin = (const char *)kcontrol->private_value;
2691
Mark Brown48a8c392012-02-14 17:11:15 -08002692 mutex_lock(&card->mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002693
2694 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002695 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002696 else
Mark Brown48a8c392012-02-14 17:11:15 -08002697 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002698
Mark Brown48a8c392012-02-14 17:11:15 -08002699 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002700
Mark Brown48a8c392012-02-14 17:11:15 -08002701 mutex_unlock(&card->mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002702
2703 return 0;
2704}
2705EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2706
Mark Brown5ba06fc2012-02-16 11:07:13 -08002707static struct snd_soc_dapm_widget *
2708snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2709 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002710{
2711 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002712 size_t name_len;
Mark Brown62ea8742012-01-21 21:14:48 +00002713 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002714
2715 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08002716 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002717
Mark Brown62ea8742012-01-21 21:14:48 +00002718 switch (w->id) {
2719 case snd_soc_dapm_regulator_supply:
2720 w->priv = devm_regulator_get(dapm->dev, w->name);
2721 if (IS_ERR(w->priv)) {
2722 ret = PTR_ERR(w->priv);
2723 dev_err(dapm->dev, "Failed to request %s: %d\n",
2724 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002725 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00002726 }
2727 break;
2728 default:
2729 break;
2730 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002731
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002732 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002733 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002734 name_len += 1 + strlen(dapm->codec->name_prefix);
2735 w->name = kmalloc(name_len, GFP_KERNEL);
2736 if (w->name == NULL) {
2737 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002738 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002739 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002740 if (dapm->codec && dapm->codec->name_prefix)
Mark Brown888df392012-02-16 19:37:51 -08002741 snprintf((char *)w->name, name_len, "%s %s",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002742 dapm->codec->name_prefix, widget->name);
2743 else
Mark Brown888df392012-02-16 19:37:51 -08002744 snprintf((char *)w->name, name_len, "%s", widget->name);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002745
Mark Brown7ca3a182011-10-08 14:04:50 +01002746 switch (w->id) {
2747 case snd_soc_dapm_switch:
2748 case snd_soc_dapm_mixer:
2749 case snd_soc_dapm_mixer_named_ctl:
2750 w->power_check = dapm_generic_check_power;
2751 break;
2752 case snd_soc_dapm_mux:
2753 case snd_soc_dapm_virt_mux:
2754 case snd_soc_dapm_value_mux:
2755 w->power_check = dapm_generic_check_power;
2756 break;
2757 case snd_soc_dapm_adc:
2758 case snd_soc_dapm_aif_out:
2759 w->power_check = dapm_adc_check_power;
2760 break;
2761 case snd_soc_dapm_dac:
2762 case snd_soc_dapm_aif_in:
2763 w->power_check = dapm_dac_check_power;
2764 break;
2765 case snd_soc_dapm_pga:
2766 case snd_soc_dapm_out_drv:
2767 case snd_soc_dapm_input:
2768 case snd_soc_dapm_output:
2769 case snd_soc_dapm_micbias:
2770 case snd_soc_dapm_spk:
2771 case snd_soc_dapm_hp:
2772 case snd_soc_dapm_mic:
2773 case snd_soc_dapm_line:
2774 w->power_check = dapm_generic_check_power;
2775 break;
2776 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002777 case snd_soc_dapm_regulator_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01002778 w->power_check = dapm_supply_check_power;
2779 break;
Mark Brown888df392012-02-16 19:37:51 -08002780 case snd_soc_dapm_dai:
2781 w->power_check = dapm_dai_check_power;
2782 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002783 default:
2784 w->power_check = dapm_always_on_check_power;
2785 break;
2786 }
2787
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002788 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002789 w->dapm = dapm;
2790 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002791 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002792 INIT_LIST_HEAD(&w->sources);
2793 INIT_LIST_HEAD(&w->sinks);
2794 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01002795 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002796 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002797
2798 /* machine layer set ups unconnected pins and insertions */
2799 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08002800 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002801}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002802
2803/**
Mark Brown4ba13272008-05-13 14:51:19 +02002804 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002805 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002806 * @widget: widget array
2807 * @num: number of widgets
2808 *
2809 * Creates new DAPM controls based upon the templates.
2810 *
2811 * Returns 0 for success else error.
2812 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002813int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002814 const struct snd_soc_dapm_widget *widget,
2815 int num)
2816{
Mark Brown5ba06fc2012-02-16 11:07:13 -08002817 struct snd_soc_dapm_widget *w;
2818 int i;
Mark Brown4ba13272008-05-13 14:51:19 +02002819
2820 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08002821 w = snd_soc_dapm_new_control(dapm, widget);
2822 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002823 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08002824 "ASoC: Failed to create DAPM control %s\n",
2825 widget->name);
2826 return -ENOMEM;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002827 }
Mark Brown4ba13272008-05-13 14:51:19 +02002828 widget++;
2829 }
2830 return 0;
2831}
2832EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2833
Mark Brown888df392012-02-16 19:37:51 -08002834int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
2835 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002836{
Mark Brown888df392012-02-16 19:37:51 -08002837 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002838 struct snd_soc_dapm_widget *w;
2839
Mark Brown888df392012-02-16 19:37:51 -08002840 WARN_ON(dapm->dev != dai->dev);
2841
2842 memset(&template, 0, sizeof(template));
2843 template.reg = SND_SOC_NOPM;
2844
2845 if (dai->driver->playback.stream_name) {
2846 template.id = snd_soc_dapm_dai;
2847 template.name = dai->driver->playback.stream_name;
2848 template.sname = dai->driver->playback.stream_name;
2849
2850 dev_dbg(dai->dev, "adding %s widget\n",
2851 template.name);
2852
2853 w = snd_soc_dapm_new_control(dapm, &template);
2854 if (!w) {
2855 dev_err(dapm->dev, "Failed to create %s widget\n",
2856 dai->driver->playback.stream_name);
2857 }
2858
2859 w->priv = dai;
2860 dai->playback_widget = w;
2861 }
2862
2863 if (dai->driver->capture.stream_name) {
2864 template.id = snd_soc_dapm_dai;
2865 template.name = dai->driver->capture.stream_name;
2866 template.sname = dai->driver->capture.stream_name;
2867
2868 dev_dbg(dai->dev, "adding %s widget\n",
2869 template.name);
2870
2871 w = snd_soc_dapm_new_control(dapm, &template);
2872 if (!w) {
2873 dev_err(dapm->dev, "Failed to create %s widget\n",
2874 dai->driver->capture.stream_name);
2875 }
2876
2877 w->priv = dai;
2878 dai->capture_widget = w;
2879 }
2880
2881 return 0;
2882}
2883
2884int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
2885{
2886 struct snd_soc_dapm_widget *dai_w, *w;
2887 struct snd_soc_dai *dai;
2888 struct snd_soc_dapm_route r;
2889
2890 memset(&r, 0, sizeof(r));
2891
2892 /* For each DAI widget... */
2893 list_for_each_entry(dai_w, &card->widgets, list) {
2894 if (dai_w->id != snd_soc_dapm_dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002895 continue;
Mark Brown888df392012-02-16 19:37:51 -08002896
2897 dai = dai_w->priv;
2898
2899 /* ...find all widgets with the same stream and link them */
2900 list_for_each_entry(w, &card->widgets, list) {
2901 if (w->dapm != dai_w->dapm)
2902 continue;
2903
2904 if (w->id == snd_soc_dapm_dai)
2905 continue;
2906
2907 if (!w->sname)
2908 continue;
2909
2910 if (dai->driver->playback.stream_name &&
2911 strstr(w->sname,
2912 dai->driver->playback.stream_name)) {
2913 r.source = dai->playback_widget->name;
2914 r.sink = w->name;
2915 dev_dbg(dai->dev, "%s -> %s\n",
2916 r.source, r.sink);
2917
2918 snd_soc_dapm_add_route(w->dapm, &r);
2919 }
2920
2921 if (dai->driver->capture.stream_name &&
2922 strstr(w->sname,
2923 dai->driver->capture.stream_name)) {
2924 r.source = w->name;
2925 r.sink = dai->capture_widget->name;
2926 dev_dbg(dai->dev, "%s -> %s\n",
2927 r.source, r.sink);
2928
2929 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002930 }
2931 }
2932 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002933
Mark Brown888df392012-02-16 19:37:51 -08002934 return 0;
2935}
Liam Girdwood64a648c2011-07-25 11:15:15 +01002936
Richard Purdie2b97eab2006-10-06 18:32:18 +02002937static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002938 int stream, struct snd_soc_dai *dai,
2939 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002940{
2941 struct snd_soc_dapm_widget *w;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002942
2943 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Mark Brownfe360682012-02-16 19:43:20 -08002944 w = dai->playback_widget;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002945 else
Mark Brownfe360682012-02-16 19:43:20 -08002946 w = dai->capture_widget;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002947
Mark Brownfe360682012-02-16 19:43:20 -08002948 if (!w)
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002949 return;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002950
Mark Brownfe360682012-02-16 19:43:20 -08002951 dapm_mark_dirty(w, "stream event");
2952
2953 switch (event) {
2954 case SND_SOC_DAPM_STREAM_START:
2955 w->active = 1;
2956 break;
2957 case SND_SOC_DAPM_STREAM_STOP:
2958 w->active = 0;
2959 break;
2960 case SND_SOC_DAPM_STREAM_SUSPEND:
2961 case SND_SOC_DAPM_STREAM_RESUME:
2962 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2963 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2964 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002965 }
2966
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002967 dapm_power_widgets(dapm, event);
2968}
2969
2970/**
2971 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2972 * @rtd: PCM runtime data
2973 * @stream: stream name
2974 * @event: stream event
2975 *
2976 * Sends a stream event to the dapm core. The core then makes any
2977 * necessary widget power changes.
2978 *
2979 * Returns 0 for success else error.
2980 */
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002981int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
2982 struct snd_soc_dai *dai, int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002983{
2984 struct snd_soc_codec *codec = rtd->codec;
2985
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002986 mutex_lock(&codec->mutex);
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002987 soc_dapm_stream_event(&codec->dapm, stream, dai, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002988 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002989 return 0;
2990}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002991
2992/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002993 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002994 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002995 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002996 *
Mark Brown74b8f952009-06-06 11:26:15 +01002997 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002998 * a valid audio route and active audio stream.
2999 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3000 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003001 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003002int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003003{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003004 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003005}
Liam Girdwooda5302182008-07-07 13:35:17 +01003006EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003007
3008/**
Mark Brownda341832010-03-15 19:23:37 +00003009 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003010 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003011 * @pin: pin name
3012 *
3013 * Enables input/output pin regardless of any other state. This is
3014 * intended for use with microphone bias supplies used in microphone
3015 * jack detection.
3016 *
3017 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3018 * do any widget power switching.
3019 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003020int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3021 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003022{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003023 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003024
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003025 if (!w) {
3026 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3027 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003028 }
3029
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003030 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3031 w->connected = 1;
3032 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003033 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003034
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003035 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003036}
3037EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3038
3039/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003040 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003041 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003042 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003043 *
Mark Brown74b8f952009-06-06 11:26:15 +01003044 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003045 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3046 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003047 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003048int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3049 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003050{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003051 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003052}
3053EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3054
3055/**
Mark Brown5817b522008-09-24 11:23:11 +01003056 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003057 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003058 * @pin: pin name
3059 *
3060 * Marks the specified pin as being not connected, disabling it along
3061 * any parent or child widgets. At present this is identical to
3062 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3063 * additional things such as disabling controls which only affect
3064 * paths through the pin.
3065 *
3066 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3067 * do any widget power switching.
3068 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003069int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003070{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003071 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003072}
3073EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3074
3075/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003076 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003077 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003078 * @pin: audio signal pin endpoint (or start point)
3079 *
3080 * Get audio pin status - connected or disconnected.
3081 *
3082 * Returns 1 for connected otherwise 0.
3083 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003084int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3085 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003086{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003087 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003088
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003089 if (w)
3090 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003091
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003092 return 0;
3093}
Liam Girdwooda5302182008-07-07 13:35:17 +01003094EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003095
3096/**
Mark Brown1547aba2010-05-07 21:11:40 +01003097 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003098 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003099 * @pin: audio signal pin endpoint (or start point)
3100 *
3101 * Mark the given endpoint or pin as ignoring suspend. When the
3102 * system is disabled a path between two endpoints flagged as ignoring
3103 * suspend will not be disabled. The path must already be enabled via
3104 * normal means at suspend time, it will not be turned on if it was not
3105 * already enabled.
3106 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003107int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3108 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003109{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003110 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003111
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003112 if (!w) {
3113 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3114 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003115 }
3116
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003117 w->ignore_suspend = 1;
3118
3119 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003120}
3121EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3122
Stephen Warren16332812011-11-23 12:42:04 -07003123static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3124 struct snd_soc_dapm_widget *w)
3125{
3126 struct snd_soc_dapm_path *p;
3127
3128 list_for_each_entry(p, &card->paths, list) {
3129 if ((p->source == w) || (p->sink == w)) {
3130 dev_dbg(card->dev,
3131 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3132 p->source->name, p->source->id, p->source->dapm,
3133 p->sink->name, p->sink->id, p->sink->dapm);
3134
3135 /* Connected to something other than the codec */
3136 if (p->source->dapm != p->sink->dapm)
3137 return true;
3138 /*
3139 * Loopback connection from codec external pin to
3140 * codec external pin
3141 */
3142 if (p->sink->id == snd_soc_dapm_input) {
3143 switch (p->source->id) {
3144 case snd_soc_dapm_output:
3145 case snd_soc_dapm_micbias:
3146 return true;
3147 default:
3148 break;
3149 }
3150 }
3151 }
3152 }
3153
3154 return false;
3155}
3156
3157/**
3158 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3159 * @codec: The codec whose pins should be processed
3160 *
3161 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3162 * which are unused. Pins are used if they are connected externally to the
3163 * codec, whether that be to some other device, or a loop-back connection to
3164 * the codec itself.
3165 */
3166void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3167{
3168 struct snd_soc_card *card = codec->card;
3169 struct snd_soc_dapm_context *dapm = &codec->dapm;
3170 struct snd_soc_dapm_widget *w;
3171
Mark Browna094b802011-11-27 19:42:20 +00003172 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003173 &card->dapm, &codec->dapm);
3174
3175 list_for_each_entry(w, &card->widgets, list) {
3176 if (w->dapm != dapm)
3177 continue;
3178 switch (w->id) {
3179 case snd_soc_dapm_input:
3180 case snd_soc_dapm_output:
3181 case snd_soc_dapm_micbias:
Mark Browna094b802011-11-27 19:42:20 +00003182 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003183 w->name);
3184 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003185 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003186 "... Not in map; disabling\n");
3187 snd_soc_dapm_nc_pin(dapm, w->name);
3188 }
3189 break;
3190 default:
3191 break;
3192 }
3193 }
3194}
3195
Mark Brown1547aba2010-05-07 21:11:40 +01003196/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003197 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003198 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003199 *
3200 * Free all dapm widgets and resources.
3201 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003202void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003203{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003204 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003205 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003206 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003207 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003208}
3209EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3210
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003211static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003212{
Mark Brown51737472009-06-22 13:16:51 +01003213 struct snd_soc_dapm_widget *w;
3214 LIST_HEAD(down_list);
3215 int powerdown = 0;
3216
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003217 list_for_each_entry(w, &dapm->card->widgets, list) {
3218 if (w->dapm != dapm)
3219 continue;
Mark Brown51737472009-06-22 13:16:51 +01003220 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003221 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003222 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003223 powerdown = 1;
3224 }
3225 }
3226
3227 /* If there were no widgets to power down we're already in
3228 * standby.
3229 */
3230 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003231 if (dapm->bias_level == SND_SOC_BIAS_ON)
3232 snd_soc_dapm_set_bias_level(dapm,
3233 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003234 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003235 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3236 snd_soc_dapm_set_bias_level(dapm,
3237 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003238 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003239}
Mark Brown51737472009-06-22 13:16:51 +01003240
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003241/*
3242 * snd_soc_dapm_shutdown - callback for system shutdown
3243 */
3244void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3245{
3246 struct snd_soc_codec *codec;
3247
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003248 list_for_each_entry(codec, &card->codec_dev_list, list) {
3249 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003250 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3251 snd_soc_dapm_set_bias_level(&codec->dapm,
3252 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003253 }
Mark Brown51737472009-06-22 13:16:51 +01003254}
3255
Richard Purdie2b97eab2006-10-06 18:32:18 +02003256/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003257MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003258MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3259MODULE_LICENSE("GPL");