blob: bdbbc6a980fa4b193765346df3f28103e2223238 [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 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +02007 * Author: Liam Girdwood
8 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
Liam Girdwood0664d882006-12-18 14:39:02 +01009 * with code, comments and ideas from :-
10 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020011 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020017 * TODO:
18 * o Add hw rules to enforce rates, etc.
19 * o More testing with other codecs/machines.
20 * o Add more codecs and platforms to ensure good API coverage.
21 * o Support TDM on PCM and I2S
22 */
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/init.h>
27#include <linux/delay.h>
28#include <linux/pm.h>
29#include <linux/bitops.h>
30#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 Girdwoodcb666e52007-02-02 17:13:49 +0100137 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
138 struct snd_soc_codec_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;
275 struct snd_soc_codec_dai *codec_dai;
276 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 Girdwoodcb666e52007-02-02 17:13:49 +0100326 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
327 struct snd_soc_codec_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
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100343 if (cpu_dai->ops.shutdown)
344 cpu_dai->ops.shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200345
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100346 if (codec_dai->ops.shutdown)
347 codec_dai->ops.shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200348
349 if (machine->ops && machine->ops->shutdown)
350 machine->ops->shutdown(substream);
351
352 if (platform->pcm_ops->close)
353 platform->pcm_ops->close(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100354 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200355
356 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
357 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100358 codec_dai->pop_wait = 1;
Takashi Iwai4bb09522006-12-19 17:16:14 +0100359 schedule_delayed_work(&socdev->delayed_work,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200360 msecs_to_jiffies(pmdown_time));
361 } else {
362 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100363 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100364 codec_dai->capture.stream_name,
365 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200366
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100367 if (codec->active == 0 && codec_dai->pop_wait == 0)
Mark Brown0be98982008-05-19 12:31:28 +0200368 snd_soc_dapm_set_bias_level(socdev,
369 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200370 }
371
372 mutex_unlock(&pcm_mutex);
373 return 0;
374}
375
376/*
377 * Called by ALSA when the PCM substream is prepared, can set format, sample
378 * rate, etc. This function is non atomic and can be called multiple times,
379 * it can refer to the runtime info.
380 */
381static int soc_pcm_prepare(struct snd_pcm_substream *substream)
382{
383 struct snd_soc_pcm_runtime *rtd = substream->private_data;
384 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100385 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200386 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100387 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
388 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200389 struct snd_soc_codec *codec = socdev->codec;
390 int ret = 0;
391
392 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100393
394 if (machine->ops && machine->ops->prepare) {
395 ret = machine->ops->prepare(substream);
396 if (ret < 0) {
397 printk(KERN_ERR "asoc: machine prepare error\n");
398 goto out;
399 }
400 }
401
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200402 if (platform->pcm_ops->prepare) {
403 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200404 if (ret < 0) {
405 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200406 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200407 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200408 }
409
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100410 if (codec_dai->ops.prepare) {
411 ret = codec_dai->ops.prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200412 if (ret < 0) {
413 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200414 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200415 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200416 }
417
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100418 if (cpu_dai->ops.prepare) {
419 ret = cpu_dai->ops.prepare(substream);
420 if (ret < 0) {
421 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
422 goto out;
423 }
424 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200425
426 /* we only want to start a DAPM playback stream if we are not waiting
427 * on an existing one stopping */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100428 if (codec_dai->pop_wait) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200429 /* we are waiting for the delayed work to start */
430 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100431 snd_soc_dapm_stream_event(socdev->codec,
432 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200433 SND_SOC_DAPM_STREAM_START);
434 else {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100435 codec_dai->pop_wait = 0;
Andrew Morton4484bb22006-12-15 09:30:07 +0100436 cancel_delayed_work(&socdev->delayed_work);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100437 if (codec_dai->dai_ops.digital_mute)
438 codec_dai->dai_ops.digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200439 }
440 } else {
441 /* no delayed work - do we need to power up codec */
Mark Brown0be98982008-05-19 12:31:28 +0200442 if (codec->bias_level != SND_SOC_BIAS_ON) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200443
Mark Brown0be98982008-05-19 12:31:28 +0200444 snd_soc_dapm_set_bias_level(socdev,
445 SND_SOC_BIAS_PREPARE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200446
447 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
448 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100449 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200450 SND_SOC_DAPM_STREAM_START);
451 else
452 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100453 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200454 SND_SOC_DAPM_STREAM_START);
455
Mark Brown0be98982008-05-19 12:31:28 +0200456 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100457 if (codec_dai->dai_ops.digital_mute)
458 codec_dai->dai_ops.digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200459
460 } else {
461 /* codec already powered - power on widgets */
462 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
463 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100464 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200465 SND_SOC_DAPM_STREAM_START);
466 else
467 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100468 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200469 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100470 if (codec_dai->dai_ops.digital_mute)
471 codec_dai->dai_ops.digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200472 }
473 }
474
475out:
476 mutex_unlock(&pcm_mutex);
477 return ret;
478}
479
480/*
481 * Called by ALSA when the hardware params are set by application. This
482 * function can also be called multiple times and can allocate buffers
483 * (using snd_pcm_lib_* ). It's non-atomic.
484 */
485static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
486 struct snd_pcm_hw_params *params)
487{
488 struct snd_soc_pcm_runtime *rtd = substream->private_data;
489 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100490 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200491 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100492 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
493 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200494 int ret = 0;
495
496 mutex_lock(&pcm_mutex);
497
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100498 if (machine->ops && machine->ops->hw_params) {
499 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200500 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100501 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200502 goto out;
503 }
504 }
505
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100506 if (codec_dai->ops.hw_params) {
507 ret = codec_dai->ops.hw_params(substream, params);
508 if (ret < 0) {
509 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
510 codec_dai->name);
511 goto codec_err;
512 }
513 }
514
515 if (cpu_dai->ops.hw_params) {
516 ret = cpu_dai->ops.hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200517 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200518 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100519 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200520 goto interface_err;
521 }
522 }
523
524 if (platform->pcm_ops->hw_params) {
525 ret = platform->pcm_ops->hw_params(substream, params);
526 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200527 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200528 platform->name);
529 goto platform_err;
530 }
531 }
532
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200533out:
534 mutex_unlock(&pcm_mutex);
535 return ret;
536
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200537platform_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100538 if (cpu_dai->ops.hw_free)
539 cpu_dai->ops.hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200540
541interface_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100542 if (codec_dai->ops.hw_free)
543 codec_dai->ops.hw_free(substream);
544
545codec_err:
Mark Brown3ff3f642008-05-19 12:32:25 +0200546 if (machine->ops && machine->ops->hw_free)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100547 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548
549 mutex_unlock(&pcm_mutex);
550 return ret;
551}
552
553/*
554 * Free's resources allocated by hw_params, can be called multiple times
555 */
556static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
557{
558 struct snd_soc_pcm_runtime *rtd = substream->private_data;
559 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100560 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200561 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100562 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
563 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564 struct snd_soc_codec *codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200565
566 mutex_lock(&pcm_mutex);
567
568 /* apply codec digital mute */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100569 if (!codec->active && codec_dai->dai_ops.digital_mute)
570 codec_dai->dai_ops.digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200571
572 /* free any machine hw params */
573 if (machine->ops && machine->ops->hw_free)
574 machine->ops->hw_free(substream);
575
576 /* free any DMA resources */
577 if (platform->pcm_ops->hw_free)
578 platform->pcm_ops->hw_free(substream);
579
580 /* now free hw params for the DAI's */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100581 if (codec_dai->ops.hw_free)
582 codec_dai->ops.hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200583
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100584 if (cpu_dai->ops.hw_free)
585 cpu_dai->ops.hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200586
587 mutex_unlock(&pcm_mutex);
588 return 0;
589}
590
591static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
592{
593 struct snd_soc_pcm_runtime *rtd = substream->private_data;
594 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100595 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100597 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
598 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200599 int ret;
600
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100601 if (codec_dai->ops.trigger) {
602 ret = codec_dai->ops.trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200603 if (ret < 0)
604 return ret;
605 }
606
607 if (platform->pcm_ops->trigger) {
608 ret = platform->pcm_ops->trigger(substream, cmd);
609 if (ret < 0)
610 return ret;
611 }
612
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100613 if (cpu_dai->ops.trigger) {
614 ret = cpu_dai->ops.trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200615 if (ret < 0)
616 return ret;
617 }
618 return 0;
619}
620
621/* ASoC PCM operations */
622static struct snd_pcm_ops soc_pcm_ops = {
623 .open = soc_pcm_open,
624 .close = soc_codec_close,
625 .hw_params = soc_pcm_hw_params,
626 .hw_free = soc_pcm_hw_free,
627 .prepare = soc_pcm_prepare,
628 .trigger = soc_pcm_trigger,
629};
630
631#ifdef CONFIG_PM
632/* powers down audio subsystem for suspend */
633static int soc_suspend(struct platform_device *pdev, pm_message_t state)
634{
Mark Brown3ff3f642008-05-19 12:32:25 +0200635 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
636 struct snd_soc_machine *machine = socdev->machine;
637 struct snd_soc_platform *platform = socdev->platform;
638 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200639 struct snd_soc_codec *codec = socdev->codec;
640 int i;
641
Andy Green6ed25972008-06-13 16:24:05 +0100642 /* Due to the resume being scheduled into a workqueue we could
643 * suspend before that's finished - wait for it to complete.
644 */
645 snd_power_lock(codec->card);
646 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
647 snd_power_unlock(codec->card);
648
649 /* we're going to block userspace touching us until resume completes */
650 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
651
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200652 /* mute any active DAC's */
Mark Brown3ff3f642008-05-19 12:32:25 +0200653 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100655 if (dai->dai_ops.digital_mute && dai->playback.active)
656 dai->dai_ops.digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200657 }
658
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100659 /* suspend all pcms */
660 for (i = 0; i < machine->num_links; i++)
661 snd_pcm_suspend_all(machine->dai_link[i].pcm);
662
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200663 if (machine->suspend_pre)
664 machine->suspend_pre(pdev, state);
665
Mark Brown3ff3f642008-05-19 12:32:25 +0200666 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200667 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
668 if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
669 cpu_dai->suspend(pdev, cpu_dai);
670 if (platform->suspend)
671 platform->suspend(pdev, cpu_dai);
672 }
673
674 /* close any waiting streams and save state */
Liam Girdwood965ac422007-01-31 14:14:57 +0100675 run_delayed_work(&socdev->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200676 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200677
Mark Brown3ff3f642008-05-19 12:32:25 +0200678 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200679 char *stream = codec->dai[i].playback.stream_name;
680 if (stream != NULL)
681 snd_soc_dapm_stream_event(codec, stream,
682 SND_SOC_DAPM_STREAM_SUSPEND);
683 stream = codec->dai[i].capture.stream_name;
684 if (stream != NULL)
685 snd_soc_dapm_stream_event(codec, stream,
686 SND_SOC_DAPM_STREAM_SUSPEND);
687 }
688
689 if (codec_dev->suspend)
690 codec_dev->suspend(pdev, state);
691
Mark Brown3ff3f642008-05-19 12:32:25 +0200692 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200693 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
694 if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
695 cpu_dai->suspend(pdev, cpu_dai);
696 }
697
698 if (machine->suspend_post)
699 machine->suspend_post(pdev, state);
700
701 return 0;
702}
703
Andy Green6ed25972008-06-13 16:24:05 +0100704/* deferred resume work, so resume can complete before we finished
705 * setting our codec back up, which can be very slow on I2C
706 */
707static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200708{
Andy Green6ed25972008-06-13 16:24:05 +0100709 struct snd_soc_device *socdev = container_of(work,
710 struct snd_soc_device,
711 deferred_resume_work);
Mark Brown3ff3f642008-05-19 12:32:25 +0200712 struct snd_soc_machine *machine = socdev->machine;
713 struct snd_soc_platform *platform = socdev->platform;
714 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200715 struct snd_soc_codec *codec = socdev->codec;
Andy Green6ed25972008-06-13 16:24:05 +0100716 struct platform_device *pdev = to_platform_device(socdev->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200717 int i;
718
Andy Green6ed25972008-06-13 16:24:05 +0100719 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
720 * so userspace apps are blocked from touching us
721 */
722
723 dev_info(socdev->dev, "starting resume work\n");
724
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200725 if (machine->resume_pre)
726 machine->resume_pre(pdev);
727
Mark Brown3ff3f642008-05-19 12:32:25 +0200728 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200729 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
730 if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
731 cpu_dai->resume(pdev, cpu_dai);
732 }
733
734 if (codec_dev->resume)
735 codec_dev->resume(pdev);
736
Mark Brown3ff3f642008-05-19 12:32:25 +0200737 for (i = 0; i < codec->num_dai; i++) {
738 char *stream = codec->dai[i].playback.stream_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200739 if (stream != NULL)
740 snd_soc_dapm_stream_event(codec, stream,
741 SND_SOC_DAPM_STREAM_RESUME);
742 stream = codec->dai[i].capture.stream_name;
743 if (stream != NULL)
744 snd_soc_dapm_stream_event(codec, stream,
745 SND_SOC_DAPM_STREAM_RESUME);
746 }
747
Mark Brown3ff3f642008-05-19 12:32:25 +0200748 /* unmute any active DACs */
749 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100751 if (dai->dai_ops.digital_mute && dai->playback.active)
752 dai->dai_ops.digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200753 }
754
Mark Brown3ff3f642008-05-19 12:32:25 +0200755 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200756 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
757 if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
758 cpu_dai->resume(pdev, cpu_dai);
759 if (platform->resume)
760 platform->resume(pdev, cpu_dai);
761 }
762
763 if (machine->resume_post)
764 machine->resume_post(pdev);
765
Andy Green6ed25972008-06-13 16:24:05 +0100766 dev_info(socdev->dev, "resume work completed\n");
767
768 /* userspace can access us now we are back as we were before */
769 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
770}
771
772/* powers up audio subsystem after a suspend */
773static int soc_resume(struct platform_device *pdev)
774{
775 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
776
777 dev_info(socdev->dev, "scheduling resume work\n");
778
779 if (!schedule_work(&socdev->deferred_resume_work))
780 dev_err(socdev->dev, "work item may be lost\n");
781
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200782 return 0;
783}
784
785#else
786#define soc_suspend NULL
787#define soc_resume NULL
788#endif
789
790/* probes a new socdev */
791static int soc_probe(struct platform_device *pdev)
792{
793 int ret = 0, i;
794 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
795 struct snd_soc_machine *machine = socdev->machine;
796 struct snd_soc_platform *platform = socdev->platform;
797 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
798
799 if (machine->probe) {
800 ret = machine->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200801 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200802 return ret;
803 }
804
805 for (i = 0; i < machine->num_links; i++) {
806 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
807 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +0100808 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +0200809 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200810 goto cpu_dai_err;
811 }
812 }
813
814 if (codec_dev->probe) {
815 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200816 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200817 goto cpu_dai_err;
818 }
819
820 if (platform->probe) {
821 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200822 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200823 goto platform_err;
824 }
825
826 /* DAPM stream work */
Andrew Morton4484bb22006-12-15 09:30:07 +0100827 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
Randy Dunlap1301a962008-06-17 19:19:34 +0100828#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +0100829 /* deferred resume work */
830 INIT_WORK(&socdev->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +0100831#endif
Andy Green6ed25972008-06-13 16:24:05 +0100832
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200833 return 0;
834
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200835platform_err:
836 if (codec_dev->remove)
837 codec_dev->remove(pdev);
838
839cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +0100840 for (i--; i >= 0; i--) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200841 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
842 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100843 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200844 }
845
846 if (machine->remove)
847 machine->remove(pdev);
848
849 return ret;
850}
851
852/* removes a socdev */
853static int soc_remove(struct platform_device *pdev)
854{
855 int i;
856 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
857 struct snd_soc_machine *machine = socdev->machine;
858 struct snd_soc_platform *platform = socdev->platform;
859 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
860
Liam Girdwood965ac422007-01-31 14:14:57 +0100861 run_delayed_work(&socdev->delayed_work);
862
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200863 if (platform->remove)
864 platform->remove(pdev);
865
866 if (codec_dev->remove)
867 codec_dev->remove(pdev);
868
869 for (i = 0; i < machine->num_links; i++) {
870 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
871 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100872 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200873 }
874
875 if (machine->remove)
876 machine->remove(pdev);
877
878 return 0;
879}
880
881/* ASoC platform driver */
882static struct platform_driver soc_driver = {
883 .driver = {
884 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +0200885 .owner = THIS_MODULE,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200886 },
887 .probe = soc_probe,
888 .remove = soc_remove,
889 .suspend = soc_suspend,
890 .resume = soc_resume,
891};
892
893/* create a new pcm */
894static int soc_new_pcm(struct snd_soc_device *socdev,
895 struct snd_soc_dai_link *dai_link, int num)
896{
897 struct snd_soc_codec *codec = socdev->codec;
898 struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
899 struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
900 struct snd_soc_pcm_runtime *rtd;
901 struct snd_pcm *pcm;
902 char new_name[64];
903 int ret = 0, playback = 0, capture = 0;
904
905 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
906 if (rtd == NULL)
907 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100908
909 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200910 rtd->socdev = socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100911 codec_dai->codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200912
913 /* check client and interface hw capabilities */
Mark Brown3ff3f642008-05-19 12:32:25 +0200914 sprintf(new_name, "%s %s-%s-%d", dai_link->stream_name, codec_dai->name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200915 get_dai_name(cpu_dai->type), num);
916
917 if (codec_dai->playback.channels_min)
918 playback = 1;
919 if (codec_dai->capture.channels_min)
920 capture = 1;
921
922 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
923 capture, &pcm);
924 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200925 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
926 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200927 kfree(rtd);
928 return ret;
929 }
930
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100931 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200932 pcm->private_data = rtd;
933 soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
934 soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
935 soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
936 soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
937 soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
938 soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
939 soc_pcm_ops.page = socdev->platform->pcm_ops->page;
940
941 if (playback)
942 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
943
944 if (capture)
945 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
946
947 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
948 if (ret < 0) {
949 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
950 kfree(rtd);
951 return ret;
952 }
953
954 pcm->private_free = socdev->platform->pcm_free;
955 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
956 cpu_dai->name);
957 return ret;
958}
959
960/* codec register dump */
961static ssize_t codec_reg_show(struct device *dev,
962 struct device_attribute *attr, char *buf)
963{
964 struct snd_soc_device *devdata = dev_get_drvdata(dev);
965 struct snd_soc_codec *codec = devdata->codec;
966 int i, step = 1, count = 0;
967
968 if (!codec->reg_cache_size)
969 return 0;
970
971 if (codec->reg_cache_step)
972 step = codec->reg_cache_step;
973
974 count += sprintf(buf, "%s registers\n", codec->name);
Mark Brown3ff3f642008-05-19 12:32:25 +0200975 for (i = 0; i < codec->reg_cache_size; i += step)
976 count += sprintf(buf + count, "%2x: %4x\n", i,
977 codec->read(codec, i));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200978
979 return count;
980}
981static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
982
983/**
984 * snd_soc_new_ac97_codec - initailise AC97 device
985 * @codec: audio codec
986 * @ops: AC97 bus operations
987 * @num: AC97 codec number
988 *
989 * Initialises AC97 codec resources for use by ad-hoc devices only.
990 */
991int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
992 struct snd_ac97_bus_ops *ops, int num)
993{
994 mutex_lock(&codec->mutex);
995
996 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
997 if (codec->ac97 == NULL) {
998 mutex_unlock(&codec->mutex);
999 return -ENOMEM;
1000 }
1001
1002 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1003 if (codec->ac97->bus == NULL) {
1004 kfree(codec->ac97);
1005 codec->ac97 = NULL;
1006 mutex_unlock(&codec->mutex);
1007 return -ENOMEM;
1008 }
1009
1010 codec->ac97->bus->ops = ops;
1011 codec->ac97->num = num;
1012 mutex_unlock(&codec->mutex);
1013 return 0;
1014}
1015EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1016
1017/**
1018 * snd_soc_free_ac97_codec - free AC97 codec device
1019 * @codec: audio codec
1020 *
1021 * Frees AC97 codec device resources.
1022 */
1023void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1024{
1025 mutex_lock(&codec->mutex);
1026 kfree(codec->ac97->bus);
1027 kfree(codec->ac97);
1028 codec->ac97 = NULL;
1029 mutex_unlock(&codec->mutex);
1030}
1031EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1032
1033/**
1034 * snd_soc_update_bits - update codec register bits
1035 * @codec: audio codec
1036 * @reg: codec register
1037 * @mask: register mask
1038 * @value: new value
1039 *
1040 * Writes new register value.
1041 *
1042 * Returns 1 for change else 0.
1043 */
1044int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1045 unsigned short mask, unsigned short value)
1046{
1047 int change;
1048 unsigned short old, new;
1049
1050 mutex_lock(&io_mutex);
1051 old = snd_soc_read(codec, reg);
1052 new = (old & ~mask) | value;
1053 change = old != new;
1054 if (change)
1055 snd_soc_write(codec, reg, new);
1056
1057 mutex_unlock(&io_mutex);
1058 return change;
1059}
1060EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1061
1062/**
1063 * snd_soc_test_bits - test register for change
1064 * @codec: audio codec
1065 * @reg: codec register
1066 * @mask: register mask
1067 * @value: new value
1068 *
1069 * Tests a register with a new value and checks if the new value is
1070 * different from the old value.
1071 *
1072 * Returns 1 for change else 0.
1073 */
1074int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1075 unsigned short mask, unsigned short value)
1076{
1077 int change;
1078 unsigned short old, new;
1079
1080 mutex_lock(&io_mutex);
1081 old = snd_soc_read(codec, reg);
1082 new = (old & ~mask) | value;
1083 change = old != new;
1084 mutex_unlock(&io_mutex);
1085
1086 return change;
1087}
1088EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1089
1090/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001091 * snd_soc_new_pcms - create new sound card and pcms
1092 * @socdev: the SoC audio device
1093 *
1094 * Create a new sound card based upon the codec and interface pcms.
1095 *
1096 * Returns 0 for success, else error.
1097 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001098int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001099{
1100 struct snd_soc_codec *codec = socdev->codec;
1101 struct snd_soc_machine *machine = socdev->machine;
1102 int ret = 0, i;
1103
1104 mutex_lock(&codec->mutex);
1105
1106 /* register a sound card */
1107 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1108 if (!codec->card) {
1109 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1110 codec->name);
1111 mutex_unlock(&codec->mutex);
1112 return -ENODEV;
1113 }
1114
1115 codec->card->dev = socdev->dev;
1116 codec->card->private_data = codec;
1117 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1118
1119 /* create the pcms */
Mark Brown3ff3f642008-05-19 12:32:25 +02001120 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001121 ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
1122 if (ret < 0) {
1123 printk(KERN_ERR "asoc: can't create pcm %s\n",
1124 machine->dai_link[i].stream_name);
1125 mutex_unlock(&codec->mutex);
1126 return ret;
1127 }
1128 }
1129
1130 mutex_unlock(&codec->mutex);
1131 return ret;
1132}
1133EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1134
1135/**
1136 * snd_soc_register_card - register sound card
1137 * @socdev: the SoC audio device
1138 *
1139 * Register a SoC sound card. Also registers an AC97 device if the
1140 * codec is AC97 for ad hoc devices.
1141 *
1142 * Returns 0 for success, else error.
1143 */
1144int snd_soc_register_card(struct snd_soc_device *socdev)
1145{
1146 struct snd_soc_codec *codec = socdev->codec;
1147 struct snd_soc_machine *machine = socdev->machine;
Liam Girdwood12e74f72006-10-16 21:19:48 +02001148 int ret = 0, i, ac97 = 0, err = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001149
Mark Brown3ff3f642008-05-19 12:32:25 +02001150 for (i = 0; i < machine->num_links; i++) {
Liam Girdwood12e74f72006-10-16 21:19:48 +02001151 if (socdev->machine->dai_link[i].init) {
1152 err = socdev->machine->dai_link[i].init(codec);
1153 if (err < 0) {
1154 printk(KERN_ERR "asoc: failed to init %s\n",
1155 socdev->machine->dai_link[i].stream_name);
1156 continue;
1157 }
1158 }
Mark Brown3ff3f642008-05-19 12:32:25 +02001159 if (socdev->machine->dai_link[i].codec_dai->type ==
Liam Girdwooda68660e2007-05-10 19:27:27 +02001160 SND_SOC_DAI_AC97_BUS)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001161 ac97 = 1;
1162 }
1163 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1164 "%s", machine->name);
1165 snprintf(codec->card->longname, sizeof(codec->card->longname),
1166 "%s (%s)", machine->name, codec->name);
1167
1168 ret = snd_card_register(codec->card);
1169 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001170 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001171 codec->name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001172 goto out;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001173 }
1174
Mark Brown08c8efe2008-01-21 14:33:37 +01001175 mutex_lock(&codec->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001176#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood12e74f72006-10-16 21:19:48 +02001177 if (ac97) {
1178 ret = soc_ac97_dev_register(codec);
1179 if (ret < 0) {
1180 printk(KERN_ERR "asoc: AC97 device register failed\n");
1181 snd_card_free(codec->card);
Mark Brown08c8efe2008-01-21 14:33:37 +01001182 mutex_unlock(&codec->mutex);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001183 goto out;
1184 }
1185 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001186#endif
1187
Liam Girdwood12e74f72006-10-16 21:19:48 +02001188 err = snd_soc_dapm_sys_add(socdev->dev);
1189 if (err < 0)
1190 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1191
1192 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1193 if (err < 0)
Mark Brown3ff3f642008-05-19 12:32:25 +02001194 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
Mark Brown08c8efe2008-01-21 14:33:37 +01001195
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001196 mutex_unlock(&codec->mutex);
Mark Brown08c8efe2008-01-21 14:33:37 +01001197
1198out:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001199 return ret;
1200}
1201EXPORT_SYMBOL_GPL(snd_soc_register_card);
1202
1203/**
1204 * snd_soc_free_pcms - free sound card and pcms
1205 * @socdev: the SoC audio device
1206 *
1207 * Frees sound card and pcms associated with the socdev.
1208 * Also unregister the codec if it is an AC97 device.
1209 */
1210void snd_soc_free_pcms(struct snd_soc_device *socdev)
1211{
1212 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001213#ifdef CONFIG_SND_SOC_AC97_BUS
1214 struct snd_soc_codec_dai *codec_dai;
1215 int i;
1216#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001217
1218 mutex_lock(&codec->mutex);
1219#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001220 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001221 codec_dai = &codec->dai[i];
1222 if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
1223 soc_ac97_dev_unregister(codec);
1224 goto free_card;
1225 }
1226 }
1227free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001228#endif
1229
1230 if (codec->card)
1231 snd_card_free(codec->card);
1232 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1233 mutex_unlock(&codec->mutex);
1234}
1235EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1236
1237/**
1238 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1239 * @substream: the pcm substream
1240 * @hw: the hardware parameters
1241 *
1242 * Sets the substream runtime hardware parameters.
1243 */
1244int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1245 const struct snd_pcm_hardware *hw)
1246{
1247 struct snd_pcm_runtime *runtime = substream->runtime;
1248 runtime->hw.info = hw->info;
1249 runtime->hw.formats = hw->formats;
1250 runtime->hw.period_bytes_min = hw->period_bytes_min;
1251 runtime->hw.period_bytes_max = hw->period_bytes_max;
1252 runtime->hw.periods_min = hw->periods_min;
1253 runtime->hw.periods_max = hw->periods_max;
1254 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1255 runtime->hw.fifo_size = hw->fifo_size;
1256 return 0;
1257}
1258EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1259
1260/**
1261 * snd_soc_cnew - create new control
1262 * @_template: control template
1263 * @data: control private data
1264 * @lnng_name: control long name
1265 *
1266 * Create a new mixer control from a template control.
1267 *
1268 * Returns 0 for success, else error.
1269 */
1270struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1271 void *data, char *long_name)
1272{
1273 struct snd_kcontrol_new template;
1274
1275 memcpy(&template, _template, sizeof(template));
1276 if (long_name)
1277 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001278 template.index = 0;
1279
1280 return snd_ctl_new1(&template, data);
1281}
1282EXPORT_SYMBOL_GPL(snd_soc_cnew);
1283
1284/**
1285 * snd_soc_info_enum_double - enumerated double mixer info callback
1286 * @kcontrol: mixer control
1287 * @uinfo: control element information
1288 *
1289 * Callback to provide information about a double enumerated
1290 * mixer control.
1291 *
1292 * Returns 0 for success.
1293 */
1294int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1295 struct snd_ctl_elem_info *uinfo)
1296{
1297 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1298
1299 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1300 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1301 uinfo->value.enumerated.items = e->mask;
1302
1303 if (uinfo->value.enumerated.item > e->mask - 1)
1304 uinfo->value.enumerated.item = e->mask - 1;
1305 strcpy(uinfo->value.enumerated.name,
1306 e->texts[uinfo->value.enumerated.item]);
1307 return 0;
1308}
1309EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1310
1311/**
1312 * snd_soc_get_enum_double - enumerated double mixer get callback
1313 * @kcontrol: mixer control
1314 * @uinfo: control element information
1315 *
1316 * Callback to get the value of a double enumerated mixer.
1317 *
1318 * Returns 0 for success.
1319 */
1320int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1321 struct snd_ctl_elem_value *ucontrol)
1322{
1323 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1324 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1325 unsigned short val, bitmask;
1326
1327 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1328 ;
1329 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001330 ucontrol->value.enumerated.item[0]
1331 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001332 if (e->shift_l != e->shift_r)
1333 ucontrol->value.enumerated.item[1] =
1334 (val >> e->shift_r) & (bitmask - 1);
1335
1336 return 0;
1337}
1338EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1339
1340/**
1341 * snd_soc_put_enum_double - enumerated double mixer put callback
1342 * @kcontrol: mixer control
1343 * @uinfo: control element information
1344 *
1345 * Callback to set the value of a double enumerated mixer.
1346 *
1347 * Returns 0 for success.
1348 */
1349int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1350 struct snd_ctl_elem_value *ucontrol)
1351{
1352 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1353 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1354 unsigned short val;
1355 unsigned short mask, bitmask;
1356
1357 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1358 ;
1359 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1360 return -EINVAL;
1361 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1362 mask = (bitmask - 1) << e->shift_l;
1363 if (e->shift_l != e->shift_r) {
1364 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
1365 return -EINVAL;
1366 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1367 mask |= (bitmask - 1) << e->shift_r;
1368 }
1369
1370 return snd_soc_update_bits(codec, e->reg, mask, val);
1371}
1372EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1373
1374/**
1375 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1376 * @kcontrol: mixer control
1377 * @uinfo: control element information
1378 *
1379 * Callback to provide information about an external enumerated
1380 * single mixer.
1381 *
1382 * Returns 0 for success.
1383 */
1384int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1385 struct snd_ctl_elem_info *uinfo)
1386{
1387 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1388
1389 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1390 uinfo->count = 1;
1391 uinfo->value.enumerated.items = e->mask;
1392
1393 if (uinfo->value.enumerated.item > e->mask - 1)
1394 uinfo->value.enumerated.item = e->mask - 1;
1395 strcpy(uinfo->value.enumerated.name,
1396 e->texts[uinfo->value.enumerated.item]);
1397 return 0;
1398}
1399EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1400
1401/**
1402 * snd_soc_info_volsw_ext - external single mixer info callback
1403 * @kcontrol: mixer control
1404 * @uinfo: control element information
1405 *
1406 * Callback to provide information about a single external mixer control.
1407 *
1408 * Returns 0 for success.
1409 */
1410int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1411 struct snd_ctl_elem_info *uinfo)
1412{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001413 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001414
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001415 if (max == 1)
1416 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1417 else
1418 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1419
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001420 uinfo->count = 1;
1421 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001422 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001423 return 0;
1424}
1425EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1426
1427/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001428 * snd_soc_info_volsw - single mixer info callback
1429 * @kcontrol: mixer control
1430 * @uinfo: control element information
1431 *
1432 * Callback to provide information about a single mixer control.
1433 *
1434 * Returns 0 for success.
1435 */
1436int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1437 struct snd_ctl_elem_info *uinfo)
1438{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001439 int max = (kcontrol->private_value >> 16) & 0xff;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001440 int shift = (kcontrol->private_value >> 8) & 0x0f;
1441 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1442
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001443 if (max == 1)
1444 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1445 else
1446 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1447
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001448 uinfo->count = shift == rshift ? 1 : 2;
1449 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001450 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001451 return 0;
1452}
1453EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1454
1455/**
1456 * snd_soc_get_volsw - single mixer get callback
1457 * @kcontrol: mixer control
1458 * @uinfo: control element information
1459 *
1460 * Callback to get the value of a single mixer control.
1461 *
1462 * Returns 0 for success.
1463 */
1464int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1465 struct snd_ctl_elem_value *ucontrol)
1466{
1467 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1468 int reg = kcontrol->private_value & 0xff;
1469 int shift = (kcontrol->private_value >> 8) & 0x0f;
1470 int rshift = (kcontrol->private_value >> 12) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001471 int max = (kcontrol->private_value >> 16) & 0xff;
1472 int mask = (1 << fls(max)) - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001473 int invert = (kcontrol->private_value >> 24) & 0x01;
1474
1475 ucontrol->value.integer.value[0] =
1476 (snd_soc_read(codec, reg) >> shift) & mask;
1477 if (shift != rshift)
1478 ucontrol->value.integer.value[1] =
1479 (snd_soc_read(codec, reg) >> rshift) & mask;
1480 if (invert) {
1481 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001482 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001483 if (shift != rshift)
1484 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001485 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001486 }
1487
1488 return 0;
1489}
1490EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1491
1492/**
1493 * snd_soc_put_volsw - single mixer put callback
1494 * @kcontrol: mixer control
1495 * @uinfo: control element information
1496 *
1497 * Callback to set the value of a single mixer control.
1498 *
1499 * Returns 0 for success.
1500 */
1501int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1502 struct snd_ctl_elem_value *ucontrol)
1503{
1504 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1505 int reg = kcontrol->private_value & 0xff;
1506 int shift = (kcontrol->private_value >> 8) & 0x0f;
1507 int rshift = (kcontrol->private_value >> 12) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001508 int max = (kcontrol->private_value >> 16) & 0xff;
1509 int mask = (1 << fls(max)) - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001510 int invert = (kcontrol->private_value >> 24) & 0x01;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001511 unsigned short val, val2, val_mask;
1512
1513 val = (ucontrol->value.integer.value[0] & mask);
1514 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001515 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001516 val_mask = mask << shift;
1517 val = val << shift;
1518 if (shift != rshift) {
1519 val2 = (ucontrol->value.integer.value[1] & mask);
1520 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001521 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001522 val_mask |= mask << rshift;
1523 val |= val2 << rshift;
1524 }
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001525 return snd_soc_update_bits(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001526}
1527EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1528
1529/**
1530 * snd_soc_info_volsw_2r - double mixer info callback
1531 * @kcontrol: mixer control
1532 * @uinfo: control element information
1533 *
1534 * Callback to provide information about a double mixer control that
1535 * spans 2 codec registers.
1536 *
1537 * Returns 0 for success.
1538 */
1539int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1540 struct snd_ctl_elem_info *uinfo)
1541{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001542 int max = (kcontrol->private_value >> 12) & 0xff;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001543
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001544 if (max == 1)
1545 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1546 else
1547 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1548
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001549 uinfo->count = 2;
1550 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001551 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001552 return 0;
1553}
1554EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1555
1556/**
1557 * snd_soc_get_volsw_2r - double mixer get callback
1558 * @kcontrol: mixer control
1559 * @uinfo: control element information
1560 *
1561 * Callback to get the value of a double mixer control that spans 2 registers.
1562 *
1563 * Returns 0 for success.
1564 */
1565int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1566 struct snd_ctl_elem_value *ucontrol)
1567{
1568 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1569 int reg = kcontrol->private_value & 0xff;
1570 int reg2 = (kcontrol->private_value >> 24) & 0xff;
1571 int shift = (kcontrol->private_value >> 8) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001572 int max = (kcontrol->private_value >> 12) & 0xff;
1573 int mask = (1<<fls(max))-1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001574 int invert = (kcontrol->private_value >> 20) & 0x01;
1575
1576 ucontrol->value.integer.value[0] =
1577 (snd_soc_read(codec, reg) >> shift) & mask;
1578 ucontrol->value.integer.value[1] =
1579 (snd_soc_read(codec, reg2) >> shift) & mask;
1580 if (invert) {
1581 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001582 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001583 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001584 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001585 }
1586
1587 return 0;
1588}
1589EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1590
1591/**
1592 * snd_soc_put_volsw_2r - double mixer set callback
1593 * @kcontrol: mixer control
1594 * @uinfo: control element information
1595 *
1596 * Callback to set the value of a double mixer control that spans 2 registers.
1597 *
1598 * Returns 0 for success.
1599 */
1600int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1601 struct snd_ctl_elem_value *ucontrol)
1602{
1603 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1604 int reg = kcontrol->private_value & 0xff;
1605 int reg2 = (kcontrol->private_value >> 24) & 0xff;
1606 int shift = (kcontrol->private_value >> 8) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001607 int max = (kcontrol->private_value >> 12) & 0xff;
1608 int mask = (1 << fls(max)) - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001609 int invert = (kcontrol->private_value >> 20) & 0x01;
1610 int err;
1611 unsigned short val, val2, val_mask;
1612
1613 val_mask = mask << shift;
1614 val = (ucontrol->value.integer.value[0] & mask);
1615 val2 = (ucontrol->value.integer.value[1] & mask);
1616
1617 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001618 val = max - val;
1619 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001620 }
1621
1622 val = val << shift;
1623 val2 = val2 << shift;
1624
Mark Brown3ff3f642008-05-19 12:32:25 +02001625 err = snd_soc_update_bits(codec, reg, val_mask, val);
1626 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001627 return err;
1628
1629 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1630 return err;
1631}
1632EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1633
Mark Browne13ac2e2008-05-28 17:58:05 +01001634/**
1635 * snd_soc_info_volsw_s8 - signed mixer info callback
1636 * @kcontrol: mixer control
1637 * @uinfo: control element information
1638 *
1639 * Callback to provide information about a signed mixer control.
1640 *
1641 * Returns 0 for success.
1642 */
1643int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1644 struct snd_ctl_elem_info *uinfo)
1645{
1646 int max = (signed char)((kcontrol->private_value >> 16) & 0xff);
1647 int min = (signed char)((kcontrol->private_value >> 24) & 0xff);
1648
1649 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1650 uinfo->count = 2;
1651 uinfo->value.integer.min = 0;
1652 uinfo->value.integer.max = max-min;
1653 return 0;
1654}
1655EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1656
1657/**
1658 * snd_soc_get_volsw_s8 - signed mixer get callback
1659 * @kcontrol: mixer control
1660 * @uinfo: control element information
1661 *
1662 * Callback to get the value of a signed mixer control.
1663 *
1664 * Returns 0 for success.
1665 */
1666int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1667 struct snd_ctl_elem_value *ucontrol)
1668{
1669 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1670 int reg = kcontrol->private_value & 0xff;
1671 int min = (signed char)((kcontrol->private_value >> 24) & 0xff);
1672 int val = snd_soc_read(codec, reg);
1673
1674 ucontrol->value.integer.value[0] =
1675 ((signed char)(val & 0xff))-min;
1676 ucontrol->value.integer.value[1] =
1677 ((signed char)((val >> 8) & 0xff))-min;
1678 return 0;
1679}
1680EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1681
1682/**
1683 * snd_soc_put_volsw_sgn - signed mixer put callback
1684 * @kcontrol: mixer control
1685 * @uinfo: control element information
1686 *
1687 * Callback to set the value of a signed mixer control.
1688 *
1689 * Returns 0 for success.
1690 */
1691int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1692 struct snd_ctl_elem_value *ucontrol)
1693{
1694 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1695 int reg = kcontrol->private_value & 0xff;
1696 int min = (signed char)((kcontrol->private_value >> 24) & 0xff);
1697 unsigned short val;
1698
1699 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1700 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1701
1702 return snd_soc_update_bits(codec, reg, 0xffff, val);
1703}
1704EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1705
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001706static int __devinit snd_soc_init(void)
1707{
1708 printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
1709 return platform_driver_register(&soc_driver);
1710}
1711
1712static void snd_soc_exit(void)
1713{
Mark Brown3ff3f642008-05-19 12:32:25 +02001714 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001715}
1716
1717module_init(snd_soc_init);
1718module_exit(snd_soc_exit);
1719
1720/* Module information */
1721MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1722MODULE_DESCRIPTION("ALSA SoC Core");
1723MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02001724MODULE_ALIAS("platform:soc-audio");