2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood
6 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 * 12th Aug 2005 Initial version.
15 * 25th Oct 2005 Working Codec, Interface and Platform registration.
18 * o Add hw rules to enforce rates, etc.
19 * o More testing with other codecs/machines.
20 * o Add more codecs and platforms to ensure good API coverage.
21 * o Support TDM on PCM and I2S
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
29 #include <linux/bitops.h>
30 #include <linux/platform_device.h>
31 #include <sound/driver.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
36 #include <sound/soc-dapm.h>
37 #include <sound/initval.h>
42 #define dbg(format, arg...) printk(format, ## arg)
44 #define dbg(format, arg...)
46 /* debug DAI capabilities matching */
47 #define SOC_DEBUG_DAI 0
49 #define dbgc(format, arg...) printk(format, ## arg)
51 #define dbgc(format, arg...)
54 static DEFINE_MUTEX(pcm_mutex
);
55 static DEFINE_MUTEX(io_mutex
);
56 static struct workqueue_struct
*soc_workq
;
57 static struct work_struct soc_stream_work
;
58 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq
);
60 /* supported sample rates */
61 /* ATTENTION: these values depend on the definition in pcm.h! */
62 static const unsigned int rates
[] = {
63 5512, 8000, 11025, 16000, 22050, 32000, 44100,
64 48000, 64000, 88200, 96000, 176400, 192000
68 * This is a timeout to do a DAPM powerdown after a stream is closed().
69 * It can be used to eliminate pops between different playback streams, e.g.
70 * between two audio tracks.
72 static int pmdown_time
= 5000;
73 module_param(pmdown_time
, int, 0);
74 MODULE_PARM_DESC(pmdown_time
, "DAPM stream powerdown time (msecs)");
76 #ifdef CONFIG_SND_SOC_AC97_BUS
77 /* unregister ac97 codec */
78 static int soc_ac97_dev_unregister(struct snd_soc_codec
*codec
)
80 if (codec
->ac97
->dev
.bus
)
81 device_unregister(&codec
->ac97
->dev
);
85 /* stop no dev release warning */
86 static void soc_ac97_device_release(struct device
*dev
){}
88 /* register ac97 codec to bus */
89 static int soc_ac97_dev_register(struct snd_soc_codec
*codec
)
93 codec
->ac97
->dev
.bus
= &ac97_bus_type
;
94 codec
->ac97
->dev
.parent
= NULL
;
95 codec
->ac97
->dev
.release
= soc_ac97_device_release
;
97 snprintf(codec
->ac97
->dev
.bus_id
, BUS_ID_SIZE
, "%d-%d:%s",
98 codec
->card
->number
, 0, codec
->name
);
99 err
= device_register(&codec
->ac97
->dev
);
101 snd_printk(KERN_ERR
"Can't register ac97 bus\n");
102 codec
->ac97
->dev
.bus
= NULL
;
109 static inline const char* get_dai_name(int type
)
112 case SND_SOC_DAI_AC97
:
114 case SND_SOC_DAI_I2S
:
116 case SND_SOC_DAI_PCM
:
122 /* get rate format from rate */
123 static inline int soc_get_rate_format(int rate
)
127 for (i
= 0; i
< ARRAY_SIZE(rates
); i
++) {
128 if (rates
[i
] == rate
)
134 /* gets the audio system mclk/sysclk for the given parameters */
135 static unsigned inline int soc_get_mclk(struct snd_soc_pcm_runtime
*rtd
,
136 struct snd_soc_clock_info
*info
)
138 struct snd_soc_device
*socdev
= rtd
->socdev
;
139 struct snd_soc_machine
*machine
= socdev
->machine
;
142 /* find the matching machine config and get it's mclk for the given
143 * sample rate and hardware format */
144 for(i
= 0; i
< machine
->num_links
; i
++) {
145 if (machine
->dai_link
[i
].cpu_dai
== rtd
->cpu_dai
&&
146 machine
->dai_link
[i
].config_sysclk
)
147 return machine
->dai_link
[i
].config_sysclk(rtd
, info
);
152 /* changes a bitclk multiplier mask to a divider mask */
153 static u16
soc_bfs_mult_to_div(u16 bfs
, int rate
, unsigned int mclk
,
154 unsigned int pcmfmt
, unsigned int chn
)
158 int size
= snd_pcm_format_physical_width(pcmfmt
), min
= 0;
163 /* the minimum bit clock that has enough bandwidth */
164 min
= size
* rate
* chn
;
165 dbgc("mult --> div min bclk %d with mclk %d\n", min
, mclk
);
167 for (i
= 0; i
< 16; i
++) {
168 if ((bfs
>> i
) & 0x1) {
169 j
= rate
* SND_SOC_FSB_REAL(1<<i
);
172 bfs_
|= SND_SOC_FSBD(mclk
/j
);
173 dbgc("mult --> div support mult %d\n",
174 SND_SOC_FSB_REAL(1<<i
));
182 /* changes a bitclk divider mask to a multiplier mask */
183 static u16
soc_bfs_div_to_mult(u16 bfs
, int rate
, unsigned int mclk
,
184 unsigned int pcmfmt
, unsigned int chn
)
189 int size
= snd_pcm_format_physical_width(pcmfmt
), min
= 0;
194 /* the minimum bit clock that has enough bandwidth */
195 min
= size
* rate
* chn
;
196 dbgc("div to mult min bclk %d with mclk %d\n", min
, mclk
);
198 for (i
= 0; i
< 16; i
++) {
199 if ((bfs
>> i
) & 0x1) {
200 j
= mclk
/ (SND_SOC_FSBD_REAL(1<<i
));
202 bfs_
|= SND_SOC_FSB(j
/rate
);
203 dbgc("div --> mult support div %d\n",
204 SND_SOC_FSBD_REAL(1<<i
));
212 /* Matches codec DAI and SoC CPU DAI hardware parameters */
213 static int soc_hw_match_params(struct snd_pcm_substream
*substream
,
214 struct snd_pcm_hw_params
*params
)
216 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
217 struct snd_soc_dai_mode
*codec_dai_mode
= NULL
;
218 struct snd_soc_dai_mode
*cpu_dai_mode
= NULL
;
219 struct snd_soc_clock_info clk_info
;
220 unsigned int fs
, mclk
, codec_bfs
, cpu_bfs
, rate
= params_rate(params
),
221 chn
, j
, k
, cpu_bclk
, codec_bclk
, pcmrate
;
224 dbg("asoc: match version %s\n", SND_SOC_VERSION
);
225 clk_info
.rate
= rate
;
226 pcmrate
= soc_get_rate_format(rate
);
228 /* try and find a match from the codec and cpu DAI capabilities */
229 for (j
= 0; j
< rtd
->codec_dai
->caps
.num_modes
; j
++) {
230 for (k
= 0; k
< rtd
->cpu_dai
->caps
.num_modes
; k
++) {
231 codec_dai_mode
= &rtd
->codec_dai
->caps
.mode
[j
];
232 cpu_dai_mode
= &rtd
->cpu_dai
->caps
.mode
[k
];
234 if (!(codec_dai_mode
->pcmrate
& cpu_dai_mode
->pcmrate
&
236 dbgc("asoc: DAI[%d:%d] failed to match rate\n", j
, k
);
240 fmt
= codec_dai_mode
->fmt
& cpu_dai_mode
->fmt
;
241 if (!(fmt
& SND_SOC_DAIFMT_FORMAT_MASK
)) {
242 dbgc("asoc: DAI[%d:%d] failed to match format\n", j
, k
);
246 if (!(fmt
& SND_SOC_DAIFMT_CLOCK_MASK
)) {
247 dbgc("asoc: DAI[%d:%d] failed to match clock masters\n",
252 if (!(fmt
& SND_SOC_DAIFMT_INV_MASK
)) {
253 dbgc("asoc: DAI[%d:%d] failed to match invert\n", j
, k
);
257 if (!(codec_dai_mode
->pcmfmt
& cpu_dai_mode
->pcmfmt
)) {
258 dbgc("asoc: DAI[%d:%d] failed to match pcm format\n", j
, k
);
262 if (!(codec_dai_mode
->pcmdir
& cpu_dai_mode
->pcmdir
)) {
263 dbgc("asoc: DAI[%d:%d] failed to match direction\n", j
, k
);
267 /* todo - still need to add tdm selection */
268 rtd
->cpu_dai
->dai_runtime
.fmt
=
269 rtd
->codec_dai
->dai_runtime
.fmt
=
270 1 << (ffs(fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) -1) |
271 1 << (ffs(fmt
& SND_SOC_DAIFMT_CLOCK_MASK
) - 1) |
272 1 << (ffs(fmt
& SND_SOC_DAIFMT_INV_MASK
) - 1);
273 clk_info
.bclk_master
=
274 rtd
->cpu_dai
->dai_runtime
.fmt
& SND_SOC_DAIFMT_CLOCK_MASK
;
276 /* make sure the ratio between rate and master
277 * clock is acceptable*/
278 fs
= (cpu_dai_mode
->fs
& codec_dai_mode
->fs
);
280 dbgc("asoc: DAI[%d:%d] failed to match FS\n", j
, k
);
283 clk_info
.fs
= rtd
->cpu_dai
->dai_runtime
.fs
=
284 rtd
->codec_dai
->dai_runtime
.fs
= fs
;
286 /* calculate audio system clocking using slowest clocks possible*/
287 mclk
= soc_get_mclk(rtd
, &clk_info
);
289 dbgc("asoc: DAI[%d:%d] configuration not clockable\n", j
, k
);
290 dbgc("asoc: rate %d fs %d master %x\n", rate
, fs
,
291 clk_info
.bclk_master
);
295 /* calculate word size (per channel) and frame size */
296 rtd
->codec_dai
->dai_runtime
.pcmfmt
=
297 rtd
->cpu_dai
->dai_runtime
.pcmfmt
=
298 1 << params_format(params
);
300 chn
= params_channels(params
);
301 /* i2s always has left and right */
302 if (params_channels(params
) == 1 &&
303 rtd
->cpu_dai
->dai_runtime
.fmt
& (SND_SOC_DAIFMT_I2S
|
304 SND_SOC_DAIFMT_RIGHT_J
| SND_SOC_DAIFMT_LEFT_J
))
307 /* Calculate bfs - the ratio between bitclock and the sample rate
308 * We must take into consideration the dividers and multipliers
309 * used in the codec and cpu DAI modes. We always choose the
310 * lowest possible clocks to reduce power.
312 if (codec_dai_mode
->flags
& cpu_dai_mode
->flags
&
313 SND_SOC_DAI_BFS_DIV
) {
314 /* cpu & codec bfs dividers */
315 rtd
->cpu_dai
->dai_runtime
.bfs
=
316 rtd
->codec_dai
->dai_runtime
.bfs
=
317 1 << (fls(codec_dai_mode
->bfs
& cpu_dai_mode
->bfs
) - 1);
318 } else if (codec_dai_mode
->flags
& SND_SOC_DAI_BFS_DIV
) {
319 /* normalise bfs codec divider & cpu mult */
320 codec_bfs
= soc_bfs_div_to_mult(codec_dai_mode
->bfs
, rate
,
321 mclk
, rtd
->codec_dai
->dai_runtime
.pcmfmt
, chn
);
322 rtd
->cpu_dai
->dai_runtime
.bfs
=
323 1 << (ffs(codec_bfs
& cpu_dai_mode
->bfs
) - 1);
324 cpu_bfs
= soc_bfs_mult_to_div(cpu_dai_mode
->bfs
, rate
, mclk
,
325 rtd
->codec_dai
->dai_runtime
.pcmfmt
, chn
);
326 rtd
->codec_dai
->dai_runtime
.bfs
=
327 1 << (fls(codec_dai_mode
->bfs
& cpu_bfs
) - 1);
328 } else if (cpu_dai_mode
->flags
& SND_SOC_DAI_BFS_DIV
) {
329 /* normalise bfs codec mult & cpu divider */
330 codec_bfs
= soc_bfs_mult_to_div(codec_dai_mode
->bfs
, rate
,
331 mclk
, rtd
->codec_dai
->dai_runtime
.pcmfmt
, chn
);
332 rtd
->cpu_dai
->dai_runtime
.bfs
=
333 1 << (fls(codec_bfs
& cpu_dai_mode
->bfs
) -1);
334 cpu_bfs
= soc_bfs_div_to_mult(cpu_dai_mode
->bfs
, rate
, mclk
,
335 rtd
->codec_dai
->dai_runtime
.pcmfmt
, chn
);
336 rtd
->codec_dai
->dai_runtime
.bfs
=
337 1 << (ffs(codec_dai_mode
->bfs
& cpu_bfs
) -1);
339 /* codec & cpu bfs rate multipliers */
340 rtd
->cpu_dai
->dai_runtime
.bfs
=
341 rtd
->codec_dai
->dai_runtime
.bfs
=
342 1 << (ffs(codec_dai_mode
->bfs
& cpu_dai_mode
->bfs
) -1);
345 /* make sure the bit clock speed is acceptable */
346 if (!rtd
->cpu_dai
->dai_runtime
.bfs
||
347 !rtd
->codec_dai
->dai_runtime
.bfs
) {
348 dbgc("asoc: DAI[%d:%d] failed to match BFS\n", j
, k
);
349 dbgc("asoc: cpu_dai %x codec %x\n",
350 rtd
->cpu_dai
->dai_runtime
.bfs
,
351 rtd
->codec_dai
->dai_runtime
.bfs
);
352 dbgc("asoc: mclk %d hwfmt %x\n", mclk
, fmt
);
359 printk(KERN_ERR
"asoc: no matching DAI found between codec and CPU\n");
363 /* we have matching DAI's, so complete the runtime info */
364 rtd
->codec_dai
->dai_runtime
.pcmrate
=
365 rtd
->cpu_dai
->dai_runtime
.pcmrate
=
366 soc_get_rate_format(rate
);
368 rtd
->codec_dai
->dai_runtime
.priv
= codec_dai_mode
->priv
;
369 rtd
->cpu_dai
->dai_runtime
.priv
= cpu_dai_mode
->priv
;
370 rtd
->codec_dai
->dai_runtime
.flags
= codec_dai_mode
->flags
;
371 rtd
->cpu_dai
->dai_runtime
.flags
= cpu_dai_mode
->flags
;
374 dbg("asoc: DAI[%d:%d] Match OK\n", j
, k
);
375 if (rtd
->codec_dai
->dai_runtime
.flags
== SND_SOC_DAI_BFS_DIV
) {
376 codec_bclk
= (rtd
->codec_dai
->dai_runtime
.fs
* params_rate(params
)) /
377 SND_SOC_FSBD_REAL(rtd
->codec_dai
->dai_runtime
.bfs
);
378 dbg("asoc: codec fs %d mclk %d bfs div %d bclk %d\n",
379 rtd
->codec_dai
->dai_runtime
.fs
, mclk
,
380 SND_SOC_FSBD_REAL(rtd
->codec_dai
->dai_runtime
.bfs
), codec_bclk
);
382 codec_bclk
= params_rate(params
) *
383 SND_SOC_FSB_REAL(rtd
->codec_dai
->dai_runtime
.bfs
);
384 dbg("asoc: codec fs %d mclk %d bfs mult %d bclk %d\n",
385 rtd
->codec_dai
->dai_runtime
.fs
, mclk
,
386 SND_SOC_FSB_REAL(rtd
->codec_dai
->dai_runtime
.bfs
), codec_bclk
);
388 if (rtd
->cpu_dai
->dai_runtime
.flags
== SND_SOC_DAI_BFS_DIV
) {
389 cpu_bclk
= (rtd
->cpu_dai
->dai_runtime
.fs
* params_rate(params
)) /
390 SND_SOC_FSBD_REAL(rtd
->cpu_dai
->dai_runtime
.bfs
);
391 dbg("asoc: cpu fs %d mclk %d bfs div %d bclk %d\n",
392 rtd
->cpu_dai
->dai_runtime
.fs
, mclk
,
393 SND_SOC_FSBD_REAL(rtd
->cpu_dai
->dai_runtime
.bfs
), cpu_bclk
);
395 cpu_bclk
= params_rate(params
) *
396 SND_SOC_FSB_REAL(rtd
->cpu_dai
->dai_runtime
.bfs
);
397 dbg("asoc: cpu fs %d mclk %d bfs mult %d bclk %d\n",
398 rtd
->cpu_dai
->dai_runtime
.fs
, mclk
,
399 SND_SOC_FSB_REAL(rtd
->cpu_dai
->dai_runtime
.bfs
), cpu_bclk
);
403 * Check we have matching bitclocks. If we don't then it means the
404 * sysclock returned by either the codec or cpu DAI (selected by the
405 * machine sysclock function) is wrong compared with the supported DAI
406 * modes for the codec or cpu DAI.
408 if (cpu_bclk
!= codec_bclk
){
410 "asoc: codec and cpu bitclocks differ, audio may be wrong speed\n"
412 printk(KERN_ERR
"asoc: codec %d != cpu %d\n", codec_bclk
, cpu_bclk
);
415 switch(rtd
->cpu_dai
->dai_runtime
.fmt
& SND_SOC_DAIFMT_CLOCK_MASK
) {
416 case SND_SOC_DAIFMT_CBM_CFM
:
417 dbg("asoc: DAI codec BCLK master, LRC master\n");
419 case SND_SOC_DAIFMT_CBS_CFM
:
420 dbg("asoc: DAI codec BCLK slave, LRC master\n");
422 case SND_SOC_DAIFMT_CBM_CFS
:
423 dbg("asoc: DAI codec BCLK master, LRC slave\n");
425 case SND_SOC_DAIFMT_CBS_CFS
:
426 dbg("asoc: DAI codec BCLK slave, LRC slave\n");
429 dbg("asoc: mode %x, invert %x\n",
430 rtd
->cpu_dai
->dai_runtime
.fmt
& SND_SOC_DAIFMT_FORMAT_MASK
,
431 rtd
->cpu_dai
->dai_runtime
.fmt
& SND_SOC_DAIFMT_INV_MASK
);
432 dbg("asoc: audio rate %d chn %d fmt %x\n", params_rate(params
),
433 params_channels(params
), params_format(params
));
438 static inline u32
get_rates(struct snd_soc_dai_mode
*modes
, int nmodes
)
443 for(i
= 0; i
< nmodes
; i
++)
444 rates
|= modes
[i
].pcmrate
;
449 static inline u64
get_formats(struct snd_soc_dai_mode
*modes
, int nmodes
)
454 for(i
= 0; i
< nmodes
; i
++)
455 formats
|= modes
[i
].pcmfmt
;
461 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
462 * then initialized and any private data can be allocated. This also calls
463 * startup for the cpu DAI, platform, machine and codec DAI.
465 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
467 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
468 struct snd_soc_device
*socdev
= rtd
->socdev
;
469 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
470 struct snd_soc_machine
*machine
= socdev
->machine
;
471 struct snd_soc_platform
*platform
= socdev
->platform
;
472 struct snd_soc_codec_dai
*codec_dai
= rtd
->codec_dai
;
473 struct snd_soc_cpu_dai
*cpu_dai
= rtd
->cpu_dai
;
476 mutex_lock(&pcm_mutex
);
478 /* startup the audio subsystem */
479 if (rtd
->cpu_dai
->ops
.startup
) {
480 ret
= rtd
->cpu_dai
->ops
.startup(substream
);
482 printk(KERN_ERR
"asoc: can't open interface %s\n",
488 if (platform
->pcm_ops
->open
) {
489 ret
= platform
->pcm_ops
->open(substream
);
491 printk(KERN_ERR
"asoc: can't open platform %s\n", platform
->name
);
496 if (machine
->ops
&& machine
->ops
->startup
) {
497 ret
= machine
->ops
->startup(substream
);
499 printk(KERN_ERR
"asoc: %s startup failed\n", machine
->name
);
504 if (rtd
->codec_dai
->ops
.startup
) {
505 ret
= rtd
->codec_dai
->ops
.startup(substream
);
507 printk(KERN_ERR
"asoc: can't open codec %s\n",
508 rtd
->codec_dai
->name
);
513 /* create runtime params from DMA, codec and cpu DAI */
514 if (runtime
->hw
.rates
)
516 get_rates(codec_dai
->caps
.mode
, codec_dai
->caps
.num_modes
) &
517 get_rates(cpu_dai
->caps
.mode
, cpu_dai
->caps
.num_modes
);
520 get_rates(codec_dai
->caps
.mode
, codec_dai
->caps
.num_modes
) &
521 get_rates(cpu_dai
->caps
.mode
, cpu_dai
->caps
.num_modes
);
522 if (runtime
->hw
.formats
)
523 runtime
->hw
.formats
&=
524 get_formats(codec_dai
->caps
.mode
, codec_dai
->caps
.num_modes
) &
525 get_formats(cpu_dai
->caps
.mode
, cpu_dai
->caps
.num_modes
);
527 runtime
->hw
.formats
=
528 get_formats(codec_dai
->caps
.mode
, codec_dai
->caps
.num_modes
) &
529 get_formats(cpu_dai
->caps
.mode
, cpu_dai
->caps
.num_modes
);
531 /* Check that the codec and cpu DAI's are compatible */
532 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
533 runtime
->hw
.rate_min
=
534 max(rtd
->codec_dai
->playback
.rate_min
,
535 rtd
->cpu_dai
->playback
.rate_min
);
536 runtime
->hw
.rate_max
=
537 min(rtd
->codec_dai
->playback
.rate_max
,
538 rtd
->cpu_dai
->playback
.rate_max
);
539 runtime
->hw
.channels_min
=
540 max(rtd
->codec_dai
->playback
.channels_min
,
541 rtd
->cpu_dai
->playback
.channels_min
);
542 runtime
->hw
.channels_max
=
543 min(rtd
->codec_dai
->playback
.channels_max
,
544 rtd
->cpu_dai
->playback
.channels_max
);
546 runtime
->hw
.rate_min
=
547 max(rtd
->codec_dai
->capture
.rate_min
,
548 rtd
->cpu_dai
->capture
.rate_min
);
549 runtime
->hw
.rate_max
=
550 min(rtd
->codec_dai
->capture
.rate_max
,
551 rtd
->cpu_dai
->capture
.rate_max
);
552 runtime
->hw
.channels_min
=
553 max(rtd
->codec_dai
->capture
.channels_min
,
554 rtd
->cpu_dai
->capture
.channels_min
);
555 runtime
->hw
.channels_max
=
556 min(rtd
->codec_dai
->capture
.channels_max
,
557 rtd
->cpu_dai
->capture
.channels_max
);
560 snd_pcm_limit_hw_rates(runtime
);
561 if (!runtime
->hw
.rates
) {
562 printk(KERN_ERR
"asoc: %s <-> %s No matching rates\n",
563 rtd
->codec_dai
->name
, rtd
->cpu_dai
->name
);
566 if (!runtime
->hw
.formats
) {
567 printk(KERN_ERR
"asoc: %s <-> %s No matching formats\n",
568 rtd
->codec_dai
->name
, rtd
->cpu_dai
->name
);
571 if (!runtime
->hw
.channels_min
|| !runtime
->hw
.channels_max
) {
572 printk(KERN_ERR
"asoc: %s <-> %s No matching channels\n",
573 rtd
->codec_dai
->name
, rtd
->cpu_dai
->name
);
577 dbg("asoc: %s <-> %s info:\n", rtd
->codec_dai
->name
, rtd
->cpu_dai
->name
);
578 dbg("asoc: rate mask 0x%x\n", runtime
->hw
.rates
);
579 dbg("asoc: min ch %d max ch %d\n", runtime
->hw
.channels_min
,
580 runtime
->hw
.channels_max
);
581 dbg("asoc: min rate %d max rate %d\n", runtime
->hw
.rate_min
,
582 runtime
->hw
.rate_max
);
585 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
586 rtd
->cpu_dai
->playback
.active
= rtd
->codec_dai
->playback
.active
= 1;
588 rtd
->cpu_dai
->capture
.active
= rtd
->codec_dai
->capture
.active
= 1;
589 rtd
->cpu_dai
->active
= rtd
->codec_dai
->active
= 1;
590 rtd
->cpu_dai
->runtime
= runtime
;
591 socdev
->codec
->active
++;
592 mutex_unlock(&pcm_mutex
);
596 if (machine
->ops
&& machine
->ops
->shutdown
)
597 machine
->ops
->shutdown(substream
);
600 if (platform
->pcm_ops
->close
)
601 platform
->pcm_ops
->close(substream
);
604 if (rtd
->cpu_dai
->ops
.shutdown
)
605 rtd
->cpu_dai
->ops
.shutdown(substream
);
607 mutex_unlock(&pcm_mutex
);
612 * Power down the audio subsytem pmdown_time msecs after close is called.
613 * This is to ensure there are no pops or clicks in between any music tracks
614 * due to DAPM power cycling.
616 static void close_delayed_work(void *data
)
618 struct snd_soc_device
*socdev
= data
;
619 struct snd_soc_codec
*codec
= socdev
->codec
;
620 struct snd_soc_codec_dai
*codec_dai
;
623 mutex_lock(&pcm_mutex
);
624 for(i
= 0; i
< codec
->num_dai
; i
++) {
625 codec_dai
= &codec
->dai
[i
];
627 dbg("pop wq checking: %s status: %s waiting: %s\n",
628 codec_dai
->playback
.stream_name
,
629 codec_dai
->playback
.active
? "active" : "inactive",
630 codec_dai
->pop_wait
? "yes" : "no");
632 /* are we waiting on this codec DAI stream */
633 if (codec_dai
->pop_wait
== 1) {
635 codec_dai
->pop_wait
= 0;
636 snd_soc_dapm_stream_event(codec
, codec_dai
->playback
.stream_name
,
637 SND_SOC_DAPM_STREAM_STOP
);
639 /* power down the codec power domain if no longer active */
640 if (codec
->active
== 0) {
641 dbg("pop wq D3 %s %s\n", codec
->name
,
642 codec_dai
->playback
.stream_name
);
643 if (codec
->dapm_event
)
644 codec
->dapm_event(codec
, SNDRV_CTL_POWER_D3hot
);
648 mutex_unlock(&pcm_mutex
);
652 * Called by ALSA when a PCM substream is closed. Private data can be
653 * freed here. The cpu DAI, codec DAI, machine and platform are also
656 static int soc_codec_close(struct snd_pcm_substream
*substream
)
658 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
659 struct snd_soc_device
*socdev
= rtd
->socdev
;
660 struct snd_soc_machine
*machine
= socdev
->machine
;
661 struct snd_soc_platform
*platform
= socdev
->platform
;
662 struct snd_soc_codec
*codec
= socdev
->codec
;
664 mutex_lock(&pcm_mutex
);
666 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
667 rtd
->cpu_dai
->playback
.active
= rtd
->codec_dai
->playback
.active
= 0;
669 rtd
->cpu_dai
->capture
.active
= rtd
->codec_dai
->capture
.active
= 0;
671 if (rtd
->codec_dai
->playback
.active
== 0 &&
672 rtd
->codec_dai
->capture
.active
== 0) {
673 rtd
->cpu_dai
->active
= rtd
->codec_dai
->active
= 0;
677 if (rtd
->cpu_dai
->ops
.shutdown
)
678 rtd
->cpu_dai
->ops
.shutdown(substream
);
680 if (rtd
->codec_dai
->ops
.shutdown
)
681 rtd
->codec_dai
->ops
.shutdown(substream
);
683 if (machine
->ops
&& machine
->ops
->shutdown
)
684 machine
->ops
->shutdown(substream
);
686 if (platform
->pcm_ops
->close
)
687 platform
->pcm_ops
->close(substream
);
688 rtd
->cpu_dai
->runtime
= NULL
;
690 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
691 /* start delayed pop wq here for playback streams */
692 rtd
->codec_dai
->pop_wait
= 1;
693 queue_delayed_work(soc_workq
, &soc_stream_work
,
694 msecs_to_jiffies(pmdown_time
));
696 /* capture streams can be powered down now */
697 snd_soc_dapm_stream_event(codec
, rtd
->codec_dai
->capture
.stream_name
,
698 SND_SOC_DAPM_STREAM_STOP
);
700 if (codec
->active
== 0 && rtd
->codec_dai
->pop_wait
== 0){
701 if (codec
->dapm_event
)
702 codec
->dapm_event(codec
, SNDRV_CTL_POWER_D3hot
);
706 mutex_unlock(&pcm_mutex
);
711 * Called by ALSA when the PCM substream is prepared, can set format, sample
712 * rate, etc. This function is non atomic and can be called multiple times,
713 * it can refer to the runtime info.
715 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
717 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
718 struct snd_soc_device
*socdev
= rtd
->socdev
;
719 struct snd_soc_platform
*platform
= socdev
->platform
;
720 struct snd_soc_codec
*codec
= socdev
->codec
;
723 mutex_lock(&pcm_mutex
);
724 if (platform
->pcm_ops
->prepare
) {
725 ret
= platform
->pcm_ops
->prepare(substream
);
730 if (rtd
->codec_dai
->ops
.prepare
) {
731 ret
= rtd
->codec_dai
->ops
.prepare(substream
);
736 if (rtd
->cpu_dai
->ops
.prepare
)
737 ret
= rtd
->cpu_dai
->ops
.prepare(substream
);
739 /* we only want to start a DAPM playback stream if we are not waiting
740 * on an existing one stopping */
741 if (rtd
->codec_dai
->pop_wait
) {
742 /* we are waiting for the delayed work to start */
743 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
744 snd_soc_dapm_stream_event(codec
,
745 rtd
->codec_dai
->capture
.stream_name
,
746 SND_SOC_DAPM_STREAM_START
);
748 rtd
->codec_dai
->pop_wait
= 0;
749 cancel_delayed_work(&soc_stream_work
);
750 if (rtd
->codec_dai
->digital_mute
)
751 rtd
->codec_dai
->digital_mute(codec
, rtd
->codec_dai
, 0);
754 /* no delayed work - do we need to power up codec */
755 if (codec
->dapm_state
!= SNDRV_CTL_POWER_D0
) {
757 if (codec
->dapm_event
)
758 codec
->dapm_event(codec
, SNDRV_CTL_POWER_D1
);
760 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
761 snd_soc_dapm_stream_event(codec
,
762 rtd
->codec_dai
->playback
.stream_name
,
763 SND_SOC_DAPM_STREAM_START
);
765 snd_soc_dapm_stream_event(codec
,
766 rtd
->codec_dai
->capture
.stream_name
,
767 SND_SOC_DAPM_STREAM_START
);
769 if (codec
->dapm_event
)
770 codec
->dapm_event(codec
, SNDRV_CTL_POWER_D0
);
771 if (rtd
->codec_dai
->digital_mute
)
772 rtd
->codec_dai
->digital_mute(codec
, rtd
->codec_dai
, 0);
775 /* codec already powered - power on widgets */
776 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
777 snd_soc_dapm_stream_event(codec
,
778 rtd
->codec_dai
->playback
.stream_name
,
779 SND_SOC_DAPM_STREAM_START
);
781 snd_soc_dapm_stream_event(codec
,
782 rtd
->codec_dai
->capture
.stream_name
,
783 SND_SOC_DAPM_STREAM_START
);
784 if (rtd
->codec_dai
->digital_mute
)
785 rtd
->codec_dai
->digital_mute(codec
, rtd
->codec_dai
, 0);
790 mutex_unlock(&pcm_mutex
);
795 * Called by ALSA when the hardware params are set by application. This
796 * function can also be called multiple times and can allocate buffers
797 * (using snd_pcm_lib_* ). It's non-atomic.
799 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
800 struct snd_pcm_hw_params
*params
)
802 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
803 struct snd_soc_device
*socdev
= rtd
->socdev
;
804 struct snd_soc_platform
*platform
= socdev
->platform
;
805 struct snd_soc_machine
*machine
= socdev
->machine
;
808 mutex_lock(&pcm_mutex
);
810 /* we don't need to match any AC97 params */
811 if (rtd
->cpu_dai
->type
!= SND_SOC_DAI_AC97
) {
812 ret
= soc_hw_match_params(substream
, params
);
816 struct snd_soc_clock_info clk_info
;
817 clk_info
.rate
= params_rate(params
);
818 ret
= soc_get_mclk(rtd
, &clk_info
);
823 if (rtd
->codec_dai
->ops
.hw_params
) {
824 ret
= rtd
->codec_dai
->ops
.hw_params(substream
, params
);
826 printk(KERN_ERR
"asoc: can't set codec %s hw params\n",
827 rtd
->codec_dai
->name
);
832 if (rtd
->cpu_dai
->ops
.hw_params
) {
833 ret
= rtd
->cpu_dai
->ops
.hw_params(substream
, params
);
835 printk(KERN_ERR
"asoc: can't set interface %s hw params\n",
841 if (platform
->pcm_ops
->hw_params
) {
842 ret
= platform
->pcm_ops
->hw_params(substream
, params
);
844 printk(KERN_ERR
"asoc: can't set platform %s hw params\n",
850 if (machine
->ops
&& machine
->ops
->hw_params
) {
851 ret
= machine
->ops
->hw_params(substream
, params
);
853 printk(KERN_ERR
"asoc: machine hw_params failed\n");
859 mutex_unlock(&pcm_mutex
);
863 if (platform
->pcm_ops
->hw_free
)
864 platform
->pcm_ops
->hw_free(substream
);
867 if (rtd
->cpu_dai
->ops
.hw_free
)
868 rtd
->cpu_dai
->ops
.hw_free(substream
);
871 if (rtd
->codec_dai
->ops
.hw_free
)
872 rtd
->codec_dai
->ops
.hw_free(substream
);
874 mutex_unlock(&pcm_mutex
);
879 * Free's resources allocated by hw_params, can be called multiple times
881 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
883 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
884 struct snd_soc_device
*socdev
= rtd
->socdev
;
885 struct snd_soc_platform
*platform
= socdev
->platform
;
886 struct snd_soc_codec
*codec
= socdev
->codec
;
887 struct snd_soc_machine
*machine
= socdev
->machine
;
889 mutex_lock(&pcm_mutex
);
891 /* apply codec digital mute */
892 if (!codec
->active
&& rtd
->codec_dai
->digital_mute
)
893 rtd
->codec_dai
->digital_mute(codec
, rtd
->codec_dai
, 1);
895 /* free any machine hw params */
896 if (machine
->ops
&& machine
->ops
->hw_free
)
897 machine
->ops
->hw_free(substream
);
899 /* free any DMA resources */
900 if (platform
->pcm_ops
->hw_free
)
901 platform
->pcm_ops
->hw_free(substream
);
903 /* now free hw params for the DAI's */
904 if (rtd
->codec_dai
->ops
.hw_free
)
905 rtd
->codec_dai
->ops
.hw_free(substream
);
907 if (rtd
->cpu_dai
->ops
.hw_free
)
908 rtd
->cpu_dai
->ops
.hw_free(substream
);
910 mutex_unlock(&pcm_mutex
);
914 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
916 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
917 struct snd_soc_device
*socdev
= rtd
->socdev
;
918 struct snd_soc_platform
*platform
= socdev
->platform
;
921 if (rtd
->codec_dai
->ops
.trigger
) {
922 ret
= rtd
->codec_dai
->ops
.trigger(substream
, cmd
);
927 if (platform
->pcm_ops
->trigger
) {
928 ret
= platform
->pcm_ops
->trigger(substream
, cmd
);
933 if (rtd
->cpu_dai
->ops
.trigger
) {
934 ret
= rtd
->cpu_dai
->ops
.trigger(substream
, cmd
);
941 /* ASoC PCM operations */
942 static struct snd_pcm_ops soc_pcm_ops
= {
943 .open
= soc_pcm_open
,
944 .close
= soc_codec_close
,
945 .hw_params
= soc_pcm_hw_params
,
946 .hw_free
= soc_pcm_hw_free
,
947 .prepare
= soc_pcm_prepare
,
948 .trigger
= soc_pcm_trigger
,
952 /* powers down audio subsystem for suspend */
953 static int soc_suspend(struct platform_device
*pdev
, pm_message_t state
)
955 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
956 struct snd_soc_machine
*machine
= socdev
->machine
;
957 struct snd_soc_platform
*platform
= socdev
->platform
;
958 struct snd_soc_codec_device
*codec_dev
= socdev
->codec_dev
;
959 struct snd_soc_codec
*codec
= socdev
->codec
;
962 /* mute any active DAC's */
963 for(i
= 0; i
< machine
->num_links
; i
++) {
964 struct snd_soc_codec_dai
*dai
= machine
->dai_link
[i
].codec_dai
;
965 if (dai
->digital_mute
&& dai
->playback
.active
)
966 dai
->digital_mute(codec
, dai
, 1);
969 if (machine
->suspend_pre
)
970 machine
->suspend_pre(pdev
, state
);
972 for(i
= 0; i
< machine
->num_links
; i
++) {
973 struct snd_soc_cpu_dai
*cpu_dai
= machine
->dai_link
[i
].cpu_dai
;
974 if (cpu_dai
->suspend
&& cpu_dai
->type
!= SND_SOC_DAI_AC97
)
975 cpu_dai
->suspend(pdev
, cpu_dai
);
976 if (platform
->suspend
)
977 platform
->suspend(pdev
, cpu_dai
);
980 /* close any waiting streams and save state */
981 flush_workqueue(soc_workq
);
982 codec
->suspend_dapm_state
= codec
->dapm_state
;
984 for(i
= 0; i
< codec
->num_dai
; i
++) {
985 char *stream
= codec
->dai
[i
].playback
.stream_name
;
987 snd_soc_dapm_stream_event(codec
, stream
,
988 SND_SOC_DAPM_STREAM_SUSPEND
);
989 stream
= codec
->dai
[i
].capture
.stream_name
;
991 snd_soc_dapm_stream_event(codec
, stream
,
992 SND_SOC_DAPM_STREAM_SUSPEND
);
995 if (codec_dev
->suspend
)
996 codec_dev
->suspend(pdev
, state
);
998 for(i
= 0; i
< machine
->num_links
; i
++) {
999 struct snd_soc_cpu_dai
*cpu_dai
= machine
->dai_link
[i
].cpu_dai
;
1000 if (cpu_dai
->suspend
&& cpu_dai
->type
== SND_SOC_DAI_AC97
)
1001 cpu_dai
->suspend(pdev
, cpu_dai
);
1004 if (machine
->suspend_post
)
1005 machine
->suspend_post(pdev
, state
);
1010 /* powers up audio subsystem after a suspend */
1011 static int soc_resume(struct platform_device
*pdev
)
1013 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
1014 struct snd_soc_machine
*machine
= socdev
->machine
;
1015 struct snd_soc_platform
*platform
= socdev
->platform
;
1016 struct snd_soc_codec_device
*codec_dev
= socdev
->codec_dev
;
1017 struct snd_soc_codec
*codec
= socdev
->codec
;
1020 if (machine
->resume_pre
)
1021 machine
->resume_pre(pdev
);
1023 for(i
= 0; i
< machine
->num_links
; i
++) {
1024 struct snd_soc_cpu_dai
*cpu_dai
= machine
->dai_link
[i
].cpu_dai
;
1025 if (cpu_dai
->resume
&& cpu_dai
->type
== SND_SOC_DAI_AC97
)
1026 cpu_dai
->resume(pdev
, cpu_dai
);
1029 if (codec_dev
->resume
)
1030 codec_dev
->resume(pdev
);
1032 for(i
= 0; i
< codec
->num_dai
; i
++) {
1033 char* stream
= codec
->dai
[i
].playback
.stream_name
;
1035 snd_soc_dapm_stream_event(codec
, stream
,
1036 SND_SOC_DAPM_STREAM_RESUME
);
1037 stream
= codec
->dai
[i
].capture
.stream_name
;
1039 snd_soc_dapm_stream_event(codec
, stream
,
1040 SND_SOC_DAPM_STREAM_RESUME
);
1043 /* unmute any active DAC's */
1044 for(i
= 0; i
< machine
->num_links
; i
++) {
1045 struct snd_soc_codec_dai
*dai
= machine
->dai_link
[i
].codec_dai
;
1046 if (dai
->digital_mute
&& dai
->playback
.active
)
1047 dai
->digital_mute(codec
, dai
, 0);
1050 for(i
= 0; i
< machine
->num_links
; i
++) {
1051 struct snd_soc_cpu_dai
*cpu_dai
= machine
->dai_link
[i
].cpu_dai
;
1052 if (cpu_dai
->resume
&& cpu_dai
->type
!= SND_SOC_DAI_AC97
)
1053 cpu_dai
->resume(pdev
, cpu_dai
);
1054 if (platform
->resume
)
1055 platform
->resume(pdev
, cpu_dai
);
1058 if (machine
->resume_post
)
1059 machine
->resume_post(pdev
);
1065 #define soc_suspend NULL
1066 #define soc_resume NULL
1069 /* probes a new socdev */
1070 static int soc_probe(struct platform_device
*pdev
)
1073 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
1074 struct snd_soc_machine
*machine
= socdev
->machine
;
1075 struct snd_soc_platform
*platform
= socdev
->platform
;
1076 struct snd_soc_codec_device
*codec_dev
= socdev
->codec_dev
;
1078 if (machine
->probe
) {
1079 ret
= machine
->probe(pdev
);
1084 for (i
= 0; i
< machine
->num_links
; i
++) {
1085 struct snd_soc_cpu_dai
*cpu_dai
= machine
->dai_link
[i
].cpu_dai
;
1086 if (cpu_dai
->probe
) {
1087 ret
= cpu_dai
->probe(pdev
);
1093 if (codec_dev
->probe
) {
1094 ret
= codec_dev
->probe(pdev
);
1099 if (platform
->probe
) {
1100 ret
= platform
->probe(pdev
);
1105 /* DAPM stream work */
1106 soc_workq
= create_workqueue("kdapm");
1107 if (soc_workq
== NULL
)
1109 INIT_WORK(&soc_stream_work
, close_delayed_work
, socdev
);
1113 if (platform
->remove
)
1114 platform
->remove(pdev
);
1117 if (codec_dev
->remove
)
1118 codec_dev
->remove(pdev
);
1121 for (i
--; i
> 0; i
--) {
1122 struct snd_soc_cpu_dai
*cpu_dai
= machine
->dai_link
[i
].cpu_dai
;
1123 if (cpu_dai
->remove
)
1124 cpu_dai
->remove(pdev
);
1127 if (machine
->remove
)
1128 machine
->remove(pdev
);
1133 /* removes a socdev */
1134 static int soc_remove(struct platform_device
*pdev
)
1137 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
1138 struct snd_soc_machine
*machine
= socdev
->machine
;
1139 struct snd_soc_platform
*platform
= socdev
->platform
;
1140 struct snd_soc_codec_device
*codec_dev
= socdev
->codec_dev
;
1143 destroy_workqueue(soc_workq
);
1145 if (platform
->remove
)
1146 platform
->remove(pdev
);
1148 if (codec_dev
->remove
)
1149 codec_dev
->remove(pdev
);
1151 for (i
= 0; i
< machine
->num_links
; i
++) {
1152 struct snd_soc_cpu_dai
*cpu_dai
= machine
->dai_link
[i
].cpu_dai
;
1153 if (cpu_dai
->remove
)
1154 cpu_dai
->remove(pdev
);
1157 if (machine
->remove
)
1158 machine
->remove(pdev
);
1163 /* ASoC platform driver */
1164 static struct platform_driver soc_driver
= {
1166 .name
= "soc-audio",
1169 .remove
= soc_remove
,
1170 .suspend
= soc_suspend
,
1171 .resume
= soc_resume
,
1174 /* create a new pcm */
1175 static int soc_new_pcm(struct snd_soc_device
*socdev
,
1176 struct snd_soc_dai_link
*dai_link
, int num
)
1178 struct snd_soc_codec
*codec
= socdev
->codec
;
1179 struct snd_soc_codec_dai
*codec_dai
= dai_link
->codec_dai
;
1180 struct snd_soc_cpu_dai
*cpu_dai
= dai_link
->cpu_dai
;
1181 struct snd_soc_pcm_runtime
*rtd
;
1182 struct snd_pcm
*pcm
;
1184 int ret
= 0, playback
= 0, capture
= 0;
1186 rtd
= kzalloc(sizeof(struct snd_soc_pcm_runtime
), GFP_KERNEL
);
1189 rtd
->cpu_dai
= cpu_dai
;
1190 rtd
->codec_dai
= codec_dai
;
1191 rtd
->socdev
= socdev
;
1193 /* check client and interface hw capabilities */
1194 sprintf(new_name
, "%s %s-%s-%d",dai_link
->stream_name
, codec_dai
->name
,
1195 get_dai_name(cpu_dai
->type
), num
);
1197 if (codec_dai
->playback
.channels_min
)
1199 if (codec_dai
->capture
.channels_min
)
1202 ret
= snd_pcm_new(codec
->card
, new_name
, codec
->pcm_devs
++, playback
,
1205 printk(KERN_ERR
"asoc: can't create pcm for codec %s\n", codec
->name
);
1210 pcm
->private_data
= rtd
;
1211 soc_pcm_ops
.mmap
= socdev
->platform
->pcm_ops
->mmap
;
1212 soc_pcm_ops
.pointer
= socdev
->platform
->pcm_ops
->pointer
;
1213 soc_pcm_ops
.ioctl
= socdev
->platform
->pcm_ops
->ioctl
;
1214 soc_pcm_ops
.copy
= socdev
->platform
->pcm_ops
->copy
;
1215 soc_pcm_ops
.silence
= socdev
->platform
->pcm_ops
->silence
;
1216 soc_pcm_ops
.ack
= socdev
->platform
->pcm_ops
->ack
;
1217 soc_pcm_ops
.page
= socdev
->platform
->pcm_ops
->page
;
1220 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &soc_pcm_ops
);
1223 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &soc_pcm_ops
);
1225 ret
= socdev
->platform
->pcm_new(codec
->card
, codec_dai
, pcm
);
1227 printk(KERN_ERR
"asoc: platform pcm constructor failed\n");
1232 pcm
->private_free
= socdev
->platform
->pcm_free
;
1233 printk(KERN_INFO
"asoc: %s <-> %s mapping ok\n", codec_dai
->name
,
1238 /* codec register dump */
1239 static ssize_t
codec_reg_show(struct device
*dev
,
1240 struct device_attribute
*attr
, char *buf
)
1242 struct snd_soc_device
*devdata
= dev_get_drvdata(dev
);
1243 struct snd_soc_codec
*codec
= devdata
->codec
;
1244 int i
, step
= 1, count
= 0;
1246 if (!codec
->reg_cache_size
)
1249 if (codec
->reg_cache_step
)
1250 step
= codec
->reg_cache_step
;
1252 count
+= sprintf(buf
, "%s registers\n", codec
->name
);
1253 for(i
= 0; i
< codec
->reg_cache_size
; i
+= step
)
1254 count
+= sprintf(buf
+ count
, "%2x: %4x\n", i
, codec
->read(codec
, i
));
1258 static DEVICE_ATTR(codec_reg
, 0444, codec_reg_show
, NULL
);
1261 * snd_soc_new_ac97_codec - initailise AC97 device
1262 * @codec: audio codec
1263 * @ops: AC97 bus operations
1264 * @num: AC97 codec number
1266 * Initialises AC97 codec resources for use by ad-hoc devices only.
1268 int snd_soc_new_ac97_codec(struct snd_soc_codec
*codec
,
1269 struct snd_ac97_bus_ops
*ops
, int num
)
1271 mutex_lock(&codec
->mutex
);
1273 codec
->ac97
= kzalloc(sizeof(struct snd_ac97
), GFP_KERNEL
);
1274 if (codec
->ac97
== NULL
) {
1275 mutex_unlock(&codec
->mutex
);
1279 codec
->ac97
->bus
= kzalloc(sizeof(struct snd_ac97_bus
), GFP_KERNEL
);
1280 if (codec
->ac97
->bus
== NULL
) {
1283 mutex_unlock(&codec
->mutex
);
1287 codec
->ac97
->bus
->ops
= ops
;
1288 codec
->ac97
->num
= num
;
1289 mutex_unlock(&codec
->mutex
);
1292 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec
);
1295 * snd_soc_free_ac97_codec - free AC97 codec device
1296 * @codec: audio codec
1298 * Frees AC97 codec device resources.
1300 void snd_soc_free_ac97_codec(struct snd_soc_codec
*codec
)
1302 mutex_lock(&codec
->mutex
);
1303 kfree(codec
->ac97
->bus
);
1306 mutex_unlock(&codec
->mutex
);
1308 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec
);
1311 * snd_soc_update_bits - update codec register bits
1312 * @codec: audio codec
1313 * @reg: codec register
1314 * @mask: register mask
1317 * Writes new register value.
1319 * Returns 1 for change else 0.
1321 int snd_soc_update_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1322 unsigned short mask
, unsigned short value
)
1325 unsigned short old
, new;
1327 mutex_lock(&io_mutex
);
1328 old
= snd_soc_read(codec
, reg
);
1329 new = (old
& ~mask
) | value
;
1330 change
= old
!= new;
1332 snd_soc_write(codec
, reg
, new);
1334 mutex_unlock(&io_mutex
);
1337 EXPORT_SYMBOL_GPL(snd_soc_update_bits
);
1340 * snd_soc_test_bits - test register for change
1341 * @codec: audio codec
1342 * @reg: codec register
1343 * @mask: register mask
1346 * Tests a register with a new value and checks if the new value is
1347 * different from the old value.
1349 * Returns 1 for change else 0.
1351 int snd_soc_test_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1352 unsigned short mask
, unsigned short value
)
1355 unsigned short old
, new;
1357 mutex_lock(&io_mutex
);
1358 old
= snd_soc_read(codec
, reg
);
1359 new = (old
& ~mask
) | value
;
1360 change
= old
!= new;
1361 mutex_unlock(&io_mutex
);
1365 EXPORT_SYMBOL_GPL(snd_soc_test_bits
);
1368 * snd_soc_get_rate - get int sample rate
1369 * @hwpcmrate: the hardware pcm rate
1371 * Returns the audio rate integaer value, else 0.
1373 int snd_soc_get_rate(int hwpcmrate
)
1375 int rate
= ffs(hwpcmrate
) - 1;
1377 if (rate
> ARRAY_SIZE(rates
))
1381 EXPORT_SYMBOL_GPL(snd_soc_get_rate
);
1384 * snd_soc_new_pcms - create new sound card and pcms
1385 * @socdev: the SoC audio device
1387 * Create a new sound card based upon the codec and interface pcms.
1389 * Returns 0 for success, else error.
1391 int snd_soc_new_pcms(struct snd_soc_device
*socdev
, int idx
, const char * xid
)
1393 struct snd_soc_codec
*codec
= socdev
->codec
;
1394 struct snd_soc_machine
*machine
= socdev
->machine
;
1397 mutex_lock(&codec
->mutex
);
1399 /* register a sound card */
1400 codec
->card
= snd_card_new(idx
, xid
, codec
->owner
, 0);
1402 printk(KERN_ERR
"asoc: can't create sound card for codec %s\n",
1404 mutex_unlock(&codec
->mutex
);
1408 codec
->card
->dev
= socdev
->dev
;
1409 codec
->card
->private_data
= codec
;
1410 strncpy(codec
->card
->driver
, codec
->name
, sizeof(codec
->card
->driver
));
1412 /* create the pcms */
1413 for(i
= 0; i
< machine
->num_links
; i
++) {
1414 ret
= soc_new_pcm(socdev
, &machine
->dai_link
[i
], i
);
1416 printk(KERN_ERR
"asoc: can't create pcm %s\n",
1417 machine
->dai_link
[i
].stream_name
);
1418 mutex_unlock(&codec
->mutex
);
1423 mutex_unlock(&codec
->mutex
);
1426 EXPORT_SYMBOL_GPL(snd_soc_new_pcms
);
1429 * snd_soc_register_card - register sound card
1430 * @socdev: the SoC audio device
1432 * Register a SoC sound card. Also registers an AC97 device if the
1433 * codec is AC97 for ad hoc devices.
1435 * Returns 0 for success, else error.
1437 int snd_soc_register_card(struct snd_soc_device
*socdev
)
1439 struct snd_soc_codec
*codec
= socdev
->codec
;
1440 struct snd_soc_machine
*machine
= socdev
->machine
;
1441 int ret
= 0, i
, ac97
= 0, err
= 0;
1443 mutex_lock(&codec
->mutex
);
1444 for(i
= 0; i
< machine
->num_links
; i
++) {
1445 if (socdev
->machine
->dai_link
[i
].init
) {
1446 err
= socdev
->machine
->dai_link
[i
].init(codec
);
1448 printk(KERN_ERR
"asoc: failed to init %s\n",
1449 socdev
->machine
->dai_link
[i
].stream_name
);
1453 if (socdev
->machine
->dai_link
[i
].cpu_dai
->type
== SND_SOC_DAI_AC97
)
1456 snprintf(codec
->card
->shortname
, sizeof(codec
->card
->shortname
),
1457 "%s", machine
->name
);
1458 snprintf(codec
->card
->longname
, sizeof(codec
->card
->longname
),
1459 "%s (%s)", machine
->name
, codec
->name
);
1461 ret
= snd_card_register(codec
->card
);
1463 printk(KERN_ERR
"asoc: failed to register soundcard for codec %s\n",
1468 #ifdef CONFIG_SND_SOC_AC97_BUS
1470 ret
= soc_ac97_dev_register(codec
);
1472 printk(KERN_ERR
"asoc: AC97 device register failed\n");
1473 snd_card_free(codec
->card
);
1479 err
= snd_soc_dapm_sys_add(socdev
->dev
);
1481 printk(KERN_WARNING
"asoc: failed to add dapm sysfs entries\n");
1483 err
= device_create_file(socdev
->dev
, &dev_attr_codec_reg
);
1485 printk(KERN_WARNING
"asoc: failed to add codec sysfs entries\n");
1487 mutex_unlock(&codec
->mutex
);
1490 EXPORT_SYMBOL_GPL(snd_soc_register_card
);
1493 * snd_soc_free_pcms - free sound card and pcms
1494 * @socdev: the SoC audio device
1496 * Frees sound card and pcms associated with the socdev.
1497 * Also unregister the codec if it is an AC97 device.
1499 void snd_soc_free_pcms(struct snd_soc_device
*socdev
)
1501 struct snd_soc_codec
*codec
= socdev
->codec
;
1503 mutex_lock(&codec
->mutex
);
1504 #ifdef CONFIG_SND_SOC_AC97_BUS
1506 soc_ac97_dev_unregister(codec
);
1510 snd_card_free(codec
->card
);
1511 device_remove_file(socdev
->dev
, &dev_attr_codec_reg
);
1512 mutex_unlock(&codec
->mutex
);
1514 EXPORT_SYMBOL_GPL(snd_soc_free_pcms
);
1517 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1518 * @substream: the pcm substream
1519 * @hw: the hardware parameters
1521 * Sets the substream runtime hardware parameters.
1523 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream
*substream
,
1524 const struct snd_pcm_hardware
*hw
)
1526 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1527 runtime
->hw
.info
= hw
->info
;
1528 runtime
->hw
.formats
= hw
->formats
;
1529 runtime
->hw
.period_bytes_min
= hw
->period_bytes_min
;
1530 runtime
->hw
.period_bytes_max
= hw
->period_bytes_max
;
1531 runtime
->hw
.periods_min
= hw
->periods_min
;
1532 runtime
->hw
.periods_max
= hw
->periods_max
;
1533 runtime
->hw
.buffer_bytes_max
= hw
->buffer_bytes_max
;
1534 runtime
->hw
.fifo_size
= hw
->fifo_size
;
1537 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams
);
1540 * snd_soc_cnew - create new control
1541 * @_template: control template
1542 * @data: control private data
1543 * @lnng_name: control long name
1545 * Create a new mixer control from a template control.
1547 * Returns 0 for success, else error.
1549 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
1550 void *data
, char *long_name
)
1552 struct snd_kcontrol_new
template;
1554 memcpy(&template, _template
, sizeof(template));
1556 template.name
= long_name
;
1557 template.access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
;
1560 return snd_ctl_new1(&template, data
);
1562 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
1565 * snd_soc_info_enum_double - enumerated double mixer info callback
1566 * @kcontrol: mixer control
1567 * @uinfo: control element information
1569 * Callback to provide information about a double enumerated
1572 * Returns 0 for success.
1574 int snd_soc_info_enum_double(struct snd_kcontrol
*kcontrol
,
1575 struct snd_ctl_elem_info
*uinfo
)
1577 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1579 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1580 uinfo
->count
= e
->shift_l
== e
->shift_r
? 1 : 2;
1581 uinfo
->value
.enumerated
.items
= e
->mask
;
1583 if (uinfo
->value
.enumerated
.item
> e
->mask
- 1)
1584 uinfo
->value
.enumerated
.item
= e
->mask
- 1;
1585 strcpy(uinfo
->value
.enumerated
.name
,
1586 e
->texts
[uinfo
->value
.enumerated
.item
]);
1589 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double
);
1592 * snd_soc_get_enum_double - enumerated double mixer get callback
1593 * @kcontrol: mixer control
1594 * @uinfo: control element information
1596 * Callback to get the value of a double enumerated mixer.
1598 * Returns 0 for success.
1600 int snd_soc_get_enum_double(struct snd_kcontrol
*kcontrol
,
1601 struct snd_ctl_elem_value
*ucontrol
)
1603 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1604 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1605 unsigned short val
, bitmask
;
1607 for (bitmask
= 1; bitmask
< e
->mask
; bitmask
<<= 1)
1609 val
= snd_soc_read(codec
, e
->reg
);
1610 ucontrol
->value
.enumerated
.item
[0] = (val
>> e
->shift_l
) & (bitmask
- 1);
1611 if (e
->shift_l
!= e
->shift_r
)
1612 ucontrol
->value
.enumerated
.item
[1] =
1613 (val
>> e
->shift_r
) & (bitmask
- 1);
1617 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double
);
1620 * snd_soc_put_enum_double - enumerated double mixer put callback
1621 * @kcontrol: mixer control
1622 * @uinfo: control element information
1624 * Callback to set the value of a double enumerated mixer.
1626 * Returns 0 for success.
1628 int snd_soc_put_enum_double(struct snd_kcontrol
*kcontrol
,
1629 struct snd_ctl_elem_value
*ucontrol
)
1631 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1632 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1634 unsigned short mask
, bitmask
;
1636 for (bitmask
= 1; bitmask
< e
->mask
; bitmask
<<= 1)
1638 if (ucontrol
->value
.enumerated
.item
[0] > e
->mask
- 1)
1640 val
= ucontrol
->value
.enumerated
.item
[0] << e
->shift_l
;
1641 mask
= (bitmask
- 1) << e
->shift_l
;
1642 if (e
->shift_l
!= e
->shift_r
) {
1643 if (ucontrol
->value
.enumerated
.item
[1] > e
->mask
- 1)
1645 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1646 mask
|= (bitmask
- 1) << e
->shift_r
;
1649 return snd_soc_update_bits(codec
, e
->reg
, mask
, val
);
1651 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double
);
1654 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1655 * @kcontrol: mixer control
1656 * @uinfo: control element information
1658 * Callback to provide information about an external enumerated
1661 * Returns 0 for success.
1663 int snd_soc_info_enum_ext(struct snd_kcontrol
*kcontrol
,
1664 struct snd_ctl_elem_info
*uinfo
)
1666 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1668 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1670 uinfo
->value
.enumerated
.items
= e
->mask
;
1672 if (uinfo
->value
.enumerated
.item
> e
->mask
- 1)
1673 uinfo
->value
.enumerated
.item
= e
->mask
- 1;
1674 strcpy(uinfo
->value
.enumerated
.name
,
1675 e
->texts
[uinfo
->value
.enumerated
.item
]);
1678 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext
);
1681 * snd_soc_info_volsw_ext - external single mixer info callback
1682 * @kcontrol: mixer control
1683 * @uinfo: control element information
1685 * Callback to provide information about a single external mixer control.
1687 * Returns 0 for success.
1689 int snd_soc_info_volsw_ext(struct snd_kcontrol
*kcontrol
,
1690 struct snd_ctl_elem_info
*uinfo
)
1692 int mask
= kcontrol
->private_value
;
1695 mask
== 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN
: SNDRV_CTL_ELEM_TYPE_INTEGER
;
1697 uinfo
->value
.integer
.min
= 0;
1698 uinfo
->value
.integer
.max
= mask
;
1701 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext
);
1704 * snd_soc_info_bool_ext - external single boolean mixer info callback
1705 * @kcontrol: mixer control
1706 * @uinfo: control element information
1708 * Callback to provide information about a single boolean external mixer control.
1710 * Returns 0 for success.
1712 int snd_soc_info_bool_ext(struct snd_kcontrol
*kcontrol
,
1713 struct snd_ctl_elem_info
*uinfo
)
1715 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1717 uinfo
->value
.integer
.min
= 0;
1718 uinfo
->value
.integer
.max
= 1;
1721 EXPORT_SYMBOL_GPL(snd_soc_info_bool_ext
);
1724 * snd_soc_info_volsw - single mixer info callback
1725 * @kcontrol: mixer control
1726 * @uinfo: control element information
1728 * Callback to provide information about a single mixer control.
1730 * Returns 0 for success.
1732 int snd_soc_info_volsw(struct snd_kcontrol
*kcontrol
,
1733 struct snd_ctl_elem_info
*uinfo
)
1735 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
1736 int shift
= (kcontrol
->private_value
>> 8) & 0x0f;
1737 int rshift
= (kcontrol
->private_value
>> 12) & 0x0f;
1740 mask
== 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN
: SNDRV_CTL_ELEM_TYPE_INTEGER
;
1741 uinfo
->count
= shift
== rshift
? 1 : 2;
1742 uinfo
->value
.integer
.min
= 0;
1743 uinfo
->value
.integer
.max
= mask
;
1746 EXPORT_SYMBOL_GPL(snd_soc_info_volsw
);
1749 * snd_soc_get_volsw - single mixer get callback
1750 * @kcontrol: mixer control
1751 * @uinfo: control element information
1753 * Callback to get the value of a single mixer control.
1755 * Returns 0 for success.
1757 int snd_soc_get_volsw(struct snd_kcontrol
*kcontrol
,
1758 struct snd_ctl_elem_value
*ucontrol
)
1760 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1761 int reg
= kcontrol
->private_value
& 0xff;
1762 int shift
= (kcontrol
->private_value
>> 8) & 0x0f;
1763 int rshift
= (kcontrol
->private_value
>> 12) & 0x0f;
1764 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
1765 int invert
= (kcontrol
->private_value
>> 24) & 0x01;
1767 ucontrol
->value
.integer
.value
[0] =
1768 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
1769 if (shift
!= rshift
)
1770 ucontrol
->value
.integer
.value
[1] =
1771 (snd_soc_read(codec
, reg
) >> rshift
) & mask
;
1773 ucontrol
->value
.integer
.value
[0] =
1774 mask
- ucontrol
->value
.integer
.value
[0];
1775 if (shift
!= rshift
)
1776 ucontrol
->value
.integer
.value
[1] =
1777 mask
- ucontrol
->value
.integer
.value
[1];
1782 EXPORT_SYMBOL_GPL(snd_soc_get_volsw
);
1785 * snd_soc_put_volsw - single mixer put callback
1786 * @kcontrol: mixer control
1787 * @uinfo: control element information
1789 * Callback to set the value of a single mixer control.
1791 * Returns 0 for success.
1793 int snd_soc_put_volsw(struct snd_kcontrol
*kcontrol
,
1794 struct snd_ctl_elem_value
*ucontrol
)
1796 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1797 int reg
= kcontrol
->private_value
& 0xff;
1798 int shift
= (kcontrol
->private_value
>> 8) & 0x0f;
1799 int rshift
= (kcontrol
->private_value
>> 12) & 0x0f;
1800 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
1801 int invert
= (kcontrol
->private_value
>> 24) & 0x01;
1803 unsigned short val
, val2
, val_mask
;
1805 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1808 val_mask
= mask
<< shift
;
1810 if (shift
!= rshift
) {
1811 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1814 val_mask
|= mask
<< rshift
;
1815 val
|= val2
<< rshift
;
1817 err
= snd_soc_update_bits(codec
, reg
, val_mask
, val
);
1820 EXPORT_SYMBOL_GPL(snd_soc_put_volsw
);
1823 * snd_soc_info_volsw_2r - double mixer info callback
1824 * @kcontrol: mixer control
1825 * @uinfo: control element information
1827 * Callback to provide information about a double mixer control that
1828 * spans 2 codec registers.
1830 * Returns 0 for success.
1832 int snd_soc_info_volsw_2r(struct snd_kcontrol
*kcontrol
,
1833 struct snd_ctl_elem_info
*uinfo
)
1835 int mask
= (kcontrol
->private_value
>> 12) & 0xff;
1838 mask
== 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN
: SNDRV_CTL_ELEM_TYPE_INTEGER
;
1840 uinfo
->value
.integer
.min
= 0;
1841 uinfo
->value
.integer
.max
= mask
;
1844 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r
);
1847 * snd_soc_get_volsw_2r - double mixer get callback
1848 * @kcontrol: mixer control
1849 * @uinfo: control element information
1851 * Callback to get the value of a double mixer control that spans 2 registers.
1853 * Returns 0 for success.
1855 int snd_soc_get_volsw_2r(struct snd_kcontrol
*kcontrol
,
1856 struct snd_ctl_elem_value
*ucontrol
)
1858 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1859 int reg
= kcontrol
->private_value
& 0xff;
1860 int reg2
= (kcontrol
->private_value
>> 24) & 0xff;
1861 int shift
= (kcontrol
->private_value
>> 8) & 0x0f;
1862 int mask
= (kcontrol
->private_value
>> 12) & 0xff;
1863 int invert
= (kcontrol
->private_value
>> 20) & 0x01;
1865 ucontrol
->value
.integer
.value
[0] =
1866 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
1867 ucontrol
->value
.integer
.value
[1] =
1868 (snd_soc_read(codec
, reg2
) >> shift
) & mask
;
1870 ucontrol
->value
.integer
.value
[0] =
1871 mask
- ucontrol
->value
.integer
.value
[0];
1872 ucontrol
->value
.integer
.value
[1] =
1873 mask
- ucontrol
->value
.integer
.value
[1];
1878 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r
);
1881 * snd_soc_put_volsw_2r - double mixer set callback
1882 * @kcontrol: mixer control
1883 * @uinfo: control element information
1885 * Callback to set the value of a double mixer control that spans 2 registers.
1887 * Returns 0 for success.
1889 int snd_soc_put_volsw_2r(struct snd_kcontrol
*kcontrol
,
1890 struct snd_ctl_elem_value
*ucontrol
)
1892 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1893 int reg
= kcontrol
->private_value
& 0xff;
1894 int reg2
= (kcontrol
->private_value
>> 24) & 0xff;
1895 int shift
= (kcontrol
->private_value
>> 8) & 0x0f;
1896 int mask
= (kcontrol
->private_value
>> 12) & 0xff;
1897 int invert
= (kcontrol
->private_value
>> 20) & 0x01;
1899 unsigned short val
, val2
, val_mask
;
1901 val_mask
= mask
<< shift
;
1902 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1903 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1911 val2
= val2
<< shift
;
1913 if ((err
= snd_soc_update_bits(codec
, reg
, val_mask
, val
)) < 0)
1916 err
= snd_soc_update_bits(codec
, reg2
, val_mask
, val2
);
1919 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r
);
1921 static int __devinit
snd_soc_init(void)
1923 printk(KERN_INFO
"ASoC version %s\n", SND_SOC_VERSION
);
1924 return platform_driver_register(&soc_driver
);
1927 static void snd_soc_exit(void)
1929 platform_driver_unregister(&soc_driver
);
1932 module_init(snd_soc_init
);
1933 module_exit(snd_soc_exit
);
1935 /* Module information */
1936 MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1937 MODULE_DESCRIPTION("ALSA SoC Core");
1938 MODULE_LICENSE("GPL");