blob: a05b3450aee86bf75f552de50ac79c25caf90541 [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
111static inline const char* get_dai_name(int type)
112{
113 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 =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100181 max(codec_dai->playback.rate_min, cpu_dai->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200182 runtime->hw.rate_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100183 min(codec_dai->playback.rate_max, cpu_dai->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200184 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100185 max(codec_dai->playback.channels_min,
186 cpu_dai->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200187 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100188 min(codec_dai->playback.channels_max,
189 cpu_dai->playback.channels_max);
190 runtime->hw.formats =
191 codec_dai->playback.formats & cpu_dai->playback.formats;
192 runtime->hw.rates =
193 codec_dai->playback.rates & cpu_dai->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200194 } else {
195 runtime->hw.rate_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100196 max(codec_dai->capture.rate_min, cpu_dai->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200197 runtime->hw.rate_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100198 min(codec_dai->capture.rate_max, cpu_dai->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200199 runtime->hw.channels_min =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100200 max(codec_dai->capture.channels_min,
201 cpu_dai->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200202 runtime->hw.channels_max =
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100203 min(codec_dai->capture.channels_max,
204 cpu_dai->capture.channels_max);
205 runtime->hw.formats =
206 codec_dai->capture.formats & cpu_dai->capture.formats;
207 runtime->hw.rates =
208 codec_dai->capture.rates & cpu_dai->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200209 }
210
211 snd_pcm_limit_hw_rates(runtime);
212 if (!runtime->hw.rates) {
213 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100214 codec_dai->name, cpu_dai->name);
215 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200216 }
217 if (!runtime->hw.formats) {
218 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100219 codec_dai->name, cpu_dai->name);
220 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200221 }
222 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
223 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100224 codec_dai->name, cpu_dai->name);
225 goto machine_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200226 }
227
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100228 dbg("asoc: %s <-> %s info:\n",codec_dai->name, cpu_dai->name);
Liam Girdwoodb5c5fd22006-10-13 19:13:41 +0200229 dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
230 dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
231 runtime->hw.channels_max);
232 dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
233 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200234
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200235 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100236 cpu_dai->playback.active = codec_dai->playback.active = 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200237 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100238 cpu_dai->capture.active = codec_dai->capture.active = 1;
239 cpu_dai->active = codec_dai->active = 1;
240 cpu_dai->runtime = runtime;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200241 socdev->codec->active++;
242 mutex_unlock(&pcm_mutex);
243 return 0;
244
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100245machine_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200246 if (machine->ops && machine->ops->shutdown)
247 machine->ops->shutdown(substream);
248
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100249codec_dai_err:
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200250 if (platform->pcm_ops->close)
251 platform->pcm_ops->close(substream);
252
253platform_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100254 if (cpu_dai->ops.shutdown)
255 cpu_dai->ops.shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200256out:
257 mutex_unlock(&pcm_mutex);
258 return ret;
259}
260
261/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200262 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200263 * This is to ensure there are no pops or clicks in between any music tracks
264 * due to DAPM power cycling.
265 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100266static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200267{
Andrew Morton4484bb22006-12-15 09:30:07 +0100268 struct snd_soc_device *socdev =
269 container_of(work, struct snd_soc_device, delayed_work.work);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200270 struct snd_soc_codec *codec = socdev->codec;
271 struct snd_soc_codec_dai *codec_dai;
272 int i;
273
274 mutex_lock(&pcm_mutex);
275 for(i = 0; i < codec->num_dai; i++) {
276 codec_dai = &codec->dai[i];
277
278 dbg("pop wq checking: %s status: %s waiting: %s\n",
279 codec_dai->playback.stream_name,
280 codec_dai->playback.active ? "active" : "inactive",
281 codec_dai->pop_wait ? "yes" : "no");
282
283 /* are we waiting on this codec DAI stream */
284 if (codec_dai->pop_wait == 1) {
285
Mark Brown0be98982008-05-19 12:31:28 +0200286 /* Reduce power if no longer active */
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100287 if (codec->active == 0) {
288 dbg("pop wq D1 %s %s\n", codec->name,
289 codec_dai->playback.stream_name);
Mark Brown0be98982008-05-19 12:31:28 +0200290 snd_soc_dapm_set_bias_level(socdev,
291 SND_SOC_BIAS_PREPARE);
Liam Girdwood3c1c47e2008-01-10 14:38:24 +0100292 }
293
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200294 codec_dai->pop_wait = 0;
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100295 snd_soc_dapm_stream_event(codec,
296 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200297 SND_SOC_DAPM_STREAM_STOP);
298
Mark Brown0be98982008-05-19 12:31:28 +0200299 /* Fall into standby if no longer active */
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200300 if (codec->active == 0) {
301 dbg("pop wq D3 %s %s\n", codec->name,
302 codec_dai->playback.stream_name);
Mark Brown0be98982008-05-19 12:31:28 +0200303 snd_soc_dapm_set_bias_level(socdev,
304 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200305 }
306 }
307 }
308 mutex_unlock(&pcm_mutex);
309}
310
311/*
312 * Called by ALSA when a PCM substream is closed. Private data can be
313 * freed here. The cpu DAI, codec DAI, machine and platform are also
314 * shutdown.
315 */
316static int soc_codec_close(struct snd_pcm_substream *substream)
317{
318 struct snd_soc_pcm_runtime *rtd = substream->private_data;
319 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100320 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200321 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100322 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
323 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200324 struct snd_soc_codec *codec = socdev->codec;
325
326 mutex_lock(&pcm_mutex);
327
328 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100329 cpu_dai->playback.active = codec_dai->playback.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200330 else
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100331 cpu_dai->capture.active = codec_dai->capture.active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200332
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100333 if (codec_dai->playback.active == 0 &&
334 codec_dai->capture.active == 0) {
335 cpu_dai->active = codec_dai->active = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200336 }
337 codec->active--;
338
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100339 if (cpu_dai->ops.shutdown)
340 cpu_dai->ops.shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200341
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100342 if (codec_dai->ops.shutdown)
343 codec_dai->ops.shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200344
345 if (machine->ops && machine->ops->shutdown)
346 machine->ops->shutdown(substream);
347
348 if (platform->pcm_ops->close)
349 platform->pcm_ops->close(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100350 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200351
352 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
353 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100354 codec_dai->pop_wait = 1;
Takashi Iwai4bb09522006-12-19 17:16:14 +0100355 schedule_delayed_work(&socdev->delayed_work,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200356 msecs_to_jiffies(pmdown_time));
357 } else {
358 /* capture streams can be powered down now */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100359 snd_soc_dapm_stream_event(codec,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100360 codec_dai->capture.stream_name,
361 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200362
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100363 if (codec->active == 0 && codec_dai->pop_wait == 0)
Mark Brown0be98982008-05-19 12:31:28 +0200364 snd_soc_dapm_set_bias_level(socdev,
365 SND_SOC_BIAS_STANDBY);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200366 }
367
368 mutex_unlock(&pcm_mutex);
369 return 0;
370}
371
372/*
373 * Called by ALSA when the PCM substream is prepared, can set format, sample
374 * rate, etc. This function is non atomic and can be called multiple times,
375 * it can refer to the runtime info.
376 */
377static int soc_pcm_prepare(struct snd_pcm_substream *substream)
378{
379 struct snd_soc_pcm_runtime *rtd = substream->private_data;
380 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100381 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200382 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100383 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
384 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200385 struct snd_soc_codec *codec = socdev->codec;
386 int ret = 0;
387
388 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100389
390 if (machine->ops && machine->ops->prepare) {
391 ret = machine->ops->prepare(substream);
392 if (ret < 0) {
393 printk(KERN_ERR "asoc: machine prepare error\n");
394 goto out;
395 }
396 }
397
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200398 if (platform->pcm_ops->prepare) {
399 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200400 if (ret < 0) {
401 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200402 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200403 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200404 }
405
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100406 if (codec_dai->ops.prepare) {
407 ret = codec_dai->ops.prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200408 if (ret < 0) {
409 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200410 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200411 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200412 }
413
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100414 if (cpu_dai->ops.prepare) {
415 ret = cpu_dai->ops.prepare(substream);
416 if (ret < 0) {
417 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
418 goto out;
419 }
420 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200421
422 /* we only want to start a DAPM playback stream if we are not waiting
423 * on an existing one stopping */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100424 if (codec_dai->pop_wait) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200425 /* we are waiting for the delayed work to start */
426 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100427 snd_soc_dapm_stream_event(socdev->codec,
428 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200429 SND_SOC_DAPM_STREAM_START);
430 else {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100431 codec_dai->pop_wait = 0;
Andrew Morton4484bb22006-12-15 09:30:07 +0100432 cancel_delayed_work(&socdev->delayed_work);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100433 if (codec_dai->dai_ops.digital_mute)
434 codec_dai->dai_ops.digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200435 }
436 } else {
437 /* no delayed work - do we need to power up codec */
Mark Brown0be98982008-05-19 12:31:28 +0200438 if (codec->bias_level != SND_SOC_BIAS_ON) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200439
Mark Brown0be98982008-05-19 12:31:28 +0200440 snd_soc_dapm_set_bias_level(socdev,
441 SND_SOC_BIAS_PREPARE);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200442
443 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
444 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100445 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200446 SND_SOC_DAPM_STREAM_START);
447 else
448 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100449 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200450 SND_SOC_DAPM_STREAM_START);
451
Mark Brown0be98982008-05-19 12:31:28 +0200452 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100453 if (codec_dai->dai_ops.digital_mute)
454 codec_dai->dai_ops.digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200455
456 } else {
457 /* codec already powered - power on widgets */
458 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
459 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100460 codec_dai->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200461 SND_SOC_DAPM_STREAM_START);
462 else
463 snd_soc_dapm_stream_event(codec,
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100464 codec_dai->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200465 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100466 if (codec_dai->dai_ops.digital_mute)
467 codec_dai->dai_ops.digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200468 }
469 }
470
471out:
472 mutex_unlock(&pcm_mutex);
473 return ret;
474}
475
476/*
477 * Called by ALSA when the hardware params are set by application. This
478 * function can also be called multiple times and can allocate buffers
479 * (using snd_pcm_lib_* ). It's non-atomic.
480 */
481static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
482 struct snd_pcm_hw_params *params)
483{
484 struct snd_soc_pcm_runtime *rtd = substream->private_data;
485 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100486 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200487 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100488 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
489 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200490 int ret = 0;
491
492 mutex_lock(&pcm_mutex);
493
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100494 if (machine->ops && machine->ops->hw_params) {
495 ret = machine->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200496 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100497 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200498 goto out;
499 }
500 }
501
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100502 if (codec_dai->ops.hw_params) {
503 ret = codec_dai->ops.hw_params(substream, params);
504 if (ret < 0) {
505 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
506 codec_dai->name);
507 goto codec_err;
508 }
509 }
510
511 if (cpu_dai->ops.hw_params) {
512 ret = cpu_dai->ops.hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200513 if (ret < 0) {
514 printk(KERN_ERR "asoc: can't set interface %s hw params\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100515 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200516 goto interface_err;
517 }
518 }
519
520 if (platform->pcm_ops->hw_params) {
521 ret = platform->pcm_ops->hw_params(substream, params);
522 if (ret < 0) {
523 printk(KERN_ERR "asoc: can't set platform %s hw params\n",
524 platform->name);
525 goto platform_err;
526 }
527 }
528
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200529out:
530 mutex_unlock(&pcm_mutex);
531 return ret;
532
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200533platform_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100534 if (cpu_dai->ops.hw_free)
535 cpu_dai->ops.hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536
537interface_err:
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100538 if (codec_dai->ops.hw_free)
539 codec_dai->ops.hw_free(substream);
540
541codec_err:
542 if(machine->ops && machine->ops->hw_free)
543 machine->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200544
545 mutex_unlock(&pcm_mutex);
546 return ret;
547}
548
549/*
550 * Free's resources allocated by hw_params, can be called multiple times
551 */
552static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
553{
554 struct snd_soc_pcm_runtime *rtd = substream->private_data;
555 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100556 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200557 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100558 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
559 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200560 struct snd_soc_codec *codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200561
562 mutex_lock(&pcm_mutex);
563
564 /* apply codec digital mute */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100565 if (!codec->active && codec_dai->dai_ops.digital_mute)
566 codec_dai->dai_ops.digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200567
568 /* free any machine hw params */
569 if (machine->ops && machine->ops->hw_free)
570 machine->ops->hw_free(substream);
571
572 /* free any DMA resources */
573 if (platform->pcm_ops->hw_free)
574 platform->pcm_ops->hw_free(substream);
575
576 /* now free hw params for the DAI's */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100577 if (codec_dai->ops.hw_free)
578 codec_dai->ops.hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200579
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100580 if (cpu_dai->ops.hw_free)
581 cpu_dai->ops.hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200582
583 mutex_unlock(&pcm_mutex);
584 return 0;
585}
586
587static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
588{
589 struct snd_soc_pcm_runtime *rtd = substream->private_data;
590 struct snd_soc_device *socdev = rtd->socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100591 struct snd_soc_dai_link *machine = rtd->dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592 struct snd_soc_platform *platform = socdev->platform;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100593 struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
594 struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200595 int ret;
596
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100597 if (codec_dai->ops.trigger) {
598 ret = codec_dai->ops.trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200599 if (ret < 0)
600 return ret;
601 }
602
603 if (platform->pcm_ops->trigger) {
604 ret = platform->pcm_ops->trigger(substream, cmd);
605 if (ret < 0)
606 return ret;
607 }
608
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100609 if (cpu_dai->ops.trigger) {
610 ret = cpu_dai->ops.trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200611 if (ret < 0)
612 return ret;
613 }
614 return 0;
615}
616
617/* ASoC PCM operations */
618static struct snd_pcm_ops soc_pcm_ops = {
619 .open = soc_pcm_open,
620 .close = soc_codec_close,
621 .hw_params = soc_pcm_hw_params,
622 .hw_free = soc_pcm_hw_free,
623 .prepare = soc_pcm_prepare,
624 .trigger = soc_pcm_trigger,
625};
626
627#ifdef CONFIG_PM
628/* powers down audio subsystem for suspend */
629static int soc_suspend(struct platform_device *pdev, pm_message_t state)
630{
631 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
632 struct snd_soc_machine *machine = socdev->machine;
633 struct snd_soc_platform *platform = socdev->platform;
634 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
635 struct snd_soc_codec *codec = socdev->codec;
636 int i;
637
638 /* mute any active DAC's */
639 for(i = 0; i < machine->num_links; i++) {
640 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100641 if (dai->dai_ops.digital_mute && dai->playback.active)
642 dai->dai_ops.digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200643 }
644
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100645 /* suspend all pcms */
646 for (i = 0; i < machine->num_links; i++)
647 snd_pcm_suspend_all(machine->dai_link[i].pcm);
648
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200649 if (machine->suspend_pre)
650 machine->suspend_pre(pdev, state);
651
652 for(i = 0; i < machine->num_links; i++) {
653 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
654 if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
655 cpu_dai->suspend(pdev, cpu_dai);
656 if (platform->suspend)
657 platform->suspend(pdev, cpu_dai);
658 }
659
660 /* close any waiting streams and save state */
Liam Girdwood965ac422007-01-31 14:14:57 +0100661 run_delayed_work(&socdev->delayed_work);
Mark Brown0be98982008-05-19 12:31:28 +0200662 codec->suspend_bias_level = codec->bias_level;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200663
664 for(i = 0; i < codec->num_dai; i++) {
665 char *stream = codec->dai[i].playback.stream_name;
666 if (stream != NULL)
667 snd_soc_dapm_stream_event(codec, stream,
668 SND_SOC_DAPM_STREAM_SUSPEND);
669 stream = codec->dai[i].capture.stream_name;
670 if (stream != NULL)
671 snd_soc_dapm_stream_event(codec, stream,
672 SND_SOC_DAPM_STREAM_SUSPEND);
673 }
674
675 if (codec_dev->suspend)
676 codec_dev->suspend(pdev, state);
677
678 for(i = 0; i < machine->num_links; i++) {
679 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
680 if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
681 cpu_dai->suspend(pdev, cpu_dai);
682 }
683
684 if (machine->suspend_post)
685 machine->suspend_post(pdev, state);
686
687 return 0;
688}
689
690/* powers up audio subsystem after a suspend */
691static int soc_resume(struct platform_device *pdev)
692{
693 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
694 struct snd_soc_machine *machine = socdev->machine;
695 struct snd_soc_platform *platform = socdev->platform;
696 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
697 struct snd_soc_codec *codec = socdev->codec;
698 int i;
699
700 if (machine->resume_pre)
701 machine->resume_pre(pdev);
702
703 for(i = 0; i < machine->num_links; i++) {
704 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
705 if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
706 cpu_dai->resume(pdev, cpu_dai);
707 }
708
709 if (codec_dev->resume)
710 codec_dev->resume(pdev);
711
712 for(i = 0; i < codec->num_dai; i++) {
713 char* stream = codec->dai[i].playback.stream_name;
714 if (stream != NULL)
715 snd_soc_dapm_stream_event(codec, stream,
716 SND_SOC_DAPM_STREAM_RESUME);
717 stream = codec->dai[i].capture.stream_name;
718 if (stream != NULL)
719 snd_soc_dapm_stream_event(codec, stream,
720 SND_SOC_DAPM_STREAM_RESUME);
721 }
722
723 /* unmute any active DAC's */
724 for(i = 0; i < machine->num_links; i++) {
725 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100726 if (dai->dai_ops.digital_mute && dai->playback.active)
727 dai->dai_ops.digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200728 }
729
730 for(i = 0; i < machine->num_links; i++) {
731 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
732 if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
733 cpu_dai->resume(pdev, cpu_dai);
734 if (platform->resume)
735 platform->resume(pdev, cpu_dai);
736 }
737
738 if (machine->resume_post)
739 machine->resume_post(pdev);
740
741 return 0;
742}
743
744#else
745#define soc_suspend NULL
746#define soc_resume NULL
747#endif
748
749/* probes a new socdev */
750static int soc_probe(struct platform_device *pdev)
751{
752 int ret = 0, i;
753 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
754 struct snd_soc_machine *machine = socdev->machine;
755 struct snd_soc_platform *platform = socdev->platform;
756 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
757
758 if (machine->probe) {
759 ret = machine->probe(pdev);
760 if(ret < 0)
761 return ret;
762 }
763
764 for (i = 0; i < machine->num_links; i++) {
765 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
766 if (cpu_dai->probe) {
767 ret = cpu_dai->probe(pdev);
768 if(ret < 0)
769 goto cpu_dai_err;
770 }
771 }
772
773 if (codec_dev->probe) {
774 ret = codec_dev->probe(pdev);
775 if(ret < 0)
776 goto cpu_dai_err;
777 }
778
779 if (platform->probe) {
780 ret = platform->probe(pdev);
781 if(ret < 0)
782 goto platform_err;
783 }
784
785 /* DAPM stream work */
Andrew Morton4484bb22006-12-15 09:30:07 +0100786 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200787 return 0;
788
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200789platform_err:
790 if (codec_dev->remove)
791 codec_dev->remove(pdev);
792
793cpu_dai_err:
Liam Girdwood18b9b3d2007-01-30 17:18:45 +0100794 for (i--; i >= 0; i--) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200795 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
796 if (cpu_dai->remove)
797 cpu_dai->remove(pdev);
798 }
799
800 if (machine->remove)
801 machine->remove(pdev);
802
803 return ret;
804}
805
806/* removes a socdev */
807static int soc_remove(struct platform_device *pdev)
808{
809 int i;
810 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
811 struct snd_soc_machine *machine = socdev->machine;
812 struct snd_soc_platform *platform = socdev->platform;
813 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
814
Liam Girdwood965ac422007-01-31 14:14:57 +0100815 run_delayed_work(&socdev->delayed_work);
816
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200817 if (platform->remove)
818 platform->remove(pdev);
819
820 if (codec_dev->remove)
821 codec_dev->remove(pdev);
822
823 for (i = 0; i < machine->num_links; i++) {
824 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
825 if (cpu_dai->remove)
826 cpu_dai->remove(pdev);
827 }
828
829 if (machine->remove)
830 machine->remove(pdev);
831
832 return 0;
833}
834
835/* ASoC platform driver */
836static struct platform_driver soc_driver = {
837 .driver = {
838 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +0200839 .owner = THIS_MODULE,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200840 },
841 .probe = soc_probe,
842 .remove = soc_remove,
843 .suspend = soc_suspend,
844 .resume = soc_resume,
845};
846
847/* create a new pcm */
848static int soc_new_pcm(struct snd_soc_device *socdev,
849 struct snd_soc_dai_link *dai_link, int num)
850{
851 struct snd_soc_codec *codec = socdev->codec;
852 struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
853 struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
854 struct snd_soc_pcm_runtime *rtd;
855 struct snd_pcm *pcm;
856 char new_name[64];
857 int ret = 0, playback = 0, capture = 0;
858
859 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
860 if (rtd == NULL)
861 return -ENOMEM;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100862
863 rtd->dai = dai_link;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200864 rtd->socdev = socdev;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100865 codec_dai->codec = socdev->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200866
867 /* check client and interface hw capabilities */
868 sprintf(new_name, "%s %s-%s-%d",dai_link->stream_name, codec_dai->name,
869 get_dai_name(cpu_dai->type), num);
870
871 if (codec_dai->playback.channels_min)
872 playback = 1;
873 if (codec_dai->capture.channels_min)
874 capture = 1;
875
876 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
877 capture, &pcm);
878 if (ret < 0) {
879 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
880 kfree(rtd);
881 return ret;
882 }
883
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100884 dai_link->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200885 pcm->private_data = rtd;
886 soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
887 soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
888 soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
889 soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
890 soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
891 soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
892 soc_pcm_ops.page = socdev->platform->pcm_ops->page;
893
894 if (playback)
895 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
896
897 if (capture)
898 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
899
900 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
901 if (ret < 0) {
902 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
903 kfree(rtd);
904 return ret;
905 }
906
907 pcm->private_free = socdev->platform->pcm_free;
908 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
909 cpu_dai->name);
910 return ret;
911}
912
913/* codec register dump */
914static ssize_t codec_reg_show(struct device *dev,
915 struct device_attribute *attr, char *buf)
916{
917 struct snd_soc_device *devdata = dev_get_drvdata(dev);
918 struct snd_soc_codec *codec = devdata->codec;
919 int i, step = 1, count = 0;
920
921 if (!codec->reg_cache_size)
922 return 0;
923
924 if (codec->reg_cache_step)
925 step = codec->reg_cache_step;
926
927 count += sprintf(buf, "%s registers\n", codec->name);
928 for(i = 0; i < codec->reg_cache_size; i += step)
929 count += sprintf(buf + count, "%2x: %4x\n", i, codec->read(codec, i));
930
931 return count;
932}
933static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
934
935/**
936 * snd_soc_new_ac97_codec - initailise AC97 device
937 * @codec: audio codec
938 * @ops: AC97 bus operations
939 * @num: AC97 codec number
940 *
941 * Initialises AC97 codec resources for use by ad-hoc devices only.
942 */
943int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
944 struct snd_ac97_bus_ops *ops, int num)
945{
946 mutex_lock(&codec->mutex);
947
948 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
949 if (codec->ac97 == NULL) {
950 mutex_unlock(&codec->mutex);
951 return -ENOMEM;
952 }
953
954 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
955 if (codec->ac97->bus == NULL) {
956 kfree(codec->ac97);
957 codec->ac97 = NULL;
958 mutex_unlock(&codec->mutex);
959 return -ENOMEM;
960 }
961
962 codec->ac97->bus->ops = ops;
963 codec->ac97->num = num;
964 mutex_unlock(&codec->mutex);
965 return 0;
966}
967EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
968
969/**
970 * snd_soc_free_ac97_codec - free AC97 codec device
971 * @codec: audio codec
972 *
973 * Frees AC97 codec device resources.
974 */
975void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
976{
977 mutex_lock(&codec->mutex);
978 kfree(codec->ac97->bus);
979 kfree(codec->ac97);
980 codec->ac97 = NULL;
981 mutex_unlock(&codec->mutex);
982}
983EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
984
985/**
986 * snd_soc_update_bits - update codec register bits
987 * @codec: audio codec
988 * @reg: codec register
989 * @mask: register mask
990 * @value: new value
991 *
992 * Writes new register value.
993 *
994 * Returns 1 for change else 0.
995 */
996int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
997 unsigned short mask, unsigned short value)
998{
999 int change;
1000 unsigned short old, new;
1001
1002 mutex_lock(&io_mutex);
1003 old = snd_soc_read(codec, reg);
1004 new = (old & ~mask) | value;
1005 change = old != new;
1006 if (change)
1007 snd_soc_write(codec, reg, new);
1008
1009 mutex_unlock(&io_mutex);
1010 return change;
1011}
1012EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1013
1014/**
1015 * snd_soc_test_bits - test register for change
1016 * @codec: audio codec
1017 * @reg: codec register
1018 * @mask: register mask
1019 * @value: new value
1020 *
1021 * Tests a register with a new value and checks if the new value is
1022 * different from the old value.
1023 *
1024 * Returns 1 for change else 0.
1025 */
1026int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1027 unsigned short mask, unsigned short value)
1028{
1029 int change;
1030 unsigned short old, new;
1031
1032 mutex_lock(&io_mutex);
1033 old = snd_soc_read(codec, reg);
1034 new = (old & ~mask) | value;
1035 change = old != new;
1036 mutex_unlock(&io_mutex);
1037
1038 return change;
1039}
1040EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1041
1042/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001043 * snd_soc_new_pcms - create new sound card and pcms
1044 * @socdev: the SoC audio device
1045 *
1046 * Create a new sound card based upon the codec and interface pcms.
1047 *
1048 * Returns 0 for success, else error.
1049 */
Liam Girdwoodbc7320c2007-02-01 12:26:07 +01001050int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001051{
1052 struct snd_soc_codec *codec = socdev->codec;
1053 struct snd_soc_machine *machine = socdev->machine;
1054 int ret = 0, i;
1055
1056 mutex_lock(&codec->mutex);
1057
1058 /* register a sound card */
1059 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1060 if (!codec->card) {
1061 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1062 codec->name);
1063 mutex_unlock(&codec->mutex);
1064 return -ENODEV;
1065 }
1066
1067 codec->card->dev = socdev->dev;
1068 codec->card->private_data = codec;
1069 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1070
1071 /* create the pcms */
1072 for(i = 0; i < machine->num_links; i++) {
1073 ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
1074 if (ret < 0) {
1075 printk(KERN_ERR "asoc: can't create pcm %s\n",
1076 machine->dai_link[i].stream_name);
1077 mutex_unlock(&codec->mutex);
1078 return ret;
1079 }
1080 }
1081
1082 mutex_unlock(&codec->mutex);
1083 return ret;
1084}
1085EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1086
1087/**
1088 * snd_soc_register_card - register sound card
1089 * @socdev: the SoC audio device
1090 *
1091 * Register a SoC sound card. Also registers an AC97 device if the
1092 * codec is AC97 for ad hoc devices.
1093 *
1094 * Returns 0 for success, else error.
1095 */
1096int snd_soc_register_card(struct snd_soc_device *socdev)
1097{
1098 struct snd_soc_codec *codec = socdev->codec;
1099 struct snd_soc_machine *machine = socdev->machine;
Liam Girdwood12e74f72006-10-16 21:19:48 +02001100 int ret = 0, i, ac97 = 0, err = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001101
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001102 for(i = 0; i < machine->num_links; i++) {
Liam Girdwood12e74f72006-10-16 21:19:48 +02001103 if (socdev->machine->dai_link[i].init) {
1104 err = socdev->machine->dai_link[i].init(codec);
1105 if (err < 0) {
1106 printk(KERN_ERR "asoc: failed to init %s\n",
1107 socdev->machine->dai_link[i].stream_name);
1108 continue;
1109 }
1110 }
Liam Girdwooda68660e2007-05-10 19:27:27 +02001111 if (socdev->machine->dai_link[i].codec_dai->type ==
1112 SND_SOC_DAI_AC97_BUS)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001113 ac97 = 1;
1114 }
1115 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1116 "%s", machine->name);
1117 snprintf(codec->card->longname, sizeof(codec->card->longname),
1118 "%s (%s)", machine->name, codec->name);
1119
1120 ret = snd_card_register(codec->card);
1121 if (ret < 0) {
1122 printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n",
1123 codec->name);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001124 goto out;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001125 }
1126
Mark Brown08c8efe2008-01-21 14:33:37 +01001127 mutex_lock(&codec->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001128#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood12e74f72006-10-16 21:19:48 +02001129 if (ac97) {
1130 ret = soc_ac97_dev_register(codec);
1131 if (ret < 0) {
1132 printk(KERN_ERR "asoc: AC97 device register failed\n");
1133 snd_card_free(codec->card);
Mark Brown08c8efe2008-01-21 14:33:37 +01001134 mutex_unlock(&codec->mutex);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001135 goto out;
1136 }
1137 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001138#endif
1139
Liam Girdwood12e74f72006-10-16 21:19:48 +02001140 err = snd_soc_dapm_sys_add(socdev->dev);
1141 if (err < 0)
1142 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1143
1144 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1145 if (err < 0)
1146 printk(KERN_WARNING "asoc: failed to add codec sysfs entries\n");
Mark Brown08c8efe2008-01-21 14:33:37 +01001147
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001148 mutex_unlock(&codec->mutex);
Mark Brown08c8efe2008-01-21 14:33:37 +01001149
1150out:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001151 return ret;
1152}
1153EXPORT_SYMBOL_GPL(snd_soc_register_card);
1154
1155/**
1156 * snd_soc_free_pcms - free sound card and pcms
1157 * @socdev: the SoC audio device
1158 *
1159 * Frees sound card and pcms associated with the socdev.
1160 * Also unregister the codec if it is an AC97 device.
1161 */
1162void snd_soc_free_pcms(struct snd_soc_device *socdev)
1163{
1164 struct snd_soc_codec *codec = socdev->codec;
Liam Girdwooda68660e2007-05-10 19:27:27 +02001165#ifdef CONFIG_SND_SOC_AC97_BUS
1166 struct snd_soc_codec_dai *codec_dai;
1167 int i;
1168#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001169
1170 mutex_lock(&codec->mutex);
1171#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwooda68660e2007-05-10 19:27:27 +02001172 for(i = 0; i < codec->num_dai; i++) {
1173 codec_dai = &codec->dai[i];
1174 if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
1175 soc_ac97_dev_unregister(codec);
1176 goto free_card;
1177 }
1178 }
1179free_card:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001180#endif
1181
1182 if (codec->card)
1183 snd_card_free(codec->card);
1184 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1185 mutex_unlock(&codec->mutex);
1186}
1187EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1188
1189/**
1190 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1191 * @substream: the pcm substream
1192 * @hw: the hardware parameters
1193 *
1194 * Sets the substream runtime hardware parameters.
1195 */
1196int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1197 const struct snd_pcm_hardware *hw)
1198{
1199 struct snd_pcm_runtime *runtime = substream->runtime;
1200 runtime->hw.info = hw->info;
1201 runtime->hw.formats = hw->formats;
1202 runtime->hw.period_bytes_min = hw->period_bytes_min;
1203 runtime->hw.period_bytes_max = hw->period_bytes_max;
1204 runtime->hw.periods_min = hw->periods_min;
1205 runtime->hw.periods_max = hw->periods_max;
1206 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1207 runtime->hw.fifo_size = hw->fifo_size;
1208 return 0;
1209}
1210EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1211
1212/**
1213 * snd_soc_cnew - create new control
1214 * @_template: control template
1215 * @data: control private data
1216 * @lnng_name: control long name
1217 *
1218 * Create a new mixer control from a template control.
1219 *
1220 * Returns 0 for success, else error.
1221 */
1222struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1223 void *data, char *long_name)
1224{
1225 struct snd_kcontrol_new template;
1226
1227 memcpy(&template, _template, sizeof(template));
1228 if (long_name)
1229 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001230 template.index = 0;
1231
1232 return snd_ctl_new1(&template, data);
1233}
1234EXPORT_SYMBOL_GPL(snd_soc_cnew);
1235
1236/**
1237 * snd_soc_info_enum_double - enumerated double mixer info callback
1238 * @kcontrol: mixer control
1239 * @uinfo: control element information
1240 *
1241 * Callback to provide information about a double enumerated
1242 * mixer control.
1243 *
1244 * Returns 0 for success.
1245 */
1246int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1247 struct snd_ctl_elem_info *uinfo)
1248{
1249 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1250
1251 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1252 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1253 uinfo->value.enumerated.items = e->mask;
1254
1255 if (uinfo->value.enumerated.item > e->mask - 1)
1256 uinfo->value.enumerated.item = e->mask - 1;
1257 strcpy(uinfo->value.enumerated.name,
1258 e->texts[uinfo->value.enumerated.item]);
1259 return 0;
1260}
1261EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1262
1263/**
1264 * snd_soc_get_enum_double - enumerated double mixer get callback
1265 * @kcontrol: mixer control
1266 * @uinfo: control element information
1267 *
1268 * Callback to get the value of a double enumerated mixer.
1269 *
1270 * Returns 0 for success.
1271 */
1272int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1273 struct snd_ctl_elem_value *ucontrol)
1274{
1275 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1276 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1277 unsigned short val, bitmask;
1278
1279 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1280 ;
1281 val = snd_soc_read(codec, e->reg);
1282 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1283 if (e->shift_l != e->shift_r)
1284 ucontrol->value.enumerated.item[1] =
1285 (val >> e->shift_r) & (bitmask - 1);
1286
1287 return 0;
1288}
1289EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1290
1291/**
1292 * snd_soc_put_enum_double - enumerated double mixer put callback
1293 * @kcontrol: mixer control
1294 * @uinfo: control element information
1295 *
1296 * Callback to set the value of a double enumerated mixer.
1297 *
1298 * Returns 0 for success.
1299 */
1300int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1301 struct snd_ctl_elem_value *ucontrol)
1302{
1303 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1304 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1305 unsigned short val;
1306 unsigned short mask, bitmask;
1307
1308 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1309 ;
1310 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1311 return -EINVAL;
1312 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1313 mask = (bitmask - 1) << e->shift_l;
1314 if (e->shift_l != e->shift_r) {
1315 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
1316 return -EINVAL;
1317 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1318 mask |= (bitmask - 1) << e->shift_r;
1319 }
1320
1321 return snd_soc_update_bits(codec, e->reg, mask, val);
1322}
1323EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1324
1325/**
1326 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1327 * @kcontrol: mixer control
1328 * @uinfo: control element information
1329 *
1330 * Callback to provide information about an external enumerated
1331 * single mixer.
1332 *
1333 * Returns 0 for success.
1334 */
1335int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1336 struct snd_ctl_elem_info *uinfo)
1337{
1338 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1339
1340 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1341 uinfo->count = 1;
1342 uinfo->value.enumerated.items = e->mask;
1343
1344 if (uinfo->value.enumerated.item > e->mask - 1)
1345 uinfo->value.enumerated.item = e->mask - 1;
1346 strcpy(uinfo->value.enumerated.name,
1347 e->texts[uinfo->value.enumerated.item]);
1348 return 0;
1349}
1350EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1351
1352/**
1353 * snd_soc_info_volsw_ext - external single mixer info callback
1354 * @kcontrol: mixer control
1355 * @uinfo: control element information
1356 *
1357 * Callback to provide information about a single external mixer control.
1358 *
1359 * Returns 0 for success.
1360 */
1361int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1362 struct snd_ctl_elem_info *uinfo)
1363{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001364 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001365
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001366 if (max == 1)
1367 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1368 else
1369 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1370
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001371 uinfo->count = 1;
1372 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001373 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001374 return 0;
1375}
1376EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1377
1378/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001379 * snd_soc_info_volsw - single mixer info callback
1380 * @kcontrol: mixer control
1381 * @uinfo: control element information
1382 *
1383 * Callback to provide information about a single mixer control.
1384 *
1385 * Returns 0 for success.
1386 */
1387int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1388 struct snd_ctl_elem_info *uinfo)
1389{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001390 int max = (kcontrol->private_value >> 16) & 0xff;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001391 int shift = (kcontrol->private_value >> 8) & 0x0f;
1392 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1393
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001394 if (max == 1)
1395 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1396 else
1397 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1398
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001399 uinfo->count = shift == rshift ? 1 : 2;
1400 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001401 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001402 return 0;
1403}
1404EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1405
1406/**
1407 * snd_soc_get_volsw - single mixer get callback
1408 * @kcontrol: mixer control
1409 * @uinfo: control element information
1410 *
1411 * Callback to get the value of a single mixer control.
1412 *
1413 * Returns 0 for success.
1414 */
1415int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1416 struct snd_ctl_elem_value *ucontrol)
1417{
1418 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1419 int reg = kcontrol->private_value & 0xff;
1420 int shift = (kcontrol->private_value >> 8) & 0x0f;
1421 int rshift = (kcontrol->private_value >> 12) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001422 int max = (kcontrol->private_value >> 16) & 0xff;
1423 int mask = (1 << fls(max)) - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001424 int invert = (kcontrol->private_value >> 24) & 0x01;
1425
1426 ucontrol->value.integer.value[0] =
1427 (snd_soc_read(codec, reg) >> shift) & mask;
1428 if (shift != rshift)
1429 ucontrol->value.integer.value[1] =
1430 (snd_soc_read(codec, reg) >> rshift) & mask;
1431 if (invert) {
1432 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001433 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001434 if (shift != rshift)
1435 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001436 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001437 }
1438
1439 return 0;
1440}
1441EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1442
1443/**
1444 * snd_soc_put_volsw - single mixer put callback
1445 * @kcontrol: mixer control
1446 * @uinfo: control element information
1447 *
1448 * Callback to set the value of a single mixer control.
1449 *
1450 * Returns 0 for success.
1451 */
1452int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1453 struct snd_ctl_elem_value *ucontrol)
1454{
1455 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1456 int reg = kcontrol->private_value & 0xff;
1457 int shift = (kcontrol->private_value >> 8) & 0x0f;
1458 int rshift = (kcontrol->private_value >> 12) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001459 int max = (kcontrol->private_value >> 16) & 0xff;
1460 int mask = (1 << fls(max)) - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001461 int invert = (kcontrol->private_value >> 24) & 0x01;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001462 unsigned short val, val2, val_mask;
1463
1464 val = (ucontrol->value.integer.value[0] & mask);
1465 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001466 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001467 val_mask = mask << shift;
1468 val = val << shift;
1469 if (shift != rshift) {
1470 val2 = (ucontrol->value.integer.value[1] & mask);
1471 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001472 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001473 val_mask |= mask << rshift;
1474 val |= val2 << rshift;
1475 }
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001476 return snd_soc_update_bits(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001477}
1478EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1479
1480/**
1481 * snd_soc_info_volsw_2r - double mixer info callback
1482 * @kcontrol: mixer control
1483 * @uinfo: control element information
1484 *
1485 * Callback to provide information about a double mixer control that
1486 * spans 2 codec registers.
1487 *
1488 * Returns 0 for success.
1489 */
1490int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1491 struct snd_ctl_elem_info *uinfo)
1492{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001493 int max = (kcontrol->private_value >> 12) & 0xff;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001494
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001495 if (max == 1)
1496 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1497 else
1498 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1499
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001500 uinfo->count = 2;
1501 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001502 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001503 return 0;
1504}
1505EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1506
1507/**
1508 * snd_soc_get_volsw_2r - double mixer get callback
1509 * @kcontrol: mixer control
1510 * @uinfo: control element information
1511 *
1512 * Callback to get the value of a double mixer control that spans 2 registers.
1513 *
1514 * Returns 0 for success.
1515 */
1516int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1517 struct snd_ctl_elem_value *ucontrol)
1518{
1519 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1520 int reg = kcontrol->private_value & 0xff;
1521 int reg2 = (kcontrol->private_value >> 24) & 0xff;
1522 int shift = (kcontrol->private_value >> 8) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001523 int max = (kcontrol->private_value >> 12) & 0xff;
1524 int mask = (1<<fls(max))-1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001525 int invert = (kcontrol->private_value >> 20) & 0x01;
1526
1527 ucontrol->value.integer.value[0] =
1528 (snd_soc_read(codec, reg) >> shift) & mask;
1529 ucontrol->value.integer.value[1] =
1530 (snd_soc_read(codec, reg2) >> shift) & mask;
1531 if (invert) {
1532 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001533 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001534 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001535 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001536 }
1537
1538 return 0;
1539}
1540EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1541
1542/**
1543 * snd_soc_put_volsw_2r - double mixer set callback
1544 * @kcontrol: mixer control
1545 * @uinfo: control element information
1546 *
1547 * Callback to set the value of a double mixer control that spans 2 registers.
1548 *
1549 * Returns 0 for success.
1550 */
1551int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1552 struct snd_ctl_elem_value *ucontrol)
1553{
1554 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1555 int reg = kcontrol->private_value & 0xff;
1556 int reg2 = (kcontrol->private_value >> 24) & 0xff;
1557 int shift = (kcontrol->private_value >> 8) & 0x0f;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001558 int max = (kcontrol->private_value >> 12) & 0xff;
1559 int mask = (1 << fls(max)) - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001560 int invert = (kcontrol->private_value >> 20) & 0x01;
1561 int err;
1562 unsigned short val, val2, val_mask;
1563
1564 val_mask = mask << shift;
1565 val = (ucontrol->value.integer.value[0] & mask);
1566 val2 = (ucontrol->value.integer.value[1] & mask);
1567
1568 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01001569 val = max - val;
1570 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001571 }
1572
1573 val = val << shift;
1574 val2 = val2 << shift;
1575
1576 if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
1577 return err;
1578
1579 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1580 return err;
1581}
1582EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1583
1584static int __devinit snd_soc_init(void)
1585{
1586 printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
1587 return platform_driver_register(&soc_driver);
1588}
1589
1590static void snd_soc_exit(void)
1591{
1592 platform_driver_unregister(&soc_driver);
1593}
1594
1595module_init(snd_soc_init);
1596module_exit(snd_soc_exit);
1597
1598/* Module information */
1599MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1600MODULE_DESCRIPTION("ALSA SoC Core");
1601MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02001602MODULE_ALIAS("platform:soc-audio");