blob: 86569044f66269ecfc59eccee2123e0174950ecf [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
209static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
210 unsigned short reg, unsigned int mask, unsigned int value)
211{
Mark Brown8a713da2011-12-03 12:33:55 +0000212 bool change;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100213 unsigned int old, new;
214 int ret;
215
Mark Brown8a713da2011-12-03 12:33:55 +0000216 if (w->codec && w->codec->using_regmap) {
217 ret = regmap_update_bits_check(w->codec->control_data,
218 reg, mask, value, &change);
219 if (ret != 0)
220 return ret;
221 } else {
222 ret = soc_widget_read(w, reg);
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100223 if (ret < 0)
224 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +0000225
226 old = ret;
227 new = (old & ~mask) | (value & mask);
228 change = old != new;
229 if (change) {
230 ret = soc_widget_write(w, reg, new);
231 if (ret < 0)
232 return ret;
233 }
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100234 }
235
236 return change;
237}
238
Mark Brown452c5ea2009-05-17 21:41:23 +0100239/**
240 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800241 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100242 * @level: level to configure
243 *
244 * Configure the bias (power) levels for the SoC audio device.
245 *
246 * Returns 0 for success else error.
247 */
Mark Browned5a4c42011-02-18 11:12:42 -0800248static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200249 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100250{
Mark Browned5a4c42011-02-18 11:12:42 -0800251 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100252 int ret = 0;
253
Mark Brown84e90932010-11-04 00:07:02 -0400254 trace_snd_soc_bias_level_start(card, level);
255
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000256 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100257 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100258 if (ret != 0)
259 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100260
Mark Browncc4c6702011-06-06 19:03:34 +0100261 if (dapm->codec) {
262 if (dapm->codec->driver->set_bias_level)
263 ret = dapm->codec->driver->set_bias_level(dapm->codec,
264 level);
265 else
266 dapm->bias_level = level;
267 }
Mark Brown171ec6b2011-06-06 18:15:19 +0100268 if (ret != 0)
269 goto out;
270
271 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100272 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100273out:
Mark Brown84e90932010-11-04 00:07:02 -0400274 trace_snd_soc_bias_level_done(card, level);
275
Mark Brown452c5ea2009-05-17 21:41:23 +0100276 return ret;
277}
278
Richard Purdie2b97eab2006-10-06 18:32:18 +0200279/* set up initial codec paths */
280static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
281 struct snd_soc_dapm_path *p, int i)
282{
283 switch (w->id) {
284 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000285 case snd_soc_dapm_mixer:
286 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200287 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100288 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600289 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400290 unsigned int reg = mc->reg;
291 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100292 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400293 unsigned int mask = (1 << fls(max)) - 1;
294 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200295
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100296 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200297 val = (val >> shift) & mask;
298
299 if ((invert && !val) || (!invert && val))
300 p->connect = 1;
301 else
302 p->connect = 0;
303 }
304 break;
305 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600306 struct soc_enum *e = (struct soc_enum *)
307 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200308 int val, item, bitmask;
309
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100310 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100311 ;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100312 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200313 item = (val >> e->shift_l) & (bitmask - 1);
314
315 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100316 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200317 if (!(strcmp(p->name, e->texts[i])) && item == i)
318 p->connect = 1;
319 }
320 }
321 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000322 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600323 struct soc_enum *e = (struct soc_enum *)
324 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000325
326 p->connect = 0;
327 /* since a virtual mux has no backing registers to
328 * decide which path to connect, it will try to match
329 * with the first enumeration. This is to ensure
330 * that the default mux choice (the first) will be
331 * correctly powered up during initialization.
332 */
333 if (!strcmp(p->name, e->texts[0]))
334 p->connect = 1;
335 }
336 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200337 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200338 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600339 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200340 int val, item;
341
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100342 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200343 val = (val >> e->shift_l) & e->mask;
344 for (item = 0; item < e->max; item++) {
345 if (val == e->values[item])
346 break;
347 }
348
349 p->connect = 0;
350 for (i = 0; i < e->max; i++) {
351 if (!(strcmp(p->name, e->texts[i])) && item == i)
352 p->connect = 1;
353 }
354 }
355 break;
Mark Brown56563102011-10-03 22:41:09 +0100356 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200357 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600358 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200359 case snd_soc_dapm_output:
360 case snd_soc_dapm_adc:
361 case snd_soc_dapm_input:
Mark Brown1ab97c82011-11-27 16:21:51 +0000362 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200363 case snd_soc_dapm_dac:
364 case snd_soc_dapm_micbias:
365 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100366 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +0000367 case snd_soc_dapm_regulator_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100368 case snd_soc_dapm_aif_in:
369 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800370 case snd_soc_dapm_dai:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200371 case snd_soc_dapm_hp:
372 case snd_soc_dapm_mic:
373 case snd_soc_dapm_spk:
374 case snd_soc_dapm_line:
Mark Brown56563102011-10-03 22:41:09 +0100375 p->connect = 1;
376 break;
377 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200378 case snd_soc_dapm_pre:
379 case snd_soc_dapm_post:
380 p->connect = 0;
381 break;
382 }
383}
384
Mark Brown74b8f952009-06-06 11:26:15 +0100385/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200386static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200387 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
388 struct snd_soc_dapm_path *path, const char *control_name,
389 const struct snd_kcontrol_new *kcontrol)
390{
391 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
392 int i;
393
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100394 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200395 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200396 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200397 list_add(&path->list_sink, &dest->sources);
398 list_add(&path->list_source, &src->sinks);
399 path->name = (char*)e->texts[i];
400 dapm_set_path_status(dest, path, 0);
401 return 0;
402 }
403 }
404
405 return -ENODEV;
406}
407
Mark Brown74b8f952009-06-06 11:26:15 +0100408/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200409static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200410 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
411 struct snd_soc_dapm_path *path, const char *control_name)
412{
413 int i;
414
415 /* search for mixer kcontrol */
416 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600417 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
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);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600421 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200422 dapm_set_path_status(dest, path, i);
423 return 0;
424 }
425 }
426 return -ENODEV;
427}
428
Stephen Warrenaf468002011-04-28 17:38:01 -0600429static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600430 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600431 const struct snd_kcontrol_new *kcontrol_new,
432 struct snd_kcontrol **kcontrol)
433{
434 struct snd_soc_dapm_widget *w;
435 int i;
436
437 *kcontrol = NULL;
438
439 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600440 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
441 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600442 for (i = 0; i < w->num_kcontrols; i++) {
443 if (&w->kcontrol_news[i] == kcontrol_new) {
444 if (w->kcontrols)
445 *kcontrol = w->kcontrols[i];
446 return 1;
447 }
448 }
449 }
450
451 return 0;
452}
453
Richard Purdie2b97eab2006-10-06 18:32:18 +0200454/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200455static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200456{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200457 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200458 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000459 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200460 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000461 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000462 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600463 struct snd_soc_dapm_widget_list *wlist;
464 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000465
466 if (dapm->codec)
467 prefix = dapm->codec->name_prefix;
468 else
469 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200470
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000471 if (prefix)
472 prefix_len = strlen(prefix) + 1;
473 else
474 prefix_len = 0;
475
Richard Purdie2b97eab2006-10-06 18:32:18 +0200476 /* add kcontrol */
477 for (i = 0; i < w->num_kcontrols; i++) {
478
479 /* match name */
480 list_for_each_entry(path, &w->sources, list_sink) {
481
482 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600483 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200484 continue;
485
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200486 if (w->kcontrols[i]) {
487 path->kcontrol = w->kcontrols[i];
488 continue;
489 }
490
Stephen Warrenfafd2172011-04-28 17:38:00 -0600491 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
492 sizeof(struct snd_soc_dapm_widget *),
493 wlist = kzalloc(wlistsize, GFP_KERNEL);
494 if (wlist == NULL) {
495 dev_err(dapm->dev,
496 "asoc: can't allocate widget list for %s\n",
497 w->name);
498 return -ENOMEM;
499 }
500 wlist->num_widgets = 1;
501 wlist->widgets[0] = w;
502
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000503 /* add dapm control with long name.
504 * for dapm_mixer this is the concatenation of the
505 * mixer and kcontrol name.
506 * for dapm_mixer_named_ctl this is simply the
507 * kcontrol name.
508 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600509 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000510 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000511 name_len += 1 + strlen(w->name);
512
Mark Brown219b93f2008-10-28 13:02:31 +0000513 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000514
Stephen Warrenfafd2172011-04-28 17:38:00 -0600515 if (path->long_name == NULL) {
516 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200517 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600518 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200519
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000520 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000521 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000522 /* The control will get a prefix from
523 * the control creation process but
524 * we're also using the same prefix
525 * for widgets so cut the prefix off
526 * the front of the widget name.
527 */
Mark Brown888df392012-02-16 19:37:51 -0800528 snprintf((char *)path->long_name, name_len,
529 "%s %s", w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600530 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000531 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000532 case snd_soc_dapm_mixer_named_ctl:
Mark Brown888df392012-02-16 19:37:51 -0800533 snprintf((char *)path->long_name, name_len,
534 "%s", w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000535 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000536 }
537
Mark Brown888df392012-02-16 19:37:51 -0800538 ((char *)path->long_name)[name_len - 1] = '\0';
Mark Brown219b93f2008-10-28 13:02:31 +0000539
Stephen Warrenfafd2172011-04-28 17:38:00 -0600540 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
541 wlist, path->long_name,
542 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200543 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200544 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200545 dev_err(dapm->dev,
546 "asoc: failed to add dapm kcontrol %s: %d\n",
547 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600548 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200549 kfree(path->long_name);
550 path->long_name = NULL;
551 return ret;
552 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600553 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200554 }
555 }
556 return ret;
557}
558
559/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200560static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200561{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200562 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200563 struct snd_soc_dapm_path *path = NULL;
564 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000565 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000566 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000567 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600568 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600569 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600570 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600571 size_t wlistsize;
Mark Brown888df392012-02-16 19:37:51 -0800572 const char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200573
Stephen Warrenaf468002011-04-28 17:38:01 -0600574 if (w->num_kcontrols != 1) {
575 dev_err(dapm->dev,
576 "asoc: mux %s has incorrect number of controls\n",
577 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200578 return -EINVAL;
579 }
580
Stephen Warren1007da02011-05-26 09:57:33 -0600581 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600582 &kcontrol);
583 if (kcontrol) {
584 wlist = kcontrol->private_data;
585 wlistentries = wlist->num_widgets + 1;
586 } else {
587 wlist = NULL;
588 wlistentries = 1;
589 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600590 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600591 wlistentries * sizeof(struct snd_soc_dapm_widget *),
592 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600593 if (wlist == NULL) {
594 dev_err(dapm->dev,
595 "asoc: can't allocate widget list for %s\n", w->name);
596 return -ENOMEM;
597 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600598 wlist->num_widgets = wlistentries;
599 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600600
Stephen Warrenaf468002011-04-28 17:38:01 -0600601 if (!kcontrol) {
602 if (dapm->codec)
603 prefix = dapm->codec->name_prefix;
604 else
605 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000606
Stephen Warrenaf468002011-04-28 17:38:01 -0600607 if (shared) {
608 name = w->kcontrol_news[0].name;
609 prefix_len = 0;
610 } else {
611 name = w->name;
612 if (prefix)
613 prefix_len = strlen(prefix) + 1;
614 else
615 prefix_len = 0;
616 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000617
Stephen Warrenaf468002011-04-28 17:38:01 -0600618 /*
619 * The control will get a prefix from the control creation
620 * process but we're also using the same prefix for widgets so
621 * cut the prefix off the front of the widget name.
622 */
623 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
624 name + prefix_len, prefix);
625 ret = snd_ctl_add(card, kcontrol);
626 if (ret < 0) {
Mark Brown53daf202011-09-05 10:51:05 -0700627 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
628 w->name, ret);
Stephen Warrenaf468002011-04-28 17:38:01 -0600629 kfree(wlist);
630 return ret;
631 }
632 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200633
Stephen Warrenaf468002011-04-28 17:38:01 -0600634 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200635
Stephen Warrenfad59882011-04-28 17:37:59 -0600636 w->kcontrols[0] = kcontrol;
637
Richard Purdie2b97eab2006-10-06 18:32:18 +0200638 list_for_each_entry(path, &w->sources, list_sink)
639 path->kcontrol = kcontrol;
640
Stephen Warrenaf468002011-04-28 17:38:01 -0600641 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200642}
643
644/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200645static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200646{
Mark Browna6c65732010-03-03 17:45:21 +0000647 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200648 dev_err(w->dapm->dev,
649 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200650
Mark Browna6c65732010-03-03 17:45:21 +0000651 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200652}
653
654/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200655static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200656{
657 struct snd_soc_dapm_path *p;
658
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200659 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200660 p->walked = 0;
661}
662
Mark Brown99497882010-05-07 20:24:05 +0100663/* We implement power down on suspend by checking the power state of
664 * the ALSA card - when we are suspending the ALSA state for the card
665 * is set to D3.
666 */
667static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
668{
Mark Brown12ea2c72011-03-02 18:17:32 +0000669 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100670
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000671 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100672 case SNDRV_CTL_POWER_D3hot:
673 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100674 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200675 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
676 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100677 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100678 default:
679 return 1;
680 }
681}
682
Richard Purdie2b97eab2006-10-06 18:32:18 +0200683/*
684 * Recursively check for a completed path to an active or physically connected
685 * output widget. Returns number of complete paths.
686 */
687static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
688{
689 struct snd_soc_dapm_path *path;
690 int con = 0;
691
Mark Brown024dc072011-10-09 11:52:05 +0100692 if (widget->outputs >= 0)
693 return widget->outputs;
694
Mark Brownde02d072011-09-20 21:43:24 +0100695 DAPM_UPDATE_STAT(widget, path_checks);
696
Mark Brown62ea8742012-01-21 21:14:48 +0000697 switch (widget->id) {
698 case snd_soc_dapm_supply:
699 case snd_soc_dapm_regulator_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100700 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000701 default:
702 break;
703 }
Mark Brown246d0a12009-04-22 18:24:55 +0100704
Mark Brown010ff262009-08-17 17:39:22 +0100705 switch (widget->id) {
706 case snd_soc_dapm_adc:
707 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -0800708 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100709 if (widget->active) {
710 widget->outputs = snd_soc_dapm_suspend_check(widget);
711 return widget->outputs;
712 }
Mark Brown010ff262009-08-17 17:39:22 +0100713 default:
714 break;
715 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200716
717 if (widget->connected) {
718 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100719 if (widget->id == snd_soc_dapm_output && !widget->ext) {
720 widget->outputs = snd_soc_dapm_suspend_check(widget);
721 return widget->outputs;
722 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200723
724 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100725 if (widget->id == snd_soc_dapm_hp ||
726 widget->id == snd_soc_dapm_spk ||
727 (widget->id == snd_soc_dapm_line &&
728 !list_empty(&widget->sources))) {
729 widget->outputs = snd_soc_dapm_suspend_check(widget);
730 return widget->outputs;
731 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200732 }
733
734 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100735 DAPM_UPDATE_STAT(widget, neighbour_checks);
736
Mark Brownbf3a9e12011-06-13 16:42:29 +0100737 if (path->weak)
738 continue;
739
Richard Purdie2b97eab2006-10-06 18:32:18 +0200740 if (path->walked)
741 continue;
742
743 if (path->sink && path->connect) {
744 path->walked = 1;
745 con += is_connected_output_ep(path->sink);
746 }
747 }
748
Mark Brown024dc072011-10-09 11:52:05 +0100749 widget->outputs = con;
750
Richard Purdie2b97eab2006-10-06 18:32:18 +0200751 return con;
752}
753
754/*
755 * Recursively check for a completed path to an active or physically connected
756 * input widget. Returns number of complete paths.
757 */
758static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
759{
760 struct snd_soc_dapm_path *path;
761 int con = 0;
762
Mark Brown024dc072011-10-09 11:52:05 +0100763 if (widget->inputs >= 0)
764 return widget->inputs;
765
Mark Brownde02d072011-09-20 21:43:24 +0100766 DAPM_UPDATE_STAT(widget, path_checks);
767
Mark Brown62ea8742012-01-21 21:14:48 +0000768 switch (widget->id) {
769 case snd_soc_dapm_supply:
770 case snd_soc_dapm_regulator_supply:
Mark Brown246d0a12009-04-22 18:24:55 +0100771 return 0;
Mark Brown62ea8742012-01-21 21:14:48 +0000772 default:
773 break;
774 }
Mark Brown246d0a12009-04-22 18:24:55 +0100775
Richard Purdie2b97eab2006-10-06 18:32:18 +0200776 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100777 switch (widget->id) {
778 case snd_soc_dapm_dac:
779 case snd_soc_dapm_aif_in:
Mark Brown888df392012-02-16 19:37:51 -0800780 case snd_soc_dapm_dai:
Mark Brown024dc072011-10-09 11:52:05 +0100781 if (widget->active) {
782 widget->inputs = snd_soc_dapm_suspend_check(widget);
783 return widget->inputs;
784 }
Mark Brown010ff262009-08-17 17:39:22 +0100785 default:
786 break;
787 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200788
789 if (widget->connected) {
790 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100791 if (widget->id == snd_soc_dapm_input && !widget->ext) {
792 widget->inputs = snd_soc_dapm_suspend_check(widget);
793 return widget->inputs;
794 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200795
796 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100797 if (widget->id == snd_soc_dapm_vmid) {
798 widget->inputs = snd_soc_dapm_suspend_check(widget);
799 return widget->inputs;
800 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200801
802 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300803 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100804 (widget->id == snd_soc_dapm_line &&
805 !list_empty(&widget->sinks))) {
806 widget->inputs = snd_soc_dapm_suspend_check(widget);
807 return widget->inputs;
808 }
809
Mark Brown1ab97c82011-11-27 16:21:51 +0000810 /* signal generator */
811 if (widget->id == snd_soc_dapm_siggen) {
812 widget->inputs = snd_soc_dapm_suspend_check(widget);
813 return widget->inputs;
814 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200815 }
816
817 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100818 DAPM_UPDATE_STAT(widget, neighbour_checks);
819
Mark Brownbf3a9e12011-06-13 16:42:29 +0100820 if (path->weak)
821 continue;
822
Richard Purdie2b97eab2006-10-06 18:32:18 +0200823 if (path->walked)
824 continue;
825
826 if (path->source && path->connect) {
827 path->walked = 1;
828 con += is_connected_input_ep(path->source);
829 }
830 }
831
Mark Brown024dc072011-10-09 11:52:05 +0100832 widget->inputs = con;
833
Richard Purdie2b97eab2006-10-06 18:32:18 +0200834 return con;
835}
836
837/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300838 * Handler for generic register modifier widget.
839 */
840int dapm_reg_event(struct snd_soc_dapm_widget *w,
841 struct snd_kcontrol *kcontrol, int event)
842{
843 unsigned int val;
844
845 if (SND_SOC_DAPM_EVENT_ON(event))
846 val = w->on_val;
847 else
848 val = w->off_val;
849
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100850 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300851 w->mask << w->shift, val << w->shift);
852
853 return 0;
854}
Mark Brown11589412008-07-29 11:42:23 +0100855EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300856
Mark Brown62ea8742012-01-21 21:14:48 +0000857/*
858 * Handler for regulator supply widget.
859 */
860int dapm_regulator_event(struct snd_soc_dapm_widget *w,
861 struct snd_kcontrol *kcontrol, int event)
862{
863 if (SND_SOC_DAPM_EVENT_ON(event))
864 return regulator_enable(w->priv);
865 else
866 return regulator_disable_deferred(w->priv, w->shift);
867}
868EXPORT_SYMBOL_GPL(dapm_regulator_event);
869
Mark Brownd8050022011-09-28 18:28:23 +0100870static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
871{
Mark Brown9b8a83b2011-10-04 22:15:59 +0100872 if (w->power_checked)
873 return w->new_power;
874
Mark Brownd8050022011-09-28 18:28:23 +0100875 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +0100876 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +0100877 else
Mark Brown9b8a83b2011-10-04 22:15:59 +0100878 w->new_power = w->power_check(w);
879
880 w->power_checked = true;
881
882 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +0100883}
884
Mark Browncd0f2d42009-04-20 16:56:59 +0100885/* Generic check to see if a widget should be powered.
886 */
887static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
888{
889 int in, out;
890
Mark Brownde02d072011-09-20 21:43:24 +0100891 DAPM_UPDATE_STAT(w, power_checks);
892
Mark Browncd0f2d42009-04-20 16:56:59 +0100893 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200894 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100895 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200896 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100897 return out != 0 && in != 0;
898}
899
Mark Brown888df392012-02-16 19:37:51 -0800900static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
901{
902 DAPM_UPDATE_STAT(w, power_checks);
903
904 return w->active;
905}
906
Mark Brown6ea31b92009-04-20 17:15:41 +0100907/* Check to see if an ADC has power */
908static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
909{
910 int in;
911
Mark Brownde02d072011-09-20 21:43:24 +0100912 DAPM_UPDATE_STAT(w, power_checks);
913
Mark Brown6ea31b92009-04-20 17:15:41 +0100914 if (w->active) {
915 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200916 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100917 return in != 0;
918 } else {
919 return dapm_generic_check_power(w);
920 }
921}
922
923/* Check to see if a DAC has power */
924static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
925{
926 int out;
927
Mark Brownde02d072011-09-20 21:43:24 +0100928 DAPM_UPDATE_STAT(w, power_checks);
929
Mark Brown6ea31b92009-04-20 17:15:41 +0100930 if (w->active) {
931 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200932 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100933 return out != 0;
934 } else {
935 return dapm_generic_check_power(w);
936 }
937}
938
Mark Brown246d0a12009-04-22 18:24:55 +0100939/* Check to see if a power supply is needed */
940static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
941{
942 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +0100943
Mark Brownde02d072011-09-20 21:43:24 +0100944 DAPM_UPDATE_STAT(w, power_checks);
945
Mark Brown246d0a12009-04-22 18:24:55 +0100946 /* Check if one of our outputs is connected */
947 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +0100948 DAPM_UPDATE_STAT(w, neighbour_checks);
949
Mark Brownbf3a9e12011-06-13 16:42:29 +0100950 if (path->weak)
951 continue;
952
Mark Brown215edda2009-09-08 18:59:05 +0100953 if (path->connected &&
954 !path->connected(path->source, path->sink))
955 continue;
956
Mark Brown30173582011-02-11 11:42:19 +0000957 if (!path->sink)
958 continue;
959
Mark Brownf68d7e12011-10-04 22:57:50 +0100960 if (dapm_widget_power_check(path->sink))
961 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +0100962 }
963
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200964 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100965
Mark Brownf68d7e12011-10-04 22:57:50 +0100966 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +0100967}
968
Mark Brown35c64bc2011-09-28 18:23:53 +0100969static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
970{
971 return 1;
972}
973
Mark Brown38357ab2009-06-06 19:03:23 +0100974static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
975 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000976 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000977{
Mark Brown828a8422011-01-15 13:14:30 +0000978 int *sort;
979
980 if (power_up)
981 sort = dapm_up_seq;
982 else
983 sort = dapm_down_seq;
984
Mark Brown38357ab2009-06-06 19:03:23 +0100985 if (sort[a->id] != sort[b->id])
986 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000987 if (a->subseq != b->subseq) {
988 if (power_up)
989 return a->subseq - b->subseq;
990 else
991 return b->subseq - a->subseq;
992 }
Mark Brownb22ead22009-06-07 12:51:26 +0100993 if (a->reg != b->reg)
994 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000995 if (a->dapm != b->dapm)
996 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000997
Mark Brown38357ab2009-06-06 19:03:23 +0100998 return 0;
999}
Mark Brown42aa3412009-03-01 19:21:10 +00001000
Mark Brown38357ab2009-06-06 19:03:23 +01001001/* Insert a widget in order into a DAPM power sequence. */
1002static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1003 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +00001004 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +01001005{
1006 struct snd_soc_dapm_widget *w;
1007
1008 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +00001009 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +01001010 list_add_tail(&new_widget->power_list, &w->power_list);
1011 return;
Mark Brown42aa3412009-03-01 19:21:10 +00001012 }
Mark Brown6ea31b92009-04-20 17:15:41 +01001013
Mark Brown38357ab2009-06-06 19:03:23 +01001014 list_add_tail(&new_widget->power_list, list);
1015}
Mark Brown42aa3412009-03-01 19:21:10 +00001016
Mark Brown68f89ad2010-11-03 23:51:49 -04001017static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1018 struct snd_soc_dapm_widget *w, int event)
1019{
1020 struct snd_soc_card *card = dapm->card;
1021 const char *ev_name;
1022 int power, ret;
1023
1024 switch (event) {
1025 case SND_SOC_DAPM_PRE_PMU:
1026 ev_name = "PRE_PMU";
1027 power = 1;
1028 break;
1029 case SND_SOC_DAPM_POST_PMU:
1030 ev_name = "POST_PMU";
1031 power = 1;
1032 break;
1033 case SND_SOC_DAPM_PRE_PMD:
1034 ev_name = "PRE_PMD";
1035 power = 0;
1036 break;
1037 case SND_SOC_DAPM_POST_PMD:
1038 ev_name = "POST_PMD";
1039 power = 0;
1040 break;
1041 default:
1042 BUG();
1043 return;
1044 }
1045
1046 if (w->power != power)
1047 return;
1048
1049 if (w->event && (w->event_flags & event)) {
1050 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1051 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -04001052 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001053 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -04001054 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -04001055 if (ret < 0)
1056 pr_err("%s: %s event failed: %d\n",
1057 ev_name, w->name, ret);
1058 }
1059}
1060
Mark Brownb22ead22009-06-07 12:51:26 +01001061/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001062static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001063 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001064{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001065 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001066 struct snd_soc_dapm_widget *w;
1067 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001068 unsigned int value = 0;
1069 unsigned int mask = 0;
1070 unsigned int cur_mask;
1071
1072 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1073 power_list)->reg;
1074
1075 list_for_each_entry(w, pending, power_list) {
1076 cur_mask = 1 << w->shift;
1077 BUG_ON(reg != w->reg);
1078
1079 if (w->invert)
1080 power = !w->power;
1081 else
1082 power = w->power;
1083
1084 mask |= cur_mask;
1085 if (power)
1086 value |= cur_mask;
1087
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001088 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001089 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1090 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001091
Mark Brown68f89ad2010-11-03 23:51:49 -04001092 /* Check for events */
1093 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1094 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001095 }
1096
Mark Brown81628102009-06-07 13:21:24 +01001097 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001098 /* Any widget will do, they should all be updating the
1099 * same register.
1100 */
1101 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1102 power_list);
1103
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001104 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001105 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001106 value, mask, reg, card->pop_time);
1107 pop_wait(card->pop_time);
Liam Girdwood0445bdf2011-06-13 19:37:36 +01001108 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001109 }
1110
1111 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001112 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1113 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001114 }
Mark Brown42aa3412009-03-01 19:21:10 +00001115}
1116
Mark Brownb22ead22009-06-07 12:51:26 +01001117/* Apply a DAPM power sequence.
1118 *
1119 * We walk over a pre-sorted list of widgets to apply power to. In
1120 * order to minimise the number of writes to the device required
1121 * multiple widgets will be updated in a single write where possible.
1122 * Currently anything that requires more than a single write is not
1123 * handled.
1124 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001125static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001126 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001127{
1128 struct snd_soc_dapm_widget *w, *n;
1129 LIST_HEAD(pending);
1130 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001131 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001132 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001133 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001134 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001135 int *sort;
1136
1137 if (power_up)
1138 sort = dapm_up_seq;
1139 else
1140 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001141
Mark Brownb22ead22009-06-07 12:51:26 +01001142 list_for_each_entry_safe(w, n, list, power_list) {
1143 ret = 0;
1144
1145 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001146 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001147 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001148 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001149 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001150
Mark Brown474b62d2011-01-18 16:14:44 +00001151 if (cur_dapm && cur_dapm->seq_notifier) {
1152 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1153 if (sort[i] == cur_sort)
1154 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001155 i,
1156 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001157 }
1158
Mark Brownb22ead22009-06-07 12:51:26 +01001159 INIT_LIST_HEAD(&pending);
1160 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001161 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001162 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001163 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001164 }
1165
Mark Brown163cac02009-06-07 10:12:52 +01001166 switch (w->id) {
1167 case snd_soc_dapm_pre:
1168 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001169 list_for_each_entry_safe_continue(w, n, list,
1170 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001171
Mark Brownb22ead22009-06-07 12:51:26 +01001172 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001173 ret = w->event(w,
1174 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001175 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001176 ret = w->event(w,
1177 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001178 break;
1179
1180 case snd_soc_dapm_post:
1181 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001182 list_for_each_entry_safe_continue(w, n, list,
1183 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001184
Mark Brownb22ead22009-06-07 12:51:26 +01001185 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001186 ret = w->event(w,
1187 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001188 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001189 ret = w->event(w,
1190 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001191 break;
1192
Mark Brown163cac02009-06-07 10:12:52 +01001193 default:
Mark Brown81628102009-06-07 13:21:24 +01001194 /* Queue it up for application */
1195 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001196 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001197 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001198 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001199 list_move(&w->power_list, &pending);
1200 break;
Mark Brown163cac02009-06-07 10:12:52 +01001201 }
Mark Brownb22ead22009-06-07 12:51:26 +01001202
1203 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001204 dev_err(w->dapm->dev,
1205 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001206 }
Mark Brownb22ead22009-06-07 12:51:26 +01001207
1208 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001209 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001210
1211 if (cur_dapm && cur_dapm->seq_notifier) {
1212 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1213 if (sort[i] == cur_sort)
1214 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001215 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001216 }
Mark Brown163cac02009-06-07 10:12:52 +01001217}
1218
Mark Brown97404f22010-12-14 16:13:57 +00001219static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1220{
1221 struct snd_soc_dapm_update *update = dapm->update;
1222 struct snd_soc_dapm_widget *w;
1223 int ret;
1224
1225 if (!update)
1226 return;
1227
1228 w = update->widget;
1229
1230 if (w->event &&
1231 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1232 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1233 if (ret != 0)
1234 pr_err("%s DAPM pre-event failed: %d\n",
1235 w->name, ret);
1236 }
1237
1238 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1239 update->val);
1240 if (ret < 0)
1241 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1242
1243 if (w->event &&
1244 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1245 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1246 if (ret != 0)
1247 pr_err("%s DAPM post-event failed: %d\n",
1248 w->name, ret);
1249 }
1250}
1251
Mark Brown9d0624a2011-02-18 11:49:43 -08001252/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1253 * they're changing state.
1254 */
1255static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1256{
1257 struct snd_soc_dapm_context *d = data;
1258 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001259
Mark Brown56fba412011-06-04 11:25:10 +01001260 /* If we're off and we're not supposed to be go into STANDBY */
1261 if (d->bias_level == SND_SOC_BIAS_OFF &&
1262 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brownf1aac482011-12-05 15:17:06 +00001263 if (d->dev)
1264 pm_runtime_get_sync(d->dev);
1265
Mark Brown9d0624a2011-02-18 11:49:43 -08001266 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1267 if (ret != 0)
1268 dev_err(d->dev,
1269 "Failed to turn on bias: %d\n", ret);
1270 }
1271
Mark Brown56fba412011-06-04 11:25:10 +01001272 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1273 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001274 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1275 if (ret != 0)
1276 dev_err(d->dev,
1277 "Failed to prepare bias: %d\n", ret);
1278 }
1279}
1280
1281/* Async callback run prior to DAPM sequences - brings to their final
1282 * state.
1283 */
1284static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1285{
1286 struct snd_soc_dapm_context *d = data;
1287 int ret;
1288
1289 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001290 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1291 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1292 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001293 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1294 if (ret != 0)
1295 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1296 ret);
1297 }
1298
1299 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001300 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1301 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001302 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1303 if (ret != 0)
1304 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
Mark Brownf1aac482011-12-05 15:17:06 +00001305
1306 if (d->dev)
Mark Brownfb644e92012-01-25 19:53:58 +00001307 pm_runtime_put(d->dev);
Mark Brown9d0624a2011-02-18 11:49:43 -08001308 }
1309
1310 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001311 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1312 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001313 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1314 if (ret != 0)
1315 dev_err(d->dev, "Failed to apply active bias: %d\n",
1316 ret);
1317 }
1318}
Mark Brown97404f22010-12-14 16:13:57 +00001319
Mark Brownfe4fda52011-10-03 22:36:57 +01001320static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1321 bool power, bool connect)
1322{
1323 /* If a connection is being made or broken then that update
1324 * will have marked the peer dirty, otherwise the widgets are
1325 * not connected and this update has no impact. */
1326 if (!connect)
1327 return;
1328
1329 /* If the peer is already in the state we're moving to then we
1330 * won't have an impact on it. */
1331 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001332 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001333}
1334
Mark Brown05623c42011-09-28 17:02:31 +01001335static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1336 struct list_head *up_list,
1337 struct list_head *down_list)
1338{
Mark Browndb432b42011-10-03 21:06:40 +01001339 struct snd_soc_dapm_path *path;
1340
Mark Brown05623c42011-09-28 17:02:31 +01001341 if (w->power == power)
1342 return;
1343
1344 trace_snd_soc_dapm_widget_power(w, power);
1345
Mark Browndb432b42011-10-03 21:06:40 +01001346 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001347 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001348 */
1349 list_for_each_entry(path, &w->sources, list_sink) {
1350 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001351 dapm_widget_set_peer_power(path->source, power,
1352 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001353 }
1354 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001355 switch (w->id) {
1356 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001357 case snd_soc_dapm_regulator_supply:
Mark Brownf3bf3e42011-10-04 22:43:31 +01001358 /* Supplies can't affect their outputs, only their inputs */
1359 break;
1360 default:
1361 list_for_each_entry(path, &w->sinks, list_source) {
1362 if (path->sink) {
1363 dapm_widget_set_peer_power(path->sink, power,
1364 path->connect);
1365 }
Mark Browndb432b42011-10-03 21:06:40 +01001366 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001367 break;
Mark Browndb432b42011-10-03 21:06:40 +01001368 }
1369
Mark Brown05623c42011-09-28 17:02:31 +01001370 if (power)
1371 dapm_seq_insert(w, up_list, true);
1372 else
1373 dapm_seq_insert(w, down_list, false);
1374
1375 w->power = power;
1376}
1377
Mark Brown7c81beb2011-09-20 22:22:32 +01001378static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1379 struct list_head *up_list,
1380 struct list_head *down_list)
1381{
Mark Brown7c81beb2011-09-20 22:22:32 +01001382 int power;
1383
1384 switch (w->id) {
1385 case snd_soc_dapm_pre:
1386 dapm_seq_insert(w, down_list, false);
1387 break;
1388 case snd_soc_dapm_post:
1389 dapm_seq_insert(w, up_list, true);
1390 break;
1391
1392 default:
Mark Brownd8050022011-09-28 18:28:23 +01001393 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001394
Mark Brown05623c42011-09-28 17:02:31 +01001395 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001396 break;
1397 }
1398}
1399
Mark Brown42aa3412009-03-01 19:21:10 +00001400/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001401 * Scan each dapm widget for complete audio path.
1402 * A complete path is a route that has valid endpoints i.e.:-
1403 *
1404 * o DAC to output pin.
1405 * o Input Pin to ADC.
1406 * o Input pin to Output pin (bypass, sidetone)
1407 * o DAC to ADC (loopback).
1408 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001409static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001410{
Mark Brown12ea2c72011-03-02 18:17:32 +00001411 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001412 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001413 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001414 LIST_HEAD(up_list);
1415 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001416 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001417 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001418
Mark Brown84e90932010-11-04 00:07:02 -04001419 trace_snd_soc_dapm_start(card);
1420
Mark Brown56fba412011-06-04 11:25:10 +01001421 list_for_each_entry(d, &card->dapm_list, list) {
1422 if (d->n_widgets || d->codec == NULL) {
1423 if (d->idle_bias_off)
1424 d->target_bias_level = SND_SOC_BIAS_OFF;
1425 else
1426 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1427 }
1428 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001429
Liam Girdwood6c120e12012-02-15 15:15:34 +00001430 dapm_reset(card);
Mark Brown9b8a83b2011-10-04 22:15:59 +01001431
Mark Brown6d3ddc82009-05-16 17:47:29 +01001432 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001433 * lists indicating if they should be powered up or down. We
1434 * only check widgets that have been flagged as dirty but note
1435 * that new widgets may be added to the dirty list while we
1436 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001437 */
Mark Browndb432b42011-10-03 21:06:40 +01001438 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001439 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001440 }
1441
Mark Brownf9de6d72011-09-28 17:19:47 +01001442 list_for_each_entry(w, &card->widgets, list) {
Mark Browndb432b42011-10-03 21:06:40 +01001443 list_del_init(&w->dirty);
1444
Mark Brownf9de6d72011-09-28 17:19:47 +01001445 if (w->power) {
1446 d = w->dapm;
1447
1448 /* Supplies and micbiases only bring the
1449 * context up to STANDBY as unless something
1450 * else is active and passing audio they
Mark Brownafe62362012-01-25 19:55:22 +00001451 * generally don't require full power. Signal
1452 * generators are virtual pins and have no
1453 * power impact themselves.
Mark Brownf9de6d72011-09-28 17:19:47 +01001454 */
1455 switch (w->id) {
Mark Brownafe62362012-01-25 19:55:22 +00001456 case snd_soc_dapm_siggen:
1457 break;
Mark Brownf9de6d72011-09-28 17:19:47 +01001458 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001459 case snd_soc_dapm_regulator_supply:
Mark Brownf9de6d72011-09-28 17:19:47 +01001460 case snd_soc_dapm_micbias:
1461 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1462 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1463 break;
1464 default:
1465 d->target_bias_level = SND_SOC_BIAS_ON;
1466 break;
1467 }
1468 }
1469
1470 }
1471
Mark Brownb14b76a2009-08-17 11:55:38 +01001472 /* If there are no DAPM widgets then try to figure out power from the
1473 * event type.
1474 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001475 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001476 switch (event) {
1477 case SND_SOC_DAPM_STREAM_START:
1478 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001479 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a2009-08-17 11:55:38 +01001480 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001481 case SND_SOC_DAPM_STREAM_STOP:
Liam Girdwoode7c80e22012-01-16 15:23:31 +00001482 if (dapm->codec && dapm->codec->active)
Mark Brown56fba412011-06-04 11:25:10 +01001483 dapm->target_bias_level = SND_SOC_BIAS_ON;
1484 else
1485 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001486 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001487 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001488 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001489 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001490 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001491 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001492 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001493 default:
1494 break;
1495 }
1496 }
1497
Mark Brown85a843c2011-09-21 21:29:47 +01001498 /* Force all contexts in the card to the same bias state if
1499 * they're not ground referenced.
1500 */
Mark Brown56fba412011-06-04 11:25:10 +01001501 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001502 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001503 if (d->target_bias_level > bias)
1504 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001505 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001506 if (!d->idle_bias_off)
1507 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001508
Mark Brownde02d072011-09-20 21:43:24 +01001509 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001510
Mark Brown9d0624a2011-02-18 11:49:43 -08001511 /* Run all the bias changes in parallel */
1512 list_for_each_entry(d, &dapm->card->dapm_list, list)
1513 async_schedule_domain(dapm_pre_sequence_async, d,
1514 &async_domain);
1515 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001516
Mark Brown6d3ddc82009-05-16 17:47:29 +01001517 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001518 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001519
Mark Brown97404f22010-12-14 16:13:57 +00001520 dapm_widget_update(dapm);
1521
Mark Brown6d3ddc82009-05-16 17:47:29 +01001522 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001523 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001524
Mark Brown9d0624a2011-02-18 11:49:43 -08001525 /* Run all the bias changes in parallel */
1526 list_for_each_entry(d, &dapm->card->dapm_list, list)
1527 async_schedule_domain(dapm_post_sequence_async, d,
1528 &async_domain);
1529 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001530
Liam Girdwood8078d872012-02-15 15:15:35 +00001531 /* do we need to notify any clients that DAPM event is complete */
1532 list_for_each_entry(d, &card->dapm_list, list) {
1533 if (d->stream_event)
1534 d->stream_event(d, event);
1535 }
1536
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001537 pop_dbg(dapm->dev, card->pop_time,
1538 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001539 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001540
Mark Brown84e90932010-11-04 00:07:02 -04001541 trace_snd_soc_dapm_done(card);
1542
Mark Brown42aa3412009-03-01 19:21:10 +00001543 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001544}
1545
Mark Brown79fb9382009-08-21 16:38:13 +01001546#ifdef CONFIG_DEBUG_FS
1547static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1548{
1549 file->private_data = inode->i_private;
1550 return 0;
1551}
1552
1553static ssize_t dapm_widget_power_read_file(struct file *file,
1554 char __user *user_buf,
1555 size_t count, loff_t *ppos)
1556{
1557 struct snd_soc_dapm_widget *w = file->private_data;
1558 char *buf;
1559 int in, out;
1560 ssize_t ret;
1561 struct snd_soc_dapm_path *p = NULL;
1562
1563 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1564 if (!buf)
1565 return -ENOMEM;
1566
1567 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001568 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001569 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001570 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001571
Mark Brownd033c362009-12-04 15:25:56 +00001572 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001573 w->name, w->power ? "On" : "Off", in, out);
1574
Mark Brownd033c362009-12-04 15:25:56 +00001575 if (w->reg >= 0)
1576 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1577 " - R%d(0x%x) bit %d",
1578 w->reg, w->reg, w->shift);
1579
1580 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1581
Mark Brown3eef08b2009-09-14 16:49:00 +01001582 if (w->sname)
1583 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1584 w->sname,
1585 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001586
1587 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001588 if (p->connected && !p->connected(w, p->sink))
1589 continue;
1590
Mark Brown79fb9382009-08-21 16:38:13 +01001591 if (p->connect)
1592 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001593 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001594 p->name ? p->name : "static",
1595 p->source->name);
1596 }
1597 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001598 if (p->connected && !p->connected(w, p->sink))
1599 continue;
1600
Mark Brown79fb9382009-08-21 16:38:13 +01001601 if (p->connect)
1602 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001603 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001604 p->name ? p->name : "static",
1605 p->sink->name);
1606 }
1607
1608 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1609
1610 kfree(buf);
1611 return ret;
1612}
1613
1614static const struct file_operations dapm_widget_power_fops = {
1615 .open = dapm_widget_power_open_file,
1616 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001617 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001618};
1619
Mark Brownef49e4f2011-04-04 20:48:13 +09001620static int dapm_bias_open_file(struct inode *inode, struct file *file)
1621{
1622 file->private_data = inode->i_private;
1623 return 0;
1624}
1625
1626static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1627 size_t count, loff_t *ppos)
1628{
1629 struct snd_soc_dapm_context *dapm = file->private_data;
1630 char *level;
1631
1632 switch (dapm->bias_level) {
1633 case SND_SOC_BIAS_ON:
1634 level = "On\n";
1635 break;
1636 case SND_SOC_BIAS_PREPARE:
1637 level = "Prepare\n";
1638 break;
1639 case SND_SOC_BIAS_STANDBY:
1640 level = "Standby\n";
1641 break;
1642 case SND_SOC_BIAS_OFF:
1643 level = "Off\n";
1644 break;
1645 default:
1646 BUG();
1647 level = "Unknown\n";
1648 break;
1649 }
1650
1651 return simple_read_from_buffer(user_buf, count, ppos, level,
1652 strlen(level));
1653}
1654
1655static const struct file_operations dapm_bias_fops = {
1656 .open = dapm_bias_open_file,
1657 .read = dapm_bias_read_file,
1658 .llseek = default_llseek,
1659};
1660
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001661void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1662 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001663{
Mark Brown79fb9382009-08-21 16:38:13 +01001664 struct dentry *d;
1665
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001666 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1667
1668 if (!dapm->debugfs_dapm) {
1669 printk(KERN_WARNING
1670 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001671 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001672 }
Mark Brown79fb9382009-08-21 16:38:13 +01001673
Mark Brownef49e4f2011-04-04 20:48:13 +09001674 d = debugfs_create_file("bias_level", 0444,
1675 dapm->debugfs_dapm, dapm,
1676 &dapm_bias_fops);
1677 if (!d)
1678 dev_warn(dapm->dev,
1679 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001680}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001681
1682static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1683{
1684 struct snd_soc_dapm_context *dapm = w->dapm;
1685 struct dentry *d;
1686
1687 if (!dapm->debugfs_dapm || !w->name)
1688 return;
1689
1690 d = debugfs_create_file(w->name, 0444,
1691 dapm->debugfs_dapm, w,
1692 &dapm_widget_power_fops);
1693 if (!d)
1694 dev_warn(w->dapm->dev,
1695 "ASoC: Failed to create %s debugfs file\n",
1696 w->name);
1697}
1698
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001699static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1700{
1701 debugfs_remove_recursive(dapm->debugfs_dapm);
1702}
1703
Mark Brown79fb9382009-08-21 16:38:13 +01001704#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001705void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1706 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001707{
1708}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001709
1710static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1711{
1712}
1713
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001714static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1715{
1716}
1717
Mark Brown79fb9382009-08-21 16:38:13 +01001718#endif
1719
Richard Purdie2b97eab2006-10-06 18:32:18 +02001720/* test and update the power status of a mux widget */
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001721int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1722 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001723{
1724 struct snd_soc_dapm_path *path;
1725 int found = 0;
1726
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001727 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001728 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001729 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001730 return -ENODEV;
1731
Richard Purdie2b97eab2006-10-06 18:32:18 +02001732 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001733 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001734 if (path->kcontrol != kcontrol)
1735 continue;
1736
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001737 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001738 continue;
1739
1740 found = 1;
1741 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001742 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001743 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001744 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001745 } else {
1746 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001747 dapm_mark_dirty(path->source,
1748 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001749 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001750 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001751 }
1752
Mark Browndb432b42011-10-03 21:06:40 +01001753 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001754 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001755 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001756 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001757
1758 return 0;
1759}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001760EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001761
Milan plzik1b075e32008-01-10 14:39:46 +01001762/* test and update the power status of a mixer or switch widget */
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001763int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001764 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001765{
1766 struct snd_soc_dapm_path *path;
1767 int found = 0;
1768
Milan plzik1b075e32008-01-10 14:39:46 +01001769 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001770 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001771 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001772 return -ENODEV;
1773
Richard Purdie2b97eab2006-10-06 18:32:18 +02001774 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001775 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001776 if (path->kcontrol != kcontrol)
1777 continue;
1778
1779 /* found, now check type */
1780 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001781 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001782 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001783 }
1784
Mark Browndb432b42011-10-03 21:06:40 +01001785 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001786 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001787 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001788 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001789
1790 return 0;
1791}
Liam Girdwood40f02cd2012-02-06 16:05:14 +00001792EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001793
1794/* show dapm widget status in sys fs */
1795static ssize_t dapm_widget_show(struct device *dev,
1796 struct device_attribute *attr, char *buf)
1797{
Mark Brown36ae1a92012-01-06 17:12:45 -08001798 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001799 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001800 struct snd_soc_dapm_widget *w;
1801 int count = 0;
1802 char *state = "not set";
1803
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001804 list_for_each_entry(w, &codec->card->widgets, list) {
1805 if (w->dapm != &codec->dapm)
1806 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001807
1808 /* only display widgets that burnm power */
1809 switch (w->id) {
1810 case snd_soc_dapm_hp:
1811 case snd_soc_dapm_mic:
1812 case snd_soc_dapm_spk:
1813 case snd_soc_dapm_line:
1814 case snd_soc_dapm_micbias:
1815 case snd_soc_dapm_dac:
1816 case snd_soc_dapm_adc:
1817 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001818 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001819 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001820 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001821 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00001822 case snd_soc_dapm_regulator_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001823 if (w->name)
1824 count += sprintf(buf + count, "%s: %s\n",
1825 w->name, w->power ? "On":"Off");
1826 break;
1827 default:
1828 break;
1829 }
1830 }
1831
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001832 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001833 case SND_SOC_BIAS_ON:
1834 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001835 break;
Mark Brown0be98982008-05-19 12:31:28 +02001836 case SND_SOC_BIAS_PREPARE:
1837 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001838 break;
Mark Brown0be98982008-05-19 12:31:28 +02001839 case SND_SOC_BIAS_STANDBY:
1840 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001841 break;
Mark Brown0be98982008-05-19 12:31:28 +02001842 case SND_SOC_BIAS_OFF:
1843 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001844 break;
1845 }
1846 count += sprintf(buf + count, "PM State: %s\n", state);
1847
1848 return count;
1849}
1850
1851static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1852
1853int snd_soc_dapm_sys_add(struct device *dev)
1854{
Troy Kisky12ef1932008-10-13 17:42:14 -07001855 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001856}
1857
1858static void snd_soc_dapm_sys_remove(struct device *dev)
1859{
Mark Brownaef90842009-05-16 17:53:16 +01001860 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001861}
1862
1863/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001864static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001865{
1866 struct snd_soc_dapm_widget *w, *next_w;
1867 struct snd_soc_dapm_path *p, *next_p;
1868
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001869 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1870 if (w->dapm != dapm)
1871 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001872 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001873 /*
1874 * remove source and sink paths associated to this widget.
1875 * While removing the path, remove reference to it from both
1876 * source and sink widgets so that path is removed only once.
1877 */
1878 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1879 list_del(&p->list_sink);
1880 list_del(&p->list_source);
1881 list_del(&p->list);
1882 kfree(p->long_name);
1883 kfree(p);
1884 }
1885 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1886 list_del(&p->list_sink);
1887 list_del(&p->list_source);
1888 list_del(&p->list);
1889 kfree(p->long_name);
1890 kfree(p);
1891 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001892 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001893 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001894 kfree(w);
1895 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001896}
1897
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001898static struct snd_soc_dapm_widget *dapm_find_widget(
1899 struct snd_soc_dapm_context *dapm, const char *pin,
1900 bool search_other_contexts)
1901{
1902 struct snd_soc_dapm_widget *w;
1903 struct snd_soc_dapm_widget *fallback = NULL;
1904
1905 list_for_each_entry(w, &dapm->card->widgets, list) {
1906 if (!strcmp(w->name, pin)) {
1907 if (w->dapm == dapm)
1908 return w;
1909 else
1910 fallback = w;
1911 }
1912 }
1913
1914 if (search_other_contexts)
1915 return fallback;
1916
1917 return NULL;
1918}
1919
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001920static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001921 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001922{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001923 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001924
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001925 if (!w) {
1926 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1927 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001928 }
1929
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001930 w->connected = status;
1931 if (status == 0)
1932 w->force = 0;
Mark Brown75c1f892011-10-04 22:28:08 +01001933 dapm_mark_dirty(w, "pin configuration");
Mark Brown0d867332011-04-06 11:38:14 +09001934
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001935 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001936}
1937
Richard Purdie2b97eab2006-10-06 18:32:18 +02001938/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001939 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001940 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001941 *
1942 * Walks all dapm audio paths and powers widgets according to their
1943 * stream or path usage.
1944 *
1945 * Returns 0 for success.
1946 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001947int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001948{
Mark Brown4f4c0072011-10-07 14:29:19 +01001949 /*
1950 * Suppress early reports (eg, jacks syncing their state) to avoid
1951 * silly DAPM runs during card startup.
1952 */
1953 if (!dapm->card || !dapm->card->instantiated)
1954 return 0;
1955
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001956 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001957}
Liam Girdwooda5302182008-07-07 13:35:17 +01001958EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001959
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001960static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001961 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001962{
1963 struct snd_soc_dapm_path *path;
1964 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001965 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001966 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001967 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001968 const char *source;
1969 char prefixed_sink[80];
1970 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001971 int ret = 0;
1972
Mark Brown88e8b9a2011-03-02 18:18:24 +00001973 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001974 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1975 dapm->codec->name_prefix, route->sink);
1976 sink = prefixed_sink;
1977 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1978 dapm->codec->name_prefix, route->source);
1979 source = prefixed_source;
1980 } else {
1981 sink = route->sink;
1982 source = route->source;
1983 }
1984
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001985 /*
1986 * find src and dest widgets over all widgets but favor a widget from
1987 * current DAPM context
1988 */
1989 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001990 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001991 wtsink = w;
1992 if (w->dapm == dapm)
1993 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001994 continue;
1995 }
1996 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001997 wtsource = w;
1998 if (w->dapm == dapm)
1999 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002000 }
2001 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002002 /* use widget from another DAPM context if not found from this */
2003 if (!wsink)
2004 wsink = wtsink;
2005 if (!wsource)
2006 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002007
2008 if (wsource == NULL || wsink == NULL)
2009 return -ENODEV;
2010
2011 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2012 if (!path)
2013 return -ENOMEM;
2014
2015 path->source = wsource;
2016 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01002017 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002018 INIT_LIST_HEAD(&path->list);
2019 INIT_LIST_HEAD(&path->list_source);
2020 INIT_LIST_HEAD(&path->list_sink);
2021
2022 /* check for external widgets */
2023 if (wsink->id == snd_soc_dapm_input) {
2024 if (wsource->id == snd_soc_dapm_micbias ||
2025 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01002026 wsource->id == snd_soc_dapm_line ||
2027 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002028 wsink->ext = 1;
2029 }
2030 if (wsource->id == snd_soc_dapm_output) {
2031 if (wsink->id == snd_soc_dapm_spk ||
2032 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02002033 wsink->id == snd_soc_dapm_line ||
2034 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002035 wsource->ext = 1;
2036 }
2037
2038 /* connect static paths */
2039 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002040 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002041 list_add(&path->list_sink, &wsink->sources);
2042 list_add(&path->list_source, &wsource->sinks);
2043 path->connect = 1;
2044 return 0;
2045 }
2046
2047 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08002048 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02002049 case snd_soc_dapm_adc:
2050 case snd_soc_dapm_dac:
2051 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002052 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002053 case snd_soc_dapm_input:
2054 case snd_soc_dapm_output:
Mark Brown1ab97c82011-11-27 16:21:51 +00002055 case snd_soc_dapm_siggen:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002056 case snd_soc_dapm_micbias:
2057 case snd_soc_dapm_vmid:
2058 case snd_soc_dapm_pre:
2059 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01002060 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002061 case snd_soc_dapm_regulator_supply:
Mark Brown010ff262009-08-17 17:39:22 +01002062 case snd_soc_dapm_aif_in:
2063 case snd_soc_dapm_aif_out:
Mark Brown888df392012-02-16 19:37:51 -08002064 case snd_soc_dapm_dai:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002065 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002066 list_add(&path->list_sink, &wsink->sources);
2067 list_add(&path->list_source, &wsource->sinks);
2068 path->connect = 1;
2069 return 0;
2070 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002071 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02002072 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002073 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002074 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002075 if (ret != 0)
2076 goto err;
2077 break;
2078 case snd_soc_dapm_switch:
2079 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002080 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002081 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002082 if (ret != 0)
2083 goto err;
2084 break;
2085 case snd_soc_dapm_hp:
2086 case snd_soc_dapm_mic:
2087 case snd_soc_dapm_line:
2088 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002089 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002090 list_add(&path->list_sink, &wsink->sources);
2091 list_add(&path->list_source, &wsource->sinks);
2092 path->connect = 0;
2093 return 0;
2094 }
2095 return 0;
2096
2097err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002098 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2099 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002100 kfree(path);
2101 return ret;
2102}
Mark Brown105f1c22008-05-13 14:52:19 +02002103
2104/**
Mark Brown105f1c22008-05-13 14:52:19 +02002105 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002106 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002107 * @route: audio routes
2108 * @num: number of routes
2109 *
2110 * Connects 2 dapm widgets together via a named audio path. The sink is
2111 * the widget receiving the audio signal, whilst the source is the sender
2112 * of the audio signal.
2113 *
2114 * Returns 0 for success else error. On error all resources can be freed
2115 * with a call to snd_soc_card_free().
2116 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002117int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002118 const struct snd_soc_dapm_route *route, int num)
2119{
2120 int i, ret;
2121
2122 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002123 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02002124 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002125 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2126 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02002127 return ret;
2128 }
2129 route++;
2130 }
2131
2132 return 0;
2133}
2134EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2135
Mark Brownbf3a9e12011-06-13 16:42:29 +01002136static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2137 const struct snd_soc_dapm_route *route)
2138{
2139 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2140 route->source,
2141 true);
2142 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2143 route->sink,
2144 true);
2145 struct snd_soc_dapm_path *path;
2146 int count = 0;
2147
2148 if (!source) {
2149 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2150 route->source);
2151 return -ENODEV;
2152 }
2153
2154 if (!sink) {
2155 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2156 route->sink);
2157 return -ENODEV;
2158 }
2159
2160 if (route->control || route->connected)
2161 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2162 route->source, route->sink);
2163
2164 list_for_each_entry(path, &source->sinks, list_source) {
2165 if (path->sink == sink) {
2166 path->weak = 1;
2167 count++;
2168 }
2169 }
2170
2171 if (count == 0)
2172 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2173 route->source, route->sink);
2174 if (count > 1)
2175 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2176 count, route->source, route->sink);
2177
2178 return 0;
2179}
2180
2181/**
2182 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2183 * @dapm: DAPM context
2184 * @route: audio routes
2185 * @num: number of routes
2186 *
2187 * Mark existing routes matching those specified in the passed array
2188 * as being weak, meaning that they are ignored for the purpose of
2189 * power decisions. The main intended use case is for sidetone paths
2190 * which couple audio between other independent paths if they are both
2191 * active in order to make the combination work better at the user
2192 * level but which aren't intended to be "used".
2193 *
2194 * Note that CODEC drivers should not use this as sidetone type paths
2195 * can frequently also be used as bypass paths.
2196 */
2197int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2198 const struct snd_soc_dapm_route *route, int num)
2199{
2200 int i, err;
2201 int ret = 0;
2202
2203 for (i = 0; i < num; i++) {
2204 err = snd_soc_dapm_weak_route(dapm, route);
2205 if (err)
2206 ret = err;
2207 route++;
2208 }
2209
2210 return ret;
2211}
2212EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2213
Mark Brown105f1c22008-05-13 14:52:19 +02002214/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002215 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002216 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002217 *
2218 * Checks the codec for any new dapm widgets and creates them if found.
2219 *
2220 * Returns 0 for success.
2221 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002222int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002223{
2224 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002225 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002226
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002227 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002228 {
2229 if (w->new)
2230 continue;
2231
Stephen Warrenfad59882011-04-28 17:37:59 -06002232 if (w->num_kcontrols) {
2233 w->kcontrols = kzalloc(w->num_kcontrols *
2234 sizeof(struct snd_kcontrol *),
2235 GFP_KERNEL);
2236 if (!w->kcontrols)
2237 return -ENOMEM;
2238 }
2239
Richard Purdie2b97eab2006-10-06 18:32:18 +02002240 switch(w->id) {
2241 case snd_soc_dapm_switch:
2242 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002243 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002244 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002245 break;
2246 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002247 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002248 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002249 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002250 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002251 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002252 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002253 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002254 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002255 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002256 break;
2257 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002258
2259 /* Read the initial power state from the device */
2260 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002261 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002262 val &= 1 << w->shift;
2263 if (w->invert)
2264 val = !val;
2265
2266 if (val)
2267 w->power = 1;
2268 }
2269
Richard Purdie2b97eab2006-10-06 18:32:18 +02002270 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002271
Mark Brown7508b122011-10-05 12:09:12 +01002272 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002273 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002274 }
2275
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002276 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002277 return 0;
2278}
2279EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2280
2281/**
2282 * snd_soc_dapm_get_volsw - dapm mixer get callback
2283 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002284 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002285 *
2286 * Callback to get the value of a dapm mixer control.
2287 *
2288 * Returns 0 for success.
2289 */
2290int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2291 struct snd_ctl_elem_value *ucontrol)
2292{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002293 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2294 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002295 struct soc_mixer_control *mc =
2296 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002297 unsigned int reg = mc->reg;
2298 unsigned int shift = mc->shift;
2299 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002300 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002301 unsigned int invert = mc->invert;
2302 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002303
Richard Purdie2b97eab2006-10-06 18:32:18 +02002304 ucontrol->value.integer.value[0] =
2305 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2306 if (shift != rshift)
2307 ucontrol->value.integer.value[1] =
2308 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2309 if (invert) {
2310 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002311 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002312 if (shift != rshift)
2313 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002314 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002315 }
2316
2317 return 0;
2318}
2319EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2320
2321/**
2322 * snd_soc_dapm_put_volsw - dapm mixer set callback
2323 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002324 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002325 *
2326 * Callback to set the value of a dapm mixer control.
2327 *
2328 * Returns 0 for success.
2329 */
2330int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_value *ucontrol)
2332{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002333 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2334 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2335 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002336 struct soc_mixer_control *mc =
2337 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002338 unsigned int reg = mc->reg;
2339 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002340 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002341 unsigned int mask = (1 << fls(max)) - 1;
2342 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002343 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002344 int connect, change;
2345 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002346 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002347
2348 val = (ucontrol->value.integer.value[0] & mask);
2349
2350 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002351 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002352 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002353 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002354
Stephen Warrenfafd2172011-04-28 17:38:00 -06002355 if (val)
2356 /* new connection */
2357 connect = invert ? 0 : 1;
2358 else
2359 /* old connection must be powered down */
2360 connect = invert ? 1 : 0;
2361
2362 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363
Stephen Warrene9cf7042011-01-27 14:54:05 -07002364 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002365 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002366 for (wi = 0; wi < wlist->num_widgets; wi++) {
2367 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002368
Stephen Warrenfafd2172011-04-28 17:38:00 -06002369 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002370
Stephen Warrenfafd2172011-04-28 17:38:00 -06002371 update.kcontrol = kcontrol;
2372 update.widget = widget;
2373 update.reg = reg;
2374 update.mask = mask;
2375 update.val = val;
2376 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002377
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002378 snd_soc_dapm_mixer_update_power(widget, kcontrol, connect);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002379
2380 widget->dapm->update = NULL;
2381 }
Mark Brown283375c2009-12-07 18:09:03 +00002382 }
2383
Stephen Warrenfafd2172011-04-28 17:38:00 -06002384 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002385 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002386}
2387EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2388
2389/**
2390 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2391 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002392 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002393 *
2394 * Callback to get the value of a dapm enumerated double mixer control.
2395 *
2396 * Returns 0 for success.
2397 */
2398int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2399 struct snd_ctl_elem_value *ucontrol)
2400{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002401 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2402 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002403 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002404 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002405
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002406 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002407 ;
2408 val = snd_soc_read(widget->codec, e->reg);
2409 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2410 if (e->shift_l != e->shift_r)
2411 ucontrol->value.enumerated.item[1] =
2412 (val >> e->shift_r) & (bitmask - 1);
2413
2414 return 0;
2415}
2416EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2417
2418/**
2419 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2420 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002421 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002422 *
2423 * Callback to set the value of a dapm enumerated double mixer control.
2424 *
2425 * Returns 0 for success.
2426 */
2427int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2428 struct snd_ctl_elem_value *ucontrol)
2429{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002430 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2431 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2432 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002433 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002434 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002435 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002436 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002437 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002438
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002439 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002440 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002441 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002442 return -EINVAL;
2443 mux = ucontrol->value.enumerated.item[0];
2444 val = mux << e->shift_l;
2445 mask = (bitmask - 1) << e->shift_l;
2446 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002447 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002448 return -EINVAL;
2449 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2450 mask |= (bitmask - 1) << e->shift_r;
2451 }
2452
Stephen Warrenfafd2172011-04-28 17:38:00 -06002453 mutex_lock(&codec->mutex);
2454
Mark Brown3a655772009-10-05 17:23:30 +01002455 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002456 if (change) {
2457 for (wi = 0; wi < wlist->num_widgets; wi++) {
2458 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002459
Stephen Warrenfafd2172011-04-28 17:38:00 -06002460 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002461
Stephen Warrenfafd2172011-04-28 17:38:00 -06002462 update.kcontrol = kcontrol;
2463 update.widget = widget;
2464 update.reg = e->reg;
2465 update.mask = mask;
2466 update.val = val;
2467 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002468
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002469 snd_soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002470
Stephen Warrenfafd2172011-04-28 17:38:00 -06002471 widget->dapm->update = NULL;
2472 }
2473 }
2474
2475 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002476 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002477}
2478EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2479
2480/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002481 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2482 * @kcontrol: mixer control
2483 * @ucontrol: control element information
2484 *
2485 * Returns 0 for success.
2486 */
2487int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2488 struct snd_ctl_elem_value *ucontrol)
2489{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002490 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2491 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002492
2493 ucontrol->value.enumerated.item[0] = widget->value;
2494
2495 return 0;
2496}
2497EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2498
2499/**
2500 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2501 * @kcontrol: mixer control
2502 * @ucontrol: control element information
2503 *
2504 * Returns 0 for success.
2505 */
2506int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2507 struct snd_ctl_elem_value *ucontrol)
2508{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002509 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2510 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2511 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002512 struct soc_enum *e =
2513 (struct soc_enum *)kcontrol->private_value;
2514 int change;
2515 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002516 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002517
2518 if (ucontrol->value.enumerated.item[0] >= e->max)
2519 return -EINVAL;
2520
Stephen Warrenfafd2172011-04-28 17:38:00 -06002521 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002522
2523 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002524 if (change) {
2525 for (wi = 0; wi < wlist->num_widgets; wi++) {
2526 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002527
Stephen Warrenfafd2172011-04-28 17:38:00 -06002528 widget->value = ucontrol->value.enumerated.item[0];
2529
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002530 snd_soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002531 }
2532 }
2533
2534 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002535 return ret;
2536}
2537EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2538
2539/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002540 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2541 * callback
2542 * @kcontrol: mixer control
2543 * @ucontrol: control element information
2544 *
2545 * Callback to get the value of a dapm semi enumerated double mixer control.
2546 *
2547 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2548 * used for handling bitfield coded enumeration for example.
2549 *
2550 * Returns 0 for success.
2551 */
2552int snd_soc_dapm_get_value_enum_double(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];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002557 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002558 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002559
2560 reg_val = snd_soc_read(widget->codec, e->reg);
2561 val = (reg_val >> e->shift_l) & e->mask;
2562 for (mux = 0; mux < e->max; mux++) {
2563 if (val == e->values[mux])
2564 break;
2565 }
2566 ucontrol->value.enumerated.item[0] = mux;
2567 if (e->shift_l != e->shift_r) {
2568 val = (reg_val >> e->shift_r) & e->mask;
2569 for (mux = 0; mux < e->max; mux++) {
2570 if (val == e->values[mux])
2571 break;
2572 }
2573 ucontrol->value.enumerated.item[1] = mux;
2574 }
2575
2576 return 0;
2577}
2578EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2579
2580/**
2581 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2582 * callback
2583 * @kcontrol: mixer control
2584 * @ucontrol: control element information
2585 *
2586 * Callback to set the value of a dapm semi enumerated double mixer control.
2587 *
2588 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2589 * used for handling bitfield coded enumeration for example.
2590 *
2591 * Returns 0 for success.
2592 */
2593int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_value *ucontrol)
2595{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002596 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2597 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2598 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002599 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002600 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002601 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002602 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002603 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002604
2605 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2606 return -EINVAL;
2607 mux = ucontrol->value.enumerated.item[0];
2608 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2609 mask = e->mask << e->shift_l;
2610 if (e->shift_l != e->shift_r) {
2611 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2612 return -EINVAL;
2613 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2614 mask |= e->mask << e->shift_r;
2615 }
2616
Stephen Warrenfafd2172011-04-28 17:38:00 -06002617 mutex_lock(&codec->mutex);
2618
Mark Brown3a655772009-10-05 17:23:30 +01002619 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002620 if (change) {
2621 for (wi = 0; wi < wlist->num_widgets; wi++) {
2622 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002623
Stephen Warrenfafd2172011-04-28 17:38:00 -06002624 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002625
Stephen Warrenfafd2172011-04-28 17:38:00 -06002626 update.kcontrol = kcontrol;
2627 update.widget = widget;
2628 update.reg = e->reg;
2629 update.mask = mask;
2630 update.val = val;
2631 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002632
Liam Girdwood40f02cd2012-02-06 16:05:14 +00002633 snd_soc_dapm_mux_update_power(widget, kcontrol, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002634
Stephen Warrenfafd2172011-04-28 17:38:00 -06002635 widget->dapm->update = NULL;
2636 }
2637 }
2638
2639 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002640 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002641}
2642EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2643
2644/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002645 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2646 *
2647 * @kcontrol: mixer control
2648 * @uinfo: control element information
2649 *
2650 * Callback to provide information about a pin switch control.
2651 */
2652int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2653 struct snd_ctl_elem_info *uinfo)
2654{
2655 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2656 uinfo->count = 1;
2657 uinfo->value.integer.min = 0;
2658 uinfo->value.integer.max = 1;
2659
2660 return 0;
2661}
2662EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2663
2664/**
2665 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2666 *
2667 * @kcontrol: mixer control
2668 * @ucontrol: Value
2669 */
2670int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2671 struct snd_ctl_elem_value *ucontrol)
2672{
Mark Brown48a8c392012-02-14 17:11:15 -08002673 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002674 const char *pin = (const char *)kcontrol->private_value;
2675
Mark Brown48a8c392012-02-14 17:11:15 -08002676 mutex_lock(&card->mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002677
2678 ucontrol->value.integer.value[0] =
Mark Brown48a8c392012-02-14 17:11:15 -08002679 snd_soc_dapm_get_pin_status(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002680
Mark Brown48a8c392012-02-14 17:11:15 -08002681 mutex_unlock(&card->mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002682
2683 return 0;
2684}
2685EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2686
2687/**
2688 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2689 *
2690 * @kcontrol: mixer control
2691 * @ucontrol: Value
2692 */
2693int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2694 struct snd_ctl_elem_value *ucontrol)
2695{
Mark Brown48a8c392012-02-14 17:11:15 -08002696 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002697 const char *pin = (const char *)kcontrol->private_value;
2698
Mark Brown48a8c392012-02-14 17:11:15 -08002699 mutex_lock(&card->mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002700
2701 if (ucontrol->value.integer.value[0])
Mark Brown48a8c392012-02-14 17:11:15 -08002702 snd_soc_dapm_enable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002703 else
Mark Brown48a8c392012-02-14 17:11:15 -08002704 snd_soc_dapm_disable_pin(&card->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002705
Mark Brown48a8c392012-02-14 17:11:15 -08002706 snd_soc_dapm_sync(&card->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002707
Mark Brown48a8c392012-02-14 17:11:15 -08002708 mutex_unlock(&card->mutex);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002709
2710 return 0;
2711}
2712EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2713
Mark Brown5ba06fc2012-02-16 11:07:13 -08002714static struct snd_soc_dapm_widget *
2715snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2716 const struct snd_soc_dapm_widget *widget)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002717{
2718 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002719 size_t name_len;
Mark Brown62ea8742012-01-21 21:14:48 +00002720 int ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002721
2722 if ((w = dapm_cnew_widget(widget)) == NULL)
Mark Brown5ba06fc2012-02-16 11:07:13 -08002723 return NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002724
Mark Brown62ea8742012-01-21 21:14:48 +00002725 switch (w->id) {
2726 case snd_soc_dapm_regulator_supply:
2727 w->priv = devm_regulator_get(dapm->dev, w->name);
2728 if (IS_ERR(w->priv)) {
2729 ret = PTR_ERR(w->priv);
2730 dev_err(dapm->dev, "Failed to request %s: %d\n",
2731 w->name, ret);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002732 return NULL;
Mark Brown62ea8742012-01-21 21:14:48 +00002733 }
2734 break;
2735 default:
2736 break;
2737 }
2738
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002739 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002740 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002741 name_len += 1 + strlen(dapm->codec->name_prefix);
2742 w->name = kmalloc(name_len, GFP_KERNEL);
2743 if (w->name == NULL) {
2744 kfree(w);
Mark Brown5ba06fc2012-02-16 11:07:13 -08002745 return NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002746 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002747 if (dapm->codec && dapm->codec->name_prefix)
Mark Brown888df392012-02-16 19:37:51 -08002748 snprintf((char *)w->name, name_len, "%s %s",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002749 dapm->codec->name_prefix, widget->name);
2750 else
Mark Brown888df392012-02-16 19:37:51 -08002751 snprintf((char *)w->name, name_len, "%s", widget->name);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002752
Mark Brown7ca3a182011-10-08 14:04:50 +01002753 switch (w->id) {
2754 case snd_soc_dapm_switch:
2755 case snd_soc_dapm_mixer:
2756 case snd_soc_dapm_mixer_named_ctl:
2757 w->power_check = dapm_generic_check_power;
2758 break;
2759 case snd_soc_dapm_mux:
2760 case snd_soc_dapm_virt_mux:
2761 case snd_soc_dapm_value_mux:
2762 w->power_check = dapm_generic_check_power;
2763 break;
2764 case snd_soc_dapm_adc:
2765 case snd_soc_dapm_aif_out:
2766 w->power_check = dapm_adc_check_power;
2767 break;
2768 case snd_soc_dapm_dac:
2769 case snd_soc_dapm_aif_in:
2770 w->power_check = dapm_dac_check_power;
2771 break;
2772 case snd_soc_dapm_pga:
2773 case snd_soc_dapm_out_drv:
2774 case snd_soc_dapm_input:
2775 case snd_soc_dapm_output:
2776 case snd_soc_dapm_micbias:
2777 case snd_soc_dapm_spk:
2778 case snd_soc_dapm_hp:
2779 case snd_soc_dapm_mic:
2780 case snd_soc_dapm_line:
2781 w->power_check = dapm_generic_check_power;
2782 break;
2783 case snd_soc_dapm_supply:
Mark Brown62ea8742012-01-21 21:14:48 +00002784 case snd_soc_dapm_regulator_supply:
Mark Brown7ca3a182011-10-08 14:04:50 +01002785 w->power_check = dapm_supply_check_power;
2786 break;
Mark Brown888df392012-02-16 19:37:51 -08002787 case snd_soc_dapm_dai:
2788 w->power_check = dapm_dai_check_power;
2789 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002790 default:
2791 w->power_check = dapm_always_on_check_power;
2792 break;
2793 }
2794
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002795 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002796 w->dapm = dapm;
2797 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002798 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002799 INIT_LIST_HEAD(&w->sources);
2800 INIT_LIST_HEAD(&w->sinks);
2801 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01002802 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002803 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002804
2805 /* machine layer set ups unconnected pins and insertions */
2806 w->connected = 1;
Mark Brown5ba06fc2012-02-16 11:07:13 -08002807 return w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002808}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002809
2810/**
Mark Brown4ba13272008-05-13 14:51:19 +02002811 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002812 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002813 * @widget: widget array
2814 * @num: number of widgets
2815 *
2816 * Creates new DAPM controls based upon the templates.
2817 *
2818 * Returns 0 for success else error.
2819 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002820int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002821 const struct snd_soc_dapm_widget *widget,
2822 int num)
2823{
Mark Brown5ba06fc2012-02-16 11:07:13 -08002824 struct snd_soc_dapm_widget *w;
2825 int i;
Mark Brown4ba13272008-05-13 14:51:19 +02002826
2827 for (i = 0; i < num; i++) {
Mark Brown5ba06fc2012-02-16 11:07:13 -08002828 w = snd_soc_dapm_new_control(dapm, widget);
2829 if (!w) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002830 dev_err(dapm->dev,
Mark Brown5ba06fc2012-02-16 11:07:13 -08002831 "ASoC: Failed to create DAPM control %s\n",
2832 widget->name);
2833 return -ENOMEM;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002834 }
Mark Brown4ba13272008-05-13 14:51:19 +02002835 widget++;
2836 }
2837 return 0;
2838}
2839EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2840
Mark Brown888df392012-02-16 19:37:51 -08002841int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
2842 struct snd_soc_dai *dai)
2843{
2844 struct snd_soc_dapm_widget template;
2845 struct snd_soc_dapm_widget *w;
2846
2847 WARN_ON(dapm->dev != dai->dev);
2848
2849 memset(&template, 0, sizeof(template));
2850 template.reg = SND_SOC_NOPM;
2851
2852 if (dai->driver->playback.stream_name) {
2853 template.id = snd_soc_dapm_dai;
2854 template.name = dai->driver->playback.stream_name;
2855 template.sname = dai->driver->playback.stream_name;
2856
2857 dev_dbg(dai->dev, "adding %s widget\n",
2858 template.name);
2859
2860 w = snd_soc_dapm_new_control(dapm, &template);
2861 if (!w) {
2862 dev_err(dapm->dev, "Failed to create %s widget\n",
2863 dai->driver->playback.stream_name);
2864 }
2865
2866 w->priv = dai;
2867 dai->playback_widget = w;
2868 }
2869
2870 if (dai->driver->capture.stream_name) {
2871 template.id = snd_soc_dapm_dai;
2872 template.name = dai->driver->capture.stream_name;
2873 template.sname = dai->driver->capture.stream_name;
2874
2875 dev_dbg(dai->dev, "adding %s widget\n",
2876 template.name);
2877
2878 w = snd_soc_dapm_new_control(dapm, &template);
2879 if (!w) {
2880 dev_err(dapm->dev, "Failed to create %s widget\n",
2881 dai->driver->capture.stream_name);
2882 }
2883
2884 w->priv = dai;
2885 dai->capture_widget = w;
2886 }
2887
2888 return 0;
2889}
2890
2891int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
2892{
2893 struct snd_soc_dapm_widget *dai_w, *w;
2894 struct snd_soc_dai *dai;
2895 struct snd_soc_dapm_route r;
2896
2897 memset(&r, 0, sizeof(r));
2898
2899 /* For each DAI widget... */
2900 list_for_each_entry(dai_w, &card->widgets, list) {
2901 if (dai_w->id != snd_soc_dapm_dai)
2902 continue;
2903
2904 dai = dai_w->priv;
2905
2906 /* ...find all widgets with the same stream and link them */
2907 list_for_each_entry(w, &card->widgets, list) {
2908 if (w->dapm != dai_w->dapm)
2909 continue;
2910
2911 if (w->id == snd_soc_dapm_dai)
2912 continue;
2913
2914 if (!w->sname)
2915 continue;
2916
2917 if (dai->driver->playback.stream_name &&
2918 strstr(w->sname,
2919 dai->driver->playback.stream_name)) {
2920 r.source = dai->playback_widget->name;
2921 r.sink = w->name;
2922 dev_dbg(dai->dev, "%s -> %s\n",
2923 r.source, r.sink);
2924
2925 snd_soc_dapm_add_route(w->dapm, &r);
2926 }
2927
2928 if (dai->driver->capture.stream_name &&
2929 strstr(w->sname,
2930 dai->driver->capture.stream_name)) {
2931 r.source = w->name;
2932 r.sink = dai->capture_widget->name;
2933 dev_dbg(dai->dev, "%s -> %s\n",
2934 r.source, r.sink);
2935
2936 snd_soc_dapm_add_route(w->dapm, &r);
2937 }
2938 }
2939 }
2940
2941 return 0;
2942}
2943
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002944static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002945 int stream, struct snd_soc_dai *dai,
2946 int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002947{
2948 struct snd_soc_dapm_widget *w;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002949
2950 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Mark Brownfe360682012-02-16 19:43:20 -08002951 w = dai->playback_widget;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002952 else
Mark Brownfe360682012-02-16 19:43:20 -08002953 w = dai->capture_widget;
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002954
Mark Brownfe360682012-02-16 19:43:20 -08002955 if (!w)
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002956 return;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002957
Mark Brownfe360682012-02-16 19:43:20 -08002958 dapm_mark_dirty(w, "stream event");
2959
2960 switch (event) {
2961 case SND_SOC_DAPM_STREAM_START:
2962 w->active = 1;
2963 break;
2964 case SND_SOC_DAPM_STREAM_STOP:
2965 w->active = 0;
2966 break;
2967 case SND_SOC_DAPM_STREAM_SUSPEND:
2968 case SND_SOC_DAPM_STREAM_RESUME:
2969 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2970 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2971 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002972 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002973
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002974 dapm_power_widgets(dapm, event);
2975}
2976
2977/**
2978 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2979 * @rtd: PCM runtime data
2980 * @stream: stream name
2981 * @event: stream event
2982 *
2983 * Sends a stream event to the dapm core. The core then makes any
2984 * necessary widget power changes.
2985 *
2986 * Returns 0 for success else error.
2987 */
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002988int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
2989 struct snd_soc_dai *dai, int event)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002990{
2991 struct snd_soc_codec *codec = rtd->codec;
2992
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002993 mutex_lock(&codec->mutex);
Mark Brown7bd3a6f2012-02-16 15:03:27 -08002994 soc_dapm_stream_event(&codec->dapm, stream, dai, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002995 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002996 return 0;
2997}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002998
2999/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003000 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003001 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003002 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02003003 *
Mark Brown74b8f952009-06-06 11:26:15 +01003004 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01003005 * a valid audio route and active audio stream.
3006 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3007 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02003008 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003009int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003010{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003011 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003012}
Liam Girdwooda5302182008-07-07 13:35:17 +01003013EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003014
3015/**
Mark Brownda341832010-03-15 19:23:37 +00003016 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003017 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00003018 * @pin: pin name
3019 *
3020 * Enables input/output pin regardless of any other state. This is
3021 * intended for use with microphone bias supplies used in microphone
3022 * jack detection.
3023 *
3024 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3025 * do any widget power switching.
3026 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003027int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3028 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00003029{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003030 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00003031
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003032 if (!w) {
3033 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3034 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00003035 }
3036
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003037 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3038 w->connected = 1;
3039 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01003040 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09003041
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003042 return 0;
Mark Brownda341832010-03-15 19:23:37 +00003043}
3044EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3045
3046/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003047 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003048 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003049 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003050 *
Mark Brown74b8f952009-06-06 11:26:15 +01003051 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01003052 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3053 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003054 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003055int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3056 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01003057{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003058 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01003059}
3060EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3061
3062/**
Mark Brown5817b522008-09-24 11:23:11 +01003063 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003064 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01003065 * @pin: pin name
3066 *
3067 * Marks the specified pin as being not connected, disabling it along
3068 * any parent or child widgets. At present this is identical to
3069 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3070 * additional things such as disabling controls which only affect
3071 * paths through the pin.
3072 *
3073 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3074 * do any widget power switching.
3075 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003076int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01003077{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003078 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01003079}
3080EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3081
3082/**
Liam Girdwooda5302182008-07-07 13:35:17 +01003083 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003084 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01003085 * @pin: audio signal pin endpoint (or start point)
3086 *
3087 * Get audio pin status - connected or disconnected.
3088 *
3089 * Returns 1 for connected otherwise 0.
3090 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003091int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3092 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003093{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003094 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003095
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003096 if (w)
3097 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06003098
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003099 return 0;
3100}
Liam Girdwooda5302182008-07-07 13:35:17 +01003101EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02003102
3103/**
Mark Brown1547aba2010-05-07 21:11:40 +01003104 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003105 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01003106 * @pin: audio signal pin endpoint (or start point)
3107 *
3108 * Mark the given endpoint or pin as ignoring suspend. When the
3109 * system is disabled a path between two endpoints flagged as ignoring
3110 * suspend will not be disabled. The path must already be enabled via
3111 * normal means at suspend time, it will not be turned on if it was not
3112 * already enabled.
3113 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003114int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3115 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01003116{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003117 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01003118
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003119 if (!w) {
3120 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3121 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01003122 }
3123
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02003124 w->ignore_suspend = 1;
3125
3126 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01003127}
3128EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3129
Stephen Warren16332812011-11-23 12:42:04 -07003130static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3131 struct snd_soc_dapm_widget *w)
3132{
3133 struct snd_soc_dapm_path *p;
3134
3135 list_for_each_entry(p, &card->paths, list) {
3136 if ((p->source == w) || (p->sink == w)) {
3137 dev_dbg(card->dev,
3138 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3139 p->source->name, p->source->id, p->source->dapm,
3140 p->sink->name, p->sink->id, p->sink->dapm);
3141
3142 /* Connected to something other than the codec */
3143 if (p->source->dapm != p->sink->dapm)
3144 return true;
3145 /*
3146 * Loopback connection from codec external pin to
3147 * codec external pin
3148 */
3149 if (p->sink->id == snd_soc_dapm_input) {
3150 switch (p->source->id) {
3151 case snd_soc_dapm_output:
3152 case snd_soc_dapm_micbias:
3153 return true;
3154 default:
3155 break;
3156 }
3157 }
3158 }
3159 }
3160
3161 return false;
3162}
3163
3164/**
3165 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3166 * @codec: The codec whose pins should be processed
3167 *
3168 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3169 * which are unused. Pins are used if they are connected externally to the
3170 * codec, whether that be to some other device, or a loop-back connection to
3171 * the codec itself.
3172 */
3173void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3174{
3175 struct snd_soc_card *card = codec->card;
3176 struct snd_soc_dapm_context *dapm = &codec->dapm;
3177 struct snd_soc_dapm_widget *w;
3178
Mark Browna094b802011-11-27 19:42:20 +00003179 dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
Stephen Warren16332812011-11-23 12:42:04 -07003180 &card->dapm, &codec->dapm);
3181
3182 list_for_each_entry(w, &card->widgets, list) {
3183 if (w->dapm != dapm)
3184 continue;
3185 switch (w->id) {
3186 case snd_soc_dapm_input:
3187 case snd_soc_dapm_output:
3188 case snd_soc_dapm_micbias:
Mark Browna094b802011-11-27 19:42:20 +00003189 dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
Stephen Warren16332812011-11-23 12:42:04 -07003190 w->name);
3191 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
Mark Browna094b802011-11-27 19:42:20 +00003192 dev_dbg(codec->dev,
Stephen Warren16332812011-11-23 12:42:04 -07003193 "... Not in map; disabling\n");
3194 snd_soc_dapm_nc_pin(dapm, w->name);
3195 }
3196 break;
3197 default:
3198 break;
3199 }
3200 }
3201}
3202
Mark Brown1547aba2010-05-07 21:11:40 +01003203/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02003204 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03003205 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02003206 *
3207 * Free all dapm widgets and resources.
3208 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003209void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02003210{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003211 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02003212 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003213 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02003214 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02003215}
3216EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3217
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003218static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01003219{
Mark Brown51737472009-06-22 13:16:51 +01003220 struct snd_soc_dapm_widget *w;
3221 LIST_HEAD(down_list);
3222 int powerdown = 0;
3223
Jarkko Nikula97c866d2010-12-14 12:18:31 +02003224 list_for_each_entry(w, &dapm->card->widgets, list) {
3225 if (w->dapm != dapm)
3226 continue;
Mark Brown51737472009-06-22 13:16:51 +01003227 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00003228 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01003229 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01003230 powerdown = 1;
3231 }
3232 }
3233
3234 /* If there were no widgets to power down we're already in
3235 * standby.
3236 */
3237 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08003238 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00003239 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08003240 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01003241 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003242}
Mark Brown51737472009-06-22 13:16:51 +01003243
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003244/*
3245 * snd_soc_dapm_shutdown - callback for system shutdown
3246 */
3247void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3248{
3249 struct snd_soc_codec *codec;
3250
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003251 list_for_each_entry(codec, &card->codec_dev_list, list) {
3252 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08003253 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003254 }
Mark Brown51737472009-06-22 13:16:51 +01003255}
3256
Richard Purdie2b97eab2006-10-06 18:32:18 +02003257/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003258MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003259MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3260MODULE_LICENSE("GPL");