blob: 4aa8e2d35061403b26b883abc78c70fbda017208 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
6 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +01008 * with code, comments and ideas from :-
9 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020010 *
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 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020016 * 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>
Troy Kisky12ef1932008-10-13 17:42:14 -070029#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020030#include <linux/platform_device.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020031#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
Frank Mandarinodb2a4162006-10-06 18:31:09 +020038static DEFINE_MUTEX(pcm_mutex);
39static DEFINE_MUTEX(io_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
41
Mark Brown384c89e2008-12-03 17:34:03 +000042#ifdef CONFIG_DEBUG_FS
43static struct dentry *debugfs_root;
44#endif
45
Mark Brownc5af3a22008-11-28 13:29:45 +000046static DEFINE_MUTEX(client_mutex);
47static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000048static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000049static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000050static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000051
52static int snd_soc_register_card(struct snd_soc_card *card);
53static int snd_soc_unregister_card(struct snd_soc_card *card);
54
Frank Mandarinodb2a4162006-10-06 18:31:09 +020055/*
56 * This is a timeout to do a DAPM powerdown after a stream is closed().
57 * It can be used to eliminate pops between different playback streams, e.g.
58 * between two audio tracks.
59 */
60static int pmdown_time = 5000;
61module_param(pmdown_time, int, 0);
62MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
63
Liam Girdwood965ac422007-01-31 14:14:57 +010064/*
65 * This function forces any delayed work to be queued and run.
66 */
67static int run_delayed_work(struct delayed_work *dwork)
68{
69 int ret;
70
71 /* cancel any work waiting to be queued. */
72 ret = cancel_delayed_work(dwork);
73
74 /* if there was any work waiting then we run it now and
75 * wait for it's completion */
76 if (ret) {
77 schedule_delayed_work(dwork, 0);
78 flush_scheduled_work();
79 }
80 return ret;
81}
82
Frank Mandarinodb2a4162006-10-06 18:31:09 +020083#ifdef CONFIG_SND_SOC_AC97_BUS
84/* unregister ac97 codec */
85static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
86{
87 if (codec->ac97->dev.bus)
88 device_unregister(&codec->ac97->dev);
89 return 0;
90}
91
92/* stop no dev release warning */
93static void soc_ac97_device_release(struct device *dev){}
94
95/* register ac97 codec to bus */
96static int soc_ac97_dev_register(struct snd_soc_codec *codec)
97{
98 int err;
99
100 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100101 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200102 codec->ac97->dev.release = soc_ac97_device_release;
103
Kay Sieversbb072bf2008-11-02 03:50:35 +0100104 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
105 codec->card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200106 err = device_register(&codec->ac97->dev);
107 if (err < 0) {
108 snd_printk(KERN_ERR "Can't register ac97 bus\n");
109 codec->ac97->dev.bus = NULL;
110 return err;
111 }
112 return 0;
113}
114#endif
115
Mark Brown06f409d2009-04-07 18:10:13 +0100116static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
117{
118 struct snd_soc_pcm_runtime *rtd = substream->private_data;
119 struct snd_soc_device *socdev = rtd->socdev;
120 struct snd_soc_card *card = socdev->card;
121 struct snd_soc_dai_link *machine = rtd->dai;
122 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
123 struct snd_soc_dai *codec_dai = machine->codec_dai;
124 int ret;
125
126 if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates ||
127 machine->symmetric_rates) {
128 dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
129 machine->rate);
130
131 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
132 SNDRV_PCM_HW_PARAM_RATE,
133 machine->rate,
134 machine->rate);
135 if (ret < 0) {
136 dev_err(card->dev,
137 "Unable to apply rate symmetry constraint: %d\n", ret);
138 return ret;
139 }
140 }
141
142 return 0;
143}
144
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200145/*
146 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
147 * then initialized and any private data can be allocated. This also calls
148 * startup for the cpu DAI, platform, machine and codec DAI.
149 */
150static int soc_pcm_open(struct snd_pcm_substream *substream)
151{
152 struct snd_soc_pcm_runtime *rtd = substream->private_data;
153 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000154 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200155 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100156 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000157 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100158 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
159 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200160 int ret = 0;
161
162 mutex_lock(&pcm_mutex);
163
164 /* startup the audio subsystem */
Eric Miao6335d052009-03-03 09:41:00 +0800165 if (cpu_dai->ops->startup) {
166 ret = cpu_dai->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200167 if (ret < 0) {
168 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100169 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200170 goto out;
171 }
172 }
173
174 if (platform->pcm_ops->open) {
175 ret = platform->pcm_ops->open(substream);
176 if (ret < 0) {
177 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
178 goto platform_err;
179 }
180 }
181
Eric Miao6335d052009-03-03 09:41:00 +0800182 if (codec_dai->ops->startup) {
183 ret = codec_dai->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100184 if (ret < 0) {
185 printk(KERN_ERR "asoc: can't open codec %s\n",
186 codec_dai->name);
187 goto codec_dai_err;
188 }
189 }
190
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200191 if (machine->ops && machine->ops->startup) {
192 ret = machine->ops->startup(substream);
193 if (ret < 0) {
194 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
195 goto machine_err;
196 }
197 }
198
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200199 /* Check that the codec and cpu DAI's are compatible */
200 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
201 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200202 max(codec_dai->playback.rate_min,
203 cpu_dai->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200204 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200205 min(codec_dai->playback.rate_max,
206 cpu_dai->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200207 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100208 max(codec_dai->playback.channels_min,
209 cpu_dai->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200210 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100211 min(codec_dai->playback.channels_max,
212 cpu_dai->playback.channels_max);
213 runtime->hw.formats =
214 codec_dai->playback.formats & cpu_dai->playback.formats;
215 runtime->hw.rates =
216 codec_dai->playback.rates & cpu_dai->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200217 } else {
218 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200219 max(codec_dai->capture.rate_min,
220 cpu_dai->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200221 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200222 min(codec_dai->capture.rate_max,
223 cpu_dai->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200224 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100225 max(codec_dai->capture.channels_min,
226 cpu_dai->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200227 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100228 min(codec_dai->capture.channels_max,
229 cpu_dai->capture.channels_max);
230 runtime->hw.formats =
231 codec_dai->capture.formats & cpu_dai->capture.formats;
232 runtime->hw.rates =
233 codec_dai->capture.rates & cpu_dai->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200234 }
235
236 snd_pcm_limit_hw_rates(runtime);
237 if (!runtime->hw.rates) {
238 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100239 codec_dai->name, cpu_dai->name);
240 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200241 }
242 if (!runtime->hw.formats) {
243 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100244 codec_dai->name, cpu_dai->name);
245 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200246 }
247 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
248 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100249 codec_dai->name, cpu_dai->name);
250 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200251 }
252
Mark Brown06f409d2009-04-07 18:10:13 +0100253 /* Symmetry only applies if we've already got an active stream. */
254 if (cpu_dai->active || codec_dai->active) {
255 ret = soc_pcm_apply_symmetry(substream);
256 if (ret != 0)
257 goto machine_err;
258 }
259
Mark Brownf24368c2008-10-21 21:45:08 +0100260 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
261 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
262 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
263 runtime->hw.channels_max);
264 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
265 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200266
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200267 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100268 cpu_dai->playback.active = codec_dai->playback.active = 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200269 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100270 cpu_dai->capture.active = codec_dai->capture.active = 1;
271 cpu_dai->active = codec_dai->active = 1;
272 cpu_dai->runtime = runtime;
Mark Brown6627a652009-01-23 22:55:23 +0000273 card->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200274 mutex_unlock(&pcm_mutex);
275 return 0;
276
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100277machine_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200278 if (machine->ops && machine->ops->shutdown)
279 machine->ops->shutdown(substream);
280
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100281codec_dai_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200282 if (platform->pcm_ops->close)
283 platform->pcm_ops->close(substream);
284
285platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800286 if (cpu_dai->ops->shutdown)
287 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200288out:
289 mutex_unlock(&pcm_mutex);
290 return ret;
291}
292
293/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200294 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200295 * This is to ensure there are no pops or clicks in between any music tracks
296 * due to DAPM power cycling.
297 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100298static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200299{
Mark Brown63084192008-12-02 15:08:03 +0000300 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
301 delayed_work.work);
Mark Brown6627a652009-01-23 22:55:23 +0000302 struct snd_soc_codec *codec = card->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100303 struct snd_soc_dai *codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200304 int i;
305
306 mutex_lock(&pcm_mutex);
Mark Brown3ff3f642008-05-19 12:32:25 +0200307 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200308 codec_dai = &codec->dai[i];
309
Mark Brownf24368c2008-10-21 21:45:08 +0100310 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
311 codec_dai->playback.stream_name,
312 codec_dai->playback.active ? "active" : "inactive",
313 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200314
315 /* are we waiting on this codec DAI stream */
316 if (codec_dai->pop_wait == 1) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200317 codec_dai->pop_wait = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100318 snd_soc_dapm_stream_event(codec,
319 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200320 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200321 }
322 }
323 mutex_unlock(&pcm_mutex);
324}
325
326/*
327 * Called by ALSA when a PCM substream is closed. Private data can be
328 * freed here. The cpu DAI, codec DAI, machine and platform are also
329 * shutdown.
330 */
331static int soc_codec_close(struct snd_pcm_substream *substream)
332{
333 struct snd_soc_pcm_runtime *rtd = substream->private_data;
334 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000335 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100336 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000337 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100338 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
339 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000340 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200341
342 mutex_lock(&pcm_mutex);
343
344 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100345 cpu_dai->playback.active = codec_dai->playback.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200346 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100347 cpu_dai->capture.active = codec_dai->capture.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200348
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100349 if (codec_dai->playback.active == 0 &&
350 codec_dai->capture.active == 0) {
351 cpu_dai->active = codec_dai->active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200352 }
353 codec->active--;
354
Mark Brown6010b2d2008-09-06 18:33:24 +0100355 /* Muting the DAC suppresses artifacts caused during digital
356 * shutdown, for example from stopping clocks.
357 */
358 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
359 snd_soc_dai_digital_mute(codec_dai, 1);
360
Eric Miao6335d052009-03-03 09:41:00 +0800361 if (cpu_dai->ops->shutdown)
362 cpu_dai->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200363
Eric Miao6335d052009-03-03 09:41:00 +0800364 if (codec_dai->ops->shutdown)
365 codec_dai->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200366
367 if (machine->ops && machine->ops->shutdown)
368 machine->ops->shutdown(substream);
369
370 if (platform->pcm_ops->close)
371 platform->pcm_ops->close(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100372 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200373
374 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
375 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100376 codec_dai->pop_wait = 1;
Mark Brown63084192008-12-02 15:08:03 +0000377 schedule_delayed_work(&card->delayed_work,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200378 msecs_to_jiffies(pmdown_time));
379 } else {
380 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100381 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100382 codec_dai->capture.stream_name,
383 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200384 }
385
386 mutex_unlock(&pcm_mutex);
387 return 0;
388}
389
390/*
391 * Called by ALSA when the PCM substream is prepared, can set format, sample
392 * rate, etc. This function is non atomic and can be called multiple times,
393 * it can refer to the runtime info.
394 */
395static int soc_pcm_prepare(struct snd_pcm_substream *substream)
396{
397 struct snd_soc_pcm_runtime *rtd = substream->private_data;
398 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000399 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100400 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000401 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100402 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
403 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000404 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200405 int ret = 0;
406
407 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100408
409 if (machine->ops && machine->ops->prepare) {
410 ret = machine->ops->prepare(substream);
411 if (ret < 0) {
412 printk(KERN_ERR "asoc: machine prepare error\n");
413 goto out;
414 }
415 }
416
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200417 if (platform->pcm_ops->prepare) {
418 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200419 if (ret < 0) {
420 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200421 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200422 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200423 }
424
Eric Miao6335d052009-03-03 09:41:00 +0800425 if (codec_dai->ops->prepare) {
426 ret = codec_dai->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200427 if (ret < 0) {
428 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200429 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200430 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200431 }
432
Eric Miao6335d052009-03-03 09:41:00 +0800433 if (cpu_dai->ops->prepare) {
434 ret = cpu_dai->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100435 if (ret < 0) {
436 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
437 goto out;
438 }
439 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200440
Mark Brownd45f6212008-10-14 13:58:36 +0100441 /* cancel any delayed stream shutdown that is pending */
442 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
443 codec_dai->pop_wait) {
444 codec_dai->pop_wait = 0;
Mark Brown63084192008-12-02 15:08:03 +0000445 cancel_delayed_work(&card->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100446 }
447
Mark Brown452c5ea2009-05-17 21:41:23 +0100448 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
449 snd_soc_dapm_stream_event(codec,
450 codec_dai->playback.stream_name,
451 SND_SOC_DAPM_STREAM_START);
452 else
453 snd_soc_dapm_stream_event(codec,
454 codec_dai->capture.stream_name,
455 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100456
Mark Brown452c5ea2009-05-17 21:41:23 +0100457 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200458
459out:
460 mutex_unlock(&pcm_mutex);
461 return ret;
462}
463
464/*
465 * Called by ALSA when the hardware params are set by application. This
466 * function can also be called multiple times and can allocate buffers
467 * (using snd_pcm_lib_* ). It's non-atomic.
468 */
469static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
470 struct snd_pcm_hw_params *params)
471{
472 struct snd_soc_pcm_runtime *rtd = substream->private_data;
473 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100474 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000475 struct snd_soc_card *card = socdev->card;
476 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100477 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
478 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200479 int ret = 0;
480
481 mutex_lock(&pcm_mutex);
482
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100483 if (machine->ops && machine->ops->hw_params) {
484 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200485 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100486 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200487 goto out;
488 }
489 }
490
Eric Miao6335d052009-03-03 09:41:00 +0800491 if (codec_dai->ops->hw_params) {
492 ret = codec_dai->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100493 if (ret < 0) {
494 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
495 codec_dai->name);
496 goto codec_err;
497 }
498 }
499
Eric Miao6335d052009-03-03 09:41:00 +0800500 if (cpu_dai->ops->hw_params) {
501 ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200502 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200503 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100504 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200505 goto interface_err;
506 }
507 }
508
509 if (platform->pcm_ops->hw_params) {
510 ret = platform->pcm_ops->hw_params(substream, params);
511 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200512 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200513 platform->name);
514 goto platform_err;
515 }
516 }
517
Mark Brown06f409d2009-04-07 18:10:13 +0100518 machine->rate = params_rate(params);
519
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200520out:
521 mutex_unlock(&pcm_mutex);
522 return ret;
523
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200524platform_err:
Eric Miao6335d052009-03-03 09:41:00 +0800525 if (cpu_dai->ops->hw_free)
526 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200527
528interface_err:
Eric Miao6335d052009-03-03 09:41:00 +0800529 if (codec_dai->ops->hw_free)
530 codec_dai->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100531
532codec_err:
Mark Brown3ff3f642008-05-19 12:32:25 +0200533 if (machine->ops && machine->ops->hw_free)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100534 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200535
536 mutex_unlock(&pcm_mutex);
537 return ret;
538}
539
540/*
541 * Free's resources allocated by hw_params, can be called multiple times
542 */
543static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
544{
545 struct snd_soc_pcm_runtime *rtd = substream->private_data;
546 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100547 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000548 struct snd_soc_card *card = socdev->card;
549 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100550 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
551 struct snd_soc_dai *codec_dai = machine->codec_dai;
Mark Brown6627a652009-01-23 22:55:23 +0000552 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200553
554 mutex_lock(&pcm_mutex);
555
556 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100557 if (!codec->active)
558 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200559
560 /* free any machine hw params */
561 if (machine->ops && machine->ops->hw_free)
562 machine->ops->hw_free(substream);
563
564 /* free any DMA resources */
565 if (platform->pcm_ops->hw_free)
566 platform->pcm_ops->hw_free(substream);
567
568 /* now free hw params for the DAI's */
Eric Miao6335d052009-03-03 09:41:00 +0800569 if (codec_dai->ops->hw_free)
570 codec_dai->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200571
Eric Miao6335d052009-03-03 09:41:00 +0800572 if (cpu_dai->ops->hw_free)
573 cpu_dai->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200574
575 mutex_unlock(&pcm_mutex);
576 return 0;
577}
578
579static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
580{
581 struct snd_soc_pcm_runtime *rtd = substream->private_data;
582 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000583 struct snd_soc_card *card= socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100584 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000585 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100586 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
587 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200588 int ret;
589
Eric Miao6335d052009-03-03 09:41:00 +0800590 if (codec_dai->ops->trigger) {
591 ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592 if (ret < 0)
593 return ret;
594 }
595
596 if (platform->pcm_ops->trigger) {
597 ret = platform->pcm_ops->trigger(substream, cmd);
598 if (ret < 0)
599 return ret;
600 }
601
Eric Miao6335d052009-03-03 09:41:00 +0800602 if (cpu_dai->ops->trigger) {
603 ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200604 if (ret < 0)
605 return ret;
606 }
607 return 0;
608}
609
610/* ASoC PCM operations */
611static struct snd_pcm_ops soc_pcm_ops = {
612 .open = soc_pcm_open,
613 .close = soc_codec_close,
614 .hw_params = soc_pcm_hw_params,
615 .hw_free = soc_pcm_hw_free,
616 .prepare = soc_pcm_prepare,
617 .trigger = soc_pcm_trigger,
618};
619
620#ifdef CONFIG_PM
621/* powers down audio subsystem for suspend */
622static int soc_suspend(struct platform_device *pdev, pm_message_t state)
623{
Mark Brown3ff3f642008-05-19 12:32:25 +0200624 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000625 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000626 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200627 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000628 struct snd_soc_codec *codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200629 int i;
630
Andy Green6ed25972008-06-13 16:24:05 +0100631 /* Due to the resume being scheduled into a workqueue we could
632 * suspend before that's finished - wait for it to complete.
633 */
634 snd_power_lock(codec->card);
635 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
636 snd_power_unlock(codec->card);
637
638 /* we're going to block userspace touching us until resume completes */
639 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
640
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200641 /* mute any active DAC's */
Mark Browndee89c42008-11-18 22:11:38 +0000642 for (i = 0; i < card->num_links; i++) {
643 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Eric Miao6335d052009-03-03 09:41:00 +0800644 if (dai->ops->digital_mute && dai->playback.active)
645 dai->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200646 }
647
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100648 /* suspend all pcms */
Mark Brown87506542008-11-18 20:50:34 +0000649 for (i = 0; i < card->num_links; i++)
650 snd_pcm_suspend_all(card->dai_link[i].pcm);
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100651
Mark Brown87506542008-11-18 20:50:34 +0000652 if (card->suspend_pre)
653 card->suspend_pre(pdev, state);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654
Mark Brown87506542008-11-18 20:50:34 +0000655 for (i = 0; i < card->num_links; i++) {
656 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000657 if (cpu_dai->suspend && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000658 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200659 if (platform->suspend)
Mark Brown07c84d02008-12-03 18:17:28 +0000660 platform->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200661 }
662
663 /* close any waiting streams and save state */
Mark Brown63084192008-12-02 15:08:03 +0000664 run_delayed_work(&card->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200665 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200666
Mark Brown3ff3f642008-05-19 12:32:25 +0200667 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200668 char *stream = codec->dai[i].playback.stream_name;
669 if (stream != NULL)
670 snd_soc_dapm_stream_event(codec, stream,
671 SND_SOC_DAPM_STREAM_SUSPEND);
672 stream = codec->dai[i].capture.stream_name;
673 if (stream != NULL)
674 snd_soc_dapm_stream_event(codec, stream,
675 SND_SOC_DAPM_STREAM_SUSPEND);
676 }
677
678 if (codec_dev->suspend)
679 codec_dev->suspend(pdev, state);
680
Mark Brown87506542008-11-18 20:50:34 +0000681 for (i = 0; i < card->num_links; i++) {
682 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000683 if (cpu_dai->suspend && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000684 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200685 }
686
Mark Brown87506542008-11-18 20:50:34 +0000687 if (card->suspend_post)
688 card->suspend_post(pdev, state);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200689
690 return 0;
691}
692
Andy Green6ed25972008-06-13 16:24:05 +0100693/* deferred resume work, so resume can complete before we finished
694 * setting our codec back up, which can be very slow on I2C
695 */
696static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200697{
Mark Brown63084192008-12-02 15:08:03 +0000698 struct snd_soc_card *card = container_of(work,
699 struct snd_soc_card,
700 deferred_resume_work);
701 struct snd_soc_device *socdev = card->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000702 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200703 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Mark Brown6627a652009-01-23 22:55:23 +0000704 struct snd_soc_codec *codec = card->codec;
Andy Green6ed25972008-06-13 16:24:05 +0100705 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200706 int i;
707
Andy Green6ed25972008-06-13 16:24:05 +0100708 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
709 * so userspace apps are blocked from touching us
710 */
711
Mark Brownfde22f22008-11-24 18:08:18 +0000712 dev_dbg(socdev->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100713
Mark Brown87506542008-11-18 20:50:34 +0000714 if (card->resume_pre)
715 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716
Mark Brown87506542008-11-18 20:50:34 +0000717 for (i = 0; i < card->num_links; i++) {
718 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000719 if (cpu_dai->resume && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000720 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200721 }
722
723 if (codec_dev->resume)
724 codec_dev->resume(pdev);
725
Mark Brown3ff3f642008-05-19 12:32:25 +0200726 for (i = 0; i < codec->num_dai; i++) {
727 char *stream = codec->dai[i].playback.stream_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200728 if (stream != NULL)
729 snd_soc_dapm_stream_event(codec, stream,
730 SND_SOC_DAPM_STREAM_RESUME);
731 stream = codec->dai[i].capture.stream_name;
732 if (stream != NULL)
733 snd_soc_dapm_stream_event(codec, stream,
734 SND_SOC_DAPM_STREAM_RESUME);
735 }
736
Mark Brown3ff3f642008-05-19 12:32:25 +0200737 /* unmute any active DACs */
Mark Browndee89c42008-11-18 22:11:38 +0000738 for (i = 0; i < card->num_links; i++) {
739 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
Eric Miao6335d052009-03-03 09:41:00 +0800740 if (dai->ops->digital_mute && dai->playback.active)
741 dai->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200742 }
743
Mark Brown87506542008-11-18 20:50:34 +0000744 for (i = 0; i < card->num_links; i++) {
745 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000746 if (cpu_dai->resume && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000747 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200748 if (platform->resume)
Mark Brown07c84d02008-12-03 18:17:28 +0000749 platform->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750 }
751
Mark Brown87506542008-11-18 20:50:34 +0000752 if (card->resume_post)
753 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200754
Mark Brownfde22f22008-11-24 18:08:18 +0000755 dev_dbg(socdev->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100756
757 /* userspace can access us now we are back as we were before */
758 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
759}
760
761/* powers up audio subsystem after a suspend */
762static int soc_resume(struct platform_device *pdev)
763{
764 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown63084192008-12-02 15:08:03 +0000765 struct snd_soc_card *card = socdev->card;
Mark Brown64ab9ba2009-03-31 11:27:03 +0100766 struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
Andy Green6ed25972008-06-13 16:24:05 +0100767
Mark Brown64ab9ba2009-03-31 11:27:03 +0100768 /* AC97 devices might have other drivers hanging off them so
769 * need to resume immediately. Other drivers don't have that
770 * problem and may take a substantial amount of time to resume
771 * due to I/O costs and anti-pop so handle them out of line.
772 */
773 if (cpu_dai->ac97_control) {
774 dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
775 soc_resume_deferred(&card->deferred_resume_work);
776 } else {
777 dev_dbg(socdev->dev, "Scheduling resume work\n");
778 if (!schedule_work(&card->deferred_resume_work))
779 dev_err(socdev->dev, "resume work item may be lost\n");
780 }
Andy Green6ed25972008-06-13 16:24:05 +0100781
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200782 return 0;
783}
784
785#else
786#define soc_suspend NULL
787#define soc_resume NULL
788#endif
789
Mark Brown435c5e22008-12-04 15:32:53 +0000790static void snd_soc_instantiate_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791{
Mark Brown435c5e22008-12-04 15:32:53 +0000792 struct platform_device *pdev = container_of(card->dev,
793 struct platform_device,
794 dev);
795 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
796 struct snd_soc_platform *platform;
797 struct snd_soc_dai *dai;
Mark Brown6b05eda2008-12-08 19:26:48 +0000798 int i, found, ret, ac97;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200799
Mark Brown435c5e22008-12-04 15:32:53 +0000800 if (card->instantiated)
801 return;
Mark Brown63084192008-12-02 15:08:03 +0000802
Mark Brown435c5e22008-12-04 15:32:53 +0000803 found = 0;
804 list_for_each_entry(platform, &platform_list, list)
805 if (card->platform == platform) {
806 found = 1;
807 break;
808 }
809 if (!found) {
810 dev_dbg(card->dev, "Platform %s not registered\n",
811 card->platform->name);
812 return;
Mark Brownc5af3a22008-11-28 13:29:45 +0000813 }
814
Mark Brown6b05eda2008-12-08 19:26:48 +0000815 ac97 = 0;
Mark Brown435c5e22008-12-04 15:32:53 +0000816 for (i = 0; i < card->num_links; i++) {
817 found = 0;
818 list_for_each_entry(dai, &dai_list, list)
819 if (card->dai_link[i].cpu_dai == dai) {
820 found = 1;
821 break;
822 }
823 if (!found) {
824 dev_dbg(card->dev, "DAI %s not registered\n",
825 card->dai_link[i].cpu_dai->name);
826 return;
827 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000828
829 if (card->dai_link[i].cpu_dai->ac97_control)
830 ac97 = 1;
Mark Brown435c5e22008-12-04 15:32:53 +0000831 }
832
Mark Brown6b05eda2008-12-08 19:26:48 +0000833 /* If we have AC97 in the system then don't wait for the
834 * codec. This will need revisiting if we have to handle
835 * systems with mixed AC97 and non-AC97 parts. Only check for
836 * DAIs currently; we can't do this per link since some AC97
837 * codecs have non-AC97 DAIs.
838 */
839 if (!ac97)
840 for (i = 0; i < card->num_links; i++) {
841 found = 0;
842 list_for_each_entry(dai, &dai_list, list)
843 if (card->dai_link[i].codec_dai == dai) {
844 found = 1;
845 break;
846 }
847 if (!found) {
848 dev_dbg(card->dev, "DAI %s not registered\n",
849 card->dai_link[i].codec_dai->name);
850 return;
851 }
852 }
853
Mark Brown435c5e22008-12-04 15:32:53 +0000854 /* Note that we do not current check for codec components */
855
856 dev_dbg(card->dev, "All components present, instantiating\n");
857
858 /* Found everything, bring it up */
Mark Brown87506542008-11-18 20:50:34 +0000859 if (card->probe) {
860 ret = card->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200861 if (ret < 0)
Mark Brown435c5e22008-12-04 15:32:53 +0000862 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200863 }
864
Mark Brown87506542008-11-18 20:50:34 +0000865 for (i = 0; i < card->num_links; i++) {
866 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200867 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +0100868 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +0200869 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200870 goto cpu_dai_err;
871 }
872 }
873
874 if (codec_dev->probe) {
875 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200876 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200877 goto cpu_dai_err;
878 }
879
880 if (platform->probe) {
881 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200882 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200883 goto platform_err;
884 }
885
886 /* DAPM stream work */
Mark Brown63084192008-12-02 15:08:03 +0000887 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +0100888#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +0100889 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +0000890 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +0100891#endif
Andy Green6ed25972008-06-13 16:24:05 +0100892
Mark Brown435c5e22008-12-04 15:32:53 +0000893 card->instantiated = 1;
894
895 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200896
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200897platform_err:
898 if (codec_dev->remove)
899 codec_dev->remove(pdev);
900
901cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +0100902 for (i--; i >= 0; i--) {
Mark Brown87506542008-11-18 20:50:34 +0000903 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200904 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100905 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200906 }
907
Mark Brown87506542008-11-18 20:50:34 +0000908 if (card->remove)
909 card->remove(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +0000910}
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200911
Mark Brown435c5e22008-12-04 15:32:53 +0000912/*
913 * Attempt to initialise any uninitalised cards. Must be called with
914 * client_mutex.
915 */
916static void snd_soc_instantiate_cards(void)
917{
918 struct snd_soc_card *card;
919 list_for_each_entry(card, &card_list, list)
920 snd_soc_instantiate_card(card);
921}
922
923/* probes a new socdev */
924static int soc_probe(struct platform_device *pdev)
925{
926 int ret = 0;
927 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
928 struct snd_soc_card *card = socdev->card;
929
930 /* Bodge while we push things out of socdev */
931 card->socdev = socdev;
932
933 /* Bodge while we unpick instantiation */
934 card->dev = &pdev->dev;
935 ret = snd_soc_register_card(card);
936 if (ret != 0) {
937 dev_err(&pdev->dev, "Failed to register card\n");
938 return ret;
939 }
940
941 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200942}
943
944/* removes a socdev */
945static int soc_remove(struct platform_device *pdev)
946{
947 int i;
948 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000949 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000950 struct snd_soc_platform *platform = card->platform;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200951 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
952
Mike Rapoport914dc182009-05-11 13:04:55 +0300953 if (!card->instantiated)
954 return 0;
955
Mark Brown63084192008-12-02 15:08:03 +0000956 run_delayed_work(&card->delayed_work);
Liam Girdwood965ac422007-01-31 14:14:57 +0100957
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200958 if (platform->remove)
959 platform->remove(pdev);
960
961 if (codec_dev->remove)
962 codec_dev->remove(pdev);
963
Mark Brown87506542008-11-18 20:50:34 +0000964 for (i = 0; i < card->num_links; i++) {
965 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200966 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100967 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200968 }
969
Mark Brown87506542008-11-18 20:50:34 +0000970 if (card->remove)
971 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200972
Mark Brownc5af3a22008-11-28 13:29:45 +0000973 snd_soc_unregister_card(card);
974
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200975 return 0;
976}
977
978/* ASoC platform driver */
979static struct platform_driver soc_driver = {
980 .driver = {
981 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +0200982 .owner = THIS_MODULE,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200983 },
984 .probe = soc_probe,
985 .remove = soc_remove,
986 .suspend = soc_suspend,
987 .resume = soc_resume,
988};
989
990/* create a new pcm */
991static int soc_new_pcm(struct snd_soc_device *socdev,
992 struct snd_soc_dai_link *dai_link, int num)
993{
Mark Brown87689d52008-12-02 16:01:14 +0000994 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +0000995 struct snd_soc_codec *codec = card->codec;
Mark Brown87689d52008-12-02 16:01:14 +0000996 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100997 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
998 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200999 struct snd_soc_pcm_runtime *rtd;
1000 struct snd_pcm *pcm;
1001 char new_name[64];
1002 int ret = 0, playback = 0, capture = 0;
1003
1004 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1005 if (rtd == NULL)
1006 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +01001007
1008 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001009 rtd->socdev = socdev;
Mark Brown6627a652009-01-23 22:55:23 +00001010 codec_dai->codec = card->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001011
1012 /* check client and interface hw capabilities */
Mark Brown3ba9e10a2008-11-24 18:01:05 +00001013 sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
1014 num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001015
1016 if (codec_dai->playback.channels_min)
1017 playback = 1;
1018 if (codec_dai->capture.channels_min)
1019 capture = 1;
1020
1021 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1022 capture, &pcm);
1023 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001024 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1025 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001026 kfree(rtd);
1027 return ret;
1028 }
1029
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001030 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001031 pcm->private_data = rtd;
Mark Brown87689d52008-12-02 16:01:14 +00001032 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1033 soc_pcm_ops.pointer = platform->pcm_ops->pointer;
1034 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1035 soc_pcm_ops.copy = platform->pcm_ops->copy;
1036 soc_pcm_ops.silence = platform->pcm_ops->silence;
1037 soc_pcm_ops.ack = platform->pcm_ops->ack;
1038 soc_pcm_ops.page = platform->pcm_ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001039
1040 if (playback)
1041 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1042
1043 if (capture)
1044 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1045
Mark Brown87689d52008-12-02 16:01:14 +00001046 ret = platform->pcm_new(codec->card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001047 if (ret < 0) {
1048 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1049 kfree(rtd);
1050 return ret;
1051 }
1052
Mark Brown87689d52008-12-02 16:01:14 +00001053 pcm->private_free = platform->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001054 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1055 cpu_dai->name);
1056 return ret;
1057}
1058
1059/* codec register dump */
Mark Brown6627a652009-01-23 22:55:23 +00001060static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001061{
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001062 int i, step = 1, count = 0;
1063
1064 if (!codec->reg_cache_size)
1065 return 0;
1066
1067 if (codec->reg_cache_step)
1068 step = codec->reg_cache_step;
1069
1070 count += sprintf(buf, "%s registers\n", codec->name);
Mark Brown58cd33c2008-07-29 11:42:25 +01001071 for (i = 0; i < codec->reg_cache_size; i += step) {
1072 count += sprintf(buf + count, "%2x: ", i);
1073 if (count >= PAGE_SIZE - 1)
1074 break;
1075
1076 if (codec->display_register)
1077 count += codec->display_register(codec, buf + count,
1078 PAGE_SIZE - count, i);
1079 else
1080 count += snprintf(buf + count, PAGE_SIZE - count,
1081 "%4x", codec->read(codec, i));
1082
1083 if (count >= PAGE_SIZE - 1)
1084 break;
1085
1086 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1087 if (count >= PAGE_SIZE - 1)
1088 break;
1089 }
1090
1091 /* Truncate count; min() would cause a warning */
1092 if (count >= PAGE_SIZE)
1093 count = PAGE_SIZE - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001094
1095 return count;
1096}
Troy Kisky12ef1932008-10-13 17:42:14 -07001097static ssize_t codec_reg_show(struct device *dev,
1098 struct device_attribute *attr, char *buf)
1099{
1100 struct snd_soc_device *devdata = dev_get_drvdata(dev);
Mark Brown6627a652009-01-23 22:55:23 +00001101 return soc_codec_reg_show(devdata->card->codec, buf);
Troy Kisky12ef1932008-10-13 17:42:14 -07001102}
1103
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001104static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1105
Troy Kisky12ef1932008-10-13 17:42:14 -07001106#ifdef CONFIG_DEBUG_FS
1107static int codec_reg_open_file(struct inode *inode, struct file *file)
1108{
1109 file->private_data = inode->i_private;
1110 return 0;
1111}
1112
1113static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1114 size_t count, loff_t *ppos)
1115{
1116 ssize_t ret;
Mark Brown384c89e2008-12-03 17:34:03 +00001117 struct snd_soc_codec *codec = file->private_data;
Troy Kisky12ef1932008-10-13 17:42:14 -07001118 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1119 if (!buf)
1120 return -ENOMEM;
Mark Brown6627a652009-01-23 22:55:23 +00001121 ret = soc_codec_reg_show(codec, buf);
Troy Kisky12ef1932008-10-13 17:42:14 -07001122 if (ret >= 0)
1123 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1124 kfree(buf);
1125 return ret;
1126}
1127
1128static ssize_t codec_reg_write_file(struct file *file,
1129 const char __user *user_buf, size_t count, loff_t *ppos)
1130{
1131 char buf[32];
1132 int buf_size;
1133 char *start = buf;
1134 unsigned long reg, value;
1135 int step = 1;
Mark Brown384c89e2008-12-03 17:34:03 +00001136 struct snd_soc_codec *codec = file->private_data;
Troy Kisky12ef1932008-10-13 17:42:14 -07001137
1138 buf_size = min(count, (sizeof(buf)-1));
1139 if (copy_from_user(buf, user_buf, buf_size))
1140 return -EFAULT;
1141 buf[buf_size] = 0;
1142
1143 if (codec->reg_cache_step)
1144 step = codec->reg_cache_step;
1145
1146 while (*start == ' ')
1147 start++;
1148 reg = simple_strtoul(start, &start, 16);
1149 if ((reg >= codec->reg_cache_size) || (reg % step))
1150 return -EINVAL;
1151 while (*start == ' ')
1152 start++;
1153 if (strict_strtoul(start, 16, &value))
1154 return -EINVAL;
1155 codec->write(codec, reg, value);
1156 return buf_size;
1157}
1158
1159static const struct file_operations codec_reg_fops = {
1160 .open = codec_reg_open_file,
1161 .read = codec_reg_read_file,
1162 .write = codec_reg_write_file,
1163};
1164
Mark Brown384c89e2008-12-03 17:34:03 +00001165static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001166{
Mark Brown384c89e2008-12-03 17:34:03 +00001167 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
1168 debugfs_root, codec,
1169 &codec_reg_fops);
1170 if (!codec->debugfs_reg)
1171 printk(KERN_WARNING
1172 "ASoC: Failed to create codec register debugfs file\n");
Troy Kisky12ef1932008-10-13 17:42:14 -07001173
Mark Brown384c89e2008-12-03 17:34:03 +00001174 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
1175 debugfs_root,
1176 &codec->pop_time);
1177 if (!codec->debugfs_pop_time)
1178 printk(KERN_WARNING
1179 "Failed to create pop time debugfs file\n");
Troy Kisky12ef1932008-10-13 17:42:14 -07001180}
1181
Mark Brown384c89e2008-12-03 17:34:03 +00001182static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001183{
Mark Brown384c89e2008-12-03 17:34:03 +00001184 debugfs_remove(codec->debugfs_pop_time);
1185 debugfs_remove(codec->debugfs_reg);
Troy Kisky12ef1932008-10-13 17:42:14 -07001186}
1187
1188#else
1189
Mark Brown384c89e2008-12-03 17:34:03 +00001190static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001191{
1192}
1193
Mark Brown384c89e2008-12-03 17:34:03 +00001194static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001195{
1196}
1197#endif
1198
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001199/**
1200 * snd_soc_new_ac97_codec - initailise AC97 device
1201 * @codec: audio codec
1202 * @ops: AC97 bus operations
1203 * @num: AC97 codec number
1204 *
1205 * Initialises AC97 codec resources for use by ad-hoc devices only.
1206 */
1207int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1208 struct snd_ac97_bus_ops *ops, int num)
1209{
1210 mutex_lock(&codec->mutex);
1211
1212 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1213 if (codec->ac97 == NULL) {
1214 mutex_unlock(&codec->mutex);
1215 return -ENOMEM;
1216 }
1217
1218 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1219 if (codec->ac97->bus == NULL) {
1220 kfree(codec->ac97);
1221 codec->ac97 = NULL;
1222 mutex_unlock(&codec->mutex);
1223 return -ENOMEM;
1224 }
1225
1226 codec->ac97->bus->ops = ops;
1227 codec->ac97->num = num;
1228 mutex_unlock(&codec->mutex);
1229 return 0;
1230}
1231EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1232
1233/**
1234 * snd_soc_free_ac97_codec - free AC97 codec device
1235 * @codec: audio codec
1236 *
1237 * Frees AC97 codec device resources.
1238 */
1239void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1240{
1241 mutex_lock(&codec->mutex);
1242 kfree(codec->ac97->bus);
1243 kfree(codec->ac97);
1244 codec->ac97 = NULL;
1245 mutex_unlock(&codec->mutex);
1246}
1247EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1248
1249/**
1250 * snd_soc_update_bits - update codec register bits
1251 * @codec: audio codec
1252 * @reg: codec register
1253 * @mask: register mask
1254 * @value: new value
1255 *
1256 * Writes new register value.
1257 *
1258 * Returns 1 for change else 0.
1259 */
1260int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1261 unsigned short mask, unsigned short value)
1262{
1263 int change;
1264 unsigned short old, new;
1265
1266 mutex_lock(&io_mutex);
1267 old = snd_soc_read(codec, reg);
1268 new = (old & ~mask) | value;
1269 change = old != new;
1270 if (change)
1271 snd_soc_write(codec, reg, new);
1272
1273 mutex_unlock(&io_mutex);
1274 return change;
1275}
1276EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1277
1278/**
1279 * snd_soc_test_bits - test register for change
1280 * @codec: audio codec
1281 * @reg: codec register
1282 * @mask: register mask
1283 * @value: new value
1284 *
1285 * Tests a register with a new value and checks if the new value is
1286 * different from the old value.
1287 *
1288 * Returns 1 for change else 0.
1289 */
1290int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1291 unsigned short mask, unsigned short value)
1292{
1293 int change;
1294 unsigned short old, new;
1295
1296 mutex_lock(&io_mutex);
1297 old = snd_soc_read(codec, reg);
1298 new = (old & ~mask) | value;
1299 change = old != new;
1300 mutex_unlock(&io_mutex);
1301
1302 return change;
1303}
1304EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1305
1306/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001307 * snd_soc_new_pcms - create new sound card and pcms
1308 * @socdev: the SoC audio device
Mark Brownac11a2b2009-01-01 12:18:17 +00001309 * @idx: ALSA card index
1310 * @xid: card identification
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001311 *
1312 * Create a new sound card based upon the codec and interface pcms.
1313 *
1314 * Returns 0 for success, else error.
1315 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001316int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001317{
Mark Brown87506542008-11-18 20:50:34 +00001318 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001319 struct snd_soc_codec *codec = card->codec;
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001320 int ret, i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001321
1322 mutex_lock(&codec->mutex);
1323
1324 /* register a sound card */
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001325 ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
1326 if (ret < 0) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001327 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1328 codec->name);
1329 mutex_unlock(&codec->mutex);
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001330 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001331 }
1332
Mark Brown452c5ea2009-05-17 21:41:23 +01001333 codec->socdev = socdev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001334 codec->card->dev = socdev->dev;
1335 codec->card->private_data = codec;
1336 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1337
1338 /* create the pcms */
Mark Brown87506542008-11-18 20:50:34 +00001339 for (i = 0; i < card->num_links; i++) {
1340 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001341 if (ret < 0) {
1342 printk(KERN_ERR "asoc: can't create pcm %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001343 card->dai_link[i].stream_name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001344 mutex_unlock(&codec->mutex);
1345 return ret;
1346 }
1347 }
1348
1349 mutex_unlock(&codec->mutex);
1350 return ret;
1351}
1352EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1353
1354/**
Mark Brown968a6022008-11-28 11:49:07 +00001355 * snd_soc_init_card - register sound card
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001356 * @socdev: the SoC audio device
1357 *
1358 * Register a SoC sound card. Also registers an AC97 device if the
1359 * codec is AC97 for ad hoc devices.
1360 *
1361 * Returns 0 for success, else error.
1362 */
Mark Brown968a6022008-11-28 11:49:07 +00001363int snd_soc_init_card(struct snd_soc_device *socdev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001364{
Mark Brown87506542008-11-18 20:50:34 +00001365 struct snd_soc_card *card = socdev->card;
Mark Brown6627a652009-01-23 22:55:23 +00001366 struct snd_soc_codec *codec = card->codec;
Liam Girdwood12e74f72006-10-16 21:19:48 +02001367 int ret = 0, i, ac97 = 0, err = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001368
Mark Brown87506542008-11-18 20:50:34 +00001369 for (i = 0; i < card->num_links; i++) {
1370 if (card->dai_link[i].init) {
1371 err = card->dai_link[i].init(codec);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001372 if (err < 0) {
1373 printk(KERN_ERR "asoc: failed to init %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001374 card->dai_link[i].stream_name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001375 continue;
1376 }
1377 }
Mark Brown3ba9e10a2008-11-24 18:01:05 +00001378 if (card->dai_link[i].codec_dai->ac97_control)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001379 ac97 = 1;
1380 }
1381 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
Mark Brown87506542008-11-18 20:50:34 +00001382 "%s", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001383 snprintf(codec->card->longname, sizeof(codec->card->longname),
Mark Brown87506542008-11-18 20:50:34 +00001384 "%s (%s)", card->name, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001385
1386 ret = snd_card_register(codec->card);
1387 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001388 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001389 codec->name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001390 goto out;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001391 }
1392
Mark Brown08c8efe2008-01-21 14:33:37 +01001393 mutex_lock(&codec->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001394#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown14fa43f2009-02-12 19:33:19 +00001395 /* Only instantiate AC97 if not already done by the adaptor
1396 * for the generic AC97 subsystem.
1397 */
1398 if (ac97 && strcmp(codec->name, "AC97") != 0) {
Liam Girdwood12e74f72006-10-16 21:19:48 +02001399 ret = soc_ac97_dev_register(codec);
1400 if (ret < 0) {
1401 printk(KERN_ERR "asoc: AC97 device register failed\n");
1402 snd_card_free(codec->card);
Mark Brown08c8efe2008-01-21 14:33:37 +01001403 mutex_unlock(&codec->mutex);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001404 goto out;
1405 }
1406 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001407#endif
1408
Liam Girdwood12e74f72006-10-16 21:19:48 +02001409 err = snd_soc_dapm_sys_add(socdev->dev);
1410 if (err < 0)
1411 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1412
1413 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1414 if (err < 0)
Mark Brown3ff3f642008-05-19 12:32:25 +02001415 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
Mark Brown08c8efe2008-01-21 14:33:37 +01001416
Mark Brown6627a652009-01-23 22:55:23 +00001417 soc_init_codec_debugfs(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001418 mutex_unlock(&codec->mutex);
Mark Brown08c8efe2008-01-21 14:33:37 +01001419
1420out:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001421 return ret;
1422}
Mark Brown968a6022008-11-28 11:49:07 +00001423EXPORT_SYMBOL_GPL(snd_soc_init_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001424
1425/**
1426 * snd_soc_free_pcms - free sound card and pcms
1427 * @socdev: the SoC audio device
1428 *
1429 * Frees sound card and pcms associated with the socdev.
1430 * Also unregister the codec if it is an AC97 device.
1431 */
1432void snd_soc_free_pcms(struct snd_soc_device *socdev)
1433{
Mark Brown6627a652009-01-23 22:55:23 +00001434 struct snd_soc_codec *codec = socdev->card->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001435#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001436 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001437 int i;
1438#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001439
1440 mutex_lock(&codec->mutex);
Mark Brown6627a652009-01-23 22:55:23 +00001441 soc_cleanup_codec_debugfs(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001442#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001443 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001444 codec_dai = &codec->dai[i];
Atsushi Nemotod2314e02009-03-16 23:26:20 +09001445 if (codec_dai->ac97_control && codec->ac97 &&
1446 strcmp(codec->name, "AC97") != 0) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001447 soc_ac97_dev_unregister(codec);
1448 goto free_card;
1449 }
1450 }
1451free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001452#endif
1453
1454 if (codec->card)
1455 snd_card_free(codec->card);
1456 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1457 mutex_unlock(&codec->mutex);
1458}
1459EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1460
1461/**
1462 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1463 * @substream: the pcm substream
1464 * @hw: the hardware parameters
1465 *
1466 * Sets the substream runtime hardware parameters.
1467 */
1468int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1469 const struct snd_pcm_hardware *hw)
1470{
1471 struct snd_pcm_runtime *runtime = substream->runtime;
1472 runtime->hw.info = hw->info;
1473 runtime->hw.formats = hw->formats;
1474 runtime->hw.period_bytes_min = hw->period_bytes_min;
1475 runtime->hw.period_bytes_max = hw->period_bytes_max;
1476 runtime->hw.periods_min = hw->periods_min;
1477 runtime->hw.periods_max = hw->periods_max;
1478 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1479 runtime->hw.fifo_size = hw->fifo_size;
1480 return 0;
1481}
1482EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1483
1484/**
1485 * snd_soc_cnew - create new control
1486 * @_template: control template
1487 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001488 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001489 *
1490 * Create a new mixer control from a template control.
1491 *
1492 * Returns 0 for success, else error.
1493 */
1494struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1495 void *data, char *long_name)
1496{
1497 struct snd_kcontrol_new template;
1498
1499 memcpy(&template, _template, sizeof(template));
1500 if (long_name)
1501 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001502 template.index = 0;
1503
1504 return snd_ctl_new1(&template, data);
1505}
1506EXPORT_SYMBOL_GPL(snd_soc_cnew);
1507
1508/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001509 * snd_soc_add_controls - add an array of controls to a codec.
1510 * Convienience function to add a list of controls. Many codecs were
1511 * duplicating this code.
1512 *
1513 * @codec: codec to add controls to
1514 * @controls: array of controls to add
1515 * @num_controls: number of elements in the array
1516 *
1517 * Return 0 for success, else error.
1518 */
1519int snd_soc_add_controls(struct snd_soc_codec *codec,
1520 const struct snd_kcontrol_new *controls, int num_controls)
1521{
1522 struct snd_card *card = codec->card;
1523 int err, i;
1524
1525 for (i = 0; i < num_controls; i++) {
1526 const struct snd_kcontrol_new *control = &controls[i];
1527 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1528 if (err < 0) {
1529 dev_err(codec->dev, "%s: Failed to add %s\n",
1530 codec->name, control->name);
1531 return err;
1532 }
1533 }
1534
1535 return 0;
1536}
1537EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1538
1539/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001540 * snd_soc_info_enum_double - enumerated double mixer info callback
1541 * @kcontrol: mixer control
1542 * @uinfo: control element information
1543 *
1544 * Callback to provide information about a double enumerated
1545 * mixer control.
1546 *
1547 * Returns 0 for success.
1548 */
1549int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1550 struct snd_ctl_elem_info *uinfo)
1551{
1552 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1553
1554 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1555 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001556 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001557
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001558 if (uinfo->value.enumerated.item > e->max - 1)
1559 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001560 strcpy(uinfo->value.enumerated.name,
1561 e->texts[uinfo->value.enumerated.item]);
1562 return 0;
1563}
1564EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1565
1566/**
1567 * snd_soc_get_enum_double - enumerated double mixer get callback
1568 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001569 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001570 *
1571 * Callback to get the value of a double enumerated mixer.
1572 *
1573 * Returns 0 for success.
1574 */
1575int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1576 struct snd_ctl_elem_value *ucontrol)
1577{
1578 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1579 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1580 unsigned short val, bitmask;
1581
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001582 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001583 ;
1584 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001585 ucontrol->value.enumerated.item[0]
1586 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001587 if (e->shift_l != e->shift_r)
1588 ucontrol->value.enumerated.item[1] =
1589 (val >> e->shift_r) & (bitmask - 1);
1590
1591 return 0;
1592}
1593EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1594
1595/**
1596 * snd_soc_put_enum_double - enumerated double mixer put callback
1597 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001598 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001599 *
1600 * Callback to set the value of a double enumerated mixer.
1601 *
1602 * Returns 0 for success.
1603 */
1604int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1605 struct snd_ctl_elem_value *ucontrol)
1606{
1607 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1608 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1609 unsigned short val;
1610 unsigned short mask, bitmask;
1611
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001612 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001613 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001614 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001615 return -EINVAL;
1616 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1617 mask = (bitmask - 1) << e->shift_l;
1618 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001619 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001620 return -EINVAL;
1621 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1622 mask |= (bitmask - 1) << e->shift_r;
1623 }
1624
1625 return snd_soc_update_bits(codec, e->reg, mask, val);
1626}
1627EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1628
1629/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001630 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
1631 * @kcontrol: mixer control
1632 * @ucontrol: control element information
1633 *
1634 * Callback to get the value of a double semi enumerated mixer.
1635 *
1636 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1637 * used for handling bitfield coded enumeration for example.
1638 *
1639 * Returns 0 for success.
1640 */
1641int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
1642 struct snd_ctl_elem_value *ucontrol)
1643{
1644 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001645 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001646 unsigned short reg_val, val, mux;
1647
1648 reg_val = snd_soc_read(codec, e->reg);
1649 val = (reg_val >> e->shift_l) & e->mask;
1650 for (mux = 0; mux < e->max; mux++) {
1651 if (val == e->values[mux])
1652 break;
1653 }
1654 ucontrol->value.enumerated.item[0] = mux;
1655 if (e->shift_l != e->shift_r) {
1656 val = (reg_val >> e->shift_r) & e->mask;
1657 for (mux = 0; mux < e->max; mux++) {
1658 if (val == e->values[mux])
1659 break;
1660 }
1661 ucontrol->value.enumerated.item[1] = mux;
1662 }
1663
1664 return 0;
1665}
1666EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
1667
1668/**
1669 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
1670 * @kcontrol: mixer control
1671 * @ucontrol: control element information
1672 *
1673 * Callback to set the value of a double semi enumerated mixer.
1674 *
1675 * Semi enumerated mixer: the enumerated items are referred as values. Can be
1676 * used for handling bitfield coded enumeration for example.
1677 *
1678 * Returns 0 for success.
1679 */
1680int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
1681 struct snd_ctl_elem_value *ucontrol)
1682{
1683 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02001684 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02001685 unsigned short val;
1686 unsigned short mask;
1687
1688 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1689 return -EINVAL;
1690 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
1691 mask = e->mask << e->shift_l;
1692 if (e->shift_l != e->shift_r) {
1693 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1694 return -EINVAL;
1695 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
1696 mask |= e->mask << e->shift_r;
1697 }
1698
1699 return snd_soc_update_bits(codec, e->reg, mask, val);
1700}
1701EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
1702
1703/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001704 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1705 * @kcontrol: mixer control
1706 * @uinfo: control element information
1707 *
1708 * Callback to provide information about an external enumerated
1709 * single mixer.
1710 *
1711 * Returns 0 for success.
1712 */
1713int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1714 struct snd_ctl_elem_info *uinfo)
1715{
1716 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1717
1718 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1719 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001720 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001721
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001722 if (uinfo->value.enumerated.item > e->max - 1)
1723 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001724 strcpy(uinfo->value.enumerated.name,
1725 e->texts[uinfo->value.enumerated.item]);
1726 return 0;
1727}
1728EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1729
1730/**
1731 * snd_soc_info_volsw_ext - external single mixer info callback
1732 * @kcontrol: mixer control
1733 * @uinfo: control element information
1734 *
1735 * Callback to provide information about a single external mixer control.
1736 *
1737 * Returns 0 for success.
1738 */
1739int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1740 struct snd_ctl_elem_info *uinfo)
1741{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001742 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001743
Mark Brownfd5dfad2009-04-15 21:37:46 +01001744 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001745 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1746 else
1747 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1748
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001749 uinfo->count = 1;
1750 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001751 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001752 return 0;
1753}
1754EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1755
1756/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001757 * snd_soc_info_volsw - single mixer info callback
1758 * @kcontrol: mixer control
1759 * @uinfo: control element information
1760 *
1761 * Callback to provide information about a single mixer control.
1762 *
1763 * Returns 0 for success.
1764 */
1765int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1766 struct snd_ctl_elem_info *uinfo)
1767{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001768 struct soc_mixer_control *mc =
1769 (struct soc_mixer_control *)kcontrol->private_value;
1770 int max = mc->max;
Mark Brown762b8df2008-10-30 12:37:08 +00001771 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001772 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001773
Mark Brownfd5dfad2009-04-15 21:37:46 +01001774 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001775 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1776 else
1777 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1778
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001779 uinfo->count = shift == rshift ? 1 : 2;
1780 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001781 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001782 return 0;
1783}
1784EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1785
1786/**
1787 * snd_soc_get_volsw - single mixer get callback
1788 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001789 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001790 *
1791 * Callback to get the value of a single mixer control.
1792 *
1793 * Returns 0 for success.
1794 */
1795int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1796 struct snd_ctl_elem_value *ucontrol)
1797{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001798 struct soc_mixer_control *mc =
1799 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001800 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001801 unsigned int reg = mc->reg;
1802 unsigned int shift = mc->shift;
1803 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001804 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001805 unsigned int mask = (1 << fls(max)) - 1;
1806 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001807
1808 ucontrol->value.integer.value[0] =
1809 (snd_soc_read(codec, reg) >> shift) & mask;
1810 if (shift != rshift)
1811 ucontrol->value.integer.value[1] =
1812 (snd_soc_read(codec, reg) >> rshift) & mask;
1813 if (invert) {
1814 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001815 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001816 if (shift != rshift)
1817 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001818 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001819 }
1820
1821 return 0;
1822}
1823EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1824
1825/**
1826 * snd_soc_put_volsw - single mixer put callback
1827 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001828 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001829 *
1830 * Callback to set the value of a single mixer control.
1831 *
1832 * Returns 0 for success.
1833 */
1834int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1835 struct snd_ctl_elem_value *ucontrol)
1836{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001837 struct soc_mixer_control *mc =
1838 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001839 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001840 unsigned int reg = mc->reg;
1841 unsigned int shift = mc->shift;
1842 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001843 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001844 unsigned int mask = (1 << fls(max)) - 1;
1845 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001846 unsigned short val, val2, val_mask;
1847
1848 val = (ucontrol->value.integer.value[0] & mask);
1849 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001850 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001851 val_mask = mask << shift;
1852 val = val << shift;
1853 if (shift != rshift) {
1854 val2 = (ucontrol->value.integer.value[1] & mask);
1855 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001856 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001857 val_mask |= mask << rshift;
1858 val |= val2 << rshift;
1859 }
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001860 return snd_soc_update_bits(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001861}
1862EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1863
1864/**
1865 * snd_soc_info_volsw_2r - double mixer info callback
1866 * @kcontrol: mixer control
1867 * @uinfo: control element information
1868 *
1869 * Callback to provide information about a double mixer control that
1870 * spans 2 codec registers.
1871 *
1872 * Returns 0 for success.
1873 */
1874int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1875 struct snd_ctl_elem_info *uinfo)
1876{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001877 struct soc_mixer_control *mc =
1878 (struct soc_mixer_control *)kcontrol->private_value;
1879 int max = mc->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001880
Mark Brownfd5dfad2009-04-15 21:37:46 +01001881 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001882 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1883 else
1884 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1885
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001886 uinfo->count = 2;
1887 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001888 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001889 return 0;
1890}
1891EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1892
1893/**
1894 * snd_soc_get_volsw_2r - double mixer get callback
1895 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001896 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001897 *
1898 * Callback to get the value of a double mixer control that spans 2 registers.
1899 *
1900 * Returns 0 for success.
1901 */
1902int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1903 struct snd_ctl_elem_value *ucontrol)
1904{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001905 struct soc_mixer_control *mc =
1906 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001907 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001908 unsigned int reg = mc->reg;
1909 unsigned int reg2 = mc->rreg;
1910 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001911 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001912 unsigned int mask = (1<<fls(max))-1;
1913 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001914
1915 ucontrol->value.integer.value[0] =
1916 (snd_soc_read(codec, reg) >> shift) & mask;
1917 ucontrol->value.integer.value[1] =
1918 (snd_soc_read(codec, reg2) >> shift) & mask;
1919 if (invert) {
1920 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001921 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001922 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001923 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001924 }
1925
1926 return 0;
1927}
1928EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1929
1930/**
1931 * snd_soc_put_volsw_2r - double mixer set callback
1932 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00001933 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001934 *
1935 * Callback to set the value of a double mixer control that spans 2 registers.
1936 *
1937 * Returns 0 for success.
1938 */
1939int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1940 struct snd_ctl_elem_value *ucontrol)
1941{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001942 struct soc_mixer_control *mc =
1943 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001944 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001945 unsigned int reg = mc->reg;
1946 unsigned int reg2 = mc->rreg;
1947 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001948 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001949 unsigned int mask = (1 << fls(max)) - 1;
1950 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001951 int err;
1952 unsigned short val, val2, val_mask;
1953
1954 val_mask = mask << shift;
1955 val = (ucontrol->value.integer.value[0] & mask);
1956 val2 = (ucontrol->value.integer.value[1] & mask);
1957
1958 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001959 val = max - val;
1960 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001961 }
1962
1963 val = val << shift;
1964 val2 = val2 << shift;
1965
Mark Brown3ff3f642008-05-19 12:32:25 +02001966 err = snd_soc_update_bits(codec, reg, val_mask, val);
1967 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001968 return err;
1969
1970 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1971 return err;
1972}
1973EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1974
Mark Browne13ac2e2008-05-28 17:58:05 +01001975/**
1976 * snd_soc_info_volsw_s8 - signed mixer info callback
1977 * @kcontrol: mixer control
1978 * @uinfo: control element information
1979 *
1980 * Callback to provide information about a signed mixer control.
1981 *
1982 * Returns 0 for success.
1983 */
1984int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1985 struct snd_ctl_elem_info *uinfo)
1986{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001987 struct soc_mixer_control *mc =
1988 (struct soc_mixer_control *)kcontrol->private_value;
1989 int max = mc->max;
1990 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001991
1992 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1993 uinfo->count = 2;
1994 uinfo->value.integer.min = 0;
1995 uinfo->value.integer.max = max-min;
1996 return 0;
1997}
1998EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1999
2000/**
2001 * snd_soc_get_volsw_s8 - signed mixer get callback
2002 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002003 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002004 *
2005 * Callback to get the value of a signed mixer control.
2006 *
2007 * Returns 0 for success.
2008 */
2009int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2010 struct snd_ctl_elem_value *ucontrol)
2011{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002012 struct soc_mixer_control *mc =
2013 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002014 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002015 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002016 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002017 int val = snd_soc_read(codec, reg);
2018
2019 ucontrol->value.integer.value[0] =
2020 ((signed char)(val & 0xff))-min;
2021 ucontrol->value.integer.value[1] =
2022 ((signed char)((val >> 8) & 0xff))-min;
2023 return 0;
2024}
2025EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2026
2027/**
2028 * snd_soc_put_volsw_sgn - signed mixer put callback
2029 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002030 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002031 *
2032 * Callback to set the value of a signed mixer control.
2033 *
2034 * Returns 0 for success.
2035 */
2036int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2037 struct snd_ctl_elem_value *ucontrol)
2038{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002039 struct soc_mixer_control *mc =
2040 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002041 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002042 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002043 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002044 unsigned short val;
2045
2046 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2047 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2048
2049 return snd_soc_update_bits(codec, reg, 0xffff, val);
2050}
2051EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2052
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002053/**
2054 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2055 * @dai: DAI
2056 * @clk_id: DAI specific clock ID
2057 * @freq: new clock frequency in Hz
2058 * @dir: new clock direction - input/output.
2059 *
2060 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2061 */
2062int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2063 unsigned int freq, int dir)
2064{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002065 if (dai->ops && dai->ops->set_sysclk)
Eric Miao6335d052009-03-03 09:41:00 +08002066 return dai->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002067 else
2068 return -EINVAL;
2069}
2070EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2071
2072/**
2073 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2074 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002075 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002076 * @div: new clock divisor.
2077 *
2078 * Configures the clock dividers. This is used to derive the best DAI bit and
2079 * frame clocks from the system or master clock. It's best to set the DAI bit
2080 * and frame clocks as low as possible to save system power.
2081 */
2082int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2083 int div_id, int div)
2084{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002085 if (dai->ops && dai->ops->set_clkdiv)
Eric Miao6335d052009-03-03 09:41:00 +08002086 return dai->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002087 else
2088 return -EINVAL;
2089}
2090EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2091
2092/**
2093 * snd_soc_dai_set_pll - configure DAI PLL.
2094 * @dai: DAI
2095 * @pll_id: DAI specific PLL ID
2096 * @freq_in: PLL input clock frequency in Hz
2097 * @freq_out: requested PLL output clock frequency in Hz
2098 *
2099 * Configures and enables PLL to generate output clock based on input clock.
2100 */
2101int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
2102 int pll_id, unsigned int freq_in, unsigned int freq_out)
2103{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002104 if (dai->ops && dai->ops->set_pll)
Eric Miao6335d052009-03-03 09:41:00 +08002105 return dai->ops->set_pll(dai, pll_id, freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002106 else
2107 return -EINVAL;
2108}
2109EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2110
2111/**
2112 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2113 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002114 * @fmt: SND_SOC_DAIFMT_ format value.
2115 *
2116 * Configures the DAI hardware format and clocking.
2117 */
2118int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2119{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002120 if (dai->ops && dai->ops->set_fmt)
Eric Miao6335d052009-03-03 09:41:00 +08002121 return dai->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002122 else
2123 return -EINVAL;
2124}
2125EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2126
2127/**
2128 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2129 * @dai: DAI
2130 * @mask: DAI specific mask representing used slots.
2131 * @slots: Number of slots in use.
2132 *
2133 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2134 * specific.
2135 */
2136int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2137 unsigned int mask, int slots)
2138{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002139 if (dai->ops && dai->ops->set_tdm_slot)
Eric Miao6335d052009-03-03 09:41:00 +08002140 return dai->ops->set_tdm_slot(dai, mask, slots);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002141 else
2142 return -EINVAL;
2143}
2144EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2145
2146/**
2147 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2148 * @dai: DAI
2149 * @tristate: tristate enable
2150 *
2151 * Tristates the DAI so that others can use it.
2152 */
2153int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2154{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002155 if (dai->ops && dai->ops->set_tristate)
Eric Miao6335d052009-03-03 09:41:00 +08002156 return dai->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002157 else
2158 return -EINVAL;
2159}
2160EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2161
2162/**
2163 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2164 * @dai: DAI
2165 * @mute: mute enable
2166 *
2167 * Mutes the DAI DAC.
2168 */
2169int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2170{
Mark Brown3f1a4d82009-04-15 21:35:26 +01002171 if (dai->ops && dai->ops->digital_mute)
Eric Miao6335d052009-03-03 09:41:00 +08002172 return dai->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002173 else
2174 return -EINVAL;
2175}
2176EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2177
Mark Brownc5af3a22008-11-28 13:29:45 +00002178/**
2179 * snd_soc_register_card - Register a card with the ASoC core
2180 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002181 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002182 *
2183 * Note that currently this is an internal only function: it will be
2184 * exposed to machine drivers after further backporting of ASoC v2
2185 * registration APIs.
2186 */
2187static int snd_soc_register_card(struct snd_soc_card *card)
2188{
2189 if (!card->name || !card->dev)
2190 return -EINVAL;
2191
2192 INIT_LIST_HEAD(&card->list);
2193 card->instantiated = 0;
2194
2195 mutex_lock(&client_mutex);
2196 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002197 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002198 mutex_unlock(&client_mutex);
2199
2200 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2201
2202 return 0;
2203}
2204
2205/**
2206 * snd_soc_unregister_card - Unregister a card with the ASoC core
2207 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002208 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002209 *
2210 * Note that currently this is an internal only function: it will be
2211 * exposed to machine drivers after further backporting of ASoC v2
2212 * registration APIs.
2213 */
2214static int snd_soc_unregister_card(struct snd_soc_card *card)
2215{
2216 mutex_lock(&client_mutex);
2217 list_del(&card->list);
2218 mutex_unlock(&client_mutex);
2219
2220 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2221
2222 return 0;
2223}
2224
Eric Miao6335d052009-03-03 09:41:00 +08002225static struct snd_soc_dai_ops null_dai_ops = {
2226};
2227
Mark Brown91151712008-11-30 23:31:24 +00002228/**
2229 * snd_soc_register_dai - Register a DAI with the ASoC core
2230 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002231 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002232 */
2233int snd_soc_register_dai(struct snd_soc_dai *dai)
2234{
2235 if (!dai->name)
2236 return -EINVAL;
2237
2238 /* The device should become mandatory over time */
2239 if (!dai->dev)
2240 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2241
Eric Miao6335d052009-03-03 09:41:00 +08002242 if (!dai->ops)
2243 dai->ops = &null_dai_ops;
2244
Mark Brown91151712008-11-30 23:31:24 +00002245 INIT_LIST_HEAD(&dai->list);
2246
2247 mutex_lock(&client_mutex);
2248 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002249 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002250 mutex_unlock(&client_mutex);
2251
2252 pr_debug("Registered DAI '%s'\n", dai->name);
2253
2254 return 0;
2255}
2256EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2257
2258/**
2259 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2260 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002261 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002262 */
2263void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2264{
2265 mutex_lock(&client_mutex);
2266 list_del(&dai->list);
2267 mutex_unlock(&client_mutex);
2268
2269 pr_debug("Unregistered DAI '%s'\n", dai->name);
2270}
2271EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2272
2273/**
2274 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2275 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002276 * @dai: Array of DAIs to register
2277 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002278 */
2279int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2280{
2281 int i, ret;
2282
2283 for (i = 0; i < count; i++) {
2284 ret = snd_soc_register_dai(&dai[i]);
2285 if (ret != 0)
2286 goto err;
2287 }
2288
2289 return 0;
2290
2291err:
2292 for (i--; i >= 0; i--)
2293 snd_soc_unregister_dai(&dai[i]);
2294
2295 return ret;
2296}
2297EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2298
2299/**
2300 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2301 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002302 * @dai: Array of DAIs to unregister
2303 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002304 */
2305void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2306{
2307 int i;
2308
2309 for (i = 0; i < count; i++)
2310 snd_soc_unregister_dai(&dai[i]);
2311}
2312EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2313
Mark Brown12a48a8c2008-12-03 19:40:30 +00002314/**
2315 * snd_soc_register_platform - Register a platform with the ASoC core
2316 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002317 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00002318 */
2319int snd_soc_register_platform(struct snd_soc_platform *platform)
2320{
2321 if (!platform->name)
2322 return -EINVAL;
2323
2324 INIT_LIST_HEAD(&platform->list);
2325
2326 mutex_lock(&client_mutex);
2327 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002328 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00002329 mutex_unlock(&client_mutex);
2330
2331 pr_debug("Registered platform '%s'\n", platform->name);
2332
2333 return 0;
2334}
2335EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2336
2337/**
2338 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2339 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002340 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00002341 */
2342void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2343{
2344 mutex_lock(&client_mutex);
2345 list_del(&platform->list);
2346 mutex_unlock(&client_mutex);
2347
2348 pr_debug("Unregistered platform '%s'\n", platform->name);
2349}
2350EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2351
Mark Brown151ab222009-05-09 16:22:58 +01002352static u64 codec_format_map[] = {
2353 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
2354 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
2355 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
2356 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
2357 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
2358 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
2359 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2360 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
2361 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
2362 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
2363 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
2364 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
2365 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
2366 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
2367 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
2368 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
2369};
2370
2371/* Fix up the DAI formats for endianness: codecs don't actually see
2372 * the endianness of the data but we're using the CPU format
2373 * definitions which do need to include endianness so we ensure that
2374 * codec DAIs always have both big and little endian variants set.
2375 */
2376static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
2377{
2378 int i;
2379
2380 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
2381 if (stream->formats & codec_format_map[i])
2382 stream->formats |= codec_format_map[i];
2383}
2384
Mark Brown0d0cf002008-12-10 14:32:45 +00002385/**
2386 * snd_soc_register_codec - Register a codec with the ASoC core
2387 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002388 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00002389 */
2390int snd_soc_register_codec(struct snd_soc_codec *codec)
2391{
Mark Brown151ab222009-05-09 16:22:58 +01002392 int i;
2393
Mark Brown0d0cf002008-12-10 14:32:45 +00002394 if (!codec->name)
2395 return -EINVAL;
2396
2397 /* The device should become mandatory over time */
2398 if (!codec->dev)
2399 printk(KERN_WARNING "No device for codec %s\n", codec->name);
2400
2401 INIT_LIST_HEAD(&codec->list);
2402
Mark Brown151ab222009-05-09 16:22:58 +01002403 for (i = 0; i < codec->num_dai; i++) {
2404 fixup_codec_formats(&codec->dai[i].playback);
2405 fixup_codec_formats(&codec->dai[i].capture);
2406 }
2407
Mark Brown0d0cf002008-12-10 14:32:45 +00002408 mutex_lock(&client_mutex);
2409 list_add(&codec->list, &codec_list);
2410 snd_soc_instantiate_cards();
2411 mutex_unlock(&client_mutex);
2412
2413 pr_debug("Registered codec '%s'\n", codec->name);
2414
2415 return 0;
2416}
2417EXPORT_SYMBOL_GPL(snd_soc_register_codec);
2418
2419/**
2420 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
2421 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002422 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00002423 */
2424void snd_soc_unregister_codec(struct snd_soc_codec *codec)
2425{
2426 mutex_lock(&client_mutex);
2427 list_del(&codec->list);
2428 mutex_unlock(&client_mutex);
2429
2430 pr_debug("Unregistered codec '%s'\n", codec->name);
2431}
2432EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
2433
Takashi Iwaic9b3a402008-12-10 07:47:22 +01002434static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002435{
Mark Brown384c89e2008-12-03 17:34:03 +00002436#ifdef CONFIG_DEBUG_FS
2437 debugfs_root = debugfs_create_dir("asoc", NULL);
2438 if (IS_ERR(debugfs_root) || !debugfs_root) {
2439 printk(KERN_WARNING
2440 "ASoC: Failed to create debugfs directory\n");
2441 debugfs_root = NULL;
2442 }
2443#endif
2444
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002445 return platform_driver_register(&soc_driver);
2446}
2447
Mark Brown7d8c16a2008-11-30 22:11:24 +00002448static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002449{
Mark Brown384c89e2008-12-03 17:34:03 +00002450#ifdef CONFIG_DEBUG_FS
2451 debugfs_remove_recursive(debugfs_root);
2452#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02002453 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002454}
2455
2456module_init(snd_soc_init);
2457module_exit(snd_soc_exit);
2458
2459/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002460MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002461MODULE_DESCRIPTION("ALSA SoC Core");
2462MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02002463MODULE_ALIAS("platform:soc-audio");