blob: 805343fe903be180af6a9496606119c7ec594666 [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.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Marek Vasut474828a2009-07-22 13:01:03 +020034#include <sound/ac97_codec.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
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000042#define NAME_SIZE 32
43
Frank Mandarinodb2a4162006-10-06 18:31:09 +020044static DEFINE_MUTEX(pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020045static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
46
Mark Brown384c89e2008-12-03 17:34:03 +000047#ifdef CONFIG_DEBUG_FS
48static struct dentry *debugfs_root;
49#endif
50
Mark Brownc5af3a22008-11-28 13:29:45 +000051static DEFINE_MUTEX(client_mutex);
52static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000053static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000054static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000055static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000056
57static int snd_soc_register_card(struct snd_soc_card *card);
58static int snd_soc_unregister_card(struct snd_soc_card *card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000059static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
Mark Brownc5af3a22008-11-28 13:29:45 +000060
Frank Mandarinodb2a4162006-10-06 18:31:09 +020061/*
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
65 */
66static int pmdown_time = 5000;
67module_param(pmdown_time, int, 0);
68MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69
Liam Girdwood965ac422007-01-31 14:14:57 +010070/*
71 * This function forces any delayed work to be queued and run.
72 */
73static int run_delayed_work(struct delayed_work *dwork)
74{
75 int ret;
76
77 /* cancel any work waiting to be queued. */
78 ret = cancel_delayed_work(dwork);
79
80 /* if there was any work waiting then we run it now and
81 * wait for it's completion */
82 if (ret) {
83 schedule_delayed_work(dwork, 0);
84 flush_scheduled_work();
85 }
86 return ret;
87}
88
Mark Brown2624d5f2009-11-03 21:56:13 +000089/* codec register dump */
90static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
91{
Mark Brown5164d742010-07-14 16:14:33 +010092 int ret, i, step = 1, count = 0;
Mark Brown2624d5f2009-11-03 21:56:13 +000093
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000094 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +000095 return 0;
96
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000097 if (codec->driver->reg_cache_step)
98 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +000099
100 count += sprintf(buf, "%s registers\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000101 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
102 if (codec->driver->readable_register && !codec->driver->readable_register(i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000103 continue;
104
105 count += sprintf(buf + count, "%2x: ", i);
106 if (count >= PAGE_SIZE - 1)
107 break;
108
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000109 if (codec->driver->display_register) {
110 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000111 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100112 } else {
113 /* If the read fails it's almost certainly due to
114 * the register being volatile and the device being
115 * powered off.
116 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000117 ret = codec->driver->read(codec, i);
Mark Brown5164d742010-07-14 16:14:33 +0100118 if (ret >= 0)
119 count += snprintf(buf + count,
120 PAGE_SIZE - count,
121 "%4x", ret);
122 else
123 count += snprintf(buf + count,
124 PAGE_SIZE - count,
125 "<no data: %d>", ret);
126 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000127
128 if (count >= PAGE_SIZE - 1)
129 break;
130
131 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
132 if (count >= PAGE_SIZE - 1)
133 break;
134 }
135
136 /* Truncate count; min() would cause a warning */
137 if (count >= PAGE_SIZE)
138 count = PAGE_SIZE - 1;
139
140 return count;
141}
142static ssize_t codec_reg_show(struct device *dev,
143 struct device_attribute *attr, char *buf)
144{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000145 struct snd_soc_pcm_runtime *rtd =
146 container_of(dev, struct snd_soc_pcm_runtime, dev);
147
148 return soc_codec_reg_show(rtd->codec, buf);
Mark Brown2624d5f2009-11-03 21:56:13 +0000149}
150
151static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
152
Mark Browndbe21402010-02-12 11:37:24 +0000153static ssize_t pmdown_time_show(struct device *dev,
154 struct device_attribute *attr, char *buf)
155{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000156 struct snd_soc_pcm_runtime *rtd =
157 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Browndbe21402010-02-12 11:37:24 +0000158
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000159 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000160}
161
162static ssize_t pmdown_time_set(struct device *dev,
163 struct device_attribute *attr,
164 const char *buf, size_t count)
165{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000166 struct snd_soc_pcm_runtime *rtd =
167 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Brownc593b522010-10-27 20:11:17 -0700168 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000169
Mark Brownc593b522010-10-27 20:11:17 -0700170 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
171 if (ret)
172 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000173
174 return count;
175}
176
177static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
178
Mark Brown2624d5f2009-11-03 21:56:13 +0000179#ifdef CONFIG_DEBUG_FS
180static int codec_reg_open_file(struct inode *inode, struct file *file)
181{
182 file->private_data = inode->i_private;
183 return 0;
184}
185
186static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
187 size_t count, loff_t *ppos)
188{
189 ssize_t ret;
190 struct snd_soc_codec *codec = file->private_data;
191 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
192 if (!buf)
193 return -ENOMEM;
194 ret = soc_codec_reg_show(codec, buf);
195 if (ret >= 0)
196 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
197 kfree(buf);
198 return ret;
199}
200
201static ssize_t codec_reg_write_file(struct file *file,
202 const char __user *user_buf, size_t count, loff_t *ppos)
203{
204 char buf[32];
205 int buf_size;
206 char *start = buf;
207 unsigned long reg, value;
208 int step = 1;
209 struct snd_soc_codec *codec = file->private_data;
210
211 buf_size = min(count, (sizeof(buf)-1));
212 if (copy_from_user(buf, user_buf, buf_size))
213 return -EFAULT;
214 buf[buf_size] = 0;
215
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000216 if (codec->driver->reg_cache_step)
217 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000218
219 while (*start == ' ')
220 start++;
221 reg = simple_strtoul(start, &start, 16);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000222 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
Mark Brown2624d5f2009-11-03 21:56:13 +0000223 return -EINVAL;
224 while (*start == ' ')
225 start++;
226 if (strict_strtoul(start, 16, &value))
227 return -EINVAL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000228 codec->driver->write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000229 return buf_size;
230}
231
232static const struct file_operations codec_reg_fops = {
233 .open = codec_reg_open_file,
234 .read = codec_reg_read_file,
235 .write = codec_reg_write_file,
236};
237
238static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
239{
Mark Brown6ba6c9c2010-08-12 15:49:52 +0100240 codec->debugfs_codec_root = debugfs_create_dir(codec->name ,
Mark Brown2624d5f2009-11-03 21:56:13 +0000241 debugfs_root);
242 if (!codec->debugfs_codec_root) {
243 printk(KERN_WARNING
244 "ASoC: Failed to create codec debugfs directory\n");
245 return;
246 }
247
248 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
249 codec->debugfs_codec_root,
250 codec, &codec_reg_fops);
251 if (!codec->debugfs_reg)
252 printk(KERN_WARNING
253 "ASoC: Failed to create codec register debugfs file\n");
254
Axel Lin708fafb2010-08-27 10:34:27 +0800255 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
Mark Brown2624d5f2009-11-03 21:56:13 +0000256 codec->debugfs_codec_root,
257 &codec->pop_time);
258 if (!codec->debugfs_pop_time)
259 printk(KERN_WARNING
260 "Failed to create pop time debugfs file\n");
261
262 codec->debugfs_dapm = debugfs_create_dir("dapm",
263 codec->debugfs_codec_root);
264 if (!codec->debugfs_dapm)
265 printk(KERN_WARNING
266 "Failed to create DAPM debugfs directory\n");
267
268 snd_soc_dapm_debugfs_init(codec);
269}
270
271static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
272{
273 debugfs_remove_recursive(codec->debugfs_codec_root);
274}
275
Mark Brownc3c5a192010-09-15 18:15:14 +0100276static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
277 size_t count, loff_t *ppos)
278{
279 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100280 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100281 struct snd_soc_codec *codec;
282
283 if (!buf)
284 return -ENOMEM;
285
Mark Brown2b194f9d2010-10-13 10:52:16 +0100286 list_for_each_entry(codec, &codec_list, list) {
287 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
288 codec->name);
289 if (len >= 0)
290 ret += len;
291 if (ret > PAGE_SIZE) {
292 ret = PAGE_SIZE;
293 break;
294 }
295 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100296
297 if (ret >= 0)
298 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
299
300 kfree(buf);
301
302 return ret;
303}
304
305static const struct file_operations codec_list_fops = {
306 .read = codec_list_read_file,
307 .llseek = default_llseek,/* read accesses f_pos */
308};
309
Mark Brownf3208782010-09-15 18:19:07 +0100310static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
311 size_t count, loff_t *ppos)
312{
313 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100314 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100315 struct snd_soc_dai *dai;
316
317 if (!buf)
318 return -ENOMEM;
319
Mark Brown2b194f9d2010-10-13 10:52:16 +0100320 list_for_each_entry(dai, &dai_list, list) {
321 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
322 if (len >= 0)
323 ret += len;
324 if (ret > PAGE_SIZE) {
325 ret = PAGE_SIZE;
326 break;
327 }
328 }
Mark Brownf3208782010-09-15 18:19:07 +0100329
Mark Brown2b194f9d2010-10-13 10:52:16 +0100330 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100331
332 kfree(buf);
333
334 return ret;
335}
336
337static const struct file_operations dai_list_fops = {
338 .read = dai_list_read_file,
339 .llseek = default_llseek,/* read accesses f_pos */
340};
341
Mark Brown19c7ac22010-09-15 18:22:40 +0100342static ssize_t platform_list_read_file(struct file *file,
343 char __user *user_buf,
344 size_t count, loff_t *ppos)
345{
346 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100347 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100348 struct snd_soc_platform *platform;
349
350 if (!buf)
351 return -ENOMEM;
352
Mark Brown2b194f9d2010-10-13 10:52:16 +0100353 list_for_each_entry(platform, &platform_list, list) {
354 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
355 platform->name);
356 if (len >= 0)
357 ret += len;
358 if (ret > PAGE_SIZE) {
359 ret = PAGE_SIZE;
360 break;
361 }
362 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100363
Mark Brown2b194f9d2010-10-13 10:52:16 +0100364 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100365
366 kfree(buf);
367
368 return ret;
369}
370
371static const struct file_operations platform_list_fops = {
372 .read = platform_list_read_file,
373 .llseek = default_llseek,/* read accesses f_pos */
374};
375
Mark Brown2624d5f2009-11-03 21:56:13 +0000376#else
377
378static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
379{
380}
381
382static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
383{
384}
385#endif
386
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200387#ifdef CONFIG_SND_SOC_AC97_BUS
388/* unregister ac97 codec */
389static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
390{
391 if (codec->ac97->dev.bus)
392 device_unregister(&codec->ac97->dev);
393 return 0;
394}
395
396/* stop no dev release warning */
397static void soc_ac97_device_release(struct device *dev){}
398
399/* register ac97 codec to bus */
400static int soc_ac97_dev_register(struct snd_soc_codec *codec)
401{
402 int err;
403
404 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100405 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200406 codec->ac97->dev.release = soc_ac97_device_release;
407
Kay Sieversbb072bf2008-11-02 03:50:35 +0100408 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000409 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200410 err = device_register(&codec->ac97->dev);
411 if (err < 0) {
412 snd_printk(KERN_ERR "Can't register ac97 bus\n");
413 codec->ac97->dev.bus = NULL;
414 return err;
415 }
416 return 0;
417}
418#endif
419
Mark Brown06f409d2009-04-07 18:10:13 +0100420static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
421{
422 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000423 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
424 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100425 int ret;
426
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000427 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
428 rtd->dai_link->symmetric_rates) {
429 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
430 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100431
432 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
433 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000434 rtd->rate,
435 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100436 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000437 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100438 "Unable to apply rate symmetry constraint: %d\n", ret);
439 return ret;
440 }
441 }
442
443 return 0;
444}
445
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200446/*
447 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
448 * then initialized and any private data can be allocated. This also calls
449 * startup for the cpu DAI, platform, machine and codec DAI.
450 */
451static int soc_pcm_open(struct snd_pcm_substream *substream)
452{
453 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200454 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000455 struct snd_soc_platform *platform = rtd->platform;
456 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
457 struct snd_soc_dai *codec_dai = rtd->codec_dai;
458 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
459 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200460 int ret = 0;
461
462 mutex_lock(&pcm_mutex);
463
464 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000465 if (cpu_dai->driver->ops->startup) {
466 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200467 if (ret < 0) {
468 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100469 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200470 goto out;
471 }
472 }
473
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000474 if (platform->driver->ops->open) {
475 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200476 if (ret < 0) {
477 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
478 goto platform_err;
479 }
480 }
481
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000482 if (codec_dai->driver->ops->startup) {
483 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100484 if (ret < 0) {
485 printk(KERN_ERR "asoc: can't open codec %s\n",
486 codec_dai->name);
487 goto codec_dai_err;
488 }
489 }
490
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000491 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
492 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200493 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000494 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200495 goto machine_err;
496 }
497 }
498
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200499 /* Check that the codec and cpu DAI's are compatible */
500 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
501 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000502 max(codec_dai_drv->playback.rate_min,
503 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200504 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000505 min(codec_dai_drv->playback.rate_max,
506 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200507 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508 max(codec_dai_drv->playback.channels_min,
509 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200510 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000511 min(codec_dai_drv->playback.channels_max,
512 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100513 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000514 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100515 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000516 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
517 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900518 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 runtime->hw.rates |= cpu_dai_drv->playback.rates;
520 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900521 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200523 } else {
524 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000525 max(codec_dai_drv->capture.rate_min,
526 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200527 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000528 min(codec_dai_drv->capture.rate_max,
529 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200530 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531 max(codec_dai_drv->capture.channels_min,
532 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200533 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000534 min(codec_dai_drv->capture.channels_max,
535 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100536 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000537 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100538 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000539 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
540 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900541 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000542 runtime->hw.rates |= cpu_dai_drv->capture.rates;
543 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900544 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000545 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200546 }
547
548 snd_pcm_limit_hw_rates(runtime);
549 if (!runtime->hw.rates) {
550 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100551 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900552 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200553 }
554 if (!runtime->hw.formats) {
555 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100556 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900557 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200558 }
559 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
560 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900562 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200563 }
564
Mark Brown06f409d2009-04-07 18:10:13 +0100565 /* Symmetry only applies if we've already got an active stream. */
566 if (cpu_dai->active || codec_dai->active) {
567 ret = soc_pcm_apply_symmetry(substream);
568 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900569 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100570 }
571
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 pr_debug("asoc: %s <-> %s info:\n",
573 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100574 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
575 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
576 runtime->hw.channels_max);
577 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
578 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200579
Jassi Brar14dc5732010-02-26 09:12:32 +0900580 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 cpu_dai->playback_active++;
582 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900583 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 cpu_dai->capture_active++;
585 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900586 }
587 cpu_dai->active++;
588 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000589 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200590 mutex_unlock(&pcm_mutex);
591 return 0;
592
Jassi Brarbb1c0472010-02-25 11:24:53 +0900593config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000594 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
595 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596
Jassi Brarbb1c0472010-02-25 11:24:53 +0900597machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 if (codec_dai->driver->ops->shutdown)
599 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900600
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100601codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000602 if (platform->driver->ops->close)
603 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200604
605platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000606 if (cpu_dai->driver->ops->shutdown)
607 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200608out:
609 mutex_unlock(&pcm_mutex);
610 return ret;
611}
612
613/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200614 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200615 * This is to ensure there are no pops or clicks in between any music tracks
616 * due to DAPM power cycling.
617 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100618static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200619{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 struct snd_soc_pcm_runtime *rtd =
621 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
622 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200623
624 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200625
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
627 codec_dai->driver->playback.stream_name,
628 codec_dai->playback_active ? "active" : "inactive",
629 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200630
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631 /* are we waiting on this codec DAI stream */
632 if (codec_dai->pop_wait == 1) {
633 codec_dai->pop_wait = 0;
634 snd_soc_dapm_stream_event(rtd,
635 codec_dai->driver->playback.stream_name,
636 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200637 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000638
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200639 mutex_unlock(&pcm_mutex);
640}
641
642/*
643 * Called by ALSA when a PCM substream is closed. Private data can be
644 * freed here. The cpu DAI, codec DAI, machine and platform are also
645 * shutdown.
646 */
647static int soc_codec_close(struct snd_pcm_substream *substream)
648{
649 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000650 struct snd_soc_platform *platform = rtd->platform;
651 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
652 struct snd_soc_dai *codec_dai = rtd->codec_dai;
653 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654
655 mutex_lock(&pcm_mutex);
656
Jassi Brar14dc5732010-02-26 09:12:32 +0900657 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 cpu_dai->playback_active--;
659 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900660 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 cpu_dai->capture_active--;
662 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200663 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900664
665 cpu_dai->active--;
666 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200667 codec->active--;
668
Mark Brown6010b2d2008-09-06 18:33:24 +0100669 /* Muting the DAC suppresses artifacts caused during digital
670 * shutdown, for example from stopping clocks.
671 */
672 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
673 snd_soc_dai_digital_mute(codec_dai, 1);
674
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000675 if (cpu_dai->driver->ops->shutdown)
676 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200677
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000678 if (codec_dai->driver->ops->shutdown)
679 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200680
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000681 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
682 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200683
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000684 if (platform->driver->ops->close)
685 platform->driver->ops->close(substream);
686 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200687
688 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
689 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100690 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000691 schedule_delayed_work(&rtd->delayed_work,
692 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200693 } else {
694 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000695 snd_soc_dapm_stream_event(rtd,
696 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100697 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200698 }
699
700 mutex_unlock(&pcm_mutex);
701 return 0;
702}
703
704/*
705 * Called by ALSA when the PCM substream is prepared, can set format, sample
706 * rate, etc. This function is non atomic and can be called multiple times,
707 * it can refer to the runtime info.
708 */
709static int soc_pcm_prepare(struct snd_pcm_substream *substream)
710{
711 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000712 struct snd_soc_platform *platform = rtd->platform;
713 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
714 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200715 int ret = 0;
716
717 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100718
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000719 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
720 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100721 if (ret < 0) {
722 printk(KERN_ERR "asoc: machine prepare error\n");
723 goto out;
724 }
725 }
726
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000727 if (platform->driver->ops->prepare) {
728 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200729 if (ret < 0) {
730 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200731 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200732 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200733 }
734
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000735 if (codec_dai->driver->ops->prepare) {
736 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200737 if (ret < 0) {
738 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200739 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200740 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200741 }
742
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743 if (cpu_dai->driver->ops->prepare) {
744 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100745 if (ret < 0) {
746 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
747 goto out;
748 }
749 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750
Mark Brownd45f6212008-10-14 13:58:36 +0100751 /* cancel any delayed stream shutdown that is pending */
752 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
753 codec_dai->pop_wait) {
754 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000755 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100756 }
757
Mark Brown452c5ea2009-05-17 21:41:23 +0100758 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000759 snd_soc_dapm_stream_event(rtd,
760 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100761 SND_SOC_DAPM_STREAM_START);
762 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000763 snd_soc_dapm_stream_event(rtd,
764 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100765 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100766
Mark Brown452c5ea2009-05-17 21:41:23 +0100767 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200768
769out:
770 mutex_unlock(&pcm_mutex);
771 return ret;
772}
773
774/*
775 * Called by ALSA when the hardware params are set by application. This
776 * function can also be called multiple times and can allocate buffers
777 * (using snd_pcm_lib_* ). It's non-atomic.
778 */
779static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
780 struct snd_pcm_hw_params *params)
781{
782 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000783 struct snd_soc_platform *platform = rtd->platform;
784 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
785 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200786 int ret = 0;
787
788 mutex_lock(&pcm_mutex);
789
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000790 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
791 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200792 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100793 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200794 goto out;
795 }
796 }
797
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000798 if (codec_dai->driver->ops->hw_params) {
799 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100800 if (ret < 0) {
801 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
802 codec_dai->name);
803 goto codec_err;
804 }
805 }
806
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000807 if (cpu_dai->driver->ops->hw_params) {
808 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200809 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200810 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100811 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200812 goto interface_err;
813 }
814 }
815
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000816 if (platform->driver->ops->hw_params) {
817 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200818 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200819 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200820 platform->name);
821 goto platform_err;
822 }
823 }
824
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000825 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100826
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200827out:
828 mutex_unlock(&pcm_mutex);
829 return ret;
830
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200831platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000832 if (cpu_dai->driver->ops->hw_free)
833 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200834
835interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000836 if (codec_dai->driver->ops->hw_free)
837 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100838
839codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000840 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
841 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200842
843 mutex_unlock(&pcm_mutex);
844 return ret;
845}
846
847/*
848 * Free's resources allocated by hw_params, can be called multiple times
849 */
850static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
851{
852 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000853 struct snd_soc_platform *platform = rtd->platform;
854 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
855 struct snd_soc_dai *codec_dai = rtd->codec_dai;
856 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200857
858 mutex_lock(&pcm_mutex);
859
860 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100861 if (!codec->active)
862 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200863
864 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000865 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
866 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200867
868 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000869 if (platform->driver->ops->hw_free)
870 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200871
872 /* now free hw params for the DAI's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000873 if (codec_dai->driver->ops->hw_free)
874 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200875
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000876 if (cpu_dai->driver->ops->hw_free)
877 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200878
879 mutex_unlock(&pcm_mutex);
880 return 0;
881}
882
883static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
884{
885 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000886 struct snd_soc_platform *platform = rtd->platform;
887 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
888 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200889 int ret;
890
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000891 if (codec_dai->driver->ops->trigger) {
892 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200893 if (ret < 0)
894 return ret;
895 }
896
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000897 if (platform->driver->ops->trigger) {
898 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200899 if (ret < 0)
900 return ret;
901 }
902
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000903 if (cpu_dai->driver->ops->trigger) {
904 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200905 if (ret < 0)
906 return ret;
907 }
908 return 0;
909}
910
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200911/*
912 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200913 * If cpu_dai, codec_dai, platform driver has the delay callback, than
914 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200915 */
916static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
917{
918 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000919 struct snd_soc_platform *platform = rtd->platform;
920 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
921 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200922 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200923 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200924 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200925
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000926 if (platform->driver->ops->pointer)
927 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200928
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000929 if (cpu_dai->driver->ops->delay)
930 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200931
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000932 if (codec_dai->driver->ops->delay)
933 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200934
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000935 if (platform->driver->delay)
936 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200937
938 runtime->delay = delay;
939
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200940 return offset;
941}
942
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200943/* ASoC PCM operations */
944static struct snd_pcm_ops soc_pcm_ops = {
945 .open = soc_pcm_open,
946 .close = soc_codec_close,
947 .hw_params = soc_pcm_hw_params,
948 .hw_free = soc_pcm_hw_free,
949 .prepare = soc_pcm_prepare,
950 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200951 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200952};
953
954#ifdef CONFIG_PM
955/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100956static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200957{
Mark Brown416356f2009-06-30 19:05:15 +0100958 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000959 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200960 int i;
961
Daniel Macke3509ff2009-06-03 17:44:49 +0200962 /* If the initialization of this soc device failed, there is no codec
963 * associated with it. Just bail out in this case.
964 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000965 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200966 return 0;
967
Andy Green6ed25972008-06-13 16:24:05 +0100968 /* Due to the resume being scheduled into a workqueue we could
969 * suspend before that's finished - wait for it to complete.
970 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000971 snd_power_lock(card->snd_card);
972 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
973 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100974
975 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000976 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100977
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200978 /* mute any active DAC's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000979 for (i = 0; i < card->num_rtd; i++) {
980 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
981 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100982
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000983 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100984 continue;
985
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000986 if (drv->ops->digital_mute && dai->playback_active)
987 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200988 }
989
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100990 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000991 for (i = 0; i < card->num_rtd; i++) {
992 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100993 continue;
994
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000995 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100996 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100997
Mark Brown87506542008-11-18 20:50:34 +0000998 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100999 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001000
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001001 for (i = 0; i < card->num_rtd; i++) {
1002 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1003 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001004
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001005 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001006 continue;
1007
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001008 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1009 cpu_dai->driver->suspend(cpu_dai);
1010 if (platform->driver->suspend && !platform->suspended) {
1011 platform->driver->suspend(cpu_dai);
1012 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +01001013 }
1014 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001015
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001016 /* close any waiting streams and save state */
1017 for (i = 0; i < card->num_rtd; i++) {
1018 run_delayed_work(&card->rtd[i].delayed_work);
1019 card->rtd[i].codec->suspend_bias_level = card->rtd[i].codec->bias_level;
1020 }
Mark Brown3efab7d2010-05-09 13:25:43 +01001021
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001022 for (i = 0; i < card->num_rtd; i++) {
1023 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1024
1025 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001026 continue;
1027
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001028 if (driver->playback.stream_name != NULL)
1029 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1030 SND_SOC_DAPM_STREAM_SUSPEND);
1031
1032 if (driver->capture.stream_name != NULL)
1033 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1034 SND_SOC_DAPM_STREAM_SUSPEND);
1035 }
1036
1037 /* suspend all CODECs */
1038 for (i = 0; i < card->num_rtd; i++) {
1039 struct snd_soc_codec *codec = card->rtd[i].codec;
1040 /* If there are paths active then the CODEC will be held with
1041 * bias _ON and should not be suspended. */
1042 if (!codec->suspended && codec->driver->suspend) {
1043 switch (codec->bias_level) {
1044 case SND_SOC_BIAS_STANDBY:
1045 case SND_SOC_BIAS_OFF:
1046 codec->driver->suspend(codec, PMSG_SUSPEND);
1047 codec->suspended = 1;
1048 break;
1049 default:
1050 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1051 break;
1052 }
1053 }
1054 }
1055
1056 for (i = 0; i < card->num_rtd; i++) {
1057 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1058
1059 if (card->rtd[i].dai_link->ignore_suspend)
1060 continue;
1061
1062 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1063 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001064 }
1065
Mark Brown87506542008-11-18 20:50:34 +00001066 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +01001067 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001068
1069 return 0;
1070}
1071
Andy Green6ed25972008-06-13 16:24:05 +01001072/* deferred resume work, so resume can complete before we finished
1073 * setting our codec back up, which can be very slow on I2C
1074 */
1075static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001076{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001077 struct snd_soc_card *card =
1078 container_of(work, struct snd_soc_card, deferred_resume_work);
1079 struct platform_device *pdev = to_platform_device(card->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001080 int i;
1081
Andy Green6ed25972008-06-13 16:24:05 +01001082 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1083 * so userspace apps are blocked from touching us
1084 */
1085
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001086 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001087
Mark Brown99497882010-05-07 20:24:05 +01001088 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001089 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +01001090
Mark Brown87506542008-11-18 20:50:34 +00001091 if (card->resume_pre)
1092 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001093
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001094 /* resume AC97 DAIs */
1095 for (i = 0; i < card->num_rtd; i++) {
1096 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001097
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001098 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001099 continue;
1100
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001101 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1102 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001103 }
1104
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001105 for (i = 0; i < card->num_rtd; i++) {
1106 struct snd_soc_codec *codec = card->rtd[i].codec;
1107 /* If the CODEC was idle over suspend then it will have been
1108 * left with bias OFF or STANDBY and suspended so we must now
1109 * resume. Otherwise the suspend was suppressed.
1110 */
1111 if (codec->driver->resume && codec->suspended) {
1112 switch (codec->bias_level) {
1113 case SND_SOC_BIAS_STANDBY:
1114 case SND_SOC_BIAS_OFF:
1115 codec->driver->resume(codec);
1116 codec->suspended = 0;
1117 break;
1118 default:
1119 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1120 break;
1121 }
Mark Brown1547aba2010-05-07 21:11:40 +01001122 }
1123 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001124
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001125 for (i = 0; i < card->num_rtd; i++) {
1126 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001127
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001128 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001129 continue;
1130
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001131 if (driver->playback.stream_name != NULL)
1132 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001133 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001134
1135 if (driver->capture.stream_name != NULL)
1136 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001137 SND_SOC_DAPM_STREAM_RESUME);
1138 }
1139
Mark Brown3ff3f642008-05-19 12:32:25 +02001140 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001141 for (i = 0; i < card->num_rtd; i++) {
1142 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1143 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001144
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001145 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001146 continue;
1147
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001148 if (drv->ops->digital_mute && dai->playback_active)
1149 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001150 }
1151
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001152 for (i = 0; i < card->num_rtd; i++) {
1153 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1154 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001156 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001157 continue;
1158
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001159 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1160 cpu_dai->driver->resume(cpu_dai);
1161 if (platform->driver->resume && platform->suspended) {
1162 platform->driver->resume(cpu_dai);
1163 platform->suspended = 0;
1164 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001165 }
1166
Mark Brown87506542008-11-18 20:50:34 +00001167 if (card->resume_post)
1168 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001169
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001170 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001171
1172 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001173 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001174}
1175
1176/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001177static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001178{
Mark Brown416356f2009-06-30 19:05:15 +01001179 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001180 struct snd_soc_card *card = platform_get_drvdata(pdev);
1181 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001182
Mark Brown64ab9ba2009-03-31 11:27:03 +01001183 /* AC97 devices might have other drivers hanging off them so
1184 * need to resume immediately. Other drivers don't have that
1185 * problem and may take a substantial amount of time to resume
1186 * due to I/O costs and anti-pop so handle them out of line.
1187 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001188 for (i = 0; i < card->num_rtd; i++) {
1189 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1190 if (cpu_dai->driver->ac97_control) {
1191 dev_dbg(dev, "Resuming AC97 immediately\n");
1192 soc_resume_deferred(&card->deferred_resume_work);
1193 } else {
1194 dev_dbg(dev, "Scheduling resume work\n");
1195 if (!schedule_work(&card->deferred_resume_work))
1196 dev_err(dev, "resume work item may be lost\n");
1197 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001198 }
Andy Green6ed25972008-06-13 16:24:05 +01001199
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001200 return 0;
1201}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001202#else
1203#define soc_suspend NULL
1204#define soc_resume NULL
1205#endif
1206
Barry Song02a06d32009-10-16 18:13:38 +08001207static struct snd_soc_dai_ops null_dai_ops = {
1208};
1209
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001210static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001211{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001212 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1213 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001214 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001215 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001216 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001217
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001218 if (rtd->complete)
1219 return 1;
1220 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001221
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001222 /* do we already have the CPU DAI for this link ? */
1223 if (rtd->cpu_dai) {
1224 goto find_codec;
1225 }
1226 /* no, then find CPU DAI from registered DAIs*/
1227 list_for_each_entry(cpu_dai, &dai_list, list) {
1228 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1229
1230 if (!try_module_get(cpu_dai->dev->driver->owner))
1231 return -ENODEV;
1232
1233 rtd->cpu_dai = cpu_dai;
1234 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001235 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001236 }
1237 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1238 dai_link->cpu_dai_name);
1239
1240find_codec:
1241 /* do we already have the CODEC for this link ? */
1242 if (rtd->codec) {
1243 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001244 }
1245
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001246 /* no, then find CODEC from registered CODECs*/
1247 list_for_each_entry(codec, &codec_list, list) {
1248 if (!strcmp(codec->name, dai_link->codec_name)) {
1249 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001250
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001251 if (!try_module_get(codec->dev->driver->owner))
1252 return -ENODEV;
Mark Brown435c5e22008-12-04 15:32:53 +00001253
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001254 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1255 list_for_each_entry(codec_dai, &dai_list, list) {
1256 if (codec->dev == codec_dai->dev &&
1257 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1258 rtd->codec_dai = codec_dai;
1259 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001260 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001261 }
1262 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1263 dai_link->codec_dai_name);
1264
1265 goto find_platform;
1266 }
1267 }
1268 dev_dbg(card->dev, "CODEC %s not registered\n",
1269 dai_link->codec_name);
1270
1271find_platform:
1272 /* do we already have the CODEC DAI for this link ? */
1273 if (rtd->platform) {
1274 goto out;
1275 }
1276 /* no, then find CPU DAI from registered DAIs*/
1277 list_for_each_entry(platform, &platform_list, list) {
1278 if (!strcmp(platform->name, dai_link->platform_name)) {
1279
1280 if (!try_module_get(platform->dev->driver->owner))
1281 return -ENODEV;
1282
1283 rtd->platform = platform;
1284 goto out;
1285 }
1286 }
1287
1288 dev_dbg(card->dev, "platform %s not registered\n",
1289 dai_link->platform_name);
1290 return 0;
1291
1292out:
1293 /* mark rtd as complete if we found all 4 of our client devices */
1294 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1295 rtd->complete = 1;
1296 card->num_rtd++;
1297 }
1298 return 1;
1299}
1300
1301static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1302{
1303 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1304 struct snd_soc_codec *codec = rtd->codec;
1305 struct snd_soc_platform *platform = rtd->platform;
1306 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1307 int err;
1308
1309 /* unregister the rtd device */
1310 if (rtd->dev_registered) {
1311 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1312 device_unregister(&rtd->dev);
1313 rtd->dev_registered = 0;
1314 }
1315
1316 /* remove the CODEC DAI */
1317 if (codec_dai && codec_dai->probed) {
1318 if (codec_dai->driver->remove) {
1319 err = codec_dai->driver->remove(codec_dai);
1320 if (err < 0)
1321 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1322 }
1323 codec_dai->probed = 0;
1324 list_del(&codec_dai->card_list);
1325 }
1326
1327 /* remove the platform */
1328 if (platform && platform->probed) {
1329 if (platform->driver->remove) {
1330 err = platform->driver->remove(platform);
1331 if (err < 0)
1332 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1333 }
1334 platform->probed = 0;
1335 list_del(&platform->card_list);
1336 module_put(platform->dev->driver->owner);
1337 }
1338
1339 /* remove the CODEC */
1340 if (codec && codec->probed) {
1341 if (codec->driver->remove) {
1342 err = codec->driver->remove(codec);
1343 if (err < 0)
1344 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1345 }
1346
1347 /* Make sure all DAPM widgets are freed */
1348 snd_soc_dapm_free(codec);
1349
1350 soc_cleanup_codec_debugfs(codec);
1351 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1352 codec->probed = 0;
1353 list_del(&codec->card_list);
1354 module_put(codec->dev->driver->owner);
1355 }
1356
1357 /* remove the cpu_dai */
1358 if (cpu_dai && cpu_dai->probed) {
1359 if (cpu_dai->driver->remove) {
1360 err = cpu_dai->driver->remove(cpu_dai);
1361 if (err < 0)
1362 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1363 }
1364 cpu_dai->probed = 0;
1365 list_del(&cpu_dai->card_list);
1366 module_put(cpu_dai->dev->driver->owner);
1367 }
1368}
1369
1370static void rtd_release(struct device *dev) {}
1371
1372static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1373{
1374 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1375 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1376 struct snd_soc_codec *codec = rtd->codec;
1377 struct snd_soc_platform *platform = rtd->platform;
1378 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1379 int ret;
1380
1381 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1382
1383 /* config components */
1384 codec_dai->codec = codec;
1385 codec->card = card;
1386 cpu_dai->platform = platform;
1387 rtd->card = card;
1388 rtd->dev.parent = card->dev;
1389 codec_dai->card = card;
1390 cpu_dai->card = card;
1391
1392 /* set default power off timeout */
1393 rtd->pmdown_time = pmdown_time;
1394
1395 /* probe the cpu_dai */
1396 if (!cpu_dai->probed) {
1397 if (cpu_dai->driver->probe) {
1398 ret = cpu_dai->driver->probe(cpu_dai);
1399 if (ret < 0) {
1400 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1401 cpu_dai->name);
1402 return ret;
1403 }
1404 }
1405 cpu_dai->probed = 1;
1406 /* mark cpu_dai as probed and add to card cpu_dai list */
1407 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1408 }
1409
1410 /* probe the CODEC */
1411 if (!codec->probed) {
1412 if (codec->driver->probe) {
1413 ret = codec->driver->probe(codec);
1414 if (ret < 0) {
1415 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1416 codec->name);
1417 return ret;
1418 }
1419 }
Mark Brown13cb61f2010-08-12 15:44:04 +01001420
1421 soc_init_codec_debugfs(codec);
1422
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001423 /* mark codec as probed and add to card codec list */
1424 codec->probed = 1;
1425 list_add(&codec->card_list, &card->codec_dev_list);
1426 }
1427
1428 /* probe the platform */
1429 if (!platform->probed) {
1430 if (platform->driver->probe) {
1431 ret = platform->driver->probe(platform);
1432 if (ret < 0) {
1433 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1434 platform->name);
1435 return ret;
1436 }
1437 }
1438 /* mark platform as probed and add to card platform list */
1439 platform->probed = 1;
1440 list_add(&platform->card_list, &card->platform_dev_list);
1441 }
1442
1443 /* probe the CODEC DAI */
1444 if (!codec_dai->probed) {
1445 if (codec_dai->driver->probe) {
1446 ret = codec_dai->driver->probe(codec_dai);
1447 if (ret < 0) {
1448 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1449 codec_dai->name);
1450 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001451 }
1452 }
1453
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001454 /* mark cpu_dai as probed and add to card cpu_dai list */
1455 codec_dai->probed = 1;
1456 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001457 }
1458
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001459 /* DAPM dai link stream work */
1460 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1461
1462 /* now that all clients have probed, initialise the DAI link */
1463 if (dai_link->init) {
1464 ret = dai_link->init(rtd);
1465 if (ret < 0) {
1466 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1467 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001468 }
1469 }
1470
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001471 /* Make sure all DAPM widgets are instantiated */
1472 snd_soc_dapm_new_widgets(codec);
1473 snd_soc_dapm_sync(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001474
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001475 /* register the rtd device */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001476 rtd->dev.release = rtd_release;
1477 rtd->dev.init_name = dai_link->name;
1478 ret = device_register(&rtd->dev);
1479 if (ret < 0) {
1480 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1481 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001482 }
1483
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001484 rtd->dev_registered = 1;
1485 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1486 if (ret < 0)
1487 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1488
1489 /* add DAPM sysfs entries for this codec */
1490 ret = snd_soc_dapm_sys_add(&rtd->dev);
1491 if (ret < 0)
1492 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1493
1494 /* add codec sysfs entries */
1495 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1496 if (ret < 0)
1497 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1498
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001499 /* create the pcm */
1500 ret = soc_new_pcm(rtd, num);
1501 if (ret < 0) {
1502 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1503 return ret;
1504 }
1505
1506 /* add platform data for AC97 devices */
1507 if (rtd->codec_dai->driver->ac97_control)
1508 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1509
1510 return 0;
1511}
1512
1513#ifdef CONFIG_SND_SOC_AC97_BUS
1514static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1515{
1516 int ret;
1517
1518 /* Only instantiate AC97 if not already done by the adaptor
1519 * for the generic AC97 subsystem.
1520 */
1521 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001522 /*
1523 * It is possible that the AC97 device is already registered to
1524 * the device subsystem. This happens when the device is created
1525 * via snd_ac97_mixer(). Currently only SoC codec that does so
1526 * is the generic AC97 glue but others migh emerge.
1527 *
1528 * In those cases we don't try to register the device again.
1529 */
1530 if (!rtd->codec->ac97_created)
1531 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001532
1533 ret = soc_ac97_dev_register(rtd->codec);
1534 if (ret < 0) {
1535 printk(KERN_ERR "asoc: AC97 device register failed\n");
1536 return ret;
1537 }
1538
1539 rtd->codec->ac97_registered = 1;
1540 }
1541 return 0;
1542}
1543
1544static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1545{
1546 if (codec->ac97_registered) {
1547 soc_ac97_dev_unregister(codec);
1548 codec->ac97_registered = 0;
1549 }
1550}
1551#endif
1552
1553static void snd_soc_instantiate_card(struct snd_soc_card *card)
1554{
1555 struct platform_device *pdev = to_platform_device(card->dev);
1556 int ret, i;
1557
1558 mutex_lock(&card->mutex);
1559
1560 if (card->instantiated) {
1561 mutex_unlock(&card->mutex);
1562 return;
1563 }
1564
1565 /* bind DAIs */
1566 for (i = 0; i < card->num_links; i++)
1567 soc_bind_dai_link(card, i);
1568
1569 /* bind completed ? */
1570 if (card->num_rtd != card->num_links) {
1571 mutex_unlock(&card->mutex);
1572 return;
1573 }
1574
1575 /* card bind complete so register a sound card */
1576 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1577 card->owner, 0, &card->snd_card);
1578 if (ret < 0) {
1579 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1580 card->name);
1581 mutex_unlock(&card->mutex);
1582 return;
1583 }
1584 card->snd_card->dev = card->dev;
1585
Randy Dunlap1301a962008-06-17 19:19:34 +01001586#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001587 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001588 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001589#endif
Andy Green6ed25972008-06-13 16:24:05 +01001590
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001591 /* initialise the sound card only once */
1592 if (card->probe) {
1593 ret = card->probe(pdev);
1594 if (ret < 0)
1595 goto card_probe_error;
1596 }
1597
Mark Brownfe3e78e2009-11-03 22:13:13 +00001598 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001599 ret = soc_probe_dai_link(card, i);
1600 if (ret < 0) {
Mark Brown321de0d2010-09-21 15:09:37 +01001601 pr_err("asoc: failed to instantiate card %s: %d\n",
1602 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001603 goto probe_dai_err;
1604 }
1605 }
1606
1607 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1608 "%s", card->name);
1609 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1610 "%s", card->name);
1611
1612 ret = snd_card_register(card->snd_card);
1613 if (ret < 0) {
1614 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1615 goto probe_dai_err;
1616 }
1617
1618#ifdef CONFIG_SND_SOC_AC97_BUS
1619 /* register any AC97 codecs */
1620 for (i = 0; i < card->num_rtd; i++) {
1621 ret = soc_register_ac97_dai_link(&card->rtd[i]);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001622 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001623 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1624 goto probe_dai_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001625 }
1626 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001627#endif
1628
Mark Brown435c5e22008-12-04 15:32:53 +00001629 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001630 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001631 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001632
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001633probe_dai_err:
1634 for (i = 0; i < card->num_links; i++)
1635 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001636
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001637card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001638 if (card->remove)
1639 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001640
1641 snd_card_free(card->snd_card);
1642
1643 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001644}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001645
Mark Brown435c5e22008-12-04 15:32:53 +00001646/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001647 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001648 * client_mutex.
1649 */
1650static void snd_soc_instantiate_cards(void)
1651{
1652 struct snd_soc_card *card;
1653 list_for_each_entry(card, &card_list, list)
1654 snd_soc_instantiate_card(card);
1655}
1656
1657/* probes a new socdev */
1658static int soc_probe(struct platform_device *pdev)
1659{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001660 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001661 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001662
1663 /* Bodge while we unpick instantiation */
1664 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001665 INIT_LIST_HEAD(&card->dai_dev_list);
1666 INIT_LIST_HEAD(&card->codec_dev_list);
1667 INIT_LIST_HEAD(&card->platform_dev_list);
1668
Mark Brown435c5e22008-12-04 15:32:53 +00001669 ret = snd_soc_register_card(card);
1670 if (ret != 0) {
1671 dev_err(&pdev->dev, "Failed to register card\n");
1672 return ret;
1673 }
1674
1675 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001676}
1677
1678/* removes a socdev */
1679static int soc_remove(struct platform_device *pdev)
1680{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001681 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001682 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001683
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001684 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001685
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001686 /* make sure any delayed work runs */
1687 for (i = 0; i < card->num_rtd; i++) {
1688 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1689 run_delayed_work(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001690 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001691
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001692 /* remove and free each DAI */
1693 for (i = 0; i < card->num_rtd; i++)
1694 soc_remove_dai_link(card, i);
1695
1696 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001697 if (card->remove)
1698 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001699
1700 kfree(card->rtd);
1701 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001702 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001703 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001704 return 0;
1705}
1706
Mark Brown416356f2009-06-30 19:05:15 +01001707static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001708{
Mark Brown416356f2009-06-30 19:05:15 +01001709 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001710 struct snd_soc_card *card = platform_get_drvdata(pdev);
1711 int i;
Mark Brown51737472009-06-22 13:16:51 +01001712
1713 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001714 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001715
1716 /* Flush out pmdown_time work - we actually do want to run it
1717 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001718 for (i = 0; i < card->num_rtd; i++) {
1719 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1720 run_delayed_work(&rtd->delayed_work);
1721 }
Mark Brown51737472009-06-22 13:16:51 +01001722
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001723 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001724
1725 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001726}
1727
Alexey Dobriyan47145212009-12-14 18:00:08 -08001728static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001729 .suspend = soc_suspend,
1730 .resume = soc_resume,
1731 .poweroff = soc_poweroff,
1732};
1733
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001734/* ASoC platform driver */
1735static struct platform_driver soc_driver = {
1736 .driver = {
1737 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001738 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001739 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001740 },
1741 .probe = soc_probe,
1742 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001743};
1744
1745/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001746static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001747{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001748 struct snd_soc_codec *codec = rtd->codec;
1749 struct snd_soc_platform *platform = rtd->platform;
1750 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1751 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001752 struct snd_pcm *pcm;
1753 char new_name[64];
1754 int ret = 0, playback = 0, capture = 0;
1755
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001756 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001757 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001758 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001759
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001760 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001761 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001762 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001763 capture = 1;
1764
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001765 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1766 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1767 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001768 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001769 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001770 return ret;
1771 }
1772
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001773 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001774 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001775 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1776 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1777 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1778 soc_pcm_ops.copy = platform->driver->ops->copy;
1779 soc_pcm_ops.silence = platform->driver->ops->silence;
1780 soc_pcm_ops.ack = platform->driver->ops->ack;
1781 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001782
1783 if (playback)
1784 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1785
1786 if (capture)
1787 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1788
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001789 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001790 if (ret < 0) {
1791 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001792 return ret;
1793 }
1794
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001795 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001796 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1797 cpu_dai->name);
1798 return ret;
1799}
1800
Mark Brown096e49d2009-07-05 15:12:22 +01001801/**
1802 * snd_soc_codec_volatile_register: Report if a register is volatile.
1803 *
1804 * @codec: CODEC to query.
1805 * @reg: Register to query.
1806 *
1807 * Boolean function indiciating if a CODEC register is volatile.
1808 */
1809int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1810{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001811 if (codec->driver->volatile_register)
1812 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001813 else
1814 return 0;
1815}
1816EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1817
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001818/**
1819 * snd_soc_new_ac97_codec - initailise AC97 device
1820 * @codec: audio codec
1821 * @ops: AC97 bus operations
1822 * @num: AC97 codec number
1823 *
1824 * Initialises AC97 codec resources for use by ad-hoc devices only.
1825 */
1826int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1827 struct snd_ac97_bus_ops *ops, int num)
1828{
1829 mutex_lock(&codec->mutex);
1830
1831 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1832 if (codec->ac97 == NULL) {
1833 mutex_unlock(&codec->mutex);
1834 return -ENOMEM;
1835 }
1836
1837 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1838 if (codec->ac97->bus == NULL) {
1839 kfree(codec->ac97);
1840 codec->ac97 = NULL;
1841 mutex_unlock(&codec->mutex);
1842 return -ENOMEM;
1843 }
1844
1845 codec->ac97->bus->ops = ops;
1846 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001847
1848 /*
1849 * Mark the AC97 device to be created by us. This way we ensure that the
1850 * device will be registered with the device subsystem later on.
1851 */
1852 codec->ac97_created = 1;
1853
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001854 mutex_unlock(&codec->mutex);
1855 return 0;
1856}
1857EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1858
1859/**
1860 * snd_soc_free_ac97_codec - free AC97 codec device
1861 * @codec: audio codec
1862 *
1863 * Frees AC97 codec device resources.
1864 */
1865void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1866{
1867 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001868#ifdef CONFIG_SND_SOC_AC97_BUS
1869 soc_unregister_ac97_dai_link(codec);
1870#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001871 kfree(codec->ac97->bus);
1872 kfree(codec->ac97);
1873 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001874 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001875 mutex_unlock(&codec->mutex);
1876}
1877EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1878
1879/**
1880 * snd_soc_update_bits - update codec register bits
1881 * @codec: audio codec
1882 * @reg: codec register
1883 * @mask: register mask
1884 * @value: new value
1885 *
1886 * Writes new register value.
1887 *
1888 * Returns 1 for change else 0.
1889 */
1890int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001891 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001892{
1893 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001894 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001895
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001896 old = snd_soc_read(codec, reg);
1897 new = (old & ~mask) | value;
1898 change = old != new;
1899 if (change)
1900 snd_soc_write(codec, reg, new);
1901
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001902 return change;
1903}
1904EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1905
1906/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001907 * snd_soc_update_bits_locked - update codec register bits
1908 * @codec: audio codec
1909 * @reg: codec register
1910 * @mask: register mask
1911 * @value: new value
1912 *
1913 * Writes new register value, and takes the codec mutex.
1914 *
1915 * Returns 1 for change else 0.
1916 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001917int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1918 unsigned short reg, unsigned int mask,
1919 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001920{
1921 int change;
1922
1923 mutex_lock(&codec->mutex);
1924 change = snd_soc_update_bits(codec, reg, mask, value);
1925 mutex_unlock(&codec->mutex);
1926
1927 return change;
1928}
Mark Browndd1b3d52009-12-04 14:22:03 +00001929EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001930
1931/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001932 * snd_soc_test_bits - test register for change
1933 * @codec: audio codec
1934 * @reg: codec register
1935 * @mask: register mask
1936 * @value: new value
1937 *
1938 * Tests a register with a new value and checks if the new value is
1939 * different from the old value.
1940 *
1941 * Returns 1 for change else 0.
1942 */
1943int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001944 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001945{
1946 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001947 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001948
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001949 old = snd_soc_read(codec, reg);
1950 new = (old & ~mask) | value;
1951 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001952
1953 return change;
1954}
1955EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1956
1957/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001958 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1959 * @substream: the pcm substream
1960 * @hw: the hardware parameters
1961 *
1962 * Sets the substream runtime hardware parameters.
1963 */
1964int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1965 const struct snd_pcm_hardware *hw)
1966{
1967 struct snd_pcm_runtime *runtime = substream->runtime;
1968 runtime->hw.info = hw->info;
1969 runtime->hw.formats = hw->formats;
1970 runtime->hw.period_bytes_min = hw->period_bytes_min;
1971 runtime->hw.period_bytes_max = hw->period_bytes_max;
1972 runtime->hw.periods_min = hw->periods_min;
1973 runtime->hw.periods_max = hw->periods_max;
1974 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1975 runtime->hw.fifo_size = hw->fifo_size;
1976 return 0;
1977}
1978EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1979
1980/**
1981 * snd_soc_cnew - create new control
1982 * @_template: control template
1983 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001984 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001985 *
1986 * Create a new mixer control from a template control.
1987 *
1988 * Returns 0 for success, else error.
1989 */
1990struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1991 void *data, char *long_name)
1992{
1993 struct snd_kcontrol_new template;
1994
1995 memcpy(&template, _template, sizeof(template));
1996 if (long_name)
1997 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001998 template.index = 0;
1999
2000 return snd_ctl_new1(&template, data);
2001}
2002EXPORT_SYMBOL_GPL(snd_soc_cnew);
2003
2004/**
Ian Molton3e8e1952009-01-09 00:23:21 +00002005 * snd_soc_add_controls - add an array of controls to a codec.
2006 * Convienience function to add a list of controls. Many codecs were
2007 * duplicating this code.
2008 *
2009 * @codec: codec to add controls to
2010 * @controls: array of controls to add
2011 * @num_controls: number of elements in the array
2012 *
2013 * Return 0 for success, else error.
2014 */
2015int snd_soc_add_controls(struct snd_soc_codec *codec,
2016 const struct snd_kcontrol_new *controls, int num_controls)
2017{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002018 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002019 int err, i;
2020
2021 for (i = 0; i < num_controls; i++) {
2022 const struct snd_kcontrol_new *control = &controls[i];
2023 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
2024 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002025 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2026 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002027 return err;
2028 }
2029 }
2030
2031 return 0;
2032}
2033EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2034
2035/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002036 * snd_soc_info_enum_double - enumerated double mixer info callback
2037 * @kcontrol: mixer control
2038 * @uinfo: control element information
2039 *
2040 * Callback to provide information about a double enumerated
2041 * mixer control.
2042 *
2043 * Returns 0 for success.
2044 */
2045int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2046 struct snd_ctl_elem_info *uinfo)
2047{
2048 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2049
2050 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2051 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002052 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002053
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002054 if (uinfo->value.enumerated.item > e->max - 1)
2055 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002056 strcpy(uinfo->value.enumerated.name,
2057 e->texts[uinfo->value.enumerated.item]);
2058 return 0;
2059}
2060EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2061
2062/**
2063 * snd_soc_get_enum_double - enumerated double mixer get callback
2064 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002065 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002066 *
2067 * Callback to get the value of a double enumerated mixer.
2068 *
2069 * Returns 0 for success.
2070 */
2071int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2072 struct snd_ctl_elem_value *ucontrol)
2073{
2074 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2075 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002076 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002077
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002078 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002079 ;
2080 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002081 ucontrol->value.enumerated.item[0]
2082 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002083 if (e->shift_l != e->shift_r)
2084 ucontrol->value.enumerated.item[1] =
2085 (val >> e->shift_r) & (bitmask - 1);
2086
2087 return 0;
2088}
2089EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2090
2091/**
2092 * snd_soc_put_enum_double - enumerated double mixer put callback
2093 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002094 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002095 *
2096 * Callback to set the value of a double enumerated mixer.
2097 *
2098 * Returns 0 for success.
2099 */
2100int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2101 struct snd_ctl_elem_value *ucontrol)
2102{
2103 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2104 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002105 unsigned int val;
2106 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002107
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002108 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002109 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002110 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002111 return -EINVAL;
2112 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2113 mask = (bitmask - 1) << e->shift_l;
2114 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002115 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002116 return -EINVAL;
2117 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2118 mask |= (bitmask - 1) << e->shift_r;
2119 }
2120
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002121 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002122}
2123EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2124
2125/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002126 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2127 * @kcontrol: mixer control
2128 * @ucontrol: control element information
2129 *
2130 * Callback to get the value of a double semi enumerated mixer.
2131 *
2132 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2133 * used for handling bitfield coded enumeration for example.
2134 *
2135 * Returns 0 for success.
2136 */
2137int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2138 struct snd_ctl_elem_value *ucontrol)
2139{
2140 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002141 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002142 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002143
2144 reg_val = snd_soc_read(codec, e->reg);
2145 val = (reg_val >> e->shift_l) & e->mask;
2146 for (mux = 0; mux < e->max; mux++) {
2147 if (val == e->values[mux])
2148 break;
2149 }
2150 ucontrol->value.enumerated.item[0] = mux;
2151 if (e->shift_l != e->shift_r) {
2152 val = (reg_val >> e->shift_r) & e->mask;
2153 for (mux = 0; mux < e->max; mux++) {
2154 if (val == e->values[mux])
2155 break;
2156 }
2157 ucontrol->value.enumerated.item[1] = mux;
2158 }
2159
2160 return 0;
2161}
2162EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2163
2164/**
2165 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2166 * @kcontrol: mixer control
2167 * @ucontrol: control element information
2168 *
2169 * Callback to set the value of a double semi enumerated mixer.
2170 *
2171 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2172 * used for handling bitfield coded enumeration for example.
2173 *
2174 * Returns 0 for success.
2175 */
2176int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2177 struct snd_ctl_elem_value *ucontrol)
2178{
2179 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002180 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002181 unsigned int val;
2182 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002183
2184 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2185 return -EINVAL;
2186 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2187 mask = e->mask << e->shift_l;
2188 if (e->shift_l != e->shift_r) {
2189 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2190 return -EINVAL;
2191 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2192 mask |= e->mask << e->shift_r;
2193 }
2194
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002195 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002196}
2197EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2198
2199/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002200 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2201 * @kcontrol: mixer control
2202 * @uinfo: control element information
2203 *
2204 * Callback to provide information about an external enumerated
2205 * single mixer.
2206 *
2207 * Returns 0 for success.
2208 */
2209int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2210 struct snd_ctl_elem_info *uinfo)
2211{
2212 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2213
2214 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2215 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002216 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002217
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002218 if (uinfo->value.enumerated.item > e->max - 1)
2219 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002220 strcpy(uinfo->value.enumerated.name,
2221 e->texts[uinfo->value.enumerated.item]);
2222 return 0;
2223}
2224EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2225
2226/**
2227 * snd_soc_info_volsw_ext - external single mixer info callback
2228 * @kcontrol: mixer control
2229 * @uinfo: control element information
2230 *
2231 * Callback to provide information about a single external mixer control.
2232 *
2233 * Returns 0 for success.
2234 */
2235int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2236 struct snd_ctl_elem_info *uinfo)
2237{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002238 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002239
Mark Brownfd5dfad2009-04-15 21:37:46 +01002240 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002241 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2242 else
2243 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2244
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002245 uinfo->count = 1;
2246 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002247 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002248 return 0;
2249}
2250EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2251
2252/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002253 * snd_soc_info_volsw - single mixer info callback
2254 * @kcontrol: mixer control
2255 * @uinfo: control element information
2256 *
2257 * Callback to provide information about a single mixer control.
2258 *
2259 * Returns 0 for success.
2260 */
2261int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2262 struct snd_ctl_elem_info *uinfo)
2263{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002264 struct soc_mixer_control *mc =
2265 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002266 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002267 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002268 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002269
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002270 if (!mc->platform_max)
2271 mc->platform_max = mc->max;
2272 platform_max = mc->platform_max;
2273
2274 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002275 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2276 else
2277 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2278
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002279 uinfo->count = shift == rshift ? 1 : 2;
2280 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002281 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002282 return 0;
2283}
2284EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2285
2286/**
2287 * snd_soc_get_volsw - single mixer get callback
2288 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002289 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002290 *
2291 * Callback to get the value of a single mixer control.
2292 *
2293 * Returns 0 for success.
2294 */
2295int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2296 struct snd_ctl_elem_value *ucontrol)
2297{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002298 struct soc_mixer_control *mc =
2299 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002300 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002301 unsigned int reg = mc->reg;
2302 unsigned int shift = mc->shift;
2303 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002304 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002305 unsigned int mask = (1 << fls(max)) - 1;
2306 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002307
2308 ucontrol->value.integer.value[0] =
2309 (snd_soc_read(codec, reg) >> shift) & mask;
2310 if (shift != rshift)
2311 ucontrol->value.integer.value[1] =
2312 (snd_soc_read(codec, reg) >> rshift) & mask;
2313 if (invert) {
2314 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002315 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002316 if (shift != rshift)
2317 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002318 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002319 }
2320
2321 return 0;
2322}
2323EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2324
2325/**
2326 * snd_soc_put_volsw - single mixer put callback
2327 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002328 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002329 *
2330 * Callback to set the value of a single mixer control.
2331 *
2332 * Returns 0 for success.
2333 */
2334int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2335 struct snd_ctl_elem_value *ucontrol)
2336{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002337 struct soc_mixer_control *mc =
2338 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002339 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002340 unsigned int reg = mc->reg;
2341 unsigned int shift = mc->shift;
2342 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002343 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002344 unsigned int mask = (1 << fls(max)) - 1;
2345 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002346 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002347
2348 val = (ucontrol->value.integer.value[0] & mask);
2349 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002350 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002351 val_mask = mask << shift;
2352 val = val << shift;
2353 if (shift != rshift) {
2354 val2 = (ucontrol->value.integer.value[1] & mask);
2355 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002356 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002357 val_mask |= mask << rshift;
2358 val |= val2 << rshift;
2359 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002360 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002361}
2362EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2363
2364/**
2365 * snd_soc_info_volsw_2r - double mixer info callback
2366 * @kcontrol: mixer control
2367 * @uinfo: control element information
2368 *
2369 * Callback to provide information about a double mixer control that
2370 * spans 2 codec registers.
2371 *
2372 * Returns 0 for success.
2373 */
2374int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2375 struct snd_ctl_elem_info *uinfo)
2376{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002377 struct soc_mixer_control *mc =
2378 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002379 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002380
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002381 if (!mc->platform_max)
2382 mc->platform_max = mc->max;
2383 platform_max = mc->platform_max;
2384
2385 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002386 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2387 else
2388 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2389
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002390 uinfo->count = 2;
2391 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002392 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002393 return 0;
2394}
2395EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2396
2397/**
2398 * snd_soc_get_volsw_2r - double mixer get callback
2399 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002400 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002401 *
2402 * Callback to get the value of a double mixer control that spans 2 registers.
2403 *
2404 * Returns 0 for success.
2405 */
2406int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2407 struct snd_ctl_elem_value *ucontrol)
2408{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002409 struct soc_mixer_control *mc =
2410 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002411 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002412 unsigned int reg = mc->reg;
2413 unsigned int reg2 = mc->rreg;
2414 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002415 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002416 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002417 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002418
2419 ucontrol->value.integer.value[0] =
2420 (snd_soc_read(codec, reg) >> shift) & mask;
2421 ucontrol->value.integer.value[1] =
2422 (snd_soc_read(codec, reg2) >> shift) & mask;
2423 if (invert) {
2424 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002425 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002426 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002427 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002428 }
2429
2430 return 0;
2431}
2432EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2433
2434/**
2435 * snd_soc_put_volsw_2r - double mixer set callback
2436 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002437 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002438 *
2439 * Callback to set the value of a double mixer control that spans 2 registers.
2440 *
2441 * Returns 0 for success.
2442 */
2443int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2444 struct snd_ctl_elem_value *ucontrol)
2445{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002446 struct soc_mixer_control *mc =
2447 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002448 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002449 unsigned int reg = mc->reg;
2450 unsigned int reg2 = mc->rreg;
2451 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002452 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002453 unsigned int mask = (1 << fls(max)) - 1;
2454 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002455 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002456 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002457
2458 val_mask = mask << shift;
2459 val = (ucontrol->value.integer.value[0] & mask);
2460 val2 = (ucontrol->value.integer.value[1] & mask);
2461
2462 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002463 val = max - val;
2464 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002465 }
2466
2467 val = val << shift;
2468 val2 = val2 << shift;
2469
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002470 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002471 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002472 return err;
2473
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002474 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002475 return err;
2476}
2477EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2478
Mark Browne13ac2e2008-05-28 17:58:05 +01002479/**
2480 * snd_soc_info_volsw_s8 - signed mixer info callback
2481 * @kcontrol: mixer control
2482 * @uinfo: control element information
2483 *
2484 * Callback to provide information about a signed mixer control.
2485 *
2486 * Returns 0 for success.
2487 */
2488int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2489 struct snd_ctl_elem_info *uinfo)
2490{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002491 struct soc_mixer_control *mc =
2492 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002493 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002494 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002495
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002496 if (!mc->platform_max)
2497 mc->platform_max = mc->max;
2498 platform_max = mc->platform_max;
2499
Mark Browne13ac2e2008-05-28 17:58:05 +01002500 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2501 uinfo->count = 2;
2502 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002503 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002504 return 0;
2505}
2506EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2507
2508/**
2509 * snd_soc_get_volsw_s8 - signed mixer get callback
2510 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002511 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002512 *
2513 * Callback to get the value of a signed mixer control.
2514 *
2515 * Returns 0 for success.
2516 */
2517int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2518 struct snd_ctl_elem_value *ucontrol)
2519{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002520 struct soc_mixer_control *mc =
2521 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002522 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002523 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002524 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002525 int val = snd_soc_read(codec, reg);
2526
2527 ucontrol->value.integer.value[0] =
2528 ((signed char)(val & 0xff))-min;
2529 ucontrol->value.integer.value[1] =
2530 ((signed char)((val >> 8) & 0xff))-min;
2531 return 0;
2532}
2533EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2534
2535/**
2536 * snd_soc_put_volsw_sgn - signed mixer put callback
2537 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002538 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002539 *
2540 * Callback to set the value of a signed mixer control.
2541 *
2542 * Returns 0 for success.
2543 */
2544int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2545 struct snd_ctl_elem_value *ucontrol)
2546{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002547 struct soc_mixer_control *mc =
2548 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002549 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002550 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002551 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002552 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002553
2554 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2555 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2556
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002557 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002558}
2559EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2560
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002561/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002562 * snd_soc_limit_volume - Set new limit to an existing volume control.
2563 *
2564 * @codec: where to look for the control
2565 * @name: Name of the control
2566 * @max: new maximum limit
2567 *
2568 * Return 0 for success, else error.
2569 */
2570int snd_soc_limit_volume(struct snd_soc_codec *codec,
2571 const char *name, int max)
2572{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002573 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002574 struct snd_kcontrol *kctl;
2575 struct soc_mixer_control *mc;
2576 int found = 0;
2577 int ret = -EINVAL;
2578
2579 /* Sanity check for name and max */
2580 if (unlikely(!name || max <= 0))
2581 return -EINVAL;
2582
2583 list_for_each_entry(kctl, &card->controls, list) {
2584 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2585 found = 1;
2586 break;
2587 }
2588 }
2589 if (found) {
2590 mc = (struct soc_mixer_control *)kctl->private_value;
2591 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002592 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002593 ret = 0;
2594 }
2595 }
2596 return ret;
2597}
2598EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2599
2600/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002601 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2602 * mixer info callback
2603 * @kcontrol: mixer control
2604 * @uinfo: control element information
2605 *
2606 * Returns 0 for success.
2607 */
2608int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2609 struct snd_ctl_elem_info *uinfo)
2610{
2611 struct soc_mixer_control *mc =
2612 (struct soc_mixer_control *)kcontrol->private_value;
2613 int max = mc->max;
2614 int min = mc->min;
2615
2616 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2617 uinfo->count = 2;
2618 uinfo->value.integer.min = 0;
2619 uinfo->value.integer.max = max-min;
2620
2621 return 0;
2622}
2623EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2624
2625/**
2626 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2627 * mixer get callback
2628 * @kcontrol: mixer control
2629 * @uinfo: control element information
2630 *
2631 * Returns 0 for success.
2632 */
2633int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2634 struct snd_ctl_elem_value *ucontrol)
2635{
2636 struct soc_mixer_control *mc =
2637 (struct soc_mixer_control *)kcontrol->private_value;
2638 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2639 unsigned int mask = (1<<mc->shift)-1;
2640 int min = mc->min;
2641 int val = snd_soc_read(codec, mc->reg) & mask;
2642 int valr = snd_soc_read(codec, mc->rreg) & mask;
2643
Stuart Longland20630c72010-06-18 12:56:10 +10002644 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2645 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002646 return 0;
2647}
2648EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2649
2650/**
2651 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2652 * mixer put callback
2653 * @kcontrol: mixer control
2654 * @uinfo: control element information
2655 *
2656 * Returns 0 for success.
2657 */
2658int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2659 struct snd_ctl_elem_value *ucontrol)
2660{
2661 struct soc_mixer_control *mc =
2662 (struct soc_mixer_control *)kcontrol->private_value;
2663 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2664 unsigned int mask = (1<<mc->shift)-1;
2665 int min = mc->min;
2666 int ret;
2667 unsigned int val, valr, oval, ovalr;
2668
2669 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2670 val &= mask;
2671 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2672 valr &= mask;
2673
2674 oval = snd_soc_read(codec, mc->reg) & mask;
2675 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2676
2677 ret = 0;
2678 if (oval != val) {
2679 ret = snd_soc_write(codec, mc->reg, val);
2680 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002681 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002682 }
2683 if (ovalr != valr) {
2684 ret = snd_soc_write(codec, mc->rreg, valr);
2685 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002686 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002687 }
2688
2689 return 0;
2690}
2691EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2692
2693/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002694 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2695 * @dai: DAI
2696 * @clk_id: DAI specific clock ID
2697 * @freq: new clock frequency in Hz
2698 * @dir: new clock direction - input/output.
2699 *
2700 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2701 */
2702int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2703 unsigned int freq, int dir)
2704{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002705 if (dai->driver && dai->driver->ops->set_sysclk)
2706 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002707 else
2708 return -EINVAL;
2709}
2710EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2711
2712/**
2713 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2714 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002715 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002716 * @div: new clock divisor.
2717 *
2718 * Configures the clock dividers. This is used to derive the best DAI bit and
2719 * frame clocks from the system or master clock. It's best to set the DAI bit
2720 * and frame clocks as low as possible to save system power.
2721 */
2722int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2723 int div_id, int div)
2724{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002725 if (dai->driver && dai->driver->ops->set_clkdiv)
2726 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002727 else
2728 return -EINVAL;
2729}
2730EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2731
2732/**
2733 * snd_soc_dai_set_pll - configure DAI PLL.
2734 * @dai: DAI
2735 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002736 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002737 * @freq_in: PLL input clock frequency in Hz
2738 * @freq_out: requested PLL output clock frequency in Hz
2739 *
2740 * Configures and enables PLL to generate output clock based on input clock.
2741 */
Mark Brown85488032009-09-05 18:52:16 +01002742int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2743 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002744{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002745 if (dai->driver && dai->driver->ops->set_pll)
2746 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002747 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002748 else
2749 return -EINVAL;
2750}
2751EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2752
2753/**
2754 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2755 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002756 * @fmt: SND_SOC_DAIFMT_ format value.
2757 *
2758 * Configures the DAI hardware format and clocking.
2759 */
2760int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2761{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002762 if (dai->driver && dai->driver->ops->set_fmt)
2763 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002764 else
2765 return -EINVAL;
2766}
2767EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2768
2769/**
2770 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2771 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002772 * @tx_mask: bitmask representing active TX slots.
2773 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002774 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002775 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002776 *
2777 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2778 * specific.
2779 */
2780int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002781 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002782{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002783 if (dai->driver && dai->driver->ops->set_tdm_slot)
2784 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002785 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002786 else
2787 return -EINVAL;
2788}
2789EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2790
2791/**
Barry Song472df3c2009-09-12 01:16:29 +08002792 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2793 * @dai: DAI
2794 * @tx_num: how many TX channels
2795 * @tx_slot: pointer to an array which imply the TX slot number channel
2796 * 0~num-1 uses
2797 * @rx_num: how many RX channels
2798 * @rx_slot: pointer to an array which imply the RX slot number channel
2799 * 0~num-1 uses
2800 *
2801 * configure the relationship between channel number and TDM slot number.
2802 */
2803int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2804 unsigned int tx_num, unsigned int *tx_slot,
2805 unsigned int rx_num, unsigned int *rx_slot)
2806{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002807 if (dai->driver && dai->driver->ops->set_channel_map)
2808 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002809 rx_num, rx_slot);
2810 else
2811 return -EINVAL;
2812}
2813EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2814
2815/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002816 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2817 * @dai: DAI
2818 * @tristate: tristate enable
2819 *
2820 * Tristates the DAI so that others can use it.
2821 */
2822int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2823{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002824 if (dai->driver && dai->driver->ops->set_tristate)
2825 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002826 else
2827 return -EINVAL;
2828}
2829EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2830
2831/**
2832 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2833 * @dai: DAI
2834 * @mute: mute enable
2835 *
2836 * Mutes the DAI DAC.
2837 */
2838int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2839{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002840 if (dai->driver && dai->driver->ops->digital_mute)
2841 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002842 else
2843 return -EINVAL;
2844}
2845EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2846
Mark Brownc5af3a22008-11-28 13:29:45 +00002847/**
2848 * snd_soc_register_card - Register a card with the ASoC core
2849 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002850 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002851 *
2852 * Note that currently this is an internal only function: it will be
2853 * exposed to machine drivers after further backporting of ASoC v2
2854 * registration APIs.
2855 */
2856static int snd_soc_register_card(struct snd_soc_card *card)
2857{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002858 int i;
2859
Mark Brownc5af3a22008-11-28 13:29:45 +00002860 if (!card->name || !card->dev)
2861 return -EINVAL;
2862
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002863 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2864 GFP_KERNEL);
2865 if (card->rtd == NULL)
2866 return -ENOMEM;
2867
2868 for (i = 0; i < card->num_links; i++)
2869 card->rtd[i].dai_link = &card->dai_link[i];
2870
Mark Brownc5af3a22008-11-28 13:29:45 +00002871 INIT_LIST_HEAD(&card->list);
2872 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002873 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002874
2875 mutex_lock(&client_mutex);
2876 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002877 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002878 mutex_unlock(&client_mutex);
2879
2880 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2881
2882 return 0;
2883}
2884
2885/**
2886 * snd_soc_unregister_card - Unregister a card with the ASoC core
2887 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002888 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002889 *
2890 * Note that currently this is an internal only function: it will be
2891 * exposed to machine drivers after further backporting of ASoC v2
2892 * registration APIs.
2893 */
2894static int snd_soc_unregister_card(struct snd_soc_card *card)
2895{
2896 mutex_lock(&client_mutex);
2897 list_del(&card->list);
2898 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002899 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2900
2901 return 0;
2902}
2903
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002904/*
2905 * Simplify DAI link configuration by removing ".-1" from device names
2906 * and sanitizing names.
2907 */
2908static inline char *fmt_single_name(struct device *dev, int *id)
2909{
2910 char *found, name[NAME_SIZE];
2911 int id1, id2;
2912
2913 if (dev_name(dev) == NULL)
2914 return NULL;
2915
2916 strncpy(name, dev_name(dev), NAME_SIZE);
2917
2918 /* are we a "%s.%d" name (platform and SPI components) */
2919 found = strstr(name, dev->driver->name);
2920 if (found) {
2921 /* get ID */
2922 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2923
2924 /* discard ID from name if ID == -1 */
2925 if (*id == -1)
2926 found[strlen(dev->driver->name)] = '\0';
2927 }
2928
2929 } else {
2930 /* I2C component devices are named "bus-addr" */
2931 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2932 char tmp[NAME_SIZE];
2933
2934 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002935 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002936
2937 /* sanitize component name for DAI link creation */
2938 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2939 strncpy(name, tmp, NAME_SIZE);
2940 } else
2941 *id = 0;
2942 }
2943
2944 return kstrdup(name, GFP_KERNEL);
2945}
2946
2947/*
2948 * Simplify DAI link naming for single devices with multiple DAIs by removing
2949 * any ".-1" and using the DAI name (instead of device name).
2950 */
2951static inline char *fmt_multiple_name(struct device *dev,
2952 struct snd_soc_dai_driver *dai_drv)
2953{
2954 if (dai_drv->name == NULL) {
2955 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2956 dev_name(dev));
2957 return NULL;
2958 }
2959
2960 return kstrdup(dai_drv->name, GFP_KERNEL);
2961}
2962
Mark Brown91151712008-11-30 23:31:24 +00002963/**
2964 * snd_soc_register_dai - Register a DAI with the ASoC core
2965 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002966 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002967 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002968int snd_soc_register_dai(struct device *dev,
2969 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00002970{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002971 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00002972
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002973 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00002974
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002975 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2976 if (dai == NULL)
2977 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08002978
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002979 /* create DAI component name */
2980 dai->name = fmt_single_name(dev, &dai->id);
2981 if (dai->name == NULL) {
2982 kfree(dai);
2983 return -ENOMEM;
2984 }
2985
2986 dai->dev = dev;
2987 dai->driver = dai_drv;
2988 if (!dai->driver->ops)
2989 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00002990
2991 mutex_lock(&client_mutex);
2992 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002993 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002994 mutex_unlock(&client_mutex);
2995
2996 pr_debug("Registered DAI '%s'\n", dai->name);
2997
2998 return 0;
2999}
3000EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3001
3002/**
3003 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3004 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003005 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003006 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003007void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003008{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003009 struct snd_soc_dai *dai;
3010
3011 list_for_each_entry(dai, &dai_list, list) {
3012 if (dev == dai->dev)
3013 goto found;
3014 }
3015 return;
3016
3017found:
Mark Brown91151712008-11-30 23:31:24 +00003018 mutex_lock(&client_mutex);
3019 list_del(&dai->list);
3020 mutex_unlock(&client_mutex);
3021
3022 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003023 kfree(dai->name);
3024 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003025}
3026EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3027
3028/**
3029 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3030 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003031 * @dai: Array of DAIs to register
3032 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003033 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003034int snd_soc_register_dais(struct device *dev,
3035 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003036{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003037 struct snd_soc_dai *dai;
3038 int i, ret = 0;
3039
Liam Girdwood720ffa42010-08-18 00:30:30 +01003040 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003041
3042 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003043
3044 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3045 if (dai == NULL)
3046 return -ENOMEM;
3047
3048 /* create DAI component name */
3049 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3050 if (dai->name == NULL) {
3051 kfree(dai);
3052 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003053 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003054 }
3055
3056 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003057 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003058 if (dai->driver->id)
3059 dai->id = dai->driver->id;
3060 else
3061 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003062 if (!dai->driver->ops)
3063 dai->driver->ops = &null_dai_ops;
3064
3065 mutex_lock(&client_mutex);
3066 list_add(&dai->list, &dai_list);
3067 mutex_unlock(&client_mutex);
3068
3069 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003070 }
3071
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003072 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003073 return 0;
3074
3075err:
3076 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003077 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003078
3079 return ret;
3080}
3081EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3082
3083/**
3084 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3085 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003086 * @dai: Array of DAIs to unregister
3087 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003088 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003089void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003090{
3091 int i;
3092
3093 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003094 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003095}
3096EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3097
Mark Brown12a48a8c2008-12-03 19:40:30 +00003098/**
3099 * snd_soc_register_platform - Register a platform with the ASoC core
3100 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003101 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003102 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003103int snd_soc_register_platform(struct device *dev,
3104 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003105{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003106 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003107
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003108 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3109
3110 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3111 if (platform == NULL)
3112 return -ENOMEM;
3113
3114 /* create platform component name */
3115 platform->name = fmt_single_name(dev, &platform->id);
3116 if (platform->name == NULL) {
3117 kfree(platform);
3118 return -ENOMEM;
3119 }
3120
3121 platform->dev = dev;
3122 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003123
3124 mutex_lock(&client_mutex);
3125 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003126 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003127 mutex_unlock(&client_mutex);
3128
3129 pr_debug("Registered platform '%s'\n", platform->name);
3130
3131 return 0;
3132}
3133EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3134
3135/**
3136 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3137 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003138 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003139 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003140void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003141{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003142 struct snd_soc_platform *platform;
3143
3144 list_for_each_entry(platform, &platform_list, list) {
3145 if (dev == platform->dev)
3146 goto found;
3147 }
3148 return;
3149
3150found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003151 mutex_lock(&client_mutex);
3152 list_del(&platform->list);
3153 mutex_unlock(&client_mutex);
3154
3155 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003156 kfree(platform->name);
3157 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003158}
3159EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3160
Mark Brown151ab222009-05-09 16:22:58 +01003161static u64 codec_format_map[] = {
3162 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3163 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3164 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3165 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3166 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3167 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3168 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3169 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3170 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3171 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3172 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3173 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3174 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3175 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3176 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3177 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3178};
3179
3180/* Fix up the DAI formats for endianness: codecs don't actually see
3181 * the endianness of the data but we're using the CPU format
3182 * definitions which do need to include endianness so we ensure that
3183 * codec DAIs always have both big and little endian variants set.
3184 */
3185static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3186{
3187 int i;
3188
3189 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3190 if (stream->formats & codec_format_map[i])
3191 stream->formats |= codec_format_map[i];
3192}
3193
Mark Brown0d0cf002008-12-10 14:32:45 +00003194/**
3195 * snd_soc_register_codec - Register a codec with the ASoC core
3196 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003197 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003198 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003199int snd_soc_register_codec(struct device *dev,
3200 struct snd_soc_codec_driver *codec_drv,
3201 struct snd_soc_dai_driver *dai_drv, int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003202{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003203 struct snd_soc_codec *codec;
3204 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003205
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003206 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003207
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003208 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3209 if (codec == NULL)
3210 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003211
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003212 /* create CODEC component name */
3213 codec->name = fmt_single_name(dev, &codec->id);
3214 if (codec->name == NULL) {
3215 kfree(codec);
3216 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003217 }
3218
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003219 /* allocate CODEC register cache */
3220 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3221
3222 if (codec_drv->reg_cache_default)
3223 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3224 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3225 else
3226 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3227 codec_drv->reg_word_size, GFP_KERNEL);
3228
3229 if (codec->reg_cache == NULL) {
3230 kfree(codec->name);
3231 kfree(codec);
3232 return -ENOMEM;
3233 }
3234 }
3235
3236 codec->dev = dev;
3237 codec->driver = codec_drv;
3238 codec->bias_level = SND_SOC_BIAS_OFF;
3239 codec->num_dai = num_dai;
3240 mutex_init(&codec->mutex);
3241 INIT_LIST_HEAD(&codec->dapm_widgets);
3242 INIT_LIST_HEAD(&codec->dapm_paths);
3243
3244 for (i = 0; i < num_dai; i++) {
3245 fixup_codec_formats(&dai_drv[i].playback);
3246 fixup_codec_formats(&dai_drv[i].capture);
3247 }
3248
Mark Brown26b01cc2010-08-18 20:20:55 +01003249 /* register any DAIs */
3250 if (num_dai) {
3251 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3252 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003253 goto error;
Mark Brown26b01cc2010-08-18 20:20:55 +01003254 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003255
Mark Brown0d0cf002008-12-10 14:32:45 +00003256 mutex_lock(&client_mutex);
3257 list_add(&codec->list, &codec_list);
3258 snd_soc_instantiate_cards();
3259 mutex_unlock(&client_mutex);
3260
3261 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003262 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003263
3264error:
3265 for (i--; i >= 0; i--)
3266 snd_soc_unregister_dai(dev);
3267
3268 if (codec->reg_cache)
3269 kfree(codec->reg_cache);
3270 kfree(codec->name);
3271 kfree(codec);
3272 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003273}
3274EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3275
3276/**
3277 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3278 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003279 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003280 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003281void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003282{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003283 struct snd_soc_codec *codec;
3284 int i;
3285
3286 list_for_each_entry(codec, &codec_list, list) {
3287 if (dev == codec->dev)
3288 goto found;
3289 }
3290 return;
3291
3292found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003293 if (codec->num_dai)
3294 for (i = 0; i < codec->num_dai; i++)
3295 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003296
Mark Brown0d0cf002008-12-10 14:32:45 +00003297 mutex_lock(&client_mutex);
3298 list_del(&codec->list);
3299 mutex_unlock(&client_mutex);
3300
3301 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003302
3303 if (codec->reg_cache)
3304 kfree(codec->reg_cache);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003305 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003306 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003307}
3308EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3309
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003310static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003311{
Mark Brown384c89e2008-12-03 17:34:03 +00003312#ifdef CONFIG_DEBUG_FS
3313 debugfs_root = debugfs_create_dir("asoc", NULL);
3314 if (IS_ERR(debugfs_root) || !debugfs_root) {
3315 printk(KERN_WARNING
3316 "ASoC: Failed to create debugfs directory\n");
3317 debugfs_root = NULL;
3318 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003319
3320 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3321 &codec_list_fops))
3322 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3323
Mark Brownf3208782010-09-15 18:19:07 +01003324 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3325 &dai_list_fops))
3326 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003327
3328 if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
3329 &platform_list_fops))
3330 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003331#endif
3332
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003333 return platform_driver_register(&soc_driver);
3334}
Mark Brown4abe8e12010-10-12 17:41:03 +01003335module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003336
Mark Brown7d8c16a2008-11-30 22:11:24 +00003337static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003338{
Mark Brown384c89e2008-12-03 17:34:03 +00003339#ifdef CONFIG_DEBUG_FS
3340 debugfs_remove_recursive(debugfs_root);
3341#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003342 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003343}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003344module_exit(snd_soc_exit);
3345
3346/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003347MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003348MODULE_DESCRIPTION("ALSA SoC Core");
3349MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003350MODULE_ALIAS("platform:soc-audio");