blob: f83852f11463c136337fff7e727134c2abbf9147 [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
Frank Mandarinodb2a4162006-10-06 18:31:09 +020046/*
47 * This is a timeout to do a DAPM powerdown after a stream is closed().
48 * It can be used to eliminate pops between different playback streams, e.g.
49 * between two audio tracks.
50 */
51static int pmdown_time = 5000;
52module_param(pmdown_time, int, 0);
53MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
54
Liam Girdwood965ac422007-01-31 14:14:57 +010055/*
56 * This function forces any delayed work to be queued and run.
57 */
58static int run_delayed_work(struct delayed_work *dwork)
59{
60 int ret;
61
62 /* cancel any work waiting to be queued. */
63 ret = cancel_delayed_work(dwork);
64
65 /* if there was any work waiting then we run it now and
66 * wait for it's completion */
67 if (ret) {
68 schedule_delayed_work(dwork, 0);
69 flush_scheduled_work();
70 }
71 return ret;
72}
73
Frank Mandarinodb2a4162006-10-06 18:31:09 +020074#ifdef CONFIG_SND_SOC_AC97_BUS
75/* unregister ac97 codec */
76static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
77{
78 if (codec->ac97->dev.bus)
79 device_unregister(&codec->ac97->dev);
80 return 0;
81}
82
83/* stop no dev release warning */
84static void soc_ac97_device_release(struct device *dev){}
85
86/* register ac97 codec to bus */
87static int soc_ac97_dev_register(struct snd_soc_codec *codec)
88{
89 int err;
90
91 codec->ac97->dev.bus = &ac97_bus_type;
92 codec->ac97->dev.parent = NULL;
93 codec->ac97->dev.release = soc_ac97_device_release;
94
Kay Sieversbb072bf2008-11-02 03:50:35 +010095 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
96 codec->card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020097 err = device_register(&codec->ac97->dev);
98 if (err < 0) {
99 snd_printk(KERN_ERR "Can't register ac97 bus\n");
100 codec->ac97->dev.bus = NULL;
101 return err;
102 }
103 return 0;
104}
105#endif
106
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200107/*
108 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
109 * then initialized and any private data can be allocated. This also calls
110 * startup for the cpu DAI, platform, machine and codec DAI.
111 */
112static int soc_pcm_open(struct snd_pcm_substream *substream)
113{
114 struct snd_soc_pcm_runtime *rtd = substream->private_data;
115 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000116 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200117 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100118 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000119 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100120 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
121 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200122 int ret = 0;
123
124 mutex_lock(&pcm_mutex);
125
126 /* startup the audio subsystem */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100127 if (cpu_dai->ops.startup) {
Mark Browndee89c42008-11-18 22:11:38 +0000128 ret = cpu_dai->ops.startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200129 if (ret < 0) {
130 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100131 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200132 goto out;
133 }
134 }
135
136 if (platform->pcm_ops->open) {
137 ret = platform->pcm_ops->open(substream);
138 if (ret < 0) {
139 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
140 goto platform_err;
141 }
142 }
143
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100144 if (codec_dai->ops.startup) {
Mark Browndee89c42008-11-18 22:11:38 +0000145 ret = codec_dai->ops.startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100146 if (ret < 0) {
147 printk(KERN_ERR "asoc: can't open codec %s\n",
148 codec_dai->name);
149 goto codec_dai_err;
150 }
151 }
152
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200153 if (machine->ops && machine->ops->startup) {
154 ret = machine->ops->startup(substream);
155 if (ret < 0) {
156 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
157 goto machine_err;
158 }
159 }
160
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200161 /* Check that the codec and cpu DAI's are compatible */
162 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
163 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200164 max(codec_dai->playback.rate_min,
165 cpu_dai->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200166 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200167 min(codec_dai->playback.rate_max,
168 cpu_dai->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200169 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100170 max(codec_dai->playback.channels_min,
171 cpu_dai->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200172 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100173 min(codec_dai->playback.channels_max,
174 cpu_dai->playback.channels_max);
175 runtime->hw.formats =
176 codec_dai->playback.formats & cpu_dai->playback.formats;
177 runtime->hw.rates =
178 codec_dai->playback.rates & cpu_dai->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200179 } else {
180 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200181 max(codec_dai->capture.rate_min,
182 cpu_dai->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200183 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200184 min(codec_dai->capture.rate_max,
185 cpu_dai->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200186 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100187 max(codec_dai->capture.channels_min,
188 cpu_dai->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200189 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100190 min(codec_dai->capture.channels_max,
191 cpu_dai->capture.channels_max);
192 runtime->hw.formats =
193 codec_dai->capture.formats & cpu_dai->capture.formats;
194 runtime->hw.rates =
195 codec_dai->capture.rates & cpu_dai->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200196 }
197
198 snd_pcm_limit_hw_rates(runtime);
199 if (!runtime->hw.rates) {
200 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100201 codec_dai->name, cpu_dai->name);
202 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200203 }
204 if (!runtime->hw.formats) {
205 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100206 codec_dai->name, cpu_dai->name);
207 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200208 }
209 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
210 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100211 codec_dai->name, cpu_dai->name);
212 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200213 }
214
Mark Brownf24368c2008-10-21 21:45:08 +0100215 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
216 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
217 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
218 runtime->hw.channels_max);
219 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
220 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200221
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200222 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100223 cpu_dai->playback.active = codec_dai->playback.active = 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200224 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100225 cpu_dai->capture.active = codec_dai->capture.active = 1;
226 cpu_dai->active = codec_dai->active = 1;
227 cpu_dai->runtime = runtime;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200228 socdev->codec->active++;
229 mutex_unlock(&pcm_mutex);
230 return 0;
231
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100232machine_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200233 if (machine->ops && machine->ops->shutdown)
234 machine->ops->shutdown(substream);
235
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100236codec_dai_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200237 if (platform->pcm_ops->close)
238 platform->pcm_ops->close(substream);
239
240platform_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100241 if (cpu_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000242 cpu_dai->ops.shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200243out:
244 mutex_unlock(&pcm_mutex);
245 return ret;
246}
247
248/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200249 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200250 * This is to ensure there are no pops or clicks in between any music tracks
251 * due to DAPM power cycling.
252 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100253static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200254{
Mark Brown63084192008-12-02 15:08:03 +0000255 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
256 delayed_work.work);
257 struct snd_soc_device *socdev = card->socdev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200258 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100259 struct snd_soc_dai *codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200260 int i;
261
262 mutex_lock(&pcm_mutex);
Mark Brown3ff3f642008-05-19 12:32:25 +0200263 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200264 codec_dai = &codec->dai[i];
265
Mark Brownf24368c2008-10-21 21:45:08 +0100266 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
267 codec_dai->playback.stream_name,
268 codec_dai->playback.active ? "active" : "inactive",
269 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200270
271 /* are we waiting on this codec DAI stream */
272 if (codec_dai->pop_wait == 1) {
273
Mark Brown0be98982008-05-19 12:31:28 +0200274 /* Reduce power if no longer active */
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100275 if (codec->active == 0) {
Mark Brownf24368c2008-10-21 21:45:08 +0100276 pr_debug("pop wq D1 %s %s\n", codec->name,
277 codec_dai->playback.stream_name);
Mark Brown0be98982008-05-19 12:31:28 +0200278 snd_soc_dapm_set_bias_level(socdev,
279 SND_SOC_BIAS_PREPARE);
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100280 }
281
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200282 codec_dai->pop_wait = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100283 snd_soc_dapm_stream_event(codec,
284 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200285 SND_SOC_DAPM_STREAM_STOP);
286
Mark Brown0be98982008-05-19 12:31:28 +0200287 /* Fall into standby if no longer active */
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200288 if (codec->active == 0) {
Mark Brownf24368c2008-10-21 21:45:08 +0100289 pr_debug("pop wq D3 %s %s\n", codec->name,
290 codec_dai->playback.stream_name);
Mark Brown0be98982008-05-19 12:31:28 +0200291 snd_soc_dapm_set_bias_level(socdev,
292 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200293 }
294 }
295 }
296 mutex_unlock(&pcm_mutex);
297}
298
299/*
300 * Called by ALSA when a PCM substream is closed. Private data can be
301 * freed here. The cpu DAI, codec DAI, machine and platform are also
302 * shutdown.
303 */
304static int soc_codec_close(struct snd_pcm_substream *substream)
305{
306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
307 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000308 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100309 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000310 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100311 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
312 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200313 struct snd_soc_codec *codec = socdev->codec;
314
315 mutex_lock(&pcm_mutex);
316
317 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100318 cpu_dai->playback.active = codec_dai->playback.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200319 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100320 cpu_dai->capture.active = codec_dai->capture.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200321
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100322 if (codec_dai->playback.active == 0 &&
323 codec_dai->capture.active == 0) {
324 cpu_dai->active = codec_dai->active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200325 }
326 codec->active--;
327
Mark Brown6010b2d2008-09-06 18:33:24 +0100328 /* Muting the DAC suppresses artifacts caused during digital
329 * shutdown, for example from stopping clocks.
330 */
331 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
332 snd_soc_dai_digital_mute(codec_dai, 1);
333
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100334 if (cpu_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000335 cpu_dai->ops.shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200336
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100337 if (codec_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000338 codec_dai->ops.shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200339
340 if (machine->ops && machine->ops->shutdown)
341 machine->ops->shutdown(substream);
342
343 if (platform->pcm_ops->close)
344 platform->pcm_ops->close(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100345 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200346
347 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
348 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100349 codec_dai->pop_wait = 1;
Mark Brown63084192008-12-02 15:08:03 +0000350 schedule_delayed_work(&card->delayed_work,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200351 msecs_to_jiffies(pmdown_time));
352 } else {
353 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100354 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100355 codec_dai->capture.stream_name,
356 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200357
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100358 if (codec->active == 0 && codec_dai->pop_wait == 0)
Mark Brown0be98982008-05-19 12:31:28 +0200359 snd_soc_dapm_set_bias_level(socdev,
360 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200361 }
362
363 mutex_unlock(&pcm_mutex);
364 return 0;
365}
366
367/*
368 * Called by ALSA when the PCM substream is prepared, can set format, sample
369 * rate, etc. This function is non atomic and can be called multiple times,
370 * it can refer to the runtime info.
371 */
372static int soc_pcm_prepare(struct snd_pcm_substream *substream)
373{
374 struct snd_soc_pcm_runtime *rtd = substream->private_data;
375 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown63084192008-12-02 15:08:03 +0000376 struct snd_soc_card *card = socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100377 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000378 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100379 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
380 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200381 struct snd_soc_codec *codec = socdev->codec;
382 int ret = 0;
383
384 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100385
386 if (machine->ops && machine->ops->prepare) {
387 ret = machine->ops->prepare(substream);
388 if (ret < 0) {
389 printk(KERN_ERR "asoc: machine prepare error\n");
390 goto out;
391 }
392 }
393
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200394 if (platform->pcm_ops->prepare) {
395 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200396 if (ret < 0) {
397 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200398 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200399 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200400 }
401
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100402 if (codec_dai->ops.prepare) {
Mark Browndee89c42008-11-18 22:11:38 +0000403 ret = codec_dai->ops.prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200404 if (ret < 0) {
405 printk(KERN_ERR "asoc: codec DAI 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 (cpu_dai->ops.prepare) {
Mark Browndee89c42008-11-18 22:11:38 +0000411 ret = cpu_dai->ops.prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100412 if (ret < 0) {
413 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
414 goto out;
415 }
416 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200417
Mark Brownd45f6212008-10-14 13:58:36 +0100418 /* cancel any delayed stream shutdown that is pending */
419 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
420 codec_dai->pop_wait) {
421 codec_dai->pop_wait = 0;
Mark Brown63084192008-12-02 15:08:03 +0000422 cancel_delayed_work(&card->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100423 }
424
425 /* do we need to power up codec */
426 if (codec->bias_level != SND_SOC_BIAS_ON) {
427 snd_soc_dapm_set_bias_level(socdev,
428 SND_SOC_BIAS_PREPARE);
429
430 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
431 snd_soc_dapm_stream_event(codec,
432 codec_dai->playback.stream_name,
433 SND_SOC_DAPM_STREAM_START);
434 else
435 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100436 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200437 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100438
439 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
440 snd_soc_dai_digital_mute(codec_dai, 0);
441
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200442 } else {
Mark Brownd45f6212008-10-14 13:58:36 +0100443 /* codec already powered - power on widgets */
444 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
445 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100446 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200447 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100448 else
449 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100450 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200451 SND_SOC_DAPM_STREAM_START);
452
Mark Brownd45f6212008-10-14 13:58:36 +0100453 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200454 }
455
456out:
457 mutex_unlock(&pcm_mutex);
458 return ret;
459}
460
461/*
462 * Called by ALSA when the hardware params are set by application. This
463 * function can also be called multiple times and can allocate buffers
464 * (using snd_pcm_lib_* ). It's non-atomic.
465 */
466static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
467 struct snd_pcm_hw_params *params)
468{
469 struct snd_soc_pcm_runtime *rtd = substream->private_data;
470 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100471 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000472 struct snd_soc_card *card = socdev->card;
473 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100474 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
475 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200476 int ret = 0;
477
478 mutex_lock(&pcm_mutex);
479
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100480 if (machine->ops && machine->ops->hw_params) {
481 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200482 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100483 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200484 goto out;
485 }
486 }
487
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100488 if (codec_dai->ops.hw_params) {
Mark Browndee89c42008-11-18 22:11:38 +0000489 ret = codec_dai->ops.hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100490 if (ret < 0) {
491 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
492 codec_dai->name);
493 goto codec_err;
494 }
495 }
496
497 if (cpu_dai->ops.hw_params) {
Mark Browndee89c42008-11-18 22:11:38 +0000498 ret = cpu_dai->ops.hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200499 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200500 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100501 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200502 goto interface_err;
503 }
504 }
505
506 if (platform->pcm_ops->hw_params) {
507 ret = platform->pcm_ops->hw_params(substream, params);
508 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200509 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200510 platform->name);
511 goto platform_err;
512 }
513 }
514
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200515out:
516 mutex_unlock(&pcm_mutex);
517 return ret;
518
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200519platform_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100520 if (cpu_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000521 cpu_dai->ops.hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200522
523interface_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100524 if (codec_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000525 codec_dai->ops.hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100526
527codec_err:
Mark Brown3ff3f642008-05-19 12:32:25 +0200528 if (machine->ops && machine->ops->hw_free)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100529 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200530
531 mutex_unlock(&pcm_mutex);
532 return ret;
533}
534
535/*
536 * Free's resources allocated by hw_params, can be called multiple times
537 */
538static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
539{
540 struct snd_soc_pcm_runtime *rtd = substream->private_data;
541 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100542 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000543 struct snd_soc_card *card = socdev->card;
544 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100545 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
546 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200547 struct snd_soc_codec *codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548
549 mutex_lock(&pcm_mutex);
550
551 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100552 if (!codec->active)
553 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554
555 /* free any machine hw params */
556 if (machine->ops && machine->ops->hw_free)
557 machine->ops->hw_free(substream);
558
559 /* free any DMA resources */
560 if (platform->pcm_ops->hw_free)
561 platform->pcm_ops->hw_free(substream);
562
563 /* now free hw params for the DAI's */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100564 if (codec_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000565 codec_dai->ops.hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200566
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100567 if (cpu_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000568 cpu_dai->ops.hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200569
570 mutex_unlock(&pcm_mutex);
571 return 0;
572}
573
574static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
575{
576 struct snd_soc_pcm_runtime *rtd = substream->private_data;
577 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000578 struct snd_soc_card *card= socdev->card;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100579 struct snd_soc_dai_link *machine = rtd->dai;
Mark Brown87689d52008-12-02 16:01:14 +0000580 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100581 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
582 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200583 int ret;
584
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100585 if (codec_dai->ops.trigger) {
Mark Browndee89c42008-11-18 22:11:38 +0000586 ret = codec_dai->ops.trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200587 if (ret < 0)
588 return ret;
589 }
590
591 if (platform->pcm_ops->trigger) {
592 ret = platform->pcm_ops->trigger(substream, cmd);
593 if (ret < 0)
594 return ret;
595 }
596
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100597 if (cpu_dai->ops.trigger) {
Mark Browndee89c42008-11-18 22:11:38 +0000598 ret = cpu_dai->ops.trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200599 if (ret < 0)
600 return ret;
601 }
602 return 0;
603}
604
605/* ASoC PCM operations */
606static struct snd_pcm_ops soc_pcm_ops = {
607 .open = soc_pcm_open,
608 .close = soc_codec_close,
609 .hw_params = soc_pcm_hw_params,
610 .hw_free = soc_pcm_hw_free,
611 .prepare = soc_pcm_prepare,
612 .trigger = soc_pcm_trigger,
613};
614
615#ifdef CONFIG_PM
616/* powers down audio subsystem for suspend */
617static int soc_suspend(struct platform_device *pdev, pm_message_t state)
618{
Mark Brown3ff3f642008-05-19 12:32:25 +0200619 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000620 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000621 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200622 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200623 struct snd_soc_codec *codec = socdev->codec;
624 int i;
625
Andy Green6ed25972008-06-13 16:24:05 +0100626 /* Due to the resume being scheduled into a workqueue we could
627 * suspend before that's finished - wait for it to complete.
628 */
629 snd_power_lock(codec->card);
630 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
631 snd_power_unlock(codec->card);
632
633 /* we're going to block userspace touching us until resume completes */
634 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
635
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200636 /* mute any active DAC's */
Mark Browndee89c42008-11-18 22:11:38 +0000637 for (i = 0; i < card->num_links; i++) {
638 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
639 if (dai->ops.digital_mute && dai->playback.active)
640 dai->ops.digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200641 }
642
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100643 /* suspend all pcms */
Mark Brown87506542008-11-18 20:50:34 +0000644 for (i = 0; i < card->num_links; i++)
645 snd_pcm_suspend_all(card->dai_link[i].pcm);
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100646
Mark Brown87506542008-11-18 20:50:34 +0000647 if (card->suspend_pre)
648 card->suspend_pre(pdev, state);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200649
Mark Brown87506542008-11-18 20:50:34 +0000650 for (i = 0; i < card->num_links; i++) {
651 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e102008-11-24 18:01:05 +0000652 if (cpu_dai->suspend && !cpu_dai->ac97_control)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200653 cpu_dai->suspend(pdev, cpu_dai);
654 if (platform->suspend)
Mark Brown07c84d02008-12-03 18:17:28 +0000655 platform->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200656 }
657
658 /* close any waiting streams and save state */
Mark Brown63084192008-12-02 15:08:03 +0000659 run_delayed_work(&card->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200660 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200661
Mark Brown3ff3f642008-05-19 12:32:25 +0200662 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200663 char *stream = codec->dai[i].playback.stream_name;
664 if (stream != NULL)
665 snd_soc_dapm_stream_event(codec, stream,
666 SND_SOC_DAPM_STREAM_SUSPEND);
667 stream = codec->dai[i].capture.stream_name;
668 if (stream != NULL)
669 snd_soc_dapm_stream_event(codec, stream,
670 SND_SOC_DAPM_STREAM_SUSPEND);
671 }
672
673 if (codec_dev->suspend)
674 codec_dev->suspend(pdev, state);
675
Mark Brown87506542008-11-18 20:50:34 +0000676 for (i = 0; i < card->num_links; i++) {
677 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e102008-11-24 18:01:05 +0000678 if (cpu_dai->suspend && cpu_dai->ac97_control)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200679 cpu_dai->suspend(pdev, cpu_dai);
680 }
681
Mark Brown87506542008-11-18 20:50:34 +0000682 if (card->suspend_post)
683 card->suspend_post(pdev, state);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200684
685 return 0;
686}
687
Andy Green6ed25972008-06-13 16:24:05 +0100688/* deferred resume work, so resume can complete before we finished
689 * setting our codec back up, which can be very slow on I2C
690 */
691static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200692{
Mark Brown63084192008-12-02 15:08:03 +0000693 struct snd_soc_card *card = container_of(work,
694 struct snd_soc_card,
695 deferred_resume_work);
696 struct snd_soc_device *socdev = card->socdev;
Mark Brown87689d52008-12-02 16:01:14 +0000697 struct snd_soc_platform *platform = card->platform;
Mark Brown3ff3f642008-05-19 12:32:25 +0200698 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699 struct snd_soc_codec *codec = socdev->codec;
Andy Green6ed25972008-06-13 16:24:05 +0100700 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200701 int i;
702
Andy Green6ed25972008-06-13 16:24:05 +0100703 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
704 * so userspace apps are blocked from touching us
705 */
706
Mark Brownfde22f22008-11-24 18:08:18 +0000707 dev_dbg(socdev->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100708
Mark Brown87506542008-11-18 20:50:34 +0000709 if (card->resume_pre)
710 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200711
Mark Brown87506542008-11-18 20:50:34 +0000712 for (i = 0; i < card->num_links; i++) {
713 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e102008-11-24 18:01:05 +0000714 if (cpu_dai->resume && cpu_dai->ac97_control)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200715 cpu_dai->resume(pdev, cpu_dai);
716 }
717
718 if (codec_dev->resume)
719 codec_dev->resume(pdev);
720
Mark Brown3ff3f642008-05-19 12:32:25 +0200721 for (i = 0; i < codec->num_dai; i++) {
722 char *stream = codec->dai[i].playback.stream_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723 if (stream != NULL)
724 snd_soc_dapm_stream_event(codec, stream,
725 SND_SOC_DAPM_STREAM_RESUME);
726 stream = codec->dai[i].capture.stream_name;
727 if (stream != NULL)
728 snd_soc_dapm_stream_event(codec, stream,
729 SND_SOC_DAPM_STREAM_RESUME);
730 }
731
Mark Brown3ff3f642008-05-19 12:32:25 +0200732 /* unmute any active DACs */
Mark Browndee89c42008-11-18 22:11:38 +0000733 for (i = 0; i < card->num_links; i++) {
734 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
735 if (dai->ops.digital_mute && dai->playback.active)
736 dai->ops.digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200737 }
738
Mark Brown87506542008-11-18 20:50:34 +0000739 for (i = 0; i < card->num_links; i++) {
740 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Mark Brown3ba9e102008-11-24 18:01:05 +0000741 if (cpu_dai->resume && !cpu_dai->ac97_control)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200742 cpu_dai->resume(pdev, cpu_dai);
743 if (platform->resume)
Mark Brown07c84d02008-12-03 18:17:28 +0000744 platform->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200745 }
746
Mark Brown87506542008-11-18 20:50:34 +0000747 if (card->resume_post)
748 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200749
Mark Brownfde22f22008-11-24 18:08:18 +0000750 dev_dbg(socdev->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100751
752 /* userspace can access us now we are back as we were before */
753 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
754}
755
756/* powers up audio subsystem after a suspend */
757static int soc_resume(struct platform_device *pdev)
758{
759 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown63084192008-12-02 15:08:03 +0000760 struct snd_soc_card *card = socdev->card;
Andy Green6ed25972008-06-13 16:24:05 +0100761
Mark Brownfde22f22008-11-24 18:08:18 +0000762 dev_dbg(socdev->dev, "scheduling resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100763
Mark Brown63084192008-12-02 15:08:03 +0000764 if (!schedule_work(&card->deferred_resume_work))
Mark Brownfde22f22008-11-24 18:08:18 +0000765 dev_err(socdev->dev, "resume work item may be lost\n");
Andy Green6ed25972008-06-13 16:24:05 +0100766
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200767 return 0;
768}
769
770#else
771#define soc_suspend NULL
772#define soc_resume NULL
773#endif
774
775/* probes a new socdev */
776static int soc_probe(struct platform_device *pdev)
777{
778 int ret = 0, i;
779 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000780 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000781 struct snd_soc_platform *platform = card->platform;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200782 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
783
Mark Brown63084192008-12-02 15:08:03 +0000784 /* Bodge while we push things out of socdev */
785 card->socdev = socdev;
786
Mark Brown87506542008-11-18 20:50:34 +0000787 if (card->probe) {
788 ret = card->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200789 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790 return ret;
791 }
792
Mark Brown87506542008-11-18 20:50:34 +0000793 for (i = 0; i < card->num_links; i++) {
794 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200795 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +0100796 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +0200797 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200798 goto cpu_dai_err;
799 }
800 }
801
802 if (codec_dev->probe) {
803 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200804 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200805 goto cpu_dai_err;
806 }
807
808 if (platform->probe) {
809 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200810 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200811 goto platform_err;
812 }
813
814 /* DAPM stream work */
Mark Brown63084192008-12-02 15:08:03 +0000815 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +0100816#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +0100817 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +0000818 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +0100819#endif
Andy Green6ed25972008-06-13 16:24:05 +0100820
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200821 return 0;
822
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200823platform_err:
824 if (codec_dev->remove)
825 codec_dev->remove(pdev);
826
827cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +0100828 for (i--; i >= 0; i--) {
Mark Brown87506542008-11-18 20:50:34 +0000829 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200830 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100831 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200832 }
833
Mark Brown87506542008-11-18 20:50:34 +0000834 if (card->remove)
835 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200836
837 return ret;
838}
839
840/* removes a socdev */
841static int soc_remove(struct platform_device *pdev)
842{
843 int i;
844 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000845 struct snd_soc_card *card = socdev->card;
Mark Brown87689d52008-12-02 16:01:14 +0000846 struct snd_soc_platform *platform = card->platform;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200847 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
848
Mark Brown63084192008-12-02 15:08:03 +0000849 run_delayed_work(&card->delayed_work);
Liam Girdwood965ac422007-01-31 14:14:57 +0100850
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200851 if (platform->remove)
852 platform->remove(pdev);
853
854 if (codec_dev->remove)
855 codec_dev->remove(pdev);
856
Mark Brown87506542008-11-18 20:50:34 +0000857 for (i = 0; i < card->num_links; i++) {
858 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200859 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100860 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200861 }
862
Mark Brown87506542008-11-18 20:50:34 +0000863 if (card->remove)
864 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200865
866 return 0;
867}
868
869/* ASoC platform driver */
870static struct platform_driver soc_driver = {
871 .driver = {
872 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +0200873 .owner = THIS_MODULE,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200874 },
875 .probe = soc_probe,
876 .remove = soc_remove,
877 .suspend = soc_suspend,
878 .resume = soc_resume,
879};
880
881/* create a new pcm */
882static int soc_new_pcm(struct snd_soc_device *socdev,
883 struct snd_soc_dai_link *dai_link, int num)
884{
885 struct snd_soc_codec *codec = socdev->codec;
Mark Brown87689d52008-12-02 16:01:14 +0000886 struct snd_soc_card *card = socdev->card;
887 struct snd_soc_platform *platform = card->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100888 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
889 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200890 struct snd_soc_pcm_runtime *rtd;
891 struct snd_pcm *pcm;
892 char new_name[64];
893 int ret = 0, playback = 0, capture = 0;
894
895 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
896 if (rtd == NULL)
897 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100898
899 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200900 rtd->socdev = socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100901 codec_dai->codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200902
903 /* check client and interface hw capabilities */
Mark Brown3ba9e102008-11-24 18:01:05 +0000904 sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
905 num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200906
907 if (codec_dai->playback.channels_min)
908 playback = 1;
909 if (codec_dai->capture.channels_min)
910 capture = 1;
911
912 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
913 capture, &pcm);
914 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200915 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
916 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200917 kfree(rtd);
918 return ret;
919 }
920
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100921 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200922 pcm->private_data = rtd;
Mark Brown87689d52008-12-02 16:01:14 +0000923 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
924 soc_pcm_ops.pointer = platform->pcm_ops->pointer;
925 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
926 soc_pcm_ops.copy = platform->pcm_ops->copy;
927 soc_pcm_ops.silence = platform->pcm_ops->silence;
928 soc_pcm_ops.ack = platform->pcm_ops->ack;
929 soc_pcm_ops.page = platform->pcm_ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200930
931 if (playback)
932 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
933
934 if (capture)
935 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
936
Mark Brown87689d52008-12-02 16:01:14 +0000937 ret = platform->pcm_new(codec->card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200938 if (ret < 0) {
939 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
940 kfree(rtd);
941 return ret;
942 }
943
Mark Brown87689d52008-12-02 16:01:14 +0000944 pcm->private_free = platform->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200945 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
946 cpu_dai->name);
947 return ret;
948}
949
950/* codec register dump */
Troy Kisky12ef1932008-10-13 17:42:14 -0700951static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200952{
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200953 struct snd_soc_codec *codec = devdata->codec;
954 int i, step = 1, count = 0;
955
956 if (!codec->reg_cache_size)
957 return 0;
958
959 if (codec->reg_cache_step)
960 step = codec->reg_cache_step;
961
962 count += sprintf(buf, "%s registers\n", codec->name);
Mark Brown58cd33c2008-07-29 11:42:25 +0100963 for (i = 0; i < codec->reg_cache_size; i += step) {
964 count += sprintf(buf + count, "%2x: ", i);
965 if (count >= PAGE_SIZE - 1)
966 break;
967
968 if (codec->display_register)
969 count += codec->display_register(codec, buf + count,
970 PAGE_SIZE - count, i);
971 else
972 count += snprintf(buf + count, PAGE_SIZE - count,
973 "%4x", codec->read(codec, i));
974
975 if (count >= PAGE_SIZE - 1)
976 break;
977
978 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
979 if (count >= PAGE_SIZE - 1)
980 break;
981 }
982
983 /* Truncate count; min() would cause a warning */
984 if (count >= PAGE_SIZE)
985 count = PAGE_SIZE - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200986
987 return count;
988}
Troy Kisky12ef1932008-10-13 17:42:14 -0700989static ssize_t codec_reg_show(struct device *dev,
990 struct device_attribute *attr, char *buf)
991{
992 struct snd_soc_device *devdata = dev_get_drvdata(dev);
993 return soc_codec_reg_show(devdata, buf);
994}
995
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200996static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
997
Troy Kisky12ef1932008-10-13 17:42:14 -0700998#ifdef CONFIG_DEBUG_FS
999static int codec_reg_open_file(struct inode *inode, struct file *file)
1000{
1001 file->private_data = inode->i_private;
1002 return 0;
1003}
1004
1005static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1006 size_t count, loff_t *ppos)
1007{
1008 ssize_t ret;
Mark Brown384c89e2008-12-03 17:34:03 +00001009 struct snd_soc_codec *codec = file->private_data;
1010 struct device *card_dev = codec->card->dev;
1011 struct snd_soc_device *devdata = card_dev->driver_data;
Troy Kisky12ef1932008-10-13 17:42:14 -07001012 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1013 if (!buf)
1014 return -ENOMEM;
1015 ret = soc_codec_reg_show(devdata, buf);
1016 if (ret >= 0)
1017 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1018 kfree(buf);
1019 return ret;
1020}
1021
1022static ssize_t codec_reg_write_file(struct file *file,
1023 const char __user *user_buf, size_t count, loff_t *ppos)
1024{
1025 char buf[32];
1026 int buf_size;
1027 char *start = buf;
1028 unsigned long reg, value;
1029 int step = 1;
Mark Brown384c89e2008-12-03 17:34:03 +00001030 struct snd_soc_codec *codec = file->private_data;
Troy Kisky12ef1932008-10-13 17:42:14 -07001031
1032 buf_size = min(count, (sizeof(buf)-1));
1033 if (copy_from_user(buf, user_buf, buf_size))
1034 return -EFAULT;
1035 buf[buf_size] = 0;
1036
1037 if (codec->reg_cache_step)
1038 step = codec->reg_cache_step;
1039
1040 while (*start == ' ')
1041 start++;
1042 reg = simple_strtoul(start, &start, 16);
1043 if ((reg >= codec->reg_cache_size) || (reg % step))
1044 return -EINVAL;
1045 while (*start == ' ')
1046 start++;
1047 if (strict_strtoul(start, 16, &value))
1048 return -EINVAL;
1049 codec->write(codec, reg, value);
1050 return buf_size;
1051}
1052
1053static const struct file_operations codec_reg_fops = {
1054 .open = codec_reg_open_file,
1055 .read = codec_reg_read_file,
1056 .write = codec_reg_write_file,
1057};
1058
Mark Brown384c89e2008-12-03 17:34:03 +00001059static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001060{
Mark Brown384c89e2008-12-03 17:34:03 +00001061 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
1062 debugfs_root, codec,
1063 &codec_reg_fops);
1064 if (!codec->debugfs_reg)
1065 printk(KERN_WARNING
1066 "ASoC: Failed to create codec register debugfs file\n");
Troy Kisky12ef1932008-10-13 17:42:14 -07001067
Mark Brown384c89e2008-12-03 17:34:03 +00001068 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
1069 debugfs_root,
1070 &codec->pop_time);
1071 if (!codec->debugfs_pop_time)
1072 printk(KERN_WARNING
1073 "Failed to create pop time debugfs file\n");
Troy Kisky12ef1932008-10-13 17:42:14 -07001074}
1075
Mark Brown384c89e2008-12-03 17:34:03 +00001076static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001077{
Mark Brown384c89e2008-12-03 17:34:03 +00001078 debugfs_remove(codec->debugfs_pop_time);
1079 debugfs_remove(codec->debugfs_reg);
Troy Kisky12ef1932008-10-13 17:42:14 -07001080}
1081
1082#else
1083
Mark Brown384c89e2008-12-03 17:34:03 +00001084static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001085{
1086}
1087
Mark Brown384c89e2008-12-03 17:34:03 +00001088static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
Troy Kisky12ef1932008-10-13 17:42:14 -07001089{
1090}
1091#endif
1092
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001093/**
1094 * snd_soc_new_ac97_codec - initailise AC97 device
1095 * @codec: audio codec
1096 * @ops: AC97 bus operations
1097 * @num: AC97 codec number
1098 *
1099 * Initialises AC97 codec resources for use by ad-hoc devices only.
1100 */
1101int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1102 struct snd_ac97_bus_ops *ops, int num)
1103{
1104 mutex_lock(&codec->mutex);
1105
1106 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1107 if (codec->ac97 == NULL) {
1108 mutex_unlock(&codec->mutex);
1109 return -ENOMEM;
1110 }
1111
1112 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1113 if (codec->ac97->bus == NULL) {
1114 kfree(codec->ac97);
1115 codec->ac97 = NULL;
1116 mutex_unlock(&codec->mutex);
1117 return -ENOMEM;
1118 }
1119
1120 codec->ac97->bus->ops = ops;
1121 codec->ac97->num = num;
1122 mutex_unlock(&codec->mutex);
1123 return 0;
1124}
1125EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1126
1127/**
1128 * snd_soc_free_ac97_codec - free AC97 codec device
1129 * @codec: audio codec
1130 *
1131 * Frees AC97 codec device resources.
1132 */
1133void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1134{
1135 mutex_lock(&codec->mutex);
1136 kfree(codec->ac97->bus);
1137 kfree(codec->ac97);
1138 codec->ac97 = NULL;
1139 mutex_unlock(&codec->mutex);
1140}
1141EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1142
1143/**
1144 * snd_soc_update_bits - update codec register bits
1145 * @codec: audio codec
1146 * @reg: codec register
1147 * @mask: register mask
1148 * @value: new value
1149 *
1150 * Writes new register value.
1151 *
1152 * Returns 1 for change else 0.
1153 */
1154int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1155 unsigned short mask, unsigned short value)
1156{
1157 int change;
1158 unsigned short old, new;
1159
1160 mutex_lock(&io_mutex);
1161 old = snd_soc_read(codec, reg);
1162 new = (old & ~mask) | value;
1163 change = old != new;
1164 if (change)
1165 snd_soc_write(codec, reg, new);
1166
1167 mutex_unlock(&io_mutex);
1168 return change;
1169}
1170EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1171
1172/**
1173 * snd_soc_test_bits - test register for change
1174 * @codec: audio codec
1175 * @reg: codec register
1176 * @mask: register mask
1177 * @value: new value
1178 *
1179 * Tests a register with a new value and checks if the new value is
1180 * different from the old value.
1181 *
1182 * Returns 1 for change else 0.
1183 */
1184int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1185 unsigned short mask, unsigned short value)
1186{
1187 int change;
1188 unsigned short old, new;
1189
1190 mutex_lock(&io_mutex);
1191 old = snd_soc_read(codec, reg);
1192 new = (old & ~mask) | value;
1193 change = old != new;
1194 mutex_unlock(&io_mutex);
1195
1196 return change;
1197}
1198EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1199
1200/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001201 * snd_soc_new_pcms - create new sound card and pcms
1202 * @socdev: the SoC audio device
1203 *
1204 * Create a new sound card based upon the codec and interface pcms.
1205 *
1206 * Returns 0 for success, else error.
1207 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001208int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001209{
1210 struct snd_soc_codec *codec = socdev->codec;
Mark Brown87506542008-11-18 20:50:34 +00001211 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001212 int ret = 0, i;
1213
1214 mutex_lock(&codec->mutex);
1215
1216 /* register a sound card */
1217 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1218 if (!codec->card) {
1219 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1220 codec->name);
1221 mutex_unlock(&codec->mutex);
1222 return -ENODEV;
1223 }
1224
1225 codec->card->dev = socdev->dev;
1226 codec->card->private_data = codec;
1227 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1228
1229 /* create the pcms */
Mark Brown87506542008-11-18 20:50:34 +00001230 for (i = 0; i < card->num_links; i++) {
1231 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001232 if (ret < 0) {
1233 printk(KERN_ERR "asoc: can't create pcm %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001234 card->dai_link[i].stream_name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001235 mutex_unlock(&codec->mutex);
1236 return ret;
1237 }
1238 }
1239
1240 mutex_unlock(&codec->mutex);
1241 return ret;
1242}
1243EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1244
1245/**
Mark Brown968a6022008-11-28 11:49:07 +00001246 * snd_soc_init_card - register sound card
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001247 * @socdev: the SoC audio device
1248 *
1249 * Register a SoC sound card. Also registers an AC97 device if the
1250 * codec is AC97 for ad hoc devices.
1251 *
1252 * Returns 0 for success, else error.
1253 */
Mark Brown968a6022008-11-28 11:49:07 +00001254int snd_soc_init_card(struct snd_soc_device *socdev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001255{
1256 struct snd_soc_codec *codec = socdev->codec;
Mark Brown87506542008-11-18 20:50:34 +00001257 struct snd_soc_card *card = socdev->card;
Liam Girdwood12e74f72006-10-16 21:19:48 +02001258 int ret = 0, i, ac97 = 0, err = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001259
Mark Brown87506542008-11-18 20:50:34 +00001260 for (i = 0; i < card->num_links; i++) {
1261 if (card->dai_link[i].init) {
1262 err = card->dai_link[i].init(codec);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001263 if (err < 0) {
1264 printk(KERN_ERR "asoc: failed to init %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001265 card->dai_link[i].stream_name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001266 continue;
1267 }
1268 }
Mark Brown3ba9e102008-11-24 18:01:05 +00001269 if (card->dai_link[i].codec_dai->ac97_control)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001270 ac97 = 1;
1271 }
1272 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
Mark Brown87506542008-11-18 20:50:34 +00001273 "%s", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001274 snprintf(codec->card->longname, sizeof(codec->card->longname),
Mark Brown87506542008-11-18 20:50:34 +00001275 "%s (%s)", card->name, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001276
1277 ret = snd_card_register(codec->card);
1278 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001279 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001280 codec->name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001281 goto out;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001282 }
1283
Mark Brown08c8efe2008-01-21 14:33:37 +01001284 mutex_lock(&codec->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001285#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood12e74f72006-10-16 21:19:48 +02001286 if (ac97) {
1287 ret = soc_ac97_dev_register(codec);
1288 if (ret < 0) {
1289 printk(KERN_ERR "asoc: AC97 device register failed\n");
1290 snd_card_free(codec->card);
Mark Brown08c8efe2008-01-21 14:33:37 +01001291 mutex_unlock(&codec->mutex);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001292 goto out;
1293 }
1294 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001295#endif
1296
Liam Girdwood12e74f72006-10-16 21:19:48 +02001297 err = snd_soc_dapm_sys_add(socdev->dev);
1298 if (err < 0)
1299 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1300
1301 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1302 if (err < 0)
Mark Brown3ff3f642008-05-19 12:32:25 +02001303 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
Mark Brown08c8efe2008-01-21 14:33:37 +01001304
Mark Brown384c89e2008-12-03 17:34:03 +00001305 soc_init_codec_debugfs(socdev->codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001306 mutex_unlock(&codec->mutex);
Mark Brown08c8efe2008-01-21 14:33:37 +01001307
1308out:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001309 return ret;
1310}
Mark Brown968a6022008-11-28 11:49:07 +00001311EXPORT_SYMBOL_GPL(snd_soc_init_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001312
1313/**
1314 * snd_soc_free_pcms - free sound card and pcms
1315 * @socdev: the SoC audio device
1316 *
1317 * Frees sound card and pcms associated with the socdev.
1318 * Also unregister the codec if it is an AC97 device.
1319 */
1320void snd_soc_free_pcms(struct snd_soc_device *socdev)
1321{
1322 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001323#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001324 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001325 int i;
1326#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001327
1328 mutex_lock(&codec->mutex);
Mark Brown384c89e2008-12-03 17:34:03 +00001329 soc_cleanup_codec_debugfs(socdev->codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001330#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001331 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001332 codec_dai = &codec->dai[i];
Mark Brown3ba9e102008-11-24 18:01:05 +00001333 if (codec_dai->ac97_control && codec->ac97) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001334 soc_ac97_dev_unregister(codec);
1335 goto free_card;
1336 }
1337 }
1338free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001339#endif
1340
1341 if (codec->card)
1342 snd_card_free(codec->card);
1343 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1344 mutex_unlock(&codec->mutex);
1345}
1346EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1347
1348/**
1349 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1350 * @substream: the pcm substream
1351 * @hw: the hardware parameters
1352 *
1353 * Sets the substream runtime hardware parameters.
1354 */
1355int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1356 const struct snd_pcm_hardware *hw)
1357{
1358 struct snd_pcm_runtime *runtime = substream->runtime;
1359 runtime->hw.info = hw->info;
1360 runtime->hw.formats = hw->formats;
1361 runtime->hw.period_bytes_min = hw->period_bytes_min;
1362 runtime->hw.period_bytes_max = hw->period_bytes_max;
1363 runtime->hw.periods_min = hw->periods_min;
1364 runtime->hw.periods_max = hw->periods_max;
1365 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1366 runtime->hw.fifo_size = hw->fifo_size;
1367 return 0;
1368}
1369EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1370
1371/**
1372 * snd_soc_cnew - create new control
1373 * @_template: control template
1374 * @data: control private data
1375 * @lnng_name: control long name
1376 *
1377 * Create a new mixer control from a template control.
1378 *
1379 * Returns 0 for success, else error.
1380 */
1381struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1382 void *data, char *long_name)
1383{
1384 struct snd_kcontrol_new template;
1385
1386 memcpy(&template, _template, sizeof(template));
1387 if (long_name)
1388 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001389 template.index = 0;
1390
1391 return snd_ctl_new1(&template, data);
1392}
1393EXPORT_SYMBOL_GPL(snd_soc_cnew);
1394
1395/**
1396 * snd_soc_info_enum_double - enumerated double mixer info callback
1397 * @kcontrol: mixer control
1398 * @uinfo: control element information
1399 *
1400 * Callback to provide information about a double enumerated
1401 * mixer control.
1402 *
1403 * Returns 0 for success.
1404 */
1405int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1406 struct snd_ctl_elem_info *uinfo)
1407{
1408 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1409
1410 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1411 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001412 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001413
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001414 if (uinfo->value.enumerated.item > e->max - 1)
1415 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001416 strcpy(uinfo->value.enumerated.name,
1417 e->texts[uinfo->value.enumerated.item]);
1418 return 0;
1419}
1420EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1421
1422/**
1423 * snd_soc_get_enum_double - enumerated double mixer get callback
1424 * @kcontrol: mixer control
1425 * @uinfo: control element information
1426 *
1427 * Callback to get the value of a double enumerated mixer.
1428 *
1429 * Returns 0 for success.
1430 */
1431int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1432 struct snd_ctl_elem_value *ucontrol)
1433{
1434 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1435 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1436 unsigned short val, bitmask;
1437
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001438 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001439 ;
1440 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001441 ucontrol->value.enumerated.item[0]
1442 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001443 if (e->shift_l != e->shift_r)
1444 ucontrol->value.enumerated.item[1] =
1445 (val >> e->shift_r) & (bitmask - 1);
1446
1447 return 0;
1448}
1449EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1450
1451/**
1452 * snd_soc_put_enum_double - enumerated double mixer put callback
1453 * @kcontrol: mixer control
1454 * @uinfo: control element information
1455 *
1456 * Callback to set the value of a double enumerated mixer.
1457 *
1458 * Returns 0 for success.
1459 */
1460int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1461 struct snd_ctl_elem_value *ucontrol)
1462{
1463 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1464 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1465 unsigned short val;
1466 unsigned short mask, bitmask;
1467
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001468 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001469 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001470 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001471 return -EINVAL;
1472 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1473 mask = (bitmask - 1) << e->shift_l;
1474 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001475 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001476 return -EINVAL;
1477 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1478 mask |= (bitmask - 1) << e->shift_r;
1479 }
1480
1481 return snd_soc_update_bits(codec, e->reg, mask, val);
1482}
1483EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1484
1485/**
1486 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1487 * @kcontrol: mixer control
1488 * @uinfo: control element information
1489 *
1490 * Callback to provide information about an external enumerated
1491 * single mixer.
1492 *
1493 * Returns 0 for success.
1494 */
1495int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1496 struct snd_ctl_elem_info *uinfo)
1497{
1498 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1499
1500 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1501 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001502 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001503
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001504 if (uinfo->value.enumerated.item > e->max - 1)
1505 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001506 strcpy(uinfo->value.enumerated.name,
1507 e->texts[uinfo->value.enumerated.item]);
1508 return 0;
1509}
1510EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1511
1512/**
1513 * snd_soc_info_volsw_ext - external single mixer info callback
1514 * @kcontrol: mixer control
1515 * @uinfo: control element information
1516 *
1517 * Callback to provide information about a single external mixer control.
1518 *
1519 * Returns 0 for success.
1520 */
1521int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1522 struct snd_ctl_elem_info *uinfo)
1523{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001524 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001525
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001526 if (max == 1)
1527 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1528 else
1529 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1530
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001531 uinfo->count = 1;
1532 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001533 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001534 return 0;
1535}
1536EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1537
1538/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001539 * snd_soc_info_volsw - single mixer info callback
1540 * @kcontrol: mixer control
1541 * @uinfo: control element information
1542 *
1543 * Callback to provide information about a single mixer control.
1544 *
1545 * Returns 0 for success.
1546 */
1547int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1548 struct snd_ctl_elem_info *uinfo)
1549{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001550 struct soc_mixer_control *mc =
1551 (struct soc_mixer_control *)kcontrol->private_value;
1552 int max = mc->max;
Mark Brown762b8df2008-10-30 12:37:08 +00001553 unsigned int shift = mc->shift;
Jon Smirl815ecf82008-07-29 10:22:24 -04001554 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001555
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001556 if (max == 1)
1557 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1558 else
1559 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1560
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001561 uinfo->count = shift == rshift ? 1 : 2;
1562 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001563 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001564 return 0;
1565}
1566EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1567
1568/**
1569 * snd_soc_get_volsw - single mixer get callback
1570 * @kcontrol: mixer control
1571 * @uinfo: control element information
1572 *
1573 * Callback to get the value of a single mixer control.
1574 *
1575 * Returns 0 for success.
1576 */
1577int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1578 struct snd_ctl_elem_value *ucontrol)
1579{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001580 struct soc_mixer_control *mc =
1581 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001582 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001583 unsigned int reg = mc->reg;
1584 unsigned int shift = mc->shift;
1585 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001586 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001587 unsigned int mask = (1 << fls(max)) - 1;
1588 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001589
1590 ucontrol->value.integer.value[0] =
1591 (snd_soc_read(codec, reg) >> shift) & mask;
1592 if (shift != rshift)
1593 ucontrol->value.integer.value[1] =
1594 (snd_soc_read(codec, reg) >> rshift) & mask;
1595 if (invert) {
1596 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001597 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001598 if (shift != rshift)
1599 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001600 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001601 }
1602
1603 return 0;
1604}
1605EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1606
1607/**
1608 * snd_soc_put_volsw - single mixer put callback
1609 * @kcontrol: mixer control
1610 * @uinfo: control element information
1611 *
1612 * Callback to set the value of a single mixer control.
1613 *
1614 * Returns 0 for success.
1615 */
1616int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1617 struct snd_ctl_elem_value *ucontrol)
1618{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001619 struct soc_mixer_control *mc =
1620 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001621 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001622 unsigned int reg = mc->reg;
1623 unsigned int shift = mc->shift;
1624 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001625 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001626 unsigned int mask = (1 << fls(max)) - 1;
1627 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001628 unsigned short val, val2, val_mask;
1629
1630 val = (ucontrol->value.integer.value[0] & mask);
1631 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001632 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001633 val_mask = mask << shift;
1634 val = val << shift;
1635 if (shift != rshift) {
1636 val2 = (ucontrol->value.integer.value[1] & mask);
1637 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001638 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001639 val_mask |= mask << rshift;
1640 val |= val2 << rshift;
1641 }
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001642 return snd_soc_update_bits(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001643}
1644EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1645
1646/**
1647 * snd_soc_info_volsw_2r - double mixer info callback
1648 * @kcontrol: mixer control
1649 * @uinfo: control element information
1650 *
1651 * Callback to provide information about a double mixer control that
1652 * spans 2 codec registers.
1653 *
1654 * Returns 0 for success.
1655 */
1656int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1657 struct snd_ctl_elem_info *uinfo)
1658{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001659 struct soc_mixer_control *mc =
1660 (struct soc_mixer_control *)kcontrol->private_value;
1661 int max = mc->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001662
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001663 if (max == 1)
1664 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1665 else
1666 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1667
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001668 uinfo->count = 2;
1669 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001670 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001671 return 0;
1672}
1673EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1674
1675/**
1676 * snd_soc_get_volsw_2r - double mixer get callback
1677 * @kcontrol: mixer control
1678 * @uinfo: control element information
1679 *
1680 * Callback to get the value of a double mixer control that spans 2 registers.
1681 *
1682 * Returns 0 for success.
1683 */
1684int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1685 struct snd_ctl_elem_value *ucontrol)
1686{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001687 struct soc_mixer_control *mc =
1688 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001689 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001690 unsigned int reg = mc->reg;
1691 unsigned int reg2 = mc->rreg;
1692 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001693 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001694 unsigned int mask = (1<<fls(max))-1;
1695 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001696
1697 ucontrol->value.integer.value[0] =
1698 (snd_soc_read(codec, reg) >> shift) & mask;
1699 ucontrol->value.integer.value[1] =
1700 (snd_soc_read(codec, reg2) >> shift) & mask;
1701 if (invert) {
1702 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001703 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001704 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001705 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001706 }
1707
1708 return 0;
1709}
1710EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1711
1712/**
1713 * snd_soc_put_volsw_2r - double mixer set callback
1714 * @kcontrol: mixer control
1715 * @uinfo: control element information
1716 *
1717 * Callback to set the value of a double mixer control that spans 2 registers.
1718 *
1719 * Returns 0 for success.
1720 */
1721int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1722 struct snd_ctl_elem_value *ucontrol)
1723{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001724 struct soc_mixer_control *mc =
1725 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001726 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001727 unsigned int reg = mc->reg;
1728 unsigned int reg2 = mc->rreg;
1729 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001730 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001731 unsigned int mask = (1 << fls(max)) - 1;
1732 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001733 int err;
1734 unsigned short val, val2, val_mask;
1735
1736 val_mask = mask << shift;
1737 val = (ucontrol->value.integer.value[0] & mask);
1738 val2 = (ucontrol->value.integer.value[1] & mask);
1739
1740 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001741 val = max - val;
1742 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001743 }
1744
1745 val = val << shift;
1746 val2 = val2 << shift;
1747
Mark Brown3ff3f642008-05-19 12:32:25 +02001748 err = snd_soc_update_bits(codec, reg, val_mask, val);
1749 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001750 return err;
1751
1752 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1753 return err;
1754}
1755EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1756
Mark Browne13ac2e2008-05-28 17:58:05 +01001757/**
1758 * snd_soc_info_volsw_s8 - signed mixer info callback
1759 * @kcontrol: mixer control
1760 * @uinfo: control element information
1761 *
1762 * Callback to provide information about a signed mixer control.
1763 *
1764 * Returns 0 for success.
1765 */
1766int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1767 struct snd_ctl_elem_info *uinfo)
1768{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001769 struct soc_mixer_control *mc =
1770 (struct soc_mixer_control *)kcontrol->private_value;
1771 int max = mc->max;
1772 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001773
1774 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1775 uinfo->count = 2;
1776 uinfo->value.integer.min = 0;
1777 uinfo->value.integer.max = max-min;
1778 return 0;
1779}
1780EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1781
1782/**
1783 * snd_soc_get_volsw_s8 - signed mixer get callback
1784 * @kcontrol: mixer control
1785 * @uinfo: control element information
1786 *
1787 * Callback to get the value of a signed mixer control.
1788 *
1789 * Returns 0 for success.
1790 */
1791int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1792 struct snd_ctl_elem_value *ucontrol)
1793{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001794 struct soc_mixer_control *mc =
1795 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01001796 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001797 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001798 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001799 int val = snd_soc_read(codec, reg);
1800
1801 ucontrol->value.integer.value[0] =
1802 ((signed char)(val & 0xff))-min;
1803 ucontrol->value.integer.value[1] =
1804 ((signed char)((val >> 8) & 0xff))-min;
1805 return 0;
1806}
1807EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1808
1809/**
1810 * snd_soc_put_volsw_sgn - signed mixer put callback
1811 * @kcontrol: mixer control
1812 * @uinfo: control element information
1813 *
1814 * Callback to set the value of a signed mixer control.
1815 *
1816 * Returns 0 for success.
1817 */
1818int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1819 struct snd_ctl_elem_value *ucontrol)
1820{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001821 struct soc_mixer_control *mc =
1822 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01001823 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001824 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001825 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001826 unsigned short val;
1827
1828 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1829 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1830
1831 return snd_soc_update_bits(codec, reg, 0xffff, val);
1832}
1833EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1834
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001835/**
1836 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1837 * @dai: DAI
1838 * @clk_id: DAI specific clock ID
1839 * @freq: new clock frequency in Hz
1840 * @dir: new clock direction - input/output.
1841 *
1842 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1843 */
1844int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1845 unsigned int freq, int dir)
1846{
Mark Browndee89c42008-11-18 22:11:38 +00001847 if (dai->ops.set_sysclk)
1848 return dai->ops.set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001849 else
1850 return -EINVAL;
1851}
1852EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1853
1854/**
1855 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1856 * @dai: DAI
1857 * @clk_id: DAI specific clock divider ID
1858 * @div: new clock divisor.
1859 *
1860 * Configures the clock dividers. This is used to derive the best DAI bit and
1861 * frame clocks from the system or master clock. It's best to set the DAI bit
1862 * and frame clocks as low as possible to save system power.
1863 */
1864int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
1865 int div_id, int div)
1866{
Mark Browndee89c42008-11-18 22:11:38 +00001867 if (dai->ops.set_clkdiv)
1868 return dai->ops.set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001869 else
1870 return -EINVAL;
1871}
1872EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
1873
1874/**
1875 * snd_soc_dai_set_pll - configure DAI PLL.
1876 * @dai: DAI
1877 * @pll_id: DAI specific PLL ID
1878 * @freq_in: PLL input clock frequency in Hz
1879 * @freq_out: requested PLL output clock frequency in Hz
1880 *
1881 * Configures and enables PLL to generate output clock based on input clock.
1882 */
1883int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
1884 int pll_id, unsigned int freq_in, unsigned int freq_out)
1885{
Mark Browndee89c42008-11-18 22:11:38 +00001886 if (dai->ops.set_pll)
1887 return dai->ops.set_pll(dai, pll_id, freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001888 else
1889 return -EINVAL;
1890}
1891EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
1892
1893/**
1894 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1895 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001896 * @fmt: SND_SOC_DAIFMT_ format value.
1897 *
1898 * Configures the DAI hardware format and clocking.
1899 */
1900int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1901{
Mark Browndee89c42008-11-18 22:11:38 +00001902 if (dai->ops.set_fmt)
1903 return dai->ops.set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001904 else
1905 return -EINVAL;
1906}
1907EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
1908
1909/**
1910 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
1911 * @dai: DAI
1912 * @mask: DAI specific mask representing used slots.
1913 * @slots: Number of slots in use.
1914 *
1915 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
1916 * specific.
1917 */
1918int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
1919 unsigned int mask, int slots)
1920{
Mark Browndee89c42008-11-18 22:11:38 +00001921 if (dai->ops.set_sysclk)
1922 return dai->ops.set_tdm_slot(dai, mask, slots);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001923 else
1924 return -EINVAL;
1925}
1926EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
1927
1928/**
1929 * snd_soc_dai_set_tristate - configure DAI system or master clock.
1930 * @dai: DAI
1931 * @tristate: tristate enable
1932 *
1933 * Tristates the DAI so that others can use it.
1934 */
1935int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
1936{
Mark Browndee89c42008-11-18 22:11:38 +00001937 if (dai->ops.set_sysclk)
1938 return dai->ops.set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001939 else
1940 return -EINVAL;
1941}
1942EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
1943
1944/**
1945 * snd_soc_dai_digital_mute - configure DAI system or master clock.
1946 * @dai: DAI
1947 * @mute: mute enable
1948 *
1949 * Mutes the DAI DAC.
1950 */
1951int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
1952{
Mark Browndee89c42008-11-18 22:11:38 +00001953 if (dai->ops.digital_mute)
1954 return dai->ops.digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001955 else
1956 return -EINVAL;
1957}
1958EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
1959
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001960static int __devinit snd_soc_init(void)
1961{
Mark Brown384c89e2008-12-03 17:34:03 +00001962#ifdef CONFIG_DEBUG_FS
1963 debugfs_root = debugfs_create_dir("asoc", NULL);
1964 if (IS_ERR(debugfs_root) || !debugfs_root) {
1965 printk(KERN_WARNING
1966 "ASoC: Failed to create debugfs directory\n");
1967 debugfs_root = NULL;
1968 }
1969#endif
1970
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001971 return platform_driver_register(&soc_driver);
1972}
1973
Mark Brown7d8c16a2008-11-30 22:11:24 +00001974static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001975{
Mark Brown384c89e2008-12-03 17:34:03 +00001976#ifdef CONFIG_DEBUG_FS
1977 debugfs_remove_recursive(debugfs_root);
1978#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02001979 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001980}
1981
1982module_init(snd_soc_init);
1983module_exit(snd_soc_exit);
1984
1985/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01001986MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001987MODULE_DESCRIPTION("ALSA SoC Core");
1988MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02001989MODULE_ALIAS("platform:soc-audio");