blob: e148db940cfc77ecb4521df7191909f00e672761 [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 *
17 * Revision history
18 * 12th Aug 2005 Initial version.
19 * 25th Oct 2005 Working Codec, Interface and Platform registration.
20 *
21 * TODO:
22 * o Add hw rules to enforce rates, etc.
23 * o More testing with other codecs/machines.
24 * o Add more codecs and platforms to ensure good API coverage.
25 * o Support TDM on PCM and I2S
26 */
27
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/init.h>
31#include <linux/delay.h>
32#include <linux/pm.h>
33#include <linux/bitops.h>
34#include <linux/platform_device.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020035#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
39#include <sound/soc-dapm.h>
40#include <sound/initval.h>
41
42/* debug */
43#define SOC_DEBUG 0
44#if SOC_DEBUG
45#define dbg(format, arg...) printk(format, ## arg)
46#else
47#define dbg(format, arg...)
48#endif
Liam Girdwooda71a4682006-10-19 20:35:56 +020049
Frank Mandarinodb2a4162006-10-06 18:31:09 +020050static DEFINE_MUTEX(pcm_mutex);
51static DEFINE_MUTEX(io_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020052static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
53
Frank Mandarinodb2a4162006-10-06 18:31:09 +020054/*
55 * This is a timeout to do a DAPM powerdown after a stream is closed().
56 * It can be used to eliminate pops between different playback streams, e.g.
57 * between two audio tracks.
58 */
59static int pmdown_time = 5000;
60module_param(pmdown_time, int, 0);
61MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
62
Liam Girdwood965ac422007-01-31 14:14:57 +010063/*
64 * This function forces any delayed work to be queued and run.
65 */
66static int run_delayed_work(struct delayed_work *dwork)
67{
68 int ret;
69
70 /* cancel any work waiting to be queued. */
71 ret = cancel_delayed_work(dwork);
72
73 /* if there was any work waiting then we run it now and
74 * wait for it's completion */
75 if (ret) {
76 schedule_delayed_work(dwork, 0);
77 flush_scheduled_work();
78 }
79 return ret;
80}
81
Frank Mandarinodb2a4162006-10-06 18:31:09 +020082#ifdef CONFIG_SND_SOC_AC97_BUS
83/* unregister ac97 codec */
84static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
85{
86 if (codec->ac97->dev.bus)
87 device_unregister(&codec->ac97->dev);
88 return 0;
89}
90
91/* stop no dev release warning */
92static void soc_ac97_device_release(struct device *dev){}
93
94/* register ac97 codec to bus */
95static int soc_ac97_dev_register(struct snd_soc_codec *codec)
96{
97 int err;
98
99 codec->ac97->dev.bus = &ac97_bus_type;
100 codec->ac97->dev.parent = NULL;
101 codec->ac97->dev.release = soc_ac97_device_release;
102
103 snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
104 codec->card->number, 0, codec->name);
105 err = device_register(&codec->ac97->dev);
106 if (err < 0) {
107 snd_printk(KERN_ERR "Can't register ac97 bus\n");
108 codec->ac97->dev.bus = NULL;
109 return err;
110 }
111 return 0;
112}
113#endif
114
115static inline const char* get_dai_name(int type)
116{
117 switch(type) {
Liam Girdwooda68660e2007-05-10 19:27:27 +0200118 case SND_SOC_DAI_AC97_BUS:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200119 case SND_SOC_DAI_AC97:
120 return "AC97";
121 case SND_SOC_DAI_I2S:
122 return "I2S";
123 case SND_SOC_DAI_PCM:
124 return "PCM";
125 }
126 return NULL;
127}
128
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200129/*
130 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
131 * then initialized and any private data can be allocated. This also calls
132 * startup for the cpu DAI, platform, machine and codec DAI.
133 */
134static int soc_pcm_open(struct snd_pcm_substream *substream)
135{
136 struct snd_soc_pcm_runtime *rtd = substream->private_data;
137 struct snd_soc_device *socdev = rtd->socdev;
138 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100139 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200140 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100141 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
142 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200143 int ret = 0;
144
145 mutex_lock(&pcm_mutex);
146
147 /* startup the audio subsystem */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100148 if (cpu_dai->ops.startup) {
149 ret = cpu_dai->ops.startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200150 if (ret < 0) {
151 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100152 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200153 goto out;
154 }
155 }
156
157 if (platform->pcm_ops->open) {
158 ret = platform->pcm_ops->open(substream);
159 if (ret < 0) {
160 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
161 goto platform_err;
162 }
163 }
164
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100165 if (codec_dai->ops.startup) {
166 ret = codec_dai->ops.startup(substream);
167 if (ret < 0) {
168 printk(KERN_ERR "asoc: can't open codec %s\n",
169 codec_dai->name);
170 goto codec_dai_err;
171 }
172 }
173
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200174 if (machine->ops && machine->ops->startup) {
175 ret = machine->ops->startup(substream);
176 if (ret < 0) {
177 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
178 goto machine_err;
179 }
180 }
181
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200182 /* Check that the codec and cpu DAI's are compatible */
183 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
184 runtime->hw.rate_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100185 max(codec_dai->playback.rate_min, cpu_dai->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200186 runtime->hw.rate_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100187 min(codec_dai->playback.rate_max, cpu_dai->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200188 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100189 max(codec_dai->playback.channels_min,
190 cpu_dai->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200191 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100192 min(codec_dai->playback.channels_max,
193 cpu_dai->playback.channels_max);
194 runtime->hw.formats =
195 codec_dai->playback.formats & cpu_dai->playback.formats;
196 runtime->hw.rates =
197 codec_dai->playback.rates & cpu_dai->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200198 } else {
199 runtime->hw.rate_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100200 max(codec_dai->capture.rate_min, cpu_dai->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200201 runtime->hw.rate_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100202 min(codec_dai->capture.rate_max, 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
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100232 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);
279 for(i = 0; i < codec->num_dai; i++) {
280 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
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100290 /* power down the codec to D1 if no longer active */
291 if (codec->active == 0) {
292 dbg("pop wq D1 %s %s\n", codec->name,
293 codec_dai->playback.stream_name);
294 snd_soc_dapm_device_event(socdev,
295 SNDRV_CTL_POWER_D1);
296 }
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
303 /* power down the codec power domain if no longer active */
304 if (codec->active == 0) {
305 dbg("pop wq D3 %s %s\n", codec->name,
306 codec_dai->playback.stream_name);
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100307 snd_soc_dapm_device_event(socdev,
308 SNDRV_CTL_POWER_D3hot);
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)
368 snd_soc_dapm_device_event(socdev,
369 SNDRV_CTL_POWER_D3hot);
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 */
442 if (codec->dapm_state != SNDRV_CTL_POWER_D0) {
443
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100444 snd_soc_dapm_device_event(socdev, SNDRV_CTL_POWER_D1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200445
446 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
447 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100448 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200449 SND_SOC_DAPM_STREAM_START);
450 else
451 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100452 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200453 SND_SOC_DAPM_STREAM_START);
454
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100455 snd_soc_dapm_device_event(socdev, SNDRV_CTL_POWER_D0);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100456 if (codec_dai->dai_ops.digital_mute)
457 codec_dai->dai_ops.digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200458
459 } else {
460 /* codec already powered - power on widgets */
461 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
462 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100463 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200464 SND_SOC_DAPM_STREAM_START);
465 else
466 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100467 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200468 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100469 if (codec_dai->dai_ops.digital_mute)
470 codec_dai->dai_ops.digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200471 }
472 }
473
474out:
475 mutex_unlock(&pcm_mutex);
476 return ret;
477}
478
479/*
480 * Called by ALSA when the hardware params are set by application. This
481 * function can also be called multiple times and can allocate buffers
482 * (using snd_pcm_lib_* ). It's non-atomic.
483 */
484static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
485 struct snd_pcm_hw_params *params)
486{
487 struct snd_soc_pcm_runtime *rtd = substream->private_data;
488 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100489 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200490 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100491 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
492 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200493 int ret = 0;
494
495 mutex_lock(&pcm_mutex);
496
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100497 if (machine->ops && machine->ops->hw_params) {
498 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200499 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100500 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200501 goto out;
502 }
503 }
504
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100505 if (codec_dai->ops.hw_params) {
506 ret = codec_dai->ops.hw_params(substream, params);
507 if (ret < 0) {
508 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
509 codec_dai->name);
510 goto codec_err;
511 }
512 }
513
514 if (cpu_dai->ops.hw_params) {
515 ret = cpu_dai->ops.hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200516 if (ret < 0) {
517 printk(KERN_ERR "asoc: can't set interface %s hw params\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100518 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200519 goto interface_err;
520 }
521 }
522
523 if (platform->pcm_ops->hw_params) {
524 ret = platform->pcm_ops->hw_params(substream, params);
525 if (ret < 0) {
526 printk(KERN_ERR "asoc: can't set platform %s hw params\n",
527 platform->name);
528 goto platform_err;
529 }
530 }
531
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200532out:
533 mutex_unlock(&pcm_mutex);
534 return ret;
535
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536platform_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100537 if (cpu_dai->ops.hw_free)
538 cpu_dai->ops.hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200539
540interface_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100541 if (codec_dai->ops.hw_free)
542 codec_dai->ops.hw_free(substream);
543
544codec_err:
545 if(machine->ops && machine->ops->hw_free)
546 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200547
548 mutex_unlock(&pcm_mutex);
549 return ret;
550}
551
552/*
553 * Free's resources allocated by hw_params, can be called multiple times
554 */
555static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
556{
557 struct snd_soc_pcm_runtime *rtd = substream->private_data;
558 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100559 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200560 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100561 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
562 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200563 struct snd_soc_codec *codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564
565 mutex_lock(&pcm_mutex);
566
567 /* apply codec digital mute */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100568 if (!codec->active && codec_dai->dai_ops.digital_mute)
569 codec_dai->dai_ops.digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200570
571 /* free any machine hw params */
572 if (machine->ops && machine->ops->hw_free)
573 machine->ops->hw_free(substream);
574
575 /* free any DMA resources */
576 if (platform->pcm_ops->hw_free)
577 platform->pcm_ops->hw_free(substream);
578
579 /* now free hw params for the DAI's */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100580 if (codec_dai->ops.hw_free)
581 codec_dai->ops.hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200582
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100583 if (cpu_dai->ops.hw_free)
584 cpu_dai->ops.hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200585
586 mutex_unlock(&pcm_mutex);
587 return 0;
588}
589
590static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
591{
592 struct snd_soc_pcm_runtime *rtd = substream->private_data;
593 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100594 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200595 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100596 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
597 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200598 int ret;
599
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100600 if (codec_dai->ops.trigger) {
601 ret = codec_dai->ops.trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200602 if (ret < 0)
603 return ret;
604 }
605
606 if (platform->pcm_ops->trigger) {
607 ret = platform->pcm_ops->trigger(substream, cmd);
608 if (ret < 0)
609 return ret;
610 }
611
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100612 if (cpu_dai->ops.trigger) {
613 ret = cpu_dai->ops.trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200614 if (ret < 0)
615 return ret;
616 }
617 return 0;
618}
619
620/* ASoC PCM operations */
621static struct snd_pcm_ops soc_pcm_ops = {
622 .open = soc_pcm_open,
623 .close = soc_codec_close,
624 .hw_params = soc_pcm_hw_params,
625 .hw_free = soc_pcm_hw_free,
626 .prepare = soc_pcm_prepare,
627 .trigger = soc_pcm_trigger,
628};
629
630#ifdef CONFIG_PM
631/* powers down audio subsystem for suspend */
632static int soc_suspend(struct platform_device *pdev, pm_message_t state)
633{
634 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
635 struct snd_soc_machine *machine = socdev->machine;
636 struct snd_soc_platform *platform = socdev->platform;
637 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
638 struct snd_soc_codec *codec = socdev->codec;
639 int i;
640
641 /* mute any active DAC's */
642 for(i = 0; i < machine->num_links; i++) {
643 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100644 if (dai->dai_ops.digital_mute && dai->playback.active)
645 dai->dai_ops.digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200646 }
647
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100648 /* suspend all pcms */
649 for (i = 0; i < machine->num_links; i++)
650 snd_pcm_suspend_all(machine->dai_link[i].pcm);
651
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200652 if (machine->suspend_pre)
653 machine->suspend_pre(pdev, state);
654
655 for(i = 0; i < machine->num_links; i++) {
656 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
657 if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
658 cpu_dai->suspend(pdev, cpu_dai);
659 if (platform->suspend)
660 platform->suspend(pdev, cpu_dai);
661 }
662
663 /* close any waiting streams and save state */
Liam Girdwood965ac422007-01-31 14:14:57 +0100664 run_delayed_work(&socdev->delayed_work);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200665 codec->suspend_dapm_state = codec->dapm_state;
666
667 for(i = 0; i < codec->num_dai; i++) {
668 char *stream = codec->dai[i].playback.stream_name;
669 if (stream != NULL)
670 snd_soc_dapm_stream_event(codec, stream,
671 SND_SOC_DAPM_STREAM_SUSPEND);
672 stream = codec->dai[i].capture.stream_name;
673 if (stream != NULL)
674 snd_soc_dapm_stream_event(codec, stream,
675 SND_SOC_DAPM_STREAM_SUSPEND);
676 }
677
678 if (codec_dev->suspend)
679 codec_dev->suspend(pdev, state);
680
681 for(i = 0; i < machine->num_links; i++) {
682 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
683 if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
684 cpu_dai->suspend(pdev, cpu_dai);
685 }
686
687 if (machine->suspend_post)
688 machine->suspend_post(pdev, state);
689
690 return 0;
691}
692
693/* powers up audio subsystem after a suspend */
694static int soc_resume(struct platform_device *pdev)
695{
696 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
697 struct snd_soc_machine *machine = socdev->machine;
698 struct snd_soc_platform *platform = socdev->platform;
699 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
700 struct snd_soc_codec *codec = socdev->codec;
701 int i;
702
703 if (machine->resume_pre)
704 machine->resume_pre(pdev);
705
706 for(i = 0; i < machine->num_links; i++) {
707 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
708 if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
709 cpu_dai->resume(pdev, cpu_dai);
710 }
711
712 if (codec_dev->resume)
713 codec_dev->resume(pdev);
714
715 for(i = 0; i < codec->num_dai; i++) {
716 char* stream = codec->dai[i].playback.stream_name;
717 if (stream != NULL)
718 snd_soc_dapm_stream_event(codec, stream,
719 SND_SOC_DAPM_STREAM_RESUME);
720 stream = codec->dai[i].capture.stream_name;
721 if (stream != NULL)
722 snd_soc_dapm_stream_event(codec, stream,
723 SND_SOC_DAPM_STREAM_RESUME);
724 }
725
726 /* unmute any active DAC's */
727 for(i = 0; i < machine->num_links; i++) {
728 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100729 if (dai->dai_ops.digital_mute && dai->playback.active)
730 dai->dai_ops.digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200731 }
732
733 for(i = 0; i < machine->num_links; i++) {
734 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
735 if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
736 cpu_dai->resume(pdev, cpu_dai);
737 if (platform->resume)
738 platform->resume(pdev, cpu_dai);
739 }
740
741 if (machine->resume_post)
742 machine->resume_post(pdev);
743
744 return 0;
745}
746
747#else
748#define soc_suspend NULL
749#define soc_resume NULL
750#endif
751
752/* probes a new socdev */
753static int soc_probe(struct platform_device *pdev)
754{
755 int ret = 0, i;
756 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
757 struct snd_soc_machine *machine = socdev->machine;
758 struct snd_soc_platform *platform = socdev->platform;
759 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
760
761 if (machine->probe) {
762 ret = machine->probe(pdev);
763 if(ret < 0)
764 return ret;
765 }
766
767 for (i = 0; i < machine->num_links; i++) {
768 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
769 if (cpu_dai->probe) {
770 ret = cpu_dai->probe(pdev);
771 if(ret < 0)
772 goto cpu_dai_err;
773 }
774 }
775
776 if (codec_dev->probe) {
777 ret = codec_dev->probe(pdev);
778 if(ret < 0)
779 goto cpu_dai_err;
780 }
781
782 if (platform->probe) {
783 ret = platform->probe(pdev);
784 if(ret < 0)
785 goto platform_err;
786 }
787
788 /* DAPM stream work */
Andrew Morton4484bb22006-12-15 09:30:07 +0100789 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790 return 0;
791
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200792platform_err:
793 if (codec_dev->remove)
794 codec_dev->remove(pdev);
795
796cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +0100797 for (i--; i >= 0; i--) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200798 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
799 if (cpu_dai->remove)
800 cpu_dai->remove(pdev);
801 }
802
803 if (machine->remove)
804 machine->remove(pdev);
805
806 return ret;
807}
808
809/* removes a socdev */
810static int soc_remove(struct platform_device *pdev)
811{
812 int i;
813 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
814 struct snd_soc_machine *machine = socdev->machine;
815 struct snd_soc_platform *platform = socdev->platform;
816 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
817
Liam Girdwood965ac422007-01-31 14:14:57 +0100818 run_delayed_work(&socdev->delayed_work);
819
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200820 if (platform->remove)
821 platform->remove(pdev);
822
823 if (codec_dev->remove)
824 codec_dev->remove(pdev);
825
826 for (i = 0; i < machine->num_links; i++) {
827 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
828 if (cpu_dai->remove)
829 cpu_dai->remove(pdev);
830 }
831
832 if (machine->remove)
833 machine->remove(pdev);
834
835 return 0;
836}
837
838/* ASoC platform driver */
839static struct platform_driver soc_driver = {
840 .driver = {
841 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +0200842 .owner = THIS_MODULE,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200843 },
844 .probe = soc_probe,
845 .remove = soc_remove,
846 .suspend = soc_suspend,
847 .resume = soc_resume,
848};
849
850/* create a new pcm */
851static int soc_new_pcm(struct snd_soc_device *socdev,
852 struct snd_soc_dai_link *dai_link, int num)
853{
854 struct snd_soc_codec *codec = socdev->codec;
855 struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
856 struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
857 struct snd_soc_pcm_runtime *rtd;
858 struct snd_pcm *pcm;
859 char new_name[64];
860 int ret = 0, playback = 0, capture = 0;
861
862 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
863 if (rtd == NULL)
864 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100865
866 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200867 rtd->socdev = socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100868 codec_dai->codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200869
870 /* check client and interface hw capabilities */
871 sprintf(new_name, "%s %s-%s-%d",dai_link->stream_name, codec_dai->name,
872 get_dai_name(cpu_dai->type), num);
873
874 if (codec_dai->playback.channels_min)
875 playback = 1;
876 if (codec_dai->capture.channels_min)
877 capture = 1;
878
879 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
880 capture, &pcm);
881 if (ret < 0) {
882 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
883 kfree(rtd);
884 return ret;
885 }
886
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100887 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200888 pcm->private_data = rtd;
889 soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
890 soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
891 soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
892 soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
893 soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
894 soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
895 soc_pcm_ops.page = socdev->platform->pcm_ops->page;
896
897 if (playback)
898 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
899
900 if (capture)
901 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
902
903 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
904 if (ret < 0) {
905 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
906 kfree(rtd);
907 return ret;
908 }
909
910 pcm->private_free = socdev->platform->pcm_free;
911 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
912 cpu_dai->name);
913 return ret;
914}
915
916/* codec register dump */
917static ssize_t codec_reg_show(struct device *dev,
918 struct device_attribute *attr, char *buf)
919{
920 struct snd_soc_device *devdata = dev_get_drvdata(dev);
921 struct snd_soc_codec *codec = devdata->codec;
922 int i, step = 1, count = 0;
923
924 if (!codec->reg_cache_size)
925 return 0;
926
927 if (codec->reg_cache_step)
928 step = codec->reg_cache_step;
929
930 count += sprintf(buf, "%s registers\n", codec->name);
931 for(i = 0; i < codec->reg_cache_size; i += step)
932 count += sprintf(buf + count, "%2x: %4x\n", i, codec->read(codec, i));
933
934 return count;
935}
936static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
937
938/**
939 * snd_soc_new_ac97_codec - initailise AC97 device
940 * @codec: audio codec
941 * @ops: AC97 bus operations
942 * @num: AC97 codec number
943 *
944 * Initialises AC97 codec resources for use by ad-hoc devices only.
945 */
946int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
947 struct snd_ac97_bus_ops *ops, int num)
948{
949 mutex_lock(&codec->mutex);
950
951 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
952 if (codec->ac97 == NULL) {
953 mutex_unlock(&codec->mutex);
954 return -ENOMEM;
955 }
956
957 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
958 if (codec->ac97->bus == NULL) {
959 kfree(codec->ac97);
960 codec->ac97 = NULL;
961 mutex_unlock(&codec->mutex);
962 return -ENOMEM;
963 }
964
965 codec->ac97->bus->ops = ops;
966 codec->ac97->num = num;
967 mutex_unlock(&codec->mutex);
968 return 0;
969}
970EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
971
972/**
973 * snd_soc_free_ac97_codec - free AC97 codec device
974 * @codec: audio codec
975 *
976 * Frees AC97 codec device resources.
977 */
978void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
979{
980 mutex_lock(&codec->mutex);
981 kfree(codec->ac97->bus);
982 kfree(codec->ac97);
983 codec->ac97 = NULL;
984 mutex_unlock(&codec->mutex);
985}
986EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
987
988/**
989 * snd_soc_update_bits - update codec register bits
990 * @codec: audio codec
991 * @reg: codec register
992 * @mask: register mask
993 * @value: new value
994 *
995 * Writes new register value.
996 *
997 * Returns 1 for change else 0.
998 */
999int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1000 unsigned short mask, unsigned short value)
1001{
1002 int change;
1003 unsigned short old, new;
1004
1005 mutex_lock(&io_mutex);
1006 old = snd_soc_read(codec, reg);
1007 new = (old & ~mask) | value;
1008 change = old != new;
1009 if (change)
1010 snd_soc_write(codec, reg, new);
1011
1012 mutex_unlock(&io_mutex);
1013 return change;
1014}
1015EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1016
1017/**
1018 * snd_soc_test_bits - test register for change
1019 * @codec: audio codec
1020 * @reg: codec register
1021 * @mask: register mask
1022 * @value: new value
1023 *
1024 * Tests a register with a new value and checks if the new value is
1025 * different from the old value.
1026 *
1027 * Returns 1 for change else 0.
1028 */
1029int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1030 unsigned short mask, unsigned short value)
1031{
1032 int change;
1033 unsigned short old, new;
1034
1035 mutex_lock(&io_mutex);
1036 old = snd_soc_read(codec, reg);
1037 new = (old & ~mask) | value;
1038 change = old != new;
1039 mutex_unlock(&io_mutex);
1040
1041 return change;
1042}
1043EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1044
1045/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001046 * snd_soc_new_pcms - create new sound card and pcms
1047 * @socdev: the SoC audio device
1048 *
1049 * Create a new sound card based upon the codec and interface pcms.
1050 *
1051 * Returns 0 for success, else error.
1052 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001053int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001054{
1055 struct snd_soc_codec *codec = socdev->codec;
1056 struct snd_soc_machine *machine = socdev->machine;
1057 int ret = 0, i;
1058
1059 mutex_lock(&codec->mutex);
1060
1061 /* register a sound card */
1062 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1063 if (!codec->card) {
1064 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1065 codec->name);
1066 mutex_unlock(&codec->mutex);
1067 return -ENODEV;
1068 }
1069
1070 codec->card->dev = socdev->dev;
1071 codec->card->private_data = codec;
1072 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1073
1074 /* create the pcms */
1075 for(i = 0; i < machine->num_links; i++) {
1076 ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
1077 if (ret < 0) {
1078 printk(KERN_ERR "asoc: can't create pcm %s\n",
1079 machine->dai_link[i].stream_name);
1080 mutex_unlock(&codec->mutex);
1081 return ret;
1082 }
1083 }
1084
1085 mutex_unlock(&codec->mutex);
1086 return ret;
1087}
1088EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1089
1090/**
1091 * snd_soc_register_card - register sound card
1092 * @socdev: the SoC audio device
1093 *
1094 * Register a SoC sound card. Also registers an AC97 device if the
1095 * codec is AC97 for ad hoc devices.
1096 *
1097 * Returns 0 for success, else error.
1098 */
1099int snd_soc_register_card(struct snd_soc_device *socdev)
1100{
1101 struct snd_soc_codec *codec = socdev->codec;
1102 struct snd_soc_machine *machine = socdev->machine;
Liam Girdwood12e74f72006-10-16 21:19:48 +02001103 int ret = 0, i, ac97 = 0, err = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001104
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001105 for(i = 0; i < machine->num_links; i++) {
Liam Girdwood12e74f72006-10-16 21:19:48 +02001106 if (socdev->machine->dai_link[i].init) {
1107 err = socdev->machine->dai_link[i].init(codec);
1108 if (err < 0) {
1109 printk(KERN_ERR "asoc: failed to init %s\n",
1110 socdev->machine->dai_link[i].stream_name);
1111 continue;
1112 }
1113 }
Liam Girdwooda68660e2007-05-10 19:27:27 +02001114 if (socdev->machine->dai_link[i].codec_dai->type ==
1115 SND_SOC_DAI_AC97_BUS)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001116 ac97 = 1;
1117 }
1118 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1119 "%s", machine->name);
1120 snprintf(codec->card->longname, sizeof(codec->card->longname),
1121 "%s (%s)", machine->name, codec->name);
1122
1123 ret = snd_card_register(codec->card);
1124 if (ret < 0) {
1125 printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n",
1126 codec->name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001127 goto out;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001128 }
1129
Mark Brown08c8efe2008-01-21 14:33:37 +01001130 mutex_lock(&codec->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001131#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood12e74f72006-10-16 21:19:48 +02001132 if (ac97) {
1133 ret = soc_ac97_dev_register(codec);
1134 if (ret < 0) {
1135 printk(KERN_ERR "asoc: AC97 device register failed\n");
1136 snd_card_free(codec->card);
Mark Brown08c8efe2008-01-21 14:33:37 +01001137 mutex_unlock(&codec->mutex);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001138 goto out;
1139 }
1140 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001141#endif
1142
Liam Girdwood12e74f72006-10-16 21:19:48 +02001143 err = snd_soc_dapm_sys_add(socdev->dev);
1144 if (err < 0)
1145 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1146
1147 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1148 if (err < 0)
1149 printk(KERN_WARNING "asoc: failed to add codec sysfs entries\n");
Mark Brown08c8efe2008-01-21 14:33:37 +01001150
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001151 mutex_unlock(&codec->mutex);
Mark Brown08c8efe2008-01-21 14:33:37 +01001152
1153out:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001154 return ret;
1155}
1156EXPORT_SYMBOL_GPL(snd_soc_register_card);
1157
1158/**
1159 * snd_soc_free_pcms - free sound card and pcms
1160 * @socdev: the SoC audio device
1161 *
1162 * Frees sound card and pcms associated with the socdev.
1163 * Also unregister the codec if it is an AC97 device.
1164 */
1165void snd_soc_free_pcms(struct snd_soc_device *socdev)
1166{
1167 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001168#ifdef CONFIG_SND_SOC_AC97_BUS
1169 struct snd_soc_codec_dai *codec_dai;
1170 int i;
1171#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001172
1173 mutex_lock(&codec->mutex);
1174#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwooda68660e2007-05-10 19:27:27 +02001175 for(i = 0; i < codec->num_dai; i++) {
1176 codec_dai = &codec->dai[i];
1177 if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
1178 soc_ac97_dev_unregister(codec);
1179 goto free_card;
1180 }
1181 }
1182free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001183#endif
1184
1185 if (codec->card)
1186 snd_card_free(codec->card);
1187 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1188 mutex_unlock(&codec->mutex);
1189}
1190EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1191
1192/**
1193 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1194 * @substream: the pcm substream
1195 * @hw: the hardware parameters
1196 *
1197 * Sets the substream runtime hardware parameters.
1198 */
1199int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1200 const struct snd_pcm_hardware *hw)
1201{
1202 struct snd_pcm_runtime *runtime = substream->runtime;
1203 runtime->hw.info = hw->info;
1204 runtime->hw.formats = hw->formats;
1205 runtime->hw.period_bytes_min = hw->period_bytes_min;
1206 runtime->hw.period_bytes_max = hw->period_bytes_max;
1207 runtime->hw.periods_min = hw->periods_min;
1208 runtime->hw.periods_max = hw->periods_max;
1209 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1210 runtime->hw.fifo_size = hw->fifo_size;
1211 return 0;
1212}
1213EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1214
1215/**
1216 * snd_soc_cnew - create new control
1217 * @_template: control template
1218 * @data: control private data
1219 * @lnng_name: control long name
1220 *
1221 * Create a new mixer control from a template control.
1222 *
1223 * Returns 0 for success, else error.
1224 */
1225struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1226 void *data, char *long_name)
1227{
1228 struct snd_kcontrol_new template;
1229
1230 memcpy(&template, _template, sizeof(template));
1231 if (long_name)
1232 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001233 template.index = 0;
1234
1235 return snd_ctl_new1(&template, data);
1236}
1237EXPORT_SYMBOL_GPL(snd_soc_cnew);
1238
1239/**
1240 * snd_soc_info_enum_double - enumerated double mixer info callback
1241 * @kcontrol: mixer control
1242 * @uinfo: control element information
1243 *
1244 * Callback to provide information about a double enumerated
1245 * mixer control.
1246 *
1247 * Returns 0 for success.
1248 */
1249int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1250 struct snd_ctl_elem_info *uinfo)
1251{
1252 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1253
1254 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1255 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1256 uinfo->value.enumerated.items = e->mask;
1257
1258 if (uinfo->value.enumerated.item > e->mask - 1)
1259 uinfo->value.enumerated.item = e->mask - 1;
1260 strcpy(uinfo->value.enumerated.name,
1261 e->texts[uinfo->value.enumerated.item]);
1262 return 0;
1263}
1264EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1265
1266/**
1267 * snd_soc_get_enum_double - enumerated double mixer get callback
1268 * @kcontrol: mixer control
1269 * @uinfo: control element information
1270 *
1271 * Callback to get the value of a double enumerated mixer.
1272 *
1273 * Returns 0 for success.
1274 */
1275int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1276 struct snd_ctl_elem_value *ucontrol)
1277{
1278 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1279 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1280 unsigned short val, bitmask;
1281
1282 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1283 ;
1284 val = snd_soc_read(codec, e->reg);
1285 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1286 if (e->shift_l != e->shift_r)
1287 ucontrol->value.enumerated.item[1] =
1288 (val >> e->shift_r) & (bitmask - 1);
1289
1290 return 0;
1291}
1292EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1293
1294/**
1295 * snd_soc_put_enum_double - enumerated double mixer put callback
1296 * @kcontrol: mixer control
1297 * @uinfo: control element information
1298 *
1299 * Callback to set the value of a double enumerated mixer.
1300 *
1301 * Returns 0 for success.
1302 */
1303int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1304 struct snd_ctl_elem_value *ucontrol)
1305{
1306 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1307 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1308 unsigned short val;
1309 unsigned short mask, bitmask;
1310
1311 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1312 ;
1313 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1314 return -EINVAL;
1315 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1316 mask = (bitmask - 1) << e->shift_l;
1317 if (e->shift_l != e->shift_r) {
1318 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
1319 return -EINVAL;
1320 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1321 mask |= (bitmask - 1) << e->shift_r;
1322 }
1323
1324 return snd_soc_update_bits(codec, e->reg, mask, val);
1325}
1326EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1327
1328/**
1329 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1330 * @kcontrol: mixer control
1331 * @uinfo: control element information
1332 *
1333 * Callback to provide information about an external enumerated
1334 * single mixer.
1335 *
1336 * Returns 0 for success.
1337 */
1338int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1339 struct snd_ctl_elem_info *uinfo)
1340{
1341 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1342
1343 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1344 uinfo->count = 1;
1345 uinfo->value.enumerated.items = e->mask;
1346
1347 if (uinfo->value.enumerated.item > e->mask - 1)
1348 uinfo->value.enumerated.item = e->mask - 1;
1349 strcpy(uinfo->value.enumerated.name,
1350 e->texts[uinfo->value.enumerated.item]);
1351 return 0;
1352}
1353EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1354
1355/**
1356 * snd_soc_info_volsw_ext - external single mixer info callback
1357 * @kcontrol: mixer control
1358 * @uinfo: control element information
1359 *
1360 * Callback to provide information about a single external mixer control.
1361 *
1362 * Returns 0 for success.
1363 */
1364int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1365 struct snd_ctl_elem_info *uinfo)
1366{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001367 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001368
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001369 if (max == 1)
1370 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1371 else
1372 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1373
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001374 uinfo->count = 1;
1375 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001376 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001377 return 0;
1378}
1379EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1380
1381/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001382 * snd_soc_info_volsw - single mixer info callback
1383 * @kcontrol: mixer control
1384 * @uinfo: control element information
1385 *
1386 * Callback to provide information about a single mixer control.
1387 *
1388 * Returns 0 for success.
1389 */
1390int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1391 struct snd_ctl_elem_info *uinfo)
1392{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001393 int max = (kcontrol->private_value >> 16) & 0xff;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001394 int shift = (kcontrol->private_value >> 8) & 0x0f;
1395 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1396
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001397 if (max == 1)
1398 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1399 else
1400 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1401
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001402 uinfo->count = shift == rshift ? 1 : 2;
1403 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001404 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001405 return 0;
1406}
1407EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1408
1409/**
1410 * snd_soc_get_volsw - single mixer get callback
1411 * @kcontrol: mixer control
1412 * @uinfo: control element information
1413 *
1414 * Callback to get the value of a single mixer control.
1415 *
1416 * Returns 0 for success.
1417 */
1418int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1419 struct snd_ctl_elem_value *ucontrol)
1420{
1421 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1422 int reg = kcontrol->private_value & 0xff;
1423 int shift = (kcontrol->private_value >> 8) & 0x0f;
1424 int rshift = (kcontrol->private_value >> 12) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001425 int max = (kcontrol->private_value >> 16) & 0xff;
1426 int mask = (1 << fls(max)) - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001427 int invert = (kcontrol->private_value >> 24) & 0x01;
1428
1429 ucontrol->value.integer.value[0] =
1430 (snd_soc_read(codec, reg) >> shift) & mask;
1431 if (shift != rshift)
1432 ucontrol->value.integer.value[1] =
1433 (snd_soc_read(codec, reg) >> rshift) & mask;
1434 if (invert) {
1435 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001436 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001437 if (shift != rshift)
1438 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001439 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001440 }
1441
1442 return 0;
1443}
1444EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1445
1446/**
1447 * snd_soc_put_volsw - single mixer put callback
1448 * @kcontrol: mixer control
1449 * @uinfo: control element information
1450 *
1451 * Callback to set the value of a single mixer control.
1452 *
1453 * Returns 0 for success.
1454 */
1455int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1456 struct snd_ctl_elem_value *ucontrol)
1457{
1458 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1459 int reg = kcontrol->private_value & 0xff;
1460 int shift = (kcontrol->private_value >> 8) & 0x0f;
1461 int rshift = (kcontrol->private_value >> 12) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001462 int max = (kcontrol->private_value >> 16) & 0xff;
1463 int mask = (1 << fls(max)) - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001464 int invert = (kcontrol->private_value >> 24) & 0x01;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001465 unsigned short val, val2, val_mask;
1466
1467 val = (ucontrol->value.integer.value[0] & mask);
1468 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001469 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001470 val_mask = mask << shift;
1471 val = val << shift;
1472 if (shift != rshift) {
1473 val2 = (ucontrol->value.integer.value[1] & mask);
1474 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001475 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001476 val_mask |= mask << rshift;
1477 val |= val2 << rshift;
1478 }
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001479 return snd_soc_update_bits(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001480}
1481EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1482
1483/**
1484 * snd_soc_info_volsw_2r - double mixer info callback
1485 * @kcontrol: mixer control
1486 * @uinfo: control element information
1487 *
1488 * Callback to provide information about a double mixer control that
1489 * spans 2 codec registers.
1490 *
1491 * Returns 0 for success.
1492 */
1493int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1494 struct snd_ctl_elem_info *uinfo)
1495{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001496 int max = (kcontrol->private_value >> 12) & 0xff;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001497
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001498 if (max == 1)
1499 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1500 else
1501 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1502
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001503 uinfo->count = 2;
1504 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001505 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001506 return 0;
1507}
1508EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1509
1510/**
1511 * snd_soc_get_volsw_2r - double mixer get callback
1512 * @kcontrol: mixer control
1513 * @uinfo: control element information
1514 *
1515 * Callback to get the value of a double mixer control that spans 2 registers.
1516 *
1517 * Returns 0 for success.
1518 */
1519int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1520 struct snd_ctl_elem_value *ucontrol)
1521{
1522 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1523 int reg = kcontrol->private_value & 0xff;
1524 int reg2 = (kcontrol->private_value >> 24) & 0xff;
1525 int shift = (kcontrol->private_value >> 8) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001526 int max = (kcontrol->private_value >> 12) & 0xff;
1527 int mask = (1<<fls(max))-1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001528 int invert = (kcontrol->private_value >> 20) & 0x01;
1529
1530 ucontrol->value.integer.value[0] =
1531 (snd_soc_read(codec, reg) >> shift) & mask;
1532 ucontrol->value.integer.value[1] =
1533 (snd_soc_read(codec, reg2) >> shift) & mask;
1534 if (invert) {
1535 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001536 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001537 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001538 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001539 }
1540
1541 return 0;
1542}
1543EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1544
1545/**
1546 * snd_soc_put_volsw_2r - double mixer set callback
1547 * @kcontrol: mixer control
1548 * @uinfo: control element information
1549 *
1550 * Callback to set the value of a double mixer control that spans 2 registers.
1551 *
1552 * Returns 0 for success.
1553 */
1554int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1555 struct snd_ctl_elem_value *ucontrol)
1556{
1557 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1558 int reg = kcontrol->private_value & 0xff;
1559 int reg2 = (kcontrol->private_value >> 24) & 0xff;
1560 int shift = (kcontrol->private_value >> 8) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001561 int max = (kcontrol->private_value >> 12) & 0xff;
1562 int mask = (1 << fls(max)) - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001563 int invert = (kcontrol->private_value >> 20) & 0x01;
1564 int err;
1565 unsigned short val, val2, val_mask;
1566
1567 val_mask = mask << shift;
1568 val = (ucontrol->value.integer.value[0] & mask);
1569 val2 = (ucontrol->value.integer.value[1] & mask);
1570
1571 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001572 val = max - val;
1573 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001574 }
1575
1576 val = val << shift;
1577 val2 = val2 << shift;
1578
1579 if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
1580 return err;
1581
1582 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1583 return err;
1584}
1585EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1586
1587static int __devinit snd_soc_init(void)
1588{
1589 printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
1590 return platform_driver_register(&soc_driver);
1591}
1592
1593static void snd_soc_exit(void)
1594{
1595 platform_driver_unregister(&soc_driver);
1596}
1597
1598module_init(snd_soc_init);
1599module_exit(snd_soc_exit);
1600
1601/* Module information */
1602MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1603MODULE_DESCRIPTION("ALSA SoC Core");
1604MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02001605MODULE_ALIAS("platform:soc-audio");