blob: f42e8b9fb17db7baeabaf7152f81123773cdecd0 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
Mark Brown74b8f952009-06-06 11:26:15 +010015 * DACs/ADCs.
Richard Purdie2b97eab2006-10-06 18:32:18 +020016 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
Mark Brown9d0624a2011-02-18 11:49:43 -080035#include <linux/async.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020036#include <linux/delay.h>
37#include <linux/pm.h>
38#include <linux/bitops.h>
39#include <linux/platform_device.h>
40#include <linux/jiffies.h>
Takashi Iwai20496ff2009-08-24 09:40:34 +020041#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020043#include <sound/core.h>
44#include <sound/pcm.h>
45#include <sound/pcm_params.h>
Liam Girdwoodce6120c2010-11-05 15:53:46 +020046#include <sound/soc.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020047#include <sound/initval.h>
48
Mark Brown84e90932010-11-04 00:07:02 -040049#include <trace/events/asoc.h>
50
Mark Brownde02d072011-09-20 21:43:24 +010051#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
52
Richard Purdie2b97eab2006-10-06 18:32:18 +020053/* dapm power sequences - make this per codec in the future */
54static int dapm_up_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010055 [snd_soc_dapm_pre] = 0,
56 [snd_soc_dapm_supply] = 1,
57 [snd_soc_dapm_micbias] = 2,
Mark Brown010ff262009-08-17 17:39:22 +010058 [snd_soc_dapm_aif_in] = 3,
59 [snd_soc_dapm_aif_out] = 3,
60 [snd_soc_dapm_mic] = 4,
61 [snd_soc_dapm_mux] = 5,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000062 [snd_soc_dapm_virt_mux] = 5,
Mark Brown010ff262009-08-17 17:39:22 +010063 [snd_soc_dapm_value_mux] = 5,
64 [snd_soc_dapm_dac] = 6,
65 [snd_soc_dapm_mixer] = 7,
66 [snd_soc_dapm_mixer_named_ctl] = 7,
67 [snd_soc_dapm_pga] = 8,
68 [snd_soc_dapm_adc] = 9,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060069 [snd_soc_dapm_out_drv] = 10,
Mark Brown010ff262009-08-17 17:39:22 +010070 [snd_soc_dapm_hp] = 10,
71 [snd_soc_dapm_spk] = 10,
72 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020073};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000074
Richard Purdie2b97eab2006-10-06 18:32:18 +020075static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010076 [snd_soc_dapm_pre] = 0,
77 [snd_soc_dapm_adc] = 1,
78 [snd_soc_dapm_hp] = 2,
Mark Brown1ca04062009-08-17 16:26:59 +010079 [snd_soc_dapm_spk] = 2,
Olaya, Margaritad88429a2010-12-10 21:11:44 -060080 [snd_soc_dapm_out_drv] = 2,
Mark Brown38357ab2009-06-06 19:03:23 +010081 [snd_soc_dapm_pga] = 4,
82 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010083 [snd_soc_dapm_mixer] = 5,
84 [snd_soc_dapm_dac] = 6,
85 [snd_soc_dapm_mic] = 7,
86 [snd_soc_dapm_micbias] = 8,
87 [snd_soc_dapm_mux] = 9,
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +000088 [snd_soc_dapm_virt_mux] = 9,
Mark Browne3d4dab2009-06-07 13:08:45 +010089 [snd_soc_dapm_value_mux] = 9,
Mark Brown010ff262009-08-17 17:39:22 +010090 [snd_soc_dapm_aif_in] = 10,
91 [snd_soc_dapm_aif_out] = 10,
92 [snd_soc_dapm_supply] = 11,
93 [snd_soc_dapm_post] = 12,
Richard Purdie2b97eab2006-10-06 18:32:18 +020094};
95
Troy Kisky12ef1932008-10-13 17:42:14 -070096static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010097{
98 if (pop_time)
99 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
100}
101
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200102static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +0100103{
104 va_list args;
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200105 char *buf;
106
107 if (!pop_time)
108 return;
109
110 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
111 if (buf == NULL)
112 return;
Mark Brown15e4c722008-07-02 11:51:20 +0100113
114 va_start(args, fmt);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200115 vsnprintf(buf, PAGE_SIZE, fmt, args);
Takashi Iwai9d01df02010-12-22 14:08:40 +0100116 dev_info(dev, "%s", buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100117 va_end(args);
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +0200118
119 kfree(buf);
Mark Brown15e4c722008-07-02 11:51:20 +0100120}
121
Mark Browndb432b42011-10-03 21:06:40 +0100122static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
123{
124 return !list_empty(&w->dirty);
125}
126
Mark Brown25c77c52011-10-08 13:36:03 +0100127void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
Mark Browndb432b42011-10-03 21:06:40 +0100128{
Mark Brown75c1f892011-10-04 22:28:08 +0100129 if (!dapm_dirty_widget(w)) {
130 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
131 w->name, reason);
Mark Browndb432b42011-10-03 21:06:40 +0100132 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
Mark Brown75c1f892011-10-04 22:28:08 +0100133 }
Mark Browndb432b42011-10-03 21:06:40 +0100134}
Mark Brown25c77c52011-10-08 13:36:03 +0100135EXPORT_SYMBOL_GPL(dapm_mark_dirty);
Mark Browndb432b42011-10-03 21:06:40 +0100136
Richard Purdie2b97eab2006-10-06 18:32:18 +0200137/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100138static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200139 const struct snd_soc_dapm_widget *_widget)
140{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100141 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200142}
143
Liam Girdwood48056082011-07-20 12:23:33 +0100144/* get snd_card from DAPM context */
145static inline struct snd_card *dapm_get_snd_card(
146 struct snd_soc_dapm_context *dapm)
147{
148 if (dapm->codec)
149 return dapm->codec->card->snd_card;
150 else if (dapm->platform)
151 return dapm->platform->card->snd_card;
152 else
153 BUG();
154
155 /* unreachable */
156 return NULL;
157}
158
159/* get soc_card from DAPM context */
160static inline struct snd_soc_card *dapm_get_soc_card(
161 struct snd_soc_dapm_context *dapm)
162{
163 if (dapm->codec)
164 return dapm->codec->card;
165 else if (dapm->platform)
166 return dapm->platform->card;
167 else
168 BUG();
169
170 /* unreachable */
171 return NULL;
172}
173
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100174static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
175{
176 if (w->codec)
177 return snd_soc_read(w->codec, reg);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100178 else if (w->platform)
179 return snd_soc_platform_read(w->platform, reg);
180
181 dev_err(w->dapm->dev, "no valid widget read method\n");
182 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100183}
184
185static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
186{
187 if (w->codec)
188 return snd_soc_write(w->codec, reg, val);
Liam Girdwoodb7950642011-07-04 22:10:52 +0100189 else if (w->platform)
190 return snd_soc_platform_write(w->platform, reg, val);
191
192 dev_err(w->dapm->dev, "no valid widget write method\n");
193 return -1;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100194}
195
196static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
197 unsigned short reg, unsigned int mask, unsigned int value)
198{
199 int change;
200 unsigned int old, new;
201 int ret;
202
203 ret = soc_widget_read(w, reg);
204 if (ret < 0)
205 return ret;
206
207 old = ret;
208 new = (old & ~mask) | (value & mask);
209 change = old != new;
210 if (change) {
211 ret = soc_widget_write(w, reg, new);
212 if (ret < 0)
213 return ret;
214 }
215
216 return change;
217}
218
Mark Brown452c5ea2009-05-17 21:41:23 +0100219/**
220 * snd_soc_dapm_set_bias_level - set the bias level for the system
Mark Browned5a4c42011-02-18 11:12:42 -0800221 * @dapm: DAPM context
Mark Brown452c5ea2009-05-17 21:41:23 +0100222 * @level: level to configure
223 *
224 * Configure the bias (power) levels for the SoC audio device.
225 *
226 * Returns 0 for success else error.
227 */
Mark Browned5a4c42011-02-18 11:12:42 -0800228static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200229 enum snd_soc_bias_level level)
Mark Brown452c5ea2009-05-17 21:41:23 +0100230{
Mark Browned5a4c42011-02-18 11:12:42 -0800231 struct snd_soc_card *card = dapm->card;
Mark Brown452c5ea2009-05-17 21:41:23 +0100232 int ret = 0;
233
Mark Brown84e90932010-11-04 00:07:02 -0400234 trace_snd_soc_bias_level_start(card, level);
235
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000236 if (card && card->set_bias_level)
Mark Brownd4c60052011-06-06 19:13:23 +0100237 ret = card->set_bias_level(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100238 if (ret != 0)
239 goto out;
Mark Brown452c5ea2009-05-17 21:41:23 +0100240
Mark Browncc4c6702011-06-06 19:03:34 +0100241 if (dapm->codec) {
242 if (dapm->codec->driver->set_bias_level)
243 ret = dapm->codec->driver->set_bias_level(dapm->codec,
244 level);
245 else
246 dapm->bias_level = level;
247 }
Mark Brown171ec6b2011-06-06 18:15:19 +0100248 if (ret != 0)
249 goto out;
250
251 if (card && card->set_bias_level_post)
Mark Brownd4c60052011-06-06 19:13:23 +0100252 ret = card->set_bias_level_post(card, dapm, level);
Mark Brown171ec6b2011-06-06 18:15:19 +0100253out:
Mark Brown84e90932010-11-04 00:07:02 -0400254 trace_snd_soc_bias_level_done(card, level);
255
Mark Brown452c5ea2009-05-17 21:41:23 +0100256 return ret;
257}
258
Richard Purdie2b97eab2006-10-06 18:32:18 +0200259/* set up initial codec paths */
260static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
261 struct snd_soc_dapm_path *p, int i)
262{
263 switch (w->id) {
264 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000265 case snd_soc_dapm_mixer:
266 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200267 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100268 struct soc_mixer_control *mc = (struct soc_mixer_control *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600269 w->kcontrol_news[i].private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -0400270 unsigned int reg = mc->reg;
271 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100272 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -0400273 unsigned int mask = (1 << fls(max)) - 1;
274 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200275
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100276 val = soc_widget_read(w, reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200277 val = (val >> shift) & mask;
278
279 if ((invert && !val) || (!invert && val))
280 p->connect = 1;
281 else
282 p->connect = 0;
283 }
284 break;
285 case snd_soc_dapm_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600286 struct soc_enum *e = (struct soc_enum *)
287 w->kcontrol_news[i].private_value;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200288 int val, item, bitmask;
289
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100290 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Mark Brown88d96082011-06-06 16:16:34 +0100291 ;
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100292 val = soc_widget_read(w, e->reg);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200293 item = (val >> e->shift_l) & (bitmask - 1);
294
295 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100296 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200297 if (!(strcmp(p->name, e->texts[i])) && item == i)
298 p->connect = 1;
299 }
300 }
301 break;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000302 case snd_soc_dapm_virt_mux: {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600303 struct soc_enum *e = (struct soc_enum *)
304 w->kcontrol_news[i].private_value;
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +0000305
306 p->connect = 0;
307 /* since a virtual mux has no backing registers to
308 * decide which path to connect, it will try to match
309 * with the first enumeration. This is to ensure
310 * that the default mux choice (the first) will be
311 * correctly powered up during initialization.
312 */
313 if (!strcmp(p->name, e->texts[0]))
314 p->connect = 1;
315 }
316 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200317 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200318 struct soc_enum *e = (struct soc_enum *)
Stephen Warren82cfecd2011-04-28 17:37:58 -0600319 w->kcontrol_news[i].private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200320 int val, item;
321
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100322 val = soc_widget_read(w, e->reg);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200323 val = (val >> e->shift_l) & e->mask;
324 for (item = 0; item < e->max; item++) {
325 if (val == e->values[item])
326 break;
327 }
328
329 p->connect = 0;
330 for (i = 0; i < e->max; i++) {
331 if (!(strcmp(p->name, e->texts[i])) && item == i)
332 p->connect = 1;
333 }
334 }
335 break;
Mark Brown56563102011-10-03 22:41:09 +0100336 /* does not affect routing - always connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200337 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -0600338 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200339 case snd_soc_dapm_output:
340 case snd_soc_dapm_adc:
341 case snd_soc_dapm_input:
342 case snd_soc_dapm_dac:
343 case snd_soc_dapm_micbias:
344 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100345 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +0100346 case snd_soc_dapm_aif_in:
347 case snd_soc_dapm_aif_out:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200348 case snd_soc_dapm_hp:
349 case snd_soc_dapm_mic:
350 case snd_soc_dapm_spk:
351 case snd_soc_dapm_line:
Mark Brown56563102011-10-03 22:41:09 +0100352 p->connect = 1;
353 break;
354 /* does affect routing - dynamically connected */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200355 case snd_soc_dapm_pre:
356 case snd_soc_dapm_post:
357 p->connect = 0;
358 break;
359 }
360}
361
Mark Brown74b8f952009-06-06 11:26:15 +0100362/* connect mux widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200363static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200364 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
365 struct snd_soc_dapm_path *path, const char *control_name,
366 const struct snd_kcontrol_new *kcontrol)
367{
368 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
369 int i;
370
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100371 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200372 if (!(strcmp(control_name, e->texts[i]))) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200373 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200374 list_add(&path->list_sink, &dest->sources);
375 list_add(&path->list_source, &src->sinks);
376 path->name = (char*)e->texts[i];
377 dapm_set_path_status(dest, path, 0);
378 return 0;
379 }
380 }
381
382 return -ENODEV;
383}
384
Mark Brown74b8f952009-06-06 11:26:15 +0100385/* connect mixer widget to its interconnecting audio paths */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200386static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +0200387 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
388 struct snd_soc_dapm_path *path, const char *control_name)
389{
390 int i;
391
392 /* search for mixer kcontrol */
393 for (i = 0; i < dest->num_kcontrols; i++) {
Stephen Warren82cfecd2011-04-28 17:37:58 -0600394 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200395 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200396 list_add(&path->list_sink, &dest->sources);
397 list_add(&path->list_source, &src->sinks);
Stephen Warren82cfecd2011-04-28 17:37:58 -0600398 path->name = dest->kcontrol_news[i].name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200399 dapm_set_path_status(dest, path, i);
400 return 0;
401 }
402 }
403 return -ENODEV;
404}
405
Stephen Warrenaf468002011-04-28 17:38:01 -0600406static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
Stephen Warren1007da02011-05-26 09:57:33 -0600407 struct snd_soc_dapm_widget *kcontrolw,
Stephen Warrenaf468002011-04-28 17:38:01 -0600408 const struct snd_kcontrol_new *kcontrol_new,
409 struct snd_kcontrol **kcontrol)
410{
411 struct snd_soc_dapm_widget *w;
412 int i;
413
414 *kcontrol = NULL;
415
416 list_for_each_entry(w, &dapm->card->widgets, list) {
Stephen Warren1007da02011-05-26 09:57:33 -0600417 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
418 continue;
Stephen Warrenaf468002011-04-28 17:38:01 -0600419 for (i = 0; i < w->num_kcontrols; i++) {
420 if (&w->kcontrol_news[i] == kcontrol_new) {
421 if (w->kcontrols)
422 *kcontrol = w->kcontrols[i];
423 return 1;
424 }
425 }
426 }
427
428 return 0;
429}
430
Richard Purdie2b97eab2006-10-06 18:32:18 +0200431/* create new dapm mixer control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200432static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200433{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200434 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200435 int i, ret = 0;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000436 size_t name_len, prefix_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200437 struct snd_soc_dapm_path *path;
Mark Brown12ea2c72011-03-02 18:17:32 +0000438 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000439 const char *prefix;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600440 struct snd_soc_dapm_widget_list *wlist;
441 size_t wlistsize;
Mark Brownefb7ac32011-03-08 17:23:24 +0000442
443 if (dapm->codec)
444 prefix = dapm->codec->name_prefix;
445 else
446 prefix = NULL;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200447
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000448 if (prefix)
449 prefix_len = strlen(prefix) + 1;
450 else
451 prefix_len = 0;
452
Richard Purdie2b97eab2006-10-06 18:32:18 +0200453 /* add kcontrol */
454 for (i = 0; i < w->num_kcontrols; i++) {
455
456 /* match name */
457 list_for_each_entry(path, &w->sources, list_sink) {
458
459 /* mixer/mux paths name must match control name */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600460 if (path->name != (char *)w->kcontrol_news[i].name)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200461 continue;
462
Lars-Peter Clausen82cd8762011-08-15 20:15:21 +0200463 if (w->kcontrols[i]) {
464 path->kcontrol = w->kcontrols[i];
465 continue;
466 }
467
Stephen Warrenfafd2172011-04-28 17:38:00 -0600468 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
469 sizeof(struct snd_soc_dapm_widget *),
470 wlist = kzalloc(wlistsize, GFP_KERNEL);
471 if (wlist == NULL) {
472 dev_err(dapm->dev,
473 "asoc: can't allocate widget list for %s\n",
474 w->name);
475 return -ENOMEM;
476 }
477 wlist->num_widgets = 1;
478 wlist->widgets[0] = w;
479
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000480 /* add dapm control with long name.
481 * for dapm_mixer this is the concatenation of the
482 * mixer and kcontrol name.
483 * for dapm_mixer_named_ctl this is simply the
484 * kcontrol name.
485 */
Stephen Warren82cfecd2011-04-28 17:37:58 -0600486 name_len = strlen(w->kcontrol_news[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000487 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000488 name_len += 1 + strlen(w->name);
489
Mark Brown219b93f2008-10-28 13:02:31 +0000490 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000491
Stephen Warrenfafd2172011-04-28 17:38:00 -0600492 if (path->long_name == NULL) {
493 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200494 return -ENOMEM;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600495 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200496
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000497 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000498 default:
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000499 /* The control will get a prefix from
500 * the control creation process but
501 * we're also using the same prefix
502 * for widgets so cut the prefix off
503 * the front of the widget name.
504 */
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000505 snprintf(path->long_name, name_len, "%s %s",
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000506 w->name + prefix_len,
Stephen Warren82cfecd2011-04-28 17:37:58 -0600507 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000508 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000509 case snd_soc_dapm_mixer_named_ctl:
510 snprintf(path->long_name, name_len, "%s",
Stephen Warren82cfecd2011-04-28 17:37:58 -0600511 w->kcontrol_news[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000512 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000513 }
514
Mark Brown219b93f2008-10-28 13:02:31 +0000515 path->long_name[name_len - 1] = '\0';
516
Stephen Warrenfafd2172011-04-28 17:38:00 -0600517 path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
518 wlist, path->long_name,
519 prefix);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200520 ret = snd_ctl_add(card, path->kcontrol);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200521 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200522 dev_err(dapm->dev,
523 "asoc: failed to add dapm kcontrol %s: %d\n",
524 path->long_name, ret);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600525 kfree(wlist);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200526 kfree(path->long_name);
527 path->long_name = NULL;
528 return ret;
529 }
Stephen Warrenfad59882011-04-28 17:37:59 -0600530 w->kcontrols[i] = path->kcontrol;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200531 }
532 }
533 return ret;
534}
535
536/* create new dapm mux control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200537static int dapm_new_mux(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200538{
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200539 struct snd_soc_dapm_context *dapm = w->dapm;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200540 struct snd_soc_dapm_path *path = NULL;
541 struct snd_kcontrol *kcontrol;
Mark Brown12ea2c72011-03-02 18:17:32 +0000542 struct snd_card *card = dapm->card->snd_card;
Mark Brownefb7ac32011-03-08 17:23:24 +0000543 const char *prefix;
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000544 size_t prefix_len;
Stephen Warrenaf468002011-04-28 17:38:01 -0600545 int ret;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600546 struct snd_soc_dapm_widget_list *wlist;
Stephen Warrenaf468002011-04-28 17:38:01 -0600547 int shared, wlistentries;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600548 size_t wlistsize;
Stephen Warrenaf468002011-04-28 17:38:01 -0600549 char *name;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200550
Stephen Warrenaf468002011-04-28 17:38:01 -0600551 if (w->num_kcontrols != 1) {
552 dev_err(dapm->dev,
553 "asoc: mux %s has incorrect number of controls\n",
554 w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200555 return -EINVAL;
556 }
557
Stephen Warren1007da02011-05-26 09:57:33 -0600558 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
Stephen Warrenaf468002011-04-28 17:38:01 -0600559 &kcontrol);
560 if (kcontrol) {
561 wlist = kcontrol->private_data;
562 wlistentries = wlist->num_widgets + 1;
563 } else {
564 wlist = NULL;
565 wlistentries = 1;
566 }
Stephen Warrenfafd2172011-04-28 17:38:00 -0600567 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
Stephen Warrenaf468002011-04-28 17:38:01 -0600568 wlistentries * sizeof(struct snd_soc_dapm_widget *),
569 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
Stephen Warrenfafd2172011-04-28 17:38:00 -0600570 if (wlist == NULL) {
571 dev_err(dapm->dev,
572 "asoc: can't allocate widget list for %s\n", w->name);
573 return -ENOMEM;
574 }
Stephen Warrenaf468002011-04-28 17:38:01 -0600575 wlist->num_widgets = wlistentries;
576 wlist->widgets[wlistentries - 1] = w;
Stephen Warrenfafd2172011-04-28 17:38:00 -0600577
Stephen Warrenaf468002011-04-28 17:38:01 -0600578 if (!kcontrol) {
579 if (dapm->codec)
580 prefix = dapm->codec->name_prefix;
581 else
582 prefix = NULL;
Mark Brownefb7ac32011-03-08 17:23:24 +0000583
Stephen Warrenaf468002011-04-28 17:38:01 -0600584 if (shared) {
585 name = w->kcontrol_news[0].name;
586 prefix_len = 0;
587 } else {
588 name = w->name;
589 if (prefix)
590 prefix_len = strlen(prefix) + 1;
591 else
592 prefix_len = 0;
593 }
Mark Brown3e5ff4d2011-03-09 11:33:09 +0000594
Stephen Warrenaf468002011-04-28 17:38:01 -0600595 /*
596 * The control will get a prefix from the control creation
597 * process but we're also using the same prefix for widgets so
598 * cut the prefix off the front of the widget name.
599 */
600 kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
601 name + prefix_len, prefix);
602 ret = snd_ctl_add(card, kcontrol);
603 if (ret < 0) {
Mark Brown53daf202011-09-05 10:51:05 -0700604 dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
605 w->name, ret);
Stephen Warrenaf468002011-04-28 17:38:01 -0600606 kfree(wlist);
607 return ret;
608 }
609 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200610
Stephen Warrenaf468002011-04-28 17:38:01 -0600611 kcontrol->private_data = wlist;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200612
Stephen Warrenfad59882011-04-28 17:37:59 -0600613 w->kcontrols[0] = kcontrol;
614
Richard Purdie2b97eab2006-10-06 18:32:18 +0200615 list_for_each_entry(path, &w->sources, list_sink)
616 path->kcontrol = kcontrol;
617
Stephen Warrenaf468002011-04-28 17:38:01 -0600618 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200619}
620
621/* create new dapm volume control */
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +0200622static int dapm_new_pga(struct snd_soc_dapm_widget *w)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200623{
Mark Browna6c65732010-03-03 17:45:21 +0000624 if (w->num_kcontrols)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200625 dev_err(w->dapm->dev,
626 "asoc: PGA controls not supported: '%s'\n", w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200627
Mark Browna6c65732010-03-03 17:45:21 +0000628 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200629}
630
631/* reset 'walked' bit for each dapm path */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200632static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200633{
634 struct snd_soc_dapm_path *p;
635
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +0200636 list_for_each_entry(p, &dapm->card->paths, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200637 p->walked = 0;
638}
639
Mark Brown99497882010-05-07 20:24:05 +0100640/* We implement power down on suspend by checking the power state of
641 * the ALSA card - when we are suspending the ALSA state for the card
642 * is set to D3.
643 */
644static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
645{
Mark Brown12ea2c72011-03-02 18:17:32 +0000646 int level = snd_power_get_state(widget->dapm->card->snd_card);
Mark Brown99497882010-05-07 20:24:05 +0100647
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000648 switch (level) {
Mark Brown99497882010-05-07 20:24:05 +0100649 case SNDRV_CTL_POWER_D3hot:
650 case SNDRV_CTL_POWER_D3cold:
Mark Brown1547aba2010-05-07 21:11:40 +0100651 if (widget->ignore_suspend)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +0200652 dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
653 widget->name);
Mark Brown1547aba2010-05-07 21:11:40 +0100654 return widget->ignore_suspend;
Mark Brown99497882010-05-07 20:24:05 +0100655 default:
656 return 1;
657 }
658}
659
Richard Purdie2b97eab2006-10-06 18:32:18 +0200660/*
661 * Recursively check for a completed path to an active or physically connected
662 * output widget. Returns number of complete paths.
663 */
664static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
665{
666 struct snd_soc_dapm_path *path;
667 int con = 0;
668
Mark Brown024dc072011-10-09 11:52:05 +0100669 if (widget->outputs >= 0)
670 return widget->outputs;
671
Mark Brownde02d072011-09-20 21:43:24 +0100672 DAPM_UPDATE_STAT(widget, path_checks);
673
Mark Brown246d0a12009-04-22 18:24:55 +0100674 if (widget->id == snd_soc_dapm_supply)
675 return 0;
676
Mark Brown010ff262009-08-17 17:39:22 +0100677 switch (widget->id) {
678 case snd_soc_dapm_adc:
679 case snd_soc_dapm_aif_out:
Mark Brown024dc072011-10-09 11:52:05 +0100680 if (widget->active) {
681 widget->outputs = snd_soc_dapm_suspend_check(widget);
682 return widget->outputs;
683 }
Mark Brown010ff262009-08-17 17:39:22 +0100684 default:
685 break;
686 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200687
688 if (widget->connected) {
689 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100690 if (widget->id == snd_soc_dapm_output && !widget->ext) {
691 widget->outputs = snd_soc_dapm_suspend_check(widget);
692 return widget->outputs;
693 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200694
695 /* connected jack or spk ? */
Mark Brown024dc072011-10-09 11:52:05 +0100696 if (widget->id == snd_soc_dapm_hp ||
697 widget->id == snd_soc_dapm_spk ||
698 (widget->id == snd_soc_dapm_line &&
699 !list_empty(&widget->sources))) {
700 widget->outputs = snd_soc_dapm_suspend_check(widget);
701 return widget->outputs;
702 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200703 }
704
705 list_for_each_entry(path, &widget->sinks, list_source) {
Mark Browne56235e2011-09-21 18:19:14 +0100706 DAPM_UPDATE_STAT(widget, neighbour_checks);
707
Mark Brownbf3a9e12011-06-13 16:42:29 +0100708 if (path->weak)
709 continue;
710
Richard Purdie2b97eab2006-10-06 18:32:18 +0200711 if (path->walked)
712 continue;
713
714 if (path->sink && path->connect) {
715 path->walked = 1;
716 con += is_connected_output_ep(path->sink);
717 }
718 }
719
Mark Brown024dc072011-10-09 11:52:05 +0100720 widget->outputs = con;
721
Richard Purdie2b97eab2006-10-06 18:32:18 +0200722 return con;
723}
724
725/*
726 * Recursively check for a completed path to an active or physically connected
727 * input widget. Returns number of complete paths.
728 */
729static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
730{
731 struct snd_soc_dapm_path *path;
732 int con = 0;
733
Mark Brown024dc072011-10-09 11:52:05 +0100734 if (widget->inputs >= 0)
735 return widget->inputs;
736
Mark Brownde02d072011-09-20 21:43:24 +0100737 DAPM_UPDATE_STAT(widget, path_checks);
738
Mark Brown246d0a12009-04-22 18:24:55 +0100739 if (widget->id == snd_soc_dapm_supply)
740 return 0;
741
Richard Purdie2b97eab2006-10-06 18:32:18 +0200742 /* active stream ? */
Mark Brown010ff262009-08-17 17:39:22 +0100743 switch (widget->id) {
744 case snd_soc_dapm_dac:
745 case snd_soc_dapm_aif_in:
Mark Brown024dc072011-10-09 11:52:05 +0100746 if (widget->active) {
747 widget->inputs = snd_soc_dapm_suspend_check(widget);
748 return widget->inputs;
749 }
Mark Brown010ff262009-08-17 17:39:22 +0100750 default:
751 break;
752 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200753
754 if (widget->connected) {
755 /* connected pin ? */
Mark Brown024dc072011-10-09 11:52:05 +0100756 if (widget->id == snd_soc_dapm_input && !widget->ext) {
757 widget->inputs = snd_soc_dapm_suspend_check(widget);
758 return widget->inputs;
759 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200760
761 /* connected VMID/Bias for lower pops */
Mark Brown024dc072011-10-09 11:52:05 +0100762 if (widget->id == snd_soc_dapm_vmid) {
763 widget->inputs = snd_soc_dapm_suspend_check(widget);
764 return widget->inputs;
765 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200766
767 /* connected jack ? */
Peter Ujfalusieaeae5d2009-09-30 09:27:24 +0300768 if (widget->id == snd_soc_dapm_mic ||
Mark Brown024dc072011-10-09 11:52:05 +0100769 (widget->id == snd_soc_dapm_line &&
770 !list_empty(&widget->sinks))) {
771 widget->inputs = snd_soc_dapm_suspend_check(widget);
772 return widget->inputs;
773 }
774
Richard Purdie2b97eab2006-10-06 18:32:18 +0200775 }
776
777 list_for_each_entry(path, &widget->sources, list_sink) {
Mark Browne56235e2011-09-21 18:19:14 +0100778 DAPM_UPDATE_STAT(widget, neighbour_checks);
779
Mark Brownbf3a9e12011-06-13 16:42:29 +0100780 if (path->weak)
781 continue;
782
Richard Purdie2b97eab2006-10-06 18:32:18 +0200783 if (path->walked)
784 continue;
785
786 if (path->source && path->connect) {
787 path->walked = 1;
788 con += is_connected_input_ep(path->source);
789 }
790 }
791
Mark Brown024dc072011-10-09 11:52:05 +0100792 widget->inputs = con;
793
Richard Purdie2b97eab2006-10-06 18:32:18 +0200794 return con;
795}
796
797/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300798 * Handler for generic register modifier widget.
799 */
800int dapm_reg_event(struct snd_soc_dapm_widget *w,
801 struct snd_kcontrol *kcontrol, int event)
802{
803 unsigned int val;
804
805 if (SND_SOC_DAPM_EVENT_ON(event))
806 val = w->on_val;
807 else
808 val = w->off_val;
809
Liam Girdwood0445bdf2011-06-13 19:37:36 +0100810 soc_widget_update_bits(w, -(w->reg + 1),
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300811 w->mask << w->shift, val << w->shift);
812
813 return 0;
814}
Mark Brown11589412008-07-29 11:42:23 +0100815EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300816
Mark Brownd8050022011-09-28 18:28:23 +0100817static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
818{
Mark Brown9b8a83b2011-10-04 22:15:59 +0100819 if (w->power_checked)
820 return w->new_power;
821
Mark Brownd8050022011-09-28 18:28:23 +0100822 if (w->force)
Mark Brown9b8a83b2011-10-04 22:15:59 +0100823 w->new_power = 1;
Mark Brownd8050022011-09-28 18:28:23 +0100824 else
Mark Brown9b8a83b2011-10-04 22:15:59 +0100825 w->new_power = w->power_check(w);
826
827 w->power_checked = true;
828
829 return w->new_power;
Mark Brownd8050022011-09-28 18:28:23 +0100830}
831
Mark Browncd0f2d42009-04-20 16:56:59 +0100832/* Generic check to see if a widget should be powered.
833 */
834static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
835{
836 int in, out;
837
Mark Brownde02d072011-09-20 21:43:24 +0100838 DAPM_UPDATE_STAT(w, power_checks);
839
Mark Browncd0f2d42009-04-20 16:56:59 +0100840 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200841 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100842 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200843 dapm_clear_walk(w->dapm);
Mark Browncd0f2d42009-04-20 16:56:59 +0100844 return out != 0 && in != 0;
845}
846
Mark Brown6ea31b92009-04-20 17:15:41 +0100847/* Check to see if an ADC has power */
848static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
849{
850 int in;
851
Mark Brownde02d072011-09-20 21:43:24 +0100852 DAPM_UPDATE_STAT(w, power_checks);
853
Mark Brown6ea31b92009-04-20 17:15:41 +0100854 if (w->active) {
855 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200856 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100857 return in != 0;
858 } else {
859 return dapm_generic_check_power(w);
860 }
861}
862
863/* Check to see if a DAC has power */
864static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
865{
866 int out;
867
Mark Brownde02d072011-09-20 21:43:24 +0100868 DAPM_UPDATE_STAT(w, power_checks);
869
Mark Brown6ea31b92009-04-20 17:15:41 +0100870 if (w->active) {
871 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200872 dapm_clear_walk(w->dapm);
Mark Brown6ea31b92009-04-20 17:15:41 +0100873 return out != 0;
874 } else {
875 return dapm_generic_check_power(w);
876 }
877}
878
Mark Brown246d0a12009-04-22 18:24:55 +0100879/* Check to see if a power supply is needed */
880static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
881{
882 struct snd_soc_dapm_path *path;
Mark Brown246d0a12009-04-22 18:24:55 +0100883
Mark Brownde02d072011-09-20 21:43:24 +0100884 DAPM_UPDATE_STAT(w, power_checks);
885
Mark Brown246d0a12009-04-22 18:24:55 +0100886 /* Check if one of our outputs is connected */
887 list_for_each_entry(path, &w->sinks, list_source) {
Mark Browna8fdac82011-09-28 18:20:26 +0100888 DAPM_UPDATE_STAT(w, neighbour_checks);
889
Mark Brownbf3a9e12011-06-13 16:42:29 +0100890 if (path->weak)
891 continue;
892
Mark Brown215edda2009-09-08 18:59:05 +0100893 if (path->connected &&
894 !path->connected(path->source, path->sink))
895 continue;
896
Mark Brown30173582011-02-11 11:42:19 +0000897 if (!path->sink)
898 continue;
899
Mark Brownf68d7e12011-10-04 22:57:50 +0100900 if (dapm_widget_power_check(path->sink))
901 return 1;
Mark Brown246d0a12009-04-22 18:24:55 +0100902 }
903
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200904 dapm_clear_walk(w->dapm);
Mark Brown246d0a12009-04-22 18:24:55 +0100905
Mark Brownf68d7e12011-10-04 22:57:50 +0100906 return 0;
Mark Brown246d0a12009-04-22 18:24:55 +0100907}
908
Mark Brown35c64bc2011-09-28 18:23:53 +0100909static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
910{
911 return 1;
912}
913
Mark Brown38357ab2009-06-06 19:03:23 +0100914static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
915 struct snd_soc_dapm_widget *b,
Mark Brown828a8422011-01-15 13:14:30 +0000916 bool power_up)
Mark Brown42aa3412009-03-01 19:21:10 +0000917{
Mark Brown828a8422011-01-15 13:14:30 +0000918 int *sort;
919
920 if (power_up)
921 sort = dapm_up_seq;
922 else
923 sort = dapm_down_seq;
924
Mark Brown38357ab2009-06-06 19:03:23 +0100925 if (sort[a->id] != sort[b->id])
926 return sort[a->id] - sort[b->id];
Mark Brown20e48592011-01-15 13:40:50 +0000927 if (a->subseq != b->subseq) {
928 if (power_up)
929 return a->subseq - b->subseq;
930 else
931 return b->subseq - a->subseq;
932 }
Mark Brownb22ead22009-06-07 12:51:26 +0100933 if (a->reg != b->reg)
934 return a->reg - b->reg;
Mark Brown84dab562010-11-12 15:28:42 +0000935 if (a->dapm != b->dapm)
936 return (unsigned long)a->dapm - (unsigned long)b->dapm;
Mark Brown42aa3412009-03-01 19:21:10 +0000937
Mark Brown38357ab2009-06-06 19:03:23 +0100938 return 0;
939}
Mark Brown42aa3412009-03-01 19:21:10 +0000940
Mark Brown38357ab2009-06-06 19:03:23 +0100941/* Insert a widget in order into a DAPM power sequence. */
942static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
943 struct list_head *list,
Mark Brown828a8422011-01-15 13:14:30 +0000944 bool power_up)
Mark Brown38357ab2009-06-06 19:03:23 +0100945{
946 struct snd_soc_dapm_widget *w;
947
948 list_for_each_entry(w, list, power_list)
Mark Brown828a8422011-01-15 13:14:30 +0000949 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
Mark Brown38357ab2009-06-06 19:03:23 +0100950 list_add_tail(&new_widget->power_list, &w->power_list);
951 return;
Mark Brown42aa3412009-03-01 19:21:10 +0000952 }
Mark Brown6ea31b92009-04-20 17:15:41 +0100953
Mark Brown38357ab2009-06-06 19:03:23 +0100954 list_add_tail(&new_widget->power_list, list);
955}
Mark Brown42aa3412009-03-01 19:21:10 +0000956
Mark Brown68f89ad2010-11-03 23:51:49 -0400957static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
958 struct snd_soc_dapm_widget *w, int event)
959{
960 struct snd_soc_card *card = dapm->card;
961 const char *ev_name;
962 int power, ret;
963
964 switch (event) {
965 case SND_SOC_DAPM_PRE_PMU:
966 ev_name = "PRE_PMU";
967 power = 1;
968 break;
969 case SND_SOC_DAPM_POST_PMU:
970 ev_name = "POST_PMU";
971 power = 1;
972 break;
973 case SND_SOC_DAPM_PRE_PMD:
974 ev_name = "PRE_PMD";
975 power = 0;
976 break;
977 case SND_SOC_DAPM_POST_PMD:
978 ev_name = "POST_PMD";
979 power = 0;
980 break;
981 default:
982 BUG();
983 return;
984 }
985
986 if (w->power != power)
987 return;
988
989 if (w->event && (w->event_flags & event)) {
990 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
991 w->name, ev_name);
Mark Brown84e90932010-11-04 00:07:02 -0400992 trace_snd_soc_dapm_widget_event_start(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400993 ret = w->event(w, NULL, event);
Mark Brown84e90932010-11-04 00:07:02 -0400994 trace_snd_soc_dapm_widget_event_done(w, event);
Mark Brown68f89ad2010-11-03 23:51:49 -0400995 if (ret < 0)
996 pr_err("%s: %s event failed: %d\n",
997 ev_name, w->name, ret);
998 }
999}
1000
Mark Brownb22ead22009-06-07 12:51:26 +01001001/* Apply the coalesced changes from a DAPM sequence */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001002static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
Mark Brownb22ead22009-06-07 12:51:26 +01001003 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +01001004{
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001005 struct snd_soc_card *card = dapm->card;
Mark Brown68f89ad2010-11-03 23:51:49 -04001006 struct snd_soc_dapm_widget *w;
1007 int reg, power;
Mark Brownb22ead22009-06-07 12:51:26 +01001008 unsigned int value = 0;
1009 unsigned int mask = 0;
1010 unsigned int cur_mask;
1011
1012 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1013 power_list)->reg;
1014
1015 list_for_each_entry(w, pending, power_list) {
1016 cur_mask = 1 << w->shift;
1017 BUG_ON(reg != w->reg);
1018
1019 if (w->invert)
1020 power = !w->power;
1021 else
1022 power = w->power;
1023
1024 mask |= cur_mask;
1025 if (power)
1026 value |= cur_mask;
1027
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001028 pop_dbg(dapm->dev, card->pop_time,
Mark Brownb22ead22009-06-07 12:51:26 +01001029 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1030 w->name, reg, value, mask);
Mark Brown81628102009-06-07 13:21:24 +01001031
Mark Brown68f89ad2010-11-03 23:51:49 -04001032 /* Check for events */
1033 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1034 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001035 }
1036
Mark Brown81628102009-06-07 13:21:24 +01001037 if (reg >= 0) {
Mark Brown29376bc2011-06-19 13:49:28 +01001038 /* Any widget will do, they should all be updating the
1039 * same register.
1040 */
1041 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1042 power_list);
1043
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001044 pop_dbg(dapm->dev, card->pop_time,
Mark Brown81628102009-06-07 13:21:24 +01001045 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001046 value, mask, reg, card->pop_time);
1047 pop_wait(card->pop_time);
Liam Girdwood0445bdf2011-06-13 19:37:36 +01001048 soc_widget_update_bits(w, reg, mask, value);
Mark Brown81628102009-06-07 13:21:24 +01001049 }
1050
1051 list_for_each_entry(w, pending, power_list) {
Mark Brown68f89ad2010-11-03 23:51:49 -04001052 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1053 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
Mark Brown42aa3412009-03-01 19:21:10 +00001054 }
Mark Brown42aa3412009-03-01 19:21:10 +00001055}
1056
Mark Brownb22ead22009-06-07 12:51:26 +01001057/* Apply a DAPM power sequence.
1058 *
1059 * We walk over a pre-sorted list of widgets to apply power to. In
1060 * order to minimise the number of writes to the device required
1061 * multiple widgets will be updated in a single write where possible.
1062 * Currently anything that requires more than a single write is not
1063 * handled.
1064 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001065static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
Mark Brown828a8422011-01-15 13:14:30 +00001066 struct list_head *list, int event, bool power_up)
Mark Brownb22ead22009-06-07 12:51:26 +01001067{
1068 struct snd_soc_dapm_widget *w, *n;
1069 LIST_HEAD(pending);
1070 int cur_sort = -1;
Mark Brown20e48592011-01-15 13:40:50 +00001071 int cur_subseq = -1;
Mark Brownb22ead22009-06-07 12:51:26 +01001072 int cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001073 struct snd_soc_dapm_context *cur_dapm = NULL;
Mark Brown474b62d2011-01-18 16:14:44 +00001074 int ret, i;
Mark Brown828a8422011-01-15 13:14:30 +00001075 int *sort;
1076
1077 if (power_up)
1078 sort = dapm_up_seq;
1079 else
1080 sort = dapm_down_seq;
Mark Brown163cac02009-06-07 10:12:52 +01001081
Mark Brownb22ead22009-06-07 12:51:26 +01001082 list_for_each_entry_safe(w, n, list, power_list) {
1083 ret = 0;
1084
1085 /* Do we need to apply any queued changes? */
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001086 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
Mark Brown20e48592011-01-15 13:40:50 +00001087 w->dapm != cur_dapm || w->subseq != cur_subseq) {
Mark Brownb22ead22009-06-07 12:51:26 +01001088 if (!list_empty(&pending))
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001089 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brownb22ead22009-06-07 12:51:26 +01001090
Mark Brown474b62d2011-01-18 16:14:44 +00001091 if (cur_dapm && cur_dapm->seq_notifier) {
1092 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1093 if (sort[i] == cur_sort)
1094 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001095 i,
1096 cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001097 }
1098
Mark Brownb22ead22009-06-07 12:51:26 +01001099 INIT_LIST_HEAD(&pending);
1100 cur_sort = -1;
Mark Brownb0b3e6f2011-07-16 10:55:08 +09001101 cur_subseq = INT_MIN;
Mark Brownb22ead22009-06-07 12:51:26 +01001102 cur_reg = SND_SOC_NOPM;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001103 cur_dapm = NULL;
Mark Brownb22ead22009-06-07 12:51:26 +01001104 }
1105
Mark Brown163cac02009-06-07 10:12:52 +01001106 switch (w->id) {
1107 case snd_soc_dapm_pre:
1108 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001109 list_for_each_entry_safe_continue(w, n, list,
1110 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001111
Mark Brownb22ead22009-06-07 12:51:26 +01001112 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001113 ret = w->event(w,
1114 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001115 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001116 ret = w->event(w,
1117 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +01001118 break;
1119
1120 case snd_soc_dapm_post:
1121 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +01001122 list_for_each_entry_safe_continue(w, n, list,
1123 power_list);
Mark Brown163cac02009-06-07 10:12:52 +01001124
Mark Brownb22ead22009-06-07 12:51:26 +01001125 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +01001126 ret = w->event(w,
1127 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +01001128 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +01001129 ret = w->event(w,
1130 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +01001131 break;
1132
Mark Brown163cac02009-06-07 10:12:52 +01001133 default:
Mark Brown81628102009-06-07 13:21:24 +01001134 /* Queue it up for application */
1135 cur_sort = sort[w->id];
Mark Brown20e48592011-01-15 13:40:50 +00001136 cur_subseq = w->subseq;
Mark Brown81628102009-06-07 13:21:24 +01001137 cur_reg = w->reg;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001138 cur_dapm = w->dapm;
Mark Brown81628102009-06-07 13:21:24 +01001139 list_move(&w->power_list, &pending);
1140 break;
Mark Brown163cac02009-06-07 10:12:52 +01001141 }
Mark Brownb22ead22009-06-07 12:51:26 +01001142
1143 if (ret < 0)
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02001144 dev_err(w->dapm->dev,
1145 "Failed to apply widget power: %d\n", ret);
Mark Brown163cac02009-06-07 10:12:52 +01001146 }
Mark Brownb22ead22009-06-07 12:51:26 +01001147
1148 if (!list_empty(&pending))
Mark Brown28e86802011-03-08 19:29:53 +00001149 dapm_seq_run_coalesced(cur_dapm, &pending);
Mark Brown474b62d2011-01-18 16:14:44 +00001150
1151 if (cur_dapm && cur_dapm->seq_notifier) {
1152 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1153 if (sort[i] == cur_sort)
1154 cur_dapm->seq_notifier(cur_dapm,
Mark Brownf85a9e02011-01-26 21:41:28 +00001155 i, cur_subseq);
Mark Brown474b62d2011-01-18 16:14:44 +00001156 }
Mark Brown163cac02009-06-07 10:12:52 +01001157}
1158
Mark Brown97404f22010-12-14 16:13:57 +00001159static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1160{
1161 struct snd_soc_dapm_update *update = dapm->update;
1162 struct snd_soc_dapm_widget *w;
1163 int ret;
1164
1165 if (!update)
1166 return;
1167
1168 w = update->widget;
1169
1170 if (w->event &&
1171 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1172 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1173 if (ret != 0)
1174 pr_err("%s DAPM pre-event failed: %d\n",
1175 w->name, ret);
1176 }
1177
1178 ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1179 update->val);
1180 if (ret < 0)
1181 pr_err("%s DAPM update failed: %d\n", w->name, ret);
1182
1183 if (w->event &&
1184 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1185 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1186 if (ret != 0)
1187 pr_err("%s DAPM post-event failed: %d\n",
1188 w->name, ret);
1189 }
1190}
1191
Mark Brown9d0624a2011-02-18 11:49:43 -08001192/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1193 * they're changing state.
1194 */
1195static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1196{
1197 struct snd_soc_dapm_context *d = data;
1198 int ret;
Mark Brown97404f22010-12-14 16:13:57 +00001199
Mark Brown56fba412011-06-04 11:25:10 +01001200 /* If we're off and we're not supposed to be go into STANDBY */
1201 if (d->bias_level == SND_SOC_BIAS_OFF &&
1202 d->target_bias_level != SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001203 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1204 if (ret != 0)
1205 dev_err(d->dev,
1206 "Failed to turn on bias: %d\n", ret);
1207 }
1208
Mark Brown56fba412011-06-04 11:25:10 +01001209 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1210 if (d->bias_level != d->target_bias_level) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001211 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1212 if (ret != 0)
1213 dev_err(d->dev,
1214 "Failed to prepare bias: %d\n", ret);
1215 }
1216}
1217
1218/* Async callback run prior to DAPM sequences - brings to their final
1219 * state.
1220 */
1221static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1222{
1223 struct snd_soc_dapm_context *d = data;
1224 int ret;
1225
1226 /* If we just powered the last thing off drop to standby bias */
Mark Brown56fba412011-06-04 11:25:10 +01001227 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1228 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1229 d->target_bias_level == SND_SOC_BIAS_OFF)) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001230 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1231 if (ret != 0)
1232 dev_err(d->dev, "Failed to apply standby bias: %d\n",
1233 ret);
1234 }
1235
1236 /* If we're in standby and can support bias off then do that */
Mark Brown56fba412011-06-04 11:25:10 +01001237 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1238 d->target_bias_level == SND_SOC_BIAS_OFF) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001239 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1240 if (ret != 0)
1241 dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
1242 }
1243
1244 /* If we just powered up then move to active bias */
Mark Brown56fba412011-06-04 11:25:10 +01001245 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1246 d->target_bias_level == SND_SOC_BIAS_ON) {
Mark Brown9d0624a2011-02-18 11:49:43 -08001247 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1248 if (ret != 0)
1249 dev_err(d->dev, "Failed to apply active bias: %d\n",
1250 ret);
1251 }
1252}
Mark Brown97404f22010-12-14 16:13:57 +00001253
Mark Brownfe4fda52011-10-03 22:36:57 +01001254static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1255 bool power, bool connect)
1256{
1257 /* If a connection is being made or broken then that update
1258 * will have marked the peer dirty, otherwise the widgets are
1259 * not connected and this update has no impact. */
1260 if (!connect)
1261 return;
1262
1263 /* If the peer is already in the state we're moving to then we
1264 * won't have an impact on it. */
1265 if (power != peer->power)
Mark Brown75c1f892011-10-04 22:28:08 +01001266 dapm_mark_dirty(peer, "peer state change");
Mark Brownfe4fda52011-10-03 22:36:57 +01001267}
1268
Mark Brown05623c42011-09-28 17:02:31 +01001269static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1270 struct list_head *up_list,
1271 struct list_head *down_list)
1272{
Mark Browndb432b42011-10-03 21:06:40 +01001273 struct snd_soc_dapm_path *path;
1274
Mark Brown05623c42011-09-28 17:02:31 +01001275 if (w->power == power)
1276 return;
1277
1278 trace_snd_soc_dapm_widget_power(w, power);
1279
Mark Browndb432b42011-10-03 21:06:40 +01001280 /* If we changed our power state perhaps our neigbours changed
Mark Brownfe4fda52011-10-03 22:36:57 +01001281 * also.
Mark Browndb432b42011-10-03 21:06:40 +01001282 */
1283 list_for_each_entry(path, &w->sources, list_sink) {
1284 if (path->source) {
Mark Brownfe4fda52011-10-03 22:36:57 +01001285 dapm_widget_set_peer_power(path->source, power,
1286 path->connect);
Mark Browndb432b42011-10-03 21:06:40 +01001287 }
1288 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001289 switch (w->id) {
1290 case snd_soc_dapm_supply:
1291 /* Supplies can't affect their outputs, only their inputs */
1292 break;
1293 default:
1294 list_for_each_entry(path, &w->sinks, list_source) {
1295 if (path->sink) {
1296 dapm_widget_set_peer_power(path->sink, power,
1297 path->connect);
1298 }
Mark Browndb432b42011-10-03 21:06:40 +01001299 }
Mark Brownf3bf3e42011-10-04 22:43:31 +01001300 break;
Mark Browndb432b42011-10-03 21:06:40 +01001301 }
1302
Mark Brown05623c42011-09-28 17:02:31 +01001303 if (power)
1304 dapm_seq_insert(w, up_list, true);
1305 else
1306 dapm_seq_insert(w, down_list, false);
1307
1308 w->power = power;
1309}
1310
Mark Brown7c81beb2011-09-20 22:22:32 +01001311static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1312 struct list_head *up_list,
1313 struct list_head *down_list)
1314{
Mark Brown7c81beb2011-09-20 22:22:32 +01001315 int power;
1316
1317 switch (w->id) {
1318 case snd_soc_dapm_pre:
1319 dapm_seq_insert(w, down_list, false);
1320 break;
1321 case snd_soc_dapm_post:
1322 dapm_seq_insert(w, up_list, true);
1323 break;
1324
1325 default:
Mark Brownd8050022011-09-28 18:28:23 +01001326 power = dapm_widget_power_check(w);
Mark Brown7c81beb2011-09-20 22:22:32 +01001327
Mark Brown05623c42011-09-28 17:02:31 +01001328 dapm_widget_set_power(w, power, up_list, down_list);
Mark Brown7c81beb2011-09-20 22:22:32 +01001329 break;
1330 }
1331}
1332
Mark Brown42aa3412009-03-01 19:21:10 +00001333/*
Richard Purdie2b97eab2006-10-06 18:32:18 +02001334 * Scan each dapm widget for complete audio path.
1335 * A complete path is a route that has valid endpoints i.e.:-
1336 *
1337 * o DAC to output pin.
1338 * o Input Pin to ADC.
1339 * o Input pin to Output pin (bypass, sidetone)
1340 * o DAC to ADC (loopback).
1341 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001342static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001343{
Mark Brown12ea2c72011-03-02 18:17:32 +00001344 struct snd_soc_card *card = dapm->card;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001345 struct snd_soc_dapm_widget *w;
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001346 struct snd_soc_dapm_context *d;
Mark Brown291f3bb2009-06-07 13:57:17 +01001347 LIST_HEAD(up_list);
1348 LIST_HEAD(down_list);
Mark Brown9d0624a2011-02-18 11:49:43 -08001349 LIST_HEAD(async_domain);
Mark Brown56fba412011-06-04 11:25:10 +01001350 enum snd_soc_bias_level bias;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001351
Mark Brown84e90932010-11-04 00:07:02 -04001352 trace_snd_soc_dapm_start(card);
1353
Mark Brown56fba412011-06-04 11:25:10 +01001354 list_for_each_entry(d, &card->dapm_list, list) {
1355 if (d->n_widgets || d->codec == NULL) {
1356 if (d->idle_bias_off)
1357 d->target_bias_level = SND_SOC_BIAS_OFF;
1358 else
1359 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1360 }
1361 }
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001362
Mark Brownde02d072011-09-20 21:43:24 +01001363 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
1364
Mark Brown9b8a83b2011-10-04 22:15:59 +01001365 list_for_each_entry(w, &card->widgets, list) {
1366 w->power_checked = false;
Mark Brown024dc072011-10-09 11:52:05 +01001367 w->inputs = -1;
1368 w->outputs = -1;
Mark Brown9b8a83b2011-10-04 22:15:59 +01001369 }
1370
Mark Brown6d3ddc82009-05-16 17:47:29 +01001371 /* Check which widgets we need to power and store them in
Mark Browndb432b42011-10-03 21:06:40 +01001372 * lists indicating if they should be powered up or down. We
1373 * only check widgets that have been flagged as dirty but note
1374 * that new widgets may be added to the dirty list while we
1375 * iterate.
Mark Brown6d3ddc82009-05-16 17:47:29 +01001376 */
Mark Browndb432b42011-10-03 21:06:40 +01001377 list_for_each_entry(w, &card->dapm_dirty, dirty) {
Mark Brown7c81beb2011-09-20 22:22:32 +01001378 dapm_power_one_widget(w, &up_list, &down_list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001379 }
1380
Mark Brownf9de6d72011-09-28 17:19:47 +01001381 list_for_each_entry(w, &card->widgets, list) {
Mark Browndb432b42011-10-03 21:06:40 +01001382 list_del_init(&w->dirty);
1383
Mark Brownf9de6d72011-09-28 17:19:47 +01001384 if (w->power) {
1385 d = w->dapm;
1386
1387 /* Supplies and micbiases only bring the
1388 * context up to STANDBY as unless something
1389 * else is active and passing audio they
1390 * generally don't require full power.
1391 */
1392 switch (w->id) {
1393 case snd_soc_dapm_supply:
1394 case snd_soc_dapm_micbias:
1395 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1396 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1397 break;
1398 default:
1399 d->target_bias_level = SND_SOC_BIAS_ON;
1400 break;
1401 }
1402 }
1403
1404 }
1405
Mark Brownb14b76a2009-08-17 11:55:38 +01001406 /* If there are no DAPM widgets then try to figure out power from the
1407 * event type.
1408 */
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001409 if (!dapm->n_widgets) {
Mark Brownb14b76a2009-08-17 11:55:38 +01001410 switch (event) {
1411 case SND_SOC_DAPM_STREAM_START:
1412 case SND_SOC_DAPM_STREAM_RESUME:
Mark Brown56fba412011-06-04 11:25:10 +01001413 dapm->target_bias_level = SND_SOC_BIAS_ON;
Mark Brownb14b76a2009-08-17 11:55:38 +01001414 break;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001415 case SND_SOC_DAPM_STREAM_STOP:
Mark Brown56fba412011-06-04 11:25:10 +01001416 if (dapm->codec->active)
1417 dapm->target_bias_level = SND_SOC_BIAS_ON;
1418 else
1419 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Jarkko Nikula862af8a2010-12-10 20:53:55 +02001420 break;
Mark Brown50b6bce2009-11-23 13:11:53 +00001421 case SND_SOC_DAPM_STREAM_SUSPEND:
Mark Brown56fba412011-06-04 11:25:10 +01001422 dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
Mark Brown50b6bce2009-11-23 13:11:53 +00001423 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001424 case SND_SOC_DAPM_STREAM_NOP:
Mark Brown56fba412011-06-04 11:25:10 +01001425 dapm->target_bias_level = dapm->bias_level;
Mark Brown50b6bce2009-11-23 13:11:53 +00001426 break;
Mark Brownb14b76a2009-08-17 11:55:38 +01001427 default:
1428 break;
1429 }
1430 }
1431
Mark Brown85a843c2011-09-21 21:29:47 +01001432 /* Force all contexts in the card to the same bias state if
1433 * they're not ground referenced.
1434 */
Mark Brown56fba412011-06-04 11:25:10 +01001435 bias = SND_SOC_BIAS_OFF;
Mark Brown52ba67b2011-04-04 21:05:11 +09001436 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown56fba412011-06-04 11:25:10 +01001437 if (d->target_bias_level > bias)
1438 bias = d->target_bias_level;
Mark Brown52ba67b2011-04-04 21:05:11 +09001439 list_for_each_entry(d, &card->dapm_list, list)
Mark Brown85a843c2011-09-21 21:29:47 +01001440 if (!d->idle_bias_off)
1441 d->target_bias_level = bias;
Mark Brown52ba67b2011-04-04 21:05:11 +09001442
Mark Brownde02d072011-09-20 21:43:24 +01001443 trace_snd_soc_dapm_walk_done(card);
Mark Brown52ba67b2011-04-04 21:05:11 +09001444
Mark Brown9d0624a2011-02-18 11:49:43 -08001445 /* Run all the bias changes in parallel */
1446 list_for_each_entry(d, &dapm->card->dapm_list, list)
1447 async_schedule_domain(dapm_pre_sequence_async, d,
1448 &async_domain);
1449 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001450
Mark Brown6d3ddc82009-05-16 17:47:29 +01001451 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brown828a8422011-01-15 13:14:30 +00001452 dapm_seq_run(dapm, &down_list, event, false);
Mark Brown6d3ddc82009-05-16 17:47:29 +01001453
Mark Brown97404f22010-12-14 16:13:57 +00001454 dapm_widget_update(dapm);
1455
Mark Brown6d3ddc82009-05-16 17:47:29 +01001456 /* Now power up. */
Mark Brown828a8422011-01-15 13:14:30 +00001457 dapm_seq_run(dapm, &up_list, event, true);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001458
Mark Brown9d0624a2011-02-18 11:49:43 -08001459 /* Run all the bias changes in parallel */
1460 list_for_each_entry(d, &dapm->card->dapm_list, list)
1461 async_schedule_domain(dapm_post_sequence_async, d,
1462 &async_domain);
1463 async_synchronize_full_domain(&async_domain);
Mark Brown452c5ea2009-05-17 21:41:23 +01001464
Jarkko Nikulafd8d3bc2010-11-09 14:40:28 +02001465 pop_dbg(dapm->dev, card->pop_time,
1466 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001467 pop_wait(card->pop_time);
Mark Browncb507e72009-07-08 18:54:57 +01001468
Mark Brown84e90932010-11-04 00:07:02 -04001469 trace_snd_soc_dapm_done(card);
1470
Mark Brown42aa3412009-03-01 19:21:10 +00001471 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001472}
1473
Mark Brown79fb9382009-08-21 16:38:13 +01001474#ifdef CONFIG_DEBUG_FS
1475static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1476{
1477 file->private_data = inode->i_private;
1478 return 0;
1479}
1480
1481static ssize_t dapm_widget_power_read_file(struct file *file,
1482 char __user *user_buf,
1483 size_t count, loff_t *ppos)
1484{
1485 struct snd_soc_dapm_widget *w = file->private_data;
1486 char *buf;
1487 int in, out;
1488 ssize_t ret;
1489 struct snd_soc_dapm_path *p = NULL;
1490
1491 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1492 if (!buf)
1493 return -ENOMEM;
1494
1495 in = is_connected_input_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001496 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001497 out = is_connected_output_ep(w);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001498 dapm_clear_walk(w->dapm);
Mark Brown79fb9382009-08-21 16:38:13 +01001499
Mark Brownd033c362009-12-04 15:25:56 +00001500 ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
Mark Brown79fb9382009-08-21 16:38:13 +01001501 w->name, w->power ? "On" : "Off", in, out);
1502
Mark Brownd033c362009-12-04 15:25:56 +00001503 if (w->reg >= 0)
1504 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1505 " - R%d(0x%x) bit %d",
1506 w->reg, w->reg, w->shift);
1507
1508 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1509
Mark Brown3eef08b2009-09-14 16:49:00 +01001510 if (w->sname)
1511 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1512 w->sname,
1513 w->active ? "active" : "inactive");
Mark Brown79fb9382009-08-21 16:38:13 +01001514
1515 list_for_each_entry(p, &w->sources, list_sink) {
Mark Brown215edda2009-09-08 18:59:05 +01001516 if (p->connected && !p->connected(w, p->sink))
1517 continue;
1518
Mark Brown79fb9382009-08-21 16:38:13 +01001519 if (p->connect)
1520 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001521 " in \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001522 p->name ? p->name : "static",
1523 p->source->name);
1524 }
1525 list_for_each_entry(p, &w->sinks, list_source) {
Mark Brown215edda2009-09-08 18:59:05 +01001526 if (p->connected && !p->connected(w, p->sink))
1527 continue;
1528
Mark Brown79fb9382009-08-21 16:38:13 +01001529 if (p->connect)
1530 ret += snprintf(buf + ret, PAGE_SIZE - ret,
Dimitris Papastamos67f5ed62011-02-24 17:09:32 +00001531 " out \"%s\" \"%s\"\n",
Mark Brown79fb9382009-08-21 16:38:13 +01001532 p->name ? p->name : "static",
1533 p->sink->name);
1534 }
1535
1536 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1537
1538 kfree(buf);
1539 return ret;
1540}
1541
1542static const struct file_operations dapm_widget_power_fops = {
1543 .open = dapm_widget_power_open_file,
1544 .read = dapm_widget_power_read_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001545 .llseek = default_llseek,
Mark Brown79fb9382009-08-21 16:38:13 +01001546};
1547
Mark Brownef49e4f2011-04-04 20:48:13 +09001548static int dapm_bias_open_file(struct inode *inode, struct file *file)
1549{
1550 file->private_data = inode->i_private;
1551 return 0;
1552}
1553
1554static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1555 size_t count, loff_t *ppos)
1556{
1557 struct snd_soc_dapm_context *dapm = file->private_data;
1558 char *level;
1559
1560 switch (dapm->bias_level) {
1561 case SND_SOC_BIAS_ON:
1562 level = "On\n";
1563 break;
1564 case SND_SOC_BIAS_PREPARE:
1565 level = "Prepare\n";
1566 break;
1567 case SND_SOC_BIAS_STANDBY:
1568 level = "Standby\n";
1569 break;
1570 case SND_SOC_BIAS_OFF:
1571 level = "Off\n";
1572 break;
1573 default:
1574 BUG();
1575 level = "Unknown\n";
1576 break;
1577 }
1578
1579 return simple_read_from_buffer(user_buf, count, ppos, level,
1580 strlen(level));
1581}
1582
1583static const struct file_operations dapm_bias_fops = {
1584 .open = dapm_bias_open_file,
1585 .read = dapm_bias_read_file,
1586 .llseek = default_llseek,
1587};
1588
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001589void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1590 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001591{
Mark Brown79fb9382009-08-21 16:38:13 +01001592 struct dentry *d;
1593
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001594 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1595
1596 if (!dapm->debugfs_dapm) {
1597 printk(KERN_WARNING
1598 "Failed to create DAPM debugfs directory\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001599 return;
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001600 }
Mark Brown79fb9382009-08-21 16:38:13 +01001601
Mark Brownef49e4f2011-04-04 20:48:13 +09001602 d = debugfs_create_file("bias_level", 0444,
1603 dapm->debugfs_dapm, dapm,
1604 &dapm_bias_fops);
1605 if (!d)
1606 dev_warn(dapm->dev,
1607 "ASoC: Failed to create bias level debugfs file\n");
Mark Brown79fb9382009-08-21 16:38:13 +01001608}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001609
1610static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1611{
1612 struct snd_soc_dapm_context *dapm = w->dapm;
1613 struct dentry *d;
1614
1615 if (!dapm->debugfs_dapm || !w->name)
1616 return;
1617
1618 d = debugfs_create_file(w->name, 0444,
1619 dapm->debugfs_dapm, w,
1620 &dapm_widget_power_fops);
1621 if (!d)
1622 dev_warn(w->dapm->dev,
1623 "ASoC: Failed to create %s debugfs file\n",
1624 w->name);
1625}
1626
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001627static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1628{
1629 debugfs_remove_recursive(dapm->debugfs_dapm);
1630}
1631
Mark Brown79fb9382009-08-21 16:38:13 +01001632#else
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +02001633void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1634 struct dentry *parent)
Mark Brown79fb9382009-08-21 16:38:13 +01001635{
1636}
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001637
1638static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1639{
1640}
1641
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02001642static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1643{
1644}
1645
Mark Brown79fb9382009-08-21 16:38:13 +01001646#endif
1647
Richard Purdie2b97eab2006-10-06 18:32:18 +02001648/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001649static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown3a655772009-10-05 17:23:30 +01001650 struct snd_kcontrol *kcontrol, int change,
1651 int mux, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001652{
1653 struct snd_soc_dapm_path *path;
1654 int found = 0;
1655
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001656 if (widget->id != snd_soc_dapm_mux &&
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001657 widget->id != snd_soc_dapm_virt_mux &&
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001658 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001659 return -ENODEV;
1660
Mark Brown3a655772009-10-05 17:23:30 +01001661 if (!change)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001662 return 0;
1663
1664 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001665 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001666 if (path->kcontrol != kcontrol)
1667 continue;
1668
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001669 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001670 continue;
1671
1672 found = 1;
1673 /* we now need to match the string in the enum to the path */
Mark Browndb432b42011-10-03 21:06:40 +01001674 if (!(strcmp(path->name, e->texts[mux]))) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001675 path->connect = 1; /* new connection */
Mark Brown75c1f892011-10-04 22:28:08 +01001676 dapm_mark_dirty(path->source, "mux connection");
Mark Browndb432b42011-10-03 21:06:40 +01001677 } else {
1678 if (path->connect)
Mark Brown75c1f892011-10-04 22:28:08 +01001679 dapm_mark_dirty(path->source,
1680 "mux disconnection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001681 path->connect = 0; /* old connection must be powered down */
Mark Browndb432b42011-10-03 21:06:40 +01001682 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001683 }
1684
Mark Browndb432b42011-10-03 21:06:40 +01001685 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001686 dapm_mark_dirty(widget, "mux change");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001687 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001688 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001689
1690 return 0;
1691}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001692
Milan plzik1b075e32008-01-10 14:39:46 +01001693/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001694static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
Mark Brown283375c2009-12-07 18:09:03 +00001695 struct snd_kcontrol *kcontrol, int connect)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001696{
1697 struct snd_soc_dapm_path *path;
1698 int found = 0;
1699
Milan plzik1b075e32008-01-10 14:39:46 +01001700 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001701 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001702 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001703 return -ENODEV;
1704
Richard Purdie2b97eab2006-10-06 18:32:18 +02001705 /* find dapm widget path assoc with kcontrol */
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001706 list_for_each_entry(path, &widget->dapm->card->paths, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001707 if (path->kcontrol != kcontrol)
1708 continue;
1709
1710 /* found, now check type */
1711 found = 1;
Mark Brown283375c2009-12-07 18:09:03 +00001712 path->connect = connect;
Mark Brown75c1f892011-10-04 22:28:08 +01001713 dapm_mark_dirty(path->source, "mixer connection");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001714 }
1715
Mark Browndb432b42011-10-03 21:06:40 +01001716 if (found) {
Mark Brown75c1f892011-10-04 22:28:08 +01001717 dapm_mark_dirty(widget, "mixer update");
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001718 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
Mark Browndb432b42011-10-03 21:06:40 +01001719 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001720
1721 return 0;
1722}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001723
1724/* show dapm widget status in sys fs */
1725static ssize_t dapm_widget_show(struct device *dev,
1726 struct device_attribute *attr, char *buf)
1727{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001728 struct snd_soc_pcm_runtime *rtd =
1729 container_of(dev, struct snd_soc_pcm_runtime, dev);
1730 struct snd_soc_codec *codec =rtd->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001731 struct snd_soc_dapm_widget *w;
1732 int count = 0;
1733 char *state = "not set";
1734
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001735 list_for_each_entry(w, &codec->card->widgets, list) {
1736 if (w->dapm != &codec->dapm)
1737 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001738
1739 /* only display widgets that burnm power */
1740 switch (w->id) {
1741 case snd_soc_dapm_hp:
1742 case snd_soc_dapm_mic:
1743 case snd_soc_dapm_spk:
1744 case snd_soc_dapm_line:
1745 case snd_soc_dapm_micbias:
1746 case snd_soc_dapm_dac:
1747 case snd_soc_dapm_adc:
1748 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001749 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001750 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001751 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001752 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001753 if (w->name)
1754 count += sprintf(buf + count, "%s: %s\n",
1755 w->name, w->power ? "On":"Off");
1756 break;
1757 default:
1758 break;
1759 }
1760 }
1761
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001762 switch (codec->dapm.bias_level) {
Mark Brown0be98982008-05-19 12:31:28 +02001763 case SND_SOC_BIAS_ON:
1764 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001765 break;
Mark Brown0be98982008-05-19 12:31:28 +02001766 case SND_SOC_BIAS_PREPARE:
1767 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001768 break;
Mark Brown0be98982008-05-19 12:31:28 +02001769 case SND_SOC_BIAS_STANDBY:
1770 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001771 break;
Mark Brown0be98982008-05-19 12:31:28 +02001772 case SND_SOC_BIAS_OFF:
1773 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001774 break;
1775 }
1776 count += sprintf(buf + count, "PM State: %s\n", state);
1777
1778 return count;
1779}
1780
1781static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1782
1783int snd_soc_dapm_sys_add(struct device *dev)
1784{
Troy Kisky12ef1932008-10-13 17:42:14 -07001785 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001786}
1787
1788static void snd_soc_dapm_sys_remove(struct device *dev)
1789{
Mark Brownaef90842009-05-16 17:53:16 +01001790 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001791}
1792
1793/* free all dapm widgets and resources */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001794static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001795{
1796 struct snd_soc_dapm_widget *w, *next_w;
1797 struct snd_soc_dapm_path *p, *next_p;
1798
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001799 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1800 if (w->dapm != dapm)
1801 continue;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001802 list_del(&w->list);
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001803 /*
1804 * remove source and sink paths associated to this widget.
1805 * While removing the path, remove reference to it from both
1806 * source and sink widgets so that path is removed only once.
1807 */
1808 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1809 list_del(&p->list_sink);
1810 list_del(&p->list_source);
1811 list_del(&p->list);
1812 kfree(p->long_name);
1813 kfree(p);
1814 }
1815 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1816 list_del(&p->list_sink);
1817 list_del(&p->list_source);
1818 list_del(&p->list);
1819 kfree(p->long_name);
1820 kfree(p);
1821 }
Stephen Warrenfad59882011-04-28 17:37:59 -06001822 kfree(w->kcontrols);
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001823 kfree(w->name);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001824 kfree(w);
1825 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001826}
1827
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001828static struct snd_soc_dapm_widget *dapm_find_widget(
1829 struct snd_soc_dapm_context *dapm, const char *pin,
1830 bool search_other_contexts)
1831{
1832 struct snd_soc_dapm_widget *w;
1833 struct snd_soc_dapm_widget *fallback = NULL;
1834
1835 list_for_each_entry(w, &dapm->card->widgets, list) {
1836 if (!strcmp(w->name, pin)) {
1837 if (w->dapm == dapm)
1838 return w;
1839 else
1840 fallback = w;
1841 }
1842 }
1843
1844 if (search_other_contexts)
1845 return fallback;
1846
1847 return NULL;
1848}
1849
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001850static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
Mark Brown16499232009-01-07 18:25:13 +00001851 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001852{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001853 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Liam Girdwooda5302182008-07-07 13:35:17 +01001854
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001855 if (!w) {
1856 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1857 return -EINVAL;
Liam Girdwooda5302182008-07-07 13:35:17 +01001858 }
1859
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001860 w->connected = status;
1861 if (status == 0)
1862 w->force = 0;
Mark Brown75c1f892011-10-04 22:28:08 +01001863 dapm_mark_dirty(w, "pin configuration");
Mark Brown0d867332011-04-06 11:38:14 +09001864
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02001865 return 0;
Liam Girdwooda5302182008-07-07 13:35:17 +01001866}
1867
Richard Purdie2b97eab2006-10-06 18:32:18 +02001868/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001869 * snd_soc_dapm_sync - scan and power dapm paths
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001870 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02001871 *
1872 * Walks all dapm audio paths and powers widgets according to their
1873 * stream or path usage.
1874 *
1875 * Returns 0 for success.
1876 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001877int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001878{
Mark Brown4f4c0072011-10-07 14:29:19 +01001879 /*
1880 * Suppress early reports (eg, jacks syncing their state) to avoid
1881 * silly DAPM runs during card startup.
1882 */
1883 if (!dapm->card || !dapm->card->instantiated)
1884 return 0;
1885
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001886 return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001887}
Liam Girdwooda5302182008-07-07 13:35:17 +01001888EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001889
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001890static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
Mark Brown215edda2009-09-08 18:59:05 +01001891 const struct snd_soc_dapm_route *route)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001892{
1893 struct snd_soc_dapm_path *path;
1894 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001895 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001896 const char *sink;
Mark Brown215edda2009-09-08 18:59:05 +01001897 const char *control = route->control;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001898 const char *source;
1899 char prefixed_sink[80];
1900 char prefixed_source[80];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001901 int ret = 0;
1902
Mark Brown88e8b9a2011-03-02 18:18:24 +00001903 if (dapm->codec && dapm->codec->name_prefix) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001904 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1905 dapm->codec->name_prefix, route->sink);
1906 sink = prefixed_sink;
1907 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1908 dapm->codec->name_prefix, route->source);
1909 source = prefixed_source;
1910 } else {
1911 sink = route->sink;
1912 source = route->source;
1913 }
1914
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001915 /*
1916 * find src and dest widgets over all widgets but favor a widget from
1917 * current DAPM context
1918 */
1919 list_for_each_entry(w, &dapm->card->widgets, list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001920 if (!wsink && !(strcmp(w->name, sink))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001921 wtsink = w;
1922 if (w->dapm == dapm)
1923 wsink = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001924 continue;
1925 }
1926 if (!wsource && !(strcmp(w->name, source))) {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001927 wtsource = w;
1928 if (w->dapm == dapm)
1929 wsource = w;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001930 }
1931 }
Jarkko Nikula97c866d2010-12-14 12:18:31 +02001932 /* use widget from another DAPM context if not found from this */
1933 if (!wsink)
1934 wsink = wtsink;
1935 if (!wsource)
1936 wsource = wtsource;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001937
1938 if (wsource == NULL || wsink == NULL)
1939 return -ENODEV;
1940
1941 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1942 if (!path)
1943 return -ENOMEM;
1944
1945 path->source = wsource;
1946 path->sink = wsink;
Mark Brown215edda2009-09-08 18:59:05 +01001947 path->connected = route->connected;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001948 INIT_LIST_HEAD(&path->list);
1949 INIT_LIST_HEAD(&path->list_source);
1950 INIT_LIST_HEAD(&path->list_sink);
1951
1952 /* check for external widgets */
1953 if (wsink->id == snd_soc_dapm_input) {
1954 if (wsource->id == snd_soc_dapm_micbias ||
1955 wsource->id == snd_soc_dapm_mic ||
Rongrong Cao087d53a2009-07-10 20:13:30 +01001956 wsource->id == snd_soc_dapm_line ||
1957 wsource->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001958 wsink->ext = 1;
1959 }
1960 if (wsource->id == snd_soc_dapm_output) {
1961 if (wsink->id == snd_soc_dapm_spk ||
1962 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001963 wsink->id == snd_soc_dapm_line ||
1964 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001965 wsource->ext = 1;
1966 }
1967
1968 /* connect static paths */
1969 if (control == NULL) {
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001970 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001971 list_add(&path->list_sink, &wsink->sources);
1972 list_add(&path->list_source, &wsource->sinks);
1973 path->connect = 1;
1974 return 0;
1975 }
1976
1977 /* connect dynamic paths */
Lu Guanqundc2bea62011-04-20 16:00:36 +08001978 switch (wsink->id) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001979 case snd_soc_dapm_adc:
1980 case snd_soc_dapm_dac:
1981 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06001982 case snd_soc_dapm_out_drv:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001983 case snd_soc_dapm_input:
1984 case snd_soc_dapm_output:
1985 case snd_soc_dapm_micbias:
1986 case snd_soc_dapm_vmid:
1987 case snd_soc_dapm_pre:
1988 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001989 case snd_soc_dapm_supply:
Mark Brown010ff262009-08-17 17:39:22 +01001990 case snd_soc_dapm_aif_in:
1991 case snd_soc_dapm_aif_out:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02001992 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001993 list_add(&path->list_sink, &wsink->sources);
1994 list_add(&path->list_source, &wsource->sinks);
1995 path->connect = 1;
1996 return 0;
1997 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00001998 case snd_soc_dapm_virt_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001999 case snd_soc_dapm_value_mux:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002000 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
Stephen Warren82cfecd2011-04-28 17:37:58 -06002001 &wsink->kcontrol_news[0]);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002002 if (ret != 0)
2003 goto err;
2004 break;
2005 case snd_soc_dapm_switch:
2006 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002007 case snd_soc_dapm_mixer_named_ctl:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002008 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002009 if (ret != 0)
2010 goto err;
2011 break;
2012 case snd_soc_dapm_hp:
2013 case snd_soc_dapm_mic:
2014 case snd_soc_dapm_line:
2015 case snd_soc_dapm_spk:
Jarkko Nikula8ddab3f2010-12-14 12:18:30 +02002016 list_add(&path->list, &dapm->card->paths);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002017 list_add(&path->list_sink, &wsink->sources);
2018 list_add(&path->list_source, &wsource->sinks);
2019 path->connect = 0;
2020 return 0;
2021 }
2022 return 0;
2023
2024err:
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002025 dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2026 source, control, sink);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002027 kfree(path);
2028 return ret;
2029}
Mark Brown105f1c22008-05-13 14:52:19 +02002030
2031/**
Mark Brown105f1c22008-05-13 14:52:19 +02002032 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002033 * @dapm: DAPM context
Mark Brown105f1c22008-05-13 14:52:19 +02002034 * @route: audio routes
2035 * @num: number of routes
2036 *
2037 * Connects 2 dapm widgets together via a named audio path. The sink is
2038 * the widget receiving the audio signal, whilst the source is the sender
2039 * of the audio signal.
2040 *
2041 * Returns 0 for success else error. On error all resources can be freed
2042 * with a call to snd_soc_card_free().
2043 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002044int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
Mark Brown105f1c22008-05-13 14:52:19 +02002045 const struct snd_soc_dapm_route *route, int num)
2046{
2047 int i, ret;
2048
2049 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002050 ret = snd_soc_dapm_add_route(dapm, route);
Mark Brown105f1c22008-05-13 14:52:19 +02002051 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002052 dev_err(dapm->dev, "Failed to add route %s->%s\n",
2053 route->source, route->sink);
Mark Brown105f1c22008-05-13 14:52:19 +02002054 return ret;
2055 }
2056 route++;
2057 }
2058
2059 return 0;
2060}
2061EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2062
Mark Brownbf3a9e12011-06-13 16:42:29 +01002063static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2064 const struct snd_soc_dapm_route *route)
2065{
2066 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2067 route->source,
2068 true);
2069 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2070 route->sink,
2071 true);
2072 struct snd_soc_dapm_path *path;
2073 int count = 0;
2074
2075 if (!source) {
2076 dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2077 route->source);
2078 return -ENODEV;
2079 }
2080
2081 if (!sink) {
2082 dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2083 route->sink);
2084 return -ENODEV;
2085 }
2086
2087 if (route->control || route->connected)
2088 dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2089 route->source, route->sink);
2090
2091 list_for_each_entry(path, &source->sinks, list_source) {
2092 if (path->sink == sink) {
2093 path->weak = 1;
2094 count++;
2095 }
2096 }
2097
2098 if (count == 0)
2099 dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2100 route->source, route->sink);
2101 if (count > 1)
2102 dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2103 count, route->source, route->sink);
2104
2105 return 0;
2106}
2107
2108/**
2109 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2110 * @dapm: DAPM context
2111 * @route: audio routes
2112 * @num: number of routes
2113 *
2114 * Mark existing routes matching those specified in the passed array
2115 * as being weak, meaning that they are ignored for the purpose of
2116 * power decisions. The main intended use case is for sidetone paths
2117 * which couple audio between other independent paths if they are both
2118 * active in order to make the combination work better at the user
2119 * level but which aren't intended to be "used".
2120 *
2121 * Note that CODEC drivers should not use this as sidetone type paths
2122 * can frequently also be used as bypass paths.
2123 */
2124int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2125 const struct snd_soc_dapm_route *route, int num)
2126{
2127 int i, err;
2128 int ret = 0;
2129
2130 for (i = 0; i < num; i++) {
2131 err = snd_soc_dapm_weak_route(dapm, route);
2132 if (err)
2133 ret = err;
2134 route++;
2135 }
2136
2137 return ret;
2138}
2139EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2140
Mark Brown105f1c22008-05-13 14:52:19 +02002141/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002142 * snd_soc_dapm_new_widgets - add new dapm widgets
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002143 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002144 *
2145 * Checks the codec for any new dapm widgets and creates them if found.
2146 *
2147 * Returns 0 for success.
2148 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002149int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002150{
2151 struct snd_soc_dapm_widget *w;
Mark Brownb66a70d2011-02-09 18:04:11 +00002152 unsigned int val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002153
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002154 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002155 {
2156 if (w->new)
2157 continue;
2158
Stephen Warrenfad59882011-04-28 17:37:59 -06002159 if (w->num_kcontrols) {
2160 w->kcontrols = kzalloc(w->num_kcontrols *
2161 sizeof(struct snd_kcontrol *),
2162 GFP_KERNEL);
2163 if (!w->kcontrols)
2164 return -ENOMEM;
2165 }
2166
Richard Purdie2b97eab2006-10-06 18:32:18 +02002167 switch(w->id) {
2168 case snd_soc_dapm_switch:
2169 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00002170 case snd_soc_dapm_mixer_named_ctl:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002171 dapm_new_mixer(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002172 break;
2173 case snd_soc_dapm_mux:
Dimitris Papastamos24ff33a2010-12-16 15:53:39 +00002174 case snd_soc_dapm_virt_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002175 case snd_soc_dapm_value_mux:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002176 dapm_new_mux(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002177 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002178 case snd_soc_dapm_pga:
Olaya, Margaritad88429a2010-12-10 21:11:44 -06002179 case snd_soc_dapm_out_drv:
Lars-Peter Clausen4b80b8c2011-06-09 13:22:36 +02002180 dapm_new_pga(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002181 break;
Mark Brown7ca3a182011-10-08 14:04:50 +01002182 default:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002183 break;
2184 }
Mark Brownb66a70d2011-02-09 18:04:11 +00002185
2186 /* Read the initial power state from the device */
2187 if (w->reg >= 0) {
Liam Girdwood0445bdf2011-06-13 19:37:36 +01002188 val = soc_widget_read(w, w->reg);
Mark Brownb66a70d2011-02-09 18:04:11 +00002189 val &= 1 << w->shift;
2190 if (w->invert)
2191 val = !val;
2192
2193 if (val)
2194 w->power = 1;
2195 }
2196
Richard Purdie2b97eab2006-10-06 18:32:18 +02002197 w->new = 1;
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002198
Mark Brown7508b122011-10-05 12:09:12 +01002199 dapm_mark_dirty(w, "new widget");
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002200 dapm_debugfs_add_widget(w);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002201 }
2202
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002203 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002204 return 0;
2205}
2206EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2207
2208/**
2209 * snd_soc_dapm_get_volsw - dapm mixer get callback
2210 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002211 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002212 *
2213 * Callback to get the value of a dapm mixer control.
2214 *
2215 * Returns 0 for success.
2216 */
2217int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2218 struct snd_ctl_elem_value *ucontrol)
2219{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002220 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2221 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Jon Smirl4eaa9812008-07-29 11:42:26 +01002222 struct soc_mixer_control *mc =
2223 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002224 unsigned int reg = mc->reg;
2225 unsigned int shift = mc->shift;
2226 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002227 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002228 unsigned int invert = mc->invert;
2229 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002230
Richard Purdie2b97eab2006-10-06 18:32:18 +02002231 ucontrol->value.integer.value[0] =
2232 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2233 if (shift != rshift)
2234 ucontrol->value.integer.value[1] =
2235 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
2236 if (invert) {
2237 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002238 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002239 if (shift != rshift)
2240 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002241 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002242 }
2243
2244 return 0;
2245}
2246EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2247
2248/**
2249 * snd_soc_dapm_put_volsw - dapm mixer set callback
2250 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002251 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002252 *
2253 * Callback to set the value of a dapm mixer control.
2254 *
2255 * Returns 0 for success.
2256 */
2257int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2258 struct snd_ctl_elem_value *ucontrol)
2259{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002260 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2261 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2262 struct snd_soc_codec *codec = widget->codec;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002263 struct soc_mixer_control *mc =
2264 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04002265 unsigned int reg = mc->reg;
2266 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002267 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002268 unsigned int mask = (1 << fls(max)) - 1;
2269 unsigned int invert = mc->invert;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002270 unsigned int val;
Mark Brown97404f22010-12-14 16:13:57 +00002271 int connect, change;
2272 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002273 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002274
2275 val = (ucontrol->value.integer.value[0] & mask);
2276
2277 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002278 val = max - val;
Stephen Warrene9cf7042011-01-27 14:54:05 -07002279 mask = mask << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002280 val = val << shift;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002281
Stephen Warrenfafd2172011-04-28 17:38:00 -06002282 if (val)
2283 /* new connection */
2284 connect = invert ? 0 : 1;
2285 else
2286 /* old connection must be powered down */
2287 connect = invert ? 1 : 0;
2288
2289 mutex_lock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002290
Stephen Warrene9cf7042011-01-27 14:54:05 -07002291 change = snd_soc_test_bits(widget->codec, reg, mask, val);
Mark Brown97404f22010-12-14 16:13:57 +00002292 if (change) {
Stephen Warrenfafd2172011-04-28 17:38:00 -06002293 for (wi = 0; wi < wlist->num_widgets; wi++) {
2294 widget = wlist->widgets[wi];
Mark Brown283375c2009-12-07 18:09:03 +00002295
Stephen Warrenfafd2172011-04-28 17:38:00 -06002296 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002297
Stephen Warrenfafd2172011-04-28 17:38:00 -06002298 update.kcontrol = kcontrol;
2299 update.widget = widget;
2300 update.reg = reg;
2301 update.mask = mask;
2302 update.val = val;
2303 widget->dapm->update = &update;
Mark Brown97404f22010-12-14 16:13:57 +00002304
Stephen Warrenfafd2172011-04-28 17:38:00 -06002305 dapm_mixer_update_power(widget, kcontrol, connect);
2306
2307 widget->dapm->update = NULL;
2308 }
Mark Brown283375c2009-12-07 18:09:03 +00002309 }
2310
Stephen Warrenfafd2172011-04-28 17:38:00 -06002311 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002312 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002313}
2314EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2315
2316/**
2317 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2318 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002319 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002320 *
2321 * Callback to get the value of a dapm enumerated double mixer control.
2322 *
2323 * Returns 0 for success.
2324 */
2325int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2326 struct snd_ctl_elem_value *ucontrol)
2327{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002328 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2329 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02002330 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002331 unsigned int val, bitmask;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002332
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002333 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002334 ;
2335 val = snd_soc_read(widget->codec, e->reg);
2336 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2337 if (e->shift_l != e->shift_r)
2338 ucontrol->value.enumerated.item[1] =
2339 (val >> e->shift_r) & (bitmask - 1);
2340
2341 return 0;
2342}
2343EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2344
2345/**
2346 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2347 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002348 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02002349 *
2350 * Callback to set the value of a dapm enumerated double mixer control.
2351 *
2352 * Returns 0 for success.
2353 */
2354int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2355 struct snd_ctl_elem_value *ucontrol)
2356{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002357 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2358 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2359 struct snd_soc_codec *codec = widget->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002360 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002361 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002362 unsigned int mask, bitmask;
Mark Brown97404f22010-12-14 16:13:57 +00002363 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002364 int wi;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002365
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002366 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002367 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002368 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002369 return -EINVAL;
2370 mux = ucontrol->value.enumerated.item[0];
2371 val = mux << e->shift_l;
2372 mask = (bitmask - 1) << e->shift_l;
2373 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002374 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002375 return -EINVAL;
2376 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2377 mask |= (bitmask - 1) << e->shift_r;
2378 }
2379
Stephen Warrenfafd2172011-04-28 17:38:00 -06002380 mutex_lock(&codec->mutex);
2381
Mark Brown3a655772009-10-05 17:23:30 +01002382 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002383 if (change) {
2384 for (wi = 0; wi < wlist->num_widgets; wi++) {
2385 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002386
Stephen Warrenfafd2172011-04-28 17:38:00 -06002387 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002388
Stephen Warrenfafd2172011-04-28 17:38:00 -06002389 update.kcontrol = kcontrol;
2390 update.widget = widget;
2391 update.reg = e->reg;
2392 update.mask = mask;
2393 update.val = val;
2394 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002395
Stephen Warrenfafd2172011-04-28 17:38:00 -06002396 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002397
Stephen Warrenfafd2172011-04-28 17:38:00 -06002398 widget->dapm->update = NULL;
2399 }
2400 }
2401
2402 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002403 return change;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002404}
2405EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2406
2407/**
Mark Brownd2b247a2009-10-06 15:21:04 +01002408 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2409 * @kcontrol: mixer control
2410 * @ucontrol: control element information
2411 *
2412 * Returns 0 for success.
2413 */
2414int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2415 struct snd_ctl_elem_value *ucontrol)
2416{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002417 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2418 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Mark Brownd2b247a2009-10-06 15:21:04 +01002419
2420 ucontrol->value.enumerated.item[0] = widget->value;
2421
2422 return 0;
2423}
2424EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2425
2426/**
2427 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2428 * @kcontrol: mixer control
2429 * @ucontrol: control element information
2430 *
2431 * Returns 0 for success.
2432 */
2433int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2434 struct snd_ctl_elem_value *ucontrol)
2435{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002436 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2437 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2438 struct snd_soc_codec *codec = widget->codec;
Mark Brownd2b247a2009-10-06 15:21:04 +01002439 struct soc_enum *e =
2440 (struct soc_enum *)kcontrol->private_value;
2441 int change;
2442 int ret = 0;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002443 int wi;
Mark Brownd2b247a2009-10-06 15:21:04 +01002444
2445 if (ucontrol->value.enumerated.item[0] >= e->max)
2446 return -EINVAL;
2447
Stephen Warrenfafd2172011-04-28 17:38:00 -06002448 mutex_lock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002449
2450 change = widget->value != ucontrol->value.enumerated.item[0];
Stephen Warrenfafd2172011-04-28 17:38:00 -06002451 if (change) {
2452 for (wi = 0; wi < wlist->num_widgets; wi++) {
2453 widget = wlist->widgets[wi];
Mark Brownd2b247a2009-10-06 15:21:04 +01002454
Stephen Warrenfafd2172011-04-28 17:38:00 -06002455 widget->value = ucontrol->value.enumerated.item[0];
2456
2457 dapm_mux_update_power(widget, kcontrol, change,
2458 widget->value, e);
2459 }
2460 }
2461
2462 mutex_unlock(&codec->mutex);
Mark Brownd2b247a2009-10-06 15:21:04 +01002463 return ret;
2464}
2465EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2466
2467/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002468 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2469 * callback
2470 * @kcontrol: mixer control
2471 * @ucontrol: control element information
2472 *
2473 * Callback to get the value of a dapm semi enumerated double mixer control.
2474 *
2475 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2476 * used for handling bitfield coded enumeration for example.
2477 *
2478 * Returns 0 for success.
2479 */
2480int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2481 struct snd_ctl_elem_value *ucontrol)
2482{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002483 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2484 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
Peter Ujfalusi74155552009-01-08 13:34:29 +02002485 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002486 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002487
2488 reg_val = snd_soc_read(widget->codec, e->reg);
2489 val = (reg_val >> e->shift_l) & e->mask;
2490 for (mux = 0; mux < e->max; mux++) {
2491 if (val == e->values[mux])
2492 break;
2493 }
2494 ucontrol->value.enumerated.item[0] = mux;
2495 if (e->shift_l != e->shift_r) {
2496 val = (reg_val >> e->shift_r) & e->mask;
2497 for (mux = 0; mux < e->max; mux++) {
2498 if (val == e->values[mux])
2499 break;
2500 }
2501 ucontrol->value.enumerated.item[1] = mux;
2502 }
2503
2504 return 0;
2505}
2506EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2507
2508/**
2509 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2510 * callback
2511 * @kcontrol: mixer control
2512 * @ucontrol: control element information
2513 *
2514 * Callback to set the value of a dapm semi enumerated double mixer control.
2515 *
2516 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2517 * used for handling bitfield coded enumeration for example.
2518 *
2519 * Returns 0 for success.
2520 */
2521int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2522 struct snd_ctl_elem_value *ucontrol)
2523{
Stephen Warrenfafd2172011-04-28 17:38:00 -06002524 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2525 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2526 struct snd_soc_codec *codec = widget->codec;
Peter Ujfalusi74155552009-01-08 13:34:29 +02002527 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Mark Brown3a655772009-10-05 17:23:30 +01002528 unsigned int val, mux, change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002529 unsigned int mask;
Mark Brown97404f22010-12-14 16:13:57 +00002530 struct snd_soc_dapm_update update;
Stephen Warrenfafd2172011-04-28 17:38:00 -06002531 int wi;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002532
2533 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2534 return -EINVAL;
2535 mux = ucontrol->value.enumerated.item[0];
2536 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2537 mask = e->mask << e->shift_l;
2538 if (e->shift_l != e->shift_r) {
2539 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2540 return -EINVAL;
2541 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2542 mask |= e->mask << e->shift_r;
2543 }
2544
Stephen Warrenfafd2172011-04-28 17:38:00 -06002545 mutex_lock(&codec->mutex);
2546
Mark Brown3a655772009-10-05 17:23:30 +01002547 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
Stephen Warrenfafd2172011-04-28 17:38:00 -06002548 if (change) {
2549 for (wi = 0; wi < wlist->num_widgets; wi++) {
2550 widget = wlist->widgets[wi];
Mark Brown97404f22010-12-14 16:13:57 +00002551
Stephen Warrenfafd2172011-04-28 17:38:00 -06002552 widget->value = val;
Mark Brown97404f22010-12-14 16:13:57 +00002553
Stephen Warrenfafd2172011-04-28 17:38:00 -06002554 update.kcontrol = kcontrol;
2555 update.widget = widget;
2556 update.reg = e->reg;
2557 update.mask = mask;
2558 update.val = val;
2559 widget->dapm->update = &update;
Mark Brown1642e3d2009-10-05 16:24:26 +01002560
Stephen Warrenfafd2172011-04-28 17:38:00 -06002561 dapm_mux_update_power(widget, kcontrol, change, mux, e);
Mark Brown1642e3d2009-10-05 16:24:26 +01002562
Stephen Warrenfafd2172011-04-28 17:38:00 -06002563 widget->dapm->update = NULL;
2564 }
2565 }
2566
2567 mutex_unlock(&codec->mutex);
Mark Brown97404f22010-12-14 16:13:57 +00002568 return change;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002569}
2570EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2571
2572/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00002573 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2574 *
2575 * @kcontrol: mixer control
2576 * @uinfo: control element information
2577 *
2578 * Callback to provide information about a pin switch control.
2579 */
2580int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_info *uinfo)
2582{
2583 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2584 uinfo->count = 1;
2585 uinfo->value.integer.min = 0;
2586 uinfo->value.integer.max = 1;
2587
2588 return 0;
2589}
2590EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2591
2592/**
2593 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2594 *
2595 * @kcontrol: mixer control
2596 * @ucontrol: Value
2597 */
2598int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_value *ucontrol)
2600{
2601 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2602 const char *pin = (const char *)kcontrol->private_value;
2603
2604 mutex_lock(&codec->mutex);
2605
2606 ucontrol->value.integer.value[0] =
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002607 snd_soc_dapm_get_pin_status(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002608
2609 mutex_unlock(&codec->mutex);
2610
2611 return 0;
2612}
2613EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2614
2615/**
2616 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2617 *
2618 * @kcontrol: mixer control
2619 * @ucontrol: Value
2620 */
2621int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2622 struct snd_ctl_elem_value *ucontrol)
2623{
2624 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2625 const char *pin = (const char *)kcontrol->private_value;
2626
2627 mutex_lock(&codec->mutex);
2628
2629 if (ucontrol->value.integer.value[0])
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002630 snd_soc_dapm_enable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002631 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002632 snd_soc_dapm_disable_pin(&codec->dapm, pin);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002633
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002634 snd_soc_dapm_sync(&codec->dapm);
Mark Brown8b37dbd2009-02-28 21:14:20 +00002635
2636 mutex_unlock(&codec->mutex);
2637
2638 return 0;
2639}
2640EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2641
2642/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002643 * snd_soc_dapm_new_control - create new dapm control
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002644 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002645 * @widget: widget template
2646 *
2647 * Creates a new dapm control based upon the template.
2648 *
2649 * Returns 0 for success else error.
2650 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002651int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
Richard Purdie2b97eab2006-10-06 18:32:18 +02002652 const struct snd_soc_dapm_widget *widget)
2653{
2654 struct snd_soc_dapm_widget *w;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002655 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002656
2657 if ((w = dapm_cnew_widget(widget)) == NULL)
2658 return -ENOMEM;
2659
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002660 name_len = strlen(widget->name) + 1;
Mark Brown88e8b9a2011-03-02 18:18:24 +00002661 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002662 name_len += 1 + strlen(dapm->codec->name_prefix);
2663 w->name = kmalloc(name_len, GFP_KERNEL);
2664 if (w->name == NULL) {
2665 kfree(w);
2666 return -ENOMEM;
2667 }
Mark Brown88e8b9a2011-03-02 18:18:24 +00002668 if (dapm->codec && dapm->codec->name_prefix)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002669 snprintf(w->name, name_len, "%s %s",
2670 dapm->codec->name_prefix, widget->name);
2671 else
2672 snprintf(w->name, name_len, "%s", widget->name);
2673
Mark Brown7ca3a182011-10-08 14:04:50 +01002674 switch (w->id) {
2675 case snd_soc_dapm_switch:
2676 case snd_soc_dapm_mixer:
2677 case snd_soc_dapm_mixer_named_ctl:
2678 w->power_check = dapm_generic_check_power;
2679 break;
2680 case snd_soc_dapm_mux:
2681 case snd_soc_dapm_virt_mux:
2682 case snd_soc_dapm_value_mux:
2683 w->power_check = dapm_generic_check_power;
2684 break;
2685 case snd_soc_dapm_adc:
2686 case snd_soc_dapm_aif_out:
2687 w->power_check = dapm_adc_check_power;
2688 break;
2689 case snd_soc_dapm_dac:
2690 case snd_soc_dapm_aif_in:
2691 w->power_check = dapm_dac_check_power;
2692 break;
2693 case snd_soc_dapm_pga:
2694 case snd_soc_dapm_out_drv:
2695 case snd_soc_dapm_input:
2696 case snd_soc_dapm_output:
2697 case snd_soc_dapm_micbias:
2698 case snd_soc_dapm_spk:
2699 case snd_soc_dapm_hp:
2700 case snd_soc_dapm_mic:
2701 case snd_soc_dapm_line:
2702 w->power_check = dapm_generic_check_power;
2703 break;
2704 case snd_soc_dapm_supply:
2705 w->power_check = dapm_supply_check_power;
2706 break;
2707 default:
2708 w->power_check = dapm_always_on_check_power;
2709 break;
2710 }
2711
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002712 dapm->n_widgets++;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002713 w->dapm = dapm;
2714 w->codec = dapm->codec;
Liam Girdwoodb7950642011-07-04 22:10:52 +01002715 w->platform = dapm->platform;
Richard Purdie2b97eab2006-10-06 18:32:18 +02002716 INIT_LIST_HEAD(&w->sources);
2717 INIT_LIST_HEAD(&w->sinks);
2718 INIT_LIST_HEAD(&w->list);
Mark Browndb432b42011-10-03 21:06:40 +01002719 INIT_LIST_HEAD(&w->dirty);
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002720 list_add(&w->list, &dapm->card->widgets);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002721
2722 /* machine layer set ups unconnected pins and insertions */
2723 w->connected = 1;
2724 return 0;
2725}
2726EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2727
2728/**
Mark Brown4ba13272008-05-13 14:51:19 +02002729 * snd_soc_dapm_new_controls - create new dapm controls
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002730 * @dapm: DAPM context
Mark Brown4ba13272008-05-13 14:51:19 +02002731 * @widget: widget array
2732 * @num: number of widgets
2733 *
2734 * Creates new DAPM controls based upon the templates.
2735 *
2736 * Returns 0 for success else error.
2737 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002738int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
Mark Brown4ba13272008-05-13 14:51:19 +02002739 const struct snd_soc_dapm_widget *widget,
2740 int num)
2741{
2742 int i, ret;
2743
2744 for (i = 0; i < num; i++) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002745 ret = snd_soc_dapm_new_control(dapm, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00002746 if (ret < 0) {
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002747 dev_err(dapm->dev,
2748 "ASoC: Failed to create DAPM control %s: %d\n",
2749 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02002750 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00002751 }
Mark Brown4ba13272008-05-13 14:51:19 +02002752 widget++;
2753 }
2754 return 0;
2755}
2756EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2757
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002758static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002759 const char *stream, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002760{
2761 struct snd_soc_dapm_widget *w;
2762
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002763 list_for_each_entry(w, &dapm->card->widgets, list)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002764 {
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002765 if (!w->sname || w->dapm != dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002766 continue;
Liam Girdwoodee47b362011-07-25 11:15:50 +01002767 dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
Jarkko Nikulaf7d41ae2010-11-09 14:40:27 +02002768 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002769 if (strstr(w->sname, stream)) {
Mark Brown75c1f892011-10-04 22:28:08 +01002770 dapm_mark_dirty(w, "stream event");
Richard Purdie2b97eab2006-10-06 18:32:18 +02002771 switch(event) {
2772 case SND_SOC_DAPM_STREAM_START:
2773 w->active = 1;
2774 break;
2775 case SND_SOC_DAPM_STREAM_STOP:
2776 w->active = 0;
2777 break;
2778 case SND_SOC_DAPM_STREAM_SUSPEND:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002779 case SND_SOC_DAPM_STREAM_RESUME:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002780 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
Richard Purdie2b97eab2006-10-06 18:32:18 +02002781 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
2782 break;
2783 }
2784 }
2785 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02002786
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002787 dapm_power_widgets(dapm, event);
Liam Girdwood64a648c2011-07-25 11:15:15 +01002788
2789 /* do we need to notify any clients that DAPM stream is complete */
2790 if (dapm->stream_event)
2791 dapm->stream_event(dapm, event);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002792}
2793
2794/**
2795 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2796 * @rtd: PCM runtime data
2797 * @stream: stream name
2798 * @event: stream event
2799 *
2800 * Sends a stream event to the dapm core. The core then makes any
2801 * necessary widget power changes.
2802 *
2803 * Returns 0 for success else error.
2804 */
2805int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2806 const char *stream, int event)
2807{
2808 struct snd_soc_codec *codec = rtd->codec;
2809
2810 if (stream == NULL)
2811 return 0;
2812
2813 mutex_lock(&codec->mutex);
2814 soc_dapm_stream_event(&codec->dapm, stream, event);
Eero Nurkkala8e8b2d62009-10-12 08:41:59 +03002815 mutex_unlock(&codec->mutex);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002816 return 0;
2817}
Richard Purdie2b97eab2006-10-06 18:32:18 +02002818
2819/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002820 * snd_soc_dapm_enable_pin - enable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002821 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002822 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02002823 *
Mark Brown74b8f952009-06-06 11:26:15 +01002824 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01002825 * a valid audio route and active audio stream.
2826 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2827 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02002828 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002829int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002830{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002831 return snd_soc_dapm_set_pin(dapm, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002832}
Liam Girdwooda5302182008-07-07 13:35:17 +01002833EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002834
2835/**
Mark Brownda341832010-03-15 19:23:37 +00002836 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002837 * @dapm: DAPM context
Mark Brownda341832010-03-15 19:23:37 +00002838 * @pin: pin name
2839 *
2840 * Enables input/output pin regardless of any other state. This is
2841 * intended for use with microphone bias supplies used in microphone
2842 * jack detection.
2843 *
2844 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2845 * do any widget power switching.
2846 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002847int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2848 const char *pin)
Mark Brownda341832010-03-15 19:23:37 +00002849{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002850 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Mark Brownda341832010-03-15 19:23:37 +00002851
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002852 if (!w) {
2853 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2854 return -EINVAL;
Mark Brownda341832010-03-15 19:23:37 +00002855 }
2856
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002857 dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2858 w->connected = 1;
2859 w->force = 1;
Mark Brown75c1f892011-10-04 22:28:08 +01002860 dapm_mark_dirty(w, "force enable");
Mark Brown0d867332011-04-06 11:38:14 +09002861
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002862 return 0;
Mark Brownda341832010-03-15 19:23:37 +00002863}
2864EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2865
2866/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002867 * snd_soc_dapm_disable_pin - disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002868 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002869 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002870 *
Mark Brown74b8f952009-06-06 11:26:15 +01002871 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01002872 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2873 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002874 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002875int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2876 const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01002877{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002878 return snd_soc_dapm_set_pin(dapm, pin, 0);
Liam Girdwooda5302182008-07-07 13:35:17 +01002879}
2880EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2881
2882/**
Mark Brown5817b522008-09-24 11:23:11 +01002883 * snd_soc_dapm_nc_pin - permanently disable pin.
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002884 * @dapm: DAPM context
Mark Brown5817b522008-09-24 11:23:11 +01002885 * @pin: pin name
2886 *
2887 * Marks the specified pin as being not connected, disabling it along
2888 * any parent or child widgets. At present this is identical to
2889 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2890 * additional things such as disabling controls which only affect
2891 * paths through the pin.
2892 *
2893 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2894 * do any widget power switching.
2895 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002896int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01002897{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002898 return snd_soc_dapm_set_pin(dapm, pin, 0);
Mark Brown5817b522008-09-24 11:23:11 +01002899}
2900EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2901
2902/**
Liam Girdwooda5302182008-07-07 13:35:17 +01002903 * snd_soc_dapm_get_pin_status - get audio pin status
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002904 * @dapm: DAPM context
Liam Girdwooda5302182008-07-07 13:35:17 +01002905 * @pin: audio signal pin endpoint (or start point)
2906 *
2907 * Get audio pin status - connected or disconnected.
2908 *
2909 * Returns 1 for connected otherwise 0.
2910 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002911int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2912 const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002913{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002914 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002915
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002916 if (w)
2917 return w->connected;
Stephen Warrena68b38a2011-04-19 15:25:11 -06002918
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002919 return 0;
2920}
Liam Girdwooda5302182008-07-07 13:35:17 +01002921EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02002922
2923/**
Mark Brown1547aba2010-05-07 21:11:40 +01002924 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002925 * @dapm: DAPM context
Mark Brown1547aba2010-05-07 21:11:40 +01002926 * @pin: audio signal pin endpoint (or start point)
2927 *
2928 * Mark the given endpoint or pin as ignoring suspend. When the
2929 * system is disabled a path between two endpoints flagged as ignoring
2930 * suspend will not be disabled. The path must already be enabled via
2931 * normal means at suspend time, it will not be turned on if it was not
2932 * already enabled.
2933 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002934int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2935 const char *pin)
Mark Brown1547aba2010-05-07 21:11:40 +01002936{
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002937 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
Mark Brown1547aba2010-05-07 21:11:40 +01002938
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002939 if (!w) {
2940 dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2941 return -EINVAL;
Mark Brown1547aba2010-05-07 21:11:40 +01002942 }
2943
Lars-Peter Clausen91a5fca2011-04-27 18:34:31 +02002944 w->ignore_suspend = 1;
2945
2946 return 0;
Mark Brown1547aba2010-05-07 21:11:40 +01002947}
2948EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2949
2950/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02002951 * snd_soc_dapm_free - free dapm resources
Peter Ujfalusi728a5222011-08-26 16:33:52 +03002952 * @dapm: DAPM context
Richard Purdie2b97eab2006-10-06 18:32:18 +02002953 *
2954 * Free all dapm widgets and resources.
2955 */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002956void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
Richard Purdie2b97eab2006-10-06 18:32:18 +02002957{
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002958 snd_soc_dapm_sys_remove(dapm->dev);
Lars-Peter Clausen6c45e122011-04-30 19:45:50 +02002959 dapm_debugfs_cleanup(dapm);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002960 dapm_free_widgets(dapm);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02002961 list_del(&dapm->list);
Richard Purdie2b97eab2006-10-06 18:32:18 +02002962}
2963EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2964
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002965static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
Mark Brown51737472009-06-22 13:16:51 +01002966{
Mark Brown51737472009-06-22 13:16:51 +01002967 struct snd_soc_dapm_widget *w;
2968 LIST_HEAD(down_list);
2969 int powerdown = 0;
2970
Jarkko Nikula97c866d2010-12-14 12:18:31 +02002971 list_for_each_entry(w, &dapm->card->widgets, list) {
2972 if (w->dapm != dapm)
2973 continue;
Mark Brown51737472009-06-22 13:16:51 +01002974 if (w->power) {
Mark Brown828a8422011-01-15 13:14:30 +00002975 dapm_seq_insert(w, &down_list, false);
Mark Brownc2caa4d2009-06-26 15:36:56 +01002976 w->power = 0;
Mark Brown51737472009-06-22 13:16:51 +01002977 powerdown = 1;
2978 }
2979 }
2980
2981 /* If there were no widgets to power down we're already in
2982 * standby.
2983 */
2984 if (powerdown) {
Mark Browned5a4c42011-02-18 11:12:42 -08002985 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
Mark Brown828a8422011-01-15 13:14:30 +00002986 dapm_seq_run(dapm, &down_list, 0, false);
Mark Browned5a4c42011-02-18 11:12:42 -08002987 snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
Mark Brown51737472009-06-22 13:16:51 +01002988 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002989}
Mark Brown51737472009-06-22 13:16:51 +01002990
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002991/*
2992 * snd_soc_dapm_shutdown - callback for system shutdown
2993 */
2994void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2995{
2996 struct snd_soc_codec *codec;
2997
Liam Girdwoodce6120c2010-11-05 15:53:46 +02002998 list_for_each_entry(codec, &card->codec_dev_list, list) {
2999 soc_dapm_shutdown_codec(&codec->dapm);
Mark Browned5a4c42011-02-18 11:12:42 -08003000 snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003001 }
Mark Brown51737472009-06-22 13:16:51 +01003002}
3003
Richard Purdie2b97eab2006-10-06 18:32:18 +02003004/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003005MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02003006MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3007MODULE_LICENSE("GPL");