blob: 62d26350453a13f9e4944bf50ce8ece7eaf0656d [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
Liam Girdwood7987a112011-01-31 19:52:42 +000032#define DEBUG
33
Richard Purdie2b97eab2006-10-06 18:32:18 +020034#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080037#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020038#include <linux/delay.h>
39#include <linux/pm.h>
40#include <linux/bitops.h>
41#include <linux/platform_device.h>
42#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020043#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020045#include <sound/core.h>
46#include <sound/pcm.h>
47#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020048#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020049#include <sound/initval.h>
50
Mark Brown84e90932010-11-04 00:07:02 -040051#include <trace/events/asoc.h>
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
Richard Purdie2b97eab2006-10-06 18:32:18 +0200122/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100123static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200124 const struct snd_soc_dapm_widget *_widget)
125{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100126 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200127}
128
Liam Girdwood7987a112011-01-31 19:52:42 +0000129static inline struct snd_card *dapm_get_card(struct snd_soc_dapm_context *dapm)
130{
131 if (dapm->codec)
132 return dapm->codec->card->snd_card;
133 else if (dapm->platform)
134 return dapm->platform->card->snd_card;
135 else
136 BUG();
137}
138
139static inline struct snd_soc_card *dapm_get_soc_card(
140 struct snd_soc_dapm_context *dapm)
141{
142 if (dapm->codec)
143 return dapm->codec->card;
144 else if (dapm->platform)
145 return dapm->platform->card;
146 else
147 BUG();
148}
149
Liam Girdwood35ca6602011-01-28 17:45:35 +0000150static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
151{
152 if (w->codec)
153 return snd_soc_read(w->codec, reg);
Liam Girdwood7987a112011-01-31 19:52:42 +0000154 else if (w->platform)
155 return snd_soc_platform_read(w->platform, reg);
Liam Girdwood35ca6602011-01-28 17:45:35 +0000156 return 0;
157}
158
159static int soc_widget_write(struct snd_soc_dapm_widget *w,int reg, int val)
160{
161 if (w->codec)
162 return snd_soc_write(w->codec, reg, val);
Liam Girdwood7987a112011-01-31 19:52:42 +0000163 else if (w->platform)
164 return snd_soc_platform_write(w->platform, reg, val);
Liam Girdwood35ca6602011-01-28 17:45:35 +0000165 return 0;
166}
167
168int soc_widget_update_bits(struct snd_soc_dapm_widget *w, unsigned short reg,
169 unsigned int mask, unsigned int value)
170{
171 int change;
172 unsigned int old, new;
173
174 old = soc_widget_read(w, reg);
175 new = (old & ~mask) | value;
176 change = old != new;
177 if (change)
178 soc_widget_write(w, reg, new);
179
180 return change;
181}
182
183int soc_widget_test_bits(struct snd_soc_dapm_widget *w, unsigned short reg,
184 unsigned int mask, unsigned int value)
185{
186 int change;
187 unsigned int old, new;
188
189 old = soc_widget_read(w, reg);
190 new = (old & ~mask) | value;
191 change = old != new;
192
193 return change;
194}
195
196
Mark Brown452c5ea2009-05-17 21:41:23 +0100197/**
198 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800199 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100200 * @level: level to configure
201 *
202 * Configure the bias (power) levels for the SoC audio device.
203 *
204 * Returns 0 for success else error.
205 */
Mark Browned5a4c42011-02-18 11:12:42 -0800206static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200207 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100208{
Mark Browned5a4c42011-02-18 11:12:42 -0800209 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100210 int ret = 0;
211
Mark Brownf83fba82009-05-18 15:44:43 +0100212 switch (level) {
213 case SND_SOC_BIAS_ON:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200214 dev_dbg(dapm->dev, "Setting full bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100215 break;
216 case SND_SOC_BIAS_PREPARE:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200217 dev_dbg(dapm->dev, "Setting bias prepare\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100218 break;
219 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200220 dev_dbg(dapm->dev, "Setting standby bias\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100221 break;
222 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200223 dev_dbg(dapm->dev, "Setting bias off\n");
Mark Brownf83fba82009-05-18 15:44:43 +0100224 break;
225 default:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200226 dev_err(dapm->dev, "Setting invalid bias %d\n", level);
Mark Brownf83fba82009-05-18 15:44:43 +0100227 return -EINVAL;
228 }
229
Mark Brown84e90932010-11-04 00:07:02 -0400230 trace_snd_soc_bias_level_start(card, level);
231
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000232 if (card && card->set_bias_level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100233 ret = card->set_bias_level(card, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100234 if (ret == 0) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200235 if (dapm->codec && dapm->codec->driver->set_bias_level)
236 ret = dapm->codec->driver->set_bias_level(dapm->codec, level);
Mark Brown474e09c2009-08-19 14:18:53 +0100237 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200238 dapm->bias_level = level;
Mark Brown474e09c2009-08-19 14:18:53 +0100239 }
Mark Brown1badabd2010-12-04 12:41:04 +0000240 if (ret == 0) {
241 if (card && card->set_bias_level_post)
242 ret = card->set_bias_level_post(card, level);
243 }
Mark Brown452c5ea2009-05-17 21:41:23 +0100244
Mark Brown84e90932010-11-04 00:07:02 -0400245 trace_snd_soc_bias_level_done(card, level);
246
Mark Brown452c5ea2009-05-17 21:41:23 +0100247 return ret;
248}
249
Richard Purdie2b97eab2006-10-06 18:32:18 +0200250/* set up initial codec paths */
251static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
252 struct snd_soc_dapm_path *p, int i)
253{
254 switch (w->id) {
255 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000256 case snd_soc_dapm_mixer:
257 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200258 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100259 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600260 w->kcontrol_news[i].private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -0400261 unsigned int reg = mc->reg;
262 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100263 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -0400264 unsigned int mask = (1 << fls(max)) - 1;
265 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200266
Liam Girdwood35ca6602011-01-28 17:45:35 +0000267 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200268 val = (val >> shift) & mask;
269
270 if ((invert && !val) || (!invert && val))
271 p->connect = 1;
272 else
273 p->connect = 0;
274 }
275 break;
276 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600277 struct soc_enum *e = (struct soc_enum *)
278 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200279 int val, item, bitmask;
280
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100281 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200282 ;
Liam Girdwood35ca6602011-01-28 17:45:35 +0000283 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200284 item = (val >> e->shift_l) & (bitmask - 1);
285
286 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100287 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200288 if (!(strcmp(p->name, e->texts[i])) && item == i)
289 p->connect = 1;
290 }
291 }
292 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000293 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600294 struct soc_enum *e = (struct soc_enum *)
295 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000296
297 p->connect = 0;
298 /* since a virtual mux has no backing registers to
299 * decide which path to connect, it will try to match
300 * with the first enumeration. This is to ensure
301 * that the default mux choice (the first) will be
302 * correctly powered up during initialization.
303 */
304 if (!strcmp(p->name, e->texts[0]))
305 p->connect = 1;
306 }
307 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200308 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200309 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600310 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200311 int val, item;
312
Liam Girdwood35ca6602011-01-28 17:45:35 +0000313 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200314 val = (val >> e->shift_l) & e->mask;
315 for (item = 0; item < e->max; item++) {
316 if (val == e->values[item])
317 break;
318 }
319
320 p->connect = 0;
321 for (i = 0; i < e->max; i++) {
322 if (!(strcmp(p->name, e->texts[i])) && item == i)
323 p->connect = 1;
324 }
325 }
326 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200327 /* does not effect routing - always connected */
328 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600329 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200330 case snd_soc_dapm_output:
331 case snd_soc_dapm_adc:
332 case snd_soc_dapm_input:
333 case snd_soc_dapm_dac:
334 case snd_soc_dapm_micbias:
335 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100336 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100337 case snd_soc_dapm_aif_in:
338 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200339 p->connect = 1;
340 break;
341 /* does effect routing - dynamically connected */
342 case snd_soc_dapm_hp:
343 case snd_soc_dapm_mic:
344 case snd_soc_dapm_spk:
345 case snd_soc_dapm_line:
346 case snd_soc_dapm_pre:
347 case snd_soc_dapm_post:
348 p->connect = 0;
349 break;
350 }
351}
352
Mark Brown74b8f952009-06-06 11:26:15 +0100353/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200354static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200355 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
356 struct snd_soc_dapm_path *path, const char *control_name,
357 const struct snd_kcontrol_new *kcontrol)
358{
359 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
360 int i;
361
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100362 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200363 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200364 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200365 list_add(&path->list_sink, &dest->sources);
366 list_add(&path->list_source, &src->sinks);
367 path->name = (char*)e->texts[i];
368 dapm_set_path_status(dest, path, 0);
369 return 0;
370 }
371 }
372
373 return -ENODEV;
374}
375
Mark Brown74b8f952009-06-06 11:26:15 +0100376/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200377static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200378 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
379 struct snd_soc_dapm_path *path, const char *control_name)
380{
381 int i;
382
383 /* search for mixer kcontrol */
384 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600385 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200386 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200387 list_add(&path->list_sink, &dest->sources);
388 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600389 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200390 dapm_set_path_status(dest, path, i);
391 return 0;
392 }
393 }
394 return -ENODEV;
395}
396
Stephen Warrenaf468002011-04-28 17:38:01 -0600397static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600398 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600399 const struct snd_kcontrol_new *kcontrol_new,
400 struct snd_kcontrol **kcontrol)
401{
402 struct snd_soc_dapm_widget *w;
403 int i;
404
405 *kcontrol = NULL;
406
407 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600408 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
409 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600410 for (i = 0; i < w->num_kcontrols; i++) {
411 if (&w->kcontrol_news[i] == kcontrol_new) {
412 if (w->kcontrols)
413 *kcontrol = w->kcontrols[i];
414 return 1;
415 }
416 }
417 }
418
419 return 0;
420}
421
Richard Purdie2b97eab2006-10-06 18:32:18 +0200422/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200423static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200424{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200425 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200426 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000427 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200428 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000429 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000430 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600431 struct snd_soc_dapm_widget_list *wlist;
432 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000433
434 if (dapm->codec)
435 prefix = dapm->codec->name_prefix;
436 else
437 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200438
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000439 if (prefix)
440 prefix_len = strlen(prefix) + 1;
441 else
442 prefix_len = 0;
443
Richard Purdie2b97eab2006-10-06 18:32:18 +0200444 /* add kcontrol */
445 for (i = 0; i < w->num_kcontrols; i++) {
446
447 /* match name */
448 list_for_each_entry(path, &w->sources, list_sink) {
449
450 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600451 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200452 continue;
453
Stephen Warrenfafd2172011-04-28 17:38:00 -0600454 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
455 sizeof(struct snd_soc_dapm_widget *),
456 wlist = kzalloc(wlistsize, GFP_KERNEL);
457 if (wlist == NULL) {
458 dev_err(dapm->dev,
459 "asoc: can't allocate widget list for %s\n",
460 w->name);
461 return -ENOMEM;
462 }
463 wlist->num_widgets = 1;
464 wlist->widgets[0] = w;
465
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000466 /* add dapm control with long name.
467 * for dapm_mixer this is the concatenation of the
468 * mixer and kcontrol name.
469 * for dapm_mixer_named_ctl this is simply the
470 * kcontrol name.
471 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600472 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000473 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000474 name_len += 1 + strlen(w->name);
475
Mark Brown219b93f2008-10-28 13:02:31 +0000476 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000477
Stephen Warrenfafd2172011-04-28 17:38:00 -0600478 if (path->long_name == NULL) {
479 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200480 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600481 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200482
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000483 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000484 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000485 /* The control will get a prefix from
486 * the control creation process but
487 * we're also using the same prefix
488 * for widgets so cut the prefix off
489 * the front of the widget name.
490 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000491 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000492 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600493 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000494 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000495 case snd_soc_dapm_mixer_named_ctl:
496 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600497 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000498 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000499 }
500
Mark Brown219b93f2008-10-28 13:02:31 +0000501 path->long_name[name_len - 1] = '\0';
502
Stephen Warrenfafd2172011-04-28 17:38:00 -0600503 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
504 wlist, path->long_name,
505 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200506 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200507 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200508 dev_err(dapm->dev,
509 "asoc: failed to add dapm kcontrol %s: %d\n",
510 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600511 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200512 kfree(path->long_name);
513 path->long_name = NULL;
514 return ret;
515 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600516 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200517 }
518 }
519 return ret;
520}
521
522/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200523static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200524{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200525 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200526 struct snd_soc_dapm_path *path = NULL;
527 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000528 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000529 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000530 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600531 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600532 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600533 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600534 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600535 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200536
Stephen Warrenaf468002011-04-28 17:38:01 -0600537 if (w->num_kcontrols != 1) {
538 dev_err(dapm->dev,
539 "asoc: mux %s has incorrect number of controls\n",
540 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200541 return -EINVAL;
542 }
543
Stephen Warren1007da02011-05-26 09:57:33 -0600544 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600545 &kcontrol);
546 if (kcontrol) {
547 wlist = kcontrol->private_data;
548 wlistentries = wlist->num_widgets + 1;
549 } else {
550 wlist = NULL;
551 wlistentries = 1;
552 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600553 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600554 wlistentries * sizeof(struct snd_soc_dapm_widget *),
555 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600556 if (wlist == NULL) {
557 dev_err(dapm->dev,
558 "asoc: can't allocate widget list for %s\n", w->name);
559 return -ENOMEM;
560 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600561 wlist->num_widgets = wlistentries;
562 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600563
Stephen Warrenaf468002011-04-28 17:38:01 -0600564 if (!kcontrol) {
Liam Girdwood4d74b362011-05-18 17:50:07 +0100565 if (dapm->codec && dapm->codec->name_prefix)
Stephen Warrenaf468002011-04-28 17:38:01 -0600566 prefix = dapm->codec->name_prefix;
567 else
Liam Girdwood4d74b362011-05-18 17:50:07 +0100568 prefix = w->name;
Mark Brownefb7ac32011-03-08 17:23:24 +0000569
Stephen Warrenaf468002011-04-28 17:38:01 -0600570 if (shared) {
571 name = w->kcontrol_news[0].name;
572 prefix_len = 0;
573 } else {
574 name = w->name;
575 if (prefix)
576 prefix_len = strlen(prefix) + 1;
577 else
578 prefix_len = 0;
579 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000580
Stephen Warrenaf468002011-04-28 17:38:01 -0600581 /*
582 * The control will get a prefix from the control creation
583 * process but we're also using the same prefix for widgets so
584 * cut the prefix off the front of the widget name.
585 */
586 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
587 name + prefix_len, prefix);
588 ret = snd_ctl_add(card, kcontrol);
589 if (ret < 0) {
590 dev_err(dapm->dev,
591 "asoc: failed to add kcontrol %s\n", w->name);
592 kfree(wlist);
593 return ret;
594 }
595 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200596
Stephen Warrenaf468002011-04-28 17:38:01 -0600597 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200598
Stephen Warrenfad59882011-04-28 17:37:59 -0600599 w->kcontrols[0] = kcontrol;
600
Richard Purdie2b97eab2006-10-06 18:32:18 +0200601 list_for_each_entry(path, &w->sources, list_sink)
602 path->kcontrol = kcontrol;
603
Stephen Warrenaf468002011-04-28 17:38:01 -0600604 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200605}
606
607/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200608static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200609{
Mark Browna6c65732010-03-03 17:45:21 +0000610 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200611 dev_err(w->dapm->dev,
612 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200613
Mark Browna6c65732010-03-03 17:45:21 +0000614 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200615}
616
617/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200618static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200619{
620 struct snd_soc_dapm_path *p;
621
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200622 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200623 p->walked = 0;
624}
625
Mark Brown99497882010-05-07 20:24:05 +0100626/* We implement power down on suspend by checking the power state of
627 * the ALSA card - when we are suspending the ALSA state for the card
628 * is set to D3.
629 */
630static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
631{
Mark Brown12ea2c72011-03-02 18:17:32 +0000632 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100633
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100635 case SNDRV_CTL_POWER_D3hot:
636 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100637 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200638 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
639 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100640 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100641 default:
642 return 1;
643 }
644}
645
Richard Purdie2b97eab2006-10-06 18:32:18 +0200646/*
647 * Recursively check for a completed path to an active or physically connected
648 * output widget. Returns number of complete paths.
649 */
650static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
651{
652 struct snd_soc_dapm_path *path;
653 int con = 0;
654
Mark Brown246d0a12009-04-22 18:24:55 +0100655 if (widget->id == snd_soc_dapm_supply)
656 return 0;
657
Mark Brown010ff262009-08-17 17:39:22 +0100658 switch (widget->id) {
659 case snd_soc_dapm_adc:
660 case snd_soc_dapm_aif_out:
661 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100662 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100663 default:
664 break;
665 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200666
667 if (widget->connected) {
668 /* connected pin ? */
669 if (widget->id == snd_soc_dapm_output && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100670 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200671
672 /* connected jack or spk ? */
673 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300674 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
Mark Brown99497882010-05-07 20:24:05 +0100675 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200676 }
677
678 list_for_each_entry(path, &widget->sinks, list_source) {
679 if (path->walked)
680 continue;
681
682 if (path->sink && path->connect) {
683 path->walked = 1;
684 con += is_connected_output_ep(path->sink);
685 }
686 }
687
688 return con;
689}
690
691/*
692 * Recursively check for a completed path to an active or physically connected
693 * input widget. Returns number of complete paths.
694 */
695static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
696{
697 struct snd_soc_dapm_path *path;
698 int con = 0;
699
Mark Brown246d0a12009-04-22 18:24:55 +0100700 if (widget->id == snd_soc_dapm_supply)
701 return 0;
702
Richard Purdie2b97eab2006-10-06 18:32:18 +0200703 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100704 switch (widget->id) {
705 case snd_soc_dapm_dac:
706 case snd_soc_dapm_aif_in:
707 if (widget->active)
Mark Brown99497882010-05-07 20:24:05 +0100708 return snd_soc_dapm_suspend_check(widget);
Mark Brown010ff262009-08-17 17:39:22 +0100709 default:
710 break;
711 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200712
713 if (widget->connected) {
714 /* connected pin ? */
715 if (widget->id == snd_soc_dapm_input && !widget->ext)
Mark Brown99497882010-05-07 20:24:05 +0100716 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200717
718 /* connected VMID/Bias for lower pops */
719 if (widget->id == snd_soc_dapm_vmid)
Mark Brown99497882010-05-07 20:24:05 +0100720 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200721
722 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300723 if (widget->id == snd_soc_dapm_mic ||
724 (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
Mark Brown99497882010-05-07 20:24:05 +0100725 return snd_soc_dapm_suspend_check(widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200726 }
727
728 list_for_each_entry(path, &widget->sources, list_sink) {
729 if (path->walked)
730 continue;
731
732 if (path->source && path->connect) {
733 path->walked = 1;
734 con += is_connected_input_ep(path->source);
735 }
736 }
737
738 return con;
739}
740
741/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300742 * Handler for generic register modifier widget.
743 */
744int dapm_reg_event(struct snd_soc_dapm_widget *w,
745 struct snd_kcontrol *kcontrol, int event)
746{
747 unsigned int val;
748
749 if (SND_SOC_DAPM_EVENT_ON(event))
750 val = w->on_val;
751 else
752 val = w->off_val;
753
Liam Girdwood35ca6602011-01-28 17:45:35 +0000754 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300755 w->mask << w->shift, val << w->shift);
756
757 return 0;
758}
Mark Brown11589412008-07-29 11:42:23 +0100759EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300760
Mark Browncd0f2d42009-04-20 16:56:59 +0100761/* Generic check to see if a widget should be powered.
762 */
763static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
764{
765 int in, out;
766
767 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200768 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100769 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200770 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100771 return out != 0 && in != 0;
772}
773
Mark Brown6ea31b92009-04-20 17:15:41 +0100774/* Check to see if an ADC has power */
775static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
776{
777 int in;
778
779 if (w->active) {
780 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200781 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100782 return in != 0;
783 } else {
784 return dapm_generic_check_power(w);
785 }
786}
787
788/* Check to see if a DAC has power */
789static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
790{
791 int out;
792
793 if (w->active) {
794 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200795 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100796 return out != 0;
797 } else {
798 return dapm_generic_check_power(w);
799 }
800}
801
Mark Brown246d0a12009-04-22 18:24:55 +0100802/* Check to see if a power supply is needed */
803static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
804{
805 struct snd_soc_dapm_path *path;
806 int power = 0;
807
808 /* Check if one of our outputs is connected */
809 list_for_each_entry(path, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +0100810 if (path->connected &&
811 !path->connected(path->source, path->sink))
812 continue;
813
Mark Brown30173582011-02-11 11:42:19 +0000814 if (!path->sink)
815 continue;
816
817 if (path->sink->force) {
818 power = 1;
819 break;
820 }
821
822 if (path->sink->power_check &&
Mark Brown246d0a12009-04-22 18:24:55 +0100823 path->sink->power_check(path->sink)) {
824 power = 1;
825 break;
826 }
827 }
828
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200829 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100830
831 return power;
832}
833
Mark Brown38357ab2009-06-06 19:03:23 +0100834static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
835 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000836 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000837{
Mark Brown828a8422011-01-15 13:14:30 +0000838 int *sort;
839
840 if (power_up)
841 sort = dapm_up_seq;
842 else
843 sort = dapm_down_seq;
844
Mark Brown38357ab2009-06-06 19:03:23 +0100845 if (sort[a->id] != sort[b->id])
846 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000847 if (a->subseq != b->subseq) {
848 if (power_up)
849 return a->subseq - b->subseq;
850 else
851 return b->subseq - a->subseq;
852 }
Mark Brownb22ead22009-06-07 12:51:26 +0100853 if (a->reg != b->reg)
854 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000855 if (a->dapm != b->dapm)
856 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000857
Mark Brown38357ab2009-06-06 19:03:23 +0100858 return 0;
859}
Mark Brown42aa3412009-03-01 19:21:10 +0000860
Mark Brown38357ab2009-06-06 19:03:23 +0100861/* Insert a widget in order into a DAPM power sequence. */
862static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
863 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000864 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100865{
866 struct snd_soc_dapm_widget *w;
867
868 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000869 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100870 list_add_tail(&new_widget->power_list, &w->power_list);
871 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000872 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100873
Mark Brown38357ab2009-06-06 19:03:23 +0100874 list_add_tail(&new_widget->power_list, list);
875}
Mark Brown42aa3412009-03-01 19:21:10 +0000876
Mark Brown68f89ad2010-11-03 23:51:49 -0400877static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
878 struct snd_soc_dapm_widget *w, int event)
879{
880 struct snd_soc_card *card = dapm->card;
881 const char *ev_name;
882 int power, ret;
883
884 switch (event) {
885 case SND_SOC_DAPM_PRE_PMU:
886 ev_name = "PRE_PMU";
887 power = 1;
888 break;
889 case SND_SOC_DAPM_POST_PMU:
890 ev_name = "POST_PMU";
891 power = 1;
892 break;
893 case SND_SOC_DAPM_PRE_PMD:
894 ev_name = "PRE_PMD";
895 power = 0;
896 break;
897 case SND_SOC_DAPM_POST_PMD:
898 ev_name = "POST_PMD";
899 power = 0;
900 break;
901 default:
902 BUG();
903 return;
904 }
905
906 if (w->power != power)
907 return;
908
909 if (w->event && (w->event_flags & event)) {
910 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
911 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400912 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400913 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400914 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400915 if (ret < 0)
916 pr_err("%s: %s event failed: %d\n",
917 ev_name, w->name, ret);
918 }
919}
920
Mark Brownb22ead22009-06-07 12:51:26 +0100921/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200922static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +0100923 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100924{
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200925 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -0400926 struct snd_soc_dapm_widget *w;
927 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +0100928 unsigned int value = 0;
929 unsigned int mask = 0;
930 unsigned int cur_mask;
931
932 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
933 power_list)->reg;
934
935 list_for_each_entry(w, pending, power_list) {
936 cur_mask = 1 << w->shift;
937 BUG_ON(reg != w->reg);
938
939 if (w->invert)
940 power = !w->power;
941 else
942 power = w->power;
943
944 mask |= cur_mask;
945 if (power)
946 value |= cur_mask;
947
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200948 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +0100949 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
950 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +0100951
Mark Brown68f89ad2010-11-03 23:51:49 -0400952 /* Check for events */
953 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
954 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100955 }
956
Mark Brown81628102009-06-07 13:21:24 +0100957 if (reg >= 0) {
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200958 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +0100959 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200960 value, mask, reg, card->pop_time);
961 pop_wait(card->pop_time);
Liam Girdwood35ca6602011-01-28 17:45:35 +0000962 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +0100963 }
964
965 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -0400966 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
967 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +0000968 }
Mark Brown42aa3412009-03-01 19:21:10 +0000969}
970
Mark Brownb22ead22009-06-07 12:51:26 +0100971/* Apply a DAPM power sequence.
972 *
973 * We walk over a pre-sorted list of widgets to apply power to. In
974 * order to minimise the number of writes to the device required
975 * multiple widgets will be updated in a single write where possible.
976 * Currently anything that requires more than a single write is not
977 * handled.
978 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200979static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +0000980 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +0100981{
982 struct snd_soc_dapm_widget *w, *n;
983 LIST_HEAD(pending);
984 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +0000985 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +0100986 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200987 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +0000988 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +0000989 int *sort;
990
991 if (power_up)
992 sort = dapm_up_seq;
993 else
994 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +0100995
Mark Brownb22ead22009-06-07 12:51:26 +0100996 list_for_each_entry_safe(w, n, list, power_list) {
997 ret = 0;
998
999 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001000 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001001 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001002 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001003 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001004
Mark Brown474b62d2011-01-18 16:14:44 +00001005 if (cur_dapm && cur_dapm->seq_notifier) {
1006 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1007 if (sort[i] == cur_sort)
1008 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001009 i,
1010 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001011 }
1012
Mark Brownb22ead22009-06-07 12:51:26 +01001013 INIT_LIST_HEAD(&pending);
1014 cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001015 cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001016 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001017 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001018 }
1019
Mark Brown163cac02009-06-07 10:12:52 +01001020 switch (w->id) {
1021 case snd_soc_dapm_pre:
1022 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001023 list_for_each_entry_safe_continue(w, n, list,
1024 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001025
Mark Brownb22ead22009-06-07 12:51:26 +01001026 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001027 ret = w->event(w,
1028 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001029 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001030 ret = w->event(w,
1031 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001032 break;
1033
1034 case snd_soc_dapm_post:
1035 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001036 list_for_each_entry_safe_continue(w, n, list,
1037 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001038
Mark Brownb22ead22009-06-07 12:51:26 +01001039 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001040 ret = w->event(w,
1041 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001042 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001043 ret = w->event(w,
1044 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001045 break;
1046
Mark Brown163cac02009-06-07 10:12:52 +01001047 default:
Mark Brown81628102009-06-07 13:21:24 +01001048 /* Queue it up for application */
1049 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001050 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001051 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001052 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001053 list_move(&w->power_list, &pending);
1054 break;
Mark Brown163cac02009-06-07 10:12:52 +01001055 }
Mark Brownb22ead22009-06-07 12:51:26 +01001056
1057 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001058 dev_err(w->dapm->dev,
1059 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001060 }
Mark Brownb22ead22009-06-07 12:51:26 +01001061
1062 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001063 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001064
1065 if (cur_dapm && cur_dapm->seq_notifier) {
1066 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1067 if (sort[i] == cur_sort)
1068 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001069 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001070 }
Mark Brown163cac02009-06-07 10:12:52 +01001071}
1072
Mark Brown97404f22010-12-14 16:13:57 +00001073static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1074{
1075 struct snd_soc_dapm_update *update = dapm->update;
1076 struct snd_soc_dapm_widget *w;
1077 int ret;
1078
1079 if (!update)
1080 return;
1081
1082 w = update->widget;
1083
1084 if (w->event &&
1085 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1086 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1087 if (ret != 0)
1088 pr_err("%s DAPM pre-event failed: %d\n",
1089 w->name, ret);
1090 }
1091
1092 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1093 update->val);
1094 if (ret < 0)
1095 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1096
1097 if (w->event &&
1098 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1099 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1100 if (ret != 0)
1101 pr_err("%s DAPM post-event failed: %d\n",
1102 w->name, ret);
1103 }
1104}
1105
Mark Brown9d0624a2011-02-18 11:49:43 -08001106/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1107 * they're changing state.
1108 */
1109static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1110{
1111 struct snd_soc_dapm_context *d = data;
1112 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001113
Mark Brown9d0624a2011-02-18 11:49:43 -08001114 if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
1115 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1116 if (ret != 0)
1117 dev_err(d->dev,
1118 "Failed to turn on bias: %d\n", ret);
1119 }
1120
1121 /* If we're changing to all on or all off then prepare */
1122 if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
1123 (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
1124 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1125 if (ret != 0)
1126 dev_err(d->dev,
1127 "Failed to prepare bias: %d\n", ret);
1128 }
1129}
1130
1131/* Async callback run prior to DAPM sequences - brings to their final
1132 * state.
1133 */
1134static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1135{
1136 struct snd_soc_dapm_context *d = data;
1137 int ret;
1138
1139 /* If we just powered the last thing off drop to standby bias */
1140 if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
1141 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1142 if (ret != 0)
1143 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1144 ret);
1145 }
1146
1147 /* If we're in standby and can support bias off then do that */
1148 if (d->bias_level == SND_SOC_BIAS_STANDBY && d->idle_bias_off) {
1149 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1150 if (ret != 0)
1151 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1152 }
1153
1154 /* If we just powered up then move to active bias */
1155 if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
1156 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1157 if (ret != 0)
1158 dev_err(d->dev, "Failed to apply active bias: %d\n",
1159 ret);
1160 }
1161}
Mark Brown97404f22010-12-14 16:13:57 +00001162
Mark Brown42aa3412009-03-01 19:21:10 +00001163/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001164 * Scan each dapm widget for complete audio path.
1165 * A complete path is a route that has valid endpoints i.e.:-
1166 *
1167 * o DAC to output pin.
1168 * o Input Pin to ADC.
1169 * o Input pin to Output pin (bypass, sidetone)
1170 * o DAC to ADC (loopback).
1171 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001172static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001173{
Mark Brown12ea2c72011-03-02 18:17:32 +00001174 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001175 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001176 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001177 LIST_HEAD(up_list);
1178 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001179 LIST_HEAD(async_domain);
Mark Brown38357ab2009-06-06 19:03:23 +01001180 int power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001181
Mark Brown84e90932010-11-04 00:07:02 -04001182 trace_snd_soc_dapm_start(card);
1183
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001184 list_for_each_entry(d, &card->dapm_list, list)
Jarkko Nikulaea77b942011-05-26 16:32:18 +03001185 if (d->n_widgets || d->codec == NULL)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001186 d->dev_power = 0;
1187
Mark Brown6d3ddc82009-05-16 17:47:29 +01001188 /* Check which widgets we need to power and store them in
1189 * lists indicating if they should be powered up or down.
1190 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001191 list_for_each_entry(w, &card->widgets, list) {
Mark Brown6d3ddc82009-05-16 17:47:29 +01001192 switch (w->id) {
1193 case snd_soc_dapm_pre:
Mark Brown828a8422011-01-15 13:14:30 +00001194 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001195 break;
1196 case snd_soc_dapm_post:
Mark Brown828a8422011-01-15 13:14:30 +00001197 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001198 break;
1199
1200 default:
1201 if (!w->power_check)
1202 continue;
1203
Mark Brown99497882010-05-07 20:24:05 +01001204 if (!w->force)
Mark Brown50b6bce2009-11-23 13:11:53 +00001205 power = w->power_check(w);
Mark Brown99497882010-05-07 20:24:05 +01001206 else
1207 power = 1;
1208 if (power)
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001209 w->dapm->dev_power = 1;
Mark Brown452c5ea2009-05-17 21:41:23 +01001210
Mark Brown6d3ddc82009-05-16 17:47:29 +01001211 if (w->power == power)
1212 continue;
1213
Mark Brown84e90932010-11-04 00:07:02 -04001214 trace_snd_soc_dapm_widget_power(w, power);
1215
Mark Brown6d3ddc82009-05-16 17:47:29 +01001216 if (power)
Mark Brown828a8422011-01-15 13:14:30 +00001217 dapm_seq_insert(w, &up_list, true);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001218 else
Mark Brown828a8422011-01-15 13:14:30 +00001219 dapm_seq_insert(w, &down_list, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001220
1221 w->power = power;
1222 break;
1223 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001224 }
1225
Mark Brownb14b76a2009-08-17 11:55:38 +01001226 /* If there are no DAPM widgets then try to figure out power from the
1227 * event type.
1228 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001229 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001230 switch (event) {
1231 case SND_SOC_DAPM_STREAM_START:
1232 case SND_SOC_DAPM_STREAM_RESUME:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001233 dapm->dev_power = 1;
Mark Brownb14b76a2009-08-17 11:55:38 +01001234 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001235 case SND_SOC_DAPM_STREAM_STOP:
Liam Girdwood7987a112011-01-31 19:52:42 +00001236#warning need re-work
1237 if (dapm->codec)
1238 dapm->dev_power = !!dapm->codec->active;
1239 else
1240 dapm->dev_power = 0;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001241 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001242 case SND_SOC_DAPM_STREAM_SUSPEND:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001243 dapm->dev_power = 0;
Mark Brown50b6bce2009-11-23 13:11:53 +00001244 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001245 case SND_SOC_DAPM_STREAM_NOP:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001246 switch (dapm->bias_level) {
Mark Browna96ca332010-01-19 22:49:43 +00001247 case SND_SOC_BIAS_STANDBY:
1248 case SND_SOC_BIAS_OFF:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001249 dapm->dev_power = 0;
Mark Browna96ca332010-01-19 22:49:43 +00001250 break;
1251 default:
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001252 dapm->dev_power = 1;
Mark Browna96ca332010-01-19 22:49:43 +00001253 break;
1254 }
Mark Brown50b6bce2009-11-23 13:11:53 +00001255 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001256 default:
1257 break;
1258 }
1259 }
1260
Mark Brown52ba67b2011-04-04 21:05:11 +09001261 /* Force all contexts in the card to the same bias state */
1262 power = 0;
1263 list_for_each_entry(d, &card->dapm_list, list)
1264 if (d->dev_power)
1265 power = 1;
1266 list_for_each_entry(d, &card->dapm_list, list)
1267 d->dev_power = power;
1268
1269
Mark Brown9d0624a2011-02-18 11:49:43 -08001270 /* Run all the bias changes in parallel */
1271 list_for_each_entry(d, &dapm->card->dapm_list, list)
1272 async_schedule_domain(dapm_pre_sequence_async, d,
1273 &async_domain);
1274 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001275
Mark Brown6d3ddc82009-05-16 17:47:29 +01001276 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001277 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001278
Mark Brown97404f22010-12-14 16:13:57 +00001279 dapm_widget_update(dapm);
1280
Mark Brown6d3ddc82009-05-16 17:47:29 +01001281 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001282 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001283
Mark Brown9d0624a2011-02-18 11:49:43 -08001284 /* Run all the bias changes in parallel */
1285 list_for_each_entry(d, &dapm->card->dapm_list, list)
1286 async_schedule_domain(dapm_post_sequence_async, d,
1287 &async_domain);
1288 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001289
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001290 pop_dbg(dapm->dev, card->pop_time,
1291 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001292 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001293
Mark Brown84e90932010-11-04 00:07:02 -04001294 trace_snd_soc_dapm_done(card);
1295
Mark Brown42aa3412009-03-01 19:21:10 +00001296 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001297}
1298
Mark Brown79fb9382009-08-21 16:38:13 +01001299#ifdef CONFIG_DEBUG_FS
1300static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1301{
1302 file->private_data = inode->i_private;
1303 return 0;
1304}
1305
1306static ssize_t dapm_widget_power_read_file(struct file *file,
1307 char __user *user_buf,
1308 size_t count, loff_t *ppos)
1309{
1310 struct snd_soc_dapm_widget *w = file->private_data;
1311 char *buf;
1312 int in, out;
1313 ssize_t ret;
1314 struct snd_soc_dapm_path *p = NULL;
1315
1316 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1317 if (!buf)
1318 return -ENOMEM;
1319
1320 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001321 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001322 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001323 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001324
Mark Brownd033c362009-12-04 15:25:56 +00001325 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001326 w->name, w->power ? "On" : "Off", in, out);
1327
Mark Brownd033c362009-12-04 15:25:56 +00001328 if (w->reg >= 0)
1329 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1330 " - R%d(0x%x) bit %d",
1331 w->reg, w->reg, w->shift);
1332
1333 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1334
Mark Brown3eef08b2009-09-14 16:49:00 +01001335 if (w->sname)
1336 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1337 w->sname,
1338 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001339
1340 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001341 if (p->connected && !p->connected(w, p->sink))
1342 continue;
1343
Mark Brown79fb9382009-08-21 16:38:13 +01001344 if (p->connect)
1345 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001346 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001347 p->name ? p->name : "static",
1348 p->source->name);
1349 }
1350 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001351 if (p->connected && !p->connected(w, p->sink))
1352 continue;
1353
Mark Brown79fb9382009-08-21 16:38:13 +01001354 if (p->connect)
1355 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001356 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001357 p->name ? p->name : "static",
1358 p->sink->name);
1359 }
1360
1361 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1362
1363 kfree(buf);
1364 return ret;
1365}
1366
1367static const struct file_operations dapm_widget_power_fops = {
1368 .open = dapm_widget_power_open_file,
1369 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001370 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001371};
1372
Mark Brownef49e4f2011-04-04 20:48:13 +09001373static int dapm_bias_open_file(struct inode *inode, struct file *file)
1374{
1375 file->private_data = inode->i_private;
1376 return 0;
1377}
1378
1379static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1380 size_t count, loff_t *ppos)
1381{
1382 struct snd_soc_dapm_context *dapm = file->private_data;
1383 char *level;
1384
1385 switch (dapm->bias_level) {
1386 case SND_SOC_BIAS_ON:
1387 level = "On\n";
1388 break;
1389 case SND_SOC_BIAS_PREPARE:
1390 level = "Prepare\n";
1391 break;
1392 case SND_SOC_BIAS_STANDBY:
1393 level = "Standby\n";
1394 break;
1395 case SND_SOC_BIAS_OFF:
1396 level = "Off\n";
1397 break;
1398 default:
1399 BUG();
1400 level = "Unknown\n";
1401 break;
1402 }
1403
1404 return simple_read_from_buffer(user_buf, count, ppos, level,
1405 strlen(level));
1406}
1407
1408static const struct file_operations dapm_bias_fops = {
1409 .open = dapm_bias_open_file,
1410 .read = dapm_bias_read_file,
1411 .llseek = default_llseek,
1412};
1413
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001414void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1415 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001416{
Mark Brown79fb9382009-08-21 16:38:13 +01001417 struct dentry *d;
1418
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001419 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1420
1421 if (!dapm->debugfs_dapm) {
1422 printk(KERN_WARNING
1423 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001424 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001425 }
Mark Brown79fb9382009-08-21 16:38:13 +01001426
Mark Brownef49e4f2011-04-04 20:48:13 +09001427 d = debugfs_create_file("bias_level", 0444,
1428 dapm->debugfs_dapm, dapm,
1429 &dapm_bias_fops);
1430 if (!d)
1431 dev_warn(dapm->dev,
1432 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001433}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001434
1435static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1436{
1437 struct snd_soc_dapm_context *dapm = w->dapm;
1438 struct dentry *d;
1439
1440 if (!dapm->debugfs_dapm || !w->name)
1441 return;
1442
1443 d = debugfs_create_file(w->name, 0444,
1444 dapm->debugfs_dapm, w,
1445 &dapm_widget_power_fops);
1446 if (!d)
1447 dev_warn(w->dapm->dev,
1448 "ASoC: Failed to create %s debugfs file\n",
1449 w->name);
1450}
1451
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001452static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1453{
1454 debugfs_remove_recursive(dapm->debugfs_dapm);
1455}
1456
Mark Brown79fb9382009-08-21 16:38:13 +01001457#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001458void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1459 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001460{
1461}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001462
1463static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1464{
1465}
1466
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001467static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1468{
1469}
1470
Mark Brown79fb9382009-08-21 16:38:13 +01001471#endif
1472
Richard Purdie2b97eab2006-10-06 18:32:18 +02001473/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001474static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001475 struct snd_kcontrol *kcontrol, int change,
1476 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001477{
1478 struct snd_soc_dapm_path *path;
1479 int found = 0;
1480
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001481 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001482 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001483 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001484 return -ENODEV;
1485
Mark Brown3a655772009-10-05 17:23:30 +01001486 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001487 return 0;
1488
1489 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001490 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001491 if (path->kcontrol != kcontrol)
1492 continue;
1493
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001494 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001495 continue;
1496
1497 found = 1;
1498 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001499 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001500 path->connect = 1; /* new connection */
1501 else
1502 path->connect = 0; /* old connection must be powered down */
1503 }
1504
Mark Brownb91b8fa2010-01-20 18:18:35 +00001505 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001506 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001507
1508 return 0;
1509}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001510
Milan plzik1b075e32008-01-10 14:39:46 +01001511/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001512static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001513 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001514{
1515 struct snd_soc_dapm_path *path;
1516 int found = 0;
1517
Milan plzik1b075e32008-01-10 14:39:46 +01001518 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001519 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001520 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001521 return -ENODEV;
1522
Richard Purdie2b97eab2006-10-06 18:32:18 +02001523 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001524 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001525 if (path->kcontrol != kcontrol)
1526 continue;
1527
1528 /* found, now check type */
1529 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001530 path->connect = connect;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001531 break;
1532 }
1533
Mark Brownb91b8fa2010-01-20 18:18:35 +00001534 if (found)
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001535 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001536
1537 return 0;
1538}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001539
1540/* show dapm widget status in sys fs */
Liam Girdwood7987a112011-01-31 19:52:42 +00001541static ssize_t widget_show(struct snd_soc_dapm_context *dapm,
1542 const char *name, char *buf, ssize_t count)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001543{
Richard Purdie2b97eab2006-10-06 18:32:18 +02001544 struct snd_soc_dapm_widget *w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001545 char *state = "not set";
1546
Liam Girdwood7987a112011-01-31 19:52:42 +00001547 count += sprintf(buf + count, "\n%s\n", name);
1548
1549 list_for_each_entry(w, &dapm->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001550
1551 /* only display widgets that burnm power */
1552 switch (w->id) {
1553 case snd_soc_dapm_hp:
1554 case snd_soc_dapm_mic:
1555 case snd_soc_dapm_spk:
1556 case snd_soc_dapm_line:
1557 case snd_soc_dapm_micbias:
1558 case snd_soc_dapm_dac:
1559 case snd_soc_dapm_adc:
1560 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001561 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001562 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001563 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001564 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001565 if (w->name)
1566 count += sprintf(buf + count, "%s: %s\n",
1567 w->name, w->power ? "On":"Off");
1568 break;
1569 default:
1570 break;
1571 }
1572 }
1573
Liam Girdwood7987a112011-01-31 19:52:42 +00001574 switch (dapm->bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001575 case SND_SOC_BIAS_ON:
1576 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001577 break;
Mark Brown0be98982008-05-19 12:31:28 +02001578 case SND_SOC_BIAS_PREPARE:
1579 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001580 break;
Mark Brown0be98982008-05-19 12:31:28 +02001581 case SND_SOC_BIAS_STANDBY:
1582 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001583 break;
Mark Brown0be98982008-05-19 12:31:28 +02001584 case SND_SOC_BIAS_OFF:
1585 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001586 break;
1587 }
1588 count += sprintf(buf + count, "PM State: %s\n", state);
1589
1590 return count;
1591}
1592
Liam Girdwood7987a112011-01-31 19:52:42 +00001593/* show dapm widget status in sys fs */
1594static ssize_t dapm_widget_show(struct device *dev,
1595 struct device_attribute *attr, char *buf)
1596{
1597 struct snd_soc_pcm_runtime *rtd =
1598 container_of(dev, struct snd_soc_pcm_runtime, dev);
1599 struct snd_soc_codec *codec = rtd->codec;
1600 struct snd_soc_platform *platform = rtd->platform;
1601 ssize_t count = 0;
1602
1603 count += widget_show(&codec->dapm, codec->name, buf, count);
1604 count += widget_show(&platform->dapm, platform->name, buf, count);
1605 return count;
1606}
1607
Richard Purdie2b97eab2006-10-06 18:32:18 +02001608static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1609
1610int snd_soc_dapm_sys_add(struct device *dev)
1611{
Troy Kisky12ef1932008-10-13 17:42:14 -07001612 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001613}
1614
1615static void snd_soc_dapm_sys_remove(struct device *dev)
1616{
Mark Brownaef90842009-05-16 17:53:16 +01001617 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001618}
1619
1620/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001621static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001622{
1623 struct snd_soc_dapm_widget *w, *next_w;
1624 struct snd_soc_dapm_path *p, *next_p;
1625
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001626 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1627 if (w->dapm != dapm)
1628 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001629 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001630 /*
1631 * remove source and sink paths associated to this widget.
1632 * While removing the path, remove reference to it from both
1633 * source and sink widgets so that path is removed only once.
1634 */
1635 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1636 list_del(&p->list_sink);
1637 list_del(&p->list_source);
1638 list_del(&p->list);
1639 kfree(p->long_name);
1640 kfree(p);
1641 }
1642 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1643 list_del(&p->list_sink);
1644 list_del(&p->list_source);
1645 list_del(&p->list);
1646 kfree(p->long_name);
1647 kfree(p);
1648 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001649 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001650 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001651 kfree(w);
1652 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001653}
1654
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001655static struct snd_soc_dapm_widget *dapm_find_widget(
1656 struct snd_soc_dapm_context *dapm, const char *pin,
1657 bool search_other_contexts)
1658{
1659 struct snd_soc_dapm_widget *w;
1660 struct snd_soc_dapm_widget *fallback = NULL;
1661
1662 list_for_each_entry(w, &dapm->card->widgets, list) {
1663 if (!strcmp(w->name, pin)) {
1664 if (w->dapm == dapm)
1665 return w;
1666 else
1667 fallback = w;
1668 }
1669 }
1670
1671 if (search_other_contexts)
1672 return fallback;
1673
1674 return NULL;
1675}
1676
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001677static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001678 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001679{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001680 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001681
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001682 if (!w) {
1683 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1684 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001685 }
1686
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001687 w->connected = status;
1688 if (status == 0)
1689 w->force = 0;
Mark Brown0d867332011-04-06 11:38:14 +09001690
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001691 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001692}
1693
Richard Purdie2b97eab2006-10-06 18:32:18 +02001694/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001695 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001696 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001697 *
1698 * Walks all dapm audio paths and powers widgets according to their
1699 * stream or path usage.
1700 *
1701 * Returns 0 for success.
1702 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001703int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001704{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001705 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001706}
Liam Girdwooda5302182008-07-07 13:35:17 +01001707EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001708
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001709static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001710 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001711{
1712 struct snd_soc_dapm_path *path;
1713 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001714 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001715 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001716 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001717 const char *source;
1718 char prefixed_sink[80];
1719 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001720 int ret = 0;
1721
Mark Brown88e8b9a2011-03-02 18:18:24 +00001722 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001723 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1724 dapm->codec->name_prefix, route->sink);
1725 sink = prefixed_sink;
1726 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1727 dapm->codec->name_prefix, route->source);
1728 source = prefixed_source;
1729 } else {
1730 sink = route->sink;
1731 source = route->source;
1732 }
1733
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001734 /*
1735 * find src and dest widgets over all widgets but favor a widget from
1736 * current DAPM context
1737 */
1738 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001739 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001740 wtsink = w;
1741 if (w->dapm == dapm)
1742 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001743 continue;
1744 }
1745 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001746 wtsource = w;
1747 if (w->dapm == dapm)
1748 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001749 }
1750 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001751 /* use widget from another DAPM context if not found from this */
1752 if (!wsink)
1753 wsink = wtsink;
1754 if (!wsource)
1755 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001756
1757 if (wsource == NULL || wsink == NULL)
1758 return -ENODEV;
1759
1760 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1761 if (!path)
1762 return -ENOMEM;
1763
1764 path->source = wsource;
1765 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001766 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001767 INIT_LIST_HEAD(&path->list);
1768 INIT_LIST_HEAD(&path->list_source);
1769 INIT_LIST_HEAD(&path->list_sink);
1770
1771 /* check for external widgets */
1772 if (wsink->id == snd_soc_dapm_input) {
1773 if (wsource->id == snd_soc_dapm_micbias ||
1774 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001775 wsource->id == snd_soc_dapm_line ||
1776 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001777 wsink->ext = 1;
1778 }
1779 if (wsource->id == snd_soc_dapm_output) {
1780 if (wsink->id == snd_soc_dapm_spk ||
1781 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001782 wsink->id == snd_soc_dapm_line ||
1783 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001784 wsource->ext = 1;
1785 }
1786
1787 /* connect static paths */
1788 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001789 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001790 list_add(&path->list_sink, &wsink->sources);
1791 list_add(&path->list_source, &wsource->sinks);
1792 path->connect = 1;
1793 return 0;
1794 }
1795
1796 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001797 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001798 case snd_soc_dapm_adc:
1799 case snd_soc_dapm_dac:
1800 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001801 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001802 case snd_soc_dapm_input:
1803 case snd_soc_dapm_output:
1804 case snd_soc_dapm_micbias:
1805 case snd_soc_dapm_vmid:
1806 case snd_soc_dapm_pre:
1807 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001808 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001809 case snd_soc_dapm_aif_in:
1810 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001811 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001812 list_add(&path->list_sink, &wsink->sources);
1813 list_add(&path->list_source, &wsource->sinks);
1814 path->connect = 1;
1815 return 0;
1816 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001817 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001818 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001819 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06001820 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001821 if (ret != 0)
1822 goto err;
1823 break;
1824 case snd_soc_dapm_switch:
1825 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001826 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001827 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001828 if (ret != 0)
1829 goto err;
1830 break;
1831 case snd_soc_dapm_hp:
1832 case snd_soc_dapm_mic:
1833 case snd_soc_dapm_line:
1834 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001835 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001836 list_add(&path->list_sink, &wsink->sources);
1837 list_add(&path->list_source, &wsource->sinks);
1838 path->connect = 0;
1839 return 0;
1840 }
1841 return 0;
1842
1843err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001844 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1845 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001846 kfree(path);
1847 return ret;
1848}
Mark Brown105f1c22008-05-13 14:52:19 +02001849
1850/**
Mark Brown105f1c22008-05-13 14:52:19 +02001851 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001852 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02001853 * @route: audio routes
1854 * @num: number of routes
1855 *
1856 * Connects 2 dapm widgets together via a named audio path. The sink is
1857 * the widget receiving the audio signal, whilst the source is the sender
1858 * of the audio signal.
1859 *
1860 * Returns 0 for success else error. On error all resources can be freed
1861 * with a call to snd_soc_card_free().
1862 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001863int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02001864 const struct snd_soc_dapm_route *route, int num)
1865{
1866 int i, ret;
1867
1868 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001869 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02001870 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001871 dev_err(dapm->dev, "Failed to add route %s->%s\n",
1872 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02001873 return ret;
1874 }
1875 route++;
1876 }
1877
1878 return 0;
1879}
1880EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1881
1882/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001883 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001884 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001885 *
1886 * Checks the codec for any new dapm widgets and creates them if found.
1887 *
1888 * Returns 0 for success.
1889 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001890int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001891{
1892 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00001893 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001894
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001895 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001896 {
1897 if (w->new)
1898 continue;
1899
Stephen Warrenfad59882011-04-28 17:37:59 -06001900 if (w->num_kcontrols) {
1901 w->kcontrols = kzalloc(w->num_kcontrols *
1902 sizeof(struct snd_kcontrol *),
1903 GFP_KERNEL);
1904 if (!w->kcontrols)
1905 return -ENOMEM;
1906 }
1907
Richard Purdie2b97eab2006-10-06 18:32:18 +02001908 switch(w->id) {
1909 case snd_soc_dapm_switch:
1910 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001911 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001912 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001913 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001914 break;
1915 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001916 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001917 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001918 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001919 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001920 break;
1921 case snd_soc_dapm_adc:
Mark Brown010ff262009-08-17 17:39:22 +01001922 case snd_soc_dapm_aif_out:
Mark Brownb75576d2009-04-20 17:56:13 +01001923 w->power_check = dapm_adc_check_power;
1924 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001925 case snd_soc_dapm_dac:
Mark Brown010ff262009-08-17 17:39:22 +01001926 case snd_soc_dapm_aif_in:
Mark Brownb75576d2009-04-20 17:56:13 +01001927 w->power_check = dapm_dac_check_power;
1928 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001929 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001930 case snd_soc_dapm_out_drv:
Mark Brownb75576d2009-04-20 17:56:13 +01001931 w->power_check = dapm_generic_check_power;
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02001932 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001933 break;
1934 case snd_soc_dapm_input:
1935 case snd_soc_dapm_output:
1936 case snd_soc_dapm_micbias:
1937 case snd_soc_dapm_spk:
1938 case snd_soc_dapm_hp:
1939 case snd_soc_dapm_mic:
1940 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001941 w->power_check = dapm_generic_check_power;
1942 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001943 case snd_soc_dapm_supply:
1944 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001945 case snd_soc_dapm_vmid:
1946 case snd_soc_dapm_pre:
1947 case snd_soc_dapm_post:
1948 break;
1949 }
Mark Brownb66a70d2011-02-09 18:04:11 +00001950
1951 /* Read the initial power state from the device */
1952 if (w->reg >= 0) {
Liam Girdwood35ca6602011-01-28 17:45:35 +00001953 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00001954 val &= 1 << w->shift;
1955 if (w->invert)
1956 val = !val;
1957
1958 if (val)
1959 w->power = 1;
1960 }
1961
Richard Purdie2b97eab2006-10-06 18:32:18 +02001962 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001963
1964 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001965 }
1966
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001967 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001968 return 0;
1969}
1970EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1971
Liam Girdwood7038dfd92011-02-01 20:58:15 +00001972const char *snd_soc_dapm_get_aif(struct snd_soc_dapm_context *dapm,
1973 const char *stream_name, enum snd_soc_dapm_type type)
1974{
1975 struct snd_soc_dapm_widget *w;
1976
1977 list_for_each_entry(w, &dapm->widgets, list) {
1978
1979 if (!w->sname)
1980 continue;
1981
1982 if (w->id == type && strstr(w->sname, stream_name))
1983 return w->name;
1984 }
1985 return NULL;
1986}
1987EXPORT_SYMBOL_GPL(snd_soc_dapm_get_aif);
1988
Richard Purdie2b97eab2006-10-06 18:32:18 +02001989/**
1990 * snd_soc_dapm_get_volsw - dapm mixer get callback
1991 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001992 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001993 *
1994 * Callback to get the value of a dapm mixer control.
1995 *
1996 * Returns 0 for success.
1997 */
1998int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1999 struct snd_ctl_elem_value *ucontrol)
2000{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002001 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2002 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002003 struct soc_mixer_control *mc =
2004 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002005 unsigned int reg = mc->reg;
2006 unsigned int shift = mc->shift;
2007 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002008 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002009 unsigned int invert = mc->invert;
2010 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002011
Richard Purdie2b97eab2006-10-06 18:32:18 +02002012 ucontrol->value.integer.value[0] =
2013 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2014 if (shift != rshift)
2015 ucontrol->value.integer.value[1] =
2016 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2017 if (invert) {
2018 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002019 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002020 if (shift != rshift)
2021 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002022 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002023 }
2024
2025 return 0;
2026}
2027EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2028
2029/**
2030 * snd_soc_dapm_put_volsw - dapm mixer set callback
2031 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002032 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002033 *
2034 * Callback to set the value of a dapm mixer control.
2035 *
2036 * Returns 0 for success.
2037 */
2038int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2039 struct snd_ctl_elem_value *ucontrol)
2040{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002041 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2042 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2043 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002044 struct soc_mixer_control *mc =
2045 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002046 unsigned int reg = mc->reg;
2047 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002048 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002049 unsigned int mask = (1 << fls(max)) - 1;
2050 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002051 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002052 int connect, change;
2053 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002054 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002055
2056 val = (ucontrol->value.integer.value[0] & mask);
2057
2058 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002059 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002060 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002061 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002062
Stephen Warrenfafd2172011-04-28 17:38:00 -06002063 if (val)
2064 /* new connection */
2065 connect = invert ? 0 : 1;
2066 else
2067 /* old connection must be powered down */
2068 connect = invert ? 1 : 0;
2069
2070 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002071
Stephen Warrene9cf7042011-01-27 14:54:05 -07002072 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002073 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002074 for (wi = 0; wi < wlist->num_widgets; wi++) {
2075 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002076
Stephen Warrenfafd2172011-04-28 17:38:00 -06002077 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002078
Stephen Warrenfafd2172011-04-28 17:38:00 -06002079 update.kcontrol = kcontrol;
2080 update.widget = widget;
2081 update.reg = reg;
2082 update.mask = mask;
2083 update.val = val;
2084 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002085
Stephen Warrenfafd2172011-04-28 17:38:00 -06002086 dapm_mixer_update_power(widget, kcontrol, connect);
2087
2088 widget->dapm->update = NULL;
2089 }
Mark Brown283375c2009-12-07 18:09:03 +00002090 }
2091
Stephen Warrenfafd2172011-04-28 17:38:00 -06002092 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002093 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002094}
2095EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2096
2097/**
2098 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2099 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002100 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002101 *
2102 * Callback to get the value of a dapm enumerated double mixer control.
2103 *
2104 * Returns 0 for success.
2105 */
2106int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2107 struct snd_ctl_elem_value *ucontrol)
2108{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002109 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2110 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002111 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002112 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002113
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002114 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002115 ;
2116 val = snd_soc_read(widget->codec, e->reg);
2117 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2118 if (e->shift_l != e->shift_r)
2119 ucontrol->value.enumerated.item[1] =
2120 (val >> e->shift_r) & (bitmask - 1);
2121
2122 return 0;
2123}
2124EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2125
2126/**
2127 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2128 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002129 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002130 *
2131 * Callback to set the value of a dapm enumerated double mixer control.
2132 *
2133 * Returns 0 for success.
2134 */
2135int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2136 struct snd_ctl_elem_value *ucontrol)
2137{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002138 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2139 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2140 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002141 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002142 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002143 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002144 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002145 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002146
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002147 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002148 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002149 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002150 return -EINVAL;
2151 mux = ucontrol->value.enumerated.item[0];
2152 val = mux << e->shift_l;
2153 mask = (bitmask - 1) << e->shift_l;
2154 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002155 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002156 return -EINVAL;
2157 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2158 mask |= (bitmask - 1) << e->shift_r;
2159 }
2160
Stephen Warrenfafd2172011-04-28 17:38:00 -06002161 mutex_lock(&codec->mutex);
2162
Mark Brown3a655772009-10-05 17:23:30 +01002163 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002164 if (change) {
2165 for (wi = 0; wi < wlist->num_widgets; wi++) {
2166 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002167
Stephen Warrenfafd2172011-04-28 17:38:00 -06002168 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002169
Stephen Warrenfafd2172011-04-28 17:38:00 -06002170 update.kcontrol = kcontrol;
2171 update.widget = widget;
2172 update.reg = e->reg;
2173 update.mask = mask;
2174 update.val = val;
2175 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002176
Stephen Warrenfafd2172011-04-28 17:38:00 -06002177 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002178
Stephen Warrenfafd2172011-04-28 17:38:00 -06002179 widget->dapm->update = NULL;
2180 }
2181 }
2182
2183 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002184 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002185}
2186EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2187
2188/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002189 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2190 * @kcontrol: mixer control
2191 * @ucontrol: control element information
2192 *
2193 * Returns 0 for success.
2194 */
2195int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2196 struct snd_ctl_elem_value *ucontrol)
2197{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002198 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2199 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002200
2201 ucontrol->value.enumerated.item[0] = widget->value;
2202
2203 return 0;
2204}
2205EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2206
2207/**
2208 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2209 * @kcontrol: mixer control
2210 * @ucontrol: control element information
2211 *
2212 * Returns 0 for success.
2213 */
2214int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2215 struct snd_ctl_elem_value *ucontrol)
2216{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002217 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2218 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2219 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002220 struct soc_enum *e =
2221 (struct soc_enum *)kcontrol->private_value;
2222 int change;
2223 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002224 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002225
2226 if (ucontrol->value.enumerated.item[0] >= e->max)
2227 return -EINVAL;
2228
Stephen Warrenfafd2172011-04-28 17:38:00 -06002229 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002230
2231 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002232 if (change) {
2233 for (wi = 0; wi < wlist->num_widgets; wi++) {
2234 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002235
Stephen Warrenfafd2172011-04-28 17:38:00 -06002236 widget->value = ucontrol->value.enumerated.item[0];
2237
2238 dapm_mux_update_power(widget, kcontrol, change,
2239 widget->value, e);
2240 }
2241 }
2242
2243 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002244 return ret;
2245}
2246EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2247
2248/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002249 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2250 * callback
2251 * @kcontrol: mixer control
2252 * @ucontrol: control element information
2253 *
2254 * Callback to get the value of a dapm semi enumerated double mixer control.
2255 *
2256 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2257 * used for handling bitfield coded enumeration for example.
2258 *
2259 * Returns 0 for success.
2260 */
2261int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2262 struct snd_ctl_elem_value *ucontrol)
2263{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002264 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2265 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002266 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002267 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002268
2269 reg_val = snd_soc_read(widget->codec, e->reg);
2270 val = (reg_val >> e->shift_l) & e->mask;
2271 for (mux = 0; mux < e->max; mux++) {
2272 if (val == e->values[mux])
2273 break;
2274 }
2275 ucontrol->value.enumerated.item[0] = mux;
2276 if (e->shift_l != e->shift_r) {
2277 val = (reg_val >> e->shift_r) & e->mask;
2278 for (mux = 0; mux < e->max; mux++) {
2279 if (val == e->values[mux])
2280 break;
2281 }
2282 ucontrol->value.enumerated.item[1] = mux;
2283 }
2284
2285 return 0;
2286}
2287EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2288
2289/**
2290 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2291 * callback
2292 * @kcontrol: mixer control
2293 * @ucontrol: control element information
2294 *
2295 * Callback to set the value of a dapm semi enumerated double mixer control.
2296 *
2297 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2298 * used for handling bitfield coded enumeration for example.
2299 *
2300 * Returns 0 for success.
2301 */
2302int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2303 struct snd_ctl_elem_value *ucontrol)
2304{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002305 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2306 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2307 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002308 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002309 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002310 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002311 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002312 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002313
2314 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2315 return -EINVAL;
2316 mux = ucontrol->value.enumerated.item[0];
2317 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2318 mask = e->mask << e->shift_l;
2319 if (e->shift_l != e->shift_r) {
2320 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2321 return -EINVAL;
2322 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2323 mask |= e->mask << e->shift_r;
2324 }
2325
Stephen Warrenfafd2172011-04-28 17:38:00 -06002326 mutex_lock(&codec->mutex);
2327
Mark Brown3a655772009-10-05 17:23:30 +01002328 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002329 if (change) {
2330 for (wi = 0; wi < wlist->num_widgets; wi++) {
2331 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002332
Stephen Warrenfafd2172011-04-28 17:38:00 -06002333 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002334
Stephen Warrenfafd2172011-04-28 17:38:00 -06002335 update.kcontrol = kcontrol;
2336 update.widget = widget;
2337 update.reg = e->reg;
2338 update.mask = mask;
2339 update.val = val;
2340 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002341
Stephen Warrenfafd2172011-04-28 17:38:00 -06002342 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002343
Stephen Warrenfafd2172011-04-28 17:38:00 -06002344 widget->dapm->update = NULL;
2345 }
2346 }
2347
2348 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002349 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002350}
2351EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2352
2353/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002354 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2355 *
2356 * @kcontrol: mixer control
2357 * @uinfo: control element information
2358 *
2359 * Callback to provide information about a pin switch control.
2360 */
2361int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2362 struct snd_ctl_elem_info *uinfo)
2363{
2364 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2365 uinfo->count = 1;
2366 uinfo->value.integer.min = 0;
2367 uinfo->value.integer.max = 1;
2368
2369 return 0;
2370}
2371EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2372
2373/**
2374 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2375 *
2376 * @kcontrol: mixer control
2377 * @ucontrol: Value
2378 */
2379int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2380 struct snd_ctl_elem_value *ucontrol)
2381{
2382 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2383 const char *pin = (const char *)kcontrol->private_value;
2384
2385 mutex_lock(&codec->mutex);
2386
2387 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002388 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002389
2390 mutex_unlock(&codec->mutex);
2391
2392 return 0;
2393}
2394EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2395
2396/**
2397 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2398 *
2399 * @kcontrol: mixer control
2400 * @ucontrol: Value
2401 */
2402int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2403 struct snd_ctl_elem_value *ucontrol)
2404{
2405 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2406 const char *pin = (const char *)kcontrol->private_value;
2407
2408 mutex_lock(&codec->mutex);
2409
2410 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002411 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002412 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002413 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002414
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002415 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002416
2417 mutex_unlock(&codec->mutex);
2418
2419 return 0;
2420}
2421EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2422
2423/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002424 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002425 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002426 * @widget: widget template
2427 *
2428 * Creates a new dapm control based upon the template.
2429 *
2430 * Returns 0 for success else error.
2431 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002432int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002433 const struct snd_soc_dapm_widget *widget)
2434{
2435 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002436 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002437
2438 if ((w = dapm_cnew_widget(widget)) == NULL)
2439 return -ENOMEM;
2440
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002441 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002442 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002443 name_len += 1 + strlen(dapm->codec->name_prefix);
2444 w->name = kmalloc(name_len, GFP_KERNEL);
2445 if (w->name == NULL) {
2446 kfree(w);
2447 return -ENOMEM;
2448 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002449 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002450 snprintf(w->name, name_len, "%s %s",
2451 dapm->codec->name_prefix, widget->name);
2452 else
2453 snprintf(w->name, name_len, "%s", widget->name);
2454
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002455 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002456 w->dapm = dapm;
2457 w->codec = dapm->codec;
Liam Girdwood7987a112011-01-31 19:52:42 +00002458 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002459 INIT_LIST_HEAD(&w->sources);
2460 INIT_LIST_HEAD(&w->sinks);
2461 INIT_LIST_HEAD(&w->list);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002462 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002463
2464 /* machine layer set ups unconnected pins and insertions */
2465 w->connected = 1;
2466 return 0;
2467}
2468EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2469
2470/**
Mark Brown4ba13272008-05-13 14:51:19 +02002471 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002472 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002473 * @widget: widget array
2474 * @num: number of widgets
2475 *
2476 * Creates new DAPM controls based upon the templates.
2477 *
2478 * Returns 0 for success else error.
2479 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002480int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002481 const struct snd_soc_dapm_widget *widget,
2482 int num)
2483{
2484 int i, ret;
2485
2486 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002487 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002488 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002489 dev_err(dapm->dev,
2490 "ASoC: Failed to create DAPM control %s: %d\n",
2491 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002492 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002493 }
Mark Brown4ba13272008-05-13 14:51:19 +02002494 widget++;
2495 }
2496 return 0;
2497}
2498EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2499
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002500static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002501 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002502{
2503 struct snd_soc_dapm_widget *w;
2504
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002505 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002506 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002507 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002508 continue;
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002509 dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2510 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002511 if (strstr(w->sname, stream)) {
2512 switch(event) {
2513 case SND_SOC_DAPM_STREAM_START:
2514 w->active = 1;
2515 break;
2516 case SND_SOC_DAPM_STREAM_STOP:
2517 w->active = 0;
2518 break;
2519 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002520 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002521 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002522 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2523 break;
2524 }
2525 }
2526 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002527
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002528 dapm_power_widgets(dapm, event);
Liam Girdwood670ff442011-03-30 23:27:27 +01002529 /* do we need to notify any clients that DAPM stream is complete */
2530 if (dapm->stream_event)
2531 dapm->stream_event(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002532}
2533
2534/**
2535 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2536 * @rtd: PCM runtime data
2537 * @stream: stream name
2538 * @event: stream event
2539 *
2540 * Sends a stream event to the dapm core. The core then makes any
2541 * necessary widget power changes.
2542 *
2543 * Returns 0 for success else error.
2544 */
2545int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2546 const char *stream, int event)
2547{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002548 if (stream == NULL)
2549 return 0;
Liam Girdwood670ff442011-03-30 23:27:27 +01002550 mutex_lock(&rtd->card->dapm_mutex);
2551
2552 soc_dapm_stream_event(&rtd->platform->dapm, stream, event);
2553 soc_dapm_stream_event(&rtd->codec->dapm, stream, event);
2554
2555 mutex_unlock(&rtd->card->dapm_mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002556 return 0;
2557}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002558
2559/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002560 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002561 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002562 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002563 *
Mark Brown74b8f952009-06-06 11:26:15 +01002564 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002565 * a valid audio route and active audio stream.
2566 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2567 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002568 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002569int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002570{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002571 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002572}
Liam Girdwooda5302182008-07-07 13:35:17 +01002573EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002574
2575/**
Mark Brownda341832010-03-15 19:23:37 +00002576 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002577 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002578 * @pin: pin name
2579 *
2580 * Enables input/output pin regardless of any other state. This is
2581 * intended for use with microphone bias supplies used in microphone
2582 * jack detection.
2583 *
2584 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2585 * do any widget power switching.
2586 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002587int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2588 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002589{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002590 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002591
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002592 if (!w) {
2593 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2594 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002595 }
2596
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002597 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2598 w->connected = 1;
2599 w->force = 1;
Mark Brown0d867332011-04-06 11:38:14 +09002600
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002601 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002602}
2603EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2604
2605/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002606 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002607 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002608 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002609 *
Mark Brown74b8f952009-06-06 11:26:15 +01002610 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002611 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2612 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002613 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002614int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2615 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002616{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002617 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002618}
2619EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2620
2621/**
Mark Brown5817b522008-09-24 11:23:11 +01002622 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002623 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002624 * @pin: pin name
2625 *
2626 * Marks the specified pin as being not connected, disabling it along
2627 * any parent or child widgets. At present this is identical to
2628 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2629 * additional things such as disabling controls which only affect
2630 * paths through the pin.
2631 *
2632 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2633 * do any widget power switching.
2634 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002635int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002636{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002637 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002638}
2639EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2640
2641/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002642 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002643 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002644 * @pin: audio signal pin endpoint (or start point)
2645 *
2646 * Get audio pin status - connected or disconnected.
2647 *
2648 * Returns 1 for connected otherwise 0.
2649 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002650int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2651 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002652{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002653 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002654
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002655 if (w)
2656 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002657
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002658 return 0;
2659}
Liam Girdwooda5302182008-07-07 13:35:17 +01002660EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002661
2662/**
Mark Brown1547aba2010-05-07 21:11:40 +01002663 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002664 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002665 * @pin: audio signal pin endpoint (or start point)
2666 *
2667 * Mark the given endpoint or pin as ignoring suspend. When the
2668 * system is disabled a path between two endpoints flagged as ignoring
2669 * suspend will not be disabled. The path must already be enabled via
2670 * normal means at suspend time, it will not be turned on if it was not
2671 * already enabled.
2672 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002673int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2674 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002675{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002676 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002677
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002678 if (!w) {
2679 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2680 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002681 }
2682
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002683 w->ignore_suspend = 1;
2684
2685 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002686}
2687EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2688
2689/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002690 * snd_soc_dapm_free - free dapm resources
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002691 * @card: SoC device
Richard Purdie2b97eab2006-10-06 18:32:18 +02002692 *
2693 * Free all dapm widgets and resources.
2694 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002695void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002696{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002697 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002698 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002699 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002700 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002701}
2702EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2703
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002704static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002705{
Mark Brown51737472009-06-22 13:16:51 +01002706 struct snd_soc_dapm_widget *w;
2707 LIST_HEAD(down_list);
2708 int powerdown = 0;
2709
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002710 list_for_each_entry(w, &dapm->card->widgets, list) {
2711 if (w->dapm != dapm)
2712 continue;
Mark Brown51737472009-06-22 13:16:51 +01002713 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002714 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002715 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002716 powerdown = 1;
2717 }
2718 }
2719
2720 /* If there were no widgets to power down we're already in
2721 * standby.
2722 */
2723 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002724 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002725 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002726 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002727 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002728}
Mark Brown51737472009-06-22 13:16:51 +01002729
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002730/*
2731 * snd_soc_dapm_shutdown - callback for system shutdown
2732 */
2733void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2734{
2735 struct snd_soc_codec *codec;
Liam Girdwood7987a112011-01-31 19:52:42 +00002736 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002737
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002738 list_for_each_entry(codec, &card->codec_dev_list, list) {
2739 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08002740 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002741 }
Liam Girdwood7987a112011-01-31 19:52:42 +00002742
2743 list_for_each_entry(platform, &card->platform_dev_list, list) {
2744 soc_dapm_shutdown_codec(&platform->dapm);
2745 snd_soc_dapm_set_bias_level(&platform->dapm, SND_SOC_BIAS_OFF);
2746 }
Mark Brown51737472009-06-22 13:16:51 +01002747}
2748
Richard Purdie2b97eab2006-10-06 18:32:18 +02002749/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002750MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002751MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2752MODULE_LICENSE("GPL");