blob: 7847f80e96d102d8120675cb9973a74502d8a86f [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
15 * DAC's/ADC's.
16 * 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 Brown246d0a12009-04-22 18:24:55 +010055 snd_soc_dapm_pre, snd_soc_dapm_supply, snd_soc_dapm_micbias,
56 snd_soc_dapm_mic, snd_soc_dapm_mux, snd_soc_dapm_value_mux,
57 snd_soc_dapm_dac, snd_soc_dapm_mixer, snd_soc_dapm_mixer_named_ctl,
58 snd_soc_dapm_pga, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
59 snd_soc_dapm_post
Richard Purdie2b97eab2006-10-06 18:32:18 +020060};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000061
Richard Purdie2b97eab2006-10-06 18:32:18 +020062static int dapm_down_seq[] = {
63 snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
Ian Moltonca9c1aa2009-01-06 20:11:51 +000064 snd_soc_dapm_pga, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_mixer,
65 snd_soc_dapm_dac, snd_soc_dapm_mic, snd_soc_dapm_micbias,
Mark Brown246d0a12009-04-22 18:24:55 +010066 snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_supply,
67 snd_soc_dapm_post
Richard Purdie2b97eab2006-10-06 18:32:18 +020068};
69
70static int dapm_status = 1;
71module_param(dapm_status, int, 0);
72MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
73
Troy Kisky12ef1932008-10-13 17:42:14 -070074static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010075{
76 if (pop_time)
77 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
78}
79
Troy Kisky12ef1932008-10-13 17:42:14 -070080static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010081{
82 va_list args;
83
84 va_start(args, fmt);
85
86 if (pop_time) {
87 vprintk(fmt, args);
Troy Kisky12ef1932008-10-13 17:42:14 -070088 pop_wait(pop_time);
Mark Brown15e4c722008-07-02 11:51:20 +010089 }
90
91 va_end(args);
92}
93
Richard Purdie2b97eab2006-10-06 18:32:18 +020094/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +010095static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +020096 const struct snd_soc_dapm_widget *_widget)
97{
Takashi Iwai88cb4292007-02-05 14:56:20 +010098 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +020099}
100
101/* set up initial codec paths */
102static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
103 struct snd_soc_dapm_path *p, int i)
104{
105 switch (w->id) {
106 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000107 case snd_soc_dapm_mixer:
108 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200109 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100110 struct soc_mixer_control *mc = (struct soc_mixer_control *)
111 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400112 unsigned int reg = mc->reg;
113 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100114 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400115 unsigned int mask = (1 << fls(max)) - 1;
116 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200117
118 val = snd_soc_read(w->codec, reg);
119 val = (val >> shift) & mask;
120
121 if ((invert && !val) || (!invert && val))
122 p->connect = 1;
123 else
124 p->connect = 0;
125 }
126 break;
127 case snd_soc_dapm_mux: {
128 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
129 int val, item, bitmask;
130
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100131 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200132 ;
133 val = snd_soc_read(w->codec, e->reg);
134 item = (val >> e->shift_l) & (bitmask - 1);
135
136 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100137 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200138 if (!(strcmp(p->name, e->texts[i])) && item == i)
139 p->connect = 1;
140 }
141 }
142 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200143 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200144 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200145 w->kcontrols[i].private_value;
146 int val, item;
147
148 val = snd_soc_read(w->codec, e->reg);
149 val = (val >> e->shift_l) & e->mask;
150 for (item = 0; item < e->max; item++) {
151 if (val == e->values[item])
152 break;
153 }
154
155 p->connect = 0;
156 for (i = 0; i < e->max; i++) {
157 if (!(strcmp(p->name, e->texts[i])) && item == i)
158 p->connect = 1;
159 }
160 }
161 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200162 /* does not effect routing - always connected */
163 case snd_soc_dapm_pga:
164 case snd_soc_dapm_output:
165 case snd_soc_dapm_adc:
166 case snd_soc_dapm_input:
167 case snd_soc_dapm_dac:
168 case snd_soc_dapm_micbias:
169 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100170 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200171 p->connect = 1;
172 break;
173 /* does effect routing - dynamically connected */
174 case snd_soc_dapm_hp:
175 case snd_soc_dapm_mic:
176 case snd_soc_dapm_spk:
177 case snd_soc_dapm_line:
178 case snd_soc_dapm_pre:
179 case snd_soc_dapm_post:
180 p->connect = 0;
181 break;
182 }
183}
184
185/* connect mux widget to it's interconnecting audio paths */
186static int dapm_connect_mux(struct snd_soc_codec *codec,
187 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
188 struct snd_soc_dapm_path *path, const char *control_name,
189 const struct snd_kcontrol_new *kcontrol)
190{
191 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
192 int i;
193
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100194 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200195 if (!(strcmp(control_name, e->texts[i]))) {
196 list_add(&path->list, &codec->dapm_paths);
197 list_add(&path->list_sink, &dest->sources);
198 list_add(&path->list_source, &src->sinks);
199 path->name = (char*)e->texts[i];
200 dapm_set_path_status(dest, path, 0);
201 return 0;
202 }
203 }
204
205 return -ENODEV;
206}
207
208/* connect mixer widget to it's interconnecting audio paths */
209static int dapm_connect_mixer(struct snd_soc_codec *codec,
210 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
211 struct snd_soc_dapm_path *path, const char *control_name)
212{
213 int i;
214
215 /* search for mixer kcontrol */
216 for (i = 0; i < dest->num_kcontrols; i++) {
217 if (!strcmp(control_name, dest->kcontrols[i].name)) {
218 list_add(&path->list, &codec->dapm_paths);
219 list_add(&path->list_sink, &dest->sources);
220 list_add(&path->list_source, &src->sinks);
221 path->name = dest->kcontrols[i].name;
222 dapm_set_path_status(dest, path, i);
223 return 0;
224 }
225 }
226 return -ENODEV;
227}
228
229/* update dapm codec register bits */
230static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
231{
232 int change, power;
233 unsigned short old, new;
234 struct snd_soc_codec *codec = widget->codec;
235
236 /* check for valid widgets */
237 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
238 widget->id == snd_soc_dapm_output ||
239 widget->id == snd_soc_dapm_hp ||
240 widget->id == snd_soc_dapm_mic ||
241 widget->id == snd_soc_dapm_line ||
242 widget->id == snd_soc_dapm_spk)
243 return 0;
244
245 power = widget->power;
246 if (widget->invert)
247 power = (power ? 0:1);
248
249 old = snd_soc_read(codec, widget->reg);
250 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
251
252 change = old != new;
253 if (change) {
Troy Kisky12ef1932008-10-13 17:42:14 -0700254 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
255 widget->name, widget->power ? "on" : "off",
256 codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200257 snd_soc_write(codec, widget->reg, new);
Troy Kisky12ef1932008-10-13 17:42:14 -0700258 pop_wait(codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200259 }
Mark Brownc1286b82008-07-07 19:26:03 +0100260 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
261 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200262 return change;
263}
264
265/* ramps the volume up or down to minimise pops before or after a
266 * DAPM power event */
267static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
268{
269 const struct snd_kcontrol_new *k = widget->kcontrols;
270
271 if (widget->muted && !power)
272 return 0;
273 if (!widget->muted && power)
274 return 0;
275
276 if (widget->num_kcontrols && k) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100277 struct soc_mixer_control *mc =
278 (struct soc_mixer_control *)k->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400279 unsigned int reg = mc->reg;
280 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100281 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400282 unsigned int mask = (1 << fls(max)) - 1;
283 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200284
285 if (power) {
286 int i;
287 /* power up has happended, increase volume to last level */
288 if (invert) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100289 for (i = max; i > widget->saved_value; i--)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200290 snd_soc_update_bits(widget->codec, reg, mask, i);
291 } else {
292 for (i = 0; i < widget->saved_value; i++)
293 snd_soc_update_bits(widget->codec, reg, mask, i);
294 }
295 widget->muted = 0;
296 } else {
297 /* power down is about to occur, decrease volume to mute */
298 int val = snd_soc_read(widget->codec, reg);
299 int i = widget->saved_value = (val >> shift) & mask;
300 if (invert) {
301 for (; i < mask; i++)
302 snd_soc_update_bits(widget->codec, reg, mask, i);
303 } else {
304 for (; i > 0; i--)
305 snd_soc_update_bits(widget->codec, reg, mask, i);
306 }
307 widget->muted = 1;
308 }
309 }
310 return 0;
311}
312
313/* create new dapm mixer control */
314static int dapm_new_mixer(struct snd_soc_codec *codec,
315 struct snd_soc_dapm_widget *w)
316{
317 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000318 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200319 struct snd_soc_dapm_path *path;
320
321 /* add kcontrol */
322 for (i = 0; i < w->num_kcontrols; i++) {
323
324 /* match name */
325 list_for_each_entry(path, &w->sources, list_sink) {
326
327 /* mixer/mux paths name must match control name */
328 if (path->name != (char*)w->kcontrols[i].name)
329 continue;
330
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000331 /* add dapm control with long name.
332 * for dapm_mixer this is the concatenation of the
333 * mixer and kcontrol name.
334 * for dapm_mixer_named_ctl this is simply the
335 * kcontrol name.
336 */
337 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000338 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000339 name_len += 1 + strlen(w->name);
340
Mark Brown219b93f2008-10-28 13:02:31 +0000341 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000342
Richard Purdie2b97eab2006-10-06 18:32:18 +0200343 if (path->long_name == NULL)
344 return -ENOMEM;
345
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000346 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000347 default:
348 snprintf(path->long_name, name_len, "%s %s",
349 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000350 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000351 case snd_soc_dapm_mixer_named_ctl:
352 snprintf(path->long_name, name_len, "%s",
353 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000354 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000355 }
356
Mark Brown219b93f2008-10-28 13:02:31 +0000357 path->long_name[name_len - 1] = '\0';
358
Richard Purdie2b97eab2006-10-06 18:32:18 +0200359 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
360 path->long_name);
361 ret = snd_ctl_add(codec->card, path->kcontrol);
362 if (ret < 0) {
Mark Brown6553e192009-04-06 16:59:32 +0100363 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
364 path->long_name,
365 ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200366 kfree(path->long_name);
367 path->long_name = NULL;
368 return ret;
369 }
370 }
371 }
372 return ret;
373}
374
375/* create new dapm mux control */
376static int dapm_new_mux(struct snd_soc_codec *codec,
377 struct snd_soc_dapm_widget *w)
378{
379 struct snd_soc_dapm_path *path = NULL;
380 struct snd_kcontrol *kcontrol;
381 int ret = 0;
382
383 if (!w->num_kcontrols) {
384 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
385 return -EINVAL;
386 }
387
388 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
389 ret = snd_ctl_add(codec->card, kcontrol);
390 if (ret < 0)
391 goto err;
392
393 list_for_each_entry(path, &w->sources, list_sink)
394 path->kcontrol = kcontrol;
395
396 return ret;
397
398err:
399 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
400 return ret;
401}
402
403/* create new dapm volume control */
404static int dapm_new_pga(struct snd_soc_codec *codec,
405 struct snd_soc_dapm_widget *w)
406{
407 struct snd_kcontrol *kcontrol;
408 int ret = 0;
409
410 if (!w->num_kcontrols)
411 return -EINVAL;
412
413 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
414 ret = snd_ctl_add(codec->card, kcontrol);
415 if (ret < 0) {
416 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
417 return ret;
418 }
419
420 return ret;
421}
422
423/* reset 'walked' bit for each dapm path */
424static inline void dapm_clear_walk(struct snd_soc_codec *codec)
425{
426 struct snd_soc_dapm_path *p;
427
428 list_for_each_entry(p, &codec->dapm_paths, list)
429 p->walked = 0;
430}
431
432/*
433 * Recursively check for a completed path to an active or physically connected
434 * output widget. Returns number of complete paths.
435 */
436static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
437{
438 struct snd_soc_dapm_path *path;
439 int con = 0;
440
Mark Brown246d0a12009-04-22 18:24:55 +0100441 if (widget->id == snd_soc_dapm_supply)
442 return 0;
443
Richard Purdie2b97eab2006-10-06 18:32:18 +0200444 if (widget->id == snd_soc_dapm_adc && widget->active)
445 return 1;
446
447 if (widget->connected) {
448 /* connected pin ? */
449 if (widget->id == snd_soc_dapm_output && !widget->ext)
450 return 1;
451
452 /* connected jack or spk ? */
453 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
454 widget->id == snd_soc_dapm_line)
455 return 1;
456 }
457
458 list_for_each_entry(path, &widget->sinks, list_source) {
459 if (path->walked)
460 continue;
461
462 if (path->sink && path->connect) {
463 path->walked = 1;
464 con += is_connected_output_ep(path->sink);
465 }
466 }
467
468 return con;
469}
470
471/*
472 * Recursively check for a completed path to an active or physically connected
473 * input widget. Returns number of complete paths.
474 */
475static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
476{
477 struct snd_soc_dapm_path *path;
478 int con = 0;
479
Mark Brown246d0a12009-04-22 18:24:55 +0100480 if (widget->id == snd_soc_dapm_supply)
481 return 0;
482
Richard Purdie2b97eab2006-10-06 18:32:18 +0200483 /* active stream ? */
484 if (widget->id == snd_soc_dapm_dac && widget->active)
485 return 1;
486
487 if (widget->connected) {
488 /* connected pin ? */
489 if (widget->id == snd_soc_dapm_input && !widget->ext)
490 return 1;
491
492 /* connected VMID/Bias for lower pops */
493 if (widget->id == snd_soc_dapm_vmid)
494 return 1;
495
496 /* connected jack ? */
497 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
498 return 1;
499 }
500
501 list_for_each_entry(path, &widget->sources, list_sink) {
502 if (path->walked)
503 continue;
504
505 if (path->source && path->connect) {
506 path->walked = 1;
507 con += is_connected_input_ep(path->source);
508 }
509 }
510
511 return con;
512}
513
514/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300515 * Handler for generic register modifier widget.
516 */
517int dapm_reg_event(struct snd_soc_dapm_widget *w,
518 struct snd_kcontrol *kcontrol, int event)
519{
520 unsigned int val;
521
522 if (SND_SOC_DAPM_EVENT_ON(event))
523 val = w->on_val;
524 else
525 val = w->off_val;
526
527 snd_soc_update_bits(w->codec, -(w->reg + 1),
528 w->mask << w->shift, val << w->shift);
529
530 return 0;
531}
Mark Brown11589412008-07-29 11:42:23 +0100532EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300533
Mark Brown025756e2009-04-13 11:09:18 +0100534/* Standard power change method, used to apply power changes to most
535 * widgets.
536 */
537static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
538{
539 int ret;
540
541 /* call any power change event handlers */
542 if (w->event)
543 pr_debug("power %s event for %s flags %x\n",
544 w->power ? "on" : "off",
545 w->name, w->event_flags);
546
547 /* power up pre event */
548 if (w->power && w->event &&
549 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
550 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
551 if (ret < 0)
552 return ret;
553 }
554
555 /* power down pre event */
556 if (!w->power && w->event &&
557 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
558 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
559 if (ret < 0)
560 return ret;
561 }
562
563 /* Lower PGA volume to reduce pops */
564 if (w->id == snd_soc_dapm_pga && !w->power)
565 dapm_set_pga(w, w->power);
566
567 dapm_update_bits(w);
568
569 /* Raise PGA volume to reduce pops */
570 if (w->id == snd_soc_dapm_pga && w->power)
571 dapm_set_pga(w, w->power);
572
573 /* power up post event */
574 if (w->power && w->event &&
575 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
576 ret = w->event(w,
577 NULL, SND_SOC_DAPM_POST_PMU);
578 if (ret < 0)
579 return ret;
580 }
581
582 /* power down post event */
583 if (!w->power && w->event &&
584 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
585 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
586 if (ret < 0)
587 return ret;
588 }
589
590 return 0;
591}
592
Mark Browncd0f2d42009-04-20 16:56:59 +0100593/* Generic check to see if a widget should be powered.
594 */
595static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
596{
597 int in, out;
598
599 in = is_connected_input_ep(w);
600 dapm_clear_walk(w->codec);
601 out = is_connected_output_ep(w);
602 dapm_clear_walk(w->codec);
603 return out != 0 && in != 0;
604}
605
Mark Brown6ea31b92009-04-20 17:15:41 +0100606/* Check to see if an ADC has power */
607static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
608{
609 int in;
610
611 if (w->active) {
612 in = is_connected_input_ep(w);
613 dapm_clear_walk(w->codec);
614 return in != 0;
615 } else {
616 return dapm_generic_check_power(w);
617 }
618}
619
620/* Check to see if a DAC has power */
621static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
622{
623 int out;
624
625 if (w->active) {
626 out = is_connected_output_ep(w);
627 dapm_clear_walk(w->codec);
628 return out != 0;
629 } else {
630 return dapm_generic_check_power(w);
631 }
632}
633
Mark Brown246d0a12009-04-22 18:24:55 +0100634/* Check to see if a power supply is needed */
635static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
636{
637 struct snd_soc_dapm_path *path;
638 int power = 0;
639
640 /* Check if one of our outputs is connected */
641 list_for_each_entry(path, &w->sinks, list_source) {
642 if (path->sink && path->sink->power_check &&
643 path->sink->power_check(path->sink)) {
644 power = 1;
645 break;
646 }
647 }
648
649 dapm_clear_walk(w->codec);
650
651 return power;
652}
653
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300654/*
Mark Brown42aa3412009-03-01 19:21:10 +0000655 * Scan a single DAPM widget for a complete audio path and update the
656 * power status appropriately.
657 */
658static int dapm_power_widget(struct snd_soc_codec *codec, int event,
659 struct snd_soc_dapm_widget *w)
660{
Mark Brown6ea31b92009-04-20 17:15:41 +0100661 int power, ret;
Mark Brown42aa3412009-03-01 19:21:10 +0000662
Mark Brown6ea31b92009-04-20 17:15:41 +0100663 switch (w->id) {
Mark Brown6ea31b92009-04-20 17:15:41 +0100664 case snd_soc_dapm_pre:
Mark Brown42aa3412009-03-01 19:21:10 +0000665 if (!w->event)
666 return 0;
667
668 if (event == SND_SOC_DAPM_STREAM_START) {
669 ret = w->event(w,
670 NULL, SND_SOC_DAPM_PRE_PMU);
671 if (ret < 0)
672 return ret;
673 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
674 ret = w->event(w,
675 NULL, SND_SOC_DAPM_PRE_PMD);
676 if (ret < 0)
677 return ret;
678 }
679 return 0;
Mark Brown6ea31b92009-04-20 17:15:41 +0100680
681 case snd_soc_dapm_post:
Mark Brown42aa3412009-03-01 19:21:10 +0000682 if (!w->event)
683 return 0;
684
685 if (event == SND_SOC_DAPM_STREAM_START) {
686 ret = w->event(w,
687 NULL, SND_SOC_DAPM_POST_PMU);
688 if (ret < 0)
689 return ret;
690 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
691 ret = w->event(w,
692 NULL, SND_SOC_DAPM_POST_PMD);
693 if (ret < 0)
694 return ret;
695 }
696 return 0;
Mark Brown6ea31b92009-04-20 17:15:41 +0100697
698 default:
Mark Brown6ea31b92009-04-20 17:15:41 +0100699 break;
Mark Brown42aa3412009-03-01 19:21:10 +0000700 }
701
Mark Brownb75576d2009-04-20 17:56:13 +0100702 if (!w->power_check)
703 return 0;
704
705 power = w->power_check(w);
Mark Brown6ea31b92009-04-20 17:15:41 +0100706 if (w->power == power)
Mark Brown42aa3412009-03-01 19:21:10 +0000707 return 0;
Mark Brown6ea31b92009-04-20 17:15:41 +0100708 w->power = power;
Mark Brown42aa3412009-03-01 19:21:10 +0000709
Mark Brown025756e2009-04-13 11:09:18 +0100710 return dapm_generic_apply_power(w);
Mark Brown42aa3412009-03-01 19:21:10 +0000711}
712
713/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200714 * Scan each dapm widget for complete audio path.
715 * A complete path is a route that has valid endpoints i.e.:-
716 *
717 * o DAC to output pin.
718 * o Input Pin to ADC.
719 * o Input pin to Output pin (bypass, sidetone)
720 * o DAC to ADC (loopback).
721 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100722static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200723{
724 struct snd_soc_dapm_widget *w;
Mark Brown42aa3412009-03-01 19:21:10 +0000725 int i, c = 1, *seq = NULL, ret = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200726
727 /* do we have a sequenced stream event */
728 if (event == SND_SOC_DAPM_STREAM_START) {
729 c = ARRAY_SIZE(dapm_up_seq);
730 seq = dapm_up_seq;
731 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
732 c = ARRAY_SIZE(dapm_down_seq);
733 seq = dapm_down_seq;
734 }
735
Mark Brown42aa3412009-03-01 19:21:10 +0000736 for (i = 0; i < c; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200737 list_for_each_entry(w, &codec->dapm_widgets, list) {
738
739 /* is widget in stream order */
740 if (seq && seq[i] && w->id != seq[i])
741 continue;
742
Mark Brown42aa3412009-03-01 19:21:10 +0000743 ret = dapm_power_widget(codec, event, w);
744 if (ret != 0)
745 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200746 }
747 }
748
Mark Brown42aa3412009-03-01 19:21:10 +0000749 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200750}
751
Mark Brownc1286b82008-07-07 19:26:03 +0100752#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +0200753static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
754{
755 struct snd_soc_dapm_widget *w;
756 struct snd_soc_dapm_path *p = NULL;
757 int in, out;
758
759 printk("DAPM %s %s\n", codec->name, action);
760
761 list_for_each_entry(w, &codec->dapm_widgets, list) {
762
763 /* only display widgets that effect routing */
764 switch (w->id) {
765 case snd_soc_dapm_pre:
766 case snd_soc_dapm_post:
767 case snd_soc_dapm_vmid:
768 continue;
769 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200770 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200771 case snd_soc_dapm_output:
772 case snd_soc_dapm_input:
773 case snd_soc_dapm_switch:
774 case snd_soc_dapm_hp:
775 case snd_soc_dapm_mic:
776 case snd_soc_dapm_spk:
777 case snd_soc_dapm_line:
778 case snd_soc_dapm_micbias:
779 case snd_soc_dapm_dac:
780 case snd_soc_dapm_adc:
781 case snd_soc_dapm_pga:
782 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000783 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +0100784 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200785 if (w->name) {
786 in = is_connected_input_ep(w);
787 dapm_clear_walk(w->codec);
788 out = is_connected_output_ep(w);
789 dapm_clear_walk(w->codec);
790 printk("%s: %s in %d out %d\n", w->name,
791 w->power ? "On":"Off",in, out);
792
793 list_for_each_entry(p, &w->sources, list_sink) {
794 if (p->connect)
795 printk(" in %s %s\n", p->name ? p->name : "static",
796 p->source->name);
797 }
798 list_for_each_entry(p, &w->sinks, list_source) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200799 if (p->connect)
800 printk(" out %s %s\n", p->name ? p->name : "static",
801 p->sink->name);
802 }
803 }
804 break;
805 }
806 }
807}
808#endif
809
810/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100811static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
812 struct snd_kcontrol *kcontrol, int mask,
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800813 int mux, int val, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200814{
815 struct snd_soc_dapm_path *path;
816 int found = 0;
817
Peter Ujfalusieff317d2009-01-15 14:40:47 +0200818 if (widget->id != snd_soc_dapm_mux &&
819 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200820 return -ENODEV;
821
822 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
823 return 0;
824
825 /* find dapm widget path assoc with kcontrol */
826 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
827 if (path->kcontrol != kcontrol)
828 continue;
829
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800830 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +0200831 continue;
832
833 found = 1;
834 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800835 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +0200836 path->connect = 1; /* new connection */
837 else
838 path->connect = 0; /* old connection must be powered down */
839 }
840
Mark Brown3fccd8b2008-07-07 19:26:04 +0100841 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200842 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100843 dump_dapm(widget->codec, "mux power update");
844 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200845
846 return 0;
847}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200848
Milan plzik1b075e32008-01-10 14:39:46 +0100849/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100850static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
851 struct snd_kcontrol *kcontrol, int reg,
852 int val_mask, int val, int invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200853{
854 struct snd_soc_dapm_path *path;
855 int found = 0;
856
Milan plzik1b075e32008-01-10 14:39:46 +0100857 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000858 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +0100859 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200860 return -ENODEV;
861
862 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
863 return 0;
864
865 /* find dapm widget path assoc with kcontrol */
866 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
867 if (path->kcontrol != kcontrol)
868 continue;
869
870 /* found, now check type */
871 found = 1;
872 if (val)
873 /* new connection */
874 path->connect = invert ? 0:1;
875 else
876 /* old connection must be powered down */
877 path->connect = invert ? 1:0;
878 break;
879 }
880
Mark Brown3fccd8b2008-07-07 19:26:04 +0100881 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200882 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100883 dump_dapm(widget->codec, "mixer power update");
884 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200885
886 return 0;
887}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200888
889/* show dapm widget status in sys fs */
890static ssize_t dapm_widget_show(struct device *dev,
891 struct device_attribute *attr, char *buf)
892{
893 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +0000894 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200895 struct snd_soc_dapm_widget *w;
896 int count = 0;
897 char *state = "not set";
898
899 list_for_each_entry(w, &codec->dapm_widgets, list) {
900
901 /* only display widgets that burnm power */
902 switch (w->id) {
903 case snd_soc_dapm_hp:
904 case snd_soc_dapm_mic:
905 case snd_soc_dapm_spk:
906 case snd_soc_dapm_line:
907 case snd_soc_dapm_micbias:
908 case snd_soc_dapm_dac:
909 case snd_soc_dapm_adc:
910 case snd_soc_dapm_pga:
911 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000912 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +0100913 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200914 if (w->name)
915 count += sprintf(buf + count, "%s: %s\n",
916 w->name, w->power ? "On":"Off");
917 break;
918 default:
919 break;
920 }
921 }
922
Mark Brown0be98982008-05-19 12:31:28 +0200923 switch (codec->bias_level) {
924 case SND_SOC_BIAS_ON:
925 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200926 break;
Mark Brown0be98982008-05-19 12:31:28 +0200927 case SND_SOC_BIAS_PREPARE:
928 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200929 break;
Mark Brown0be98982008-05-19 12:31:28 +0200930 case SND_SOC_BIAS_STANDBY:
931 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200932 break;
Mark Brown0be98982008-05-19 12:31:28 +0200933 case SND_SOC_BIAS_OFF:
934 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200935 break;
936 }
937 count += sprintf(buf + count, "PM State: %s\n", state);
938
939 return count;
940}
941
942static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
943
944int snd_soc_dapm_sys_add(struct device *dev)
945{
Mark Brownf0062a92008-08-28 12:46:24 +0100946 if (!dapm_status)
947 return 0;
Troy Kisky12ef1932008-10-13 17:42:14 -0700948 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200949}
950
951static void snd_soc_dapm_sys_remove(struct device *dev)
952{
Mark Brown15e4c722008-07-02 11:51:20 +0100953 if (dapm_status) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200954 device_remove_file(dev, &dev_attr_dapm_widget);
Mark Brown15e4c722008-07-02 11:51:20 +0100955 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200956}
957
958/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100959static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200960{
961 struct snd_soc_dapm_widget *w, *next_w;
962 struct snd_soc_dapm_path *p, *next_p;
963
964 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
965 list_del(&w->list);
966 kfree(w);
967 }
968
969 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
970 list_del(&p->list);
971 kfree(p->long_name);
972 kfree(p);
973 }
974}
975
Liam Girdwooda5302182008-07-07 13:35:17 +0100976static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +0000977 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +0100978{
979 struct snd_soc_dapm_widget *w;
980
981 list_for_each_entry(w, &codec->dapm_widgets, list) {
982 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +0100983 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +0100984 w->connected = status;
985 return 0;
986 }
987 }
988
Mark Brownc1286b82008-07-07 19:26:03 +0100989 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +0100990 return -EINVAL;
991}
992
Richard Purdie2b97eab2006-10-06 18:32:18 +0200993/**
Liam Girdwooda5302182008-07-07 13:35:17 +0100994 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +0200995 * @codec: audio codec
996 *
997 * Walks all dapm audio paths and powers widgets according to their
998 * stream or path usage.
999 *
1000 * Returns 0 for success.
1001 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001002int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001003{
Mark Brown3fccd8b2008-07-07 19:26:04 +01001004 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1005 dump_dapm(codec, "sync");
1006 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001007}
Liam Girdwooda5302182008-07-07 13:35:17 +01001008EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001009
Mark Brown105f1c22008-05-13 14:52:19 +02001010static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
1011 const char *sink, const char *control, const char *source)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001012{
1013 struct snd_soc_dapm_path *path;
1014 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1015 int ret = 0;
1016
1017 /* find src and dest widgets */
1018 list_for_each_entry(w, &codec->dapm_widgets, list) {
1019
1020 if (!wsink && !(strcmp(w->name, sink))) {
1021 wsink = w;
1022 continue;
1023 }
1024 if (!wsource && !(strcmp(w->name, source))) {
1025 wsource = w;
1026 }
1027 }
1028
1029 if (wsource == NULL || wsink == NULL)
1030 return -ENODEV;
1031
1032 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1033 if (!path)
1034 return -ENOMEM;
1035
1036 path->source = wsource;
1037 path->sink = wsink;
1038 INIT_LIST_HEAD(&path->list);
1039 INIT_LIST_HEAD(&path->list_source);
1040 INIT_LIST_HEAD(&path->list_sink);
1041
1042 /* check for external widgets */
1043 if (wsink->id == snd_soc_dapm_input) {
1044 if (wsource->id == snd_soc_dapm_micbias ||
1045 wsource->id == snd_soc_dapm_mic ||
Seth Forshee1e392212007-04-16 15:36:42 +02001046 wsink->id == snd_soc_dapm_line ||
1047 wsink->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001048 wsink->ext = 1;
1049 }
1050 if (wsource->id == snd_soc_dapm_output) {
1051 if (wsink->id == snd_soc_dapm_spk ||
1052 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001053 wsink->id == snd_soc_dapm_line ||
1054 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001055 wsource->ext = 1;
1056 }
1057
1058 /* connect static paths */
1059 if (control == NULL) {
1060 list_add(&path->list, &codec->dapm_paths);
1061 list_add(&path->list_sink, &wsink->sources);
1062 list_add(&path->list_source, &wsource->sinks);
1063 path->connect = 1;
1064 return 0;
1065 }
1066
1067 /* connect dynamic paths */
1068 switch(wsink->id) {
1069 case snd_soc_dapm_adc:
1070 case snd_soc_dapm_dac:
1071 case snd_soc_dapm_pga:
1072 case snd_soc_dapm_input:
1073 case snd_soc_dapm_output:
1074 case snd_soc_dapm_micbias:
1075 case snd_soc_dapm_vmid:
1076 case snd_soc_dapm_pre:
1077 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001078 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001079 list_add(&path->list, &codec->dapm_paths);
1080 list_add(&path->list_sink, &wsink->sources);
1081 list_add(&path->list_source, &wsource->sinks);
1082 path->connect = 1;
1083 return 0;
1084 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001085 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001086 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1087 &wsink->kcontrols[0]);
1088 if (ret != 0)
1089 goto err;
1090 break;
1091 case snd_soc_dapm_switch:
1092 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001093 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001094 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1095 if (ret != 0)
1096 goto err;
1097 break;
1098 case snd_soc_dapm_hp:
1099 case snd_soc_dapm_mic:
1100 case snd_soc_dapm_line:
1101 case snd_soc_dapm_spk:
1102 list_add(&path->list, &codec->dapm_paths);
1103 list_add(&path->list_sink, &wsink->sources);
1104 list_add(&path->list_source, &wsource->sinks);
1105 path->connect = 0;
1106 return 0;
1107 }
1108 return 0;
1109
1110err:
1111 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1112 control, sink);
1113 kfree(path);
1114 return ret;
1115}
Mark Brown105f1c22008-05-13 14:52:19 +02001116
1117/**
Mark Brown105f1c22008-05-13 14:52:19 +02001118 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1119 * @codec: codec
1120 * @route: audio routes
1121 * @num: number of routes
1122 *
1123 * Connects 2 dapm widgets together via a named audio path. The sink is
1124 * the widget receiving the audio signal, whilst the source is the sender
1125 * of the audio signal.
1126 *
1127 * Returns 0 for success else error. On error all resources can be freed
1128 * with a call to snd_soc_card_free().
1129 */
1130int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1131 const struct snd_soc_dapm_route *route, int num)
1132{
1133 int i, ret;
1134
1135 for (i = 0; i < num; i++) {
1136 ret = snd_soc_dapm_add_route(codec, route->sink,
1137 route->control, route->source);
1138 if (ret < 0) {
1139 printk(KERN_ERR "Failed to add route %s->%s\n",
1140 route->source,
1141 route->sink);
1142 return ret;
1143 }
1144 route++;
1145 }
1146
1147 return 0;
1148}
1149EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1150
1151/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001152 * snd_soc_dapm_new_widgets - add new dapm widgets
1153 * @codec: audio codec
1154 *
1155 * Checks the codec for any new dapm widgets and creates them if found.
1156 *
1157 * Returns 0 for success.
1158 */
1159int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1160{
1161 struct snd_soc_dapm_widget *w;
1162
Richard Purdie2b97eab2006-10-06 18:32:18 +02001163 list_for_each_entry(w, &codec->dapm_widgets, list)
1164 {
1165 if (w->new)
1166 continue;
1167
1168 switch(w->id) {
1169 case snd_soc_dapm_switch:
1170 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001171 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001172 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001173 dapm_new_mixer(codec, w);
1174 break;
1175 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001176 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001177 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001178 dapm_new_mux(codec, w);
1179 break;
1180 case snd_soc_dapm_adc:
Mark Brownb75576d2009-04-20 17:56:13 +01001181 w->power_check = dapm_adc_check_power;
1182 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001183 case snd_soc_dapm_dac:
Mark Brownb75576d2009-04-20 17:56:13 +01001184 w->power_check = dapm_dac_check_power;
1185 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001186 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001187 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001188 dapm_new_pga(codec, w);
1189 break;
1190 case snd_soc_dapm_input:
1191 case snd_soc_dapm_output:
1192 case snd_soc_dapm_micbias:
1193 case snd_soc_dapm_spk:
1194 case snd_soc_dapm_hp:
1195 case snd_soc_dapm_mic:
1196 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001197 w->power_check = dapm_generic_check_power;
1198 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001199 case snd_soc_dapm_supply:
1200 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001201 case snd_soc_dapm_vmid:
1202 case snd_soc_dapm_pre:
1203 case snd_soc_dapm_post:
1204 break;
1205 }
1206 w->new = 1;
1207 }
1208
1209 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001210 return 0;
1211}
1212EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1213
1214/**
1215 * snd_soc_dapm_get_volsw - dapm mixer get callback
1216 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001217 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001218 *
1219 * Callback to get the value of a dapm mixer control.
1220 *
1221 * Returns 0 for success.
1222 */
1223int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1224 struct snd_ctl_elem_value *ucontrol)
1225{
1226 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001227 struct soc_mixer_control *mc =
1228 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001229 unsigned int reg = mc->reg;
1230 unsigned int shift = mc->shift;
1231 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001232 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001233 unsigned int invert = mc->invert;
1234 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001235
1236 /* return the saved value if we are powered down */
1237 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1238 ucontrol->value.integer.value[0] = widget->saved_value;
1239 return 0;
1240 }
1241
1242 ucontrol->value.integer.value[0] =
1243 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1244 if (shift != rshift)
1245 ucontrol->value.integer.value[1] =
1246 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1247 if (invert) {
1248 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001249 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001250 if (shift != rshift)
1251 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001252 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001253 }
1254
1255 return 0;
1256}
1257EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1258
1259/**
1260 * snd_soc_dapm_put_volsw - dapm mixer set callback
1261 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001262 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001263 *
1264 * Callback to set the value of a dapm mixer control.
1265 *
1266 * Returns 0 for success.
1267 */
1268int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1269 struct snd_ctl_elem_value *ucontrol)
1270{
1271 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001272 struct soc_mixer_control *mc =
1273 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001274 unsigned int reg = mc->reg;
1275 unsigned int shift = mc->shift;
1276 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001277 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001278 unsigned int mask = (1 << fls(max)) - 1;
1279 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001280 unsigned short val, val2, val_mask;
1281 int ret;
1282
1283 val = (ucontrol->value.integer.value[0] & mask);
1284
1285 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001286 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001287 val_mask = mask << shift;
1288 val = val << shift;
1289 if (shift != rshift) {
1290 val2 = (ucontrol->value.integer.value[1] & mask);
1291 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001292 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001293 val_mask |= mask << rshift;
1294 val |= val2 << rshift;
1295 }
1296
1297 mutex_lock(&widget->codec->mutex);
1298 widget->value = val;
1299
1300 /* save volume value if the widget is powered down */
1301 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1302 widget->saved_value = val;
1303 mutex_unlock(&widget->codec->mutex);
1304 return 1;
1305 }
1306
1307 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1308 if (widget->event) {
1309 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001310 ret = widget->event(widget, kcontrol,
1311 SND_SOC_DAPM_PRE_REG);
1312 if (ret < 0) {
1313 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001314 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001315 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001316 }
1317 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1318 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001319 ret = widget->event(widget, kcontrol,
1320 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001321 } else
1322 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1323
1324out:
1325 mutex_unlock(&widget->codec->mutex);
1326 return ret;
1327}
1328EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1329
1330/**
1331 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1332 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001333 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001334 *
1335 * Callback to get the value of a dapm enumerated double mixer control.
1336 *
1337 * Returns 0 for success.
1338 */
1339int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1340 struct snd_ctl_elem_value *ucontrol)
1341{
1342 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1343 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1344 unsigned short val, bitmask;
1345
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001346 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001347 ;
1348 val = snd_soc_read(widget->codec, e->reg);
1349 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1350 if (e->shift_l != e->shift_r)
1351 ucontrol->value.enumerated.item[1] =
1352 (val >> e->shift_r) & (bitmask - 1);
1353
1354 return 0;
1355}
1356EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1357
1358/**
1359 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1360 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001361 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001362 *
1363 * Callback to set the value of a dapm enumerated double mixer control.
1364 *
1365 * Returns 0 for success.
1366 */
1367int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1368 struct snd_ctl_elem_value *ucontrol)
1369{
1370 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1371 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1372 unsigned short val, mux;
1373 unsigned short mask, bitmask;
1374 int ret = 0;
1375
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001376 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001377 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001378 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001379 return -EINVAL;
1380 mux = ucontrol->value.enumerated.item[0];
1381 val = mux << e->shift_l;
1382 mask = (bitmask - 1) << e->shift_l;
1383 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001384 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001385 return -EINVAL;
1386 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1387 mask |= (bitmask - 1) << e->shift_r;
1388 }
1389
1390 mutex_lock(&widget->codec->mutex);
1391 widget->value = val;
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001392 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001393 if (widget->event) {
1394 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001395 ret = widget->event(widget,
1396 kcontrol, SND_SOC_DAPM_PRE_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001397 if (ret < 0)
1398 goto out;
1399 }
1400 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1401 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001402 ret = widget->event(widget,
1403 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001404 } else
1405 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1406
1407out:
1408 mutex_unlock(&widget->codec->mutex);
1409 return ret;
1410}
1411EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1412
1413/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001414 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1415 * callback
1416 * @kcontrol: mixer control
1417 * @ucontrol: control element information
1418 *
1419 * Callback to get the value of a dapm semi enumerated double mixer control.
1420 *
1421 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1422 * used for handling bitfield coded enumeration for example.
1423 *
1424 * Returns 0 for success.
1425 */
1426int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1427 struct snd_ctl_elem_value *ucontrol)
1428{
1429 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001430 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001431 unsigned short reg_val, val, mux;
1432
1433 reg_val = snd_soc_read(widget->codec, e->reg);
1434 val = (reg_val >> e->shift_l) & e->mask;
1435 for (mux = 0; mux < e->max; mux++) {
1436 if (val == e->values[mux])
1437 break;
1438 }
1439 ucontrol->value.enumerated.item[0] = mux;
1440 if (e->shift_l != e->shift_r) {
1441 val = (reg_val >> e->shift_r) & e->mask;
1442 for (mux = 0; mux < e->max; mux++) {
1443 if (val == e->values[mux])
1444 break;
1445 }
1446 ucontrol->value.enumerated.item[1] = mux;
1447 }
1448
1449 return 0;
1450}
1451EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1452
1453/**
1454 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1455 * callback
1456 * @kcontrol: mixer control
1457 * @ucontrol: control element information
1458 *
1459 * Callback to set the value of a dapm semi enumerated double mixer control.
1460 *
1461 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1462 * used for handling bitfield coded enumeration for example.
1463 *
1464 * Returns 0 for success.
1465 */
1466int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1467 struct snd_ctl_elem_value *ucontrol)
1468{
1469 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001470 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001471 unsigned short val, mux;
1472 unsigned short mask;
1473 int ret = 0;
1474
1475 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1476 return -EINVAL;
1477 mux = ucontrol->value.enumerated.item[0];
1478 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1479 mask = e->mask << e->shift_l;
1480 if (e->shift_l != e->shift_r) {
1481 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1482 return -EINVAL;
1483 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1484 mask |= e->mask << e->shift_r;
1485 }
1486
1487 mutex_lock(&widget->codec->mutex);
1488 widget->value = val;
Peter Ujfalusi74155552009-01-08 13:34:29 +02001489 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001490 if (widget->event) {
1491 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1492 ret = widget->event(widget,
1493 kcontrol, SND_SOC_DAPM_PRE_REG);
1494 if (ret < 0)
1495 goto out;
1496 }
1497 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1498 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1499 ret = widget->event(widget,
1500 kcontrol, SND_SOC_DAPM_POST_REG);
1501 } else
1502 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1503
1504out:
1505 mutex_unlock(&widget->codec->mutex);
1506 return ret;
1507}
1508EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1509
1510/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001511 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1512 *
1513 * @kcontrol: mixer control
1514 * @uinfo: control element information
1515 *
1516 * Callback to provide information about a pin switch control.
1517 */
1518int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1519 struct snd_ctl_elem_info *uinfo)
1520{
1521 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1522 uinfo->count = 1;
1523 uinfo->value.integer.min = 0;
1524 uinfo->value.integer.max = 1;
1525
1526 return 0;
1527}
1528EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1529
1530/**
1531 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1532 *
1533 * @kcontrol: mixer control
1534 * @ucontrol: Value
1535 */
1536int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1537 struct snd_ctl_elem_value *ucontrol)
1538{
1539 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1540 const char *pin = (const char *)kcontrol->private_value;
1541
1542 mutex_lock(&codec->mutex);
1543
1544 ucontrol->value.integer.value[0] =
1545 snd_soc_dapm_get_pin_status(codec, pin);
1546
1547 mutex_unlock(&codec->mutex);
1548
1549 return 0;
1550}
1551EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1552
1553/**
1554 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1555 *
1556 * @kcontrol: mixer control
1557 * @ucontrol: Value
1558 */
1559int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1560 struct snd_ctl_elem_value *ucontrol)
1561{
1562 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1563 const char *pin = (const char *)kcontrol->private_value;
1564
1565 mutex_lock(&codec->mutex);
1566
1567 if (ucontrol->value.integer.value[0])
1568 snd_soc_dapm_enable_pin(codec, pin);
1569 else
1570 snd_soc_dapm_disable_pin(codec, pin);
1571
1572 snd_soc_dapm_sync(codec);
1573
1574 mutex_unlock(&codec->mutex);
1575
1576 return 0;
1577}
1578EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1579
1580/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001581 * snd_soc_dapm_new_control - create new dapm control
1582 * @codec: audio codec
1583 * @widget: widget template
1584 *
1585 * Creates a new dapm control based upon the template.
1586 *
1587 * Returns 0 for success else error.
1588 */
1589int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1590 const struct snd_soc_dapm_widget *widget)
1591{
1592 struct snd_soc_dapm_widget *w;
1593
1594 if ((w = dapm_cnew_widget(widget)) == NULL)
1595 return -ENOMEM;
1596
1597 w->codec = codec;
1598 INIT_LIST_HEAD(&w->sources);
1599 INIT_LIST_HEAD(&w->sinks);
1600 INIT_LIST_HEAD(&w->list);
1601 list_add(&w->list, &codec->dapm_widgets);
1602
1603 /* machine layer set ups unconnected pins and insertions */
1604 w->connected = 1;
1605 return 0;
1606}
1607EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1608
1609/**
Mark Brown4ba13272008-05-13 14:51:19 +02001610 * snd_soc_dapm_new_controls - create new dapm controls
1611 * @codec: audio codec
1612 * @widget: widget array
1613 * @num: number of widgets
1614 *
1615 * Creates new DAPM controls based upon the templates.
1616 *
1617 * Returns 0 for success else error.
1618 */
1619int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1620 const struct snd_soc_dapm_widget *widget,
1621 int num)
1622{
1623 int i, ret;
1624
1625 for (i = 0; i < num; i++) {
1626 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00001627 if (ret < 0) {
1628 printk(KERN_ERR
1629 "ASoC: Failed to create DAPM control %s: %d\n",
1630 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02001631 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00001632 }
Mark Brown4ba13272008-05-13 14:51:19 +02001633 widget++;
1634 }
1635 return 0;
1636}
1637EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1638
1639
1640/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001641 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1642 * @codec: audio codec
1643 * @stream: stream name
1644 * @event: stream event
1645 *
1646 * Sends a stream event to the dapm core. The core then makes any
1647 * necessary widget power changes.
1648 *
1649 * Returns 0 for success else error.
1650 */
1651int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1652 char *stream, int event)
1653{
1654 struct snd_soc_dapm_widget *w;
1655
Seth Forshee11da21a2007-02-02 17:14:19 +01001656 if (stream == NULL)
1657 return 0;
1658
Richard Purdie2b97eab2006-10-06 18:32:18 +02001659 mutex_lock(&codec->mutex);
1660 list_for_each_entry(w, &codec->dapm_widgets, list)
1661 {
1662 if (!w->sname)
1663 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01001664 pr_debug("widget %s\n %s stream %s event %d\n",
1665 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001666 if (strstr(w->sname, stream)) {
1667 switch(event) {
1668 case SND_SOC_DAPM_STREAM_START:
1669 w->active = 1;
1670 break;
1671 case SND_SOC_DAPM_STREAM_STOP:
1672 w->active = 0;
1673 break;
1674 case SND_SOC_DAPM_STREAM_SUSPEND:
1675 if (w->active)
1676 w->suspend = 1;
1677 w->active = 0;
1678 break;
1679 case SND_SOC_DAPM_STREAM_RESUME:
1680 if (w->suspend) {
1681 w->active = 1;
1682 w->suspend = 0;
1683 }
1684 break;
1685 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1686 break;
1687 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1688 break;
1689 }
1690 }
1691 }
1692 mutex_unlock(&codec->mutex);
1693
1694 dapm_power_widgets(codec, event);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08001695 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001696 return 0;
1697}
1698EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1699
1700/**
Mark Brown0be98982008-05-19 12:31:28 +02001701 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001702 * @socdev: audio device
Mark Brown0be98982008-05-19 12:31:28 +02001703 * @level: level to configure
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001704 *
Mark Brown0be98982008-05-19 12:31:28 +02001705 * Configure the bias (power) levels for the SoC audio device.
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001706 *
1707 * Returns 0 for success else error.
1708 */
Mark Brown0be98982008-05-19 12:31:28 +02001709int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1710 enum snd_soc_bias_level level)
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001711{
Mark Brown87506542008-11-18 20:50:34 +00001712 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001713 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown0be98982008-05-19 12:31:28 +02001714 int ret = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001715
Mark Brown87506542008-11-18 20:50:34 +00001716 if (card->set_bias_level)
1717 ret = card->set_bias_level(card, level);
Mark Brown0be98982008-05-19 12:31:28 +02001718 if (ret == 0 && codec->set_bias_level)
1719 ret = codec->set_bias_level(codec, level);
1720
1721 return ret;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001722}
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001723
1724/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001725 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00001726 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01001727 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02001728 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001729 * Enables input/output pin and it's parents or children widgets iff there is
1730 * a valid audio route and active audio stream.
1731 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1732 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001733 */
Mark Brown16499232009-01-07 18:25:13 +00001734int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001735{
Liam Girdwooda5302182008-07-07 13:35:17 +01001736 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001737}
Liam Girdwooda5302182008-07-07 13:35:17 +01001738EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001739
1740/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001741 * snd_soc_dapm_disable_pin - disable pin.
1742 * @codec: SoC codec
1743 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001744 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001745 * Disables input/output pin and it's parents or children widgets.
1746 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1747 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001748 */
Mark Brown16499232009-01-07 18:25:13 +00001749int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01001750{
1751 return snd_soc_dapm_set_pin(codec, pin, 0);
1752}
1753EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1754
1755/**
Mark Brown5817b522008-09-24 11:23:11 +01001756 * snd_soc_dapm_nc_pin - permanently disable pin.
1757 * @codec: SoC codec
1758 * @pin: pin name
1759 *
1760 * Marks the specified pin as being not connected, disabling it along
1761 * any parent or child widgets. At present this is identical to
1762 * snd_soc_dapm_disable_pin() but in future it will be extended to do
1763 * additional things such as disabling controls which only affect
1764 * paths through the pin.
1765 *
1766 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1767 * do any widget power switching.
1768 */
Mark Brown16499232009-01-07 18:25:13 +00001769int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01001770{
1771 return snd_soc_dapm_set_pin(codec, pin, 0);
1772}
1773EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1774
1775/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001776 * snd_soc_dapm_get_pin_status - get audio pin status
1777 * @codec: audio codec
1778 * @pin: audio signal pin endpoint (or start point)
1779 *
1780 * Get audio pin status - connected or disconnected.
1781 *
1782 * Returns 1 for connected otherwise 0.
1783 */
Mark Brown16499232009-01-07 18:25:13 +00001784int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001785{
1786 struct snd_soc_dapm_widget *w;
1787
1788 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01001789 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001790 return w->connected;
1791 }
1792
1793 return 0;
1794}
Liam Girdwooda5302182008-07-07 13:35:17 +01001795EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001796
1797/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001798 * snd_soc_dapm_free - free dapm resources
1799 * @socdev: SoC device
1800 *
1801 * Free all dapm widgets and resources.
1802 */
1803void snd_soc_dapm_free(struct snd_soc_device *socdev)
1804{
Mark Brown6627a652009-01-23 22:55:23 +00001805 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001806
1807 snd_soc_dapm_sys_remove(socdev->dev);
1808 dapm_free_widgets(codec);
1809}
1810EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1811
1812/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01001813MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001814MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1815MODULE_LICENSE("GPL");