blob: 9187db18a0424dec5b2ac581676704b905d7948a [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>
35#include <linux/delay.h>
36#include <linux/pm.h>
37#include <linux/bitops.h>
38#include <linux/platform_device.h>
39#include <linux/jiffies.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
43#include <sound/soc-dapm.h>
44#include <sound/initval.h>
45
46/* debug */
Mark Brownc1286b82008-07-07 19:26:03 +010047#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +020048#define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020049#else
50#define dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020051#endif
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,
58 [snd_soc_dapm_mic] = 3,
59 [snd_soc_dapm_mux] = 4,
Mark Browne3d4dab2009-06-07 13:08:45 +010060 [snd_soc_dapm_value_mux] = 4,
61 [snd_soc_dapm_dac] = 5,
62 [snd_soc_dapm_mixer] = 6,
63 [snd_soc_dapm_mixer_named_ctl] = 6,
64 [snd_soc_dapm_pga] = 7,
65 [snd_soc_dapm_adc] = 8,
66 [snd_soc_dapm_hp] = 9,
67 [snd_soc_dapm_spk] = 10,
68 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020069};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000070
Richard Purdie2b97eab2006-10-06 18:32:18 +020071static int dapm_down_seq[] = {
Mark Brown38357ab2009-06-06 19:03:23 +010072 [snd_soc_dapm_pre] = 0,
73 [snd_soc_dapm_adc] = 1,
74 [snd_soc_dapm_hp] = 2,
75 [snd_soc_dapm_spk] = 3,
76 [snd_soc_dapm_pga] = 4,
77 [snd_soc_dapm_mixer_named_ctl] = 5,
Mark Browne3d4dab2009-06-07 13:08:45 +010078 [snd_soc_dapm_mixer] = 5,
79 [snd_soc_dapm_dac] = 6,
80 [snd_soc_dapm_mic] = 7,
81 [snd_soc_dapm_micbias] = 8,
82 [snd_soc_dapm_mux] = 9,
83 [snd_soc_dapm_value_mux] = 9,
84 [snd_soc_dapm_supply] = 10,
85 [snd_soc_dapm_post] = 11,
Richard Purdie2b97eab2006-10-06 18:32:18 +020086};
87
Troy Kisky12ef1932008-10-13 17:42:14 -070088static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010089{
90 if (pop_time)
91 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
92}
93
Troy Kisky12ef1932008-10-13 17:42:14 -070094static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010095{
96 va_list args;
97
98 va_start(args, fmt);
99
100 if (pop_time) {
101 vprintk(fmt, args);
Troy Kisky12ef1932008-10-13 17:42:14 -0700102 pop_wait(pop_time);
Mark Brown15e4c722008-07-02 11:51:20 +0100103 }
104
105 va_end(args);
106}
107
Richard Purdie2b97eab2006-10-06 18:32:18 +0200108/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +0100109static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +0200110 const struct snd_soc_dapm_widget *_widget)
111{
Takashi Iwai88cb4292007-02-05 14:56:20 +0100112 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200113}
114
Mark Brown452c5ea2009-05-17 21:41:23 +0100115/**
116 * snd_soc_dapm_set_bias_level - set the bias level for the system
117 * @socdev: audio device
118 * @level: level to configure
119 *
120 * Configure the bias (power) levels for the SoC audio device.
121 *
122 * Returns 0 for success else error.
123 */
124static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
125 enum snd_soc_bias_level level)
126{
127 struct snd_soc_card *card = socdev->card;
128 struct snd_soc_codec *codec = socdev->card->codec;
129 int ret = 0;
130
Mark Brownf83fba82009-05-18 15:44:43 +0100131 switch (level) {
132 case SND_SOC_BIAS_ON:
133 dev_dbg(socdev->dev, "Setting full bias\n");
134 break;
135 case SND_SOC_BIAS_PREPARE:
136 dev_dbg(socdev->dev, "Setting bias prepare\n");
137 break;
138 case SND_SOC_BIAS_STANDBY:
139 dev_dbg(socdev->dev, "Setting standby bias\n");
140 break;
141 case SND_SOC_BIAS_OFF:
142 dev_dbg(socdev->dev, "Setting bias off\n");
143 break;
144 default:
145 dev_err(socdev->dev, "Setting invalid bias %d\n", level);
146 return -EINVAL;
147 }
148
Mark Brown452c5ea2009-05-17 21:41:23 +0100149 if (card->set_bias_level)
150 ret = card->set_bias_level(card, level);
151 if (ret == 0 && codec->set_bias_level)
152 ret = codec->set_bias_level(codec, level);
153
154 return ret;
155}
156
Richard Purdie2b97eab2006-10-06 18:32:18 +0200157/* set up initial codec paths */
158static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
159 struct snd_soc_dapm_path *p, int i)
160{
161 switch (w->id) {
162 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000163 case snd_soc_dapm_mixer:
164 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200165 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100166 struct soc_mixer_control *mc = (struct soc_mixer_control *)
167 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400168 unsigned int reg = mc->reg;
169 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100170 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400171 unsigned int mask = (1 << fls(max)) - 1;
172 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200173
174 val = snd_soc_read(w->codec, reg);
175 val = (val >> shift) & mask;
176
177 if ((invert && !val) || (!invert && val))
178 p->connect = 1;
179 else
180 p->connect = 0;
181 }
182 break;
183 case snd_soc_dapm_mux: {
184 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
185 int val, item, bitmask;
186
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100187 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200188 ;
189 val = snd_soc_read(w->codec, e->reg);
190 item = (val >> e->shift_l) & (bitmask - 1);
191
192 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100193 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200194 if (!(strcmp(p->name, e->texts[i])) && item == i)
195 p->connect = 1;
196 }
197 }
198 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200199 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200200 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200201 w->kcontrols[i].private_value;
202 int val, item;
203
204 val = snd_soc_read(w->codec, e->reg);
205 val = (val >> e->shift_l) & e->mask;
206 for (item = 0; item < e->max; item++) {
207 if (val == e->values[item])
208 break;
209 }
210
211 p->connect = 0;
212 for (i = 0; i < e->max; i++) {
213 if (!(strcmp(p->name, e->texts[i])) && item == i)
214 p->connect = 1;
215 }
216 }
217 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200218 /* does not effect routing - always connected */
219 case snd_soc_dapm_pga:
220 case snd_soc_dapm_output:
221 case snd_soc_dapm_adc:
222 case snd_soc_dapm_input:
223 case snd_soc_dapm_dac:
224 case snd_soc_dapm_micbias:
225 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100226 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200227 p->connect = 1;
228 break;
229 /* does effect routing - dynamically connected */
230 case snd_soc_dapm_hp:
231 case snd_soc_dapm_mic:
232 case snd_soc_dapm_spk:
233 case snd_soc_dapm_line:
234 case snd_soc_dapm_pre:
235 case snd_soc_dapm_post:
236 p->connect = 0;
237 break;
238 }
239}
240
Mark Brown74b8f952009-06-06 11:26:15 +0100241/* connect mux widget to its interconnecting audio paths */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200242static int dapm_connect_mux(struct snd_soc_codec *codec,
243 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
244 struct snd_soc_dapm_path *path, const char *control_name,
245 const struct snd_kcontrol_new *kcontrol)
246{
247 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
248 int i;
249
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100250 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200251 if (!(strcmp(control_name, e->texts[i]))) {
252 list_add(&path->list, &codec->dapm_paths);
253 list_add(&path->list_sink, &dest->sources);
254 list_add(&path->list_source, &src->sinks);
255 path->name = (char*)e->texts[i];
256 dapm_set_path_status(dest, path, 0);
257 return 0;
258 }
259 }
260
261 return -ENODEV;
262}
263
Mark Brown74b8f952009-06-06 11:26:15 +0100264/* connect mixer widget to its interconnecting audio paths */
Richard Purdie2b97eab2006-10-06 18:32:18 +0200265static int dapm_connect_mixer(struct snd_soc_codec *codec,
266 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
267 struct snd_soc_dapm_path *path, const char *control_name)
268{
269 int i;
270
271 /* search for mixer kcontrol */
272 for (i = 0; i < dest->num_kcontrols; i++) {
273 if (!strcmp(control_name, dest->kcontrols[i].name)) {
274 list_add(&path->list, &codec->dapm_paths);
275 list_add(&path->list_sink, &dest->sources);
276 list_add(&path->list_source, &src->sinks);
277 path->name = dest->kcontrols[i].name;
278 dapm_set_path_status(dest, path, i);
279 return 0;
280 }
281 }
282 return -ENODEV;
283}
284
285/* update dapm codec register bits */
286static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
287{
288 int change, power;
289 unsigned short old, new;
290 struct snd_soc_codec *codec = widget->codec;
291
292 /* check for valid widgets */
293 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
294 widget->id == snd_soc_dapm_output ||
295 widget->id == snd_soc_dapm_hp ||
296 widget->id == snd_soc_dapm_mic ||
297 widget->id == snd_soc_dapm_line ||
298 widget->id == snd_soc_dapm_spk)
299 return 0;
300
301 power = widget->power;
302 if (widget->invert)
303 power = (power ? 0:1);
304
305 old = snd_soc_read(codec, widget->reg);
306 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
307
308 change = old != new;
309 if (change) {
Troy Kisky12ef1932008-10-13 17:42:14 -0700310 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
311 widget->name, widget->power ? "on" : "off",
312 codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200313 snd_soc_write(codec, widget->reg, new);
Troy Kisky12ef1932008-10-13 17:42:14 -0700314 pop_wait(codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200315 }
Mark Brownc1286b82008-07-07 19:26:03 +0100316 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
317 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200318 return change;
319}
320
321/* ramps the volume up or down to minimise pops before or after a
322 * DAPM power event */
323static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
324{
325 const struct snd_kcontrol_new *k = widget->kcontrols;
326
327 if (widget->muted && !power)
328 return 0;
329 if (!widget->muted && power)
330 return 0;
331
332 if (widget->num_kcontrols && k) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100333 struct soc_mixer_control *mc =
334 (struct soc_mixer_control *)k->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400335 unsigned int reg = mc->reg;
336 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100337 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400338 unsigned int mask = (1 << fls(max)) - 1;
339 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200340
341 if (power) {
342 int i;
343 /* power up has happended, increase volume to last level */
344 if (invert) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100345 for (i = max; i > widget->saved_value; i--)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200346 snd_soc_update_bits(widget->codec, reg, mask, i);
347 } else {
348 for (i = 0; i < widget->saved_value; i++)
349 snd_soc_update_bits(widget->codec, reg, mask, i);
350 }
351 widget->muted = 0;
352 } else {
353 /* power down is about to occur, decrease volume to mute */
354 int val = snd_soc_read(widget->codec, reg);
355 int i = widget->saved_value = (val >> shift) & mask;
356 if (invert) {
357 for (; i < mask; i++)
358 snd_soc_update_bits(widget->codec, reg, mask, i);
359 } else {
360 for (; i > 0; i--)
361 snd_soc_update_bits(widget->codec, reg, mask, i);
362 }
363 widget->muted = 1;
364 }
365 }
366 return 0;
367}
368
369/* create new dapm mixer control */
370static int dapm_new_mixer(struct snd_soc_codec *codec,
371 struct snd_soc_dapm_widget *w)
372{
373 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000374 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200375 struct snd_soc_dapm_path *path;
376
377 /* add kcontrol */
378 for (i = 0; i < w->num_kcontrols; i++) {
379
380 /* match name */
381 list_for_each_entry(path, &w->sources, list_sink) {
382
383 /* mixer/mux paths name must match control name */
384 if (path->name != (char*)w->kcontrols[i].name)
385 continue;
386
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000387 /* add dapm control with long name.
388 * for dapm_mixer this is the concatenation of the
389 * mixer and kcontrol name.
390 * for dapm_mixer_named_ctl this is simply the
391 * kcontrol name.
392 */
393 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000394 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000395 name_len += 1 + strlen(w->name);
396
Mark Brown219b93f2008-10-28 13:02:31 +0000397 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000398
Richard Purdie2b97eab2006-10-06 18:32:18 +0200399 if (path->long_name == NULL)
400 return -ENOMEM;
401
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000402 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000403 default:
404 snprintf(path->long_name, name_len, "%s %s",
405 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000406 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000407 case snd_soc_dapm_mixer_named_ctl:
408 snprintf(path->long_name, name_len, "%s",
409 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000410 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000411 }
412
Mark Brown219b93f2008-10-28 13:02:31 +0000413 path->long_name[name_len - 1] = '\0';
414
Richard Purdie2b97eab2006-10-06 18:32:18 +0200415 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
416 path->long_name);
417 ret = snd_ctl_add(codec->card, path->kcontrol);
418 if (ret < 0) {
Mark Brown6553e192009-04-06 16:59:32 +0100419 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
420 path->long_name,
421 ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200422 kfree(path->long_name);
423 path->long_name = NULL;
424 return ret;
425 }
426 }
427 }
428 return ret;
429}
430
431/* create new dapm mux control */
432static int dapm_new_mux(struct snd_soc_codec *codec,
433 struct snd_soc_dapm_widget *w)
434{
435 struct snd_soc_dapm_path *path = NULL;
436 struct snd_kcontrol *kcontrol;
437 int ret = 0;
438
439 if (!w->num_kcontrols) {
440 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
441 return -EINVAL;
442 }
443
444 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
445 ret = snd_ctl_add(codec->card, kcontrol);
446 if (ret < 0)
447 goto err;
448
449 list_for_each_entry(path, &w->sources, list_sink)
450 path->kcontrol = kcontrol;
451
452 return ret;
453
454err:
455 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
456 return ret;
457}
458
459/* create new dapm volume control */
460static int dapm_new_pga(struct snd_soc_codec *codec,
461 struct snd_soc_dapm_widget *w)
462{
463 struct snd_kcontrol *kcontrol;
464 int ret = 0;
465
466 if (!w->num_kcontrols)
467 return -EINVAL;
468
469 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
470 ret = snd_ctl_add(codec->card, kcontrol);
471 if (ret < 0) {
472 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
473 return ret;
474 }
475
476 return ret;
477}
478
479/* reset 'walked' bit for each dapm path */
480static inline void dapm_clear_walk(struct snd_soc_codec *codec)
481{
482 struct snd_soc_dapm_path *p;
483
484 list_for_each_entry(p, &codec->dapm_paths, list)
485 p->walked = 0;
486}
487
488/*
489 * Recursively check for a completed path to an active or physically connected
490 * output widget. Returns number of complete paths.
491 */
492static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
493{
494 struct snd_soc_dapm_path *path;
495 int con = 0;
496
Mark Brown246d0a12009-04-22 18:24:55 +0100497 if (widget->id == snd_soc_dapm_supply)
498 return 0;
499
Richard Purdie2b97eab2006-10-06 18:32:18 +0200500 if (widget->id == snd_soc_dapm_adc && widget->active)
501 return 1;
502
503 if (widget->connected) {
504 /* connected pin ? */
505 if (widget->id == snd_soc_dapm_output && !widget->ext)
506 return 1;
507
508 /* connected jack or spk ? */
509 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
510 widget->id == snd_soc_dapm_line)
511 return 1;
512 }
513
514 list_for_each_entry(path, &widget->sinks, list_source) {
515 if (path->walked)
516 continue;
517
518 if (path->sink && path->connect) {
519 path->walked = 1;
520 con += is_connected_output_ep(path->sink);
521 }
522 }
523
524 return con;
525}
526
527/*
528 * Recursively check for a completed path to an active or physically connected
529 * input widget. Returns number of complete paths.
530 */
531static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
532{
533 struct snd_soc_dapm_path *path;
534 int con = 0;
535
Mark Brown246d0a12009-04-22 18:24:55 +0100536 if (widget->id == snd_soc_dapm_supply)
537 return 0;
538
Richard Purdie2b97eab2006-10-06 18:32:18 +0200539 /* active stream ? */
540 if (widget->id == snd_soc_dapm_dac && widget->active)
541 return 1;
542
543 if (widget->connected) {
544 /* connected pin ? */
545 if (widget->id == snd_soc_dapm_input && !widget->ext)
546 return 1;
547
548 /* connected VMID/Bias for lower pops */
549 if (widget->id == snd_soc_dapm_vmid)
550 return 1;
551
552 /* connected jack ? */
553 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
554 return 1;
555 }
556
557 list_for_each_entry(path, &widget->sources, list_sink) {
558 if (path->walked)
559 continue;
560
561 if (path->source && path->connect) {
562 path->walked = 1;
563 con += is_connected_input_ep(path->source);
564 }
565 }
566
567 return con;
568}
569
570/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300571 * Handler for generic register modifier widget.
572 */
573int dapm_reg_event(struct snd_soc_dapm_widget *w,
574 struct snd_kcontrol *kcontrol, int event)
575{
576 unsigned int val;
577
578 if (SND_SOC_DAPM_EVENT_ON(event))
579 val = w->on_val;
580 else
581 val = w->off_val;
582
583 snd_soc_update_bits(w->codec, -(w->reg + 1),
584 w->mask << w->shift, val << w->shift);
585
586 return 0;
587}
Mark Brown11589412008-07-29 11:42:23 +0100588EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300589
Mark Brown025756e2009-04-13 11:09:18 +0100590/* Standard power change method, used to apply power changes to most
591 * widgets.
592 */
593static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
594{
595 int ret;
596
597 /* call any power change event handlers */
598 if (w->event)
599 pr_debug("power %s event for %s flags %x\n",
600 w->power ? "on" : "off",
601 w->name, w->event_flags);
602
603 /* power up pre event */
604 if (w->power && w->event &&
605 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
606 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
607 if (ret < 0)
608 return ret;
609 }
610
611 /* power down pre event */
612 if (!w->power && w->event &&
613 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
614 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
615 if (ret < 0)
616 return ret;
617 }
618
619 /* Lower PGA volume to reduce pops */
620 if (w->id == snd_soc_dapm_pga && !w->power)
621 dapm_set_pga(w, w->power);
622
623 dapm_update_bits(w);
624
625 /* Raise PGA volume to reduce pops */
626 if (w->id == snd_soc_dapm_pga && w->power)
627 dapm_set_pga(w, w->power);
628
629 /* power up post event */
630 if (w->power && w->event &&
631 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
632 ret = w->event(w,
633 NULL, SND_SOC_DAPM_POST_PMU);
634 if (ret < 0)
635 return ret;
636 }
637
638 /* power down post event */
639 if (!w->power && w->event &&
640 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
641 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
642 if (ret < 0)
643 return ret;
644 }
645
646 return 0;
647}
648
Mark Browncd0f2d42009-04-20 16:56:59 +0100649/* Generic check to see if a widget should be powered.
650 */
651static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
652{
653 int in, out;
654
655 in = is_connected_input_ep(w);
656 dapm_clear_walk(w->codec);
657 out = is_connected_output_ep(w);
658 dapm_clear_walk(w->codec);
659 return out != 0 && in != 0;
660}
661
Mark Brown6ea31b92009-04-20 17:15:41 +0100662/* Check to see if an ADC has power */
663static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
664{
665 int in;
666
667 if (w->active) {
668 in = is_connected_input_ep(w);
669 dapm_clear_walk(w->codec);
670 return in != 0;
671 } else {
672 return dapm_generic_check_power(w);
673 }
674}
675
676/* Check to see if a DAC has power */
677static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
678{
679 int out;
680
681 if (w->active) {
682 out = is_connected_output_ep(w);
683 dapm_clear_walk(w->codec);
684 return out != 0;
685 } else {
686 return dapm_generic_check_power(w);
687 }
688}
689
Mark Brown246d0a12009-04-22 18:24:55 +0100690/* Check to see if a power supply is needed */
691static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
692{
693 struct snd_soc_dapm_path *path;
694 int power = 0;
695
696 /* Check if one of our outputs is connected */
697 list_for_each_entry(path, &w->sinks, list_source) {
698 if (path->sink && path->sink->power_check &&
699 path->sink->power_check(path->sink)) {
700 power = 1;
701 break;
702 }
703 }
704
705 dapm_clear_walk(w->codec);
706
707 return power;
708}
709
Mark Brown38357ab2009-06-06 19:03:23 +0100710static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
711 struct snd_soc_dapm_widget *b,
712 int sort[])
713{
714 if (sort[a->id] != sort[b->id])
715 return sort[a->id] - sort[b->id];
Mark Brownb22ead22009-06-07 12:51:26 +0100716 if (a->reg != b->reg)
717 return a->reg - b->reg;
Mark Brown38357ab2009-06-06 19:03:23 +0100718
719 return 0;
720}
721
722/* Insert a widget in order into a DAPM power sequence. */
723static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
724 struct list_head *list,
725 int sort[])
726{
727 struct snd_soc_dapm_widget *w;
728
729 list_for_each_entry(w, list, power_list)
730 if (dapm_seq_compare(new_widget, w, sort) < 0) {
731 list_add_tail(&new_widget->power_list, &w->power_list);
732 return;
733 }
734
735 list_add_tail(&new_widget->power_list, list);
736}
737
Mark Brownb22ead22009-06-07 12:51:26 +0100738/* Apply the coalesced changes from a DAPM sequence */
739static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
740 struct list_head *pending)
Mark Brown163cac02009-06-07 10:12:52 +0100741{
742 struct snd_soc_dapm_widget *w;
Mark Brownb22ead22009-06-07 12:51:26 +0100743 int reg, power;
744 unsigned int value = 0;
745 unsigned int mask = 0;
746 unsigned int cur_mask;
747
748 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
749 power_list)->reg;
750
751 list_for_each_entry(w, pending, power_list) {
752 cur_mask = 1 << w->shift;
753 BUG_ON(reg != w->reg);
754
755 if (w->invert)
756 power = !w->power;
757 else
758 power = w->power;
759
760 mask |= cur_mask;
761 if (power)
762 value |= cur_mask;
763
764 pop_dbg(codec->pop_time,
765 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
766 w->name, reg, value, mask);
767 }
768
769 pop_dbg(codec->pop_time,
770 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
771 value, mask, reg, codec->pop_time);
772 pop_wait(codec->pop_time);
773 snd_soc_update_bits(codec, reg, mask, value);
774}
775
776/* Apply a DAPM power sequence.
777 *
778 * We walk over a pre-sorted list of widgets to apply power to. In
779 * order to minimise the number of writes to the device required
780 * multiple widgets will be updated in a single write where possible.
781 * Currently anything that requires more than a single write is not
782 * handled.
783 */
784static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
785 int event, int sort[])
786{
787 struct snd_soc_dapm_widget *w, *n;
788 LIST_HEAD(pending);
789 int cur_sort = -1;
790 int cur_reg = SND_SOC_NOPM;
Mark Brown163cac02009-06-07 10:12:52 +0100791 int ret;
792
Mark Brownb22ead22009-06-07 12:51:26 +0100793 list_for_each_entry_safe(w, n, list, power_list) {
794 ret = 0;
795
796 /* Do we need to apply any queued changes? */
797 if (sort[w->id] != cur_sort || w->reg != cur_reg) {
798 if (!list_empty(&pending))
799 dapm_seq_run_coalesced(codec, &pending);
800
801 INIT_LIST_HEAD(&pending);
802 cur_sort = -1;
803 cur_reg = SND_SOC_NOPM;
804 }
805
Mark Brown163cac02009-06-07 10:12:52 +0100806 switch (w->id) {
807 case snd_soc_dapm_pre:
808 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100809 list_for_each_entry_safe_continue(w, n, list,
810 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100811
Mark Brownb22ead22009-06-07 12:51:26 +0100812 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100813 ret = w->event(w,
814 NULL, SND_SOC_DAPM_PRE_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100815 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100816 ret = w->event(w,
817 NULL, SND_SOC_DAPM_PRE_PMD);
Mark Brown163cac02009-06-07 10:12:52 +0100818 break;
819
820 case snd_soc_dapm_post:
821 if (!w->event)
Mark Brownb22ead22009-06-07 12:51:26 +0100822 list_for_each_entry_safe_continue(w, n, list,
823 power_list);
Mark Brown163cac02009-06-07 10:12:52 +0100824
Mark Brownb22ead22009-06-07 12:51:26 +0100825 if (event == SND_SOC_DAPM_STREAM_START)
Mark Brown163cac02009-06-07 10:12:52 +0100826 ret = w->event(w,
827 NULL, SND_SOC_DAPM_POST_PMU);
Mark Brownb22ead22009-06-07 12:51:26 +0100828 else if (event == SND_SOC_DAPM_STREAM_STOP)
Mark Brown163cac02009-06-07 10:12:52 +0100829 ret = w->event(w,
830 NULL, SND_SOC_DAPM_POST_PMD);
Mark Brownb22ead22009-06-07 12:51:26 +0100831 break;
832
833 case snd_soc_dapm_input:
834 case snd_soc_dapm_output:
835 case snd_soc_dapm_hp:
836 case snd_soc_dapm_mic:
837 case snd_soc_dapm_line:
838 case snd_soc_dapm_spk:
839 /* No register support currently */
840 case snd_soc_dapm_pga:
841 /* Don't coalsece these yet due to gain ramping */
842 ret = dapm_generic_apply_power(w);
Mark Brown163cac02009-06-07 10:12:52 +0100843 break;
844
845 default:
Mark Brownb22ead22009-06-07 12:51:26 +0100846 /* If there's an event or an invalid register
847 * then run immediately, otherwise store the
848 * updates so that we can coalesce. */
849 if (w->reg >= 0 && !w->event) {
850 cur_sort = sort[w->id];
851 cur_reg = w->reg;
852 list_move(&w->power_list, &pending);
853 } else {
854 ret = dapm_generic_apply_power(w);
855 }
Mark Brown163cac02009-06-07 10:12:52 +0100856 }
Mark Brownb22ead22009-06-07 12:51:26 +0100857
858 if (ret < 0)
859 pr_err("Failed to apply widget power: %d\n",
860 ret);
Mark Brown163cac02009-06-07 10:12:52 +0100861 }
Mark Brownb22ead22009-06-07 12:51:26 +0100862
863 if (!list_empty(&pending))
864 dapm_seq_run_coalesced(codec, &pending);
Mark Brown163cac02009-06-07 10:12:52 +0100865}
866
Mark Brown42aa3412009-03-01 19:21:10 +0000867/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200868 * Scan each dapm widget for complete audio path.
869 * A complete path is a route that has valid endpoints i.e.:-
870 *
871 * o DAC to output pin.
872 * o Input Pin to ADC.
873 * o Input pin to Output pin (bypass, sidetone)
874 * o DAC to ADC (loopback).
875 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100876static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200877{
Mark Brown452c5ea2009-05-17 21:41:23 +0100878 struct snd_soc_device *socdev = codec->socdev;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200879 struct snd_soc_dapm_widget *w;
Mark Brown6d3ddc82009-05-16 17:47:29 +0100880 int ret = 0;
Mark Brown38357ab2009-06-06 19:03:23 +0100881 int power;
Mark Brown452c5ea2009-05-17 21:41:23 +0100882 int sys_power = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200883
Mark Brown6d3ddc82009-05-16 17:47:29 +0100884 INIT_LIST_HEAD(&codec->up_list);
885 INIT_LIST_HEAD(&codec->down_list);
886
887 /* Check which widgets we need to power and store them in
888 * lists indicating if they should be powered up or down.
889 */
890 list_for_each_entry(w, &codec->dapm_widgets, list) {
891 switch (w->id) {
892 case snd_soc_dapm_pre:
Mark Brown38357ab2009-06-06 19:03:23 +0100893 dapm_seq_insert(w, &codec->down_list, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100894 break;
895 case snd_soc_dapm_post:
Mark Brown38357ab2009-06-06 19:03:23 +0100896 dapm_seq_insert(w, &codec->up_list, dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100897 break;
898
899 default:
900 if (!w->power_check)
901 continue;
902
903 power = w->power_check(w);
Mark Brown452c5ea2009-05-17 21:41:23 +0100904 if (power)
905 sys_power = 1;
906
Mark Brown6d3ddc82009-05-16 17:47:29 +0100907 if (w->power == power)
908 continue;
909
910 if (power)
Mark Brown38357ab2009-06-06 19:03:23 +0100911 dapm_seq_insert(w, &codec->up_list,
912 dapm_up_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100913 else
Mark Brown38357ab2009-06-06 19:03:23 +0100914 dapm_seq_insert(w, &codec->down_list,
915 dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100916
917 w->power = power;
918 break;
919 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200920 }
921
Mark Brown452c5ea2009-05-17 21:41:23 +0100922 /* If we're changing to all on or all off then prepare */
923 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
924 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
925 ret = snd_soc_dapm_set_bias_level(socdev,
926 SND_SOC_BIAS_PREPARE);
927 if (ret != 0)
928 pr_err("Failed to prepare bias: %d\n", ret);
929 }
930
Mark Brown6d3ddc82009-05-16 17:47:29 +0100931 /* Power down widgets first; try to avoid amplifying pops. */
Mark Brownb22ead22009-06-07 12:51:26 +0100932 dapm_seq_run(codec, &codec->down_list, event, dapm_down_seq);
Mark Brown6d3ddc82009-05-16 17:47:29 +0100933
934 /* Now power up. */
Mark Brownb22ead22009-06-07 12:51:26 +0100935 dapm_seq_run(codec, &codec->up_list, event, dapm_up_seq);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200936
Mark Brown452c5ea2009-05-17 21:41:23 +0100937 /* If we just powered the last thing off drop to standby bias */
938 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
939 ret = snd_soc_dapm_set_bias_level(socdev,
940 SND_SOC_BIAS_STANDBY);
941 if (ret != 0)
942 pr_err("Failed to apply standby bias: %d\n", ret);
943 }
944
945 /* If we just powered up then move to active bias */
946 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
947 ret = snd_soc_dapm_set_bias_level(socdev,
948 SND_SOC_BIAS_ON);
949 if (ret != 0)
950 pr_err("Failed to apply active bias: %d\n", ret);
951 }
952
Mark Brown42aa3412009-03-01 19:21:10 +0000953 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200954}
955
Mark Brownc1286b82008-07-07 19:26:03 +0100956#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +0200957static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
958{
959 struct snd_soc_dapm_widget *w;
960 struct snd_soc_dapm_path *p = NULL;
961 int in, out;
962
963 printk("DAPM %s %s\n", codec->name, action);
964
965 list_for_each_entry(w, &codec->dapm_widgets, list) {
966
967 /* only display widgets that effect routing */
968 switch (w->id) {
969 case snd_soc_dapm_pre:
970 case snd_soc_dapm_post:
971 case snd_soc_dapm_vmid:
972 continue;
973 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200974 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200975 case snd_soc_dapm_output:
976 case snd_soc_dapm_input:
977 case snd_soc_dapm_switch:
978 case snd_soc_dapm_hp:
979 case snd_soc_dapm_mic:
980 case snd_soc_dapm_spk:
981 case snd_soc_dapm_line:
982 case snd_soc_dapm_micbias:
983 case snd_soc_dapm_dac:
984 case snd_soc_dapm_adc:
985 case snd_soc_dapm_pga:
986 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000987 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +0100988 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200989 if (w->name) {
990 in = is_connected_input_ep(w);
991 dapm_clear_walk(w->codec);
992 out = is_connected_output_ep(w);
993 dapm_clear_walk(w->codec);
994 printk("%s: %s in %d out %d\n", w->name,
995 w->power ? "On":"Off",in, out);
996
997 list_for_each_entry(p, &w->sources, list_sink) {
998 if (p->connect)
999 printk(" in %s %s\n", p->name ? p->name : "static",
1000 p->source->name);
1001 }
1002 list_for_each_entry(p, &w->sinks, list_source) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001003 if (p->connect)
1004 printk(" out %s %s\n", p->name ? p->name : "static",
1005 p->sink->name);
1006 }
1007 }
1008 break;
1009 }
1010 }
1011}
1012#endif
1013
1014/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001015static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1016 struct snd_kcontrol *kcontrol, int mask,
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001017 int mux, int val, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001018{
1019 struct snd_soc_dapm_path *path;
1020 int found = 0;
1021
Peter Ujfalusieff317d2009-01-15 14:40:47 +02001022 if (widget->id != snd_soc_dapm_mux &&
1023 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001024 return -ENODEV;
1025
1026 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
1027 return 0;
1028
1029 /* find dapm widget path assoc with kcontrol */
1030 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1031 if (path->kcontrol != kcontrol)
1032 continue;
1033
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001034 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +02001035 continue;
1036
1037 found = 1;
1038 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001039 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +02001040 path->connect = 1; /* new connection */
1041 else
1042 path->connect = 0; /* old connection must be powered down */
1043 }
1044
Mark Brown3fccd8b2008-07-07 19:26:04 +01001045 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001046 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +01001047 dump_dapm(widget->codec, "mux power update");
1048 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001049
1050 return 0;
1051}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001052
Milan plzik1b075e32008-01-10 14:39:46 +01001053/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001054static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1055 struct snd_kcontrol *kcontrol, int reg,
1056 int val_mask, int val, int invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001057{
1058 struct snd_soc_dapm_path *path;
1059 int found = 0;
1060
Milan plzik1b075e32008-01-10 14:39:46 +01001061 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001062 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +01001063 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001064 return -ENODEV;
1065
1066 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
1067 return 0;
1068
1069 /* find dapm widget path assoc with kcontrol */
1070 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
1071 if (path->kcontrol != kcontrol)
1072 continue;
1073
1074 /* found, now check type */
1075 found = 1;
1076 if (val)
1077 /* new connection */
1078 path->connect = invert ? 0:1;
1079 else
1080 /* old connection must be powered down */
1081 path->connect = invert ? 1:0;
1082 break;
1083 }
1084
Mark Brown3fccd8b2008-07-07 19:26:04 +01001085 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +02001086 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +01001087 dump_dapm(widget->codec, "mixer power update");
1088 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001089
1090 return 0;
1091}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001092
1093/* show dapm widget status in sys fs */
1094static ssize_t dapm_widget_show(struct device *dev,
1095 struct device_attribute *attr, char *buf)
1096{
1097 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +00001098 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001099 struct snd_soc_dapm_widget *w;
1100 int count = 0;
1101 char *state = "not set";
1102
1103 list_for_each_entry(w, &codec->dapm_widgets, list) {
1104
1105 /* only display widgets that burnm power */
1106 switch (w->id) {
1107 case snd_soc_dapm_hp:
1108 case snd_soc_dapm_mic:
1109 case snd_soc_dapm_spk:
1110 case snd_soc_dapm_line:
1111 case snd_soc_dapm_micbias:
1112 case snd_soc_dapm_dac:
1113 case snd_soc_dapm_adc:
1114 case snd_soc_dapm_pga:
1115 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001116 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +01001117 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001118 if (w->name)
1119 count += sprintf(buf + count, "%s: %s\n",
1120 w->name, w->power ? "On":"Off");
1121 break;
1122 default:
1123 break;
1124 }
1125 }
1126
Mark Brown0be98982008-05-19 12:31:28 +02001127 switch (codec->bias_level) {
1128 case SND_SOC_BIAS_ON:
1129 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001130 break;
Mark Brown0be98982008-05-19 12:31:28 +02001131 case SND_SOC_BIAS_PREPARE:
1132 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001133 break;
Mark Brown0be98982008-05-19 12:31:28 +02001134 case SND_SOC_BIAS_STANDBY:
1135 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001136 break;
Mark Brown0be98982008-05-19 12:31:28 +02001137 case SND_SOC_BIAS_OFF:
1138 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001139 break;
1140 }
1141 count += sprintf(buf + count, "PM State: %s\n", state);
1142
1143 return count;
1144}
1145
1146static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1147
1148int snd_soc_dapm_sys_add(struct device *dev)
1149{
Troy Kisky12ef1932008-10-13 17:42:14 -07001150 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001151}
1152
1153static void snd_soc_dapm_sys_remove(struct device *dev)
1154{
Mark Brownaef90842009-05-16 17:53:16 +01001155 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001156}
1157
1158/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001159static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001160{
1161 struct snd_soc_dapm_widget *w, *next_w;
1162 struct snd_soc_dapm_path *p, *next_p;
1163
1164 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1165 list_del(&w->list);
1166 kfree(w);
1167 }
1168
1169 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1170 list_del(&p->list);
1171 kfree(p->long_name);
1172 kfree(p);
1173 }
1174}
1175
Liam Girdwooda5302182008-07-07 13:35:17 +01001176static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +00001177 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001178{
1179 struct snd_soc_dapm_widget *w;
1180
1181 list_for_each_entry(w, &codec->dapm_widgets, list) {
1182 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +01001183 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001184 w->connected = status;
1185 return 0;
1186 }
1187 }
1188
Mark Brownc1286b82008-07-07 19:26:03 +01001189 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001190 return -EINVAL;
1191}
1192
Richard Purdie2b97eab2006-10-06 18:32:18 +02001193/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001194 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +02001195 * @codec: audio codec
1196 *
1197 * Walks all dapm audio paths and powers widgets according to their
1198 * stream or path usage.
1199 *
1200 * Returns 0 for success.
1201 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001202int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001203{
Mark Brown3fccd8b2008-07-07 19:26:04 +01001204 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1205 dump_dapm(codec, "sync");
1206 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001207}
Liam Girdwooda5302182008-07-07 13:35:17 +01001208EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001209
Mark Brown105f1c22008-05-13 14:52:19 +02001210static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
1211 const char *sink, const char *control, const char *source)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001212{
1213 struct snd_soc_dapm_path *path;
1214 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1215 int ret = 0;
1216
1217 /* find src and dest widgets */
1218 list_for_each_entry(w, &codec->dapm_widgets, list) {
1219
1220 if (!wsink && !(strcmp(w->name, sink))) {
1221 wsink = w;
1222 continue;
1223 }
1224 if (!wsource && !(strcmp(w->name, source))) {
1225 wsource = w;
1226 }
1227 }
1228
1229 if (wsource == NULL || wsink == NULL)
1230 return -ENODEV;
1231
1232 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1233 if (!path)
1234 return -ENOMEM;
1235
1236 path->source = wsource;
1237 path->sink = wsink;
1238 INIT_LIST_HEAD(&path->list);
1239 INIT_LIST_HEAD(&path->list_source);
1240 INIT_LIST_HEAD(&path->list_sink);
1241
1242 /* check for external widgets */
1243 if (wsink->id == snd_soc_dapm_input) {
1244 if (wsource->id == snd_soc_dapm_micbias ||
1245 wsource->id == snd_soc_dapm_mic ||
Seth Forshee1e392212007-04-16 15:36:42 +02001246 wsink->id == snd_soc_dapm_line ||
1247 wsink->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001248 wsink->ext = 1;
1249 }
1250 if (wsource->id == snd_soc_dapm_output) {
1251 if (wsink->id == snd_soc_dapm_spk ||
1252 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001253 wsink->id == snd_soc_dapm_line ||
1254 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001255 wsource->ext = 1;
1256 }
1257
1258 /* connect static paths */
1259 if (control == NULL) {
1260 list_add(&path->list, &codec->dapm_paths);
1261 list_add(&path->list_sink, &wsink->sources);
1262 list_add(&path->list_source, &wsource->sinks);
1263 path->connect = 1;
1264 return 0;
1265 }
1266
1267 /* connect dynamic paths */
1268 switch(wsink->id) {
1269 case snd_soc_dapm_adc:
1270 case snd_soc_dapm_dac:
1271 case snd_soc_dapm_pga:
1272 case snd_soc_dapm_input:
1273 case snd_soc_dapm_output:
1274 case snd_soc_dapm_micbias:
1275 case snd_soc_dapm_vmid:
1276 case snd_soc_dapm_pre:
1277 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001278 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001279 list_add(&path->list, &codec->dapm_paths);
1280 list_add(&path->list_sink, &wsink->sources);
1281 list_add(&path->list_source, &wsource->sinks);
1282 path->connect = 1;
1283 return 0;
1284 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001285 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001286 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1287 &wsink->kcontrols[0]);
1288 if (ret != 0)
1289 goto err;
1290 break;
1291 case snd_soc_dapm_switch:
1292 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001293 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001294 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1295 if (ret != 0)
1296 goto err;
1297 break;
1298 case snd_soc_dapm_hp:
1299 case snd_soc_dapm_mic:
1300 case snd_soc_dapm_line:
1301 case snd_soc_dapm_spk:
1302 list_add(&path->list, &codec->dapm_paths);
1303 list_add(&path->list_sink, &wsink->sources);
1304 list_add(&path->list_source, &wsource->sinks);
1305 path->connect = 0;
1306 return 0;
1307 }
1308 return 0;
1309
1310err:
1311 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1312 control, sink);
1313 kfree(path);
1314 return ret;
1315}
Mark Brown105f1c22008-05-13 14:52:19 +02001316
1317/**
Mark Brown105f1c22008-05-13 14:52:19 +02001318 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1319 * @codec: codec
1320 * @route: audio routes
1321 * @num: number of routes
1322 *
1323 * Connects 2 dapm widgets together via a named audio path. The sink is
1324 * the widget receiving the audio signal, whilst the source is the sender
1325 * of the audio signal.
1326 *
1327 * Returns 0 for success else error. On error all resources can be freed
1328 * with a call to snd_soc_card_free().
1329 */
1330int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1331 const struct snd_soc_dapm_route *route, int num)
1332{
1333 int i, ret;
1334
1335 for (i = 0; i < num; i++) {
1336 ret = snd_soc_dapm_add_route(codec, route->sink,
1337 route->control, route->source);
1338 if (ret < 0) {
1339 printk(KERN_ERR "Failed to add route %s->%s\n",
1340 route->source,
1341 route->sink);
1342 return ret;
1343 }
1344 route++;
1345 }
1346
1347 return 0;
1348}
1349EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1350
1351/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001352 * snd_soc_dapm_new_widgets - add new dapm widgets
1353 * @codec: audio codec
1354 *
1355 * Checks the codec for any new dapm widgets and creates them if found.
1356 *
1357 * Returns 0 for success.
1358 */
1359int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1360{
1361 struct snd_soc_dapm_widget *w;
1362
Richard Purdie2b97eab2006-10-06 18:32:18 +02001363 list_for_each_entry(w, &codec->dapm_widgets, list)
1364 {
1365 if (w->new)
1366 continue;
1367
1368 switch(w->id) {
1369 case snd_soc_dapm_switch:
1370 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001371 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001372 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001373 dapm_new_mixer(codec, w);
1374 break;
1375 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001376 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001377 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001378 dapm_new_mux(codec, w);
1379 break;
1380 case snd_soc_dapm_adc:
Mark Brownb75576d2009-04-20 17:56:13 +01001381 w->power_check = dapm_adc_check_power;
1382 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001383 case snd_soc_dapm_dac:
Mark Brownb75576d2009-04-20 17:56:13 +01001384 w->power_check = dapm_dac_check_power;
1385 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001386 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001387 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001388 dapm_new_pga(codec, w);
1389 break;
1390 case snd_soc_dapm_input:
1391 case snd_soc_dapm_output:
1392 case snd_soc_dapm_micbias:
1393 case snd_soc_dapm_spk:
1394 case snd_soc_dapm_hp:
1395 case snd_soc_dapm_mic:
1396 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001397 w->power_check = dapm_generic_check_power;
1398 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001399 case snd_soc_dapm_supply:
1400 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001401 case snd_soc_dapm_vmid:
1402 case snd_soc_dapm_pre:
1403 case snd_soc_dapm_post:
1404 break;
1405 }
1406 w->new = 1;
1407 }
1408
1409 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001410 return 0;
1411}
1412EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1413
1414/**
1415 * snd_soc_dapm_get_volsw - dapm mixer get callback
1416 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001417 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001418 *
1419 * Callback to get the value of a dapm mixer control.
1420 *
1421 * Returns 0 for success.
1422 */
1423int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1424 struct snd_ctl_elem_value *ucontrol)
1425{
1426 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001427 struct soc_mixer_control *mc =
1428 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001429 unsigned int reg = mc->reg;
1430 unsigned int shift = mc->shift;
1431 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001432 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001433 unsigned int invert = mc->invert;
1434 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001435
1436 /* return the saved value if we are powered down */
1437 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1438 ucontrol->value.integer.value[0] = widget->saved_value;
1439 return 0;
1440 }
1441
1442 ucontrol->value.integer.value[0] =
1443 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1444 if (shift != rshift)
1445 ucontrol->value.integer.value[1] =
1446 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1447 if (invert) {
1448 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001449 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001450 if (shift != rshift)
1451 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001452 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001453 }
1454
1455 return 0;
1456}
1457EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1458
1459/**
1460 * snd_soc_dapm_put_volsw - dapm mixer set callback
1461 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001462 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001463 *
1464 * Callback to set the value of a dapm mixer control.
1465 *
1466 * Returns 0 for success.
1467 */
1468int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1469 struct snd_ctl_elem_value *ucontrol)
1470{
1471 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001472 struct soc_mixer_control *mc =
1473 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001474 unsigned int reg = mc->reg;
1475 unsigned int shift = mc->shift;
1476 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001477 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001478 unsigned int mask = (1 << fls(max)) - 1;
1479 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001480 unsigned short val, val2, val_mask;
1481 int ret;
1482
1483 val = (ucontrol->value.integer.value[0] & mask);
1484
1485 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001486 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001487 val_mask = mask << shift;
1488 val = val << shift;
1489 if (shift != rshift) {
1490 val2 = (ucontrol->value.integer.value[1] & mask);
1491 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001492 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001493 val_mask |= mask << rshift;
1494 val |= val2 << rshift;
1495 }
1496
1497 mutex_lock(&widget->codec->mutex);
1498 widget->value = val;
1499
1500 /* save volume value if the widget is powered down */
1501 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1502 widget->saved_value = val;
1503 mutex_unlock(&widget->codec->mutex);
1504 return 1;
1505 }
1506
1507 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1508 if (widget->event) {
1509 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001510 ret = widget->event(widget, kcontrol,
1511 SND_SOC_DAPM_PRE_REG);
1512 if (ret < 0) {
1513 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001514 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001515 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001516 }
1517 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1518 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001519 ret = widget->event(widget, kcontrol,
1520 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001521 } else
1522 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1523
1524out:
1525 mutex_unlock(&widget->codec->mutex);
1526 return ret;
1527}
1528EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1529
1530/**
1531 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1532 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001533 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001534 *
1535 * Callback to get the value of a dapm enumerated double mixer control.
1536 *
1537 * Returns 0 for success.
1538 */
1539int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1540 struct snd_ctl_elem_value *ucontrol)
1541{
1542 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1543 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1544 unsigned short val, bitmask;
1545
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001546 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001547 ;
1548 val = snd_soc_read(widget->codec, e->reg);
1549 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1550 if (e->shift_l != e->shift_r)
1551 ucontrol->value.enumerated.item[1] =
1552 (val >> e->shift_r) & (bitmask - 1);
1553
1554 return 0;
1555}
1556EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1557
1558/**
1559 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1560 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001561 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001562 *
1563 * Callback to set the value of a dapm enumerated double mixer control.
1564 *
1565 * Returns 0 for success.
1566 */
1567int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1568 struct snd_ctl_elem_value *ucontrol)
1569{
1570 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1571 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1572 unsigned short val, mux;
1573 unsigned short mask, bitmask;
1574 int ret = 0;
1575
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001576 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001577 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001578 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001579 return -EINVAL;
1580 mux = ucontrol->value.enumerated.item[0];
1581 val = mux << e->shift_l;
1582 mask = (bitmask - 1) << e->shift_l;
1583 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001584 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001585 return -EINVAL;
1586 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1587 mask |= (bitmask - 1) << e->shift_r;
1588 }
1589
1590 mutex_lock(&widget->codec->mutex);
1591 widget->value = val;
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001592 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001593 if (widget->event) {
1594 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001595 ret = widget->event(widget,
1596 kcontrol, SND_SOC_DAPM_PRE_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001597 if (ret < 0)
1598 goto out;
1599 }
1600 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1601 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001602 ret = widget->event(widget,
1603 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001604 } else
1605 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1606
1607out:
1608 mutex_unlock(&widget->codec->mutex);
1609 return ret;
1610}
1611EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1612
1613/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001614 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1615 * callback
1616 * @kcontrol: mixer control
1617 * @ucontrol: control element information
1618 *
1619 * Callback to get the value of a dapm semi enumerated double mixer control.
1620 *
1621 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1622 * used for handling bitfield coded enumeration for example.
1623 *
1624 * Returns 0 for success.
1625 */
1626int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1627 struct snd_ctl_elem_value *ucontrol)
1628{
1629 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001630 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001631 unsigned short reg_val, val, mux;
1632
1633 reg_val = snd_soc_read(widget->codec, e->reg);
1634 val = (reg_val >> e->shift_l) & e->mask;
1635 for (mux = 0; mux < e->max; mux++) {
1636 if (val == e->values[mux])
1637 break;
1638 }
1639 ucontrol->value.enumerated.item[0] = mux;
1640 if (e->shift_l != e->shift_r) {
1641 val = (reg_val >> e->shift_r) & e->mask;
1642 for (mux = 0; mux < e->max; mux++) {
1643 if (val == e->values[mux])
1644 break;
1645 }
1646 ucontrol->value.enumerated.item[1] = mux;
1647 }
1648
1649 return 0;
1650}
1651EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1652
1653/**
1654 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1655 * callback
1656 * @kcontrol: mixer control
1657 * @ucontrol: control element information
1658 *
1659 * Callback to set the value of a dapm semi enumerated double mixer control.
1660 *
1661 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1662 * used for handling bitfield coded enumeration for example.
1663 *
1664 * Returns 0 for success.
1665 */
1666int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1667 struct snd_ctl_elem_value *ucontrol)
1668{
1669 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001670 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001671 unsigned short val, mux;
1672 unsigned short mask;
1673 int ret = 0;
1674
1675 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1676 return -EINVAL;
1677 mux = ucontrol->value.enumerated.item[0];
1678 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1679 mask = e->mask << e->shift_l;
1680 if (e->shift_l != e->shift_r) {
1681 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1682 return -EINVAL;
1683 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1684 mask |= e->mask << e->shift_r;
1685 }
1686
1687 mutex_lock(&widget->codec->mutex);
1688 widget->value = val;
Peter Ujfalusi74155552009-01-08 13:34:29 +02001689 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001690 if (widget->event) {
1691 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1692 ret = widget->event(widget,
1693 kcontrol, SND_SOC_DAPM_PRE_REG);
1694 if (ret < 0)
1695 goto out;
1696 }
1697 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1698 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1699 ret = widget->event(widget,
1700 kcontrol, SND_SOC_DAPM_POST_REG);
1701 } else
1702 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1703
1704out:
1705 mutex_unlock(&widget->codec->mutex);
1706 return ret;
1707}
1708EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1709
1710/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001711 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1712 *
1713 * @kcontrol: mixer control
1714 * @uinfo: control element information
1715 *
1716 * Callback to provide information about a pin switch control.
1717 */
1718int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1719 struct snd_ctl_elem_info *uinfo)
1720{
1721 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1722 uinfo->count = 1;
1723 uinfo->value.integer.min = 0;
1724 uinfo->value.integer.max = 1;
1725
1726 return 0;
1727}
1728EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1729
1730/**
1731 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1732 *
1733 * @kcontrol: mixer control
1734 * @ucontrol: Value
1735 */
1736int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1737 struct snd_ctl_elem_value *ucontrol)
1738{
1739 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1740 const char *pin = (const char *)kcontrol->private_value;
1741
1742 mutex_lock(&codec->mutex);
1743
1744 ucontrol->value.integer.value[0] =
1745 snd_soc_dapm_get_pin_status(codec, pin);
1746
1747 mutex_unlock(&codec->mutex);
1748
1749 return 0;
1750}
1751EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1752
1753/**
1754 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1755 *
1756 * @kcontrol: mixer control
1757 * @ucontrol: Value
1758 */
1759int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1760 struct snd_ctl_elem_value *ucontrol)
1761{
1762 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1763 const char *pin = (const char *)kcontrol->private_value;
1764
1765 mutex_lock(&codec->mutex);
1766
1767 if (ucontrol->value.integer.value[0])
1768 snd_soc_dapm_enable_pin(codec, pin);
1769 else
1770 snd_soc_dapm_disable_pin(codec, pin);
1771
1772 snd_soc_dapm_sync(codec);
1773
1774 mutex_unlock(&codec->mutex);
1775
1776 return 0;
1777}
1778EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1779
1780/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001781 * snd_soc_dapm_new_control - create new dapm control
1782 * @codec: audio codec
1783 * @widget: widget template
1784 *
1785 * Creates a new dapm control based upon the template.
1786 *
1787 * Returns 0 for success else error.
1788 */
1789int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1790 const struct snd_soc_dapm_widget *widget)
1791{
1792 struct snd_soc_dapm_widget *w;
1793
1794 if ((w = dapm_cnew_widget(widget)) == NULL)
1795 return -ENOMEM;
1796
1797 w->codec = codec;
1798 INIT_LIST_HEAD(&w->sources);
1799 INIT_LIST_HEAD(&w->sinks);
1800 INIT_LIST_HEAD(&w->list);
1801 list_add(&w->list, &codec->dapm_widgets);
1802
1803 /* machine layer set ups unconnected pins and insertions */
1804 w->connected = 1;
1805 return 0;
1806}
1807EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1808
1809/**
Mark Brown4ba13272008-05-13 14:51:19 +02001810 * snd_soc_dapm_new_controls - create new dapm controls
1811 * @codec: audio codec
1812 * @widget: widget array
1813 * @num: number of widgets
1814 *
1815 * Creates new DAPM controls based upon the templates.
1816 *
1817 * Returns 0 for success else error.
1818 */
1819int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1820 const struct snd_soc_dapm_widget *widget,
1821 int num)
1822{
1823 int i, ret;
1824
1825 for (i = 0; i < num; i++) {
1826 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00001827 if (ret < 0) {
1828 printk(KERN_ERR
1829 "ASoC: Failed to create DAPM control %s: %d\n",
1830 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02001831 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00001832 }
Mark Brown4ba13272008-05-13 14:51:19 +02001833 widget++;
1834 }
1835 return 0;
1836}
1837EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1838
1839
1840/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001841 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1842 * @codec: audio codec
1843 * @stream: stream name
1844 * @event: stream event
1845 *
1846 * Sends a stream event to the dapm core. The core then makes any
1847 * necessary widget power changes.
1848 *
1849 * Returns 0 for success else error.
1850 */
1851int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1852 char *stream, int event)
1853{
1854 struct snd_soc_dapm_widget *w;
1855
Seth Forshee11da21a2007-02-02 17:14:19 +01001856 if (stream == NULL)
1857 return 0;
1858
Richard Purdie2b97eab2006-10-06 18:32:18 +02001859 mutex_lock(&codec->mutex);
1860 list_for_each_entry(w, &codec->dapm_widgets, list)
1861 {
1862 if (!w->sname)
1863 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01001864 pr_debug("widget %s\n %s stream %s event %d\n",
1865 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001866 if (strstr(w->sname, stream)) {
1867 switch(event) {
1868 case SND_SOC_DAPM_STREAM_START:
1869 w->active = 1;
1870 break;
1871 case SND_SOC_DAPM_STREAM_STOP:
1872 w->active = 0;
1873 break;
1874 case SND_SOC_DAPM_STREAM_SUSPEND:
1875 if (w->active)
1876 w->suspend = 1;
1877 w->active = 0;
1878 break;
1879 case SND_SOC_DAPM_STREAM_RESUME:
1880 if (w->suspend) {
1881 w->active = 1;
1882 w->suspend = 0;
1883 }
1884 break;
1885 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1886 break;
1887 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1888 break;
1889 }
1890 }
1891 }
1892 mutex_unlock(&codec->mutex);
1893
1894 dapm_power_widgets(codec, event);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08001895 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001896 return 0;
1897}
1898EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1899
1900/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001901 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00001902 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01001903 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02001904 *
Mark Brown74b8f952009-06-06 11:26:15 +01001905 * Enables input/output pin and its parents or children widgets iff there is
Liam Girdwooda5302182008-07-07 13:35:17 +01001906 * a valid audio route and active audio stream.
1907 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1908 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001909 */
Mark Brown16499232009-01-07 18:25:13 +00001910int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001911{
Liam Girdwooda5302182008-07-07 13:35:17 +01001912 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001913}
Liam Girdwooda5302182008-07-07 13:35:17 +01001914EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001915
1916/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001917 * snd_soc_dapm_disable_pin - disable pin.
1918 * @codec: SoC codec
1919 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001920 *
Mark Brown74b8f952009-06-06 11:26:15 +01001921 * Disables input/output pin and its parents or children widgets.
Liam Girdwooda5302182008-07-07 13:35:17 +01001922 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1923 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001924 */
Mark Brown16499232009-01-07 18:25:13 +00001925int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01001926{
1927 return snd_soc_dapm_set_pin(codec, pin, 0);
1928}
1929EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1930
1931/**
Mark Brown5817b522008-09-24 11:23:11 +01001932 * snd_soc_dapm_nc_pin - permanently disable pin.
1933 * @codec: SoC codec
1934 * @pin: pin name
1935 *
1936 * Marks the specified pin as being not connected, disabling it along
1937 * any parent or child widgets. At present this is identical to
1938 * snd_soc_dapm_disable_pin() but in future it will be extended to do
1939 * additional things such as disabling controls which only affect
1940 * paths through the pin.
1941 *
1942 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1943 * do any widget power switching.
1944 */
Mark Brown16499232009-01-07 18:25:13 +00001945int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01001946{
1947 return snd_soc_dapm_set_pin(codec, pin, 0);
1948}
1949EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1950
1951/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001952 * snd_soc_dapm_get_pin_status - get audio pin status
1953 * @codec: audio codec
1954 * @pin: audio signal pin endpoint (or start point)
1955 *
1956 * Get audio pin status - connected or disconnected.
1957 *
1958 * Returns 1 for connected otherwise 0.
1959 */
Mark Brown16499232009-01-07 18:25:13 +00001960int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001961{
1962 struct snd_soc_dapm_widget *w;
1963
1964 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01001965 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001966 return w->connected;
1967 }
1968
1969 return 0;
1970}
Liam Girdwooda5302182008-07-07 13:35:17 +01001971EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001972
1973/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001974 * snd_soc_dapm_free - free dapm resources
1975 * @socdev: SoC device
1976 *
1977 * Free all dapm widgets and resources.
1978 */
1979void snd_soc_dapm_free(struct snd_soc_device *socdev)
1980{
Mark Brown6627a652009-01-23 22:55:23 +00001981 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001982
1983 snd_soc_dapm_sys_remove(socdev->dev);
1984 dapm_free_widgets(codec);
1985}
1986EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1987
1988/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01001989MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001990MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1991MODULE_LICENSE("GPL");