blob: bda29ccf88f4bf9b31a71eb52fd62fe7e3de2a2f [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
Liam Girdwood612a3fe2012-02-06 16:05:29 +000017 * mic/headphone insertion events.
Richard Purdie2b97eab2006-10-06 18:32:18 +020018 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Liam Girdwood612a3fe2012-02-06 16:05:29 +000021 * o Delayed power down of audio subsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020024 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080029#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020030#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/bitops.h>
33#include <linux/platform_device.h>
34#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020035#include <linux/debugfs.h>
Mark Brownf1aac482011-12-05 15:17:06 +000036#include <linux/pm_runtime.h>
Mark Brown62ea8742012-01-21 21:14:48 +000037#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020039#include <sound/core.h>
40#include <sound/pcm.h>
41#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020042#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020043#include <sound/initval.h>
44
Mark Brown84e90932010-11-04 00:07:02 -040045#include <trace/events/asoc.h>
46
Mark Brownde02d072011-09-20 21:43:24 +010047#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
48
Richard Purdie2b97eab2006-10-06 18:32:18 +020049/* dapm power sequences - make this per codec in the future */
50static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010051 [snd_soc_dapm_pre] = 0,
52 [snd_soc_dapm_supply] = 1,
Mark Brown62ea8742012-01-21 21:14:48 +000053 [snd_soc_dapm_regulator_supply] = 1,
Mark Brown38357ab2009-06-06 19:03:23 +010054 [snd_soc_dapm_micbias] = 2,
Mark Brown888df392012-02-16 19:37:51 -080055 [snd_soc_dapm_dai] = 3,
Mark Brown010ff262009-08-17 17:39:22 +010056 [snd_soc_dapm_aif_in] = 3,
57 [snd_soc_dapm_aif_out] = 3,
58 [snd_soc_dapm_mic] = 4,
59 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000060 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010061 [snd_soc_dapm_value_mux] = 5,
62 [snd_soc_dapm_dac] = 6,
63 [snd_soc_dapm_mixer] = 7,
64 [snd_soc_dapm_mixer_named_ctl] = 7,
65 [snd_soc_dapm_pga] = 8,
66 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060067 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010068 [snd_soc_dapm_hp] = 10,
69 [snd_soc_dapm_spk] = 10,
70 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020071};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000072
Richard Purdie2b97eab2006-10-06 18:32:18 +020073static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010074 [snd_soc_dapm_pre] = 0,
75 [snd_soc_dapm_adc] = 1,
76 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010077 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060078 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010079 [snd_soc_dapm_pga] = 4,
80 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010081 [snd_soc_dapm_mixer] = 5,
82 [snd_soc_dapm_dac] = 6,
83 [snd_soc_dapm_mic] = 7,
84 [snd_soc_dapm_micbias] = 8,
85 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000086 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010087 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010088 [snd_soc_dapm_aif_in] = 10,
89 [snd_soc_dapm_aif_out] = 10,
Mark Brown888df392012-02-16 19:37:51 -080090 [snd_soc_dapm_dai] = 10,
Mark Brown62ea8742012-01-21 21:14:48 +000091 [snd_soc_dapm_regulator_supply] = 11,
Mark Brown010ff262009-08-17 17:39:22 +010092 [snd_soc_dapm_supply] = 11,
93 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020094};
95
Troy Kisky12ef1932008-10-13 17:42:14 -070096static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010097{
98 if (pop_time)
99 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
100}
101
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200102static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100103{
104 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200105 char *buf;
106
107 if (!pop_time)
108 return;
109
110 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
111 if (buf == NULL)
112 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100113
114 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200115 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100116 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100117 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200118
119 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100120}
121
Mark Browndb432b42011-10-03 21:06:40 +0100122static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
123{
124 return !list_empty(&w->dirty);
125}
126
Mark Brown25c77c52011-10-08 13:36:03 +0100127void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100128{
Mark Brown75c1f892011-10-04 22:28:08 +0100129 if (!dapm_dirty_widget(w)) {
130 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
131 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100132 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100133 }
Mark Browndb432b42011-10-03 21:06:40 +0100134}
Mark Brown25c77c52011-10-08 13:36:03 +0100135EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100136
Richard Purdie2b97eab2006-10-06 18:32:18 +0200137/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100138static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200139 const struct snd_soc_dapm_widget *_widget)
140{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100141 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200142}
143
Liam Girdwood48056082011-07-20 12:23:33 +0100144/* get snd_card from DAPM context */
145static inline struct snd_card *dapm_get_snd_card(
146 struct snd_soc_dapm_context *dapm)
147{
148 if (dapm->codec)
149 return dapm->codec->card->snd_card;
150 else if (dapm->platform)
151 return dapm->platform->card->snd_card;
152 else
153 BUG();
154
155 /* unreachable */
156 return NULL;
157}
158
159/* get soc_card from DAPM context */
160static inline struct snd_soc_card *dapm_get_soc_card(
161 struct snd_soc_dapm_context *dapm)
162{
163 if (dapm->codec)
164 return dapm->codec->card;
165 else if (dapm->platform)
166 return dapm->platform->card;
167 else
168 BUG();
169
170 /* unreachable */
171 return NULL;
172}
173
Liam Girdwood6c120e12012-02-15 15:15:34 +0000174static void dapm_reset(struct snd_soc_card *card)
175{
176 struct snd_soc_dapm_widget *w;
177
178 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
179
180 list_for_each_entry(w, &card->widgets, list) {
181 w->power_checked = false;
182 w->inputs = -1;
183 w->outputs = -1;
184 }
185}
186
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100187static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
188{
189 if (w->codec)
190 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100191 else if (w->platform)
192 return snd_soc_platform_read(w->platform, reg);
193
194 dev_err(w->dapm->dev, "no valid widget read method\n");
195 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100196}
197
198static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
199{
200 if (w->codec)
201 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100202 else if (w->platform)
203 return snd_soc_platform_write(w->platform, reg, val);
204
205 dev_err(w->dapm->dev, "no valid widget write method\n");
206 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100207}
208
Liam Girdwood49575fb52012-03-06 18:16:19 +0000209static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
210{
Mark Browne06ab3b2012-03-06 23:58:22 +0000211 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000212 mutex_lock(&w->codec->mutex);
213 else if (w->platform)
214 mutex_lock(&w->platform->mutex);
215}
216
217static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
218{
Mark Browne06ab3b2012-03-06 23:58:22 +0000219 if (w->codec && !w->codec->using_regmap)
Liam Girdwood49575fb52012-03-06 18:16:19 +0000220 mutex_unlock(&w->codec->mutex);
221 else if (w->platform)
222 mutex_unlock(&w->platform->mutex);
223}
224
225static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100226 unsigned short reg, unsigned int mask, unsigned int value)
227{
Mark Brown8a713da2011-12-03 12:33:55 +0000228 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100229 unsigned int old, new;
230 int ret;
231
Mark Brown8a713da2011-12-03 12:33:55 +0000232 if (w->codec && w->codec->using_regmap) {
233 ret = regmap_update_bits_check(w->codec->control_data,
234 reg, mask, value, &change);
235 if (ret != 0)
236 return ret;
237 } else {
Liam Girdwood49575fb52012-03-06 18:16:19 +0000238 soc_widget_lock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000239 ret = soc_widget_read(w, reg);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000240 if (ret < 0) {
241 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100242 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000243 }
Mark Brown8a713da2011-12-03 12:33:55 +0000244
245 old = ret;
246 new = (old & ~mask) | (value & mask);
247 change = old != new;
248 if (change) {
249 ret = soc_widget_write(w, reg, new);
Liam Girdwood49575fb52012-03-06 18:16:19 +0000250 if (ret < 0) {
251 soc_widget_unlock(w);
Mark Brown8a713da2011-12-03 12:33:55 +0000252 return ret;
Liam Girdwood49575fb52012-03-06 18:16:19 +0000253 }
Mark Brown8a713da2011-12-03 12:33:55 +0000254 }
Liam Girdwood49575fb52012-03-06 18:16:19 +0000255 soc_widget_unlock(w);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100256 }
257
258 return change;
259}
260
Mark Brown452c5ea2009-05-17 21:41:23 +0100261/**
262 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800263 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100264 * @level: level to configure
265 *
266 * Configure the bias (power) levels for the SoC audio device.
267 *
268 * Returns 0 for success else error.
269 */
Mark Browned5a4c42011-02-18 11:12:42 -0800270static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200271 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100272{
Mark Browned5a4c42011-02-18 11:12:42 -0800273 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100274 int ret = 0;
275
Mark Brown84e90932010-11-04 00:07:02 -0400276 trace_snd_soc_bias_level_start(card, level);
277
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000278 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100279 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100280 if (ret != 0)
281 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100282
Mark Browncc4c6702011-06-06 19:03:34 +0100283 if (dapm->codec) {
284 if (dapm->codec->driver->set_bias_level)
285 ret = dapm->codec->driver->set_bias_level(dapm->codec,
286 level);
287 else
288 dapm->bias_level = level;
289 }
Mark Brown171ec6b2011-06-06 18:15:19 +0100290 if (ret != 0)
291 goto out;
292
293 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100294 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100295out:
Mark Brown84e90932010-11-04 00:07:02 -0400296 trace_snd_soc_bias_level_done(card, level);
297
Mark Brown452c5ea2009-05-17 21:41:23 +0100298 return ret;
299}
300
Richard Purdie2b97eab2006-10-06 18:32:18 +0200301/* set up initial codec paths */
302static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
303 struct snd_soc_dapm_path *p, int i)
304{
305 switch (w->id) {
306 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000307 case snd_soc_dapm_mixer:
308 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200309 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100310 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600311 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400312 unsigned int reg = mc->reg;
313 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100314 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400315 unsigned int mask = (1 << fls(max)) - 1;
316 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200317
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100318 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200319 val = (val >> shift) & mask;
320
321 if ((invert && !val) || (!invert && val))
322 p->connect = 1;
323 else
324 p->connect = 0;
325 }
326 break;
327 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600328 struct soc_enum *e = (struct soc_enum *)
329 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200330 int val, item, bitmask;
331
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100332 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100333 ;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100334 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200335 item = (val >> e->shift_l) & (bitmask - 1);
336
337 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100338 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200339 if (!(strcmp(p->name, e->texts[i])) && item == i)
340 p->connect = 1;
341 }
342 }
343 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000344 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600345 struct soc_enum *e = (struct soc_enum *)
346 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000347
348 p->connect = 0;
349 /* since a virtual mux has no backing registers to
350 * decide which path to connect, it will try to match
351 * with the first enumeration. This is to ensure
352 * that the default mux choice (the first) will be
353 * correctly powered up during initialization.
354 */
355 if (!strcmp(p->name, e->texts[0]))
356 p->connect = 1;
357 }
358 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200359 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200360 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600361 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200362 int val, item;
363
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100364 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200365 val = (val >> e->shift_l) & e->mask;
366 for (item = 0; item < e->max; item++) {
367 if (val == e->values[item])
368 break;
369 }
370
371 p->connect = 0;
372 for (i = 0; i < e->max; i++) {
373 if (!(strcmp(p->name, e->texts[i])) && item == i)
374 p->connect = 1;
375 }
376 }
377 break;
Mark Brown56563102011-10-03 22:41:09 +0100378 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200379 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600380 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200381 case snd_soc_dapm_output:
382 case snd_soc_dapm_adc:
383 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000384 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200385 case snd_soc_dapm_dac:
386 case snd_soc_dapm_micbias:
387 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100388 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000389 case snd_soc_dapm_regulator_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100390 case snd_soc_dapm_aif_in:
391 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800392 case snd_soc_dapm_dai:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200393 case snd_soc_dapm_hp:
394 case snd_soc_dapm_mic:
395 case snd_soc_dapm_spk:
396 case snd_soc_dapm_line:
Mark Brown56563102011-10-03 22:41:09 +0100397 p->connect = 1;
398 break;
399 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200400 case snd_soc_dapm_pre:
401 case snd_soc_dapm_post:
402 p->connect = 0;
403 break;
404 }
405}
406
Mark Brown74b8f952009-06-06 11:26:15 +0100407/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200408static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200409 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
410 struct snd_soc_dapm_path *path, const char *control_name,
411 const struct snd_kcontrol_new *kcontrol)
412{
413 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
414 int i;
415
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100416 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200417 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200418 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200419 list_add(&path->list_sink, &dest->sources);
420 list_add(&path->list_source, &src->sinks);
421 path->name = (char*)e->texts[i];
422 dapm_set_path_status(dest, path, 0);
423 return 0;
424 }
425 }
426
427 return -ENODEV;
428}
429
Mark Brown74b8f952009-06-06 11:26:15 +0100430/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200431static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200432 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
433 struct snd_soc_dapm_path *path, const char *control_name)
434{
435 int i;
436
437 /* search for mixer kcontrol */
438 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600439 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200440 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200441 list_add(&path->list_sink, &dest->sources);
442 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600443 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200444 dapm_set_path_status(dest, path, i);
445 return 0;
446 }
447 }
448 return -ENODEV;
449}
450
Stephen Warrenaf468002011-04-28 17:38:01 -0600451static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600452 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600453 const struct snd_kcontrol_new *kcontrol_new,
454 struct snd_kcontrol **kcontrol)
455{
456 struct snd_soc_dapm_widget *w;
457 int i;
458
459 *kcontrol = NULL;
460
461 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600462 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
463 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600464 for (i = 0; i < w->num_kcontrols; i++) {
465 if (&w->kcontrol_news[i] == kcontrol_new) {
466 if (w->kcontrols)
467 *kcontrol = w->kcontrols[i];
468 return 1;
469 }
470 }
471 }
472
473 return 0;
474}
475
Richard Purdie2b97eab2006-10-06 18:32:18 +0200476/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200477static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200478{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200479 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200480 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000481 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200482 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000483 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000484 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600485 struct snd_soc_dapm_widget_list *wlist;
486 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000487
488 if (dapm->codec)
489 prefix = dapm->codec->name_prefix;
490 else
491 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200492
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000493 if (prefix)
494 prefix_len = strlen(prefix) + 1;
495 else
496 prefix_len = 0;
497
Richard Purdie2b97eab2006-10-06 18:32:18 +0200498 /* add kcontrol */
499 for (i = 0; i < w->num_kcontrols; i++) {
500
501 /* match name */
502 list_for_each_entry(path, &w->sources, list_sink) {
503
504 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600505 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200506 continue;
507
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200508 if (w->kcontrols[i]) {
509 path->kcontrol = w->kcontrols[i];
510 continue;
511 }
512
Stephen Warrenfafd2172011-04-28 17:38:00 -0600513 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
514 sizeof(struct snd_soc_dapm_widget *),
515 wlist = kzalloc(wlistsize, GFP_KERNEL);
516 if (wlist == NULL) {
517 dev_err(dapm->dev,
518 "asoc: can't allocate widget list for %s\n",
519 w->name);
520 return -ENOMEM;
521 }
522 wlist->num_widgets = 1;
523 wlist->widgets[0] = w;
524
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000525 /* add dapm control with long name.
526 * for dapm_mixer this is the concatenation of the
527 * mixer and kcontrol name.
528 * for dapm_mixer_named_ctl this is simply the
529 * kcontrol name.
530 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600531 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000532 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000533 name_len += 1 + strlen(w->name);
534
Mark Brown219b93f2008-10-28 13:02:31 +0000535 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000536
Stephen Warrenfafd2172011-04-28 17:38:00 -0600537 if (path->long_name == NULL) {
538 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200539 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600540 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200541
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000542 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000543 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000544 /* The control will get a prefix from
545 * the control creation process but
546 * we're also using the same prefix
547 * for widgets so cut the prefix off
548 * the front of the widget name.
549 */
Mark Brown888df392012-02-16 19:37:51 -0800550 snprintf((char *)path->long_name, name_len,
551 "%s %s", w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600552 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000553 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000554 case snd_soc_dapm_mixer_named_ctl:
Mark Brown888df392012-02-16 19:37:51 -0800555 snprintf((char *)path->long_name, name_len,
556 "%s", w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000557 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000558 }
559
Mark Brown888df392012-02-16 19:37:51 -0800560 ((char *)path->long_name)[name_len - 1] = '\0';
Mark Brown219b93f2008-10-28 13:02:31 +0000561
Stephen Warrenfafd2172011-04-28 17:38:00 -0600562 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
563 wlist, path->long_name,
564 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200565 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200566 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200567 dev_err(dapm->dev,
568 "asoc: failed to add dapm kcontrol %s: %d\n",
569 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600570 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200571 kfree(path->long_name);
572 path->long_name = NULL;
573 return ret;
574 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600575 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200576 }
577 }
578 return ret;
579}
580
581/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200582static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200583{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200584 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200585 struct snd_soc_dapm_path *path = NULL;
586 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000587 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000588 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000589 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600590 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600591 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600592 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600593 size_t wlistsize;
Mark Brown888df392012-02-16 19:37:51 -0800594 const char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200595
Stephen Warrenaf468002011-04-28 17:38:01 -0600596 if (w->num_kcontrols != 1) {
597 dev_err(dapm->dev,
598 "asoc: mux %s has incorrect number of controls\n",
599 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200600 return -EINVAL;
601 }
602
Stephen Warren1007da02011-05-26 09:57:33 -0600603 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600604 &kcontrol);
605 if (kcontrol) {
606 wlist = kcontrol->private_data;
607 wlistentries = wlist->num_widgets + 1;
608 } else {
609 wlist = NULL;
610 wlistentries = 1;
611 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600612 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600613 wlistentries * sizeof(struct snd_soc_dapm_widget *),
614 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600615 if (wlist == NULL) {
616 dev_err(dapm->dev,
617 "asoc: can't allocate widget list for %s\n", w->name);
618 return -ENOMEM;
619 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600620 wlist->num_widgets = wlistentries;
621 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600622
Stephen Warrenaf468002011-04-28 17:38:01 -0600623 if (!kcontrol) {
624 if (dapm->codec)
625 prefix = dapm->codec->name_prefix;
626 else
627 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000628
Stephen Warrenaf468002011-04-28 17:38:01 -0600629 if (shared) {
630 name = w->kcontrol_news[0].name;
631 prefix_len = 0;
632 } else {
633 name = w->name;
634 if (prefix)
635 prefix_len = strlen(prefix) + 1;
636 else
637 prefix_len = 0;
638 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000639
Stephen Warrenaf468002011-04-28 17:38:01 -0600640 /*
641 * The control will get a prefix from the control creation
642 * process but we're also using the same prefix for widgets so
643 * cut the prefix off the front of the widget name.
644 */
645 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
646 name + prefix_len, prefix);
647 ret = snd_ctl_add(card, kcontrol);
648 if (ret < 0) {
Mark Brown53daf202011-09-05 10:51:05 -0700649 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
650 w->name, ret);
Stephen Warrenaf468002011-04-28 17:38:01 -0600651 kfree(wlist);
652 return ret;
653 }
654 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200655
Stephen Warrenaf468002011-04-28 17:38:01 -0600656 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200657
Stephen Warrenfad59882011-04-28 17:37:59 -0600658 w->kcontrols[0] = kcontrol;
659
Richard Purdie2b97eab2006-10-06 18:32:18 +0200660 list_for_each_entry(path, &w->sources, list_sink)
661 path->kcontrol = kcontrol;
662
Stephen Warrenaf468002011-04-28 17:38:01 -0600663 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200664}
665
666/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200667static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200668{
Mark Browna6c65732010-03-03 17:45:21 +0000669 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200670 dev_err(w->dapm->dev,
671 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200672
Mark Browna6c65732010-03-03 17:45:21 +0000673 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200674}
675
676/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200677static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200678{
679 struct snd_soc_dapm_path *p;
680
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200681 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200682 p->walked = 0;
683}
684
Mark Brown99497882010-05-07 20:24:05 +0100685/* We implement power down on suspend by checking the power state of
686 * the ALSA card - when we are suspending the ALSA state for the card
687 * is set to D3.
688 */
689static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
690{
Mark Brown12ea2c72011-03-02 18:17:32 +0000691 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100692
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100694 case SNDRV_CTL_POWER_D3hot:
695 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100696 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200697 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
698 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100699 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100700 default:
701 return 1;
702 }
703}
704
Richard Purdie2b97eab2006-10-06 18:32:18 +0200705/*
706 * Recursively check for a completed path to an active or physically connected
707 * output widget. Returns number of complete paths.
708 */
709static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
710{
711 struct snd_soc_dapm_path *path;
712 int con = 0;
713
Mark Brown024dc072011-10-09 11:52:05 +0100714 if (widget->outputs >= 0)
715 return widget->outputs;
716
Mark Brownde02d072011-09-20 21:43:24 +0100717 DAPM_UPDATE_STAT(widget, path_checks);
718
Mark Brown62ea8742012-01-21 21:14:48 +0000719 switch (widget->id) {
720 case snd_soc_dapm_supply:
721 case snd_soc_dapm_regulator_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100722 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000723 default:
724 break;
725 }
Mark Brown246d0a12009-04-22 18:24:55 +0100726
Mark Brown010ff262009-08-17 17:39:22 +0100727 switch (widget->id) {
728 case snd_soc_dapm_adc:
729 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800730 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100731 if (widget->active) {
732 widget->outputs = snd_soc_dapm_suspend_check(widget);
733 return widget->outputs;
734 }
Mark Brown010ff262009-08-17 17:39:22 +0100735 default:
736 break;
737 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200738
739 if (widget->connected) {
740 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100741 if (widget->id == snd_soc_dapm_output && !widget->ext) {
742 widget->outputs = snd_soc_dapm_suspend_check(widget);
743 return widget->outputs;
744 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200745
746 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100747 if (widget->id == snd_soc_dapm_hp ||
748 widget->id == snd_soc_dapm_spk ||
749 (widget->id == snd_soc_dapm_line &&
750 !list_empty(&widget->sources))) {
751 widget->outputs = snd_soc_dapm_suspend_check(widget);
752 return widget->outputs;
753 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200754 }
755
756 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100757 DAPM_UPDATE_STAT(widget, neighbour_checks);
758
Mark Brownbf3a9e12011-06-13 16:42:29 +0100759 if (path->weak)
760 continue;
761
Richard Purdie2b97eab2006-10-06 18:32:18 +0200762 if (path->walked)
763 continue;
764
765 if (path->sink && path->connect) {
766 path->walked = 1;
767 con += is_connected_output_ep(path->sink);
768 }
769 }
770
Mark Brown024dc072011-10-09 11:52:05 +0100771 widget->outputs = con;
772
Richard Purdie2b97eab2006-10-06 18:32:18 +0200773 return con;
774}
775
776/*
777 * Recursively check for a completed path to an active or physically connected
778 * input widget. Returns number of complete paths.
779 */
780static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
781{
782 struct snd_soc_dapm_path *path;
783 int con = 0;
784
Mark Brown024dc072011-10-09 11:52:05 +0100785 if (widget->inputs >= 0)
786 return widget->inputs;
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:
Mark Brown246d0a12009-04-22 18:24:55 +0100793 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000794 default:
795 break;
796 }
Mark Brown246d0a12009-04-22 18:24:55 +0100797
Richard Purdie2b97eab2006-10-06 18:32:18 +0200798 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100799 switch (widget->id) {
800 case snd_soc_dapm_dac:
801 case snd_soc_dapm_aif_in:
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->inputs = snd_soc_dapm_suspend_check(widget);
805 return widget->inputs;
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_input && !widget->ext) {
814 widget->inputs = snd_soc_dapm_suspend_check(widget);
815 return widget->inputs;
816 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200817
818 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100819 if (widget->id == snd_soc_dapm_vmid) {
820 widget->inputs = snd_soc_dapm_suspend_check(widget);
821 return widget->inputs;
822 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200823
824 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300825 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100826 (widget->id == snd_soc_dapm_line &&
827 !list_empty(&widget->sinks))) {
828 widget->inputs = snd_soc_dapm_suspend_check(widget);
829 return widget->inputs;
830 }
831
Mark Brown1ab97c82011-11-27 16:21:51 +0000832 /* signal generator */
833 if (widget->id == snd_soc_dapm_siggen) {
834 widget->inputs = snd_soc_dapm_suspend_check(widget);
835 return widget->inputs;
836 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200837 }
838
839 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100840 DAPM_UPDATE_STAT(widget, neighbour_checks);
841
Mark Brownbf3a9e12011-06-13 16:42:29 +0100842 if (path->weak)
843 continue;
844
Richard Purdie2b97eab2006-10-06 18:32:18 +0200845 if (path->walked)
846 continue;
847
848 if (path->source && path->connect) {
849 path->walked = 1;
850 con += is_connected_input_ep(path->source);
851 }
852 }
853
Mark Brown024dc072011-10-09 11:52:05 +0100854 widget->inputs = con;
855
Richard Purdie2b97eab2006-10-06 18:32:18 +0200856 return con;
857}
858
859/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300860 * Handler for generic register modifier widget.
861 */
862int dapm_reg_event(struct snd_soc_dapm_widget *w,
863 struct snd_kcontrol *kcontrol, int event)
864{
865 unsigned int val;
866
867 if (SND_SOC_DAPM_EVENT_ON(event))
868 val = w->on_val;
869 else
870 val = w->off_val;
871
Liam Girdwood49575fb52012-03-06 18:16:19 +0000872 soc_widget_update_bits_locked(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300873 w->mask << w->shift, val << w->shift);
874
875 return 0;
876}
Mark Brown11589412008-07-29 11:42:23 +0100877EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300878
Mark Brown62ea8742012-01-21 21:14:48 +0000879/*
880 * Handler for regulator supply widget.
881 */
882int dapm_regulator_event(struct snd_soc_dapm_widget *w,
883 struct snd_kcontrol *kcontrol, int event)
884{
885 if (SND_SOC_DAPM_EVENT_ON(event))
Liam Girdwooda3cc0562012-03-09 17:20:16 +0000886 return regulator_enable(w->regulator);
Mark Brown62ea8742012-01-21 21:14:48 +0000887 else
Liam Girdwooda3cc0562012-03-09 17:20:16 +0000888 return regulator_disable_deferred(w->regulator, w->shift);
Mark Brown62ea8742012-01-21 21:14:48 +0000889}
890EXPORT_SYMBOL_GPL(dapm_regulator_event);
891
Mark Brownd8050022011-09-28 18:28:23 +0100892static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
893{
Mark Brown9b8a83b2011-10-04 22:15:59 +0100894 if (w->power_checked)
895 return w->new_power;
896
Mark Brownd8050022011-09-28 18:28:23 +0100897 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +0100898 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +0100899 else
Mark Brown9b8a83b2011-10-04 22:15:59 +0100900 w->new_power = w->power_check(w);
901
902 w->power_checked = true;
903
904 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +0100905}
906
Mark Browncd0f2d42009-04-20 16:56:59 +0100907/* Generic check to see if a widget should be powered.
908 */
909static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
910{
911 int in, out;
912
Mark Brownde02d072011-09-20 21:43:24 +0100913 DAPM_UPDATE_STAT(w, power_checks);
914
Mark Browncd0f2d42009-04-20 16:56:59 +0100915 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200916 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100917 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200918 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100919 return out != 0 && in != 0;
920}
921
Mark Brown888df392012-02-16 19:37:51 -0800922static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
923{
924 DAPM_UPDATE_STAT(w, power_checks);
925
Mark Brown1eee1b32012-04-10 13:57:46 +0100926 if (w->active)
927 return w->active;
928
929 return dapm_generic_check_power(w);
Mark Brown888df392012-02-16 19:37:51 -0800930}
931
Mark Brown6ea31b92009-04-20 17:15:41 +0100932/* Check to see if an ADC has power */
933static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
934{
935 int in;
936
Mark Brownde02d072011-09-20 21:43:24 +0100937 DAPM_UPDATE_STAT(w, power_checks);
938
Mark Brown6ea31b92009-04-20 17:15:41 +0100939 if (w->active) {
940 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200941 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100942 return in != 0;
943 } else {
944 return dapm_generic_check_power(w);
945 }
946}
947
948/* Check to see if a DAC has power */
949static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
950{
951 int out;
952
Mark Brownde02d072011-09-20 21:43:24 +0100953 DAPM_UPDATE_STAT(w, power_checks);
954
Mark Brown6ea31b92009-04-20 17:15:41 +0100955 if (w->active) {
956 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200957 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100958 return out != 0;
959 } else {
960 return dapm_generic_check_power(w);
961 }
962}
963
Mark Brown246d0a12009-04-22 18:24:55 +0100964/* Check to see if a power supply is needed */
965static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
966{
967 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +0100968
Mark Brownde02d072011-09-20 21:43:24 +0100969 DAPM_UPDATE_STAT(w, power_checks);
970
Mark Brown246d0a12009-04-22 18:24:55 +0100971 /* Check if one of our outputs is connected */
972 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +0100973 DAPM_UPDATE_STAT(w, neighbour_checks);
974
Mark Brownbf3a9e12011-06-13 16:42:29 +0100975 if (path->weak)
976 continue;
977
Mark Brown215edda2009-09-08 18:59:05 +0100978 if (path->connected &&
979 !path->connected(path->source, path->sink))
980 continue;
981
Mark Brown30173582011-02-11 11:42:19 +0000982 if (!path->sink)
983 continue;
984
Mark Brownf68d7e12011-10-04 22:57:50 +0100985 if (dapm_widget_power_check(path->sink))
986 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +0100987 }
988
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200989 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100990
Mark Brownf68d7e12011-10-04 22:57:50 +0100991 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +0100992}
993
Mark Brown35c64bc2011-09-28 18:23:53 +0100994static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
995{
996 return 1;
997}
998
Mark Brown38357ab2009-06-06 19:03:23 +0100999static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1000 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +00001001 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +00001002{
Mark Brown828a8422011-01-15 13:14:30 +00001003 int *sort;
1004
1005 if (power_up)
1006 sort = dapm_up_seq;
1007 else
1008 sort = dapm_down_seq;
1009
Mark Brown38357ab2009-06-06 19:03:23 +01001010 if (sort[a->id] != sort[b->id])
1011 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001012 if (a->subseq != b->subseq) {
1013 if (power_up)
1014 return a->subseq - b->subseq;
1015 else
1016 return b->subseq - a->subseq;
1017 }
Mark Brownb22ead22009-06-07 12:51:26 +01001018 if (a->reg != b->reg)
1019 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001020 if (a->dapm != b->dapm)
1021 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001022
Mark Brown38357ab2009-06-06 19:03:23 +01001023 return 0;
1024}
Mark Brown42aa3412009-03-01 19:21:10 +00001025
Mark Brown38357ab2009-06-06 19:03:23 +01001026/* Insert a widget in order into a DAPM power sequence. */
1027static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1028 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001029 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001030{
1031 struct snd_soc_dapm_widget *w;
1032
1033 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001034 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001035 list_add_tail(&new_widget->power_list, &w->power_list);
1036 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001037 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001038
Mark Brown38357ab2009-06-06 19:03:23 +01001039 list_add_tail(&new_widget->power_list, list);
1040}
Mark Brown42aa3412009-03-01 19:21:10 +00001041
Mark Brown68f89ad2010-11-03 23:51:49 -04001042static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1043 struct snd_soc_dapm_widget *w, int event)
1044{
1045 struct snd_soc_card *card = dapm->card;
1046 const char *ev_name;
1047 int power, ret;
1048
1049 switch (event) {
1050 case SND_SOC_DAPM_PRE_PMU:
1051 ev_name = "PRE_PMU";
1052 power = 1;
1053 break;
1054 case SND_SOC_DAPM_POST_PMU:
1055 ev_name = "POST_PMU";
1056 power = 1;
1057 break;
1058 case SND_SOC_DAPM_PRE_PMD:
1059 ev_name = "PRE_PMD";
1060 power = 0;
1061 break;
1062 case SND_SOC_DAPM_POST_PMD:
1063 ev_name = "POST_PMD";
1064 power = 0;
1065 break;
1066 default:
1067 BUG();
1068 return;
1069 }
1070
1071 if (w->power != power)
1072 return;
1073
1074 if (w->event && (w->event_flags & event)) {
1075 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1076 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001077 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001078 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001079 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001080 if (ret < 0)
1081 pr_err("%s: %s event failed: %d\n",
1082 ev_name, w->name, ret);
1083 }
1084}
1085
Mark Brownb22ead22009-06-07 12:51:26 +01001086/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001087static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001088 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001089{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001090 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001091 struct snd_soc_dapm_widget *w;
1092 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001093 unsigned int value = 0;
1094 unsigned int mask = 0;
1095 unsigned int cur_mask;
1096
1097 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1098 power_list)->reg;
1099
1100 list_for_each_entry(w, pending, power_list) {
1101 cur_mask = 1 << w->shift;
1102 BUG_ON(reg != w->reg);
1103
1104 if (w->invert)
1105 power = !w->power;
1106 else
1107 power = w->power;
1108
1109 mask |= cur_mask;
1110 if (power)
1111 value |= cur_mask;
1112
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001113 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001114 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1115 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001116
Mark Brown68f89ad2010-11-03 23:51:49 -04001117 /* Check for events */
1118 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1119 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001120 }
1121
Mark Brown81628102009-06-07 13:21:24 +01001122 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001123 /* Any widget will do, they should all be updating the
1124 * same register.
1125 */
1126 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1127 power_list);
1128
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001129 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001130 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001131 value, mask, reg, card->pop_time);
1132 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001133 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001134 }
1135
1136 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001137 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1138 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001139 }
Mark Brown42aa3412009-03-01 19:21:10 +00001140}
1141
Mark Brownb22ead22009-06-07 12:51:26 +01001142/* Apply a DAPM power sequence.
1143 *
1144 * We walk over a pre-sorted list of widgets to apply power to. In
1145 * order to minimise the number of writes to the device required
1146 * multiple widgets will be updated in a single write where possible.
1147 * Currently anything that requires more than a single write is not
1148 * handled.
1149 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001150static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001151 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001152{
1153 struct snd_soc_dapm_widget *w, *n;
1154 LIST_HEAD(pending);
1155 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001156 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001157 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001158 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001159 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001160 int *sort;
1161
1162 if (power_up)
1163 sort = dapm_up_seq;
1164 else
1165 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001166
Mark Brownb22ead22009-06-07 12:51:26 +01001167 list_for_each_entry_safe(w, n, list, power_list) {
1168 ret = 0;
1169
1170 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001171 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001172 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001173 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001174 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001175
Mark Brown474b62d2011-01-18 16:14:44 +00001176 if (cur_dapm && cur_dapm->seq_notifier) {
1177 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1178 if (sort[i] == cur_sort)
1179 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001180 i,
1181 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001182 }
1183
Mark Brownb22ead22009-06-07 12:51:26 +01001184 INIT_LIST_HEAD(&pending);
1185 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001186 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001187 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001188 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001189 }
1190
Mark Brown163cac02009-06-07 10:12:52 +01001191 switch (w->id) {
1192 case snd_soc_dapm_pre:
1193 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001194 list_for_each_entry_safe_continue(w, n, list,
1195 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001196
Mark Brownb22ead22009-06-07 12:51:26 +01001197 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001198 ret = w->event(w,
1199 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001200 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001201 ret = w->event(w,
1202 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001203 break;
1204
1205 case snd_soc_dapm_post:
1206 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001207 list_for_each_entry_safe_continue(w, n, list,
1208 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001209
Mark Brownb22ead22009-06-07 12:51:26 +01001210 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001211 ret = w->event(w,
1212 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001213 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001214 ret = w->event(w,
1215 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001216 break;
1217
Mark Brown163cac02009-06-07 10:12:52 +01001218 default:
Mark Brown81628102009-06-07 13:21:24 +01001219 /* Queue it up for application */
1220 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001221 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001222 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001223 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001224 list_move(&w->power_list, &pending);
1225 break;
Mark Brown163cac02009-06-07 10:12:52 +01001226 }
Mark Brownb22ead22009-06-07 12:51:26 +01001227
1228 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001229 dev_err(w->dapm->dev,
1230 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001231 }
Mark Brownb22ead22009-06-07 12:51:26 +01001232
1233 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001234 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001235
1236 if (cur_dapm && cur_dapm->seq_notifier) {
1237 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1238 if (sort[i] == cur_sort)
1239 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001240 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001241 }
Mark Brown163cac02009-06-07 10:12:52 +01001242}
1243
Mark Brown97404f22010-12-14 16:13:57 +00001244static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1245{
1246 struct snd_soc_dapm_update *update = dapm->update;
1247 struct snd_soc_dapm_widget *w;
1248 int ret;
1249
1250 if (!update)
1251 return;
1252
1253 w = update->widget;
1254
1255 if (w->event &&
1256 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1257 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1258 if (ret != 0)
1259 pr_err("%s DAPM pre-event failed: %d\n",
1260 w->name, ret);
1261 }
1262
Liam Girdwood49575fb52012-03-06 18:16:19 +00001263 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001264 update->val);
1265 if (ret < 0)
1266 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1267
1268 if (w->event &&
1269 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1270 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1271 if (ret != 0)
1272 pr_err("%s DAPM post-event failed: %d\n",
1273 w->name, ret);
1274 }
1275}
1276
Mark Brown9d0624a2011-02-18 11:49:43 -08001277/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1278 * they're changing state.
1279 */
1280static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1281{
1282 struct snd_soc_dapm_context *d = data;
1283 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001284
Mark Brown56fba412011-06-04 11:25:10 +01001285 /* If we're off and we're not supposed to be go into STANDBY */
1286 if (d->bias_level == SND_SOC_BIAS_OFF &&
1287 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001288 if (d->dev)
1289 pm_runtime_get_sync(d->dev);
1290
Mark Brown9d0624a2011-02-18 11:49:43 -08001291 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1292 if (ret != 0)
1293 dev_err(d->dev,
1294 "Failed to turn on bias: %d\n", ret);
1295 }
1296
Mark Brown56fba412011-06-04 11:25:10 +01001297 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1298 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001299 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1300 if (ret != 0)
1301 dev_err(d->dev,
1302 "Failed to prepare bias: %d\n", ret);
1303 }
1304}
1305
1306/* Async callback run prior to DAPM sequences - brings to their final
1307 * state.
1308 */
1309static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1310{
1311 struct snd_soc_dapm_context *d = data;
1312 int ret;
1313
1314 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001315 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1316 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1317 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001318 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1319 if (ret != 0)
1320 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1321 ret);
1322 }
1323
1324 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001325 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1326 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001327 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1328 if (ret != 0)
1329 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001330
1331 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001332 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001333 }
1334
1335 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001336 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1337 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001338 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1339 if (ret != 0)
1340 dev_err(d->dev, "Failed to apply active bias: %d\n",
1341 ret);
1342 }
1343}
Mark Brown97404f22010-12-14 16:13:57 +00001344
Mark Brownfe4fda52011-10-03 22:36:57 +01001345static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1346 bool power, bool connect)
1347{
1348 /* If a connection is being made or broken then that update
1349 * will have marked the peer dirty, otherwise the widgets are
1350 * not connected and this update has no impact. */
1351 if (!connect)
1352 return;
1353
1354 /* If the peer is already in the state we're moving to then we
1355 * won't have an impact on it. */
1356 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001357 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001358}
1359
Mark Brown05623c42011-09-28 17:02:31 +01001360static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1361 struct list_head *up_list,
1362 struct list_head *down_list)
1363{
Mark Browndb432b42011-10-03 21:06:40 +01001364 struct snd_soc_dapm_path *path;
1365
Mark Brown05623c42011-09-28 17:02:31 +01001366 if (w->power == power)
1367 return;
1368
1369 trace_snd_soc_dapm_widget_power(w, power);
1370
Mark Browndb432b42011-10-03 21:06:40 +01001371 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001372 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001373 */
1374 list_for_each_entry(path, &w->sources, list_sink) {
1375 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001376 dapm_widget_set_peer_power(path->source, power,
1377 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001378 }
1379 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001380 switch (w->id) {
1381 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001382 case snd_soc_dapm_regulator_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001383 /* Supplies can't affect their outputs, only their inputs */
1384 break;
1385 default:
1386 list_for_each_entry(path, &w->sinks, list_source) {
1387 if (path->sink) {
1388 dapm_widget_set_peer_power(path->sink, power,
1389 path->connect);
1390 }
Mark Browndb432b42011-10-03 21:06:40 +01001391 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001392 break;
Mark Browndb432b42011-10-03 21:06:40 +01001393 }
1394
Mark Brown05623c42011-09-28 17:02:31 +01001395 if (power)
1396 dapm_seq_insert(w, up_list, true);
1397 else
1398 dapm_seq_insert(w, down_list, false);
1399
1400 w->power = power;
1401}
1402
Mark Brown7c81beb2011-09-20 22:22:32 +01001403static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1404 struct list_head *up_list,
1405 struct list_head *down_list)
1406{
Mark Brown7c81beb2011-09-20 22:22:32 +01001407 int power;
1408
1409 switch (w->id) {
1410 case snd_soc_dapm_pre:
1411 dapm_seq_insert(w, down_list, false);
1412 break;
1413 case snd_soc_dapm_post:
1414 dapm_seq_insert(w, up_list, true);
1415 break;
1416
1417 default:
Mark Brownd8050022011-09-28 18:28:23 +01001418 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001419
Mark Brown05623c42011-09-28 17:02:31 +01001420 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001421 break;
1422 }
1423}
1424
Mark Brown42aa3412009-03-01 19:21:10 +00001425/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001426 * Scan each dapm widget for complete audio path.
1427 * A complete path is a route that has valid endpoints i.e.:-
1428 *
1429 * o DAC to output pin.
1430 * o Input Pin to ADC.
1431 * o Input pin to Output pin (bypass, sidetone)
1432 * o DAC to ADC (loopback).
1433 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001434static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001435{
Mark Brown12ea2c72011-03-02 18:17:32 +00001436 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001437 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001438 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001439 LIST_HEAD(up_list);
1440 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001441 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001442 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001443
Mark Brown84e90932010-11-04 00:07:02 -04001444 trace_snd_soc_dapm_start(card);
1445
Mark Brown56fba412011-06-04 11:25:10 +01001446 list_for_each_entry(d, &card->dapm_list, list) {
Mark Brown497098be2012-03-08 15:06:09 +00001447 if (d->idle_bias_off)
1448 d->target_bias_level = SND_SOC_BIAS_OFF;
1449 else
1450 d->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown56fba412011-06-04 11:25:10 +01001451 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001452
Liam Girdwood6c120e12012-02-15 15:15:34 +00001453 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001454
Mark Brown6d3ddc82009-05-16 17:47:29 +01001455 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001456 * lists indicating if they should be powered up or down. We
1457 * only check widgets that have been flagged as dirty but note
1458 * that new widgets may be added to the dirty list while we
1459 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001460 */
Mark Browndb432b42011-10-03 21:06:40 +01001461 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001462 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001463 }
1464
Mark Brownf9de6d72011-09-28 17:19:47 +01001465 list_for_each_entry(w, &card->widgets, list) {
Mark Browndb432b42011-10-03 21:06:40 +01001466 list_del_init(&w->dirty);
1467
Mark Brownf9de6d72011-09-28 17:19:47 +01001468 if (w->power) {
1469 d = w->dapm;
1470
1471 /* Supplies and micbiases only bring the
1472 * context up to STANDBY as unless something
1473 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001474 * generally don't require full power. Signal
1475 * generators are virtual pins and have no
1476 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001477 */
1478 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001479 case snd_soc_dapm_siggen:
1480 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001481 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001482 case snd_soc_dapm_regulator_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001483 case snd_soc_dapm_micbias:
1484 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1485 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1486 break;
1487 default:
1488 d->target_bias_level = SND_SOC_BIAS_ON;
1489 break;
1490 }
1491 }
1492
1493 }
1494
Mark Brown85a843c2011-09-21 21:29:47 +01001495 /* Force all contexts in the card to the same bias state if
1496 * they're not ground referenced.
1497 */
Mark Brown56fba412011-06-04 11:25:10 +01001498 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001499 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001500 if (d->target_bias_level > bias)
1501 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001502 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001503 if (!d->idle_bias_off)
1504 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001505
Mark Brownde02d072011-09-20 21:43:24 +01001506 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001507
Mark Brown9d0624a2011-02-18 11:49:43 -08001508 /* Run all the bias changes in parallel */
1509 list_for_each_entry(d, &dapm->card->dapm_list, list)
1510 async_schedule_domain(dapm_pre_sequence_async, d,
1511 &async_domain);
1512 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001513
Mark Brown6d3ddc82009-05-16 17:47:29 +01001514 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001515 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001516
Mark Brown97404f22010-12-14 16:13:57 +00001517 dapm_widget_update(dapm);
1518
Mark Brown6d3ddc82009-05-16 17:47:29 +01001519 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001520 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001521
Mark Brown9d0624a2011-02-18 11:49:43 -08001522 /* Run all the bias changes in parallel */
1523 list_for_each_entry(d, &dapm->card->dapm_list, list)
1524 async_schedule_domain(dapm_post_sequence_async, d,
1525 &async_domain);
1526 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001527
Liam Girdwood8078d872012-02-15 15:15:35 +00001528 /* do we need to notify any clients that DAPM event is complete */
1529 list_for_each_entry(d, &card->dapm_list, list) {
1530 if (d->stream_event)
1531 d->stream_event(d, event);
1532 }
1533
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001534 pop_dbg(dapm->dev, card->pop_time,
1535 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001536 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001537
Mark Brown84e90932010-11-04 00:07:02 -04001538 trace_snd_soc_dapm_done(card);
1539
Mark Brown42aa3412009-03-01 19:21:10 +00001540 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001541}
1542
Mark Brown79fb9382009-08-21 16:38:13 +01001543#ifdef CONFIG_DEBUG_FS
Mark Brown79fb9382009-08-21 16:38:13 +01001544static ssize_t dapm_widget_power_read_file(struct file *file,
1545 char __user *user_buf,
1546 size_t count, loff_t *ppos)
1547{
1548 struct snd_soc_dapm_widget *w = file->private_data;
1549 char *buf;
1550 int in, out;
1551 ssize_t ret;
1552 struct snd_soc_dapm_path *p = NULL;
1553
1554 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1555 if (!buf)
1556 return -ENOMEM;
1557
1558 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001559 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001560 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001561 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001562
Mark Brownf13ebad2012-03-03 18:01:01 +00001563 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1564 w->name, w->power ? "On" : "Off",
1565 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001566
Mark Brownd033c362009-12-04 15:25:56 +00001567 if (w->reg >= 0)
1568 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1569 " - R%d(0x%x) bit %d",
1570 w->reg, w->reg, w->shift);
1571
1572 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1573
Mark Brown3eef08b2009-09-14 16:49:00 +01001574 if (w->sname)
1575 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1576 w->sname,
1577 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001578
1579 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001580 if (p->connected && !p->connected(w, p->sink))
1581 continue;
1582
Mark Brown79fb9382009-08-21 16:38:13 +01001583 if (p->connect)
1584 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001585 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001586 p->name ? p->name : "static",
1587 p->source->name);
1588 }
1589 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001590 if (p->connected && !p->connected(w, p->sink))
1591 continue;
1592
Mark Brown79fb9382009-08-21 16:38:13 +01001593 if (p->connect)
1594 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001595 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001596 p->name ? p->name : "static",
1597 p->sink->name);
1598 }
1599
1600 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1601
1602 kfree(buf);
1603 return ret;
1604}
1605
1606static const struct file_operations dapm_widget_power_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001607 .open = simple_open,
Mark Brown79fb9382009-08-21 16:38:13 +01001608 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001609 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001610};
1611
Mark Brownef49e4f2011-04-04 20:48:13 +09001612static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1613 size_t count, loff_t *ppos)
1614{
1615 struct snd_soc_dapm_context *dapm = file->private_data;
1616 char *level;
1617
1618 switch (dapm->bias_level) {
1619 case SND_SOC_BIAS_ON:
1620 level = "On\n";
1621 break;
1622 case SND_SOC_BIAS_PREPARE:
1623 level = "Prepare\n";
1624 break;
1625 case SND_SOC_BIAS_STANDBY:
1626 level = "Standby\n";
1627 break;
1628 case SND_SOC_BIAS_OFF:
1629 level = "Off\n";
1630 break;
1631 default:
1632 BUG();
1633 level = "Unknown\n";
1634 break;
1635 }
1636
1637 return simple_read_from_buffer(user_buf, count, ppos, level,
1638 strlen(level));
1639}
1640
1641static const struct file_operations dapm_bias_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -07001642 .open = simple_open,
Mark Brownef49e4f2011-04-04 20:48:13 +09001643 .read = dapm_bias_read_file,
1644 .llseek = default_llseek,
1645};
1646
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001647void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1648 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001649{
Mark Brown79fb9382009-08-21 16:38:13 +01001650 struct dentry *d;
1651
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001652 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1653
1654 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001655 dev_warn(dapm->dev,
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001656 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001657 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001658 }
Mark Brown79fb9382009-08-21 16:38:13 +01001659
Mark Brownef49e4f2011-04-04 20:48:13 +09001660 d = debugfs_create_file("bias_level", 0444,
1661 dapm->debugfs_dapm, dapm,
1662 &dapm_bias_fops);
1663 if (!d)
1664 dev_warn(dapm->dev,
1665 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001666}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001667
1668static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1669{
1670 struct snd_soc_dapm_context *dapm = w->dapm;
1671 struct dentry *d;
1672
1673 if (!dapm->debugfs_dapm || !w->name)
1674 return;
1675
1676 d = debugfs_create_file(w->name, 0444,
1677 dapm->debugfs_dapm, w,
1678 &dapm_widget_power_fops);
1679 if (!d)
1680 dev_warn(w->dapm->dev,
1681 "ASoC: Failed to create %s debugfs file\n",
1682 w->name);
1683}
1684
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001685static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1686{
1687 debugfs_remove_recursive(dapm->debugfs_dapm);
1688}
1689
Mark Brown79fb9382009-08-21 16:38:13 +01001690#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001691void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1692 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001693{
1694}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001695
1696static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1697{
1698}
1699
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001700static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1701{
1702}
1703
Mark Brown79fb9382009-08-21 16:38:13 +01001704#endif
1705
Richard Purdie2b97eab2006-10-06 18:32:18 +02001706/* test and update the power status of a mux widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001707static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001708 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001709{
1710 struct snd_soc_dapm_path *path;
1711 int found = 0;
1712
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001713 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001714 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001715 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001716 return -ENODEV;
1717
Richard Purdie2b97eab2006-10-06 18:32:18 +02001718 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001719 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001720 if (path->kcontrol != kcontrol)
1721 continue;
1722
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001723 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001724 continue;
1725
1726 found = 1;
1727 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001728 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001729 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001730 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001731 } else {
1732 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001733 dapm_mark_dirty(path->source,
1734 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001735 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001736 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001737 }
1738
Mark Browndb432b42011-10-03 21:06:40 +01001739 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001740 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001741 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001742 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001743
1744 return 0;
1745}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001746
1747int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1748 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1749{
1750 struct snd_soc_card *card = widget->dapm->card;
1751 int ret;
1752
Liam Girdwood3cd04342012-03-09 12:02:08 +00001753 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001754 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1755 mutex_unlock(&card->dapm_mutex);
1756 return ret;
1757}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001758EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001759
Milan plzik1b075e32008-01-10 14:39:46 +01001760/* test and update the power status of a mixer or switch widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001761static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001762 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001763{
1764 struct snd_soc_dapm_path *path;
1765 int found = 0;
1766
Milan plzik1b075e32008-01-10 14:39:46 +01001767 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001768 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001769 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001770 return -ENODEV;
1771
Richard Purdie2b97eab2006-10-06 18:32:18 +02001772 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001773 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001774 if (path->kcontrol != kcontrol)
1775 continue;
1776
1777 /* found, now check type */
1778 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001779 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001780 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001781 }
1782
Mark Browndb432b42011-10-03 21:06:40 +01001783 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001784 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001785 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001786 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001787
1788 return 0;
1789}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001790
1791int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1792 struct snd_kcontrol *kcontrol, int connect)
1793{
1794 struct snd_soc_card *card = widget->dapm->card;
1795 int ret;
1796
Liam Girdwood3cd04342012-03-09 12:02:08 +00001797 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001798 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
1799 mutex_unlock(&card->dapm_mutex);
1800 return ret;
1801}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001802EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001803
1804/* show dapm widget status in sys fs */
1805static ssize_t dapm_widget_show(struct device *dev,
1806 struct device_attribute *attr, char *buf)
1807{
Mark Brown36ae1a92012-01-06 17:12:45 -08001808 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001809 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001810 struct snd_soc_dapm_widget *w;
1811 int count = 0;
1812 char *state = "not set";
1813
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001814 list_for_each_entry(w, &codec->card->widgets, list) {
1815 if (w->dapm != &codec->dapm)
1816 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001817
1818 /* only display widgets that burnm power */
1819 switch (w->id) {
1820 case snd_soc_dapm_hp:
1821 case snd_soc_dapm_mic:
1822 case snd_soc_dapm_spk:
1823 case snd_soc_dapm_line:
1824 case snd_soc_dapm_micbias:
1825 case snd_soc_dapm_dac:
1826 case snd_soc_dapm_adc:
1827 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001828 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001829 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001830 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001831 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001832 case snd_soc_dapm_regulator_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001833 if (w->name)
1834 count += sprintf(buf + count, "%s: %s\n",
1835 w->name, w->power ? "On":"Off");
1836 break;
1837 default:
1838 break;
1839 }
1840 }
1841
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001842 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001843 case SND_SOC_BIAS_ON:
1844 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001845 break;
Mark Brown0be98982008-05-19 12:31:28 +02001846 case SND_SOC_BIAS_PREPARE:
1847 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001848 break;
Mark Brown0be98982008-05-19 12:31:28 +02001849 case SND_SOC_BIAS_STANDBY:
1850 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001851 break;
Mark Brown0be98982008-05-19 12:31:28 +02001852 case SND_SOC_BIAS_OFF:
1853 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001854 break;
1855 }
1856 count += sprintf(buf + count, "PM State: %s\n", state);
1857
1858 return count;
1859}
1860
1861static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1862
1863int snd_soc_dapm_sys_add(struct device *dev)
1864{
Troy Kisky12ef1932008-10-13 17:42:14 -07001865 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001866}
1867
1868static void snd_soc_dapm_sys_remove(struct device *dev)
1869{
Mark Brownaef90842009-05-16 17:53:16 +01001870 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001871}
1872
1873/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001874static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001875{
1876 struct snd_soc_dapm_widget *w, *next_w;
1877 struct snd_soc_dapm_path *p, *next_p;
1878
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001879 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1880 if (w->dapm != dapm)
1881 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001882 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001883 /*
1884 * remove source and sink paths associated to this widget.
1885 * While removing the path, remove reference to it from both
1886 * source and sink widgets so that path is removed only once.
1887 */
1888 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1889 list_del(&p->list_sink);
1890 list_del(&p->list_source);
1891 list_del(&p->list);
1892 kfree(p->long_name);
1893 kfree(p);
1894 }
1895 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1896 list_del(&p->list_sink);
1897 list_del(&p->list_source);
1898 list_del(&p->list);
1899 kfree(p->long_name);
1900 kfree(p);
1901 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001902 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001903 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001904 kfree(w);
1905 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001906}
1907
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001908static struct snd_soc_dapm_widget *dapm_find_widget(
1909 struct snd_soc_dapm_context *dapm, const char *pin,
1910 bool search_other_contexts)
1911{
1912 struct snd_soc_dapm_widget *w;
1913 struct snd_soc_dapm_widget *fallback = NULL;
1914
1915 list_for_each_entry(w, &dapm->card->widgets, list) {
1916 if (!strcmp(w->name, pin)) {
1917 if (w->dapm == dapm)
1918 return w;
1919 else
1920 fallback = w;
1921 }
1922 }
1923
1924 if (search_other_contexts)
1925 return fallback;
1926
1927 return NULL;
1928}
1929
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001930static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001931 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001932{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001933 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001934
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001935 if (!w) {
1936 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1937 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001938 }
1939
Mark Brown1a8b2d92012-02-16 11:50:07 -08001940 if (w->connected != status)
1941 dapm_mark_dirty(w, "pin configuration");
1942
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001943 w->connected = status;
1944 if (status == 0)
1945 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001946
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001947 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001948}
1949
Richard Purdie2b97eab2006-10-06 18:32:18 +02001950/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001951 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001952 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001953 *
1954 * Walks all dapm audio paths and powers widgets according to their
1955 * stream or path usage.
1956 *
1957 * Returns 0 for success.
1958 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001959int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001960{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00001961 int ret;
1962
Mark Brown4f4c0072011-10-07 14:29:19 +01001963 /*
1964 * Suppress early reports (eg, jacks syncing their state) to avoid
1965 * silly DAPM runs during card startup.
1966 */
1967 if (!dapm->card || !dapm->card->instantiated)
1968 return 0;
1969
Liam Girdwood3cd04342012-03-09 12:02:08 +00001970 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00001971 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
1972 mutex_unlock(&dapm->card->dapm_mutex);
1973 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001974}
Liam Girdwooda5302182008-07-07 13:35:17 +01001975EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001976
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001977static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001978 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001979{
1980 struct snd_soc_dapm_path *path;
1981 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001982 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001983 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001984 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001985 const char *source;
1986 char prefixed_sink[80];
1987 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001988 int ret = 0;
1989
Mark Brown88e8b9a2011-03-02 18:18:24 +00001990 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001991 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1992 dapm->codec->name_prefix, route->sink);
1993 sink = prefixed_sink;
1994 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1995 dapm->codec->name_prefix, route->source);
1996 source = prefixed_source;
1997 } else {
1998 sink = route->sink;
1999 source = route->source;
2000 }
2001
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002002 /*
2003 * find src and dest widgets over all widgets but favor a widget from
2004 * current DAPM context
2005 */
2006 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002007 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002008 wtsink = w;
2009 if (w->dapm == dapm)
2010 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002011 continue;
2012 }
2013 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002014 wtsource = w;
2015 if (w->dapm == dapm)
2016 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002017 }
2018 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002019 /* use widget from another DAPM context if not found from this */
2020 if (!wsink)
2021 wsink = wtsink;
2022 if (!wsource)
2023 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002024
2025 if (wsource == NULL || wsink == NULL)
2026 return -ENODEV;
2027
2028 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2029 if (!path)
2030 return -ENOMEM;
2031
2032 path->source = wsource;
2033 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002034 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002035 INIT_LIST_HEAD(&path->list);
2036 INIT_LIST_HEAD(&path->list_source);
2037 INIT_LIST_HEAD(&path->list_sink);
2038
2039 /* check for external widgets */
2040 if (wsink->id == snd_soc_dapm_input) {
2041 if (wsource->id == snd_soc_dapm_micbias ||
2042 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002043 wsource->id == snd_soc_dapm_line ||
2044 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002045 wsink->ext = 1;
2046 }
2047 if (wsource->id == snd_soc_dapm_output) {
2048 if (wsink->id == snd_soc_dapm_spk ||
2049 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002050 wsink->id == snd_soc_dapm_line ||
2051 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002052 wsource->ext = 1;
2053 }
2054
2055 /* connect static paths */
2056 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002057 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002058 list_add(&path->list_sink, &wsink->sources);
2059 list_add(&path->list_source, &wsource->sinks);
2060 path->connect = 1;
2061 return 0;
2062 }
2063
2064 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002065 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002066 case snd_soc_dapm_adc:
2067 case snd_soc_dapm_dac:
2068 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002069 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002070 case snd_soc_dapm_input:
2071 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002072 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002073 case snd_soc_dapm_micbias:
2074 case snd_soc_dapm_vmid:
2075 case snd_soc_dapm_pre:
2076 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002077 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002078 case snd_soc_dapm_regulator_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002079 case snd_soc_dapm_aif_in:
2080 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -08002081 case snd_soc_dapm_dai:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002082 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002083 list_add(&path->list_sink, &wsink->sources);
2084 list_add(&path->list_source, &wsource->sinks);
2085 path->connect = 1;
2086 return 0;
2087 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002088 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002089 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002090 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002091 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002092 if (ret != 0)
2093 goto err;
2094 break;
2095 case snd_soc_dapm_switch:
2096 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002097 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002098 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002099 if (ret != 0)
2100 goto err;
2101 break;
2102 case snd_soc_dapm_hp:
2103 case snd_soc_dapm_mic:
2104 case snd_soc_dapm_line:
2105 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002106 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002107 list_add(&path->list_sink, &wsink->sources);
2108 list_add(&path->list_source, &wsource->sinks);
2109 path->connect = 0;
2110 return 0;
2111 }
2112 return 0;
2113
2114err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002115 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2116 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002117 kfree(path);
2118 return ret;
2119}
Mark Brown105f1c22008-05-13 14:52:19 +02002120
2121/**
Mark Brown105f1c22008-05-13 14:52:19 +02002122 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002123 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002124 * @route: audio routes
2125 * @num: number of routes
2126 *
2127 * Connects 2 dapm widgets together via a named audio path. The sink is
2128 * the widget receiving the audio signal, whilst the source is the sender
2129 * of the audio signal.
2130 *
2131 * Returns 0 for success else error. On error all resources can be freed
2132 * with a call to snd_soc_card_free().
2133 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002134int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002135 const struct snd_soc_dapm_route *route, int num)
2136{
Dan Carpenter60884c22012-04-13 22:25:43 +03002137 int i, ret = 0;
Mark Brown105f1c22008-05-13 14:52:19 +02002138
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002139 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002140 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002141 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02002142 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002143 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2144 route->source, route->sink);
Dan Carpenter60884c22012-04-13 22:25:43 +03002145 break;
Mark Brown105f1c22008-05-13 14:52:19 +02002146 }
2147 route++;
2148 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002149 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002150
Dan Carpenter60884c22012-04-13 22:25:43 +03002151 return ret;
Mark Brown105f1c22008-05-13 14:52:19 +02002152}
2153EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2154
Mark Brownbf3a9e12011-06-13 16:42:29 +01002155static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2156 const struct snd_soc_dapm_route *route)
2157{
2158 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2159 route->source,
2160 true);
2161 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2162 route->sink,
2163 true);
2164 struct snd_soc_dapm_path *path;
2165 int count = 0;
2166
2167 if (!source) {
2168 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2169 route->source);
2170 return -ENODEV;
2171 }
2172
2173 if (!sink) {
2174 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2175 route->sink);
2176 return -ENODEV;
2177 }
2178
2179 if (route->control || route->connected)
2180 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2181 route->source, route->sink);
2182
2183 list_for_each_entry(path, &source->sinks, list_source) {
2184 if (path->sink == sink) {
2185 path->weak = 1;
2186 count++;
2187 }
2188 }
2189
2190 if (count == 0)
2191 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2192 route->source, route->sink);
2193 if (count > 1)
2194 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2195 count, route->source, route->sink);
2196
2197 return 0;
2198}
2199
2200/**
2201 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2202 * @dapm: DAPM context
2203 * @route: audio routes
2204 * @num: number of routes
2205 *
2206 * Mark existing routes matching those specified in the passed array
2207 * as being weak, meaning that they are ignored for the purpose of
2208 * power decisions. The main intended use case is for sidetone paths
2209 * which couple audio between other independent paths if they are both
2210 * active in order to make the combination work better at the user
2211 * level but which aren't intended to be "used".
2212 *
2213 * Note that CODEC drivers should not use this as sidetone type paths
2214 * can frequently also be used as bypass paths.
2215 */
2216int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2217 const struct snd_soc_dapm_route *route, int num)
2218{
2219 int i, err;
2220 int ret = 0;
2221
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002222 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002223 for (i = 0; i < num; i++) {
2224 err = snd_soc_dapm_weak_route(dapm, route);
2225 if (err)
2226 ret = err;
2227 route++;
2228 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002229 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002230
2231 return ret;
2232}
2233EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2234
Mark Brown105f1c22008-05-13 14:52:19 +02002235/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002236 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002237 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002238 *
2239 * Checks the codec for any new dapm widgets and creates them if found.
2240 *
2241 * Returns 0 for success.
2242 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002243int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002244{
2245 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002246 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002247
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002248 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2249
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002250 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002251 {
2252 if (w->new)
2253 continue;
2254
Stephen Warrenfad59882011-04-28 17:37:59 -06002255 if (w->num_kcontrols) {
2256 w->kcontrols = kzalloc(w->num_kcontrols *
2257 sizeof(struct snd_kcontrol *),
2258 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002259 if (!w->kcontrols) {
2260 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002261 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002262 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002263 }
2264
Richard Purdie2b97eab2006-10-06 18:32:18 +02002265 switch(w->id) {
2266 case snd_soc_dapm_switch:
2267 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002268 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002269 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002270 break;
2271 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002272 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002273 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002274 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002276 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002277 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002278 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002279 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002280 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002281 break;
2282 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002283
2284 /* Read the initial power state from the device */
2285 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002286 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002287 val &= 1 << w->shift;
2288 if (w->invert)
2289 val = !val;
2290
2291 if (val)
2292 w->power = 1;
2293 }
2294
Richard Purdie2b97eab2006-10-06 18:32:18 +02002295 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002296
Mark Brown7508b122011-10-05 12:09:12 +01002297 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002298 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002299 }
2300
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002301 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002302 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002303 return 0;
2304}
2305EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2306
2307/**
2308 * snd_soc_dapm_get_volsw - dapm mixer get callback
2309 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002310 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002311 *
2312 * Callback to get the value of a dapm mixer control.
2313 *
2314 * Returns 0 for success.
2315 */
2316int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2317 struct snd_ctl_elem_value *ucontrol)
2318{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002319 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2320 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002321 struct soc_mixer_control *mc =
2322 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002323 unsigned int reg = mc->reg;
2324 unsigned int shift = mc->shift;
2325 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002326 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002327 unsigned int invert = mc->invert;
2328 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002329
Richard Purdie2b97eab2006-10-06 18:32:18 +02002330 ucontrol->value.integer.value[0] =
2331 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2332 if (shift != rshift)
2333 ucontrol->value.integer.value[1] =
2334 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2335 if (invert) {
2336 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002337 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002338 if (shift != rshift)
2339 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002340 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002341 }
2342
2343 return 0;
2344}
2345EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2346
2347/**
2348 * snd_soc_dapm_put_volsw - dapm mixer set callback
2349 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002350 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002351 *
2352 * Callback to set the value of a dapm mixer control.
2353 *
2354 * Returns 0 for success.
2355 */
2356int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2357 struct snd_ctl_elem_value *ucontrol)
2358{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002359 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2360 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2361 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002362 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002363 struct soc_mixer_control *mc =
2364 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002365 unsigned int reg = mc->reg;
2366 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002367 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002368 unsigned int mask = (1 << fls(max)) - 1;
2369 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002370 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002371 int connect, change;
2372 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002373 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002374
2375 val = (ucontrol->value.integer.value[0] & mask);
2376
2377 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002378 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002379 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002380 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002381
Stephen Warrenfafd2172011-04-28 17:38:00 -06002382 if (val)
2383 /* new connection */
2384 connect = invert ? 0 : 1;
2385 else
2386 /* old connection must be powered down */
2387 connect = invert ? 1 : 0;
2388
Liam Girdwood3cd04342012-03-09 12:02:08 +00002389 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002390
Stephen Warrene9cf7042011-01-27 14:54:05 -07002391 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002392 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002393 for (wi = 0; wi < wlist->num_widgets; wi++) {
2394 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002395
Stephen Warrenfafd2172011-04-28 17:38:00 -06002396 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002397
Stephen Warrenfafd2172011-04-28 17:38:00 -06002398 update.kcontrol = kcontrol;
2399 update.widget = widget;
2400 update.reg = reg;
2401 update.mask = mask;
2402 update.val = val;
2403 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002404
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002405 soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002406
2407 widget->dapm->update = NULL;
2408 }
Mark Brown283375c2009-12-07 18:09:03 +00002409 }
2410
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002411 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002412 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002413}
2414EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2415
2416/**
2417 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2418 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002419 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002420 *
2421 * Callback to get the value of a dapm enumerated double mixer control.
2422 *
2423 * Returns 0 for success.
2424 */
2425int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_value *ucontrol)
2427{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002428 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2429 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002430 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002431 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002432
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002433 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002434 ;
2435 val = snd_soc_read(widget->codec, e->reg);
2436 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2437 if (e->shift_l != e->shift_r)
2438 ucontrol->value.enumerated.item[1] =
2439 (val >> e->shift_r) & (bitmask - 1);
2440
2441 return 0;
2442}
2443EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2444
2445/**
2446 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2447 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002448 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002449 *
2450 * Callback to set the value of a dapm enumerated double mixer control.
2451 *
2452 * Returns 0 for success.
2453 */
2454int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2455 struct snd_ctl_elem_value *ucontrol)
2456{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002457 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2458 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2459 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002460 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002461 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002462 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002463 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002464 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002465 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002466
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002467 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002468 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002469 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002470 return -EINVAL;
2471 mux = ucontrol->value.enumerated.item[0];
2472 val = mux << e->shift_l;
2473 mask = (bitmask - 1) << e->shift_l;
2474 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002475 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002476 return -EINVAL;
2477 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2478 mask |= (bitmask - 1) << e->shift_r;
2479 }
2480
Liam Girdwood3cd04342012-03-09 12:02:08 +00002481 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002482
Mark Brown3a655772009-10-05 17:23:30 +01002483 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002484 if (change) {
2485 for (wi = 0; wi < wlist->num_widgets; wi++) {
2486 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002487
Stephen Warrenfafd2172011-04-28 17:38:00 -06002488 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002489
Stephen Warrenfafd2172011-04-28 17:38:00 -06002490 update.kcontrol = kcontrol;
2491 update.widget = widget;
2492 update.reg = e->reg;
2493 update.mask = mask;
2494 update.val = val;
2495 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002496
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002497 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002498
Stephen Warrenfafd2172011-04-28 17:38:00 -06002499 widget->dapm->update = NULL;
2500 }
2501 }
2502
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002503 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002504 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002505}
2506EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2507
2508/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002509 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2510 * @kcontrol: mixer control
2511 * @ucontrol: control element information
2512 *
2513 * Returns 0 for success.
2514 */
2515int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2516 struct snd_ctl_elem_value *ucontrol)
2517{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002518 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2519 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002520
2521 ucontrol->value.enumerated.item[0] = widget->value;
2522
2523 return 0;
2524}
2525EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2526
2527/**
2528 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2529 * @kcontrol: mixer control
2530 * @ucontrol: control element information
2531 *
2532 * Returns 0 for success.
2533 */
2534int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2535 struct snd_ctl_elem_value *ucontrol)
2536{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002537 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2538 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2539 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002540 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002541 struct soc_enum *e =
2542 (struct soc_enum *)kcontrol->private_value;
2543 int change;
2544 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002545 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002546
2547 if (ucontrol->value.enumerated.item[0] >= e->max)
2548 return -EINVAL;
2549
Liam Girdwood3cd04342012-03-09 12:02:08 +00002550 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002551
2552 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002553 if (change) {
2554 for (wi = 0; wi < wlist->num_widgets; wi++) {
2555 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002556
Stephen Warrenfafd2172011-04-28 17:38:00 -06002557 widget->value = ucontrol->value.enumerated.item[0];
2558
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002559 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002560 }
2561 }
2562
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002563 mutex_unlock(&card->dapm_mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002564 return ret;
2565}
2566EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2567
2568/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002569 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2570 * callback
2571 * @kcontrol: mixer control
2572 * @ucontrol: control element information
2573 *
2574 * Callback to get the value of a dapm semi enumerated double mixer control.
2575 *
2576 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2577 * used for handling bitfield coded enumeration for example.
2578 *
2579 * Returns 0 for success.
2580 */
2581int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2582 struct snd_ctl_elem_value *ucontrol)
2583{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002584 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2585 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002586 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002587 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002588
2589 reg_val = snd_soc_read(widget->codec, e->reg);
2590 val = (reg_val >> e->shift_l) & e->mask;
2591 for (mux = 0; mux < e->max; mux++) {
2592 if (val == e->values[mux])
2593 break;
2594 }
2595 ucontrol->value.enumerated.item[0] = mux;
2596 if (e->shift_l != e->shift_r) {
2597 val = (reg_val >> e->shift_r) & e->mask;
2598 for (mux = 0; mux < e->max; mux++) {
2599 if (val == e->values[mux])
2600 break;
2601 }
2602 ucontrol->value.enumerated.item[1] = mux;
2603 }
2604
2605 return 0;
2606}
2607EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2608
2609/**
2610 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2611 * callback
2612 * @kcontrol: mixer control
2613 * @ucontrol: control element information
2614 *
2615 * Callback to set the value of a dapm semi enumerated double mixer control.
2616 *
2617 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2618 * used for handling bitfield coded enumeration for example.
2619 *
2620 * Returns 0 for success.
2621 */
2622int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2623 struct snd_ctl_elem_value *ucontrol)
2624{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002625 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2626 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2627 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002628 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002629 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002630 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002631 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002632 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002633 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002634
2635 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2636 return -EINVAL;
2637 mux = ucontrol->value.enumerated.item[0];
2638 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2639 mask = e->mask << e->shift_l;
2640 if (e->shift_l != e->shift_r) {
2641 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2642 return -EINVAL;
2643 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2644 mask |= e->mask << e->shift_r;
2645 }
2646
Liam Girdwood3cd04342012-03-09 12:02:08 +00002647 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002648
Mark Brown3a655772009-10-05 17:23:30 +01002649 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002650 if (change) {
2651 for (wi = 0; wi < wlist->num_widgets; wi++) {
2652 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002653
Stephen Warrenfafd2172011-04-28 17:38:00 -06002654 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002655
Stephen Warrenfafd2172011-04-28 17:38:00 -06002656 update.kcontrol = kcontrol;
2657 update.widget = widget;
2658 update.reg = e->reg;
2659 update.mask = mask;
2660 update.val = val;
2661 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002662
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002663 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002664
Stephen Warrenfafd2172011-04-28 17:38:00 -06002665 widget->dapm->update = NULL;
2666 }
2667 }
2668
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002669 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002670 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002671}
2672EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2673
2674/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002675 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2676 *
2677 * @kcontrol: mixer control
2678 * @uinfo: control element information
2679 *
2680 * Callback to provide information about a pin switch control.
2681 */
2682int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2683 struct snd_ctl_elem_info *uinfo)
2684{
2685 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2686 uinfo->count = 1;
2687 uinfo->value.integer.min = 0;
2688 uinfo->value.integer.max = 1;
2689
2690 return 0;
2691}
2692EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2693
2694/**
2695 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2696 *
2697 * @kcontrol: mixer control
2698 * @ucontrol: Value
2699 */
2700int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2701 struct snd_ctl_elem_value *ucontrol)
2702{
Mark Brown48a8c392012-02-14 17:11:15 -08002703 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002704 const char *pin = (const char *)kcontrol->private_value;
2705
Liam Girdwood3cd04342012-03-09 12:02:08 +00002706 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002707
2708 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002709 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002710
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002711 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002712
2713 return 0;
2714}
2715EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2716
2717/**
2718 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2719 *
2720 * @kcontrol: mixer control
2721 * @ucontrol: Value
2722 */
2723int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2724 struct snd_ctl_elem_value *ucontrol)
2725{
Mark Brown48a8c392012-02-14 17:11:15 -08002726 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002727 const char *pin = (const char *)kcontrol->private_value;
2728
Liam Girdwood3cd04342012-03-09 12:02:08 +00002729 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002730
2731 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002732 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002733 else
Mark Brown48a8c392012-02-14 17:11:15 -08002734 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002735
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002736 mutex_unlock(&card->dapm_mutex);
2737
Mark Brown48a8c392012-02-14 17:11:15 -08002738 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002739 return 0;
2740}
2741EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2742
Mark Brown5ba06fc2012-02-16 11:07:13 -08002743static struct snd_soc_dapm_widget *
2744snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2745 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002746{
2747 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002748 size_t name_len;
Mark Brown62ea8742012-01-21 21:14:48 +00002749 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002750
2751 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08002752 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002753
Mark Brown62ea8742012-01-21 21:14:48 +00002754 switch (w->id) {
2755 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00002756 w->regulator = devm_regulator_get(dapm->dev, w->name);
2757 if (IS_ERR(w->regulator)) {
2758 ret = PTR_ERR(w->regulator);
Mark Brown62ea8742012-01-21 21:14:48 +00002759 dev_err(dapm->dev, "Failed to request %s: %d\n",
2760 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002761 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00002762 }
2763 break;
2764 default:
2765 break;
2766 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002767
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002768 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002769 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002770 name_len += 1 + strlen(dapm->codec->name_prefix);
2771 w->name = kmalloc(name_len, GFP_KERNEL);
2772 if (w->name == NULL) {
2773 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002774 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002775 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002776 if (dapm->codec && dapm->codec->name_prefix)
Mark Brown888df392012-02-16 19:37:51 -08002777 snprintf((char *)w->name, name_len, "%s %s",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002778 dapm->codec->name_prefix, widget->name);
2779 else
Mark Brown888df392012-02-16 19:37:51 -08002780 snprintf((char *)w->name, name_len, "%s", widget->name);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002781
Mark Brown7ca3a182011-10-08 14:04:50 +01002782 switch (w->id) {
2783 case snd_soc_dapm_switch:
2784 case snd_soc_dapm_mixer:
2785 case snd_soc_dapm_mixer_named_ctl:
2786 w->power_check = dapm_generic_check_power;
2787 break;
2788 case snd_soc_dapm_mux:
2789 case snd_soc_dapm_virt_mux:
2790 case snd_soc_dapm_value_mux:
2791 w->power_check = dapm_generic_check_power;
2792 break;
2793 case snd_soc_dapm_adc:
2794 case snd_soc_dapm_aif_out:
2795 w->power_check = dapm_adc_check_power;
2796 break;
2797 case snd_soc_dapm_dac:
2798 case snd_soc_dapm_aif_in:
2799 w->power_check = dapm_dac_check_power;
2800 break;
2801 case snd_soc_dapm_pga:
2802 case snd_soc_dapm_out_drv:
2803 case snd_soc_dapm_input:
2804 case snd_soc_dapm_output:
2805 case snd_soc_dapm_micbias:
2806 case snd_soc_dapm_spk:
2807 case snd_soc_dapm_hp:
2808 case snd_soc_dapm_mic:
2809 case snd_soc_dapm_line:
2810 w->power_check = dapm_generic_check_power;
2811 break;
2812 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002813 case snd_soc_dapm_regulator_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01002814 w->power_check = dapm_supply_check_power;
2815 break;
Mark Brown888df392012-02-16 19:37:51 -08002816 case snd_soc_dapm_dai:
2817 w->power_check = dapm_dai_check_power;
2818 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002819 default:
2820 w->power_check = dapm_always_on_check_power;
2821 break;
2822 }
2823
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002824 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002825 w->dapm = dapm;
2826 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002827 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002828 INIT_LIST_HEAD(&w->sources);
2829 INIT_LIST_HEAD(&w->sinks);
2830 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01002831 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002832 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002833
2834 /* machine layer set ups unconnected pins and insertions */
2835 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08002836 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002837}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002838
2839/**
Mark Brown4ba13272008-05-13 14:51:19 +02002840 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002841 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002842 * @widget: widget array
2843 * @num: number of widgets
2844 *
2845 * Creates new DAPM controls based upon the templates.
2846 *
2847 * Returns 0 for success else error.
2848 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002849int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002850 const struct snd_soc_dapm_widget *widget,
2851 int num)
2852{
Mark Brown5ba06fc2012-02-16 11:07:13 -08002853 struct snd_soc_dapm_widget *w;
2854 int i;
Dan Carpenter60884c22012-04-13 22:25:43 +03002855 int ret = 0;
Mark Brown4ba13272008-05-13 14:51:19 +02002856
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002857 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02002858 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08002859 w = snd_soc_dapm_new_control(dapm, widget);
2860 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002861 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08002862 "ASoC: Failed to create DAPM control %s\n",
2863 widget->name);
Dan Carpenter60884c22012-04-13 22:25:43 +03002864 ret = -ENOMEM;
2865 break;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002866 }
Mark Brown4ba13272008-05-13 14:51:19 +02002867 widget++;
2868 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002869 mutex_unlock(&dapm->card->dapm_mutex);
Dan Carpenter60884c22012-04-13 22:25:43 +03002870 return ret;
Mark Brown4ba13272008-05-13 14:51:19 +02002871}
2872EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2873
Mark Brown888df392012-02-16 19:37:51 -08002874int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
2875 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002876{
Mark Brown888df392012-02-16 19:37:51 -08002877 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002878 struct snd_soc_dapm_widget *w;
2879
Mark Brown888df392012-02-16 19:37:51 -08002880 WARN_ON(dapm->dev != dai->dev);
2881
2882 memset(&template, 0, sizeof(template));
2883 template.reg = SND_SOC_NOPM;
2884
2885 if (dai->driver->playback.stream_name) {
2886 template.id = snd_soc_dapm_dai;
2887 template.name = dai->driver->playback.stream_name;
2888 template.sname = dai->driver->playback.stream_name;
2889
2890 dev_dbg(dai->dev, "adding %s widget\n",
2891 template.name);
2892
2893 w = snd_soc_dapm_new_control(dapm, &template);
2894 if (!w) {
2895 dev_err(dapm->dev, "Failed to create %s widget\n",
2896 dai->driver->playback.stream_name);
2897 }
2898
2899 w->priv = dai;
2900 dai->playback_widget = w;
2901 }
2902
2903 if (dai->driver->capture.stream_name) {
2904 template.id = snd_soc_dapm_dai;
2905 template.name = dai->driver->capture.stream_name;
2906 template.sname = dai->driver->capture.stream_name;
2907
2908 dev_dbg(dai->dev, "adding %s widget\n",
2909 template.name);
2910
2911 w = snd_soc_dapm_new_control(dapm, &template);
2912 if (!w) {
2913 dev_err(dapm->dev, "Failed to create %s widget\n",
2914 dai->driver->capture.stream_name);
2915 }
2916
2917 w->priv = dai;
2918 dai->capture_widget = w;
2919 }
2920
2921 return 0;
2922}
2923
2924int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
2925{
2926 struct snd_soc_dapm_widget *dai_w, *w;
2927 struct snd_soc_dai *dai;
2928 struct snd_soc_dapm_route r;
2929
2930 memset(&r, 0, sizeof(r));
2931
2932 /* For each DAI widget... */
2933 list_for_each_entry(dai_w, &card->widgets, list) {
2934 if (dai_w->id != snd_soc_dapm_dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002935 continue;
Mark Brown888df392012-02-16 19:37:51 -08002936
2937 dai = dai_w->priv;
2938
2939 /* ...find all widgets with the same stream and link them */
2940 list_for_each_entry(w, &card->widgets, list) {
2941 if (w->dapm != dai_w->dapm)
2942 continue;
2943
2944 if (w->id == snd_soc_dapm_dai)
2945 continue;
2946
2947 if (!w->sname)
2948 continue;
2949
2950 if (dai->driver->playback.stream_name &&
2951 strstr(w->sname,
2952 dai->driver->playback.stream_name)) {
2953 r.source = dai->playback_widget->name;
2954 r.sink = w->name;
2955 dev_dbg(dai->dev, "%s -> %s\n",
2956 r.source, r.sink);
2957
2958 snd_soc_dapm_add_route(w->dapm, &r);
2959 }
2960
2961 if (dai->driver->capture.stream_name &&
2962 strstr(w->sname,
2963 dai->driver->capture.stream_name)) {
2964 r.source = w->name;
2965 r.sink = dai->capture_widget->name;
2966 dev_dbg(dai->dev, "%s -> %s\n",
2967 r.source, r.sink);
2968
2969 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002970 }
2971 }
2972 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002973
Mark Brown888df392012-02-16 19:37:51 -08002974 return 0;
2975}
Liam Girdwood64a648c2011-07-25 11:15:15 +01002976
Liam Girdwoodd9b09512012-03-07 16:32:59 +00002977static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
2978 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002979{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002980
Liam Girdwoodd9b09512012-03-07 16:32:59 +00002981 struct snd_soc_dapm_widget *w_cpu, *w_codec;
2982 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2983 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002984
Liam Girdwoodd9b09512012-03-07 16:32:59 +00002985 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2986 w_cpu = cpu_dai->playback_widget;
2987 w_codec = codec_dai->playback_widget;
2988 } else {
2989 w_cpu = cpu_dai->capture_widget;
2990 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002991 }
2992
Liam Girdwoodd9b09512012-03-07 16:32:59 +00002993 if (w_cpu) {
2994
2995 dapm_mark_dirty(w_cpu, "stream event");
2996
2997 switch (event) {
2998 case SND_SOC_DAPM_STREAM_START:
2999 w_cpu->active = 1;
3000 break;
3001 case SND_SOC_DAPM_STREAM_STOP:
3002 w_cpu->active = 0;
3003 break;
3004 case SND_SOC_DAPM_STREAM_SUSPEND:
3005 case SND_SOC_DAPM_STREAM_RESUME:
3006 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3007 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3008 break;
3009 }
3010 }
3011
3012 if (w_codec) {
3013
3014 dapm_mark_dirty(w_codec, "stream event");
3015
3016 switch (event) {
3017 case SND_SOC_DAPM_STREAM_START:
3018 w_codec->active = 1;
3019 break;
3020 case SND_SOC_DAPM_STREAM_STOP:
3021 w_codec->active = 0;
3022 break;
3023 case SND_SOC_DAPM_STREAM_SUSPEND:
3024 case SND_SOC_DAPM_STREAM_RESUME:
3025 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3026 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3027 break;
3028 }
3029 }
3030
3031 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003032}
3033
3034/**
3035 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3036 * @rtd: PCM runtime data
3037 * @stream: stream name
3038 * @event: stream event
3039 *
3040 * Sends a stream event to the dapm core. The core then makes any
3041 * necessary widget power changes.
3042 *
3043 * Returns 0 for success else error.
3044 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003045void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3046 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003047{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003048 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003049
Liam Girdwood3cd04342012-03-09 12:02:08 +00003050 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003051 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003052 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003053}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003054
3055/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003056 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003057 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003058 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003059 *
Mark Brown74b8f952009-06-06 11:26:15 +01003060 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003061 * a valid audio route and active audio stream.
3062 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3063 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003064 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003065int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003066{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003067 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003068}
Liam Girdwooda5302182008-07-07 13:35:17 +01003069EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003070
3071/**
Mark Brownda341832010-03-15 19:23:37 +00003072 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003073 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003074 * @pin: pin name
3075 *
3076 * Enables input/output pin regardless of any other state. This is
3077 * intended for use with microphone bias supplies used in microphone
3078 * jack detection.
3079 *
3080 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3081 * do any widget power switching.
3082 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003083int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3084 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003085{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003086 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003087
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003088 if (!w) {
3089 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3090 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003091 }
3092
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003093 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3094 w->connected = 1;
3095 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003096 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003097
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003098 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003099}
3100EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3101
3102/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003103 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003104 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003105 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003106 *
Mark Brown74b8f952009-06-06 11:26:15 +01003107 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003108 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3109 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003110 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003111int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3112 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003113{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003114 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003115}
3116EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3117
3118/**
Mark Brown5817b522008-09-24 11:23:11 +01003119 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003120 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003121 * @pin: pin name
3122 *
3123 * Marks the specified pin as being not connected, disabling it along
3124 * any parent or child widgets. At present this is identical to
3125 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3126 * additional things such as disabling controls which only affect
3127 * paths through the pin.
3128 *
3129 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3130 * do any widget power switching.
3131 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003132int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003133{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003134 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003135}
3136EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3137
3138/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003139 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003140 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003141 * @pin: audio signal pin endpoint (or start point)
3142 *
3143 * Get audio pin status - connected or disconnected.
3144 *
3145 * Returns 1 for connected otherwise 0.
3146 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003147int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3148 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003149{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003150 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003151
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003152 if (w)
3153 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003154
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003155 return 0;
3156}
Liam Girdwooda5302182008-07-07 13:35:17 +01003157EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003158
3159/**
Mark Brown1547aba2010-05-07 21:11:40 +01003160 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003161 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003162 * @pin: audio signal pin endpoint (or start point)
3163 *
3164 * Mark the given endpoint or pin as ignoring suspend. When the
3165 * system is disabled a path between two endpoints flagged as ignoring
3166 * suspend will not be disabled. The path must already be enabled via
3167 * normal means at suspend time, it will not be turned on if it was not
3168 * already enabled.
3169 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003170int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3171 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003172{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003173 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003174
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003175 if (!w) {
3176 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3177 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003178 }
3179
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003180 w->ignore_suspend = 1;
3181
3182 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003183}
3184EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3185
Stephen Warren16332812011-11-23 12:42:04 -07003186static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3187 struct snd_soc_dapm_widget *w)
3188{
3189 struct snd_soc_dapm_path *p;
3190
3191 list_for_each_entry(p, &card->paths, list) {
3192 if ((p->source == w) || (p->sink == w)) {
3193 dev_dbg(card->dev,
3194 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3195 p->source->name, p->source->id, p->source->dapm,
3196 p->sink->name, p->sink->id, p->sink->dapm);
3197
3198 /* Connected to something other than the codec */
3199 if (p->source->dapm != p->sink->dapm)
3200 return true;
3201 /*
3202 * Loopback connection from codec external pin to
3203 * codec external pin
3204 */
3205 if (p->sink->id == snd_soc_dapm_input) {
3206 switch (p->source->id) {
3207 case snd_soc_dapm_output:
3208 case snd_soc_dapm_micbias:
3209 return true;
3210 default:
3211 break;
3212 }
3213 }
3214 }
3215 }
3216
3217 return false;
3218}
3219
3220/**
3221 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3222 * @codec: The codec whose pins should be processed
3223 *
3224 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3225 * which are unused. Pins are used if they are connected externally to the
3226 * codec, whether that be to some other device, or a loop-back connection to
3227 * the codec itself.
3228 */
3229void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3230{
3231 struct snd_soc_card *card = codec->card;
3232 struct snd_soc_dapm_context *dapm = &codec->dapm;
3233 struct snd_soc_dapm_widget *w;
3234
Mark Browna094b802011-11-27 19:42:20 +00003235 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003236 &card->dapm, &codec->dapm);
3237
3238 list_for_each_entry(w, &card->widgets, list) {
3239 if (w->dapm != dapm)
3240 continue;
3241 switch (w->id) {
3242 case snd_soc_dapm_input:
3243 case snd_soc_dapm_output:
3244 case snd_soc_dapm_micbias:
Mark Browna094b802011-11-27 19:42:20 +00003245 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003246 w->name);
3247 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003248 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003249 "... Not in map; disabling\n");
3250 snd_soc_dapm_nc_pin(dapm, w->name);
3251 }
3252 break;
3253 default:
3254 break;
3255 }
3256 }
3257}
3258
Mark Brown1547aba2010-05-07 21:11:40 +01003259/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003260 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003261 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003262 *
3263 * Free all dapm widgets and resources.
3264 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003265void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003266{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003267 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003268 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003269 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003270 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003271}
3272EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3273
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003274static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003275{
Mark Brown51737472009-06-22 13:16:51 +01003276 struct snd_soc_dapm_widget *w;
3277 LIST_HEAD(down_list);
3278 int powerdown = 0;
3279
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003280 list_for_each_entry(w, &dapm->card->widgets, list) {
3281 if (w->dapm != dapm)
3282 continue;
Mark Brown51737472009-06-22 13:16:51 +01003283 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003284 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003285 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003286 powerdown = 1;
3287 }
3288 }
3289
3290 /* If there were no widgets to power down we're already in
3291 * standby.
3292 */
3293 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003294 if (dapm->bias_level == SND_SOC_BIAS_ON)
3295 snd_soc_dapm_set_bias_level(dapm,
3296 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003297 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003298 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3299 snd_soc_dapm_set_bias_level(dapm,
3300 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003301 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003302}
Mark Brown51737472009-06-22 13:16:51 +01003303
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003304/*
3305 * snd_soc_dapm_shutdown - callback for system shutdown
3306 */
3307void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3308{
3309 struct snd_soc_codec *codec;
3310
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003311 list_for_each_entry(codec, &card->codec_dev_list, list) {
3312 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003313 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3314 snd_soc_dapm_set_bias_level(&codec->dapm,
3315 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003316 }
Mark Brown51737472009-06-22 13:16:51 +01003317}
3318
Richard Purdie2b97eab2006-10-06 18:32:18 +02003319/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003320MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003321MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3322MODULE_LICENSE("GPL");