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