blob: c96a6184d66eccfcf1bc31bd650c92db8bee2b7b [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
642 /* mute any active DAC's */
Mark Brown3ff3f642008-05-19 12:32:25 +0200643 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200644 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100645 if (dai->dai_ops.digital_mute && dai->playback.active)
646 dai->dai_ops.digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200647 }
648
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100649 /* suspend all pcms */
650 for (i = 0; i < machine->num_links; i++)
651 snd_pcm_suspend_all(machine->dai_link[i].pcm);
652
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200653 if (machine->suspend_pre)
654 machine->suspend_pre(pdev, state);
655
Mark Brown3ff3f642008-05-19 12:32:25 +0200656 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200657 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
658 if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
659 cpu_dai->suspend(pdev, cpu_dai);
660 if (platform->suspend)
661 platform->suspend(pdev, cpu_dai);
662 }
663
664 /* close any waiting streams and save state */
Liam Girdwood965ac422007-01-31 14:14:57 +0100665 run_delayed_work(&socdev->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200666 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200667
Mark Brown3ff3f642008-05-19 12:32:25 +0200668 for (i = 0; i < codec->num_dai; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200669 char *stream = codec->dai[i].playback.stream_name;
670 if (stream != NULL)
671 snd_soc_dapm_stream_event(codec, stream,
672 SND_SOC_DAPM_STREAM_SUSPEND);
673 stream = codec->dai[i].capture.stream_name;
674 if (stream != NULL)
675 snd_soc_dapm_stream_event(codec, stream,
676 SND_SOC_DAPM_STREAM_SUSPEND);
677 }
678
679 if (codec_dev->suspend)
680 codec_dev->suspend(pdev, state);
681
Mark Brown3ff3f642008-05-19 12:32:25 +0200682 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200683 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
684 if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
685 cpu_dai->suspend(pdev, cpu_dai);
686 }
687
688 if (machine->suspend_post)
689 machine->suspend_post(pdev, state);
690
691 return 0;
692}
693
694/* powers up audio subsystem after a suspend */
695static int soc_resume(struct platform_device *pdev)
696{
Mark Brown3ff3f642008-05-19 12:32:25 +0200697 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
698 struct snd_soc_machine *machine = socdev->machine;
699 struct snd_soc_platform *platform = socdev->platform;
700 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200701 struct snd_soc_codec *codec = socdev->codec;
702 int i;
703
704 if (machine->resume_pre)
705 machine->resume_pre(pdev);
706
Mark Brown3ff3f642008-05-19 12:32:25 +0200707 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200708 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
709 if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
710 cpu_dai->resume(pdev, cpu_dai);
711 }
712
713 if (codec_dev->resume)
714 codec_dev->resume(pdev);
715
Mark Brown3ff3f642008-05-19 12:32:25 +0200716 for (i = 0; i < codec->num_dai; i++) {
717 char *stream = codec->dai[i].playback.stream_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200718 if (stream != NULL)
719 snd_soc_dapm_stream_event(codec, stream,
720 SND_SOC_DAPM_STREAM_RESUME);
721 stream = codec->dai[i].capture.stream_name;
722 if (stream != NULL)
723 snd_soc_dapm_stream_event(codec, stream,
724 SND_SOC_DAPM_STREAM_RESUME);
725 }
726
Mark Brown3ff3f642008-05-19 12:32:25 +0200727 /* unmute any active DACs */
728 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200729 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100730 if (dai->dai_ops.digital_mute && dai->playback.active)
731 dai->dai_ops.digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200732 }
733
Mark Brown3ff3f642008-05-19 12:32:25 +0200734 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200735 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
736 if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
737 cpu_dai->resume(pdev, cpu_dai);
738 if (platform->resume)
739 platform->resume(pdev, cpu_dai);
740 }
741
742 if (machine->resume_post)
743 machine->resume_post(pdev);
744
745 return 0;
746}
747
748#else
749#define soc_suspend NULL
750#define soc_resume NULL
751#endif
752
753/* probes a new socdev */
754static int soc_probe(struct platform_device *pdev)
755{
756 int ret = 0, i;
757 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
758 struct snd_soc_machine *machine = socdev->machine;
759 struct snd_soc_platform *platform = socdev->platform;
760 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
761
762 if (machine->probe) {
763 ret = machine->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200764 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200765 return ret;
766 }
767
768 for (i = 0; i < machine->num_links; i++) {
769 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
770 if (cpu_dai->probe) {
Mark Brownbdb92872008-06-11 13:47:10 +0100771 ret = cpu_dai->probe(pdev, cpu_dai);
Mark Brown3ff3f642008-05-19 12:32:25 +0200772 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200773 goto cpu_dai_err;
774 }
775 }
776
777 if (codec_dev->probe) {
778 ret = codec_dev->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200779 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200780 goto cpu_dai_err;
781 }
782
783 if (platform->probe) {
784 ret = platform->probe(pdev);
Mark Brown3ff3f642008-05-19 12:32:25 +0200785 if (ret < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200786 goto platform_err;
787 }
788
789 /* DAPM stream work */
Andrew Morton4484bb22006-12-15 09:30:07 +0100790 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791 return 0;
792
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200793platform_err:
794 if (codec_dev->remove)
795 codec_dev->remove(pdev);
796
797cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +0100798 for (i--; i >= 0; i--) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200799 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
800 if (cpu_dai->remove)
Mark Brownbdb92872008-06-11 13:47:10 +0100801 cpu_dai->remove(pdev, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200802 }
803
804 if (machine->remove)
805 machine->remove(pdev);
806
807 return ret;
808}
809
810/* removes a socdev */
811static int soc_remove(struct platform_device *pdev)
812{
813 int i;
814 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
815 struct snd_soc_machine *machine = socdev->machine;
816 struct snd_soc_platform *platform = socdev->platform;
817 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
818
Liam Girdwood965ac422007-01-31 14:14:57 +0100819 run_delayed_work(&socdev->delayed_work);
820
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200821 if (platform->remove)
822 platform->remove(pdev);
823
824 if (codec_dev->remove)
825 codec_dev->remove(pdev);
826
827 for (i = 0; i < machine->num_links; i++) {
828 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
829 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
833 if (machine->remove)
834 machine->remove(pdev);
835
836 return 0;
837}
838
839/* ASoC platform driver */
840static struct platform_driver soc_driver = {
841 .driver = {
842 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +0200843 .owner = THIS_MODULE,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200844 },
845 .probe = soc_probe,
846 .remove = soc_remove,
847 .suspend = soc_suspend,
848 .resume = soc_resume,
849};
850
851/* create a new pcm */
852static int soc_new_pcm(struct snd_soc_device *socdev,
853 struct snd_soc_dai_link *dai_link, int num)
854{
855 struct snd_soc_codec *codec = socdev->codec;
856 struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
857 struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
858 struct snd_soc_pcm_runtime *rtd;
859 struct snd_pcm *pcm;
860 char new_name[64];
861 int ret = 0, playback = 0, capture = 0;
862
863 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
864 if (rtd == NULL)
865 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100866
867 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200868 rtd->socdev = socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100869 codec_dai->codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200870
871 /* check client and interface hw capabilities */
Mark Brown3ff3f642008-05-19 12:32:25 +0200872 sprintf(new_name, "%s %s-%s-%d", dai_link->stream_name, codec_dai->name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200873 get_dai_name(cpu_dai->type), num);
874
875 if (codec_dai->playback.channels_min)
876 playback = 1;
877 if (codec_dai->capture.channels_min)
878 capture = 1;
879
880 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
881 capture, &pcm);
882 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200883 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
884 codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200885 kfree(rtd);
886 return ret;
887 }
888
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100889 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200890 pcm->private_data = rtd;
891 soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
892 soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
893 soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
894 soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
895 soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
896 soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
897 soc_pcm_ops.page = socdev->platform->pcm_ops->page;
898
899 if (playback)
900 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
901
902 if (capture)
903 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
904
905 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
906 if (ret < 0) {
907 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
908 kfree(rtd);
909 return ret;
910 }
911
912 pcm->private_free = socdev->platform->pcm_free;
913 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
914 cpu_dai->name);
915 return ret;
916}
917
918/* codec register dump */
919static ssize_t codec_reg_show(struct device *dev,
920 struct device_attribute *attr, char *buf)
921{
922 struct snd_soc_device *devdata = dev_get_drvdata(dev);
923 struct snd_soc_codec *codec = devdata->codec;
924 int i, step = 1, count = 0;
925
926 if (!codec->reg_cache_size)
927 return 0;
928
929 if (codec->reg_cache_step)
930 step = codec->reg_cache_step;
931
932 count += sprintf(buf, "%s registers\n", codec->name);
Mark Brown3ff3f642008-05-19 12:32:25 +0200933 for (i = 0; i < codec->reg_cache_size; i += step)
934 count += sprintf(buf + count, "%2x: %4x\n", i,
935 codec->read(codec, i));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200936
937 return count;
938}
939static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
940
941/**
942 * snd_soc_new_ac97_codec - initailise AC97 device
943 * @codec: audio codec
944 * @ops: AC97 bus operations
945 * @num: AC97 codec number
946 *
947 * Initialises AC97 codec resources for use by ad-hoc devices only.
948 */
949int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
950 struct snd_ac97_bus_ops *ops, int num)
951{
952 mutex_lock(&codec->mutex);
953
954 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
955 if (codec->ac97 == NULL) {
956 mutex_unlock(&codec->mutex);
957 return -ENOMEM;
958 }
959
960 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
961 if (codec->ac97->bus == NULL) {
962 kfree(codec->ac97);
963 codec->ac97 = NULL;
964 mutex_unlock(&codec->mutex);
965 return -ENOMEM;
966 }
967
968 codec->ac97->bus->ops = ops;
969 codec->ac97->num = num;
970 mutex_unlock(&codec->mutex);
971 return 0;
972}
973EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
974
975/**
976 * snd_soc_free_ac97_codec - free AC97 codec device
977 * @codec: audio codec
978 *
979 * Frees AC97 codec device resources.
980 */
981void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
982{
983 mutex_lock(&codec->mutex);
984 kfree(codec->ac97->bus);
985 kfree(codec->ac97);
986 codec->ac97 = NULL;
987 mutex_unlock(&codec->mutex);
988}
989EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
990
991/**
992 * snd_soc_update_bits - update codec register bits
993 * @codec: audio codec
994 * @reg: codec register
995 * @mask: register mask
996 * @value: new value
997 *
998 * Writes new register value.
999 *
1000 * Returns 1 for change else 0.
1001 */
1002int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1003 unsigned short mask, unsigned short value)
1004{
1005 int change;
1006 unsigned short old, new;
1007
1008 mutex_lock(&io_mutex);
1009 old = snd_soc_read(codec, reg);
1010 new = (old & ~mask) | value;
1011 change = old != new;
1012 if (change)
1013 snd_soc_write(codec, reg, new);
1014
1015 mutex_unlock(&io_mutex);
1016 return change;
1017}
1018EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1019
1020/**
1021 * snd_soc_test_bits - test register for change
1022 * @codec: audio codec
1023 * @reg: codec register
1024 * @mask: register mask
1025 * @value: new value
1026 *
1027 * Tests a register with a new value and checks if the new value is
1028 * different from the old value.
1029 *
1030 * Returns 1 for change else 0.
1031 */
1032int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1033 unsigned short mask, unsigned short value)
1034{
1035 int change;
1036 unsigned short old, new;
1037
1038 mutex_lock(&io_mutex);
1039 old = snd_soc_read(codec, reg);
1040 new = (old & ~mask) | value;
1041 change = old != new;
1042 mutex_unlock(&io_mutex);
1043
1044 return change;
1045}
1046EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1047
1048/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001049 * snd_soc_new_pcms - create new sound card and pcms
1050 * @socdev: the SoC audio device
1051 *
1052 * Create a new sound card based upon the codec and interface pcms.
1053 *
1054 * Returns 0 for success, else error.
1055 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001056int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001057{
1058 struct snd_soc_codec *codec = socdev->codec;
1059 struct snd_soc_machine *machine = socdev->machine;
1060 int ret = 0, i;
1061
1062 mutex_lock(&codec->mutex);
1063
1064 /* register a sound card */
1065 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1066 if (!codec->card) {
1067 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1068 codec->name);
1069 mutex_unlock(&codec->mutex);
1070 return -ENODEV;
1071 }
1072
1073 codec->card->dev = socdev->dev;
1074 codec->card->private_data = codec;
1075 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1076
1077 /* create the pcms */
Mark Brown3ff3f642008-05-19 12:32:25 +02001078 for (i = 0; i < machine->num_links; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001079 ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
1080 if (ret < 0) {
1081 printk(KERN_ERR "asoc: can't create pcm %s\n",
1082 machine->dai_link[i].stream_name);
1083 mutex_unlock(&codec->mutex);
1084 return ret;
1085 }
1086 }
1087
1088 mutex_unlock(&codec->mutex);
1089 return ret;
1090}
1091EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1092
1093/**
1094 * snd_soc_register_card - register sound card
1095 * @socdev: the SoC audio device
1096 *
1097 * Register a SoC sound card. Also registers an AC97 device if the
1098 * codec is AC97 for ad hoc devices.
1099 *
1100 * Returns 0 for success, else error.
1101 */
1102int snd_soc_register_card(struct snd_soc_device *socdev)
1103{
1104 struct snd_soc_codec *codec = socdev->codec;
1105 struct snd_soc_machine *machine = socdev->machine;
Liam Girdwood12e74f72006-10-16 21:19:48 +02001106 int ret = 0, i, ac97 = 0, err = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001107
Mark Brown3ff3f642008-05-19 12:32:25 +02001108 for (i = 0; i < machine->num_links; i++) {
Liam Girdwood12e74f72006-10-16 21:19:48 +02001109 if (socdev->machine->dai_link[i].init) {
1110 err = socdev->machine->dai_link[i].init(codec);
1111 if (err < 0) {
1112 printk(KERN_ERR "asoc: failed to init %s\n",
1113 socdev->machine->dai_link[i].stream_name);
1114 continue;
1115 }
1116 }
Mark Brown3ff3f642008-05-19 12:32:25 +02001117 if (socdev->machine->dai_link[i].codec_dai->type ==
Liam Girdwooda68660e2007-05-10 19:27:27 +02001118 SND_SOC_DAI_AC97_BUS)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001119 ac97 = 1;
1120 }
1121 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1122 "%s", machine->name);
1123 snprintf(codec->card->longname, sizeof(codec->card->longname),
1124 "%s (%s)", machine->name, codec->name);
1125
1126 ret = snd_card_register(codec->card);
1127 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +02001128 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001129 codec->name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001130 goto out;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001131 }
1132
Mark Brown08c8efe2008-01-21 14:33:37 +01001133 mutex_lock(&codec->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001134#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood12e74f72006-10-16 21:19:48 +02001135 if (ac97) {
1136 ret = soc_ac97_dev_register(codec);
1137 if (ret < 0) {
1138 printk(KERN_ERR "asoc: AC97 device register failed\n");
1139 snd_card_free(codec->card);
Mark Brown08c8efe2008-01-21 14:33:37 +01001140 mutex_unlock(&codec->mutex);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001141 goto out;
1142 }
1143 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001144#endif
1145
Liam Girdwood12e74f72006-10-16 21:19:48 +02001146 err = snd_soc_dapm_sys_add(socdev->dev);
1147 if (err < 0)
1148 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1149
1150 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1151 if (err < 0)
Mark Brown3ff3f642008-05-19 12:32:25 +02001152 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
Mark Brown08c8efe2008-01-21 14:33:37 +01001153
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001154 mutex_unlock(&codec->mutex);
Mark Brown08c8efe2008-01-21 14:33:37 +01001155
1156out:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001157 return ret;
1158}
1159EXPORT_SYMBOL_GPL(snd_soc_register_card);
1160
1161/**
1162 * snd_soc_free_pcms - free sound card and pcms
1163 * @socdev: the SoC audio device
1164 *
1165 * Frees sound card and pcms associated with the socdev.
1166 * Also unregister the codec if it is an AC97 device.
1167 */
1168void snd_soc_free_pcms(struct snd_soc_device *socdev)
1169{
1170 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001171#ifdef CONFIG_SND_SOC_AC97_BUS
1172 struct snd_soc_codec_dai *codec_dai;
1173 int i;
1174#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001175
1176 mutex_lock(&codec->mutex);
1177#ifdef CONFIG_SND_SOC_AC97_BUS
Mark Brown3ff3f642008-05-19 12:32:25 +02001178 for (i = 0; i < codec->num_dai; i++) {
Liam Girdwooda68660e2007-05-10 19:27:27 +02001179 codec_dai = &codec->dai[i];
1180 if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
1181 soc_ac97_dev_unregister(codec);
1182 goto free_card;
1183 }
1184 }
1185free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001186#endif
1187
1188 if (codec->card)
1189 snd_card_free(codec->card);
1190 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1191 mutex_unlock(&codec->mutex);
1192}
1193EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1194
1195/**
1196 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1197 * @substream: the pcm substream
1198 * @hw: the hardware parameters
1199 *
1200 * Sets the substream runtime hardware parameters.
1201 */
1202int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1203 const struct snd_pcm_hardware *hw)
1204{
1205 struct snd_pcm_runtime *runtime = substream->runtime;
1206 runtime->hw.info = hw->info;
1207 runtime->hw.formats = hw->formats;
1208 runtime->hw.period_bytes_min = hw->period_bytes_min;
1209 runtime->hw.period_bytes_max = hw->period_bytes_max;
1210 runtime->hw.periods_min = hw->periods_min;
1211 runtime->hw.periods_max = hw->periods_max;
1212 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1213 runtime->hw.fifo_size = hw->fifo_size;
1214 return 0;
1215}
1216EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1217
1218/**
1219 * snd_soc_cnew - create new control
1220 * @_template: control template
1221 * @data: control private data
1222 * @lnng_name: control long name
1223 *
1224 * Create a new mixer control from a template control.
1225 *
1226 * Returns 0 for success, else error.
1227 */
1228struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1229 void *data, char *long_name)
1230{
1231 struct snd_kcontrol_new template;
1232
1233 memcpy(&template, _template, sizeof(template));
1234 if (long_name)
1235 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001236 template.index = 0;
1237
1238 return snd_ctl_new1(&template, data);
1239}
1240EXPORT_SYMBOL_GPL(snd_soc_cnew);
1241
1242/**
1243 * snd_soc_info_enum_double - enumerated double mixer info callback
1244 * @kcontrol: mixer control
1245 * @uinfo: control element information
1246 *
1247 * Callback to provide information about a double enumerated
1248 * mixer control.
1249 *
1250 * Returns 0 for success.
1251 */
1252int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1253 struct snd_ctl_elem_info *uinfo)
1254{
1255 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1256
1257 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1258 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1259 uinfo->value.enumerated.items = e->mask;
1260
1261 if (uinfo->value.enumerated.item > e->mask - 1)
1262 uinfo->value.enumerated.item = e->mask - 1;
1263 strcpy(uinfo->value.enumerated.name,
1264 e->texts[uinfo->value.enumerated.item]);
1265 return 0;
1266}
1267EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1268
1269/**
1270 * snd_soc_get_enum_double - enumerated double mixer get callback
1271 * @kcontrol: mixer control
1272 * @uinfo: control element information
1273 *
1274 * Callback to get the value of a double enumerated mixer.
1275 *
1276 * Returns 0 for success.
1277 */
1278int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1279 struct snd_ctl_elem_value *ucontrol)
1280{
1281 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1282 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1283 unsigned short val, bitmask;
1284
1285 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1286 ;
1287 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02001288 ucontrol->value.enumerated.item[0]
1289 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001290 if (e->shift_l != e->shift_r)
1291 ucontrol->value.enumerated.item[1] =
1292 (val >> e->shift_r) & (bitmask - 1);
1293
1294 return 0;
1295}
1296EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1297
1298/**
1299 * snd_soc_put_enum_double - enumerated double mixer put callback
1300 * @kcontrol: mixer control
1301 * @uinfo: control element information
1302 *
1303 * Callback to set the value of a double enumerated mixer.
1304 *
1305 * Returns 0 for success.
1306 */
1307int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1308 struct snd_ctl_elem_value *ucontrol)
1309{
1310 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1311 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1312 unsigned short val;
1313 unsigned short mask, bitmask;
1314
1315 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1316 ;
1317 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1318 return -EINVAL;
1319 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1320 mask = (bitmask - 1) << e->shift_l;
1321 if (e->shift_l != e->shift_r) {
1322 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
1323 return -EINVAL;
1324 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1325 mask |= (bitmask - 1) << e->shift_r;
1326 }
1327
1328 return snd_soc_update_bits(codec, e->reg, mask, val);
1329}
1330EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1331
1332/**
1333 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1334 * @kcontrol: mixer control
1335 * @uinfo: control element information
1336 *
1337 * Callback to provide information about an external enumerated
1338 * single mixer.
1339 *
1340 * Returns 0 for success.
1341 */
1342int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1343 struct snd_ctl_elem_info *uinfo)
1344{
1345 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1346
1347 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1348 uinfo->count = 1;
1349 uinfo->value.enumerated.items = e->mask;
1350
1351 if (uinfo->value.enumerated.item > e->mask - 1)
1352 uinfo->value.enumerated.item = e->mask - 1;
1353 strcpy(uinfo->value.enumerated.name,
1354 e->texts[uinfo->value.enumerated.item]);
1355 return 0;
1356}
1357EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1358
1359/**
1360 * snd_soc_info_volsw_ext - external single mixer info callback
1361 * @kcontrol: mixer control
1362 * @uinfo: control element information
1363 *
1364 * Callback to provide information about a single external mixer control.
1365 *
1366 * Returns 0 for success.
1367 */
1368int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1369 struct snd_ctl_elem_info *uinfo)
1370{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001371 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001372
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001373 if (max == 1)
1374 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1375 else
1376 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1377
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001378 uinfo->count = 1;
1379 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001380 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001381 return 0;
1382}
1383EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1384
1385/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001386 * snd_soc_info_volsw - single mixer info callback
1387 * @kcontrol: mixer control
1388 * @uinfo: control element information
1389 *
1390 * Callback to provide information about a single mixer control.
1391 *
1392 * Returns 0 for success.
1393 */
1394int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1395 struct snd_ctl_elem_info *uinfo)
1396{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001397 int max = (kcontrol->private_value >> 16) & 0xff;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001398 int shift = (kcontrol->private_value >> 8) & 0x0f;
1399 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1400
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001401 if (max == 1)
1402 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1403 else
1404 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1405
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001406 uinfo->count = shift == rshift ? 1 : 2;
1407 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001408 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001409 return 0;
1410}
1411EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1412
1413/**
1414 * snd_soc_get_volsw - single mixer get callback
1415 * @kcontrol: mixer control
1416 * @uinfo: control element information
1417 *
1418 * Callback to get the value of a single mixer control.
1419 *
1420 * Returns 0 for success.
1421 */
1422int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1423 struct snd_ctl_elem_value *ucontrol)
1424{
1425 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1426 int reg = kcontrol->private_value & 0xff;
1427 int shift = (kcontrol->private_value >> 8) & 0x0f;
1428 int rshift = (kcontrol->private_value >> 12) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001429 int max = (kcontrol->private_value >> 16) & 0xff;
1430 int mask = (1 << fls(max)) - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001431 int invert = (kcontrol->private_value >> 24) & 0x01;
1432
1433 ucontrol->value.integer.value[0] =
1434 (snd_soc_read(codec, reg) >> shift) & mask;
1435 if (shift != rshift)
1436 ucontrol->value.integer.value[1] =
1437 (snd_soc_read(codec, reg) >> rshift) & mask;
1438 if (invert) {
1439 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001440 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001441 if (shift != rshift)
1442 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001443 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001444 }
1445
1446 return 0;
1447}
1448EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1449
1450/**
1451 * snd_soc_put_volsw - single mixer put callback
1452 * @kcontrol: mixer control
1453 * @uinfo: control element information
1454 *
1455 * Callback to set the value of a single mixer control.
1456 *
1457 * Returns 0 for success.
1458 */
1459int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1460 struct snd_ctl_elem_value *ucontrol)
1461{
1462 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1463 int reg = kcontrol->private_value & 0xff;
1464 int shift = (kcontrol->private_value >> 8) & 0x0f;
1465 int rshift = (kcontrol->private_value >> 12) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001466 int max = (kcontrol->private_value >> 16) & 0xff;
1467 int mask = (1 << fls(max)) - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001468 int invert = (kcontrol->private_value >> 24) & 0x01;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001469 unsigned short val, val2, val_mask;
1470
1471 val = (ucontrol->value.integer.value[0] & mask);
1472 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001473 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001474 val_mask = mask << shift;
1475 val = val << shift;
1476 if (shift != rshift) {
1477 val2 = (ucontrol->value.integer.value[1] & mask);
1478 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001479 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001480 val_mask |= mask << rshift;
1481 val |= val2 << rshift;
1482 }
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001483 return snd_soc_update_bits(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001484}
1485EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1486
1487/**
1488 * snd_soc_info_volsw_2r - double mixer info callback
1489 * @kcontrol: mixer control
1490 * @uinfo: control element information
1491 *
1492 * Callback to provide information about a double mixer control that
1493 * spans 2 codec registers.
1494 *
1495 * Returns 0 for success.
1496 */
1497int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1498 struct snd_ctl_elem_info *uinfo)
1499{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001500 int max = (kcontrol->private_value >> 12) & 0xff;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001501
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001502 if (max == 1)
1503 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1504 else
1505 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1506
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001507 uinfo->count = 2;
1508 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001509 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001510 return 0;
1511}
1512EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1513
1514/**
1515 * snd_soc_get_volsw_2r - double mixer get callback
1516 * @kcontrol: mixer control
1517 * @uinfo: control element information
1518 *
1519 * Callback to get the value of a double mixer control that spans 2 registers.
1520 *
1521 * Returns 0 for success.
1522 */
1523int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1524 struct snd_ctl_elem_value *ucontrol)
1525{
1526 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1527 int reg = kcontrol->private_value & 0xff;
1528 int reg2 = (kcontrol->private_value >> 24) & 0xff;
1529 int shift = (kcontrol->private_value >> 8) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001530 int max = (kcontrol->private_value >> 12) & 0xff;
1531 int mask = (1<<fls(max))-1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001532 int invert = (kcontrol->private_value >> 20) & 0x01;
1533
1534 ucontrol->value.integer.value[0] =
1535 (snd_soc_read(codec, reg) >> shift) & mask;
1536 ucontrol->value.integer.value[1] =
1537 (snd_soc_read(codec, reg2) >> shift) & mask;
1538 if (invert) {
1539 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001540 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001541 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001542 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001543 }
1544
1545 return 0;
1546}
1547EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1548
1549/**
1550 * snd_soc_put_volsw_2r - double mixer set callback
1551 * @kcontrol: mixer control
1552 * @uinfo: control element information
1553 *
1554 * Callback to set the value of a double mixer control that spans 2 registers.
1555 *
1556 * Returns 0 for success.
1557 */
1558int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1559 struct snd_ctl_elem_value *ucontrol)
1560{
1561 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1562 int reg = kcontrol->private_value & 0xff;
1563 int reg2 = (kcontrol->private_value >> 24) & 0xff;
1564 int shift = (kcontrol->private_value >> 8) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001565 int max = (kcontrol->private_value >> 12) & 0xff;
1566 int mask = (1 << fls(max)) - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001567 int invert = (kcontrol->private_value >> 20) & 0x01;
1568 int err;
1569 unsigned short val, val2, val_mask;
1570
1571 val_mask = mask << shift;
1572 val = (ucontrol->value.integer.value[0] & mask);
1573 val2 = (ucontrol->value.integer.value[1] & mask);
1574
1575 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001576 val = max - val;
1577 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001578 }
1579
1580 val = val << shift;
1581 val2 = val2 << shift;
1582
Mark Brown3ff3f642008-05-19 12:32:25 +02001583 err = snd_soc_update_bits(codec, reg, val_mask, val);
1584 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001585 return err;
1586
1587 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1588 return err;
1589}
1590EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1591
Mark Browne13ac2e2008-05-28 17:58:05 +01001592/**
1593 * snd_soc_info_volsw_s8 - signed mixer info callback
1594 * @kcontrol: mixer control
1595 * @uinfo: control element information
1596 *
1597 * Callback to provide information about a signed mixer control.
1598 *
1599 * Returns 0 for success.
1600 */
1601int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1602 struct snd_ctl_elem_info *uinfo)
1603{
1604 int max = (signed char)((kcontrol->private_value >> 16) & 0xff);
1605 int min = (signed char)((kcontrol->private_value >> 24) & 0xff);
1606
1607 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1608 uinfo->count = 2;
1609 uinfo->value.integer.min = 0;
1610 uinfo->value.integer.max = max-min;
1611 return 0;
1612}
1613EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1614
1615/**
1616 * snd_soc_get_volsw_s8 - signed mixer get callback
1617 * @kcontrol: mixer control
1618 * @uinfo: control element information
1619 *
1620 * Callback to get the value of a signed mixer control.
1621 *
1622 * Returns 0 for success.
1623 */
1624int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1625 struct snd_ctl_elem_value *ucontrol)
1626{
1627 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1628 int reg = kcontrol->private_value & 0xff;
1629 int min = (signed char)((kcontrol->private_value >> 24) & 0xff);
1630 int val = snd_soc_read(codec, reg);
1631
1632 ucontrol->value.integer.value[0] =
1633 ((signed char)(val & 0xff))-min;
1634 ucontrol->value.integer.value[1] =
1635 ((signed char)((val >> 8) & 0xff))-min;
1636 return 0;
1637}
1638EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1639
1640/**
1641 * snd_soc_put_volsw_sgn - signed mixer put callback
1642 * @kcontrol: mixer control
1643 * @uinfo: control element information
1644 *
1645 * Callback to set the value of a signed mixer control.
1646 *
1647 * Returns 0 for success.
1648 */
1649int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1650 struct snd_ctl_elem_value *ucontrol)
1651{
1652 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1653 int reg = kcontrol->private_value & 0xff;
1654 int min = (signed char)((kcontrol->private_value >> 24) & 0xff);
1655 unsigned short val;
1656
1657 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1658 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1659
1660 return snd_soc_update_bits(codec, reg, 0xffff, val);
1661}
1662EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1663
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001664static int __devinit snd_soc_init(void)
1665{
1666 printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
1667 return platform_driver_register(&soc_driver);
1668}
1669
1670static void snd_soc_exit(void)
1671{
Mark Brown3ff3f642008-05-19 12:32:25 +02001672 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001673}
1674
1675module_init(snd_soc_init);
1676module_exit(snd_soc_exit);
1677
1678/* Module information */
1679MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1680MODULE_DESCRIPTION("ALSA SoC Core");
1681MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02001682MODULE_ALIAS("platform:soc-audio");