blob: 43f4060dbe755fe6031310a3c7bb5b430a332e2f [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
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042/*
43 * This is a timeout to do a DAPM powerdown after a stream is closed().
44 * It can be used to eliminate pops between different playback streams, e.g.
45 * between two audio tracks.
46 */
47static int pmdown_time = 5000;
48module_param(pmdown_time, int, 0);
49MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
50
Liam Girdwood965ac422007-01-31 14:14:57 +010051/*
52 * This function forces any delayed work to be queued and run.
53 */
54static int run_delayed_work(struct delayed_work *dwork)
55{
56 int ret;
57
58 /* cancel any work waiting to be queued. */
59 ret = cancel_delayed_work(dwork);
60
61 /* if there was any work waiting then we run it now and
62 * wait for it's completion */
63 if (ret) {
64 schedule_delayed_work(dwork, 0);
65 flush_scheduled_work();
66 }
67 return ret;
68}
69
Frank Mandarinodb2a4162006-10-06 18:31:09 +020070#ifdef CONFIG_SND_SOC_AC97_BUS
71/* unregister ac97 codec */
72static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
73{
74 if (codec->ac97->dev.bus)
75 device_unregister(&codec->ac97->dev);
76 return 0;
77}
78
79/* stop no dev release warning */
80static void soc_ac97_device_release(struct device *dev){}
81
82/* register ac97 codec to bus */
83static int soc_ac97_dev_register(struct snd_soc_codec *codec)
84{
85 int err;
86
87 codec->ac97->dev.bus = &ac97_bus_type;
88 codec->ac97->dev.parent = NULL;
89 codec->ac97->dev.release = soc_ac97_device_release;
90
91 snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
92 codec->card->number, 0, codec->name);
93 err = device_register(&codec->ac97->dev);
94 if (err < 0) {
95 snd_printk(KERN_ERR "Can't register ac97 bus\n");
96 codec->ac97->dev.bus = NULL;
97 return err;
98 }
99 return 0;
100}
101#endif
102
Mark Brown3ff3f642008-05-19 12:32:25 +0200103static inline const char *get_dai_name(int type)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200104{
Mark Brown3ff3f642008-05-19 12:32:25 +0200105 switch (type) {
Liam Girdwooda68660e2007-05-10 19:27:27 +0200106 case SND_SOC_DAI_AC97_BUS:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200107 case SND_SOC_DAI_AC97:
108 return "AC97";
109 case SND_SOC_DAI_I2S:
110 return "I2S";
111 case SND_SOC_DAI_PCM:
112 return "PCM";
113 }
114 return NULL;
115}
116
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200117/*
118 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
119 * then initialized and any private data can be allocated. This also calls
120 * startup for the cpu DAI, platform, machine and codec DAI.
121 */
122static int soc_pcm_open(struct snd_pcm_substream *substream)
123{
124 struct snd_soc_pcm_runtime *rtd = substream->private_data;
125 struct snd_soc_device *socdev = rtd->socdev;
126 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100127 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200128 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100129 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
130 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200131 int ret = 0;
132
133 mutex_lock(&pcm_mutex);
134
135 /* startup the audio subsystem */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100136 if (cpu_dai->ops.startup) {
Mark Browndee89c42008-11-18 22:11:38 +0000137 ret = cpu_dai->ops.startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200138 if (ret < 0) {
139 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100140 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200141 goto out;
142 }
143 }
144
145 if (platform->pcm_ops->open) {
146 ret = platform->pcm_ops->open(substream);
147 if (ret < 0) {
148 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
149 goto platform_err;
150 }
151 }
152
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100153 if (codec_dai->ops.startup) {
Mark Browndee89c42008-11-18 22:11:38 +0000154 ret = codec_dai->ops.startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100155 if (ret < 0) {
156 printk(KERN_ERR "asoc: can't open codec %s\n",
157 codec_dai->name);
158 goto codec_dai_err;
159 }
160 }
161
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200162 if (machine->ops && machine->ops->startup) {
163 ret = machine->ops->startup(substream);
164 if (ret < 0) {
165 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
166 goto machine_err;
167 }
168 }
169
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200170 /* Check that the codec and cpu DAI's are compatible */
171 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
172 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200173 max(codec_dai->playback.rate_min,
174 cpu_dai->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200175 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200176 min(codec_dai->playback.rate_max,
177 cpu_dai->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200178 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100179 max(codec_dai->playback.channels_min,
180 cpu_dai->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200181 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100182 min(codec_dai->playback.channels_max,
183 cpu_dai->playback.channels_max);
184 runtime->hw.formats =
185 codec_dai->playback.formats & cpu_dai->playback.formats;
186 runtime->hw.rates =
187 codec_dai->playback.rates & cpu_dai->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200188 } else {
189 runtime->hw.rate_min =
Mark Brown3ff3f642008-05-19 12:32:25 +0200190 max(codec_dai->capture.rate_min,
191 cpu_dai->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200192 runtime->hw.rate_max =
Mark Brown3ff3f642008-05-19 12:32:25 +0200193 min(codec_dai->capture.rate_max,
194 cpu_dai->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200195 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100196 max(codec_dai->capture.channels_min,
197 cpu_dai->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200198 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100199 min(codec_dai->capture.channels_max,
200 cpu_dai->capture.channels_max);
201 runtime->hw.formats =
202 codec_dai->capture.formats & cpu_dai->capture.formats;
203 runtime->hw.rates =
204 codec_dai->capture.rates & cpu_dai->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200205 }
206
207 snd_pcm_limit_hw_rates(runtime);
208 if (!runtime->hw.rates) {
209 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100210 codec_dai->name, cpu_dai->name);
211 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200212 }
213 if (!runtime->hw.formats) {
214 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100215 codec_dai->name, cpu_dai->name);
216 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200217 }
218 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
219 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100220 codec_dai->name, cpu_dai->name);
221 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200222 }
223
Mark Brownf24368c2008-10-21 21:45:08 +0100224 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
225 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
226 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
227 runtime->hw.channels_max);
228 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
229 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200230
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200231 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100232 cpu_dai->playback.active = codec_dai->playback.active = 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200233 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100234 cpu_dai->capture.active = codec_dai->capture.active = 1;
235 cpu_dai->active = codec_dai->active = 1;
236 cpu_dai->runtime = runtime;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200237 socdev->codec->active++;
238 mutex_unlock(&pcm_mutex);
239 return 0;
240
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100241machine_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200242 if (machine->ops && machine->ops->shutdown)
243 machine->ops->shutdown(substream);
244
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100245codec_dai_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200246 if (platform->pcm_ops->close)
247 platform->pcm_ops->close(substream);
248
249platform_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100250 if (cpu_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000251 cpu_dai->ops.shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200252out:
253 mutex_unlock(&pcm_mutex);
254 return ret;
255}
256
257/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200258 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200259 * This is to ensure there are no pops or clicks in between any music tracks
260 * due to DAPM power cycling.
261 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100262static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200263{
Andrew Morton4484bb22006-12-15 09:30:07 +0100264 struct snd_soc_device *socdev =
265 container_of(work, struct snd_soc_device, delayed_work.work);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200266 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100267 struct snd_soc_dai *codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200268 int i;
269
270 mutex_lock(&pcm_mutex);
Mark Brown3ff3f642008-05-19 12:32:25 +0200271 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200272 codec_dai = &codec->dai[i];
273
Mark Brownf24368c2008-10-21 21:45:08 +0100274 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
275 codec_dai->playback.stream_name,
276 codec_dai->playback.active ? "active" : "inactive",
277 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200278
279 /* are we waiting on this codec DAI stream */
280 if (codec_dai->pop_wait == 1) {
281
Mark Brown0be98982008-05-19 12:31:28 +0200282 /* Reduce power if no longer active */
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100283 if (codec->active == 0) {
Mark Brownf24368c2008-10-21 21:45:08 +0100284 pr_debug("pop wq D1 %s %s\n", codec->name,
285 codec_dai->playback.stream_name);
Mark Brown0be98982008-05-19 12:31:28 +0200286 snd_soc_dapm_set_bias_level(socdev,
287 SND_SOC_BIAS_PREPARE);
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100288 }
289
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200290 codec_dai->pop_wait = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100291 snd_soc_dapm_stream_event(codec,
292 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200293 SND_SOC_DAPM_STREAM_STOP);
294
Mark Brown0be98982008-05-19 12:31:28 +0200295 /* Fall into standby if no longer active */
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200296 if (codec->active == 0) {
Mark Brownf24368c2008-10-21 21:45:08 +0100297 pr_debug("pop wq D3 %s %s\n", codec->name,
298 codec_dai->playback.stream_name);
Mark Brown0be98982008-05-19 12:31:28 +0200299 snd_soc_dapm_set_bias_level(socdev,
300 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200301 }
302 }
303 }
304 mutex_unlock(&pcm_mutex);
305}
306
307/*
308 * Called by ALSA when a PCM substream is closed. Private data can be
309 * freed here. The cpu DAI, codec DAI, machine and platform are also
310 * shutdown.
311 */
312static int soc_codec_close(struct snd_pcm_substream *substream)
313{
314 struct snd_soc_pcm_runtime *rtd = substream->private_data;
315 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100316 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200317 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100318 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
319 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200320 struct snd_soc_codec *codec = socdev->codec;
321
322 mutex_lock(&pcm_mutex);
323
324 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100325 cpu_dai->playback.active = codec_dai->playback.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200326 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100327 cpu_dai->capture.active = codec_dai->capture.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200328
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100329 if (codec_dai->playback.active == 0 &&
330 codec_dai->capture.active == 0) {
331 cpu_dai->active = codec_dai->active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200332 }
333 codec->active--;
334
Mark Brown6010b2d2008-09-06 18:33:24 +0100335 /* Muting the DAC suppresses artifacts caused during digital
336 * shutdown, for example from stopping clocks.
337 */
338 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
339 snd_soc_dai_digital_mute(codec_dai, 1);
340
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100341 if (cpu_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000342 cpu_dai->ops.shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200343
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100344 if (codec_dai->ops.shutdown)
Mark Browndee89c42008-11-18 22:11:38 +0000345 codec_dai->ops.shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200346
347 if (machine->ops && machine->ops->shutdown)
348 machine->ops->shutdown(substream);
349
350 if (platform->pcm_ops->close)
351 platform->pcm_ops->close(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100352 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200353
354 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
355 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100356 codec_dai->pop_wait = 1;
Takashi Iwai4bb09522006-12-19 17:16:14 +0100357 schedule_delayed_work(&socdev->delayed_work,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200358 msecs_to_jiffies(pmdown_time));
359 } else {
360 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100361 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100362 codec_dai->capture.stream_name,
363 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200364
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100365 if (codec->active == 0 && codec_dai->pop_wait == 0)
Mark Brown0be98982008-05-19 12:31:28 +0200366 snd_soc_dapm_set_bias_level(socdev,
367 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200368 }
369
370 mutex_unlock(&pcm_mutex);
371 return 0;
372}
373
374/*
375 * Called by ALSA when the PCM substream is prepared, can set format, sample
376 * rate, etc. This function is non atomic and can be called multiple times,
377 * it can refer to the runtime info.
378 */
379static int soc_pcm_prepare(struct snd_pcm_substream *substream)
380{
381 struct snd_soc_pcm_runtime *rtd = substream->private_data;
382 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100383 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200384 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100385 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
386 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200387 struct snd_soc_codec *codec = socdev->codec;
388 int ret = 0;
389
390 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100391
392 if (machine->ops && machine->ops->prepare) {
393 ret = machine->ops->prepare(substream);
394 if (ret < 0) {
395 printk(KERN_ERR "asoc: machine prepare error\n");
396 goto out;
397 }
398 }
399
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200400 if (platform->pcm_ops->prepare) {
401 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200402 if (ret < 0) {
403 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200404 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200405 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200406 }
407
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100408 if (codec_dai->ops.prepare) {
Mark Browndee89c42008-11-18 22:11:38 +0000409 ret = codec_dai->ops.prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200410 if (ret < 0) {
411 printk(KERN_ERR "asoc: codec DAI 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 (cpu_dai->ops.prepare) {
Mark Browndee89c42008-11-18 22:11:38 +0000417 ret = cpu_dai->ops.prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100418 if (ret < 0) {
419 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
420 goto out;
421 }
422 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200423
Mark Brownd45f6212008-10-14 13:58:36 +0100424 /* cancel any delayed stream shutdown that is pending */
425 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
426 codec_dai->pop_wait) {
427 codec_dai->pop_wait = 0;
428 cancel_delayed_work(&socdev->delayed_work);
429 }
430
431 /* do we need to power up codec */
432 if (codec->bias_level != SND_SOC_BIAS_ON) {
433 snd_soc_dapm_set_bias_level(socdev,
434 SND_SOC_BIAS_PREPARE);
435
436 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
437 snd_soc_dapm_stream_event(codec,
438 codec_dai->playback.stream_name,
439 SND_SOC_DAPM_STREAM_START);
440 else
441 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100442 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200443 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100444
445 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
446 snd_soc_dai_digital_mute(codec_dai, 0);
447
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200448 } else {
Mark Brownd45f6212008-10-14 13:58:36 +0100449 /* codec already powered - power on widgets */
450 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
451 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100452 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200453 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100454 else
455 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100456 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200457 SND_SOC_DAPM_STREAM_START);
458
Mark Brownd45f6212008-10-14 13:58:36 +0100459 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200460 }
461
462out:
463 mutex_unlock(&pcm_mutex);
464 return ret;
465}
466
467/*
468 * Called by ALSA when the hardware params are set by application. This
469 * function can also be called multiple times and can allocate buffers
470 * (using snd_pcm_lib_* ). It's non-atomic.
471 */
472static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
473 struct snd_pcm_hw_params *params)
474{
475 struct snd_soc_pcm_runtime *rtd = substream->private_data;
476 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100477 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200478 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100479 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
480 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200481 int ret = 0;
482
483 mutex_lock(&pcm_mutex);
484
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100485 if (machine->ops && machine->ops->hw_params) {
486 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200487 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100488 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200489 goto out;
490 }
491 }
492
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100493 if (codec_dai->ops.hw_params) {
Mark Browndee89c42008-11-18 22:11:38 +0000494 ret = codec_dai->ops.hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100495 if (ret < 0) {
496 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
497 codec_dai->name);
498 goto codec_err;
499 }
500 }
501
502 if (cpu_dai->ops.hw_params) {
Mark Browndee89c42008-11-18 22:11:38 +0000503 ret = cpu_dai->ops.hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200504 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200505 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100506 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200507 goto interface_err;
508 }
509 }
510
511 if (platform->pcm_ops->hw_params) {
512 ret = platform->pcm_ops->hw_params(substream, params);
513 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200514 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200515 platform->name);
516 goto platform_err;
517 }
518 }
519
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200520out:
521 mutex_unlock(&pcm_mutex);
522 return ret;
523
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200524platform_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100525 if (cpu_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000526 cpu_dai->ops.hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200527
528interface_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100529 if (codec_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000530 codec_dai->ops.hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100531
532codec_err:
Mark Brown3ff3f642008-05-19 12:32:25 +0200533 if (machine->ops && machine->ops->hw_free)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100534 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200535
536 mutex_unlock(&pcm_mutex);
537 return ret;
538}
539
540/*
541 * Free's resources allocated by hw_params, can be called multiple times
542 */
543static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
544{
545 struct snd_soc_pcm_runtime *rtd = substream->private_data;
546 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100547 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100549 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
550 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200551 struct snd_soc_codec *codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200552
553 mutex_lock(&pcm_mutex);
554
555 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100556 if (!codec->active)
557 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200558
559 /* free any machine hw params */
560 if (machine->ops && machine->ops->hw_free)
561 machine->ops->hw_free(substream);
562
563 /* free any DMA resources */
564 if (platform->pcm_ops->hw_free)
565 platform->pcm_ops->hw_free(substream);
566
567 /* now free hw params for the DAI's */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100568 if (codec_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000569 codec_dai->ops.hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200570
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100571 if (cpu_dai->ops.hw_free)
Mark Browndee89c42008-11-18 22:11:38 +0000572 cpu_dai->ops.hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200573
574 mutex_unlock(&pcm_mutex);
575 return 0;
576}
577
578static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
579{
580 struct snd_soc_pcm_runtime *rtd = substream->private_data;
581 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100582 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200583 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100584 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
585 struct snd_soc_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200586 int ret;
587
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100588 if (codec_dai->ops.trigger) {
Mark Browndee89c42008-11-18 22:11:38 +0000589 ret = codec_dai->ops.trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200590 if (ret < 0)
591 return ret;
592 }
593
594 if (platform->pcm_ops->trigger) {
595 ret = platform->pcm_ops->trigger(substream, cmd);
596 if (ret < 0)
597 return ret;
598 }
599
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100600 if (cpu_dai->ops.trigger) {
Mark Browndee89c42008-11-18 22:11:38 +0000601 ret = cpu_dai->ops.trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200602 if (ret < 0)
603 return ret;
604 }
605 return 0;
606}
607
608/* ASoC PCM operations */
609static struct snd_pcm_ops soc_pcm_ops = {
610 .open = soc_pcm_open,
611 .close = soc_codec_close,
612 .hw_params = soc_pcm_hw_params,
613 .hw_free = soc_pcm_hw_free,
614 .prepare = soc_pcm_prepare,
615 .trigger = soc_pcm_trigger,
616};
617
618#ifdef CONFIG_PM
619/* powers down audio subsystem for suspend */
620static int soc_suspend(struct platform_device *pdev, pm_message_t state)
621{
Mark Brown3ff3f642008-05-19 12:32:25 +0200622 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000623 struct snd_soc_card *card = socdev->card;
Mark Brown3ff3f642008-05-19 12:32:25 +0200624 struct snd_soc_platform *platform = socdev->platform;
625 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200626 struct snd_soc_codec *codec = socdev->codec;
627 int i;
628
Andy Green6ed25972008-06-13 16:24:05 +0100629 /* Due to the resume being scheduled into a workqueue we could
630 * suspend before that's finished - wait for it to complete.
631 */
632 snd_power_lock(codec->card);
633 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
634 snd_power_unlock(codec->card);
635
636 /* we're going to block userspace touching us until resume completes */
637 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
638
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200639 /* mute any active DAC's */
Mark Browndee89c42008-11-18 22:11:38 +0000640 for (i = 0; i < card->num_links; i++) {
641 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
642 if (dai->ops.digital_mute && dai->playback.active)
643 dai->ops.digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200644 }
645
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100646 /* suspend all pcms */
Mark Brown87506542008-11-18 20:50:34 +0000647 for (i = 0; i < card->num_links; i++)
648 snd_pcm_suspend_all(card->dai_link[i].pcm);
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100649
Mark Brown87506542008-11-18 20:50:34 +0000650 if (card->suspend_pre)
651 card->suspend_pre(pdev, state);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200652
Mark Brown87506542008-11-18 20:50:34 +0000653 for (i = 0; i < card->num_links; i++) {
654 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200655 if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
656 cpu_dai->suspend(pdev, cpu_dai);
657 if (platform->suspend)
658 platform->suspend(pdev, cpu_dai);
659 }
660
661 /* close any waiting streams and save state */
Liam Girdwood965ac422007-01-31 14:14:57 +0100662 run_delayed_work(&socdev->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200663 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200664
Mark Brown3ff3f642008-05-19 12:32:25 +0200665 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200666 char *stream = codec->dai[i].playback.stream_name;
667 if (stream != NULL)
668 snd_soc_dapm_stream_event(codec, stream,
669 SND_SOC_DAPM_STREAM_SUSPEND);
670 stream = codec->dai[i].capture.stream_name;
671 if (stream != NULL)
672 snd_soc_dapm_stream_event(codec, stream,
673 SND_SOC_DAPM_STREAM_SUSPEND);
674 }
675
676 if (codec_dev->suspend)
677 codec_dev->suspend(pdev, state);
678
Mark Brown87506542008-11-18 20:50:34 +0000679 for (i = 0; i < card->num_links; i++) {
680 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200681 if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
682 cpu_dai->suspend(pdev, cpu_dai);
683 }
684
Mark Brown87506542008-11-18 20:50:34 +0000685 if (card->suspend_post)
686 card->suspend_post(pdev, state);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200687
688 return 0;
689}
690
Andy Green6ed25972008-06-13 16:24:05 +0100691/* deferred resume work, so resume can complete before we finished
692 * setting our codec back up, which can be very slow on I2C
693 */
694static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200695{
Andy Green6ed25972008-06-13 16:24:05 +0100696 struct snd_soc_device *socdev = container_of(work,
697 struct snd_soc_device,
698 deferred_resume_work);
Mark Brown87506542008-11-18 20:50:34 +0000699 struct snd_soc_card *card = socdev->card;
Mark Brown3ff3f642008-05-19 12:32:25 +0200700 struct snd_soc_platform *platform = socdev->platform;
701 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200702 struct snd_soc_codec *codec = socdev->codec;
Andy Green6ed25972008-06-13 16:24:05 +0100703 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200704 int i;
705
Andy Green6ed25972008-06-13 16:24:05 +0100706 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
707 * so userspace apps are blocked from touching us
708 */
709
710 dev_info(socdev->dev, "starting resume work\n");
711
Mark Brown87506542008-11-18 20:50:34 +0000712 if (card->resume_pre)
713 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200714
Mark Brown87506542008-11-18 20:50:34 +0000715 for (i = 0; i < card->num_links; i++) {
716 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200717 if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
718 cpu_dai->resume(pdev, cpu_dai);
719 }
720
721 if (codec_dev->resume)
722 codec_dev->resume(pdev);
723
Mark Brown3ff3f642008-05-19 12:32:25 +0200724 for (i = 0; i < codec->num_dai; i++) {
725 char *stream = codec->dai[i].playback.stream_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200726 if (stream != NULL)
727 snd_soc_dapm_stream_event(codec, stream,
728 SND_SOC_DAPM_STREAM_RESUME);
729 stream = codec->dai[i].capture.stream_name;
730 if (stream != NULL)
731 snd_soc_dapm_stream_event(codec, stream,
732 SND_SOC_DAPM_STREAM_RESUME);
733 }
734
Mark Brown3ff3f642008-05-19 12:32:25 +0200735 /* unmute any active DACs */
Mark Browndee89c42008-11-18 22:11:38 +0000736 for (i = 0; i < card->num_links; i++) {
737 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
738 if (dai->ops.digital_mute && dai->playback.active)
739 dai->ops.digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200740 }
741
Mark Brown87506542008-11-18 20:50:34 +0000742 for (i = 0; i < card->num_links; i++) {
743 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200744 if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
745 cpu_dai->resume(pdev, cpu_dai);
746 if (platform->resume)
747 platform->resume(pdev, cpu_dai);
748 }
749
Mark Brown87506542008-11-18 20:50:34 +0000750 if (card->resume_post)
751 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200752
Andy Green6ed25972008-06-13 16:24:05 +0100753 dev_info(socdev->dev, "resume work completed\n");
754
755 /* userspace can access us now we are back as we were before */
756 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
757}
758
759/* powers up audio subsystem after a suspend */
760static int soc_resume(struct platform_device *pdev)
761{
762 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
763
764 dev_info(socdev->dev, "scheduling resume work\n");
765
766 if (!schedule_work(&socdev->deferred_resume_work))
767 dev_err(socdev->dev, "work item may be lost\n");
768
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200769 return 0;
770}
771
772#else
773#define soc_suspend NULL
774#define soc_resume NULL
775#endif
776
777/* probes a new socdev */
778static int soc_probe(struct platform_device *pdev)
779{
780 int ret = 0, i;
781 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000782 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200783 struct snd_soc_platform *platform = socdev->platform;
784 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
785
Mark Brown87506542008-11-18 20:50:34 +0000786 if (card->probe) {
787 ret = card->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200788 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200789 return ret;
790 }
791
Mark Brown87506542008-11-18 20:50:34 +0000792 for (i = 0; i < card->num_links; i++) {
793 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200794 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +0100795 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +0200796 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200797 goto cpu_dai_err;
798 }
799 }
800
801 if (codec_dev->probe) {
802 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200803 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200804 goto cpu_dai_err;
805 }
806
807 if (platform->probe) {
808 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200809 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200810 goto platform_err;
811 }
812
813 /* DAPM stream work */
Andrew Morton4484bb22006-12-15 09:30:07 +0100814 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +0100815#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +0100816 /* deferred resume work */
817 INIT_WORK(&socdev->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +0100818#endif
Andy Green6ed25972008-06-13 16:24:05 +0100819
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200820 return 0;
821
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200822platform_err:
823 if (codec_dev->remove)
824 codec_dev->remove(pdev);
825
826cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +0100827 for (i--; i >= 0; i--) {
Mark Brown87506542008-11-18 20:50:34 +0000828 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200829 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100830 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200831 }
832
Mark Brown87506542008-11-18 20:50:34 +0000833 if (card->remove)
834 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200835
836 return ret;
837}
838
839/* removes a socdev */
840static int soc_remove(struct platform_device *pdev)
841{
842 int i;
843 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown87506542008-11-18 20:50:34 +0000844 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200845 struct snd_soc_platform *platform = socdev->platform;
846 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
847
Liam Girdwood965ac422007-01-31 14:14:57 +0100848 run_delayed_work(&socdev->delayed_work);
849
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200850 if (platform->remove)
851 platform->remove(pdev);
852
853 if (codec_dev->remove)
854 codec_dev->remove(pdev);
855
Mark Brown87506542008-11-18 20:50:34 +0000856 for (i = 0; i < card->num_links; i++) {
857 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100859 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200860 }
861
Mark Brown87506542008-11-18 20:50:34 +0000862 if (card->remove)
863 card->remove(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200864
865 return 0;
866}
867
868/* ASoC platform driver */
869static struct platform_driver soc_driver = {
870 .driver = {
871 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +0200872 .owner = THIS_MODULE,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200873 },
874 .probe = soc_probe,
875 .remove = soc_remove,
876 .suspend = soc_suspend,
877 .resume = soc_resume,
878};
879
880/* create a new pcm */
881static int soc_new_pcm(struct snd_soc_device *socdev,
882 struct snd_soc_dai_link *dai_link, int num)
883{
884 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwood3c4b2662008-07-07 16:07:17 +0100885 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
886 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200887 struct snd_soc_pcm_runtime *rtd;
888 struct snd_pcm *pcm;
889 char new_name[64];
890 int ret = 0, playback = 0, capture = 0;
891
892 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
893 if (rtd == NULL)
894 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100895
896 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200897 rtd->socdev = socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100898 codec_dai->codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200899
900 /* check client and interface hw capabilities */
Mark Brown3ff3f642008-05-19 12:32:25 +0200901 sprintf(new_name, "%s %s-%s-%d", dai_link->stream_name, codec_dai->name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200902 get_dai_name(cpu_dai->type), num);
903
904 if (codec_dai->playback.channels_min)
905 playback = 1;
906 if (codec_dai->capture.channels_min)
907 capture = 1;
908
909 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
910 capture, &pcm);
911 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200912 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
913 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200914 kfree(rtd);
915 return ret;
916 }
917
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100918 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200919 pcm->private_data = rtd;
920 soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
921 soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
922 soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
923 soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
924 soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
925 soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
926 soc_pcm_ops.page = socdev->platform->pcm_ops->page;
927
928 if (playback)
929 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
930
931 if (capture)
932 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
933
934 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
935 if (ret < 0) {
936 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
937 kfree(rtd);
938 return ret;
939 }
940
941 pcm->private_free = socdev->platform->pcm_free;
942 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
943 cpu_dai->name);
944 return ret;
945}
946
947/* codec register dump */
Troy Kisky12ef1932008-10-13 17:42:14 -0700948static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200949{
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200950 struct snd_soc_codec *codec = devdata->codec;
951 int i, step = 1, count = 0;
952
953 if (!codec->reg_cache_size)
954 return 0;
955
956 if (codec->reg_cache_step)
957 step = codec->reg_cache_step;
958
959 count += sprintf(buf, "%s registers\n", codec->name);
Mark Brown58cd33c2008-07-29 11:42:25 +0100960 for (i = 0; i < codec->reg_cache_size; i += step) {
961 count += sprintf(buf + count, "%2x: ", i);
962 if (count >= PAGE_SIZE - 1)
963 break;
964
965 if (codec->display_register)
966 count += codec->display_register(codec, buf + count,
967 PAGE_SIZE - count, i);
968 else
969 count += snprintf(buf + count, PAGE_SIZE - count,
970 "%4x", codec->read(codec, i));
971
972 if (count >= PAGE_SIZE - 1)
973 break;
974
975 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
976 if (count >= PAGE_SIZE - 1)
977 break;
978 }
979
980 /* Truncate count; min() would cause a warning */
981 if (count >= PAGE_SIZE)
982 count = PAGE_SIZE - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200983
984 return count;
985}
Troy Kisky12ef1932008-10-13 17:42:14 -0700986static ssize_t codec_reg_show(struct device *dev,
987 struct device_attribute *attr, char *buf)
988{
989 struct snd_soc_device *devdata = dev_get_drvdata(dev);
990 return soc_codec_reg_show(devdata, buf);
991}
992
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200993static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
994
Troy Kisky12ef1932008-10-13 17:42:14 -0700995#ifdef CONFIG_DEBUG_FS
996static int codec_reg_open_file(struct inode *inode, struct file *file)
997{
998 file->private_data = inode->i_private;
999 return 0;
1000}
1001
1002static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1003 size_t count, loff_t *ppos)
1004{
1005 ssize_t ret;
1006 struct snd_soc_device *devdata = file->private_data;
1007 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1008 if (!buf)
1009 return -ENOMEM;
1010 ret = soc_codec_reg_show(devdata, buf);
1011 if (ret >= 0)
1012 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1013 kfree(buf);
1014 return ret;
1015}
1016
1017static ssize_t codec_reg_write_file(struct file *file,
1018 const char __user *user_buf, size_t count, loff_t *ppos)
1019{
1020 char buf[32];
1021 int buf_size;
1022 char *start = buf;
1023 unsigned long reg, value;
1024 int step = 1;
1025 struct snd_soc_device *devdata = file->private_data;
1026 struct snd_soc_codec *codec = devdata->codec;
1027
1028 buf_size = min(count, (sizeof(buf)-1));
1029 if (copy_from_user(buf, user_buf, buf_size))
1030 return -EFAULT;
1031 buf[buf_size] = 0;
1032
1033 if (codec->reg_cache_step)
1034 step = codec->reg_cache_step;
1035
1036 while (*start == ' ')
1037 start++;
1038 reg = simple_strtoul(start, &start, 16);
1039 if ((reg >= codec->reg_cache_size) || (reg % step))
1040 return -EINVAL;
1041 while (*start == ' ')
1042 start++;
1043 if (strict_strtoul(start, 16, &value))
1044 return -EINVAL;
1045 codec->write(codec, reg, value);
1046 return buf_size;
1047}
1048
1049static const struct file_operations codec_reg_fops = {
1050 .open = codec_reg_open_file,
1051 .read = codec_reg_read_file,
1052 .write = codec_reg_write_file,
1053};
1054
1055static void soc_init_debugfs(struct snd_soc_device *socdev)
1056{
1057 struct dentry *root, *file;
1058 struct snd_soc_codec *codec = socdev->codec;
1059 root = debugfs_create_dir(dev_name(socdev->dev), NULL);
1060 if (IS_ERR(root) || !root)
1061 goto exit1;
1062
1063 file = debugfs_create_file("codec_reg", 0644,
1064 root, socdev, &codec_reg_fops);
1065 if (!file)
1066 goto exit2;
1067
1068 file = debugfs_create_u32("dapm_pop_time", 0744,
1069 root, &codec->pop_time);
1070 if (!file)
1071 goto exit2;
1072 socdev->debugfs_root = root;
1073 return;
1074exit2:
1075 debugfs_remove_recursive(root);
1076exit1:
1077 dev_err(socdev->dev, "debugfs is not available\n");
1078}
1079
1080static void soc_cleanup_debugfs(struct snd_soc_device *socdev)
1081{
1082 debugfs_remove_recursive(socdev->debugfs_root);
1083 socdev->debugfs_root = NULL;
1084}
1085
1086#else
1087
1088static inline void soc_init_debugfs(struct snd_soc_device *socdev)
1089{
1090}
1091
1092static inline void soc_cleanup_debugfs(struct snd_soc_device *socdev)
1093{
1094}
1095#endif
1096
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001097/**
1098 * snd_soc_new_ac97_codec - initailise AC97 device
1099 * @codec: audio codec
1100 * @ops: AC97 bus operations
1101 * @num: AC97 codec number
1102 *
1103 * Initialises AC97 codec resources for use by ad-hoc devices only.
1104 */
1105int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1106 struct snd_ac97_bus_ops *ops, int num)
1107{
1108 mutex_lock(&codec->mutex);
1109
1110 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1111 if (codec->ac97 == NULL) {
1112 mutex_unlock(&codec->mutex);
1113 return -ENOMEM;
1114 }
1115
1116 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1117 if (codec->ac97->bus == NULL) {
1118 kfree(codec->ac97);
1119 codec->ac97 = NULL;
1120 mutex_unlock(&codec->mutex);
1121 return -ENOMEM;
1122 }
1123
1124 codec->ac97->bus->ops = ops;
1125 codec->ac97->num = num;
1126 mutex_unlock(&codec->mutex);
1127 return 0;
1128}
1129EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1130
1131/**
1132 * snd_soc_free_ac97_codec - free AC97 codec device
1133 * @codec: audio codec
1134 *
1135 * Frees AC97 codec device resources.
1136 */
1137void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1138{
1139 mutex_lock(&codec->mutex);
1140 kfree(codec->ac97->bus);
1141 kfree(codec->ac97);
1142 codec->ac97 = NULL;
1143 mutex_unlock(&codec->mutex);
1144}
1145EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1146
1147/**
1148 * snd_soc_update_bits - update codec register bits
1149 * @codec: audio codec
1150 * @reg: codec register
1151 * @mask: register mask
1152 * @value: new value
1153 *
1154 * Writes new register value.
1155 *
1156 * Returns 1 for change else 0.
1157 */
1158int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1159 unsigned short mask, unsigned short value)
1160{
1161 int change;
1162 unsigned short old, new;
1163
1164 mutex_lock(&io_mutex);
1165 old = snd_soc_read(codec, reg);
1166 new = (old & ~mask) | value;
1167 change = old != new;
1168 if (change)
1169 snd_soc_write(codec, reg, new);
1170
1171 mutex_unlock(&io_mutex);
1172 return change;
1173}
1174EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1175
1176/**
1177 * snd_soc_test_bits - test register for change
1178 * @codec: audio codec
1179 * @reg: codec register
1180 * @mask: register mask
1181 * @value: new value
1182 *
1183 * Tests a register with a new value and checks if the new value is
1184 * different from the old value.
1185 *
1186 * Returns 1 for change else 0.
1187 */
1188int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1189 unsigned short mask, unsigned short value)
1190{
1191 int change;
1192 unsigned short old, new;
1193
1194 mutex_lock(&io_mutex);
1195 old = snd_soc_read(codec, reg);
1196 new = (old & ~mask) | value;
1197 change = old != new;
1198 mutex_unlock(&io_mutex);
1199
1200 return change;
1201}
1202EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1203
1204/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001205 * snd_soc_new_pcms - create new sound card and pcms
1206 * @socdev: the SoC audio device
1207 *
1208 * Create a new sound card based upon the codec and interface pcms.
1209 *
1210 * Returns 0 for success, else error.
1211 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001212int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001213{
1214 struct snd_soc_codec *codec = socdev->codec;
Mark Brown87506542008-11-18 20:50:34 +00001215 struct snd_soc_card *card = socdev->card;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001216 int ret = 0, i;
1217
1218 mutex_lock(&codec->mutex);
1219
1220 /* register a sound card */
1221 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1222 if (!codec->card) {
1223 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1224 codec->name);
1225 mutex_unlock(&codec->mutex);
1226 return -ENODEV;
1227 }
1228
1229 codec->card->dev = socdev->dev;
1230 codec->card->private_data = codec;
1231 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1232
1233 /* create the pcms */
Mark Brown87506542008-11-18 20:50:34 +00001234 for (i = 0; i < card->num_links; i++) {
1235 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001236 if (ret < 0) {
1237 printk(KERN_ERR "asoc: can't create pcm %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001238 card->dai_link[i].stream_name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001239 mutex_unlock(&codec->mutex);
1240 return ret;
1241 }
1242 }
1243
1244 mutex_unlock(&codec->mutex);
1245 return ret;
1246}
1247EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1248
1249/**
1250 * snd_soc_register_card - register sound card
1251 * @socdev: the SoC audio device
1252 *
1253 * Register a SoC sound card. Also registers an AC97 device if the
1254 * codec is AC97 for ad hoc devices.
1255 *
1256 * Returns 0 for success, else error.
1257 */
1258int snd_soc_register_card(struct snd_soc_device *socdev)
1259{
1260 struct snd_soc_codec *codec = socdev->codec;
Mark Brown87506542008-11-18 20:50:34 +00001261 struct snd_soc_card *card = socdev->card;
Liam Girdwood12e74f72006-10-16 21:19:48 +02001262 int ret = 0, i, ac97 = 0, err = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001263
Mark Brown87506542008-11-18 20:50:34 +00001264 for (i = 0; i < card->num_links; i++) {
1265 if (card->dai_link[i].init) {
1266 err = card->dai_link[i].init(codec);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001267 if (err < 0) {
1268 printk(KERN_ERR "asoc: failed to init %s\n",
Mark Brown87506542008-11-18 20:50:34 +00001269 card->dai_link[i].stream_name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001270 continue;
1271 }
1272 }
Mark Brown87506542008-11-18 20:50:34 +00001273 if (card->dai_link[i].codec_dai->type ==
Liam Girdwooda68660e2007-05-10 19:27:27 +02001274 SND_SOC_DAI_AC97_BUS)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001275 ac97 = 1;
1276 }
1277 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
Mark Brown87506542008-11-18 20:50:34 +00001278 "%s", card->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001279 snprintf(codec->card->longname, sizeof(codec->card->longname),
Mark Brown87506542008-11-18 20:50:34 +00001280 "%s (%s)", card->name, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001281
1282 ret = snd_card_register(codec->card);
1283 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001284 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001285 codec->name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001286 goto out;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001287 }
1288
Mark Brown08c8efe2008-01-21 14:33:37 +01001289 mutex_lock(&codec->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001290#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood12e74f72006-10-16 21:19:48 +02001291 if (ac97) {
1292 ret = soc_ac97_dev_register(codec);
1293 if (ret < 0) {
1294 printk(KERN_ERR "asoc: AC97 device register failed\n");
1295 snd_card_free(codec->card);
Mark Brown08c8efe2008-01-21 14:33:37 +01001296 mutex_unlock(&codec->mutex);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001297 goto out;
1298 }
1299 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001300#endif
1301
Liam Girdwood12e74f72006-10-16 21:19:48 +02001302 err = snd_soc_dapm_sys_add(socdev->dev);
1303 if (err < 0)
1304 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1305
1306 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1307 if (err < 0)
Mark Brown3ff3f642008-05-19 12:32:25 +02001308 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
Mark Brown08c8efe2008-01-21 14:33:37 +01001309
Troy Kisky12ef1932008-10-13 17:42:14 -07001310 soc_init_debugfs(socdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001311 mutex_unlock(&codec->mutex);
Mark Brown08c8efe2008-01-21 14:33:37 +01001312
1313out:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001314 return ret;
1315}
1316EXPORT_SYMBOL_GPL(snd_soc_register_card);
1317
1318/**
1319 * snd_soc_free_pcms - free sound card and pcms
1320 * @socdev: the SoC audio device
1321 *
1322 * Frees sound card and pcms associated with the socdev.
1323 * Also unregister the codec if it is an AC97 device.
1324 */
1325void snd_soc_free_pcms(struct snd_soc_device *socdev)
1326{
1327 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001328#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood3c4b2662008-07-07 16:07:17 +01001329 struct snd_soc_dai *codec_dai;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001330 int i;
1331#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001332
1333 mutex_lock(&codec->mutex);
Troy Kisky12ef1932008-10-13 17:42:14 -07001334 soc_cleanup_debugfs(socdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001335#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001336 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001337 codec_dai = &codec->dai[i];
1338 if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
1339 soc_ac97_dev_unregister(codec);
1340 goto free_card;
1341 }
1342 }
1343free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001344#endif
1345
1346 if (codec->card)
1347 snd_card_free(codec->card);
1348 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1349 mutex_unlock(&codec->mutex);
1350}
1351EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1352
1353/**
1354 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1355 * @substream: the pcm substream
1356 * @hw: the hardware parameters
1357 *
1358 * Sets the substream runtime hardware parameters.
1359 */
1360int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1361 const struct snd_pcm_hardware *hw)
1362{
1363 struct snd_pcm_runtime *runtime = substream->runtime;
1364 runtime->hw.info = hw->info;
1365 runtime->hw.formats = hw->formats;
1366 runtime->hw.period_bytes_min = hw->period_bytes_min;
1367 runtime->hw.period_bytes_max = hw->period_bytes_max;
1368 runtime->hw.periods_min = hw->periods_min;
1369 runtime->hw.periods_max = hw->periods_max;
1370 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1371 runtime->hw.fifo_size = hw->fifo_size;
1372 return 0;
1373}
1374EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1375
1376/**
1377 * snd_soc_cnew - create new control
1378 * @_template: control template
1379 * @data: control private data
1380 * @lnng_name: control long name
1381 *
1382 * Create a new mixer control from a template control.
1383 *
1384 * Returns 0 for success, else error.
1385 */
1386struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1387 void *data, char *long_name)
1388{
1389 struct snd_kcontrol_new template;
1390
1391 memcpy(&template, _template, sizeof(template));
1392 if (long_name)
1393 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001394 template.index = 0;
1395
1396 return snd_ctl_new1(&template, data);
1397}
1398EXPORT_SYMBOL_GPL(snd_soc_cnew);
1399
1400/**
1401 * snd_soc_info_enum_double - enumerated double mixer info callback
1402 * @kcontrol: mixer control
1403 * @uinfo: control element information
1404 *
1405 * Callback to provide information about a double enumerated
1406 * mixer control.
1407 *
1408 * Returns 0 for success.
1409 */
1410int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1411 struct snd_ctl_elem_info *uinfo)
1412{
1413 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1414
1415 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1416 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001417 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001418
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001419 if (uinfo->value.enumerated.item > e->max - 1)
1420 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001421 strcpy(uinfo->value.enumerated.name,
1422 e->texts[uinfo->value.enumerated.item]);
1423 return 0;
1424}
1425EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1426
1427/**
1428 * snd_soc_get_enum_double - enumerated double mixer get callback
1429 * @kcontrol: mixer control
1430 * @uinfo: control element information
1431 *
1432 * Callback to get the value of a double enumerated mixer.
1433 *
1434 * Returns 0 for success.
1435 */
1436int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1437 struct snd_ctl_elem_value *ucontrol)
1438{
1439 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1440 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1441 unsigned short val, bitmask;
1442
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001443 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001444 ;
1445 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001446 ucontrol->value.enumerated.item[0]
1447 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001448 if (e->shift_l != e->shift_r)
1449 ucontrol->value.enumerated.item[1] =
1450 (val >> e->shift_r) & (bitmask - 1);
1451
1452 return 0;
1453}
1454EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1455
1456/**
1457 * snd_soc_put_enum_double - enumerated double mixer put callback
1458 * @kcontrol: mixer control
1459 * @uinfo: control element information
1460 *
1461 * Callback to set the value of a double enumerated mixer.
1462 *
1463 * Returns 0 for success.
1464 */
1465int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1466 struct snd_ctl_elem_value *ucontrol)
1467{
1468 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1469 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1470 unsigned short val;
1471 unsigned short mask, bitmask;
1472
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001473 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001474 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001475 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001476 return -EINVAL;
1477 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1478 mask = (bitmask - 1) << e->shift_l;
1479 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001480 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001481 return -EINVAL;
1482 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1483 mask |= (bitmask - 1) << e->shift_r;
1484 }
1485
1486 return snd_soc_update_bits(codec, e->reg, mask, val);
1487}
1488EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1489
1490/**
1491 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1492 * @kcontrol: mixer control
1493 * @uinfo: control element information
1494 *
1495 * Callback to provide information about an external enumerated
1496 * single mixer.
1497 *
1498 * Returns 0 for success.
1499 */
1500int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1501 struct snd_ctl_elem_info *uinfo)
1502{
1503 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1504
1505 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1506 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001507 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001508
Jon Smirlf8ba0b72008-07-29 11:42:27 +01001509 if (uinfo->value.enumerated.item > e->max - 1)
1510 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001511 strcpy(uinfo->value.enumerated.name,
1512 e->texts[uinfo->value.enumerated.item]);
1513 return 0;
1514}
1515EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1516
1517/**
1518 * snd_soc_info_volsw_ext - external single mixer info callback
1519 * @kcontrol: mixer control
1520 * @uinfo: control element information
1521 *
1522 * Callback to provide information about a single external mixer control.
1523 *
1524 * Returns 0 for success.
1525 */
1526int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1527 struct snd_ctl_elem_info *uinfo)
1528{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001529 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001530
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001531 if (max == 1)
1532 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1533 else
1534 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1535
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001536 uinfo->count = 1;
1537 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001538 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001539 return 0;
1540}
1541EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1542
1543/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001544 * snd_soc_info_volsw - single mixer info callback
1545 * @kcontrol: mixer control
1546 * @uinfo: control element information
1547 *
1548 * Callback to provide information about a single mixer control.
1549 *
1550 * Returns 0 for success.
1551 */
1552int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1553 struct snd_ctl_elem_info *uinfo)
1554{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001555 struct soc_mixer_control *mc =
1556 (struct soc_mixer_control *)kcontrol->private_value;
1557 int max = mc->max;
Mark Brown762b8df2008-10-30 12:37:08 +00001558 unsigned int shift = mc->shift;
Jon Smirl815ecf82008-07-29 10:22:24 -04001559 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001560
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001561 if (max == 1)
1562 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1563 else
1564 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1565
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001566 uinfo->count = shift == rshift ? 1 : 2;
1567 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001568 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001569 return 0;
1570}
1571EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1572
1573/**
1574 * snd_soc_get_volsw - single mixer get callback
1575 * @kcontrol: mixer control
1576 * @uinfo: control element information
1577 *
1578 * Callback to get the value of a single mixer control.
1579 *
1580 * Returns 0 for success.
1581 */
1582int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1583 struct snd_ctl_elem_value *ucontrol)
1584{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001585 struct soc_mixer_control *mc =
1586 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001587 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001588 unsigned int reg = mc->reg;
1589 unsigned int shift = mc->shift;
1590 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001591 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001592 unsigned int mask = (1 << fls(max)) - 1;
1593 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001594
1595 ucontrol->value.integer.value[0] =
1596 (snd_soc_read(codec, reg) >> shift) & mask;
1597 if (shift != rshift)
1598 ucontrol->value.integer.value[1] =
1599 (snd_soc_read(codec, reg) >> rshift) & mask;
1600 if (invert) {
1601 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001602 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001603 if (shift != rshift)
1604 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001605 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001606 }
1607
1608 return 0;
1609}
1610EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1611
1612/**
1613 * snd_soc_put_volsw - single mixer put callback
1614 * @kcontrol: mixer control
1615 * @uinfo: control element information
1616 *
1617 * Callback to set the value of a single mixer control.
1618 *
1619 * Returns 0 for success.
1620 */
1621int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_value *ucontrol)
1623{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001624 struct soc_mixer_control *mc =
1625 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001626 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001627 unsigned int reg = mc->reg;
1628 unsigned int shift = mc->shift;
1629 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001630 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001631 unsigned int mask = (1 << fls(max)) - 1;
1632 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001633 unsigned short val, val2, val_mask;
1634
1635 val = (ucontrol->value.integer.value[0] & mask);
1636 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001637 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001638 val_mask = mask << shift;
1639 val = val << shift;
1640 if (shift != rshift) {
1641 val2 = (ucontrol->value.integer.value[1] & mask);
1642 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001643 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001644 val_mask |= mask << rshift;
1645 val |= val2 << rshift;
1646 }
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001647 return snd_soc_update_bits(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001648}
1649EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1650
1651/**
1652 * snd_soc_info_volsw_2r - double mixer info callback
1653 * @kcontrol: mixer control
1654 * @uinfo: control element information
1655 *
1656 * Callback to provide information about a double mixer control that
1657 * spans 2 codec registers.
1658 *
1659 * Returns 0 for success.
1660 */
1661int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1662 struct snd_ctl_elem_info *uinfo)
1663{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001664 struct soc_mixer_control *mc =
1665 (struct soc_mixer_control *)kcontrol->private_value;
1666 int max = mc->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001667
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001668 if (max == 1)
1669 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1670 else
1671 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1672
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001673 uinfo->count = 2;
1674 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001675 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001676 return 0;
1677}
1678EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1679
1680/**
1681 * snd_soc_get_volsw_2r - double mixer get callback
1682 * @kcontrol: mixer control
1683 * @uinfo: control element information
1684 *
1685 * Callback to get the value of a double mixer control that spans 2 registers.
1686 *
1687 * Returns 0 for success.
1688 */
1689int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1690 struct snd_ctl_elem_value *ucontrol)
1691{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001692 struct soc_mixer_control *mc =
1693 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001694 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001695 unsigned int reg = mc->reg;
1696 unsigned int reg2 = mc->rreg;
1697 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001698 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001699 unsigned int mask = (1<<fls(max))-1;
1700 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001701
1702 ucontrol->value.integer.value[0] =
1703 (snd_soc_read(codec, reg) >> shift) & mask;
1704 ucontrol->value.integer.value[1] =
1705 (snd_soc_read(codec, reg2) >> shift) & mask;
1706 if (invert) {
1707 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001708 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001709 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001710 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001711 }
1712
1713 return 0;
1714}
1715EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1716
1717/**
1718 * snd_soc_put_volsw_2r - double mixer set callback
1719 * @kcontrol: mixer control
1720 * @uinfo: control element information
1721 *
1722 * Callback to set the value of a double mixer control that spans 2 registers.
1723 *
1724 * Returns 0 for success.
1725 */
1726int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1727 struct snd_ctl_elem_value *ucontrol)
1728{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001729 struct soc_mixer_control *mc =
1730 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001731 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001732 unsigned int reg = mc->reg;
1733 unsigned int reg2 = mc->rreg;
1734 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001735 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04001736 unsigned int mask = (1 << fls(max)) - 1;
1737 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001738 int err;
1739 unsigned short val, val2, val_mask;
1740
1741 val_mask = mask << shift;
1742 val = (ucontrol->value.integer.value[0] & mask);
1743 val2 = (ucontrol->value.integer.value[1] & mask);
1744
1745 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001746 val = max - val;
1747 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001748 }
1749
1750 val = val << shift;
1751 val2 = val2 << shift;
1752
Mark Brown3ff3f642008-05-19 12:32:25 +02001753 err = snd_soc_update_bits(codec, reg, val_mask, val);
1754 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001755 return err;
1756
1757 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1758 return err;
1759}
1760EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1761
Mark Browne13ac2e2008-05-28 17:58:05 +01001762/**
1763 * snd_soc_info_volsw_s8 - signed mixer info callback
1764 * @kcontrol: mixer control
1765 * @uinfo: control element information
1766 *
1767 * Callback to provide information about a signed mixer control.
1768 *
1769 * Returns 0 for success.
1770 */
1771int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1772 struct snd_ctl_elem_info *uinfo)
1773{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001774 struct soc_mixer_control *mc =
1775 (struct soc_mixer_control *)kcontrol->private_value;
1776 int max = mc->max;
1777 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001778
1779 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1780 uinfo->count = 2;
1781 uinfo->value.integer.min = 0;
1782 uinfo->value.integer.max = max-min;
1783 return 0;
1784}
1785EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1786
1787/**
1788 * snd_soc_get_volsw_s8 - signed mixer get callback
1789 * @kcontrol: mixer control
1790 * @uinfo: control element information
1791 *
1792 * Callback to get the value of a signed mixer control.
1793 *
1794 * Returns 0 for success.
1795 */
1796int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1797 struct snd_ctl_elem_value *ucontrol)
1798{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001799 struct soc_mixer_control *mc =
1800 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01001801 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001802 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001803 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001804 int val = snd_soc_read(codec, reg);
1805
1806 ucontrol->value.integer.value[0] =
1807 ((signed char)(val & 0xff))-min;
1808 ucontrol->value.integer.value[1] =
1809 ((signed char)((val >> 8) & 0xff))-min;
1810 return 0;
1811}
1812EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1813
1814/**
1815 * snd_soc_put_volsw_sgn - signed mixer put callback
1816 * @kcontrol: mixer control
1817 * @uinfo: control element information
1818 *
1819 * Callback to set the value of a signed mixer control.
1820 *
1821 * Returns 0 for success.
1822 */
1823int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1824 struct snd_ctl_elem_value *ucontrol)
1825{
Jon Smirl4eaa9812008-07-29 11:42:26 +01001826 struct soc_mixer_control *mc =
1827 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01001828 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04001829 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01001830 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01001831 unsigned short val;
1832
1833 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1834 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1835
1836 return snd_soc_update_bits(codec, reg, 0xffff, val);
1837}
1838EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1839
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001840/**
1841 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1842 * @dai: DAI
1843 * @clk_id: DAI specific clock ID
1844 * @freq: new clock frequency in Hz
1845 * @dir: new clock direction - input/output.
1846 *
1847 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1848 */
1849int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1850 unsigned int freq, int dir)
1851{
Mark Browndee89c42008-11-18 22:11:38 +00001852 if (dai->ops.set_sysclk)
1853 return dai->ops.set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001854 else
1855 return -EINVAL;
1856}
1857EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1858
1859/**
1860 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1861 * @dai: DAI
1862 * @clk_id: DAI specific clock divider ID
1863 * @div: new clock divisor.
1864 *
1865 * Configures the clock dividers. This is used to derive the best DAI bit and
1866 * frame clocks from the system or master clock. It's best to set the DAI bit
1867 * and frame clocks as low as possible to save system power.
1868 */
1869int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
1870 int div_id, int div)
1871{
Mark Browndee89c42008-11-18 22:11:38 +00001872 if (dai->ops.set_clkdiv)
1873 return dai->ops.set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001874 else
1875 return -EINVAL;
1876}
1877EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
1878
1879/**
1880 * snd_soc_dai_set_pll - configure DAI PLL.
1881 * @dai: DAI
1882 * @pll_id: DAI specific PLL ID
1883 * @freq_in: PLL input clock frequency in Hz
1884 * @freq_out: requested PLL output clock frequency in Hz
1885 *
1886 * Configures and enables PLL to generate output clock based on input clock.
1887 */
1888int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
1889 int pll_id, unsigned int freq_in, unsigned int freq_out)
1890{
Mark Browndee89c42008-11-18 22:11:38 +00001891 if (dai->ops.set_pll)
1892 return dai->ops.set_pll(dai, pll_id, freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001893 else
1894 return -EINVAL;
1895}
1896EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
1897
1898/**
1899 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1900 * @dai: DAI
1901 * @clk_id: DAI specific clock ID
1902 * @fmt: SND_SOC_DAIFMT_ format value.
1903 *
1904 * Configures the DAI hardware format and clocking.
1905 */
1906int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1907{
Mark Browndee89c42008-11-18 22:11:38 +00001908 if (dai->ops.set_fmt)
1909 return dai->ops.set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001910 else
1911 return -EINVAL;
1912}
1913EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
1914
1915/**
1916 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
1917 * @dai: DAI
1918 * @mask: DAI specific mask representing used slots.
1919 * @slots: Number of slots in use.
1920 *
1921 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
1922 * specific.
1923 */
1924int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
1925 unsigned int mask, int slots)
1926{
Mark Browndee89c42008-11-18 22:11:38 +00001927 if (dai->ops.set_sysclk)
1928 return dai->ops.set_tdm_slot(dai, mask, slots);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001929 else
1930 return -EINVAL;
1931}
1932EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
1933
1934/**
1935 * snd_soc_dai_set_tristate - configure DAI system or master clock.
1936 * @dai: DAI
1937 * @tristate: tristate enable
1938 *
1939 * Tristates the DAI so that others can use it.
1940 */
1941int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
1942{
Mark Browndee89c42008-11-18 22:11:38 +00001943 if (dai->ops.set_sysclk)
1944 return dai->ops.set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001945 else
1946 return -EINVAL;
1947}
1948EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
1949
1950/**
1951 * snd_soc_dai_digital_mute - configure DAI system or master clock.
1952 * @dai: DAI
1953 * @mute: mute enable
1954 *
1955 * Mutes the DAI DAC.
1956 */
1957int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
1958{
Mark Browndee89c42008-11-18 22:11:38 +00001959 if (dai->ops.digital_mute)
1960 return dai->ops.digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01001961 else
1962 return -EINVAL;
1963}
1964EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
1965
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001966static int __devinit snd_soc_init(void)
1967{
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001968 return platform_driver_register(&soc_driver);
1969}
1970
1971static void snd_soc_exit(void)
1972{
Mark Brown3ff3f642008-05-19 12:32:25 +02001973 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001974}
1975
1976module_init(snd_soc_init);
1977module_exit(snd_soc_exit);
1978
1979/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01001980MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001981MODULE_DESCRIPTION("ALSA SoC Core");
1982MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02001983MODULE_ALIAS("platform:soc-audio");