blob: 94296b5dc58f05232d13d572536e7f960a1567c2 [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.
5 * Author: Liam Girdwood
6 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020013 * Features:
14 * o Changes power status of internal codec blocks depending on the
15 * dynamic configuration of codec internal audio paths and active
16 * DAC's/ADC's.
17 * o Platform power domain - can support external components i.e. amps and
18 * mic/meadphone insertion events.
19 * o Automatic Mic Bias support
20 * o Jack insertion power event initiation - e.g. hp insertion will enable
21 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020022 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020023 * device reopen.
24 *
25 * Todo:
26 * o DAPM power change sequencing - allow for configurable per
27 * codec sequences.
28 * o Support for analogue bias optimisation.
29 * o Support for reduced codec oversampling rates.
30 * o Support for reduced codec bias currents.
31 */
32
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/init.h>
36#include <linux/delay.h>
37#include <linux/pm.h>
38#include <linux/bitops.h>
39#include <linux/platform_device.h>
40#include <linux/jiffies.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020041#include <sound/core.h>
42#include <sound/pcm.h>
43#include <sound/pcm_params.h>
44#include <sound/soc-dapm.h>
45#include <sound/initval.h>
46
47/* debug */
48#define DAPM_DEBUG 0
49#if DAPM_DEBUG
50#define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
51#define dbg(format, arg...) printk(format, ## arg)
52#else
53#define dump_dapm(codec, action)
54#define dbg(format, arg...)
55#endif
56
Richard Purdie2b97eab2006-10-06 18:32:18 +020057/* dapm power sequences - make this per codec in the future */
58static int dapm_up_seq[] = {
59 snd_soc_dapm_pre, snd_soc_dapm_micbias, snd_soc_dapm_mic,
60 snd_soc_dapm_mux, snd_soc_dapm_dac, snd_soc_dapm_mixer, snd_soc_dapm_pga,
61 snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, snd_soc_dapm_post
62};
63static int dapm_down_seq[] = {
64 snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
65 snd_soc_dapm_pga, snd_soc_dapm_mixer, snd_soc_dapm_dac, snd_soc_dapm_mic,
66 snd_soc_dapm_micbias, snd_soc_dapm_mux, snd_soc_dapm_post
67};
68
69static int dapm_status = 1;
70module_param(dapm_status, int, 0);
71MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
72
Mark Brown15e4c722008-07-02 11:51:20 +010073static unsigned int pop_time;
74
75static void pop_wait(void)
76{
77 if (pop_time)
78 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
79}
80
81static void pop_dbg(const char *fmt, ...)
82{
83 va_list args;
84
85 va_start(args, fmt);
86
87 if (pop_time) {
88 vprintk(fmt, args);
89 pop_wait();
90 }
91
92 va_end(args);
93}
94
Richard Purdie2b97eab2006-10-06 18:32:18 +020095/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +010096static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +020097 const struct snd_soc_dapm_widget *_widget)
98{
Takashi Iwai88cb4292007-02-05 14:56:20 +010099 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200100}
101
102/* set up initial codec paths */
103static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
104 struct snd_soc_dapm_path *p, int i)
105{
106 switch (w->id) {
107 case snd_soc_dapm_switch:
108 case snd_soc_dapm_mixer: {
109 int val;
110 int reg = w->kcontrols[i].private_value & 0xff;
111 int shift = (w->kcontrols[i].private_value >> 8) & 0x0f;
112 int mask = (w->kcontrols[i].private_value >> 16) & 0xff;
113 int invert = (w->kcontrols[i].private_value >> 24) & 0x01;
114
115 val = snd_soc_read(w->codec, reg);
116 val = (val >> shift) & mask;
117
118 if ((invert && !val) || (!invert && val))
119 p->connect = 1;
120 else
121 p->connect = 0;
122 }
123 break;
124 case snd_soc_dapm_mux: {
125 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
126 int val, item, bitmask;
127
128 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
129 ;
130 val = snd_soc_read(w->codec, e->reg);
131 item = (val >> e->shift_l) & (bitmask - 1);
132
133 p->connect = 0;
134 for (i = 0; i < e->mask; i++) {
135 if (!(strcmp(p->name, e->texts[i])) && item == i)
136 p->connect = 1;
137 }
138 }
139 break;
140 /* does not effect routing - always connected */
141 case snd_soc_dapm_pga:
142 case snd_soc_dapm_output:
143 case snd_soc_dapm_adc:
144 case snd_soc_dapm_input:
145 case snd_soc_dapm_dac:
146 case snd_soc_dapm_micbias:
147 case snd_soc_dapm_vmid:
148 p->connect = 1;
149 break;
150 /* does effect routing - dynamically connected */
151 case snd_soc_dapm_hp:
152 case snd_soc_dapm_mic:
153 case snd_soc_dapm_spk:
154 case snd_soc_dapm_line:
155 case snd_soc_dapm_pre:
156 case snd_soc_dapm_post:
157 p->connect = 0;
158 break;
159 }
160}
161
162/* connect mux widget to it's interconnecting audio paths */
163static int dapm_connect_mux(struct snd_soc_codec *codec,
164 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
165 struct snd_soc_dapm_path *path, const char *control_name,
166 const struct snd_kcontrol_new *kcontrol)
167{
168 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
169 int i;
170
171 for (i = 0; i < e->mask; i++) {
172 if (!(strcmp(control_name, e->texts[i]))) {
173 list_add(&path->list, &codec->dapm_paths);
174 list_add(&path->list_sink, &dest->sources);
175 list_add(&path->list_source, &src->sinks);
176 path->name = (char*)e->texts[i];
177 dapm_set_path_status(dest, path, 0);
178 return 0;
179 }
180 }
181
182 return -ENODEV;
183}
184
185/* connect mixer widget to it's interconnecting audio paths */
186static int dapm_connect_mixer(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{
190 int i;
191
192 /* search for mixer kcontrol */
193 for (i = 0; i < dest->num_kcontrols; i++) {
194 if (!strcmp(control_name, dest->kcontrols[i].name)) {
195 list_add(&path->list, &codec->dapm_paths);
196 list_add(&path->list_sink, &dest->sources);
197 list_add(&path->list_source, &src->sinks);
198 path->name = dest->kcontrols[i].name;
199 dapm_set_path_status(dest, path, i);
200 return 0;
201 }
202 }
203 return -ENODEV;
204}
205
206/* update dapm codec register bits */
207static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
208{
209 int change, power;
210 unsigned short old, new;
211 struct snd_soc_codec *codec = widget->codec;
212
213 /* check for valid widgets */
214 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
215 widget->id == snd_soc_dapm_output ||
216 widget->id == snd_soc_dapm_hp ||
217 widget->id == snd_soc_dapm_mic ||
218 widget->id == snd_soc_dapm_line ||
219 widget->id == snd_soc_dapm_spk)
220 return 0;
221
222 power = widget->power;
223 if (widget->invert)
224 power = (power ? 0:1);
225
226 old = snd_soc_read(codec, widget->reg);
227 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
228
229 change = old != new;
230 if (change) {
231 pop_dbg("pop test %s : %s in %d ms\n", widget->name,
Mark Brown15e4c722008-07-02 11:51:20 +0100232 widget->power ? "on" : "off", pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200233 snd_soc_write(codec, widget->reg, new);
Mark Brown15e4c722008-07-02 11:51:20 +0100234 pop_wait();
Richard Purdie2b97eab2006-10-06 18:32:18 +0200235 }
Mark Brown32f48762008-04-14 12:59:27 +0200236 dbg("reg %x old %x new %x change %d\n", widget->reg, old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200237 return change;
238}
239
240/* ramps the volume up or down to minimise pops before or after a
241 * DAPM power event */
242static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
243{
244 const struct snd_kcontrol_new *k = widget->kcontrols;
245
246 if (widget->muted && !power)
247 return 0;
248 if (!widget->muted && power)
249 return 0;
250
251 if (widget->num_kcontrols && k) {
252 int reg = k->private_value & 0xff;
253 int shift = (k->private_value >> 8) & 0x0f;
254 int mask = (k->private_value >> 16) & 0xff;
255 int invert = (k->private_value >> 24) & 0x01;
256
257 if (power) {
258 int i;
259 /* power up has happended, increase volume to last level */
260 if (invert) {
261 for (i = mask; i > widget->saved_value; i--)
262 snd_soc_update_bits(widget->codec, reg, mask, i);
263 } else {
264 for (i = 0; i < widget->saved_value; i++)
265 snd_soc_update_bits(widget->codec, reg, mask, i);
266 }
267 widget->muted = 0;
268 } else {
269 /* power down is about to occur, decrease volume to mute */
270 int val = snd_soc_read(widget->codec, reg);
271 int i = widget->saved_value = (val >> shift) & mask;
272 if (invert) {
273 for (; i < mask; i++)
274 snd_soc_update_bits(widget->codec, reg, mask, i);
275 } else {
276 for (; i > 0; i--)
277 snd_soc_update_bits(widget->codec, reg, mask, i);
278 }
279 widget->muted = 1;
280 }
281 }
282 return 0;
283}
284
285/* create new dapm mixer control */
286static int dapm_new_mixer(struct snd_soc_codec *codec,
287 struct snd_soc_dapm_widget *w)
288{
289 int i, ret = 0;
290 char name[32];
291 struct snd_soc_dapm_path *path;
292
293 /* add kcontrol */
294 for (i = 0; i < w->num_kcontrols; i++) {
295
296 /* match name */
297 list_for_each_entry(path, &w->sources, list_sink) {
298
299 /* mixer/mux paths name must match control name */
300 if (path->name != (char*)w->kcontrols[i].name)
301 continue;
302
303 /* add dapm control with long name */
304 snprintf(name, 32, "%s %s", w->name, w->kcontrols[i].name);
305 path->long_name = kstrdup (name, GFP_KERNEL);
306 if (path->long_name == NULL)
307 return -ENOMEM;
308
309 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
310 path->long_name);
311 ret = snd_ctl_add(codec->card, path->kcontrol);
312 if (ret < 0) {
313 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s\n",
314 path->long_name);
315 kfree(path->long_name);
316 path->long_name = NULL;
317 return ret;
318 }
319 }
320 }
321 return ret;
322}
323
324/* create new dapm mux control */
325static int dapm_new_mux(struct snd_soc_codec *codec,
326 struct snd_soc_dapm_widget *w)
327{
328 struct snd_soc_dapm_path *path = NULL;
329 struct snd_kcontrol *kcontrol;
330 int ret = 0;
331
332 if (!w->num_kcontrols) {
333 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
334 return -EINVAL;
335 }
336
337 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
338 ret = snd_ctl_add(codec->card, kcontrol);
339 if (ret < 0)
340 goto err;
341
342 list_for_each_entry(path, &w->sources, list_sink)
343 path->kcontrol = kcontrol;
344
345 return ret;
346
347err:
348 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
349 return ret;
350}
351
352/* create new dapm volume control */
353static int dapm_new_pga(struct snd_soc_codec *codec,
354 struct snd_soc_dapm_widget *w)
355{
356 struct snd_kcontrol *kcontrol;
357 int ret = 0;
358
359 if (!w->num_kcontrols)
360 return -EINVAL;
361
362 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
363 ret = snd_ctl_add(codec->card, kcontrol);
364 if (ret < 0) {
365 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
366 return ret;
367 }
368
369 return ret;
370}
371
372/* reset 'walked' bit for each dapm path */
373static inline void dapm_clear_walk(struct snd_soc_codec *codec)
374{
375 struct snd_soc_dapm_path *p;
376
377 list_for_each_entry(p, &codec->dapm_paths, list)
378 p->walked = 0;
379}
380
381/*
382 * Recursively check for a completed path to an active or physically connected
383 * output widget. Returns number of complete paths.
384 */
385static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
386{
387 struct snd_soc_dapm_path *path;
388 int con = 0;
389
390 if (widget->id == snd_soc_dapm_adc && widget->active)
391 return 1;
392
393 if (widget->connected) {
394 /* connected pin ? */
395 if (widget->id == snd_soc_dapm_output && !widget->ext)
396 return 1;
397
398 /* connected jack or spk ? */
399 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
400 widget->id == snd_soc_dapm_line)
401 return 1;
402 }
403
404 list_for_each_entry(path, &widget->sinks, list_source) {
405 if (path->walked)
406 continue;
407
408 if (path->sink && path->connect) {
409 path->walked = 1;
410 con += is_connected_output_ep(path->sink);
411 }
412 }
413
414 return con;
415}
416
417/*
418 * Recursively check for a completed path to an active or physically connected
419 * input widget. Returns number of complete paths.
420 */
421static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
422{
423 struct snd_soc_dapm_path *path;
424 int con = 0;
425
426 /* active stream ? */
427 if (widget->id == snd_soc_dapm_dac && widget->active)
428 return 1;
429
430 if (widget->connected) {
431 /* connected pin ? */
432 if (widget->id == snd_soc_dapm_input && !widget->ext)
433 return 1;
434
435 /* connected VMID/Bias for lower pops */
436 if (widget->id == snd_soc_dapm_vmid)
437 return 1;
438
439 /* connected jack ? */
440 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
441 return 1;
442 }
443
444 list_for_each_entry(path, &widget->sources, list_sink) {
445 if (path->walked)
446 continue;
447
448 if (path->source && path->connect) {
449 path->walked = 1;
450 con += is_connected_input_ep(path->source);
451 }
452 }
453
454 return con;
455}
456
457/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300458 * Handler for generic register modifier widget.
459 */
460int dapm_reg_event(struct snd_soc_dapm_widget *w,
461 struct snd_kcontrol *kcontrol, int event)
462{
463 unsigned int val;
464
465 if (SND_SOC_DAPM_EVENT_ON(event))
466 val = w->on_val;
467 else
468 val = w->off_val;
469
470 snd_soc_update_bits(w->codec, -(w->reg + 1),
471 w->mask << w->shift, val << w->shift);
472
473 return 0;
474}
475
476/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200477 * Scan each dapm widget for complete audio path.
478 * A complete path is a route that has valid endpoints i.e.:-
479 *
480 * o DAC to output pin.
481 * o Input Pin to ADC.
482 * o Input pin to Output pin (bypass, sidetone)
483 * o DAC to ADC (loopback).
484 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100485static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200486{
487 struct snd_soc_dapm_widget *w;
488 int in, out, i, c = 1, *seq = NULL, ret = 0, power_change, power;
489
490 /* do we have a sequenced stream event */
491 if (event == SND_SOC_DAPM_STREAM_START) {
492 c = ARRAY_SIZE(dapm_up_seq);
493 seq = dapm_up_seq;
494 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
495 c = ARRAY_SIZE(dapm_down_seq);
496 seq = dapm_down_seq;
497 }
498
499 for(i = 0; i < c; i++) {
500 list_for_each_entry(w, &codec->dapm_widgets, list) {
501
502 /* is widget in stream order */
503 if (seq && seq[i] && w->id != seq[i])
504 continue;
505
506 /* vmid - no action */
507 if (w->id == snd_soc_dapm_vmid)
508 continue;
509
510 /* active ADC */
511 if (w->id == snd_soc_dapm_adc && w->active) {
512 in = is_connected_input_ep(w);
513 dapm_clear_walk(w->codec);
514 w->power = (in != 0) ? 1 : 0;
515 dapm_update_bits(w);
516 continue;
517 }
518
519 /* active DAC */
520 if (w->id == snd_soc_dapm_dac && w->active) {
521 out = is_connected_output_ep(w);
522 dapm_clear_walk(w->codec);
523 w->power = (out != 0) ? 1 : 0;
524 dapm_update_bits(w);
525 continue;
526 }
527
528 /* programmable gain/attenuation */
529 if (w->id == snd_soc_dapm_pga) {
530 int on;
531 in = is_connected_input_ep(w);
532 dapm_clear_walk(w->codec);
533 out = is_connected_output_ep(w);
534 dapm_clear_walk(w->codec);
535 w->power = on = (out != 0 && in != 0) ? 1 : 0;
536
537 if (!on)
538 dapm_set_pga(w, on); /* lower volume to reduce pops */
539 dapm_update_bits(w);
540 if (on)
541 dapm_set_pga(w, on); /* restore volume from zero */
542
543 continue;
544 }
545
546 /* pre and post event widgets */
547 if (w->id == snd_soc_dapm_pre) {
548 if (!w->event)
549 continue;
550
551 if (event == SND_SOC_DAPM_STREAM_START) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100552 ret = w->event(w,
553 NULL, SND_SOC_DAPM_PRE_PMU);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200554 if (ret < 0)
555 return ret;
556 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100557 ret = w->event(w,
558 NULL, SND_SOC_DAPM_PRE_PMD);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200559 if (ret < 0)
560 return ret;
561 }
562 continue;
563 }
564 if (w->id == snd_soc_dapm_post) {
565 if (!w->event)
566 continue;
567
568 if (event == SND_SOC_DAPM_STREAM_START) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100569 ret = w->event(w,
570 NULL, SND_SOC_DAPM_POST_PMU);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200571 if (ret < 0)
572 return ret;
573 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100574 ret = w->event(w,
575 NULL, SND_SOC_DAPM_POST_PMD);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200576 if (ret < 0)
577 return ret;
578 }
579 continue;
580 }
581
582 /* all other widgets */
583 in = is_connected_input_ep(w);
584 dapm_clear_walk(w->codec);
585 out = is_connected_output_ep(w);
586 dapm_clear_walk(w->codec);
587 power = (out != 0 && in != 0) ? 1 : 0;
588 power_change = (w->power == power) ? 0: 1;
589 w->power = power;
590
591 /* call any power change event handlers */
592 if (power_change) {
593 if (w->event) {
594 dbg("power %s event for %s flags %x\n",
595 w->power ? "on" : "off", w->name, w->event_flags);
596 if (power) {
597 /* power up event */
598 if (w->event_flags & SND_SOC_DAPM_PRE_PMU) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100599 ret = w->event(w,
600 NULL, SND_SOC_DAPM_PRE_PMU);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200601 if (ret < 0)
602 return ret;
603 }
604 dapm_update_bits(w);
605 if (w->event_flags & SND_SOC_DAPM_POST_PMU){
Laim Girdwood9af6d952008-01-10 14:41:02 +0100606 ret = w->event(w,
607 NULL, SND_SOC_DAPM_POST_PMU);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200608 if (ret < 0)
609 return ret;
610 }
611 } else {
612 /* power down event */
613 if (w->event_flags & SND_SOC_DAPM_PRE_PMD) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100614 ret = w->event(w,
615 NULL, SND_SOC_DAPM_PRE_PMD);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200616 if (ret < 0)
617 return ret;
618 }
619 dapm_update_bits(w);
620 if (w->event_flags & SND_SOC_DAPM_POST_PMD) {
Laim Girdwood9af6d952008-01-10 14:41:02 +0100621 ret = w->event(w,
622 NULL, SND_SOC_DAPM_POST_PMD);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200623 if (ret < 0)
624 return ret;
625 }
626 }
627 } else
628 /* no event handler */
629 dapm_update_bits(w);
630 }
631 }
632 }
633
634 return ret;
635}
636
637#if DAPM_DEBUG
638static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
639{
640 struct snd_soc_dapm_widget *w;
641 struct snd_soc_dapm_path *p = NULL;
642 int in, out;
643
644 printk("DAPM %s %s\n", codec->name, action);
645
646 list_for_each_entry(w, &codec->dapm_widgets, list) {
647
648 /* only display widgets that effect routing */
649 switch (w->id) {
650 case snd_soc_dapm_pre:
651 case snd_soc_dapm_post:
652 case snd_soc_dapm_vmid:
653 continue;
654 case snd_soc_dapm_mux:
655 case snd_soc_dapm_output:
656 case snd_soc_dapm_input:
657 case snd_soc_dapm_switch:
658 case snd_soc_dapm_hp:
659 case snd_soc_dapm_mic:
660 case snd_soc_dapm_spk:
661 case snd_soc_dapm_line:
662 case snd_soc_dapm_micbias:
663 case snd_soc_dapm_dac:
664 case snd_soc_dapm_adc:
665 case snd_soc_dapm_pga:
666 case snd_soc_dapm_mixer:
667 if (w->name) {
668 in = is_connected_input_ep(w);
669 dapm_clear_walk(w->codec);
670 out = is_connected_output_ep(w);
671 dapm_clear_walk(w->codec);
672 printk("%s: %s in %d out %d\n", w->name,
673 w->power ? "On":"Off",in, out);
674
675 list_for_each_entry(p, &w->sources, list_sink) {
676 if (p->connect)
677 printk(" in %s %s\n", p->name ? p->name : "static",
678 p->source->name);
679 }
680 list_for_each_entry(p, &w->sinks, list_source) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200681 if (p->connect)
682 printk(" out %s %s\n", p->name ? p->name : "static",
683 p->sink->name);
684 }
685 }
686 break;
687 }
688 }
689}
690#endif
691
692/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100693static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
694 struct snd_kcontrol *kcontrol, int mask,
695 int val, struct soc_enum* e)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200696{
697 struct snd_soc_dapm_path *path;
698 int found = 0;
699
700 if (widget->id != snd_soc_dapm_mux)
701 return -ENODEV;
702
703 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
704 return 0;
705
706 /* find dapm widget path assoc with kcontrol */
707 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
708 if (path->kcontrol != kcontrol)
709 continue;
710
711 if (!path->name || ! e->texts[val])
712 continue;
713
714 found = 1;
715 /* we now need to match the string in the enum to the path */
716 if (!(strcmp(path->name, e->texts[val])))
717 path->connect = 1; /* new connection */
718 else
719 path->connect = 0; /* old connection must be powered down */
720 }
721
722 if (found)
723 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
724
725 return 0;
726}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200727
Milan plzik1b075e32008-01-10 14:39:46 +0100728/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100729static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
730 struct snd_kcontrol *kcontrol, int reg,
731 int val_mask, int val, int invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200732{
733 struct snd_soc_dapm_path *path;
734 int found = 0;
735
Milan plzik1b075e32008-01-10 14:39:46 +0100736 if (widget->id != snd_soc_dapm_mixer &&
737 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200738 return -ENODEV;
739
740 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
741 return 0;
742
743 /* find dapm widget path assoc with kcontrol */
744 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
745 if (path->kcontrol != kcontrol)
746 continue;
747
748 /* found, now check type */
749 found = 1;
750 if (val)
751 /* new connection */
752 path->connect = invert ? 0:1;
753 else
754 /* old connection must be powered down */
755 path->connect = invert ? 1:0;
756 break;
757 }
758
759 if (found)
760 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
761
762 return 0;
763}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200764
765/* show dapm widget status in sys fs */
766static ssize_t dapm_widget_show(struct device *dev,
767 struct device_attribute *attr, char *buf)
768{
769 struct snd_soc_device *devdata = dev_get_drvdata(dev);
770 struct snd_soc_codec *codec = devdata->codec;
771 struct snd_soc_dapm_widget *w;
772 int count = 0;
773 char *state = "not set";
774
775 list_for_each_entry(w, &codec->dapm_widgets, list) {
776
777 /* only display widgets that burnm power */
778 switch (w->id) {
779 case snd_soc_dapm_hp:
780 case snd_soc_dapm_mic:
781 case snd_soc_dapm_spk:
782 case snd_soc_dapm_line:
783 case snd_soc_dapm_micbias:
784 case snd_soc_dapm_dac:
785 case snd_soc_dapm_adc:
786 case snd_soc_dapm_pga:
787 case snd_soc_dapm_mixer:
788 if (w->name)
789 count += sprintf(buf + count, "%s: %s\n",
790 w->name, w->power ? "On":"Off");
791 break;
792 default:
793 break;
794 }
795 }
796
Mark Brown0be98982008-05-19 12:31:28 +0200797 switch (codec->bias_level) {
798 case SND_SOC_BIAS_ON:
799 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200800 break;
Mark Brown0be98982008-05-19 12:31:28 +0200801 case SND_SOC_BIAS_PREPARE:
802 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200803 break;
Mark Brown0be98982008-05-19 12:31:28 +0200804 case SND_SOC_BIAS_STANDBY:
805 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200806 break;
Mark Brown0be98982008-05-19 12:31:28 +0200807 case SND_SOC_BIAS_OFF:
808 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +0200809 break;
810 }
811 count += sprintf(buf + count, "PM State: %s\n", state);
812
813 return count;
814}
815
816static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
817
Mark Brown15e4c722008-07-02 11:51:20 +0100818/* pop/click delay times */
819static ssize_t dapm_pop_time_show(struct device *dev,
820 struct device_attribute *attr, char *buf)
821{
822 return sprintf(buf, "%d\n", pop_time);
823}
824
825static ssize_t dapm_pop_time_store(struct device *dev,
826 struct device_attribute *attr,
827 const char *buf, size_t count)
828
829{
Mark Brown73ead482008-07-04 16:01:14 +0100830 unsigned long val;
831
832 if (strict_strtoul(buf, 10, &val) >= 0)
833 pop_time = val;
834 else
Mark Brown15e4c722008-07-02 11:51:20 +0100835 printk(KERN_ERR "Unable to parse pop_time setting\n");
836
837 return count;
838}
839
840static DEVICE_ATTR(dapm_pop_time, 0744, dapm_pop_time_show,
841 dapm_pop_time_store);
842
Richard Purdie2b97eab2006-10-06 18:32:18 +0200843int snd_soc_dapm_sys_add(struct device *dev)
844{
845 int ret = 0;
846
Mark Brown15e4c722008-07-02 11:51:20 +0100847 if (dapm_status) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200848 ret = device_create_file(dev, &dev_attr_dapm_widget);
849
Mark Brown15e4c722008-07-02 11:51:20 +0100850 if (ret == 0)
851 ret = device_create_file(dev, &dev_attr_dapm_pop_time);
852 }
853
Richard Purdie2b97eab2006-10-06 18:32:18 +0200854 return ret;
855}
856
857static void snd_soc_dapm_sys_remove(struct device *dev)
858{
Mark Brown15e4c722008-07-02 11:51:20 +0100859 if (dapm_status) {
860 device_remove_file(dev, &dev_attr_dapm_pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200861 device_remove_file(dev, &dev_attr_dapm_widget);
Mark Brown15e4c722008-07-02 11:51:20 +0100862 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200863}
864
865/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100866static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200867{
868 struct snd_soc_dapm_widget *w, *next_w;
869 struct snd_soc_dapm_path *p, *next_p;
870
871 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
872 list_del(&w->list);
873 kfree(w);
874 }
875
876 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
877 list_del(&p->list);
878 kfree(p->long_name);
879 kfree(p);
880 }
881}
882
Liam Girdwooda5302182008-07-07 13:35:17 +0100883static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
884 char *pin, int status)
885{
886 struct snd_soc_dapm_widget *w;
887
888 list_for_each_entry(w, &codec->dapm_widgets, list) {
889 if (!strcmp(w->name, pin)) {
890 dbg("dapm: %s: pin %s\n", codec->name, pin);
891 w->connected = status;
892 return 0;
893 }
894 }
895
896 dbg("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
897 return -EINVAL;
898}
899
Richard Purdie2b97eab2006-10-06 18:32:18 +0200900/**
Liam Girdwooda5302182008-07-07 13:35:17 +0100901 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +0200902 * @codec: audio codec
903 *
904 * Walks all dapm audio paths and powers widgets according to their
905 * stream or path usage.
906 *
907 * Returns 0 for success.
908 */
Liam Girdwooda5302182008-07-07 13:35:17 +0100909int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200910{
911 return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
912}
Liam Girdwooda5302182008-07-07 13:35:17 +0100913EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200914
Mark Brown105f1c22008-05-13 14:52:19 +0200915static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
916 const char *sink, const char *control, const char *source)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200917{
918 struct snd_soc_dapm_path *path;
919 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
920 int ret = 0;
921
922 /* find src and dest widgets */
923 list_for_each_entry(w, &codec->dapm_widgets, list) {
924
925 if (!wsink && !(strcmp(w->name, sink))) {
926 wsink = w;
927 continue;
928 }
929 if (!wsource && !(strcmp(w->name, source))) {
930 wsource = w;
931 }
932 }
933
934 if (wsource == NULL || wsink == NULL)
935 return -ENODEV;
936
937 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
938 if (!path)
939 return -ENOMEM;
940
941 path->source = wsource;
942 path->sink = wsink;
943 INIT_LIST_HEAD(&path->list);
944 INIT_LIST_HEAD(&path->list_source);
945 INIT_LIST_HEAD(&path->list_sink);
946
947 /* check for external widgets */
948 if (wsink->id == snd_soc_dapm_input) {
949 if (wsource->id == snd_soc_dapm_micbias ||
950 wsource->id == snd_soc_dapm_mic ||
Seth Forshee1e392212007-04-16 15:36:42 +0200951 wsink->id == snd_soc_dapm_line ||
952 wsink->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200953 wsink->ext = 1;
954 }
955 if (wsource->id == snd_soc_dapm_output) {
956 if (wsink->id == snd_soc_dapm_spk ||
957 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +0200958 wsink->id == snd_soc_dapm_line ||
959 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200960 wsource->ext = 1;
961 }
962
963 /* connect static paths */
964 if (control == NULL) {
965 list_add(&path->list, &codec->dapm_paths);
966 list_add(&path->list_sink, &wsink->sources);
967 list_add(&path->list_source, &wsource->sinks);
968 path->connect = 1;
969 return 0;
970 }
971
972 /* connect dynamic paths */
973 switch(wsink->id) {
974 case snd_soc_dapm_adc:
975 case snd_soc_dapm_dac:
976 case snd_soc_dapm_pga:
977 case snd_soc_dapm_input:
978 case snd_soc_dapm_output:
979 case snd_soc_dapm_micbias:
980 case snd_soc_dapm_vmid:
981 case snd_soc_dapm_pre:
982 case snd_soc_dapm_post:
983 list_add(&path->list, &codec->dapm_paths);
984 list_add(&path->list_sink, &wsink->sources);
985 list_add(&path->list_source, &wsource->sinks);
986 path->connect = 1;
987 return 0;
988 case snd_soc_dapm_mux:
989 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
990 &wsink->kcontrols[0]);
991 if (ret != 0)
992 goto err;
993 break;
994 case snd_soc_dapm_switch:
995 case snd_soc_dapm_mixer:
996 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
997 if (ret != 0)
998 goto err;
999 break;
1000 case snd_soc_dapm_hp:
1001 case snd_soc_dapm_mic:
1002 case snd_soc_dapm_line:
1003 case snd_soc_dapm_spk:
1004 list_add(&path->list, &codec->dapm_paths);
1005 list_add(&path->list_sink, &wsink->sources);
1006 list_add(&path->list_source, &wsource->sinks);
1007 path->connect = 0;
1008 return 0;
1009 }
1010 return 0;
1011
1012err:
1013 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1014 control, sink);
1015 kfree(path);
1016 return ret;
1017}
Mark Brown105f1c22008-05-13 14:52:19 +02001018
1019/**
1020 * snd_soc_dapm_connect_input - connect dapm widgets
1021 * @codec: audio codec
1022 * @sink: name of target widget
1023 * @control: mixer control name
1024 * @source: name of source name
1025 *
1026 * Connects 2 dapm widgets together via a named audio path. The sink is
1027 * the widget receiving the audio signal, whilst the source is the sender
1028 * of the audio signal.
1029 *
1030 * This function has been deprecated in favour of snd_soc_dapm_add_routes().
1031 *
1032 * Returns 0 for success else error.
1033 */
1034int snd_soc_dapm_connect_input(struct snd_soc_codec *codec, const char *sink,
1035 const char *control, const char *source)
1036{
1037 return snd_soc_dapm_add_route(codec, sink, control, source);
1038}
Richard Purdie2b97eab2006-10-06 18:32:18 +02001039EXPORT_SYMBOL_GPL(snd_soc_dapm_connect_input);
1040
1041/**
Mark Brown105f1c22008-05-13 14:52:19 +02001042 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1043 * @codec: codec
1044 * @route: audio routes
1045 * @num: number of routes
1046 *
1047 * Connects 2 dapm widgets together via a named audio path. The sink is
1048 * the widget receiving the audio signal, whilst the source is the sender
1049 * of the audio signal.
1050 *
1051 * Returns 0 for success else error. On error all resources can be freed
1052 * with a call to snd_soc_card_free().
1053 */
1054int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1055 const struct snd_soc_dapm_route *route, int num)
1056{
1057 int i, ret;
1058
1059 for (i = 0; i < num; i++) {
1060 ret = snd_soc_dapm_add_route(codec, route->sink,
1061 route->control, route->source);
1062 if (ret < 0) {
1063 printk(KERN_ERR "Failed to add route %s->%s\n",
1064 route->source,
1065 route->sink);
1066 return ret;
1067 }
1068 route++;
1069 }
1070
1071 return 0;
1072}
1073EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1074
1075/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001076 * snd_soc_dapm_new_widgets - add new dapm widgets
1077 * @codec: audio codec
1078 *
1079 * Checks the codec for any new dapm widgets and creates them if found.
1080 *
1081 * Returns 0 for success.
1082 */
1083int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1084{
1085 struct snd_soc_dapm_widget *w;
1086
Richard Purdie2b97eab2006-10-06 18:32:18 +02001087 list_for_each_entry(w, &codec->dapm_widgets, list)
1088 {
1089 if (w->new)
1090 continue;
1091
1092 switch(w->id) {
1093 case snd_soc_dapm_switch:
1094 case snd_soc_dapm_mixer:
1095 dapm_new_mixer(codec, w);
1096 break;
1097 case snd_soc_dapm_mux:
1098 dapm_new_mux(codec, w);
1099 break;
1100 case snd_soc_dapm_adc:
1101 case snd_soc_dapm_dac:
1102 case snd_soc_dapm_pga:
1103 dapm_new_pga(codec, w);
1104 break;
1105 case snd_soc_dapm_input:
1106 case snd_soc_dapm_output:
1107 case snd_soc_dapm_micbias:
1108 case snd_soc_dapm_spk:
1109 case snd_soc_dapm_hp:
1110 case snd_soc_dapm_mic:
1111 case snd_soc_dapm_line:
1112 case snd_soc_dapm_vmid:
1113 case snd_soc_dapm_pre:
1114 case snd_soc_dapm_post:
1115 break;
1116 }
1117 w->new = 1;
1118 }
1119
1120 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001121 return 0;
1122}
1123EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1124
1125/**
1126 * snd_soc_dapm_get_volsw - dapm mixer get callback
1127 * @kcontrol: mixer control
1128 * @uinfo: control element information
1129 *
1130 * Callback to get the value of a dapm mixer control.
1131 *
1132 * Returns 0 for success.
1133 */
1134int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1135 struct snd_ctl_elem_value *ucontrol)
1136{
1137 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1138 int reg = kcontrol->private_value & 0xff;
1139 int shift = (kcontrol->private_value >> 8) & 0x0f;
1140 int rshift = (kcontrol->private_value >> 12) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001141 int max = (kcontrol->private_value >> 16) & 0xff;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001142 int invert = (kcontrol->private_value >> 24) & 0x01;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001143 int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001144
1145 /* return the saved value if we are powered down */
1146 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1147 ucontrol->value.integer.value[0] = widget->saved_value;
1148 return 0;
1149 }
1150
1151 ucontrol->value.integer.value[0] =
1152 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1153 if (shift != rshift)
1154 ucontrol->value.integer.value[1] =
1155 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1156 if (invert) {
1157 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001158 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001159 if (shift != rshift)
1160 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001161 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001162 }
1163
1164 return 0;
1165}
1166EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1167
1168/**
1169 * snd_soc_dapm_put_volsw - dapm mixer set callback
1170 * @kcontrol: mixer control
1171 * @uinfo: control element information
1172 *
1173 * Callback to set the value of a dapm mixer control.
1174 *
1175 * Returns 0 for success.
1176 */
1177int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1178 struct snd_ctl_elem_value *ucontrol)
1179{
1180 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1181 int reg = kcontrol->private_value & 0xff;
1182 int shift = (kcontrol->private_value >> 8) & 0x0f;
1183 int rshift = (kcontrol->private_value >> 12) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001184 int max = (kcontrol->private_value >> 16) & 0xff;
1185 int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001186 int invert = (kcontrol->private_value >> 24) & 0x01;
1187 unsigned short val, val2, val_mask;
1188 int ret;
1189
1190 val = (ucontrol->value.integer.value[0] & mask);
1191
1192 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001193 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001194 val_mask = mask << shift;
1195 val = val << shift;
1196 if (shift != rshift) {
1197 val2 = (ucontrol->value.integer.value[1] & mask);
1198 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001199 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001200 val_mask |= mask << rshift;
1201 val |= val2 << rshift;
1202 }
1203
1204 mutex_lock(&widget->codec->mutex);
1205 widget->value = val;
1206
1207 /* save volume value if the widget is powered down */
1208 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1209 widget->saved_value = val;
1210 mutex_unlock(&widget->codec->mutex);
1211 return 1;
1212 }
1213
1214 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1215 if (widget->event) {
1216 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001217 ret = widget->event(widget, kcontrol,
1218 SND_SOC_DAPM_PRE_REG);
1219 if (ret < 0) {
1220 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001221 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001222 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001223 }
1224 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1225 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001226 ret = widget->event(widget, kcontrol,
1227 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001228 } else
1229 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1230
1231out:
1232 mutex_unlock(&widget->codec->mutex);
1233 return ret;
1234}
1235EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1236
1237/**
1238 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1239 * @kcontrol: mixer control
1240 * @uinfo: control element information
1241 *
1242 * Callback to get the value of a dapm enumerated double mixer control.
1243 *
1244 * Returns 0 for success.
1245 */
1246int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1247 struct snd_ctl_elem_value *ucontrol)
1248{
1249 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1250 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1251 unsigned short val, bitmask;
1252
1253 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1254 ;
1255 val = snd_soc_read(widget->codec, e->reg);
1256 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1257 if (e->shift_l != e->shift_r)
1258 ucontrol->value.enumerated.item[1] =
1259 (val >> e->shift_r) & (bitmask - 1);
1260
1261 return 0;
1262}
1263EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1264
1265/**
1266 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1267 * @kcontrol: mixer control
1268 * @uinfo: control element information
1269 *
1270 * Callback to set the value of a dapm enumerated double mixer control.
1271 *
1272 * Returns 0 for success.
1273 */
1274int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1275 struct snd_ctl_elem_value *ucontrol)
1276{
1277 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1278 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1279 unsigned short val, mux;
1280 unsigned short mask, bitmask;
1281 int ret = 0;
1282
1283 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1284 ;
1285 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1286 return -EINVAL;
1287 mux = ucontrol->value.enumerated.item[0];
1288 val = mux << e->shift_l;
1289 mask = (bitmask - 1) << e->shift_l;
1290 if (e->shift_l != e->shift_r) {
1291 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
1292 return -EINVAL;
1293 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1294 mask |= (bitmask - 1) << e->shift_r;
1295 }
1296
1297 mutex_lock(&widget->codec->mutex);
1298 widget->value = val;
1299 dapm_mux_update_power(widget, kcontrol, mask, mux, e);
1300 if (widget->event) {
1301 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001302 ret = widget->event(widget,
1303 kcontrol, SND_SOC_DAPM_PRE_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001304 if (ret < 0)
1305 goto out;
1306 }
1307 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1308 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001309 ret = widget->event(widget,
1310 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001311 } else
1312 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1313
1314out:
1315 mutex_unlock(&widget->codec->mutex);
1316 return ret;
1317}
1318EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1319
1320/**
1321 * snd_soc_dapm_new_control - create new dapm control
1322 * @codec: audio codec
1323 * @widget: widget template
1324 *
1325 * Creates a new dapm control based upon the template.
1326 *
1327 * Returns 0 for success else error.
1328 */
1329int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1330 const struct snd_soc_dapm_widget *widget)
1331{
1332 struct snd_soc_dapm_widget *w;
1333
1334 if ((w = dapm_cnew_widget(widget)) == NULL)
1335 return -ENOMEM;
1336
1337 w->codec = codec;
1338 INIT_LIST_HEAD(&w->sources);
1339 INIT_LIST_HEAD(&w->sinks);
1340 INIT_LIST_HEAD(&w->list);
1341 list_add(&w->list, &codec->dapm_widgets);
1342
1343 /* machine layer set ups unconnected pins and insertions */
1344 w->connected = 1;
1345 return 0;
1346}
1347EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1348
1349/**
Mark Brown4ba13272008-05-13 14:51:19 +02001350 * snd_soc_dapm_new_controls - create new dapm controls
1351 * @codec: audio codec
1352 * @widget: widget array
1353 * @num: number of widgets
1354 *
1355 * Creates new DAPM controls based upon the templates.
1356 *
1357 * Returns 0 for success else error.
1358 */
1359int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1360 const struct snd_soc_dapm_widget *widget,
1361 int num)
1362{
1363 int i, ret;
1364
1365 for (i = 0; i < num; i++) {
1366 ret = snd_soc_dapm_new_control(codec, widget);
1367 if (ret < 0)
1368 return ret;
1369 widget++;
1370 }
1371 return 0;
1372}
1373EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1374
1375
1376/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001377 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1378 * @codec: audio codec
1379 * @stream: stream name
1380 * @event: stream event
1381 *
1382 * Sends a stream event to the dapm core. The core then makes any
1383 * necessary widget power changes.
1384 *
1385 * Returns 0 for success else error.
1386 */
1387int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1388 char *stream, int event)
1389{
1390 struct snd_soc_dapm_widget *w;
1391
Seth Forshee11da21a2007-02-02 17:14:19 +01001392 if (stream == NULL)
1393 return 0;
1394
Richard Purdie2b97eab2006-10-06 18:32:18 +02001395 mutex_lock(&codec->mutex);
1396 list_for_each_entry(w, &codec->dapm_widgets, list)
1397 {
1398 if (!w->sname)
1399 continue;
1400 dbg("widget %s\n %s stream %s event %d\n", w->name, w->sname,
1401 stream, event);
1402 if (strstr(w->sname, stream)) {
1403 switch(event) {
1404 case SND_SOC_DAPM_STREAM_START:
1405 w->active = 1;
1406 break;
1407 case SND_SOC_DAPM_STREAM_STOP:
1408 w->active = 0;
1409 break;
1410 case SND_SOC_DAPM_STREAM_SUSPEND:
1411 if (w->active)
1412 w->suspend = 1;
1413 w->active = 0;
1414 break;
1415 case SND_SOC_DAPM_STREAM_RESUME:
1416 if (w->suspend) {
1417 w->active = 1;
1418 w->suspend = 0;
1419 }
1420 break;
1421 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1422 break;
1423 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1424 break;
1425 }
1426 }
1427 }
1428 mutex_unlock(&codec->mutex);
1429
1430 dapm_power_widgets(codec, event);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08001431 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001432 return 0;
1433}
1434EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1435
1436/**
Mark Brown0be98982008-05-19 12:31:28 +02001437 * snd_soc_dapm_set_bias_level - set the bias level for the system
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001438 * @socdev: audio device
Mark Brown0be98982008-05-19 12:31:28 +02001439 * @level: level to configure
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001440 *
Mark Brown0be98982008-05-19 12:31:28 +02001441 * Configure the bias (power) levels for the SoC audio device.
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001442 *
1443 * Returns 0 for success else error.
1444 */
Mark Brown0be98982008-05-19 12:31:28 +02001445int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
1446 enum snd_soc_bias_level level)
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001447{
1448 struct snd_soc_codec *codec = socdev->codec;
1449 struct snd_soc_machine *machine = socdev->machine;
Mark Brown0be98982008-05-19 12:31:28 +02001450 int ret = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001451
Mark Brown0be98982008-05-19 12:31:28 +02001452 if (machine->set_bias_level)
1453 ret = machine->set_bias_level(machine, level);
1454 if (ret == 0 && codec->set_bias_level)
1455 ret = codec->set_bias_level(codec, level);
1456
1457 return ret;
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001458}
Liam Girdwood0b4d2212008-01-10 14:36:20 +01001459
1460/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001461 * snd_soc_dapm_enable_pin - enable pin.
1462 * @snd_soc_codec: SoC codec
1463 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02001464 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001465 * Enables input/output pin and it's parents or children widgets iff there is
1466 * a valid audio route and active audio stream.
1467 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1468 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001469 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001470int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001471{
Liam Girdwooda5302182008-07-07 13:35:17 +01001472 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001473}
Liam Girdwooda5302182008-07-07 13:35:17 +01001474EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001475
1476/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001477 * snd_soc_dapm_disable_pin - disable pin.
1478 * @codec: SoC codec
1479 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001480 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001481 * Disables input/output pin and it's parents or children widgets.
1482 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1483 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001484 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001485int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, char *pin)
1486{
1487 return snd_soc_dapm_set_pin(codec, pin, 0);
1488}
1489EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1490
1491/**
1492 * snd_soc_dapm_get_pin_status - get audio pin status
1493 * @codec: audio codec
1494 * @pin: audio signal pin endpoint (or start point)
1495 *
1496 * Get audio pin status - connected or disconnected.
1497 *
1498 * Returns 1 for connected otherwise 0.
1499 */
1500int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001501{
1502 struct snd_soc_dapm_widget *w;
1503
1504 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01001505 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001506 return w->connected;
1507 }
1508
1509 return 0;
1510}
Liam Girdwooda5302182008-07-07 13:35:17 +01001511EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001512
1513/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001514 * snd_soc_dapm_free - free dapm resources
1515 * @socdev: SoC device
1516 *
1517 * Free all dapm widgets and resources.
1518 */
1519void snd_soc_dapm_free(struct snd_soc_device *socdev)
1520{
1521 struct snd_soc_codec *codec = socdev->codec;
1522
1523 snd_soc_dapm_sys_remove(socdev->dev);
1524 dapm_free_widgets(codec);
1525}
1526EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1527
1528/* Module information */
1529MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1530MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1531MODULE_LICENSE("GPL");