blob: 258326b031cfa115304cab000b3a5ff7eb852d91 [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
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080035#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020036#include <linux/delay.h>
37#include <linux/pm.h>
38#include <linux/bitops.h>
39#include <linux/platform_device.h>
40#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020041#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020043#include <sound/core.h>
44#include <sound/pcm.h>
45#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020046#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020047#include <sound/initval.h>
48
Mark Brown84e90932010-11-04 00:07:02 -040049#include <trace/events/asoc.h>
50
Mark Brownde02d072011-09-20 21:43:24 +010051#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
52
Richard Purdie2b97eab2006-10-06 18:32:18 +020053/* dapm power sequences - make this per codec in the future */
54static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010055 [snd_soc_dapm_pre] = 0,
56 [snd_soc_dapm_supply] = 1,
57 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010058 [snd_soc_dapm_aif_in] = 3,
59 [snd_soc_dapm_aif_out] = 3,
60 [snd_soc_dapm_mic] = 4,
61 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000062 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010063 [snd_soc_dapm_value_mux] = 5,
64 [snd_soc_dapm_dac] = 6,
65 [snd_soc_dapm_mixer] = 7,
66 [snd_soc_dapm_mixer_named_ctl] = 7,
67 [snd_soc_dapm_pga] = 8,
68 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060069 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010070 [snd_soc_dapm_hp] = 10,
71 [snd_soc_dapm_spk] = 10,
72 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020073};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000074
Richard Purdie2b97eab2006-10-06 18:32:18 +020075static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010076 [snd_soc_dapm_pre] = 0,
77 [snd_soc_dapm_adc] = 1,
78 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010079 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060080 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010081 [snd_soc_dapm_pga] = 4,
82 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010083 [snd_soc_dapm_mixer] = 5,
84 [snd_soc_dapm_dac] = 6,
85 [snd_soc_dapm_mic] = 7,
86 [snd_soc_dapm_micbias] = 8,
87 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000088 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010089 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010090 [snd_soc_dapm_aif_in] = 10,
91 [snd_soc_dapm_aif_out] = 10,
92 [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 Brown15e4c72f2008-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 Brown15e4c72f2008-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 Brown15e4c72f2008-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 Brown15e4c72f2008-07-02 11:51:20 +0100117 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200118
119 kfree(buf);
Mark Brown15e4c72f2008-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 Brown75c1f892011-10-04 22:28:08 +0100127static void 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}
135
Richard Purdie2b97eab2006-10-06 18:32:18 +0200136/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100137static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200138 const struct snd_soc_dapm_widget *_widget)
139{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100140 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200141}
142
Liam Girdwood48056082011-07-20 12:23:33 +0100143/* get snd_card from DAPM context */
144static inline struct snd_card *dapm_get_snd_card(
145 struct snd_soc_dapm_context *dapm)
146{
147 if (dapm->codec)
148 return dapm->codec->card->snd_card;
149 else if (dapm->platform)
150 return dapm->platform->card->snd_card;
151 else
152 BUG();
153
154 /* unreachable */
155 return NULL;
156}
157
158/* get soc_card from DAPM context */
159static inline struct snd_soc_card *dapm_get_soc_card(
160 struct snd_soc_dapm_context *dapm)
161{
162 if (dapm->codec)
163 return dapm->codec->card;
164 else if (dapm->platform)
165 return dapm->platform->card;
166 else
167 BUG();
168
169 /* unreachable */
170 return NULL;
171}
172
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100173static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
174{
175 if (w->codec)
176 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100177 else if (w->platform)
178 return snd_soc_platform_read(w->platform, reg);
179
180 dev_err(w->dapm->dev, "no valid widget read method\n");
181 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100182}
183
184static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
185{
186 if (w->codec)
187 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100188 else if (w->platform)
189 return snd_soc_platform_write(w->platform, reg, val);
190
191 dev_err(w->dapm->dev, "no valid widget write method\n");
192 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100193}
194
195static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
196 unsigned short reg, unsigned int mask, unsigned int value)
197{
198 int change;
199 unsigned int old, new;
200 int ret;
201
202 ret = soc_widget_read(w, reg);
203 if (ret < 0)
204 return ret;
205
206 old = ret;
207 new = (old & ~mask) | (value & mask);
208 change = old != new;
209 if (change) {
210 ret = soc_widget_write(w, reg, new);
211 if (ret < 0)
212 return ret;
213 }
214
215 return change;
216}
217
Mark Brown452c5ea2009-05-17 21:41:23 +0100218/**
219 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800220 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100221 * @level: level to configure
222 *
223 * Configure the bias (power) levels for the SoC audio device.
224 *
225 * Returns 0 for success else error.
226 */
Mark Browned5a4c42011-02-18 11:12:42 -0800227static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200228 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100229{
Mark Browned5a4c42011-02-18 11:12:42 -0800230 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100231 int ret = 0;
232
Mark Brown84e90932010-11-04 00:07:02 -0400233 trace_snd_soc_bias_level_start(card, level);
234
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000235 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100236 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100237 if (ret != 0)
238 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100239
Mark Browncc4c6702011-06-06 19:03:34 +0100240 if (dapm->codec) {
241 if (dapm->codec->driver->set_bias_level)
242 ret = dapm->codec->driver->set_bias_level(dapm->codec,
243 level);
244 else
245 dapm->bias_level = level;
246 }
Mark Brown171ec6b2011-06-06 18:15:19 +0100247 if (ret != 0)
248 goto out;
249
250 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100251 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100252out:
Mark Brown84e90932010-11-04 00:07:02 -0400253 trace_snd_soc_bias_level_done(card, level);
254
Mark Brown452c5ea2009-05-17 21:41:23 +0100255 return ret;
256}
257
Richard Purdie2b97eab2006-10-06 18:32:18 +0200258/* set up initial codec paths */
259static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
260 struct snd_soc_dapm_path *p, int i)
261{
262 switch (w->id) {
263 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000264 case snd_soc_dapm_mixer:
265 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200266 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100267 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600268 w->kcontrol_news[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400269 unsigned int reg = mc->reg;
270 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100271 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400272 unsigned int mask = (1 << fls(max)) - 1;
273 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200274
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100275 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200276 val = (val >> shift) & mask;
277
278 if ((invert && !val) || (!invert && val))
279 p->connect = 1;
280 else
281 p->connect = 0;
282 }
283 break;
284 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600285 struct soc_enum *e = (struct soc_enum *)
286 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200287 int val, item, bitmask;
288
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100289 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100290 ;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100291 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200292 item = (val >> e->shift_l) & (bitmask - 1);
293
294 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100295 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200296 if (!(strcmp(p->name, e->texts[i])) && item == i)
297 p->connect = 1;
298 }
299 }
300 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000301 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600302 struct soc_enum *e = (struct soc_enum *)
303 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000304
305 p->connect = 0;
306 /* since a virtual mux has no backing registers to
307 * decide which path to connect, it will try to match
308 * with the first enumeration. This is to ensure
309 * that the default mux choice (the first) will be
310 * correctly powered up during initialization.
311 */
312 if (!strcmp(p->name, e->texts[0]))
313 p->connect = 1;
314 }
315 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200316 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200317 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600318 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200319 int val, item;
320
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100321 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200322 val = (val >> e->shift_l) & e->mask;
323 for (item = 0; item < e->max; item++) {
324 if (val == e->values[item])
325 break;
326 }
327
328 p->connect = 0;
329 for (i = 0; i < e->max; i++) {
330 if (!(strcmp(p->name, e->texts[i])) && item == i)
331 p->connect = 1;
332 }
333 }
334 break;
Mark Brown56563102011-10-03 22:41:09 +0100335 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200336 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600337 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200338 case snd_soc_dapm_output:
339 case snd_soc_dapm_adc:
340 case snd_soc_dapm_input:
341 case snd_soc_dapm_dac:
342 case snd_soc_dapm_micbias:
343 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100344 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100345 case snd_soc_dapm_aif_in:
346 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200347 case snd_soc_dapm_hp:
348 case snd_soc_dapm_mic:
349 case snd_soc_dapm_spk:
350 case snd_soc_dapm_line:
Mark Brown56563102011-10-03 22:41:09 +0100351 p->connect = 1;
352 break;
353 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200354 case snd_soc_dapm_pre:
355 case snd_soc_dapm_post:
356 p->connect = 0;
357 break;
358 }
359}
360
Mark Brown74b8f952009-06-06 11:26:15 +0100361/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200362static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200363 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
364 struct snd_soc_dapm_path *path, const char *control_name,
365 const struct snd_kcontrol_new *kcontrol)
366{
367 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
368 int i;
369
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100370 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200371 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200372 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200373 list_add(&path->list_sink, &dest->sources);
374 list_add(&path->list_source, &src->sinks);
375 path->name = (char*)e->texts[i];
376 dapm_set_path_status(dest, path, 0);
377 return 0;
378 }
379 }
380
381 return -ENODEV;
382}
383
Mark Brown74b8f952009-06-06 11:26:15 +0100384/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200385static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200386 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
387 struct snd_soc_dapm_path *path, const char *control_name)
388{
389 int i;
390
391 /* search for mixer kcontrol */
392 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600393 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200394 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200395 list_add(&path->list_sink, &dest->sources);
396 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600397 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200398 dapm_set_path_status(dest, path, i);
399 return 0;
400 }
401 }
402 return -ENODEV;
403}
404
Stephen Warrenaf468002011-04-28 17:38:01 -0600405static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600406 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600407 const struct snd_kcontrol_new *kcontrol_new,
408 struct snd_kcontrol **kcontrol)
409{
410 struct snd_soc_dapm_widget *w;
411 int i;
412
413 *kcontrol = NULL;
414
415 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600416 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
417 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600418 for (i = 0; i < w->num_kcontrols; i++) {
419 if (&w->kcontrol_news[i] == kcontrol_new) {
420 if (w->kcontrols)
421 *kcontrol = w->kcontrols[i];
422 return 1;
423 }
424 }
425 }
426
427 return 0;
428}
429
Richard Purdie2b97eab2006-10-06 18:32:18 +0200430/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200431static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200432{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200433 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200434 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000435 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200436 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000437 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000438 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600439 struct snd_soc_dapm_widget_list *wlist;
440 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000441
442 if (dapm->codec)
443 prefix = dapm->codec->name_prefix;
444 else
445 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200446
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000447 if (prefix)
448 prefix_len = strlen(prefix) + 1;
449 else
450 prefix_len = 0;
451
Richard Purdie2b97eab2006-10-06 18:32:18 +0200452 /* add kcontrol */
453 for (i = 0; i < w->num_kcontrols; i++) {
454
455 /* match name */
456 list_for_each_entry(path, &w->sources, list_sink) {
457
458 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600459 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200460 continue;
461
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200462 if (w->kcontrols[i]) {
463 path->kcontrol = w->kcontrols[i];
464 continue;
465 }
466
Stephen Warrenfafd2172011-04-28 17:38:00 -0600467 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
468 sizeof(struct snd_soc_dapm_widget *),
469 wlist = kzalloc(wlistsize, GFP_KERNEL);
470 if (wlist == NULL) {
471 dev_err(dapm->dev,
472 "asoc: can't allocate widget list for %s\n",
473 w->name);
474 return -ENOMEM;
475 }
476 wlist->num_widgets = 1;
477 wlist->widgets[0] = w;
478
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000479 /* add dapm control with long name.
480 * for dapm_mixer this is the concatenation of the
481 * mixer and kcontrol name.
482 * for dapm_mixer_named_ctl this is simply the
483 * kcontrol name.
484 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600485 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000486 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000487 name_len += 1 + strlen(w->name);
488
Mark Brown219b93f2008-10-28 13:02:31 +0000489 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000490
Stephen Warrenfafd2172011-04-28 17:38:00 -0600491 if (path->long_name == NULL) {
492 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200493 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600494 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200495
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000496 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000497 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000498 /* The control will get a prefix from
499 * the control creation process but
500 * we're also using the same prefix
501 * for widgets so cut the prefix off
502 * the front of the widget name.
503 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000504 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000505 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600506 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000507 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000508 case snd_soc_dapm_mixer_named_ctl:
509 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600510 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000511 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000512 }
513
Mark Brown219b93f2008-10-28 13:02:31 +0000514 path->long_name[name_len - 1] = '\0';
515
Stephen Warrenfafd2172011-04-28 17:38:00 -0600516 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
517 wlist, path->long_name,
518 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200519 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200520 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200521 dev_err(dapm->dev,
522 "asoc: failed to add dapm kcontrol %s: %d\n",
523 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600524 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200525 kfree(path->long_name);
526 path->long_name = NULL;
527 return ret;
528 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600529 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200530 }
531 }
532 return ret;
533}
534
535/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200536static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200537{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200538 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200539 struct snd_soc_dapm_path *path = NULL;
540 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000541 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000542 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000543 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600544 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600545 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600546 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600547 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600548 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200549
Stephen Warrenaf468002011-04-28 17:38:01 -0600550 if (w->num_kcontrols != 1) {
551 dev_err(dapm->dev,
552 "asoc: mux %s has incorrect number of controls\n",
553 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200554 return -EINVAL;
555 }
556
Stephen Warren1007da02011-05-26 09:57:33 -0600557 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600558 &kcontrol);
559 if (kcontrol) {
560 wlist = kcontrol->private_data;
561 wlistentries = wlist->num_widgets + 1;
562 } else {
563 wlist = NULL;
564 wlistentries = 1;
565 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600566 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600567 wlistentries * sizeof(struct snd_soc_dapm_widget *),
568 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600569 if (wlist == NULL) {
570 dev_err(dapm->dev,
571 "asoc: can't allocate widget list for %s\n", w->name);
572 return -ENOMEM;
573 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600574 wlist->num_widgets = wlistentries;
575 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600576
Stephen Warrenaf468002011-04-28 17:38:01 -0600577 if (!kcontrol) {
578 if (dapm->codec)
579 prefix = dapm->codec->name_prefix;
580 else
581 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000582
Stephen Warrenaf468002011-04-28 17:38:01 -0600583 if (shared) {
584 name = w->kcontrol_news[0].name;
585 prefix_len = 0;
586 } else {
587 name = w->name;
588 if (prefix)
589 prefix_len = strlen(prefix) + 1;
590 else
591 prefix_len = 0;
592 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000593
Stephen Warrenaf468002011-04-28 17:38:01 -0600594 /*
595 * The control will get a prefix from the control creation
596 * process but we're also using the same prefix for widgets so
597 * cut the prefix off the front of the widget name.
598 */
599 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
600 name + prefix_len, prefix);
601 ret = snd_ctl_add(card, kcontrol);
602 if (ret < 0) {
Mark Brown53daf202011-09-05 10:51:05 -0700603 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
604 w->name, ret);
Stephen Warrenaf468002011-04-28 17:38:01 -0600605 kfree(wlist);
606 return ret;
607 }
608 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200609
Stephen Warrenaf468002011-04-28 17:38:01 -0600610 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200611
Stephen Warrenfad59882011-04-28 17:37:59 -0600612 w->kcontrols[0] = kcontrol;
613
Richard Purdie2b97eab2006-10-06 18:32:18 +0200614 list_for_each_entry(path, &w->sources, list_sink)
615 path->kcontrol = kcontrol;
616
Stephen Warrenaf468002011-04-28 17:38:01 -0600617 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200618}
619
620/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200621static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200622{
Mark Browna6c65732010-03-03 17:45:21 +0000623 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200624 dev_err(w->dapm->dev,
625 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200626
Mark Browna6c65732010-03-03 17:45:21 +0000627 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200628}
629
630/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200631static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200632{
633 struct snd_soc_dapm_path *p;
634
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200635 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200636 p->walked = 0;
637}
638
Mark Brown9949788b2010-05-07 20:24:05 +0100639/* We implement power down on suspend by checking the power state of
640 * the ALSA card - when we are suspending the ALSA state for the card
641 * is set to D3.
642 */
643static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
644{
Mark Brown12ea2c72011-03-02 18:17:32 +0000645 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown9949788b2010-05-07 20:24:05 +0100646
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000647 switch (level) {
Mark Brown9949788b2010-05-07 20:24:05 +0100648 case SNDRV_CTL_POWER_D3hot:
649 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100650 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200651 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
652 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100653 return widget->ignore_suspend;
Mark Brown9949788b2010-05-07 20:24:05 +0100654 default:
655 return 1;
656 }
657}
658
Richard Purdie2b97eab2006-10-06 18:32:18 +0200659/*
660 * Recursively check for a completed path to an active or physically connected
661 * output widget. Returns number of complete paths.
662 */
663static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
664{
665 struct snd_soc_dapm_path *path;
666 int con = 0;
667
Mark Brown024dc072011-10-09 11:52:05 +0100668 if (widget->outputs >= 0)
669 return widget->outputs;
670
Mark Brownde02d072011-09-20 21:43:24 +0100671 DAPM_UPDATE_STAT(widget, path_checks);
672
Mark Brown246d0a12009-04-22 18:24:55 +0100673 if (widget->id == snd_soc_dapm_supply)
674 return 0;
675
Mark Brown010ff262009-08-17 17:39:22 +0100676 switch (widget->id) {
677 case snd_soc_dapm_adc:
678 case snd_soc_dapm_aif_out:
Mark Brown024dc072011-10-09 11:52:05 +0100679 if (widget->active) {
680 widget->outputs = snd_soc_dapm_suspend_check(widget);
681 return widget->outputs;
682 }
Mark Brown010ff262009-08-17 17:39:22 +0100683 default:
684 break;
685 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200686
687 if (widget->connected) {
688 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100689 if (widget->id == snd_soc_dapm_output && !widget->ext) {
690 widget->outputs = snd_soc_dapm_suspend_check(widget);
691 return widget->outputs;
692 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200693
694 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100695 if (widget->id == snd_soc_dapm_hp ||
696 widget->id == snd_soc_dapm_spk ||
697 (widget->id == snd_soc_dapm_line &&
698 !list_empty(&widget->sources))) {
699 widget->outputs = snd_soc_dapm_suspend_check(widget);
700 return widget->outputs;
701 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200702 }
703
704 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e02011-09-21 18:19:14 +0100705 DAPM_UPDATE_STAT(widget, neighbour_checks);
706
Mark Brownbf3a9e12011-06-13 16:42:29 +0100707 if (path->weak)
708 continue;
709
Richard Purdie2b97eab2006-10-06 18:32:18 +0200710 if (path->walked)
711 continue;
712
713 if (path->sink && path->connect) {
714 path->walked = 1;
715 con += is_connected_output_ep(path->sink);
716 }
717 }
718
Mark Brown024dc072011-10-09 11:52:05 +0100719 widget->outputs = con;
720
Richard Purdie2b97eab2006-10-06 18:32:18 +0200721 return con;
722}
723
724/*
725 * Recursively check for a completed path to an active or physically connected
726 * input widget. Returns number of complete paths.
727 */
728static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
729{
730 struct snd_soc_dapm_path *path;
731 int con = 0;
732
Mark Brown024dc072011-10-09 11:52:05 +0100733 if (widget->inputs >= 0)
734 return widget->inputs;
735
Mark Brownde02d072011-09-20 21:43:24 +0100736 DAPM_UPDATE_STAT(widget, path_checks);
737
Mark Brown246d0a12009-04-22 18:24:55 +0100738 if (widget->id == snd_soc_dapm_supply)
739 return 0;
740
Richard Purdie2b97eab2006-10-06 18:32:18 +0200741 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100742 switch (widget->id) {
743 case snd_soc_dapm_dac:
744 case snd_soc_dapm_aif_in:
Mark Brown024dc072011-10-09 11:52:05 +0100745 if (widget->active) {
746 widget->inputs = snd_soc_dapm_suspend_check(widget);
747 return widget->inputs;
748 }
Mark Brown010ff262009-08-17 17:39:22 +0100749 default:
750 break;
751 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200752
753 if (widget->connected) {
754 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100755 if (widget->id == snd_soc_dapm_input && !widget->ext) {
756 widget->inputs = snd_soc_dapm_suspend_check(widget);
757 return widget->inputs;
758 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200759
760 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100761 if (widget->id == snd_soc_dapm_vmid) {
762 widget->inputs = snd_soc_dapm_suspend_check(widget);
763 return widget->inputs;
764 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200765
766 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300767 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100768 (widget->id == snd_soc_dapm_line &&
769 !list_empty(&widget->sinks))) {
770 widget->inputs = snd_soc_dapm_suspend_check(widget);
771 return widget->inputs;
772 }
773
Richard Purdie2b97eab2006-10-06 18:32:18 +0200774 }
775
776 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e02011-09-21 18:19:14 +0100777 DAPM_UPDATE_STAT(widget, neighbour_checks);
778
Mark Brownbf3a9e12011-06-13 16:42:29 +0100779 if (path->weak)
780 continue;
781
Richard Purdie2b97eab2006-10-06 18:32:18 +0200782 if (path->walked)
783 continue;
784
785 if (path->source && path->connect) {
786 path->walked = 1;
787 con += is_connected_input_ep(path->source);
788 }
789 }
790
Mark Brown024dc072011-10-09 11:52:05 +0100791 widget->inputs = con;
792
Richard Purdie2b97eab2006-10-06 18:32:18 +0200793 return con;
794}
795
796/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300797 * Handler for generic register modifier widget.
798 */
799int dapm_reg_event(struct snd_soc_dapm_widget *w,
800 struct snd_kcontrol *kcontrol, int event)
801{
802 unsigned int val;
803
804 if (SND_SOC_DAPM_EVENT_ON(event))
805 val = w->on_val;
806 else
807 val = w->off_val;
808
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100809 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300810 w->mask << w->shift, val << w->shift);
811
812 return 0;
813}
Mark Brown11589412008-07-29 11:42:23 +0100814EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300815
Mark Brownd805002b2011-09-28 18:28:23 +0100816static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
817{
Mark Brown9b8a83b2011-10-04 22:15:59 +0100818 if (w->power_checked)
819 return w->new_power;
820
Mark Brownd805002b2011-09-28 18:28:23 +0100821 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +0100822 w->new_power = 1;
Mark Brownd805002b2011-09-28 18:28:23 +0100823 else
Mark Brown9b8a83b2011-10-04 22:15:59 +0100824 w->new_power = w->power_check(w);
825
826 w->power_checked = true;
827
828 return w->new_power;
Mark Brownd805002b2011-09-28 18:28:23 +0100829}
830
Mark Browncd0f2d42009-04-20 16:56:59 +0100831/* Generic check to see if a widget should be powered.
832 */
833static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
834{
835 int in, out;
836
Mark Brownde02d072011-09-20 21:43:24 +0100837 DAPM_UPDATE_STAT(w, power_checks);
838
Mark Browncd0f2d42009-04-20 16:56:59 +0100839 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200840 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100841 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200842 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100843 return out != 0 && in != 0;
844}
845
Mark Brown6ea31b92009-04-20 17:15:41 +0100846/* Check to see if an ADC has power */
847static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
848{
849 int in;
850
Mark Brownde02d072011-09-20 21:43:24 +0100851 DAPM_UPDATE_STAT(w, power_checks);
852
Mark Brown6ea31b92009-04-20 17:15:41 +0100853 if (w->active) {
854 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200855 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100856 return in != 0;
857 } else {
858 return dapm_generic_check_power(w);
859 }
860}
861
862/* Check to see if a DAC has power */
863static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
864{
865 int out;
866
Mark Brownde02d072011-09-20 21:43:24 +0100867 DAPM_UPDATE_STAT(w, power_checks);
868
Mark Brown6ea31b92009-04-20 17:15:41 +0100869 if (w->active) {
870 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200871 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100872 return out != 0;
873 } else {
874 return dapm_generic_check_power(w);
875 }
876}
877
Mark Brown246d0a12009-04-22 18:24:55 +0100878/* Check to see if a power supply is needed */
879static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
880{
881 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +0100882
Mark Brownde02d072011-09-20 21:43:24 +0100883 DAPM_UPDATE_STAT(w, power_checks);
884
Mark Brown246d0a12009-04-22 18:24:55 +0100885 /* Check if one of our outputs is connected */
886 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +0100887 DAPM_UPDATE_STAT(w, neighbour_checks);
888
Mark Brownbf3a9e12011-06-13 16:42:29 +0100889 if (path->weak)
890 continue;
891
Mark Brown215edda2009-09-08 18:59:05 +0100892 if (path->connected &&
893 !path->connected(path->source, path->sink))
894 continue;
895
Mark Brown30173582011-02-11 11:42:19 +0000896 if (!path->sink)
897 continue;
898
Mark Brownf68d7e12011-10-04 22:57:50 +0100899 if (dapm_widget_power_check(path->sink))
900 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +0100901 }
902
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200903 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100904
Mark Brownf68d7e12011-10-04 22:57:50 +0100905 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +0100906}
907
Mark Brown35c64bc2011-09-28 18:23:53 +0100908static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
909{
910 return 1;
911}
912
Mark Brown38357ab2009-06-06 19:03:23 +0100913static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
914 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000915 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000916{
Mark Brown828a8422011-01-15 13:14:30 +0000917 int *sort;
918
919 if (power_up)
920 sort = dapm_up_seq;
921 else
922 sort = dapm_down_seq;
923
Mark Brown38357ab2009-06-06 19:03:23 +0100924 if (sort[a->id] != sort[b->id])
925 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000926 if (a->subseq != b->subseq) {
927 if (power_up)
928 return a->subseq - b->subseq;
929 else
930 return b->subseq - a->subseq;
931 }
Mark Brownb22ead22009-06-07 12:51:26 +0100932 if (a->reg != b->reg)
933 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000934 if (a->dapm != b->dapm)
935 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000936
Mark Brown38357ab2009-06-06 19:03:23 +0100937 return 0;
938}
Mark Brown42aa3412009-03-01 19:21:10 +0000939
Mark Brown38357ab2009-06-06 19:03:23 +0100940/* Insert a widget in order into a DAPM power sequence. */
941static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
942 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000943 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100944{
945 struct snd_soc_dapm_widget *w;
946
947 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000948 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100949 list_add_tail(&new_widget->power_list, &w->power_list);
950 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000951 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100952
Mark Brown38357ab2009-06-06 19:03:23 +0100953 list_add_tail(&new_widget->power_list, list);
954}
Mark Brown42aa3412009-03-01 19:21:10 +0000955
Mark Brown68f89ad2010-11-03 23:51:49 -0400956static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
957 struct snd_soc_dapm_widget *w, int event)
958{
959 struct snd_soc_card *card = dapm->card;
960 const char *ev_name;
961 int power, ret;
962
963 switch (event) {
964 case SND_SOC_DAPM_PRE_PMU:
965 ev_name = "PRE_PMU";
966 power = 1;
967 break;
968 case SND_SOC_DAPM_POST_PMU:
969 ev_name = "POST_PMU";
970 power = 1;
971 break;
972 case SND_SOC_DAPM_PRE_PMD:
973 ev_name = "PRE_PMD";
974 power = 0;
975 break;
976 case SND_SOC_DAPM_POST_PMD:
977 ev_name = "POST_PMD";
978 power = 0;
979 break;
980 default:
981 BUG();
982 return;
983 }
984
985 if (w->power != power)
986 return;
987
988 if (w->event && (w->event_flags & event)) {
989 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
990 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400991 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400992 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400993 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400994 if (ret < 0)
995 pr_err("%s: %s event failed: %d\n",
996 ev_name, w->name, ret);
997 }
998}
999
Mark Brownb22ead22009-06-07 12:51:26 +01001000/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001001static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001002 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001003{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001004 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001005 struct snd_soc_dapm_widget *w;
1006 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001007 unsigned int value = 0;
1008 unsigned int mask = 0;
1009 unsigned int cur_mask;
1010
1011 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1012 power_list)->reg;
1013
1014 list_for_each_entry(w, pending, power_list) {
1015 cur_mask = 1 << w->shift;
1016 BUG_ON(reg != w->reg);
1017
1018 if (w->invert)
1019 power = !w->power;
1020 else
1021 power = w->power;
1022
1023 mask |= cur_mask;
1024 if (power)
1025 value |= cur_mask;
1026
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001027 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001028 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1029 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001030
Mark Brown68f89ad2010-11-03 23:51:49 -04001031 /* Check for events */
1032 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1033 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001034 }
1035
Mark Brown81628102009-06-07 13:21:24 +01001036 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001037 /* Any widget will do, they should all be updating the
1038 * same register.
1039 */
1040 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1041 power_list);
1042
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001043 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001044 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001045 value, mask, reg, card->pop_time);
1046 pop_wait(card->pop_time);
Liam Girdwood0445bdf2011-06-13 19:37:36 +01001047 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001048 }
1049
1050 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001051 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1052 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001053 }
Mark Brown42aa3412009-03-01 19:21:10 +00001054}
1055
Mark Brownb22ead22009-06-07 12:51:26 +01001056/* Apply a DAPM power sequence.
1057 *
1058 * We walk over a pre-sorted list of widgets to apply power to. In
1059 * order to minimise the number of writes to the device required
1060 * multiple widgets will be updated in a single write where possible.
1061 * Currently anything that requires more than a single write is not
1062 * handled.
1063 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001064static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001065 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001066{
1067 struct snd_soc_dapm_widget *w, *n;
1068 LIST_HEAD(pending);
1069 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001070 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001071 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001072 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001073 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001074 int *sort;
1075
1076 if (power_up)
1077 sort = dapm_up_seq;
1078 else
1079 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001080
Mark Brownb22ead22009-06-07 12:51:26 +01001081 list_for_each_entry_safe(w, n, list, power_list) {
1082 ret = 0;
1083
1084 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001085 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001086 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001087 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001088 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001089
Mark Brown474b62d2011-01-18 16:14:44 +00001090 if (cur_dapm && cur_dapm->seq_notifier) {
1091 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1092 if (sort[i] == cur_sort)
1093 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001094 i,
1095 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001096 }
1097
Mark Brownb22ead22009-06-07 12:51:26 +01001098 INIT_LIST_HEAD(&pending);
1099 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001100 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001101 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001102 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001103 }
1104
Mark Brown163cac02009-06-07 10:12:52 +01001105 switch (w->id) {
1106 case snd_soc_dapm_pre:
1107 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001108 list_for_each_entry_safe_continue(w, n, list,
1109 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001110
Mark Brownb22ead22009-06-07 12:51:26 +01001111 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001112 ret = w->event(w,
1113 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001114 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001115 ret = w->event(w,
1116 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001117 break;
1118
1119 case snd_soc_dapm_post:
1120 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001121 list_for_each_entry_safe_continue(w, n, list,
1122 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001123
Mark Brownb22ead22009-06-07 12:51:26 +01001124 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001125 ret = w->event(w,
1126 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001127 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001128 ret = w->event(w,
1129 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001130 break;
1131
Mark Brown163cac02009-06-07 10:12:52 +01001132 default:
Mark Brown81628102009-06-07 13:21:24 +01001133 /* Queue it up for application */
1134 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001135 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001136 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001137 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001138 list_move(&w->power_list, &pending);
1139 break;
Mark Brown163cac02009-06-07 10:12:52 +01001140 }
Mark Brownb22ead22009-06-07 12:51:26 +01001141
1142 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001143 dev_err(w->dapm->dev,
1144 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001145 }
Mark Brownb22ead22009-06-07 12:51:26 +01001146
1147 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001148 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001149
1150 if (cur_dapm && cur_dapm->seq_notifier) {
1151 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1152 if (sort[i] == cur_sort)
1153 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001154 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001155 }
Mark Brown163cac02009-06-07 10:12:52 +01001156}
1157
Mark Brown97404f22010-12-14 16:13:57 +00001158static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1159{
1160 struct snd_soc_dapm_update *update = dapm->update;
1161 struct snd_soc_dapm_widget *w;
1162 int ret;
1163
1164 if (!update)
1165 return;
1166
1167 w = update->widget;
1168
1169 if (w->event &&
1170 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1171 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1172 if (ret != 0)
1173 pr_err("%s DAPM pre-event failed: %d\n",
1174 w->name, ret);
1175 }
1176
1177 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1178 update->val);
1179 if (ret < 0)
1180 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1181
1182 if (w->event &&
1183 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1184 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1185 if (ret != 0)
1186 pr_err("%s DAPM post-event failed: %d\n",
1187 w->name, ret);
1188 }
1189}
1190
Mark Brown9d0624a2011-02-18 11:49:43 -08001191/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1192 * they're changing state.
1193 */
1194static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1195{
1196 struct snd_soc_dapm_context *d = data;
1197 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001198
Mark Brown56fba412011-06-04 11:25:10 +01001199 /* If we're off and we're not supposed to be go into STANDBY */
1200 if (d->bias_level == SND_SOC_BIAS_OFF &&
1201 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001202 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1203 if (ret != 0)
1204 dev_err(d->dev,
1205 "Failed to turn on bias: %d\n", ret);
1206 }
1207
Mark Brown56fba412011-06-04 11:25:10 +01001208 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1209 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001210 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1211 if (ret != 0)
1212 dev_err(d->dev,
1213 "Failed to prepare bias: %d\n", ret);
1214 }
1215}
1216
1217/* Async callback run prior to DAPM sequences - brings to their final
1218 * state.
1219 */
1220static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1221{
1222 struct snd_soc_dapm_context *d = data;
1223 int ret;
1224
1225 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001226 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1227 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1228 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001229 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1230 if (ret != 0)
1231 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1232 ret);
1233 }
1234
1235 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001236 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1237 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001238 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1239 if (ret != 0)
1240 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1241 }
1242
1243 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001244 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1245 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001246 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1247 if (ret != 0)
1248 dev_err(d->dev, "Failed to apply active bias: %d\n",
1249 ret);
1250 }
1251}
Mark Brown97404f22010-12-14 16:13:57 +00001252
Mark Brownfe4fda52011-10-03 22:36:57 +01001253static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1254 bool power, bool connect)
1255{
1256 /* If a connection is being made or broken then that update
1257 * will have marked the peer dirty, otherwise the widgets are
1258 * not connected and this update has no impact. */
1259 if (!connect)
1260 return;
1261
1262 /* If the peer is already in the state we're moving to then we
1263 * won't have an impact on it. */
1264 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001265 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001266}
1267
Mark Brown05623c42011-09-28 17:02:31 +01001268static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1269 struct list_head *up_list,
1270 struct list_head *down_list)
1271{
Mark Browndb432b42011-10-03 21:06:40 +01001272 struct snd_soc_dapm_path *path;
1273
Mark Brown05623c42011-09-28 17:02:31 +01001274 if (w->power == power)
1275 return;
1276
1277 trace_snd_soc_dapm_widget_power(w, power);
1278
Mark Browndb432b42011-10-03 21:06:40 +01001279 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001280 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001281 */
1282 list_for_each_entry(path, &w->sources, list_sink) {
1283 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001284 dapm_widget_set_peer_power(path->source, power,
1285 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001286 }
1287 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001288 switch (w->id) {
1289 case snd_soc_dapm_supply:
1290 /* Supplies can't affect their outputs, only their inputs */
1291 break;
1292 default:
1293 list_for_each_entry(path, &w->sinks, list_source) {
1294 if (path->sink) {
1295 dapm_widget_set_peer_power(path->sink, power,
1296 path->connect);
1297 }
Mark Browndb432b42011-10-03 21:06:40 +01001298 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001299 break;
Mark Browndb432b42011-10-03 21:06:40 +01001300 }
1301
Mark Brown05623c42011-09-28 17:02:31 +01001302 if (power)
1303 dapm_seq_insert(w, up_list, true);
1304 else
1305 dapm_seq_insert(w, down_list, false);
1306
1307 w->power = power;
1308}
1309
Mark Brown7c81beb2011-09-20 22:22:32 +01001310static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1311 struct list_head *up_list,
1312 struct list_head *down_list)
1313{
Mark Brown7c81beb2011-09-20 22:22:32 +01001314 int power;
1315
1316 switch (w->id) {
1317 case snd_soc_dapm_pre:
1318 dapm_seq_insert(w, down_list, false);
1319 break;
1320 case snd_soc_dapm_post:
1321 dapm_seq_insert(w, up_list, true);
1322 break;
1323
1324 default:
Mark Brownd805002b2011-09-28 18:28:23 +01001325 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001326
Mark Brown05623c42011-09-28 17:02:31 +01001327 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001328 break;
1329 }
1330}
1331
Mark Brown42aa3412009-03-01 19:21:10 +00001332/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001333 * Scan each dapm widget for complete audio path.
1334 * A complete path is a route that has valid endpoints i.e.:-
1335 *
1336 * o DAC to output pin.
1337 * o Input Pin to ADC.
1338 * o Input pin to Output pin (bypass, sidetone)
1339 * o DAC to ADC (loopback).
1340 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001341static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001342{
Mark Brown12ea2c72011-03-02 18:17:32 +00001343 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001344 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001345 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001346 LIST_HEAD(up_list);
1347 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001348 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001349 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001350
Mark Brown84e90932010-11-04 00:07:02 -04001351 trace_snd_soc_dapm_start(card);
1352
Mark Brown56fba412011-06-04 11:25:10 +01001353 list_for_each_entry(d, &card->dapm_list, list) {
1354 if (d->n_widgets || d->codec == NULL) {
1355 if (d->idle_bias_off)
1356 d->target_bias_level = SND_SOC_BIAS_OFF;
1357 else
1358 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1359 }
1360 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001361
Mark Brownde02d072011-09-20 21:43:24 +01001362 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1363
Mark Brown9b8a83b2011-10-04 22:15:59 +01001364 list_for_each_entry(w, &card->widgets, list) {
1365 w->power_checked = false;
Mark Brown024dc072011-10-09 11:52:05 +01001366 w->inputs = -1;
1367 w->outputs = -1;
Mark Brown9b8a83b2011-10-04 22:15:59 +01001368 }
1369
Mark Brown6d3ddc82009-05-16 17:47:29 +01001370 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001371 * lists indicating if they should be powered up or down. We
1372 * only check widgets that have been flagged as dirty but note
1373 * that new widgets may be added to the dirty list while we
1374 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001375 */
Mark Browndb432b42011-10-03 21:06:40 +01001376 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001377 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001378 }
1379
Mark Brownf9de6d72011-09-28 17:19:47 +01001380 list_for_each_entry(w, &card->widgets, list) {
Mark Browndb432b42011-10-03 21:06:40 +01001381 list_del_init(&w->dirty);
1382
Mark Brownf9de6d72011-09-28 17:19:47 +01001383 if (w->power) {
1384 d = w->dapm;
1385
1386 /* Supplies and micbiases only bring the
1387 * context up to STANDBY as unless something
1388 * else is active and passing audio they
1389 * generally don't require full power.
1390 */
1391 switch (w->id) {
1392 case snd_soc_dapm_supply:
1393 case snd_soc_dapm_micbias:
1394 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1395 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1396 break;
1397 default:
1398 d->target_bias_level = SND_SOC_BIAS_ON;
1399 break;
1400 }
1401 }
1402
1403 }
1404
Mark Brownb14b76a52009-08-17 11:55:38 +01001405 /* If there are no DAPM widgets then try to figure out power from the
1406 * event type.
1407 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001408 if (!dapm->n_widgets) {
Mark Brownb14b76a52009-08-17 11:55:38 +01001409 switch (event) {
1410 case SND_SOC_DAPM_STREAM_START:
1411 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001412 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a52009-08-17 11:55:38 +01001413 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001414 case SND_SOC_DAPM_STREAM_STOP:
Mark Brown56fba412011-06-04 11:25:10 +01001415 if (dapm->codec->active)
1416 dapm->target_bias_level = SND_SOC_BIAS_ON;
1417 else
1418 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001419 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001420 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001421 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001422 break;
Mark Brownb14b76a52009-08-17 11:55:38 +01001423 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001424 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001425 break;
Mark Brownb14b76a52009-08-17 11:55:38 +01001426 default:
1427 break;
1428 }
1429 }
1430
Mark Brown85a843c2011-09-21 21:29:47 +01001431 /* Force all contexts in the card to the same bias state if
1432 * they're not ground referenced.
1433 */
Mark Brown56fba412011-06-04 11:25:10 +01001434 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001435 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001436 if (d->target_bias_level > bias)
1437 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001438 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001439 if (!d->idle_bias_off)
1440 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001441
Mark Brownde02d072011-09-20 21:43:24 +01001442 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001443
Mark Brown9d0624a2011-02-18 11:49:43 -08001444 /* Run all the bias changes in parallel */
1445 list_for_each_entry(d, &dapm->card->dapm_list, list)
1446 async_schedule_domain(dapm_pre_sequence_async, d,
1447 &async_domain);
1448 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001449
Mark Brown6d3ddc82009-05-16 17:47:29 +01001450 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001451 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001452
Mark Brown97404f22010-12-14 16:13:57 +00001453 dapm_widget_update(dapm);
1454
Mark Brown6d3ddc82009-05-16 17:47:29 +01001455 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001456 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001457
Mark Brown9d0624a2011-02-18 11:49:43 -08001458 /* Run all the bias changes in parallel */
1459 list_for_each_entry(d, &dapm->card->dapm_list, list)
1460 async_schedule_domain(dapm_post_sequence_async, d,
1461 &async_domain);
1462 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001463
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001464 pop_dbg(dapm->dev, card->pop_time,
1465 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001466 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001467
Mark Brown84e90932010-11-04 00:07:02 -04001468 trace_snd_soc_dapm_done(card);
1469
Mark Brown42aa3412009-03-01 19:21:10 +00001470 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001471}
1472
Mark Brown79fb9382009-08-21 16:38:13 +01001473#ifdef CONFIG_DEBUG_FS
1474static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1475{
1476 file->private_data = inode->i_private;
1477 return 0;
1478}
1479
1480static ssize_t dapm_widget_power_read_file(struct file *file,
1481 char __user *user_buf,
1482 size_t count, loff_t *ppos)
1483{
1484 struct snd_soc_dapm_widget *w = file->private_data;
1485 char *buf;
1486 int in, out;
1487 ssize_t ret;
1488 struct snd_soc_dapm_path *p = NULL;
1489
1490 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1491 if (!buf)
1492 return -ENOMEM;
1493
1494 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001495 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001496 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001497 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001498
Mark Brownd033c362009-12-04 15:25:56 +00001499 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001500 w->name, w->power ? "On" : "Off", in, out);
1501
Mark Brownd033c362009-12-04 15:25:56 +00001502 if (w->reg >= 0)
1503 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1504 " - R%d(0x%x) bit %d",
1505 w->reg, w->reg, w->shift);
1506
1507 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1508
Mark Brown3eef08b2009-09-14 16:49:00 +01001509 if (w->sname)
1510 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1511 w->sname,
1512 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001513
1514 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001515 if (p->connected && !p->connected(w, p->sink))
1516 continue;
1517
Mark Brown79fb9382009-08-21 16:38:13 +01001518 if (p->connect)
1519 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001520 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001521 p->name ? p->name : "static",
1522 p->source->name);
1523 }
1524 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001525 if (p->connected && !p->connected(w, p->sink))
1526 continue;
1527
Mark Brown79fb9382009-08-21 16:38:13 +01001528 if (p->connect)
1529 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001530 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001531 p->name ? p->name : "static",
1532 p->sink->name);
1533 }
1534
1535 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1536
1537 kfree(buf);
1538 return ret;
1539}
1540
1541static const struct file_operations dapm_widget_power_fops = {
1542 .open = dapm_widget_power_open_file,
1543 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001544 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001545};
1546
Mark Brownef49e4f2011-04-04 20:48:13 +09001547static int dapm_bias_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_bias_read_file(struct file *file, char __user *user_buf,
1554 size_t count, loff_t *ppos)
1555{
1556 struct snd_soc_dapm_context *dapm = file->private_data;
1557 char *level;
1558
1559 switch (dapm->bias_level) {
1560 case SND_SOC_BIAS_ON:
1561 level = "On\n";
1562 break;
1563 case SND_SOC_BIAS_PREPARE:
1564 level = "Prepare\n";
1565 break;
1566 case SND_SOC_BIAS_STANDBY:
1567 level = "Standby\n";
1568 break;
1569 case SND_SOC_BIAS_OFF:
1570 level = "Off\n";
1571 break;
1572 default:
1573 BUG();
1574 level = "Unknown\n";
1575 break;
1576 }
1577
1578 return simple_read_from_buffer(user_buf, count, ppos, level,
1579 strlen(level));
1580}
1581
1582static const struct file_operations dapm_bias_fops = {
1583 .open = dapm_bias_open_file,
1584 .read = dapm_bias_read_file,
1585 .llseek = default_llseek,
1586};
1587
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001588void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1589 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001590{
Mark Brown79fb9382009-08-21 16:38:13 +01001591 struct dentry *d;
1592
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001593 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1594
1595 if (!dapm->debugfs_dapm) {
1596 printk(KERN_WARNING
1597 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001598 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001599 }
Mark Brown79fb9382009-08-21 16:38:13 +01001600
Mark Brownef49e4f2011-04-04 20:48:13 +09001601 d = debugfs_create_file("bias_level", 0444,
1602 dapm->debugfs_dapm, dapm,
1603 &dapm_bias_fops);
1604 if (!d)
1605 dev_warn(dapm->dev,
1606 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001607}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001608
1609static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1610{
1611 struct snd_soc_dapm_context *dapm = w->dapm;
1612 struct dentry *d;
1613
1614 if (!dapm->debugfs_dapm || !w->name)
1615 return;
1616
1617 d = debugfs_create_file(w->name, 0444,
1618 dapm->debugfs_dapm, w,
1619 &dapm_widget_power_fops);
1620 if (!d)
1621 dev_warn(w->dapm->dev,
1622 "ASoC: Failed to create %s debugfs file\n",
1623 w->name);
1624}
1625
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001626static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1627{
1628 debugfs_remove_recursive(dapm->debugfs_dapm);
1629}
1630
Mark Brown79fb9382009-08-21 16:38:13 +01001631#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001632void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1633 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001634{
1635}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001636
1637static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1638{
1639}
1640
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001641static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1642{
1643}
1644
Mark Brown79fb9382009-08-21 16:38:13 +01001645#endif
1646
Richard Purdie2b97eab2006-10-06 18:32:18 +02001647/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001648static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001649 struct snd_kcontrol *kcontrol, int change,
1650 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001651{
1652 struct snd_soc_dapm_path *path;
1653 int found = 0;
1654
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001655 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001656 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001657 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001658 return -ENODEV;
1659
Mark Brown3a655772009-10-05 17:23:30 +01001660 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001661 return 0;
1662
1663 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001664 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001665 if (path->kcontrol != kcontrol)
1666 continue;
1667
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001668 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001669 continue;
1670
1671 found = 1;
1672 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001673 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001674 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001675 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001676 } else {
1677 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001678 dapm_mark_dirty(path->source,
1679 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001680 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001681 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001682 }
1683
Mark Browndb432b42011-10-03 21:06:40 +01001684 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001685 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001686 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001687 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001688
1689 return 0;
1690}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001691
Milan plzik1b075e32008-01-10 14:39:46 +01001692/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001693static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001694 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001695{
1696 struct snd_soc_dapm_path *path;
1697 int found = 0;
1698
Milan plzik1b075e32008-01-10 14:39:46 +01001699 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001700 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001701 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001702 return -ENODEV;
1703
Richard Purdie2b97eab2006-10-06 18:32:18 +02001704 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001705 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001706 if (path->kcontrol != kcontrol)
1707 continue;
1708
1709 /* found, now check type */
1710 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001711 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001712 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001713 }
1714
Mark Browndb432b42011-10-03 21:06:40 +01001715 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001716 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001717 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001718 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001719
1720 return 0;
1721}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001722
1723/* show dapm widget status in sys fs */
1724static ssize_t dapm_widget_show(struct device *dev,
1725 struct device_attribute *attr, char *buf)
1726{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001727 struct snd_soc_pcm_runtime *rtd =
1728 container_of(dev, struct snd_soc_pcm_runtime, dev);
1729 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001730 struct snd_soc_dapm_widget *w;
1731 int count = 0;
1732 char *state = "not set";
1733
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001734 list_for_each_entry(w, &codec->card->widgets, list) {
1735 if (w->dapm != &codec->dapm)
1736 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001737
1738 /* only display widgets that burnm power */
1739 switch (w->id) {
1740 case snd_soc_dapm_hp:
1741 case snd_soc_dapm_mic:
1742 case snd_soc_dapm_spk:
1743 case snd_soc_dapm_line:
1744 case snd_soc_dapm_micbias:
1745 case snd_soc_dapm_dac:
1746 case snd_soc_dapm_adc:
1747 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001748 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001749 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001750 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001751 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001752 if (w->name)
1753 count += sprintf(buf + count, "%s: %s\n",
1754 w->name, w->power ? "On":"Off");
1755 break;
1756 default:
1757 break;
1758 }
1759 }
1760
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001761 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001762 case SND_SOC_BIAS_ON:
1763 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001764 break;
Mark Brown0be98982008-05-19 12:31:28 +02001765 case SND_SOC_BIAS_PREPARE:
1766 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001767 break;
Mark Brown0be98982008-05-19 12:31:28 +02001768 case SND_SOC_BIAS_STANDBY:
1769 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001770 break;
Mark Brown0be98982008-05-19 12:31:28 +02001771 case SND_SOC_BIAS_OFF:
1772 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001773 break;
1774 }
1775 count += sprintf(buf + count, "PM State: %s\n", state);
1776
1777 return count;
1778}
1779
1780static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1781
1782int snd_soc_dapm_sys_add(struct device *dev)
1783{
Troy Kisky12ef1932008-10-13 17:42:14 -07001784 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001785}
1786
1787static void snd_soc_dapm_sys_remove(struct device *dev)
1788{
Mark Brownaef90842009-05-16 17:53:16 +01001789 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001790}
1791
1792/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001793static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001794{
1795 struct snd_soc_dapm_widget *w, *next_w;
1796 struct snd_soc_dapm_path *p, *next_p;
1797
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001798 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1799 if (w->dapm != dapm)
1800 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001801 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001802 /*
1803 * remove source and sink paths associated to this widget.
1804 * While removing the path, remove reference to it from both
1805 * source and sink widgets so that path is removed only once.
1806 */
1807 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1808 list_del(&p->list_sink);
1809 list_del(&p->list_source);
1810 list_del(&p->list);
1811 kfree(p->long_name);
1812 kfree(p);
1813 }
1814 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1815 list_del(&p->list_sink);
1816 list_del(&p->list_source);
1817 list_del(&p->list);
1818 kfree(p->long_name);
1819 kfree(p);
1820 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001821 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001822 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001823 kfree(w);
1824 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001825}
1826
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001827static struct snd_soc_dapm_widget *dapm_find_widget(
1828 struct snd_soc_dapm_context *dapm, const char *pin,
1829 bool search_other_contexts)
1830{
1831 struct snd_soc_dapm_widget *w;
1832 struct snd_soc_dapm_widget *fallback = NULL;
1833
1834 list_for_each_entry(w, &dapm->card->widgets, list) {
1835 if (!strcmp(w->name, pin)) {
1836 if (w->dapm == dapm)
1837 return w;
1838 else
1839 fallback = w;
1840 }
1841 }
1842
1843 if (search_other_contexts)
1844 return fallback;
1845
1846 return NULL;
1847}
1848
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001849static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001850 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001851{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001852 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001853
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001854 if (!w) {
1855 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1856 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001857 }
1858
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001859 w->connected = status;
1860 if (status == 0)
1861 w->force = 0;
Mark Brown75c1f892011-10-04 22:28:08 +01001862 dapm_mark_dirty(w, "pin configuration");
Mark Brown0d867332011-04-06 11:38:14 +09001863
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001864 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001865}
1866
Richard Purdie2b97eab2006-10-06 18:32:18 +02001867/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001868 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001869 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001870 *
1871 * Walks all dapm audio paths and powers widgets according to their
1872 * stream or path usage.
1873 *
1874 * Returns 0 for success.
1875 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001876int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001877{
Mark Brown4f4c0072011-10-07 14:29:19 +01001878 /*
1879 * Suppress early reports (eg, jacks syncing their state) to avoid
1880 * silly DAPM runs during card startup.
1881 */
1882 if (!dapm->card || !dapm->card->instantiated)
1883 return 0;
1884
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001885 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001886}
Liam Girdwooda5302182008-07-07 13:35:17 +01001887EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001888
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001889static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001890 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001891{
1892 struct snd_soc_dapm_path *path;
1893 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001894 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001895 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001896 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001897 const char *source;
1898 char prefixed_sink[80];
1899 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001900 int ret = 0;
1901
Mark Brown88e8b9a2011-03-02 18:18:24 +00001902 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001903 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1904 dapm->codec->name_prefix, route->sink);
1905 sink = prefixed_sink;
1906 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1907 dapm->codec->name_prefix, route->source);
1908 source = prefixed_source;
1909 } else {
1910 sink = route->sink;
1911 source = route->source;
1912 }
1913
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001914 /*
1915 * find src and dest widgets over all widgets but favor a widget from
1916 * current DAPM context
1917 */
1918 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001919 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001920 wtsink = w;
1921 if (w->dapm == dapm)
1922 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001923 continue;
1924 }
1925 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001926 wtsource = w;
1927 if (w->dapm == dapm)
1928 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001929 }
1930 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001931 /* use widget from another DAPM context if not found from this */
1932 if (!wsink)
1933 wsink = wtsink;
1934 if (!wsource)
1935 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001936
1937 if (wsource == NULL || wsink == NULL)
1938 return -ENODEV;
1939
1940 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1941 if (!path)
1942 return -ENOMEM;
1943
1944 path->source = wsource;
1945 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001946 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001947 INIT_LIST_HEAD(&path->list);
1948 INIT_LIST_HEAD(&path->list_source);
1949 INIT_LIST_HEAD(&path->list_sink);
1950
1951 /* check for external widgets */
1952 if (wsink->id == snd_soc_dapm_input) {
1953 if (wsource->id == snd_soc_dapm_micbias ||
1954 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001955 wsource->id == snd_soc_dapm_line ||
1956 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001957 wsink->ext = 1;
1958 }
1959 if (wsource->id == snd_soc_dapm_output) {
1960 if (wsink->id == snd_soc_dapm_spk ||
1961 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001962 wsink->id == snd_soc_dapm_line ||
1963 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001964 wsource->ext = 1;
1965 }
1966
1967 /* connect static paths */
1968 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001969 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001970 list_add(&path->list_sink, &wsink->sources);
1971 list_add(&path->list_source, &wsource->sinks);
1972 path->connect = 1;
1973 return 0;
1974 }
1975
1976 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001977 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001978 case snd_soc_dapm_adc:
1979 case snd_soc_dapm_dac:
1980 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001981 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001982 case snd_soc_dapm_input:
1983 case snd_soc_dapm_output:
1984 case snd_soc_dapm_micbias:
1985 case snd_soc_dapm_vmid:
1986 case snd_soc_dapm_pre:
1987 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001988 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001989 case snd_soc_dapm_aif_in:
1990 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001991 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001992 list_add(&path->list_sink, &wsink->sources);
1993 list_add(&path->list_source, &wsource->sinks);
1994 path->connect = 1;
1995 return 0;
1996 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001997 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001998 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001999 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002000 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002001 if (ret != 0)
2002 goto err;
2003 break;
2004 case snd_soc_dapm_switch:
2005 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002006 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002007 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002008 if (ret != 0)
2009 goto err;
2010 break;
2011 case snd_soc_dapm_hp:
2012 case snd_soc_dapm_mic:
2013 case snd_soc_dapm_line:
2014 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002015 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002016 list_add(&path->list_sink, &wsink->sources);
2017 list_add(&path->list_source, &wsource->sinks);
2018 path->connect = 0;
2019 return 0;
2020 }
2021 return 0;
2022
2023err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002024 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2025 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002026 kfree(path);
2027 return ret;
2028}
Mark Brown105f1c22008-05-13 14:52:19 +02002029
2030/**
Mark Brown105f1c22008-05-13 14:52:19 +02002031 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002032 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002033 * @route: audio routes
2034 * @num: number of routes
2035 *
2036 * Connects 2 dapm widgets together via a named audio path. The sink is
2037 * the widget receiving the audio signal, whilst the source is the sender
2038 * of the audio signal.
2039 *
2040 * Returns 0 for success else error. On error all resources can be freed
2041 * with a call to snd_soc_card_free().
2042 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002043int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002044 const struct snd_soc_dapm_route *route, int num)
2045{
2046 int i, ret;
2047
2048 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002049 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02002050 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002051 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2052 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02002053 return ret;
2054 }
2055 route++;
2056 }
2057
2058 return 0;
2059}
2060EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2061
Mark Brownbf3a9e12011-06-13 16:42:29 +01002062static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2063 const struct snd_soc_dapm_route *route)
2064{
2065 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2066 route->source,
2067 true);
2068 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2069 route->sink,
2070 true);
2071 struct snd_soc_dapm_path *path;
2072 int count = 0;
2073
2074 if (!source) {
2075 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2076 route->source);
2077 return -ENODEV;
2078 }
2079
2080 if (!sink) {
2081 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2082 route->sink);
2083 return -ENODEV;
2084 }
2085
2086 if (route->control || route->connected)
2087 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2088 route->source, route->sink);
2089
2090 list_for_each_entry(path, &source->sinks, list_source) {
2091 if (path->sink == sink) {
2092 path->weak = 1;
2093 count++;
2094 }
2095 }
2096
2097 if (count == 0)
2098 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2099 route->source, route->sink);
2100 if (count > 1)
2101 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2102 count, route->source, route->sink);
2103
2104 return 0;
2105}
2106
2107/**
2108 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2109 * @dapm: DAPM context
2110 * @route: audio routes
2111 * @num: number of routes
2112 *
2113 * Mark existing routes matching those specified in the passed array
2114 * as being weak, meaning that they are ignored for the purpose of
2115 * power decisions. The main intended use case is for sidetone paths
2116 * which couple audio between other independent paths if they are both
2117 * active in order to make the combination work better at the user
2118 * level but which aren't intended to be "used".
2119 *
2120 * Note that CODEC drivers should not use this as sidetone type paths
2121 * can frequently also be used as bypass paths.
2122 */
2123int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2124 const struct snd_soc_dapm_route *route, int num)
2125{
2126 int i, err;
2127 int ret = 0;
2128
2129 for (i = 0; i < num; i++) {
2130 err = snd_soc_dapm_weak_route(dapm, route);
2131 if (err)
2132 ret = err;
2133 route++;
2134 }
2135
2136 return ret;
2137}
2138EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2139
Mark Brown105f1c22008-05-13 14:52:19 +02002140/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002141 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002142 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002143 *
2144 * Checks the codec for any new dapm widgets and creates them if found.
2145 *
2146 * Returns 0 for success.
2147 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002148int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002149{
2150 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002151 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002152
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002153 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002154 {
2155 if (w->new)
2156 continue;
2157
Stephen Warrenfad59882011-04-28 17:37:59 -06002158 if (w->num_kcontrols) {
2159 w->kcontrols = kzalloc(w->num_kcontrols *
2160 sizeof(struct snd_kcontrol *),
2161 GFP_KERNEL);
2162 if (!w->kcontrols)
2163 return -ENOMEM;
2164 }
2165
Richard Purdie2b97eab2006-10-06 18:32:18 +02002166 switch(w->id) {
2167 case snd_soc_dapm_switch:
2168 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002169 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002170 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002171 break;
2172 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002173 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002174 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002175 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002176 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002177 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002178 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002179 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002180 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002181 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002182 break;
2183 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002184
2185 /* Read the initial power state from the device */
2186 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002187 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002188 val &= 1 << w->shift;
2189 if (w->invert)
2190 val = !val;
2191
2192 if (val)
2193 w->power = 1;
2194 }
2195
Richard Purdie2b97eab2006-10-06 18:32:18 +02002196 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002197
Mark Brown7508b122011-10-05 12:09:12 +01002198 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002199 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002200 }
2201
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002202 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002203 return 0;
2204}
2205EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2206
2207/**
2208 * snd_soc_dapm_get_volsw - dapm mixer get callback
2209 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002210 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002211 *
2212 * Callback to get the value of a dapm mixer control.
2213 *
2214 * Returns 0 for success.
2215 */
2216int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2217 struct snd_ctl_elem_value *ucontrol)
2218{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002219 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2220 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002221 struct soc_mixer_control *mc =
2222 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002223 unsigned int reg = mc->reg;
2224 unsigned int shift = mc->shift;
2225 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002226 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002227 unsigned int invert = mc->invert;
2228 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002229
Richard Purdie2b97eab2006-10-06 18:32:18 +02002230 ucontrol->value.integer.value[0] =
2231 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2232 if (shift != rshift)
2233 ucontrol->value.integer.value[1] =
2234 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2235 if (invert) {
2236 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002237 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002238 if (shift != rshift)
2239 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002240 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002241 }
2242
2243 return 0;
2244}
2245EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2246
2247/**
2248 * snd_soc_dapm_put_volsw - dapm mixer set callback
2249 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002250 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002251 *
2252 * Callback to set the value of a dapm mixer control.
2253 *
2254 * Returns 0 for success.
2255 */
2256int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2257 struct snd_ctl_elem_value *ucontrol)
2258{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002259 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2260 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2261 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002262 struct soc_mixer_control *mc =
2263 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002264 unsigned int reg = mc->reg;
2265 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002266 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002267 unsigned int mask = (1 << fls(max)) - 1;
2268 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002269 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002270 int connect, change;
2271 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002272 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002273
2274 val = (ucontrol->value.integer.value[0] & mask);
2275
2276 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002277 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002278 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002279 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280
Stephen Warrenfafd2172011-04-28 17:38:00 -06002281 if (val)
2282 /* new connection */
2283 connect = invert ? 0 : 1;
2284 else
2285 /* old connection must be powered down */
2286 connect = invert ? 1 : 0;
2287
2288 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002289
Stephen Warrene9cf7042011-01-27 14:54:05 -07002290 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002291 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002292 for (wi = 0; wi < wlist->num_widgets; wi++) {
2293 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002294
Stephen Warrenfafd2172011-04-28 17:38:00 -06002295 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002296
Stephen Warrenfafd2172011-04-28 17:38:00 -06002297 update.kcontrol = kcontrol;
2298 update.widget = widget;
2299 update.reg = reg;
2300 update.mask = mask;
2301 update.val = val;
2302 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002303
Stephen Warrenfafd2172011-04-28 17:38:00 -06002304 dapm_mixer_update_power(widget, kcontrol, connect);
2305
2306 widget->dapm->update = NULL;
2307 }
Mark Brown283375c2009-12-07 18:09:03 +00002308 }
2309
Stephen Warrenfafd2172011-04-28 17:38:00 -06002310 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002311 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002312}
2313EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2314
2315/**
2316 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2317 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002318 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002319 *
2320 * Callback to get the value of a dapm enumerated double mixer control.
2321 *
2322 * Returns 0 for success.
2323 */
2324int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2325 struct snd_ctl_elem_value *ucontrol)
2326{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002327 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2328 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002329 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002330 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002331
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002332 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002333 ;
2334 val = snd_soc_read(widget->codec, e->reg);
2335 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2336 if (e->shift_l != e->shift_r)
2337 ucontrol->value.enumerated.item[1] =
2338 (val >> e->shift_r) & (bitmask - 1);
2339
2340 return 0;
2341}
2342EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2343
2344/**
2345 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2346 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002347 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002348 *
2349 * Callback to set the value of a dapm enumerated double mixer control.
2350 *
2351 * Returns 0 for success.
2352 */
2353int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2354 struct snd_ctl_elem_value *ucontrol)
2355{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002356 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2357 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2358 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002359 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002360 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002361 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002362 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002363 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002364
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002365 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002366 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002367 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002368 return -EINVAL;
2369 mux = ucontrol->value.enumerated.item[0];
2370 val = mux << e->shift_l;
2371 mask = (bitmask - 1) << e->shift_l;
2372 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002373 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002374 return -EINVAL;
2375 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2376 mask |= (bitmask - 1) << e->shift_r;
2377 }
2378
Stephen Warrenfafd2172011-04-28 17:38:00 -06002379 mutex_lock(&codec->mutex);
2380
Mark Brown3a655772009-10-05 17:23:30 +01002381 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002382 if (change) {
2383 for (wi = 0; wi < wlist->num_widgets; wi++) {
2384 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002385
Stephen Warrenfafd2172011-04-28 17:38:00 -06002386 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002387
Stephen Warrenfafd2172011-04-28 17:38:00 -06002388 update.kcontrol = kcontrol;
2389 update.widget = widget;
2390 update.reg = e->reg;
2391 update.mask = mask;
2392 update.val = val;
2393 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002394
Stephen Warrenfafd2172011-04-28 17:38:00 -06002395 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002396
Stephen Warrenfafd2172011-04-28 17:38:00 -06002397 widget->dapm->update = NULL;
2398 }
2399 }
2400
2401 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002402 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002403}
2404EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2405
2406/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002407 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2408 * @kcontrol: mixer control
2409 * @ucontrol: control element information
2410 *
2411 * Returns 0 for success.
2412 */
2413int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2414 struct snd_ctl_elem_value *ucontrol)
2415{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002416 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2417 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002418
2419 ucontrol->value.enumerated.item[0] = widget->value;
2420
2421 return 0;
2422}
2423EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2424
2425/**
2426 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2427 * @kcontrol: mixer control
2428 * @ucontrol: control element information
2429 *
2430 * Returns 0 for success.
2431 */
2432int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2433 struct snd_ctl_elem_value *ucontrol)
2434{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002435 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2436 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2437 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002438 struct soc_enum *e =
2439 (struct soc_enum *)kcontrol->private_value;
2440 int change;
2441 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002442 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002443
2444 if (ucontrol->value.enumerated.item[0] >= e->max)
2445 return -EINVAL;
2446
Stephen Warrenfafd2172011-04-28 17:38:00 -06002447 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002448
2449 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002450 if (change) {
2451 for (wi = 0; wi < wlist->num_widgets; wi++) {
2452 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002453
Stephen Warrenfafd2172011-04-28 17:38:00 -06002454 widget->value = ucontrol->value.enumerated.item[0];
2455
2456 dapm_mux_update_power(widget, kcontrol, change,
2457 widget->value, e);
2458 }
2459 }
2460
2461 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002462 return ret;
2463}
2464EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2465
2466/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002467 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2468 * callback
2469 * @kcontrol: mixer control
2470 * @ucontrol: control element information
2471 *
2472 * Callback to get the value of a dapm semi enumerated double mixer control.
2473 *
2474 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2475 * used for handling bitfield coded enumeration for example.
2476 *
2477 * Returns 0 for success.
2478 */
2479int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2480 struct snd_ctl_elem_value *ucontrol)
2481{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002482 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2483 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002484 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002485 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002486
2487 reg_val = snd_soc_read(widget->codec, e->reg);
2488 val = (reg_val >> e->shift_l) & e->mask;
2489 for (mux = 0; mux < e->max; mux++) {
2490 if (val == e->values[mux])
2491 break;
2492 }
2493 ucontrol->value.enumerated.item[0] = mux;
2494 if (e->shift_l != e->shift_r) {
2495 val = (reg_val >> e->shift_r) & e->mask;
2496 for (mux = 0; mux < e->max; mux++) {
2497 if (val == e->values[mux])
2498 break;
2499 }
2500 ucontrol->value.enumerated.item[1] = mux;
2501 }
2502
2503 return 0;
2504}
2505EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2506
2507/**
2508 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2509 * callback
2510 * @kcontrol: mixer control
2511 * @ucontrol: control element information
2512 *
2513 * Callback to set the value of a dapm semi enumerated double mixer control.
2514 *
2515 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2516 * used for handling bitfield coded enumeration for example.
2517 *
2518 * Returns 0 for success.
2519 */
2520int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2521 struct snd_ctl_elem_value *ucontrol)
2522{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002523 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2524 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2525 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002526 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002527 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002528 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002529 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002530 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002531
2532 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2533 return -EINVAL;
2534 mux = ucontrol->value.enumerated.item[0];
2535 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2536 mask = e->mask << e->shift_l;
2537 if (e->shift_l != e->shift_r) {
2538 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2539 return -EINVAL;
2540 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2541 mask |= e->mask << e->shift_r;
2542 }
2543
Stephen Warrenfafd2172011-04-28 17:38:00 -06002544 mutex_lock(&codec->mutex);
2545
Mark Brown3a655772009-10-05 17:23:30 +01002546 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002547 if (change) {
2548 for (wi = 0; wi < wlist->num_widgets; wi++) {
2549 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002550
Stephen Warrenfafd2172011-04-28 17:38:00 -06002551 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002552
Stephen Warrenfafd2172011-04-28 17:38:00 -06002553 update.kcontrol = kcontrol;
2554 update.widget = widget;
2555 update.reg = e->reg;
2556 update.mask = mask;
2557 update.val = val;
2558 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002559
Stephen Warrenfafd2172011-04-28 17:38:00 -06002560 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002561
Stephen Warrenfafd2172011-04-28 17:38:00 -06002562 widget->dapm->update = NULL;
2563 }
2564 }
2565
2566 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002567 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002568}
2569EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2570
2571/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002572 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2573 *
2574 * @kcontrol: mixer control
2575 * @uinfo: control element information
2576 *
2577 * Callback to provide information about a pin switch control.
2578 */
2579int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2580 struct snd_ctl_elem_info *uinfo)
2581{
2582 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2583 uinfo->count = 1;
2584 uinfo->value.integer.min = 0;
2585 uinfo->value.integer.max = 1;
2586
2587 return 0;
2588}
2589EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2590
2591/**
2592 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2593 *
2594 * @kcontrol: mixer control
2595 * @ucontrol: Value
2596 */
2597int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2598 struct snd_ctl_elem_value *ucontrol)
2599{
2600 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2601 const char *pin = (const char *)kcontrol->private_value;
2602
2603 mutex_lock(&codec->mutex);
2604
2605 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002606 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002607
2608 mutex_unlock(&codec->mutex);
2609
2610 return 0;
2611}
2612EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2613
2614/**
2615 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2616 *
2617 * @kcontrol: mixer control
2618 * @ucontrol: Value
2619 */
2620int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2621 struct snd_ctl_elem_value *ucontrol)
2622{
2623 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2624 const char *pin = (const char *)kcontrol->private_value;
2625
2626 mutex_lock(&codec->mutex);
2627
2628 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002629 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002630 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002631 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002632
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002633 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002634
2635 mutex_unlock(&codec->mutex);
2636
2637 return 0;
2638}
2639EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2640
2641/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002642 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002643 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002644 * @widget: widget template
2645 *
2646 * Creates a new dapm control based upon the template.
2647 *
2648 * Returns 0 for success else error.
2649 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002650int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002651 const struct snd_soc_dapm_widget *widget)
2652{
2653 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002654 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002655
2656 if ((w = dapm_cnew_widget(widget)) == NULL)
2657 return -ENOMEM;
2658
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002659 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002660 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002661 name_len += 1 + strlen(dapm->codec->name_prefix);
2662 w->name = kmalloc(name_len, GFP_KERNEL);
2663 if (w->name == NULL) {
2664 kfree(w);
2665 return -ENOMEM;
2666 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002667 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002668 snprintf(w->name, name_len, "%s %s",
2669 dapm->codec->name_prefix, widget->name);
2670 else
2671 snprintf(w->name, name_len, "%s", widget->name);
2672
Mark Brown7ca3a182011-10-08 14:04:50 +01002673 switch (w->id) {
2674 case snd_soc_dapm_switch:
2675 case snd_soc_dapm_mixer:
2676 case snd_soc_dapm_mixer_named_ctl:
2677 w->power_check = dapm_generic_check_power;
2678 break;
2679 case snd_soc_dapm_mux:
2680 case snd_soc_dapm_virt_mux:
2681 case snd_soc_dapm_value_mux:
2682 w->power_check = dapm_generic_check_power;
2683 break;
2684 case snd_soc_dapm_adc:
2685 case snd_soc_dapm_aif_out:
2686 w->power_check = dapm_adc_check_power;
2687 break;
2688 case snd_soc_dapm_dac:
2689 case snd_soc_dapm_aif_in:
2690 w->power_check = dapm_dac_check_power;
2691 break;
2692 case snd_soc_dapm_pga:
2693 case snd_soc_dapm_out_drv:
2694 case snd_soc_dapm_input:
2695 case snd_soc_dapm_output:
2696 case snd_soc_dapm_micbias:
2697 case snd_soc_dapm_spk:
2698 case snd_soc_dapm_hp:
2699 case snd_soc_dapm_mic:
2700 case snd_soc_dapm_line:
2701 w->power_check = dapm_generic_check_power;
2702 break;
2703 case snd_soc_dapm_supply:
2704 w->power_check = dapm_supply_check_power;
2705 break;
2706 default:
2707 w->power_check = dapm_always_on_check_power;
2708 break;
2709 }
2710
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002711 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002712 w->dapm = dapm;
2713 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002714 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002715 INIT_LIST_HEAD(&w->sources);
2716 INIT_LIST_HEAD(&w->sinks);
2717 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01002718 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002719 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002720
2721 /* machine layer set ups unconnected pins and insertions */
2722 w->connected = 1;
2723 return 0;
2724}
2725EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2726
2727/**
Mark Brown4ba13272008-05-13 14:51:19 +02002728 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002729 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002730 * @widget: widget array
2731 * @num: number of widgets
2732 *
2733 * Creates new DAPM controls based upon the templates.
2734 *
2735 * Returns 0 for success else error.
2736 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002737int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002738 const struct snd_soc_dapm_widget *widget,
2739 int num)
2740{
2741 int i, ret;
2742
2743 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002744 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002745 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002746 dev_err(dapm->dev,
2747 "ASoC: Failed to create DAPM control %s: %d\n",
2748 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002749 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002750 }
Mark Brown4ba13272008-05-13 14:51:19 +02002751 widget++;
2752 }
2753 return 0;
2754}
2755EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2756
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002757static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002758 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002759{
2760 struct snd_soc_dapm_widget *w;
2761
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002762 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002763 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002764 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002765 continue;
Liam Girdwoodee47b362011-07-25 11:15:50 +01002766 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002767 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002768 if (strstr(w->sname, stream)) {
Mark Brown75c1f892011-10-04 22:28:08 +01002769 dapm_mark_dirty(w, "stream event");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002770 switch(event) {
2771 case SND_SOC_DAPM_STREAM_START:
2772 w->active = 1;
2773 break;
2774 case SND_SOC_DAPM_STREAM_STOP:
2775 w->active = 0;
2776 break;
2777 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002778 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002779 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002780 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2781 break;
2782 }
2783 }
2784 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002785
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002786 dapm_power_widgets(dapm, event);
Liam Girdwood64a648c2011-07-25 11:15:15 +01002787
2788 /* do we need to notify any clients that DAPM stream is complete */
2789 if (dapm->stream_event)
2790 dapm->stream_event(dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002791}
2792
2793/**
2794 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2795 * @rtd: PCM runtime data
2796 * @stream: stream name
2797 * @event: stream event
2798 *
2799 * Sends a stream event to the dapm core. The core then makes any
2800 * necessary widget power changes.
2801 *
2802 * Returns 0 for success else error.
2803 */
2804int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2805 const char *stream, int event)
2806{
2807 struct snd_soc_codec *codec = rtd->codec;
2808
2809 if (stream == NULL)
2810 return 0;
2811
2812 mutex_lock(&codec->mutex);
2813 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002814 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002815 return 0;
2816}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002817
2818/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002819 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002820 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002821 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002822 *
Mark Brown74b8f952009-06-06 11:26:15 +01002823 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002824 * a valid audio route and active audio stream.
2825 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2826 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002827 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002828int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002829{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002830 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002831}
Liam Girdwooda5302182008-07-07 13:35:17 +01002832EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002833
2834/**
Mark Brownda341832010-03-15 19:23:37 +00002835 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002836 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002837 * @pin: pin name
2838 *
2839 * Enables input/output pin regardless of any other state. This is
2840 * intended for use with microphone bias supplies used in microphone
2841 * jack detection.
2842 *
2843 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2844 * do any widget power switching.
2845 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002846int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2847 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002848{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002849 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002850
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002851 if (!w) {
2852 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2853 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002854 }
2855
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002856 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2857 w->connected = 1;
2858 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01002859 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09002860
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002861 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002862}
2863EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2864
2865/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002866 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002867 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002868 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002869 *
Mark Brown74b8f952009-06-06 11:26:15 +01002870 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002871 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2872 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002873 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002874int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2875 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002876{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002877 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002878}
2879EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2880
2881/**
Mark Brown5817b522008-09-24 11:23:11 +01002882 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002883 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002884 * @pin: pin name
2885 *
2886 * Marks the specified pin as being not connected, disabling it along
2887 * any parent or child widgets. At present this is identical to
2888 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2889 * additional things such as disabling controls which only affect
2890 * paths through the pin.
2891 *
2892 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2893 * do any widget power switching.
2894 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002895int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002896{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002897 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002898}
2899EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2900
2901/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002902 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002903 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002904 * @pin: audio signal pin endpoint (or start point)
2905 *
2906 * Get audio pin status - connected or disconnected.
2907 *
2908 * Returns 1 for connected otherwise 0.
2909 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002910int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2911 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002912{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002913 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002914
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002915 if (w)
2916 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002917
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002918 return 0;
2919}
Liam Girdwooda5302182008-07-07 13:35:17 +01002920EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002921
2922/**
Mark Brown1547aba2010-05-07 21:11:40 +01002923 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002924 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002925 * @pin: audio signal pin endpoint (or start point)
2926 *
2927 * Mark the given endpoint or pin as ignoring suspend. When the
2928 * system is disabled a path between two endpoints flagged as ignoring
2929 * suspend will not be disabled. The path must already be enabled via
2930 * normal means at suspend time, it will not be turned on if it was not
2931 * already enabled.
2932 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002933int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2934 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002935{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002936 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002937
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002938 if (!w) {
2939 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2940 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002941 }
2942
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002943 w->ignore_suspend = 1;
2944
2945 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002946}
2947EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2948
2949/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002950 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03002951 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002952 *
2953 * Free all dapm widgets and resources.
2954 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002955void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002956{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002957 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002958 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002959 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002960 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002961}
2962EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2963
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002964static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002965{
Mark Brown51737472009-06-22 13:16:51 +01002966 struct snd_soc_dapm_widget *w;
2967 LIST_HEAD(down_list);
2968 int powerdown = 0;
2969
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002970 list_for_each_entry(w, &dapm->card->widgets, list) {
2971 if (w->dapm != dapm)
2972 continue;
Mark Brown51737472009-06-22 13:16:51 +01002973 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002974 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002975 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002976 powerdown = 1;
2977 }
2978 }
2979
2980 /* If there were no widgets to power down we're already in
2981 * standby.
2982 */
2983 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002984 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002985 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002986 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002987 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002988}
Mark Brown51737472009-06-22 13:16:51 +01002989
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002990/*
2991 * snd_soc_dapm_shutdown - callback for system shutdown
2992 */
2993void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2994{
2995 struct snd_soc_codec *codec;
2996
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002997 list_for_each_entry(codec, &card->codec_dev_list, list) {
2998 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002999 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003000 }
Mark Brown51737472009-06-22 13:16:51 +01003001}
3002
Richard Purdie2b97eab2006-10-06 18:32:18 +02003003/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003004MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003005MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3006MODULE_LICENSE("GPL");