blob: e49c56d4951e6ce4288d822c066acbb9e2303554 [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 Brown15e4c722008-07-02 11:51:20 +010097{
98 if (pop_time)
99 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
100}
101
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200102static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100103{
104 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200105 char *buf;
106
107 if (!pop_time)
108 return;
109
110 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
111 if (buf == NULL)
112 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100113
114 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200115 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100116 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100117 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200118
119 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100120}
121
Mark Browndb432b42011-10-03 21:06:40 +0100122static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
123{
124 return !list_empty(&w->dirty);
125}
126
Mark 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 Smirl815ecf82008-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 Smirl815ecf82008-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 Smirlf8ba0b7b2008-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 Smirlf8ba0b7b2008-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 Smirlf8ba0b7b2008-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 Brown99497882010-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 Brown99497882010-05-07 20:24:05 +0100646
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000647 switch (level) {
Mark Brown99497882010-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 Brown99497882010-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 Brownde02d072011-09-20 21:43:24 +0100668 DAPM_UPDATE_STAT(widget, path_checks);
669
Mark Brown246d0a12009-04-22 18:24:55 +0100670 if (widget->id == snd_soc_dapm_supply)
671 return 0;
672
Mark Brown010ff262009-08-17 17:39:22 +0100673 switch (widget->id) {
674 case snd_soc_dapm_adc:
675 case snd_soc_dapm_aif_out:
676 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100677 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100678 default:
679 break;
680 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200681
682 if (widget->connected) {
683 /* connected pin ? */
684 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100685 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200686
687 /* connected jack or spk ? */
688 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300689 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100690 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200691 }
692
693 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100694 DAPM_UPDATE_STAT(widget, neighbour_checks);
695
Mark Brownbf3a9e12011-06-13 16:42:29 +0100696 if (path->weak)
697 continue;
698
Richard Purdie2b97eab2006-10-06 18:32:18 +0200699 if (path->walked)
700 continue;
701
702 if (path->sink && path->connect) {
703 path->walked = 1;
704 con += is_connected_output_ep(path->sink);
705 }
706 }
707
708 return con;
709}
710
711/*
712 * Recursively check for a completed path to an active or physically connected
713 * input widget. Returns number of complete paths.
714 */
715static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
716{
717 struct snd_soc_dapm_path *path;
718 int con = 0;
719
Mark Brownde02d072011-09-20 21:43:24 +0100720 DAPM_UPDATE_STAT(widget, path_checks);
721
Mark Brown246d0a12009-04-22 18:24:55 +0100722 if (widget->id == snd_soc_dapm_supply)
723 return 0;
724
Richard Purdie2b97eab2006-10-06 18:32:18 +0200725 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100726 switch (widget->id) {
727 case snd_soc_dapm_dac:
728 case snd_soc_dapm_aif_in:
729 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100730 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100731 default:
732 break;
733 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200734
735 if (widget->connected) {
736 /* connected pin ? */
737 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100738 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200739
740 /* connected VMID/Bias for lower pops */
741 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100742 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200743
744 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300745 if (widget->id == snd_soc_dapm_mic ||
746 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100747 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200748 }
749
750 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100751 DAPM_UPDATE_STAT(widget, neighbour_checks);
752
Mark Brownbf3a9e12011-06-13 16:42:29 +0100753 if (path->weak)
754 continue;
755
Richard Purdie2b97eab2006-10-06 18:32:18 +0200756 if (path->walked)
757 continue;
758
759 if (path->source && path->connect) {
760 path->walked = 1;
761 con += is_connected_input_ep(path->source);
762 }
763 }
764
765 return con;
766}
767
768/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300769 * Handler for generic register modifier widget.
770 */
771int dapm_reg_event(struct snd_soc_dapm_widget *w,
772 struct snd_kcontrol *kcontrol, int event)
773{
774 unsigned int val;
775
776 if (SND_SOC_DAPM_EVENT_ON(event))
777 val = w->on_val;
778 else
779 val = w->off_val;
780
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100781 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300782 w->mask << w->shift, val << w->shift);
783
784 return 0;
785}
Mark Brown11589412008-07-29 11:42:23 +0100786EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300787
Mark Brownd8050022011-09-28 18:28:23 +0100788static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
789{
Mark Brown9b8a83b2011-10-04 22:15:59 +0100790 if (w->power_checked)
791 return w->new_power;
792
Mark Brownd8050022011-09-28 18:28:23 +0100793 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +0100794 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +0100795 else
Mark Brown9b8a83b2011-10-04 22:15:59 +0100796 w->new_power = w->power_check(w);
797
798 w->power_checked = true;
799
800 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +0100801}
802
Mark Browncd0f2d42009-04-20 16:56:59 +0100803/* Generic check to see if a widget should be powered.
804 */
805static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
806{
807 int in, out;
808
Mark Brownde02d072011-09-20 21:43:24 +0100809 DAPM_UPDATE_STAT(w, power_checks);
810
Mark Browncd0f2d42009-04-20 16:56:59 +0100811 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200812 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100813 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200814 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100815 return out != 0 && in != 0;
816}
817
Mark Brown6ea31b92009-04-20 17:15:41 +0100818/* Check to see if an ADC has power */
819static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
820{
821 int in;
822
Mark Brownde02d072011-09-20 21:43:24 +0100823 DAPM_UPDATE_STAT(w, power_checks);
824
Mark Brown6ea31b92009-04-20 17:15:41 +0100825 if (w->active) {
826 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200827 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100828 return in != 0;
829 } else {
830 return dapm_generic_check_power(w);
831 }
832}
833
834/* Check to see if a DAC has power */
835static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
836{
837 int out;
838
Mark Brownde02d072011-09-20 21:43:24 +0100839 DAPM_UPDATE_STAT(w, power_checks);
840
Mark Brown6ea31b92009-04-20 17:15:41 +0100841 if (w->active) {
842 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200843 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100844 return out != 0;
845 } else {
846 return dapm_generic_check_power(w);
847 }
848}
849
Mark Brown246d0a12009-04-22 18:24:55 +0100850/* Check to see if a power supply is needed */
851static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
852{
853 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +0100854
Mark Brownde02d072011-09-20 21:43:24 +0100855 DAPM_UPDATE_STAT(w, power_checks);
856
Mark Brown246d0a12009-04-22 18:24:55 +0100857 /* Check if one of our outputs is connected */
858 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +0100859 DAPM_UPDATE_STAT(w, neighbour_checks);
860
Mark Brownbf3a9e12011-06-13 16:42:29 +0100861 if (path->weak)
862 continue;
863
Mark Brown215edda2009-09-08 18:59:05 +0100864 if (path->connected &&
865 !path->connected(path->source, path->sink))
866 continue;
867
Mark Brown30173582011-02-11 11:42:19 +0000868 if (!path->sink)
869 continue;
870
Mark Brownf68d7e12011-10-04 22:57:50 +0100871 if (dapm_widget_power_check(path->sink))
872 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +0100873 }
874
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200875 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100876
Mark Brownf68d7e12011-10-04 22:57:50 +0100877 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +0100878}
879
Mark Brown35c64bca2011-09-28 18:23:53 +0100880static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
881{
882 return 1;
883}
884
Mark Brown38357ab2009-06-06 19:03:23 +0100885static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
886 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000887 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000888{
Mark Brown828a8422011-01-15 13:14:30 +0000889 int *sort;
890
891 if (power_up)
892 sort = dapm_up_seq;
893 else
894 sort = dapm_down_seq;
895
Mark Brown38357ab2009-06-06 19:03:23 +0100896 if (sort[a->id] != sort[b->id])
897 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000898 if (a->subseq != b->subseq) {
899 if (power_up)
900 return a->subseq - b->subseq;
901 else
902 return b->subseq - a->subseq;
903 }
Mark Brownb22ead22009-06-07 12:51:26 +0100904 if (a->reg != b->reg)
905 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000906 if (a->dapm != b->dapm)
907 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000908
Mark Brown38357ab2009-06-06 19:03:23 +0100909 return 0;
910}
Mark Brown42aa3412009-03-01 19:21:10 +0000911
Mark Brown38357ab2009-06-06 19:03:23 +0100912/* Insert a widget in order into a DAPM power sequence. */
913static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
914 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000915 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100916{
917 struct snd_soc_dapm_widget *w;
918
919 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000920 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100921 list_add_tail(&new_widget->power_list, &w->power_list);
922 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000923 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100924
Mark Brown38357ab2009-06-06 19:03:23 +0100925 list_add_tail(&new_widget->power_list, list);
926}
Mark Brown42aa3412009-03-01 19:21:10 +0000927
Mark Brown68f89ad2010-11-03 23:51:49 -0400928static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
929 struct snd_soc_dapm_widget *w, int event)
930{
931 struct snd_soc_card *card = dapm->card;
932 const char *ev_name;
933 int power, ret;
934
935 switch (event) {
936 case SND_SOC_DAPM_PRE_PMU:
937 ev_name = "PRE_PMU";
938 power = 1;
939 break;
940 case SND_SOC_DAPM_POST_PMU:
941 ev_name = "POST_PMU";
942 power = 1;
943 break;
944 case SND_SOC_DAPM_PRE_PMD:
945 ev_name = "PRE_PMD";
946 power = 0;
947 break;
948 case SND_SOC_DAPM_POST_PMD:
949 ev_name = "POST_PMD";
950 power = 0;
951 break;
952 default:
953 BUG();
954 return;
955 }
956
957 if (w->power != power)
958 return;
959
960 if (w->event && (w->event_flags & event)) {
961 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
962 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400963 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400964 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400965 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400966 if (ret < 0)
967 pr_err("%s: %s event failed: %d\n",
968 ev_name, w->name, ret);
969 }
970}
971
Mark Brownb22ead22009-06-07 12:51:26 +0100972/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200973static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100974 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100975{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200976 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400977 struct snd_soc_dapm_widget *w;
978 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100979 unsigned int value = 0;
980 unsigned int mask = 0;
981 unsigned int cur_mask;
982
983 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
984 power_list)->reg;
985
986 list_for_each_entry(w, pending, power_list) {
987 cur_mask = 1 << w->shift;
988 BUG_ON(reg != w->reg);
989
990 if (w->invert)
991 power = !w->power;
992 else
993 power = w->power;
994
995 mask |= cur_mask;
996 if (power)
997 value |= cur_mask;
998
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200999 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001000 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1001 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001002
Mark Brown68f89ad2010-11-03 23:51:49 -04001003 /* Check for events */
1004 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1005 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001006 }
1007
Mark Brown81628102009-06-07 13:21:24 +01001008 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001009 /* Any widget will do, they should all be updating the
1010 * same register.
1011 */
1012 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1013 power_list);
1014
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001015 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001016 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001017 value, mask, reg, card->pop_time);
1018 pop_wait(card->pop_time);
Liam Girdwood0445bdf2011-06-13 19:37:36 +01001019 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001020 }
1021
1022 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001023 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1024 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001025 }
Mark Brown42aa3412009-03-01 19:21:10 +00001026}
1027
Mark Brownb22ead22009-06-07 12:51:26 +01001028/* Apply a DAPM power sequence.
1029 *
1030 * We walk over a pre-sorted list of widgets to apply power to. In
1031 * order to minimise the number of writes to the device required
1032 * multiple widgets will be updated in a single write where possible.
1033 * Currently anything that requires more than a single write is not
1034 * handled.
1035 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001036static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001037 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001038{
1039 struct snd_soc_dapm_widget *w, *n;
1040 LIST_HEAD(pending);
1041 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001042 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001043 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001044 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001045 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001046 int *sort;
1047
1048 if (power_up)
1049 sort = dapm_up_seq;
1050 else
1051 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001052
Mark Brownb22ead22009-06-07 12:51:26 +01001053 list_for_each_entry_safe(w, n, list, power_list) {
1054 ret = 0;
1055
1056 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001057 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001058 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001059 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001060 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001061
Mark Brown474b62d2011-01-18 16:14:44 +00001062 if (cur_dapm && cur_dapm->seq_notifier) {
1063 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1064 if (sort[i] == cur_sort)
1065 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001066 i,
1067 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001068 }
1069
Mark Brownb22ead22009-06-07 12:51:26 +01001070 INIT_LIST_HEAD(&pending);
1071 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001072 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001073 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001074 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001075 }
1076
Mark Brown163cac02009-06-07 10:12:52 +01001077 switch (w->id) {
1078 case snd_soc_dapm_pre:
1079 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001080 list_for_each_entry_safe_continue(w, n, list,
1081 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001082
Mark Brownb22ead22009-06-07 12:51:26 +01001083 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001084 ret = w->event(w,
1085 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001086 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001087 ret = w->event(w,
1088 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001089 break;
1090
1091 case snd_soc_dapm_post:
1092 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001093 list_for_each_entry_safe_continue(w, n, list,
1094 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001095
Mark Brownb22ead22009-06-07 12:51:26 +01001096 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001097 ret = w->event(w,
1098 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001099 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001100 ret = w->event(w,
1101 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001102 break;
1103
Mark Brown163cac02009-06-07 10:12:52 +01001104 default:
Mark Brown81628102009-06-07 13:21:24 +01001105 /* Queue it up for application */
1106 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001107 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001108 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001109 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001110 list_move(&w->power_list, &pending);
1111 break;
Mark Brown163cac02009-06-07 10:12:52 +01001112 }
Mark Brownb22ead22009-06-07 12:51:26 +01001113
1114 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001115 dev_err(w->dapm->dev,
1116 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001117 }
Mark Brownb22ead22009-06-07 12:51:26 +01001118
1119 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001120 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001121
1122 if (cur_dapm && cur_dapm->seq_notifier) {
1123 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1124 if (sort[i] == cur_sort)
1125 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001126 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001127 }
Mark Brown163cac02009-06-07 10:12:52 +01001128}
1129
Mark Brown97404f22010-12-14 16:13:57 +00001130static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1131{
1132 struct snd_soc_dapm_update *update = dapm->update;
1133 struct snd_soc_dapm_widget *w;
1134 int ret;
1135
1136 if (!update)
1137 return;
1138
1139 w = update->widget;
1140
1141 if (w->event &&
1142 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1143 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1144 if (ret != 0)
1145 pr_err("%s DAPM pre-event failed: %d\n",
1146 w->name, ret);
1147 }
1148
1149 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1150 update->val);
1151 if (ret < 0)
1152 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1153
1154 if (w->event &&
1155 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1156 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1157 if (ret != 0)
1158 pr_err("%s DAPM post-event failed: %d\n",
1159 w->name, ret);
1160 }
1161}
1162
Mark Brown9d0624a2011-02-18 11:49:43 -08001163/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1164 * they're changing state.
1165 */
1166static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1167{
1168 struct snd_soc_dapm_context *d = data;
1169 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001170
Mark Brown56fba412011-06-04 11:25:10 +01001171 /* If we're off and we're not supposed to be go into STANDBY */
1172 if (d->bias_level == SND_SOC_BIAS_OFF &&
1173 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001174 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1175 if (ret != 0)
1176 dev_err(d->dev,
1177 "Failed to turn on bias: %d\n", ret);
1178 }
1179
Mark Brown56fba412011-06-04 11:25:10 +01001180 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1181 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001182 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1183 if (ret != 0)
1184 dev_err(d->dev,
1185 "Failed to prepare bias: %d\n", ret);
1186 }
1187}
1188
1189/* Async callback run prior to DAPM sequences - brings to their final
1190 * state.
1191 */
1192static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1193{
1194 struct snd_soc_dapm_context *d = data;
1195 int ret;
1196
1197 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001198 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1199 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1200 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001201 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1202 if (ret != 0)
1203 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1204 ret);
1205 }
1206
1207 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001208 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1209 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001210 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1211 if (ret != 0)
1212 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1213 }
1214
1215 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001216 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1217 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001218 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1219 if (ret != 0)
1220 dev_err(d->dev, "Failed to apply active bias: %d\n",
1221 ret);
1222 }
1223}
Mark Brown97404f22010-12-14 16:13:57 +00001224
Mark Brownfe4fda52011-10-03 22:36:57 +01001225static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1226 bool power, bool connect)
1227{
1228 /* If a connection is being made or broken then that update
1229 * will have marked the peer dirty, otherwise the widgets are
1230 * not connected and this update has no impact. */
1231 if (!connect)
1232 return;
1233
1234 /* If the peer is already in the state we're moving to then we
1235 * won't have an impact on it. */
1236 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001237 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001238}
1239
Mark Brown05623c42011-09-28 17:02:31 +01001240static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1241 struct list_head *up_list,
1242 struct list_head *down_list)
1243{
Mark Browndb432b42011-10-03 21:06:40 +01001244 struct snd_soc_dapm_path *path;
1245
Mark Brown05623c42011-09-28 17:02:31 +01001246 if (w->power == power)
1247 return;
1248
1249 trace_snd_soc_dapm_widget_power(w, power);
1250
Mark Browndb432b42011-10-03 21:06:40 +01001251 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001252 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001253 */
1254 list_for_each_entry(path, &w->sources, list_sink) {
1255 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001256 dapm_widget_set_peer_power(path->source, power,
1257 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001258 }
1259 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001260 switch (w->id) {
1261 case snd_soc_dapm_supply:
1262 /* Supplies can't affect their outputs, only their inputs */
1263 break;
1264 default:
1265 list_for_each_entry(path, &w->sinks, list_source) {
1266 if (path->sink) {
1267 dapm_widget_set_peer_power(path->sink, power,
1268 path->connect);
1269 }
Mark Browndb432b42011-10-03 21:06:40 +01001270 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001271 break;
Mark Browndb432b42011-10-03 21:06:40 +01001272 }
1273
Mark Brown05623c42011-09-28 17:02:31 +01001274 if (power)
1275 dapm_seq_insert(w, up_list, true);
1276 else
1277 dapm_seq_insert(w, down_list, false);
1278
1279 w->power = power;
1280}
1281
Mark Brown7c81beb2011-09-20 22:22:32 +01001282static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1283 struct list_head *up_list,
1284 struct list_head *down_list)
1285{
Mark Brown7c81beb2011-09-20 22:22:32 +01001286 int power;
1287
1288 switch (w->id) {
1289 case snd_soc_dapm_pre:
1290 dapm_seq_insert(w, down_list, false);
1291 break;
1292 case snd_soc_dapm_post:
1293 dapm_seq_insert(w, up_list, true);
1294 break;
1295
1296 default:
Mark Brownd8050022011-09-28 18:28:23 +01001297 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001298
Mark Brown05623c42011-09-28 17:02:31 +01001299 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001300 break;
1301 }
1302}
1303
Mark Brown42aa3412009-03-01 19:21:10 +00001304/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001305 * Scan each dapm widget for complete audio path.
1306 * A complete path is a route that has valid endpoints i.e.:-
1307 *
1308 * o DAC to output pin.
1309 * o Input Pin to ADC.
1310 * o Input pin to Output pin (bypass, sidetone)
1311 * o DAC to ADC (loopback).
1312 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001313static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001314{
Mark Brown12ea2c72011-03-02 18:17:32 +00001315 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001316 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001317 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001318 LIST_HEAD(up_list);
1319 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001320 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001321 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001322
Mark Brown84e90932010-11-04 00:07:02 -04001323 trace_snd_soc_dapm_start(card);
1324
Mark Brown56fba412011-06-04 11:25:10 +01001325 list_for_each_entry(d, &card->dapm_list, list) {
1326 if (d->n_widgets || d->codec == NULL) {
1327 if (d->idle_bias_off)
1328 d->target_bias_level = SND_SOC_BIAS_OFF;
1329 else
1330 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1331 }
1332 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001333
Mark Brownde02d072011-09-20 21:43:24 +01001334 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1335
Mark Brown9b8a83b2011-10-04 22:15:59 +01001336 list_for_each_entry(w, &card->widgets, list) {
1337 w->power_checked = false;
1338 }
1339
Mark Brown6d3ddc82009-05-16 17:47:29 +01001340 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001341 * lists indicating if they should be powered up or down. We
1342 * only check widgets that have been flagged as dirty but note
1343 * that new widgets may be added to the dirty list while we
1344 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001345 */
Mark Browndb432b42011-10-03 21:06:40 +01001346 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001347 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001348 }
1349
Mark Brownf9de6d72011-09-28 17:19:47 +01001350 list_for_each_entry(w, &card->widgets, list) {
Mark Browndb432b42011-10-03 21:06:40 +01001351 list_del_init(&w->dirty);
1352
Mark Brownf9de6d72011-09-28 17:19:47 +01001353 if (w->power) {
1354 d = w->dapm;
1355
1356 /* Supplies and micbiases only bring the
1357 * context up to STANDBY as unless something
1358 * else is active and passing audio they
1359 * generally don't require full power.
1360 */
1361 switch (w->id) {
1362 case snd_soc_dapm_supply:
1363 case snd_soc_dapm_micbias:
1364 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1365 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1366 break;
1367 default:
1368 d->target_bias_level = SND_SOC_BIAS_ON;
1369 break;
1370 }
1371 }
1372
1373 }
1374
Mark Brownb14b76a2009-08-17 11:55:38 +01001375 /* If there are no DAPM widgets then try to figure out power from the
1376 * event type.
1377 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001378 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001379 switch (event) {
1380 case SND_SOC_DAPM_STREAM_START:
1381 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001382 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a2009-08-17 11:55:38 +01001383 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001384 case SND_SOC_DAPM_STREAM_STOP:
Mark Brown56fba412011-06-04 11:25:10 +01001385 if (dapm->codec->active)
1386 dapm->target_bias_level = SND_SOC_BIAS_ON;
1387 else
1388 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001389 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001390 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001391 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001392 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001393 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001394 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001395 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001396 default:
1397 break;
1398 }
1399 }
1400
Mark Brown85a843c2011-09-21 21:29:47 +01001401 /* Force all contexts in the card to the same bias state if
1402 * they're not ground referenced.
1403 */
Mark Brown56fba412011-06-04 11:25:10 +01001404 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001405 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001406 if (d->target_bias_level > bias)
1407 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001408 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001409 if (!d->idle_bias_off)
1410 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001411
Mark Brownde02d072011-09-20 21:43:24 +01001412 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001413
Mark Brown9d0624a2011-02-18 11:49:43 -08001414 /* Run all the bias changes in parallel */
1415 list_for_each_entry(d, &dapm->card->dapm_list, list)
1416 async_schedule_domain(dapm_pre_sequence_async, d,
1417 &async_domain);
1418 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001419
Mark Brown6d3ddc82009-05-16 17:47:29 +01001420 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001421 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001422
Mark Brown97404f22010-12-14 16:13:57 +00001423 dapm_widget_update(dapm);
1424
Mark Brown6d3ddc82009-05-16 17:47:29 +01001425 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001426 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001427
Mark Brown9d0624a2011-02-18 11:49:43 -08001428 /* Run all the bias changes in parallel */
1429 list_for_each_entry(d, &dapm->card->dapm_list, list)
1430 async_schedule_domain(dapm_post_sequence_async, d,
1431 &async_domain);
1432 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001433
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001434 pop_dbg(dapm->dev, card->pop_time,
1435 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001436 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001437
Mark Brown84e90932010-11-04 00:07:02 -04001438 trace_snd_soc_dapm_done(card);
1439
Mark Brown42aa3412009-03-01 19:21:10 +00001440 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001441}
1442
Mark Brown79fb9382009-08-21 16:38:13 +01001443#ifdef CONFIG_DEBUG_FS
1444static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1445{
1446 file->private_data = inode->i_private;
1447 return 0;
1448}
1449
1450static ssize_t dapm_widget_power_read_file(struct file *file,
1451 char __user *user_buf,
1452 size_t count, loff_t *ppos)
1453{
1454 struct snd_soc_dapm_widget *w = file->private_data;
1455 char *buf;
1456 int in, out;
1457 ssize_t ret;
1458 struct snd_soc_dapm_path *p = NULL;
1459
1460 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1461 if (!buf)
1462 return -ENOMEM;
1463
1464 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001465 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001466 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001467 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001468
Mark Brownd033c362009-12-04 15:25:56 +00001469 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001470 w->name, w->power ? "On" : "Off", in, out);
1471
Mark Brownd033c362009-12-04 15:25:56 +00001472 if (w->reg >= 0)
1473 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1474 " - R%d(0x%x) bit %d",
1475 w->reg, w->reg, w->shift);
1476
1477 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1478
Mark Brown3eef08b2009-09-14 16:49:00 +01001479 if (w->sname)
1480 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1481 w->sname,
1482 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001483
1484 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001485 if (p->connected && !p->connected(w, p->sink))
1486 continue;
1487
Mark Brown79fb9382009-08-21 16:38:13 +01001488 if (p->connect)
1489 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001490 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001491 p->name ? p->name : "static",
1492 p->source->name);
1493 }
1494 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001495 if (p->connected && !p->connected(w, p->sink))
1496 continue;
1497
Mark Brown79fb9382009-08-21 16:38:13 +01001498 if (p->connect)
1499 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001500 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001501 p->name ? p->name : "static",
1502 p->sink->name);
1503 }
1504
1505 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1506
1507 kfree(buf);
1508 return ret;
1509}
1510
1511static const struct file_operations dapm_widget_power_fops = {
1512 .open = dapm_widget_power_open_file,
1513 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001514 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001515};
1516
Mark Brownef49e4f2011-04-04 20:48:13 +09001517static int dapm_bias_open_file(struct inode *inode, struct file *file)
1518{
1519 file->private_data = inode->i_private;
1520 return 0;
1521}
1522
1523static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1524 size_t count, loff_t *ppos)
1525{
1526 struct snd_soc_dapm_context *dapm = file->private_data;
1527 char *level;
1528
1529 switch (dapm->bias_level) {
1530 case SND_SOC_BIAS_ON:
1531 level = "On\n";
1532 break;
1533 case SND_SOC_BIAS_PREPARE:
1534 level = "Prepare\n";
1535 break;
1536 case SND_SOC_BIAS_STANDBY:
1537 level = "Standby\n";
1538 break;
1539 case SND_SOC_BIAS_OFF:
1540 level = "Off\n";
1541 break;
1542 default:
1543 BUG();
1544 level = "Unknown\n";
1545 break;
1546 }
1547
1548 return simple_read_from_buffer(user_buf, count, ppos, level,
1549 strlen(level));
1550}
1551
1552static const struct file_operations dapm_bias_fops = {
1553 .open = dapm_bias_open_file,
1554 .read = dapm_bias_read_file,
1555 .llseek = default_llseek,
1556};
1557
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001558void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1559 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001560{
Mark Brown79fb9382009-08-21 16:38:13 +01001561 struct dentry *d;
1562
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001563 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1564
1565 if (!dapm->debugfs_dapm) {
1566 printk(KERN_WARNING
1567 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001568 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001569 }
Mark Brown79fb9382009-08-21 16:38:13 +01001570
Mark Brownef49e4f2011-04-04 20:48:13 +09001571 d = debugfs_create_file("bias_level", 0444,
1572 dapm->debugfs_dapm, dapm,
1573 &dapm_bias_fops);
1574 if (!d)
1575 dev_warn(dapm->dev,
1576 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001577}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001578
1579static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1580{
1581 struct snd_soc_dapm_context *dapm = w->dapm;
1582 struct dentry *d;
1583
1584 if (!dapm->debugfs_dapm || !w->name)
1585 return;
1586
1587 d = debugfs_create_file(w->name, 0444,
1588 dapm->debugfs_dapm, w,
1589 &dapm_widget_power_fops);
1590 if (!d)
1591 dev_warn(w->dapm->dev,
1592 "ASoC: Failed to create %s debugfs file\n",
1593 w->name);
1594}
1595
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001596static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1597{
1598 debugfs_remove_recursive(dapm->debugfs_dapm);
1599}
1600
Mark Brown79fb9382009-08-21 16:38:13 +01001601#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001602void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1603 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001604{
1605}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001606
1607static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1608{
1609}
1610
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001611static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1612{
1613}
1614
Mark Brown79fb9382009-08-21 16:38:13 +01001615#endif
1616
Richard Purdie2b97eab2006-10-06 18:32:18 +02001617/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001618static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001619 struct snd_kcontrol *kcontrol, int change,
1620 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001621{
1622 struct snd_soc_dapm_path *path;
1623 int found = 0;
1624
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001625 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001626 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001627 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001628 return -ENODEV;
1629
Mark Brown3a655772009-10-05 17:23:30 +01001630 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001631 return 0;
1632
1633 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001634 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001635 if (path->kcontrol != kcontrol)
1636 continue;
1637
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001638 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001639 continue;
1640
1641 found = 1;
1642 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001643 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001644 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001645 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001646 } else {
1647 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001648 dapm_mark_dirty(path->source,
1649 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001650 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001651 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001652 }
1653
Mark Browndb432b42011-10-03 21:06:40 +01001654 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001655 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001656 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001657 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001658
1659 return 0;
1660}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001661
Milan plzik1b075e32008-01-10 14:39:46 +01001662/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001663static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001664 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001665{
1666 struct snd_soc_dapm_path *path;
1667 int found = 0;
1668
Milan plzik1b075e32008-01-10 14:39:46 +01001669 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001670 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001671 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001672 return -ENODEV;
1673
Richard Purdie2b97eab2006-10-06 18:32:18 +02001674 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001675 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001676 if (path->kcontrol != kcontrol)
1677 continue;
1678
1679 /* found, now check type */
1680 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001681 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001682 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001683 }
1684
Mark Browndb432b42011-10-03 21:06:40 +01001685 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001686 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001687 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001688 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001689
1690 return 0;
1691}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001692
1693/* show dapm widget status in sys fs */
1694static ssize_t dapm_widget_show(struct device *dev,
1695 struct device_attribute *attr, char *buf)
1696{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001697 struct snd_soc_pcm_runtime *rtd =
1698 container_of(dev, struct snd_soc_pcm_runtime, dev);
1699 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001700 struct snd_soc_dapm_widget *w;
1701 int count = 0;
1702 char *state = "not set";
1703
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001704 list_for_each_entry(w, &codec->card->widgets, list) {
1705 if (w->dapm != &codec->dapm)
1706 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001707
1708 /* only display widgets that burnm power */
1709 switch (w->id) {
1710 case snd_soc_dapm_hp:
1711 case snd_soc_dapm_mic:
1712 case snd_soc_dapm_spk:
1713 case snd_soc_dapm_line:
1714 case snd_soc_dapm_micbias:
1715 case snd_soc_dapm_dac:
1716 case snd_soc_dapm_adc:
1717 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001718 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001719 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001720 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001721 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001722 if (w->name)
1723 count += sprintf(buf + count, "%s: %s\n",
1724 w->name, w->power ? "On":"Off");
1725 break;
1726 default:
1727 break;
1728 }
1729 }
1730
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001731 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001732 case SND_SOC_BIAS_ON:
1733 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001734 break;
Mark Brown0be98982008-05-19 12:31:28 +02001735 case SND_SOC_BIAS_PREPARE:
1736 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001737 break;
Mark Brown0be98982008-05-19 12:31:28 +02001738 case SND_SOC_BIAS_STANDBY:
1739 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001740 break;
Mark Brown0be98982008-05-19 12:31:28 +02001741 case SND_SOC_BIAS_OFF:
1742 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001743 break;
1744 }
1745 count += sprintf(buf + count, "PM State: %s\n", state);
1746
1747 return count;
1748}
1749
1750static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1751
1752int snd_soc_dapm_sys_add(struct device *dev)
1753{
Troy Kisky12ef1932008-10-13 17:42:14 -07001754 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001755}
1756
1757static void snd_soc_dapm_sys_remove(struct device *dev)
1758{
Mark Brownaef90842009-05-16 17:53:16 +01001759 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001760}
1761
1762/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001763static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001764{
1765 struct snd_soc_dapm_widget *w, *next_w;
1766 struct snd_soc_dapm_path *p, *next_p;
1767
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001768 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1769 if (w->dapm != dapm)
1770 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001771 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001772 /*
1773 * remove source and sink paths associated to this widget.
1774 * While removing the path, remove reference to it from both
1775 * source and sink widgets so that path is removed only once.
1776 */
1777 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1778 list_del(&p->list_sink);
1779 list_del(&p->list_source);
1780 list_del(&p->list);
1781 kfree(p->long_name);
1782 kfree(p);
1783 }
1784 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1785 list_del(&p->list_sink);
1786 list_del(&p->list_source);
1787 list_del(&p->list);
1788 kfree(p->long_name);
1789 kfree(p);
1790 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001791 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001792 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001793 kfree(w);
1794 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001795}
1796
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001797static struct snd_soc_dapm_widget *dapm_find_widget(
1798 struct snd_soc_dapm_context *dapm, const char *pin,
1799 bool search_other_contexts)
1800{
1801 struct snd_soc_dapm_widget *w;
1802 struct snd_soc_dapm_widget *fallback = NULL;
1803
1804 list_for_each_entry(w, &dapm->card->widgets, list) {
1805 if (!strcmp(w->name, pin)) {
1806 if (w->dapm == dapm)
1807 return w;
1808 else
1809 fallback = w;
1810 }
1811 }
1812
1813 if (search_other_contexts)
1814 return fallback;
1815
1816 return NULL;
1817}
1818
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001819static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001820 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001821{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001822 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001823
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001824 if (!w) {
1825 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1826 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001827 }
1828
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001829 w->connected = status;
1830 if (status == 0)
1831 w->force = 0;
Mark Brown75c1f892011-10-04 22:28:08 +01001832 dapm_mark_dirty(w, "pin configuration");
Mark Brown0d867332011-04-06 11:38:14 +09001833
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001834 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001835}
1836
Richard Purdie2b97eab2006-10-06 18:32:18 +02001837/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001838 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001839 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001840 *
1841 * Walks all dapm audio paths and powers widgets according to their
1842 * stream or path usage.
1843 *
1844 * Returns 0 for success.
1845 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001846int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001847{
Mark Brown4f4c0072011-10-07 14:29:19 +01001848 /*
1849 * Suppress early reports (eg, jacks syncing their state) to avoid
1850 * silly DAPM runs during card startup.
1851 */
1852 if (!dapm->card || !dapm->card->instantiated)
1853 return 0;
1854
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001855 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001856}
Liam Girdwooda5302182008-07-07 13:35:17 +01001857EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001858
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001859static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001860 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001861{
1862 struct snd_soc_dapm_path *path;
1863 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001864 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001865 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001866 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001867 const char *source;
1868 char prefixed_sink[80];
1869 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001870 int ret = 0;
1871
Mark Brown88e8b9a2011-03-02 18:18:24 +00001872 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001873 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1874 dapm->codec->name_prefix, route->sink);
1875 sink = prefixed_sink;
1876 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1877 dapm->codec->name_prefix, route->source);
1878 source = prefixed_source;
1879 } else {
1880 sink = route->sink;
1881 source = route->source;
1882 }
1883
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001884 /*
1885 * find src and dest widgets over all widgets but favor a widget from
1886 * current DAPM context
1887 */
1888 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001889 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001890 wtsink = w;
1891 if (w->dapm == dapm)
1892 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001893 continue;
1894 }
1895 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001896 wtsource = w;
1897 if (w->dapm == dapm)
1898 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001899 }
1900 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001901 /* use widget from another DAPM context if not found from this */
1902 if (!wsink)
1903 wsink = wtsink;
1904 if (!wsource)
1905 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001906
1907 if (wsource == NULL || wsink == NULL)
1908 return -ENODEV;
1909
1910 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1911 if (!path)
1912 return -ENOMEM;
1913
1914 path->source = wsource;
1915 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001916 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001917 INIT_LIST_HEAD(&path->list);
1918 INIT_LIST_HEAD(&path->list_source);
1919 INIT_LIST_HEAD(&path->list_sink);
1920
1921 /* check for external widgets */
1922 if (wsink->id == snd_soc_dapm_input) {
1923 if (wsource->id == snd_soc_dapm_micbias ||
1924 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001925 wsource->id == snd_soc_dapm_line ||
1926 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001927 wsink->ext = 1;
1928 }
1929 if (wsource->id == snd_soc_dapm_output) {
1930 if (wsink->id == snd_soc_dapm_spk ||
1931 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001932 wsink->id == snd_soc_dapm_line ||
1933 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001934 wsource->ext = 1;
1935 }
1936
1937 /* connect static paths */
1938 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001939 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001940 list_add(&path->list_sink, &wsink->sources);
1941 list_add(&path->list_source, &wsource->sinks);
1942 path->connect = 1;
1943 return 0;
1944 }
1945
1946 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001947 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001948 case snd_soc_dapm_adc:
1949 case snd_soc_dapm_dac:
1950 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001951 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001952 case snd_soc_dapm_input:
1953 case snd_soc_dapm_output:
1954 case snd_soc_dapm_micbias:
1955 case snd_soc_dapm_vmid:
1956 case snd_soc_dapm_pre:
1957 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001958 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001959 case snd_soc_dapm_aif_in:
1960 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001961 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001962 list_add(&path->list_sink, &wsink->sources);
1963 list_add(&path->list_source, &wsource->sinks);
1964 path->connect = 1;
1965 return 0;
1966 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001967 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001968 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001969 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06001970 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001971 if (ret != 0)
1972 goto err;
1973 break;
1974 case snd_soc_dapm_switch:
1975 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001976 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001977 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001978 if (ret != 0)
1979 goto err;
1980 break;
1981 case snd_soc_dapm_hp:
1982 case snd_soc_dapm_mic:
1983 case snd_soc_dapm_line:
1984 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001985 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001986 list_add(&path->list_sink, &wsink->sources);
1987 list_add(&path->list_source, &wsource->sinks);
1988 path->connect = 0;
1989 return 0;
1990 }
1991 return 0;
1992
1993err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001994 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1995 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001996 kfree(path);
1997 return ret;
1998}
Mark Brown105f1c22008-05-13 14:52:19 +02001999
2000/**
Mark Brown105f1c22008-05-13 14:52:19 +02002001 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002002 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002003 * @route: audio routes
2004 * @num: number of routes
2005 *
2006 * Connects 2 dapm widgets together via a named audio path. The sink is
2007 * the widget receiving the audio signal, whilst the source is the sender
2008 * of the audio signal.
2009 *
2010 * Returns 0 for success else error. On error all resources can be freed
2011 * with a call to snd_soc_card_free().
2012 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002013int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002014 const struct snd_soc_dapm_route *route, int num)
2015{
2016 int i, ret;
2017
2018 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002019 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02002020 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002021 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2022 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02002023 return ret;
2024 }
2025 route++;
2026 }
2027
2028 return 0;
2029}
2030EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2031
Mark Brownbf3a9e12011-06-13 16:42:29 +01002032static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2033 const struct snd_soc_dapm_route *route)
2034{
2035 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2036 route->source,
2037 true);
2038 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2039 route->sink,
2040 true);
2041 struct snd_soc_dapm_path *path;
2042 int count = 0;
2043
2044 if (!source) {
2045 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2046 route->source);
2047 return -ENODEV;
2048 }
2049
2050 if (!sink) {
2051 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2052 route->sink);
2053 return -ENODEV;
2054 }
2055
2056 if (route->control || route->connected)
2057 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2058 route->source, route->sink);
2059
2060 list_for_each_entry(path, &source->sinks, list_source) {
2061 if (path->sink == sink) {
2062 path->weak = 1;
2063 count++;
2064 }
2065 }
2066
2067 if (count == 0)
2068 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2069 route->source, route->sink);
2070 if (count > 1)
2071 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2072 count, route->source, route->sink);
2073
2074 return 0;
2075}
2076
2077/**
2078 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2079 * @dapm: DAPM context
2080 * @route: audio routes
2081 * @num: number of routes
2082 *
2083 * Mark existing routes matching those specified in the passed array
2084 * as being weak, meaning that they are ignored for the purpose of
2085 * power decisions. The main intended use case is for sidetone paths
2086 * which couple audio between other independent paths if they are both
2087 * active in order to make the combination work better at the user
2088 * level but which aren't intended to be "used".
2089 *
2090 * Note that CODEC drivers should not use this as sidetone type paths
2091 * can frequently also be used as bypass paths.
2092 */
2093int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2094 const struct snd_soc_dapm_route *route, int num)
2095{
2096 int i, err;
2097 int ret = 0;
2098
2099 for (i = 0; i < num; i++) {
2100 err = snd_soc_dapm_weak_route(dapm, route);
2101 if (err)
2102 ret = err;
2103 route++;
2104 }
2105
2106 return ret;
2107}
2108EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2109
Mark Brown105f1c22008-05-13 14:52:19 +02002110/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002111 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002112 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002113 *
2114 * Checks the codec for any new dapm widgets and creates them if found.
2115 *
2116 * Returns 0 for success.
2117 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002118int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002119{
2120 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002121 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002122
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002123 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002124 {
2125 if (w->new)
2126 continue;
2127
Stephen Warrenfad59882011-04-28 17:37:59 -06002128 if (w->num_kcontrols) {
2129 w->kcontrols = kzalloc(w->num_kcontrols *
2130 sizeof(struct snd_kcontrol *),
2131 GFP_KERNEL);
2132 if (!w->kcontrols)
2133 return -ENOMEM;
2134 }
2135
Richard Purdie2b97eab2006-10-06 18:32:18 +02002136 switch(w->id) {
2137 case snd_soc_dapm_switch:
2138 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002139 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01002140 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002141 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002142 break;
2143 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002144 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002145 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01002146 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002147 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002148 break;
2149 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01002150 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01002151 w->power_check = dapm_adc_check_power;
2152 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002153 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01002154 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01002155 w->power_check = dapm_dac_check_power;
2156 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002157 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002158 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01002159 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002160 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002161 break;
2162 case snd_soc_dapm_input:
2163 case snd_soc_dapm_output:
2164 case snd_soc_dapm_micbias:
2165 case snd_soc_dapm_spk:
2166 case snd_soc_dapm_hp:
2167 case snd_soc_dapm_mic:
2168 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01002169 w->power_check = dapm_generic_check_power;
2170 break;
Mark Brown246d0a12009-04-22 18:24:55 +01002171 case snd_soc_dapm_supply:
2172 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002173 case snd_soc_dapm_vmid:
2174 case snd_soc_dapm_pre:
2175 case snd_soc_dapm_post:
2176 break;
2177 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002178
Mark Brown35c64bca2011-09-28 18:23:53 +01002179 if (!w->power_check)
2180 w->power_check = dapm_always_on_check_power;
2181
Mark Brownb66a70d2011-02-09 18:04:11 +00002182 /* Read the initial power state from the device */
2183 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002184 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002185 val &= 1 << w->shift;
2186 if (w->invert)
2187 val = !val;
2188
2189 if (val)
2190 w->power = 1;
2191 }
2192
Richard Purdie2b97eab2006-10-06 18:32:18 +02002193 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002194
Mark Brown7508b122011-10-05 12:09:12 +01002195 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002196 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197 }
2198
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002199 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002200 return 0;
2201}
2202EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2203
2204/**
2205 * snd_soc_dapm_get_volsw - dapm mixer get callback
2206 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002207 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002208 *
2209 * Callback to get the value of a dapm mixer control.
2210 *
2211 * Returns 0 for success.
2212 */
2213int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2214 struct snd_ctl_elem_value *ucontrol)
2215{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002216 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2217 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002218 struct soc_mixer_control *mc =
2219 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002220 unsigned int reg = mc->reg;
2221 unsigned int shift = mc->shift;
2222 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002223 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002224 unsigned int invert = mc->invert;
2225 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002226
Richard Purdie2b97eab2006-10-06 18:32:18 +02002227 ucontrol->value.integer.value[0] =
2228 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2229 if (shift != rshift)
2230 ucontrol->value.integer.value[1] =
2231 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2232 if (invert) {
2233 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002234 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002235 if (shift != rshift)
2236 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002237 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002238 }
2239
2240 return 0;
2241}
2242EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2243
2244/**
2245 * snd_soc_dapm_put_volsw - dapm mixer set callback
2246 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002247 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002248 *
2249 * Callback to set the value of a dapm mixer control.
2250 *
2251 * Returns 0 for success.
2252 */
2253int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2254 struct snd_ctl_elem_value *ucontrol)
2255{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002256 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2257 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2258 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002259 struct soc_mixer_control *mc =
2260 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002261 unsigned int reg = mc->reg;
2262 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002263 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002264 unsigned int mask = (1 << fls(max)) - 1;
2265 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002266 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002267 int connect, change;
2268 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002269 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002270
2271 val = (ucontrol->value.integer.value[0] & mask);
2272
2273 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002274 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002275 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002276 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002277
Stephen Warrenfafd2172011-04-28 17:38:00 -06002278 if (val)
2279 /* new connection */
2280 connect = invert ? 0 : 1;
2281 else
2282 /* old connection must be powered down */
2283 connect = invert ? 1 : 0;
2284
2285 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002286
Stephen Warrene9cf7042011-01-27 14:54:05 -07002287 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002288 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002289 for (wi = 0; wi < wlist->num_widgets; wi++) {
2290 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002291
Stephen Warrenfafd2172011-04-28 17:38:00 -06002292 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002293
Stephen Warrenfafd2172011-04-28 17:38:00 -06002294 update.kcontrol = kcontrol;
2295 update.widget = widget;
2296 update.reg = reg;
2297 update.mask = mask;
2298 update.val = val;
2299 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002300
Stephen Warrenfafd2172011-04-28 17:38:00 -06002301 dapm_mixer_update_power(widget, kcontrol, connect);
2302
2303 widget->dapm->update = NULL;
2304 }
Mark Brown283375c2009-12-07 18:09:03 +00002305 }
2306
Stephen Warrenfafd2172011-04-28 17:38:00 -06002307 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002308 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002309}
2310EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2311
2312/**
2313 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2314 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002315 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002316 *
2317 * Callback to get the value of a dapm enumerated double mixer control.
2318 *
2319 * Returns 0 for success.
2320 */
2321int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2322 struct snd_ctl_elem_value *ucontrol)
2323{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002324 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2325 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002326 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002327 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002328
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002329 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002330 ;
2331 val = snd_soc_read(widget->codec, e->reg);
2332 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2333 if (e->shift_l != e->shift_r)
2334 ucontrol->value.enumerated.item[1] =
2335 (val >> e->shift_r) & (bitmask - 1);
2336
2337 return 0;
2338}
2339EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2340
2341/**
2342 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2343 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002344 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002345 *
2346 * Callback to set the value of a dapm enumerated double mixer control.
2347 *
2348 * Returns 0 for success.
2349 */
2350int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2351 struct snd_ctl_elem_value *ucontrol)
2352{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002353 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2354 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2355 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002356 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002357 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002358 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002359 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002360 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002361
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002362 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002363 ;
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002364 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002365 return -EINVAL;
2366 mux = ucontrol->value.enumerated.item[0];
2367 val = mux << e->shift_l;
2368 mask = (bitmask - 1) << e->shift_l;
2369 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b7b2008-07-29 11:42:27 +01002370 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002371 return -EINVAL;
2372 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2373 mask |= (bitmask - 1) << e->shift_r;
2374 }
2375
Stephen Warrenfafd2172011-04-28 17:38:00 -06002376 mutex_lock(&codec->mutex);
2377
Mark Brown3a655772009-10-05 17:23:30 +01002378 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002379 if (change) {
2380 for (wi = 0; wi < wlist->num_widgets; wi++) {
2381 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002382
Stephen Warrenfafd2172011-04-28 17:38:00 -06002383 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002384
Stephen Warrenfafd2172011-04-28 17:38:00 -06002385 update.kcontrol = kcontrol;
2386 update.widget = widget;
2387 update.reg = e->reg;
2388 update.mask = mask;
2389 update.val = val;
2390 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002391
Stephen Warrenfafd2172011-04-28 17:38:00 -06002392 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002393
Stephen Warrenfafd2172011-04-28 17:38:00 -06002394 widget->dapm->update = NULL;
2395 }
2396 }
2397
2398 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002399 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002400}
2401EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2402
2403/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002404 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2405 * @kcontrol: mixer control
2406 * @ucontrol: control element information
2407 *
2408 * Returns 0 for success.
2409 */
2410int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2411 struct snd_ctl_elem_value *ucontrol)
2412{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002413 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2414 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002415
2416 ucontrol->value.enumerated.item[0] = widget->value;
2417
2418 return 0;
2419}
2420EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2421
2422/**
2423 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2424 * @kcontrol: mixer control
2425 * @ucontrol: control element information
2426 *
2427 * Returns 0 for success.
2428 */
2429int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2430 struct snd_ctl_elem_value *ucontrol)
2431{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002432 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2433 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2434 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002435 struct soc_enum *e =
2436 (struct soc_enum *)kcontrol->private_value;
2437 int change;
2438 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002439 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002440
2441 if (ucontrol->value.enumerated.item[0] >= e->max)
2442 return -EINVAL;
2443
Stephen Warrenfafd2172011-04-28 17:38:00 -06002444 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002445
2446 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002447 if (change) {
2448 for (wi = 0; wi < wlist->num_widgets; wi++) {
2449 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002450
Stephen Warrenfafd2172011-04-28 17:38:00 -06002451 widget->value = ucontrol->value.enumerated.item[0];
2452
2453 dapm_mux_update_power(widget, kcontrol, change,
2454 widget->value, e);
2455 }
2456 }
2457
2458 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002459 return ret;
2460}
2461EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2462
2463/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002464 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2465 * callback
2466 * @kcontrol: mixer control
2467 * @ucontrol: control element information
2468 *
2469 * Callback to get the value of a dapm semi enumerated double mixer control.
2470 *
2471 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2472 * used for handling bitfield coded enumeration for example.
2473 *
2474 * Returns 0 for success.
2475 */
2476int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2477 struct snd_ctl_elem_value *ucontrol)
2478{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002479 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2480 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002481 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002482 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002483
2484 reg_val = snd_soc_read(widget->codec, e->reg);
2485 val = (reg_val >> e->shift_l) & e->mask;
2486 for (mux = 0; mux < e->max; mux++) {
2487 if (val == e->values[mux])
2488 break;
2489 }
2490 ucontrol->value.enumerated.item[0] = mux;
2491 if (e->shift_l != e->shift_r) {
2492 val = (reg_val >> e->shift_r) & e->mask;
2493 for (mux = 0; mux < e->max; mux++) {
2494 if (val == e->values[mux])
2495 break;
2496 }
2497 ucontrol->value.enumerated.item[1] = mux;
2498 }
2499
2500 return 0;
2501}
2502EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2503
2504/**
2505 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2506 * callback
2507 * @kcontrol: mixer control
2508 * @ucontrol: control element information
2509 *
2510 * Callback to set the value of a dapm semi enumerated double mixer control.
2511 *
2512 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2513 * used for handling bitfield coded enumeration for example.
2514 *
2515 * Returns 0 for success.
2516 */
2517int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2518 struct snd_ctl_elem_value *ucontrol)
2519{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002520 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2521 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2522 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002523 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002524 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002525 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002526 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002527 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002528
2529 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2530 return -EINVAL;
2531 mux = ucontrol->value.enumerated.item[0];
2532 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2533 mask = e->mask << e->shift_l;
2534 if (e->shift_l != e->shift_r) {
2535 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2536 return -EINVAL;
2537 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2538 mask |= e->mask << e->shift_r;
2539 }
2540
Stephen Warrenfafd2172011-04-28 17:38:00 -06002541 mutex_lock(&codec->mutex);
2542
Mark Brown3a655772009-10-05 17:23:30 +01002543 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002544 if (change) {
2545 for (wi = 0; wi < wlist->num_widgets; wi++) {
2546 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002547
Stephen Warrenfafd2172011-04-28 17:38:00 -06002548 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002549
Stephen Warrenfafd2172011-04-28 17:38:00 -06002550 update.kcontrol = kcontrol;
2551 update.widget = widget;
2552 update.reg = e->reg;
2553 update.mask = mask;
2554 update.val = val;
2555 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002556
Stephen Warrenfafd2172011-04-28 17:38:00 -06002557 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002558
Stephen Warrenfafd2172011-04-28 17:38:00 -06002559 widget->dapm->update = NULL;
2560 }
2561 }
2562
2563 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002564 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002565}
2566EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2567
2568/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002569 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2570 *
2571 * @kcontrol: mixer control
2572 * @uinfo: control element information
2573 *
2574 * Callback to provide information about a pin switch control.
2575 */
2576int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2577 struct snd_ctl_elem_info *uinfo)
2578{
2579 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2580 uinfo->count = 1;
2581 uinfo->value.integer.min = 0;
2582 uinfo->value.integer.max = 1;
2583
2584 return 0;
2585}
2586EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2587
2588/**
2589 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2590 *
2591 * @kcontrol: mixer control
2592 * @ucontrol: Value
2593 */
2594int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2595 struct snd_ctl_elem_value *ucontrol)
2596{
2597 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2598 const char *pin = (const char *)kcontrol->private_value;
2599
2600 mutex_lock(&codec->mutex);
2601
2602 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002603 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002604
2605 mutex_unlock(&codec->mutex);
2606
2607 return 0;
2608}
2609EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2610
2611/**
2612 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2613 *
2614 * @kcontrol: mixer control
2615 * @ucontrol: Value
2616 */
2617int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2618 struct snd_ctl_elem_value *ucontrol)
2619{
2620 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2621 const char *pin = (const char *)kcontrol->private_value;
2622
2623 mutex_lock(&codec->mutex);
2624
2625 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002626 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002627 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002628 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002629
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002630 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002631
2632 mutex_unlock(&codec->mutex);
2633
2634 return 0;
2635}
2636EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2637
2638/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002639 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002640 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002641 * @widget: widget template
2642 *
2643 * Creates a new dapm control based upon the template.
2644 *
2645 * Returns 0 for success else error.
2646 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002647int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002648 const struct snd_soc_dapm_widget *widget)
2649{
2650 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002651 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002652
2653 if ((w = dapm_cnew_widget(widget)) == NULL)
2654 return -ENOMEM;
2655
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002656 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002657 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002658 name_len += 1 + strlen(dapm->codec->name_prefix);
2659 w->name = kmalloc(name_len, GFP_KERNEL);
2660 if (w->name == NULL) {
2661 kfree(w);
2662 return -ENOMEM;
2663 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002664 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002665 snprintf(w->name, name_len, "%s %s",
2666 dapm->codec->name_prefix, widget->name);
2667 else
2668 snprintf(w->name, name_len, "%s", widget->name);
2669
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002670 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002671 w->dapm = dapm;
2672 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002673 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002674 INIT_LIST_HEAD(&w->sources);
2675 INIT_LIST_HEAD(&w->sinks);
2676 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01002677 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002678 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002679
2680 /* machine layer set ups unconnected pins and insertions */
2681 w->connected = 1;
2682 return 0;
2683}
2684EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2685
2686/**
Mark Brown4ba13272008-05-13 14:51:19 +02002687 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002688 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002689 * @widget: widget array
2690 * @num: number of widgets
2691 *
2692 * Creates new DAPM controls based upon the templates.
2693 *
2694 * Returns 0 for success else error.
2695 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002696int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002697 const struct snd_soc_dapm_widget *widget,
2698 int num)
2699{
2700 int i, ret;
2701
2702 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002703 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002704 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002705 dev_err(dapm->dev,
2706 "ASoC: Failed to create DAPM control %s: %d\n",
2707 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002708 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002709 }
Mark Brown4ba13272008-05-13 14:51:19 +02002710 widget++;
2711 }
2712 return 0;
2713}
2714EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2715
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002716static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002717 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002718{
2719 struct snd_soc_dapm_widget *w;
2720
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002721 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002722 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002723 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002724 continue;
Liam Girdwoodee47b362011-07-25 11:15:50 +01002725 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002726 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002727 if (strstr(w->sname, stream)) {
Mark Brown75c1f892011-10-04 22:28:08 +01002728 dapm_mark_dirty(w, "stream event");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002729 switch(event) {
2730 case SND_SOC_DAPM_STREAM_START:
2731 w->active = 1;
2732 break;
2733 case SND_SOC_DAPM_STREAM_STOP:
2734 w->active = 0;
2735 break;
2736 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002737 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002738 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002739 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2740 break;
2741 }
2742 }
2743 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002744
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002745 dapm_power_widgets(dapm, event);
Liam Girdwood64a648c222011-07-25 11:15:15 +01002746
2747 /* do we need to notify any clients that DAPM stream is complete */
2748 if (dapm->stream_event)
2749 dapm->stream_event(dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002750}
2751
2752/**
2753 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2754 * @rtd: PCM runtime data
2755 * @stream: stream name
2756 * @event: stream event
2757 *
2758 * Sends a stream event to the dapm core. The core then makes any
2759 * necessary widget power changes.
2760 *
2761 * Returns 0 for success else error.
2762 */
2763int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2764 const char *stream, int event)
2765{
2766 struct snd_soc_codec *codec = rtd->codec;
2767
2768 if (stream == NULL)
2769 return 0;
2770
2771 mutex_lock(&codec->mutex);
2772 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002773 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002774 return 0;
2775}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002776
2777/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002778 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002779 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002780 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002781 *
Mark Brown74b8f952009-06-06 11:26:15 +01002782 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002783 * a valid audio route and active audio stream.
2784 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2785 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002786 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002787int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002788{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002789 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002790}
Liam Girdwooda5302182008-07-07 13:35:17 +01002791EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002792
2793/**
Mark Brownda341832010-03-15 19:23:37 +00002794 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002795 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002796 * @pin: pin name
2797 *
2798 * Enables input/output pin regardless of any other state. This is
2799 * intended for use with microphone bias supplies used in microphone
2800 * jack detection.
2801 *
2802 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2803 * do any widget power switching.
2804 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002805int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2806 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002807{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002808 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002809
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002810 if (!w) {
2811 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2812 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002813 }
2814
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002815 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2816 w->connected = 1;
2817 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01002818 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09002819
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002820 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002821}
2822EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2823
2824/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002825 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002826 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002827 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002828 *
Mark Brown74b8f952009-06-06 11:26:15 +01002829 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002830 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2831 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002832 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002833int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2834 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002835{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002836 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002837}
2838EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2839
2840/**
Mark Brown5817b522008-09-24 11:23:11 +01002841 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002842 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002843 * @pin: pin name
2844 *
2845 * Marks the specified pin as being not connected, disabling it along
2846 * any parent or child widgets. At present this is identical to
2847 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2848 * additional things such as disabling controls which only affect
2849 * paths through the pin.
2850 *
2851 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2852 * do any widget power switching.
2853 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002854int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002855{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002856 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002857}
2858EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2859
2860/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002861 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002862 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002863 * @pin: audio signal pin endpoint (or start point)
2864 *
2865 * Get audio pin status - connected or disconnected.
2866 *
2867 * Returns 1 for connected otherwise 0.
2868 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002869int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2870 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002871{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002872 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002873
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002874 if (w)
2875 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002876
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002877 return 0;
2878}
Liam Girdwooda5302182008-07-07 13:35:17 +01002879EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002880
2881/**
Mark Brown1547aba2010-05-07 21:11:40 +01002882 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002883 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002884 * @pin: audio signal pin endpoint (or start point)
2885 *
2886 * Mark the given endpoint or pin as ignoring suspend. When the
2887 * system is disabled a path between two endpoints flagged as ignoring
2888 * suspend will not be disabled. The path must already be enabled via
2889 * normal means at suspend time, it will not be turned on if it was not
2890 * already enabled.
2891 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002892int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2893 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002894{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002895 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002896
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002897 if (!w) {
2898 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2899 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002900 }
2901
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002902 w->ignore_suspend = 1;
2903
2904 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002905}
2906EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2907
2908/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002909 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03002910 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002911 *
2912 * Free all dapm widgets and resources.
2913 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002914void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002915{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002916 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002917 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002918 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002919 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002920}
2921EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2922
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002923static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002924{
Mark Brown51737472009-06-22 13:16:51 +01002925 struct snd_soc_dapm_widget *w;
2926 LIST_HEAD(down_list);
2927 int powerdown = 0;
2928
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002929 list_for_each_entry(w, &dapm->card->widgets, list) {
2930 if (w->dapm != dapm)
2931 continue;
Mark Brown51737472009-06-22 13:16:51 +01002932 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002933 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002934 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002935 powerdown = 1;
2936 }
2937 }
2938
2939 /* If there were no widgets to power down we're already in
2940 * standby.
2941 */
2942 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002943 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002944 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002945 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002946 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002947}
Mark Brown51737472009-06-22 13:16:51 +01002948
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002949/*
2950 * snd_soc_dapm_shutdown - callback for system shutdown
2951 */
2952void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2953{
2954 struct snd_soc_codec *codec;
2955
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002956 list_for_each_entry(codec, &card->codec_dev_list, list) {
2957 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002958 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002959 }
Mark Brown51737472009-06-22 13:16:51 +01002960}
2961
Richard Purdie2b97eab2006-10-06 18:32:18 +02002962/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002963MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002964MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2965MODULE_LICENSE("GPL");