blob: 4ca5e56388a3884dc95b40002f6393d0f65097d8 [file] [log] [blame]
Richard Purdie2b97eab2006-10-06 18:32:18 +02001/*
2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwoodd3311242008-10-12 13:17:36 +01005 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Richard Purdie2b97eab2006-10-06 18:32:18 +02006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
Richard Purdie2b97eab2006-10-06 18:32:18 +020012 * Features:
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
15 * DAC's/ADC's.
16 * o Platform power domain - can support external components i.e. amps and
17 * mic/meadphone insertion events.
18 * o Automatic Mic Bias support
19 * o Jack insertion power event initiation - e.g. hp insertion will enable
20 * sinks, dacs, etc
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020021 * o Delayed powerdown of audio susbsystem to reduce pops between a quick
Richard Purdie2b97eab2006-10-06 18:32:18 +020022 * device reopen.
23 *
24 * Todo:
25 * o DAPM power change sequencing - allow for configurable per
26 * codec sequences.
27 * o Support for analogue bias optimisation.
28 * o Support for reduced codec oversampling rates.
29 * o Support for reduced codec bias currents.
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/init.h>
35#include <linux/delay.h>
36#include <linux/pm.h>
37#include <linux/bitops.h>
38#include <linux/platform_device.h>
39#include <linux/jiffies.h>
Richard Purdie2b97eab2006-10-06 18:32:18 +020040#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
43#include <sound/soc-dapm.h>
44#include <sound/initval.h>
45
46/* debug */
Mark Brownc1286b82008-07-07 19:26:03 +010047#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +020048#define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020049#else
50#define dump_dapm(codec, action)
Richard Purdie2b97eab2006-10-06 18:32:18 +020051#endif
52
Richard Purdie2b97eab2006-10-06 18:32:18 +020053/* dapm power sequences - make this per codec in the future */
54static int dapm_up_seq[] = {
Mark Brown246d0a12009-04-22 18:24:55 +010055 snd_soc_dapm_pre, snd_soc_dapm_supply, snd_soc_dapm_micbias,
56 snd_soc_dapm_mic, snd_soc_dapm_mux, snd_soc_dapm_value_mux,
57 snd_soc_dapm_dac, snd_soc_dapm_mixer, snd_soc_dapm_mixer_named_ctl,
58 snd_soc_dapm_pga, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
59 snd_soc_dapm_post
Richard Purdie2b97eab2006-10-06 18:32:18 +020060};
Ian Moltonca9c1aa2009-01-06 20:11:51 +000061
Richard Purdie2b97eab2006-10-06 18:32:18 +020062static int dapm_down_seq[] = {
63 snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
Ian Moltonca9c1aa2009-01-06 20:11:51 +000064 snd_soc_dapm_pga, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_mixer,
65 snd_soc_dapm_dac, snd_soc_dapm_mic, snd_soc_dapm_micbias,
Mark Brown246d0a12009-04-22 18:24:55 +010066 snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_supply,
67 snd_soc_dapm_post
Richard Purdie2b97eab2006-10-06 18:32:18 +020068};
69
Troy Kisky12ef1932008-10-13 17:42:14 -070070static void pop_wait(u32 pop_time)
Mark Brown15e4c722008-07-02 11:51:20 +010071{
72 if (pop_time)
73 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
74}
75
Troy Kisky12ef1932008-10-13 17:42:14 -070076static void pop_dbg(u32 pop_time, const char *fmt, ...)
Mark Brown15e4c722008-07-02 11:51:20 +010077{
78 va_list args;
79
80 va_start(args, fmt);
81
82 if (pop_time) {
83 vprintk(fmt, args);
Troy Kisky12ef1932008-10-13 17:42:14 -070084 pop_wait(pop_time);
Mark Brown15e4c722008-07-02 11:51:20 +010085 }
86
87 va_end(args);
88}
89
Richard Purdie2b97eab2006-10-06 18:32:18 +020090/* create a new dapm widget */
Takashi Iwai88cb4292007-02-05 14:56:20 +010091static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
Richard Purdie2b97eab2006-10-06 18:32:18 +020092 const struct snd_soc_dapm_widget *_widget)
93{
Takashi Iwai88cb4292007-02-05 14:56:20 +010094 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
Richard Purdie2b97eab2006-10-06 18:32:18 +020095}
96
Mark Brown452c5ea2009-05-17 21:41:23 +010097/**
98 * snd_soc_dapm_set_bias_level - set the bias level for the system
99 * @socdev: audio device
100 * @level: level to configure
101 *
102 * Configure the bias (power) levels for the SoC audio device.
103 *
104 * Returns 0 for success else error.
105 */
106static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
107 enum snd_soc_bias_level level)
108{
109 struct snd_soc_card *card = socdev->card;
110 struct snd_soc_codec *codec = socdev->card->codec;
111 int ret = 0;
112
113 if (card->set_bias_level)
114 ret = card->set_bias_level(card, level);
115 if (ret == 0 && codec->set_bias_level)
116 ret = codec->set_bias_level(codec, level);
117
118 return ret;
119}
120
Richard Purdie2b97eab2006-10-06 18:32:18 +0200121/* set up initial codec paths */
122static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
123 struct snd_soc_dapm_path *p, int i)
124{
125 switch (w->id) {
126 case snd_soc_dapm_switch:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000127 case snd_soc_dapm_mixer:
128 case snd_soc_dapm_mixer_named_ctl: {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200129 int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100130 struct soc_mixer_control *mc = (struct soc_mixer_control *)
131 w->kcontrols[i].private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -0400132 unsigned int reg = mc->reg;
133 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100134 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -0400135 unsigned int mask = (1 << fls(max)) - 1;
136 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200137
138 val = snd_soc_read(w->codec, reg);
139 val = (val >> shift) & mask;
140
141 if ((invert && !val) || (!invert && val))
142 p->connect = 1;
143 else
144 p->connect = 0;
145 }
146 break;
147 case snd_soc_dapm_mux: {
148 struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
149 int val, item, bitmask;
150
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100151 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200152 ;
153 val = snd_soc_read(w->codec, e->reg);
154 item = (val >> e->shift_l) & (bitmask - 1);
155
156 p->connect = 0;
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100157 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200158 if (!(strcmp(p->name, e->texts[i])) && item == i)
159 p->connect = 1;
160 }
161 }
162 break;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200163 case snd_soc_dapm_value_mux: {
Peter Ujfalusi74155552009-01-08 13:34:29 +0200164 struct soc_enum *e = (struct soc_enum *)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200165 w->kcontrols[i].private_value;
166 int val, item;
167
168 val = snd_soc_read(w->codec, e->reg);
169 val = (val >> e->shift_l) & e->mask;
170 for (item = 0; item < e->max; item++) {
171 if (val == e->values[item])
172 break;
173 }
174
175 p->connect = 0;
176 for (i = 0; i < e->max; i++) {
177 if (!(strcmp(p->name, e->texts[i])) && item == i)
178 p->connect = 1;
179 }
180 }
181 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200182 /* does not effect routing - always connected */
183 case snd_soc_dapm_pga:
184 case snd_soc_dapm_output:
185 case snd_soc_dapm_adc:
186 case snd_soc_dapm_input:
187 case snd_soc_dapm_dac:
188 case snd_soc_dapm_micbias:
189 case snd_soc_dapm_vmid:
Mark Brown246d0a12009-04-22 18:24:55 +0100190 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200191 p->connect = 1;
192 break;
193 /* does effect routing - dynamically connected */
194 case snd_soc_dapm_hp:
195 case snd_soc_dapm_mic:
196 case snd_soc_dapm_spk:
197 case snd_soc_dapm_line:
198 case snd_soc_dapm_pre:
199 case snd_soc_dapm_post:
200 p->connect = 0;
201 break;
202 }
203}
204
205/* connect mux widget to it's interconnecting audio paths */
206static int dapm_connect_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_enum *e = (struct soc_enum *)kcontrol->private_value;
212 int i;
213
Jon Smirlf8ba0b72008-07-29 11:42:27 +0100214 for (i = 0; i < e->max; i++) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200215 if (!(strcmp(control_name, e->texts[i]))) {
216 list_add(&path->list, &codec->dapm_paths);
217 list_add(&path->list_sink, &dest->sources);
218 list_add(&path->list_source, &src->sinks);
219 path->name = (char*)e->texts[i];
220 dapm_set_path_status(dest, path, 0);
221 return 0;
222 }
223 }
224
225 return -ENODEV;
226}
227
228/* connect mixer widget to it's interconnecting audio paths */
229static int dapm_connect_mixer(struct snd_soc_codec *codec,
230 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
231 struct snd_soc_dapm_path *path, const char *control_name)
232{
233 int i;
234
235 /* search for mixer kcontrol */
236 for (i = 0; i < dest->num_kcontrols; i++) {
237 if (!strcmp(control_name, dest->kcontrols[i].name)) {
238 list_add(&path->list, &codec->dapm_paths);
239 list_add(&path->list_sink, &dest->sources);
240 list_add(&path->list_source, &src->sinks);
241 path->name = dest->kcontrols[i].name;
242 dapm_set_path_status(dest, path, i);
243 return 0;
244 }
245 }
246 return -ENODEV;
247}
248
249/* update dapm codec register bits */
250static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
251{
252 int change, power;
253 unsigned short old, new;
254 struct snd_soc_codec *codec = widget->codec;
255
256 /* check for valid widgets */
257 if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
258 widget->id == snd_soc_dapm_output ||
259 widget->id == snd_soc_dapm_hp ||
260 widget->id == snd_soc_dapm_mic ||
261 widget->id == snd_soc_dapm_line ||
262 widget->id == snd_soc_dapm_spk)
263 return 0;
264
265 power = widget->power;
266 if (widget->invert)
267 power = (power ? 0:1);
268
269 old = snd_soc_read(codec, widget->reg);
270 new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
271
272 change = old != new;
273 if (change) {
Troy Kisky12ef1932008-10-13 17:42:14 -0700274 pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
275 widget->name, widget->power ? "on" : "off",
276 codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200277 snd_soc_write(codec, widget->reg, new);
Troy Kisky12ef1932008-10-13 17:42:14 -0700278 pop_wait(codec->pop_time);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200279 }
Mark Brownc1286b82008-07-07 19:26:03 +0100280 pr_debug("reg %x old %x new %x change %d\n", widget->reg,
281 old, new, change);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200282 return change;
283}
284
285/* ramps the volume up or down to minimise pops before or after a
286 * DAPM power event */
287static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
288{
289 const struct snd_kcontrol_new *k = widget->kcontrols;
290
291 if (widget->muted && !power)
292 return 0;
293 if (!widget->muted && power)
294 return 0;
295
296 if (widget->num_kcontrols && k) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100297 struct soc_mixer_control *mc =
298 (struct soc_mixer_control *)k->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -0400299 unsigned int reg = mc->reg;
300 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +0100301 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -0400302 unsigned int mask = (1 << fls(max)) - 1;
303 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200304
305 if (power) {
306 int i;
307 /* power up has happended, increase volume to last level */
308 if (invert) {
Jon Smirl4eaa9812008-07-29 11:42:26 +0100309 for (i = max; i > widget->saved_value; i--)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200310 snd_soc_update_bits(widget->codec, reg, mask, i);
311 } else {
312 for (i = 0; i < widget->saved_value; i++)
313 snd_soc_update_bits(widget->codec, reg, mask, i);
314 }
315 widget->muted = 0;
316 } else {
317 /* power down is about to occur, decrease volume to mute */
318 int val = snd_soc_read(widget->codec, reg);
319 int i = widget->saved_value = (val >> shift) & mask;
320 if (invert) {
321 for (; i < mask; i++)
322 snd_soc_update_bits(widget->codec, reg, mask, i);
323 } else {
324 for (; i > 0; i--)
325 snd_soc_update_bits(widget->codec, reg, mask, i);
326 }
327 widget->muted = 1;
328 }
329 }
330 return 0;
331}
332
333/* create new dapm mixer control */
334static int dapm_new_mixer(struct snd_soc_codec *codec,
335 struct snd_soc_dapm_widget *w)
336{
337 int i, ret = 0;
Mark Brown219b93f2008-10-28 13:02:31 +0000338 size_t name_len;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200339 struct snd_soc_dapm_path *path;
340
341 /* add kcontrol */
342 for (i = 0; i < w->num_kcontrols; i++) {
343
344 /* match name */
345 list_for_each_entry(path, &w->sources, list_sink) {
346
347 /* mixer/mux paths name must match control name */
348 if (path->name != (char*)w->kcontrols[i].name)
349 continue;
350
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000351 /* add dapm control with long name.
352 * for dapm_mixer this is the concatenation of the
353 * mixer and kcontrol name.
354 * for dapm_mixer_named_ctl this is simply the
355 * kcontrol name.
356 */
357 name_len = strlen(w->kcontrols[i].name) + 1;
Mark Brown07495f32009-03-05 17:06:23 +0000358 if (w->id != snd_soc_dapm_mixer_named_ctl)
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000359 name_len += 1 + strlen(w->name);
360
Mark Brown219b93f2008-10-28 13:02:31 +0000361 path->long_name = kmalloc(name_len, GFP_KERNEL);
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000362
Richard Purdie2b97eab2006-10-06 18:32:18 +0200363 if (path->long_name == NULL)
364 return -ENOMEM;
365
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000366 switch (w->id) {
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000367 default:
368 snprintf(path->long_name, name_len, "%s %s",
369 w->name, w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000370 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000371 case snd_soc_dapm_mixer_named_ctl:
372 snprintf(path->long_name, name_len, "%s",
373 w->kcontrols[i].name);
Mark Brown07495f32009-03-05 17:06:23 +0000374 break;
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000375 }
376
Mark Brown219b93f2008-10-28 13:02:31 +0000377 path->long_name[name_len - 1] = '\0';
378
Richard Purdie2b97eab2006-10-06 18:32:18 +0200379 path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
380 path->long_name);
381 ret = snd_ctl_add(codec->card, path->kcontrol);
382 if (ret < 0) {
Mark Brown6553e192009-04-06 16:59:32 +0100383 printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
384 path->long_name,
385 ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200386 kfree(path->long_name);
387 path->long_name = NULL;
388 return ret;
389 }
390 }
391 }
392 return ret;
393}
394
395/* create new dapm mux control */
396static int dapm_new_mux(struct snd_soc_codec *codec,
397 struct snd_soc_dapm_widget *w)
398{
399 struct snd_soc_dapm_path *path = NULL;
400 struct snd_kcontrol *kcontrol;
401 int ret = 0;
402
403 if (!w->num_kcontrols) {
404 printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
405 return -EINVAL;
406 }
407
408 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
409 ret = snd_ctl_add(codec->card, kcontrol);
410 if (ret < 0)
411 goto err;
412
413 list_for_each_entry(path, &w->sources, list_sink)
414 path->kcontrol = kcontrol;
415
416 return ret;
417
418err:
419 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
420 return ret;
421}
422
423/* create new dapm volume control */
424static int dapm_new_pga(struct snd_soc_codec *codec,
425 struct snd_soc_dapm_widget *w)
426{
427 struct snd_kcontrol *kcontrol;
428 int ret = 0;
429
430 if (!w->num_kcontrols)
431 return -EINVAL;
432
433 kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
434 ret = snd_ctl_add(codec->card, kcontrol);
435 if (ret < 0) {
436 printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
437 return ret;
438 }
439
440 return ret;
441}
442
443/* reset 'walked' bit for each dapm path */
444static inline void dapm_clear_walk(struct snd_soc_codec *codec)
445{
446 struct snd_soc_dapm_path *p;
447
448 list_for_each_entry(p, &codec->dapm_paths, list)
449 p->walked = 0;
450}
451
452/*
453 * Recursively check for a completed path to an active or physically connected
454 * output widget. Returns number of complete paths.
455 */
456static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
457{
458 struct snd_soc_dapm_path *path;
459 int con = 0;
460
Mark Brown246d0a12009-04-22 18:24:55 +0100461 if (widget->id == snd_soc_dapm_supply)
462 return 0;
463
Richard Purdie2b97eab2006-10-06 18:32:18 +0200464 if (widget->id == snd_soc_dapm_adc && widget->active)
465 return 1;
466
467 if (widget->connected) {
468 /* connected pin ? */
469 if (widget->id == snd_soc_dapm_output && !widget->ext)
470 return 1;
471
472 /* connected jack or spk ? */
473 if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
474 widget->id == snd_soc_dapm_line)
475 return 1;
476 }
477
478 list_for_each_entry(path, &widget->sinks, list_source) {
479 if (path->walked)
480 continue;
481
482 if (path->sink && path->connect) {
483 path->walked = 1;
484 con += is_connected_output_ep(path->sink);
485 }
486 }
487
488 return con;
489}
490
491/*
492 * Recursively check for a completed path to an active or physically connected
493 * input widget. Returns number of complete paths.
494 */
495static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
496{
497 struct snd_soc_dapm_path *path;
498 int con = 0;
499
Mark Brown246d0a12009-04-22 18:24:55 +0100500 if (widget->id == snd_soc_dapm_supply)
501 return 0;
502
Richard Purdie2b97eab2006-10-06 18:32:18 +0200503 /* active stream ? */
504 if (widget->id == snd_soc_dapm_dac && widget->active)
505 return 1;
506
507 if (widget->connected) {
508 /* connected pin ? */
509 if (widget->id == snd_soc_dapm_input && !widget->ext)
510 return 1;
511
512 /* connected VMID/Bias for lower pops */
513 if (widget->id == snd_soc_dapm_vmid)
514 return 1;
515
516 /* connected jack ? */
517 if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
518 return 1;
519 }
520
521 list_for_each_entry(path, &widget->sources, list_sink) {
522 if (path->walked)
523 continue;
524
525 if (path->source && path->connect) {
526 path->walked = 1;
527 con += is_connected_input_ep(path->source);
528 }
529 }
530
531 return con;
532}
533
534/*
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300535 * Handler for generic register modifier widget.
536 */
537int dapm_reg_event(struct snd_soc_dapm_widget *w,
538 struct snd_kcontrol *kcontrol, int event)
539{
540 unsigned int val;
541
542 if (SND_SOC_DAPM_EVENT_ON(event))
543 val = w->on_val;
544 else
545 val = w->off_val;
546
547 snd_soc_update_bits(w->codec, -(w->reg + 1),
548 w->mask << w->shift, val << w->shift);
549
550 return 0;
551}
Mark Brown11589412008-07-29 11:42:23 +0100552EXPORT_SYMBOL_GPL(dapm_reg_event);
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300553
Mark Brown025756e2009-04-13 11:09:18 +0100554/* Standard power change method, used to apply power changes to most
555 * widgets.
556 */
557static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
558{
559 int ret;
560
561 /* call any power change event handlers */
562 if (w->event)
563 pr_debug("power %s event for %s flags %x\n",
564 w->power ? "on" : "off",
565 w->name, w->event_flags);
566
567 /* power up pre event */
568 if (w->power && w->event &&
569 (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
570 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
571 if (ret < 0)
572 return ret;
573 }
574
575 /* power down pre event */
576 if (!w->power && w->event &&
577 (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
578 ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
579 if (ret < 0)
580 return ret;
581 }
582
583 /* Lower PGA volume to reduce pops */
584 if (w->id == snd_soc_dapm_pga && !w->power)
585 dapm_set_pga(w, w->power);
586
587 dapm_update_bits(w);
588
589 /* Raise PGA volume to reduce pops */
590 if (w->id == snd_soc_dapm_pga && w->power)
591 dapm_set_pga(w, w->power);
592
593 /* power up post event */
594 if (w->power && w->event &&
595 (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
596 ret = w->event(w,
597 NULL, SND_SOC_DAPM_POST_PMU);
598 if (ret < 0)
599 return ret;
600 }
601
602 /* power down post event */
603 if (!w->power && w->event &&
604 (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
605 ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
606 if (ret < 0)
607 return ret;
608 }
609
610 return 0;
611}
612
Mark Browncd0f2d42009-04-20 16:56:59 +0100613/* Generic check to see if a widget should be powered.
614 */
615static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
616{
617 int in, out;
618
619 in = is_connected_input_ep(w);
620 dapm_clear_walk(w->codec);
621 out = is_connected_output_ep(w);
622 dapm_clear_walk(w->codec);
623 return out != 0 && in != 0;
624}
625
Mark Brown6ea31b92009-04-20 17:15:41 +0100626/* Check to see if an ADC has power */
627static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
628{
629 int in;
630
631 if (w->active) {
632 in = is_connected_input_ep(w);
633 dapm_clear_walk(w->codec);
634 return in != 0;
635 } else {
636 return dapm_generic_check_power(w);
637 }
638}
639
640/* Check to see if a DAC has power */
641static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
642{
643 int out;
644
645 if (w->active) {
646 out = is_connected_output_ep(w);
647 dapm_clear_walk(w->codec);
648 return out != 0;
649 } else {
650 return dapm_generic_check_power(w);
651 }
652}
653
Mark Brown246d0a12009-04-22 18:24:55 +0100654/* Check to see if a power supply is needed */
655static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
656{
657 struct snd_soc_dapm_path *path;
658 int power = 0;
659
660 /* Check if one of our outputs is connected */
661 list_for_each_entry(path, &w->sinks, list_source) {
662 if (path->sink && path->sink->power_check &&
663 path->sink->power_check(path->sink)) {
664 power = 1;
665 break;
666 }
667 }
668
669 dapm_clear_walk(w->codec);
670
671 return power;
672}
673
Jarkko Nikulae2be2cc2008-06-25 14:42:07 +0300674/*
Mark Brown42aa3412009-03-01 19:21:10 +0000675 * Scan a single DAPM widget for a complete audio path and update the
676 * power status appropriately.
677 */
678static int dapm_power_widget(struct snd_soc_codec *codec, int event,
679 struct snd_soc_dapm_widget *w)
680{
Mark Brown6d3ddc82009-05-16 17:47:29 +0100681 int ret;
Mark Brown42aa3412009-03-01 19:21:10 +0000682
Mark Brown6ea31b92009-04-20 17:15:41 +0100683 switch (w->id) {
Mark Brown6ea31b92009-04-20 17:15:41 +0100684 case snd_soc_dapm_pre:
Mark Brown42aa3412009-03-01 19:21:10 +0000685 if (!w->event)
686 return 0;
687
688 if (event == SND_SOC_DAPM_STREAM_START) {
689 ret = w->event(w,
690 NULL, SND_SOC_DAPM_PRE_PMU);
691 if (ret < 0)
692 return ret;
693 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
694 ret = w->event(w,
695 NULL, SND_SOC_DAPM_PRE_PMD);
696 if (ret < 0)
697 return ret;
698 }
699 return 0;
Mark Brown6ea31b92009-04-20 17:15:41 +0100700
701 case snd_soc_dapm_post:
Mark Brown42aa3412009-03-01 19:21:10 +0000702 if (!w->event)
703 return 0;
704
705 if (event == SND_SOC_DAPM_STREAM_START) {
706 ret = w->event(w,
707 NULL, SND_SOC_DAPM_POST_PMU);
708 if (ret < 0)
709 return ret;
710 } else if (event == SND_SOC_DAPM_STREAM_STOP) {
711 ret = w->event(w,
712 NULL, SND_SOC_DAPM_POST_PMD);
713 if (ret < 0)
714 return ret;
715 }
716 return 0;
Mark Brown6ea31b92009-04-20 17:15:41 +0100717
718 default:
Mark Brown6d3ddc82009-05-16 17:47:29 +0100719 return dapm_generic_apply_power(w);
Mark Brown42aa3412009-03-01 19:21:10 +0000720 }
Mark Brown42aa3412009-03-01 19:21:10 +0000721}
722
723/*
Richard Purdie2b97eab2006-10-06 18:32:18 +0200724 * Scan each dapm widget for complete audio path.
725 * A complete path is a route that has valid endpoints i.e.:-
726 *
727 * o DAC to output pin.
728 * o Input Pin to ADC.
729 * o Input pin to Output pin (bypass, sidetone)
730 * o DAC to ADC (loopback).
731 */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100732static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200733{
Mark Brown452c5ea2009-05-17 21:41:23 +0100734 struct snd_soc_device *socdev = codec->socdev;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200735 struct snd_soc_dapm_widget *w;
Mark Brown6d3ddc82009-05-16 17:47:29 +0100736 int ret = 0;
737 int i, power;
Mark Brown452c5ea2009-05-17 21:41:23 +0100738 int sys_power = 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200739
Mark Brown6d3ddc82009-05-16 17:47:29 +0100740 INIT_LIST_HEAD(&codec->up_list);
741 INIT_LIST_HEAD(&codec->down_list);
742
743 /* Check which widgets we need to power and store them in
744 * lists indicating if they should be powered up or down.
745 */
746 list_for_each_entry(w, &codec->dapm_widgets, list) {
747 switch (w->id) {
748 case snd_soc_dapm_pre:
749 list_add_tail(&codec->down_list, &w->power_list);
750 break;
751 case snd_soc_dapm_post:
752 list_add_tail(&codec->up_list, &w->power_list);
753 break;
754
755 default:
756 if (!w->power_check)
757 continue;
758
759 power = w->power_check(w);
Mark Brown452c5ea2009-05-17 21:41:23 +0100760 if (power)
761 sys_power = 1;
762
Mark Brown6d3ddc82009-05-16 17:47:29 +0100763 if (w->power == power)
764 continue;
765
766 if (power)
767 list_add_tail(&w->power_list, &codec->up_list);
768 else
769 list_add_tail(&w->power_list,
770 &codec->down_list);
771
772 w->power = power;
773 break;
774 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200775 }
776
Mark Brown452c5ea2009-05-17 21:41:23 +0100777 /* If we're changing to all on or all off then prepare */
778 if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
779 (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
780 ret = snd_soc_dapm_set_bias_level(socdev,
781 SND_SOC_BIAS_PREPARE);
782 if (ret != 0)
783 pr_err("Failed to prepare bias: %d\n", ret);
784 }
785
Mark Brown6d3ddc82009-05-16 17:47:29 +0100786 /* Power down widgets first; try to avoid amplifying pops. */
787 for (i = 0; i < ARRAY_SIZE(dapm_down_seq); i++) {
788 list_for_each_entry(w, &codec->down_list, power_list) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200789 /* is widget in stream order */
Mark Brown6d3ddc82009-05-16 17:47:29 +0100790 if (w->id != dapm_down_seq[i])
Richard Purdie2b97eab2006-10-06 18:32:18 +0200791 continue;
792
Mark Brown42aa3412009-03-01 19:21:10 +0000793 ret = dapm_power_widget(codec, event, w);
794 if (ret != 0)
Mark Brown6d3ddc82009-05-16 17:47:29 +0100795 pr_err("Failed to power down %s: %d\n",
796 w->name, ret);
797 }
798 }
799
800 /* Now power up. */
801 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++) {
802 list_for_each_entry(w, &codec->up_list, power_list) {
803 /* is widget in stream order */
804 if (w->id != dapm_up_seq[i])
805 continue;
806
807 ret = dapm_power_widget(codec, event, w);
808 if (ret != 0)
809 pr_err("Failed to power up %s: %d\n",
810 w->name, ret);
Richard Purdie2b97eab2006-10-06 18:32:18 +0200811 }
812 }
813
Mark Brown452c5ea2009-05-17 21:41:23 +0100814 /* If we just powered the last thing off drop to standby bias */
815 if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
816 ret = snd_soc_dapm_set_bias_level(socdev,
817 SND_SOC_BIAS_STANDBY);
818 if (ret != 0)
819 pr_err("Failed to apply standby bias: %d\n", ret);
820 }
821
822 /* If we just powered up then move to active bias */
823 if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
824 ret = snd_soc_dapm_set_bias_level(socdev,
825 SND_SOC_BIAS_ON);
826 if (ret != 0)
827 pr_err("Failed to apply active bias: %d\n", ret);
828 }
829
Mark Brown42aa3412009-03-01 19:21:10 +0000830 return 0;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200831}
832
Mark Brownc1286b82008-07-07 19:26:03 +0100833#ifdef DEBUG
Richard Purdie2b97eab2006-10-06 18:32:18 +0200834static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
835{
836 struct snd_soc_dapm_widget *w;
837 struct snd_soc_dapm_path *p = NULL;
838 int in, out;
839
840 printk("DAPM %s %s\n", codec->name, action);
841
842 list_for_each_entry(w, &codec->dapm_widgets, list) {
843
844 /* only display widgets that effect routing */
845 switch (w->id) {
846 case snd_soc_dapm_pre:
847 case snd_soc_dapm_post:
848 case snd_soc_dapm_vmid:
849 continue;
850 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +0200851 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200852 case snd_soc_dapm_output:
853 case snd_soc_dapm_input:
854 case snd_soc_dapm_switch:
855 case snd_soc_dapm_hp:
856 case snd_soc_dapm_mic:
857 case snd_soc_dapm_spk:
858 case snd_soc_dapm_line:
859 case snd_soc_dapm_micbias:
860 case snd_soc_dapm_dac:
861 case snd_soc_dapm_adc:
862 case snd_soc_dapm_pga:
863 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000864 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +0100865 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200866 if (w->name) {
867 in = is_connected_input_ep(w);
868 dapm_clear_walk(w->codec);
869 out = is_connected_output_ep(w);
870 dapm_clear_walk(w->codec);
871 printk("%s: %s in %d out %d\n", w->name,
872 w->power ? "On":"Off",in, out);
873
874 list_for_each_entry(p, &w->sources, list_sink) {
875 if (p->connect)
876 printk(" in %s %s\n", p->name ? p->name : "static",
877 p->source->name);
878 }
879 list_for_each_entry(p, &w->sinks, list_source) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200880 if (p->connect)
881 printk(" out %s %s\n", p->name ? p->name : "static",
882 p->sink->name);
883 }
884 }
885 break;
886 }
887 }
888}
889#endif
890
891/* test and update the power status of a mux widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100892static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
893 struct snd_kcontrol *kcontrol, int mask,
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800894 int mux, int val, struct soc_enum *e)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200895{
896 struct snd_soc_dapm_path *path;
897 int found = 0;
898
Peter Ujfalusieff317d2009-01-15 14:40:47 +0200899 if (widget->id != snd_soc_dapm_mux &&
900 widget->id != snd_soc_dapm_value_mux)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200901 return -ENODEV;
902
903 if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
904 return 0;
905
906 /* find dapm widget path assoc with kcontrol */
907 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
908 if (path->kcontrol != kcontrol)
909 continue;
910
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800911 if (!path->name || !e->texts[mux])
Richard Purdie2b97eab2006-10-06 18:32:18 +0200912 continue;
913
914 found = 1;
915 /* we now need to match the string in the enum to the path */
Richard Zhaocb01e2b2008-10-07 08:05:20 +0800916 if (!(strcmp(path->name, e->texts[mux])))
Richard Purdie2b97eab2006-10-06 18:32:18 +0200917 path->connect = 1; /* new connection */
918 else
919 path->connect = 0; /* old connection must be powered down */
920 }
921
Mark Brown3fccd8b2008-07-07 19:26:04 +0100922 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200923 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100924 dump_dapm(widget->codec, "mux power update");
925 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200926
927 return 0;
928}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200929
Milan plzik1b075e32008-01-10 14:39:46 +0100930/* test and update the power status of a mixer or switch widget */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +0100931static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
932 struct snd_kcontrol *kcontrol, int reg,
933 int val_mask, int val, int invert)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200934{
935 struct snd_soc_dapm_path *path;
936 int found = 0;
937
Milan plzik1b075e32008-01-10 14:39:46 +0100938 if (widget->id != snd_soc_dapm_mixer &&
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000939 widget->id != snd_soc_dapm_mixer_named_ctl &&
Milan plzik1b075e32008-01-10 14:39:46 +0100940 widget->id != snd_soc_dapm_switch)
Richard Purdie2b97eab2006-10-06 18:32:18 +0200941 return -ENODEV;
942
943 if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
944 return 0;
945
946 /* find dapm widget path assoc with kcontrol */
947 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
948 if (path->kcontrol != kcontrol)
949 continue;
950
951 /* found, now check type */
952 found = 1;
953 if (val)
954 /* new connection */
955 path->connect = invert ? 0:1;
956 else
957 /* old connection must be powered down */
958 path->connect = invert ? 1:0;
959 break;
960 }
961
Mark Brown3fccd8b2008-07-07 19:26:04 +0100962 if (found) {
Richard Purdie2b97eab2006-10-06 18:32:18 +0200963 dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
Mark Brown3fccd8b2008-07-07 19:26:04 +0100964 dump_dapm(widget->codec, "mixer power update");
965 }
Richard Purdie2b97eab2006-10-06 18:32:18 +0200966
967 return 0;
968}
Richard Purdie2b97eab2006-10-06 18:32:18 +0200969
970/* show dapm widget status in sys fs */
971static ssize_t dapm_widget_show(struct device *dev,
972 struct device_attribute *attr, char *buf)
973{
974 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +0000975 struct snd_soc_codec *codec = devdata->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +0200976 struct snd_soc_dapm_widget *w;
977 int count = 0;
978 char *state = "not set";
979
980 list_for_each_entry(w, &codec->dapm_widgets, list) {
981
982 /* only display widgets that burnm power */
983 switch (w->id) {
984 case snd_soc_dapm_hp:
985 case snd_soc_dapm_mic:
986 case snd_soc_dapm_spk:
987 case snd_soc_dapm_line:
988 case snd_soc_dapm_micbias:
989 case snd_soc_dapm_dac:
990 case snd_soc_dapm_adc:
991 case snd_soc_dapm_pga:
992 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +0000993 case snd_soc_dapm_mixer_named_ctl:
Mark Brown246d0a12009-04-22 18:24:55 +0100994 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +0200995 if (w->name)
996 count += sprintf(buf + count, "%s: %s\n",
997 w->name, w->power ? "On":"Off");
998 break;
999 default:
1000 break;
1001 }
1002 }
1003
Mark Brown0be98982008-05-19 12:31:28 +02001004 switch (codec->bias_level) {
1005 case SND_SOC_BIAS_ON:
1006 state = "On";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001007 break;
Mark Brown0be98982008-05-19 12:31:28 +02001008 case SND_SOC_BIAS_PREPARE:
1009 state = "Prepare";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001010 break;
Mark Brown0be98982008-05-19 12:31:28 +02001011 case SND_SOC_BIAS_STANDBY:
1012 state = "Standby";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001013 break;
Mark Brown0be98982008-05-19 12:31:28 +02001014 case SND_SOC_BIAS_OFF:
1015 state = "Off";
Richard Purdie2b97eab2006-10-06 18:32:18 +02001016 break;
1017 }
1018 count += sprintf(buf + count, "PM State: %s\n", state);
1019
1020 return count;
1021}
1022
1023static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1024
1025int snd_soc_dapm_sys_add(struct device *dev)
1026{
Troy Kisky12ef1932008-10-13 17:42:14 -07001027 return device_create_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001028}
1029
1030static void snd_soc_dapm_sys_remove(struct device *dev)
1031{
Mark Brownaef90842009-05-16 17:53:16 +01001032 device_remove_file(dev, &dev_attr_dapm_widget);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001033}
1034
1035/* free all dapm widgets and resources */
Adrian Bunkd9c96cf2006-11-28 12:10:09 +01001036static void dapm_free_widgets(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001037{
1038 struct snd_soc_dapm_widget *w, *next_w;
1039 struct snd_soc_dapm_path *p, *next_p;
1040
1041 list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
1042 list_del(&w->list);
1043 kfree(w);
1044 }
1045
1046 list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
1047 list_del(&p->list);
1048 kfree(p->long_name);
1049 kfree(p);
1050 }
1051}
1052
Liam Girdwooda5302182008-07-07 13:35:17 +01001053static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
Mark Brown16499232009-01-07 18:25:13 +00001054 const char *pin, int status)
Liam Girdwooda5302182008-07-07 13:35:17 +01001055{
1056 struct snd_soc_dapm_widget *w;
1057
1058 list_for_each_entry(w, &codec->dapm_widgets, list) {
1059 if (!strcmp(w->name, pin)) {
Mark Brownc1286b82008-07-07 19:26:03 +01001060 pr_debug("dapm: %s: pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001061 w->connected = status;
1062 return 0;
1063 }
1064 }
1065
Mark Brownc1286b82008-07-07 19:26:03 +01001066 pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
Liam Girdwooda5302182008-07-07 13:35:17 +01001067 return -EINVAL;
1068}
1069
Richard Purdie2b97eab2006-10-06 18:32:18 +02001070/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001071 * snd_soc_dapm_sync - scan and power dapm paths
Richard Purdie2b97eab2006-10-06 18:32:18 +02001072 * @codec: audio codec
1073 *
1074 * Walks all dapm audio paths and powers widgets according to their
1075 * stream or path usage.
1076 *
1077 * Returns 0 for success.
1078 */
Liam Girdwooda5302182008-07-07 13:35:17 +01001079int snd_soc_dapm_sync(struct snd_soc_codec *codec)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001080{
Mark Brown3fccd8b2008-07-07 19:26:04 +01001081 int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
1082 dump_dapm(codec, "sync");
1083 return ret;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001084}
Liam Girdwooda5302182008-07-07 13:35:17 +01001085EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001086
Mark Brown105f1c22008-05-13 14:52:19 +02001087static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
1088 const char *sink, const char *control, const char *source)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001089{
1090 struct snd_soc_dapm_path *path;
1091 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1092 int ret = 0;
1093
1094 /* find src and dest widgets */
1095 list_for_each_entry(w, &codec->dapm_widgets, list) {
1096
1097 if (!wsink && !(strcmp(w->name, sink))) {
1098 wsink = w;
1099 continue;
1100 }
1101 if (!wsource && !(strcmp(w->name, source))) {
1102 wsource = w;
1103 }
1104 }
1105
1106 if (wsource == NULL || wsink == NULL)
1107 return -ENODEV;
1108
1109 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1110 if (!path)
1111 return -ENOMEM;
1112
1113 path->source = wsource;
1114 path->sink = wsink;
1115 INIT_LIST_HEAD(&path->list);
1116 INIT_LIST_HEAD(&path->list_source);
1117 INIT_LIST_HEAD(&path->list_sink);
1118
1119 /* check for external widgets */
1120 if (wsink->id == snd_soc_dapm_input) {
1121 if (wsource->id == snd_soc_dapm_micbias ||
1122 wsource->id == snd_soc_dapm_mic ||
Seth Forshee1e392212007-04-16 15:36:42 +02001123 wsink->id == snd_soc_dapm_line ||
1124 wsink->id == snd_soc_dapm_output)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001125 wsink->ext = 1;
1126 }
1127 if (wsource->id == snd_soc_dapm_output) {
1128 if (wsink->id == snd_soc_dapm_spk ||
1129 wsink->id == snd_soc_dapm_hp ||
Seth Forshee1e392212007-04-16 15:36:42 +02001130 wsink->id == snd_soc_dapm_line ||
1131 wsink->id == snd_soc_dapm_input)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001132 wsource->ext = 1;
1133 }
1134
1135 /* connect static paths */
1136 if (control == NULL) {
1137 list_add(&path->list, &codec->dapm_paths);
1138 list_add(&path->list_sink, &wsink->sources);
1139 list_add(&path->list_source, &wsource->sinks);
1140 path->connect = 1;
1141 return 0;
1142 }
1143
1144 /* connect dynamic paths */
1145 switch(wsink->id) {
1146 case snd_soc_dapm_adc:
1147 case snd_soc_dapm_dac:
1148 case snd_soc_dapm_pga:
1149 case snd_soc_dapm_input:
1150 case snd_soc_dapm_output:
1151 case snd_soc_dapm_micbias:
1152 case snd_soc_dapm_vmid:
1153 case snd_soc_dapm_pre:
1154 case snd_soc_dapm_post:
Mark Brown246d0a12009-04-22 18:24:55 +01001155 case snd_soc_dapm_supply:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001156 list_add(&path->list, &codec->dapm_paths);
1157 list_add(&path->list_sink, &wsink->sources);
1158 list_add(&path->list_source, &wsource->sinks);
1159 path->connect = 1;
1160 return 0;
1161 case snd_soc_dapm_mux:
Peter Ujfalusi74155552009-01-08 13:34:29 +02001162 case snd_soc_dapm_value_mux:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001163 ret = dapm_connect_mux(codec, wsource, wsink, path, control,
1164 &wsink->kcontrols[0]);
1165 if (ret != 0)
1166 goto err;
1167 break;
1168 case snd_soc_dapm_switch:
1169 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001170 case snd_soc_dapm_mixer_named_ctl:
Richard Purdie2b97eab2006-10-06 18:32:18 +02001171 ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
1172 if (ret != 0)
1173 goto err;
1174 break;
1175 case snd_soc_dapm_hp:
1176 case snd_soc_dapm_mic:
1177 case snd_soc_dapm_line:
1178 case snd_soc_dapm_spk:
1179 list_add(&path->list, &codec->dapm_paths);
1180 list_add(&path->list_sink, &wsink->sources);
1181 list_add(&path->list_source, &wsource->sinks);
1182 path->connect = 0;
1183 return 0;
1184 }
1185 return 0;
1186
1187err:
1188 printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
1189 control, sink);
1190 kfree(path);
1191 return ret;
1192}
Mark Brown105f1c22008-05-13 14:52:19 +02001193
1194/**
Mark Brown105f1c22008-05-13 14:52:19 +02001195 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1196 * @codec: codec
1197 * @route: audio routes
1198 * @num: number of routes
1199 *
1200 * Connects 2 dapm widgets together via a named audio path. The sink is
1201 * the widget receiving the audio signal, whilst the source is the sender
1202 * of the audio signal.
1203 *
1204 * Returns 0 for success else error. On error all resources can be freed
1205 * with a call to snd_soc_card_free().
1206 */
1207int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
1208 const struct snd_soc_dapm_route *route, int num)
1209{
1210 int i, ret;
1211
1212 for (i = 0; i < num; i++) {
1213 ret = snd_soc_dapm_add_route(codec, route->sink,
1214 route->control, route->source);
1215 if (ret < 0) {
1216 printk(KERN_ERR "Failed to add route %s->%s\n",
1217 route->source,
1218 route->sink);
1219 return ret;
1220 }
1221 route++;
1222 }
1223
1224 return 0;
1225}
1226EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1227
1228/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001229 * snd_soc_dapm_new_widgets - add new dapm widgets
1230 * @codec: audio codec
1231 *
1232 * Checks the codec for any new dapm widgets and creates them if found.
1233 *
1234 * Returns 0 for success.
1235 */
1236int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
1237{
1238 struct snd_soc_dapm_widget *w;
1239
Richard Purdie2b97eab2006-10-06 18:32:18 +02001240 list_for_each_entry(w, &codec->dapm_widgets, list)
1241 {
1242 if (w->new)
1243 continue;
1244
1245 switch(w->id) {
1246 case snd_soc_dapm_switch:
1247 case snd_soc_dapm_mixer:
Ian Moltonca9c1aa2009-01-06 20:11:51 +00001248 case snd_soc_dapm_mixer_named_ctl:
Mark Brownb75576d2009-04-20 17:56:13 +01001249 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001250 dapm_new_mixer(codec, w);
1251 break;
1252 case snd_soc_dapm_mux:
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001253 case snd_soc_dapm_value_mux:
Mark Brownb75576d2009-04-20 17:56:13 +01001254 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001255 dapm_new_mux(codec, w);
1256 break;
1257 case snd_soc_dapm_adc:
Mark Brownb75576d2009-04-20 17:56:13 +01001258 w->power_check = dapm_adc_check_power;
1259 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001260 case snd_soc_dapm_dac:
Mark Brownb75576d2009-04-20 17:56:13 +01001261 w->power_check = dapm_dac_check_power;
1262 break;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001263 case snd_soc_dapm_pga:
Mark Brownb75576d2009-04-20 17:56:13 +01001264 w->power_check = dapm_generic_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001265 dapm_new_pga(codec, w);
1266 break;
1267 case snd_soc_dapm_input:
1268 case snd_soc_dapm_output:
1269 case snd_soc_dapm_micbias:
1270 case snd_soc_dapm_spk:
1271 case snd_soc_dapm_hp:
1272 case snd_soc_dapm_mic:
1273 case snd_soc_dapm_line:
Mark Brownb75576d2009-04-20 17:56:13 +01001274 w->power_check = dapm_generic_check_power;
1275 break;
Mark Brown246d0a12009-04-22 18:24:55 +01001276 case snd_soc_dapm_supply:
1277 w->power_check = dapm_supply_check_power;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001278 case snd_soc_dapm_vmid:
1279 case snd_soc_dapm_pre:
1280 case snd_soc_dapm_post:
1281 break;
1282 }
1283 w->new = 1;
1284 }
1285
1286 dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001287 return 0;
1288}
1289EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
1290
1291/**
1292 * snd_soc_dapm_get_volsw - dapm mixer get callback
1293 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001294 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001295 *
1296 * Callback to get the value of a dapm mixer control.
1297 *
1298 * Returns 0 for success.
1299 */
1300int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
1301 struct snd_ctl_elem_value *ucontrol)
1302{
1303 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001304 struct soc_mixer_control *mc =
1305 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04001306 unsigned int reg = mc->reg;
1307 unsigned int shift = mc->shift;
1308 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001309 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001310 unsigned int invert = mc->invert;
1311 unsigned int mask = (1 << fls(max)) - 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001312
1313 /* return the saved value if we are powered down */
1314 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1315 ucontrol->value.integer.value[0] = widget->saved_value;
1316 return 0;
1317 }
1318
1319 ucontrol->value.integer.value[0] =
1320 (snd_soc_read(widget->codec, reg) >> shift) & mask;
1321 if (shift != rshift)
1322 ucontrol->value.integer.value[1] =
1323 (snd_soc_read(widget->codec, reg) >> rshift) & mask;
1324 if (invert) {
1325 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001326 max - ucontrol->value.integer.value[0];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001327 if (shift != rshift)
1328 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001329 max - ucontrol->value.integer.value[1];
Richard Purdie2b97eab2006-10-06 18:32:18 +02001330 }
1331
1332 return 0;
1333}
1334EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
1335
1336/**
1337 * snd_soc_dapm_put_volsw - dapm mixer set callback
1338 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001339 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001340 *
1341 * Callback to set the value of a dapm mixer control.
1342 *
1343 * Returns 0 for success.
1344 */
1345int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
1346 struct snd_ctl_elem_value *ucontrol)
1347{
1348 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01001349 struct soc_mixer_control *mc =
1350 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf82008-07-29 10:22:24 -04001351 unsigned int reg = mc->reg;
1352 unsigned int shift = mc->shift;
1353 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001354 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001355 unsigned int mask = (1 << fls(max)) - 1;
1356 unsigned int invert = mc->invert;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001357 unsigned short val, val2, val_mask;
1358 int ret;
1359
1360 val = (ucontrol->value.integer.value[0] & mask);
1361
1362 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001363 val = max - val;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001364 val_mask = mask << shift;
1365 val = val << shift;
1366 if (shift != rshift) {
1367 val2 = (ucontrol->value.integer.value[1] & mask);
1368 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001369 val2 = max - val2;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001370 val_mask |= mask << rshift;
1371 val |= val2 << rshift;
1372 }
1373
1374 mutex_lock(&widget->codec->mutex);
1375 widget->value = val;
1376
1377 /* save volume value if the widget is powered down */
1378 if (widget->id == snd_soc_dapm_pga && !widget->power) {
1379 widget->saved_value = val;
1380 mutex_unlock(&widget->codec->mutex);
1381 return 1;
1382 }
1383
1384 dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
1385 if (widget->event) {
1386 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001387 ret = widget->event(widget, kcontrol,
1388 SND_SOC_DAPM_PRE_REG);
1389 if (ret < 0) {
1390 ret = 1;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001391 goto out;
Laim Girdwood9af6d952008-01-10 14:41:02 +01001392 }
Richard Purdie2b97eab2006-10-06 18:32:18 +02001393 }
1394 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1395 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001396 ret = widget->event(widget, kcontrol,
1397 SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001398 } else
1399 ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
1400
1401out:
1402 mutex_unlock(&widget->codec->mutex);
1403 return ret;
1404}
1405EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
1406
1407/**
1408 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
1409 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001410 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001411 *
1412 * Callback to get the value of a dapm enumerated double mixer control.
1413 *
1414 * Returns 0 for success.
1415 */
1416int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
1417 struct snd_ctl_elem_value *ucontrol)
1418{
1419 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1420 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1421 unsigned short val, bitmask;
1422
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001423 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001424 ;
1425 val = snd_soc_read(widget->codec, e->reg);
1426 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1427 if (e->shift_l != e->shift_r)
1428 ucontrol->value.enumerated.item[1] =
1429 (val >> e->shift_r) & (bitmask - 1);
1430
1431 return 0;
1432}
1433EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
1434
1435/**
1436 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
1437 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001438 * @ucontrol: control element information
Richard Purdie2b97eab2006-10-06 18:32:18 +02001439 *
1440 * Callback to set the value of a dapm enumerated double mixer control.
1441 *
1442 * Returns 0 for success.
1443 */
1444int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
1445 struct snd_ctl_elem_value *ucontrol)
1446{
1447 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
1448 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1449 unsigned short val, mux;
1450 unsigned short mask, bitmask;
1451 int ret = 0;
1452
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001453 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001454 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001455 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001456 return -EINVAL;
1457 mux = ucontrol->value.enumerated.item[0];
1458 val = mux << e->shift_l;
1459 mask = (bitmask - 1) << e->shift_l;
1460 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001461 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001462 return -EINVAL;
1463 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1464 mask |= (bitmask - 1) << e->shift_r;
1465 }
1466
1467 mutex_lock(&widget->codec->mutex);
1468 widget->value = val;
Richard Zhaocb01e2b2008-10-07 08:05:20 +08001469 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001470 if (widget->event) {
1471 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
Laim Girdwood9af6d952008-01-10 14:41:02 +01001472 ret = widget->event(widget,
1473 kcontrol, SND_SOC_DAPM_PRE_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001474 if (ret < 0)
1475 goto out;
1476 }
1477 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1478 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
Laim Girdwood9af6d952008-01-10 14:41:02 +01001479 ret = widget->event(widget,
1480 kcontrol, SND_SOC_DAPM_POST_REG);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001481 } else
1482 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1483
1484out:
1485 mutex_unlock(&widget->codec->mutex);
1486 return ret;
1487}
1488EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
1489
1490/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001491 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
1492 * callback
1493 * @kcontrol: mixer control
1494 * @ucontrol: control element information
1495 *
1496 * Callback to get the value of a dapm semi enumerated double mixer control.
1497 *
1498 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1499 * used for handling bitfield coded enumeration for example.
1500 *
1501 * Returns 0 for success.
1502 */
1503int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
1504 struct snd_ctl_elem_value *ucontrol)
1505{
1506 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001507 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001508 unsigned short reg_val, val, mux;
1509
1510 reg_val = snd_soc_read(widget->codec, e->reg);
1511 val = (reg_val >> e->shift_l) & e->mask;
1512 for (mux = 0; mux < e->max; mux++) {
1513 if (val == e->values[mux])
1514 break;
1515 }
1516 ucontrol->value.enumerated.item[0] = mux;
1517 if (e->shift_l != e->shift_r) {
1518 val = (reg_val >> e->shift_r) & e->mask;
1519 for (mux = 0; mux < e->max; mux++) {
1520 if (val == e->values[mux])
1521 break;
1522 }
1523 ucontrol->value.enumerated.item[1] = mux;
1524 }
1525
1526 return 0;
1527}
1528EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
1529
1530/**
1531 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
1532 * callback
1533 * @kcontrol: mixer control
1534 * @ucontrol: control element information
1535 *
1536 * Callback to set the value of a dapm semi enumerated double mixer control.
1537 *
1538 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1539 * used for handling bitfield coded enumeration for example.
1540 *
1541 * Returns 0 for success.
1542 */
1543int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
1544 struct snd_ctl_elem_value *ucontrol)
1545{
1546 struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001547 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001548 unsigned short val, mux;
1549 unsigned short mask;
1550 int ret = 0;
1551
1552 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1553 return -EINVAL;
1554 mux = ucontrol->value.enumerated.item[0];
1555 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1556 mask = e->mask << e->shift_l;
1557 if (e->shift_l != e->shift_r) {
1558 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1559 return -EINVAL;
1560 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1561 mask |= e->mask << e->shift_r;
1562 }
1563
1564 mutex_lock(&widget->codec->mutex);
1565 widget->value = val;
Peter Ujfalusi74155552009-01-08 13:34:29 +02001566 dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001567 if (widget->event) {
1568 if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
1569 ret = widget->event(widget,
1570 kcontrol, SND_SOC_DAPM_PRE_REG);
1571 if (ret < 0)
1572 goto out;
1573 }
1574 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1575 if (widget->event_flags & SND_SOC_DAPM_POST_REG)
1576 ret = widget->event(widget,
1577 kcontrol, SND_SOC_DAPM_POST_REG);
1578 } else
1579 ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
1580
1581out:
1582 mutex_unlock(&widget->codec->mutex);
1583 return ret;
1584}
1585EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
1586
1587/**
Mark Brown8b37dbd2009-02-28 21:14:20 +00001588 * snd_soc_dapm_info_pin_switch - Info for a pin switch
1589 *
1590 * @kcontrol: mixer control
1591 * @uinfo: control element information
1592 *
1593 * Callback to provide information about a pin switch control.
1594 */
1595int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
1596 struct snd_ctl_elem_info *uinfo)
1597{
1598 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1599 uinfo->count = 1;
1600 uinfo->value.integer.min = 0;
1601 uinfo->value.integer.max = 1;
1602
1603 return 0;
1604}
1605EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
1606
1607/**
1608 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
1609 *
1610 * @kcontrol: mixer control
1611 * @ucontrol: Value
1612 */
1613int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
1614 struct snd_ctl_elem_value *ucontrol)
1615{
1616 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1617 const char *pin = (const char *)kcontrol->private_value;
1618
1619 mutex_lock(&codec->mutex);
1620
1621 ucontrol->value.integer.value[0] =
1622 snd_soc_dapm_get_pin_status(codec, pin);
1623
1624 mutex_unlock(&codec->mutex);
1625
1626 return 0;
1627}
1628EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
1629
1630/**
1631 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
1632 *
1633 * @kcontrol: mixer control
1634 * @ucontrol: Value
1635 */
1636int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
1637 struct snd_ctl_elem_value *ucontrol)
1638{
1639 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1640 const char *pin = (const char *)kcontrol->private_value;
1641
1642 mutex_lock(&codec->mutex);
1643
1644 if (ucontrol->value.integer.value[0])
1645 snd_soc_dapm_enable_pin(codec, pin);
1646 else
1647 snd_soc_dapm_disable_pin(codec, pin);
1648
1649 snd_soc_dapm_sync(codec);
1650
1651 mutex_unlock(&codec->mutex);
1652
1653 return 0;
1654}
1655EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
1656
1657/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001658 * snd_soc_dapm_new_control - create new dapm control
1659 * @codec: audio codec
1660 * @widget: widget template
1661 *
1662 * Creates a new dapm control based upon the template.
1663 *
1664 * Returns 0 for success else error.
1665 */
1666int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
1667 const struct snd_soc_dapm_widget *widget)
1668{
1669 struct snd_soc_dapm_widget *w;
1670
1671 if ((w = dapm_cnew_widget(widget)) == NULL)
1672 return -ENOMEM;
1673
1674 w->codec = codec;
1675 INIT_LIST_HEAD(&w->sources);
1676 INIT_LIST_HEAD(&w->sinks);
1677 INIT_LIST_HEAD(&w->list);
1678 list_add(&w->list, &codec->dapm_widgets);
1679
1680 /* machine layer set ups unconnected pins and insertions */
1681 w->connected = 1;
1682 return 0;
1683}
1684EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
1685
1686/**
Mark Brown4ba13272008-05-13 14:51:19 +02001687 * snd_soc_dapm_new_controls - create new dapm controls
1688 * @codec: audio codec
1689 * @widget: widget array
1690 * @num: number of widgets
1691 *
1692 * Creates new DAPM controls based upon the templates.
1693 *
1694 * Returns 0 for success else error.
1695 */
1696int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
1697 const struct snd_soc_dapm_widget *widget,
1698 int num)
1699{
1700 int i, ret;
1701
1702 for (i = 0; i < num; i++) {
1703 ret = snd_soc_dapm_new_control(codec, widget);
Mark Brownb8b33cb2008-12-18 11:19:30 +00001704 if (ret < 0) {
1705 printk(KERN_ERR
1706 "ASoC: Failed to create DAPM control %s: %d\n",
1707 widget->name, ret);
Mark Brown4ba13272008-05-13 14:51:19 +02001708 return ret;
Mark Brownb8b33cb2008-12-18 11:19:30 +00001709 }
Mark Brown4ba13272008-05-13 14:51:19 +02001710 widget++;
1711 }
1712 return 0;
1713}
1714EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
1715
1716
1717/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001718 * snd_soc_dapm_stream_event - send a stream event to the dapm core
1719 * @codec: audio codec
1720 * @stream: stream name
1721 * @event: stream event
1722 *
1723 * Sends a stream event to the dapm core. The core then makes any
1724 * necessary widget power changes.
1725 *
1726 * Returns 0 for success else error.
1727 */
1728int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
1729 char *stream, int event)
1730{
1731 struct snd_soc_dapm_widget *w;
1732
Seth Forshee11da21a2007-02-02 17:14:19 +01001733 if (stream == NULL)
1734 return 0;
1735
Richard Purdie2b97eab2006-10-06 18:32:18 +02001736 mutex_lock(&codec->mutex);
1737 list_for_each_entry(w, &codec->dapm_widgets, list)
1738 {
1739 if (!w->sname)
1740 continue;
Mark Brownc1286b82008-07-07 19:26:03 +01001741 pr_debug("widget %s\n %s stream %s event %d\n",
1742 w->name, w->sname, stream, event);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001743 if (strstr(w->sname, stream)) {
1744 switch(event) {
1745 case SND_SOC_DAPM_STREAM_START:
1746 w->active = 1;
1747 break;
1748 case SND_SOC_DAPM_STREAM_STOP:
1749 w->active = 0;
1750 break;
1751 case SND_SOC_DAPM_STREAM_SUSPEND:
1752 if (w->active)
1753 w->suspend = 1;
1754 w->active = 0;
1755 break;
1756 case SND_SOC_DAPM_STREAM_RESUME:
1757 if (w->suspend) {
1758 w->active = 1;
1759 w->suspend = 0;
1760 }
1761 break;
1762 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
1763 break;
1764 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
1765 break;
1766 }
1767 }
1768 }
1769 mutex_unlock(&codec->mutex);
1770
1771 dapm_power_widgets(codec, event);
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -08001772 dump_dapm(codec, __func__);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001773 return 0;
1774}
1775EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
1776
1777/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001778 * snd_soc_dapm_enable_pin - enable pin.
Mark Brownac11a2b2009-01-01 12:18:17 +00001779 * @codec: SoC codec
Liam Girdwooda5302182008-07-07 13:35:17 +01001780 * @pin: pin name
Richard Purdie2b97eab2006-10-06 18:32:18 +02001781 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001782 * Enables input/output pin and it's parents or children widgets iff there is
1783 * a valid audio route and active audio stream.
1784 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1785 * do any widget power switching.
Richard Purdie2b97eab2006-10-06 18:32:18 +02001786 */
Mark Brown16499232009-01-07 18:25:13 +00001787int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
Richard Purdie2b97eab2006-10-06 18:32:18 +02001788{
Liam Girdwooda5302182008-07-07 13:35:17 +01001789 return snd_soc_dapm_set_pin(codec, pin, 1);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001790}
Liam Girdwooda5302182008-07-07 13:35:17 +01001791EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
Richard Purdie2b97eab2006-10-06 18:32:18 +02001792
1793/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001794 * snd_soc_dapm_disable_pin - disable pin.
1795 * @codec: SoC codec
1796 * @pin: pin name
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001797 *
Liam Girdwooda5302182008-07-07 13:35:17 +01001798 * Disables input/output pin and it's parents or children widgets.
1799 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1800 * do any widget power switching.
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001801 */
Mark Brown16499232009-01-07 18:25:13 +00001802int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
Liam Girdwooda5302182008-07-07 13:35:17 +01001803{
1804 return snd_soc_dapm_set_pin(codec, pin, 0);
1805}
1806EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
1807
1808/**
Mark Brown5817b522008-09-24 11:23:11 +01001809 * snd_soc_dapm_nc_pin - permanently disable pin.
1810 * @codec: SoC codec
1811 * @pin: pin name
1812 *
1813 * Marks the specified pin as being not connected, disabling it along
1814 * any parent or child widgets. At present this is identical to
1815 * snd_soc_dapm_disable_pin() but in future it will be extended to do
1816 * additional things such as disabling controls which only affect
1817 * paths through the pin.
1818 *
1819 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
1820 * do any widget power switching.
1821 */
Mark Brown16499232009-01-07 18:25:13 +00001822int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
Mark Brown5817b522008-09-24 11:23:11 +01001823{
1824 return snd_soc_dapm_set_pin(codec, pin, 0);
1825}
1826EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
1827
1828/**
Liam Girdwooda5302182008-07-07 13:35:17 +01001829 * snd_soc_dapm_get_pin_status - get audio pin status
1830 * @codec: audio codec
1831 * @pin: audio signal pin endpoint (or start point)
1832 *
1833 * Get audio pin status - connected or disconnected.
1834 *
1835 * Returns 1 for connected otherwise 0.
1836 */
Mark Brown16499232009-01-07 18:25:13 +00001837int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001838{
1839 struct snd_soc_dapm_widget *w;
1840
1841 list_for_each_entry(w, &codec->dapm_widgets, list) {
Liam Girdwooda5302182008-07-07 13:35:17 +01001842 if (!strcmp(w->name, pin))
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001843 return w->connected;
1844 }
1845
1846 return 0;
1847}
Liam Girdwooda5302182008-07-07 13:35:17 +01001848EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
Graeme Gregoryeeec12b2008-04-30 19:27:40 +02001849
1850/**
Richard Purdie2b97eab2006-10-06 18:32:18 +02001851 * snd_soc_dapm_free - free dapm resources
1852 * @socdev: SoC device
1853 *
1854 * Free all dapm widgets and resources.
1855 */
1856void snd_soc_dapm_free(struct snd_soc_device *socdev)
1857{
Mark Brown6627a652009-01-23 22:55:23 +00001858 struct snd_soc_codec *codec = socdev->card->codec;
Richard Purdie2b97eab2006-10-06 18:32:18 +02001859
1860 snd_soc_dapm_sys_remove(socdev->dev);
1861 dapm_free_widgets(codec);
1862}
1863EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
1864
1865/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01001866MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Richard Purdie2b97eab2006-10-06 18:32:18 +02001867MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
1868MODULE_LICENSE("GPL");