ASoC: Rename snd_soc_register_card() to snd_soc_init_card()
[deliverable/linux.git] / sound / soc / soc-core.c
1 /*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 *
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8 * with code, comments and ideas from :-
9 * Richard Purdie <richard@openedhand.com>
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 *
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>
29 #include <linux/debugfs.h>
30 #include <linux/platform_device.h>
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
38 static DEFINE_MUTEX(pcm_mutex);
39 static DEFINE_MUTEX(io_mutex);
40 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
41
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 */
47 static int pmdown_time = 5000;
48 module_param(pmdown_time, int, 0);
49 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
50
51 /*
52 * This function forces any delayed work to be queued and run.
53 */
54 static 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
70 #ifdef CONFIG_SND_SOC_AC97_BUS
71 /* unregister ac97 codec */
72 static 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 */
80 static void soc_ac97_device_release(struct device *dev){}
81
82 /* register ac97 codec to bus */
83 static 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
91 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
92 codec->card->number, 0, codec->name);
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
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 */
108 static 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;
113 struct snd_soc_dai_link *machine = rtd->dai;
114 struct snd_soc_platform *platform = socdev->platform;
115 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
116 struct snd_soc_dai *codec_dai = machine->codec_dai;
117 int ret = 0;
118
119 mutex_lock(&pcm_mutex);
120
121 /* startup the audio subsystem */
122 if (cpu_dai->ops.startup) {
123 ret = cpu_dai->ops.startup(substream, cpu_dai);
124 if (ret < 0) {
125 printk(KERN_ERR "asoc: can't open interface %s\n",
126 cpu_dai->name);
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
139 if (codec_dai->ops.startup) {
140 ret = codec_dai->ops.startup(substream, codec_dai);
141 if (ret < 0) {
142 printk(KERN_ERR "asoc: can't open codec %s\n",
143 codec_dai->name);
144 goto codec_dai_err;
145 }
146 }
147
148 if (machine->ops && machine->ops->startup) {
149 ret = machine->ops->startup(substream);
150 if (ret < 0) {
151 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
152 goto machine_err;
153 }
154 }
155
156 /* Check that the codec and cpu DAI's are compatible */
157 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
158 runtime->hw.rate_min =
159 max(codec_dai->playback.rate_min,
160 cpu_dai->playback.rate_min);
161 runtime->hw.rate_max =
162 min(codec_dai->playback.rate_max,
163 cpu_dai->playback.rate_max);
164 runtime->hw.channels_min =
165 max(codec_dai->playback.channels_min,
166 cpu_dai->playback.channels_min);
167 runtime->hw.channels_max =
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;
174 } else {
175 runtime->hw.rate_min =
176 max(codec_dai->capture.rate_min,
177 cpu_dai->capture.rate_min);
178 runtime->hw.rate_max =
179 min(codec_dai->capture.rate_max,
180 cpu_dai->capture.rate_max);
181 runtime->hw.channels_min =
182 max(codec_dai->capture.channels_min,
183 cpu_dai->capture.channels_min);
184 runtime->hw.channels_max =
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;
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",
196 codec_dai->name, cpu_dai->name);
197 goto machine_err;
198 }
199 if (!runtime->hw.formats) {
200 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
201 codec_dai->name, cpu_dai->name);
202 goto machine_err;
203 }
204 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
205 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
206 codec_dai->name, cpu_dai->name);
207 goto machine_err;
208 }
209
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);
216
217 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
218 cpu_dai->playback.active = codec_dai->playback.active = 1;
219 else
220 cpu_dai->capture.active = codec_dai->capture.active = 1;
221 cpu_dai->active = codec_dai->active = 1;
222 cpu_dai->runtime = runtime;
223 socdev->codec->active++;
224 mutex_unlock(&pcm_mutex);
225 return 0;
226
227 machine_err:
228 if (machine->ops && machine->ops->shutdown)
229 machine->ops->shutdown(substream);
230
231 codec_dai_err:
232 if (platform->pcm_ops->close)
233 platform->pcm_ops->close(substream);
234
235 platform_err:
236 if (cpu_dai->ops.shutdown)
237 cpu_dai->ops.shutdown(substream, cpu_dai);
238 out:
239 mutex_unlock(&pcm_mutex);
240 return ret;
241 }
242
243 /*
244 * Power down the audio subsystem pmdown_time msecs after close is called.
245 * This is to ensure there are no pops or clicks in between any music tracks
246 * due to DAPM power cycling.
247 */
248 static void close_delayed_work(struct work_struct *work)
249 {
250 struct snd_soc_device *socdev =
251 container_of(work, struct snd_soc_device, delayed_work.work);
252 struct snd_soc_codec *codec = socdev->codec;
253 struct snd_soc_dai *codec_dai;
254 int i;
255
256 mutex_lock(&pcm_mutex);
257 for (i = 0; i < codec->num_dai; i++) {
258 codec_dai = &codec->dai[i];
259
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");
264
265 /* are we waiting on this codec DAI stream */
266 if (codec_dai->pop_wait == 1) {
267
268 /* Reduce power if no longer active */
269 if (codec->active == 0) {
270 pr_debug("pop wq D1 %s %s\n", codec->name,
271 codec_dai->playback.stream_name);
272 snd_soc_dapm_set_bias_level(socdev,
273 SND_SOC_BIAS_PREPARE);
274 }
275
276 codec_dai->pop_wait = 0;
277 snd_soc_dapm_stream_event(codec,
278 codec_dai->playback.stream_name,
279 SND_SOC_DAPM_STREAM_STOP);
280
281 /* Fall into standby if no longer active */
282 if (codec->active == 0) {
283 pr_debug("pop wq D3 %s %s\n", codec->name,
284 codec_dai->playback.stream_name);
285 snd_soc_dapm_set_bias_level(socdev,
286 SND_SOC_BIAS_STANDBY);
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 */
298 static 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;
302 struct snd_soc_dai_link *machine = rtd->dai;
303 struct snd_soc_platform *platform = socdev->platform;
304 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
305 struct snd_soc_dai *codec_dai = machine->codec_dai;
306 struct snd_soc_codec *codec = socdev->codec;
307
308 mutex_lock(&pcm_mutex);
309
310 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
311 cpu_dai->playback.active = codec_dai->playback.active = 0;
312 else
313 cpu_dai->capture.active = codec_dai->capture.active = 0;
314
315 if (codec_dai->playback.active == 0 &&
316 codec_dai->capture.active == 0) {
317 cpu_dai->active = codec_dai->active = 0;
318 }
319 codec->active--;
320
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
327 if (cpu_dai->ops.shutdown)
328 cpu_dai->ops.shutdown(substream, cpu_dai);
329
330 if (codec_dai->ops.shutdown)
331 codec_dai->ops.shutdown(substream, codec_dai);
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);
338 cpu_dai->runtime = NULL;
339
340 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
341 /* start delayed pop wq here for playback streams */
342 codec_dai->pop_wait = 1;
343 schedule_delayed_work(&socdev->delayed_work,
344 msecs_to_jiffies(pmdown_time));
345 } else {
346 /* capture streams can be powered down now */
347 snd_soc_dapm_stream_event(codec,
348 codec_dai->capture.stream_name,
349 SND_SOC_DAPM_STREAM_STOP);
350
351 if (codec->active == 0 && codec_dai->pop_wait == 0)
352 snd_soc_dapm_set_bias_level(socdev,
353 SND_SOC_BIAS_STANDBY);
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 */
365 static 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;
369 struct snd_soc_dai_link *machine = rtd->dai;
370 struct snd_soc_platform *platform = socdev->platform;
371 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
372 struct snd_soc_dai *codec_dai = machine->codec_dai;
373 struct snd_soc_codec *codec = socdev->codec;
374 int ret = 0;
375
376 mutex_lock(&pcm_mutex);
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
386 if (platform->pcm_ops->prepare) {
387 ret = platform->pcm_ops->prepare(substream);
388 if (ret < 0) {
389 printk(KERN_ERR "asoc: platform prepare error\n");
390 goto out;
391 }
392 }
393
394 if (codec_dai->ops.prepare) {
395 ret = codec_dai->ops.prepare(substream, codec_dai);
396 if (ret < 0) {
397 printk(KERN_ERR "asoc: codec DAI prepare error\n");
398 goto out;
399 }
400 }
401
402 if (cpu_dai->ops.prepare) {
403 ret = cpu_dai->ops.prepare(substream, cpu_dai);
404 if (ret < 0) {
405 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
406 goto out;
407 }
408 }
409
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 }
416
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);
421
422 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
423 snd_soc_dapm_stream_event(codec,
424 codec_dai->playback.stream_name,
425 SND_SOC_DAPM_STREAM_START);
426 else
427 snd_soc_dapm_stream_event(codec,
428 codec_dai->capture.stream_name,
429 SND_SOC_DAPM_STREAM_START);
430
431 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
432 snd_soc_dai_digital_mute(codec_dai, 0);
433
434 } else {
435 /* codec already powered - power on widgets */
436 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
437 snd_soc_dapm_stream_event(codec,
438 codec_dai->playback.stream_name,
439 SND_SOC_DAPM_STREAM_START);
440 else
441 snd_soc_dapm_stream_event(codec,
442 codec_dai->capture.stream_name,
443 SND_SOC_DAPM_STREAM_START);
444
445 snd_soc_dai_digital_mute(codec_dai, 0);
446 }
447
448 out:
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 */
458 static 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;
463 struct snd_soc_dai_link *machine = rtd->dai;
464 struct snd_soc_platform *platform = socdev->platform;
465 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
466 struct snd_soc_dai *codec_dai = machine->codec_dai;
467 int ret = 0;
468
469 mutex_lock(&pcm_mutex);
470
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");
475 goto out;
476 }
477 }
478
479 if (codec_dai->ops.hw_params) {
480 ret = codec_dai->ops.hw_params(substream, params, codec_dai);
481 if (ret < 0) {
482 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
483 codec_dai->name);
484 goto codec_err;
485 }
486 }
487
488 if (cpu_dai->ops.hw_params) {
489 ret = cpu_dai->ops.hw_params(substream, params, cpu_dai);
490 if (ret < 0) {
491 printk(KERN_ERR "asoc: interface %s hw params failed\n",
492 cpu_dai->name);
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) {
500 printk(KERN_ERR "asoc: platform %s hw params failed\n",
501 platform->name);
502 goto platform_err;
503 }
504 }
505
506 out:
507 mutex_unlock(&pcm_mutex);
508 return ret;
509
510 platform_err:
511 if (cpu_dai->ops.hw_free)
512 cpu_dai->ops.hw_free(substream, cpu_dai);
513
514 interface_err:
515 if (codec_dai->ops.hw_free)
516 codec_dai->ops.hw_free(substream, codec_dai);
517
518 codec_err:
519 if (machine->ops && machine->ops->hw_free)
520 machine->ops->hw_free(substream);
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 */
529 static 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;
533 struct snd_soc_dai_link *machine = rtd->dai;
534 struct snd_soc_platform *platform = socdev->platform;
535 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
536 struct snd_soc_dai *codec_dai = machine->codec_dai;
537 struct snd_soc_codec *codec = socdev->codec;
538
539 mutex_lock(&pcm_mutex);
540
541 /* apply codec digital mute */
542 if (!codec->active)
543 snd_soc_dai_digital_mute(codec_dai, 1);
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 */
554 if (codec_dai->ops.hw_free)
555 codec_dai->ops.hw_free(substream, codec_dai);
556
557 if (cpu_dai->ops.hw_free)
558 cpu_dai->ops.hw_free(substream, cpu_dai);
559
560 mutex_unlock(&pcm_mutex);
561 return 0;
562 }
563
564 static 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;
568 struct snd_soc_dai_link *machine = rtd->dai;
569 struct snd_soc_platform *platform = socdev->platform;
570 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
571 struct snd_soc_dai *codec_dai = machine->codec_dai;
572 int ret;
573
574 if (codec_dai->ops.trigger) {
575 ret = codec_dai->ops.trigger(substream, cmd, codec_dai);
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
586 if (cpu_dai->ops.trigger) {
587 ret = cpu_dai->ops.trigger(substream, cmd, cpu_dai);
588 if (ret < 0)
589 return ret;
590 }
591 return 0;
592 }
593
594 /* ASoC PCM operations */
595 static 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 */
606 static int soc_suspend(struct platform_device *pdev, pm_message_t state)
607 {
608 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
609 struct snd_soc_card *card = socdev->card;
610 struct snd_soc_platform *platform = socdev->platform;
611 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
612 struct snd_soc_codec *codec = socdev->codec;
613 int i;
614
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
625 /* mute any active DAC's */
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);
630 }
631
632 /* suspend all pcms */
633 for (i = 0; i < card->num_links; i++)
634 snd_pcm_suspend_all(card->dai_link[i].pcm);
635
636 if (card->suspend_pre)
637 card->suspend_pre(pdev, state);
638
639 for (i = 0; i < card->num_links; i++) {
640 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
641 if (cpu_dai->suspend && !cpu_dai->ac97_control)
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 */
648 run_delayed_work(&socdev->delayed_work);
649 codec->suspend_bias_level = codec->bias_level;
650
651 for (i = 0; i < codec->num_dai; i++) {
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
665 for (i = 0; i < card->num_links; i++) {
666 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
667 if (cpu_dai->suspend && cpu_dai->ac97_control)
668 cpu_dai->suspend(pdev, cpu_dai);
669 }
670
671 if (card->suspend_post)
672 card->suspend_post(pdev, state);
673
674 return 0;
675 }
676
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 */
680 static void soc_resume_deferred(struct work_struct *work)
681 {
682 struct snd_soc_device *socdev = container_of(work,
683 struct snd_soc_device,
684 deferred_resume_work);
685 struct snd_soc_card *card = socdev->card;
686 struct snd_soc_platform *platform = socdev->platform;
687 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
688 struct snd_soc_codec *codec = socdev->codec;
689 struct platform_device *pdev = to_platform_device(socdev->dev);
690 int i;
691
692 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
693 * so userspace apps are blocked from touching us
694 */
695
696 dev_dbg(socdev->dev, "starting resume work\n");
697
698 if (card->resume_pre)
699 card->resume_pre(pdev);
700
701 for (i = 0; i < card->num_links; i++) {
702 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
703 if (cpu_dai->resume && cpu_dai->ac97_control)
704 cpu_dai->resume(pdev, cpu_dai);
705 }
706
707 if (codec_dev->resume)
708 codec_dev->resume(pdev);
709
710 for (i = 0; i < codec->num_dai; i++) {
711 char *stream = codec->dai[i].playback.stream_name;
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
721 /* unmute any active DACs */
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);
726 }
727
728 for (i = 0; i < card->num_links; i++) {
729 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
730 if (cpu_dai->resume && !cpu_dai->ac97_control)
731 cpu_dai->resume(pdev, cpu_dai);
732 if (platform->resume)
733 platform->resume(pdev, cpu_dai);
734 }
735
736 if (card->resume_post)
737 card->resume_post(pdev);
738
739 dev_dbg(socdev->dev, "resume work completed\n");
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 */
746 static int soc_resume(struct platform_device *pdev)
747 {
748 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
749
750 dev_dbg(socdev->dev, "scheduling resume work\n");
751
752 if (!schedule_work(&socdev->deferred_resume_work))
753 dev_err(socdev->dev, "resume work item may be lost\n");
754
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 */
764 static int soc_probe(struct platform_device *pdev)
765 {
766 int ret = 0, i;
767 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
768 struct snd_soc_card *card = socdev->card;
769 struct snd_soc_platform *platform = socdev->platform;
770 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
771
772 if (card->probe) {
773 ret = card->probe(pdev);
774 if (ret < 0)
775 return ret;
776 }
777
778 for (i = 0; i < card->num_links; i++) {
779 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
780 if (cpu_dai->probe) {
781 ret = cpu_dai->probe(pdev, cpu_dai);
782 if (ret < 0)
783 goto cpu_dai_err;
784 }
785 }
786
787 if (codec_dev->probe) {
788 ret = codec_dev->probe(pdev);
789 if (ret < 0)
790 goto cpu_dai_err;
791 }
792
793 if (platform->probe) {
794 ret = platform->probe(pdev);
795 if (ret < 0)
796 goto platform_err;
797 }
798
799 /* DAPM stream work */
800 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
801 #ifdef CONFIG_PM
802 /* deferred resume work */
803 INIT_WORK(&socdev->deferred_resume_work, soc_resume_deferred);
804 #endif
805
806 return 0;
807
808 platform_err:
809 if (codec_dev->remove)
810 codec_dev->remove(pdev);
811
812 cpu_dai_err:
813 for (i--; i >= 0; i--) {
814 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
815 if (cpu_dai->remove)
816 cpu_dai->remove(pdev, cpu_dai);
817 }
818
819 if (card->remove)
820 card->remove(pdev);
821
822 return ret;
823 }
824
825 /* removes a socdev */
826 static int soc_remove(struct platform_device *pdev)
827 {
828 int i;
829 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
830 struct snd_soc_card *card = socdev->card;
831 struct snd_soc_platform *platform = socdev->platform;
832 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
833
834 run_delayed_work(&socdev->delayed_work);
835
836 if (platform->remove)
837 platform->remove(pdev);
838
839 if (codec_dev->remove)
840 codec_dev->remove(pdev);
841
842 for (i = 0; i < card->num_links; i++) {
843 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
844 if (cpu_dai->remove)
845 cpu_dai->remove(pdev, cpu_dai);
846 }
847
848 if (card->remove)
849 card->remove(pdev);
850
851 return 0;
852 }
853
854 /* ASoC platform driver */
855 static struct platform_driver soc_driver = {
856 .driver = {
857 .name = "soc-audio",
858 .owner = THIS_MODULE,
859 },
860 .probe = soc_probe,
861 .remove = soc_remove,
862 .suspend = soc_suspend,
863 .resume = soc_resume,
864 };
865
866 /* create a new pcm */
867 static 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;
871 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
872 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
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;
881
882 rtd->dai = dai_link;
883 rtd->socdev = socdev;
884 codec_dai->codec = socdev->codec;
885
886 /* check client and interface hw capabilities */
887 sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
888 num);
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) {
898 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
899 codec->name);
900 kfree(rtd);
901 return ret;
902 }
903
904 dai_link->pcm = pcm;
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 */
934 static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
935 {
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);
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;
969
970 return count;
971 }
972 static 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
979 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
980
981 #ifdef CONFIG_DEBUG_FS
982 static int codec_reg_open_file(struct inode *inode, struct file *file)
983 {
984 file->private_data = inode->i_private;
985 return 0;
986 }
987
988 static 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
1003 static 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
1035 static 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
1041 static 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;
1060 exit2:
1061 debugfs_remove_recursive(root);
1062 exit1:
1063 dev_err(socdev->dev, "debugfs is not available\n");
1064 }
1065
1066 static 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
1074 static inline void soc_init_debugfs(struct snd_soc_device *socdev)
1075 {
1076 }
1077
1078 static inline void soc_cleanup_debugfs(struct snd_soc_device *socdev)
1079 {
1080 }
1081 #endif
1082
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 */
1091 int 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 }
1115 EXPORT_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 */
1123 void 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 }
1131 EXPORT_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 */
1144 int 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 }
1160 EXPORT_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 */
1174 int 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 }
1188 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1189
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 */
1198 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1199 {
1200 struct snd_soc_codec *codec = socdev->codec;
1201 struct snd_soc_card *card = socdev->card;
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 */
1220 for (i = 0; i < card->num_links; i++) {
1221 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1222 if (ret < 0) {
1223 printk(KERN_ERR "asoc: can't create pcm %s\n",
1224 card->dai_link[i].stream_name);
1225 mutex_unlock(&codec->mutex);
1226 return ret;
1227 }
1228 }
1229
1230 mutex_unlock(&codec->mutex);
1231 return ret;
1232 }
1233 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1234
1235 /**
1236 * snd_soc_init_card - register sound card
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 */
1244 int snd_soc_init_card(struct snd_soc_device *socdev)
1245 {
1246 struct snd_soc_codec *codec = socdev->codec;
1247 struct snd_soc_card *card = socdev->card;
1248 int ret = 0, i, ac97 = 0, err = 0;
1249
1250 for (i = 0; i < card->num_links; i++) {
1251 if (card->dai_link[i].init) {
1252 err = card->dai_link[i].init(codec);
1253 if (err < 0) {
1254 printk(KERN_ERR "asoc: failed to init %s\n",
1255 card->dai_link[i].stream_name);
1256 continue;
1257 }
1258 }
1259 if (card->dai_link[i].codec_dai->ac97_control)
1260 ac97 = 1;
1261 }
1262 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1263 "%s", card->name);
1264 snprintf(codec->card->longname, sizeof(codec->card->longname),
1265 "%s (%s)", card->name, codec->name);
1266
1267 ret = snd_card_register(codec->card);
1268 if (ret < 0) {
1269 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1270 codec->name);
1271 goto out;
1272 }
1273
1274 mutex_lock(&codec->mutex);
1275 #ifdef CONFIG_SND_SOC_AC97_BUS
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);
1281 mutex_unlock(&codec->mutex);
1282 goto out;
1283 }
1284 }
1285 #endif
1286
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)
1293 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1294
1295 soc_init_debugfs(socdev);
1296 mutex_unlock(&codec->mutex);
1297
1298 out:
1299 return ret;
1300 }
1301 EXPORT_SYMBOL_GPL(snd_soc_init_card);
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 */
1310 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1311 {
1312 struct snd_soc_codec *codec = socdev->codec;
1313 #ifdef CONFIG_SND_SOC_AC97_BUS
1314 struct snd_soc_dai *codec_dai;
1315 int i;
1316 #endif
1317
1318 mutex_lock(&codec->mutex);
1319 soc_cleanup_debugfs(socdev);
1320 #ifdef CONFIG_SND_SOC_AC97_BUS
1321 for (i = 0; i < codec->num_dai; i++) {
1322 codec_dai = &codec->dai[i];
1323 if (codec_dai->ac97_control && codec->ac97) {
1324 soc_ac97_dev_unregister(codec);
1325 goto free_card;
1326 }
1327 }
1328 free_card:
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 }
1336 EXPORT_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 */
1345 int 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 }
1359 EXPORT_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 */
1371 struct 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;
1379 template.index = 0;
1380
1381 return snd_ctl_new1(&template, data);
1382 }
1383 EXPORT_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 */
1395 int 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;
1402 uinfo->value.enumerated.items = e->max;
1403
1404 if (uinfo->value.enumerated.item > e->max - 1)
1405 uinfo->value.enumerated.item = e->max - 1;
1406 strcpy(uinfo->value.enumerated.name,
1407 e->texts[uinfo->value.enumerated.item]);
1408 return 0;
1409 }
1410 EXPORT_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 */
1421 int 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
1428 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1429 ;
1430 val = snd_soc_read(codec, e->reg);
1431 ucontrol->value.enumerated.item[0]
1432 = (val >> e->shift_l) & (bitmask - 1);
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 }
1439 EXPORT_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 */
1450 int 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
1458 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1459 ;
1460 if (ucontrol->value.enumerated.item[0] > e->max - 1)
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) {
1465 if (ucontrol->value.enumerated.item[1] > e->max - 1)
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 }
1473 EXPORT_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 */
1485 int 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;
1492 uinfo->value.enumerated.items = e->max;
1493
1494 if (uinfo->value.enumerated.item > e->max - 1)
1495 uinfo->value.enumerated.item = e->max - 1;
1496 strcpy(uinfo->value.enumerated.name,
1497 e->texts[uinfo->value.enumerated.item]);
1498 return 0;
1499 }
1500 EXPORT_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 */
1511 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1512 struct snd_ctl_elem_info *uinfo)
1513 {
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;
1520
1521 uinfo->count = 1;
1522 uinfo->value.integer.min = 0;
1523 uinfo->value.integer.max = max;
1524 return 0;
1525 }
1526 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1527
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 */
1537 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1538 struct snd_ctl_elem_info *uinfo)
1539 {
1540 struct soc_mixer_control *mc =
1541 (struct soc_mixer_control *)kcontrol->private_value;
1542 int max = mc->max;
1543 unsigned int shift = mc->shift;
1544 unsigned int rshift = mc->rshift;
1545
1546 if (max == 1)
1547 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1548 else
1549 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1550
1551 uinfo->count = shift == rshift ? 1 : 2;
1552 uinfo->value.integer.min = 0;
1553 uinfo->value.integer.max = max;
1554 return 0;
1555 }
1556 EXPORT_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 */
1567 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1568 struct snd_ctl_elem_value *ucontrol)
1569 {
1570 struct soc_mixer_control *mc =
1571 (struct soc_mixer_control *)kcontrol->private_value;
1572 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1573 unsigned int reg = mc->reg;
1574 unsigned int shift = mc->shift;
1575 unsigned int rshift = mc->rshift;
1576 int max = mc->max;
1577 unsigned int mask = (1 << fls(max)) - 1;
1578 unsigned int invert = mc->invert;
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] =
1587 max - ucontrol->value.integer.value[0];
1588 if (shift != rshift)
1589 ucontrol->value.integer.value[1] =
1590 max - ucontrol->value.integer.value[1];
1591 }
1592
1593 return 0;
1594 }
1595 EXPORT_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 */
1606 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1607 struct snd_ctl_elem_value *ucontrol)
1608 {
1609 struct soc_mixer_control *mc =
1610 (struct soc_mixer_control *)kcontrol->private_value;
1611 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1612 unsigned int reg = mc->reg;
1613 unsigned int shift = mc->shift;
1614 unsigned int rshift = mc->rshift;
1615 int max = mc->max;
1616 unsigned int mask = (1 << fls(max)) - 1;
1617 unsigned int invert = mc->invert;
1618 unsigned short val, val2, val_mask;
1619
1620 val = (ucontrol->value.integer.value[0] & mask);
1621 if (invert)
1622 val = max - val;
1623 val_mask = mask << shift;
1624 val = val << shift;
1625 if (shift != rshift) {
1626 val2 = (ucontrol->value.integer.value[1] & mask);
1627 if (invert)
1628 val2 = max - val2;
1629 val_mask |= mask << rshift;
1630 val |= val2 << rshift;
1631 }
1632 return snd_soc_update_bits(codec, reg, val_mask, val);
1633 }
1634 EXPORT_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 */
1646 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1647 struct snd_ctl_elem_info *uinfo)
1648 {
1649 struct soc_mixer_control *mc =
1650 (struct soc_mixer_control *)kcontrol->private_value;
1651 int max = mc->max;
1652
1653 if (max == 1)
1654 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1655 else
1656 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1657
1658 uinfo->count = 2;
1659 uinfo->value.integer.min = 0;
1660 uinfo->value.integer.max = max;
1661 return 0;
1662 }
1663 EXPORT_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 */
1674 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1675 struct snd_ctl_elem_value *ucontrol)
1676 {
1677 struct soc_mixer_control *mc =
1678 (struct soc_mixer_control *)kcontrol->private_value;
1679 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1680 unsigned int reg = mc->reg;
1681 unsigned int reg2 = mc->rreg;
1682 unsigned int shift = mc->shift;
1683 int max = mc->max;
1684 unsigned int mask = (1<<fls(max))-1;
1685 unsigned int invert = mc->invert;
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] =
1693 max - ucontrol->value.integer.value[0];
1694 ucontrol->value.integer.value[1] =
1695 max - ucontrol->value.integer.value[1];
1696 }
1697
1698 return 0;
1699 }
1700 EXPORT_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 */
1711 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1712 struct snd_ctl_elem_value *ucontrol)
1713 {
1714 struct soc_mixer_control *mc =
1715 (struct soc_mixer_control *)kcontrol->private_value;
1716 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1717 unsigned int reg = mc->reg;
1718 unsigned int reg2 = mc->rreg;
1719 unsigned int shift = mc->shift;
1720 int max = mc->max;
1721 unsigned int mask = (1 << fls(max)) - 1;
1722 unsigned int invert = mc->invert;
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) {
1731 val = max - val;
1732 val2 = max - val2;
1733 }
1734
1735 val = val << shift;
1736 val2 = val2 << shift;
1737
1738 err = snd_soc_update_bits(codec, reg, val_mask, val);
1739 if (err < 0)
1740 return err;
1741
1742 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1743 return err;
1744 }
1745 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1746
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 */
1756 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1757 struct snd_ctl_elem_info *uinfo)
1758 {
1759 struct soc_mixer_control *mc =
1760 (struct soc_mixer_control *)kcontrol->private_value;
1761 int max = mc->max;
1762 int min = mc->min;
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 }
1770 EXPORT_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 */
1781 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1782 struct snd_ctl_elem_value *ucontrol)
1783 {
1784 struct soc_mixer_control *mc =
1785 (struct soc_mixer_control *)kcontrol->private_value;
1786 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1787 unsigned int reg = mc->reg;
1788 int min = mc->min;
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 }
1797 EXPORT_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 */
1808 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1809 struct snd_ctl_elem_value *ucontrol)
1810 {
1811 struct soc_mixer_control *mc =
1812 (struct soc_mixer_control *)kcontrol->private_value;
1813 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1814 unsigned int reg = mc->reg;
1815 int min = mc->min;
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 }
1823 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1824
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 */
1834 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1835 unsigned int freq, int dir)
1836 {
1837 if (dai->ops.set_sysclk)
1838 return dai->ops.set_sysclk(dai, clk_id, freq, dir);
1839 else
1840 return -EINVAL;
1841 }
1842 EXPORT_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 */
1854 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
1855 int div_id, int div)
1856 {
1857 if (dai->ops.set_clkdiv)
1858 return dai->ops.set_clkdiv(dai, div_id, div);
1859 else
1860 return -EINVAL;
1861 }
1862 EXPORT_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 */
1873 int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
1874 int pll_id, unsigned int freq_in, unsigned int freq_out)
1875 {
1876 if (dai->ops.set_pll)
1877 return dai->ops.set_pll(dai, pll_id, freq_in, freq_out);
1878 else
1879 return -EINVAL;
1880 }
1881 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
1882
1883 /**
1884 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1885 * @dai: DAI
1886 * @fmt: SND_SOC_DAIFMT_ format value.
1887 *
1888 * Configures the DAI hardware format and clocking.
1889 */
1890 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1891 {
1892 if (dai->ops.set_fmt)
1893 return dai->ops.set_fmt(dai, fmt);
1894 else
1895 return -EINVAL;
1896 }
1897 EXPORT_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 */
1908 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
1909 unsigned int mask, int slots)
1910 {
1911 if (dai->ops.set_sysclk)
1912 return dai->ops.set_tdm_slot(dai, mask, slots);
1913 else
1914 return -EINVAL;
1915 }
1916 EXPORT_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 */
1925 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
1926 {
1927 if (dai->ops.set_sysclk)
1928 return dai->ops.set_tristate(dai, tristate);
1929 else
1930 return -EINVAL;
1931 }
1932 EXPORT_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 */
1941 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
1942 {
1943 if (dai->ops.digital_mute)
1944 return dai->ops.digital_mute(dai, mute);
1945 else
1946 return -EINVAL;
1947 }
1948 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
1949
1950 static int __devinit snd_soc_init(void)
1951 {
1952 return platform_driver_register(&soc_driver);
1953 }
1954
1955 static void snd_soc_exit(void)
1956 {
1957 platform_driver_unregister(&soc_driver);
1958 }
1959
1960 module_init(snd_soc_init);
1961 module_exit(snd_soc_exit);
1962
1963 /* Module information */
1964 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1965 MODULE_DESCRIPTION("ALSA SoC Core");
1966 MODULE_LICENSE("GPL");
1967 MODULE_ALIAS("platform:soc-audio");
This page took 0.072641 seconds and 5 git commands to generate.