blob: 4d2db7cfaf4c4cfcfb724ac639370be6233b4b1c [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 Brownc5af3a22008-11-28 13:29:45 +000050
51static int snd_soc_register_card(struct snd_soc_card *card);
52static int snd_soc_unregister_card(struct snd_soc_card *card);
53
Frank Mandarinodb2a4162006-10-06 18:31:09 +020054/*
55 * This is a timeout to do a DAPM powerdown after a stream is closed().
56 * It can be used to eliminate pops between different playback streams, e.g.
57 * between two audio tracks.
58 */
59static int pmdown_time = 5000;
60module_param(pmdown_time, int, 0);
61MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
62
Liam Girdwood965ac422007-01-31 14:14:57 +010063/*
64 * This function forces any delayed work to be queued and run.
65 */
66static int run_delayed_work(struct delayed_work *dwork)
67{
68 int ret;
69
70 /* cancel any work waiting to be queued. */
71 ret = cancel_delayed_work(dwork);
72
73 /* if there was any work waiting then we run it now and
74 * wait for it's completion */
75 if (ret) {
76 schedule_delayed_work(dwork, 0);
77 flush_scheduled_work();
78 }
79 return ret;
80}
81
Frank Mandarinodb2a4162006-10-06 18:31:09 +020082#ifdef CONFIG_SND_SOC_AC97_BUS
83/* unregister ac97 codec */
84static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
85{
86 if (codec->ac97->dev.bus)
87 device_unregister(&codec->ac97->dev);
88 return 0;
89}
90
91/* stop no dev release warning */
92static void soc_ac97_device_release(struct device *dev){}
93
94/* register ac97 codec to bus */
95static int soc_ac97_dev_register(struct snd_soc_codec *codec)
96{
97 int err;
98
99 codec->ac97->dev.bus = &ac97_bus_type;
100 codec->ac97->dev.parent = NULL;
101 codec->ac97->dev.release = soc_ac97_device_release;
102
Kay Sieversbb072bf2008-11-02 03:50:35 +0100103 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
104 codec->card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200105 err = device_register(&codec->ac97->dev);
106 if (err < 0) {
107 snd_printk(KERN_ERR "Can't register ac97 bus\n");
108 codec->ac97->dev.bus = NULL;
109 return err;
110 }
111 return 0;
112}
113#endif
114
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200115/*
116 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
117 * then initialized and any private data can be allocated. This also calls
118 * startup for the cpu DAI, platform, machine and codec DAI.
119 */
120static int soc_pcm_open(struct snd_pcm_substream *substream)
121{
122 struct snd_soc_pcm_runtime *rtd = substream->private_data;
123 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000124 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200125 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100126 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000127 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100128 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
129 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200130 int ret = 0;
131
132 mutex_lock(&pcm_mutex);
133
134 /* startup the audio subsystem */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100135 if (cpu_dai->ops.startup) {
Mark Browndee89c42008-11-18 22:11:38 +0000136 ret = cpu_dai->ops.startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200137 if (ret < 0) {
138 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100139 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200140 goto out;
141 }
142 }
143
144 if (platform->pcm_ops->open) {
145 ret = platform->pcm_ops->open(substream);
146 if (ret < 0) {
147 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
148 goto platform_err;
149 }
150 }
151
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100152 if (codec_dai->ops.startup) {
Mark Browndee89c42008-11-18 22:11:38 +0000153 ret = codec_dai->ops.startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100154 if (ret < 0) {
155 printk(KERN_ERR "asoc: can't open codec %s\n",
156 codec_dai->name);
157 goto codec_dai_err;
158 }
159 }
160
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200161 if (machine->ops && machine->ops->startup) {
162 ret = machine->ops->startup(substream);
163 if (ret < 0) {
164 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
165 goto machine_err;
166 }
167 }
168
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200169 /* Check that the codec and cpu DAI's are compatible */
170 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
171 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200172 max(codec_dai->playback.rate_min,
173 cpu_dai->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200174 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200175 min(codec_dai->playback.rate_max,
176 cpu_dai->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200177 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100178 max(codec_dai->playback.channels_min,
179 cpu_dai->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200180 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100181 min(codec_dai->playback.channels_max,
182 cpu_dai->playback.channels_max);
183 runtime->hw.formats =
184 codec_dai->playback.formats & cpu_dai->playback.formats;
185 runtime->hw.rates =
186 codec_dai->playback.rates & cpu_dai->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200187 } else {
188 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200189 max(codec_dai->capture.rate_min,
190 cpu_dai->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200191 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200192 min(codec_dai->capture.rate_max,
193 cpu_dai->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200194 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100195 max(codec_dai->capture.channels_min,
196 cpu_dai->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200197 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100198 min(codec_dai->capture.channels_max,
199 cpu_dai->capture.channels_max);
200 runtime->hw.formats =
201 codec_dai->capture.formats & cpu_dai->capture.formats;
202 runtime->hw.rates =
203 codec_dai->capture.rates & cpu_dai->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200204 }
205
206 snd_pcm_limit_hw_rates(runtime);
207 if (!runtime->hw.rates) {
208 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100209 codec_dai->name, cpu_dai->name);
210 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200211 }
212 if (!runtime->hw.formats) {
213 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100214 codec_dai->name, cpu_dai->name);
215 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200216 }
217 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
218 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100219 codec_dai->name, cpu_dai->name);
220 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200221 }
222
Mark Brownf24368c2008-10-21 21:45:08 +0100223 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
224 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
225 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
226 runtime->hw.channels_max);
227 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
228 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200229
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200230 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100231 cpu_dai->playback.active = codec_dai->playback.active = 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200232 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100233 cpu_dai->capture.active = codec_dai->capture.active = 1;
234 cpu_dai->active = codec_dai->active = 1;
235 cpu_dai->runtime = runtime;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200236 socdev->codec->active++;
237 mutex_unlock(&pcm_mutex);
238 return 0;
239
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100240machine_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200241 if (machine->ops && machine->ops->shutdown)
242 machine->ops->shutdown(substream);
243
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100244codec_dai_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200245 if (platform->pcm_ops->close)
246 platform->pcm_ops->close(substream);
247
248platform_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100249 if (cpu_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000250 cpu_dai->ops.shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200251out:
252 mutex_unlock(&pcm_mutex);
253 return ret;
254}
255
256/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200257 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200258 * This is to ensure there are no pops or clicks in between any music tracks
259 * due to DAPM power cycling.
260 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100261static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200262{
Mark Brown63084192008-12-02 15:08:03 +0000263 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
264 delayed_work.work);
265 struct snd_soc_device *socdev = card->socdev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200266 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100267 struct snd_soc_dai *codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200268 int i;
269
270 mutex_lock(&pcm_mutex);
Mark Brown3ff3f642008-05-19 12:32:25 +0200271 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200272 codec_dai = &codec->dai[i];
273
Mark Brownf24368c2008-10-21 21:45:08 +0100274 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
275 codec_dai->playback.stream_name,
276 codec_dai->playback.active ? "active" : "inactive",
277 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200278
279 /* are we waiting on this codec DAI stream */
280 if (codec_dai->pop_wait == 1) {
281
Mark Brown0be98982008-05-19 12:31:28 +0200282 /* Reduce power if no longer active */
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100283 if (codec->active == 0) {
Mark Brownf24368c2008-10-21 21:45:08 +0100284 pr_debug("pop wq D1 %s %s\n", codec->name,
285 codec_dai->playback.stream_name);
Mark Brown0be98982008-05-19 12:31:28 +0200286 snd_soc_dapm_set_bias_level(socdev,
287 SND_SOC_BIAS_PREPARE);
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100288 }
289
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200290 codec_dai->pop_wait = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100291 snd_soc_dapm_stream_event(codec,
292 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200293 SND_SOC_DAPM_STREAM_STOP);
294
Mark Brown0be98982008-05-19 12:31:28 +0200295 /* Fall into standby if no longer active */
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200296 if (codec->active == 0) {
Mark Brownf24368c2008-10-21 21:45:08 +0100297 pr_debug("pop wq D3 %s %s\n", codec->name,
298 codec_dai->playback.stream_name);
Mark Brown0be98982008-05-19 12:31:28 +0200299 snd_soc_dapm_set_bias_level(socdev,
300 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200301 }
302 }
303 }
304 mutex_unlock(&pcm_mutex);
305}
306
307/*
308 * Called by ALSA when a PCM substream is closed. Private data can be
309 * freed here. The cpu DAI, codec DAI, machine and platform are also
310 * shutdown.
311 */
312static int soc_codec_close(struct snd_pcm_substream *substream)
313{
314 struct snd_soc_pcm_runtime *rtd = substream->private_data;
315 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000316 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100317 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000318 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100319 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
320 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200321 struct snd_soc_codec *codec = socdev->codec;
322
323 mutex_lock(&pcm_mutex);
324
325 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100326 cpu_dai->playback.active = codec_dai->playback.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200327 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100328 cpu_dai->capture.active = codec_dai->capture.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200329
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100330 if (codec_dai->playback.active == 0 &&
331 codec_dai->capture.active == 0) {
332 cpu_dai->active = codec_dai->active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200333 }
334 codec->active--;
335
Mark Brown6010b2d2008-09-06 18:33:24 +0100336 /* Muting the DAC suppresses artifacts caused during digital
337 * shutdown, for example from stopping clocks.
338 */
339 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
340 snd_soc_dai_digital_mute(codec_dai, 1);
341
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100342 if (cpu_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000343 cpu_dai->ops.shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200344
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100345 if (codec_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000346 codec_dai->ops.shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200347
348 if (machine->ops && machine->ops->shutdown)
349 machine->ops->shutdown(substream);
350
351 if (platform->pcm_ops->close)
352 platform->pcm_ops->close(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100353 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200354
355 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
356 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100357 codec_dai->pop_wait = 1;
Mark Brown63084192008-12-02 15:08:03 +0000358 schedule_delayed_work(&card->delayed_work,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200359 msecs_to_jiffies(pmdown_time));
360 } else {
361 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100362 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100363 codec_dai->capture.stream_name,
364 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200365
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100366 if (codec->active == 0 && codec_dai->pop_wait == 0)
Mark Brown0be98982008-05-19 12:31:28 +0200367 snd_soc_dapm_set_bias_level(socdev,
368 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200369 }
370
371 mutex_unlock(&pcm_mutex);
372 return 0;
373}
374
375/*
376 * Called by ALSA when the PCM substream is prepared, can set format, sample
377 * rate, etc. This function is non atomic and can be called multiple times,
378 * it can refer to the runtime info.
379 */
380static int soc_pcm_prepare(struct snd_pcm_substream *substream)
381{
382 struct snd_soc_pcm_runtime *rtd = substream->private_data;
383 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000384 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100385 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000386 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100387 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
388 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200389 struct snd_soc_codec *codec = socdev->codec;
390 int ret = 0;
391
392 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100393
394 if (machine->ops && machine->ops->prepare) {
395 ret = machine->ops->prepare(substream);
396 if (ret < 0) {
397 printk(KERN_ERR "asoc: machine prepare error\n");
398 goto out;
399 }
400 }
401
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200402 if (platform->pcm_ops->prepare) {
403 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200404 if (ret < 0) {
405 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200406 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200407 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200408 }
409
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100410 if (codec_dai->ops.prepare) {
Mark Browndee89c42008-11-18 22:11:38 +0000411 ret = codec_dai->ops.prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200412 if (ret < 0) {
413 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200414 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200415 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200416 }
417
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100418 if (cpu_dai->ops.prepare) {
Mark Browndee89c42008-11-18 22:11:38 +0000419 ret = cpu_dai->ops.prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100420 if (ret < 0) {
421 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
422 goto out;
423 }
424 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200425
Mark Brownd45f6212008-10-14 13:58:36 +0100426 /* cancel any delayed stream shutdown that is pending */
427 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
428 codec_dai->pop_wait) {
429 codec_dai->pop_wait = 0;
Mark Brown63084192008-12-02 15:08:03 +0000430 cancel_delayed_work(&card->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100431 }
432
433 /* do we need to power up codec */
434 if (codec->bias_level != SND_SOC_BIAS_ON) {
435 snd_soc_dapm_set_bias_level(socdev,
436 SND_SOC_BIAS_PREPARE);
437
438 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
439 snd_soc_dapm_stream_event(codec,
440 codec_dai->playback.stream_name,
441 SND_SOC_DAPM_STREAM_START);
442 else
443 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100444 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200445 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100446
447 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
448 snd_soc_dai_digital_mute(codec_dai, 0);
449
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200450 } else {
Mark Brownd45f6212008-10-14 13:58:36 +0100451 /* codec already powered - power on widgets */
452 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
453 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100454 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200455 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100456 else
457 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100458 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200459 SND_SOC_DAPM_STREAM_START);
460
Mark Brownd45f6212008-10-14 13:58:36 +0100461 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200462 }
463
464out:
465 mutex_unlock(&pcm_mutex);
466 return ret;
467}
468
469/*
470 * Called by ALSA when the hardware params are set by application. This
471 * function can also be called multiple times and can allocate buffers
472 * (using snd_pcm_lib_* ). It's non-atomic.
473 */
474static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
475 struct snd_pcm_hw_params *params)
476{
477 struct snd_soc_pcm_runtime *rtd = substream->private_data;
478 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100479 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000480 struct snd_soc_card *card = socdev->card;
481 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100482 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
483 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200484 int ret = 0;
485
486 mutex_lock(&pcm_mutex);
487
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100488 if (machine->ops && machine->ops->hw_params) {
489 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200490 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100491 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200492 goto out;
493 }
494 }
495
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100496 if (codec_dai->ops.hw_params) {
Mark Browndee89c42008-11-18 22:11:38 +0000497 ret = codec_dai->ops.hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100498 if (ret < 0) {
499 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
500 codec_dai->name);
501 goto codec_err;
502 }
503 }
504
505 if (cpu_dai->ops.hw_params) {
Mark Browndee89c42008-11-18 22:11:38 +0000506 ret = cpu_dai->ops.hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200507 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200508 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100509 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200510 goto interface_err;
511 }
512 }
513
514 if (platform->pcm_ops->hw_params) {
515 ret = platform->pcm_ops->hw_params(substream, params);
516 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200517 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200518 platform->name);
519 goto platform_err;
520 }
521 }
522
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200523out:
524 mutex_unlock(&pcm_mutex);
525 return ret;
526
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200527platform_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100528 if (cpu_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000529 cpu_dai->ops.hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200530
531interface_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100532 if (codec_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000533 codec_dai->ops.hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100534
535codec_err:
Mark Brown3ff3f642008-05-19 12:32:25 +0200536 if (machine->ops && machine->ops->hw_free)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100537 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200538
539 mutex_unlock(&pcm_mutex);
540 return ret;
541}
542
543/*
544 * Free's resources allocated by hw_params, can be called multiple times
545 */
546static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
547{
548 struct snd_soc_pcm_runtime *rtd = substream->private_data;
549 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100550 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000551 struct snd_soc_card *card = socdev->card;
552 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100553 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
554 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200555 struct snd_soc_codec *codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200556
557 mutex_lock(&pcm_mutex);
558
559 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100560 if (!codec->active)
561 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200562
563 /* free any machine hw params */
564 if (machine->ops && machine->ops->hw_free)
565 machine->ops->hw_free(substream);
566
567 /* free any DMA resources */
568 if (platform->pcm_ops->hw_free)
569 platform->pcm_ops->hw_free(substream);
570
571 /* now free hw params for the DAI's */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100572 if (codec_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000573 codec_dai->ops.hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200574
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100575 if (cpu_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000576 cpu_dai->ops.hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200577
578 mutex_unlock(&pcm_mutex);
579 return 0;
580}
581
582static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
583{
584 struct snd_soc_pcm_runtime *rtd = substream->private_data;
585 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000586 struct snd_soc_card *card= socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100587 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000588 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100589 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
590 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200591 int ret;
592
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100593 if (codec_dai->ops.trigger) {
Mark Browndee89c42008-11-18 22:11:38 +0000594 ret = codec_dai->ops.trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200595 if (ret < 0)
596 return ret;
597 }
598
599 if (platform->pcm_ops->trigger) {
600 ret = platform->pcm_ops->trigger(substream, cmd);
601 if (ret < 0)
602 return ret;
603 }
604
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100605 if (cpu_dai->ops.trigger) {
Mark Browndee89c42008-11-18 22:11:38 +0000606 ret = cpu_dai->ops.trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200607 if (ret < 0)
608 return ret;
609 }
610 return 0;
611}
612
613/* ASoC PCM operations */
614static struct snd_pcm_ops soc_pcm_ops = {
615 .open = soc_pcm_open,
616 .close = soc_codec_close,
617 .hw_params = soc_pcm_hw_params,
618 .hw_free = soc_pcm_hw_free,
619 .prepare = soc_pcm_prepare,
620 .trigger = soc_pcm_trigger,
621};
622
623#ifdef CONFIG_PM
624/* powers down audio subsystem for suspend */
625static int soc_suspend(struct platform_device *pdev, pm_message_t state)
626{
Mark Brown3ff3f642008-05-19 12:32:25 +0200627 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000628 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000629 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200630 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200631 struct snd_soc_codec *codec = socdev->codec;
632 int i;
633
Andy Green6ed25972008-06-13 16:24:05 +0100634 /* Due to the resume being scheduled into a workqueue we could
635 * suspend before that's finished - wait for it to complete.
636 */
637 snd_power_lock(codec->card);
638 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
639 snd_power_unlock(codec->card);
640
641 /* we're going to block userspace touching us until resume completes */
642 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
643
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200644 /* mute any active DAC's */
Mark Browndee89c42008-11-18 22:11:38 +0000645 for (i = 0; i < card->num_links; i++) {
646 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
647 if (dai->ops.digital_mute && dai->playback.active)
648 dai->ops.digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200649 }
650
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100651 /* suspend all pcms */
Mark Brown87506542008-11-18 20:50:34 +0000652 for (i = 0; i < card->num_links; i++)
653 snd_pcm_suspend_all(card->dai_link[i].pcm);
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100654
Mark Brown87506542008-11-18 20:50:34 +0000655 if (card->suspend_pre)
656 card->suspend_pre(pdev, state);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200657
Mark Brown87506542008-11-18 20:50:34 +0000658 for (i = 0; i < card->num_links; i++) {
659 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000660 if (cpu_dai->suspend && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000661 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200662 if (platform->suspend)
Mark Brown07c84d02008-12-03 18:17:28 +0000663 platform->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200664 }
665
666 /* close any waiting streams and save state */
Mark Brown63084192008-12-02 15:08:03 +0000667 run_delayed_work(&card->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200668 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200669
Mark Brown3ff3f642008-05-19 12:32:25 +0200670 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200671 char *stream = codec->dai[i].playback.stream_name;
672 if (stream != NULL)
673 snd_soc_dapm_stream_event(codec, stream,
674 SND_SOC_DAPM_STREAM_SUSPEND);
675 stream = codec->dai[i].capture.stream_name;
676 if (stream != NULL)
677 snd_soc_dapm_stream_event(codec, stream,
678 SND_SOC_DAPM_STREAM_SUSPEND);
679 }
680
681 if (codec_dev->suspend)
682 codec_dev->suspend(pdev, state);
683
Mark Brown87506542008-11-18 20:50:34 +0000684 for (i = 0; i < card->num_links; i++) {
685 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000686 if (cpu_dai->suspend && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000687 cpu_dai->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200688 }
689
Mark Brown87506542008-11-18 20:50:34 +0000690 if (card->suspend_post)
691 card->suspend_post(pdev, state);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200692
693 return 0;
694}
695
Andy Green6ed25972008-06-13 16:24:05 +0100696/* deferred resume work, so resume can complete before we finished
697 * setting our codec back up, which can be very slow on I2C
698 */
699static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700{
Mark Brown63084192008-12-02 15:08:03 +0000701 struct snd_soc_card *card = container_of(work,
702 struct snd_soc_card,
703 deferred_resume_work);
704 struct snd_soc_device *socdev = card->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000705 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200706 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200707 struct snd_soc_codec *codec = socdev->codec;
Andy Green6ed25972008-06-13 16:24:05 +0100708 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200709 int i;
710
Andy Green6ed25972008-06-13 16:24:05 +0100711 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
712 * so userspace apps are blocked from touching us
713 */
714
Mark Brownfde22f22008-11-24 18:08:18 +0000715 dev_dbg(socdev->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100716
Mark Brown87506542008-11-18 20:50:34 +0000717 if (card->resume_pre)
718 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200719
Mark Brown87506542008-11-18 20:50:34 +0000720 for (i = 0; i < card->num_links; i++) {
721 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000722 if (cpu_dai->resume && cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000723 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200724 }
725
726 if (codec_dev->resume)
727 codec_dev->resume(pdev);
728
Mark Brown3ff3f642008-05-19 12:32:25 +0200729 for (i = 0; i < codec->num_dai; i++) {
730 char *stream = codec->dai[i].playback.stream_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200731 if (stream != NULL)
732 snd_soc_dapm_stream_event(codec, stream,
733 SND_SOC_DAPM_STREAM_RESUME);
734 stream = codec->dai[i].capture.stream_name;
735 if (stream != NULL)
736 snd_soc_dapm_stream_event(codec, stream,
737 SND_SOC_DAPM_STREAM_RESUME);
738 }
739
Mark Brown3ff3f642008-05-19 12:32:25 +0200740 /* unmute any active DACs */
Mark Browndee89c42008-11-18 22:11:38 +0000741 for (i = 0; i < card->num_links; i++) {
742 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
743 if (dai->ops.digital_mute && dai->playback.active)
744 dai->ops.digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200745 }
746
Mark Brown87506542008-11-18 20:50:34 +0000747 for (i = 0; i < card->num_links; i++) {
748 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e10a2008-11-24 18:01:05 +0000749 if (cpu_dai->resume && !cpu_dai->ac97_control)
Mark Browndc7d7b82008-12-03 18:21:52 +0000750 cpu_dai->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200751 if (platform->resume)
Mark Brown07c84d02008-12-03 18:17:28 +0000752 platform->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200753 }
754
Mark Brown87506542008-11-18 20:50:34 +0000755 if (card->resume_post)
756 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200757
Mark Brownfde22f22008-11-24 18:08:18 +0000758 dev_dbg(socdev->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100759
760 /* userspace can access us now we are back as we were before */
761 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
762}
763
764/* powers up audio subsystem after a suspend */
765static int soc_resume(struct platform_device *pdev)
766{
767 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown63084192008-12-02 15:08:03 +0000768 struct snd_soc_card *card = socdev->card;
Andy Green6ed25972008-06-13 16:24:05 +0100769
Mark Brownfde22f22008-11-24 18:08:18 +0000770 dev_dbg(socdev->dev, "scheduling resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100771
Mark Brown63084192008-12-02 15:08:03 +0000772 if (!schedule_work(&card->deferred_resume_work))
Mark Brownfde22f22008-11-24 18:08:18 +0000773 dev_err(socdev->dev, "resume work item may be lost\n");
Andy Green6ed25972008-06-13 16:24:05 +0100774
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200775 return 0;
776}
777
778#else
779#define soc_suspend NULL
780#define soc_resume NULL
781#endif
782
Mark Brown435c5e22008-12-04 15:32:53 +0000783static void snd_soc_instantiate_card(struct snd_soc_card *card)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200784{
Mark Brown435c5e22008-12-04 15:32:53 +0000785 struct platform_device *pdev = container_of(card->dev,
786 struct platform_device,
787 dev);
788 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
789 struct snd_soc_platform *platform;
790 struct snd_soc_dai *dai;
Mark Brown6b05eda2008-12-08 19:26:48 +0000791 int i, found, ret, ac97;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200792
Mark Brown435c5e22008-12-04 15:32:53 +0000793 if (card->instantiated)
794 return;
Mark Brown63084192008-12-02 15:08:03 +0000795
Mark Brown435c5e22008-12-04 15:32:53 +0000796 found = 0;
797 list_for_each_entry(platform, &platform_list, list)
798 if (card->platform == platform) {
799 found = 1;
800 break;
801 }
802 if (!found) {
803 dev_dbg(card->dev, "Platform %s not registered\n",
804 card->platform->name);
805 return;
Mark Brownc5af3a22008-11-28 13:29:45 +0000806 }
807
Mark Brown6b05eda2008-12-08 19:26:48 +0000808 ac97 = 0;
Mark Brown435c5e22008-12-04 15:32:53 +0000809 for (i = 0; i < card->num_links; i++) {
810 found = 0;
811 list_for_each_entry(dai, &dai_list, list)
812 if (card->dai_link[i].cpu_dai == dai) {
813 found = 1;
814 break;
815 }
816 if (!found) {
817 dev_dbg(card->dev, "DAI %s not registered\n",
818 card->dai_link[i].cpu_dai->name);
819 return;
820 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000821
822 if (card->dai_link[i].cpu_dai->ac97_control)
823 ac97 = 1;
Mark Brown435c5e22008-12-04 15:32:53 +0000824 }
825
Mark Brown6b05eda2008-12-08 19:26:48 +0000826 /* If we have AC97 in the system then don't wait for the
827 * codec. This will need revisiting if we have to handle
828 * systems with mixed AC97 and non-AC97 parts. Only check for
829 * DAIs currently; we can't do this per link since some AC97
830 * codecs have non-AC97 DAIs.
831 */
832 if (!ac97)
833 for (i = 0; i < card->num_links; i++) {
834 found = 0;
835 list_for_each_entry(dai, &dai_list, list)
836 if (card->dai_link[i].codec_dai == dai) {
837 found = 1;
838 break;
839 }
840 if (!found) {
841 dev_dbg(card->dev, "DAI %s not registered\n",
842 card->dai_link[i].codec_dai->name);
843 return;
844 }
845 }
846
Mark Brown435c5e22008-12-04 15:32:53 +0000847 /* Note that we do not current check for codec components */
848
849 dev_dbg(card->dev, "All components present, instantiating\n");
850
851 /* Found everything, bring it up */
Mark Brown87506542008-11-18 20:50:34 +0000852 if (card->probe) {
853 ret = card->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200854 if (ret < 0)
Mark Brown435c5e22008-12-04 15:32:53 +0000855 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200856 }
857
Mark Brown87506542008-11-18 20:50:34 +0000858 for (i = 0; i < card->num_links; i++) {
859 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200860 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +0100861 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +0200862 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200863 goto cpu_dai_err;
864 }
865 }
866
867 if (codec_dev->probe) {
868 ret = codec_dev->probe(pdev);
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 if (platform->probe) {
874 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200875 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200876 goto platform_err;
877 }
878
879 /* DAPM stream work */
Mark Brown63084192008-12-02 15:08:03 +0000880 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +0100881#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +0100882 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +0000883 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +0100884#endif
Andy Green6ed25972008-06-13 16:24:05 +0100885
Mark Brown435c5e22008-12-04 15:32:53 +0000886 card->instantiated = 1;
887
888 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200889
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200890platform_err:
891 if (codec_dev->remove)
892 codec_dev->remove(pdev);
893
894cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +0100895 for (i--; i >= 0; i--) {
Mark Brown87506542008-11-18 20:50:34 +0000896 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200897 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100898 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200899 }
900
Mark Brown87506542008-11-18 20:50:34 +0000901 if (card->remove)
902 card->remove(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +0000903}
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200904
Mark Brown435c5e22008-12-04 15:32:53 +0000905/*
906 * Attempt to initialise any uninitalised cards. Must be called with
907 * client_mutex.
908 */
909static void snd_soc_instantiate_cards(void)
910{
911 struct snd_soc_card *card;
912 list_for_each_entry(card, &card_list, list)
913 snd_soc_instantiate_card(card);
914}
915
916/* probes a new socdev */
917static int soc_probe(struct platform_device *pdev)
918{
919 int ret = 0;
920 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
921 struct snd_soc_card *card = socdev->card;
922
923 /* Bodge while we push things out of socdev */
924 card->socdev = socdev;
925
926 /* Bodge while we unpick instantiation */
927 card->dev = &pdev->dev;
928 ret = snd_soc_register_card(card);
929 if (ret != 0) {
930 dev_err(&pdev->dev, "Failed to register card\n");
931 return ret;
932 }
933
934 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200935}
936
937/* removes a socdev */
938static int soc_remove(struct platform_device *pdev)
939{
940 int i;
941 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000942 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000943 struct snd_soc_platform *platform = card->platform;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200944 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
945
Mark Brown63084192008-12-02 15:08:03 +0000946 run_delayed_work(&card->delayed_work);
Liam Girdwood965ac422007-01-31 14:14:57 +0100947
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200948 if (platform->remove)
949 platform->remove(pdev);
950
951 if (codec_dev->remove)
952 codec_dev->remove(pdev);
953
Mark Brown87506542008-11-18 20:50:34 +0000954 for (i = 0; i < card->num_links; i++) {
955 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200956 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100957 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200958 }
959
Mark Brown87506542008-11-18 20:50:34 +0000960 if (card->remove)
961 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200962
Mark Brownc5af3a22008-11-28 13:29:45 +0000963 snd_soc_unregister_card(card);
964
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200965 return 0;
966}
967
968/* ASoC platform driver */
969static struct platform_driver soc_driver = {
970 .driver = {
971 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +0200972 .owner = THIS_MODULE,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200973 },
974 .probe = soc_probe,
975 .remove = soc_remove,
976 .suspend = soc_suspend,
977 .resume = soc_resume,
978};
979
980/* create a new pcm */
981static int soc_new_pcm(struct snd_soc_device *socdev,
982 struct snd_soc_dai_link *dai_link, int num)
983{
984 struct snd_soc_codec *codec = socdev->codec;
Mark Brown87689d52008-12-02 16:01:14 +0000985 struct snd_soc_card *card = socdev->card;
986 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100987 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
988 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200989 struct snd_soc_pcm_runtime *rtd;
990 struct snd_pcm *pcm;
991 char new_name[64];
992 int ret = 0, playback = 0, capture = 0;
993
994 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
995 if (rtd == NULL)
996 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100997
998 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200999 rtd->socdev = socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +01001000 codec_dai->codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001001
1002 /* check client and interface hw capabilities */
Mark Brown3ba9e10a2008-11-24 18:01:05 +00001003 sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
1004 num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001005
1006 if (codec_dai->playback.channels_min)
1007 playback = 1;
1008 if (codec_dai->capture.channels_min)
1009 capture = 1;
1010
1011 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1012 capture, &pcm);
1013 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001014 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1015 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001016 kfree(rtd);
1017 return ret;
1018 }
1019
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001020 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001021 pcm->private_data = rtd;
Mark Brown87689d52008-12-02 16:01:14 +00001022 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1023 soc_pcm_ops.pointer = platform->pcm_ops->pointer;
1024 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1025 soc_pcm_ops.copy = platform->pcm_ops->copy;
1026 soc_pcm_ops.silence = platform->pcm_ops->silence;
1027 soc_pcm_ops.ack = platform->pcm_ops->ack;
1028 soc_pcm_ops.page = platform->pcm_ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001029
1030 if (playback)
1031 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1032
1033 if (capture)
1034 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1035
Mark Brown87689d52008-12-02 16:01:14 +00001036 ret = platform->pcm_new(codec->card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001037 if (ret < 0) {
1038 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1039 kfree(rtd);
1040 return ret;
1041 }
1042
Mark Brown87689d52008-12-02 16:01:14 +00001043 pcm->private_free = platform->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001044 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1045 cpu_dai->name);
1046 return ret;
1047}
1048
1049/* codec register dump */
Troy Kisky12ef1932008-10-13 17:42:14 -07001050static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001051{
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001052 struct snd_soc_codec *codec = devdata->codec;
1053 int i, step = 1, count = 0;
1054
1055 if (!codec->reg_cache_size)
1056 return 0;
1057
1058 if (codec->reg_cache_step)
1059 step = codec->reg_cache_step;
1060
1061 count += sprintf(buf, "%s registers\n", codec->name);
Mark Brown58cd33c2008-07-29 11:42:25 +01001062 for (i = 0; i < codec->reg_cache_size; i += step) {
1063 count += sprintf(buf + count, "%2x: ", i);
1064 if (count >= PAGE_SIZE - 1)
1065 break;
1066
1067 if (codec->display_register)
1068 count += codec->display_register(codec, buf + count,
1069 PAGE_SIZE - count, i);
1070 else
1071 count += snprintf(buf + count, PAGE_SIZE - count,
1072 "%4x", codec->read(codec, i));
1073
1074 if (count >= PAGE_SIZE - 1)
1075 break;
1076
1077 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1078 if (count >= PAGE_SIZE - 1)
1079 break;
1080 }
1081
1082 /* Truncate count; min() would cause a warning */
1083 if (count >= PAGE_SIZE)
1084 count = PAGE_SIZE - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001085
1086 return count;
1087}
Troy Kisky12ef1932008-10-13 17:42:14 -07001088static ssize_t codec_reg_show(struct device *dev,
1089 struct device_attribute *attr, char *buf)
1090{
1091 struct snd_soc_device *devdata = dev_get_drvdata(dev);
1092 return soc_codec_reg_show(devdata, buf);
1093}
1094
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001095static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1096
Troy Kisky12ef1932008-10-13 17:42:14 -07001097#ifdef CONFIG_DEBUG_FS
1098static int codec_reg_open_file(struct inode *inode, struct file *file)
1099{
1100 file->private_data = inode->i_private;
1101 return 0;
1102}
1103
1104static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1105 size_t count, loff_t *ppos)
1106{
1107 ssize_t ret;
Mark Brown384c89e2008-12-03 17:34:03 +00001108 struct snd_soc_codec *codec = file->private_data;
1109 struct device *card_dev = codec->card->dev;
1110 struct snd_soc_device *devdata = card_dev->driver_data;
Troy Kisky12ef1932008-10-13 17:42:14 -07001111 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1112 if (!buf)
1113 return -ENOMEM;
1114 ret = soc_codec_reg_show(devdata, buf);
1115 if (ret >= 0)
1116 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1117 kfree(buf);
1118 return ret;
1119}
1120
1121static ssize_t codec_reg_write_file(struct file *file,
1122 const char __user *user_buf, size_t count, loff_t *ppos)
1123{
1124 char buf[32];
1125 int buf_size;
1126 char *start = buf;
1127 unsigned long reg, value;
1128 int step = 1;
Mark Brown384c89e2008-12-03 17:34:03 +00001129 struct snd_soc_codec *codec = file->private_data;
Troy Kisky12ef1932008-10-13 17:42:14 -07001130
1131 buf_size = min(count, (sizeof(buf)-1));
1132 if (copy_from_user(buf, user_buf, buf_size))
1133 return -EFAULT;
1134 buf[buf_size] = 0;
1135
1136 if (codec->reg_cache_step)
1137 step = codec->reg_cache_step;
1138
1139 while (*start == ' ')
1140 start++;
1141 reg = simple_strtoul(start, &start, 16);
1142 if ((reg >= codec->reg_cache_size) || (reg % step))
1143 return -EINVAL;
1144 while (*start == ' ')
1145 start++;
1146 if (strict_strtoul(start, 16, &value))
1147 return -EINVAL;
1148 codec->write(codec, reg, value);
1149 return buf_size;
1150}
1151
1152static const struct file_operations codec_reg_fops = {
1153 .open = codec_reg_open_file,
1154 .read = codec_reg_read_file,
1155 .write = codec_reg_write_file,
1156};
1157
Mark Brown384c89e2008-12-03 17:34:03 +00001158static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001159{
Mark Brown384c89e2008-12-03 17:34:03 +00001160 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
1161 debugfs_root, codec,
1162 &codec_reg_fops);
1163 if (!codec->debugfs_reg)
1164 printk(KERN_WARNING
1165 "ASoC: Failed to create codec register debugfs file\n");
Troy Kisky12ef1932008-10-13 17:42:14 -07001166
Mark Brown384c89e2008-12-03 17:34:03 +00001167 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
1168 debugfs_root,
1169 &codec->pop_time);
1170 if (!codec->debugfs_pop_time)
1171 printk(KERN_WARNING
1172 "Failed to create pop time debugfs file\n");
Troy Kisky12ef1932008-10-13 17:42:14 -07001173}
1174
Mark Brown384c89e2008-12-03 17:34:03 +00001175static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001176{
Mark Brown384c89e2008-12-03 17:34:03 +00001177 debugfs_remove(codec->debugfs_pop_time);
1178 debugfs_remove(codec->debugfs_reg);
Troy Kisky12ef1932008-10-13 17:42:14 -07001179}
1180
1181#else
1182
Mark Brown384c89e2008-12-03 17:34:03 +00001183static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001184{
1185}
1186
Mark Brown384c89e2008-12-03 17:34:03 +00001187static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001188{
1189}
1190#endif
1191
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001192/**
1193 * snd_soc_new_ac97_codec - initailise AC97 device
1194 * @codec: audio codec
1195 * @ops: AC97 bus operations
1196 * @num: AC97 codec number
1197 *
1198 * Initialises AC97 codec resources for use by ad-hoc devices only.
1199 */
1200int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1201 struct snd_ac97_bus_ops *ops, int num)
1202{
1203 mutex_lock(&codec->mutex);
1204
1205 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1206 if (codec->ac97 == NULL) {
1207 mutex_unlock(&codec->mutex);
1208 return -ENOMEM;
1209 }
1210
1211 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1212 if (codec->ac97->bus == NULL) {
1213 kfree(codec->ac97);
1214 codec->ac97 = NULL;
1215 mutex_unlock(&codec->mutex);
1216 return -ENOMEM;
1217 }
1218
1219 codec->ac97->bus->ops = ops;
1220 codec->ac97->num = num;
1221 mutex_unlock(&codec->mutex);
1222 return 0;
1223}
1224EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1225
1226/**
1227 * snd_soc_free_ac97_codec - free AC97 codec device
1228 * @codec: audio codec
1229 *
1230 * Frees AC97 codec device resources.
1231 */
1232void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1233{
1234 mutex_lock(&codec->mutex);
1235 kfree(codec->ac97->bus);
1236 kfree(codec->ac97);
1237 codec->ac97 = NULL;
1238 mutex_unlock(&codec->mutex);
1239}
1240EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1241
1242/**
1243 * snd_soc_update_bits - update codec register bits
1244 * @codec: audio codec
1245 * @reg: codec register
1246 * @mask: register mask
1247 * @value: new value
1248 *
1249 * Writes new register value.
1250 *
1251 * Returns 1 for change else 0.
1252 */
1253int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1254 unsigned short mask, unsigned short value)
1255{
1256 int change;
1257 unsigned short old, new;
1258
1259 mutex_lock(&io_mutex);
1260 old = snd_soc_read(codec, reg);
1261 new = (old & ~mask) | value;
1262 change = old != new;
1263 if (change)
1264 snd_soc_write(codec, reg, new);
1265
1266 mutex_unlock(&io_mutex);
1267 return change;
1268}
1269EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1270
1271/**
1272 * snd_soc_test_bits - test register for change
1273 * @codec: audio codec
1274 * @reg: codec register
1275 * @mask: register mask
1276 * @value: new value
1277 *
1278 * Tests a register with a new value and checks if the new value is
1279 * different from the old value.
1280 *
1281 * Returns 1 for change else 0.
1282 */
1283int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1284 unsigned short mask, unsigned short value)
1285{
1286 int change;
1287 unsigned short old, new;
1288
1289 mutex_lock(&io_mutex);
1290 old = snd_soc_read(codec, reg);
1291 new = (old & ~mask) | value;
1292 change = old != new;
1293 mutex_unlock(&io_mutex);
1294
1295 return change;
1296}
1297EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1298
1299/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001300 * snd_soc_new_pcms - create new sound card and pcms
1301 * @socdev: the SoC audio device
1302 *
1303 * Create a new sound card based upon the codec and interface pcms.
1304 *
1305 * Returns 0 for success, else error.
1306 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001307int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001308{
1309 struct snd_soc_codec *codec = socdev->codec;
Mark Brown87506542008-11-18 20:50:34 +00001310 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001311 int ret = 0, i;
1312
1313 mutex_lock(&codec->mutex);
1314
1315 /* register a sound card */
1316 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1317 if (!codec->card) {
1318 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1319 codec->name);
1320 mutex_unlock(&codec->mutex);
1321 return -ENODEV;
1322 }
1323
1324 codec->card->dev = socdev->dev;
1325 codec->card->private_data = codec;
1326 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1327
1328 /* create the pcms */
Mark Brown87506542008-11-18 20:50:34 +00001329 for (i = 0; i < card->num_links; i++) {
1330 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001331 if (ret < 0) {
1332 printk(KERN_ERR "asoc: can't create pcm %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001333 card->dai_link[i].stream_name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001334 mutex_unlock(&codec->mutex);
1335 return ret;
1336 }
1337 }
1338
1339 mutex_unlock(&codec->mutex);
1340 return ret;
1341}
1342EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1343
1344/**
Mark Brown968a6022008-11-28 11:49:07 +00001345 * snd_soc_init_card - register sound card
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001346 * @socdev: the SoC audio device
1347 *
1348 * Register a SoC sound card. Also registers an AC97 device if the
1349 * codec is AC97 for ad hoc devices.
1350 *
1351 * Returns 0 for success, else error.
1352 */
Mark Brown968a6022008-11-28 11:49:07 +00001353int snd_soc_init_card(struct snd_soc_device *socdev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001354{
1355 struct snd_soc_codec *codec = socdev->codec;
Mark Brown87506542008-11-18 20:50:34 +00001356 struct snd_soc_card *card = socdev->card;
Liam Girdwood12e74f72006-10-16 21:19:48 +02001357 int ret = 0, i, ac97 = 0, err = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001358
Mark Brown87506542008-11-18 20:50:34 +00001359 for (i = 0; i < card->num_links; i++) {
1360 if (card->dai_link[i].init) {
1361 err = card->dai_link[i].init(codec);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001362 if (err < 0) {
1363 printk(KERN_ERR "asoc: failed to init %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001364 card->dai_link[i].stream_name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001365 continue;
1366 }
1367 }
Mark Brown3ba9e10a2008-11-24 18:01:05 +00001368 if (card->dai_link[i].codec_dai->ac97_control)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001369 ac97 = 1;
1370 }
1371 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
Mark Brown87506542008-11-18 20:50:34 +00001372 "%s", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001373 snprintf(codec->card->longname, sizeof(codec->card->longname),
Mark Brown87506542008-11-18 20:50:34 +00001374 "%s (%s)", card->name, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001375
1376 ret = snd_card_register(codec->card);
1377 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001378 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001379 codec->name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001380 goto out;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001381 }
1382
Mark Brown08c8efe2008-01-21 14:33:37 +01001383 mutex_lock(&codec->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001384#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood12e74f72006-10-16 21:19:48 +02001385 if (ac97) {
1386 ret = soc_ac97_dev_register(codec);
1387 if (ret < 0) {
1388 printk(KERN_ERR "asoc: AC97 device register failed\n");
1389 snd_card_free(codec->card);
Mark Brown08c8efe2008-01-21 14:33:37 +01001390 mutex_unlock(&codec->mutex);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001391 goto out;
1392 }
1393 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001394#endif
1395
Liam Girdwood12e74f72006-10-16 21:19:48 +02001396 err = snd_soc_dapm_sys_add(socdev->dev);
1397 if (err < 0)
1398 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1399
1400 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1401 if (err < 0)
Mark Brown3ff3f642008-05-19 12:32:25 +02001402 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
Mark Brown08c8efe2008-01-21 14:33:37 +01001403
Mark Brown384c89e2008-12-03 17:34:03 +00001404 soc_init_codec_debugfs(socdev->codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001405 mutex_unlock(&codec->mutex);
Mark Brown08c8efe2008-01-21 14:33:37 +01001406
1407out:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001408 return ret;
1409}
Mark Brown968a6022008-11-28 11:49:07 +00001410EXPORT_SYMBOL_GPL(snd_soc_init_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001411
1412/**
1413 * snd_soc_free_pcms - free sound card and pcms
1414 * @socdev: the SoC audio device
1415 *
1416 * Frees sound card and pcms associated with the socdev.
1417 * Also unregister the codec if it is an AC97 device.
1418 */
1419void snd_soc_free_pcms(struct snd_soc_device *socdev)
1420{
1421 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001422#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001423 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001424 int i;
1425#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001426
1427 mutex_lock(&codec->mutex);
Mark Brown384c89e2008-12-03 17:34:03 +00001428 soc_cleanup_codec_debugfs(socdev->codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001429#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001430 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001431 codec_dai = &codec->dai[i];
Mark Brown3ba9e10a2008-11-24 18:01:05 +00001432 if (codec_dai->ac97_control && codec->ac97) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001433 soc_ac97_dev_unregister(codec);
1434 goto free_card;
1435 }
1436 }
1437free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001438#endif
1439
1440 if (codec->card)
1441 snd_card_free(codec->card);
1442 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1443 mutex_unlock(&codec->mutex);
1444}
1445EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1446
1447/**
1448 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1449 * @substream: the pcm substream
1450 * @hw: the hardware parameters
1451 *
1452 * Sets the substream runtime hardware parameters.
1453 */
1454int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1455 const struct snd_pcm_hardware *hw)
1456{
1457 struct snd_pcm_runtime *runtime = substream->runtime;
1458 runtime->hw.info = hw->info;
1459 runtime->hw.formats = hw->formats;
1460 runtime->hw.period_bytes_min = hw->period_bytes_min;
1461 runtime->hw.period_bytes_max = hw->period_bytes_max;
1462 runtime->hw.periods_min = hw->periods_min;
1463 runtime->hw.periods_max = hw->periods_max;
1464 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1465 runtime->hw.fifo_size = hw->fifo_size;
1466 return 0;
1467}
1468EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1469
1470/**
1471 * snd_soc_cnew - create new control
1472 * @_template: control template
1473 * @data: control private data
1474 * @lnng_name: control long name
1475 *
1476 * Create a new mixer control from a template control.
1477 *
1478 * Returns 0 for success, else error.
1479 */
1480struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1481 void *data, char *long_name)
1482{
1483 struct snd_kcontrol_new template;
1484
1485 memcpy(&template, _template, sizeof(template));
1486 if (long_name)
1487 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001488 template.index = 0;
1489
1490 return snd_ctl_new1(&template, data);
1491}
1492EXPORT_SYMBOL_GPL(snd_soc_cnew);
1493
1494/**
1495 * snd_soc_info_enum_double - enumerated double mixer info callback
1496 * @kcontrol: mixer control
1497 * @uinfo: control element information
1498 *
1499 * Callback to provide information about a double enumerated
1500 * mixer control.
1501 *
1502 * Returns 0 for success.
1503 */
1504int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1505 struct snd_ctl_elem_info *uinfo)
1506{
1507 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1508
1509 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1510 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001511 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001512
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001513 if (uinfo->value.enumerated.item > e->max - 1)
1514 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001515 strcpy(uinfo->value.enumerated.name,
1516 e->texts[uinfo->value.enumerated.item]);
1517 return 0;
1518}
1519EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1520
1521/**
1522 * snd_soc_get_enum_double - enumerated double mixer get callback
1523 * @kcontrol: mixer control
1524 * @uinfo: control element information
1525 *
1526 * Callback to get the value of a double enumerated mixer.
1527 *
1528 * Returns 0 for success.
1529 */
1530int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1531 struct snd_ctl_elem_value *ucontrol)
1532{
1533 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1534 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1535 unsigned short val, bitmask;
1536
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001537 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001538 ;
1539 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001540 ucontrol->value.enumerated.item[0]
1541 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001542 if (e->shift_l != e->shift_r)
1543 ucontrol->value.enumerated.item[1] =
1544 (val >> e->shift_r) & (bitmask - 1);
1545
1546 return 0;
1547}
1548EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1549
1550/**
1551 * snd_soc_put_enum_double - enumerated double mixer put callback
1552 * @kcontrol: mixer control
1553 * @uinfo: control element information
1554 *
1555 * Callback to set the value of a double enumerated mixer.
1556 *
1557 * Returns 0 for success.
1558 */
1559int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1560 struct snd_ctl_elem_value *ucontrol)
1561{
1562 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1563 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1564 unsigned short val;
1565 unsigned short mask, bitmask;
1566
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001567 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001568 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001569 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001570 return -EINVAL;
1571 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1572 mask = (bitmask - 1) << e->shift_l;
1573 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001574 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001575 return -EINVAL;
1576 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1577 mask |= (bitmask - 1) << e->shift_r;
1578 }
1579
1580 return snd_soc_update_bits(codec, e->reg, mask, val);
1581}
1582EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1583
1584/**
1585 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1586 * @kcontrol: mixer control
1587 * @uinfo: control element information
1588 *
1589 * Callback to provide information about an external enumerated
1590 * single mixer.
1591 *
1592 * Returns 0 for success.
1593 */
1594int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1595 struct snd_ctl_elem_info *uinfo)
1596{
1597 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1598
1599 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1600 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001601 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001602
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001603 if (uinfo->value.enumerated.item > e->max - 1)
1604 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001605 strcpy(uinfo->value.enumerated.name,
1606 e->texts[uinfo->value.enumerated.item]);
1607 return 0;
1608}
1609EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1610
1611/**
1612 * snd_soc_info_volsw_ext - external single mixer info callback
1613 * @kcontrol: mixer control
1614 * @uinfo: control element information
1615 *
1616 * Callback to provide information about a single external mixer control.
1617 *
1618 * Returns 0 for success.
1619 */
1620int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1621 struct snd_ctl_elem_info *uinfo)
1622{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001623 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001624
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001625 if (max == 1)
1626 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1627 else
1628 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1629
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001630 uinfo->count = 1;
1631 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001632 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001633 return 0;
1634}
1635EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1636
1637/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001638 * snd_soc_info_volsw - single mixer info callback
1639 * @kcontrol: mixer control
1640 * @uinfo: control element information
1641 *
1642 * Callback to provide information about a single mixer control.
1643 *
1644 * Returns 0 for success.
1645 */
1646int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1647 struct snd_ctl_elem_info *uinfo)
1648{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001649 struct soc_mixer_control *mc =
1650 (struct soc_mixer_control *)kcontrol->private_value;
1651 int max = mc->max;
Mark Brown762b8df2008-10-30 12:37:08 +00001652 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001653 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001654
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001655 if (max == 1)
1656 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1657 else
1658 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1659
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001660 uinfo->count = shift == rshift ? 1 : 2;
1661 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001662 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001663 return 0;
1664}
1665EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1666
1667/**
1668 * snd_soc_get_volsw - single mixer get callback
1669 * @kcontrol: mixer control
1670 * @uinfo: control element information
1671 *
1672 * Callback to get the value of a single mixer control.
1673 *
1674 * Returns 0 for success.
1675 */
1676int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1677 struct snd_ctl_elem_value *ucontrol)
1678{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001679 struct soc_mixer_control *mc =
1680 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001681 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001682 unsigned int reg = mc->reg;
1683 unsigned int shift = mc->shift;
1684 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001685 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001686 unsigned int mask = (1 << fls(max)) - 1;
1687 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001688
1689 ucontrol->value.integer.value[0] =
1690 (snd_soc_read(codec, reg) >> shift) & mask;
1691 if (shift != rshift)
1692 ucontrol->value.integer.value[1] =
1693 (snd_soc_read(codec, reg) >> rshift) & mask;
1694 if (invert) {
1695 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001696 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001697 if (shift != rshift)
1698 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001699 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001700 }
1701
1702 return 0;
1703}
1704EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1705
1706/**
1707 * snd_soc_put_volsw - single mixer put callback
1708 * @kcontrol: mixer control
1709 * @uinfo: control element information
1710 *
1711 * Callback to set the value of a single mixer control.
1712 *
1713 * Returns 0 for success.
1714 */
1715int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1716 struct snd_ctl_elem_value *ucontrol)
1717{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001718 struct soc_mixer_control *mc =
1719 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001720 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001721 unsigned int reg = mc->reg;
1722 unsigned int shift = mc->shift;
1723 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001724 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001725 unsigned int mask = (1 << fls(max)) - 1;
1726 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001727 unsigned short val, val2, val_mask;
1728
1729 val = (ucontrol->value.integer.value[0] & mask);
1730 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001731 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001732 val_mask = mask << shift;
1733 val = val << shift;
1734 if (shift != rshift) {
1735 val2 = (ucontrol->value.integer.value[1] & mask);
1736 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001737 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001738 val_mask |= mask << rshift;
1739 val |= val2 << rshift;
1740 }
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001741 return snd_soc_update_bits(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001742}
1743EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1744
1745/**
1746 * snd_soc_info_volsw_2r - double mixer info callback
1747 * @kcontrol: mixer control
1748 * @uinfo: control element information
1749 *
1750 * Callback to provide information about a double mixer control that
1751 * spans 2 codec registers.
1752 *
1753 * Returns 0 for success.
1754 */
1755int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1756 struct snd_ctl_elem_info *uinfo)
1757{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001758 struct soc_mixer_control *mc =
1759 (struct soc_mixer_control *)kcontrol->private_value;
1760 int max = mc->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001761
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001762 if (max == 1)
1763 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1764 else
1765 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1766
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001767 uinfo->count = 2;
1768 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001769 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001770 return 0;
1771}
1772EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1773
1774/**
1775 * snd_soc_get_volsw_2r - double mixer get callback
1776 * @kcontrol: mixer control
1777 * @uinfo: control element information
1778 *
1779 * Callback to get the value of a double mixer control that spans 2 registers.
1780 *
1781 * Returns 0 for success.
1782 */
1783int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1784 struct snd_ctl_elem_value *ucontrol)
1785{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001786 struct soc_mixer_control *mc =
1787 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001788 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001789 unsigned int reg = mc->reg;
1790 unsigned int reg2 = mc->rreg;
1791 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001792 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001793 unsigned int mask = (1<<fls(max))-1;
1794 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001795
1796 ucontrol->value.integer.value[0] =
1797 (snd_soc_read(codec, reg) >> shift) & mask;
1798 ucontrol->value.integer.value[1] =
1799 (snd_soc_read(codec, reg2) >> shift) & mask;
1800 if (invert) {
1801 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001802 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001803 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001804 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001805 }
1806
1807 return 0;
1808}
1809EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1810
1811/**
1812 * snd_soc_put_volsw_2r - double mixer set callback
1813 * @kcontrol: mixer control
1814 * @uinfo: control element information
1815 *
1816 * Callback to set the value of a double mixer control that spans 2 registers.
1817 *
1818 * Returns 0 for success.
1819 */
1820int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1821 struct snd_ctl_elem_value *ucontrol)
1822{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001823 struct soc_mixer_control *mc =
1824 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001825 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001826 unsigned int reg = mc->reg;
1827 unsigned int reg2 = mc->rreg;
1828 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001829 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001830 unsigned int mask = (1 << fls(max)) - 1;
1831 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001832 int err;
1833 unsigned short val, val2, val_mask;
1834
1835 val_mask = mask << shift;
1836 val = (ucontrol->value.integer.value[0] & mask);
1837 val2 = (ucontrol->value.integer.value[1] & mask);
1838
1839 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001840 val = max - val;
1841 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001842 }
1843
1844 val = val << shift;
1845 val2 = val2 << shift;
1846
Mark Brown3ff3f642008-05-19 12:32:25 +02001847 err = snd_soc_update_bits(codec, reg, val_mask, val);
1848 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001849 return err;
1850
1851 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1852 return err;
1853}
1854EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1855
Mark Browne13ac2e2008-05-28 17:58:05 +01001856/**
1857 * snd_soc_info_volsw_s8 - signed mixer info callback
1858 * @kcontrol: mixer control
1859 * @uinfo: control element information
1860 *
1861 * Callback to provide information about a signed mixer control.
1862 *
1863 * Returns 0 for success.
1864 */
1865int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1866 struct snd_ctl_elem_info *uinfo)
1867{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001868 struct soc_mixer_control *mc =
1869 (struct soc_mixer_control *)kcontrol->private_value;
1870 int max = mc->max;
1871 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001872
1873 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1874 uinfo->count = 2;
1875 uinfo->value.integer.min = 0;
1876 uinfo->value.integer.max = max-min;
1877 return 0;
1878}
1879EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1880
1881/**
1882 * snd_soc_get_volsw_s8 - signed mixer get callback
1883 * @kcontrol: mixer control
1884 * @uinfo: control element information
1885 *
1886 * Callback to get the value of a signed mixer control.
1887 *
1888 * Returns 0 for success.
1889 */
1890int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1891 struct snd_ctl_elem_value *ucontrol)
1892{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001893 struct soc_mixer_control *mc =
1894 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01001895 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001896 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001897 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001898 int val = snd_soc_read(codec, reg);
1899
1900 ucontrol->value.integer.value[0] =
1901 ((signed char)(val & 0xff))-min;
1902 ucontrol->value.integer.value[1] =
1903 ((signed char)((val >> 8) & 0xff))-min;
1904 return 0;
1905}
1906EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1907
1908/**
1909 * snd_soc_put_volsw_sgn - signed mixer put callback
1910 * @kcontrol: mixer control
1911 * @uinfo: control element information
1912 *
1913 * Callback to set the value of a signed mixer control.
1914 *
1915 * Returns 0 for success.
1916 */
1917int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1918 struct snd_ctl_elem_value *ucontrol)
1919{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001920 struct soc_mixer_control *mc =
1921 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01001922 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04001923 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001924 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001925 unsigned short val;
1926
1927 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1928 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1929
1930 return snd_soc_update_bits(codec, reg, 0xffff, val);
1931}
1932EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1933
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001934/**
1935 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1936 * @dai: DAI
1937 * @clk_id: DAI specific clock ID
1938 * @freq: new clock frequency in Hz
1939 * @dir: new clock direction - input/output.
1940 *
1941 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1942 */
1943int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1944 unsigned int freq, int dir)
1945{
Mark Browndee89c42008-11-18 22:11:38 +00001946 if (dai->ops.set_sysclk)
1947 return dai->ops.set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001948 else
1949 return -EINVAL;
1950}
1951EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1952
1953/**
1954 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1955 * @dai: DAI
1956 * @clk_id: DAI specific clock divider ID
1957 * @div: new clock divisor.
1958 *
1959 * Configures the clock dividers. This is used to derive the best DAI bit and
1960 * frame clocks from the system or master clock. It's best to set the DAI bit
1961 * and frame clocks as low as possible to save system power.
1962 */
1963int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
1964 int div_id, int div)
1965{
Mark Browndee89c42008-11-18 22:11:38 +00001966 if (dai->ops.set_clkdiv)
1967 return dai->ops.set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001968 else
1969 return -EINVAL;
1970}
1971EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
1972
1973/**
1974 * snd_soc_dai_set_pll - configure DAI PLL.
1975 * @dai: DAI
1976 * @pll_id: DAI specific PLL ID
1977 * @freq_in: PLL input clock frequency in Hz
1978 * @freq_out: requested PLL output clock frequency in Hz
1979 *
1980 * Configures and enables PLL to generate output clock based on input clock.
1981 */
1982int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
1983 int pll_id, unsigned int freq_in, unsigned int freq_out)
1984{
Mark Browndee89c42008-11-18 22:11:38 +00001985 if (dai->ops.set_pll)
1986 return dai->ops.set_pll(dai, pll_id, freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001987 else
1988 return -EINVAL;
1989}
1990EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
1991
1992/**
1993 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1994 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001995 * @fmt: SND_SOC_DAIFMT_ format value.
1996 *
1997 * Configures the DAI hardware format and clocking.
1998 */
1999int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2000{
Mark Browndee89c42008-11-18 22:11:38 +00002001 if (dai->ops.set_fmt)
2002 return dai->ops.set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002003 else
2004 return -EINVAL;
2005}
2006EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2007
2008/**
2009 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2010 * @dai: DAI
2011 * @mask: DAI specific mask representing used slots.
2012 * @slots: Number of slots in use.
2013 *
2014 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2015 * specific.
2016 */
2017int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2018 unsigned int mask, int slots)
2019{
Mark Browndee89c42008-11-18 22:11:38 +00002020 if (dai->ops.set_sysclk)
2021 return dai->ops.set_tdm_slot(dai, mask, slots);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002022 else
2023 return -EINVAL;
2024}
2025EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2026
2027/**
2028 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2029 * @dai: DAI
2030 * @tristate: tristate enable
2031 *
2032 * Tristates the DAI so that others can use it.
2033 */
2034int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2035{
Mark Browndee89c42008-11-18 22:11:38 +00002036 if (dai->ops.set_sysclk)
2037 return dai->ops.set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002038 else
2039 return -EINVAL;
2040}
2041EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2042
2043/**
2044 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2045 * @dai: DAI
2046 * @mute: mute enable
2047 *
2048 * Mutes the DAI DAC.
2049 */
2050int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2051{
Mark Browndee89c42008-11-18 22:11:38 +00002052 if (dai->ops.digital_mute)
2053 return dai->ops.digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002054 else
2055 return -EINVAL;
2056}
2057EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2058
Mark Brownc5af3a22008-11-28 13:29:45 +00002059/**
2060 * snd_soc_register_card - Register a card with the ASoC core
2061 *
2062 * @param card Card to register
2063 *
2064 * Note that currently this is an internal only function: it will be
2065 * exposed to machine drivers after further backporting of ASoC v2
2066 * registration APIs.
2067 */
2068static int snd_soc_register_card(struct snd_soc_card *card)
2069{
2070 if (!card->name || !card->dev)
2071 return -EINVAL;
2072
2073 INIT_LIST_HEAD(&card->list);
2074 card->instantiated = 0;
2075
2076 mutex_lock(&client_mutex);
2077 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002078 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002079 mutex_unlock(&client_mutex);
2080
2081 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2082
2083 return 0;
2084}
2085
2086/**
2087 * snd_soc_unregister_card - Unregister a card with the ASoC core
2088 *
2089 * @param card Card to unregister
2090 *
2091 * Note that currently this is an internal only function: it will be
2092 * exposed to machine drivers after further backporting of ASoC v2
2093 * registration APIs.
2094 */
2095static int snd_soc_unregister_card(struct snd_soc_card *card)
2096{
2097 mutex_lock(&client_mutex);
2098 list_del(&card->list);
2099 mutex_unlock(&client_mutex);
2100
2101 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2102
2103 return 0;
2104}
2105
Mark Brown91151712008-11-30 23:31:24 +00002106/**
2107 * snd_soc_register_dai - Register a DAI with the ASoC core
2108 *
2109 * @param dai DAI to register
2110 */
2111int snd_soc_register_dai(struct snd_soc_dai *dai)
2112{
2113 if (!dai->name)
2114 return -EINVAL;
2115
2116 /* The device should become mandatory over time */
2117 if (!dai->dev)
2118 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2119
2120 INIT_LIST_HEAD(&dai->list);
2121
2122 mutex_lock(&client_mutex);
2123 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002124 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002125 mutex_unlock(&client_mutex);
2126
2127 pr_debug("Registered DAI '%s'\n", dai->name);
2128
2129 return 0;
2130}
2131EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2132
2133/**
2134 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2135 *
2136 * @param dai DAI to unregister
2137 */
2138void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2139{
2140 mutex_lock(&client_mutex);
2141 list_del(&dai->list);
2142 mutex_unlock(&client_mutex);
2143
2144 pr_debug("Unregistered DAI '%s'\n", dai->name);
2145}
2146EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2147
2148/**
2149 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2150 *
2151 * @param dai Array of DAIs to register
2152 * @param count Number of DAIs
2153 */
2154int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2155{
2156 int i, ret;
2157
2158 for (i = 0; i < count; i++) {
2159 ret = snd_soc_register_dai(&dai[i]);
2160 if (ret != 0)
2161 goto err;
2162 }
2163
2164 return 0;
2165
2166err:
2167 for (i--; i >= 0; i--)
2168 snd_soc_unregister_dai(&dai[i]);
2169
2170 return ret;
2171}
2172EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2173
2174/**
2175 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2176 *
2177 * @param dai Array of DAIs to unregister
2178 * @param count Number of DAIs
2179 */
2180void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2181{
2182 int i;
2183
2184 for (i = 0; i < count; i++)
2185 snd_soc_unregister_dai(&dai[i]);
2186}
2187EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2188
Mark Brown12a48a8c2008-12-03 19:40:30 +00002189/**
2190 * snd_soc_register_platform - Register a platform with the ASoC core
2191 *
2192 * @param platform platform to register
2193 */
2194int snd_soc_register_platform(struct snd_soc_platform *platform)
2195{
2196 if (!platform->name)
2197 return -EINVAL;
2198
2199 INIT_LIST_HEAD(&platform->list);
2200
2201 mutex_lock(&client_mutex);
2202 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002203 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00002204 mutex_unlock(&client_mutex);
2205
2206 pr_debug("Registered platform '%s'\n", platform->name);
2207
2208 return 0;
2209}
2210EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2211
2212/**
2213 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2214 *
2215 * @param platform platform to unregister
2216 */
2217void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2218{
2219 mutex_lock(&client_mutex);
2220 list_del(&platform->list);
2221 mutex_unlock(&client_mutex);
2222
2223 pr_debug("Unregistered platform '%s'\n", platform->name);
2224}
2225EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2226
Takashi Iwaic9b3a402008-12-10 07:47:22 +01002227static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002228{
Mark Brown384c89e2008-12-03 17:34:03 +00002229#ifdef CONFIG_DEBUG_FS
2230 debugfs_root = debugfs_create_dir("asoc", NULL);
2231 if (IS_ERR(debugfs_root) || !debugfs_root) {
2232 printk(KERN_WARNING
2233 "ASoC: Failed to create debugfs directory\n");
2234 debugfs_root = NULL;
2235 }
2236#endif
2237
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002238 return platform_driver_register(&soc_driver);
2239}
2240
Mark Brown7d8c16a2008-11-30 22:11:24 +00002241static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002242{
Mark Brown384c89e2008-12-03 17:34:03 +00002243#ifdef CONFIG_DEBUG_FS
2244 debugfs_remove_recursive(debugfs_root);
2245#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02002246 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002247}
2248
2249module_init(snd_soc_init);
2250module_exit(snd_soc_exit);
2251
2252/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01002253MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002254MODULE_DESCRIPTION("ALSA SoC Core");
2255MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02002256MODULE_ALIAS("platform:soc-audio");