blob: b0f635ccf17635260b918c5e4e158096286b2bd1 [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,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200236 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000237};
238
239static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
240{
Mark Brown6ba6c9c2010-08-12 15:49:52 +0100241 codec->debugfs_codec_root = debugfs_create_dir(codec->name ,
Mark Brown2624d5f2009-11-03 21:56:13 +0000242 debugfs_root);
243 if (!codec->debugfs_codec_root) {
244 printk(KERN_WARNING
245 "ASoC: Failed to create codec debugfs directory\n");
246 return;
247 }
248
249 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
250 codec->debugfs_codec_root,
251 codec, &codec_reg_fops);
252 if (!codec->debugfs_reg)
253 printk(KERN_WARNING
254 "ASoC: Failed to create codec register debugfs file\n");
255
Axel Lin708fafb2010-08-27 10:34:27 +0800256 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
Mark Brown2624d5f2009-11-03 21:56:13 +0000257 codec->debugfs_codec_root,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200258 &codec->dapm.pop_time);
Mark Brown2624d5f2009-11-03 21:56:13 +0000259 if (!codec->debugfs_pop_time)
260 printk(KERN_WARNING
261 "Failed to create pop time debugfs file\n");
262
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200263 codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
Mark Brown2624d5f2009-11-03 21:56:13 +0000264 codec->debugfs_codec_root);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200265 if (!codec->dapm.debugfs_dapm)
Mark Brown2624d5f2009-11-03 21:56:13 +0000266 printk(KERN_WARNING
267 "Failed to create DAPM debugfs directory\n");
268
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200269 snd_soc_dapm_debugfs_init(&codec->dapm);
Mark Brown2624d5f2009-11-03 21:56:13 +0000270}
271
272static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
273{
274 debugfs_remove_recursive(codec->debugfs_codec_root);
275}
276
Mark Brownc3c5a192010-09-15 18:15:14 +0100277static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
278 size_t count, loff_t *ppos)
279{
280 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100281 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100282 struct snd_soc_codec *codec;
283
284 if (!buf)
285 return -ENOMEM;
286
Mark Brown2b194f9d2010-10-13 10:52:16 +0100287 list_for_each_entry(codec, &codec_list, list) {
288 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
289 codec->name);
290 if (len >= 0)
291 ret += len;
292 if (ret > PAGE_SIZE) {
293 ret = PAGE_SIZE;
294 break;
295 }
296 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100297
298 if (ret >= 0)
299 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
300
301 kfree(buf);
302
303 return ret;
304}
305
306static const struct file_operations codec_list_fops = {
307 .read = codec_list_read_file,
308 .llseek = default_llseek,/* read accesses f_pos */
309};
310
Mark Brownf3208782010-09-15 18:19:07 +0100311static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
312 size_t count, loff_t *ppos)
313{
314 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100315 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100316 struct snd_soc_dai *dai;
317
318 if (!buf)
319 return -ENOMEM;
320
Mark Brown2b194f9d2010-10-13 10:52:16 +0100321 list_for_each_entry(dai, &dai_list, list) {
322 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
323 if (len >= 0)
324 ret += len;
325 if (ret > PAGE_SIZE) {
326 ret = PAGE_SIZE;
327 break;
328 }
329 }
Mark Brownf3208782010-09-15 18:19:07 +0100330
Mark Brown2b194f9d2010-10-13 10:52:16 +0100331 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100332
333 kfree(buf);
334
335 return ret;
336}
337
338static const struct file_operations dai_list_fops = {
339 .read = dai_list_read_file,
340 .llseek = default_llseek,/* read accesses f_pos */
341};
342
Mark Brown19c7ac22010-09-15 18:22:40 +0100343static ssize_t platform_list_read_file(struct file *file,
344 char __user *user_buf,
345 size_t count, loff_t *ppos)
346{
347 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100348 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100349 struct snd_soc_platform *platform;
350
351 if (!buf)
352 return -ENOMEM;
353
Mark Brown2b194f9d2010-10-13 10:52:16 +0100354 list_for_each_entry(platform, &platform_list, list) {
355 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
356 platform->name);
357 if (len >= 0)
358 ret += len;
359 if (ret > PAGE_SIZE) {
360 ret = PAGE_SIZE;
361 break;
362 }
363 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100364
Mark Brown2b194f9d2010-10-13 10:52:16 +0100365 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100366
367 kfree(buf);
368
369 return ret;
370}
371
372static const struct file_operations platform_list_fops = {
373 .read = platform_list_read_file,
374 .llseek = default_llseek,/* read accesses f_pos */
375};
376
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200377static void soc_init_card_debugfs(struct snd_soc_card *card)
378{
379 card->debugfs_card_root = debugfs_create_dir(card->name,
380 debugfs_root);
381 if (!card->debugfs_card_root)
382 dev_warn(card->dev,
383 "ASoC: Failed to create codec debugfs directory\n");
384}
385
386static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
387{
388 debugfs_remove_recursive(card->debugfs_card_root);
389}
390
Mark Brown2624d5f2009-11-03 21:56:13 +0000391#else
392
393static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
394{
395}
396
397static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
398{
399}
400#endif
401
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200402#ifdef CONFIG_SND_SOC_AC97_BUS
403/* unregister ac97 codec */
404static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
405{
406 if (codec->ac97->dev.bus)
407 device_unregister(&codec->ac97->dev);
408 return 0;
409}
410
411/* stop no dev release warning */
412static void soc_ac97_device_release(struct device *dev){}
413
414/* register ac97 codec to bus */
415static int soc_ac97_dev_register(struct snd_soc_codec *codec)
416{
417 int err;
418
419 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100420 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200421 codec->ac97->dev.release = soc_ac97_device_release;
422
Kay Sieversbb072bf2008-11-02 03:50:35 +0100423 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000424 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200425 err = device_register(&codec->ac97->dev);
426 if (err < 0) {
427 snd_printk(KERN_ERR "Can't register ac97 bus\n");
428 codec->ac97->dev.bus = NULL;
429 return err;
430 }
431 return 0;
432}
433#endif
434
Mark Brown06f409d2009-04-07 18:10:13 +0100435static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
436{
437 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000438 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
439 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100440 int ret;
441
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000442 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
443 rtd->dai_link->symmetric_rates) {
444 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
445 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100446
447 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
448 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000449 rtd->rate,
450 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100451 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000452 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100453 "Unable to apply rate symmetry constraint: %d\n", ret);
454 return ret;
455 }
456 }
457
458 return 0;
459}
460
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200461/*
462 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
463 * then initialized and any private data can be allocated. This also calls
464 * startup for the cpu DAI, platform, machine and codec DAI.
465 */
466static int soc_pcm_open(struct snd_pcm_substream *substream)
467{
468 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200469 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000470 struct snd_soc_platform *platform = rtd->platform;
471 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
472 struct snd_soc_dai *codec_dai = rtd->codec_dai;
473 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
474 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200475 int ret = 0;
476
477 mutex_lock(&pcm_mutex);
478
479 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000480 if (cpu_dai->driver->ops->startup) {
481 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200482 if (ret < 0) {
483 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100484 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200485 goto out;
486 }
487 }
488
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000489 if (platform->driver->ops->open) {
490 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200491 if (ret < 0) {
492 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
493 goto platform_err;
494 }
495 }
496
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000497 if (codec_dai->driver->ops->startup) {
498 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100499 if (ret < 0) {
500 printk(KERN_ERR "asoc: can't open codec %s\n",
501 codec_dai->name);
502 goto codec_dai_err;
503 }
504 }
505
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000506 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
507 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200508 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000509 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200510 goto machine_err;
511 }
512 }
513
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200514 /* Check that the codec and cpu DAI's are compatible */
515 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
516 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000517 max(codec_dai_drv->playback.rate_min,
518 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200519 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000520 min(codec_dai_drv->playback.rate_max,
521 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200522 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000523 max(codec_dai_drv->playback.channels_min,
524 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200525 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000526 min(codec_dai_drv->playback.channels_max,
527 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100528 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000529 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100530 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
532 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900533 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000534 runtime->hw.rates |= cpu_dai_drv->playback.rates;
535 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900536 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000537 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200538 } else {
539 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000540 max(codec_dai_drv->capture.rate_min,
541 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200542 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000543 min(codec_dai_drv->capture.rate_max,
544 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200545 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000546 max(codec_dai_drv->capture.channels_min,
547 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000549 min(codec_dai_drv->capture.channels_max,
550 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100551 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100553 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
555 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900556 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000557 runtime->hw.rates |= cpu_dai_drv->capture.rates;
558 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900559 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000560 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200561 }
562
563 snd_pcm_limit_hw_rates(runtime);
564 if (!runtime->hw.rates) {
565 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100566 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900567 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200568 }
569 if (!runtime->hw.formats) {
570 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100571 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900572 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200573 }
574 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
575 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000576 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900577 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200578 }
579
Mark Brown06f409d2009-04-07 18:10:13 +0100580 /* Symmetry only applies if we've already got an active stream. */
581 if (cpu_dai->active || codec_dai->active) {
582 ret = soc_pcm_apply_symmetry(substream);
583 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900584 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100585 }
586
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 pr_debug("asoc: %s <-> %s info:\n",
588 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100589 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
590 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
591 runtime->hw.channels_max);
592 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
593 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200594
Jassi Brar14dc5732010-02-26 09:12:32 +0900595 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 cpu_dai->playback_active++;
597 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900598 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 cpu_dai->capture_active++;
600 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900601 }
602 cpu_dai->active++;
603 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000604 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200605 mutex_unlock(&pcm_mutex);
606 return 0;
607
Jassi Brarbb1c0472010-02-25 11:24:53 +0900608config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
610 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200611
Jassi Brarbb1c0472010-02-25 11:24:53 +0900612machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 if (codec_dai->driver->ops->shutdown)
614 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900615
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100616codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 if (platform->driver->ops->close)
618 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200619
620platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 if (cpu_dai->driver->ops->shutdown)
622 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200623out:
624 mutex_unlock(&pcm_mutex);
625 return ret;
626}
627
628/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200629 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200630 * This is to ensure there are no pops or clicks in between any music tracks
631 * due to DAPM power cycling.
632 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100633static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000635 struct snd_soc_pcm_runtime *rtd =
636 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
637 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200638
639 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200640
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000641 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
642 codec_dai->driver->playback.stream_name,
643 codec_dai->playback_active ? "active" : "inactive",
644 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200645
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 /* are we waiting on this codec DAI stream */
647 if (codec_dai->pop_wait == 1) {
648 codec_dai->pop_wait = 0;
649 snd_soc_dapm_stream_event(rtd,
650 codec_dai->driver->playback.stream_name,
651 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200652 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000653
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654 mutex_unlock(&pcm_mutex);
655}
656
657/*
658 * Called by ALSA when a PCM substream is closed. Private data can be
659 * freed here. The cpu DAI, codec DAI, machine and platform are also
660 * shutdown.
661 */
662static int soc_codec_close(struct snd_pcm_substream *substream)
663{
664 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665 struct snd_soc_platform *platform = rtd->platform;
666 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
667 struct snd_soc_dai *codec_dai = rtd->codec_dai;
668 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200669
670 mutex_lock(&pcm_mutex);
671
Jassi Brar14dc5732010-02-26 09:12:32 +0900672 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 cpu_dai->playback_active--;
674 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900675 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000676 cpu_dai->capture_active--;
677 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200678 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900679
680 cpu_dai->active--;
681 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200682 codec->active--;
683
Mark Brown6010b2d2008-09-06 18:33:24 +0100684 /* Muting the DAC suppresses artifacts caused during digital
685 * shutdown, for example from stopping clocks.
686 */
687 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
688 snd_soc_dai_digital_mute(codec_dai, 1);
689
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690 if (cpu_dai->driver->ops->shutdown)
691 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200692
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693 if (codec_dai->driver->ops->shutdown)
694 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200695
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
697 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200698
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000699 if (platform->driver->ops->close)
700 platform->driver->ops->close(substream);
701 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200702
703 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
704 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100705 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000706 schedule_delayed_work(&rtd->delayed_work,
707 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200708 } else {
709 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000710 snd_soc_dapm_stream_event(rtd,
711 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100712 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200713 }
714
715 mutex_unlock(&pcm_mutex);
716 return 0;
717}
718
719/*
720 * Called by ALSA when the PCM substream is prepared, can set format, sample
721 * rate, etc. This function is non atomic and can be called multiple times,
722 * it can refer to the runtime info.
723 */
724static int soc_pcm_prepare(struct snd_pcm_substream *substream)
725{
726 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000727 struct snd_soc_platform *platform = rtd->platform;
728 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
729 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200730 int ret = 0;
731
732 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100733
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000734 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
735 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100736 if (ret < 0) {
737 printk(KERN_ERR "asoc: machine prepare error\n");
738 goto out;
739 }
740 }
741
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000742 if (platform->driver->ops->prepare) {
743 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200744 if (ret < 0) {
745 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200746 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200747 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200748 }
749
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 if (codec_dai->driver->ops->prepare) {
751 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200752 if (ret < 0) {
753 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200754 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200755 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200756 }
757
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000758 if (cpu_dai->driver->ops->prepare) {
759 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100760 if (ret < 0) {
761 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
762 goto out;
763 }
764 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200765
Mark Brownd45f6212008-10-14 13:58:36 +0100766 /* cancel any delayed stream shutdown that is pending */
767 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
768 codec_dai->pop_wait) {
769 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000770 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100771 }
772
Mark Brown452c5ea2009-05-17 21:41:23 +0100773 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000774 snd_soc_dapm_stream_event(rtd,
775 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100776 SND_SOC_DAPM_STREAM_START);
777 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778 snd_soc_dapm_stream_event(rtd,
779 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100780 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100781
Mark Brown452c5ea2009-05-17 21:41:23 +0100782 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200783
784out:
785 mutex_unlock(&pcm_mutex);
786 return ret;
787}
788
789/*
790 * Called by ALSA when the hardware params are set by application. This
791 * function can also be called multiple times and can allocate buffers
792 * (using snd_pcm_lib_* ). It's non-atomic.
793 */
794static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
795 struct snd_pcm_hw_params *params)
796{
797 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000798 struct snd_soc_platform *platform = rtd->platform;
799 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
800 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200801 int ret = 0;
802
803 mutex_lock(&pcm_mutex);
804
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000805 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
806 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200807 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100808 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200809 goto out;
810 }
811 }
812
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000813 if (codec_dai->driver->ops->hw_params) {
814 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100815 if (ret < 0) {
816 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
817 codec_dai->name);
818 goto codec_err;
819 }
820 }
821
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000822 if (cpu_dai->driver->ops->hw_params) {
823 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200824 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200825 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100826 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200827 goto interface_err;
828 }
829 }
830
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000831 if (platform->driver->ops->hw_params) {
832 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200833 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200834 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200835 platform->name);
836 goto platform_err;
837 }
838 }
839
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000840 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100841
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200842out:
843 mutex_unlock(&pcm_mutex);
844 return ret;
845
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200846platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000847 if (cpu_dai->driver->ops->hw_free)
848 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849
850interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000851 if (codec_dai->driver->ops->hw_free)
852 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100853
854codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000855 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
856 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200857
858 mutex_unlock(&pcm_mutex);
859 return ret;
860}
861
862/*
863 * Free's resources allocated by hw_params, can be called multiple times
864 */
865static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
866{
867 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000868 struct snd_soc_platform *platform = rtd->platform;
869 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
870 struct snd_soc_dai *codec_dai = rtd->codec_dai;
871 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200872
873 mutex_lock(&pcm_mutex);
874
875 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100876 if (!codec->active)
877 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200878
879 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000880 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
881 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200882
883 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000884 if (platform->driver->ops->hw_free)
885 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200886
887 /* now free hw params for the DAI's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000888 if (codec_dai->driver->ops->hw_free)
889 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200890
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000891 if (cpu_dai->driver->ops->hw_free)
892 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200893
894 mutex_unlock(&pcm_mutex);
895 return 0;
896}
897
898static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
899{
900 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000901 struct snd_soc_platform *platform = rtd->platform;
902 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
903 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200904 int ret;
905
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000906 if (codec_dai->driver->ops->trigger) {
907 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200908 if (ret < 0)
909 return ret;
910 }
911
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000912 if (platform->driver->ops->trigger) {
913 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200914 if (ret < 0)
915 return ret;
916 }
917
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000918 if (cpu_dai->driver->ops->trigger) {
919 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200920 if (ret < 0)
921 return ret;
922 }
923 return 0;
924}
925
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200926/*
927 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200928 * If cpu_dai, codec_dai, platform driver has the delay callback, than
929 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200930 */
931static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
932{
933 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000934 struct snd_soc_platform *platform = rtd->platform;
935 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
936 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200937 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200938 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200939 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200940
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000941 if (platform->driver->ops->pointer)
942 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200943
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000944 if (cpu_dai->driver->ops->delay)
945 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200946
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000947 if (codec_dai->driver->ops->delay)
948 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200949
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000950 if (platform->driver->delay)
951 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200952
953 runtime->delay = delay;
954
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200955 return offset;
956}
957
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200958/* ASoC PCM operations */
959static struct snd_pcm_ops soc_pcm_ops = {
960 .open = soc_pcm_open,
961 .close = soc_codec_close,
962 .hw_params = soc_pcm_hw_params,
963 .hw_free = soc_pcm_hw_free,
964 .prepare = soc_pcm_prepare,
965 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200966 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200967};
968
969#ifdef CONFIG_PM
970/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100971static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200972{
Mark Brown416356f2009-06-30 19:05:15 +0100973 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000974 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200975 int i;
976
Daniel Macke3509ff2009-06-03 17:44:49 +0200977 /* If the initialization of this soc device failed, there is no codec
978 * associated with it. Just bail out in this case.
979 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000980 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200981 return 0;
982
Andy Green6ed25972008-06-13 16:24:05 +0100983 /* Due to the resume being scheduled into a workqueue we could
984 * suspend before that's finished - wait for it to complete.
985 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000986 snd_power_lock(card->snd_card);
987 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
988 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100989
990 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000991 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100992
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200993 /* mute any active DAC's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000994 for (i = 0; i < card->num_rtd; i++) {
995 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
996 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100997
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000998 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100999 continue;
1000
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001001 if (drv->ops->digital_mute && dai->playback_active)
1002 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001003 }
1004
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001005 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001006 for (i = 0; i < card->num_rtd; i++) {
1007 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001008 continue;
1009
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001010 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +01001011 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001012
Mark Brown87506542008-11-18 20:50:34 +00001013 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +01001014 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001015
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001016 for (i = 0; i < card->num_rtd; i++) {
1017 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1018 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001019
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001020 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001021 continue;
1022
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001023 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1024 cpu_dai->driver->suspend(cpu_dai);
1025 if (platform->driver->suspend && !platform->suspended) {
1026 platform->driver->suspend(cpu_dai);
1027 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +01001028 }
1029 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001030
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001031 /* close any waiting streams and save state */
1032 for (i = 0; i < card->num_rtd; i++) {
1033 run_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001034 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001035 }
Mark Brown3efab7d2010-05-09 13:25:43 +01001036
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001037 for (i = 0; i < card->num_rtd; i++) {
1038 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1039
1040 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001041 continue;
1042
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001043 if (driver->playback.stream_name != NULL)
1044 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1045 SND_SOC_DAPM_STREAM_SUSPEND);
1046
1047 if (driver->capture.stream_name != NULL)
1048 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1049 SND_SOC_DAPM_STREAM_SUSPEND);
1050 }
1051
1052 /* suspend all CODECs */
1053 for (i = 0; i < card->num_rtd; i++) {
1054 struct snd_soc_codec *codec = card->rtd[i].codec;
1055 /* If there are paths active then the CODEC will be held with
1056 * bias _ON and should not be suspended. */
1057 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001058 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001059 case SND_SOC_BIAS_STANDBY:
1060 case SND_SOC_BIAS_OFF:
1061 codec->driver->suspend(codec, PMSG_SUSPEND);
1062 codec->suspended = 1;
1063 break;
1064 default:
1065 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1066 break;
1067 }
1068 }
1069 }
1070
1071 for (i = 0; i < card->num_rtd; i++) {
1072 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1073
1074 if (card->rtd[i].dai_link->ignore_suspend)
1075 continue;
1076
1077 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1078 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001079 }
1080
Mark Brown87506542008-11-18 20:50:34 +00001081 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +01001082 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001083
1084 return 0;
1085}
1086
Andy Green6ed25972008-06-13 16:24:05 +01001087/* deferred resume work, so resume can complete before we finished
1088 * setting our codec back up, which can be very slow on I2C
1089 */
1090static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001091{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001092 struct snd_soc_card *card =
1093 container_of(work, struct snd_soc_card, deferred_resume_work);
1094 struct platform_device *pdev = to_platform_device(card->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001095 int i;
1096
Andy Green6ed25972008-06-13 16:24:05 +01001097 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1098 * so userspace apps are blocked from touching us
1099 */
1100
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001101 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001102
Mark Brown9949788b2010-05-07 20:24:05 +01001103 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001104 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +01001105
Mark Brown87506542008-11-18 20:50:34 +00001106 if (card->resume_pre)
1107 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001108
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001109 /* resume AC97 DAIs */
1110 for (i = 0; i < card->num_rtd; i++) {
1111 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001112
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001113 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001114 continue;
1115
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001116 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1117 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001118 }
1119
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001120 for (i = 0; i < card->num_rtd; i++) {
1121 struct snd_soc_codec *codec = card->rtd[i].codec;
1122 /* If the CODEC was idle over suspend then it will have been
1123 * left with bias OFF or STANDBY and suspended so we must now
1124 * resume. Otherwise the suspend was suppressed.
1125 */
1126 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001127 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001128 case SND_SOC_BIAS_STANDBY:
1129 case SND_SOC_BIAS_OFF:
1130 codec->driver->resume(codec);
1131 codec->suspended = 0;
1132 break;
1133 default:
1134 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1135 break;
1136 }
Mark Brown1547aba2010-05-07 21:11:40 +01001137 }
1138 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001139
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001140 for (i = 0; i < card->num_rtd; i++) {
1141 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001142
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001143 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001144 continue;
1145
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001146 if (driver->playback.stream_name != NULL)
1147 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001148 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001149
1150 if (driver->capture.stream_name != NULL)
1151 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001152 SND_SOC_DAPM_STREAM_RESUME);
1153 }
1154
Mark Brown3ff3f642008-05-19 12:32:25 +02001155 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001156 for (i = 0; i < card->num_rtd; i++) {
1157 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1158 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001159
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001160 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001161 continue;
1162
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001163 if (drv->ops->digital_mute && dai->playback_active)
1164 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001165 }
1166
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001167 for (i = 0; i < card->num_rtd; i++) {
1168 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1169 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001170
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001171 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001172 continue;
1173
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001174 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1175 cpu_dai->driver->resume(cpu_dai);
1176 if (platform->driver->resume && platform->suspended) {
1177 platform->driver->resume(cpu_dai);
1178 platform->suspended = 0;
1179 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001180 }
1181
Mark Brown87506542008-11-18 20:50:34 +00001182 if (card->resume_post)
1183 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001184
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001185 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001186
1187 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001188 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001189}
1190
1191/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001192static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001193{
Mark Brown416356f2009-06-30 19:05:15 +01001194 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001195 struct snd_soc_card *card = platform_get_drvdata(pdev);
1196 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001197
Mark Brown64ab9ba2009-03-31 11:27:03 +01001198 /* AC97 devices might have other drivers hanging off them so
1199 * need to resume immediately. Other drivers don't have that
1200 * problem and may take a substantial amount of time to resume
1201 * due to I/O costs and anti-pop so handle them out of line.
1202 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001203 for (i = 0; i < card->num_rtd; i++) {
1204 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1205 if (cpu_dai->driver->ac97_control) {
1206 dev_dbg(dev, "Resuming AC97 immediately\n");
1207 soc_resume_deferred(&card->deferred_resume_work);
1208 } else {
1209 dev_dbg(dev, "Scheduling resume work\n");
1210 if (!schedule_work(&card->deferred_resume_work))
1211 dev_err(dev, "resume work item may be lost\n");
1212 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001213 }
Andy Green6ed25972008-06-13 16:24:05 +01001214
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001215 return 0;
1216}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001217#else
1218#define soc_suspend NULL
1219#define soc_resume NULL
1220#endif
1221
Barry Song02a06d32009-10-16 18:13:38 +08001222static struct snd_soc_dai_ops null_dai_ops = {
1223};
1224
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001225static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001226{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001227 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1228 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001229 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001230 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001231 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001232
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001233 if (rtd->complete)
1234 return 1;
1235 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001236
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001237 /* do we already have the CPU DAI for this link ? */
1238 if (rtd->cpu_dai) {
1239 goto find_codec;
1240 }
1241 /* no, then find CPU DAI from registered DAIs*/
1242 list_for_each_entry(cpu_dai, &dai_list, list) {
1243 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1244
1245 if (!try_module_get(cpu_dai->dev->driver->owner))
1246 return -ENODEV;
1247
1248 rtd->cpu_dai = cpu_dai;
1249 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001250 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001251 }
1252 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1253 dai_link->cpu_dai_name);
1254
1255find_codec:
1256 /* do we already have the CODEC for this link ? */
1257 if (rtd->codec) {
1258 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001259 }
1260
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001261 /* no, then find CODEC from registered CODECs*/
1262 list_for_each_entry(codec, &codec_list, list) {
1263 if (!strcmp(codec->name, dai_link->codec_name)) {
1264 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001265
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001266 if (!try_module_get(codec->dev->driver->owner))
1267 return -ENODEV;
Mark Brown435c5e22008-12-04 15:32:53 +00001268
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001269 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1270 list_for_each_entry(codec_dai, &dai_list, list) {
1271 if (codec->dev == codec_dai->dev &&
1272 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1273 rtd->codec_dai = codec_dai;
1274 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001275 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001276 }
1277 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1278 dai_link->codec_dai_name);
1279
1280 goto find_platform;
1281 }
1282 }
1283 dev_dbg(card->dev, "CODEC %s not registered\n",
1284 dai_link->codec_name);
1285
1286find_platform:
1287 /* do we already have the CODEC DAI for this link ? */
1288 if (rtd->platform) {
1289 goto out;
1290 }
1291 /* no, then find CPU DAI from registered DAIs*/
1292 list_for_each_entry(platform, &platform_list, list) {
1293 if (!strcmp(platform->name, dai_link->platform_name)) {
1294
1295 if (!try_module_get(platform->dev->driver->owner))
1296 return -ENODEV;
1297
1298 rtd->platform = platform;
1299 goto out;
1300 }
1301 }
1302
1303 dev_dbg(card->dev, "platform %s not registered\n",
1304 dai_link->platform_name);
1305 return 0;
1306
1307out:
1308 /* mark rtd as complete if we found all 4 of our client devices */
1309 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1310 rtd->complete = 1;
1311 card->num_rtd++;
1312 }
1313 return 1;
1314}
1315
1316static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1317{
1318 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1319 struct snd_soc_codec *codec = rtd->codec;
1320 struct snd_soc_platform *platform = rtd->platform;
1321 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1322 int err;
1323
1324 /* unregister the rtd device */
1325 if (rtd->dev_registered) {
1326 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1327 device_unregister(&rtd->dev);
1328 rtd->dev_registered = 0;
1329 }
1330
1331 /* remove the CODEC DAI */
1332 if (codec_dai && codec_dai->probed) {
1333 if (codec_dai->driver->remove) {
1334 err = codec_dai->driver->remove(codec_dai);
1335 if (err < 0)
1336 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1337 }
1338 codec_dai->probed = 0;
1339 list_del(&codec_dai->card_list);
1340 }
1341
1342 /* remove the platform */
1343 if (platform && platform->probed) {
1344 if (platform->driver->remove) {
1345 err = platform->driver->remove(platform);
1346 if (err < 0)
1347 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1348 }
1349 platform->probed = 0;
1350 list_del(&platform->card_list);
1351 module_put(platform->dev->driver->owner);
1352 }
1353
1354 /* remove the CODEC */
1355 if (codec && codec->probed) {
1356 if (codec->driver->remove) {
1357 err = codec->driver->remove(codec);
1358 if (err < 0)
1359 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1360 }
1361
1362 /* Make sure all DAPM widgets are freed */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001363 snd_soc_dapm_free(&codec->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001364
1365 soc_cleanup_codec_debugfs(codec);
1366 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1367 codec->probed = 0;
1368 list_del(&codec->card_list);
1369 module_put(codec->dev->driver->owner);
1370 }
1371
1372 /* remove the cpu_dai */
1373 if (cpu_dai && cpu_dai->probed) {
1374 if (cpu_dai->driver->remove) {
1375 err = cpu_dai->driver->remove(cpu_dai);
1376 if (err < 0)
1377 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1378 }
1379 cpu_dai->probed = 0;
1380 list_del(&cpu_dai->card_list);
1381 module_put(cpu_dai->dev->driver->owner);
1382 }
1383}
1384
1385static void rtd_release(struct device *dev) {}
1386
1387static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1388{
1389 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1390 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1391 struct snd_soc_codec *codec = rtd->codec;
1392 struct snd_soc_platform *platform = rtd->platform;
1393 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1394 int ret;
1395
1396 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1397
1398 /* config components */
1399 codec_dai->codec = codec;
1400 codec->card = card;
1401 cpu_dai->platform = platform;
1402 rtd->card = card;
1403 rtd->dev.parent = card->dev;
1404 codec_dai->card = card;
1405 cpu_dai->card = card;
1406
1407 /* set default power off timeout */
1408 rtd->pmdown_time = pmdown_time;
1409
1410 /* probe the cpu_dai */
1411 if (!cpu_dai->probed) {
1412 if (cpu_dai->driver->probe) {
1413 ret = cpu_dai->driver->probe(cpu_dai);
1414 if (ret < 0) {
1415 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1416 cpu_dai->name);
1417 return ret;
1418 }
1419 }
1420 cpu_dai->probed = 1;
1421 /* mark cpu_dai as probed and add to card cpu_dai list */
1422 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1423 }
1424
1425 /* probe the CODEC */
1426 if (!codec->probed) {
1427 if (codec->driver->probe) {
1428 ret = codec->driver->probe(codec);
1429 if (ret < 0) {
1430 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1431 codec->name);
1432 return ret;
1433 }
1434 }
Mark Brown13cb61f2010-08-12 15:44:04 +01001435
1436 soc_init_codec_debugfs(codec);
1437
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001438 /* mark codec as probed and add to card codec list */
1439 codec->probed = 1;
1440 list_add(&codec->card_list, &card->codec_dev_list);
1441 }
1442
1443 /* probe the platform */
1444 if (!platform->probed) {
1445 if (platform->driver->probe) {
1446 ret = platform->driver->probe(platform);
1447 if (ret < 0) {
1448 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1449 platform->name);
1450 return ret;
1451 }
1452 }
1453 /* mark platform as probed and add to card platform list */
1454 platform->probed = 1;
1455 list_add(&platform->card_list, &card->platform_dev_list);
1456 }
1457
1458 /* probe the CODEC DAI */
1459 if (!codec_dai->probed) {
1460 if (codec_dai->driver->probe) {
1461 ret = codec_dai->driver->probe(codec_dai);
1462 if (ret < 0) {
1463 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1464 codec_dai->name);
1465 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001466 }
1467 }
1468
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001469 /* mark cpu_dai as probed and add to card cpu_dai list */
1470 codec_dai->probed = 1;
1471 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001472 }
1473
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001474 /* DAPM dai link stream work */
1475 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1476
1477 /* now that all clients have probed, initialise the DAI link */
1478 if (dai_link->init) {
1479 ret = dai_link->init(rtd);
1480 if (ret < 0) {
1481 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1482 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001483 }
1484 }
1485
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001486 /* Make sure all DAPM widgets are instantiated */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001487 snd_soc_dapm_new_widgets(&codec->dapm);
1488 snd_soc_dapm_sync(&codec->dapm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001489
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001490 /* register the rtd device */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001491 rtd->dev.release = rtd_release;
1492 rtd->dev.init_name = dai_link->name;
1493 ret = device_register(&rtd->dev);
1494 if (ret < 0) {
1495 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1496 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001497 }
1498
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001499 rtd->dev_registered = 1;
1500 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1501 if (ret < 0)
1502 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1503
1504 /* add DAPM sysfs entries for this codec */
1505 ret = snd_soc_dapm_sys_add(&rtd->dev);
1506 if (ret < 0)
1507 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1508
1509 /* add codec sysfs entries */
1510 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1511 if (ret < 0)
1512 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1513
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001514 /* create the pcm */
1515 ret = soc_new_pcm(rtd, num);
1516 if (ret < 0) {
1517 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1518 return ret;
1519 }
1520
1521 /* add platform data for AC97 devices */
1522 if (rtd->codec_dai->driver->ac97_control)
1523 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1524
1525 return 0;
1526}
1527
1528#ifdef CONFIG_SND_SOC_AC97_BUS
1529static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1530{
1531 int ret;
1532
1533 /* Only instantiate AC97 if not already done by the adaptor
1534 * for the generic AC97 subsystem.
1535 */
1536 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001537 /*
1538 * It is possible that the AC97 device is already registered to
1539 * the device subsystem. This happens when the device is created
1540 * via snd_ac97_mixer(). Currently only SoC codec that does so
1541 * is the generic AC97 glue but others migh emerge.
1542 *
1543 * In those cases we don't try to register the device again.
1544 */
1545 if (!rtd->codec->ac97_created)
1546 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001547
1548 ret = soc_ac97_dev_register(rtd->codec);
1549 if (ret < 0) {
1550 printk(KERN_ERR "asoc: AC97 device register failed\n");
1551 return ret;
1552 }
1553
1554 rtd->codec->ac97_registered = 1;
1555 }
1556 return 0;
1557}
1558
1559static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1560{
1561 if (codec->ac97_registered) {
1562 soc_ac97_dev_unregister(codec);
1563 codec->ac97_registered = 0;
1564 }
1565}
1566#endif
1567
1568static void snd_soc_instantiate_card(struct snd_soc_card *card)
1569{
1570 struct platform_device *pdev = to_platform_device(card->dev);
1571 int ret, i;
1572
1573 mutex_lock(&card->mutex);
1574
1575 if (card->instantiated) {
1576 mutex_unlock(&card->mutex);
1577 return;
1578 }
1579
1580 /* bind DAIs */
1581 for (i = 0; i < card->num_links; i++)
1582 soc_bind_dai_link(card, i);
1583
1584 /* bind completed ? */
1585 if (card->num_rtd != card->num_links) {
1586 mutex_unlock(&card->mutex);
1587 return;
1588 }
1589
1590 /* card bind complete so register a sound card */
1591 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1592 card->owner, 0, &card->snd_card);
1593 if (ret < 0) {
1594 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1595 card->name);
1596 mutex_unlock(&card->mutex);
1597 return;
1598 }
1599 card->snd_card->dev = card->dev;
1600
Randy Dunlap1301a962008-06-17 19:19:34 +01001601#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001602 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001603 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001604#endif
Andy Green6ed25972008-06-13 16:24:05 +01001605
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001606 /* initialise the sound card only once */
1607 if (card->probe) {
1608 ret = card->probe(pdev);
1609 if (ret < 0)
1610 goto card_probe_error;
1611 }
1612
Mark Brownfe3e78e2009-11-03 22:13:13 +00001613 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001614 ret = soc_probe_dai_link(card, i);
1615 if (ret < 0) {
Mark Brown321de0d2010-09-21 15:09:37 +01001616 pr_err("asoc: failed to instantiate card %s: %d\n",
1617 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001618 goto probe_dai_err;
1619 }
1620 }
1621
1622 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1623 "%s", card->name);
1624 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1625 "%s", card->name);
1626
1627 ret = snd_card_register(card->snd_card);
1628 if (ret < 0) {
1629 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1630 goto probe_dai_err;
1631 }
1632
1633#ifdef CONFIG_SND_SOC_AC97_BUS
1634 /* register any AC97 codecs */
1635 for (i = 0; i < card->num_rtd; i++) {
1636 ret = soc_register_ac97_dai_link(&card->rtd[i]);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001637 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001638 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1639 goto probe_dai_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001640 }
1641 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001642#endif
1643
Mark Brown435c5e22008-12-04 15:32:53 +00001644 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001645 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001646 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001647
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001648probe_dai_err:
1649 for (i = 0; i < card->num_links; i++)
1650 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001651
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001652card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001653 if (card->remove)
1654 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001655
1656 snd_card_free(card->snd_card);
1657
1658 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001659}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001660
Mark Brown435c5e22008-12-04 15:32:53 +00001661/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001662 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001663 * client_mutex.
1664 */
1665static void snd_soc_instantiate_cards(void)
1666{
1667 struct snd_soc_card *card;
1668 list_for_each_entry(card, &card_list, list)
1669 snd_soc_instantiate_card(card);
1670}
1671
1672/* probes a new socdev */
1673static int soc_probe(struct platform_device *pdev)
1674{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001675 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001676 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001677
1678 /* Bodge while we unpick instantiation */
1679 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001680 INIT_LIST_HEAD(&card->dai_dev_list);
1681 INIT_LIST_HEAD(&card->codec_dev_list);
1682 INIT_LIST_HEAD(&card->platform_dev_list);
1683
Jarkko Nikulaa6052152010-11-05 20:35:19 +02001684 soc_init_card_debugfs(card);
1685
Mark Brown435c5e22008-12-04 15:32:53 +00001686 ret = snd_soc_register_card(card);
1687 if (ret != 0) {
1688 dev_err(&pdev->dev, "Failed to register card\n");
1689 return ret;
1690 }
1691
1692 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001693}
1694
1695/* removes a socdev */
1696static int soc_remove(struct platform_device *pdev)
1697{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001698 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001699 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001700
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001701 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001702
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001703 /* make sure any delayed work runs */
1704 for (i = 0; i < card->num_rtd; i++) {
1705 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1706 run_delayed_work(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001707 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001708
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001709 /* remove and free each DAI */
1710 for (i = 0; i < card->num_rtd; i++)
1711 soc_remove_dai_link(card, i);
1712
Jarkko Nikulaa6052152010-11-05 20:35:19 +02001713 soc_cleanup_card_debugfs(card);
1714
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001715 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001716 if (card->remove)
1717 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001718
1719 kfree(card->rtd);
1720 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001721 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001722 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001723 return 0;
1724}
1725
Mark Brown416356f2009-06-30 19:05:15 +01001726static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001727{
Mark Brown416356f2009-06-30 19:05:15 +01001728 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001729 struct snd_soc_card *card = platform_get_drvdata(pdev);
1730 int i;
Mark Brown51737472009-06-22 13:16:51 +01001731
1732 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001733 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001734
1735 /* Flush out pmdown_time work - we actually do want to run it
1736 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001737 for (i = 0; i < card->num_rtd; i++) {
1738 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1739 run_delayed_work(&rtd->delayed_work);
1740 }
Mark Brown51737472009-06-22 13:16:51 +01001741
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001742 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001743
1744 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001745}
1746
Alexey Dobriyan47145212009-12-14 18:00:08 -08001747static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001748 .suspend = soc_suspend,
1749 .resume = soc_resume,
1750 .poweroff = soc_poweroff,
1751};
1752
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001753/* ASoC platform driver */
1754static struct platform_driver soc_driver = {
1755 .driver = {
1756 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001757 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001758 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001759 },
1760 .probe = soc_probe,
1761 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001762};
1763
1764/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001765static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001766{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001767 struct snd_soc_codec *codec = rtd->codec;
1768 struct snd_soc_platform *platform = rtd->platform;
1769 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1770 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001771 struct snd_pcm *pcm;
1772 char new_name[64];
1773 int ret = 0, playback = 0, capture = 0;
1774
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001775 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001776 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001777 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001778
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001779 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001780 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001781 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001782 capture = 1;
1783
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001784 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1785 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1786 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001787 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001788 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001789 return ret;
1790 }
1791
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001792 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001793 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001794 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1795 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1796 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1797 soc_pcm_ops.copy = platform->driver->ops->copy;
1798 soc_pcm_ops.silence = platform->driver->ops->silence;
1799 soc_pcm_ops.ack = platform->driver->ops->ack;
1800 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001801
1802 if (playback)
1803 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1804
1805 if (capture)
1806 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1807
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001808 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001809 if (ret < 0) {
1810 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001811 return ret;
1812 }
1813
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001814 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001815 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1816 cpu_dai->name);
1817 return ret;
1818}
1819
Mark Brown096e49d2009-07-05 15:12:22 +01001820/**
1821 * snd_soc_codec_volatile_register: Report if a register is volatile.
1822 *
1823 * @codec: CODEC to query.
1824 * @reg: Register to query.
1825 *
1826 * Boolean function indiciating if a CODEC register is volatile.
1827 */
1828int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1829{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001830 if (codec->driver->volatile_register)
1831 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001832 else
1833 return 0;
1834}
1835EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1836
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001837/**
1838 * snd_soc_new_ac97_codec - initailise AC97 device
1839 * @codec: audio codec
1840 * @ops: AC97 bus operations
1841 * @num: AC97 codec number
1842 *
1843 * Initialises AC97 codec resources for use by ad-hoc devices only.
1844 */
1845int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1846 struct snd_ac97_bus_ops *ops, int num)
1847{
1848 mutex_lock(&codec->mutex);
1849
1850 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1851 if (codec->ac97 == NULL) {
1852 mutex_unlock(&codec->mutex);
1853 return -ENOMEM;
1854 }
1855
1856 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1857 if (codec->ac97->bus == NULL) {
1858 kfree(codec->ac97);
1859 codec->ac97 = NULL;
1860 mutex_unlock(&codec->mutex);
1861 return -ENOMEM;
1862 }
1863
1864 codec->ac97->bus->ops = ops;
1865 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001866
1867 /*
1868 * Mark the AC97 device to be created by us. This way we ensure that the
1869 * device will be registered with the device subsystem later on.
1870 */
1871 codec->ac97_created = 1;
1872
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001873 mutex_unlock(&codec->mutex);
1874 return 0;
1875}
1876EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1877
1878/**
1879 * snd_soc_free_ac97_codec - free AC97 codec device
1880 * @codec: audio codec
1881 *
1882 * Frees AC97 codec device resources.
1883 */
1884void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1885{
1886 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001887#ifdef CONFIG_SND_SOC_AC97_BUS
1888 soc_unregister_ac97_dai_link(codec);
1889#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001890 kfree(codec->ac97->bus);
1891 kfree(codec->ac97);
1892 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001893 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001894 mutex_unlock(&codec->mutex);
1895}
1896EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1897
Mark Brownc3753702010-11-01 15:41:57 -04001898unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1899{
1900 unsigned int ret;
1901
1902 ret = codec->driver->read(codec, reg);
1903 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1904
1905 return ret;
1906}
1907EXPORT_SYMBOL_GPL(snd_soc_read);
1908
1909unsigned int snd_soc_write(struct snd_soc_codec *codec,
1910 unsigned int reg, unsigned int val)
1911{
1912 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1913 return codec->driver->write(codec, reg, val);
1914}
1915EXPORT_SYMBOL_GPL(snd_soc_write);
1916
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001917/**
1918 * snd_soc_update_bits - update codec register bits
1919 * @codec: audio codec
1920 * @reg: codec register
1921 * @mask: register mask
1922 * @value: new value
1923 *
1924 * Writes new register value.
1925 *
1926 * Returns 1 for change else 0.
1927 */
1928int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001929 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001930{
1931 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001932 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001933
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001934 old = snd_soc_read(codec, reg);
1935 new = (old & ~mask) | value;
1936 change = old != new;
1937 if (change)
1938 snd_soc_write(codec, reg, new);
1939
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001940 return change;
1941}
1942EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1943
1944/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001945 * snd_soc_update_bits_locked - update codec register bits
1946 * @codec: audio codec
1947 * @reg: codec register
1948 * @mask: register mask
1949 * @value: new value
1950 *
1951 * Writes new register value, and takes the codec mutex.
1952 *
1953 * Returns 1 for change else 0.
1954 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001955int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1956 unsigned short reg, unsigned int mask,
1957 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001958{
1959 int change;
1960
1961 mutex_lock(&codec->mutex);
1962 change = snd_soc_update_bits(codec, reg, mask, value);
1963 mutex_unlock(&codec->mutex);
1964
1965 return change;
1966}
Mark Browndd1b3d52009-12-04 14:22:03 +00001967EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001968
1969/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001970 * snd_soc_test_bits - test register for change
1971 * @codec: audio codec
1972 * @reg: codec register
1973 * @mask: register mask
1974 * @value: new value
1975 *
1976 * Tests a register with a new value and checks if the new value is
1977 * different from the old value.
1978 *
1979 * Returns 1 for change else 0.
1980 */
1981int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001982 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001983{
1984 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001985 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001986
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001987 old = snd_soc_read(codec, reg);
1988 new = (old & ~mask) | value;
1989 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001990
1991 return change;
1992}
1993EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1994
1995/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001996 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1997 * @substream: the pcm substream
1998 * @hw: the hardware parameters
1999 *
2000 * Sets the substream runtime hardware parameters.
2001 */
2002int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2003 const struct snd_pcm_hardware *hw)
2004{
2005 struct snd_pcm_runtime *runtime = substream->runtime;
2006 runtime->hw.info = hw->info;
2007 runtime->hw.formats = hw->formats;
2008 runtime->hw.period_bytes_min = hw->period_bytes_min;
2009 runtime->hw.period_bytes_max = hw->period_bytes_max;
2010 runtime->hw.periods_min = hw->periods_min;
2011 runtime->hw.periods_max = hw->periods_max;
2012 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2013 runtime->hw.fifo_size = hw->fifo_size;
2014 return 0;
2015}
2016EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2017
2018/**
2019 * snd_soc_cnew - create new control
2020 * @_template: control template
2021 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002022 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002023 *
2024 * Create a new mixer control from a template control.
2025 *
2026 * Returns 0 for success, else error.
2027 */
2028struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2029 void *data, char *long_name)
2030{
2031 struct snd_kcontrol_new template;
2032
2033 memcpy(&template, _template, sizeof(template));
2034 if (long_name)
2035 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002036 template.index = 0;
2037
2038 return snd_ctl_new1(&template, data);
2039}
2040EXPORT_SYMBOL_GPL(snd_soc_cnew);
2041
2042/**
Ian Molton3e8e1952009-01-09 00:23:21 +00002043 * snd_soc_add_controls - add an array of controls to a codec.
2044 * Convienience function to add a list of controls. Many codecs were
2045 * duplicating this code.
2046 *
2047 * @codec: codec to add controls to
2048 * @controls: array of controls to add
2049 * @num_controls: number of elements in the array
2050 *
2051 * Return 0 for success, else error.
2052 */
2053int snd_soc_add_controls(struct snd_soc_codec *codec,
2054 const struct snd_kcontrol_new *controls, int num_controls)
2055{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002056 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002057 int err, i;
2058
2059 for (i = 0; i < num_controls; i++) {
2060 const struct snd_kcontrol_new *control = &controls[i];
2061 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
2062 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002063 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2064 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002065 return err;
2066 }
2067 }
2068
2069 return 0;
2070}
2071EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2072
2073/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002074 * snd_soc_info_enum_double - enumerated double mixer info callback
2075 * @kcontrol: mixer control
2076 * @uinfo: control element information
2077 *
2078 * Callback to provide information about a double enumerated
2079 * mixer control.
2080 *
2081 * Returns 0 for success.
2082 */
2083int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2084 struct snd_ctl_elem_info *uinfo)
2085{
2086 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2087
2088 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2089 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002090 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002091
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002092 if (uinfo->value.enumerated.item > e->max - 1)
2093 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002094 strcpy(uinfo->value.enumerated.name,
2095 e->texts[uinfo->value.enumerated.item]);
2096 return 0;
2097}
2098EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2099
2100/**
2101 * snd_soc_get_enum_double - enumerated double mixer get callback
2102 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002103 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002104 *
2105 * Callback to get the value of a double enumerated mixer.
2106 *
2107 * Returns 0 for success.
2108 */
2109int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2110 struct snd_ctl_elem_value *ucontrol)
2111{
2112 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2113 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002114 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002115
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002116 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002117 ;
2118 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002119 ucontrol->value.enumerated.item[0]
2120 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002121 if (e->shift_l != e->shift_r)
2122 ucontrol->value.enumerated.item[1] =
2123 (val >> e->shift_r) & (bitmask - 1);
2124
2125 return 0;
2126}
2127EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2128
2129/**
2130 * snd_soc_put_enum_double - enumerated double mixer put callback
2131 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002132 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002133 *
2134 * Callback to set the value of a double enumerated mixer.
2135 *
2136 * Returns 0 for success.
2137 */
2138int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2139 struct snd_ctl_elem_value *ucontrol)
2140{
2141 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2142 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002143 unsigned int val;
2144 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002145
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002146 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002147 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002148 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002149 return -EINVAL;
2150 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2151 mask = (bitmask - 1) << e->shift_l;
2152 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002153 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002154 return -EINVAL;
2155 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2156 mask |= (bitmask - 1) << e->shift_r;
2157 }
2158
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002159 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002160}
2161EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2162
2163/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002164 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2165 * @kcontrol: mixer control
2166 * @ucontrol: control element information
2167 *
2168 * Callback to get the value of a double semi enumerated mixer.
2169 *
2170 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2171 * used for handling bitfield coded enumeration for example.
2172 *
2173 * Returns 0 for success.
2174 */
2175int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2176 struct snd_ctl_elem_value *ucontrol)
2177{
2178 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002179 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002180 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002181
2182 reg_val = snd_soc_read(codec, e->reg);
2183 val = (reg_val >> e->shift_l) & e->mask;
2184 for (mux = 0; mux < e->max; mux++) {
2185 if (val == e->values[mux])
2186 break;
2187 }
2188 ucontrol->value.enumerated.item[0] = mux;
2189 if (e->shift_l != e->shift_r) {
2190 val = (reg_val >> e->shift_r) & e->mask;
2191 for (mux = 0; mux < e->max; mux++) {
2192 if (val == e->values[mux])
2193 break;
2194 }
2195 ucontrol->value.enumerated.item[1] = mux;
2196 }
2197
2198 return 0;
2199}
2200EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2201
2202/**
2203 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2204 * @kcontrol: mixer control
2205 * @ucontrol: control element information
2206 *
2207 * Callback to set the value of a double semi enumerated mixer.
2208 *
2209 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2210 * used for handling bitfield coded enumeration for example.
2211 *
2212 * Returns 0 for success.
2213 */
2214int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2215 struct snd_ctl_elem_value *ucontrol)
2216{
2217 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002218 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002219 unsigned int val;
2220 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002221
2222 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2223 return -EINVAL;
2224 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2225 mask = e->mask << e->shift_l;
2226 if (e->shift_l != e->shift_r) {
2227 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2228 return -EINVAL;
2229 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2230 mask |= e->mask << e->shift_r;
2231 }
2232
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002233 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002234}
2235EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2236
2237/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002238 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2239 * @kcontrol: mixer control
2240 * @uinfo: control element information
2241 *
2242 * Callback to provide information about an external enumerated
2243 * single mixer.
2244 *
2245 * Returns 0 for success.
2246 */
2247int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2248 struct snd_ctl_elem_info *uinfo)
2249{
2250 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2251
2252 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2253 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002254 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002255
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002256 if (uinfo->value.enumerated.item > e->max - 1)
2257 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002258 strcpy(uinfo->value.enumerated.name,
2259 e->texts[uinfo->value.enumerated.item]);
2260 return 0;
2261}
2262EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2263
2264/**
2265 * snd_soc_info_volsw_ext - external single mixer info callback
2266 * @kcontrol: mixer control
2267 * @uinfo: control element information
2268 *
2269 * Callback to provide information about a single external mixer control.
2270 *
2271 * Returns 0 for success.
2272 */
2273int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2274 struct snd_ctl_elem_info *uinfo)
2275{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002276 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002277
Mark Brownfd5dfad2009-04-15 21:37:46 +01002278 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002279 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2280 else
2281 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2282
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002283 uinfo->count = 1;
2284 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002285 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002286 return 0;
2287}
2288EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2289
2290/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002291 * snd_soc_info_volsw - single mixer info callback
2292 * @kcontrol: mixer control
2293 * @uinfo: control element information
2294 *
2295 * Callback to provide information about a single mixer control.
2296 *
2297 * Returns 0 for success.
2298 */
2299int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2300 struct snd_ctl_elem_info *uinfo)
2301{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002302 struct soc_mixer_control *mc =
2303 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002304 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002305 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002306 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002307
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002308 if (!mc->platform_max)
2309 mc->platform_max = mc->max;
2310 platform_max = mc->platform_max;
2311
2312 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002313 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2314 else
2315 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2316
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002317 uinfo->count = shift == rshift ? 1 : 2;
2318 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002319 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002320 return 0;
2321}
2322EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2323
2324/**
2325 * snd_soc_get_volsw - single mixer get callback
2326 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002327 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002328 *
2329 * Callback to get the value of a single mixer control.
2330 *
2331 * Returns 0 for success.
2332 */
2333int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_value *ucontrol)
2335{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002336 struct soc_mixer_control *mc =
2337 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002338 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002339 unsigned int reg = mc->reg;
2340 unsigned int shift = mc->shift;
2341 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002342 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002343 unsigned int mask = (1 << fls(max)) - 1;
2344 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002345
2346 ucontrol->value.integer.value[0] =
2347 (snd_soc_read(codec, reg) >> shift) & mask;
2348 if (shift != rshift)
2349 ucontrol->value.integer.value[1] =
2350 (snd_soc_read(codec, reg) >> rshift) & mask;
2351 if (invert) {
2352 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002353 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002354 if (shift != rshift)
2355 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002356 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002357 }
2358
2359 return 0;
2360}
2361EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2362
2363/**
2364 * snd_soc_put_volsw - single mixer put callback
2365 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002366 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002367 *
2368 * Callback to set the value of a single mixer control.
2369 *
2370 * Returns 0 for success.
2371 */
2372int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2373 struct snd_ctl_elem_value *ucontrol)
2374{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002375 struct soc_mixer_control *mc =
2376 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002377 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002378 unsigned int reg = mc->reg;
2379 unsigned int shift = mc->shift;
2380 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002381 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002382 unsigned int mask = (1 << fls(max)) - 1;
2383 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002384 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002385
2386 val = (ucontrol->value.integer.value[0] & mask);
2387 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002388 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002389 val_mask = mask << shift;
2390 val = val << shift;
2391 if (shift != rshift) {
2392 val2 = (ucontrol->value.integer.value[1] & mask);
2393 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002394 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002395 val_mask |= mask << rshift;
2396 val |= val2 << rshift;
2397 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002398 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002399}
2400EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2401
2402/**
2403 * snd_soc_info_volsw_2r - double mixer info callback
2404 * @kcontrol: mixer control
2405 * @uinfo: control element information
2406 *
2407 * Callback to provide information about a double mixer control that
2408 * spans 2 codec registers.
2409 *
2410 * Returns 0 for success.
2411 */
2412int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2413 struct snd_ctl_elem_info *uinfo)
2414{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002415 struct soc_mixer_control *mc =
2416 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002417 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002418
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002419 if (!mc->platform_max)
2420 mc->platform_max = mc->max;
2421 platform_max = mc->platform_max;
2422
2423 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002424 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2425 else
2426 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2427
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002428 uinfo->count = 2;
2429 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002430 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002431 return 0;
2432}
2433EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2434
2435/**
2436 * snd_soc_get_volsw_2r - double mixer get callback
2437 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002438 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002439 *
2440 * Callback to get the value of a double mixer control that spans 2 registers.
2441 *
2442 * Returns 0 for success.
2443 */
2444int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2445 struct snd_ctl_elem_value *ucontrol)
2446{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002447 struct soc_mixer_control *mc =
2448 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002449 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002450 unsigned int reg = mc->reg;
2451 unsigned int reg2 = mc->rreg;
2452 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002453 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002454 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002455 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002456
2457 ucontrol->value.integer.value[0] =
2458 (snd_soc_read(codec, reg) >> shift) & mask;
2459 ucontrol->value.integer.value[1] =
2460 (snd_soc_read(codec, reg2) >> shift) & mask;
2461 if (invert) {
2462 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002463 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002464 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002465 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002466 }
2467
2468 return 0;
2469}
2470EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2471
2472/**
2473 * snd_soc_put_volsw_2r - double mixer set callback
2474 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002475 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002476 *
2477 * Callback to set the value of a double mixer control that spans 2 registers.
2478 *
2479 * Returns 0 for success.
2480 */
2481int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2482 struct snd_ctl_elem_value *ucontrol)
2483{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002484 struct soc_mixer_control *mc =
2485 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002486 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002487 unsigned int reg = mc->reg;
2488 unsigned int reg2 = mc->rreg;
2489 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002490 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002491 unsigned int mask = (1 << fls(max)) - 1;
2492 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002493 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002494 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002495
2496 val_mask = mask << shift;
2497 val = (ucontrol->value.integer.value[0] & mask);
2498 val2 = (ucontrol->value.integer.value[1] & mask);
2499
2500 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002501 val = max - val;
2502 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002503 }
2504
2505 val = val << shift;
2506 val2 = val2 << shift;
2507
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002508 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002509 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002510 return err;
2511
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002512 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002513 return err;
2514}
2515EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2516
Mark Browne13ac2e2008-05-28 17:58:05 +01002517/**
2518 * snd_soc_info_volsw_s8 - signed mixer info callback
2519 * @kcontrol: mixer control
2520 * @uinfo: control element information
2521 *
2522 * Callback to provide information about a signed mixer control.
2523 *
2524 * Returns 0 for success.
2525 */
2526int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2527 struct snd_ctl_elem_info *uinfo)
2528{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002529 struct soc_mixer_control *mc =
2530 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002531 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002532 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002533
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002534 if (!mc->platform_max)
2535 mc->platform_max = mc->max;
2536 platform_max = mc->platform_max;
2537
Mark Browne13ac2e2008-05-28 17:58:05 +01002538 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2539 uinfo->count = 2;
2540 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002541 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002542 return 0;
2543}
2544EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2545
2546/**
2547 * snd_soc_get_volsw_s8 - signed mixer get callback
2548 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002549 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002550 *
2551 * Callback to get the value of a signed mixer control.
2552 *
2553 * Returns 0 for success.
2554 */
2555int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2556 struct snd_ctl_elem_value *ucontrol)
2557{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002558 struct soc_mixer_control *mc =
2559 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002560 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002561 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002562 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002563 int val = snd_soc_read(codec, reg);
2564
2565 ucontrol->value.integer.value[0] =
2566 ((signed char)(val & 0xff))-min;
2567 ucontrol->value.integer.value[1] =
2568 ((signed char)((val >> 8) & 0xff))-min;
2569 return 0;
2570}
2571EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2572
2573/**
2574 * snd_soc_put_volsw_sgn - signed mixer put callback
2575 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002576 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002577 *
2578 * Callback to set the value of a signed mixer control.
2579 *
2580 * Returns 0 for success.
2581 */
2582int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2583 struct snd_ctl_elem_value *ucontrol)
2584{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002585 struct soc_mixer_control *mc =
2586 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002587 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002588 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002589 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002590 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002591
2592 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2593 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2594
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002595 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002596}
2597EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2598
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002599/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002600 * snd_soc_limit_volume - Set new limit to an existing volume control.
2601 *
2602 * @codec: where to look for the control
2603 * @name: Name of the control
2604 * @max: new maximum limit
2605 *
2606 * Return 0 for success, else error.
2607 */
2608int snd_soc_limit_volume(struct snd_soc_codec *codec,
2609 const char *name, int max)
2610{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002611 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002612 struct snd_kcontrol *kctl;
2613 struct soc_mixer_control *mc;
2614 int found = 0;
2615 int ret = -EINVAL;
2616
2617 /* Sanity check for name and max */
2618 if (unlikely(!name || max <= 0))
2619 return -EINVAL;
2620
2621 list_for_each_entry(kctl, &card->controls, list) {
2622 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2623 found = 1;
2624 break;
2625 }
2626 }
2627 if (found) {
2628 mc = (struct soc_mixer_control *)kctl->private_value;
2629 if (max <= mc->max) {
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002630 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002631 ret = 0;
2632 }
2633 }
2634 return ret;
2635}
2636EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2637
2638/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002639 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2640 * mixer info callback
2641 * @kcontrol: mixer control
2642 * @uinfo: control element information
2643 *
2644 * Returns 0 for success.
2645 */
2646int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2647 struct snd_ctl_elem_info *uinfo)
2648{
2649 struct soc_mixer_control *mc =
2650 (struct soc_mixer_control *)kcontrol->private_value;
2651 int max = mc->max;
2652 int min = mc->min;
2653
2654 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2655 uinfo->count = 2;
2656 uinfo->value.integer.min = 0;
2657 uinfo->value.integer.max = max-min;
2658
2659 return 0;
2660}
2661EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2662
2663/**
2664 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2665 * mixer get callback
2666 * @kcontrol: mixer control
2667 * @uinfo: control element information
2668 *
2669 * Returns 0 for success.
2670 */
2671int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2672 struct snd_ctl_elem_value *ucontrol)
2673{
2674 struct soc_mixer_control *mc =
2675 (struct soc_mixer_control *)kcontrol->private_value;
2676 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2677 unsigned int mask = (1<<mc->shift)-1;
2678 int min = mc->min;
2679 int val = snd_soc_read(codec, mc->reg) & mask;
2680 int valr = snd_soc_read(codec, mc->rreg) & mask;
2681
Stuart Longland20630c72010-06-18 12:56:10 +10002682 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2683 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002684 return 0;
2685}
2686EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2687
2688/**
2689 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2690 * mixer put callback
2691 * @kcontrol: mixer control
2692 * @uinfo: control element information
2693 *
2694 * Returns 0 for success.
2695 */
2696int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2697 struct snd_ctl_elem_value *ucontrol)
2698{
2699 struct soc_mixer_control *mc =
2700 (struct soc_mixer_control *)kcontrol->private_value;
2701 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2702 unsigned int mask = (1<<mc->shift)-1;
2703 int min = mc->min;
2704 int ret;
2705 unsigned int val, valr, oval, ovalr;
2706
2707 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2708 val &= mask;
2709 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2710 valr &= mask;
2711
2712 oval = snd_soc_read(codec, mc->reg) & mask;
2713 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2714
2715 ret = 0;
2716 if (oval != val) {
2717 ret = snd_soc_write(codec, mc->reg, val);
2718 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002719 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002720 }
2721 if (ovalr != valr) {
2722 ret = snd_soc_write(codec, mc->rreg, valr);
2723 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002724 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002725 }
2726
2727 return 0;
2728}
2729EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2730
2731/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002732 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2733 * @dai: DAI
2734 * @clk_id: DAI specific clock ID
2735 * @freq: new clock frequency in Hz
2736 * @dir: new clock direction - input/output.
2737 *
2738 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2739 */
2740int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2741 unsigned int freq, int dir)
2742{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002743 if (dai->driver && dai->driver->ops->set_sysclk)
2744 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002745 else
2746 return -EINVAL;
2747}
2748EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2749
2750/**
2751 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2752 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002753 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002754 * @div: new clock divisor.
2755 *
2756 * Configures the clock dividers. This is used to derive the best DAI bit and
2757 * frame clocks from the system or master clock. It's best to set the DAI bit
2758 * and frame clocks as low as possible to save system power.
2759 */
2760int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2761 int div_id, int div)
2762{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002763 if (dai->driver && dai->driver->ops->set_clkdiv)
2764 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002765 else
2766 return -EINVAL;
2767}
2768EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2769
2770/**
2771 * snd_soc_dai_set_pll - configure DAI PLL.
2772 * @dai: DAI
2773 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002774 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002775 * @freq_in: PLL input clock frequency in Hz
2776 * @freq_out: requested PLL output clock frequency in Hz
2777 *
2778 * Configures and enables PLL to generate output clock based on input clock.
2779 */
Mark Brown85488032009-09-05 18:52:16 +01002780int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2781 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002782{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002783 if (dai->driver && dai->driver->ops->set_pll)
2784 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002785 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002786 else
2787 return -EINVAL;
2788}
2789EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2790
2791/**
2792 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2793 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002794 * @fmt: SND_SOC_DAIFMT_ format value.
2795 *
2796 * Configures the DAI hardware format and clocking.
2797 */
2798int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2799{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002800 if (dai->driver && dai->driver->ops->set_fmt)
2801 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002802 else
2803 return -EINVAL;
2804}
2805EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2806
2807/**
2808 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2809 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002810 * @tx_mask: bitmask representing active TX slots.
2811 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002812 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002813 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002814 *
2815 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2816 * specific.
2817 */
2818int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002819 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002820{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002821 if (dai->driver && dai->driver->ops->set_tdm_slot)
2822 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002823 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002824 else
2825 return -EINVAL;
2826}
2827EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2828
2829/**
Barry Song472df3c2009-09-12 01:16:29 +08002830 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2831 * @dai: DAI
2832 * @tx_num: how many TX channels
2833 * @tx_slot: pointer to an array which imply the TX slot number channel
2834 * 0~num-1 uses
2835 * @rx_num: how many RX channels
2836 * @rx_slot: pointer to an array which imply the RX slot number channel
2837 * 0~num-1 uses
2838 *
2839 * configure the relationship between channel number and TDM slot number.
2840 */
2841int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2842 unsigned int tx_num, unsigned int *tx_slot,
2843 unsigned int rx_num, unsigned int *rx_slot)
2844{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002845 if (dai->driver && dai->driver->ops->set_channel_map)
2846 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002847 rx_num, rx_slot);
2848 else
2849 return -EINVAL;
2850}
2851EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2852
2853/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002854 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2855 * @dai: DAI
2856 * @tristate: tristate enable
2857 *
2858 * Tristates the DAI so that others can use it.
2859 */
2860int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2861{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002862 if (dai->driver && dai->driver->ops->set_tristate)
2863 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002864 else
2865 return -EINVAL;
2866}
2867EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2868
2869/**
2870 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2871 * @dai: DAI
2872 * @mute: mute enable
2873 *
2874 * Mutes the DAI DAC.
2875 */
2876int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2877{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002878 if (dai->driver && dai->driver->ops->digital_mute)
2879 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002880 else
2881 return -EINVAL;
2882}
2883EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2884
Mark Brownc5af3a22008-11-28 13:29:45 +00002885/**
2886 * snd_soc_register_card - Register a card with the ASoC core
2887 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002888 * @card: Card to register
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_register_card(struct snd_soc_card *card)
2895{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002896 int i;
2897
Mark Brownc5af3a22008-11-28 13:29:45 +00002898 if (!card->name || !card->dev)
2899 return -EINVAL;
2900
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002901 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2902 GFP_KERNEL);
2903 if (card->rtd == NULL)
2904 return -ENOMEM;
2905
2906 for (i = 0; i < card->num_links; i++)
2907 card->rtd[i].dai_link = &card->dai_link[i];
2908
Mark Brownc5af3a22008-11-28 13:29:45 +00002909 INIT_LIST_HEAD(&card->list);
2910 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002911 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002912
2913 mutex_lock(&client_mutex);
2914 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002915 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002916 mutex_unlock(&client_mutex);
2917
2918 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2919
2920 return 0;
2921}
2922
2923/**
2924 * snd_soc_unregister_card - Unregister a card with the ASoC core
2925 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002926 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002927 *
2928 * Note that currently this is an internal only function: it will be
2929 * exposed to machine drivers after further backporting of ASoC v2
2930 * registration APIs.
2931 */
2932static int snd_soc_unregister_card(struct snd_soc_card *card)
2933{
2934 mutex_lock(&client_mutex);
2935 list_del(&card->list);
2936 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002937 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2938
2939 return 0;
2940}
2941
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002942/*
2943 * Simplify DAI link configuration by removing ".-1" from device names
2944 * and sanitizing names.
2945 */
2946static inline char *fmt_single_name(struct device *dev, int *id)
2947{
2948 char *found, name[NAME_SIZE];
2949 int id1, id2;
2950
2951 if (dev_name(dev) == NULL)
2952 return NULL;
2953
2954 strncpy(name, dev_name(dev), NAME_SIZE);
2955
2956 /* are we a "%s.%d" name (platform and SPI components) */
2957 found = strstr(name, dev->driver->name);
2958 if (found) {
2959 /* get ID */
2960 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2961
2962 /* discard ID from name if ID == -1 */
2963 if (*id == -1)
2964 found[strlen(dev->driver->name)] = '\0';
2965 }
2966
2967 } else {
2968 /* I2C component devices are named "bus-addr" */
2969 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2970 char tmp[NAME_SIZE];
2971
2972 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002973 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002974
2975 /* sanitize component name for DAI link creation */
2976 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2977 strncpy(name, tmp, NAME_SIZE);
2978 } else
2979 *id = 0;
2980 }
2981
2982 return kstrdup(name, GFP_KERNEL);
2983}
2984
2985/*
2986 * Simplify DAI link naming for single devices with multiple DAIs by removing
2987 * any ".-1" and using the DAI name (instead of device name).
2988 */
2989static inline char *fmt_multiple_name(struct device *dev,
2990 struct snd_soc_dai_driver *dai_drv)
2991{
2992 if (dai_drv->name == NULL) {
2993 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2994 dev_name(dev));
2995 return NULL;
2996 }
2997
2998 return kstrdup(dai_drv->name, GFP_KERNEL);
2999}
3000
Mark Brown91151712008-11-30 23:31:24 +00003001/**
3002 * snd_soc_register_dai - Register a DAI with the ASoC core
3003 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003004 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003005 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003006int snd_soc_register_dai(struct device *dev,
3007 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003008{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003009 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003010
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003011 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003012
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003013 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3014 if (dai == NULL)
3015 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003016
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003017 /* create DAI component name */
3018 dai->name = fmt_single_name(dev, &dai->id);
3019 if (dai->name == NULL) {
3020 kfree(dai);
3021 return -ENOMEM;
3022 }
3023
3024 dai->dev = dev;
3025 dai->driver = dai_drv;
3026 if (!dai->driver->ops)
3027 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003028
3029 mutex_lock(&client_mutex);
3030 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003031 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003032 mutex_unlock(&client_mutex);
3033
3034 pr_debug("Registered DAI '%s'\n", dai->name);
3035
3036 return 0;
3037}
3038EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3039
3040/**
3041 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3042 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003043 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003044 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003045void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003046{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003047 struct snd_soc_dai *dai;
3048
3049 list_for_each_entry(dai, &dai_list, list) {
3050 if (dev == dai->dev)
3051 goto found;
3052 }
3053 return;
3054
3055found:
Mark Brown91151712008-11-30 23:31:24 +00003056 mutex_lock(&client_mutex);
3057 list_del(&dai->list);
3058 mutex_unlock(&client_mutex);
3059
3060 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003061 kfree(dai->name);
3062 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003063}
3064EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3065
3066/**
3067 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3068 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003069 * @dai: Array of DAIs to register
3070 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003071 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003072int snd_soc_register_dais(struct device *dev,
3073 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003074{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003075 struct snd_soc_dai *dai;
3076 int i, ret = 0;
3077
Liam Girdwood720ffa42010-08-18 00:30:30 +01003078 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003079
3080 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003081
3082 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003083 if (dai == NULL) {
3084 ret = -ENOMEM;
3085 goto err;
3086 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003087
3088 /* create DAI component name */
3089 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3090 if (dai->name == NULL) {
3091 kfree(dai);
3092 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003093 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003094 }
3095
3096 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003097 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003098 if (dai->driver->id)
3099 dai->id = dai->driver->id;
3100 else
3101 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003102 if (!dai->driver->ops)
3103 dai->driver->ops = &null_dai_ops;
3104
3105 mutex_lock(&client_mutex);
3106 list_add(&dai->list, &dai_list);
3107 mutex_unlock(&client_mutex);
3108
3109 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003110 }
3111
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003112 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003113 return 0;
3114
3115err:
3116 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003117 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003118
3119 return ret;
3120}
3121EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3122
3123/**
3124 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3125 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003126 * @dai: Array of DAIs to unregister
3127 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003128 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003129void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003130{
3131 int i;
3132
3133 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003134 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003135}
3136EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3137
Mark Brown12a48a8c2008-12-03 19:40:30 +00003138/**
3139 * snd_soc_register_platform - Register a platform with the ASoC core
3140 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003141 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003142 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003143int snd_soc_register_platform(struct device *dev,
3144 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003145{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003146 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003147
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003148 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3149
3150 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3151 if (platform == NULL)
3152 return -ENOMEM;
3153
3154 /* create platform component name */
3155 platform->name = fmt_single_name(dev, &platform->id);
3156 if (platform->name == NULL) {
3157 kfree(platform);
3158 return -ENOMEM;
3159 }
3160
3161 platform->dev = dev;
3162 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003163
3164 mutex_lock(&client_mutex);
3165 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003166 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003167 mutex_unlock(&client_mutex);
3168
3169 pr_debug("Registered platform '%s'\n", platform->name);
3170
3171 return 0;
3172}
3173EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3174
3175/**
3176 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3177 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003178 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003179 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003180void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003181{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003182 struct snd_soc_platform *platform;
3183
3184 list_for_each_entry(platform, &platform_list, list) {
3185 if (dev == platform->dev)
3186 goto found;
3187 }
3188 return;
3189
3190found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003191 mutex_lock(&client_mutex);
3192 list_del(&platform->list);
3193 mutex_unlock(&client_mutex);
3194
3195 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003196 kfree(platform->name);
3197 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003198}
3199EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3200
Mark Brown151ab222009-05-09 16:22:58 +01003201static u64 codec_format_map[] = {
3202 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3203 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3204 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3205 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3206 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3207 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3208 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3209 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3210 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3211 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3212 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3213 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3214 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3215 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3216 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3217 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3218};
3219
3220/* Fix up the DAI formats for endianness: codecs don't actually see
3221 * the endianness of the data but we're using the CPU format
3222 * definitions which do need to include endianness so we ensure that
3223 * codec DAIs always have both big and little endian variants set.
3224 */
3225static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3226{
3227 int i;
3228
3229 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3230 if (stream->formats & codec_format_map[i])
3231 stream->formats |= codec_format_map[i];
3232}
3233
Mark Brown0d0cf002008-12-10 14:32:45 +00003234/**
3235 * snd_soc_register_codec - Register a codec with the ASoC core
3236 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003237 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003238 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003239int snd_soc_register_codec(struct device *dev,
3240 struct snd_soc_codec_driver *codec_drv,
3241 struct snd_soc_dai_driver *dai_drv, int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003242{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003243 struct snd_soc_codec *codec;
3244 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003245
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003246 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003247
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003248 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3249 if (codec == NULL)
3250 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003251
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003252 /* create CODEC component name */
3253 codec->name = fmt_single_name(dev, &codec->id);
3254 if (codec->name == NULL) {
3255 kfree(codec);
3256 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003257 }
3258
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003259 INIT_LIST_HEAD(&codec->dapm.widgets);
3260 INIT_LIST_HEAD(&codec->dapm.paths);
3261 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3262 codec->dapm.dev = dev;
3263 codec->dapm.codec = codec;
3264
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003265 /* allocate CODEC register cache */
3266 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3267
3268 if (codec_drv->reg_cache_default)
3269 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3270 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3271 else
3272 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3273 codec_drv->reg_word_size, GFP_KERNEL);
3274
3275 if (codec->reg_cache == NULL) {
3276 kfree(codec->name);
3277 kfree(codec);
3278 return -ENOMEM;
3279 }
3280 }
3281
3282 codec->dev = dev;
3283 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003284 codec->num_dai = num_dai;
3285 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003286
3287 for (i = 0; i < num_dai; i++) {
3288 fixup_codec_formats(&dai_drv[i].playback);
3289 fixup_codec_formats(&dai_drv[i].capture);
3290 }
3291
Mark Brown26b01cc2010-08-18 20:20:55 +01003292 /* register any DAIs */
3293 if (num_dai) {
3294 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3295 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003296 goto error;
Mark Brown26b01cc2010-08-18 20:20:55 +01003297 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003298
Mark Brown0d0cf002008-12-10 14:32:45 +00003299 mutex_lock(&client_mutex);
3300 list_add(&codec->list, &codec_list);
3301 snd_soc_instantiate_cards();
3302 mutex_unlock(&client_mutex);
3303
3304 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003305 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003306
3307error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003308 if (codec->reg_cache)
3309 kfree(codec->reg_cache);
3310 kfree(codec->name);
3311 kfree(codec);
3312 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003313}
3314EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3315
3316/**
3317 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3318 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003319 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003320 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003321void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003322{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003323 struct snd_soc_codec *codec;
3324 int i;
3325
3326 list_for_each_entry(codec, &codec_list, list) {
3327 if (dev == codec->dev)
3328 goto found;
3329 }
3330 return;
3331
3332found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003333 if (codec->num_dai)
3334 for (i = 0; i < codec->num_dai; i++)
3335 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003336
Mark Brown0d0cf002008-12-10 14:32:45 +00003337 mutex_lock(&client_mutex);
3338 list_del(&codec->list);
3339 mutex_unlock(&client_mutex);
3340
3341 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003342
3343 if (codec->reg_cache)
3344 kfree(codec->reg_cache);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003345 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003346 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003347}
3348EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3349
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003350static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003351{
Mark Brown384c89e2008-12-03 17:34:03 +00003352#ifdef CONFIG_DEBUG_FS
3353 debugfs_root = debugfs_create_dir("asoc", NULL);
3354 if (IS_ERR(debugfs_root) || !debugfs_root) {
3355 printk(KERN_WARNING
3356 "ASoC: Failed to create debugfs directory\n");
3357 debugfs_root = NULL;
3358 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003359
3360 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3361 &codec_list_fops))
3362 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3363
Mark Brownf3208782010-09-15 18:19:07 +01003364 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3365 &dai_list_fops))
3366 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003367
3368 if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
3369 &platform_list_fops))
3370 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003371#endif
3372
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003373 return platform_driver_register(&soc_driver);
3374}
Mark Brown4abe8e12010-10-12 17:41:03 +01003375module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003376
Mark Brown7d8c16a2008-11-30 22:11:24 +00003377static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003378{
Mark Brown384c89e2008-12-03 17:34:03 +00003379#ifdef CONFIG_DEBUG_FS
3380 debugfs_remove_recursive(debugfs_root);
3381#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003382 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003383}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003384module_exit(snd_soc_exit);
3385
3386/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003387MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003388MODULE_DESCRIPTION("ALSA SoC Core");
3389MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003390MODULE_ALIAS("platform:soc-audio");