blob: 1e449f375d98ea82636dee7a246a3fe6af57b2b6 [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{
211 if (w->codec)
212 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{
219 if (w->codec)
220 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
926 return w->active;
927}
928
Mark Brown6ea31b92009-04-20 17:15:41 +0100929/* Check to see if an ADC has power */
930static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
931{
932 int in;
933
Mark Brownde02d072011-09-20 21:43:24 +0100934 DAPM_UPDATE_STAT(w, power_checks);
935
Mark Brown6ea31b92009-04-20 17:15:41 +0100936 if (w->active) {
937 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200938 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100939 return in != 0;
940 } else {
941 return dapm_generic_check_power(w);
942 }
943}
944
945/* Check to see if a DAC has power */
946static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
947{
948 int out;
949
Mark Brownde02d072011-09-20 21:43:24 +0100950 DAPM_UPDATE_STAT(w, power_checks);
951
Mark Brown6ea31b92009-04-20 17:15:41 +0100952 if (w->active) {
953 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200954 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100955 return out != 0;
956 } else {
957 return dapm_generic_check_power(w);
958 }
959}
960
Mark Brown246d0a12009-04-22 18:24:55 +0100961/* Check to see if a power supply is needed */
962static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
963{
964 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +0100965
Mark Brownde02d072011-09-20 21:43:24 +0100966 DAPM_UPDATE_STAT(w, power_checks);
967
Mark Brown246d0a12009-04-22 18:24:55 +0100968 /* Check if one of our outputs is connected */
969 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +0100970 DAPM_UPDATE_STAT(w, neighbour_checks);
971
Mark Brownbf3a9e12011-06-13 16:42:29 +0100972 if (path->weak)
973 continue;
974
Mark Brown215edda2009-09-08 18:59:05 +0100975 if (path->connected &&
976 !path->connected(path->source, path->sink))
977 continue;
978
Mark Brown30173582011-02-11 11:42:19 +0000979 if (!path->sink)
980 continue;
981
Mark Brownf68d7e12011-10-04 22:57:50 +0100982 if (dapm_widget_power_check(path->sink))
983 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +0100984 }
985
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200986 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100987
Mark Brownf68d7e12011-10-04 22:57:50 +0100988 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +0100989}
990
Mark Brown35c64bc2011-09-28 18:23:53 +0100991static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
992{
993 return 1;
994}
995
Mark Brown38357ab2009-06-06 19:03:23 +0100996static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
997 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000998 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000999{
Mark Brown828a8422011-01-15 13:14:30 +00001000 int *sort;
1001
1002 if (power_up)
1003 sort = dapm_up_seq;
1004 else
1005 sort = dapm_down_seq;
1006
Mark Brown38357ab2009-06-06 19:03:23 +01001007 if (sort[a->id] != sort[b->id])
1008 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +00001009 if (a->subseq != b->subseq) {
1010 if (power_up)
1011 return a->subseq - b->subseq;
1012 else
1013 return b->subseq - a->subseq;
1014 }
Mark Brownb22ead22009-06-07 12:51:26 +01001015 if (a->reg != b->reg)
1016 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +00001017 if (a->dapm != b->dapm)
1018 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +00001019
Mark Brown38357ab2009-06-06 19:03:23 +01001020 return 0;
1021}
Mark Brown42aa3412009-03-01 19:21:10 +00001022
Mark Brown38357ab2009-06-06 19:03:23 +01001023/* Insert a widget in order into a DAPM power sequence. */
1024static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1025 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001026 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001027{
1028 struct snd_soc_dapm_widget *w;
1029
1030 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001031 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001032 list_add_tail(&new_widget->power_list, &w->power_list);
1033 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001034 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001035
Mark Brown38357ab2009-06-06 19:03:23 +01001036 list_add_tail(&new_widget->power_list, list);
1037}
Mark Brown42aa3412009-03-01 19:21:10 +00001038
Mark Brown68f89ad2010-11-03 23:51:49 -04001039static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1040 struct snd_soc_dapm_widget *w, int event)
1041{
1042 struct snd_soc_card *card = dapm->card;
1043 const char *ev_name;
1044 int power, ret;
1045
1046 switch (event) {
1047 case SND_SOC_DAPM_PRE_PMU:
1048 ev_name = "PRE_PMU";
1049 power = 1;
1050 break;
1051 case SND_SOC_DAPM_POST_PMU:
1052 ev_name = "POST_PMU";
1053 power = 1;
1054 break;
1055 case SND_SOC_DAPM_PRE_PMD:
1056 ev_name = "PRE_PMD";
1057 power = 0;
1058 break;
1059 case SND_SOC_DAPM_POST_PMD:
1060 ev_name = "POST_PMD";
1061 power = 0;
1062 break;
1063 default:
1064 BUG();
1065 return;
1066 }
1067
1068 if (w->power != power)
1069 return;
1070
1071 if (w->event && (w->event_flags & event)) {
1072 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1073 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001074 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001075 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001076 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001077 if (ret < 0)
1078 pr_err("%s: %s event failed: %d\n",
1079 ev_name, w->name, ret);
1080 }
1081}
1082
Mark Brownb22ead22009-06-07 12:51:26 +01001083/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001084static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001085 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001086{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001087 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001088 struct snd_soc_dapm_widget *w;
1089 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001090 unsigned int value = 0;
1091 unsigned int mask = 0;
1092 unsigned int cur_mask;
1093
1094 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1095 power_list)->reg;
1096
1097 list_for_each_entry(w, pending, power_list) {
1098 cur_mask = 1 << w->shift;
1099 BUG_ON(reg != w->reg);
1100
1101 if (w->invert)
1102 power = !w->power;
1103 else
1104 power = w->power;
1105
1106 mask |= cur_mask;
1107 if (power)
1108 value |= cur_mask;
1109
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001110 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001111 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1112 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001113
Mark Brown68f89ad2010-11-03 23:51:49 -04001114 /* Check for events */
1115 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1116 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001117 }
1118
Mark Brown81628102009-06-07 13:21:24 +01001119 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001120 /* Any widget will do, they should all be updating the
1121 * same register.
1122 */
1123 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1124 power_list);
1125
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001126 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001127 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001128 value, mask, reg, card->pop_time);
1129 pop_wait(card->pop_time);
Liam Girdwood49575fb52012-03-06 18:16:19 +00001130 soc_widget_update_bits_locked(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001131 }
1132
1133 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001134 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1135 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001136 }
Mark Brown42aa3412009-03-01 19:21:10 +00001137}
1138
Mark Brownb22ead22009-06-07 12:51:26 +01001139/* Apply a DAPM power sequence.
1140 *
1141 * We walk over a pre-sorted list of widgets to apply power to. In
1142 * order to minimise the number of writes to the device required
1143 * multiple widgets will be updated in a single write where possible.
1144 * Currently anything that requires more than a single write is not
1145 * handled.
1146 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001147static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001148 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001149{
1150 struct snd_soc_dapm_widget *w, *n;
1151 LIST_HEAD(pending);
1152 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001153 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001154 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001155 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001156 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001157 int *sort;
1158
1159 if (power_up)
1160 sort = dapm_up_seq;
1161 else
1162 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001163
Mark Brownb22ead22009-06-07 12:51:26 +01001164 list_for_each_entry_safe(w, n, list, power_list) {
1165 ret = 0;
1166
1167 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001168 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001169 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001170 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001171 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001172
Mark Brown474b62d2011-01-18 16:14:44 +00001173 if (cur_dapm && cur_dapm->seq_notifier) {
1174 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1175 if (sort[i] == cur_sort)
1176 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001177 i,
1178 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001179 }
1180
Mark Brownb22ead22009-06-07 12:51:26 +01001181 INIT_LIST_HEAD(&pending);
1182 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001183 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001184 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001185 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001186 }
1187
Mark Brown163cac02009-06-07 10:12:52 +01001188 switch (w->id) {
1189 case snd_soc_dapm_pre:
1190 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001191 list_for_each_entry_safe_continue(w, n, list,
1192 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001193
Mark Brownb22ead22009-06-07 12:51:26 +01001194 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001195 ret = w->event(w,
1196 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001197 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001198 ret = w->event(w,
1199 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001200 break;
1201
1202 case snd_soc_dapm_post:
1203 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001204 list_for_each_entry_safe_continue(w, n, list,
1205 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001206
Mark Brownb22ead22009-06-07 12:51:26 +01001207 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001208 ret = w->event(w,
1209 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001210 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001211 ret = w->event(w,
1212 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001213 break;
1214
Mark Brown163cac02009-06-07 10:12:52 +01001215 default:
Mark Brown81628102009-06-07 13:21:24 +01001216 /* Queue it up for application */
1217 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001218 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001219 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001220 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001221 list_move(&w->power_list, &pending);
1222 break;
Mark Brown163cac02009-06-07 10:12:52 +01001223 }
Mark Brownb22ead22009-06-07 12:51:26 +01001224
1225 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001226 dev_err(w->dapm->dev,
1227 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001228 }
Mark Brownb22ead22009-06-07 12:51:26 +01001229
1230 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001231 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001232
1233 if (cur_dapm && cur_dapm->seq_notifier) {
1234 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1235 if (sort[i] == cur_sort)
1236 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001237 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001238 }
Mark Brown163cac02009-06-07 10:12:52 +01001239}
1240
Mark Brown97404f22010-12-14 16:13:57 +00001241static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1242{
1243 struct snd_soc_dapm_update *update = dapm->update;
1244 struct snd_soc_dapm_widget *w;
1245 int ret;
1246
1247 if (!update)
1248 return;
1249
1250 w = update->widget;
1251
1252 if (w->event &&
1253 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1254 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1255 if (ret != 0)
1256 pr_err("%s DAPM pre-event failed: %d\n",
1257 w->name, ret);
1258 }
1259
Liam Girdwood49575fb52012-03-06 18:16:19 +00001260 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
Mark Brown97404f22010-12-14 16:13:57 +00001261 update->val);
1262 if (ret < 0)
1263 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1264
1265 if (w->event &&
1266 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1267 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1268 if (ret != 0)
1269 pr_err("%s DAPM post-event failed: %d\n",
1270 w->name, ret);
1271 }
1272}
1273
Mark Brown9d0624a2011-02-18 11:49:43 -08001274/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1275 * they're changing state.
1276 */
1277static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1278{
1279 struct snd_soc_dapm_context *d = data;
1280 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001281
Mark Brown56fba412011-06-04 11:25:10 +01001282 /* If we're off and we're not supposed to be go into STANDBY */
1283 if (d->bias_level == SND_SOC_BIAS_OFF &&
1284 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001285 if (d->dev)
1286 pm_runtime_get_sync(d->dev);
1287
Mark Brown9d0624a2011-02-18 11:49:43 -08001288 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1289 if (ret != 0)
1290 dev_err(d->dev,
1291 "Failed to turn on bias: %d\n", ret);
1292 }
1293
Mark Brown56fba412011-06-04 11:25:10 +01001294 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1295 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001296 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1297 if (ret != 0)
1298 dev_err(d->dev,
1299 "Failed to prepare bias: %d\n", ret);
1300 }
1301}
1302
1303/* Async callback run prior to DAPM sequences - brings to their final
1304 * state.
1305 */
1306static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1307{
1308 struct snd_soc_dapm_context *d = data;
1309 int ret;
1310
1311 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001312 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1313 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1314 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001315 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1316 if (ret != 0)
1317 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1318 ret);
1319 }
1320
1321 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001322 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1323 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001324 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1325 if (ret != 0)
1326 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001327
1328 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001329 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001330 }
1331
1332 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001333 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1334 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001335 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1336 if (ret != 0)
1337 dev_err(d->dev, "Failed to apply active bias: %d\n",
1338 ret);
1339 }
1340}
Mark Brown97404f22010-12-14 16:13:57 +00001341
Mark Brownfe4fda52011-10-03 22:36:57 +01001342static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1343 bool power, bool connect)
1344{
1345 /* If a connection is being made or broken then that update
1346 * will have marked the peer dirty, otherwise the widgets are
1347 * not connected and this update has no impact. */
1348 if (!connect)
1349 return;
1350
1351 /* If the peer is already in the state we're moving to then we
1352 * won't have an impact on it. */
1353 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001354 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001355}
1356
Mark Brown05623c42011-09-28 17:02:31 +01001357static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1358 struct list_head *up_list,
1359 struct list_head *down_list)
1360{
Mark Browndb432b42011-10-03 21:06:40 +01001361 struct snd_soc_dapm_path *path;
1362
Mark Brown05623c42011-09-28 17:02:31 +01001363 if (w->power == power)
1364 return;
1365
1366 trace_snd_soc_dapm_widget_power(w, power);
1367
Mark Browndb432b42011-10-03 21:06:40 +01001368 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001369 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001370 */
1371 list_for_each_entry(path, &w->sources, list_sink) {
1372 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001373 dapm_widget_set_peer_power(path->source, power,
1374 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001375 }
1376 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001377 switch (w->id) {
1378 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001379 case snd_soc_dapm_regulator_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001380 /* Supplies can't affect their outputs, only their inputs */
1381 break;
1382 default:
1383 list_for_each_entry(path, &w->sinks, list_source) {
1384 if (path->sink) {
1385 dapm_widget_set_peer_power(path->sink, power,
1386 path->connect);
1387 }
Mark Browndb432b42011-10-03 21:06:40 +01001388 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001389 break;
Mark Browndb432b42011-10-03 21:06:40 +01001390 }
1391
Mark Brown05623c42011-09-28 17:02:31 +01001392 if (power)
1393 dapm_seq_insert(w, up_list, true);
1394 else
1395 dapm_seq_insert(w, down_list, false);
1396
1397 w->power = power;
1398}
1399
Mark Brown7c81beb2011-09-20 22:22:32 +01001400static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1401 struct list_head *up_list,
1402 struct list_head *down_list)
1403{
Mark Brown7c81beb2011-09-20 22:22:32 +01001404 int power;
1405
1406 switch (w->id) {
1407 case snd_soc_dapm_pre:
1408 dapm_seq_insert(w, down_list, false);
1409 break;
1410 case snd_soc_dapm_post:
1411 dapm_seq_insert(w, up_list, true);
1412 break;
1413
1414 default:
Mark Brownd8050022011-09-28 18:28:23 +01001415 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001416
Mark Brown05623c42011-09-28 17:02:31 +01001417 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001418 break;
1419 }
1420}
1421
Mark Brown42aa3412009-03-01 19:21:10 +00001422/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001423 * Scan each dapm widget for complete audio path.
1424 * A complete path is a route that has valid endpoints i.e.:-
1425 *
1426 * o DAC to output pin.
1427 * o Input Pin to ADC.
1428 * o Input pin to Output pin (bypass, sidetone)
1429 * o DAC to ADC (loopback).
1430 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001431static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001432{
Mark Brown12ea2c72011-03-02 18:17:32 +00001433 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001434 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001435 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001436 LIST_HEAD(up_list);
1437 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001438 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001439 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001440
Mark Brown84e90932010-11-04 00:07:02 -04001441 trace_snd_soc_dapm_start(card);
1442
Mark Brown56fba412011-06-04 11:25:10 +01001443 list_for_each_entry(d, &card->dapm_list, list) {
1444 if (d->n_widgets || d->codec == NULL) {
1445 if (d->idle_bias_off)
1446 d->target_bias_level = SND_SOC_BIAS_OFF;
1447 else
1448 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1449 }
1450 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001451
Liam Girdwood6c120e12012-02-15 15:15:34 +00001452 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001453
Mark Brown6d3ddc82009-05-16 17:47:29 +01001454 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001455 * lists indicating if they should be powered up or down. We
1456 * only check widgets that have been flagged as dirty but note
1457 * that new widgets may be added to the dirty list while we
1458 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001459 */
Mark Browndb432b42011-10-03 21:06:40 +01001460 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001461 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001462 }
1463
Mark Brownf9de6d72011-09-28 17:19:47 +01001464 list_for_each_entry(w, &card->widgets, list) {
Mark Browndb432b42011-10-03 21:06:40 +01001465 list_del_init(&w->dirty);
1466
Mark Brownf9de6d72011-09-28 17:19:47 +01001467 if (w->power) {
1468 d = w->dapm;
1469
1470 /* Supplies and micbiases only bring the
1471 * context up to STANDBY as unless something
1472 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001473 * generally don't require full power. Signal
1474 * generators are virtual pins and have no
1475 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001476 */
1477 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001478 case snd_soc_dapm_siggen:
1479 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001480 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001481 case snd_soc_dapm_regulator_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001482 case snd_soc_dapm_micbias:
1483 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1484 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1485 break;
1486 default:
1487 d->target_bias_level = SND_SOC_BIAS_ON;
1488 break;
1489 }
1490 }
1491
1492 }
1493
Mark Brownb14b76a2009-08-17 11:55:38 +01001494 /* If there are no DAPM widgets then try to figure out power from the
1495 * event type.
1496 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001497 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001498 switch (event) {
1499 case SND_SOC_DAPM_STREAM_START:
1500 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001501 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a2009-08-17 11:55:38 +01001502 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001503 case SND_SOC_DAPM_STREAM_STOP:
Liam Girdwoode7c80e22012-01-16 15:23:31 +00001504 if (dapm->codec && dapm->codec->active)
Mark Brown56fba412011-06-04 11:25:10 +01001505 dapm->target_bias_level = SND_SOC_BIAS_ON;
1506 else
1507 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001508 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001509 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001510 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001511 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001512 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001513 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001514 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001515 default:
1516 break;
1517 }
1518 }
1519
Mark Brown85a843c2011-09-21 21:29:47 +01001520 /* Force all contexts in the card to the same bias state if
1521 * they're not ground referenced.
1522 */
Mark Brown56fba412011-06-04 11:25:10 +01001523 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001524 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001525 if (d->target_bias_level > bias)
1526 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001527 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001528 if (!d->idle_bias_off)
1529 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001530
Mark Brownde02d072011-09-20 21:43:24 +01001531 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001532
Mark Brown9d0624a2011-02-18 11:49:43 -08001533 /* Run all the bias changes in parallel */
1534 list_for_each_entry(d, &dapm->card->dapm_list, list)
1535 async_schedule_domain(dapm_pre_sequence_async, d,
1536 &async_domain);
1537 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001538
Mark Brown6d3ddc82009-05-16 17:47:29 +01001539 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001540 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001541
Mark Brown97404f22010-12-14 16:13:57 +00001542 dapm_widget_update(dapm);
1543
Mark Brown6d3ddc82009-05-16 17:47:29 +01001544 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001545 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001546
Mark Brown9d0624a2011-02-18 11:49:43 -08001547 /* Run all the bias changes in parallel */
1548 list_for_each_entry(d, &dapm->card->dapm_list, list)
1549 async_schedule_domain(dapm_post_sequence_async, d,
1550 &async_domain);
1551 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001552
Liam Girdwood8078d872012-02-15 15:15:35 +00001553 /* do we need to notify any clients that DAPM event is complete */
1554 list_for_each_entry(d, &card->dapm_list, list) {
1555 if (d->stream_event)
1556 d->stream_event(d, event);
1557 }
1558
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001559 pop_dbg(dapm->dev, card->pop_time,
1560 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001561 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001562
Mark Brown84e90932010-11-04 00:07:02 -04001563 trace_snd_soc_dapm_done(card);
1564
Mark Brown42aa3412009-03-01 19:21:10 +00001565 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001566}
1567
Mark Brown79fb9382009-08-21 16:38:13 +01001568#ifdef CONFIG_DEBUG_FS
1569static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1570{
1571 file->private_data = inode->i_private;
1572 return 0;
1573}
1574
1575static ssize_t dapm_widget_power_read_file(struct file *file,
1576 char __user *user_buf,
1577 size_t count, loff_t *ppos)
1578{
1579 struct snd_soc_dapm_widget *w = file->private_data;
1580 char *buf;
1581 int in, out;
1582 ssize_t ret;
1583 struct snd_soc_dapm_path *p = NULL;
1584
1585 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1586 if (!buf)
1587 return -ENOMEM;
1588
1589 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001590 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001591 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001592 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001593
Mark Brownf13ebad2012-03-03 18:01:01 +00001594 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1595 w->name, w->power ? "On" : "Off",
1596 w->force ? " (forced)" : "", in, out);
Mark Brown79fb9382009-08-21 16:38:13 +01001597
Mark Brownd033c362009-12-04 15:25:56 +00001598 if (w->reg >= 0)
1599 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1600 " - R%d(0x%x) bit %d",
1601 w->reg, w->reg, w->shift);
1602
1603 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1604
Mark Brown3eef08b2009-09-14 16:49:00 +01001605 if (w->sname)
1606 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1607 w->sname,
1608 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001609
1610 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001611 if (p->connected && !p->connected(w, p->sink))
1612 continue;
1613
Mark Brown79fb9382009-08-21 16:38:13 +01001614 if (p->connect)
1615 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001616 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001617 p->name ? p->name : "static",
1618 p->source->name);
1619 }
1620 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001621 if (p->connected && !p->connected(w, p->sink))
1622 continue;
1623
Mark Brown79fb9382009-08-21 16:38:13 +01001624 if (p->connect)
1625 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001626 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001627 p->name ? p->name : "static",
1628 p->sink->name);
1629 }
1630
1631 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1632
1633 kfree(buf);
1634 return ret;
1635}
1636
1637static const struct file_operations dapm_widget_power_fops = {
1638 .open = dapm_widget_power_open_file,
1639 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001640 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001641};
1642
Mark Brownef49e4f2011-04-04 20:48:13 +09001643static int dapm_bias_open_file(struct inode *inode, struct file *file)
1644{
1645 file->private_data = inode->i_private;
1646 return 0;
1647}
1648
1649static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1650 size_t count, loff_t *ppos)
1651{
1652 struct snd_soc_dapm_context *dapm = file->private_data;
1653 char *level;
1654
1655 switch (dapm->bias_level) {
1656 case SND_SOC_BIAS_ON:
1657 level = "On\n";
1658 break;
1659 case SND_SOC_BIAS_PREPARE:
1660 level = "Prepare\n";
1661 break;
1662 case SND_SOC_BIAS_STANDBY:
1663 level = "Standby\n";
1664 break;
1665 case SND_SOC_BIAS_OFF:
1666 level = "Off\n";
1667 break;
1668 default:
1669 BUG();
1670 level = "Unknown\n";
1671 break;
1672 }
1673
1674 return simple_read_from_buffer(user_buf, count, ppos, level,
1675 strlen(level));
1676}
1677
1678static const struct file_operations dapm_bias_fops = {
1679 .open = dapm_bias_open_file,
1680 .read = dapm_bias_read_file,
1681 .llseek = default_llseek,
1682};
1683
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001684void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1685 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001686{
Mark Brown79fb9382009-08-21 16:38:13 +01001687 struct dentry *d;
1688
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001689 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1690
1691 if (!dapm->debugfs_dapm) {
Liam Girdwoodf1e90af2012-03-06 18:13:25 +00001692 dev_warn(dapm->dev,
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001693 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001694 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001695 }
Mark Brown79fb9382009-08-21 16:38:13 +01001696
Mark Brownef49e4f2011-04-04 20:48:13 +09001697 d = debugfs_create_file("bias_level", 0444,
1698 dapm->debugfs_dapm, dapm,
1699 &dapm_bias_fops);
1700 if (!d)
1701 dev_warn(dapm->dev,
1702 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001703}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001704
1705static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1706{
1707 struct snd_soc_dapm_context *dapm = w->dapm;
1708 struct dentry *d;
1709
1710 if (!dapm->debugfs_dapm || !w->name)
1711 return;
1712
1713 d = debugfs_create_file(w->name, 0444,
1714 dapm->debugfs_dapm, w,
1715 &dapm_widget_power_fops);
1716 if (!d)
1717 dev_warn(w->dapm->dev,
1718 "ASoC: Failed to create %s debugfs file\n",
1719 w->name);
1720}
1721
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001722static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1723{
1724 debugfs_remove_recursive(dapm->debugfs_dapm);
1725}
1726
Mark Brown79fb9382009-08-21 16:38:13 +01001727#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001728void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1729 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001730{
1731}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001732
1733static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1734{
1735}
1736
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001737static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1738{
1739}
1740
Mark Brown79fb9382009-08-21 16:38:13 +01001741#endif
1742
Richard Purdie2b97eab2006-10-06 18:32:18 +02001743/* test and update the power status of a mux widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001744static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001745 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001746{
1747 struct snd_soc_dapm_path *path;
1748 int found = 0;
1749
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001750 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001751 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001752 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001753 return -ENODEV;
1754
Richard Purdie2b97eab2006-10-06 18:32:18 +02001755 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001756 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001757 if (path->kcontrol != kcontrol)
1758 continue;
1759
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001760 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001761 continue;
1762
1763 found = 1;
1764 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001765 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001766 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001767 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001768 } else {
1769 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001770 dapm_mark_dirty(path->source,
1771 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001772 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001773 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001774 }
1775
Mark Browndb432b42011-10-03 21:06:40 +01001776 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001777 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001778 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001779 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001780
1781 return 0;
1782}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001783
1784int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1785 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1786{
1787 struct snd_soc_card *card = widget->dapm->card;
1788 int ret;
1789
Liam Girdwood3cd04342012-03-09 12:02:08 +00001790 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001791 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1792 mutex_unlock(&card->dapm_mutex);
1793 return ret;
1794}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001795EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001796
Milan plzik1b075e32008-01-10 14:39:46 +01001797/* test and update the power status of a mixer or switch widget */
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001798static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001799 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001800{
1801 struct snd_soc_dapm_path *path;
1802 int found = 0;
1803
Milan plzik1b075e32008-01-10 14:39:46 +01001804 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001805 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001806 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001807 return -ENODEV;
1808
Richard Purdie2b97eab2006-10-06 18:32:18 +02001809 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001810 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001811 if (path->kcontrol != kcontrol)
1812 continue;
1813
1814 /* found, now check type */
1815 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001816 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001817 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001818 }
1819
Mark Browndb432b42011-10-03 21:06:40 +01001820 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001821 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001822 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001823 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001824
1825 return 0;
1826}
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001827
1828int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1829 struct snd_kcontrol *kcontrol, int connect)
1830{
1831 struct snd_soc_card *card = widget->dapm->card;
1832 int ret;
1833
Liam Girdwood3cd04342012-03-09 12:02:08 +00001834 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwood4edbb3452012-03-07 10:38:27 +00001835 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
1836 mutex_unlock(&card->dapm_mutex);
1837 return ret;
1838}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001839EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001840
1841/* show dapm widget status in sys fs */
1842static ssize_t dapm_widget_show(struct device *dev,
1843 struct device_attribute *attr, char *buf)
1844{
Mark Brown36ae1a92012-01-06 17:12:45 -08001845 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001846 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001847 struct snd_soc_dapm_widget *w;
1848 int count = 0;
1849 char *state = "not set";
1850
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001851 list_for_each_entry(w, &codec->card->widgets, list) {
1852 if (w->dapm != &codec->dapm)
1853 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001854
1855 /* only display widgets that burnm power */
1856 switch (w->id) {
1857 case snd_soc_dapm_hp:
1858 case snd_soc_dapm_mic:
1859 case snd_soc_dapm_spk:
1860 case snd_soc_dapm_line:
1861 case snd_soc_dapm_micbias:
1862 case snd_soc_dapm_dac:
1863 case snd_soc_dapm_adc:
1864 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001865 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001866 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001867 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001868 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001869 case snd_soc_dapm_regulator_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001870 if (w->name)
1871 count += sprintf(buf + count, "%s: %s\n",
1872 w->name, w->power ? "On":"Off");
1873 break;
1874 default:
1875 break;
1876 }
1877 }
1878
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001879 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001880 case SND_SOC_BIAS_ON:
1881 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001882 break;
Mark Brown0be98982008-05-19 12:31:28 +02001883 case SND_SOC_BIAS_PREPARE:
1884 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001885 break;
Mark Brown0be98982008-05-19 12:31:28 +02001886 case SND_SOC_BIAS_STANDBY:
1887 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001888 break;
Mark Brown0be98982008-05-19 12:31:28 +02001889 case SND_SOC_BIAS_OFF:
1890 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001891 break;
1892 }
1893 count += sprintf(buf + count, "PM State: %s\n", state);
1894
1895 return count;
1896}
1897
1898static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1899
1900int snd_soc_dapm_sys_add(struct device *dev)
1901{
Troy Kisky12ef1932008-10-13 17:42:14 -07001902 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001903}
1904
1905static void snd_soc_dapm_sys_remove(struct device *dev)
1906{
Mark Brownaef90842009-05-16 17:53:16 +01001907 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001908}
1909
1910/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001911static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001912{
1913 struct snd_soc_dapm_widget *w, *next_w;
1914 struct snd_soc_dapm_path *p, *next_p;
1915
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001916 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1917 if (w->dapm != dapm)
1918 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001919 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001920 /*
1921 * remove source and sink paths associated to this widget.
1922 * While removing the path, remove reference to it from both
1923 * source and sink widgets so that path is removed only once.
1924 */
1925 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1926 list_del(&p->list_sink);
1927 list_del(&p->list_source);
1928 list_del(&p->list);
1929 kfree(p->long_name);
1930 kfree(p);
1931 }
1932 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1933 list_del(&p->list_sink);
1934 list_del(&p->list_source);
1935 list_del(&p->list);
1936 kfree(p->long_name);
1937 kfree(p);
1938 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001939 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001940 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001941 kfree(w);
1942 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001943}
1944
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001945static struct snd_soc_dapm_widget *dapm_find_widget(
1946 struct snd_soc_dapm_context *dapm, const char *pin,
1947 bool search_other_contexts)
1948{
1949 struct snd_soc_dapm_widget *w;
1950 struct snd_soc_dapm_widget *fallback = NULL;
1951
1952 list_for_each_entry(w, &dapm->card->widgets, list) {
1953 if (!strcmp(w->name, pin)) {
1954 if (w->dapm == dapm)
1955 return w;
1956 else
1957 fallback = w;
1958 }
1959 }
1960
1961 if (search_other_contexts)
1962 return fallback;
1963
1964 return NULL;
1965}
1966
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001967static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001968 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001969{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001970 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001971
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001972 if (!w) {
1973 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1974 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001975 }
1976
Mark Brown1a8b2d92012-02-16 11:50:07 -08001977 if (w->connected != status)
1978 dapm_mark_dirty(w, "pin configuration");
1979
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001980 w->connected = status;
1981 if (status == 0)
1982 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001983
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001984 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001985}
1986
Richard Purdie2b97eab2006-10-06 18:32:18 +02001987/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001988 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001989 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001990 *
1991 * Walks all dapm audio paths and powers widgets according to their
1992 * stream or path usage.
1993 *
1994 * Returns 0 for success.
1995 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001996int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001997{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00001998 int ret;
1999
Mark Brown4f4c0072011-10-07 14:29:19 +01002000 /*
2001 * Suppress early reports (eg, jacks syncing their state) to avoid
2002 * silly DAPM runs during card startup.
2003 */
2004 if (!dapm->card || !dapm->card->instantiated)
2005 return 0;
2006
Liam Girdwood3cd04342012-03-09 12:02:08 +00002007 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002008 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2009 mutex_unlock(&dapm->card->dapm_mutex);
2010 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002011}
Liam Girdwooda5302182008-07-07 13:35:17 +01002012EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002013
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002014static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01002015 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002016{
2017 struct snd_soc_dapm_path *path;
2018 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002019 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002020 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01002021 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002022 const char *source;
2023 char prefixed_sink[80];
2024 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002025 int ret = 0;
2026
Mark Brown88e8b9a2011-03-02 18:18:24 +00002027 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002028 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2029 dapm->codec->name_prefix, route->sink);
2030 sink = prefixed_sink;
2031 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2032 dapm->codec->name_prefix, route->source);
2033 source = prefixed_source;
2034 } else {
2035 sink = route->sink;
2036 source = route->source;
2037 }
2038
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002039 /*
2040 * find src and dest widgets over all widgets but favor a widget from
2041 * current DAPM context
2042 */
2043 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002044 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002045 wtsink = w;
2046 if (w->dapm == dapm)
2047 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002048 continue;
2049 }
2050 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002051 wtsource = w;
2052 if (w->dapm == dapm)
2053 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002054 }
2055 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002056 /* use widget from another DAPM context if not found from this */
2057 if (!wsink)
2058 wsink = wtsink;
2059 if (!wsource)
2060 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002061
2062 if (wsource == NULL || wsink == NULL)
2063 return -ENODEV;
2064
2065 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2066 if (!path)
2067 return -ENOMEM;
2068
2069 path->source = wsource;
2070 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002071 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002072 INIT_LIST_HEAD(&path->list);
2073 INIT_LIST_HEAD(&path->list_source);
2074 INIT_LIST_HEAD(&path->list_sink);
2075
2076 /* check for external widgets */
2077 if (wsink->id == snd_soc_dapm_input) {
2078 if (wsource->id == snd_soc_dapm_micbias ||
2079 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002080 wsource->id == snd_soc_dapm_line ||
2081 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002082 wsink->ext = 1;
2083 }
2084 if (wsource->id == snd_soc_dapm_output) {
2085 if (wsink->id == snd_soc_dapm_spk ||
2086 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002087 wsink->id == snd_soc_dapm_line ||
2088 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002089 wsource->ext = 1;
2090 }
2091
2092 /* connect static paths */
2093 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002094 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002095 list_add(&path->list_sink, &wsink->sources);
2096 list_add(&path->list_source, &wsource->sinks);
2097 path->connect = 1;
2098 return 0;
2099 }
2100
2101 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002102 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002103 case snd_soc_dapm_adc:
2104 case snd_soc_dapm_dac:
2105 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002106 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002107 case snd_soc_dapm_input:
2108 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002109 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002110 case snd_soc_dapm_micbias:
2111 case snd_soc_dapm_vmid:
2112 case snd_soc_dapm_pre:
2113 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002114 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002115 case snd_soc_dapm_regulator_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002116 case snd_soc_dapm_aif_in:
2117 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -08002118 case snd_soc_dapm_dai:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002119 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002120 list_add(&path->list_sink, &wsink->sources);
2121 list_add(&path->list_source, &wsource->sinks);
2122 path->connect = 1;
2123 return 0;
2124 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002125 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002126 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002127 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002128 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002129 if (ret != 0)
2130 goto err;
2131 break;
2132 case snd_soc_dapm_switch:
2133 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002134 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002135 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002136 if (ret != 0)
2137 goto err;
2138 break;
2139 case snd_soc_dapm_hp:
2140 case snd_soc_dapm_mic:
2141 case snd_soc_dapm_line:
2142 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002143 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002144 list_add(&path->list_sink, &wsink->sources);
2145 list_add(&path->list_source, &wsource->sinks);
2146 path->connect = 0;
2147 return 0;
2148 }
2149 return 0;
2150
2151err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002152 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2153 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002154 kfree(path);
2155 return ret;
2156}
Mark Brown105f1c22008-05-13 14:52:19 +02002157
2158/**
Mark Brown105f1c22008-05-13 14:52:19 +02002159 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002160 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002161 * @route: audio routes
2162 * @num: number of routes
2163 *
2164 * Connects 2 dapm widgets together via a named audio path. The sink is
2165 * the widget receiving the audio signal, whilst the source is the sender
2166 * of the audio signal.
2167 *
2168 * Returns 0 for success else error. On error all resources can be freed
2169 * with a call to snd_soc_card_free().
2170 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002171int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002172 const struct snd_soc_dapm_route *route, int num)
2173{
2174 int i, ret;
2175
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002176 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown105f1c22008-05-13 14:52:19 +02002177 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002178 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02002179 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002180 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2181 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02002182 return ret;
2183 }
2184 route++;
2185 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002186 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown105f1c22008-05-13 14:52:19 +02002187
2188 return 0;
2189}
2190EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2191
Mark Brownbf3a9e12011-06-13 16:42:29 +01002192static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2193 const struct snd_soc_dapm_route *route)
2194{
2195 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2196 route->source,
2197 true);
2198 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2199 route->sink,
2200 true);
2201 struct snd_soc_dapm_path *path;
2202 int count = 0;
2203
2204 if (!source) {
2205 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2206 route->source);
2207 return -ENODEV;
2208 }
2209
2210 if (!sink) {
2211 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2212 route->sink);
2213 return -ENODEV;
2214 }
2215
2216 if (route->control || route->connected)
2217 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2218 route->source, route->sink);
2219
2220 list_for_each_entry(path, &source->sinks, list_source) {
2221 if (path->sink == sink) {
2222 path->weak = 1;
2223 count++;
2224 }
2225 }
2226
2227 if (count == 0)
2228 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2229 route->source, route->sink);
2230 if (count > 1)
2231 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2232 count, route->source, route->sink);
2233
2234 return 0;
2235}
2236
2237/**
2238 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2239 * @dapm: DAPM context
2240 * @route: audio routes
2241 * @num: number of routes
2242 *
2243 * Mark existing routes matching those specified in the passed array
2244 * as being weak, meaning that they are ignored for the purpose of
2245 * power decisions. The main intended use case is for sidetone paths
2246 * which couple audio between other independent paths if they are both
2247 * active in order to make the combination work better at the user
2248 * level but which aren't intended to be "used".
2249 *
2250 * Note that CODEC drivers should not use this as sidetone type paths
2251 * can frequently also be used as bypass paths.
2252 */
2253int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2254 const struct snd_soc_dapm_route *route, int num)
2255{
2256 int i, err;
2257 int ret = 0;
2258
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002259 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002260 for (i = 0; i < num; i++) {
2261 err = snd_soc_dapm_weak_route(dapm, route);
2262 if (err)
2263 ret = err;
2264 route++;
2265 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002266 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brownbf3a9e12011-06-13 16:42:29 +01002267
2268 return ret;
2269}
2270EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2271
Mark Brown105f1c22008-05-13 14:52:19 +02002272/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002273 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002274 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002275 *
2276 * Checks the codec for any new dapm widgets and creates them if found.
2277 *
2278 * Returns 0 for success.
2279 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002280int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002281{
2282 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002283 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002284
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002285 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2286
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002287 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002288 {
2289 if (w->new)
2290 continue;
2291
Stephen Warrenfad59882011-04-28 17:37:59 -06002292 if (w->num_kcontrols) {
2293 w->kcontrols = kzalloc(w->num_kcontrols *
2294 sizeof(struct snd_kcontrol *),
2295 GFP_KERNEL);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002296 if (!w->kcontrols) {
2297 mutex_unlock(&dapm->card->dapm_mutex);
Stephen Warrenfad59882011-04-28 17:37:59 -06002298 return -ENOMEM;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002299 }
Stephen Warrenfad59882011-04-28 17:37:59 -06002300 }
2301
Richard Purdie2b97eab2006-10-06 18:32:18 +02002302 switch(w->id) {
2303 case snd_soc_dapm_switch:
2304 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002305 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002306 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002307 break;
2308 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002309 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002310 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002311 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002312 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002313 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002314 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002315 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002316 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002317 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002318 break;
2319 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002320
2321 /* Read the initial power state from the device */
2322 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002323 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002324 val &= 1 << w->shift;
2325 if (w->invert)
2326 val = !val;
2327
2328 if (val)
2329 w->power = 1;
2330 }
2331
Richard Purdie2b97eab2006-10-06 18:32:18 +02002332 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002333
Mark Brown7508b122011-10-05 12:09:12 +01002334 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002335 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002336 }
2337
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002338 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002339 mutex_unlock(&dapm->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002340 return 0;
2341}
2342EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2343
2344/**
2345 * snd_soc_dapm_get_volsw - dapm mixer get callback
2346 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002347 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002348 *
2349 * Callback to get the value of a dapm mixer control.
2350 *
2351 * Returns 0 for success.
2352 */
2353int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2354 struct snd_ctl_elem_value *ucontrol)
2355{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002356 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2357 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002358 struct soc_mixer_control *mc =
2359 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002360 unsigned int reg = mc->reg;
2361 unsigned int shift = mc->shift;
2362 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002363 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002364 unsigned int invert = mc->invert;
2365 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002366
Richard Purdie2b97eab2006-10-06 18:32:18 +02002367 ucontrol->value.integer.value[0] =
2368 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2369 if (shift != rshift)
2370 ucontrol->value.integer.value[1] =
2371 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2372 if (invert) {
2373 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002374 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002375 if (shift != rshift)
2376 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002377 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002378 }
2379
2380 return 0;
2381}
2382EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2383
2384/**
2385 * snd_soc_dapm_put_volsw - dapm mixer set callback
2386 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002387 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002388 *
2389 * Callback to set the value of a dapm mixer control.
2390 *
2391 * Returns 0 for success.
2392 */
2393int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2394 struct snd_ctl_elem_value *ucontrol)
2395{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002396 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2397 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2398 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002399 struct snd_soc_card *card = codec->card;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002400 struct soc_mixer_control *mc =
2401 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002402 unsigned int reg = mc->reg;
2403 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002404 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002405 unsigned int mask = (1 << fls(max)) - 1;
2406 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002407 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002408 int connect, change;
2409 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002410 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002411
2412 val = (ucontrol->value.integer.value[0] & mask);
2413
2414 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002415 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002416 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002417 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002418
Stephen Warrenfafd2172011-04-28 17:38:00 -06002419 if (val)
2420 /* new connection */
2421 connect = invert ? 0 : 1;
2422 else
2423 /* old connection must be powered down */
2424 connect = invert ? 1 : 0;
2425
Liam Girdwood3cd04342012-03-09 12:02:08 +00002426 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002427
Stephen Warrene9cf7042011-01-27 14:54:05 -07002428 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002429 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002430 for (wi = 0; wi < wlist->num_widgets; wi++) {
2431 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002432
Stephen Warrenfafd2172011-04-28 17:38:00 -06002433 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002434
Stephen Warrenfafd2172011-04-28 17:38:00 -06002435 update.kcontrol = kcontrol;
2436 update.widget = widget;
2437 update.reg = reg;
2438 update.mask = mask;
2439 update.val = val;
2440 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002441
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002442 soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002443
2444 widget->dapm->update = NULL;
2445 }
Mark Brown283375c2009-12-07 18:09:03 +00002446 }
2447
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002448 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002449 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002450}
2451EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2452
2453/**
2454 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2455 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002456 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002457 *
2458 * Callback to get the value of a dapm enumerated double mixer control.
2459 *
2460 * Returns 0 for success.
2461 */
2462int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2463 struct snd_ctl_elem_value *ucontrol)
2464{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002465 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2466 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002467 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002468 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002469
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002470 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002471 ;
2472 val = snd_soc_read(widget->codec, e->reg);
2473 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2474 if (e->shift_l != e->shift_r)
2475 ucontrol->value.enumerated.item[1] =
2476 (val >> e->shift_r) & (bitmask - 1);
2477
2478 return 0;
2479}
2480EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2481
2482/**
2483 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2484 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002485 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002486 *
2487 * Callback to set the value of a dapm enumerated double mixer control.
2488 *
2489 * Returns 0 for success.
2490 */
2491int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2492 struct snd_ctl_elem_value *ucontrol)
2493{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002494 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2495 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2496 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002497 struct snd_soc_card *card = codec->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002498 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002499 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002500 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002501 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002502 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002503
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002504 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002505 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002506 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002507 return -EINVAL;
2508 mux = ucontrol->value.enumerated.item[0];
2509 val = mux << e->shift_l;
2510 mask = (bitmask - 1) << e->shift_l;
2511 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002512 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002513 return -EINVAL;
2514 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2515 mask |= (bitmask - 1) << e->shift_r;
2516 }
2517
Liam Girdwood3cd04342012-03-09 12:02:08 +00002518 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002519
Mark Brown3a655772009-10-05 17:23:30 +01002520 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002521 if (change) {
2522 for (wi = 0; wi < wlist->num_widgets; wi++) {
2523 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002524
Stephen Warrenfafd2172011-04-28 17:38:00 -06002525 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002526
Stephen Warrenfafd2172011-04-28 17:38:00 -06002527 update.kcontrol = kcontrol;
2528 update.widget = widget;
2529 update.reg = e->reg;
2530 update.mask = mask;
2531 update.val = val;
2532 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002533
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002534 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002535
Stephen Warrenfafd2172011-04-28 17:38:00 -06002536 widget->dapm->update = NULL;
2537 }
2538 }
2539
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002540 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002541 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002542}
2543EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2544
2545/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002546 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2547 * @kcontrol: mixer control
2548 * @ucontrol: control element information
2549 *
2550 * Returns 0 for success.
2551 */
2552int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2553 struct snd_ctl_elem_value *ucontrol)
2554{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002555 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2556 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002557
2558 ucontrol->value.enumerated.item[0] = widget->value;
2559
2560 return 0;
2561}
2562EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2563
2564/**
2565 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2566 * @kcontrol: mixer control
2567 * @ucontrol: control element information
2568 *
2569 * Returns 0 for success.
2570 */
2571int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2572 struct snd_ctl_elem_value *ucontrol)
2573{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002574 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2575 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2576 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002577 struct snd_soc_card *card = codec->card;
Mark Brownd2b247a2009-10-06 15:21:04 +01002578 struct soc_enum *e =
2579 (struct soc_enum *)kcontrol->private_value;
2580 int change;
2581 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002582 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002583
2584 if (ucontrol->value.enumerated.item[0] >= e->max)
2585 return -EINVAL;
2586
Liam Girdwood3cd04342012-03-09 12:02:08 +00002587 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brownd2b247a2009-10-06 15:21:04 +01002588
2589 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002590 if (change) {
2591 for (wi = 0; wi < wlist->num_widgets; wi++) {
2592 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002593
Stephen Warrenfafd2172011-04-28 17:38:00 -06002594 widget->value = ucontrol->value.enumerated.item[0];
2595
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002596 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002597 }
2598 }
2599
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002600 mutex_unlock(&card->dapm_mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002601 return ret;
2602}
2603EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2604
2605/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002606 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2607 * callback
2608 * @kcontrol: mixer control
2609 * @ucontrol: control element information
2610 *
2611 * Callback to get the value of a dapm semi enumerated double mixer control.
2612 *
2613 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2614 * used for handling bitfield coded enumeration for example.
2615 *
2616 * Returns 0 for success.
2617 */
2618int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2619 struct snd_ctl_elem_value *ucontrol)
2620{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002621 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2622 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002623 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002624 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002625
2626 reg_val = snd_soc_read(widget->codec, e->reg);
2627 val = (reg_val >> e->shift_l) & e->mask;
2628 for (mux = 0; mux < e->max; mux++) {
2629 if (val == e->values[mux])
2630 break;
2631 }
2632 ucontrol->value.enumerated.item[0] = mux;
2633 if (e->shift_l != e->shift_r) {
2634 val = (reg_val >> e->shift_r) & e->mask;
2635 for (mux = 0; mux < e->max; mux++) {
2636 if (val == e->values[mux])
2637 break;
2638 }
2639 ucontrol->value.enumerated.item[1] = mux;
2640 }
2641
2642 return 0;
2643}
2644EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2645
2646/**
2647 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2648 * callback
2649 * @kcontrol: mixer control
2650 * @ucontrol: control element information
2651 *
2652 * Callback to set the value of a dapm semi enumerated double mixer control.
2653 *
2654 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2655 * used for handling bitfield coded enumeration for example.
2656 *
2657 * Returns 0 for success.
2658 */
2659int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_value *ucontrol)
2661{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002662 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2663 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2664 struct snd_soc_codec *codec = widget->codec;
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002665 struct snd_soc_card *card = codec->card;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002666 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002667 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002668 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002669 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002670 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002671
2672 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2673 return -EINVAL;
2674 mux = ucontrol->value.enumerated.item[0];
2675 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2676 mask = e->mask << e->shift_l;
2677 if (e->shift_l != e->shift_r) {
2678 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2679 return -EINVAL;
2680 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2681 mask |= e->mask << e->shift_r;
2682 }
2683
Liam Girdwood3cd04342012-03-09 12:02:08 +00002684 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002685
Mark Brown3a655772009-10-05 17:23:30 +01002686 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002687 if (change) {
2688 for (wi = 0; wi < wlist->num_widgets; wi++) {
2689 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002690
Stephen Warrenfafd2172011-04-28 17:38:00 -06002691 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002692
Stephen Warrenfafd2172011-04-28 17:38:00 -06002693 update.kcontrol = kcontrol;
2694 update.widget = widget;
2695 update.reg = e->reg;
2696 update.mask = mask;
2697 update.val = val;
2698 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002699
Liam Girdwood4edbb3452012-03-07 10:38:27 +00002700 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002701
Stephen Warrenfafd2172011-04-28 17:38:00 -06002702 widget->dapm->update = NULL;
2703 }
2704 }
2705
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002706 mutex_unlock(&card->dapm_mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002707 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002708}
2709EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2710
2711/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002712 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2713 *
2714 * @kcontrol: mixer control
2715 * @uinfo: control element information
2716 *
2717 * Callback to provide information about a pin switch control.
2718 */
2719int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2720 struct snd_ctl_elem_info *uinfo)
2721{
2722 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2723 uinfo->count = 1;
2724 uinfo->value.integer.min = 0;
2725 uinfo->value.integer.max = 1;
2726
2727 return 0;
2728}
2729EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2730
2731/**
2732 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2733 *
2734 * @kcontrol: mixer control
2735 * @ucontrol: Value
2736 */
2737int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2738 struct snd_ctl_elem_value *ucontrol)
2739{
Mark Brown48a8c392012-02-14 17:11:15 -08002740 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002741 const char *pin = (const char *)kcontrol->private_value;
2742
Liam Girdwood3cd04342012-03-09 12:02:08 +00002743 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002744
2745 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002746 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002747
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002748 mutex_unlock(&card->dapm_mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002749
2750 return 0;
2751}
2752EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2753
2754/**
2755 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2756 *
2757 * @kcontrol: mixer control
2758 * @ucontrol: Value
2759 */
2760int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2761 struct snd_ctl_elem_value *ucontrol)
2762{
Mark Brown48a8c392012-02-14 17:11:15 -08002763 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002764 const char *pin = (const char *)kcontrol->private_value;
2765
Liam Girdwood3cd04342012-03-09 12:02:08 +00002766 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002767
2768 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002769 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002770 else
Mark Brown48a8c392012-02-14 17:11:15 -08002771 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002772
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002773 mutex_unlock(&card->dapm_mutex);
2774
Mark Brown48a8c392012-02-14 17:11:15 -08002775 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002776 return 0;
2777}
2778EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2779
Mark Brown5ba06fc2012-02-16 11:07:13 -08002780static struct snd_soc_dapm_widget *
2781snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2782 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002783{
2784 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002785 size_t name_len;
Mark Brown62ea8742012-01-21 21:14:48 +00002786 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002787
2788 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08002789 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002790
Mark Brown62ea8742012-01-21 21:14:48 +00002791 switch (w->id) {
2792 case snd_soc_dapm_regulator_supply:
Liam Girdwooda3cc0562012-03-09 17:20:16 +00002793 w->regulator = devm_regulator_get(dapm->dev, w->name);
2794 if (IS_ERR(w->regulator)) {
2795 ret = PTR_ERR(w->regulator);
Mark Brown62ea8742012-01-21 21:14:48 +00002796 dev_err(dapm->dev, "Failed to request %s: %d\n",
2797 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002798 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00002799 }
2800 break;
2801 default:
2802 break;
2803 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002804
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002805 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002806 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002807 name_len += 1 + strlen(dapm->codec->name_prefix);
2808 w->name = kmalloc(name_len, GFP_KERNEL);
2809 if (w->name == NULL) {
2810 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002811 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002812 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002813 if (dapm->codec && dapm->codec->name_prefix)
Mark Brown888df392012-02-16 19:37:51 -08002814 snprintf((char *)w->name, name_len, "%s %s",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002815 dapm->codec->name_prefix, widget->name);
2816 else
Mark Brown888df392012-02-16 19:37:51 -08002817 snprintf((char *)w->name, name_len, "%s", widget->name);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002818
Mark Brown7ca3a182011-10-08 14:04:50 +01002819 switch (w->id) {
2820 case snd_soc_dapm_switch:
2821 case snd_soc_dapm_mixer:
2822 case snd_soc_dapm_mixer_named_ctl:
2823 w->power_check = dapm_generic_check_power;
2824 break;
2825 case snd_soc_dapm_mux:
2826 case snd_soc_dapm_virt_mux:
2827 case snd_soc_dapm_value_mux:
2828 w->power_check = dapm_generic_check_power;
2829 break;
2830 case snd_soc_dapm_adc:
2831 case snd_soc_dapm_aif_out:
2832 w->power_check = dapm_adc_check_power;
2833 break;
2834 case snd_soc_dapm_dac:
2835 case snd_soc_dapm_aif_in:
2836 w->power_check = dapm_dac_check_power;
2837 break;
2838 case snd_soc_dapm_pga:
2839 case snd_soc_dapm_out_drv:
2840 case snd_soc_dapm_input:
2841 case snd_soc_dapm_output:
2842 case snd_soc_dapm_micbias:
2843 case snd_soc_dapm_spk:
2844 case snd_soc_dapm_hp:
2845 case snd_soc_dapm_mic:
2846 case snd_soc_dapm_line:
2847 w->power_check = dapm_generic_check_power;
2848 break;
2849 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002850 case snd_soc_dapm_regulator_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01002851 w->power_check = dapm_supply_check_power;
2852 break;
Mark Brown888df392012-02-16 19:37:51 -08002853 case snd_soc_dapm_dai:
2854 w->power_check = dapm_dai_check_power;
2855 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002856 default:
2857 w->power_check = dapm_always_on_check_power;
2858 break;
2859 }
2860
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002861 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002862 w->dapm = dapm;
2863 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002864 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002865 INIT_LIST_HEAD(&w->sources);
2866 INIT_LIST_HEAD(&w->sinks);
2867 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01002868 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002869 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002870
2871 /* machine layer set ups unconnected pins and insertions */
2872 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08002873 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002874}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002875
2876/**
Mark Brown4ba13272008-05-13 14:51:19 +02002877 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002878 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002879 * @widget: widget array
2880 * @num: number of widgets
2881 *
2882 * Creates new DAPM controls based upon the templates.
2883 *
2884 * Returns 0 for success else error.
2885 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002886int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002887 const struct snd_soc_dapm_widget *widget,
2888 int num)
2889{
Mark Brown5ba06fc2012-02-16 11:07:13 -08002890 struct snd_soc_dapm_widget *w;
2891 int i;
Mark Brown4ba13272008-05-13 14:51:19 +02002892
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002893 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
Mark Brown4ba13272008-05-13 14:51:19 +02002894 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08002895 w = snd_soc_dapm_new_control(dapm, widget);
2896 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002897 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08002898 "ASoC: Failed to create DAPM control %s\n",
2899 widget->name);
2900 return -ENOMEM;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002901 }
Mark Brown4ba13272008-05-13 14:51:19 +02002902 widget++;
2903 }
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00002904 mutex_unlock(&dapm->card->dapm_mutex);
Mark Brown4ba13272008-05-13 14:51:19 +02002905 return 0;
2906}
2907EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2908
Mark Brown888df392012-02-16 19:37:51 -08002909int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
2910 struct snd_soc_dai *dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002911{
Mark Brown888df392012-02-16 19:37:51 -08002912 struct snd_soc_dapm_widget template;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002913 struct snd_soc_dapm_widget *w;
2914
Mark Brown888df392012-02-16 19:37:51 -08002915 WARN_ON(dapm->dev != dai->dev);
2916
2917 memset(&template, 0, sizeof(template));
2918 template.reg = SND_SOC_NOPM;
2919
2920 if (dai->driver->playback.stream_name) {
2921 template.id = snd_soc_dapm_dai;
2922 template.name = dai->driver->playback.stream_name;
2923 template.sname = dai->driver->playback.stream_name;
2924
2925 dev_dbg(dai->dev, "adding %s widget\n",
2926 template.name);
2927
2928 w = snd_soc_dapm_new_control(dapm, &template);
2929 if (!w) {
2930 dev_err(dapm->dev, "Failed to create %s widget\n",
2931 dai->driver->playback.stream_name);
2932 }
2933
2934 w->priv = dai;
2935 dai->playback_widget = w;
2936 }
2937
2938 if (dai->driver->capture.stream_name) {
2939 template.id = snd_soc_dapm_dai;
2940 template.name = dai->driver->capture.stream_name;
2941 template.sname = dai->driver->capture.stream_name;
2942
2943 dev_dbg(dai->dev, "adding %s widget\n",
2944 template.name);
2945
2946 w = snd_soc_dapm_new_control(dapm, &template);
2947 if (!w) {
2948 dev_err(dapm->dev, "Failed to create %s widget\n",
2949 dai->driver->capture.stream_name);
2950 }
2951
2952 w->priv = dai;
2953 dai->capture_widget = w;
2954 }
2955
2956 return 0;
2957}
2958
2959int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
2960{
2961 struct snd_soc_dapm_widget *dai_w, *w;
2962 struct snd_soc_dai *dai;
2963 struct snd_soc_dapm_route r;
2964
2965 memset(&r, 0, sizeof(r));
2966
2967 /* For each DAI widget... */
2968 list_for_each_entry(dai_w, &card->widgets, list) {
2969 if (dai_w->id != snd_soc_dapm_dai)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002970 continue;
Mark Brown888df392012-02-16 19:37:51 -08002971
2972 dai = dai_w->priv;
2973
2974 /* ...find all widgets with the same stream and link them */
2975 list_for_each_entry(w, &card->widgets, list) {
2976 if (w->dapm != dai_w->dapm)
2977 continue;
2978
2979 if (w->id == snd_soc_dapm_dai)
2980 continue;
2981
2982 if (!w->sname)
2983 continue;
2984
2985 if (dai->driver->playback.stream_name &&
2986 strstr(w->sname,
2987 dai->driver->playback.stream_name)) {
2988 r.source = dai->playback_widget->name;
2989 r.sink = w->name;
2990 dev_dbg(dai->dev, "%s -> %s\n",
2991 r.source, r.sink);
2992
2993 snd_soc_dapm_add_route(w->dapm, &r);
2994 }
2995
2996 if (dai->driver->capture.stream_name &&
2997 strstr(w->sname,
2998 dai->driver->capture.stream_name)) {
2999 r.source = w->name;
3000 r.sink = dai->capture_widget->name;
3001 dev_dbg(dai->dev, "%s -> %s\n",
3002 r.source, r.sink);
3003
3004 snd_soc_dapm_add_route(w->dapm, &r);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003005 }
3006 }
3007 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02003008
Mark Brown888df392012-02-16 19:37:51 -08003009 return 0;
3010}
Liam Girdwood64a648c2011-07-25 11:15:15 +01003011
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003012static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3013 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003014{
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003015
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003016 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3017 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3018 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08003019
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003020 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3021 w_cpu = cpu_dai->playback_widget;
3022 w_codec = codec_dai->playback_widget;
3023 } else {
3024 w_cpu = cpu_dai->capture_widget;
3025 w_codec = codec_dai->capture_widget;
Richard Purdie2b97eab2006-10-06 18:32:18 +02003026 }
3027
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003028 if (w_cpu) {
3029
3030 dapm_mark_dirty(w_cpu, "stream event");
3031
3032 switch (event) {
3033 case SND_SOC_DAPM_STREAM_START:
3034 w_cpu->active = 1;
3035 break;
3036 case SND_SOC_DAPM_STREAM_STOP:
3037 w_cpu->active = 0;
3038 break;
3039 case SND_SOC_DAPM_STREAM_SUSPEND:
3040 case SND_SOC_DAPM_STREAM_RESUME:
3041 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3042 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3043 break;
3044 }
3045 }
3046
3047 if (w_codec) {
3048
3049 dapm_mark_dirty(w_codec, "stream event");
3050
3051 switch (event) {
3052 case SND_SOC_DAPM_STREAM_START:
3053 w_codec->active = 1;
3054 break;
3055 case SND_SOC_DAPM_STREAM_STOP:
3056 w_codec->active = 0;
3057 break;
3058 case SND_SOC_DAPM_STREAM_SUSPEND:
3059 case SND_SOC_DAPM_STREAM_RESUME:
3060 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3061 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3062 break;
3063 }
3064 }
3065
3066 dapm_power_widgets(&rtd->card->dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003067}
3068
3069/**
3070 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3071 * @rtd: PCM runtime data
3072 * @stream: stream name
3073 * @event: stream event
3074 *
3075 * Sends a stream event to the dapm core. The core then makes any
3076 * necessary widget power changes.
3077 *
3078 * Returns 0 for success else error.
3079 */
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003080void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3081 int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003082{
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003083 struct snd_soc_card *card = rtd->card;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003084
Liam Girdwood3cd04342012-03-09 12:02:08 +00003085 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
Liam Girdwoodd9b09512012-03-07 16:32:59 +00003086 soc_dapm_stream_event(rtd, stream, event);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003087 mutex_unlock(&card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003088}
Richard Purdie2b97eab2006-10-06 18:32:18 +02003089
3090/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003091 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003092 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003093 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003094 *
Mark Brown74b8f952009-06-06 11:26:15 +01003095 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003096 * a valid audio route and active audio stream.
3097 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3098 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003099 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003100int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003101{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003102 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003103}
Liam Girdwooda5302182008-07-07 13:35:17 +01003104EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003105
3106/**
Mark Brownda341832010-03-15 19:23:37 +00003107 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003108 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003109 * @pin: pin name
3110 *
3111 * Enables input/output pin regardless of any other state. This is
3112 * intended for use with microphone bias supplies used in microphone
3113 * jack detection.
3114 *
3115 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3116 * do any widget power switching.
3117 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003118int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3119 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003120{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003121 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003122
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003123 if (!w) {
3124 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3125 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003126 }
3127
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003128 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3129 w->connected = 1;
3130 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003131 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003132
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003133 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003134}
3135EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3136
3137/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003138 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003139 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003140 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003141 *
Mark Brown74b8f952009-06-06 11:26:15 +01003142 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003143 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3144 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003145 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003146int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3147 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003148{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003149 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003150}
3151EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3152
3153/**
Mark Brown5817b522008-09-24 11:23:11 +01003154 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003155 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003156 * @pin: pin name
3157 *
3158 * Marks the specified pin as being not connected, disabling it along
3159 * any parent or child widgets. At present this is identical to
3160 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3161 * additional things such as disabling controls which only affect
3162 * paths through the pin.
3163 *
3164 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3165 * do any widget power switching.
3166 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003167int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003168{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003169 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003170}
3171EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3172
3173/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003174 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003175 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003176 * @pin: audio signal pin endpoint (or start point)
3177 *
3178 * Get audio pin status - connected or disconnected.
3179 *
3180 * Returns 1 for connected otherwise 0.
3181 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003182int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3183 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003184{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003185 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003186
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003187 if (w)
3188 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003189
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003190 return 0;
3191}
Liam Girdwooda5302182008-07-07 13:35:17 +01003192EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003193
3194/**
Mark Brown1547aba2010-05-07 21:11:40 +01003195 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003196 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003197 * @pin: audio signal pin endpoint (or start point)
3198 *
3199 * Mark the given endpoint or pin as ignoring suspend. When the
3200 * system is disabled a path between two endpoints flagged as ignoring
3201 * suspend will not be disabled. The path must already be enabled via
3202 * normal means at suspend time, it will not be turned on if it was not
3203 * already enabled.
3204 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003205int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3206 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003207{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003208 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003209
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003210 if (!w) {
3211 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3212 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003213 }
3214
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003215 w->ignore_suspend = 1;
3216
3217 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003218}
3219EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3220
Stephen Warren16332812011-11-23 12:42:04 -07003221static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3222 struct snd_soc_dapm_widget *w)
3223{
3224 struct snd_soc_dapm_path *p;
3225
3226 list_for_each_entry(p, &card->paths, list) {
3227 if ((p->source == w) || (p->sink == w)) {
3228 dev_dbg(card->dev,
3229 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3230 p->source->name, p->source->id, p->source->dapm,
3231 p->sink->name, p->sink->id, p->sink->dapm);
3232
3233 /* Connected to something other than the codec */
3234 if (p->source->dapm != p->sink->dapm)
3235 return true;
3236 /*
3237 * Loopback connection from codec external pin to
3238 * codec external pin
3239 */
3240 if (p->sink->id == snd_soc_dapm_input) {
3241 switch (p->source->id) {
3242 case snd_soc_dapm_output:
3243 case snd_soc_dapm_micbias:
3244 return true;
3245 default:
3246 break;
3247 }
3248 }
3249 }
3250 }
3251
3252 return false;
3253}
3254
3255/**
3256 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3257 * @codec: The codec whose pins should be processed
3258 *
3259 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3260 * which are unused. Pins are used if they are connected externally to the
3261 * codec, whether that be to some other device, or a loop-back connection to
3262 * the codec itself.
3263 */
3264void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3265{
3266 struct snd_soc_card *card = codec->card;
3267 struct snd_soc_dapm_context *dapm = &codec->dapm;
3268 struct snd_soc_dapm_widget *w;
3269
Mark Browna094b802011-11-27 19:42:20 +00003270 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003271 &card->dapm, &codec->dapm);
3272
3273 list_for_each_entry(w, &card->widgets, list) {
3274 if (w->dapm != dapm)
3275 continue;
3276 switch (w->id) {
3277 case snd_soc_dapm_input:
3278 case snd_soc_dapm_output:
3279 case snd_soc_dapm_micbias:
Mark Browna094b802011-11-27 19:42:20 +00003280 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003281 w->name);
3282 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003283 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003284 "... Not in map; disabling\n");
3285 snd_soc_dapm_nc_pin(dapm, w->name);
3286 }
3287 break;
3288 default:
3289 break;
3290 }
3291 }
3292}
3293
Mark Brown1547aba2010-05-07 21:11:40 +01003294/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003295 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003296 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003297 *
3298 * Free all dapm widgets and resources.
3299 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003300void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003301{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003302 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003303 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003304 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003305 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003306}
3307EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3308
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003309static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003310{
Mark Brown51737472009-06-22 13:16:51 +01003311 struct snd_soc_dapm_widget *w;
3312 LIST_HEAD(down_list);
3313 int powerdown = 0;
3314
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003315 list_for_each_entry(w, &dapm->card->widgets, list) {
3316 if (w->dapm != dapm)
3317 continue;
Mark Brown51737472009-06-22 13:16:51 +01003318 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003319 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003320 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003321 powerdown = 1;
3322 }
3323 }
3324
3325 /* If there were no widgets to power down we're already in
3326 * standby.
3327 */
3328 if (powerdown) {
Mark Brown7679e422012-02-22 15:52:56 +00003329 if (dapm->bias_level == SND_SOC_BIAS_ON)
3330 snd_soc_dapm_set_bias_level(dapm,
3331 SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003332 dapm_seq_run(dapm, &down_list, 0, false);
Mark Brown7679e422012-02-22 15:52:56 +00003333 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3334 snd_soc_dapm_set_bias_level(dapm,
3335 SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003336 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003337}
Mark Brown51737472009-06-22 13:16:51 +01003338
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003339/*
3340 * snd_soc_dapm_shutdown - callback for system shutdown
3341 */
3342void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3343{
3344 struct snd_soc_codec *codec;
3345
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003346 list_for_each_entry(codec, &card->codec_dev_list, list) {
3347 soc_dapm_shutdown_codec(&codec->dapm);
Mark Brown7679e422012-02-22 15:52:56 +00003348 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3349 snd_soc_dapm_set_bias_level(&codec->dapm,
3350 SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003351 }
Mark Brown51737472009-06-22 13:16:51 +01003352}
3353
Richard Purdie2b97eab2006-10-06 18:32:18 +02003354/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003355MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003356MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3357MODULE_LICENSE("GPL");