ASoC: TWL4030: Do not alter the Headset output volume on power-up/down
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / soc / soc-core.c
CommitLineData
db2a4165
FM
1/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
0664d888
LG
5 * Copyright 2005 Openedhand Ltd.
6 *
d331124d 7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
0664d888
LG
8 * with code, comments and ideas from :-
9 * Richard Purdie <richard@openedhand.com>
db2a4165
FM
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
db2a4165
FM
16 * TODO:
17 * o Add hw rules to enforce rates, etc.
18 * o More testing with other codecs/machines.
19 * o Add more codecs and platforms to ensure good API coverage.
20 * o Support TDM on PCM and I2S
21 */
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/pm.h>
28#include <linux/bitops.h>
12ef193d 29#include <linux/debugfs.h>
db2a4165 30#include <linux/platform_device.h>
db2a4165
FM
31#include <sound/core.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/soc.h>
35#include <sound/soc-dapm.h>
36#include <sound/initval.h>
37
db2a4165
FM
38static DEFINE_MUTEX(pcm_mutex);
39static DEFINE_MUTEX(io_mutex);
db2a4165
FM
40static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
41
384c89e2
MB
42#ifdef CONFIG_DEBUG_FS
43static struct dentry *debugfs_root;
44#endif
45
db2a4165
FM
46/*
47 * This is a timeout to do a DAPM powerdown after a stream is closed().
48 * It can be used to eliminate pops between different playback streams, e.g.
49 * between two audio tracks.
50 */
51static int pmdown_time = 5000;
52module_param(pmdown_time, int, 0);
53MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
54
965ac42c
LG
55/*
56 * This function forces any delayed work to be queued and run.
57 */
58static int run_delayed_work(struct delayed_work *dwork)
59{
60 int ret;
61
62 /* cancel any work waiting to be queued. */
63 ret = cancel_delayed_work(dwork);
64
65 /* if there was any work waiting then we run it now and
66 * wait for it's completion */
67 if (ret) {
68 schedule_delayed_work(dwork, 0);
69 flush_scheduled_work();
70 }
71 return ret;
72}
73
db2a4165
FM
74#ifdef CONFIG_SND_SOC_AC97_BUS
75/* unregister ac97 codec */
76static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
77{
78 if (codec->ac97->dev.bus)
79 device_unregister(&codec->ac97->dev);
80 return 0;
81}
82
83/* stop no dev release warning */
84static void soc_ac97_device_release(struct device *dev){}
85
86/* register ac97 codec to bus */
87static int soc_ac97_dev_register(struct snd_soc_codec *codec)
88{
89 int err;
90
91 codec->ac97->dev.bus = &ac97_bus_type;
92 codec->ac97->dev.parent = NULL;
93 codec->ac97->dev.release = soc_ac97_device_release;
94
bb072bf0
KS
95 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
96 codec->card->number, 0, codec->name);
db2a4165
FM
97 err = device_register(&codec->ac97->dev);
98 if (err < 0) {
99 snd_printk(KERN_ERR "Can't register ac97 bus\n");
100 codec->ac97->dev.bus = NULL;
101 return err;
102 }
103 return 0;
104}
105#endif
106
db2a4165
FM
107/*
108 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
109 * then initialized and any private data can be allocated. This also calls
110 * startup for the cpu DAI, platform, machine and codec DAI.
111 */
112static int soc_pcm_open(struct snd_pcm_substream *substream)
113{
114 struct snd_soc_pcm_runtime *rtd = substream->private_data;
115 struct snd_soc_device *socdev = rtd->socdev;
87689d56 116 struct snd_soc_card *card = socdev->card;
db2a4165 117 struct snd_pcm_runtime *runtime = substream->runtime;
cb666e5b 118 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 119 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
120 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
121 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
122 int ret = 0;
123
124 mutex_lock(&pcm_mutex);
125
126 /* startup the audio subsystem */
cb666e5b 127 if (cpu_dai->ops.startup) {
dee89c4d 128 ret = cpu_dai->ops.startup(substream, cpu_dai);
db2a4165
FM
129 if (ret < 0) {
130 printk(KERN_ERR "asoc: can't open interface %s\n",
cb666e5b 131 cpu_dai->name);
db2a4165
FM
132 goto out;
133 }
134 }
135
136 if (platform->pcm_ops->open) {
137 ret = platform->pcm_ops->open(substream);
138 if (ret < 0) {
139 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
140 goto platform_err;
141 }
142 }
143
cb666e5b 144 if (codec_dai->ops.startup) {
dee89c4d 145 ret = codec_dai->ops.startup(substream, codec_dai);
db2a4165 146 if (ret < 0) {
cb666e5b
LG
147 printk(KERN_ERR "asoc: can't open codec %s\n",
148 codec_dai->name);
149 goto codec_dai_err;
db2a4165
FM
150 }
151 }
152
cb666e5b
LG
153 if (machine->ops && machine->ops->startup) {
154 ret = machine->ops->startup(substream);
db2a4165 155 if (ret < 0) {
cb666e5b
LG
156 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
157 goto machine_err;
db2a4165
FM
158 }
159 }
160
db2a4165
FM
161 /* Check that the codec and cpu DAI's are compatible */
162 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
163 runtime->hw.rate_min =
3ff3f64b
MB
164 max(codec_dai->playback.rate_min,
165 cpu_dai->playback.rate_min);
db2a4165 166 runtime->hw.rate_max =
3ff3f64b
MB
167 min(codec_dai->playback.rate_max,
168 cpu_dai->playback.rate_max);
db2a4165 169 runtime->hw.channels_min =
cb666e5b
LG
170 max(codec_dai->playback.channels_min,
171 cpu_dai->playback.channels_min);
db2a4165 172 runtime->hw.channels_max =
cb666e5b
LG
173 min(codec_dai->playback.channels_max,
174 cpu_dai->playback.channels_max);
175 runtime->hw.formats =
176 codec_dai->playback.formats & cpu_dai->playback.formats;
177 runtime->hw.rates =
178 codec_dai->playback.rates & cpu_dai->playback.rates;
db2a4165
FM
179 } else {
180 runtime->hw.rate_min =
3ff3f64b
MB
181 max(codec_dai->capture.rate_min,
182 cpu_dai->capture.rate_min);
db2a4165 183 runtime->hw.rate_max =
3ff3f64b
MB
184 min(codec_dai->capture.rate_max,
185 cpu_dai->capture.rate_max);
db2a4165 186 runtime->hw.channels_min =
cb666e5b
LG
187 max(codec_dai->capture.channels_min,
188 cpu_dai->capture.channels_min);
db2a4165 189 runtime->hw.channels_max =
cb666e5b
LG
190 min(codec_dai->capture.channels_max,
191 cpu_dai->capture.channels_max);
192 runtime->hw.formats =
193 codec_dai->capture.formats & cpu_dai->capture.formats;
194 runtime->hw.rates =
195 codec_dai->capture.rates & cpu_dai->capture.rates;
db2a4165
FM
196 }
197
198 snd_pcm_limit_hw_rates(runtime);
199 if (!runtime->hw.rates) {
200 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
cb666e5b
LG
201 codec_dai->name, cpu_dai->name);
202 goto machine_err;
db2a4165
FM
203 }
204 if (!runtime->hw.formats) {
205 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
cb666e5b
LG
206 codec_dai->name, cpu_dai->name);
207 goto machine_err;
db2a4165
FM
208 }
209 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
210 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
cb666e5b
LG
211 codec_dai->name, cpu_dai->name);
212 goto machine_err;
db2a4165
FM
213 }
214
f24368c2
MB
215 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
216 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
217 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
218 runtime->hw.channels_max);
219 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
220 runtime->hw.rate_max);
db2a4165 221
db2a4165 222 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
cb666e5b 223 cpu_dai->playback.active = codec_dai->playback.active = 1;
db2a4165 224 else
cb666e5b
LG
225 cpu_dai->capture.active = codec_dai->capture.active = 1;
226 cpu_dai->active = codec_dai->active = 1;
227 cpu_dai->runtime = runtime;
db2a4165
FM
228 socdev->codec->active++;
229 mutex_unlock(&pcm_mutex);
230 return 0;
231
cb666e5b 232machine_err:
db2a4165
FM
233 if (machine->ops && machine->ops->shutdown)
234 machine->ops->shutdown(substream);
235
cb666e5b 236codec_dai_err:
db2a4165
FM
237 if (platform->pcm_ops->close)
238 platform->pcm_ops->close(substream);
239
240platform_err:
cb666e5b 241 if (cpu_dai->ops.shutdown)
dee89c4d 242 cpu_dai->ops.shutdown(substream, cpu_dai);
db2a4165
FM
243out:
244 mutex_unlock(&pcm_mutex);
245 return ret;
246}
247
248/*
3a4fa0a2 249 * Power down the audio subsystem pmdown_time msecs after close is called.
db2a4165
FM
250 * This is to ensure there are no pops or clicks in between any music tracks
251 * due to DAPM power cycling.
252 */
4484bb2e 253static void close_delayed_work(struct work_struct *work)
db2a4165 254{
6308419a
MB
255 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
256 delayed_work.work);
257 struct snd_soc_device *socdev = card->socdev;
db2a4165 258 struct snd_soc_codec *codec = socdev->codec;
3c4b266f 259 struct snd_soc_dai *codec_dai;
db2a4165
FM
260 int i;
261
262 mutex_lock(&pcm_mutex);
3ff3f64b 263 for (i = 0; i < codec->num_dai; i++) {
db2a4165
FM
264 codec_dai = &codec->dai[i];
265
f24368c2
MB
266 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
267 codec_dai->playback.stream_name,
268 codec_dai->playback.active ? "active" : "inactive",
269 codec_dai->pop_wait ? "yes" : "no");
db2a4165
FM
270
271 /* are we waiting on this codec DAI stream */
272 if (codec_dai->pop_wait == 1) {
273
0be9898a 274 /* Reduce power if no longer active */
3c1c47e0 275 if (codec->active == 0) {
f24368c2
MB
276 pr_debug("pop wq D1 %s %s\n", codec->name,
277 codec_dai->playback.stream_name);
0be9898a
MB
278 snd_soc_dapm_set_bias_level(socdev,
279 SND_SOC_BIAS_PREPARE);
3c1c47e0
LG
280 }
281
db2a4165 282 codec_dai->pop_wait = 0;
0b4d221b
LG
283 snd_soc_dapm_stream_event(codec,
284 codec_dai->playback.stream_name,
db2a4165
FM
285 SND_SOC_DAPM_STREAM_STOP);
286
0be9898a 287 /* Fall into standby if no longer active */
db2a4165 288 if (codec->active == 0) {
f24368c2
MB
289 pr_debug("pop wq D3 %s %s\n", codec->name,
290 codec_dai->playback.stream_name);
0be9898a
MB
291 snd_soc_dapm_set_bias_level(socdev,
292 SND_SOC_BIAS_STANDBY);
db2a4165
FM
293 }
294 }
295 }
296 mutex_unlock(&pcm_mutex);
297}
298
299/*
300 * Called by ALSA when a PCM substream is closed. Private data can be
301 * freed here. The cpu DAI, codec DAI, machine and platform are also
302 * shutdown.
303 */
304static int soc_codec_close(struct snd_pcm_substream *substream)
305{
306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
307 struct snd_soc_device *socdev = rtd->socdev;
6308419a 308 struct snd_soc_card *card = socdev->card;
cb666e5b 309 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 310 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
311 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
312 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
313 struct snd_soc_codec *codec = socdev->codec;
314
315 mutex_lock(&pcm_mutex);
316
317 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
cb666e5b 318 cpu_dai->playback.active = codec_dai->playback.active = 0;
db2a4165 319 else
cb666e5b 320 cpu_dai->capture.active = codec_dai->capture.active = 0;
db2a4165 321
cb666e5b
LG
322 if (codec_dai->playback.active == 0 &&
323 codec_dai->capture.active == 0) {
324 cpu_dai->active = codec_dai->active = 0;
db2a4165
FM
325 }
326 codec->active--;
327
6010b2da
MB
328 /* Muting the DAC suppresses artifacts caused during digital
329 * shutdown, for example from stopping clocks.
330 */
331 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
332 snd_soc_dai_digital_mute(codec_dai, 1);
333
cb666e5b 334 if (cpu_dai->ops.shutdown)
dee89c4d 335 cpu_dai->ops.shutdown(substream, cpu_dai);
db2a4165 336
cb666e5b 337 if (codec_dai->ops.shutdown)
dee89c4d 338 codec_dai->ops.shutdown(substream, codec_dai);
db2a4165
FM
339
340 if (machine->ops && machine->ops->shutdown)
341 machine->ops->shutdown(substream);
342
343 if (platform->pcm_ops->close)
344 platform->pcm_ops->close(substream);
cb666e5b 345 cpu_dai->runtime = NULL;
db2a4165
FM
346
347 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
348 /* start delayed pop wq here for playback streams */
cb666e5b 349 codec_dai->pop_wait = 1;
6308419a 350 schedule_delayed_work(&card->delayed_work,
db2a4165
FM
351 msecs_to_jiffies(pmdown_time));
352 } else {
353 /* capture streams can be powered down now */
cb666e5b 354 snd_soc_dapm_stream_event(codec,
0b4d221b
LG
355 codec_dai->capture.stream_name,
356 SND_SOC_DAPM_STREAM_STOP);
db2a4165 357
0b4d221b 358 if (codec->active == 0 && codec_dai->pop_wait == 0)
0be9898a
MB
359 snd_soc_dapm_set_bias_level(socdev,
360 SND_SOC_BIAS_STANDBY);
db2a4165
FM
361 }
362
363 mutex_unlock(&pcm_mutex);
364 return 0;
365}
366
367/*
368 * Called by ALSA when the PCM substream is prepared, can set format, sample
369 * rate, etc. This function is non atomic and can be called multiple times,
370 * it can refer to the runtime info.
371 */
372static int soc_pcm_prepare(struct snd_pcm_substream *substream)
373{
374 struct snd_soc_pcm_runtime *rtd = substream->private_data;
375 struct snd_soc_device *socdev = rtd->socdev;
6308419a 376 struct snd_soc_card *card = socdev->card;
cb666e5b 377 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 378 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
379 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
380 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
381 struct snd_soc_codec *codec = socdev->codec;
382 int ret = 0;
383
384 mutex_lock(&pcm_mutex);
cb666e5b
LG
385
386 if (machine->ops && machine->ops->prepare) {
387 ret = machine->ops->prepare(substream);
388 if (ret < 0) {
389 printk(KERN_ERR "asoc: machine prepare error\n");
390 goto out;
391 }
392 }
393
db2a4165
FM
394 if (platform->pcm_ops->prepare) {
395 ret = platform->pcm_ops->prepare(substream);
a71a468a
LG
396 if (ret < 0) {
397 printk(KERN_ERR "asoc: platform prepare error\n");
db2a4165 398 goto out;
a71a468a 399 }
db2a4165
FM
400 }
401
cb666e5b 402 if (codec_dai->ops.prepare) {
dee89c4d 403 ret = codec_dai->ops.prepare(substream, codec_dai);
a71a468a
LG
404 if (ret < 0) {
405 printk(KERN_ERR "asoc: codec DAI prepare error\n");
db2a4165 406 goto out;
a71a468a 407 }
db2a4165
FM
408 }
409
cb666e5b 410 if (cpu_dai->ops.prepare) {
dee89c4d 411 ret = cpu_dai->ops.prepare(substream, cpu_dai);
cb666e5b
LG
412 if (ret < 0) {
413 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
414 goto out;
415 }
416 }
db2a4165 417
d45f6219
MB
418 /* cancel any delayed stream shutdown that is pending */
419 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
420 codec_dai->pop_wait) {
421 codec_dai->pop_wait = 0;
6308419a 422 cancel_delayed_work(&card->delayed_work);
d45f6219 423 }
db2a4165 424
d45f6219
MB
425 /* do we need to power up codec */
426 if (codec->bias_level != SND_SOC_BIAS_ON) {
427 snd_soc_dapm_set_bias_level(socdev,
428 SND_SOC_BIAS_PREPARE);
db2a4165 429
d45f6219
MB
430 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
431 snd_soc_dapm_stream_event(codec,
cb666e5b 432 codec_dai->playback.stream_name,
db2a4165 433 SND_SOC_DAPM_STREAM_START);
d45f6219
MB
434 else
435 snd_soc_dapm_stream_event(codec,
cb666e5b 436 codec_dai->capture.stream_name,
db2a4165
FM
437 SND_SOC_DAPM_STREAM_START);
438
d45f6219
MB
439 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
440 snd_soc_dai_digital_mute(codec_dai, 0);
db2a4165 441
d45f6219
MB
442 } else {
443 /* codec already powered - power on widgets */
444 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
445 snd_soc_dapm_stream_event(codec,
cb666e5b 446 codec_dai->playback.stream_name,
db2a4165 447 SND_SOC_DAPM_STREAM_START);
d45f6219
MB
448 else
449 snd_soc_dapm_stream_event(codec,
cb666e5b 450 codec_dai->capture.stream_name,
db2a4165 451 SND_SOC_DAPM_STREAM_START);
8c6529db 452
d45f6219 453 snd_soc_dai_digital_mute(codec_dai, 0);
db2a4165
FM
454 }
455
456out:
457 mutex_unlock(&pcm_mutex);
458 return ret;
459}
460
461/*
462 * Called by ALSA when the hardware params are set by application. This
463 * function can also be called multiple times and can allocate buffers
464 * (using snd_pcm_lib_* ). It's non-atomic.
465 */
466static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
467 struct snd_pcm_hw_params *params)
468{
469 struct snd_soc_pcm_runtime *rtd = substream->private_data;
470 struct snd_soc_device *socdev = rtd->socdev;
cb666e5b 471 struct snd_soc_dai_link *machine = rtd->dai;
87689d56
MB
472 struct snd_soc_card *card = socdev->card;
473 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
474 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
475 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
476 int ret = 0;
477
478 mutex_lock(&pcm_mutex);
479
cb666e5b
LG
480 if (machine->ops && machine->ops->hw_params) {
481 ret = machine->ops->hw_params(substream, params);
482 if (ret < 0) {
483 printk(KERN_ERR "asoc: machine hw_params failed\n");
db2a4165 484 goto out;
cb666e5b 485 }
db2a4165
FM
486 }
487
cb666e5b 488 if (codec_dai->ops.hw_params) {
dee89c4d 489 ret = codec_dai->ops.hw_params(substream, params, codec_dai);
db2a4165
FM
490 if (ret < 0) {
491 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
cb666e5b
LG
492 codec_dai->name);
493 goto codec_err;
db2a4165
FM
494 }
495 }
496
cb666e5b 497 if (cpu_dai->ops.hw_params) {
dee89c4d 498 ret = cpu_dai->ops.hw_params(substream, params, cpu_dai);
db2a4165 499 if (ret < 0) {
3ff3f64b 500 printk(KERN_ERR "asoc: interface %s hw params failed\n",
cb666e5b 501 cpu_dai->name);
db2a4165
FM
502 goto interface_err;
503 }
504 }
505
506 if (platform->pcm_ops->hw_params) {
507 ret = platform->pcm_ops->hw_params(substream, params);
508 if (ret < 0) {
3ff3f64b 509 printk(KERN_ERR "asoc: platform %s hw params failed\n",
db2a4165
FM
510 platform->name);
511 goto platform_err;
512 }
513 }
514
db2a4165
FM
515out:
516 mutex_unlock(&pcm_mutex);
517 return ret;
518
db2a4165 519platform_err:
cb666e5b 520 if (cpu_dai->ops.hw_free)
dee89c4d 521 cpu_dai->ops.hw_free(substream, cpu_dai);
db2a4165
FM
522
523interface_err:
cb666e5b 524 if (codec_dai->ops.hw_free)
dee89c4d 525 codec_dai->ops.hw_free(substream, codec_dai);
cb666e5b
LG
526
527codec_err:
3ff3f64b 528 if (machine->ops && machine->ops->hw_free)
cb666e5b 529 machine->ops->hw_free(substream);
db2a4165
FM
530
531 mutex_unlock(&pcm_mutex);
532 return ret;
533}
534
535/*
536 * Free's resources allocated by hw_params, can be called multiple times
537 */
538static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
539{
540 struct snd_soc_pcm_runtime *rtd = substream->private_data;
541 struct snd_soc_device *socdev = rtd->socdev;
cb666e5b 542 struct snd_soc_dai_link *machine = rtd->dai;
87689d56
MB
543 struct snd_soc_card *card = socdev->card;
544 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
545 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
546 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165 547 struct snd_soc_codec *codec = socdev->codec;
db2a4165
FM
548
549 mutex_lock(&pcm_mutex);
550
551 /* apply codec digital mute */
8c6529db
LG
552 if (!codec->active)
553 snd_soc_dai_digital_mute(codec_dai, 1);
db2a4165
FM
554
555 /* free any machine hw params */
556 if (machine->ops && machine->ops->hw_free)
557 machine->ops->hw_free(substream);
558
559 /* free any DMA resources */
560 if (platform->pcm_ops->hw_free)
561 platform->pcm_ops->hw_free(substream);
562
563 /* now free hw params for the DAI's */
cb666e5b 564 if (codec_dai->ops.hw_free)
dee89c4d 565 codec_dai->ops.hw_free(substream, codec_dai);
db2a4165 566
cb666e5b 567 if (cpu_dai->ops.hw_free)
dee89c4d 568 cpu_dai->ops.hw_free(substream, cpu_dai);
db2a4165
FM
569
570 mutex_unlock(&pcm_mutex);
571 return 0;
572}
573
574static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
575{
576 struct snd_soc_pcm_runtime *rtd = substream->private_data;
577 struct snd_soc_device *socdev = rtd->socdev;
87689d56 578 struct snd_soc_card *card= socdev->card;
cb666e5b 579 struct snd_soc_dai_link *machine = rtd->dai;
87689d56 580 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
581 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
582 struct snd_soc_dai *codec_dai = machine->codec_dai;
db2a4165
FM
583 int ret;
584
cb666e5b 585 if (codec_dai->ops.trigger) {
dee89c4d 586 ret = codec_dai->ops.trigger(substream, cmd, codec_dai);
db2a4165
FM
587 if (ret < 0)
588 return ret;
589 }
590
591 if (platform->pcm_ops->trigger) {
592 ret = platform->pcm_ops->trigger(substream, cmd);
593 if (ret < 0)
594 return ret;
595 }
596
cb666e5b 597 if (cpu_dai->ops.trigger) {
dee89c4d 598 ret = cpu_dai->ops.trigger(substream, cmd, cpu_dai);
db2a4165
FM
599 if (ret < 0)
600 return ret;
601 }
602 return 0;
603}
604
605/* ASoC PCM operations */
606static struct snd_pcm_ops soc_pcm_ops = {
607 .open = soc_pcm_open,
608 .close = soc_codec_close,
609 .hw_params = soc_pcm_hw_params,
610 .hw_free = soc_pcm_hw_free,
611 .prepare = soc_pcm_prepare,
612 .trigger = soc_pcm_trigger,
613};
614
615#ifdef CONFIG_PM
616/* powers down audio subsystem for suspend */
617static int soc_suspend(struct platform_device *pdev, pm_message_t state)
618{
3ff3f64b 619 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
87506549 620 struct snd_soc_card *card = socdev->card;
87689d56 621 struct snd_soc_platform *platform = card->platform;
3ff3f64b 622 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
db2a4165
FM
623 struct snd_soc_codec *codec = socdev->codec;
624 int i;
625
6ed25978
AG
626 /* Due to the resume being scheduled into a workqueue we could
627 * suspend before that's finished - wait for it to complete.
628 */
629 snd_power_lock(codec->card);
630 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
631 snd_power_unlock(codec->card);
632
633 /* we're going to block userspace touching us until resume completes */
634 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
635
db2a4165 636 /* mute any active DAC's */
dee89c4d
MB
637 for (i = 0; i < card->num_links; i++) {
638 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
639 if (dai->ops.digital_mute && dai->playback.active)
640 dai->ops.digital_mute(dai, 1);
db2a4165
FM
641 }
642
4ccab3e7 643 /* suspend all pcms */
87506549
MB
644 for (i = 0; i < card->num_links; i++)
645 snd_pcm_suspend_all(card->dai_link[i].pcm);
4ccab3e7 646
87506549
MB
647 if (card->suspend_pre)
648 card->suspend_pre(pdev, state);
db2a4165 649
87506549
MB
650 for (i = 0; i < card->num_links; i++) {
651 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3ba9e10a 652 if (cpu_dai->suspend && !cpu_dai->ac97_control)
dc7d7b83 653 cpu_dai->suspend(cpu_dai);
db2a4165 654 if (platform->suspend)
07c84d04 655 platform->suspend(cpu_dai);
db2a4165
FM
656 }
657
658 /* close any waiting streams and save state */
6308419a 659 run_delayed_work(&card->delayed_work);
0be9898a 660 codec->suspend_bias_level = codec->bias_level;
db2a4165 661
3ff3f64b 662 for (i = 0; i < codec->num_dai; i++) {
db2a4165
FM
663 char *stream = codec->dai[i].playback.stream_name;
664 if (stream != NULL)
665 snd_soc_dapm_stream_event(codec, stream,
666 SND_SOC_DAPM_STREAM_SUSPEND);
667 stream = codec->dai[i].capture.stream_name;
668 if (stream != NULL)
669 snd_soc_dapm_stream_event(codec, stream,
670 SND_SOC_DAPM_STREAM_SUSPEND);
671 }
672
673 if (codec_dev->suspend)
674 codec_dev->suspend(pdev, state);
675
87506549
MB
676 for (i = 0; i < card->num_links; i++) {
677 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3ba9e10a 678 if (cpu_dai->suspend && cpu_dai->ac97_control)
dc7d7b83 679 cpu_dai->suspend(cpu_dai);
db2a4165
FM
680 }
681
87506549
MB
682 if (card->suspend_post)
683 card->suspend_post(pdev, state);
db2a4165
FM
684
685 return 0;
686}
687
6ed25978
AG
688/* deferred resume work, so resume can complete before we finished
689 * setting our codec back up, which can be very slow on I2C
690 */
691static void soc_resume_deferred(struct work_struct *work)
db2a4165 692{
6308419a
MB
693 struct snd_soc_card *card = container_of(work,
694 struct snd_soc_card,
695 deferred_resume_work);
696 struct snd_soc_device *socdev = card->socdev;
87689d56 697 struct snd_soc_platform *platform = card->platform;
3ff3f64b 698 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
db2a4165 699 struct snd_soc_codec *codec = socdev->codec;
6ed25978 700 struct platform_device *pdev = to_platform_device(socdev->dev);
db2a4165
FM
701 int i;
702
6ed25978
AG
703 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
704 * so userspace apps are blocked from touching us
705 */
706
fde22f27 707 dev_dbg(socdev->dev, "starting resume work\n");
6ed25978 708
87506549
MB
709 if (card->resume_pre)
710 card->resume_pre(pdev);
db2a4165 711
87506549
MB
712 for (i = 0; i < card->num_links; i++) {
713 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3ba9e10a 714 if (cpu_dai->resume && cpu_dai->ac97_control)
dc7d7b83 715 cpu_dai->resume(cpu_dai);
db2a4165
FM
716 }
717
718 if (codec_dev->resume)
719 codec_dev->resume(pdev);
720
3ff3f64b
MB
721 for (i = 0; i < codec->num_dai; i++) {
722 char *stream = codec->dai[i].playback.stream_name;
db2a4165
FM
723 if (stream != NULL)
724 snd_soc_dapm_stream_event(codec, stream,
725 SND_SOC_DAPM_STREAM_RESUME);
726 stream = codec->dai[i].capture.stream_name;
727 if (stream != NULL)
728 snd_soc_dapm_stream_event(codec, stream,
729 SND_SOC_DAPM_STREAM_RESUME);
730 }
731
3ff3f64b 732 /* unmute any active DACs */
dee89c4d
MB
733 for (i = 0; i < card->num_links; i++) {
734 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
735 if (dai->ops.digital_mute && dai->playback.active)
736 dai->ops.digital_mute(dai, 0);
db2a4165
FM
737 }
738
87506549
MB
739 for (i = 0; i < card->num_links; i++) {
740 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
3ba9e10a 741 if (cpu_dai->resume && !cpu_dai->ac97_control)
dc7d7b83 742 cpu_dai->resume(cpu_dai);
db2a4165 743 if (platform->resume)
07c84d04 744 platform->resume(cpu_dai);
db2a4165
FM
745 }
746
87506549
MB
747 if (card->resume_post)
748 card->resume_post(pdev);
db2a4165 749
fde22f27 750 dev_dbg(socdev->dev, "resume work completed\n");
6ed25978
AG
751
752 /* userspace can access us now we are back as we were before */
753 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
754}
755
756/* powers up audio subsystem after a suspend */
757static int soc_resume(struct platform_device *pdev)
758{
759 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
6308419a 760 struct snd_soc_card *card = socdev->card;
6ed25978 761
fde22f27 762 dev_dbg(socdev->dev, "scheduling resume work\n");
6ed25978 763
6308419a 764 if (!schedule_work(&card->deferred_resume_work))
fde22f27 765 dev_err(socdev->dev, "resume work item may be lost\n");
6ed25978 766
db2a4165
FM
767 return 0;
768}
769
770#else
771#define soc_suspend NULL
772#define soc_resume NULL
773#endif
774
775/* probes a new socdev */
776static int soc_probe(struct platform_device *pdev)
777{
778 int ret = 0, i;
779 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
87506549 780 struct snd_soc_card *card = socdev->card;
87689d56 781 struct snd_soc_platform *platform = card->platform;
db2a4165
FM
782 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
783
6308419a
MB
784 /* Bodge while we push things out of socdev */
785 card->socdev = socdev;
786
87506549
MB
787 if (card->probe) {
788 ret = card->probe(pdev);
3ff3f64b 789 if (ret < 0)
db2a4165
FM
790 return ret;
791 }
792
87506549
MB
793 for (i = 0; i < card->num_links; i++) {
794 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
db2a4165 795 if (cpu_dai->probe) {
bdb92876 796 ret = cpu_dai->probe(pdev, cpu_dai);
3ff3f64b 797 if (ret < 0)
db2a4165
FM
798 goto cpu_dai_err;
799 }
800 }
801
802 if (codec_dev->probe) {
803 ret = codec_dev->probe(pdev);
3ff3f64b 804 if (ret < 0)
db2a4165
FM
805 goto cpu_dai_err;
806 }
807
808 if (platform->probe) {
809 ret = platform->probe(pdev);
3ff3f64b 810 if (ret < 0)
db2a4165
FM
811 goto platform_err;
812 }
813
814 /* DAPM stream work */
6308419a 815 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
1301a964 816#ifdef CONFIG_PM
6ed25978 817 /* deferred resume work */
6308419a 818 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1301a964 819#endif
6ed25978 820
db2a4165
FM
821 return 0;
822
db2a4165
FM
823platform_err:
824 if (codec_dev->remove)
825 codec_dev->remove(pdev);
826
827cpu_dai_err:
18b9b3d9 828 for (i--; i >= 0; i--) {
87506549 829 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
db2a4165 830 if (cpu_dai->remove)
bdb92876 831 cpu_dai->remove(pdev, cpu_dai);
db2a4165
FM
832 }
833
87506549
MB
834 if (card->remove)
835 card->remove(pdev);
db2a4165
FM
836
837 return ret;
838}
839
840/* removes a socdev */
841static int soc_remove(struct platform_device *pdev)
842{
843 int i;
844 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
87506549 845 struct snd_soc_card *card = socdev->card;
87689d56 846 struct snd_soc_platform *platform = card->platform;
db2a4165
FM
847 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
848
6308419a 849 run_delayed_work(&card->delayed_work);
965ac42c 850
db2a4165
FM
851 if (platform->remove)
852 platform->remove(pdev);
853
854 if (codec_dev->remove)
855 codec_dev->remove(pdev);
856
87506549
MB
857 for (i = 0; i < card->num_links; i++) {
858 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
db2a4165 859 if (cpu_dai->remove)
bdb92876 860 cpu_dai->remove(pdev, cpu_dai);
db2a4165
FM
861 }
862
87506549
MB
863 if (card->remove)
864 card->remove(pdev);
db2a4165
FM
865
866 return 0;
867}
868
869/* ASoC platform driver */
870static struct platform_driver soc_driver = {
871 .driver = {
872 .name = "soc-audio",
8b45a209 873 .owner = THIS_MODULE,
db2a4165
FM
874 },
875 .probe = soc_probe,
876 .remove = soc_remove,
877 .suspend = soc_suspend,
878 .resume = soc_resume,
879};
880
881/* create a new pcm */
882static int soc_new_pcm(struct snd_soc_device *socdev,
883 struct snd_soc_dai_link *dai_link, int num)
884{
885 struct snd_soc_codec *codec = socdev->codec;
87689d56
MB
886 struct snd_soc_card *card = socdev->card;
887 struct snd_soc_platform *platform = card->platform;
3c4b266f
LG
888 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
889 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
db2a4165
FM
890 struct snd_soc_pcm_runtime *rtd;
891 struct snd_pcm *pcm;
892 char new_name[64];
893 int ret = 0, playback = 0, capture = 0;
894
895 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
896 if (rtd == NULL)
897 return -ENOMEM;
cb666e5b
LG
898
899 rtd->dai = dai_link;
db2a4165 900 rtd->socdev = socdev;
cb666e5b 901 codec_dai->codec = socdev->codec;
db2a4165
FM
902
903 /* check client and interface hw capabilities */
3ba9e10a
MB
904 sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
905 num);
db2a4165
FM
906
907 if (codec_dai->playback.channels_min)
908 playback = 1;
909 if (codec_dai->capture.channels_min)
910 capture = 1;
911
912 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
913 capture, &pcm);
914 if (ret < 0) {
3ff3f64b
MB
915 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
916 codec->name);
db2a4165
FM
917 kfree(rtd);
918 return ret;
919 }
920
4ccab3e7 921 dai_link->pcm = pcm;
db2a4165 922 pcm->private_data = rtd;
87689d56
MB
923 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
924 soc_pcm_ops.pointer = platform->pcm_ops->pointer;
925 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
926 soc_pcm_ops.copy = platform->pcm_ops->copy;
927 soc_pcm_ops.silence = platform->pcm_ops->silence;
928 soc_pcm_ops.ack = platform->pcm_ops->ack;
929 soc_pcm_ops.page = platform->pcm_ops->page;
db2a4165
FM
930
931 if (playback)
932 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
933
934 if (capture)
935 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
936
87689d56 937 ret = platform->pcm_new(codec->card, codec_dai, pcm);
db2a4165
FM
938 if (ret < 0) {
939 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
940 kfree(rtd);
941 return ret;
942 }
943
87689d56 944 pcm->private_free = platform->pcm_free;
db2a4165
FM
945 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
946 cpu_dai->name);
947 return ret;
948}
949
950/* codec register dump */
12ef193d 951static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
db2a4165 952{
db2a4165
FM
953 struct snd_soc_codec *codec = devdata->codec;
954 int i, step = 1, count = 0;
955
956 if (!codec->reg_cache_size)
957 return 0;
958
959 if (codec->reg_cache_step)
960 step = codec->reg_cache_step;
961
962 count += sprintf(buf, "%s registers\n", codec->name);
58cd33c0
MB
963 for (i = 0; i < codec->reg_cache_size; i += step) {
964 count += sprintf(buf + count, "%2x: ", i);
965 if (count >= PAGE_SIZE - 1)
966 break;
967
968 if (codec->display_register)
969 count += codec->display_register(codec, buf + count,
970 PAGE_SIZE - count, i);
971 else
972 count += snprintf(buf + count, PAGE_SIZE - count,
973 "%4x", codec->read(codec, i));
974
975 if (count >= PAGE_SIZE - 1)
976 break;
977
978 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
979 if (count >= PAGE_SIZE - 1)
980 break;
981 }
982
983 /* Truncate count; min() would cause a warning */
984 if (count >= PAGE_SIZE)
985 count = PAGE_SIZE - 1;
db2a4165
FM
986
987 return count;
988}
12ef193d
TK
989static ssize_t codec_reg_show(struct device *dev,
990 struct device_attribute *attr, char *buf)
991{
992 struct snd_soc_device *devdata = dev_get_drvdata(dev);
993 return soc_codec_reg_show(devdata, buf);
994}
995
db2a4165
FM
996static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
997
12ef193d
TK
998#ifdef CONFIG_DEBUG_FS
999static int codec_reg_open_file(struct inode *inode, struct file *file)
1000{
1001 file->private_data = inode->i_private;
1002 return 0;
1003}
1004
1005static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1006 size_t count, loff_t *ppos)
1007{
1008 ssize_t ret;
384c89e2
MB
1009 struct snd_soc_codec *codec = file->private_data;
1010 struct device *card_dev = codec->card->dev;
1011 struct snd_soc_device *devdata = card_dev->driver_data;
12ef193d
TK
1012 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1013 if (!buf)
1014 return -ENOMEM;
1015 ret = soc_codec_reg_show(devdata, buf);
1016 if (ret >= 0)
1017 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1018 kfree(buf);
1019 return ret;
1020}
1021
1022static ssize_t codec_reg_write_file(struct file *file,
1023 const char __user *user_buf, size_t count, loff_t *ppos)
1024{
1025 char buf[32];
1026 int buf_size;
1027 char *start = buf;
1028 unsigned long reg, value;
1029 int step = 1;
384c89e2 1030 struct snd_soc_codec *codec = file->private_data;
12ef193d
TK
1031
1032 buf_size = min(count, (sizeof(buf)-1));
1033 if (copy_from_user(buf, user_buf, buf_size))
1034 return -EFAULT;
1035 buf[buf_size] = 0;
1036
1037 if (codec->reg_cache_step)
1038 step = codec->reg_cache_step;
1039
1040 while (*start == ' ')
1041 start++;
1042 reg = simple_strtoul(start, &start, 16);
1043 if ((reg >= codec->reg_cache_size) || (reg % step))
1044 return -EINVAL;
1045 while (*start == ' ')
1046 start++;
1047 if (strict_strtoul(start, 16, &value))
1048 return -EINVAL;
1049 codec->write(codec, reg, value);
1050 return buf_size;
1051}
1052
1053static const struct file_operations codec_reg_fops = {
1054 .open = codec_reg_open_file,
1055 .read = codec_reg_read_file,
1056 .write = codec_reg_write_file,
1057};
1058
384c89e2 1059static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
12ef193d 1060{
384c89e2
MB
1061 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
1062 debugfs_root, codec,
1063 &codec_reg_fops);
1064 if (!codec->debugfs_reg)
1065 printk(KERN_WARNING
1066 "ASoC: Failed to create codec register debugfs file\n");
1067
1068 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
1069 debugfs_root,
1070 &codec->pop_time);
1071 if (!codec->debugfs_pop_time)
1072 printk(KERN_WARNING
1073 "Failed to create pop time debugfs file\n");
12ef193d
TK
1074}
1075
384c89e2 1076static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
12ef193d 1077{
384c89e2
MB
1078 debugfs_remove(codec->debugfs_pop_time);
1079 debugfs_remove(codec->debugfs_reg);
12ef193d
TK
1080}
1081
1082#else
1083
384c89e2 1084static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
12ef193d
TK
1085{
1086}
1087
384c89e2 1088static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
12ef193d
TK
1089{
1090}
1091#endif
1092
db2a4165
FM
1093/**
1094 * snd_soc_new_ac97_codec - initailise AC97 device
1095 * @codec: audio codec
1096 * @ops: AC97 bus operations
1097 * @num: AC97 codec number
1098 *
1099 * Initialises AC97 codec resources for use by ad-hoc devices only.
1100 */
1101int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1102 struct snd_ac97_bus_ops *ops, int num)
1103{
1104 mutex_lock(&codec->mutex);
1105
1106 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1107 if (codec->ac97 == NULL) {
1108 mutex_unlock(&codec->mutex);
1109 return -ENOMEM;
1110 }
1111
1112 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1113 if (codec->ac97->bus == NULL) {
1114 kfree(codec->ac97);
1115 codec->ac97 = NULL;
1116 mutex_unlock(&codec->mutex);
1117 return -ENOMEM;
1118 }
1119
1120 codec->ac97->bus->ops = ops;
1121 codec->ac97->num = num;
1122 mutex_unlock(&codec->mutex);
1123 return 0;
1124}
1125EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1126
1127/**
1128 * snd_soc_free_ac97_codec - free AC97 codec device
1129 * @codec: audio codec
1130 *
1131 * Frees AC97 codec device resources.
1132 */
1133void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1134{
1135 mutex_lock(&codec->mutex);
1136 kfree(codec->ac97->bus);
1137 kfree(codec->ac97);
1138 codec->ac97 = NULL;
1139 mutex_unlock(&codec->mutex);
1140}
1141EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1142
1143/**
1144 * snd_soc_update_bits - update codec register bits
1145 * @codec: audio codec
1146 * @reg: codec register
1147 * @mask: register mask
1148 * @value: new value
1149 *
1150 * Writes new register value.
1151 *
1152 * Returns 1 for change else 0.
1153 */
1154int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1155 unsigned short mask, unsigned short value)
1156{
1157 int change;
1158 unsigned short old, new;
1159
1160 mutex_lock(&io_mutex);
1161 old = snd_soc_read(codec, reg);
1162 new = (old & ~mask) | value;
1163 change = old != new;
1164 if (change)
1165 snd_soc_write(codec, reg, new);
1166
1167 mutex_unlock(&io_mutex);
1168 return change;
1169}
1170EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1171
1172/**
1173 * snd_soc_test_bits - test register for change
1174 * @codec: audio codec
1175 * @reg: codec register
1176 * @mask: register mask
1177 * @value: new value
1178 *
1179 * Tests a register with a new value and checks if the new value is
1180 * different from the old value.
1181 *
1182 * Returns 1 for change else 0.
1183 */
1184int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1185 unsigned short mask, unsigned short value)
1186{
1187 int change;
1188 unsigned short old, new;
1189
1190 mutex_lock(&io_mutex);
1191 old = snd_soc_read(codec, reg);
1192 new = (old & ~mask) | value;
1193 change = old != new;
1194 mutex_unlock(&io_mutex);
1195
1196 return change;
1197}
1198EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1199
db2a4165
FM
1200/**
1201 * snd_soc_new_pcms - create new sound card and pcms
1202 * @socdev: the SoC audio device
1203 *
1204 * Create a new sound card based upon the codec and interface pcms.
1205 *
1206 * Returns 0 for success, else error.
1207 */
bc7320c5 1208int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
db2a4165
FM
1209{
1210 struct snd_soc_codec *codec = socdev->codec;
87506549 1211 struct snd_soc_card *card = socdev->card;
db2a4165
FM
1212 int ret = 0, i;
1213
1214 mutex_lock(&codec->mutex);
1215
1216 /* register a sound card */
1217 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1218 if (!codec->card) {
1219 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1220 codec->name);
1221 mutex_unlock(&codec->mutex);
1222 return -ENODEV;
1223 }
1224
1225 codec->card->dev = socdev->dev;
1226 codec->card->private_data = codec;
1227 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1228
1229 /* create the pcms */
87506549
MB
1230 for (i = 0; i < card->num_links; i++) {
1231 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
db2a4165
FM
1232 if (ret < 0) {
1233 printk(KERN_ERR "asoc: can't create pcm %s\n",
87506549 1234 card->dai_link[i].stream_name);
db2a4165
FM
1235 mutex_unlock(&codec->mutex);
1236 return ret;
1237 }
1238 }
1239
1240 mutex_unlock(&codec->mutex);
1241 return ret;
1242}
1243EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1244
1245/**
968a6025 1246 * snd_soc_init_card - register sound card
db2a4165
FM
1247 * @socdev: the SoC audio device
1248 *
1249 * Register a SoC sound card. Also registers an AC97 device if the
1250 * codec is AC97 for ad hoc devices.
1251 *
1252 * Returns 0 for success, else error.
1253 */
968a6025 1254int snd_soc_init_card(struct snd_soc_device *socdev)
db2a4165
FM
1255{
1256 struct snd_soc_codec *codec = socdev->codec;
87506549 1257 struct snd_soc_card *card = socdev->card;
12e74f7d 1258 int ret = 0, i, ac97 = 0, err = 0;
db2a4165 1259
87506549
MB
1260 for (i = 0; i < card->num_links; i++) {
1261 if (card->dai_link[i].init) {
1262 err = card->dai_link[i].init(codec);
12e74f7d
LG
1263 if (err < 0) {
1264 printk(KERN_ERR "asoc: failed to init %s\n",
87506549 1265 card->dai_link[i].stream_name);
12e74f7d
LG
1266 continue;
1267 }
1268 }
3ba9e10a 1269 if (card->dai_link[i].codec_dai->ac97_control)
db2a4165
FM
1270 ac97 = 1;
1271 }
1272 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
87506549 1273 "%s", card->name);
db2a4165 1274 snprintf(codec->card->longname, sizeof(codec->card->longname),
87506549 1275 "%s (%s)", card->name, codec->name);
db2a4165
FM
1276
1277 ret = snd_card_register(codec->card);
1278 if (ret < 0) {
3ff3f64b 1279 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
db2a4165 1280 codec->name);
12e74f7d 1281 goto out;
db2a4165
FM
1282 }
1283
08c8efe6 1284 mutex_lock(&codec->mutex);
db2a4165 1285#ifdef CONFIG_SND_SOC_AC97_BUS
12e74f7d
LG
1286 if (ac97) {
1287 ret = soc_ac97_dev_register(codec);
1288 if (ret < 0) {
1289 printk(KERN_ERR "asoc: AC97 device register failed\n");
1290 snd_card_free(codec->card);
08c8efe6 1291 mutex_unlock(&codec->mutex);
12e74f7d
LG
1292 goto out;
1293 }
1294 }
db2a4165
FM
1295#endif
1296
12e74f7d
LG
1297 err = snd_soc_dapm_sys_add(socdev->dev);
1298 if (err < 0)
1299 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1300
1301 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1302 if (err < 0)
3ff3f64b 1303 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
08c8efe6 1304
384c89e2 1305 soc_init_codec_debugfs(socdev->codec);
db2a4165 1306 mutex_unlock(&codec->mutex);
08c8efe6
MB
1307
1308out:
db2a4165
FM
1309 return ret;
1310}
968a6025 1311EXPORT_SYMBOL_GPL(snd_soc_init_card);
db2a4165
FM
1312
1313/**
1314 * snd_soc_free_pcms - free sound card and pcms
1315 * @socdev: the SoC audio device
1316 *
1317 * Frees sound card and pcms associated with the socdev.
1318 * Also unregister the codec if it is an AC97 device.
1319 */
1320void snd_soc_free_pcms(struct snd_soc_device *socdev)
1321{
1322 struct snd_soc_codec *codec = socdev->codec;
a68660e0 1323#ifdef CONFIG_SND_SOC_AC97_BUS
3c4b266f 1324 struct snd_soc_dai *codec_dai;
a68660e0
LG
1325 int i;
1326#endif
db2a4165
FM
1327
1328 mutex_lock(&codec->mutex);
384c89e2 1329 soc_cleanup_codec_debugfs(socdev->codec);
db2a4165 1330#ifdef CONFIG_SND_SOC_AC97_BUS
3ff3f64b 1331 for (i = 0; i < codec->num_dai; i++) {
a68660e0 1332 codec_dai = &codec->dai[i];
3ba9e10a 1333 if (codec_dai->ac97_control && codec->ac97) {
a68660e0
LG
1334 soc_ac97_dev_unregister(codec);
1335 goto free_card;
1336 }
1337 }
1338free_card:
db2a4165
FM
1339#endif
1340
1341 if (codec->card)
1342 snd_card_free(codec->card);
1343 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1344 mutex_unlock(&codec->mutex);
1345}
1346EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1347
1348/**
1349 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1350 * @substream: the pcm substream
1351 * @hw: the hardware parameters
1352 *
1353 * Sets the substream runtime hardware parameters.
1354 */
1355int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1356 const struct snd_pcm_hardware *hw)
1357{
1358 struct snd_pcm_runtime *runtime = substream->runtime;
1359 runtime->hw.info = hw->info;
1360 runtime->hw.formats = hw->formats;
1361 runtime->hw.period_bytes_min = hw->period_bytes_min;
1362 runtime->hw.period_bytes_max = hw->period_bytes_max;
1363 runtime->hw.periods_min = hw->periods_min;
1364 runtime->hw.periods_max = hw->periods_max;
1365 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1366 runtime->hw.fifo_size = hw->fifo_size;
1367 return 0;
1368}
1369EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1370
1371/**
1372 * snd_soc_cnew - create new control
1373 * @_template: control template
1374 * @data: control private data
1375 * @lnng_name: control long name
1376 *
1377 * Create a new mixer control from a template control.
1378 *
1379 * Returns 0 for success, else error.
1380 */
1381struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1382 void *data, char *long_name)
1383{
1384 struct snd_kcontrol_new template;
1385
1386 memcpy(&template, _template, sizeof(template));
1387 if (long_name)
1388 template.name = long_name;
db2a4165
FM
1389 template.index = 0;
1390
1391 return snd_ctl_new1(&template, data);
1392}
1393EXPORT_SYMBOL_GPL(snd_soc_cnew);
1394
1395/**
1396 * snd_soc_info_enum_double - enumerated double mixer info callback
1397 * @kcontrol: mixer control
1398 * @uinfo: control element information
1399 *
1400 * Callback to provide information about a double enumerated
1401 * mixer control.
1402 *
1403 * Returns 0 for success.
1404 */
1405int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1406 struct snd_ctl_elem_info *uinfo)
1407{
1408 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1409
1410 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1411 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
f8ba0b7b 1412 uinfo->value.enumerated.items = e->max;
db2a4165 1413
f8ba0b7b
JS
1414 if (uinfo->value.enumerated.item > e->max - 1)
1415 uinfo->value.enumerated.item = e->max - 1;
db2a4165
FM
1416 strcpy(uinfo->value.enumerated.name,
1417 e->texts[uinfo->value.enumerated.item]);
1418 return 0;
1419}
1420EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1421
1422/**
1423 * snd_soc_get_enum_double - enumerated double mixer get callback
1424 * @kcontrol: mixer control
1425 * @uinfo: control element information
1426 *
1427 * Callback to get the value of a double enumerated mixer.
1428 *
1429 * Returns 0 for success.
1430 */
1431int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1432 struct snd_ctl_elem_value *ucontrol)
1433{
1434 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1435 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1436 unsigned short val, bitmask;
1437
f8ba0b7b 1438 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
db2a4165
FM
1439 ;
1440 val = snd_soc_read(codec, e->reg);
3ff3f64b
MB
1441 ucontrol->value.enumerated.item[0]
1442 = (val >> e->shift_l) & (bitmask - 1);
db2a4165
FM
1443 if (e->shift_l != e->shift_r)
1444 ucontrol->value.enumerated.item[1] =
1445 (val >> e->shift_r) & (bitmask - 1);
1446
1447 return 0;
1448}
1449EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1450
1451/**
1452 * snd_soc_put_enum_double - enumerated double mixer put callback
1453 * @kcontrol: mixer control
1454 * @uinfo: control element information
1455 *
1456 * Callback to set the value of a double enumerated mixer.
1457 *
1458 * Returns 0 for success.
1459 */
1460int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1461 struct snd_ctl_elem_value *ucontrol)
1462{
1463 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1464 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1465 unsigned short val;
1466 unsigned short mask, bitmask;
1467
f8ba0b7b 1468 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
db2a4165 1469 ;
f8ba0b7b 1470 if (ucontrol->value.enumerated.item[0] > e->max - 1)
db2a4165
FM
1471 return -EINVAL;
1472 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1473 mask = (bitmask - 1) << e->shift_l;
1474 if (e->shift_l != e->shift_r) {
f8ba0b7b 1475 if (ucontrol->value.enumerated.item[1] > e->max - 1)
db2a4165
FM
1476 return -EINVAL;
1477 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1478 mask |= (bitmask - 1) << e->shift_r;
1479 }
1480
1481 return snd_soc_update_bits(codec, e->reg, mask, val);
1482}
1483EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1484
1485/**
1486 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1487 * @kcontrol: mixer control
1488 * @uinfo: control element information
1489 *
1490 * Callback to provide information about an external enumerated
1491 * single mixer.
1492 *
1493 * Returns 0 for success.
1494 */
1495int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1496 struct snd_ctl_elem_info *uinfo)
1497{
1498 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1499
1500 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1501 uinfo->count = 1;
f8ba0b7b 1502 uinfo->value.enumerated.items = e->max;
db2a4165 1503
f8ba0b7b
JS
1504 if (uinfo->value.enumerated.item > e->max - 1)
1505 uinfo->value.enumerated.item = e->max - 1;
db2a4165
FM
1506 strcpy(uinfo->value.enumerated.name,
1507 e->texts[uinfo->value.enumerated.item]);
1508 return 0;
1509}
1510EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1511
1512/**
1513 * snd_soc_info_volsw_ext - external single mixer info callback
1514 * @kcontrol: mixer control
1515 * @uinfo: control element information
1516 *
1517 * Callback to provide information about a single external mixer control.
1518 *
1519 * Returns 0 for success.
1520 */
1521int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1522 struct snd_ctl_elem_info *uinfo)
1523{
a7a4ac86
PZ
1524 int max = kcontrol->private_value;
1525
1526 if (max == 1)
1527 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1528 else
1529 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 1530
db2a4165
FM
1531 uinfo->count = 1;
1532 uinfo->value.integer.min = 0;
a7a4ac86 1533 uinfo->value.integer.max = max;
db2a4165
FM
1534 return 0;
1535}
1536EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1537
db2a4165
FM
1538/**
1539 * snd_soc_info_volsw - single mixer info callback
1540 * @kcontrol: mixer control
1541 * @uinfo: control element information
1542 *
1543 * Callback to provide information about a single mixer control.
1544 *
1545 * Returns 0 for success.
1546 */
1547int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1548 struct snd_ctl_elem_info *uinfo)
1549{
4eaa9819
JS
1550 struct soc_mixer_control *mc =
1551 (struct soc_mixer_control *)kcontrol->private_value;
1552 int max = mc->max;
762b8df7 1553 unsigned int shift = mc->shift;
815ecf8d 1554 unsigned int rshift = mc->rshift;
db2a4165 1555
a7a4ac86
PZ
1556 if (max == 1)
1557 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1558 else
1559 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1560
db2a4165
FM
1561 uinfo->count = shift == rshift ? 1 : 2;
1562 uinfo->value.integer.min = 0;
a7a4ac86 1563 uinfo->value.integer.max = max;
db2a4165
FM
1564 return 0;
1565}
1566EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1567
1568/**
1569 * snd_soc_get_volsw - single mixer get callback
1570 * @kcontrol: mixer control
1571 * @uinfo: control element information
1572 *
1573 * Callback to get the value of a single mixer control.
1574 *
1575 * Returns 0 for success.
1576 */
1577int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1578 struct snd_ctl_elem_value *ucontrol)
1579{
4eaa9819
JS
1580 struct soc_mixer_control *mc =
1581 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 1582 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
1583 unsigned int reg = mc->reg;
1584 unsigned int shift = mc->shift;
1585 unsigned int rshift = mc->rshift;
4eaa9819 1586 int max = mc->max;
815ecf8d
JS
1587 unsigned int mask = (1 << fls(max)) - 1;
1588 unsigned int invert = mc->invert;
db2a4165
FM
1589
1590 ucontrol->value.integer.value[0] =
1591 (snd_soc_read(codec, reg) >> shift) & mask;
1592 if (shift != rshift)
1593 ucontrol->value.integer.value[1] =
1594 (snd_soc_read(codec, reg) >> rshift) & mask;
1595 if (invert) {
1596 ucontrol->value.integer.value[0] =
a7a4ac86 1597 max - ucontrol->value.integer.value[0];
db2a4165
FM
1598 if (shift != rshift)
1599 ucontrol->value.integer.value[1] =
a7a4ac86 1600 max - ucontrol->value.integer.value[1];
db2a4165
FM
1601 }
1602
1603 return 0;
1604}
1605EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1606
1607/**
1608 * snd_soc_put_volsw - single mixer put callback
1609 * @kcontrol: mixer control
1610 * @uinfo: control element information
1611 *
1612 * Callback to set the value of a single mixer control.
1613 *
1614 * Returns 0 for success.
1615 */
1616int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1617 struct snd_ctl_elem_value *ucontrol)
1618{
4eaa9819
JS
1619 struct soc_mixer_control *mc =
1620 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 1621 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
1622 unsigned int reg = mc->reg;
1623 unsigned int shift = mc->shift;
1624 unsigned int rshift = mc->rshift;
4eaa9819 1625 int max = mc->max;
815ecf8d
JS
1626 unsigned int mask = (1 << fls(max)) - 1;
1627 unsigned int invert = mc->invert;
db2a4165
FM
1628 unsigned short val, val2, val_mask;
1629
1630 val = (ucontrol->value.integer.value[0] & mask);
1631 if (invert)
a7a4ac86 1632 val = max - val;
db2a4165
FM
1633 val_mask = mask << shift;
1634 val = val << shift;
1635 if (shift != rshift) {
1636 val2 = (ucontrol->value.integer.value[1] & mask);
1637 if (invert)
a7a4ac86 1638 val2 = max - val2;
db2a4165
FM
1639 val_mask |= mask << rshift;
1640 val |= val2 << rshift;
1641 }
a7a4ac86 1642 return snd_soc_update_bits(codec, reg, val_mask, val);
db2a4165
FM
1643}
1644EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1645
1646/**
1647 * snd_soc_info_volsw_2r - double mixer info callback
1648 * @kcontrol: mixer control
1649 * @uinfo: control element information
1650 *
1651 * Callback to provide information about a double mixer control that
1652 * spans 2 codec registers.
1653 *
1654 * Returns 0 for success.
1655 */
1656int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1657 struct snd_ctl_elem_info *uinfo)
1658{
4eaa9819
JS
1659 struct soc_mixer_control *mc =
1660 (struct soc_mixer_control *)kcontrol->private_value;
1661 int max = mc->max;
a7a4ac86
PZ
1662
1663 if (max == 1)
1664 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1665 else
1666 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 1667
db2a4165
FM
1668 uinfo->count = 2;
1669 uinfo->value.integer.min = 0;
a7a4ac86 1670 uinfo->value.integer.max = max;
db2a4165
FM
1671 return 0;
1672}
1673EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1674
1675/**
1676 * snd_soc_get_volsw_2r - double mixer get callback
1677 * @kcontrol: mixer control
1678 * @uinfo: control element information
1679 *
1680 * Callback to get the value of a double mixer control that spans 2 registers.
1681 *
1682 * Returns 0 for success.
1683 */
1684int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1685 struct snd_ctl_elem_value *ucontrol)
1686{
4eaa9819
JS
1687 struct soc_mixer_control *mc =
1688 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 1689 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
1690 unsigned int reg = mc->reg;
1691 unsigned int reg2 = mc->rreg;
1692 unsigned int shift = mc->shift;
4eaa9819 1693 int max = mc->max;
815ecf8d
JS
1694 unsigned int mask = (1<<fls(max))-1;
1695 unsigned int invert = mc->invert;
db2a4165
FM
1696
1697 ucontrol->value.integer.value[0] =
1698 (snd_soc_read(codec, reg) >> shift) & mask;
1699 ucontrol->value.integer.value[1] =
1700 (snd_soc_read(codec, reg2) >> shift) & mask;
1701 if (invert) {
1702 ucontrol->value.integer.value[0] =
a7a4ac86 1703 max - ucontrol->value.integer.value[0];
db2a4165 1704 ucontrol->value.integer.value[1] =
a7a4ac86 1705 max - ucontrol->value.integer.value[1];
db2a4165
FM
1706 }
1707
1708 return 0;
1709}
1710EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1711
1712/**
1713 * snd_soc_put_volsw_2r - double mixer set callback
1714 * @kcontrol: mixer control
1715 * @uinfo: control element information
1716 *
1717 * Callback to set the value of a double mixer control that spans 2 registers.
1718 *
1719 * Returns 0 for success.
1720 */
1721int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1722 struct snd_ctl_elem_value *ucontrol)
1723{
4eaa9819
JS
1724 struct soc_mixer_control *mc =
1725 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 1726 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
1727 unsigned int reg = mc->reg;
1728 unsigned int reg2 = mc->rreg;
1729 unsigned int shift = mc->shift;
4eaa9819 1730 int max = mc->max;
815ecf8d
JS
1731 unsigned int mask = (1 << fls(max)) - 1;
1732 unsigned int invert = mc->invert;
db2a4165
FM
1733 int err;
1734 unsigned short val, val2, val_mask;
1735
1736 val_mask = mask << shift;
1737 val = (ucontrol->value.integer.value[0] & mask);
1738 val2 = (ucontrol->value.integer.value[1] & mask);
1739
1740 if (invert) {
a7a4ac86
PZ
1741 val = max - val;
1742 val2 = max - val2;
db2a4165
FM
1743 }
1744
1745 val = val << shift;
1746 val2 = val2 << shift;
1747
3ff3f64b
MB
1748 err = snd_soc_update_bits(codec, reg, val_mask, val);
1749 if (err < 0)
db2a4165
FM
1750 return err;
1751
1752 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1753 return err;
1754}
1755EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1756
e13ac2e9
MB
1757/**
1758 * snd_soc_info_volsw_s8 - signed mixer info callback
1759 * @kcontrol: mixer control
1760 * @uinfo: control element information
1761 *
1762 * Callback to provide information about a signed mixer control.
1763 *
1764 * Returns 0 for success.
1765 */
1766int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1767 struct snd_ctl_elem_info *uinfo)
1768{
4eaa9819
JS
1769 struct soc_mixer_control *mc =
1770 (struct soc_mixer_control *)kcontrol->private_value;
1771 int max = mc->max;
1772 int min = mc->min;
e13ac2e9
MB
1773
1774 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1775 uinfo->count = 2;
1776 uinfo->value.integer.min = 0;
1777 uinfo->value.integer.max = max-min;
1778 return 0;
1779}
1780EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1781
1782/**
1783 * snd_soc_get_volsw_s8 - signed mixer get callback
1784 * @kcontrol: mixer control
1785 * @uinfo: control element information
1786 *
1787 * Callback to get the value of a signed mixer control.
1788 *
1789 * Returns 0 for success.
1790 */
1791int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1792 struct snd_ctl_elem_value *ucontrol)
1793{
4eaa9819
JS
1794 struct soc_mixer_control *mc =
1795 (struct soc_mixer_control *)kcontrol->private_value;
e13ac2e9 1796 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 1797 unsigned int reg = mc->reg;
4eaa9819 1798 int min = mc->min;
e13ac2e9
MB
1799 int val = snd_soc_read(codec, reg);
1800
1801 ucontrol->value.integer.value[0] =
1802 ((signed char)(val & 0xff))-min;
1803 ucontrol->value.integer.value[1] =
1804 ((signed char)((val >> 8) & 0xff))-min;
1805 return 0;
1806}
1807EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1808
1809/**
1810 * snd_soc_put_volsw_sgn - signed mixer put callback
1811 * @kcontrol: mixer control
1812 * @uinfo: control element information
1813 *
1814 * Callback to set the value of a signed mixer control.
1815 *
1816 * Returns 0 for success.
1817 */
1818int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1819 struct snd_ctl_elem_value *ucontrol)
1820{
4eaa9819
JS
1821 struct soc_mixer_control *mc =
1822 (struct soc_mixer_control *)kcontrol->private_value;
e13ac2e9 1823 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 1824 unsigned int reg = mc->reg;
4eaa9819 1825 int min = mc->min;
e13ac2e9
MB
1826 unsigned short val;
1827
1828 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1829 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1830
1831 return snd_soc_update_bits(codec, reg, 0xffff, val);
1832}
1833EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1834
8c6529db
LG
1835/**
1836 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1837 * @dai: DAI
1838 * @clk_id: DAI specific clock ID
1839 * @freq: new clock frequency in Hz
1840 * @dir: new clock direction - input/output.
1841 *
1842 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1843 */
1844int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1845 unsigned int freq, int dir)
1846{
dee89c4d
MB
1847 if (dai->ops.set_sysclk)
1848 return dai->ops.set_sysclk(dai, clk_id, freq, dir);
8c6529db
LG
1849 else
1850 return -EINVAL;
1851}
1852EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1853
1854/**
1855 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1856 * @dai: DAI
1857 * @clk_id: DAI specific clock divider ID
1858 * @div: new clock divisor.
1859 *
1860 * Configures the clock dividers. This is used to derive the best DAI bit and
1861 * frame clocks from the system or master clock. It's best to set the DAI bit
1862 * and frame clocks as low as possible to save system power.
1863 */
1864int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
1865 int div_id, int div)
1866{
dee89c4d
MB
1867 if (dai->ops.set_clkdiv)
1868 return dai->ops.set_clkdiv(dai, div_id, div);
8c6529db
LG
1869 else
1870 return -EINVAL;
1871}
1872EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
1873
1874/**
1875 * snd_soc_dai_set_pll - configure DAI PLL.
1876 * @dai: DAI
1877 * @pll_id: DAI specific PLL ID
1878 * @freq_in: PLL input clock frequency in Hz
1879 * @freq_out: requested PLL output clock frequency in Hz
1880 *
1881 * Configures and enables PLL to generate output clock based on input clock.
1882 */
1883int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
1884 int pll_id, unsigned int freq_in, unsigned int freq_out)
1885{
dee89c4d
MB
1886 if (dai->ops.set_pll)
1887 return dai->ops.set_pll(dai, pll_id, freq_in, freq_out);
8c6529db
LG
1888 else
1889 return -EINVAL;
1890}
1891EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
1892
1893/**
1894 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1895 * @dai: DAI
8c6529db
LG
1896 * @fmt: SND_SOC_DAIFMT_ format value.
1897 *
1898 * Configures the DAI hardware format and clocking.
1899 */
1900int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1901{
dee89c4d
MB
1902 if (dai->ops.set_fmt)
1903 return dai->ops.set_fmt(dai, fmt);
8c6529db
LG
1904 else
1905 return -EINVAL;
1906}
1907EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
1908
1909/**
1910 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
1911 * @dai: DAI
1912 * @mask: DAI specific mask representing used slots.
1913 * @slots: Number of slots in use.
1914 *
1915 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
1916 * specific.
1917 */
1918int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
1919 unsigned int mask, int slots)
1920{
dee89c4d
MB
1921 if (dai->ops.set_sysclk)
1922 return dai->ops.set_tdm_slot(dai, mask, slots);
8c6529db
LG
1923 else
1924 return -EINVAL;
1925}
1926EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
1927
1928/**
1929 * snd_soc_dai_set_tristate - configure DAI system or master clock.
1930 * @dai: DAI
1931 * @tristate: tristate enable
1932 *
1933 * Tristates the DAI so that others can use it.
1934 */
1935int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
1936{
dee89c4d
MB
1937 if (dai->ops.set_sysclk)
1938 return dai->ops.set_tristate(dai, tristate);
8c6529db
LG
1939 else
1940 return -EINVAL;
1941}
1942EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
1943
1944/**
1945 * snd_soc_dai_digital_mute - configure DAI system or master clock.
1946 * @dai: DAI
1947 * @mute: mute enable
1948 *
1949 * Mutes the DAI DAC.
1950 */
1951int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
1952{
dee89c4d
MB
1953 if (dai->ops.digital_mute)
1954 return dai->ops.digital_mute(dai, mute);
8c6529db
LG
1955 else
1956 return -EINVAL;
1957}
1958EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
1959
db2a4165
FM
1960static int __devinit snd_soc_init(void)
1961{
384c89e2
MB
1962#ifdef CONFIG_DEBUG_FS
1963 debugfs_root = debugfs_create_dir("asoc", NULL);
1964 if (IS_ERR(debugfs_root) || !debugfs_root) {
1965 printk(KERN_WARNING
1966 "ASoC: Failed to create debugfs directory\n");
1967 debugfs_root = NULL;
1968 }
1969#endif
1970
db2a4165
FM
1971 return platform_driver_register(&soc_driver);
1972}
1973
7d8c16a6 1974static void __exit snd_soc_exit(void)
db2a4165 1975{
384c89e2
MB
1976#ifdef CONFIG_DEBUG_FS
1977 debugfs_remove_recursive(debugfs_root);
1978#endif
3ff3f64b 1979 platform_driver_unregister(&soc_driver);
db2a4165
FM
1980}
1981
1982module_init(snd_soc_init);
1983module_exit(snd_soc_exit);
1984
1985/* Module information */
d331124d 1986MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
db2a4165
FM
1987MODULE_DESCRIPTION("ALSA SoC Core");
1988MODULE_LICENSE("GPL");
8b45a209 1989MODULE_ALIAS("platform:soc-audio");