blob: 4707042b3dadb388db221b9c0ad4c8a5d32c4372 [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
38/* debug */
39#define SOC_DEBUG 0
40#if SOC_DEBUG
41#define dbg(format, arg...) printk(format, ## arg)
42#else
43#define dbg(format, arg...)
44#endif
Liam Girdwooda71a4682006-10-19 20:35:56 +020045
Frank Mandarinodb2a4162006-10-06 18:31:09 +020046static DEFINE_MUTEX(pcm_mutex);
47static DEFINE_MUTEX(io_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020048static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
49
Frank Mandarinodb2a4162006-10-06 18:31:09 +020050/*
51 * This is a timeout to do a DAPM powerdown after a stream is closed().
52 * It can be used to eliminate pops between different playback streams, e.g.
53 * between two audio tracks.
54 */
55static int pmdown_time = 5000;
56module_param(pmdown_time, int, 0);
57MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
58
Liam Girdwood965ac422007-01-31 14:14:57 +010059/*
60 * This function forces any delayed work to be queued and run.
61 */
62static int run_delayed_work(struct delayed_work *dwork)
63{
64 int ret;
65
66 /* cancel any work waiting to be queued. */
67 ret = cancel_delayed_work(dwork);
68
69 /* if there was any work waiting then we run it now and
70 * wait for it's completion */
71 if (ret) {
72 schedule_delayed_work(dwork, 0);
73 flush_scheduled_work();
74 }
75 return ret;
76}
77
Frank Mandarinodb2a4162006-10-06 18:31:09 +020078#ifdef CONFIG_SND_SOC_AC97_BUS
79/* unregister ac97 codec */
80static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
81{
82 if (codec->ac97->dev.bus)
83 device_unregister(&codec->ac97->dev);
84 return 0;
85}
86
87/* stop no dev release warning */
88static void soc_ac97_device_release(struct device *dev){}
89
90/* register ac97 codec to bus */
91static int soc_ac97_dev_register(struct snd_soc_codec *codec)
92{
93 int err;
94
95 codec->ac97->dev.bus = &ac97_bus_type;
96 codec->ac97->dev.parent = NULL;
97 codec->ac97->dev.release = soc_ac97_device_release;
98
99 snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
100 codec->card->number, 0, codec->name);
101 err = device_register(&codec->ac97->dev);
102 if (err < 0) {
103 snd_printk(KERN_ERR "Can't register ac97 bus\n");
104 codec->ac97->dev.bus = NULL;
105 return err;
106 }
107 return 0;
108}
109#endif
110
Mark Brown3ff3f642008-05-19 12:32:25 +0200111static inline const char *get_dai_name(int type)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200112{
Mark Brown3ff3f642008-05-19 12:32:25 +0200113 switch (type) {
Liam Girdwooda68660e2007-05-10 19:27:27 +0200114 case SND_SOC_DAI_AC97_BUS:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200115 case SND_SOC_DAI_AC97:
116 return "AC97";
117 case SND_SOC_DAI_I2S:
118 return "I2S";
119 case SND_SOC_DAI_PCM:
120 return "PCM";
121 }
122 return NULL;
123}
124
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200125/*
126 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
127 * then initialized and any private data can be allocated. This also calls
128 * startup for the cpu DAI, platform, machine and codec DAI.
129 */
130static int soc_pcm_open(struct snd_pcm_substream *substream)
131{
132 struct snd_soc_pcm_runtime *rtd = substream->private_data;
133 struct snd_soc_device *socdev = rtd->socdev;
134 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100135 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200136 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100137 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
138 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200139 int ret = 0;
140
141 mutex_lock(&pcm_mutex);
142
143 /* startup the audio subsystem */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100144 if (cpu_dai->ops.startup) {
145 ret = cpu_dai->ops.startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200146 if (ret < 0) {
147 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100148 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200149 goto out;
150 }
151 }
152
153 if (platform->pcm_ops->open) {
154 ret = platform->pcm_ops->open(substream);
155 if (ret < 0) {
156 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
157 goto platform_err;
158 }
159 }
160
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100161 if (codec_dai->ops.startup) {
162 ret = codec_dai->ops.startup(substream);
163 if (ret < 0) {
164 printk(KERN_ERR "asoc: can't open codec %s\n",
165 codec_dai->name);
166 goto codec_dai_err;
167 }
168 }
169
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200170 if (machine->ops && machine->ops->startup) {
171 ret = machine->ops->startup(substream);
172 if (ret < 0) {
173 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
174 goto machine_err;
175 }
176 }
177
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200178 /* Check that the codec and cpu DAI's are compatible */
179 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
180 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200181 max(codec_dai->playback.rate_min,
182 cpu_dai->playback.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->playback.rate_max,
185 cpu_dai->playback.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->playback.channels_min,
188 cpu_dai->playback.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->playback.channels_max,
191 cpu_dai->playback.channels_max);
192 runtime->hw.formats =
193 codec_dai->playback.formats & cpu_dai->playback.formats;
194 runtime->hw.rates =
195 codec_dai->playback.rates & cpu_dai->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200196 } else {
197 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200198 max(codec_dai->capture.rate_min,
199 cpu_dai->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200200 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200201 min(codec_dai->capture.rate_max,
202 cpu_dai->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200203 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100204 max(codec_dai->capture.channels_min,
205 cpu_dai->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200206 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100207 min(codec_dai->capture.channels_max,
208 cpu_dai->capture.channels_max);
209 runtime->hw.formats =
210 codec_dai->capture.formats & cpu_dai->capture.formats;
211 runtime->hw.rates =
212 codec_dai->capture.rates & cpu_dai->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200213 }
214
215 snd_pcm_limit_hw_rates(runtime);
216 if (!runtime->hw.rates) {
217 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100218 codec_dai->name, cpu_dai->name);
219 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200220 }
221 if (!runtime->hw.formats) {
222 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100223 codec_dai->name, cpu_dai->name);
224 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200225 }
226 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
227 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100228 codec_dai->name, cpu_dai->name);
229 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200230 }
231
Mark Brown3ff3f642008-05-19 12:32:25 +0200232 dbg("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
Liam Girdwoodb5c5fd22006-10-13 19:13:41 +0200233 dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
234 dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
235 runtime->hw.channels_max);
236 dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
237 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200238
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200239 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100240 cpu_dai->playback.active = codec_dai->playback.active = 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200241 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100242 cpu_dai->capture.active = codec_dai->capture.active = 1;
243 cpu_dai->active = codec_dai->active = 1;
244 cpu_dai->runtime = runtime;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200245 socdev->codec->active++;
246 mutex_unlock(&pcm_mutex);
247 return 0;
248
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100249machine_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200250 if (machine->ops && machine->ops->shutdown)
251 machine->ops->shutdown(substream);
252
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100253codec_dai_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200254 if (platform->pcm_ops->close)
255 platform->pcm_ops->close(substream);
256
257platform_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100258 if (cpu_dai->ops.shutdown)
259 cpu_dai->ops.shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200260out:
261 mutex_unlock(&pcm_mutex);
262 return ret;
263}
264
265/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200266 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200267 * This is to ensure there are no pops or clicks in between any music tracks
268 * due to DAPM power cycling.
269 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100270static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200271{
Andrew Morton4484bb22006-12-15 09:30:07 +0100272 struct snd_soc_device *socdev =
273 container_of(work, struct snd_soc_device, delayed_work.work);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200274 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100275 struct snd_soc_dai *codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200276 int i;
277
278 mutex_lock(&pcm_mutex);
Mark Brown3ff3f642008-05-19 12:32:25 +0200279 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200280 codec_dai = &codec->dai[i];
281
282 dbg("pop wq checking: %s status: %s waiting: %s\n",
283 codec_dai->playback.stream_name,
284 codec_dai->playback.active ? "active" : "inactive",
285 codec_dai->pop_wait ? "yes" : "no");
286
287 /* are we waiting on this codec DAI stream */
288 if (codec_dai->pop_wait == 1) {
289
Mark Brown0be98982008-05-19 12:31:28 +0200290 /* Reduce power if no longer active */
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100291 if (codec->active == 0) {
292 dbg("pop wq D1 %s %s\n", codec->name,
293 codec_dai->playback.stream_name);
Mark Brown0be98982008-05-19 12:31:28 +0200294 snd_soc_dapm_set_bias_level(socdev,
295 SND_SOC_BIAS_PREPARE);
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100296 }
297
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200298 codec_dai->pop_wait = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100299 snd_soc_dapm_stream_event(codec,
300 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200301 SND_SOC_DAPM_STREAM_STOP);
302
Mark Brown0be98982008-05-19 12:31:28 +0200303 /* Fall into standby if no longer active */
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200304 if (codec->active == 0) {
305 dbg("pop wq D3 %s %s\n", codec->name,
306 codec_dai->playback.stream_name);
Mark Brown0be98982008-05-19 12:31:28 +0200307 snd_soc_dapm_set_bias_level(socdev,
308 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200309 }
310 }
311 }
312 mutex_unlock(&pcm_mutex);
313}
314
315/*
316 * Called by ALSA when a PCM substream is closed. Private data can be
317 * freed here. The cpu DAI, codec DAI, machine and platform are also
318 * shutdown.
319 */
320static int soc_codec_close(struct snd_pcm_substream *substream)
321{
322 struct snd_soc_pcm_runtime *rtd = substream->private_data;
323 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100324 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200325 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100326 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
327 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200328 struct snd_soc_codec *codec = socdev->codec;
329
330 mutex_lock(&pcm_mutex);
331
332 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100333 cpu_dai->playback.active = codec_dai->playback.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200334 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100335 cpu_dai->capture.active = codec_dai->capture.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200336
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100337 if (codec_dai->playback.active == 0 &&
338 codec_dai->capture.active == 0) {
339 cpu_dai->active = codec_dai->active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200340 }
341 codec->active--;
342
Mark Brown6010b2d2008-09-06 18:33:24 +0100343 /* Muting the DAC suppresses artifacts caused during digital
344 * shutdown, for example from stopping clocks.
345 */
346 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
347 snd_soc_dai_digital_mute(codec_dai, 1);
348
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100349 if (cpu_dai->ops.shutdown)
350 cpu_dai->ops.shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200351
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100352 if (codec_dai->ops.shutdown)
353 codec_dai->ops.shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200354
355 if (machine->ops && machine->ops->shutdown)
356 machine->ops->shutdown(substream);
357
358 if (platform->pcm_ops->close)
359 platform->pcm_ops->close(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100360 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200361
362 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
363 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100364 codec_dai->pop_wait = 1;
Takashi Iwai4bb09522006-12-19 17:16:14 +0100365 schedule_delayed_work(&socdev->delayed_work,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200366 msecs_to_jiffies(pmdown_time));
367 } else {
368 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100369 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100370 codec_dai->capture.stream_name,
371 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200372
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100373 if (codec->active == 0 && codec_dai->pop_wait == 0)
Mark Brown0be98982008-05-19 12:31:28 +0200374 snd_soc_dapm_set_bias_level(socdev,
375 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200376 }
377
378 mutex_unlock(&pcm_mutex);
379 return 0;
380}
381
382/*
383 * Called by ALSA when the PCM substream is prepared, can set format, sample
384 * rate, etc. This function is non atomic and can be called multiple times,
385 * it can refer to the runtime info.
386 */
387static int soc_pcm_prepare(struct snd_pcm_substream *substream)
388{
389 struct snd_soc_pcm_runtime *rtd = substream->private_data;
390 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100391 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200392 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100393 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
394 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200395 struct snd_soc_codec *codec = socdev->codec;
396 int ret = 0;
397
398 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100399
400 if (machine->ops && machine->ops->prepare) {
401 ret = machine->ops->prepare(substream);
402 if (ret < 0) {
403 printk(KERN_ERR "asoc: machine prepare error\n");
404 goto out;
405 }
406 }
407
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200408 if (platform->pcm_ops->prepare) {
409 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200410 if (ret < 0) {
411 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200412 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200413 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200414 }
415
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100416 if (codec_dai->ops.prepare) {
417 ret = codec_dai->ops.prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200418 if (ret < 0) {
419 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200420 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200421 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200422 }
423
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100424 if (cpu_dai->ops.prepare) {
425 ret = cpu_dai->ops.prepare(substream);
426 if (ret < 0) {
427 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
428 goto out;
429 }
430 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200431
432 /* we only want to start a DAPM playback stream if we are not waiting
433 * on an existing one stopping */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100434 if (codec_dai->pop_wait) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200435 /* we are waiting for the delayed work to start */
436 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100437 snd_soc_dapm_stream_event(socdev->codec,
438 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200439 SND_SOC_DAPM_STREAM_START);
440 else {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100441 codec_dai->pop_wait = 0;
Andrew Morton4484bb22006-12-15 09:30:07 +0100442 cancel_delayed_work(&socdev->delayed_work);
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100443 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200444 }
445 } else {
446 /* no delayed work - do we need to power up codec */
Mark Brown0be98982008-05-19 12:31:28 +0200447 if (codec->bias_level != SND_SOC_BIAS_ON) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200448
Mark Brown0be98982008-05-19 12:31:28 +0200449 snd_soc_dapm_set_bias_level(socdev,
450 SND_SOC_BIAS_PREPARE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200451
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);
456 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 Brown0be98982008-05-19 12:31:28 +0200461 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100462 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200463
464 } else {
465 /* codec already powered - power on widgets */
466 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
467 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100468 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200469 SND_SOC_DAPM_STREAM_START);
470 else
471 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100472 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200473 SND_SOC_DAPM_STREAM_START);
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100474
475 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200476 }
477 }
478
479out:
480 mutex_unlock(&pcm_mutex);
481 return ret;
482}
483
484/*
485 * Called by ALSA when the hardware params are set by application. This
486 * function can also be called multiple times and can allocate buffers
487 * (using snd_pcm_lib_* ). It's non-atomic.
488 */
489static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
490 struct snd_pcm_hw_params *params)
491{
492 struct snd_soc_pcm_runtime *rtd = substream->private_data;
493 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100494 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200495 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100496 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
497 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200498 int ret = 0;
499
500 mutex_lock(&pcm_mutex);
501
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100502 if (machine->ops && machine->ops->hw_params) {
503 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200504 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100505 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200506 goto out;
507 }
508 }
509
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100510 if (codec_dai->ops.hw_params) {
511 ret = codec_dai->ops.hw_params(substream, params);
512 if (ret < 0) {
513 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
514 codec_dai->name);
515 goto codec_err;
516 }
517 }
518
519 if (cpu_dai->ops.hw_params) {
520 ret = cpu_dai->ops.hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200521 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200522 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100523 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200524 goto interface_err;
525 }
526 }
527
528 if (platform->pcm_ops->hw_params) {
529 ret = platform->pcm_ops->hw_params(substream, params);
530 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200531 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200532 platform->name);
533 goto platform_err;
534 }
535 }
536
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200537out:
538 mutex_unlock(&pcm_mutex);
539 return ret;
540
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200541platform_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100542 if (cpu_dai->ops.hw_free)
543 cpu_dai->ops.hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200544
545interface_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100546 if (codec_dai->ops.hw_free)
547 codec_dai->ops.hw_free(substream);
548
549codec_err:
Mark Brown3ff3f642008-05-19 12:32:25 +0200550 if (machine->ops && machine->ops->hw_free)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100551 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200552
553 mutex_unlock(&pcm_mutex);
554 return ret;
555}
556
557/*
558 * Free's resources allocated by hw_params, can be called multiple times
559 */
560static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
561{
562 struct snd_soc_pcm_runtime *rtd = substream->private_data;
563 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100564 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200565 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100566 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
567 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200568 struct snd_soc_codec *codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200569
570 mutex_lock(&pcm_mutex);
571
572 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100573 if (!codec->active)
574 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200575
576 /* free any machine hw params */
577 if (machine->ops && machine->ops->hw_free)
578 machine->ops->hw_free(substream);
579
580 /* free any DMA resources */
581 if (platform->pcm_ops->hw_free)
582 platform->pcm_ops->hw_free(substream);
583
584 /* now free hw params for the DAI's */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100585 if (codec_dai->ops.hw_free)
586 codec_dai->ops.hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200587
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100588 if (cpu_dai->ops.hw_free)
589 cpu_dai->ops.hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200590
591 mutex_unlock(&pcm_mutex);
592 return 0;
593}
594
595static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
596{
597 struct snd_soc_pcm_runtime *rtd = substream->private_data;
598 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100599 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200600 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100601 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
602 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200603 int ret;
604
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100605 if (codec_dai->ops.trigger) {
606 ret = codec_dai->ops.trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200607 if (ret < 0)
608 return ret;
609 }
610
611 if (platform->pcm_ops->trigger) {
612 ret = platform->pcm_ops->trigger(substream, cmd);
613 if (ret < 0)
614 return ret;
615 }
616
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100617 if (cpu_dai->ops.trigger) {
618 ret = cpu_dai->ops.trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200619 if (ret < 0)
620 return ret;
621 }
622 return 0;
623}
624
625/* ASoC PCM operations */
626static struct snd_pcm_ops soc_pcm_ops = {
627 .open = soc_pcm_open,
628 .close = soc_codec_close,
629 .hw_params = soc_pcm_hw_params,
630 .hw_free = soc_pcm_hw_free,
631 .prepare = soc_pcm_prepare,
632 .trigger = soc_pcm_trigger,
633};
634
635#ifdef CONFIG_PM
636/* powers down audio subsystem for suspend */
637static int soc_suspend(struct platform_device *pdev, pm_message_t state)
638{
Mark Brown3ff3f642008-05-19 12:32:25 +0200639 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
640 struct snd_soc_machine *machine = socdev->machine;
641 struct snd_soc_platform *platform = socdev->platform;
642 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200643 struct snd_soc_codec *codec = socdev->codec;
644 int i;
645
Andy Green6ed25972008-06-13 16:24:05 +0100646 /* Due to the resume being scheduled into a workqueue we could
647 * suspend before that's finished - wait for it to complete.
648 */
649 snd_power_lock(codec->card);
650 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
651 snd_power_unlock(codec->card);
652
653 /* we're going to block userspace touching us until resume completes */
654 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
655
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200656 /* mute any active DAC's */
Mark Brown3ff3f642008-05-19 12:32:25 +0200657 for (i = 0; i < machine->num_links; i++) {
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100658 struct snd_soc_dai *dai = machine->dai_link[i].codec_dai;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100659 if (dai->dai_ops.digital_mute && dai->playback.active)
660 dai->dai_ops.digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200661 }
662
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100663 /* suspend all pcms */
664 for (i = 0; i < machine->num_links; i++)
665 snd_pcm_suspend_all(machine->dai_link[i].pcm);
666
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200667 if (machine->suspend_pre)
668 machine->suspend_pre(pdev, state);
669
Mark Brown3ff3f642008-05-19 12:32:25 +0200670 for (i = 0; i < machine->num_links; i++) {
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100671 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200672 if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
673 cpu_dai->suspend(pdev, cpu_dai);
674 if (platform->suspend)
675 platform->suspend(pdev, cpu_dai);
676 }
677
678 /* close any waiting streams and save state */
Liam Girdwood965ac422007-01-31 14:14:57 +0100679 run_delayed_work(&socdev->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200680 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200681
Mark Brown3ff3f642008-05-19 12:32:25 +0200682 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200683 char *stream = codec->dai[i].playback.stream_name;
684 if (stream != NULL)
685 snd_soc_dapm_stream_event(codec, stream,
686 SND_SOC_DAPM_STREAM_SUSPEND);
687 stream = codec->dai[i].capture.stream_name;
688 if (stream != NULL)
689 snd_soc_dapm_stream_event(codec, stream,
690 SND_SOC_DAPM_STREAM_SUSPEND);
691 }
692
693 if (codec_dev->suspend)
694 codec_dev->suspend(pdev, state);
695
Mark Brown3ff3f642008-05-19 12:32:25 +0200696 for (i = 0; i < machine->num_links; i++) {
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100697 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200698 if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
699 cpu_dai->suspend(pdev, cpu_dai);
700 }
701
702 if (machine->suspend_post)
703 machine->suspend_post(pdev, state);
704
705 return 0;
706}
707
Andy Green6ed25972008-06-13 16:24:05 +0100708/* deferred resume work, so resume can complete before we finished
709 * setting our codec back up, which can be very slow on I2C
710 */
711static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200712{
Andy Green6ed25972008-06-13 16:24:05 +0100713 struct snd_soc_device *socdev = container_of(work,
714 struct snd_soc_device,
715 deferred_resume_work);
Mark Brown3ff3f642008-05-19 12:32:25 +0200716 struct snd_soc_machine *machine = socdev->machine;
717 struct snd_soc_platform *platform = socdev->platform;
718 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200719 struct snd_soc_codec *codec = socdev->codec;
Andy Green6ed25972008-06-13 16:24:05 +0100720 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200721 int i;
722
Andy Green6ed25972008-06-13 16:24:05 +0100723 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
724 * so userspace apps are blocked from touching us
725 */
726
727 dev_info(socdev->dev, "starting resume work\n");
728
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200729 if (machine->resume_pre)
730 machine->resume_pre(pdev);
731
Mark Brown3ff3f642008-05-19 12:32:25 +0200732 for (i = 0; i < machine->num_links; i++) {
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100733 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200734 if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
735 cpu_dai->resume(pdev, cpu_dai);
736 }
737
738 if (codec_dev->resume)
739 codec_dev->resume(pdev);
740
Mark Brown3ff3f642008-05-19 12:32:25 +0200741 for (i = 0; i < codec->num_dai; i++) {
742 char *stream = codec->dai[i].playback.stream_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200743 if (stream != NULL)
744 snd_soc_dapm_stream_event(codec, stream,
745 SND_SOC_DAPM_STREAM_RESUME);
746 stream = codec->dai[i].capture.stream_name;
747 if (stream != NULL)
748 snd_soc_dapm_stream_event(codec, stream,
749 SND_SOC_DAPM_STREAM_RESUME);
750 }
751
Mark Brown3ff3f642008-05-19 12:32:25 +0200752 /* unmute any active DACs */
753 for (i = 0; i < machine->num_links; i++) {
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100754 struct snd_soc_dai *dai = machine->dai_link[i].codec_dai;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100755 if (dai->dai_ops.digital_mute && dai->playback.active)
756 dai->dai_ops.digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200757 }
758
Mark Brown3ff3f642008-05-19 12:32:25 +0200759 for (i = 0; i < machine->num_links; i++) {
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100760 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200761 if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
762 cpu_dai->resume(pdev, cpu_dai);
763 if (platform->resume)
764 platform->resume(pdev, cpu_dai);
765 }
766
767 if (machine->resume_post)
768 machine->resume_post(pdev);
769
Andy Green6ed25972008-06-13 16:24:05 +0100770 dev_info(socdev->dev, "resume work completed\n");
771
772 /* userspace can access us now we are back as we were before */
773 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
774}
775
776/* powers up audio subsystem after a suspend */
777static int soc_resume(struct platform_device *pdev)
778{
779 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
780
781 dev_info(socdev->dev, "scheduling resume work\n");
782
783 if (!schedule_work(&socdev->deferred_resume_work))
784 dev_err(socdev->dev, "work item may be lost\n");
785
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200786 return 0;
787}
788
789#else
790#define soc_suspend NULL
791#define soc_resume NULL
792#endif
793
794/* probes a new socdev */
795static int soc_probe(struct platform_device *pdev)
796{
797 int ret = 0, i;
798 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
799 struct snd_soc_machine *machine = socdev->machine;
800 struct snd_soc_platform *platform = socdev->platform;
801 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
802
803 if (machine->probe) {
804 ret = machine->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200805 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200806 return ret;
807 }
808
809 for (i = 0; i < machine->num_links; i++) {
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100810 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200811 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +0100812 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +0200813 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200814 goto cpu_dai_err;
815 }
816 }
817
818 if (codec_dev->probe) {
819 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200820 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200821 goto cpu_dai_err;
822 }
823
824 if (platform->probe) {
825 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200826 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200827 goto platform_err;
828 }
829
830 /* DAPM stream work */
Andrew Morton4484bb22006-12-15 09:30:07 +0100831 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +0100832#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +0100833 /* deferred resume work */
834 INIT_WORK(&socdev->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +0100835#endif
Andy Green6ed25972008-06-13 16:24:05 +0100836
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200837 return 0;
838
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200839platform_err:
840 if (codec_dev->remove)
841 codec_dev->remove(pdev);
842
843cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +0100844 for (i--; i >= 0; i--) {
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100845 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200846 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100847 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200848 }
849
850 if (machine->remove)
851 machine->remove(pdev);
852
853 return ret;
854}
855
856/* removes a socdev */
857static int soc_remove(struct platform_device *pdev)
858{
859 int i;
860 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
861 struct snd_soc_machine *machine = socdev->machine;
862 struct snd_soc_platform *platform = socdev->platform;
863 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
864
Liam Girdwood965ac422007-01-31 14:14:57 +0100865 run_delayed_work(&socdev->delayed_work);
866
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200867 if (platform->remove)
868 platform->remove(pdev);
869
870 if (codec_dev->remove)
871 codec_dev->remove(pdev);
872
873 for (i = 0; i < machine->num_links; i++) {
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100874 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200875 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100876 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200877 }
878
879 if (machine->remove)
880 machine->remove(pdev);
881
882 return 0;
883}
884
885/* ASoC platform driver */
886static struct platform_driver soc_driver = {
887 .driver = {
888 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +0200889 .owner = THIS_MODULE,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200890 },
891 .probe = soc_probe,
892 .remove = soc_remove,
893 .suspend = soc_suspend,
894 .resume = soc_resume,
895};
896
897/* create a new pcm */
898static int soc_new_pcm(struct snd_soc_device *socdev,
899 struct snd_soc_dai_link *dai_link, int num)
900{
901 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100902 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
903 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200904 struct snd_soc_pcm_runtime *rtd;
905 struct snd_pcm *pcm;
906 char new_name[64];
907 int ret = 0, playback = 0, capture = 0;
908
909 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
910 if (rtd == NULL)
911 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100912
913 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200914 rtd->socdev = socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100915 codec_dai->codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200916
917 /* check client and interface hw capabilities */
Mark Brown3ff3f642008-05-19 12:32:25 +0200918 sprintf(new_name, "%s %s-%s-%d", dai_link->stream_name, codec_dai->name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200919 get_dai_name(cpu_dai->type), num);
920
921 if (codec_dai->playback.channels_min)
922 playback = 1;
923 if (codec_dai->capture.channels_min)
924 capture = 1;
925
926 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
927 capture, &pcm);
928 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200929 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
930 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200931 kfree(rtd);
932 return ret;
933 }
934
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100935 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200936 pcm->private_data = rtd;
937 soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
938 soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
939 soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
940 soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
941 soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
942 soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
943 soc_pcm_ops.page = socdev->platform->pcm_ops->page;
944
945 if (playback)
946 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
947
948 if (capture)
949 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
950
951 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
952 if (ret < 0) {
953 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
954 kfree(rtd);
955 return ret;
956 }
957
958 pcm->private_free = socdev->platform->pcm_free;
959 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
960 cpu_dai->name);
961 return ret;
962}
963
964/* codec register dump */
Troy Kisky12ef1932008-10-13 17:42:14 -0700965static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200966{
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200967 struct snd_soc_codec *codec = devdata->codec;
968 int i, step = 1, count = 0;
969
970 if (!codec->reg_cache_size)
971 return 0;
972
973 if (codec->reg_cache_step)
974 step = codec->reg_cache_step;
975
976 count += sprintf(buf, "%s registers\n", codec->name);
Mark Brown58cd33c2008-07-29 11:42:25 +0100977 for (i = 0; i < codec->reg_cache_size; i += step) {
978 count += sprintf(buf + count, "%2x: ", i);
979 if (count >= PAGE_SIZE - 1)
980 break;
981
982 if (codec->display_register)
983 count += codec->display_register(codec, buf + count,
984 PAGE_SIZE - count, i);
985 else
986 count += snprintf(buf + count, PAGE_SIZE - count,
987 "%4x", codec->read(codec, i));
988
989 if (count >= PAGE_SIZE - 1)
990 break;
991
992 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
993 if (count >= PAGE_SIZE - 1)
994 break;
995 }
996
997 /* Truncate count; min() would cause a warning */
998 if (count >= PAGE_SIZE)
999 count = PAGE_SIZE - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001000
1001 return count;
1002}
Troy Kisky12ef1932008-10-13 17:42:14 -07001003static ssize_t codec_reg_show(struct device *dev,
1004 struct device_attribute *attr, char *buf)
1005{
1006 struct snd_soc_device *devdata = dev_get_drvdata(dev);
1007 return soc_codec_reg_show(devdata, buf);
1008}
1009
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001010static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1011
Troy Kisky12ef1932008-10-13 17:42:14 -07001012#ifdef CONFIG_DEBUG_FS
1013static int codec_reg_open_file(struct inode *inode, struct file *file)
1014{
1015 file->private_data = inode->i_private;
1016 return 0;
1017}
1018
1019static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1020 size_t count, loff_t *ppos)
1021{
1022 ssize_t ret;
1023 struct snd_soc_device *devdata = file->private_data;
1024 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1025 if (!buf)
1026 return -ENOMEM;
1027 ret = soc_codec_reg_show(devdata, buf);
1028 if (ret >= 0)
1029 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1030 kfree(buf);
1031 return ret;
1032}
1033
1034static ssize_t codec_reg_write_file(struct file *file,
1035 const char __user *user_buf, size_t count, loff_t *ppos)
1036{
1037 char buf[32];
1038 int buf_size;
1039 char *start = buf;
1040 unsigned long reg, value;
1041 int step = 1;
1042 struct snd_soc_device *devdata = file->private_data;
1043 struct snd_soc_codec *codec = devdata->codec;
1044
1045 buf_size = min(count, (sizeof(buf)-1));
1046 if (copy_from_user(buf, user_buf, buf_size))
1047 return -EFAULT;
1048 buf[buf_size] = 0;
1049
1050 if (codec->reg_cache_step)
1051 step = codec->reg_cache_step;
1052
1053 while (*start == ' ')
1054 start++;
1055 reg = simple_strtoul(start, &start, 16);
1056 if ((reg >= codec->reg_cache_size) || (reg % step))
1057 return -EINVAL;
1058 while (*start == ' ')
1059 start++;
1060 if (strict_strtoul(start, 16, &value))
1061 return -EINVAL;
1062 codec->write(codec, reg, value);
1063 return buf_size;
1064}
1065
1066static const struct file_operations codec_reg_fops = {
1067 .open = codec_reg_open_file,
1068 .read = codec_reg_read_file,
1069 .write = codec_reg_write_file,
1070};
1071
1072static void soc_init_debugfs(struct snd_soc_device *socdev)
1073{
1074 struct dentry *root, *file;
1075 struct snd_soc_codec *codec = socdev->codec;
1076 root = debugfs_create_dir(dev_name(socdev->dev), NULL);
1077 if (IS_ERR(root) || !root)
1078 goto exit1;
1079
1080 file = debugfs_create_file("codec_reg", 0644,
1081 root, socdev, &codec_reg_fops);
1082 if (!file)
1083 goto exit2;
1084
1085 file = debugfs_create_u32("dapm_pop_time", 0744,
1086 root, &codec->pop_time);
1087 if (!file)
1088 goto exit2;
1089 socdev->debugfs_root = root;
1090 return;
1091exit2:
1092 debugfs_remove_recursive(root);
1093exit1:
1094 dev_err(socdev->dev, "debugfs is not available\n");
1095}
1096
1097static void soc_cleanup_debugfs(struct snd_soc_device *socdev)
1098{
1099 debugfs_remove_recursive(socdev->debugfs_root);
1100 socdev->debugfs_root = NULL;
1101}
1102
1103#else
1104
1105static inline void soc_init_debugfs(struct snd_soc_device *socdev)
1106{
1107}
1108
1109static inline void soc_cleanup_debugfs(struct snd_soc_device *socdev)
1110{
1111}
1112#endif
1113
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001114/**
1115 * snd_soc_new_ac97_codec - initailise AC97 device
1116 * @codec: audio codec
1117 * @ops: AC97 bus operations
1118 * @num: AC97 codec number
1119 *
1120 * Initialises AC97 codec resources for use by ad-hoc devices only.
1121 */
1122int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1123 struct snd_ac97_bus_ops *ops, int num)
1124{
1125 mutex_lock(&codec->mutex);
1126
1127 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1128 if (codec->ac97 == NULL) {
1129 mutex_unlock(&codec->mutex);
1130 return -ENOMEM;
1131 }
1132
1133 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1134 if (codec->ac97->bus == NULL) {
1135 kfree(codec->ac97);
1136 codec->ac97 = NULL;
1137 mutex_unlock(&codec->mutex);
1138 return -ENOMEM;
1139 }
1140
1141 codec->ac97->bus->ops = ops;
1142 codec->ac97->num = num;
1143 mutex_unlock(&codec->mutex);
1144 return 0;
1145}
1146EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1147
1148/**
1149 * snd_soc_free_ac97_codec - free AC97 codec device
1150 * @codec: audio codec
1151 *
1152 * Frees AC97 codec device resources.
1153 */
1154void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1155{
1156 mutex_lock(&codec->mutex);
1157 kfree(codec->ac97->bus);
1158 kfree(codec->ac97);
1159 codec->ac97 = NULL;
1160 mutex_unlock(&codec->mutex);
1161}
1162EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1163
1164/**
1165 * snd_soc_update_bits - update codec register bits
1166 * @codec: audio codec
1167 * @reg: codec register
1168 * @mask: register mask
1169 * @value: new value
1170 *
1171 * Writes new register value.
1172 *
1173 * Returns 1 for change else 0.
1174 */
1175int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1176 unsigned short mask, unsigned short value)
1177{
1178 int change;
1179 unsigned short old, new;
1180
1181 mutex_lock(&io_mutex);
1182 old = snd_soc_read(codec, reg);
1183 new = (old & ~mask) | value;
1184 change = old != new;
1185 if (change)
1186 snd_soc_write(codec, reg, new);
1187
1188 mutex_unlock(&io_mutex);
1189 return change;
1190}
1191EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1192
1193/**
1194 * snd_soc_test_bits - test register for change
1195 * @codec: audio codec
1196 * @reg: codec register
1197 * @mask: register mask
1198 * @value: new value
1199 *
1200 * Tests a register with a new value and checks if the new value is
1201 * different from the old value.
1202 *
1203 * Returns 1 for change else 0.
1204 */
1205int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1206 unsigned short mask, unsigned short value)
1207{
1208 int change;
1209 unsigned short old, new;
1210
1211 mutex_lock(&io_mutex);
1212 old = snd_soc_read(codec, reg);
1213 new = (old & ~mask) | value;
1214 change = old != new;
1215 mutex_unlock(&io_mutex);
1216
1217 return change;
1218}
1219EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1220
1221/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001222 * snd_soc_new_pcms - create new sound card and pcms
1223 * @socdev: the SoC audio device
1224 *
1225 * Create a new sound card based upon the codec and interface pcms.
1226 *
1227 * Returns 0 for success, else error.
1228 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001229int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001230{
1231 struct snd_soc_codec *codec = socdev->codec;
1232 struct snd_soc_machine *machine = socdev->machine;
1233 int ret = 0, i;
1234
1235 mutex_lock(&codec->mutex);
1236
1237 /* register a sound card */
1238 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1239 if (!codec->card) {
1240 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1241 codec->name);
1242 mutex_unlock(&codec->mutex);
1243 return -ENODEV;
1244 }
1245
1246 codec->card->dev = socdev->dev;
1247 codec->card->private_data = codec;
1248 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1249
1250 /* create the pcms */
Mark Brown3ff3f642008-05-19 12:32:25 +02001251 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001252 ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
1253 if (ret < 0) {
1254 printk(KERN_ERR "asoc: can't create pcm %s\n",
1255 machine->dai_link[i].stream_name);
1256 mutex_unlock(&codec->mutex);
1257 return ret;
1258 }
1259 }
1260
1261 mutex_unlock(&codec->mutex);
1262 return ret;
1263}
1264EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1265
1266/**
1267 * snd_soc_register_card - register sound card
1268 * @socdev: the SoC audio device
1269 *
1270 * Register a SoC sound card. Also registers an AC97 device if the
1271 * codec is AC97 for ad hoc devices.
1272 *
1273 * Returns 0 for success, else error.
1274 */
1275int snd_soc_register_card(struct snd_soc_device *socdev)
1276{
1277 struct snd_soc_codec *codec = socdev->codec;
1278 struct snd_soc_machine *machine = socdev->machine;
Liam Girdwood12e74f72006-10-16 21:19:48 +02001279 int ret = 0, i, ac97 = 0, err = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001280
Mark Brown3ff3f642008-05-19 12:32:25 +02001281 for (i = 0; i < machine->num_links; i++) {
Liam Girdwood12e74f72006-10-16 21:19:48 +02001282 if (socdev->machine->dai_link[i].init) {
1283 err = socdev->machine->dai_link[i].init(codec);
1284 if (err < 0) {
1285 printk(KERN_ERR "asoc: failed to init %s\n",
1286 socdev->machine->dai_link[i].stream_name);
1287 continue;
1288 }
1289 }
Mark Brown3ff3f642008-05-19 12:32:25 +02001290 if (socdev->machine->dai_link[i].codec_dai->type ==
Liam Girdwooda68660e2007-05-10 19:27:27 +02001291 SND_SOC_DAI_AC97_BUS)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001292 ac97 = 1;
1293 }
1294 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1295 "%s", machine->name);
1296 snprintf(codec->card->longname, sizeof(codec->card->longname),
1297 "%s (%s)", machine->name, codec->name);
1298
1299 ret = snd_card_register(codec->card);
1300 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001301 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001302 codec->name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001303 goto out;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001304 }
1305
Mark Brown08c8efe2008-01-21 14:33:37 +01001306 mutex_lock(&codec->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001307#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood12e74f72006-10-16 21:19:48 +02001308 if (ac97) {
1309 ret = soc_ac97_dev_register(codec);
1310 if (ret < 0) {
1311 printk(KERN_ERR "asoc: AC97 device register failed\n");
1312 snd_card_free(codec->card);
Mark Brown08c8efe2008-01-21 14:33:37 +01001313 mutex_unlock(&codec->mutex);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001314 goto out;
1315 }
1316 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001317#endif
1318
Liam Girdwood12e74f72006-10-16 21:19:48 +02001319 err = snd_soc_dapm_sys_add(socdev->dev);
1320 if (err < 0)
1321 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1322
1323 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1324 if (err < 0)
Mark Brown3ff3f642008-05-19 12:32:25 +02001325 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
Mark Brown08c8efe2008-01-21 14:33:37 +01001326
Troy Kisky12ef1932008-10-13 17:42:14 -07001327 soc_init_debugfs(socdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001328 mutex_unlock(&codec->mutex);
Mark Brown08c8efe2008-01-21 14:33:37 +01001329
1330out:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001331 return ret;
1332}
1333EXPORT_SYMBOL_GPL(snd_soc_register_card);
1334
1335/**
1336 * snd_soc_free_pcms - free sound card and pcms
1337 * @socdev: the SoC audio device
1338 *
1339 * Frees sound card and pcms associated with the socdev.
1340 * Also unregister the codec if it is an AC97 device.
1341 */
1342void snd_soc_free_pcms(struct snd_soc_device *socdev)
1343{
1344 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001345#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001346 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001347 int i;
1348#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001349
1350 mutex_lock(&codec->mutex);
Troy Kisky12ef1932008-10-13 17:42:14 -07001351 soc_cleanup_debugfs(socdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001352#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001353 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001354 codec_dai = &codec->dai[i];
1355 if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
1356 soc_ac97_dev_unregister(codec);
1357 goto free_card;
1358 }
1359 }
1360free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001361#endif
1362
1363 if (codec->card)
1364 snd_card_free(codec->card);
1365 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1366 mutex_unlock(&codec->mutex);
1367}
1368EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1369
1370/**
1371 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1372 * @substream: the pcm substream
1373 * @hw: the hardware parameters
1374 *
1375 * Sets the substream runtime hardware parameters.
1376 */
1377int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1378 const struct snd_pcm_hardware *hw)
1379{
1380 struct snd_pcm_runtime *runtime = substream->runtime;
1381 runtime->hw.info = hw->info;
1382 runtime->hw.formats = hw->formats;
1383 runtime->hw.period_bytes_min = hw->period_bytes_min;
1384 runtime->hw.period_bytes_max = hw->period_bytes_max;
1385 runtime->hw.periods_min = hw->periods_min;
1386 runtime->hw.periods_max = hw->periods_max;
1387 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1388 runtime->hw.fifo_size = hw->fifo_size;
1389 return 0;
1390}
1391EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1392
1393/**
1394 * snd_soc_cnew - create new control
1395 * @_template: control template
1396 * @data: control private data
1397 * @lnng_name: control long name
1398 *
1399 * Create a new mixer control from a template control.
1400 *
1401 * Returns 0 for success, else error.
1402 */
1403struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1404 void *data, char *long_name)
1405{
1406 struct snd_kcontrol_new template;
1407
1408 memcpy(&template, _template, sizeof(template));
1409 if (long_name)
1410 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001411 template.index = 0;
1412
1413 return snd_ctl_new1(&template, data);
1414}
1415EXPORT_SYMBOL_GPL(snd_soc_cnew);
1416
1417/**
1418 * snd_soc_info_enum_double - enumerated double mixer info callback
1419 * @kcontrol: mixer control
1420 * @uinfo: control element information
1421 *
1422 * Callback to provide information about a double enumerated
1423 * mixer control.
1424 *
1425 * Returns 0 for success.
1426 */
1427int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1428 struct snd_ctl_elem_info *uinfo)
1429{
1430 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1431
1432 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1433 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001434 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001435
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001436 if (uinfo->value.enumerated.item > e->max - 1)
1437 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001438 strcpy(uinfo->value.enumerated.name,
1439 e->texts[uinfo->value.enumerated.item]);
1440 return 0;
1441}
1442EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1443
1444/**
1445 * snd_soc_get_enum_double - enumerated double mixer get callback
1446 * @kcontrol: mixer control
1447 * @uinfo: control element information
1448 *
1449 * Callback to get the value of a double enumerated mixer.
1450 *
1451 * Returns 0 for success.
1452 */
1453int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1454 struct snd_ctl_elem_value *ucontrol)
1455{
1456 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1457 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1458 unsigned short val, bitmask;
1459
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001460 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001461 ;
1462 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001463 ucontrol->value.enumerated.item[0]
1464 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001465 if (e->shift_l != e->shift_r)
1466 ucontrol->value.enumerated.item[1] =
1467 (val >> e->shift_r) & (bitmask - 1);
1468
1469 return 0;
1470}
1471EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1472
1473/**
1474 * snd_soc_put_enum_double - enumerated double mixer put callback
1475 * @kcontrol: mixer control
1476 * @uinfo: control element information
1477 *
1478 * Callback to set the value of a double enumerated mixer.
1479 *
1480 * Returns 0 for success.
1481 */
1482int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1483 struct snd_ctl_elem_value *ucontrol)
1484{
1485 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1486 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1487 unsigned short val;
1488 unsigned short mask, bitmask;
1489
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001490 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001491 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001492 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001493 return -EINVAL;
1494 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1495 mask = (bitmask - 1) << e->shift_l;
1496 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001497 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001498 return -EINVAL;
1499 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1500 mask |= (bitmask - 1) << e->shift_r;
1501 }
1502
1503 return snd_soc_update_bits(codec, e->reg, mask, val);
1504}
1505EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1506
1507/**
1508 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1509 * @kcontrol: mixer control
1510 * @uinfo: control element information
1511 *
1512 * Callback to provide information about an external enumerated
1513 * single mixer.
1514 *
1515 * Returns 0 for success.
1516 */
1517int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1518 struct snd_ctl_elem_info *uinfo)
1519{
1520 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1521
1522 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1523 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001524 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001525
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001526 if (uinfo->value.enumerated.item > e->max - 1)
1527 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001528 strcpy(uinfo->value.enumerated.name,
1529 e->texts[uinfo->value.enumerated.item]);
1530 return 0;
1531}
1532EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1533
1534/**
1535 * snd_soc_info_volsw_ext - external single mixer info callback
1536 * @kcontrol: mixer control
1537 * @uinfo: control element information
1538 *
1539 * Callback to provide information about a single external mixer control.
1540 *
1541 * Returns 0 for success.
1542 */
1543int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1544 struct snd_ctl_elem_info *uinfo)
1545{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001546 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001547
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001548 if (max == 1)
1549 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1550 else
1551 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1552
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001553 uinfo->count = 1;
1554 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001555 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001556 return 0;
1557}
1558EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1559
1560/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001561 * snd_soc_info_volsw - single mixer info callback
1562 * @kcontrol: mixer control
1563 * @uinfo: control element information
1564 *
1565 * Callback to provide information about a single mixer control.
1566 *
1567 * Returns 0 for success.
1568 */
1569int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1570 struct snd_ctl_elem_info *uinfo)
1571{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001572 struct soc_mixer_control *mc =
1573 (struct soc_mixer_control *)kcontrol->private_value;
1574 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001575 unsigned int shift = mc->min;
1576 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001577
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001578 if (max == 1)
1579 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1580 else
1581 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1582
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001583 uinfo->count = shift == rshift ? 1 : 2;
1584 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001585 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001586 return 0;
1587}
1588EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1589
1590/**
1591 * snd_soc_get_volsw - single mixer get callback
1592 * @kcontrol: mixer control
1593 * @uinfo: control element information
1594 *
1595 * Callback to get the value of a single mixer control.
1596 *
1597 * Returns 0 for success.
1598 */
1599int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1600 struct snd_ctl_elem_value *ucontrol)
1601{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001602 struct soc_mixer_control *mc =
1603 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001604 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001605 unsigned int reg = mc->reg;
1606 unsigned int shift = mc->shift;
1607 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001608 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001609 unsigned int mask = (1 << fls(max)) - 1;
1610 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001611
1612 ucontrol->value.integer.value[0] =
1613 (snd_soc_read(codec, reg) >> shift) & mask;
1614 if (shift != rshift)
1615 ucontrol->value.integer.value[1] =
1616 (snd_soc_read(codec, reg) >> rshift) & mask;
1617 if (invert) {
1618 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001619 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001620 if (shift != rshift)
1621 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001622 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001623 }
1624
1625 return 0;
1626}
1627EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1628
1629/**
1630 * snd_soc_put_volsw - single mixer put callback
1631 * @kcontrol: mixer control
1632 * @uinfo: control element information
1633 *
1634 * Callback to set the value of a single mixer control.
1635 *
1636 * Returns 0 for success.
1637 */
1638int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1639 struct snd_ctl_elem_value *ucontrol)
1640{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001641 struct soc_mixer_control *mc =
1642 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001643 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001644 unsigned int reg = mc->reg;
1645 unsigned int shift = mc->shift;
1646 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001647 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001648 unsigned int mask = (1 << fls(max)) - 1;
1649 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001650 unsigned short val, val2, val_mask;
1651
1652 val = (ucontrol->value.integer.value[0] & mask);
1653 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001654 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001655 val_mask = mask << shift;
1656 val = val << shift;
1657 if (shift != rshift) {
1658 val2 = (ucontrol->value.integer.value[1] & mask);
1659 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001660 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001661 val_mask |= mask << rshift;
1662 val |= val2 << rshift;
1663 }
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001664 return snd_soc_update_bits(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001665}
1666EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1667
1668/**
1669 * snd_soc_info_volsw_2r - double mixer info callback
1670 * @kcontrol: mixer control
1671 * @uinfo: control element information
1672 *
1673 * Callback to provide information about a double mixer control that
1674 * spans 2 codec registers.
1675 *
1676 * Returns 0 for success.
1677 */
1678int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1679 struct snd_ctl_elem_info *uinfo)
1680{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001681 struct soc_mixer_control *mc =
1682 (struct soc_mixer_control *)kcontrol->private_value;
1683 int max = mc->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001684
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001685 if (max == 1)
1686 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1687 else
1688 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1689
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001690 uinfo->count = 2;
1691 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001692 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001693 return 0;
1694}
1695EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1696
1697/**
1698 * snd_soc_get_volsw_2r - double mixer get callback
1699 * @kcontrol: mixer control
1700 * @uinfo: control element information
1701 *
1702 * Callback to get the value of a double mixer control that spans 2 registers.
1703 *
1704 * Returns 0 for success.
1705 */
1706int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1707 struct snd_ctl_elem_value *ucontrol)
1708{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001709 struct soc_mixer_control *mc =
1710 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001711 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001712 unsigned int reg = mc->reg;
1713 unsigned int reg2 = mc->rreg;
1714 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001715 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001716 unsigned int mask = (1<<fls(max))-1;
1717 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001718
1719 ucontrol->value.integer.value[0] =
1720 (snd_soc_read(codec, reg) >> shift) & mask;
1721 ucontrol->value.integer.value[1] =
1722 (snd_soc_read(codec, reg2) >> shift) & mask;
1723 if (invert) {
1724 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001725 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001726 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001727 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001728 }
1729
1730 return 0;
1731}
1732EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1733
1734/**
1735 * snd_soc_put_volsw_2r - double mixer set callback
1736 * @kcontrol: mixer control
1737 * @uinfo: control element information
1738 *
1739 * Callback to set the value of a double mixer control that spans 2 registers.
1740 *
1741 * Returns 0 for success.
1742 */
1743int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1744 struct snd_ctl_elem_value *ucontrol)
1745{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001746 struct soc_mixer_control *mc =
1747 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001748 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001749 unsigned int reg = mc->reg;
1750 unsigned int reg2 = mc->rreg;
1751 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001752 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001753 unsigned int mask = (1 << fls(max)) - 1;
1754 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001755 int err;
1756 unsigned short val, val2, val_mask;
1757
1758 val_mask = mask << shift;
1759 val = (ucontrol->value.integer.value[0] & mask);
1760 val2 = (ucontrol->value.integer.value[1] & mask);
1761
1762 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001763 val = max - val;
1764 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001765 }
1766
1767 val = val << shift;
1768 val2 = val2 << shift;
1769
Mark Brown3ff3f642008-05-19 12:32:25 +02001770 err = snd_soc_update_bits(codec, reg, val_mask, val);
1771 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001772 return err;
1773
1774 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1775 return err;
1776}
1777EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1778
Mark Browne13ac2e2008-05-28 17:58:05 +01001779/**
1780 * snd_soc_info_volsw_s8 - signed mixer info callback
1781 * @kcontrol: mixer control
1782 * @uinfo: control element information
1783 *
1784 * Callback to provide information about a signed mixer control.
1785 *
1786 * Returns 0 for success.
1787 */
1788int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1789 struct snd_ctl_elem_info *uinfo)
1790{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001791 struct soc_mixer_control *mc =
1792 (struct soc_mixer_control *)kcontrol->private_value;
1793 int max = mc->max;
1794 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001795
1796 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1797 uinfo->count = 2;
1798 uinfo->value.integer.min = 0;
1799 uinfo->value.integer.max = max-min;
1800 return 0;
1801}
1802EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1803
1804/**
1805 * snd_soc_get_volsw_s8 - signed mixer get callback
1806 * @kcontrol: mixer control
1807 * @uinfo: control element information
1808 *
1809 * Callback to get the value of a signed mixer control.
1810 *
1811 * Returns 0 for success.
1812 */
1813int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1814 struct snd_ctl_elem_value *ucontrol)
1815{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001816 struct soc_mixer_control *mc =
1817 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01001818 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001819 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001820 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001821 int val = snd_soc_read(codec, reg);
1822
1823 ucontrol->value.integer.value[0] =
1824 ((signed char)(val & 0xff))-min;
1825 ucontrol->value.integer.value[1] =
1826 ((signed char)((val >> 8) & 0xff))-min;
1827 return 0;
1828}
1829EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1830
1831/**
1832 * snd_soc_put_volsw_sgn - signed mixer put callback
1833 * @kcontrol: mixer control
1834 * @uinfo: control element information
1835 *
1836 * Callback to set the value of a signed mixer control.
1837 *
1838 * Returns 0 for success.
1839 */
1840int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1841 struct snd_ctl_elem_value *ucontrol)
1842{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001843 struct soc_mixer_control *mc =
1844 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01001845 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001846 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001847 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001848 unsigned short val;
1849
1850 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1851 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1852
1853 return snd_soc_update_bits(codec, reg, 0xffff, val);
1854}
1855EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1856
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001857/**
1858 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1859 * @dai: DAI
1860 * @clk_id: DAI specific clock ID
1861 * @freq: new clock frequency in Hz
1862 * @dir: new clock direction - input/output.
1863 *
1864 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1865 */
1866int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1867 unsigned int freq, int dir)
1868{
1869 if (dai->dai_ops.set_sysclk)
1870 return dai->dai_ops.set_sysclk(dai, clk_id, freq, dir);
1871 else
1872 return -EINVAL;
1873}
1874EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1875
1876/**
1877 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1878 * @dai: DAI
1879 * @clk_id: DAI specific clock divider ID
1880 * @div: new clock divisor.
1881 *
1882 * Configures the clock dividers. This is used to derive the best DAI bit and
1883 * frame clocks from the system or master clock. It's best to set the DAI bit
1884 * and frame clocks as low as possible to save system power.
1885 */
1886int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
1887 int div_id, int div)
1888{
1889 if (dai->dai_ops.set_clkdiv)
1890 return dai->dai_ops.set_clkdiv(dai, div_id, div);
1891 else
1892 return -EINVAL;
1893}
1894EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
1895
1896/**
1897 * snd_soc_dai_set_pll - configure DAI PLL.
1898 * @dai: DAI
1899 * @pll_id: DAI specific PLL ID
1900 * @freq_in: PLL input clock frequency in Hz
1901 * @freq_out: requested PLL output clock frequency in Hz
1902 *
1903 * Configures and enables PLL to generate output clock based on input clock.
1904 */
1905int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
1906 int pll_id, unsigned int freq_in, unsigned int freq_out)
1907{
1908 if (dai->dai_ops.set_pll)
1909 return dai->dai_ops.set_pll(dai, pll_id, freq_in, freq_out);
1910 else
1911 return -EINVAL;
1912}
1913EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
1914
1915/**
1916 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1917 * @dai: DAI
1918 * @clk_id: DAI specific clock ID
1919 * @fmt: SND_SOC_DAIFMT_ format value.
1920 *
1921 * Configures the DAI hardware format and clocking.
1922 */
1923int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1924{
1925 if (dai->dai_ops.set_fmt)
1926 return dai->dai_ops.set_fmt(dai, fmt);
1927 else
1928 return -EINVAL;
1929}
1930EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
1931
1932/**
1933 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
1934 * @dai: DAI
1935 * @mask: DAI specific mask representing used slots.
1936 * @slots: Number of slots in use.
1937 *
1938 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
1939 * specific.
1940 */
1941int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
1942 unsigned int mask, int slots)
1943{
1944 if (dai->dai_ops.set_sysclk)
1945 return dai->dai_ops.set_tdm_slot(dai, mask, slots);
1946 else
1947 return -EINVAL;
1948}
1949EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
1950
1951/**
1952 * snd_soc_dai_set_tristate - configure DAI system or master clock.
1953 * @dai: DAI
1954 * @tristate: tristate enable
1955 *
1956 * Tristates the DAI so that others can use it.
1957 */
1958int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
1959{
1960 if (dai->dai_ops.set_sysclk)
1961 return dai->dai_ops.set_tristate(dai, tristate);
1962 else
1963 return -EINVAL;
1964}
1965EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
1966
1967/**
1968 * snd_soc_dai_digital_mute - configure DAI system or master clock.
1969 * @dai: DAI
1970 * @mute: mute enable
1971 *
1972 * Mutes the DAI DAC.
1973 */
1974int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
1975{
1976 if (dai->dai_ops.digital_mute)
1977 return dai->dai_ops.digital_mute(dai, mute);
1978 else
1979 return -EINVAL;
1980}
1981EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
1982
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001983static int __devinit snd_soc_init(void)
1984{
1985 printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
1986 return platform_driver_register(&soc_driver);
1987}
1988
1989static void snd_soc_exit(void)
1990{
Mark Brown3ff3f642008-05-19 12:32:25 +02001991 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001992}
1993
1994module_init(snd_soc_init);
1995module_exit(snd_soc_exit);
1996
1997/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01001998MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001999MODULE_DESCRIPTION("ALSA SoC Core");
2000MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02002001MODULE_ALIAS("platform:soc-audio");