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