blob: a35ce69d9d78810f505747607fd55f92690e95d8 [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[] = {
55 snd_soc_dapm_pre, snd_soc_dapm_micbias, snd_soc_dapm_mic,
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +020056 snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_dac,
Ian Moltonca9c1aa2009-01-06 20:11:51 +000057 snd_soc_dapm_mixer, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_pga,
58 snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, snd_soc_dapm_post
Richard Purdie2b97eab2006-10-06 18:32:18 +020059};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000060
Richard Purdie2b97eab2006-10-06 18:32:18 +020061static int dapm_down_seq[] = {
62 snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
Ian Moltonca9c1aa2009-01-06 20:11:51 +000063 snd_soc_dapm_pga, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_mixer,
64 snd_soc_dapm_dac, snd_soc_dapm_mic, snd_soc_dapm_micbias,
65 snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_post
Richard Purdie2b97eab2006-10-06 18:32:18 +020066};
67
68static int dapm_status = 1;
69module_param(dapm_status, int, 0);
70MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
71
Troy Kisky12ef1932008-10-13 17:42:14 -070072static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010073{
74 if (pop_time)
75 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
76}
77
Troy Kisky12ef1932008-10-13 17:42:14 -070078static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010079{
80 va_list args;
81
82 va_start(args, fmt);
83
84 if (pop_time) {
85 vprintk(fmt, args);
Troy Kisky12ef1932008-10-13 17:42:14 -070086 pop_wait(pop_time);
Mark Brown15e4c722008-07-02 11:51:20 +010087 }
88
89 va_end(args);
90}
91
Richard Purdie2b97eab2006-10-06 18:32:18 +020092/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +010093static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +020094 const struct snd_soc_dapm_widget *_widget)
95{
Takashi Iwai88cb4292007-02-05 14:56:20 +010096 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +020097}
98
99/* set up initial codec paths */
100static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
101 struct snd_soc_dapm_path *p, int i)
102{
103 switch (w->id) {
104 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000105 case snd_soc_dapm_mixer:
106 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200107 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100108 struct soc_mixer_control *mc = (struct soc_mixer_control *)
109 w->kcontrols[i].private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400110 unsigned int reg = mc->reg;
111 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100112 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400113 unsigned int mask = (1 << fls(max)) - 1;
114 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200115
116 val = snd_soc_read(w->codec, reg);
117 val = (val >> shift) & mask;
118
119 if ((invert && !val) || (!invert && val))
120 p->connect = 1;
121 else
122 p->connect = 0;
123 }
124 break;
125 case snd_soc_dapm_mux: {
126 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
127 int val, item, bitmask;
128
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100129 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200130 ;
131 val = snd_soc_read(w->codec, e->reg);
132 item = (val >> e->shift_l) & (bitmask - 1);
133
134 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100135 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200136 if (!(strcmp(p->name, e->texts[i])) && item == i)
137 p->connect = 1;
138 }
139 }
140 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200141 case snd_soc_dapm_value_mux: {
142 struct soc_value_enum *e = (struct soc_value_enum *)
143 w->kcontrols[i].private_value;
144 int val, item;
145
146 val = snd_soc_read(w->codec, e->reg);
147 val = (val >> e->shift_l) & e->mask;
148 for (item = 0; item < e->max; item++) {
149 if (val == e->values[item])
150 break;
151 }
152
153 p->connect = 0;
154 for (i = 0; i < e->max; i++) {
155 if (!(strcmp(p->name, e->texts[i])) && item == i)
156 p->connect = 1;
157 }
158 }
159 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200160 /* does not effect routing - always connected */
161 case snd_soc_dapm_pga:
162 case snd_soc_dapm_output:
163 case snd_soc_dapm_adc:
164 case snd_soc_dapm_input:
165 case snd_soc_dapm_dac:
166 case snd_soc_dapm_micbias:
167 case snd_soc_dapm_vmid:
168 p->connect = 1;
169 break;
170 /* does effect routing - dynamically connected */
171 case snd_soc_dapm_hp:
172 case snd_soc_dapm_mic:
173 case snd_soc_dapm_spk:
174 case snd_soc_dapm_line:
175 case snd_soc_dapm_pre:
176 case snd_soc_dapm_post:
177 p->connect = 0;
178 break;
179 }
180}
181
182/* connect mux widget to it's interconnecting audio paths */
183static int dapm_connect_mux(struct snd_soc_codec *codec,
184 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
185 struct snd_soc_dapm_path *path, const char *control_name,
186 const struct snd_kcontrol_new *kcontrol)
187{
188 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
189 int i;
190
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100191 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200192 if (!(strcmp(control_name, e->texts[i]))) {
193 list_add(&path->list, &codec->dapm_paths);
194 list_add(&path->list_sink, &dest->sources);
195 list_add(&path->list_source, &src->sinks);
196 path->name = (char*)e->texts[i];
197 dapm_set_path_status(dest, path, 0);
198 return 0;
199 }
200 }
201
202 return -ENODEV;
203}
204
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200205/* connect value_mux widget to it's interconnecting audio paths */
206static int dapm_connect_value_mux(struct snd_soc_codec *codec,
207 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
208 struct snd_soc_dapm_path *path, const char *control_name,
209 const struct snd_kcontrol_new *kcontrol)
210{
211 struct soc_value_enum *e = (struct soc_value_enum *)
212 kcontrol->private_value;
213 int i;
214
215 for (i = 0; i < e->max; i++) {
216 if (!(strcmp(control_name, e->texts[i]))) {
217 list_add(&path->list, &codec->dapm_paths);
218 list_add(&path->list_sink, &dest->sources);
219 list_add(&path->list_source, &src->sinks);
220 path->name = (char *)e->texts[i];
221 dapm_set_path_status(dest, path, 0);
222 return 0;
223 }
224 }
225
226 return -ENODEV;
227}
228
Richard Purdie2b97eab2006-10-06 18:32:18 +0200229/* connect mixer widget to it's interconnecting audio paths */
230static int dapm_connect_mixer(struct snd_soc_codec *codec,
231 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
232 struct snd_soc_dapm_path *path, const char *control_name)
233{
234 int i;
235
236 /* search for mixer kcontrol */
237 for (i = 0; i < dest->num_kcontrols; i++) {
238 if (!strcmp(control_name, dest->kcontrols[i].name)) {
239 list_add(&path->list, &codec->dapm_paths);
240 list_add(&path->list_sink, &dest->sources);
241 list_add(&path->list_source, &src->sinks);
242 path->name = dest->kcontrols[i].name;
243 dapm_set_path_status(dest, path, i);
244 return 0;
245 }
246 }
247 return -ENODEV;
248}
249
250/* update dapm codec register bits */
251static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
252{
253 int change, power;
254 unsigned short old, new;
255 struct snd_soc_codec *codec = widget->codec;
256
257 /* check for valid widgets */
258 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
259 widget->id == snd_soc_dapm_output ||
260 widget->id == snd_soc_dapm_hp ||
261 widget->id == snd_soc_dapm_mic ||
262 widget->id == snd_soc_dapm_line ||
263 widget->id == snd_soc_dapm_spk)
264 return 0;
265
266 power = widget->power;
267 if (widget->invert)
268 power = (power ? 0:1);
269
270 old = snd_soc_read(codec, widget->reg);
271 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
272
273 change = old != new;
274 if (change) {
Troy Kisky12ef1932008-10-13 17:42:14 -0700275 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
276 widget->name, widget->power ? "on" : "off",
277 codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200278 snd_soc_write(codec, widget->reg, new);
Troy Kisky12ef1932008-10-13 17:42:14 -0700279 pop_wait(codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200280 }
Mark Brownc1286b82008-07-07 19:26:03 +0100281 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
282 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200283 return change;
284}
285
286/* ramps the volume up or down to minimise pops before or after a
287 * DAPM power event */
288static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
289{
290 const struct snd_kcontrol_new *k = widget->kcontrols;
291
292 if (widget->muted && !power)
293 return 0;
294 if (!widget->muted && power)
295 return 0;
296
297 if (widget->num_kcontrols && k) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100298 struct soc_mixer_control *mc =
299 (struct soc_mixer_control *)k->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400300 unsigned int reg = mc->reg;
301 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100302 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -0400303 unsigned int mask = (1 << fls(max)) - 1;
304 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200305
306 if (power) {
307 int i;
308 /* power up has happended, increase volume to last level */
309 if (invert) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100310 for (i = max; i > widget->saved_value; i--)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200311 snd_soc_update_bits(widget->codec, reg, mask, i);
312 } else {
313 for (i = 0; i < widget->saved_value; i++)
314 snd_soc_update_bits(widget->codec, reg, mask, i);
315 }
316 widget->muted = 0;
317 } else {
318 /* power down is about to occur, decrease volume to mute */
319 int val = snd_soc_read(widget->codec, reg);
320 int i = widget->saved_value = (val >> shift) & mask;
321 if (invert) {
322 for (; i < mask; i++)
323 snd_soc_update_bits(widget->codec, reg, mask, i);
324 } else {
325 for (; i > 0; i--)
326 snd_soc_update_bits(widget->codec, reg, mask, i);
327 }
328 widget->muted = 1;
329 }
330 }
331 return 0;
332}
333
334/* create new dapm mixer control */
335static int dapm_new_mixer(struct snd_soc_codec *codec,
336 struct snd_soc_dapm_widget *w)
337{
338 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000339 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200340 struct snd_soc_dapm_path *path;
341
342 /* add kcontrol */
343 for (i = 0; i < w->num_kcontrols; i++) {
344
345 /* match name */
346 list_for_each_entry(path, &w->sources, list_sink) {
347
348 /* mixer/mux paths name must match control name */
349 if (path->name != (char*)w->kcontrols[i].name)
350 continue;
351
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000352 /* add dapm control with long name.
353 * for dapm_mixer this is the concatenation of the
354 * mixer and kcontrol name.
355 * for dapm_mixer_named_ctl this is simply the
356 * kcontrol name.
357 */
358 name_len = strlen(w->kcontrols[i].name) + 1;
359 if (w->id == snd_soc_dapm_mixer)
360 name_len += 1 + strlen(w->name);
361
Mark Brown219b93f2008-10-28 13:02:31 +0000362 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000363
Richard Purdie2b97eab2006-10-06 18:32:18 +0200364 if (path->long_name == NULL)
365 return -ENOMEM;
366
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000367 switch (w->id) {
368 case snd_soc_dapm_mixer:
369 default:
370 snprintf(path->long_name, name_len, "%s %s",
371 w->name, w->kcontrols[i].name);
372 break;
373 case snd_soc_dapm_mixer_named_ctl:
374 snprintf(path->long_name, name_len, "%s",
375 w->kcontrols[i].name);
376 break;
377 }
378
Mark Brown219b93f2008-10-28 13:02:31 +0000379 path->long_name[name_len - 1] = '\0';
380
Richard Purdie2b97eab2006-10-06 18:32:18 +0200381 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
382 path->long_name);
383 ret = snd_ctl_add(codec->card, path->kcontrol);
384 if (ret < 0) {
385 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s\n",
386 path->long_name);
387 kfree(path->long_name);
388 path->long_name = NULL;
389 return ret;
390 }
391 }
392 }
393 return ret;
394}
395
396/* create new dapm mux control */
397static int dapm_new_mux(struct snd_soc_codec *codec,
398 struct snd_soc_dapm_widget *w)
399{
400 struct snd_soc_dapm_path *path = NULL;
401 struct snd_kcontrol *kcontrol;
402 int ret = 0;
403
404 if (!w->num_kcontrols) {
405 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
406 return -EINVAL;
407 }
408
409 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
410 ret = snd_ctl_add(codec->card, kcontrol);
411 if (ret < 0)
412 goto err;
413
414 list_for_each_entry(path, &w->sources, list_sink)
415 path->kcontrol = kcontrol;
416
417 return ret;
418
419err:
420 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
421 return ret;
422}
423
424/* create new dapm volume control */
425static int dapm_new_pga(struct snd_soc_codec *codec,
426 struct snd_soc_dapm_widget *w)
427{
428 struct snd_kcontrol *kcontrol;
429 int ret = 0;
430
431 if (!w->num_kcontrols)
432 return -EINVAL;
433
434 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
435 ret = snd_ctl_add(codec->card, kcontrol);
436 if (ret < 0) {
437 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
438 return ret;
439 }
440
441 return ret;
442}
443
444/* reset 'walked' bit for each dapm path */
445static inline void dapm_clear_walk(struct snd_soc_codec *codec)
446{
447 struct snd_soc_dapm_path *p;
448
449 list_for_each_entry(p, &codec->dapm_paths, list)
450 p->walked = 0;
451}
452
453/*
454 * Recursively check for a completed path to an active or physically connected
455 * output widget. Returns number of complete paths.
456 */
457static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
458{
459 struct snd_soc_dapm_path *path;
460 int con = 0;
461
462 if (widget->id == snd_soc_dapm_adc && widget->active)
463 return 1;
464
465 if (widget->connected) {
466 /* connected pin ? */
467 if (widget->id == snd_soc_dapm_output && !widget->ext)
468 return 1;
469
470 /* connected jack or spk ? */
471 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
472 widget->id == snd_soc_dapm_line)
473 return 1;
474 }
475
476 list_for_each_entry(path, &widget->sinks, list_source) {
477 if (path->walked)
478 continue;
479
480 if (path->sink && path->connect) {
481 path->walked = 1;
482 con += is_connected_output_ep(path->sink);
483 }
484 }
485
486 return con;
487}
488
489/*
490 * Recursively check for a completed path to an active or physically connected
491 * input widget. Returns number of complete paths.
492 */
493static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
494{
495 struct snd_soc_dapm_path *path;
496 int con = 0;
497
498 /* active stream ? */
499 if (widget->id == snd_soc_dapm_dac && widget->active)
500 return 1;
501
502 if (widget->connected) {
503 /* connected pin ? */
504 if (widget->id == snd_soc_dapm_input && !widget->ext)
505 return 1;
506
507 /* connected VMID/Bias for lower pops */
508 if (widget->id == snd_soc_dapm_vmid)
509 return 1;
510
511 /* connected jack ? */
512 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
513 return 1;
514 }
515
516 list_for_each_entry(path, &widget->sources, list_sink) {
517 if (path->walked)
518 continue;
519
520 if (path->source && path->connect) {
521 path->walked = 1;
522 con += is_connected_input_ep(path->source);
523 }
524 }
525
526 return con;
527}
528
529/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300530 * Handler for generic register modifier widget.
531 */
532int dapm_reg_event(struct snd_soc_dapm_widget *w,
533 struct snd_kcontrol *kcontrol, int event)
534{
535 unsigned int val;
536
537 if (SND_SOC_DAPM_EVENT_ON(event))
538 val = w->on_val;
539 else
540 val = w->off_val;
541
542 snd_soc_update_bits(w->codec, -(w->reg + 1),
543 w->mask << w->shift, val << w->shift);
544
545 return 0;
546}
Mark Brown11589412008-07-29 11:42:23 +0100547EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300548
549/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200550 * Scan each dapm widget for complete audio path.
551 * A complete path is a route that has valid endpoints i.e.:-
552 *
553 * o DAC to output pin.
554 * o Input Pin to ADC.
555 * o Input pin to Output pin (bypass, sidetone)
556 * o DAC to ADC (loopback).
557 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100558static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200559{
560 struct snd_soc_dapm_widget *w;
561 int in, out, i, c = 1, *seq = NULL, ret = 0, power_change, power;
562
563 /* do we have a sequenced stream event */
564 if (event == SND_SOC_DAPM_STREAM_START) {
565 c = ARRAY_SIZE(dapm_up_seq);
566 seq = dapm_up_seq;
567 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
568 c = ARRAY_SIZE(dapm_down_seq);
569 seq = dapm_down_seq;
570 }
571
572 for(i = 0; i < c; i++) {
573 list_for_each_entry(w, &codec->dapm_widgets, list) {
574
575 /* is widget in stream order */
576 if (seq && seq[i] && w->id != seq[i])
577 continue;
578
579 /* vmid - no action */
580 if (w->id == snd_soc_dapm_vmid)
581 continue;
582
583 /* active ADC */
584 if (w->id == snd_soc_dapm_adc && w->active) {
585 in = is_connected_input_ep(w);
586 dapm_clear_walk(w->codec);
587 w->power = (in != 0) ? 1 : 0;
588 dapm_update_bits(w);
589 continue;
590 }
591
592 /* active DAC */
593 if (w->id == snd_soc_dapm_dac && w->active) {
594 out = is_connected_output_ep(w);
595 dapm_clear_walk(w->codec);
596 w->power = (out != 0) ? 1 : 0;
597 dapm_update_bits(w);
598 continue;
599 }
600
Richard Purdie2b97eab2006-10-06 18:32:18 +0200601 /* pre and post event widgets */
602 if (w->id == snd_soc_dapm_pre) {
603 if (!w->event)
604 continue;
605
606 if (event == SND_SOC_DAPM_STREAM_START) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100607 ret = w->event(w,
608 NULL, SND_SOC_DAPM_PRE_PMU);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200609 if (ret < 0)
610 return ret;
611 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100612 ret = w->event(w,
613 NULL, SND_SOC_DAPM_PRE_PMD);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200614 if (ret < 0)
615 return ret;
616 }
617 continue;
618 }
619 if (w->id == snd_soc_dapm_post) {
620 if (!w->event)
621 continue;
622
623 if (event == SND_SOC_DAPM_STREAM_START) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100624 ret = w->event(w,
625 NULL, SND_SOC_DAPM_POST_PMU);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200626 if (ret < 0)
627 return ret;
628 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100629 ret = w->event(w,
630 NULL, SND_SOC_DAPM_POST_PMD);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200631 if (ret < 0)
632 return ret;
633 }
634 continue;
635 }
636
637 /* all other widgets */
638 in = is_connected_input_ep(w);
639 dapm_clear_walk(w->codec);
640 out = is_connected_output_ep(w);
641 dapm_clear_walk(w->codec);
642 power = (out != 0 && in != 0) ? 1 : 0;
643 power_change = (w->power == power) ? 0: 1;
644 w->power = power;
645
Mark Brown2927d6e2008-07-17 15:06:50 +0100646 if (!power_change)
647 continue;
648
Richard Purdie2b97eab2006-10-06 18:32:18 +0200649 /* call any power change event handlers */
Mark Brown2927d6e2008-07-17 15:06:50 +0100650 if (w->event)
651 pr_debug("power %s event for %s flags %x\n",
652 w->power ? "on" : "off",
653 w->name, w->event_flags);
654
655 /* power up pre event */
656 if (power && w->event &&
657 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
658 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
659 if (ret < 0)
660 return ret;
661 }
662
663 /* power down pre event */
664 if (!power && w->event &&
665 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
666 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
667 if (ret < 0)
668 return ret;
669 }
670
Mark Brown9dd8d812008-07-17 15:06:51 +0100671 /* Lower PGA volume to reduce pops */
672 if (w->id == snd_soc_dapm_pga && !power)
673 dapm_set_pga(w, power);
674
Mark Brown2927d6e2008-07-17 15:06:50 +0100675 dapm_update_bits(w);
676
Mark Brown9dd8d812008-07-17 15:06:51 +0100677 /* Raise PGA volume to reduce pops */
678 if (w->id == snd_soc_dapm_pga && power)
679 dapm_set_pga(w, power);
680
Mark Brown2927d6e2008-07-17 15:06:50 +0100681 /* power up post event */
682 if (power && w->event &&
683 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
684 ret = w->event(w,
685 NULL, SND_SOC_DAPM_POST_PMU);
686 if (ret < 0)
687 return ret;
688 }
689
690 /* power down post event */
691 if (!power && w->event &&
692 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
693 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
694 if (ret < 0)
695 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200696 }
697 }
698 }
699
700 return ret;
701}
702
Mark Brownc1286b82008-07-07 19:26:03 +0100703#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +0200704static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
705{
706 struct snd_soc_dapm_widget *w;
707 struct snd_soc_dapm_path *p = NULL;
708 int in, out;
709
710 printk("DAPM %s %s\n", codec->name, action);
711
712 list_for_each_entry(w, &codec->dapm_widgets, list) {
713
714 /* only display widgets that effect routing */
715 switch (w->id) {
716 case snd_soc_dapm_pre:
717 case snd_soc_dapm_post:
718 case snd_soc_dapm_vmid:
719 continue;
720 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200721 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200722 case snd_soc_dapm_output:
723 case snd_soc_dapm_input:
724 case snd_soc_dapm_switch:
725 case snd_soc_dapm_hp:
726 case snd_soc_dapm_mic:
727 case snd_soc_dapm_spk:
728 case snd_soc_dapm_line:
729 case snd_soc_dapm_micbias:
730 case snd_soc_dapm_dac:
731 case snd_soc_dapm_adc:
732 case snd_soc_dapm_pga:
733 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000734 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200735 if (w->name) {
736 in = is_connected_input_ep(w);
737 dapm_clear_walk(w->codec);
738 out = is_connected_output_ep(w);
739 dapm_clear_walk(w->codec);
740 printk("%s: %s in %d out %d\n", w->name,
741 w->power ? "On":"Off",in, out);
742
743 list_for_each_entry(p, &w->sources, list_sink) {
744 if (p->connect)
745 printk(" in %s %s\n", p->name ? p->name : "static",
746 p->source->name);
747 }
748 list_for_each_entry(p, &w->sinks, list_source) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200749 if (p->connect)
750 printk(" out %s %s\n", p->name ? p->name : "static",
751 p->sink->name);
752 }
753 }
754 break;
755 }
756 }
757}
758#endif
759
760/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100761static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
762 struct snd_kcontrol *kcontrol, int mask,
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800763 int mux, int val, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200764{
765 struct snd_soc_dapm_path *path;
766 int found = 0;
767
768 if (widget->id != snd_soc_dapm_mux)
769 return -ENODEV;
770
771 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
772 return 0;
773
774 /* find dapm widget path assoc with kcontrol */
775 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
776 if (path->kcontrol != kcontrol)
777 continue;
778
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800779 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +0200780 continue;
781
782 found = 1;
783 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800784 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +0200785 path->connect = 1; /* new connection */
786 else
787 path->connect = 0; /* old connection must be powered down */
788 }
789
Mark Brown3fccd8b2008-07-07 19:26:04 +0100790 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200791 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100792 dump_dapm(widget->codec, "mux power update");
793 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200794
795 return 0;
796}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200797
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200798/* test and update the power status of a value_mux widget */
799static int dapm_value_mux_update_power(struct snd_soc_dapm_widget *widget,
800 struct snd_kcontrol *kcontrol, int mask,
801 int mux, int val, struct soc_value_enum *e)
802{
803 struct snd_soc_dapm_path *path;
804 int found = 0;
805
806 if (widget->id != snd_soc_dapm_value_mux)
807 return -ENODEV;
808
809 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
810 return 0;
811
812 /* find dapm widget path assoc with kcontrol */
813 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
814 if (path->kcontrol != kcontrol)
815 continue;
816
817 if (!path->name || !e->texts[mux])
818 continue;
819
820 found = 1;
821 /* we now need to match the string in the enum to the path */
822 if (!(strcmp(path->name, e->texts[mux])))
823 path->connect = 1; /* new connection */
824 else
825 path->connect = 0; /* old connection must be
826 powered down */
827 }
828
829 if (found) {
830 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
831 dump_dapm(widget->codec, "mux power update");
832 }
833
834 return 0;
835}
836
Milan plzik1b075e32008-01-10 14:39:46 +0100837/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100838static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
839 struct snd_kcontrol *kcontrol, int reg,
840 int val_mask, int val, int invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200841{
842 struct snd_soc_dapm_path *path;
843 int found = 0;
844
Milan plzik1b075e32008-01-10 14:39:46 +0100845 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000846 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +0100847 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200848 return -ENODEV;
849
850 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
851 return 0;
852
853 /* find dapm widget path assoc with kcontrol */
854 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
855 if (path->kcontrol != kcontrol)
856 continue;
857
858 /* found, now check type */
859 found = 1;
860 if (val)
861 /* new connection */
862 path->connect = invert ? 0:1;
863 else
864 /* old connection must be powered down */
865 path->connect = invert ? 1:0;
866 break;
867 }
868
Mark Brown3fccd8b2008-07-07 19:26:04 +0100869 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200870 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100871 dump_dapm(widget->codec, "mixer power update");
872 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200873
874 return 0;
875}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200876
877/* show dapm widget status in sys fs */
878static ssize_t dapm_widget_show(struct device *dev,
879 struct device_attribute *attr, char *buf)
880{
881 struct snd_soc_device *devdata = dev_get_drvdata(dev);
882 struct snd_soc_codec *codec = devdata->codec;
883 struct snd_soc_dapm_widget *w;
884 int count = 0;
885 char *state = "not set";
886
887 list_for_each_entry(w, &codec->dapm_widgets, list) {
888
889 /* only display widgets that burnm power */
890 switch (w->id) {
891 case snd_soc_dapm_hp:
892 case snd_soc_dapm_mic:
893 case snd_soc_dapm_spk:
894 case snd_soc_dapm_line:
895 case snd_soc_dapm_micbias:
896 case snd_soc_dapm_dac:
897 case snd_soc_dapm_adc:
898 case snd_soc_dapm_pga:
899 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000900 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200901 if (w->name)
902 count += sprintf(buf + count, "%s: %s\n",
903 w->name, w->power ? "On":"Off");
904 break;
905 default:
906 break;
907 }
908 }
909
Mark Brown0be98982008-05-19 12:31:28 +0200910 switch (codec->bias_level) {
911 case SND_SOC_BIAS_ON:
912 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200913 break;
Mark Brown0be98982008-05-19 12:31:28 +0200914 case SND_SOC_BIAS_PREPARE:
915 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200916 break;
Mark Brown0be98982008-05-19 12:31:28 +0200917 case SND_SOC_BIAS_STANDBY:
918 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200919 break;
Mark Brown0be98982008-05-19 12:31:28 +0200920 case SND_SOC_BIAS_OFF:
921 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200922 break;
923 }
924 count += sprintf(buf + count, "PM State: %s\n", state);
925
926 return count;
927}
928
929static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
930
931int snd_soc_dapm_sys_add(struct device *dev)
932{
Mark Brownf0062a92008-08-28 12:46:24 +0100933 if (!dapm_status)
934 return 0;
Troy Kisky12ef1932008-10-13 17:42:14 -0700935 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200936}
937
938static void snd_soc_dapm_sys_remove(struct device *dev)
939{
Mark Brown15e4c722008-07-02 11:51:20 +0100940 if (dapm_status) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200941 device_remove_file(dev, &dev_attr_dapm_widget);
Mark Brown15e4c722008-07-02 11:51:20 +0100942 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200943}
944
945/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100946static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200947{
948 struct snd_soc_dapm_widget *w, *next_w;
949 struct snd_soc_dapm_path *p, *next_p;
950
951 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
952 list_del(&w->list);
953 kfree(w);
954 }
955
956 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
957 list_del(&p->list);
958 kfree(p->long_name);
959 kfree(p);
960 }
961}
962
Liam Girdwooda5302182008-07-07 13:35:17 +0100963static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +0000964 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +0100965{
966 struct snd_soc_dapm_widget *w;
967
968 list_for_each_entry(w, &codec->dapm_widgets, list) {
969 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +0100970 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +0100971 w->connected = status;
972 return 0;
973 }
974 }
975
Mark Brownc1286b82008-07-07 19:26:03 +0100976 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +0100977 return -EINVAL;
978}
979
Richard Purdie2b97eab2006-10-06 18:32:18 +0200980/**
Liam Girdwooda5302182008-07-07 13:35:17 +0100981 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +0200982 * @codec: audio codec
983 *
984 * Walks all dapm audio paths and powers widgets according to their
985 * stream or path usage.
986 *
987 * Returns 0 for success.
988 */
Liam Girdwooda5302182008-07-07 13:35:17 +0100989int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200990{
Mark Brown3fccd8b2008-07-07 19:26:04 +0100991 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
992 dump_dapm(codec, "sync");
993 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200994}
Liam Girdwooda5302182008-07-07 13:35:17 +0100995EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200996
Mark Brown105f1c22008-05-13 14:52:19 +0200997static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
998 const char *sink, const char *control, const char *source)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200999{
1000 struct snd_soc_dapm_path *path;
1001 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1002 int ret = 0;
1003
1004 /* find src and dest widgets */
1005 list_for_each_entry(w, &codec->dapm_widgets, list) {
1006
1007 if (!wsink && !(strcmp(w->name, sink))) {
1008 wsink = w;
1009 continue;
1010 }
1011 if (!wsource && !(strcmp(w->name, source))) {
1012 wsource = w;
1013 }
1014 }
1015
1016 if (wsource == NULL || wsink == NULL)
1017 return -ENODEV;
1018
1019 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1020 if (!path)
1021 return -ENOMEM;
1022
1023 path->source = wsource;
1024 path->sink = wsink;
1025 INIT_LIST_HEAD(&path->list);
1026 INIT_LIST_HEAD(&path->list_source);
1027 INIT_LIST_HEAD(&path->list_sink);
1028
1029 /* check for external widgets */
1030 if (wsink->id == snd_soc_dapm_input) {
1031 if (wsource->id == snd_soc_dapm_micbias ||
1032 wsource->id == snd_soc_dapm_mic ||
Seth Forshee1e392212007-04-16 15:36:42 +02001033 wsink->id == snd_soc_dapm_line ||
1034 wsink->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001035 wsink->ext = 1;
1036 }
1037 if (wsource->id == snd_soc_dapm_output) {
1038 if (wsink->id == snd_soc_dapm_spk ||
1039 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001040 wsink->id == snd_soc_dapm_line ||
1041 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001042 wsource->ext = 1;
1043 }
1044
1045 /* connect static paths */
1046 if (control == NULL) {
1047 list_add(&path->list, &codec->dapm_paths);
1048 list_add(&path->list_sink, &wsink->sources);
1049 list_add(&path->list_source, &wsource->sinks);
1050 path->connect = 1;
1051 return 0;
1052 }
1053
1054 /* connect dynamic paths */
1055 switch(wsink->id) {
1056 case snd_soc_dapm_adc:
1057 case snd_soc_dapm_dac:
1058 case snd_soc_dapm_pga:
1059 case snd_soc_dapm_input:
1060 case snd_soc_dapm_output:
1061 case snd_soc_dapm_micbias:
1062 case snd_soc_dapm_vmid:
1063 case snd_soc_dapm_pre:
1064 case snd_soc_dapm_post:
1065 list_add(&path->list, &codec->dapm_paths);
1066 list_add(&path->list_sink, &wsink->sources);
1067 list_add(&path->list_source, &wsource->sinks);
1068 path->connect = 1;
1069 return 0;
1070 case snd_soc_dapm_mux:
1071 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1072 &wsink->kcontrols[0]);
1073 if (ret != 0)
1074 goto err;
1075 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001076 case snd_soc_dapm_value_mux:
1077 ret = dapm_connect_value_mux(codec, wsource, wsink, path,
1078 control, &wsink->kcontrols[0]);
1079 if (ret != 0)
1080 goto err;
1081 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001082 case snd_soc_dapm_switch:
1083 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001084 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001085 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1086 if (ret != 0)
1087 goto err;
1088 break;
1089 case snd_soc_dapm_hp:
1090 case snd_soc_dapm_mic:
1091 case snd_soc_dapm_line:
1092 case snd_soc_dapm_spk:
1093 list_add(&path->list, &codec->dapm_paths);
1094 list_add(&path->list_sink, &wsink->sources);
1095 list_add(&path->list_source, &wsource->sinks);
1096 path->connect = 0;
1097 return 0;
1098 }
1099 return 0;
1100
1101err:
1102 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1103 control, sink);
1104 kfree(path);
1105 return ret;
1106}
Mark Brown105f1c22008-05-13 14:52:19 +02001107
1108/**
Mark Brown105f1c22008-05-13 14:52:19 +02001109 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1110 * @codec: codec
1111 * @route: audio routes
1112 * @num: number of routes
1113 *
1114 * Connects 2 dapm widgets together via a named audio path. The sink is
1115 * the widget receiving the audio signal, whilst the source is the sender
1116 * of the audio signal.
1117 *
1118 * Returns 0 for success else error. On error all resources can be freed
1119 * with a call to snd_soc_card_free().
1120 */
1121int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1122 const struct snd_soc_dapm_route *route, int num)
1123{
1124 int i, ret;
1125
1126 for (i = 0; i < num; i++) {
1127 ret = snd_soc_dapm_add_route(codec, route->sink,
1128 route->control, route->source);
1129 if (ret < 0) {
1130 printk(KERN_ERR "Failed to add route %s->%s\n",
1131 route->source,
1132 route->sink);
1133 return ret;
1134 }
1135 route++;
1136 }
1137
1138 return 0;
1139}
1140EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1141
1142/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001143 * snd_soc_dapm_new_widgets - add new dapm widgets
1144 * @codec: audio codec
1145 *
1146 * Checks the codec for any new dapm widgets and creates them if found.
1147 *
1148 * Returns 0 for success.
1149 */
1150int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1151{
1152 struct snd_soc_dapm_widget *w;
1153
Richard Purdie2b97eab2006-10-06 18:32:18 +02001154 list_for_each_entry(w, &codec->dapm_widgets, list)
1155 {
1156 if (w->new)
1157 continue;
1158
1159 switch(w->id) {
1160 case snd_soc_dapm_switch:
1161 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001162 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001163 dapm_new_mixer(codec, w);
1164 break;
1165 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001166 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001167 dapm_new_mux(codec, w);
1168 break;
1169 case snd_soc_dapm_adc:
1170 case snd_soc_dapm_dac:
1171 case snd_soc_dapm_pga:
1172 dapm_new_pga(codec, w);
1173 break;
1174 case snd_soc_dapm_input:
1175 case snd_soc_dapm_output:
1176 case snd_soc_dapm_micbias:
1177 case snd_soc_dapm_spk:
1178 case snd_soc_dapm_hp:
1179 case snd_soc_dapm_mic:
1180 case snd_soc_dapm_line:
1181 case snd_soc_dapm_vmid:
1182 case snd_soc_dapm_pre:
1183 case snd_soc_dapm_post:
1184 break;
1185 }
1186 w->new = 1;
1187 }
1188
1189 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001190 return 0;
1191}
1192EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1193
1194/**
1195 * snd_soc_dapm_get_volsw - dapm mixer get callback
1196 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001197 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001198 *
1199 * Callback to get the value of a dapm mixer control.
1200 *
1201 * Returns 0 for success.
1202 */
1203int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1204 struct snd_ctl_elem_value *ucontrol)
1205{
1206 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001207 struct soc_mixer_control *mc =
1208 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001209 unsigned int reg = mc->reg;
1210 unsigned int shift = mc->shift;
1211 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001212 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001213 unsigned int invert = mc->invert;
1214 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001215
1216 /* return the saved value if we are powered down */
1217 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1218 ucontrol->value.integer.value[0] = widget->saved_value;
1219 return 0;
1220 }
1221
1222 ucontrol->value.integer.value[0] =
1223 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1224 if (shift != rshift)
1225 ucontrol->value.integer.value[1] =
1226 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1227 if (invert) {
1228 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001229 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001230 if (shift != rshift)
1231 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001232 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001233 }
1234
1235 return 0;
1236}
1237EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1238
1239/**
1240 * snd_soc_dapm_put_volsw - dapm mixer set callback
1241 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001242 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001243 *
1244 * Callback to set the value of a dapm mixer control.
1245 *
1246 * Returns 0 for success.
1247 */
1248int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1249 struct snd_ctl_elem_value *ucontrol)
1250{
1251 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001252 struct soc_mixer_control *mc =
1253 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001254 unsigned int reg = mc->reg;
1255 unsigned int shift = mc->shift;
1256 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001257 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001258 unsigned int mask = (1 << fls(max)) - 1;
1259 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001260 unsigned short val, val2, val_mask;
1261 int ret;
1262
1263 val = (ucontrol->value.integer.value[0] & mask);
1264
1265 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001266 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001267 val_mask = mask << shift;
1268 val = val << shift;
1269 if (shift != rshift) {
1270 val2 = (ucontrol->value.integer.value[1] & mask);
1271 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001272 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001273 val_mask |= mask << rshift;
1274 val |= val2 << rshift;
1275 }
1276
1277 mutex_lock(&widget->codec->mutex);
1278 widget->value = val;
1279
1280 /* save volume value if the widget is powered down */
1281 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1282 widget->saved_value = val;
1283 mutex_unlock(&widget->codec->mutex);
1284 return 1;
1285 }
1286
1287 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1288 if (widget->event) {
1289 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001290 ret = widget->event(widget, kcontrol,
1291 SND_SOC_DAPM_PRE_REG);
1292 if (ret < 0) {
1293 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001294 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001295 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001296 }
1297 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1298 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001299 ret = widget->event(widget, kcontrol,
1300 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001301 } else
1302 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1303
1304out:
1305 mutex_unlock(&widget->codec->mutex);
1306 return ret;
1307}
1308EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1309
1310/**
1311 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1312 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001313 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001314 *
1315 * Callback to get the value of a dapm enumerated double mixer control.
1316 *
1317 * Returns 0 for success.
1318 */
1319int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1320 struct snd_ctl_elem_value *ucontrol)
1321{
1322 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1323 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1324 unsigned short val, bitmask;
1325
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001326 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001327 ;
1328 val = snd_soc_read(widget->codec, e->reg);
1329 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1330 if (e->shift_l != e->shift_r)
1331 ucontrol->value.enumerated.item[1] =
1332 (val >> e->shift_r) & (bitmask - 1);
1333
1334 return 0;
1335}
1336EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1337
1338/**
1339 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1340 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001341 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001342 *
1343 * Callback to set the value of a dapm enumerated double mixer control.
1344 *
1345 * Returns 0 for success.
1346 */
1347int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1348 struct snd_ctl_elem_value *ucontrol)
1349{
1350 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1351 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1352 unsigned short val, mux;
1353 unsigned short mask, bitmask;
1354 int ret = 0;
1355
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001356 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001357 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001358 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001359 return -EINVAL;
1360 mux = ucontrol->value.enumerated.item[0];
1361 val = mux << e->shift_l;
1362 mask = (bitmask - 1) << e->shift_l;
1363 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001364 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001365 return -EINVAL;
1366 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1367 mask |= (bitmask - 1) << e->shift_r;
1368 }
1369
1370 mutex_lock(&widget->codec->mutex);
1371 widget->value = val;
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001372 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001373 if (widget->event) {
1374 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001375 ret = widget->event(widget,
1376 kcontrol, SND_SOC_DAPM_PRE_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001377 if (ret < 0)
1378 goto out;
1379 }
1380 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1381 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001382 ret = widget->event(widget,
1383 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001384 } else
1385 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1386
1387out:
1388 mutex_unlock(&widget->codec->mutex);
1389 return ret;
1390}
1391EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1392
1393/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001394 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1395 * callback
1396 * @kcontrol: mixer control
1397 * @ucontrol: control element information
1398 *
1399 * Callback to get the value of a dapm semi enumerated double mixer control.
1400 *
1401 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1402 * used for handling bitfield coded enumeration for example.
1403 *
1404 * Returns 0 for success.
1405 */
1406int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1407 struct snd_ctl_elem_value *ucontrol)
1408{
1409 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1410 struct soc_value_enum *e = (struct soc_value_enum *)
1411 kcontrol->private_value;
1412 unsigned short reg_val, val, mux;
1413
1414 reg_val = snd_soc_read(widget->codec, e->reg);
1415 val = (reg_val >> e->shift_l) & e->mask;
1416 for (mux = 0; mux < e->max; mux++) {
1417 if (val == e->values[mux])
1418 break;
1419 }
1420 ucontrol->value.enumerated.item[0] = mux;
1421 if (e->shift_l != e->shift_r) {
1422 val = (reg_val >> e->shift_r) & e->mask;
1423 for (mux = 0; mux < e->max; mux++) {
1424 if (val == e->values[mux])
1425 break;
1426 }
1427 ucontrol->value.enumerated.item[1] = mux;
1428 }
1429
1430 return 0;
1431}
1432EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1433
1434/**
1435 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1436 * callback
1437 * @kcontrol: mixer control
1438 * @ucontrol: control element information
1439 *
1440 * Callback to set the value of a dapm semi enumerated double mixer control.
1441 *
1442 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1443 * used for handling bitfield coded enumeration for example.
1444 *
1445 * Returns 0 for success.
1446 */
1447int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1448 struct snd_ctl_elem_value *ucontrol)
1449{
1450 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1451 struct soc_value_enum *e = (struct soc_value_enum *)
1452 kcontrol->private_value;
1453 unsigned short val, mux;
1454 unsigned short mask;
1455 int ret = 0;
1456
1457 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1458 return -EINVAL;
1459 mux = ucontrol->value.enumerated.item[0];
1460 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1461 mask = e->mask << e->shift_l;
1462 if (e->shift_l != e->shift_r) {
1463 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1464 return -EINVAL;
1465 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1466 mask |= e->mask << e->shift_r;
1467 }
1468
1469 mutex_lock(&widget->codec->mutex);
1470 widget->value = val;
1471 dapm_value_mux_update_power(widget, kcontrol, mask, mux, val, e);
1472 if (widget->event) {
1473 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1474 ret = widget->event(widget,
1475 kcontrol, SND_SOC_DAPM_PRE_REG);
1476 if (ret < 0)
1477 goto out;
1478 }
1479 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1480 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1481 ret = widget->event(widget,
1482 kcontrol, SND_SOC_DAPM_POST_REG);
1483 } else
1484 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1485
1486out:
1487 mutex_unlock(&widget->codec->mutex);
1488 return ret;
1489}
1490EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1491
1492/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001493 * snd_soc_dapm_new_control - create new dapm control
1494 * @codec: audio codec
1495 * @widget: widget template
1496 *
1497 * Creates a new dapm control based upon the template.
1498 *
1499 * Returns 0 for success else error.
1500 */
1501int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1502 const struct snd_soc_dapm_widget *widget)
1503{
1504 struct snd_soc_dapm_widget *w;
1505
1506 if ((w = dapm_cnew_widget(widget)) == NULL)
1507 return -ENOMEM;
1508
1509 w->codec = codec;
1510 INIT_LIST_HEAD(&w->sources);
1511 INIT_LIST_HEAD(&w->sinks);
1512 INIT_LIST_HEAD(&w->list);
1513 list_add(&w->list, &codec->dapm_widgets);
1514
1515 /* machine layer set ups unconnected pins and insertions */
1516 w->connected = 1;
1517 return 0;
1518}
1519EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1520
1521/**
Mark Brown4ba13272008-05-13 14:51:19 +02001522 * snd_soc_dapm_new_controls - create new dapm controls
1523 * @codec: audio codec
1524 * @widget: widget array
1525 * @num: number of widgets
1526 *
1527 * Creates new DAPM controls based upon the templates.
1528 *
1529 * Returns 0 for success else error.
1530 */
1531int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1532 const struct snd_soc_dapm_widget *widget,
1533 int num)
1534{
1535 int i, ret;
1536
1537 for (i = 0; i < num; i++) {
1538 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00001539 if (ret < 0) {
1540 printk(KERN_ERR
1541 "ASoC: Failed to create DAPM control %s: %d\n",
1542 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02001543 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00001544 }
Mark Brown4ba13272008-05-13 14:51:19 +02001545 widget++;
1546 }
1547 return 0;
1548}
1549EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1550
1551
1552/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001553 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1554 * @codec: audio codec
1555 * @stream: stream name
1556 * @event: stream event
1557 *
1558 * Sends a stream event to the dapm core. The core then makes any
1559 * necessary widget power changes.
1560 *
1561 * Returns 0 for success else error.
1562 */
1563int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1564 char *stream, int event)
1565{
1566 struct snd_soc_dapm_widget *w;
1567
Seth Forshee11da21a2007-02-02 17:14:19 +01001568 if (stream == NULL)
1569 return 0;
1570
Richard Purdie2b97eab2006-10-06 18:32:18 +02001571 mutex_lock(&codec->mutex);
1572 list_for_each_entry(w, &codec->dapm_widgets, list)
1573 {
1574 if (!w->sname)
1575 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01001576 pr_debug("widget %s\n %s stream %s event %d\n",
1577 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001578 if (strstr(w->sname, stream)) {
1579 switch(event) {
1580 case SND_SOC_DAPM_STREAM_START:
1581 w->active = 1;
1582 break;
1583 case SND_SOC_DAPM_STREAM_STOP:
1584 w->active = 0;
1585 break;
1586 case SND_SOC_DAPM_STREAM_SUSPEND:
1587 if (w->active)
1588 w->suspend = 1;
1589 w->active = 0;
1590 break;
1591 case SND_SOC_DAPM_STREAM_RESUME:
1592 if (w->suspend) {
1593 w->active = 1;
1594 w->suspend = 0;
1595 }
1596 break;
1597 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1598 break;
1599 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1600 break;
1601 }
1602 }
1603 }
1604 mutex_unlock(&codec->mutex);
1605
1606 dapm_power_widgets(codec, event);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08001607 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001608 return 0;
1609}
1610EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1611
1612/**
Mark Brown0be98982008-05-19 12:31:28 +02001613 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001614 * @socdev: audio device
Mark Brown0be98982008-05-19 12:31:28 +02001615 * @level: level to configure
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001616 *
Mark Brown0be98982008-05-19 12:31:28 +02001617 * Configure the bias (power) levels for the SoC audio device.
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001618 *
1619 * Returns 0 for success else error.
1620 */
Mark Brown0be98982008-05-19 12:31:28 +02001621int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1622 enum snd_soc_bias_level level)
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001623{
1624 struct snd_soc_codec *codec = socdev->codec;
Mark Brown87506542008-11-18 20:50:34 +00001625 struct snd_soc_card *card = socdev->card;
Mark Brown0be98982008-05-19 12:31:28 +02001626 int ret = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001627
Mark Brown87506542008-11-18 20:50:34 +00001628 if (card->set_bias_level)
1629 ret = card->set_bias_level(card, level);
Mark Brown0be98982008-05-19 12:31:28 +02001630 if (ret == 0 && codec->set_bias_level)
1631 ret = codec->set_bias_level(codec, level);
1632
1633 return ret;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001634}
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001635
1636/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001637 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00001638 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01001639 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02001640 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001641 * Enables input/output pin and it's parents or children widgets iff there is
1642 * a valid audio route and active audio stream.
1643 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1644 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001645 */
Mark Brown16499232009-01-07 18:25:13 +00001646int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001647{
Liam Girdwooda5302182008-07-07 13:35:17 +01001648 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001649}
Liam Girdwooda5302182008-07-07 13:35:17 +01001650EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001651
1652/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001653 * snd_soc_dapm_disable_pin - disable pin.
1654 * @codec: SoC codec
1655 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001656 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001657 * Disables input/output pin and it's parents or children widgets.
1658 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1659 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001660 */
Mark Brown16499232009-01-07 18:25:13 +00001661int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01001662{
1663 return snd_soc_dapm_set_pin(codec, pin, 0);
1664}
1665EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1666
1667/**
Mark Brown5817b522008-09-24 11:23:11 +01001668 * snd_soc_dapm_nc_pin - permanently disable pin.
1669 * @codec: SoC codec
1670 * @pin: pin name
1671 *
1672 * Marks the specified pin as being not connected, disabling it along
1673 * any parent or child widgets. At present this is identical to
1674 * snd_soc_dapm_disable_pin() but in future it will be extended to do
1675 * additional things such as disabling controls which only affect
1676 * paths through the pin.
1677 *
1678 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1679 * do any widget power switching.
1680 */
Mark Brown16499232009-01-07 18:25:13 +00001681int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01001682{
1683 return snd_soc_dapm_set_pin(codec, pin, 0);
1684}
1685EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1686
1687/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001688 * snd_soc_dapm_get_pin_status - get audio pin status
1689 * @codec: audio codec
1690 * @pin: audio signal pin endpoint (or start point)
1691 *
1692 * Get audio pin status - connected or disconnected.
1693 *
1694 * Returns 1 for connected otherwise 0.
1695 */
Mark Brown16499232009-01-07 18:25:13 +00001696int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001697{
1698 struct snd_soc_dapm_widget *w;
1699
1700 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01001701 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001702 return w->connected;
1703 }
1704
1705 return 0;
1706}
Liam Girdwooda5302182008-07-07 13:35:17 +01001707EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001708
1709/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001710 * snd_soc_dapm_free - free dapm resources
1711 * @socdev: SoC device
1712 *
1713 * Free all dapm widgets and resources.
1714 */
1715void snd_soc_dapm_free(struct snd_soc_device *socdev)
1716{
1717 struct snd_soc_codec *codec = socdev->codec;
1718
1719 snd_soc_dapm_sys_remove(socdev->dev);
1720 dapm_free_widgets(codec);
1721}
1722EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1723
1724/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01001725MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001726MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1727MODULE_LICENSE("GPL");