blob: d0a4be38dc0f0473bdafe38d6b18ca30b11de61b [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>
Ola Liljad7e7eb92012-05-24 15:26:25 +020038#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020043#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020044#include <sound/initval.h>
45
Mark Brown84e90932010-11-04 00:07:02 -040046#include <trace/events/asoc.h>
47
Mark Brownde02d072011-09-20 21:43:24 +010048#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
49
Richard Purdie2b97eab2006-10-06 18:32:18 +020050/* dapm power sequences - make this per codec in the future */
51static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010052 [snd_soc_dapm_pre] = 0,
53 [snd_soc_dapm_supply] = 1,
Mark Brown62ea8742012-01-21 21:14:48 +000054 [snd_soc_dapm_regulator_supply] = 1,
Ola Liljad7e7eb92012-05-24 15:26:25 +020055 [snd_soc_dapm_clock_supply] = 1,
Mark Brown38357ab2009-06-06 19:03:23 +010056 [snd_soc_dapm_micbias] = 2,
Mark Brownc74184e2012-04-04 22:12:09 +010057 [snd_soc_dapm_dai_link] = 2,
Mark Brown888df392012-02-16 19:37:51 -080058 [snd_soc_dapm_dai] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010059 [snd_soc_dapm_aif_in] = 3,
60 [snd_soc_dapm_aif_out] = 3,
61 [snd_soc_dapm_mic] = 4,
62 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000063 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010064 [snd_soc_dapm_value_mux] = 5,
65 [snd_soc_dapm_dac] = 6,
66 [snd_soc_dapm_mixer] = 7,
67 [snd_soc_dapm_mixer_named_ctl] = 7,
68 [snd_soc_dapm_pga] = 8,
69 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060070 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010071 [snd_soc_dapm_hp] = 10,
72 [snd_soc_dapm_spk] = 10,
Mark Brown7e1f7c82012-04-12 17:29:36 +010073 [snd_soc_dapm_line] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010074 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020075};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000076
Richard Purdie2b97eab2006-10-06 18:32:18 +020077static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010078 [snd_soc_dapm_pre] = 0,
79 [snd_soc_dapm_adc] = 1,
80 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010081 [snd_soc_dapm_spk] = 2,
Mark Brown7e1f7c82012-04-12 17:29:36 +010082 [snd_soc_dapm_line] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060083 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010084 [snd_soc_dapm_pga] = 4,
85 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010086 [snd_soc_dapm_mixer] = 5,
87 [snd_soc_dapm_dac] = 6,
88 [snd_soc_dapm_mic] = 7,
89 [snd_soc_dapm_micbias] = 8,
90 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000091 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010092 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010093 [snd_soc_dapm_aif_in] = 10,
94 [snd_soc_dapm_aif_out] = 10,
Mark Brown888df392012-02-16 19:37:51 -080095 [snd_soc_dapm_dai] = 10,
Mark Brownc74184e2012-04-04 22:12:09 +010096 [snd_soc_dapm_dai_link] = 11,
Ola Liljad7e7eb92012-05-24 15:26:25 +020097 [snd_soc_dapm_clock_supply] = 12,
Mark Brownc74184e2012-04-04 22:12:09 +010098 [snd_soc_dapm_regulator_supply] = 12,
99 [snd_soc_dapm_supply] = 12,
100 [snd_soc_dapm_post] = 13,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200101};
102
Troy Kisky12ef1932008-10-13 17:42:14 -0700103static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +0100104{
105 if (pop_time)
106 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
107}
108
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200109static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100110{
111 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200112 char *buf;
113
114 if (!pop_time)
115 return;
116
117 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
118 if (buf == NULL)
119 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100120
121 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200122 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100123 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100124 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200125
126 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100127}
128
Mark Browndb432b42011-10-03 21:06:40 +0100129static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
130{
131 return !list_empty(&w->dirty);
132}
133
Mark Brown25c77c52011-10-08 13:36:03 +0100134void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100135{
Mark Brown75c1f892011-10-04 22:28:08 +0100136 if (!dapm_dirty_widget(w)) {
137 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
138 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100139 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100140 }
Mark Browndb432b42011-10-03 21:06:40 +0100141}
Mark Brown25c77c52011-10-08 13:36:03 +0100142EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100143
Mark Browne2d32ff2012-08-31 17:38:32 -0700144void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
145{
146 struct snd_soc_card *card = dapm->card;
147 struct snd_soc_dapm_widget *w;
148
149 mutex_lock(&card->dapm_mutex);
150
151 list_for_each_entry(w, &card->widgets, list) {
152 switch (w->id) {
153 case snd_soc_dapm_input:
154 case snd_soc_dapm_output:
155 dapm_mark_dirty(w, "Rechecking inputs and outputs");
156 break;
157 default:
158 break;
159 }
160 }
161
162 mutex_unlock(&card->dapm_mutex);
163}
164EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
165
Richard Purdie2b97eab2006-10-06 18:32:18 +0200166/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100167static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200168 const struct snd_soc_dapm_widget *_widget)
169{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100170 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200171}
172
Liam Girdwood48056082011-07-20 12:23:33 +0100173/* get snd_card from DAPM context */
174static inline struct snd_card *dapm_get_snd_card(
175 struct snd_soc_dapm_context *dapm)
176{
177 if (dapm->codec)
178 return dapm->codec->card->snd_card;
179 else if (dapm->platform)
180 return dapm->platform->card->snd_card;
181 else
182 BUG();
183
184 /* unreachable */
185 return NULL;
186}
187
188/* get soc_card from DAPM context */
189static inline struct snd_soc_card *dapm_get_soc_card(
190 struct snd_soc_dapm_context *dapm)
191{
192 if (dapm->codec)
193 return dapm->codec->card;
194 else if (dapm->platform)
195 return dapm->platform->card;
196 else
197 BUG();
198
199 /* unreachable */
200 return NULL;
201}
202
Liam Girdwood6c120e12012-02-15 15:15:34 +0000203static void dapm_reset(struct snd_soc_card *card)
204{
205 struct snd_soc_dapm_widget *w;
206
207 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
208
209 list_for_each_entry(w, &card->widgets, list) {
210 w->power_checked = false;
211 w->inputs = -1;
212 w->outputs = -1;
213 }
214}
215
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100216static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
217{
218 if (w->codec)
219 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100220 else if (w->platform)
221 return snd_soc_platform_read(w->platform, reg);
222
223 dev_err(w->dapm->dev, "no valid widget read method\n");
224 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100225}
226
227static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
228{
229 if (w->codec)
230 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100231 else if (w->platform)
232 return snd_soc_platform_write(w->platform, reg, val);
233
234 dev_err(w->dapm->dev, "no valid widget write method\n");
235 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100236}
237
Liam Girdwood49575fb52012-03-06 18:16:19 +0000238static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
239{
Mark Browne06ab3b2012-03-06 23:58:22 +0000240 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000241 mutex_lock(&w->codec->mutex);
242 else if (w->platform)
243 mutex_lock(&w->platform->mutex);
244}
245
246static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
247{
Mark Browne06ab3b2012-03-06 23:58:22 +0000248 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000249 mutex_unlock(&w->codec->mutex);
250 else if (w->platform)
251 mutex_unlock(&w->platform->mutex);
252}
253
254static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100255 unsigned short reg, unsigned int mask, unsigned int value)
256{
Mark Brown8a713da2011-12-03 12:33:55 +0000257 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100258 unsigned int old, new;
259 int ret;
260
Mark Brown8a713da2011-12-03 12:33:55 +0000261 if (w->codec && w->codec->using_regmap) {
262 ret = regmap_update_bits_check(w->codec->control_data,
263 reg, mask, value, &change);
264 if (ret != 0)
265 return ret;
266 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000267 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000268 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000269 if (ret < 0) {
270 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100271 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000272 }
Mark Brown8a713da2011-12-03 12:33:55 +0000273
274 old = ret;
275 new = (old & ~mask) | (value & mask);
276 change = old != new;
277 if (change) {
278 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000279 if (ret < 0) {
280 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000281 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000282 }
Mark Brown8a713da2011-12-03 12:33:55 +0000283 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000284 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100285 }
286
287 return change;
288}
289
Mark Brown452c5ea2009-05-17 21:41:23 +0100290/**
291 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800292 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100293 * @level: level to configure
294 *
295 * Configure the bias (power) levels for the SoC audio device.
296 *
297 * Returns 0 for success else error.
298 */
Mark Browned5a4c42011-02-18 11:12:42 -0800299static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200300 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100301{
Mark Browned5a4c42011-02-18 11:12:42 -0800302 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100303 int ret = 0;
304
Mark Brown84e90932010-11-04 00:07:02 -0400305 trace_snd_soc_bias_level_start(card, level);
306
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000307 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100308 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100309 if (ret != 0)
310 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100311
Mark Browncc4c6702011-06-06 19:03:34 +0100312 if (dapm->codec) {
313 if (dapm->codec->driver->set_bias_level)
314 ret = dapm->codec->driver->set_bias_level(dapm->codec,
315 level);
Mark Brownd8c3bb92012-08-23 18:10:42 +0100316 else
317 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100318 } else if (!card || dapm != &card->dapm) {
Liam Girdwood41231282012-07-06 16:56:16 +0100319 dapm->bias_level = level;
Mark Brown4e872a42012-08-23 18:20:49 +0100320 }
Liam Girdwood41231282012-07-06 16:56:16 +0100321
Mark Brown171ec6b2011-06-06 18:15:19 +0100322 if (ret != 0)
323 goto out;
324
325 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100326 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100327out:
Mark Brown84e90932010-11-04 00:07:02 -0400328 trace_snd_soc_bias_level_done(card, level);
329
Mark Brown452c5ea2009-05-17 21:41:23 +0100330 return ret;
331}
332
Richard Purdie2b97eab2006-10-06 18:32:18 +0200333/* set up initial codec paths */
334static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
335 struct snd_soc_dapm_path *p, int i)
336{
337 switch (w->id) {
338 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000339 case snd_soc_dapm_mixer:
340 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200341 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100342 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600343 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400344 unsigned int reg = mc->reg;
345 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100346 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400347 unsigned int mask = (1 << fls(max)) - 1;
348 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200349
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100350 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200351 val = (val >> shift) & mask;
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200352 if (invert)
353 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200354
Benoît Thébaudeau32fee7a2012-07-02 13:45:21 +0200355 p->connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200356 }
357 break;
358 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600359 struct soc_enum *e = (struct soc_enum *)
360 w->kcontrol_news[i].private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200361 int val, item;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200362
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100363 val = soc_widget_read(w, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +0200364 item = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200365
366 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100367 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200368 if (!(strcmp(p->name, e->texts[i])) && item == i)
369 p->connect = 1;
370 }
371 }
372 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000373 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600374 struct soc_enum *e = (struct soc_enum *)
375 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000376
377 p->connect = 0;
378 /* since a virtual mux has no backing registers to
379 * decide which path to connect, it will try to match
380 * with the first enumeration. This is to ensure
381 * that the default mux choice (the first) will be
382 * correctly powered up during initialization.
383 */
384 if (!strcmp(p->name, e->texts[0]))
385 p->connect = 1;
386 }
387 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200388 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200389 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600390 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200391 int val, item;
392
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100393 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200394 val = (val >> e->shift_l) & e->mask;
395 for (item = 0; item < e->max; item++) {
396 if (val == e->values[item])
397 break;
398 }
399
400 p->connect = 0;
401 for (i = 0; i < e->max; i++) {
402 if (!(strcmp(p->name, e->texts[i])) && item == i)
403 p->connect = 1;
404 }
405 }
406 break;
Mark Brown56563102011-10-03 22:41:09 +0100407 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200408 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600409 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200410 case snd_soc_dapm_output:
411 case snd_soc_dapm_adc:
412 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000413 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200414 case snd_soc_dapm_dac:
415 case snd_soc_dapm_micbias:
416 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100417 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000418 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200419 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100420 case snd_soc_dapm_aif_in:
421 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800422 case snd_soc_dapm_dai:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200423 case snd_soc_dapm_hp:
424 case snd_soc_dapm_mic:
425 case snd_soc_dapm_spk:
426 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +0100427 case snd_soc_dapm_dai_link:
Mark Brown56563102011-10-03 22:41:09 +0100428 p->connect = 1;
429 break;
430 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200431 case snd_soc_dapm_pre:
432 case snd_soc_dapm_post:
433 p->connect = 0;
434 break;
435 }
436}
437
Mark Brown74b8f952009-06-06 11:26:15 +0100438/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200439static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200440 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
441 struct snd_soc_dapm_path *path, const char *control_name,
442 const struct snd_kcontrol_new *kcontrol)
443{
444 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
445 int i;
446
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100447 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200448 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200449 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200450 list_add(&path->list_sink, &dest->sources);
451 list_add(&path->list_source, &src->sinks);
452 path->name = (char*)e->texts[i];
453 dapm_set_path_status(dest, path, 0);
454 return 0;
455 }
456 }
457
458 return -ENODEV;
459}
460
Mark Brown74b8f952009-06-06 11:26:15 +0100461/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200462static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200463 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
464 struct snd_soc_dapm_path *path, const char *control_name)
465{
466 int i;
467
468 /* search for mixer kcontrol */
469 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600470 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200471 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200472 list_add(&path->list_sink, &dest->sources);
473 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600474 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200475 dapm_set_path_status(dest, path, i);
476 return 0;
477 }
478 }
479 return -ENODEV;
480}
481
Stephen Warrenaf468002011-04-28 17:38:01 -0600482static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600483 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600484 const struct snd_kcontrol_new *kcontrol_new,
485 struct snd_kcontrol **kcontrol)
486{
487 struct snd_soc_dapm_widget *w;
488 int i;
489
490 *kcontrol = NULL;
491
492 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600493 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
494 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600495 for (i = 0; i < w->num_kcontrols; i++) {
496 if (&w->kcontrol_news[i] == kcontrol_new) {
497 if (w->kcontrols)
498 *kcontrol = w->kcontrols[i];
499 return 1;
500 }
501 }
502 }
503
504 return 0;
505}
506
Richard Purdie2b97eab2006-10-06 18:32:18 +0200507/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200508static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200509{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200510 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200511 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000512 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200513 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000514 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000515 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600516 struct snd_soc_dapm_widget_list *wlist;
517 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000518
519 if (dapm->codec)
520 prefix = dapm->codec->name_prefix;
521 else
522 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200523
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000524 if (prefix)
525 prefix_len = strlen(prefix) + 1;
526 else
527 prefix_len = 0;
528
Richard Purdie2b97eab2006-10-06 18:32:18 +0200529 /* add kcontrol */
530 for (i = 0; i < w->num_kcontrols; i++) {
531
532 /* match name */
533 list_for_each_entry(path, &w->sources, list_sink) {
534
535 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600536 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200537 continue;
538
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200539 if (w->kcontrols[i]) {
540 path->kcontrol = w->kcontrols[i];
541 continue;
542 }
543
Stephen Warrenfafd2172011-04-28 17:38:00 -0600544 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
545 sizeof(struct snd_soc_dapm_widget *),
546 wlist = kzalloc(wlistsize, GFP_KERNEL);
547 if (wlist == NULL) {
548 dev_err(dapm->dev,
549 "asoc: can't allocate widget list for %s\n",
550 w->name);
551 return -ENOMEM;
552 }
553 wlist->num_widgets = 1;
554 wlist->widgets[0] = w;
555
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000556 /* add dapm control with long name.
557 * for dapm_mixer this is the concatenation of the
558 * mixer and kcontrol name.
559 * for dapm_mixer_named_ctl this is simply the
560 * kcontrol name.
561 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600562 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000563 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000564 name_len += 1 + strlen(w->name);
565
Mark Brown219b93f2008-10-28 13:02:31 +0000566 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000567
Stephen Warrenfafd2172011-04-28 17:38:00 -0600568 if (path->long_name == NULL) {
569 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200570 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600571 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200572
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000573 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000574 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000575 /* The control will get a prefix from
576 * the control creation process but
577 * we're also using the same prefix
578 * for widgets so cut the prefix off
579 * the front of the widget name.
580 */
Mark Brown888df392012-02-16 19:37:51 -0800581 snprintf((char *)path->long_name, name_len,
582 "%s %s", w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600583 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000584 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000585 case snd_soc_dapm_mixer_named_ctl:
Mark Brown888df392012-02-16 19:37:51 -0800586 snprintf((char *)path->long_name, name_len,
587 "%s", w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000588 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000589 }
590
Mark Brown888df392012-02-16 19:37:51 -0800591 ((char *)path->long_name)[name_len - 1] = '\0';
Mark Brown219b93f2008-10-28 13:02:31 +0000592
Stephen Warrenfafd2172011-04-28 17:38:00 -0600593 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
594 wlist, path->long_name,
595 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200596 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200597 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200598 dev_err(dapm->dev,
599 "asoc: failed to add dapm kcontrol %s: %d\n",
600 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600601 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200602 kfree(path->long_name);
603 path->long_name = NULL;
604 return ret;
605 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600606 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200607 }
608 }
609 return ret;
610}
611
612/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200613static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200614{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200615 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200616 struct snd_soc_dapm_path *path = NULL;
617 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000618 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000619 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000620 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600621 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600622 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600623 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600624 size_t wlistsize;
Mark Brown888df392012-02-16 19:37:51 -0800625 const char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200626
Stephen Warrenaf468002011-04-28 17:38:01 -0600627 if (w->num_kcontrols != 1) {
628 dev_err(dapm->dev,
629 "asoc: mux %s has incorrect number of controls\n",
630 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200631 return -EINVAL;
632 }
633
Stephen Warren1007da02011-05-26 09:57:33 -0600634 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600635 &kcontrol);
636 if (kcontrol) {
637 wlist = kcontrol->private_data;
638 wlistentries = wlist->num_widgets + 1;
639 } else {
640 wlist = NULL;
641 wlistentries = 1;
642 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600643 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600644 wlistentries * sizeof(struct snd_soc_dapm_widget *),
645 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600646 if (wlist == NULL) {
647 dev_err(dapm->dev,
648 "asoc: can't allocate widget list for %s\n", w->name);
649 return -ENOMEM;
650 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600651 wlist->num_widgets = wlistentries;
652 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600653
Stephen Warrenaf468002011-04-28 17:38:01 -0600654 if (!kcontrol) {
655 if (dapm->codec)
656 prefix = dapm->codec->name_prefix;
657 else
658 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000659
Stephen Warrenaf468002011-04-28 17:38:01 -0600660 if (shared) {
661 name = w->kcontrol_news[0].name;
662 prefix_len = 0;
663 } else {
664 name = w->name;
665 if (prefix)
666 prefix_len = strlen(prefix) + 1;
667 else
668 prefix_len = 0;
669 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000670
Stephen Warrenaf468002011-04-28 17:38:01 -0600671 /*
672 * The control will get a prefix from the control creation
673 * process but we're also using the same prefix for widgets so
674 * cut the prefix off the front of the widget name.
675 */
676 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
677 name + prefix_len, prefix);
678 ret = snd_ctl_add(card, kcontrol);
679 if (ret < 0) {
Mark Brown53daf202011-09-05 10:51:05 -0700680 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
681 w->name, ret);
Stephen Warrenaf468002011-04-28 17:38:01 -0600682 kfree(wlist);
683 return ret;
684 }
685 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200686
Stephen Warrenaf468002011-04-28 17:38:01 -0600687 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200688
Stephen Warrenfad59882011-04-28 17:37:59 -0600689 w->kcontrols[0] = kcontrol;
690
Richard Purdie2b97eab2006-10-06 18:32:18 +0200691 list_for_each_entry(path, &w->sources, list_sink)
692 path->kcontrol = kcontrol;
693
Stephen Warrenaf468002011-04-28 17:38:01 -0600694 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200695}
696
697/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200698static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200699{
Mark Browna6c65732010-03-03 17:45:21 +0000700 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200701 dev_err(w->dapm->dev,
702 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200703
Mark Browna6c65732010-03-03 17:45:21 +0000704 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200705}
706
707/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200708static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200709{
710 struct snd_soc_dapm_path *p;
711
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200712 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200713 p->walked = 0;
714}
715
Mark Brown99497882010-05-07 20:24:05 +0100716/* We implement power down on suspend by checking the power state of
717 * the ALSA card - when we are suspending the ALSA state for the card
718 * is set to D3.
719 */
720static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
721{
Mark Brown12ea2c72011-03-02 18:17:32 +0000722 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100725 case SNDRV_CTL_POWER_D3hot:
726 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100727 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200728 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
729 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100730 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100731 default:
732 return 1;
733 }
734}
735
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100736/* add widget to list if it's not already in the list */
737static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
738 struct snd_soc_dapm_widget *w)
739{
740 struct snd_soc_dapm_widget_list *wlist;
741 int wlistsize, wlistentries, i;
742
743 if (*list == NULL)
744 return -EINVAL;
745
746 wlist = *list;
747
748 /* is this widget already in the list */
749 for (i = 0; i < wlist->num_widgets; i++) {
750 if (wlist->widgets[i] == w)
751 return 0;
752 }
753
754 /* allocate some new space */
755 wlistentries = wlist->num_widgets + 1;
756 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
757 wlistentries * sizeof(struct snd_soc_dapm_widget *);
758 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
759 if (*list == NULL) {
760 dev_err(w->dapm->dev, "can't allocate widget list for %s\n",
761 w->name);
762 return -ENOMEM;
763 }
764 wlist = *list;
765
766 /* insert the widget */
767 dev_dbg(w->dapm->dev, "added %s in widget list pos %d\n",
768 w->name, wlist->num_widgets);
769
770 wlist->widgets[wlist->num_widgets] = w;
771 wlist->num_widgets++;
772 return 1;
773}
774
Richard Purdie2b97eab2006-10-06 18:32:18 +0200775/*
776 * Recursively check for a completed path to an active or physically connected
777 * output widget. Returns number of complete paths.
778 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100779static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
780 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200781{
782 struct snd_soc_dapm_path *path;
783 int con = 0;
784
Mark Brown024dc072011-10-09 11:52:05 +0100785 if (widget->outputs >= 0)
786 return widget->outputs;
787
Mark Brownde02d072011-09-20 21:43:24 +0100788 DAPM_UPDATE_STAT(widget, path_checks);
789
Mark Brown62ea8742012-01-21 21:14:48 +0000790 switch (widget->id) {
791 case snd_soc_dapm_supply:
792 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200793 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100794 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000795 default:
796 break;
797 }
Mark Brown246d0a12009-04-22 18:24:55 +0100798
Mark Brown010ff262009-08-17 17:39:22 +0100799 switch (widget->id) {
800 case snd_soc_dapm_adc:
801 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800802 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100803 if (widget->active) {
804 widget->outputs = snd_soc_dapm_suspend_check(widget);
805 return widget->outputs;
806 }
Mark Brown010ff262009-08-17 17:39:22 +0100807 default:
808 break;
809 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200810
811 if (widget->connected) {
812 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100813 if (widget->id == snd_soc_dapm_output && !widget->ext) {
814 widget->outputs = snd_soc_dapm_suspend_check(widget);
815 return widget->outputs;
816 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200817
818 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100819 if (widget->id == snd_soc_dapm_hp ||
820 widget->id == snd_soc_dapm_spk ||
821 (widget->id == snd_soc_dapm_line &&
822 !list_empty(&widget->sources))) {
823 widget->outputs = snd_soc_dapm_suspend_check(widget);
824 return widget->outputs;
825 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200826 }
827
828 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100829 DAPM_UPDATE_STAT(widget, neighbour_checks);
830
Mark Brownbf3a9e12011-06-13 16:42:29 +0100831 if (path->weak)
832 continue;
833
Richard Purdie2b97eab2006-10-06 18:32:18 +0200834 if (path->walked)
835 continue;
836
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100837 trace_snd_soc_dapm_output_path(widget, path);
838
Richard Purdie2b97eab2006-10-06 18:32:18 +0200839 if (path->sink && path->connect) {
840 path->walked = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100841
842 /* do we need to add this widget to the list ? */
843 if (list) {
844 int err;
845 err = dapm_list_add_widget(list, path->sink);
846 if (err < 0) {
847 dev_err(widget->dapm->dev, "could not add widget %s\n",
848 widget->name);
849 return con;
850 }
851 }
852
853 con += is_connected_output_ep(path->sink, list);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200854 }
855 }
856
Mark Brown024dc072011-10-09 11:52:05 +0100857 widget->outputs = con;
858
Richard Purdie2b97eab2006-10-06 18:32:18 +0200859 return con;
860}
861
862/*
863 * Recursively check for a completed path to an active or physically connected
864 * input widget. Returns number of complete paths.
865 */
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100866static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
867 struct snd_soc_dapm_widget_list **list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200868{
869 struct snd_soc_dapm_path *path;
870 int con = 0;
871
Mark Brown024dc072011-10-09 11:52:05 +0100872 if (widget->inputs >= 0)
873 return widget->inputs;
874
Mark Brownde02d072011-09-20 21:43:24 +0100875 DAPM_UPDATE_STAT(widget, path_checks);
876
Mark Brown62ea8742012-01-21 21:14:48 +0000877 switch (widget->id) {
878 case snd_soc_dapm_supply:
879 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +0200880 case snd_soc_dapm_clock_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100881 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000882 default:
883 break;
884 }
Mark Brown246d0a12009-04-22 18:24:55 +0100885
Richard Purdie2b97eab2006-10-06 18:32:18 +0200886 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100887 switch (widget->id) {
888 case snd_soc_dapm_dac:
889 case snd_soc_dapm_aif_in:
Mark Brown888df392012-02-16 19:37:51 -0800890 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100891 if (widget->active) {
892 widget->inputs = snd_soc_dapm_suspend_check(widget);
893 return widget->inputs;
894 }
Mark Brown010ff262009-08-17 17:39:22 +0100895 default:
896 break;
897 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200898
899 if (widget->connected) {
900 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100901 if (widget->id == snd_soc_dapm_input && !widget->ext) {
902 widget->inputs = snd_soc_dapm_suspend_check(widget);
903 return widget->inputs;
904 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200905
906 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100907 if (widget->id == snd_soc_dapm_vmid) {
908 widget->inputs = snd_soc_dapm_suspend_check(widget);
909 return widget->inputs;
910 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200911
912 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300913 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100914 (widget->id == snd_soc_dapm_line &&
915 !list_empty(&widget->sinks))) {
916 widget->inputs = snd_soc_dapm_suspend_check(widget);
917 return widget->inputs;
918 }
919
Mark Brown1ab97c82011-11-27 16:21:51 +0000920 /* signal generator */
921 if (widget->id == snd_soc_dapm_siggen) {
922 widget->inputs = snd_soc_dapm_suspend_check(widget);
923 return widget->inputs;
924 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200925 }
926
927 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100928 DAPM_UPDATE_STAT(widget, neighbour_checks);
929
Mark Brownbf3a9e12011-06-13 16:42:29 +0100930 if (path->weak)
931 continue;
932
Richard Purdie2b97eab2006-10-06 18:32:18 +0200933 if (path->walked)
934 continue;
935
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100936 trace_snd_soc_dapm_input_path(widget, path);
937
Richard Purdie2b97eab2006-10-06 18:32:18 +0200938 if (path->source && path->connect) {
939 path->walked = 1;
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100940
941 /* do we need to add this widget to the list ? */
942 if (list) {
943 int err;
Liam Girdwood90c6ce02012-06-05 19:27:15 +0100944 err = dapm_list_add_widget(list, path->source);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100945 if (err < 0) {
946 dev_err(widget->dapm->dev, "could not add widget %s\n",
947 widget->name);
948 return con;
949 }
950 }
951
952 con += is_connected_input_ep(path->source, list);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200953 }
954 }
955
Mark Brown024dc072011-10-09 11:52:05 +0100956 widget->inputs = con;
957
Richard Purdie2b97eab2006-10-06 18:32:18 +0200958 return con;
959}
960
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100961/**
962 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
963 * @dai: the soc DAI.
964 * @stream: stream direction.
965 * @list: list of active widgets for this stream.
966 *
967 * Queries DAPM graph as to whether an valid audio stream path exists for
968 * the initial stream specified by name. This takes into account
969 * current mixer and mux kcontrol settings. Creates list of valid widgets.
970 *
971 * Returns the number of valid paths or negative error.
972 */
973int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
974 struct snd_soc_dapm_widget_list **list)
975{
976 struct snd_soc_card *card = dai->card;
977 int paths;
978
979 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
980 dapm_reset(card);
981
982 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
983 paths = is_connected_output_ep(dai->playback_widget, list);
984 else
Liam Girdwoodd298caa2012-06-01 18:03:00 +0100985 paths = is_connected_input_ep(dai->capture_widget, list);
Liam Girdwoodec2e3032012-04-18 11:41:11 +0100986
987 trace_snd_soc_dapm_connected(paths, stream);
988 dapm_clear_walk(&card->dapm);
989 mutex_unlock(&card->dapm_mutex);
990
991 return paths;
992}
993
Richard Purdie2b97eab2006-10-06 18:32:18 +0200994/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300995 * Handler for generic register modifier widget.
996 */
997int dapm_reg_event(struct snd_soc_dapm_widget *w,
998 struct snd_kcontrol *kcontrol, int event)
999{
1000 unsigned int val;
1001
1002 if (SND_SOC_DAPM_EVENT_ON(event))
1003 val = w->on_val;
1004 else
1005 val = w->off_val;
1006
Liam Girdwood49575fb52012-03-06 18:16:19 +00001007 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001008 w->mask << w->shift, val << w->shift);
1009
1010 return 0;
1011}
Mark Brown11589412008-07-29 11:42:23 +01001012EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +03001013
Mark Brown62ea8742012-01-21 21:14:48 +00001014/*
1015 * Handler for regulator supply widget.
1016 */
1017int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1018 struct snd_kcontrol *kcontrol, int event)
1019{
Mark Brownc05b84d2012-09-07 12:57:11 +08001020 int ret;
1021
1022 if (SND_SOC_DAPM_EVENT_ON(event)) {
1023 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
1024 ret = regulator_allow_bypass(w->regulator, true);
1025 if (ret != 0)
1026 dev_warn(w->dapm->dev,
1027 "Failed to bypass %s: %d\n",
1028 w->name, ret);
1029 }
1030
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001031 return regulator_enable(w->regulator);
Mark Brownc05b84d2012-09-07 12:57:11 +08001032 } else {
1033 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
1034 ret = regulator_allow_bypass(w->regulator, false);
1035 if (ret != 0)
1036 dev_warn(w->dapm->dev,
1037 "Failed to unbypass %s: %d\n",
1038 w->name, ret);
1039 }
1040
Liam Girdwooda3cc0562012-03-09 17:20:16 +00001041 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brownc05b84d2012-09-07 12:57:11 +08001042 }
Mark Brown62ea8742012-01-21 21:14:48 +00001043}
1044EXPORT_SYMBOL_GPL(dapm_regulator_event);
1045
Ola Liljad7e7eb92012-05-24 15:26:25 +02001046/*
1047 * Handler for clock supply widget.
1048 */
1049int dapm_clock_event(struct snd_soc_dapm_widget *w,
1050 struct snd_kcontrol *kcontrol, int event)
1051{
1052 if (!w->clk)
1053 return -EIO;
1054
Mark Brownec029952012-06-04 08:16:20 +01001055#ifdef CONFIG_HAVE_CLK
Ola Liljad7e7eb92012-05-24 15:26:25 +02001056 if (SND_SOC_DAPM_EVENT_ON(event)) {
1057 return clk_enable(w->clk);
1058 } else {
1059 clk_disable(w->clk);
1060 return 0;
1061 }
Mark Brownec029952012-06-04 08:16:20 +01001062#endif
Marek Belisko98b3cf12012-07-12 23:00:16 +02001063 return 0;
Ola Liljad7e7eb92012-05-24 15:26:25 +02001064}
1065EXPORT_SYMBOL_GPL(dapm_clock_event);
1066
Mark Brownd8050022011-09-28 18:28:23 +01001067static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1068{
Mark Brown9b8a83b2011-10-04 22:15:59 +01001069 if (w->power_checked)
1070 return w->new_power;
1071
Mark Brownd8050022011-09-28 18:28:23 +01001072 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +01001073 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +01001074 else
Mark Brown9b8a83b2011-10-04 22:15:59 +01001075 w->new_power = w->power_check(w);
1076
1077 w->power_checked = true;
1078
1079 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +01001080}
1081
Mark Browncd0f2d42009-04-20 16:56:59 +01001082/* Generic check to see if a widget should be powered.
1083 */
1084static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1085{
1086 int in, out;
1087
Mark Brownde02d072011-09-20 21:43:24 +01001088 DAPM_UPDATE_STAT(w, power_checks);
1089
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001090 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001091 dapm_clear_walk(w->dapm);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001092 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001093 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +01001094 return out != 0 && in != 0;
1095}
1096
Mark Brown888df392012-02-16 19:37:51 -08001097static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
1098{
1099 DAPM_UPDATE_STAT(w, power_checks);
1100
Mark Brown1eee1b32012-04-10 13:57:46 +01001101 if (w->active)
1102 return w->active;
1103
1104 return dapm_generic_check_power(w);
Mark Brown888df392012-02-16 19:37:51 -08001105}
1106
Mark Brown6ea31b92009-04-20 17:15:41 +01001107/* Check to see if an ADC has power */
1108static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1109{
1110 int in;
1111
Mark Brownde02d072011-09-20 21:43:24 +01001112 DAPM_UPDATE_STAT(w, power_checks);
1113
Mark Brown6ea31b92009-04-20 17:15:41 +01001114 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001115 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001116 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001117 return in != 0;
1118 } else {
1119 return dapm_generic_check_power(w);
1120 }
1121}
1122
1123/* Check to see if a DAC has power */
1124static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1125{
1126 int out;
1127
Mark Brownde02d072011-09-20 21:43:24 +01001128 DAPM_UPDATE_STAT(w, power_checks);
1129
Mark Brown6ea31b92009-04-20 17:15:41 +01001130 if (w->active) {
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001131 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001132 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +01001133 return out != 0;
1134 } else {
1135 return dapm_generic_check_power(w);
1136 }
1137}
1138
Mark Brown246d0a12009-04-22 18:24:55 +01001139/* Check to see if a power supply is needed */
1140static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1141{
1142 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +01001143
Mark Brownde02d072011-09-20 21:43:24 +01001144 DAPM_UPDATE_STAT(w, power_checks);
1145
Mark Brown246d0a12009-04-22 18:24:55 +01001146 /* Check if one of our outputs is connected */
1147 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +01001148 DAPM_UPDATE_STAT(w, neighbour_checks);
1149
Mark Brownbf3a9e12011-06-13 16:42:29 +01001150 if (path->weak)
1151 continue;
1152
Mark Brown215edda2009-09-08 18:59:05 +01001153 if (path->connected &&
1154 !path->connected(path->source, path->sink))
1155 continue;
1156
Mark Brown30173582011-02-11 11:42:19 +00001157 if (!path->sink)
1158 continue;
1159
Mark Brownf68d7e12011-10-04 22:57:50 +01001160 if (dapm_widget_power_check(path->sink))
1161 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +01001162 }
1163
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001164 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +01001165
Mark Brownf68d7e12011-10-04 22:57:50 +01001166 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +01001167}
1168
Mark Brown35c64bc2011-09-28 18:23:53 +01001169static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1170{
1171 return 1;
1172}
1173
Mark Brown38357ab2009-06-06 19:03:23 +01001174static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1175 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001176 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001177{
Mark Brown828a8422011-01-15 13:14:30 +00001178 int *sort;
1179
1180 if (power_up)
1181 sort = dapm_up_seq;
1182 else
1183 sort = dapm_down_seq;
1184
Mark Brown38357ab2009-06-06 19:03:23 +01001185 if (sort[a->id] != sort[b->id])
1186 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001187 if (a->subseq != b->subseq) {
1188 if (power_up)
1189 return a->subseq - b->subseq;
1190 else
1191 return b->subseq - a->subseq;
1192 }
Mark Brownb22ead22009-06-07 12:51:26 +01001193 if (a->reg != b->reg)
1194 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001195 if (a->dapm != b->dapm)
1196 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001197
Mark Brown38357ab2009-06-06 19:03:23 +01001198 return 0;
1199}
Mark Brown42aa3412009-03-01 19:21:10 +00001200
Mark Brown38357ab2009-06-06 19:03:23 +01001201/* Insert a widget in order into a DAPM power sequence. */
1202static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1203 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001204 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001205{
1206 struct snd_soc_dapm_widget *w;
1207
1208 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001209 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001210 list_add_tail(&new_widget->power_list, &w->power_list);
1211 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001212 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001213
Mark Brown38357ab2009-06-06 19:03:23 +01001214 list_add_tail(&new_widget->power_list, list);
1215}
Mark Brown42aa3412009-03-01 19:21:10 +00001216
Mark Brown68f89ad2010-11-03 23:51:49 -04001217static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1218 struct snd_soc_dapm_widget *w, int event)
1219{
1220 struct snd_soc_card *card = dapm->card;
1221 const char *ev_name;
1222 int power, ret;
1223
1224 switch (event) {
1225 case SND_SOC_DAPM_PRE_PMU:
1226 ev_name = "PRE_PMU";
1227 power = 1;
1228 break;
1229 case SND_SOC_DAPM_POST_PMU:
1230 ev_name = "POST_PMU";
1231 power = 1;
1232 break;
1233 case SND_SOC_DAPM_PRE_PMD:
1234 ev_name = "PRE_PMD";
1235 power = 0;
1236 break;
1237 case SND_SOC_DAPM_POST_PMD:
1238 ev_name = "POST_PMD";
1239 power = 0;
1240 break;
1241 default:
1242 BUG();
1243 return;
1244 }
1245
1246 if (w->power != power)
1247 return;
1248
1249 if (w->event && (w->event_flags & event)) {
1250 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1251 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001252 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001253 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001254 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001255 if (ret < 0)
1256 pr_err("%s: %s event failed: %d\n",
1257 ev_name, w->name, ret);
1258 }
1259}
1260
Mark Brownb22ead22009-06-07 12:51:26 +01001261/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001262static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001263 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001264{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001265 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001266 struct snd_soc_dapm_widget *w;
1267 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001268 unsigned int value = 0;
1269 unsigned int mask = 0;
1270 unsigned int cur_mask;
1271
1272 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1273 power_list)->reg;
1274
1275 list_for_each_entry(w, pending, power_list) {
1276 cur_mask = 1 << w->shift;
1277 BUG_ON(reg != w->reg);
1278
1279 if (w->invert)
1280 power = !w->power;
1281 else
1282 power = w->power;
1283
1284 mask |= cur_mask;
1285 if (power)
1286 value |= cur_mask;
1287
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001288 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001289 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1290 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001291
Mark Brown68f89ad2010-11-03 23:51:49 -04001292 /* Check for events */
1293 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1294 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001295 }
1296
Mark Brown81628102009-06-07 13:21:24 +01001297 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001298 /* Any widget will do, they should all be updating the
1299 * same register.
1300 */
1301 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1302 power_list);
1303
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001304 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001305 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001306 value, mask, reg, card->pop_time);
1307 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001308 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001309 }
1310
1311 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001312 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1313 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001314 }
Mark Brown42aa3412009-03-01 19:21:10 +00001315}
1316
Mark Brownb22ead22009-06-07 12:51:26 +01001317/* Apply a DAPM power sequence.
1318 *
1319 * We walk over a pre-sorted list of widgets to apply power to. In
1320 * order to minimise the number of writes to the device required
1321 * multiple widgets will be updated in a single write where possible.
1322 * Currently anything that requires more than a single write is not
1323 * handled.
1324 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001325static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001326 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001327{
1328 struct snd_soc_dapm_widget *w, *n;
1329 LIST_HEAD(pending);
1330 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001331 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001332 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001333 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001334 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001335 int *sort;
1336
1337 if (power_up)
1338 sort = dapm_up_seq;
1339 else
1340 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001341
Mark Brownb22ead22009-06-07 12:51:26 +01001342 list_for_each_entry_safe(w, n, list, power_list) {
1343 ret = 0;
1344
1345 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001346 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001347 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001348 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001349 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001350
Mark Brown474b62d2011-01-18 16:14:44 +00001351 if (cur_dapm && cur_dapm->seq_notifier) {
1352 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1353 if (sort[i] == cur_sort)
1354 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001355 i,
1356 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001357 }
1358
Mark Brownb22ead22009-06-07 12:51:26 +01001359 INIT_LIST_HEAD(&pending);
1360 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001361 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001362 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001363 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001364 }
1365
Mark Brown163cac02009-06-07 10:12:52 +01001366 switch (w->id) {
1367 case snd_soc_dapm_pre:
1368 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001369 list_for_each_entry_safe_continue(w, n, list,
1370 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001371
Mark Brownb22ead22009-06-07 12:51:26 +01001372 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001373 ret = w->event(w,
1374 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001375 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001376 ret = w->event(w,
1377 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001378 break;
1379
1380 case snd_soc_dapm_post:
1381 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001382 list_for_each_entry_safe_continue(w, n, list,
1383 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001384
Mark Brownb22ead22009-06-07 12:51:26 +01001385 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001386 ret = w->event(w,
1387 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001388 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001389 ret = w->event(w,
1390 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001391 break;
1392
Mark Brown163cac02009-06-07 10:12:52 +01001393 default:
Mark Brown81628102009-06-07 13:21:24 +01001394 /* Queue it up for application */
1395 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001396 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001397 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001398 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001399 list_move(&w->power_list, &pending);
1400 break;
Mark Brown163cac02009-06-07 10:12:52 +01001401 }
Mark Brownb22ead22009-06-07 12:51:26 +01001402
1403 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001404 dev_err(w->dapm->dev,
1405 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001406 }
Mark Brownb22ead22009-06-07 12:51:26 +01001407
1408 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001409 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001410
1411 if (cur_dapm && cur_dapm->seq_notifier) {
1412 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1413 if (sort[i] == cur_sort)
1414 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001415 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001416 }
Mark Brown163cac02009-06-07 10:12:52 +01001417}
1418
Mark Brown97404f22010-12-14 16:13:57 +00001419static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1420{
1421 struct snd_soc_dapm_update *update = dapm->update;
1422 struct snd_soc_dapm_widget *w;
1423 int ret;
1424
1425 if (!update)
1426 return;
1427
1428 w = update->widget;
1429
1430 if (w->event &&
1431 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1432 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1433 if (ret != 0)
1434 pr_err("%s DAPM pre-event failed: %d\n",
1435 w->name, ret);
1436 }
1437
Liam Girdwood49575fb52012-03-06 18:16:19 +00001438 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001439 update->val);
1440 if (ret < 0)
1441 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1442
1443 if (w->event &&
1444 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1445 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1446 if (ret != 0)
1447 pr_err("%s DAPM post-event failed: %d\n",
1448 w->name, ret);
1449 }
1450}
1451
Mark Brown9d0624a2011-02-18 11:49:43 -08001452/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1453 * they're changing state.
1454 */
1455static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1456{
1457 struct snd_soc_dapm_context *d = data;
1458 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001459
Mark Brown56fba412011-06-04 11:25:10 +01001460 /* If we're off and we're not supposed to be go into STANDBY */
1461 if (d->bias_level == SND_SOC_BIAS_OFF &&
1462 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001463 if (d->dev)
1464 pm_runtime_get_sync(d->dev);
1465
Mark Brown9d0624a2011-02-18 11:49:43 -08001466 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1467 if (ret != 0)
1468 dev_err(d->dev,
1469 "Failed to turn on bias: %d\n", ret);
1470 }
1471
Mark Brown56fba412011-06-04 11:25:10 +01001472 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1473 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001474 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1475 if (ret != 0)
1476 dev_err(d->dev,
1477 "Failed to prepare bias: %d\n", ret);
1478 }
1479}
1480
1481/* Async callback run prior to DAPM sequences - brings to their final
1482 * state.
1483 */
1484static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1485{
1486 struct snd_soc_dapm_context *d = data;
1487 int ret;
1488
1489 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001490 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1491 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1492 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001493 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1494 if (ret != 0)
1495 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1496 ret);
1497 }
1498
1499 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001500 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1501 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001502 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1503 if (ret != 0)
1504 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001505
1506 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001507 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001508 }
1509
1510 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001511 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1512 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001513 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1514 if (ret != 0)
1515 dev_err(d->dev, "Failed to apply active bias: %d\n",
1516 ret);
1517 }
1518}
Mark Brown97404f22010-12-14 16:13:57 +00001519
Mark Brownfe4fda52011-10-03 22:36:57 +01001520static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1521 bool power, bool connect)
1522{
1523 /* If a connection is being made or broken then that update
1524 * will have marked the peer dirty, otherwise the widgets are
1525 * not connected and this update has no impact. */
1526 if (!connect)
1527 return;
1528
1529 /* If the peer is already in the state we're moving to then we
1530 * won't have an impact on it. */
1531 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001532 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001533}
1534
Mark Brown05623c42011-09-28 17:02:31 +01001535static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1536 struct list_head *up_list,
1537 struct list_head *down_list)
1538{
Mark Browndb432b42011-10-03 21:06:40 +01001539 struct snd_soc_dapm_path *path;
1540
Mark Brown05623c42011-09-28 17:02:31 +01001541 if (w->power == power)
1542 return;
1543
1544 trace_snd_soc_dapm_widget_power(w, power);
1545
Mark Browndb432b42011-10-03 21:06:40 +01001546 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001547 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001548 */
1549 list_for_each_entry(path, &w->sources, list_sink) {
1550 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001551 dapm_widget_set_peer_power(path->source, power,
1552 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001553 }
1554 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001555 switch (w->id) {
1556 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001557 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001558 case snd_soc_dapm_clock_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001559 /* Supplies can't affect their outputs, only their inputs */
1560 break;
1561 default:
1562 list_for_each_entry(path, &w->sinks, list_source) {
1563 if (path->sink) {
1564 dapm_widget_set_peer_power(path->sink, power,
1565 path->connect);
1566 }
Mark Browndb432b42011-10-03 21:06:40 +01001567 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001568 break;
Mark Browndb432b42011-10-03 21:06:40 +01001569 }
1570
Mark Brown05623c42011-09-28 17:02:31 +01001571 if (power)
1572 dapm_seq_insert(w, up_list, true);
1573 else
1574 dapm_seq_insert(w, down_list, false);
1575
1576 w->power = power;
1577}
1578
Mark Brown7c81beb2011-09-20 22:22:32 +01001579static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1580 struct list_head *up_list,
1581 struct list_head *down_list)
1582{
Mark Brown7c81beb2011-09-20 22:22:32 +01001583 int power;
1584
1585 switch (w->id) {
1586 case snd_soc_dapm_pre:
1587 dapm_seq_insert(w, down_list, false);
1588 break;
1589 case snd_soc_dapm_post:
1590 dapm_seq_insert(w, up_list, true);
1591 break;
1592
1593 default:
Mark Brownd8050022011-09-28 18:28:23 +01001594 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001595
Mark Brown05623c42011-09-28 17:02:31 +01001596 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001597 break;
1598 }
1599}
1600
Mark Brown42aa3412009-03-01 19:21:10 +00001601/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001602 * Scan each dapm widget for complete audio path.
1603 * A complete path is a route that has valid endpoints i.e.:-
1604 *
1605 * o DAC to output pin.
1606 * o Input Pin to ADC.
1607 * o Input pin to Output pin (bypass, sidetone)
1608 * o DAC to ADC (loopback).
1609 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001610static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001611{
Mark Brown12ea2c72011-03-02 18:17:32 +00001612 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001613 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001614 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001615 LIST_HEAD(up_list);
1616 LIST_HEAD(down_list);
Dan Williams2955b472012-07-09 19:33:25 -07001617 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001618 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001619
Mark Brown84e90932010-11-04 00:07:02 -04001620 trace_snd_soc_dapm_start(card);
1621
Mark Brown56fba412011-06-04 11:25:10 +01001622 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001623 if (d->idle_bias_off)
1624 d->target_bias_level = SND_SOC_BIAS_OFF;
1625 else
1626 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001627 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001628
Liam Girdwood6c120e12012-02-15 15:15:34 +00001629 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001630
Mark Brown6d3ddc82009-05-16 17:47:29 +01001631 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001632 * lists indicating if they should be powered up or down. We
1633 * only check widgets that have been flagged as dirty but note
1634 * that new widgets may be added to the dirty list while we
1635 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001636 */
Mark Browndb432b42011-10-03 21:06:40 +01001637 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001638 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001639 }
1640
Mark Brownf9de6d72011-09-28 17:19:47 +01001641 list_for_each_entry(w, &card->widgets, list) {
Mark Brown0ff97eb2012-07-20 17:29:34 +01001642 switch (w->id) {
1643 case snd_soc_dapm_pre:
1644 case snd_soc_dapm_post:
1645 /* These widgets always need to be powered */
1646 break;
1647 default:
1648 list_del_init(&w->dirty);
1649 break;
1650 }
Mark Browndb432b42011-10-03 21:06:40 +01001651
Mark Brownf9de6d72011-09-28 17:19:47 +01001652 if (w->power) {
1653 d = w->dapm;
1654
1655 /* Supplies and micbiases only bring the
1656 * context up to STANDBY as unless something
1657 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001658 * generally don't require full power. Signal
1659 * generators are virtual pins and have no
1660 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001661 */
1662 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001663 case snd_soc_dapm_siggen:
1664 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001665 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001666 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02001667 case snd_soc_dapm_clock_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001668 case snd_soc_dapm_micbias:
1669 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1670 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1671 break;
1672 default:
1673 d->target_bias_level = SND_SOC_BIAS_ON;
1674 break;
1675 }
1676 }
1677
1678 }
1679
Mark Brown85a843c2011-09-21 21:29:47 +01001680 /* Force all contexts in the card to the same bias state if
1681 * they're not ground referenced.
1682 */
Mark Brown56fba412011-06-04 11:25:10 +01001683 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001684 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001685 if (d->target_bias_level > bias)
1686 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001687 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001688 if (!d->idle_bias_off)
1689 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001690
Mark Brownde02d072011-09-20 21:43:24 +01001691 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001692
Mark Brown9d0624a2011-02-18 11:49:43 -08001693 /* Run all the bias changes in parallel */
1694 list_for_each_entry(d, &dapm->card->dapm_list, list)
1695 async_schedule_domain(dapm_pre_sequence_async, d,
1696 &async_domain);
1697 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001698
Mark Brown6d3ddc82009-05-16 17:47:29 +01001699 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001700 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001701
Mark Brown97404f22010-12-14 16:13:57 +00001702 dapm_widget_update(dapm);
1703
Mark Brown6d3ddc82009-05-16 17:47:29 +01001704 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001705 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001706
Mark Brown9d0624a2011-02-18 11:49:43 -08001707 /* Run all the bias changes in parallel */
1708 list_for_each_entry(d, &dapm->card->dapm_list, list)
1709 async_schedule_domain(dapm_post_sequence_async, d,
1710 &async_domain);
1711 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001712
Liam Girdwood8078d872012-02-15 15:15:35 +00001713 /* do we need to notify any clients that DAPM event is complete */
1714 list_for_each_entry(d, &card->dapm_list, list) {
1715 if (d->stream_event)
1716 d->stream_event(d, event);
1717 }
1718
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001719 pop_dbg(dapm->dev, card->pop_time,
1720 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001721 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001722
Mark Brown84e90932010-11-04 00:07:02 -04001723 trace_snd_soc_dapm_done(card);
1724
Mark Brown42aa3412009-03-01 19:21:10 +00001725 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001726}
1727
Mark Brown79fb9382009-08-21 16:38:13 +01001728#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001729static ssize_t dapm_widget_power_read_file(struct file *file,
1730 char __user *user_buf,
1731 size_t count, loff_t *ppos)
1732{
1733 struct snd_soc_dapm_widget *w = file->private_data;
1734 char *buf;
1735 int in, out;
1736 ssize_t ret;
1737 struct snd_soc_dapm_path *p = NULL;
1738
1739 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1740 if (!buf)
1741 return -ENOMEM;
1742
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001743 in = is_connected_input_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001744 dapm_clear_walk(w->dapm);
Liam Girdwoodec2e3032012-04-18 11:41:11 +01001745 out = is_connected_output_ep(w, NULL);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001746 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001747
Mark Brownf13ebad2012-03-03 18:01:01 +00001748 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1749 w->name, w->power ? "On" : "Off",
1750 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001751
Mark Brownd033c362009-12-04 15:25:56 +00001752 if (w->reg >= 0)
1753 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1754 " - R%d(0x%x) bit %d",
1755 w->reg, w->reg, w->shift);
1756
1757 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1758
Mark Brown3eef08b2009-09-14 16:49:00 +01001759 if (w->sname)
1760 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1761 w->sname,
1762 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001763
1764 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001765 if (p->connected && !p->connected(w, p->sink))
1766 continue;
1767
Mark Brown79fb9382009-08-21 16:38:13 +01001768 if (p->connect)
1769 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001770 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001771 p->name ? p->name : "static",
1772 p->source->name);
1773 }
1774 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001775 if (p->connected && !p->connected(w, p->sink))
1776 continue;
1777
Mark Brown79fb9382009-08-21 16:38:13 +01001778 if (p->connect)
1779 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001780 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001781 p->name ? p->name : "static",
1782 p->sink->name);
1783 }
1784
1785 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1786
1787 kfree(buf);
1788 return ret;
1789}
1790
1791static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001792 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001793 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001794 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001795};
1796
Mark Brownef49e4f2011-04-04 20:48:13 +09001797static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1798 size_t count, loff_t *ppos)
1799{
1800 struct snd_soc_dapm_context *dapm = file->private_data;
1801 char *level;
1802
1803 switch (dapm->bias_level) {
1804 case SND_SOC_BIAS_ON:
1805 level = "On\n";
1806 break;
1807 case SND_SOC_BIAS_PREPARE:
1808 level = "Prepare\n";
1809 break;
1810 case SND_SOC_BIAS_STANDBY:
1811 level = "Standby\n";
1812 break;
1813 case SND_SOC_BIAS_OFF:
1814 level = "Off\n";
1815 break;
1816 default:
1817 BUG();
1818 level = "Unknown\n";
1819 break;
1820 }
1821
1822 return simple_read_from_buffer(user_buf, count, ppos, level,
1823 strlen(level));
1824}
1825
1826static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001827 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001828 .read = dapm_bias_read_file,
1829 .llseek = default_llseek,
1830};
1831
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001832void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1833 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001834{
Mark Brown79fb9382009-08-21 16:38:13 +01001835 struct dentry *d;
1836
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001837 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1838
1839 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001840 dev_warn(dapm->dev,
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001841 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001842 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001843 }
Mark Brown79fb9382009-08-21 16:38:13 +01001844
Mark Brownef49e4f2011-04-04 20:48:13 +09001845 d = debugfs_create_file("bias_level", 0444,
1846 dapm->debugfs_dapm, dapm,
1847 &dapm_bias_fops);
1848 if (!d)
1849 dev_warn(dapm->dev,
1850 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001851}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001852
1853static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1854{
1855 struct snd_soc_dapm_context *dapm = w->dapm;
1856 struct dentry *d;
1857
1858 if (!dapm->debugfs_dapm || !w->name)
1859 return;
1860
1861 d = debugfs_create_file(w->name, 0444,
1862 dapm->debugfs_dapm, w,
1863 &dapm_widget_power_fops);
1864 if (!d)
1865 dev_warn(w->dapm->dev,
1866 "ASoC: Failed to create %s debugfs file\n",
1867 w->name);
1868}
1869
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001870static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1871{
1872 debugfs_remove_recursive(dapm->debugfs_dapm);
1873}
1874
Mark Brown79fb9382009-08-21 16:38:13 +01001875#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001876void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1877 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001878{
1879}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001880
1881static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1882{
1883}
1884
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001885static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1886{
1887}
1888
Mark Brown79fb9382009-08-21 16:38:13 +01001889#endif
1890
Richard Purdie2b97eab2006-10-06 18:32:18 +02001891/* test and update the power status of a mux widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001892static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001893 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001894{
1895 struct snd_soc_dapm_path *path;
1896 int found = 0;
1897
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001898 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001899 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001900 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001901 return -ENODEV;
1902
Richard Purdie2b97eab2006-10-06 18:32:18 +02001903 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001904 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001905 if (path->kcontrol != kcontrol)
1906 continue;
1907
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001908 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001909 continue;
1910
1911 found = 1;
1912 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001913 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001914 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001915 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001916 } else {
1917 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001918 dapm_mark_dirty(path->source,
1919 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001920 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001921 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001922 }
1923
Mark Browndb432b42011-10-03 21:06:40 +01001924 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001925 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001926 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001927 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001928
Liam Girdwood618dae12012-04-25 12:12:51 +01001929 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001930}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001931
1932int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1933 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1934{
1935 struct snd_soc_card *card = widget->dapm->card;
1936 int ret;
1937
Liam Girdwood3cd04342012-03-09 12:02:08 +00001938 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001939 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1940 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001941 if (ret > 0)
1942 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001943 return ret;
1944}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001945EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001946
Milan plzik1b075e32008-01-10 14:39:46 +01001947/* test and update the power status of a mixer or switch widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001948static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001949 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001950{
1951 struct snd_soc_dapm_path *path;
1952 int found = 0;
1953
Milan plzik1b075e32008-01-10 14:39:46 +01001954 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001955 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001956 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001957 return -ENODEV;
1958
Richard Purdie2b97eab2006-10-06 18:32:18 +02001959 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001960 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001961 if (path->kcontrol != kcontrol)
1962 continue;
1963
1964 /* found, now check type */
1965 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001966 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001967 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001968 }
1969
Mark Browndb432b42011-10-03 21:06:40 +01001970 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001971 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001972 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001973 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001974
Liam Girdwood618dae12012-04-25 12:12:51 +01001975 return found;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001976}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001977
1978int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1979 struct snd_kcontrol *kcontrol, int connect)
1980{
1981 struct snd_soc_card *card = widget->dapm->card;
1982 int ret;
1983
Liam Girdwood3cd04342012-03-09 12:02:08 +00001984 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001985 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
1986 mutex_unlock(&card->dapm_mutex);
Liam Girdwood618dae12012-04-25 12:12:51 +01001987 if (ret > 0)
1988 soc_dpcm_runtime_update(widget);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001989 return ret;
1990}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001991EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001992
1993/* show dapm widget status in sys fs */
1994static ssize_t dapm_widget_show(struct device *dev,
1995 struct device_attribute *attr, char *buf)
1996{
Mark Brown36ae1a92012-01-06 17:12:45 -08001997 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001998 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001999 struct snd_soc_dapm_widget *w;
2000 int count = 0;
2001 char *state = "not set";
2002
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002003 list_for_each_entry(w, &codec->card->widgets, list) {
2004 if (w->dapm != &codec->dapm)
2005 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002006
2007 /* only display widgets that burnm power */
2008 switch (w->id) {
2009 case snd_soc_dapm_hp:
2010 case snd_soc_dapm_mic:
2011 case snd_soc_dapm_spk:
2012 case snd_soc_dapm_line:
2013 case snd_soc_dapm_micbias:
2014 case snd_soc_dapm_dac:
2015 case snd_soc_dapm_adc:
2016 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002017 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002018 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002019 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01002020 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002021 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002022 case snd_soc_dapm_clock_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002023 if (w->name)
2024 count += sprintf(buf + count, "%s: %s\n",
2025 w->name, w->power ? "On":"Off");
2026 break;
2027 default:
2028 break;
2029 }
2030 }
2031
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002032 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02002033 case SND_SOC_BIAS_ON:
2034 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002035 break;
Mark Brown0be98982008-05-19 12:31:28 +02002036 case SND_SOC_BIAS_PREPARE:
2037 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002038 break;
Mark Brown0be98982008-05-19 12:31:28 +02002039 case SND_SOC_BIAS_STANDBY:
2040 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002041 break;
Mark Brown0be98982008-05-19 12:31:28 +02002042 case SND_SOC_BIAS_OFF:
2043 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02002044 break;
2045 }
2046 count += sprintf(buf + count, "PM State: %s\n", state);
2047
2048 return count;
2049}
2050
2051static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2052
2053int snd_soc_dapm_sys_add(struct device *dev)
2054{
Troy Kisky12ef1932008-10-13 17:42:14 -07002055 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002056}
2057
2058static void snd_soc_dapm_sys_remove(struct device *dev)
2059{
Mark Brownaef90842009-05-16 17:53:16 +01002060 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002061}
2062
2063/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002064static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002065{
2066 struct snd_soc_dapm_widget *w, *next_w;
2067 struct snd_soc_dapm_path *p, *next_p;
2068
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002069 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2070 if (w->dapm != dapm)
2071 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002072 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002073 /*
2074 * remove source and sink paths associated to this widget.
2075 * While removing the path, remove reference to it from both
2076 * source and sink widgets so that path is removed only once.
2077 */
2078 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2079 list_del(&p->list_sink);
2080 list_del(&p->list_source);
2081 list_del(&p->list);
2082 kfree(p->long_name);
2083 kfree(p);
2084 }
2085 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2086 list_del(&p->list_sink);
2087 list_del(&p->list_source);
2088 list_del(&p->list);
2089 kfree(p->long_name);
2090 kfree(p);
2091 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002092 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002093 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002094 kfree(w);
2095 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002096}
2097
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002098static struct snd_soc_dapm_widget *dapm_find_widget(
2099 struct snd_soc_dapm_context *dapm, const char *pin,
2100 bool search_other_contexts)
2101{
2102 struct snd_soc_dapm_widget *w;
2103 struct snd_soc_dapm_widget *fallback = NULL;
2104
2105 list_for_each_entry(w, &dapm->card->widgets, list) {
2106 if (!strcmp(w->name, pin)) {
2107 if (w->dapm == dapm)
2108 return w;
2109 else
2110 fallback = w;
2111 }
2112 }
2113
2114 if (search_other_contexts)
2115 return fallback;
2116
2117 return NULL;
2118}
2119
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002120static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00002121 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01002122{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002123 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01002124
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002125 if (!w) {
2126 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2127 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01002128 }
2129
Mark Brown1a8b2d92012-02-16 11:50:07 -08002130 if (w->connected != status)
2131 dapm_mark_dirty(w, "pin configuration");
2132
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002133 w->connected = status;
2134 if (status == 0)
2135 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09002136
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002137 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01002138}
2139
Richard Purdie2b97eab2006-10-06 18:32:18 +02002140/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002141 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002142 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002143 *
2144 * Walks all dapm audio paths and powers widgets according to their
2145 * stream or path usage.
2146 *
2147 * Returns 0 for success.
2148 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002149int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002150{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002151 int ret;
2152
Mark Brown4f4c0072011-10-07 14:29:19 +01002153 /*
2154 * Suppress early reports (eg, jacks syncing their state) to avoid
2155 * silly DAPM runs during card startup.
2156 */
2157 if (!dapm->card || !dapm->card->instantiated)
2158 return 0;
2159
Liam Girdwood3cd04342012-03-09 12:02:08 +00002160 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002161 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2162 mutex_unlock(&dapm->card->dapm_mutex);
2163 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002164}
Liam Girdwooda5302182008-07-07 13:35:17 +01002165EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002166
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002167static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002168 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002169{
2170 struct snd_soc_dapm_path *path;
2171 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002172 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002173 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002174 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002175 const char *source;
2176 char prefixed_sink[80];
2177 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002178 int ret = 0;
2179
Mark Brown88e8b9a2011-03-02 18:18:24 +00002180 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002181 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2182 dapm->codec->name_prefix, route->sink);
2183 sink = prefixed_sink;
2184 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2185 dapm->codec->name_prefix, route->source);
2186 source = prefixed_source;
2187 } else {
2188 sink = route->sink;
2189 source = route->source;
2190 }
2191
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002192 /*
2193 * find src and dest widgets over all widgets but favor a widget from
2194 * current DAPM context
2195 */
2196 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002198 wtsink = w;
2199 if (w->dapm == dapm)
2200 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002201 continue;
2202 }
2203 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002204 wtsource = w;
2205 if (w->dapm == dapm)
2206 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002207 }
2208 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002209 /* use widget from another DAPM context if not found from this */
2210 if (!wsink)
2211 wsink = wtsink;
2212 if (!wsource)
2213 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002214
2215 if (wsource == NULL || wsink == NULL)
2216 return -ENODEV;
2217
2218 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2219 if (!path)
2220 return -ENOMEM;
2221
2222 path->source = wsource;
2223 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002224 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002225 INIT_LIST_HEAD(&path->list);
2226 INIT_LIST_HEAD(&path->list_source);
2227 INIT_LIST_HEAD(&path->list_sink);
2228
2229 /* check for external widgets */
2230 if (wsink->id == snd_soc_dapm_input) {
2231 if (wsource->id == snd_soc_dapm_micbias ||
2232 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002233 wsource->id == snd_soc_dapm_line ||
2234 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002235 wsink->ext = 1;
2236 }
2237 if (wsource->id == snd_soc_dapm_output) {
2238 if (wsink->id == snd_soc_dapm_spk ||
2239 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002240 wsink->id == snd_soc_dapm_line ||
2241 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002242 wsource->ext = 1;
2243 }
2244
2245 /* connect static paths */
2246 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002247 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248 list_add(&path->list_sink, &wsink->sources);
2249 list_add(&path->list_source, &wsource->sinks);
2250 path->connect = 1;
2251 return 0;
2252 }
2253
2254 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002255 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002256 case snd_soc_dapm_adc:
2257 case snd_soc_dapm_dac:
2258 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002259 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002260 case snd_soc_dapm_input:
2261 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002262 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002263 case snd_soc_dapm_micbias:
2264 case snd_soc_dapm_vmid:
2265 case snd_soc_dapm_pre:
2266 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002267 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002268 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02002269 case snd_soc_dapm_clock_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002270 case snd_soc_dapm_aif_in:
2271 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -08002272 case snd_soc_dapm_dai:
Mark Brownc74184e2012-04-04 22:12:09 +01002273 case snd_soc_dapm_dai_link:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002274 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275 list_add(&path->list_sink, &wsink->sources);
2276 list_add(&path->list_source, &wsource->sinks);
2277 path->connect = 1;
2278 return 0;
2279 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002280 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002281 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002282 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002283 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002284 if (ret != 0)
2285 goto err;
2286 break;
2287 case snd_soc_dapm_switch:
2288 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002289 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002290 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002291 if (ret != 0)
2292 goto err;
2293 break;
2294 case snd_soc_dapm_hp:
2295 case snd_soc_dapm_mic:
2296 case snd_soc_dapm_line:
2297 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002298 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002299 list_add(&path->list_sink, &wsink->sources);
2300 list_add(&path->list_source, &wsource->sinks);
2301 path->connect = 0;
2302 return 0;
2303 }
Mark Brownfabd0382012-07-05 17:20:06 +01002304
2305 dapm_mark_dirty(wsource, "Route added");
2306 dapm_mark_dirty(wsink, "Route added");
2307
Richard Purdie2b97eab2006-10-06 18:32:18 +02002308 return 0;
2309
2310err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002311 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2312 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002313 kfree(path);
2314 return ret;
2315}
Mark Brown105f1c22008-05-13 14:52:19 +02002316
Mark Brownefcc3c62012-07-05 17:24:19 +01002317static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2318 const struct snd_soc_dapm_route *route)
2319{
2320 struct snd_soc_dapm_path *path, *p;
2321 const char *sink;
2322 const char *source;
2323 char prefixed_sink[80];
2324 char prefixed_source[80];
2325
2326 if (route->control) {
2327 dev_err(dapm->dev,
2328 "Removal of routes with controls not supported\n");
2329 return -EINVAL;
2330 }
2331
2332 if (dapm->codec && dapm->codec->name_prefix) {
2333 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2334 dapm->codec->name_prefix, route->sink);
2335 sink = prefixed_sink;
2336 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2337 dapm->codec->name_prefix, route->source);
2338 source = prefixed_source;
2339 } else {
2340 sink = route->sink;
2341 source = route->source;
2342 }
2343
2344 path = NULL;
2345 list_for_each_entry(p, &dapm->card->paths, list) {
2346 if (strcmp(p->source->name, source) != 0)
2347 continue;
2348 if (strcmp(p->sink->name, sink) != 0)
2349 continue;
2350 path = p;
2351 break;
2352 }
2353
2354 if (path) {
2355 dapm_mark_dirty(path->source, "Route removed");
2356 dapm_mark_dirty(path->sink, "Route removed");
2357
2358 list_del(&path->list);
2359 list_del(&path->list_sink);
2360 list_del(&path->list_source);
2361 kfree(path);
2362 } else {
2363 dev_warn(dapm->dev, "Route %s->%s does not exist\n",
2364 source, sink);
2365 }
2366
2367 return 0;
2368}
2369
Mark Brown105f1c22008-05-13 14:52:19 +02002370/**
Mark Brown105f1c22008-05-13 14:52:19 +02002371 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002372 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002373 * @route: audio routes
2374 * @num: number of routes
2375 *
2376 * Connects 2 dapm widgets together via a named audio path. The sink is
2377 * the widget receiving the audio signal, whilst the source is the sender
2378 * of the audio signal.
2379 *
2380 * Returns 0 for success else error. On error all resources can be freed
2381 * with a call to snd_soc_card_free().
2382 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002383int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002384 const struct snd_soc_dapm_route *route, int num)
2385{
Mark Brown62d4a4b2012-06-22 12:21:49 +01002386 int i, r, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002387
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002388 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002389 for (i = 0; i < num; i++) {
Mark Brown62d4a4b2012-06-22 12:21:49 +01002390 r = snd_soc_dapm_add_route(dapm, route);
2391 if (r < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002392 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2393 route->source, route->sink);
Mark Brown62d4a4b2012-06-22 12:21:49 +01002394 ret = r;
Mark Brown105f1c22008-05-13 14:52:19 +02002395 }
2396 route++;
2397 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002398 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002399
Dan Carpenter60884c22012-04-13 22:25:43 +03002400 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002401}
2402EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2403
Mark Brownefcc3c62012-07-05 17:24:19 +01002404/**
2405 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2406 * @dapm: DAPM context
2407 * @route: audio routes
2408 * @num: number of routes
2409 *
2410 * Removes routes from the DAPM context.
2411 */
2412int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2413 const struct snd_soc_dapm_route *route, int num)
2414{
2415 int i, ret = 0;
2416
2417 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2418 for (i = 0; i < num; i++) {
2419 snd_soc_dapm_del_route(dapm, route);
2420 route++;
2421 }
2422 mutex_unlock(&dapm->card->dapm_mutex);
2423
2424 return ret;
2425}
2426EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2427
Mark Brownbf3a9e12011-06-13 16:42:29 +01002428static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2429 const struct snd_soc_dapm_route *route)
2430{
2431 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2432 route->source,
2433 true);
2434 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2435 route->sink,
2436 true);
2437 struct snd_soc_dapm_path *path;
2438 int count = 0;
2439
2440 if (!source) {
2441 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2442 route->source);
2443 return -ENODEV;
2444 }
2445
2446 if (!sink) {
2447 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2448 route->sink);
2449 return -ENODEV;
2450 }
2451
2452 if (route->control || route->connected)
2453 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2454 route->source, route->sink);
2455
2456 list_for_each_entry(path, &source->sinks, list_source) {
2457 if (path->sink == sink) {
2458 path->weak = 1;
2459 count++;
2460 }
2461 }
2462
2463 if (count == 0)
2464 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2465 route->source, route->sink);
2466 if (count > 1)
2467 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2468 count, route->source, route->sink);
2469
2470 return 0;
2471}
2472
2473/**
2474 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2475 * @dapm: DAPM context
2476 * @route: audio routes
2477 * @num: number of routes
2478 *
2479 * Mark existing routes matching those specified in the passed array
2480 * as being weak, meaning that they are ignored for the purpose of
2481 * power decisions. The main intended use case is for sidetone paths
2482 * which couple audio between other independent paths if they are both
2483 * active in order to make the combination work better at the user
2484 * level but which aren't intended to be "used".
2485 *
2486 * Note that CODEC drivers should not use this as sidetone type paths
2487 * can frequently also be used as bypass paths.
2488 */
2489int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2490 const struct snd_soc_dapm_route *route, int num)
2491{
2492 int i, err;
2493 int ret = 0;
2494
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002495 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002496 for (i = 0; i < num; i++) {
2497 err = snd_soc_dapm_weak_route(dapm, route);
2498 if (err)
2499 ret = err;
2500 route++;
2501 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002502 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002503
2504 return ret;
2505}
2506EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2507
Mark Brown105f1c22008-05-13 14:52:19 +02002508/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002509 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002510 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002511 *
2512 * Checks the codec for any new dapm widgets and creates them if found.
2513 *
2514 * Returns 0 for success.
2515 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002516int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002517{
2518 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002519 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002520
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002521 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2522
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002523 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002524 {
2525 if (w->new)
2526 continue;
2527
Stephen Warrenfad59882011-04-28 17:37:59 -06002528 if (w->num_kcontrols) {
2529 w->kcontrols = kzalloc(w->num_kcontrols *
2530 sizeof(struct snd_kcontrol *),
2531 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002532 if (!w->kcontrols) {
2533 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002534 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002535 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002536 }
2537
Richard Purdie2b97eab2006-10-06 18:32:18 +02002538 switch(w->id) {
2539 case snd_soc_dapm_switch:
2540 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002541 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002542 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002543 break;
2544 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002545 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002546 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002547 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002548 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002549 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002550 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002551 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002552 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002553 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002554 break;
2555 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002556
2557 /* Read the initial power state from the device */
2558 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002559 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002560 val &= 1 << w->shift;
2561 if (w->invert)
2562 val = !val;
2563
2564 if (val)
2565 w->power = 1;
2566 }
2567
Richard Purdie2b97eab2006-10-06 18:32:18 +02002568 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002569
Mark Brown7508b122011-10-05 12:09:12 +01002570 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002571 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002572 }
2573
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002574 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002575 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002576 return 0;
2577}
2578EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2579
2580/**
2581 * snd_soc_dapm_get_volsw - dapm mixer get callback
2582 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002583 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002584 *
2585 * Callback to get the value of a dapm mixer control.
2586 *
2587 * Returns 0 for success.
2588 */
2589int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2590 struct snd_ctl_elem_value *ucontrol)
2591{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002592 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2593 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002594 struct soc_mixer_control *mc =
2595 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002596 unsigned int reg = mc->reg;
2597 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002598 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002599 unsigned int mask = (1 << fls(max)) - 1;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002600 unsigned int invert = mc->invert;
2601
2602 if (snd_soc_volsw_is_stereo(mc))
2603 dev_warn(widget->dapm->dev,
2604 "Control '%s' is stereo, which is not supported\n",
2605 kcontrol->id.name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002606
Richard Purdie2b97eab2006-10-06 18:32:18 +02002607 ucontrol->value.integer.value[0] =
2608 (snd_soc_read(widget->codec, reg) >> shift) & mask;
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002609 if (invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002610 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002611 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002612
2613 return 0;
2614}
2615EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2616
2617/**
2618 * snd_soc_dapm_put_volsw - dapm mixer set callback
2619 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002620 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002621 *
2622 * Callback to set the value of a dapm mixer control.
2623 *
2624 * Returns 0 for success.
2625 */
2626int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2627 struct snd_ctl_elem_value *ucontrol)
2628{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002629 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2630 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2631 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002632 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002633 struct soc_mixer_control *mc =
2634 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002635 unsigned int reg = mc->reg;
2636 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002637 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002638 unsigned int mask = (1 << fls(max)) - 1;
2639 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002640 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002641 int connect, change;
2642 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002643 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002644
Benoît Thébaudeauda602ab2012-07-03 20:18:17 +02002645 if (snd_soc_volsw_is_stereo(mc))
2646 dev_warn(widget->dapm->dev,
2647 "Control '%s' is stereo, which is not supported\n",
2648 kcontrol->id.name);
2649
Richard Purdie2b97eab2006-10-06 18:32:18 +02002650 val = (ucontrol->value.integer.value[0] & mask);
Benoît Thébaudeau8a720712012-06-18 22:41:28 +02002651 connect = !!val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002652
2653 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002654 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002655 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002656 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002657
Liam Girdwood3cd04342012-03-09 12:02:08 +00002658 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002659
Stephen Warrene9cf7042011-01-27 14:54:05 -07002660 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002661 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002662 for (wi = 0; wi < wlist->num_widgets; wi++) {
2663 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002664
Stephen Warrenfafd2172011-04-28 17:38:00 -06002665 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002666
Stephen Warrenfafd2172011-04-28 17:38:00 -06002667 update.kcontrol = kcontrol;
2668 update.widget = widget;
2669 update.reg = reg;
2670 update.mask = mask;
2671 update.val = val;
2672 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002673
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002674 soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002675
2676 widget->dapm->update = NULL;
2677 }
Mark Brown283375c2009-12-07 18:09:03 +00002678 }
2679
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002680 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002681 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002682}
2683EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2684
2685/**
2686 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2687 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002688 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002689 *
2690 * Callback to get the value of a dapm enumerated double mixer control.
2691 *
2692 * Returns 0 for success.
2693 */
2694int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2695 struct snd_ctl_elem_value *ucontrol)
2696{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002697 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2698 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002699 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002700 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002701
Richard Purdie2b97eab2006-10-06 18:32:18 +02002702 val = snd_soc_read(widget->codec, e->reg);
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002703 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002704 if (e->shift_l != e->shift_r)
2705 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002706 (val >> e->shift_r) & e->mask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002707
2708 return 0;
2709}
2710EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2711
2712/**
2713 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2714 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002715 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002716 *
2717 * Callback to set the value of a dapm enumerated double mixer control.
2718 *
2719 * Returns 0 for success.
2720 */
2721int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2722 struct snd_ctl_elem_value *ucontrol)
2723{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002724 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2725 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2726 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002727 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002728 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002729 unsigned int val, mux, change;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002730 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002731 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002732 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002733
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002734 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002735 return -EINVAL;
2736 mux = ucontrol->value.enumerated.item[0];
2737 val = mux << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002738 mask = e->mask << e->shift_l;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002739 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002740 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002741 return -EINVAL;
2742 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002743 mask |= e->mask << e->shift_r;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002744 }
2745
Liam Girdwood3cd04342012-03-09 12:02:08 +00002746 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002747
Mark Brown3a655772009-10-05 17:23:30 +01002748 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002749 if (change) {
2750 for (wi = 0; wi < wlist->num_widgets; wi++) {
2751 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002752
Stephen Warrenfafd2172011-04-28 17:38:00 -06002753 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002754
Stephen Warrenfafd2172011-04-28 17:38:00 -06002755 update.kcontrol = kcontrol;
2756 update.widget = widget;
2757 update.reg = e->reg;
2758 update.mask = mask;
2759 update.val = val;
2760 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002761
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002762 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002763
Stephen Warrenfafd2172011-04-28 17:38:00 -06002764 widget->dapm->update = NULL;
2765 }
2766 }
2767
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002768 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002769 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002770}
2771EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2772
2773/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002774 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2775 * @kcontrol: mixer control
2776 * @ucontrol: control element information
2777 *
2778 * Returns 0 for success.
2779 */
2780int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2781 struct snd_ctl_elem_value *ucontrol)
2782{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002783 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2784 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002785
2786 ucontrol->value.enumerated.item[0] = widget->value;
2787
2788 return 0;
2789}
2790EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2791
2792/**
2793 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2794 * @kcontrol: mixer control
2795 * @ucontrol: control element information
2796 *
2797 * Returns 0 for success.
2798 */
2799int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2800 struct snd_ctl_elem_value *ucontrol)
2801{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002802 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2803 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2804 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002805 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002806 struct soc_enum *e =
2807 (struct soc_enum *)kcontrol->private_value;
2808 int change;
2809 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002810 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002811
2812 if (ucontrol->value.enumerated.item[0] >= e->max)
2813 return -EINVAL;
2814
Liam Girdwood3cd04342012-03-09 12:02:08 +00002815 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002816
2817 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002818 if (change) {
2819 for (wi = 0; wi < wlist->num_widgets; wi++) {
2820 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002821
Stephen Warrenfafd2172011-04-28 17:38:00 -06002822 widget->value = ucontrol->value.enumerated.item[0];
2823
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002824 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002825 }
2826 }
2827
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002828 mutex_unlock(&card->dapm_mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002829 return ret;
2830}
2831EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2832
2833/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002834 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2835 * callback
2836 * @kcontrol: mixer control
2837 * @ucontrol: control element information
2838 *
2839 * Callback to get the value of a dapm semi enumerated double mixer control.
2840 *
2841 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2842 * used for handling bitfield coded enumeration for example.
2843 *
2844 * Returns 0 for success.
2845 */
2846int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2847 struct snd_ctl_elem_value *ucontrol)
2848{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002849 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2850 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002851 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002852 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002853
2854 reg_val = snd_soc_read(widget->codec, e->reg);
2855 val = (reg_val >> e->shift_l) & e->mask;
2856 for (mux = 0; mux < e->max; mux++) {
2857 if (val == e->values[mux])
2858 break;
2859 }
2860 ucontrol->value.enumerated.item[0] = mux;
2861 if (e->shift_l != e->shift_r) {
2862 val = (reg_val >> e->shift_r) & e->mask;
2863 for (mux = 0; mux < e->max; mux++) {
2864 if (val == e->values[mux])
2865 break;
2866 }
2867 ucontrol->value.enumerated.item[1] = mux;
2868 }
2869
2870 return 0;
2871}
2872EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2873
2874/**
2875 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2876 * callback
2877 * @kcontrol: mixer control
2878 * @ucontrol: control element information
2879 *
2880 * Callback to set the value of a dapm semi enumerated double mixer control.
2881 *
2882 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2883 * used for handling bitfield coded enumeration for example.
2884 *
2885 * Returns 0 for success.
2886 */
2887int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2888 struct snd_ctl_elem_value *ucontrol)
2889{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002890 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2891 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2892 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002893 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002894 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002895 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002896 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002897 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002898 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002899
2900 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2901 return -EINVAL;
2902 mux = ucontrol->value.enumerated.item[0];
2903 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2904 mask = e->mask << e->shift_l;
2905 if (e->shift_l != e->shift_r) {
2906 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2907 return -EINVAL;
2908 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2909 mask |= e->mask << e->shift_r;
2910 }
2911
Liam Girdwood3cd04342012-03-09 12:02:08 +00002912 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002913
Mark Brown3a655772009-10-05 17:23:30 +01002914 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002915 if (change) {
2916 for (wi = 0; wi < wlist->num_widgets; wi++) {
2917 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002918
Stephen Warrenfafd2172011-04-28 17:38:00 -06002919 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002920
Stephen Warrenfafd2172011-04-28 17:38:00 -06002921 update.kcontrol = kcontrol;
2922 update.widget = widget;
2923 update.reg = e->reg;
2924 update.mask = mask;
2925 update.val = val;
2926 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002927
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002928 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002929
Stephen Warrenfafd2172011-04-28 17:38:00 -06002930 widget->dapm->update = NULL;
2931 }
2932 }
2933
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002934 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002935 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002936}
2937EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2938
2939/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002940 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2941 *
2942 * @kcontrol: mixer control
2943 * @uinfo: control element information
2944 *
2945 * Callback to provide information about a pin switch control.
2946 */
2947int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2948 struct snd_ctl_elem_info *uinfo)
2949{
2950 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2951 uinfo->count = 1;
2952 uinfo->value.integer.min = 0;
2953 uinfo->value.integer.max = 1;
2954
2955 return 0;
2956}
2957EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2958
2959/**
2960 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2961 *
2962 * @kcontrol: mixer control
2963 * @ucontrol: Value
2964 */
2965int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2966 struct snd_ctl_elem_value *ucontrol)
2967{
Mark Brown48a8c392012-02-14 17:11:15 -08002968 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002969 const char *pin = (const char *)kcontrol->private_value;
2970
Liam Girdwood3cd04342012-03-09 12:02:08 +00002971 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002972
2973 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002974 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002975
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002976 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002977
2978 return 0;
2979}
2980EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2981
2982/**
2983 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2984 *
2985 * @kcontrol: mixer control
2986 * @ucontrol: Value
2987 */
2988int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2989 struct snd_ctl_elem_value *ucontrol)
2990{
Mark Brown48a8c392012-02-14 17:11:15 -08002991 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002992 const char *pin = (const char *)kcontrol->private_value;
2993
Liam Girdwood3cd04342012-03-09 12:02:08 +00002994 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002995
2996 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002997 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002998 else
Mark Brown48a8c392012-02-14 17:11:15 -08002999 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003000
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003001 mutex_unlock(&card->dapm_mutex);
3002
Mark Brown48a8c392012-02-14 17:11:15 -08003003 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00003004 return 0;
3005}
3006EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3007
Mark Brown5ba06fc2012-02-16 11:07:13 -08003008static struct snd_soc_dapm_widget *
3009snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3010 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003011{
3012 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003013 size_t name_len;
Mark Brown62ea8742012-01-21 21:14:48 +00003014 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003015
3016 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08003017 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003018
Mark Brown62ea8742012-01-21 21:14:48 +00003019 switch (w->id) {
3020 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00003021 w->regulator = devm_regulator_get(dapm->dev, w->name);
3022 if (IS_ERR(w->regulator)) {
3023 ret = PTR_ERR(w->regulator);
Mark Brown62ea8742012-01-21 21:14:48 +00003024 dev_err(dapm->dev, "Failed to request %s: %d\n",
3025 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003026 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00003027 }
3028 break;
Ola Liljad7e7eb92012-05-24 15:26:25 +02003029 case snd_soc_dapm_clock_supply:
Mark Brown165961e2012-06-05 10:44:23 +01003030#ifdef CONFIG_CLKDEV_LOOKUP
Mark Brown695594f12012-06-04 08:14:13 +01003031 w->clk = devm_clk_get(dapm->dev, w->name);
Ola Liljad7e7eb92012-05-24 15:26:25 +02003032 if (IS_ERR(w->clk)) {
3033 ret = PTR_ERR(w->clk);
3034 dev_err(dapm->dev, "Failed to request %s: %d\n",
3035 w->name, ret);
3036 return NULL;
3037 }
Mark Brownec029952012-06-04 08:16:20 +01003038#else
3039 return NULL;
3040#endif
Ola Liljad7e7eb92012-05-24 15:26:25 +02003041 break;
Mark Brown62ea8742012-01-21 21:14:48 +00003042 default:
3043 break;
3044 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003045
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003046 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00003047 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003048 name_len += 1 + strlen(dapm->codec->name_prefix);
3049 w->name = kmalloc(name_len, GFP_KERNEL);
3050 if (w->name == NULL) {
3051 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08003052 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003053 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00003054 if (dapm->codec && dapm->codec->name_prefix)
Mark Brown888df392012-02-16 19:37:51 -08003055 snprintf((char *)w->name, name_len, "%s %s",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003056 dapm->codec->name_prefix, widget->name);
3057 else
Mark Brown888df392012-02-16 19:37:51 -08003058 snprintf((char *)w->name, name_len, "%s", widget->name);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02003059
Mark Brown7ca3a182011-10-08 14:04:50 +01003060 switch (w->id) {
3061 case snd_soc_dapm_switch:
3062 case snd_soc_dapm_mixer:
3063 case snd_soc_dapm_mixer_named_ctl:
3064 w->power_check = dapm_generic_check_power;
3065 break;
3066 case snd_soc_dapm_mux:
3067 case snd_soc_dapm_virt_mux:
3068 case snd_soc_dapm_value_mux:
3069 w->power_check = dapm_generic_check_power;
3070 break;
3071 case snd_soc_dapm_adc:
3072 case snd_soc_dapm_aif_out:
3073 w->power_check = dapm_adc_check_power;
3074 break;
3075 case snd_soc_dapm_dac:
3076 case snd_soc_dapm_aif_in:
3077 w->power_check = dapm_dac_check_power;
3078 break;
3079 case snd_soc_dapm_pga:
3080 case snd_soc_dapm_out_drv:
3081 case snd_soc_dapm_input:
3082 case snd_soc_dapm_output:
3083 case snd_soc_dapm_micbias:
3084 case snd_soc_dapm_spk:
3085 case snd_soc_dapm_hp:
3086 case snd_soc_dapm_mic:
3087 case snd_soc_dapm_line:
Mark Brownc74184e2012-04-04 22:12:09 +01003088 case snd_soc_dapm_dai_link:
Mark Brown7ca3a182011-10-08 14:04:50 +01003089 w->power_check = dapm_generic_check_power;
3090 break;
3091 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00003092 case snd_soc_dapm_regulator_supply:
Ola Liljad7e7eb92012-05-24 15:26:25 +02003093 case snd_soc_dapm_clock_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01003094 w->power_check = dapm_supply_check_power;
3095 break;
Mark Brown888df392012-02-16 19:37:51 -08003096 case snd_soc_dapm_dai:
3097 w->power_check = dapm_dai_check_power;
3098 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01003099 default:
3100 w->power_check = dapm_always_on_check_power;
3101 break;
3102 }
3103
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003104 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003105 w->dapm = dapm;
3106 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003107 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003108 INIT_LIST_HEAD(&w->sources);
3109 INIT_LIST_HEAD(&w->sinks);
3110 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01003111 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003112 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003113
3114 /* machine layer set ups unconnected pins and insertions */
3115 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08003116 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003117}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003118
3119/**
Mark Brown4ba13272008-05-13 14:51:19 +02003120 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003121 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02003122 * @widget: widget array
3123 * @num: number of widgets
3124 *
3125 * Creates new DAPM controls based upon the templates.
3126 *
3127 * Returns 0 for success else error.
3128 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003129int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02003130 const struct snd_soc_dapm_widget *widget,
3131 int num)
3132{
Mark Brown5ba06fc2012-02-16 11:07:13 -08003133 struct snd_soc_dapm_widget *w;
3134 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03003135 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02003136
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003137 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02003138 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08003139 w = snd_soc_dapm_new_control(dapm, widget);
3140 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02003141 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08003142 "ASoC: Failed to create DAPM control %s\n",
3143 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03003144 ret = -ENOMEM;
3145 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00003146 }
Mark Brown4ba13272008-05-13 14:51:19 +02003147 widget++;
3148 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003149 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03003150 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02003151}
3152EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3153
Mark Brownc74184e2012-04-04 22:12:09 +01003154static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3155 struct snd_kcontrol *kcontrol, int event)
3156{
3157 struct snd_soc_dapm_path *source_p, *sink_p;
3158 struct snd_soc_dai *source, *sink;
3159 const struct snd_soc_pcm_stream *config = w->params;
3160 struct snd_pcm_substream substream;
Mark Brown9747cec2012-04-26 19:12:21 +01003161 struct snd_pcm_hw_params *params = NULL;
Mark Brownc74184e2012-04-04 22:12:09 +01003162 u64 fmt;
3163 int ret;
3164
3165 BUG_ON(!config);
3166 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3167
3168 /* We only support a single source and sink, pick the first */
3169 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3170 list_sink);
3171 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3172 list_source);
3173
3174 BUG_ON(!source_p || !sink_p);
3175 BUG_ON(!sink_p->source || !source_p->sink);
3176 BUG_ON(!source_p->source || !sink_p->sink);
3177
3178 source = source_p->source->priv;
3179 sink = sink_p->sink->priv;
3180
3181 /* Be a little careful as we don't want to overflow the mask array */
3182 if (config->formats) {
3183 fmt = ffs(config->formats) - 1;
3184 } else {
Fabio Estevam516541a2012-04-16 10:53:09 -03003185 dev_warn(w->dapm->dev, "Invalid format %llx specified\n",
Mark Brownc74184e2012-04-04 22:12:09 +01003186 config->formats);
3187 fmt = 0;
3188 }
3189
3190 /* Currently very limited parameter selection */
Mark Brown9747cec2012-04-26 19:12:21 +01003191 params = kzalloc(sizeof(*params), GFP_KERNEL);
3192 if (!params) {
3193 ret = -ENOMEM;
3194 goto out;
3195 }
3196 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
Mark Brownc74184e2012-04-04 22:12:09 +01003197
Mark Brown9747cec2012-04-26 19:12:21 +01003198 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
Mark Brownc74184e2012-04-04 22:12:09 +01003199 config->rate_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003200 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
Mark Brownc74184e2012-04-04 22:12:09 +01003201 config->rate_max;
3202
Mark Brown9747cec2012-04-26 19:12:21 +01003203 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
Mark Brownc74184e2012-04-04 22:12:09 +01003204 = config->channels_min;
Mark Brown9747cec2012-04-26 19:12:21 +01003205 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
Mark Brownc74184e2012-04-04 22:12:09 +01003206 = config->channels_max;
3207
3208 memset(&substream, 0, sizeof(substream));
3209
3210 switch (event) {
3211 case SND_SOC_DAPM_PRE_PMU:
3212 if (source->driver->ops && source->driver->ops->hw_params) {
3213 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3214 ret = source->driver->ops->hw_params(&substream,
Mark Brown9747cec2012-04-26 19:12:21 +01003215 params, source);
Mark Brownc74184e2012-04-04 22:12:09 +01003216 if (ret != 0) {
3217 dev_err(source->dev,
3218 "hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003219 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003220 }
3221 }
3222
3223 if (sink->driver->ops && sink->driver->ops->hw_params) {
3224 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
Mark Brown9747cec2012-04-26 19:12:21 +01003225 ret = sink->driver->ops->hw_params(&substream, params,
Mark Brownc74184e2012-04-04 22:12:09 +01003226 sink);
3227 if (ret != 0) {
3228 dev_err(sink->dev,
3229 "hw_params() failed: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003230 goto out;
Mark Brownc74184e2012-04-04 22:12:09 +01003231 }
3232 }
3233 break;
3234
3235 case SND_SOC_DAPM_POST_PMU:
3236 ret = snd_soc_dai_digital_mute(sink, 0);
3237 if (ret != 0 && ret != -ENOTSUPP)
3238 dev_warn(sink->dev, "Failed to unmute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003239 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003240 break;
3241
3242 case SND_SOC_DAPM_PRE_PMD:
3243 ret = snd_soc_dai_digital_mute(sink, 1);
3244 if (ret != 0 && ret != -ENOTSUPP)
3245 dev_warn(sink->dev, "Failed to mute: %d\n", ret);
Mark Brown9747cec2012-04-26 19:12:21 +01003246 ret = 0;
Mark Brownc74184e2012-04-04 22:12:09 +01003247 break;
3248
3249 default:
3250 BUG();
3251 return -EINVAL;
3252 }
3253
Mark Brown9747cec2012-04-26 19:12:21 +01003254out:
3255 kfree(params);
3256 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01003257}
3258
3259int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3260 const struct snd_soc_pcm_stream *params,
3261 struct snd_soc_dapm_widget *source,
3262 struct snd_soc_dapm_widget *sink)
3263{
3264 struct snd_soc_dapm_route routes[2];
3265 struct snd_soc_dapm_widget template;
3266 struct snd_soc_dapm_widget *w;
3267 size_t len;
3268 char *link_name;
3269
3270 len = strlen(source->name) + strlen(sink->name) + 2;
3271 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3272 if (!link_name)
3273 return -ENOMEM;
3274 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3275
3276 memset(&template, 0, sizeof(template));
3277 template.reg = SND_SOC_NOPM;
3278 template.id = snd_soc_dapm_dai_link;
3279 template.name = link_name;
3280 template.event = snd_soc_dai_link_event;
3281 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3282 SND_SOC_DAPM_PRE_PMD;
3283
3284 dev_dbg(card->dev, "adding %s widget\n", link_name);
3285
3286 w = snd_soc_dapm_new_control(&card->dapm, &template);
3287 if (!w) {
3288 dev_err(card->dev, "Failed to create %s widget\n",
3289 link_name);
3290 return -ENOMEM;
3291 }
3292
3293 w->params = params;
3294
3295 memset(&routes, 0, sizeof(routes));
3296
3297 routes[0].source = source->name;
3298 routes[0].sink = link_name;
3299 routes[1].source = link_name;
3300 routes[1].sink = sink->name;
3301
3302 return snd_soc_dapm_add_routes(&card->dapm, routes,
3303 ARRAY_SIZE(routes));
3304}
3305
Mark Brown888df392012-02-16 19:37:51 -08003306int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3307 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003308{
Mark Brown888df392012-02-16 19:37:51 -08003309 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003310 struct snd_soc_dapm_widget *w;
3311
Mark Brown888df392012-02-16 19:37:51 -08003312 WARN_ON(dapm->dev != dai->dev);
3313
3314 memset(&template, 0, sizeof(template));
3315 template.reg = SND_SOC_NOPM;
3316
3317 if (dai->driver->playback.stream_name) {
3318 template.id = snd_soc_dapm_dai;
3319 template.name = dai->driver->playback.stream_name;
3320 template.sname = dai->driver->playback.stream_name;
3321
3322 dev_dbg(dai->dev, "adding %s widget\n",
3323 template.name);
3324
3325 w = snd_soc_dapm_new_control(dapm, &template);
3326 if (!w) {
3327 dev_err(dapm->dev, "Failed to create %s widget\n",
3328 dai->driver->playback.stream_name);
3329 }
3330
3331 w->priv = dai;
3332 dai->playback_widget = w;
3333 }
3334
3335 if (dai->driver->capture.stream_name) {
3336 template.id = snd_soc_dapm_dai;
3337 template.name = dai->driver->capture.stream_name;
3338 template.sname = dai->driver->capture.stream_name;
3339
3340 dev_dbg(dai->dev, "adding %s widget\n",
3341 template.name);
3342
3343 w = snd_soc_dapm_new_control(dapm, &template);
3344 if (!w) {
3345 dev_err(dapm->dev, "Failed to create %s widget\n",
3346 dai->driver->capture.stream_name);
3347 }
3348
3349 w->priv = dai;
3350 dai->capture_widget = w;
3351 }
3352
3353 return 0;
3354}
3355
3356int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3357{
3358 struct snd_soc_dapm_widget *dai_w, *w;
3359 struct snd_soc_dai *dai;
3360 struct snd_soc_dapm_route r;
3361
3362 memset(&r, 0, sizeof(r));
3363
3364 /* For each DAI widget... */
3365 list_for_each_entry(dai_w, &card->widgets, list) {
3366 if (dai_w->id != snd_soc_dapm_dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003367 continue;
Mark Brown888df392012-02-16 19:37:51 -08003368
3369 dai = dai_w->priv;
3370
3371 /* ...find all widgets with the same stream and link them */
3372 list_for_each_entry(w, &card->widgets, list) {
3373 if (w->dapm != dai_w->dapm)
3374 continue;
3375
3376 if (w->id == snd_soc_dapm_dai)
3377 continue;
3378
3379 if (!w->sname)
3380 continue;
3381
3382 if (dai->driver->playback.stream_name &&
3383 strstr(w->sname,
3384 dai->driver->playback.stream_name)) {
3385 r.source = dai->playback_widget->name;
3386 r.sink = w->name;
3387 dev_dbg(dai->dev, "%s -> %s\n",
3388 r.source, r.sink);
3389
3390 snd_soc_dapm_add_route(w->dapm, &r);
3391 }
3392
3393 if (dai->driver->capture.stream_name &&
3394 strstr(w->sname,
3395 dai->driver->capture.stream_name)) {
3396 r.source = w->name;
3397 r.sink = dai->capture_widget->name;
3398 dev_dbg(dai->dev, "%s -> %s\n",
3399 r.source, r.sink);
3400
3401 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003402 }
3403 }
3404 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003405
Mark Brown888df392012-02-16 19:37:51 -08003406 return 0;
3407}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003408
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003409static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3410 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003411{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003412
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003413 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3414 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3415 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003416
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003417 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3418 w_cpu = cpu_dai->playback_widget;
3419 w_codec = codec_dai->playback_widget;
3420 } else {
3421 w_cpu = cpu_dai->capture_widget;
3422 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003423 }
3424
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003425 if (w_cpu) {
3426
3427 dapm_mark_dirty(w_cpu, "stream event");
3428
3429 switch (event) {
3430 case SND_SOC_DAPM_STREAM_START:
3431 w_cpu->active = 1;
3432 break;
3433 case SND_SOC_DAPM_STREAM_STOP:
3434 w_cpu->active = 0;
3435 break;
3436 case SND_SOC_DAPM_STREAM_SUSPEND:
3437 case SND_SOC_DAPM_STREAM_RESUME:
3438 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3439 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3440 break;
3441 }
3442 }
3443
3444 if (w_codec) {
3445
3446 dapm_mark_dirty(w_codec, "stream event");
3447
3448 switch (event) {
3449 case SND_SOC_DAPM_STREAM_START:
3450 w_codec->active = 1;
3451 break;
3452 case SND_SOC_DAPM_STREAM_STOP:
3453 w_codec->active = 0;
3454 break;
3455 case SND_SOC_DAPM_STREAM_SUSPEND:
3456 case SND_SOC_DAPM_STREAM_RESUME:
3457 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3458 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3459 break;
3460 }
3461 }
3462
3463 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003464}
3465
3466/**
3467 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3468 * @rtd: PCM runtime data
3469 * @stream: stream name
3470 * @event: stream event
3471 *
3472 * Sends a stream event to the dapm core. The core then makes any
3473 * necessary widget power changes.
3474 *
3475 * Returns 0 for success else error.
3476 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003477void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3478 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003479{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003480 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003481
Liam Girdwood3cd04342012-03-09 12:02:08 +00003482 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003483 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003484 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003485}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003486
3487/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003488 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003489 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003490 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003491 *
Mark Brown74b8f952009-06-06 11:26:15 +01003492 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003493 * a valid audio route and active audio stream.
3494 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3495 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003496 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003497int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003498{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003499 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003500}
Liam Girdwooda5302182008-07-07 13:35:17 +01003501EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003502
3503/**
Mark Brownda341832010-03-15 19:23:37 +00003504 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003505 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003506 * @pin: pin name
3507 *
3508 * Enables input/output pin regardless of any other state. This is
3509 * intended for use with microphone bias supplies used in microphone
3510 * jack detection.
3511 *
3512 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3513 * do any widget power switching.
3514 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003515int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3516 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003517{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003518 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003519
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003520 if (!w) {
3521 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3522 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003523 }
3524
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003525 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3526 w->connected = 1;
3527 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003528 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003529
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003530 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003531}
3532EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3533
3534/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003535 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003536 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003537 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003538 *
Mark Brown74b8f952009-06-06 11:26:15 +01003539 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003540 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3541 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003542 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003543int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3544 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003545{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003546 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003547}
3548EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3549
3550/**
Mark Brown5817b522008-09-24 11:23:11 +01003551 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003552 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003553 * @pin: pin name
3554 *
3555 * Marks the specified pin as being not connected, disabling it along
3556 * any parent or child widgets. At present this is identical to
3557 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3558 * additional things such as disabling controls which only affect
3559 * paths through the pin.
3560 *
3561 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3562 * do any widget power switching.
3563 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003564int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003565{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003566 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003567}
3568EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3569
3570/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003571 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003572 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003573 * @pin: audio signal pin endpoint (or start point)
3574 *
3575 * Get audio pin status - connected or disconnected.
3576 *
3577 * Returns 1 for connected otherwise 0.
3578 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003579int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3580 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003581{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003582 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003583
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003584 if (w)
3585 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003586
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003587 return 0;
3588}
Liam Girdwooda5302182008-07-07 13:35:17 +01003589EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003590
3591/**
Mark Brown1547aba2010-05-07 21:11:40 +01003592 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003593 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003594 * @pin: audio signal pin endpoint (or start point)
3595 *
3596 * Mark the given endpoint or pin as ignoring suspend. When the
3597 * system is disabled a path between two endpoints flagged as ignoring
3598 * suspend will not be disabled. The path must already be enabled via
3599 * normal means at suspend time, it will not be turned on if it was not
3600 * already enabled.
3601 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003602int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3603 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003604{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003605 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003606
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003607 if (!w) {
3608 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3609 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003610 }
3611
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003612 w->ignore_suspend = 1;
3613
3614 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003615}
3616EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3617
Stephen Warren16332812011-11-23 12:42:04 -07003618static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3619 struct snd_soc_dapm_widget *w)
3620{
3621 struct snd_soc_dapm_path *p;
3622
3623 list_for_each_entry(p, &card->paths, list) {
3624 if ((p->source == w) || (p->sink == w)) {
3625 dev_dbg(card->dev,
3626 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3627 p->source->name, p->source->id, p->source->dapm,
3628 p->sink->name, p->sink->id, p->sink->dapm);
3629
3630 /* Connected to something other than the codec */
3631 if (p->source->dapm != p->sink->dapm)
3632 return true;
3633 /*
3634 * Loopback connection from codec external pin to
3635 * codec external pin
3636 */
3637 if (p->sink->id == snd_soc_dapm_input) {
3638 switch (p->source->id) {
3639 case snd_soc_dapm_output:
3640 case snd_soc_dapm_micbias:
3641 return true;
3642 default:
3643 break;
3644 }
3645 }
3646 }
3647 }
3648
3649 return false;
3650}
3651
3652/**
3653 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3654 * @codec: The codec whose pins should be processed
3655 *
3656 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3657 * which are unused. Pins are used if they are connected externally to the
3658 * codec, whether that be to some other device, or a loop-back connection to
3659 * the codec itself.
3660 */
3661void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3662{
3663 struct snd_soc_card *card = codec->card;
3664 struct snd_soc_dapm_context *dapm = &codec->dapm;
3665 struct snd_soc_dapm_widget *w;
3666
Mark Browna094b802011-11-27 19:42:20 +00003667 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003668 &card->dapm, &codec->dapm);
3669
3670 list_for_each_entry(w, &card->widgets, list) {
3671 if (w->dapm != dapm)
3672 continue;
3673 switch (w->id) {
3674 case snd_soc_dapm_input:
3675 case snd_soc_dapm_output:
3676 case snd_soc_dapm_micbias:
Mark Browna094b802011-11-27 19:42:20 +00003677 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003678 w->name);
3679 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003680 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003681 "... Not in map; disabling\n");
3682 snd_soc_dapm_nc_pin(dapm, w->name);
3683 }
3684 break;
3685 default:
3686 break;
3687 }
3688 }
3689}
3690
Mark Brown1547aba2010-05-07 21:11:40 +01003691/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003692 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003693 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003694 *
3695 * Free all dapm widgets and resources.
3696 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003697void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003698{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003699 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003700 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003701 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003702 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003703}
3704EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3705
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003706static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003707{
Liam Girdwood01005a72012-07-06 16:57:05 +01003708 struct snd_soc_card *card = dapm->card;
Mark Brown51737472009-06-22 13:16:51 +01003709 struct snd_soc_dapm_widget *w;
3710 LIST_HEAD(down_list);
3711 int powerdown = 0;
3712
Liam Girdwood01005a72012-07-06 16:57:05 +01003713 mutex_lock(&card->dapm_mutex);
3714
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003715 list_for_each_entry(w, &dapm->card->widgets, list) {
3716 if (w->dapm != dapm)
3717 continue;
Mark Brown51737472009-06-22 13:16:51 +01003718 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003719 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003720 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003721 powerdown = 1;
3722 }
3723 }
3724
3725 /* If there were no widgets to power down we're already in
3726 * standby.
3727 */
3728 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003729 if (dapm->bias_level == SND_SOC_BIAS_ON)
3730 snd_soc_dapm_set_bias_level(dapm,
3731 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003732 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003733 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3734 snd_soc_dapm_set_bias_level(dapm,
3735 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003736 }
Liam Girdwood01005a72012-07-06 16:57:05 +01003737
3738 mutex_unlock(&card->dapm_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003739}
Mark Brown51737472009-06-22 13:16:51 +01003740
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003741/*
3742 * snd_soc_dapm_shutdown - callback for system shutdown
3743 */
3744void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3745{
3746 struct snd_soc_codec *codec;
3747
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003748 list_for_each_entry(codec, &card->codec_dev_list, list) {
3749 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003750 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3751 snd_soc_dapm_set_bias_level(&codec->dapm,
3752 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003753 }
Mark Brown51737472009-06-22 13:16:51 +01003754}
3755
Richard Purdie2b97eab2006-10-06 18:32:18 +02003756/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003757MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003758MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3759MODULE_LICENSE("GPL");