blob: 57e5d7bfb130636705a1d9356d5779f5442555e2 [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{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200241 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
242
243 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
244 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000245 if (!codec->debugfs_codec_root) {
246 printk(KERN_WARNING
247 "ASoC: Failed to create codec debugfs directory\n");
248 return;
249 }
250
251 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
252 codec->debugfs_codec_root,
253 codec, &codec_reg_fops);
254 if (!codec->debugfs_reg)
255 printk(KERN_WARNING
256 "ASoC: Failed to create codec register debugfs file\n");
257
Axel Lin708fafb2010-08-27 10:34:27 +0800258 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
Mark Brown2624d5f2009-11-03 21:56:13 +0000259 codec->debugfs_codec_root,
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200260 &codec->dapm.pop_time);
Mark Brown2624d5f2009-11-03 21:56:13 +0000261 if (!codec->debugfs_pop_time)
262 printk(KERN_WARNING
263 "Failed to create pop time debugfs file\n");
264
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200265 codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
Mark Brown2624d5f2009-11-03 21:56:13 +0000266 codec->debugfs_codec_root);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200267 if (!codec->dapm.debugfs_dapm)
Mark Brown2624d5f2009-11-03 21:56:13 +0000268 printk(KERN_WARNING
269 "Failed to create DAPM debugfs directory\n");
270
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200271 snd_soc_dapm_debugfs_init(&codec->dapm);
Mark Brown2624d5f2009-11-03 21:56:13 +0000272}
273
274static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
275{
276 debugfs_remove_recursive(codec->debugfs_codec_root);
277}
278
Mark Brownc3c5a192010-09-15 18:15:14 +0100279static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
280 size_t count, loff_t *ppos)
281{
282 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100283 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100284 struct snd_soc_codec *codec;
285
286 if (!buf)
287 return -ENOMEM;
288
Mark Brown2b194f9d2010-10-13 10:52:16 +0100289 list_for_each_entry(codec, &codec_list, list) {
290 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
291 codec->name);
292 if (len >= 0)
293 ret += len;
294 if (ret > PAGE_SIZE) {
295 ret = PAGE_SIZE;
296 break;
297 }
298 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100299
300 if (ret >= 0)
301 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
302
303 kfree(buf);
304
305 return ret;
306}
307
308static const struct file_operations codec_list_fops = {
309 .read = codec_list_read_file,
310 .llseek = default_llseek,/* read accesses f_pos */
311};
312
Mark Brownf3208782010-09-15 18:19:07 +0100313static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
314 size_t count, loff_t *ppos)
315{
316 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100317 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100318 struct snd_soc_dai *dai;
319
320 if (!buf)
321 return -ENOMEM;
322
Mark Brown2b194f9d2010-10-13 10:52:16 +0100323 list_for_each_entry(dai, &dai_list, list) {
324 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
325 if (len >= 0)
326 ret += len;
327 if (ret > PAGE_SIZE) {
328 ret = PAGE_SIZE;
329 break;
330 }
331 }
Mark Brownf3208782010-09-15 18:19:07 +0100332
Mark Brown2b194f9d2010-10-13 10:52:16 +0100333 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100334
335 kfree(buf);
336
337 return ret;
338}
339
340static const struct file_operations dai_list_fops = {
341 .read = dai_list_read_file,
342 .llseek = default_llseek,/* read accesses f_pos */
343};
344
Mark Brown19c7ac22010-09-15 18:22:40 +0100345static ssize_t platform_list_read_file(struct file *file,
346 char __user *user_buf,
347 size_t count, loff_t *ppos)
348{
349 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100350 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100351 struct snd_soc_platform *platform;
352
353 if (!buf)
354 return -ENOMEM;
355
Mark Brown2b194f9d2010-10-13 10:52:16 +0100356 list_for_each_entry(platform, &platform_list, list) {
357 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
358 platform->name);
359 if (len >= 0)
360 ret += len;
361 if (ret > PAGE_SIZE) {
362 ret = PAGE_SIZE;
363 break;
364 }
365 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100366
Mark Brown2b194f9d2010-10-13 10:52:16 +0100367 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100368
369 kfree(buf);
370
371 return ret;
372}
373
374static const struct file_operations platform_list_fops = {
375 .read = platform_list_read_file,
376 .llseek = default_llseek,/* read accesses f_pos */
377};
378
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200379static void soc_init_card_debugfs(struct snd_soc_card *card)
380{
381 card->debugfs_card_root = debugfs_create_dir(card->name,
382 debugfs_root);
383 if (!card->debugfs_card_root)
384 dev_warn(card->dev,
385 "ASoC: Failed to create codec debugfs directory\n");
386}
387
388static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
389{
390 debugfs_remove_recursive(card->debugfs_card_root);
391}
392
Mark Brown2624d5f2009-11-03 21:56:13 +0000393#else
394
395static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
396{
397}
398
399static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
400{
401}
402#endif
403
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200404#ifdef CONFIG_SND_SOC_AC97_BUS
405/* unregister ac97 codec */
406static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
407{
408 if (codec->ac97->dev.bus)
409 device_unregister(&codec->ac97->dev);
410 return 0;
411}
412
413/* stop no dev release warning */
414static void soc_ac97_device_release(struct device *dev){}
415
416/* register ac97 codec to bus */
417static int soc_ac97_dev_register(struct snd_soc_codec *codec)
418{
419 int err;
420
421 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100422 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200423 codec->ac97->dev.release = soc_ac97_device_release;
424
Kay Sieversbb072bf2008-11-02 03:50:35 +0100425 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000426 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200427 err = device_register(&codec->ac97->dev);
428 if (err < 0) {
429 snd_printk(KERN_ERR "Can't register ac97 bus\n");
430 codec->ac97->dev.bus = NULL;
431 return err;
432 }
433 return 0;
434}
435#endif
436
Mark Brown06f409d2009-04-07 18:10:13 +0100437static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
438{
439 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000440 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
441 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100442 int ret;
443
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000444 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
445 rtd->dai_link->symmetric_rates) {
446 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
447 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100448
449 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
450 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000451 rtd->rate,
452 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100453 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000454 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100455 "Unable to apply rate symmetry constraint: %d\n", ret);
456 return ret;
457 }
458 }
459
460 return 0;
461}
462
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200463/*
464 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
465 * then initialized and any private data can be allocated. This also calls
466 * startup for the cpu DAI, platform, machine and codec DAI.
467 */
468static int soc_pcm_open(struct snd_pcm_substream *substream)
469{
470 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200471 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000472 struct snd_soc_platform *platform = rtd->platform;
473 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
474 struct snd_soc_dai *codec_dai = rtd->codec_dai;
475 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
476 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200477 int ret = 0;
478
479 mutex_lock(&pcm_mutex);
480
481 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000482 if (cpu_dai->driver->ops->startup) {
483 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200484 if (ret < 0) {
485 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100486 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200487 goto out;
488 }
489 }
490
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000491 if (platform->driver->ops->open) {
492 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200493 if (ret < 0) {
494 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
495 goto platform_err;
496 }
497 }
498
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000499 if (codec_dai->driver->ops->startup) {
500 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100501 if (ret < 0) {
502 printk(KERN_ERR "asoc: can't open codec %s\n",
503 codec_dai->name);
504 goto codec_dai_err;
505 }
506 }
507
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
509 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200510 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000511 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200512 goto machine_err;
513 }
514 }
515
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200516 /* Check that the codec and cpu DAI's are compatible */
517 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
518 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 max(codec_dai_drv->playback.rate_min,
520 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200521 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 min(codec_dai_drv->playback.rate_max,
523 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200524 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000525 max(codec_dai_drv->playback.channels_min,
526 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200527 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000528 min(codec_dai_drv->playback.channels_max,
529 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100530 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100532 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000533 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
534 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900535 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000536 runtime->hw.rates |= cpu_dai_drv->playback.rates;
537 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900538 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000539 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200540 } else {
541 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000542 max(codec_dai_drv->capture.rate_min,
543 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200544 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000545 min(codec_dai_drv->capture.rate_max,
546 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200547 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548 max(codec_dai_drv->capture.channels_min,
549 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200550 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000551 min(codec_dai_drv->capture.channels_max,
552 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100553 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100555 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000556 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
557 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900558 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559 runtime->hw.rates |= cpu_dai_drv->capture.rates;
560 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900561 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200563 }
564
565 snd_pcm_limit_hw_rates(runtime);
566 if (!runtime->hw.rates) {
567 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100568 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900569 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200570 }
571 if (!runtime->hw.formats) {
572 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100573 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900574 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200575 }
576 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
577 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000578 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900579 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200580 }
581
Mark Brown06f409d2009-04-07 18:10:13 +0100582 /* Symmetry only applies if we've already got an active stream. */
583 if (cpu_dai->active || codec_dai->active) {
584 ret = soc_pcm_apply_symmetry(substream);
585 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900586 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100587 }
588
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000589 pr_debug("asoc: %s <-> %s info:\n",
590 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100591 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
592 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
593 runtime->hw.channels_max);
594 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
595 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596
Jassi Brar14dc5732010-02-26 09:12:32 +0900597 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 cpu_dai->playback_active++;
599 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900600 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 cpu_dai->capture_active++;
602 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900603 }
604 cpu_dai->active++;
605 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000606 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200607 mutex_unlock(&pcm_mutex);
608 return 0;
609
Jassi Brarbb1c0472010-02-25 11:24:53 +0900610config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000611 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
612 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200613
Jassi Brarbb1c0472010-02-25 11:24:53 +0900614machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000615 if (codec_dai->driver->ops->shutdown)
616 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900617
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100618codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000619 if (platform->driver->ops->close)
620 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200621
622platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623 if (cpu_dai->driver->ops->shutdown)
624 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200625out:
626 mutex_unlock(&pcm_mutex);
627 return ret;
628}
629
630/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200631 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200632 * This is to ensure there are no pops or clicks in between any music tracks
633 * due to DAPM power cycling.
634 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100635static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200636{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000637 struct snd_soc_pcm_runtime *rtd =
638 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
639 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200640
641 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200642
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000643 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
644 codec_dai->driver->playback.stream_name,
645 codec_dai->playback_active ? "active" : "inactive",
646 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200647
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000648 /* are we waiting on this codec DAI stream */
649 if (codec_dai->pop_wait == 1) {
650 codec_dai->pop_wait = 0;
651 snd_soc_dapm_stream_event(rtd,
652 codec_dai->driver->playback.stream_name,
653 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000655
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200656 mutex_unlock(&pcm_mutex);
657}
658
659/*
660 * Called by ALSA when a PCM substream is closed. Private data can be
661 * freed here. The cpu DAI, codec DAI, machine and platform are also
662 * shutdown.
663 */
664static int soc_codec_close(struct snd_pcm_substream *substream)
665{
666 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000667 struct snd_soc_platform *platform = rtd->platform;
668 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
669 struct snd_soc_dai *codec_dai = rtd->codec_dai;
670 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200671
672 mutex_lock(&pcm_mutex);
673
Jassi Brar14dc5732010-02-26 09:12:32 +0900674 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000675 cpu_dai->playback_active--;
676 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900677 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000678 cpu_dai->capture_active--;
679 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200680 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900681
682 cpu_dai->active--;
683 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200684 codec->active--;
685
Mark Brown6010b2d2008-09-06 18:33:24 +0100686 /* Muting the DAC suppresses artifacts caused during digital
687 * shutdown, for example from stopping clocks.
688 */
689 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
690 snd_soc_dai_digital_mute(codec_dai, 1);
691
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000692 if (cpu_dai->driver->ops->shutdown)
693 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200694
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000695 if (codec_dai->driver->ops->shutdown)
696 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200697
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000698 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
699 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000701 if (platform->driver->ops->close)
702 platform->driver->ops->close(substream);
703 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200704
705 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
706 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100707 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000708 schedule_delayed_work(&rtd->delayed_work,
709 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200710 } else {
711 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000712 snd_soc_dapm_stream_event(rtd,
713 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100714 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200715 }
716
717 mutex_unlock(&pcm_mutex);
718 return 0;
719}
720
721/*
722 * Called by ALSA when the PCM substream is prepared, can set format, sample
723 * rate, etc. This function is non atomic and can be called multiple times,
724 * it can refer to the runtime info.
725 */
726static int soc_pcm_prepare(struct snd_pcm_substream *substream)
727{
728 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000729 struct snd_soc_platform *platform = rtd->platform;
730 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
731 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200732 int ret = 0;
733
734 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100735
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000736 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
737 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100738 if (ret < 0) {
739 printk(KERN_ERR "asoc: machine prepare error\n");
740 goto out;
741 }
742 }
743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000744 if (platform->driver->ops->prepare) {
745 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200746 if (ret < 0) {
747 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200748 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200749 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750 }
751
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000752 if (codec_dai->driver->ops->prepare) {
753 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200754 if (ret < 0) {
755 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200756 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200757 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200758 }
759
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000760 if (cpu_dai->driver->ops->prepare) {
761 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100762 if (ret < 0) {
763 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
764 goto out;
765 }
766 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200767
Mark Brownd45f6212008-10-14 13:58:36 +0100768 /* cancel any delayed stream shutdown that is pending */
769 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
770 codec_dai->pop_wait) {
771 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000772 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100773 }
774
Mark Brown452c5ea2009-05-17 21:41:23 +0100775 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000776 snd_soc_dapm_stream_event(rtd,
777 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100778 SND_SOC_DAPM_STREAM_START);
779 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000780 snd_soc_dapm_stream_event(rtd,
781 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100782 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100783
Mark Brown452c5ea2009-05-17 21:41:23 +0100784 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200785
786out:
787 mutex_unlock(&pcm_mutex);
788 return ret;
789}
790
791/*
792 * Called by ALSA when the hardware params are set by application. This
793 * function can also be called multiple times and can allocate buffers
794 * (using snd_pcm_lib_* ). It's non-atomic.
795 */
796static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
797 struct snd_pcm_hw_params *params)
798{
799 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000800 struct snd_soc_platform *platform = rtd->platform;
801 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
802 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200803 int ret = 0;
804
805 mutex_lock(&pcm_mutex);
806
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000807 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
808 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200809 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100810 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200811 goto out;
812 }
813 }
814
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000815 if (codec_dai->driver->ops->hw_params) {
816 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100817 if (ret < 0) {
818 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
819 codec_dai->name);
820 goto codec_err;
821 }
822 }
823
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000824 if (cpu_dai->driver->ops->hw_params) {
825 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200826 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200827 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100828 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200829 goto interface_err;
830 }
831 }
832
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000833 if (platform->driver->ops->hw_params) {
834 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200835 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200836 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200837 platform->name);
838 goto platform_err;
839 }
840 }
841
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000842 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100843
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200844out:
845 mutex_unlock(&pcm_mutex);
846 return ret;
847
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200848platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000849 if (cpu_dai->driver->ops->hw_free)
850 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200851
852interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000853 if (codec_dai->driver->ops->hw_free)
854 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100855
856codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
858 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200859
860 mutex_unlock(&pcm_mutex);
861 return ret;
862}
863
864/*
865 * Free's resources allocated by hw_params, can be called multiple times
866 */
867static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
868{
869 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000870 struct snd_soc_platform *platform = rtd->platform;
871 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
872 struct snd_soc_dai *codec_dai = rtd->codec_dai;
873 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200874
875 mutex_lock(&pcm_mutex);
876
877 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100878 if (!codec->active)
879 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200880
881 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000882 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
883 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200884
885 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000886 if (platform->driver->ops->hw_free)
887 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200888
889 /* now free hw params for the DAI's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000890 if (codec_dai->driver->ops->hw_free)
891 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200892
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000893 if (cpu_dai->driver->ops->hw_free)
894 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200895
896 mutex_unlock(&pcm_mutex);
897 return 0;
898}
899
900static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
901{
902 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000903 struct snd_soc_platform *platform = rtd->platform;
904 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
905 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200906 int ret;
907
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000908 if (codec_dai->driver->ops->trigger) {
909 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200910 if (ret < 0)
911 return ret;
912 }
913
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000914 if (platform->driver->ops->trigger) {
915 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200916 if (ret < 0)
917 return ret;
918 }
919
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000920 if (cpu_dai->driver->ops->trigger) {
921 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200922 if (ret < 0)
923 return ret;
924 }
925 return 0;
926}
927
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200928/*
929 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200930 * If cpu_dai, codec_dai, platform driver has the delay callback, than
931 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200932 */
933static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
934{
935 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000936 struct snd_soc_platform *platform = rtd->platform;
937 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
938 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200939 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200940 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200941 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200942
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000943 if (platform->driver->ops->pointer)
944 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200945
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000946 if (cpu_dai->driver->ops->delay)
947 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200948
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000949 if (codec_dai->driver->ops->delay)
950 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200951
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000952 if (platform->driver->delay)
953 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200954
955 runtime->delay = delay;
956
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200957 return offset;
958}
959
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200960/* ASoC PCM operations */
961static struct snd_pcm_ops soc_pcm_ops = {
962 .open = soc_pcm_open,
963 .close = soc_codec_close,
964 .hw_params = soc_pcm_hw_params,
965 .hw_free = soc_pcm_hw_free,
966 .prepare = soc_pcm_prepare,
967 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200968 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200969};
970
971#ifdef CONFIG_PM
972/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100973static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200974{
Mark Brown416356f2009-06-30 19:05:15 +0100975 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000976 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200977 int i;
978
Daniel Macke3509ff2009-06-03 17:44:49 +0200979 /* If the initialization of this soc device failed, there is no codec
980 * associated with it. Just bail out in this case.
981 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000982 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200983 return 0;
984
Andy Green6ed25972008-06-13 16:24:05 +0100985 /* Due to the resume being scheduled into a workqueue we could
986 * suspend before that's finished - wait for it to complete.
987 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000988 snd_power_lock(card->snd_card);
989 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
990 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100991
992 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000993 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100994
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200995 /* mute any active DAC's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000996 for (i = 0; i < card->num_rtd; i++) {
997 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
998 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100999
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001000 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001001 continue;
1002
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001003 if (drv->ops->digital_mute && dai->playback_active)
1004 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001005 }
1006
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001007 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001008 for (i = 0; i < card->num_rtd; i++) {
1009 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001010 continue;
1011
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001012 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +01001013 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001014
Mark Brown87506542008-11-18 20:50:34 +00001015 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +01001016 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001017
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001018 for (i = 0; i < card->num_rtd; i++) {
1019 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1020 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001021
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001022 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001023 continue;
1024
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001025 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1026 cpu_dai->driver->suspend(cpu_dai);
1027 if (platform->driver->suspend && !platform->suspended) {
1028 platform->driver->suspend(cpu_dai);
1029 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +01001030 }
1031 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001032
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001033 /* close any waiting streams and save state */
1034 for (i = 0; i < card->num_rtd; i++) {
1035 run_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001036 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001037 }
Mark Brown3efab7d2010-05-09 13:25:43 +01001038
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001039 for (i = 0; i < card->num_rtd; i++) {
1040 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1041
1042 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001043 continue;
1044
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001045 if (driver->playback.stream_name != NULL)
1046 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1047 SND_SOC_DAPM_STREAM_SUSPEND);
1048
1049 if (driver->capture.stream_name != NULL)
1050 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1051 SND_SOC_DAPM_STREAM_SUSPEND);
1052 }
1053
1054 /* suspend all CODECs */
1055 for (i = 0; i < card->num_rtd; i++) {
1056 struct snd_soc_codec *codec = card->rtd[i].codec;
1057 /* If there are paths active then the CODEC will be held with
1058 * bias _ON and should not be suspended. */
1059 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001060 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001061 case SND_SOC_BIAS_STANDBY:
1062 case SND_SOC_BIAS_OFF:
1063 codec->driver->suspend(codec, PMSG_SUSPEND);
1064 codec->suspended = 1;
1065 break;
1066 default:
1067 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1068 break;
1069 }
1070 }
1071 }
1072
1073 for (i = 0; i < card->num_rtd; i++) {
1074 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1075
1076 if (card->rtd[i].dai_link->ignore_suspend)
1077 continue;
1078
1079 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1080 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001081 }
1082
Mark Brown87506542008-11-18 20:50:34 +00001083 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +01001084 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001085
1086 return 0;
1087}
1088
Andy Green6ed25972008-06-13 16:24:05 +01001089/* deferred resume work, so resume can complete before we finished
1090 * setting our codec back up, which can be very slow on I2C
1091 */
1092static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001093{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001094 struct snd_soc_card *card =
1095 container_of(work, struct snd_soc_card, deferred_resume_work);
1096 struct platform_device *pdev = to_platform_device(card->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001097 int i;
1098
Andy Green6ed25972008-06-13 16:24:05 +01001099 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1100 * so userspace apps are blocked from touching us
1101 */
1102
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001103 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001104
Mark Brown9949788b2010-05-07 20:24:05 +01001105 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001106 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +01001107
Mark Brown87506542008-11-18 20:50:34 +00001108 if (card->resume_pre)
1109 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001110
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001111 /* resume AC97 DAIs */
1112 for (i = 0; i < card->num_rtd; i++) {
1113 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001114
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001115 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001116 continue;
1117
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001118 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1119 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001120 }
1121
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001122 for (i = 0; i < card->num_rtd; i++) {
1123 struct snd_soc_codec *codec = card->rtd[i].codec;
1124 /* If the CODEC was idle over suspend then it will have been
1125 * left with bias OFF or STANDBY and suspended so we must now
1126 * resume. Otherwise the suspend was suppressed.
1127 */
1128 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001129 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001130 case SND_SOC_BIAS_STANDBY:
1131 case SND_SOC_BIAS_OFF:
1132 codec->driver->resume(codec);
1133 codec->suspended = 0;
1134 break;
1135 default:
1136 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1137 break;
1138 }
Mark Brown1547aba2010-05-07 21:11:40 +01001139 }
1140 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001141
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001142 for (i = 0; i < card->num_rtd; i++) {
1143 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001144
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001145 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001146 continue;
1147
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001148 if (driver->playback.stream_name != NULL)
1149 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001150 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001151
1152 if (driver->capture.stream_name != NULL)
1153 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001154 SND_SOC_DAPM_STREAM_RESUME);
1155 }
1156
Mark Brown3ff3f642008-05-19 12:32:25 +02001157 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001158 for (i = 0; i < card->num_rtd; i++) {
1159 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1160 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001161
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001162 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001163 continue;
1164
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001165 if (drv->ops->digital_mute && dai->playback_active)
1166 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001167 }
1168
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001169 for (i = 0; i < card->num_rtd; i++) {
1170 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1171 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001172
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001173 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001174 continue;
1175
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001176 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1177 cpu_dai->driver->resume(cpu_dai);
1178 if (platform->driver->resume && platform->suspended) {
1179 platform->driver->resume(cpu_dai);
1180 platform->suspended = 0;
1181 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001182 }
1183
Mark Brown87506542008-11-18 20:50:34 +00001184 if (card->resume_post)
1185 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001186
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001187 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001188
1189 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001190 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001191}
1192
1193/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001194static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001195{
Mark Brown416356f2009-06-30 19:05:15 +01001196 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001197 struct snd_soc_card *card = platform_get_drvdata(pdev);
1198 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001199
Mark Brown64ab9ba2009-03-31 11:27:03 +01001200 /* AC97 devices might have other drivers hanging off them so
1201 * need to resume immediately. Other drivers don't have that
1202 * problem and may take a substantial amount of time to resume
1203 * due to I/O costs and anti-pop so handle them out of line.
1204 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001205 for (i = 0; i < card->num_rtd; i++) {
1206 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1207 if (cpu_dai->driver->ac97_control) {
1208 dev_dbg(dev, "Resuming AC97 immediately\n");
1209 soc_resume_deferred(&card->deferred_resume_work);
1210 } else {
1211 dev_dbg(dev, "Scheduling resume work\n");
1212 if (!schedule_work(&card->deferred_resume_work))
1213 dev_err(dev, "resume work item may be lost\n");
1214 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001215 }
Andy Green6ed25972008-06-13 16:24:05 +01001216
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001217 return 0;
1218}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001219#else
1220#define soc_suspend NULL
1221#define soc_resume NULL
1222#endif
1223
Barry Song02a06d32009-10-16 18:13:38 +08001224static struct snd_soc_dai_ops null_dai_ops = {
1225};
1226
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001227static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001228{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001229 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1230 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001231 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001232 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001233 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001234
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001235 if (rtd->complete)
1236 return 1;
1237 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001238
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001239 /* do we already have the CPU DAI for this link ? */
1240 if (rtd->cpu_dai) {
1241 goto find_codec;
1242 }
1243 /* no, then find CPU DAI from registered DAIs*/
1244 list_for_each_entry(cpu_dai, &dai_list, list) {
1245 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1246
1247 if (!try_module_get(cpu_dai->dev->driver->owner))
1248 return -ENODEV;
1249
1250 rtd->cpu_dai = cpu_dai;
1251 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001252 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001253 }
1254 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1255 dai_link->cpu_dai_name);
1256
1257find_codec:
1258 /* do we already have the CODEC for this link ? */
1259 if (rtd->codec) {
1260 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001261 }
1262
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001263 /* no, then find CODEC from registered CODECs*/
1264 list_for_each_entry(codec, &codec_list, list) {
1265 if (!strcmp(codec->name, dai_link->codec_name)) {
1266 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001267
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001268 if (!try_module_get(codec->dev->driver->owner))
1269 return -ENODEV;
Mark Brown435c5e22008-12-04 15:32:53 +00001270
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001271 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1272 list_for_each_entry(codec_dai, &dai_list, list) {
1273 if (codec->dev == codec_dai->dev &&
1274 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1275 rtd->codec_dai = codec_dai;
1276 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001277 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001278 }
1279 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1280 dai_link->codec_dai_name);
1281
1282 goto find_platform;
1283 }
1284 }
1285 dev_dbg(card->dev, "CODEC %s not registered\n",
1286 dai_link->codec_name);
1287
1288find_platform:
1289 /* do we already have the CODEC DAI for this link ? */
1290 if (rtd->platform) {
1291 goto out;
1292 }
1293 /* no, then find CPU DAI from registered DAIs*/
1294 list_for_each_entry(platform, &platform_list, list) {
1295 if (!strcmp(platform->name, dai_link->platform_name)) {
1296
1297 if (!try_module_get(platform->dev->driver->owner))
1298 return -ENODEV;
1299
1300 rtd->platform = platform;
1301 goto out;
1302 }
1303 }
1304
1305 dev_dbg(card->dev, "platform %s not registered\n",
1306 dai_link->platform_name);
1307 return 0;
1308
1309out:
1310 /* mark rtd as complete if we found all 4 of our client devices */
1311 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1312 rtd->complete = 1;
1313 card->num_rtd++;
1314 }
1315 return 1;
1316}
1317
1318static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1319{
1320 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1321 struct snd_soc_codec *codec = rtd->codec;
1322 struct snd_soc_platform *platform = rtd->platform;
1323 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1324 int err;
1325
1326 /* unregister the rtd device */
1327 if (rtd->dev_registered) {
1328 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1329 device_unregister(&rtd->dev);
1330 rtd->dev_registered = 0;
1331 }
1332
1333 /* remove the CODEC DAI */
1334 if (codec_dai && codec_dai->probed) {
1335 if (codec_dai->driver->remove) {
1336 err = codec_dai->driver->remove(codec_dai);
1337 if (err < 0)
1338 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1339 }
1340 codec_dai->probed = 0;
1341 list_del(&codec_dai->card_list);
1342 }
1343
1344 /* remove the platform */
1345 if (platform && platform->probed) {
1346 if (platform->driver->remove) {
1347 err = platform->driver->remove(platform);
1348 if (err < 0)
1349 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1350 }
1351 platform->probed = 0;
1352 list_del(&platform->card_list);
1353 module_put(platform->dev->driver->owner);
1354 }
1355
1356 /* remove the CODEC */
1357 if (codec && codec->probed) {
1358 if (codec->driver->remove) {
1359 err = codec->driver->remove(codec);
1360 if (err < 0)
1361 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1362 }
1363
1364 /* Make sure all DAPM widgets are freed */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001365 snd_soc_dapm_free(&codec->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001366
1367 soc_cleanup_codec_debugfs(codec);
1368 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1369 codec->probed = 0;
1370 list_del(&codec->card_list);
1371 module_put(codec->dev->driver->owner);
1372 }
1373
1374 /* remove the cpu_dai */
1375 if (cpu_dai && cpu_dai->probed) {
1376 if (cpu_dai->driver->remove) {
1377 err = cpu_dai->driver->remove(cpu_dai);
1378 if (err < 0)
1379 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1380 }
1381 cpu_dai->probed = 0;
1382 list_del(&cpu_dai->card_list);
1383 module_put(cpu_dai->dev->driver->owner);
1384 }
1385}
1386
1387static void rtd_release(struct device *dev) {}
1388
1389static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1390{
1391 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1392 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1393 struct snd_soc_codec *codec = rtd->codec;
1394 struct snd_soc_platform *platform = rtd->platform;
1395 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1396 int ret;
1397
1398 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1399
1400 /* config components */
1401 codec_dai->codec = codec;
1402 codec->card = card;
1403 cpu_dai->platform = platform;
1404 rtd->card = card;
1405 rtd->dev.parent = card->dev;
1406 codec_dai->card = card;
1407 cpu_dai->card = card;
1408
1409 /* set default power off timeout */
1410 rtd->pmdown_time = pmdown_time;
1411
1412 /* probe the cpu_dai */
1413 if (!cpu_dai->probed) {
1414 if (cpu_dai->driver->probe) {
1415 ret = cpu_dai->driver->probe(cpu_dai);
1416 if (ret < 0) {
1417 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1418 cpu_dai->name);
1419 return ret;
1420 }
1421 }
1422 cpu_dai->probed = 1;
1423 /* mark cpu_dai as probed and add to card cpu_dai list */
1424 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1425 }
1426
1427 /* probe the CODEC */
1428 if (!codec->probed) {
1429 if (codec->driver->probe) {
1430 ret = codec->driver->probe(codec);
1431 if (ret < 0) {
1432 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1433 codec->name);
1434 return ret;
1435 }
1436 }
Mark Brown13cb61f2010-08-12 15:44:04 +01001437
1438 soc_init_codec_debugfs(codec);
1439
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001440 /* mark codec as probed and add to card codec list */
1441 codec->probed = 1;
1442 list_add(&codec->card_list, &card->codec_dev_list);
1443 }
1444
1445 /* probe the platform */
1446 if (!platform->probed) {
1447 if (platform->driver->probe) {
1448 ret = platform->driver->probe(platform);
1449 if (ret < 0) {
1450 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1451 platform->name);
1452 return ret;
1453 }
1454 }
1455 /* mark platform as probed and add to card platform list */
1456 platform->probed = 1;
1457 list_add(&platform->card_list, &card->platform_dev_list);
1458 }
1459
1460 /* probe the CODEC DAI */
1461 if (!codec_dai->probed) {
1462 if (codec_dai->driver->probe) {
1463 ret = codec_dai->driver->probe(codec_dai);
1464 if (ret < 0) {
1465 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1466 codec_dai->name);
1467 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001468 }
1469 }
1470
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001471 /* mark cpu_dai as probed and add to card cpu_dai list */
1472 codec_dai->probed = 1;
1473 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001474 }
1475
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001476 /* DAPM dai link stream work */
1477 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1478
1479 /* now that all clients have probed, initialise the DAI link */
1480 if (dai_link->init) {
1481 ret = dai_link->init(rtd);
1482 if (ret < 0) {
1483 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1484 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001485 }
1486 }
1487
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001488 /* Make sure all DAPM widgets are instantiated */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001489 snd_soc_dapm_new_widgets(&codec->dapm);
1490 snd_soc_dapm_sync(&codec->dapm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001491
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001492 /* register the rtd device */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001493 rtd->dev.release = rtd_release;
1494 rtd->dev.init_name = dai_link->name;
1495 ret = device_register(&rtd->dev);
1496 if (ret < 0) {
1497 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1498 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001499 }
1500
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001501 rtd->dev_registered = 1;
1502 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1503 if (ret < 0)
1504 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1505
1506 /* add DAPM sysfs entries for this codec */
1507 ret = snd_soc_dapm_sys_add(&rtd->dev);
1508 if (ret < 0)
1509 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1510
1511 /* add codec sysfs entries */
1512 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1513 if (ret < 0)
1514 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1515
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001516 /* create the pcm */
1517 ret = soc_new_pcm(rtd, num);
1518 if (ret < 0) {
1519 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1520 return ret;
1521 }
1522
1523 /* add platform data for AC97 devices */
1524 if (rtd->codec_dai->driver->ac97_control)
1525 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1526
1527 return 0;
1528}
1529
1530#ifdef CONFIG_SND_SOC_AC97_BUS
1531static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1532{
1533 int ret;
1534
1535 /* Only instantiate AC97 if not already done by the adaptor
1536 * for the generic AC97 subsystem.
1537 */
1538 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001539 /*
1540 * It is possible that the AC97 device is already registered to
1541 * the device subsystem. This happens when the device is created
1542 * via snd_ac97_mixer(). Currently only SoC codec that does so
1543 * is the generic AC97 glue but others migh emerge.
1544 *
1545 * In those cases we don't try to register the device again.
1546 */
1547 if (!rtd->codec->ac97_created)
1548 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001549
1550 ret = soc_ac97_dev_register(rtd->codec);
1551 if (ret < 0) {
1552 printk(KERN_ERR "asoc: AC97 device register failed\n");
1553 return ret;
1554 }
1555
1556 rtd->codec->ac97_registered = 1;
1557 }
1558 return 0;
1559}
1560
1561static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1562{
1563 if (codec->ac97_registered) {
1564 soc_ac97_dev_unregister(codec);
1565 codec->ac97_registered = 0;
1566 }
1567}
1568#endif
1569
1570static void snd_soc_instantiate_card(struct snd_soc_card *card)
1571{
1572 struct platform_device *pdev = to_platform_device(card->dev);
1573 int ret, i;
1574
1575 mutex_lock(&card->mutex);
1576
1577 if (card->instantiated) {
1578 mutex_unlock(&card->mutex);
1579 return;
1580 }
1581
1582 /* bind DAIs */
1583 for (i = 0; i < card->num_links; i++)
1584 soc_bind_dai_link(card, i);
1585
1586 /* bind completed ? */
1587 if (card->num_rtd != card->num_links) {
1588 mutex_unlock(&card->mutex);
1589 return;
1590 }
1591
1592 /* card bind complete so register a sound card */
1593 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1594 card->owner, 0, &card->snd_card);
1595 if (ret < 0) {
1596 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1597 card->name);
1598 mutex_unlock(&card->mutex);
1599 return;
1600 }
1601 card->snd_card->dev = card->dev;
1602
Randy Dunlap1301a962008-06-17 19:19:34 +01001603#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001604 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001605 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001606#endif
Andy Green6ed25972008-06-13 16:24:05 +01001607
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001608 /* initialise the sound card only once */
1609 if (card->probe) {
1610 ret = card->probe(pdev);
1611 if (ret < 0)
1612 goto card_probe_error;
1613 }
1614
Mark Brownfe3e78e2009-11-03 22:13:13 +00001615 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001616 ret = soc_probe_dai_link(card, i);
1617 if (ret < 0) {
Mark Brown321de0d2010-09-21 15:09:37 +01001618 pr_err("asoc: failed to instantiate card %s: %d\n",
1619 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001620 goto probe_dai_err;
1621 }
1622 }
1623
1624 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1625 "%s", card->name);
1626 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1627 "%s", card->name);
1628
1629 ret = snd_card_register(card->snd_card);
1630 if (ret < 0) {
1631 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1632 goto probe_dai_err;
1633 }
1634
1635#ifdef CONFIG_SND_SOC_AC97_BUS
1636 /* register any AC97 codecs */
1637 for (i = 0; i < card->num_rtd; i++) {
1638 ret = soc_register_ac97_dai_link(&card->rtd[i]);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001639 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001640 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1641 goto probe_dai_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001642 }
1643 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001644#endif
1645
Mark Brown435c5e22008-12-04 15:32:53 +00001646 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001647 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001648 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001649
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001650probe_dai_err:
1651 for (i = 0; i < card->num_links; i++)
1652 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001653
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001654card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001655 if (card->remove)
1656 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001657
1658 snd_card_free(card->snd_card);
1659
1660 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001661}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001662
Mark Brown435c5e22008-12-04 15:32:53 +00001663/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001664 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001665 * client_mutex.
1666 */
1667static void snd_soc_instantiate_cards(void)
1668{
1669 struct snd_soc_card *card;
1670 list_for_each_entry(card, &card_list, list)
1671 snd_soc_instantiate_card(card);
1672}
1673
1674/* probes a new socdev */
1675static int soc_probe(struct platform_device *pdev)
1676{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001677 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001678 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001679
1680 /* Bodge while we unpick instantiation */
1681 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001682 INIT_LIST_HEAD(&card->dai_dev_list);
1683 INIT_LIST_HEAD(&card->codec_dev_list);
1684 INIT_LIST_HEAD(&card->platform_dev_list);
1685
Jarkko Nikulaa6052152010-11-05 20:35:19 +02001686 soc_init_card_debugfs(card);
1687
Mark Brown435c5e22008-12-04 15:32:53 +00001688 ret = snd_soc_register_card(card);
1689 if (ret != 0) {
1690 dev_err(&pdev->dev, "Failed to register card\n");
1691 return ret;
1692 }
1693
1694 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001695}
1696
1697/* removes a socdev */
1698static int soc_remove(struct platform_device *pdev)
1699{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001700 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001701 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001702
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001703 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001704
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001705 /* make sure any delayed work runs */
1706 for (i = 0; i < card->num_rtd; i++) {
1707 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1708 run_delayed_work(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001709 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001710
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001711 /* remove and free each DAI */
1712 for (i = 0; i < card->num_rtd; i++)
1713 soc_remove_dai_link(card, i);
1714
Jarkko Nikulaa6052152010-11-05 20:35:19 +02001715 soc_cleanup_card_debugfs(card);
1716
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001717 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001718 if (card->remove)
1719 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001720
1721 kfree(card->rtd);
1722 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001723 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001724 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001725 return 0;
1726}
1727
Mark Brown416356f2009-06-30 19:05:15 +01001728static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001729{
Mark Brown416356f2009-06-30 19:05:15 +01001730 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001731 struct snd_soc_card *card = platform_get_drvdata(pdev);
1732 int i;
Mark Brown51737472009-06-22 13:16:51 +01001733
1734 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001735 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001736
1737 /* Flush out pmdown_time work - we actually do want to run it
1738 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001739 for (i = 0; i < card->num_rtd; i++) {
1740 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1741 run_delayed_work(&rtd->delayed_work);
1742 }
Mark Brown51737472009-06-22 13:16:51 +01001743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001744 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001745
1746 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001747}
1748
Alexey Dobriyan47145212009-12-14 18:00:08 -08001749static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001750 .suspend = soc_suspend,
1751 .resume = soc_resume,
1752 .poweroff = soc_poweroff,
1753};
1754
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001755/* ASoC platform driver */
1756static struct platform_driver soc_driver = {
1757 .driver = {
1758 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001759 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001760 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001761 },
1762 .probe = soc_probe,
1763 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001764};
1765
1766/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001767static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001768{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001769 struct snd_soc_codec *codec = rtd->codec;
1770 struct snd_soc_platform *platform = rtd->platform;
1771 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1772 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001773 struct snd_pcm *pcm;
1774 char new_name[64];
1775 int ret = 0, playback = 0, capture = 0;
1776
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001777 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001778 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001779 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001780
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001781 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001782 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001783 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001784 capture = 1;
1785
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001786 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1787 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1788 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001789 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001790 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001791 return ret;
1792 }
1793
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001794 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001795 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001796 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1797 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1798 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1799 soc_pcm_ops.copy = platform->driver->ops->copy;
1800 soc_pcm_ops.silence = platform->driver->ops->silence;
1801 soc_pcm_ops.ack = platform->driver->ops->ack;
1802 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001803
1804 if (playback)
1805 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1806
1807 if (capture)
1808 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1809
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001810 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001811 if (ret < 0) {
1812 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001813 return ret;
1814 }
1815
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001816 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001817 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1818 cpu_dai->name);
1819 return ret;
1820}
1821
Mark Brown096e49d2009-07-05 15:12:22 +01001822/**
1823 * snd_soc_codec_volatile_register: Report if a register is volatile.
1824 *
1825 * @codec: CODEC to query.
1826 * @reg: Register to query.
1827 *
1828 * Boolean function indiciating if a CODEC register is volatile.
1829 */
1830int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1831{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001832 if (codec->driver->volatile_register)
1833 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001834 else
1835 return 0;
1836}
1837EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1838
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001839/**
1840 * snd_soc_new_ac97_codec - initailise AC97 device
1841 * @codec: audio codec
1842 * @ops: AC97 bus operations
1843 * @num: AC97 codec number
1844 *
1845 * Initialises AC97 codec resources for use by ad-hoc devices only.
1846 */
1847int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1848 struct snd_ac97_bus_ops *ops, int num)
1849{
1850 mutex_lock(&codec->mutex);
1851
1852 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1853 if (codec->ac97 == NULL) {
1854 mutex_unlock(&codec->mutex);
1855 return -ENOMEM;
1856 }
1857
1858 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1859 if (codec->ac97->bus == NULL) {
1860 kfree(codec->ac97);
1861 codec->ac97 = NULL;
1862 mutex_unlock(&codec->mutex);
1863 return -ENOMEM;
1864 }
1865
1866 codec->ac97->bus->ops = ops;
1867 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001868
1869 /*
1870 * Mark the AC97 device to be created by us. This way we ensure that the
1871 * device will be registered with the device subsystem later on.
1872 */
1873 codec->ac97_created = 1;
1874
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001875 mutex_unlock(&codec->mutex);
1876 return 0;
1877}
1878EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1879
1880/**
1881 * snd_soc_free_ac97_codec - free AC97 codec device
1882 * @codec: audio codec
1883 *
1884 * Frees AC97 codec device resources.
1885 */
1886void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1887{
1888 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001889#ifdef CONFIG_SND_SOC_AC97_BUS
1890 soc_unregister_ac97_dai_link(codec);
1891#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001892 kfree(codec->ac97->bus);
1893 kfree(codec->ac97);
1894 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001895 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001896 mutex_unlock(&codec->mutex);
1897}
1898EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1899
Mark Brownc3753702010-11-01 15:41:57 -04001900unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1901{
1902 unsigned int ret;
1903
1904 ret = codec->driver->read(codec, reg);
1905 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1906
1907 return ret;
1908}
1909EXPORT_SYMBOL_GPL(snd_soc_read);
1910
1911unsigned int snd_soc_write(struct snd_soc_codec *codec,
1912 unsigned int reg, unsigned int val)
1913{
1914 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1915 return codec->driver->write(codec, reg, val);
1916}
1917EXPORT_SYMBOL_GPL(snd_soc_write);
1918
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001919/**
1920 * snd_soc_update_bits - update codec register bits
1921 * @codec: audio codec
1922 * @reg: codec register
1923 * @mask: register mask
1924 * @value: new value
1925 *
1926 * Writes new register value.
1927 *
1928 * Returns 1 for change else 0.
1929 */
1930int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001931 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001932{
1933 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001934 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001935
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001936 old = snd_soc_read(codec, reg);
1937 new = (old & ~mask) | value;
1938 change = old != new;
1939 if (change)
1940 snd_soc_write(codec, reg, new);
1941
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001942 return change;
1943}
1944EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1945
1946/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001947 * snd_soc_update_bits_locked - update codec register bits
1948 * @codec: audio codec
1949 * @reg: codec register
1950 * @mask: register mask
1951 * @value: new value
1952 *
1953 * Writes new register value, and takes the codec mutex.
1954 *
1955 * Returns 1 for change else 0.
1956 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001957int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1958 unsigned short reg, unsigned int mask,
1959 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001960{
1961 int change;
1962
1963 mutex_lock(&codec->mutex);
1964 change = snd_soc_update_bits(codec, reg, mask, value);
1965 mutex_unlock(&codec->mutex);
1966
1967 return change;
1968}
Mark Browndd1b3d52009-12-04 14:22:03 +00001969EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001970
1971/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001972 * snd_soc_test_bits - test register for change
1973 * @codec: audio codec
1974 * @reg: codec register
1975 * @mask: register mask
1976 * @value: new value
1977 *
1978 * Tests a register with a new value and checks if the new value is
1979 * different from the old value.
1980 *
1981 * Returns 1 for change else 0.
1982 */
1983int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001984 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001985{
1986 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001987 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001988
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001989 old = snd_soc_read(codec, reg);
1990 new = (old & ~mask) | value;
1991 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001992
1993 return change;
1994}
1995EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1996
1997/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001998 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1999 * @substream: the pcm substream
2000 * @hw: the hardware parameters
2001 *
2002 * Sets the substream runtime hardware parameters.
2003 */
2004int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2005 const struct snd_pcm_hardware *hw)
2006{
2007 struct snd_pcm_runtime *runtime = substream->runtime;
2008 runtime->hw.info = hw->info;
2009 runtime->hw.formats = hw->formats;
2010 runtime->hw.period_bytes_min = hw->period_bytes_min;
2011 runtime->hw.period_bytes_max = hw->period_bytes_max;
2012 runtime->hw.periods_min = hw->periods_min;
2013 runtime->hw.periods_max = hw->periods_max;
2014 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2015 runtime->hw.fifo_size = hw->fifo_size;
2016 return 0;
2017}
2018EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2019
2020/**
2021 * snd_soc_cnew - create new control
2022 * @_template: control template
2023 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002024 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002025 *
2026 * Create a new mixer control from a template control.
2027 *
2028 * Returns 0 for success, else error.
2029 */
2030struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2031 void *data, char *long_name)
2032{
2033 struct snd_kcontrol_new template;
2034
2035 memcpy(&template, _template, sizeof(template));
2036 if (long_name)
2037 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002038 template.index = 0;
2039
2040 return snd_ctl_new1(&template, data);
2041}
2042EXPORT_SYMBOL_GPL(snd_soc_cnew);
2043
2044/**
Ian Molton3e8e1952009-01-09 00:23:21 +00002045 * snd_soc_add_controls - add an array of controls to a codec.
2046 * Convienience function to add a list of controls. Many codecs were
2047 * duplicating this code.
2048 *
2049 * @codec: codec to add controls to
2050 * @controls: array of controls to add
2051 * @num_controls: number of elements in the array
2052 *
2053 * Return 0 for success, else error.
2054 */
2055int snd_soc_add_controls(struct snd_soc_codec *codec,
2056 const struct snd_kcontrol_new *controls, int num_controls)
2057{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002058 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002059 int err, i;
2060
2061 for (i = 0; i < num_controls; i++) {
2062 const struct snd_kcontrol_new *control = &controls[i];
2063 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
2064 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002065 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2066 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002067 return err;
2068 }
2069 }
2070
2071 return 0;
2072}
2073EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2074
2075/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002076 * snd_soc_info_enum_double - enumerated double mixer info callback
2077 * @kcontrol: mixer control
2078 * @uinfo: control element information
2079 *
2080 * Callback to provide information about a double enumerated
2081 * mixer control.
2082 *
2083 * Returns 0 for success.
2084 */
2085int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2086 struct snd_ctl_elem_info *uinfo)
2087{
2088 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2089
2090 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2091 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002092 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002093
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002094 if (uinfo->value.enumerated.item > e->max - 1)
2095 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002096 strcpy(uinfo->value.enumerated.name,
2097 e->texts[uinfo->value.enumerated.item]);
2098 return 0;
2099}
2100EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2101
2102/**
2103 * snd_soc_get_enum_double - enumerated double mixer get callback
2104 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002105 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002106 *
2107 * Callback to get the value of a double enumerated mixer.
2108 *
2109 * Returns 0 for success.
2110 */
2111int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2112 struct snd_ctl_elem_value *ucontrol)
2113{
2114 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2115 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002116 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002117
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002118 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002119 ;
2120 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002121 ucontrol->value.enumerated.item[0]
2122 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002123 if (e->shift_l != e->shift_r)
2124 ucontrol->value.enumerated.item[1] =
2125 (val >> e->shift_r) & (bitmask - 1);
2126
2127 return 0;
2128}
2129EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2130
2131/**
2132 * snd_soc_put_enum_double - enumerated double mixer put callback
2133 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002134 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002135 *
2136 * Callback to set the value of a double enumerated mixer.
2137 *
2138 * Returns 0 for success.
2139 */
2140int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2141 struct snd_ctl_elem_value *ucontrol)
2142{
2143 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2144 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002145 unsigned int val;
2146 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002147
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002148 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002149 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002150 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002151 return -EINVAL;
2152 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2153 mask = (bitmask - 1) << e->shift_l;
2154 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002155 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002156 return -EINVAL;
2157 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2158 mask |= (bitmask - 1) << e->shift_r;
2159 }
2160
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002161 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002162}
2163EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2164
2165/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002166 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2167 * @kcontrol: mixer control
2168 * @ucontrol: control element information
2169 *
2170 * Callback to get the value of a double semi enumerated mixer.
2171 *
2172 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2173 * used for handling bitfield coded enumeration for example.
2174 *
2175 * Returns 0 for success.
2176 */
2177int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2178 struct snd_ctl_elem_value *ucontrol)
2179{
2180 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002181 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002182 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002183
2184 reg_val = snd_soc_read(codec, e->reg);
2185 val = (reg_val >> e->shift_l) & e->mask;
2186 for (mux = 0; mux < e->max; mux++) {
2187 if (val == e->values[mux])
2188 break;
2189 }
2190 ucontrol->value.enumerated.item[0] = mux;
2191 if (e->shift_l != e->shift_r) {
2192 val = (reg_val >> e->shift_r) & e->mask;
2193 for (mux = 0; mux < e->max; mux++) {
2194 if (val == e->values[mux])
2195 break;
2196 }
2197 ucontrol->value.enumerated.item[1] = mux;
2198 }
2199
2200 return 0;
2201}
2202EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2203
2204/**
2205 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2206 * @kcontrol: mixer control
2207 * @ucontrol: control element information
2208 *
2209 * Callback to set the value of a double semi enumerated mixer.
2210 *
2211 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2212 * used for handling bitfield coded enumeration for example.
2213 *
2214 * Returns 0 for success.
2215 */
2216int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2217 struct snd_ctl_elem_value *ucontrol)
2218{
2219 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002220 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002221 unsigned int val;
2222 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002223
2224 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2225 return -EINVAL;
2226 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2227 mask = e->mask << e->shift_l;
2228 if (e->shift_l != e->shift_r) {
2229 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2230 return -EINVAL;
2231 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2232 mask |= e->mask << e->shift_r;
2233 }
2234
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002235 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002236}
2237EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2238
2239/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002240 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2241 * @kcontrol: mixer control
2242 * @uinfo: control element information
2243 *
2244 * Callback to provide information about an external enumerated
2245 * single mixer.
2246 *
2247 * Returns 0 for success.
2248 */
2249int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2250 struct snd_ctl_elem_info *uinfo)
2251{
2252 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2253
2254 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2255 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002256 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002257
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002258 if (uinfo->value.enumerated.item > e->max - 1)
2259 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002260 strcpy(uinfo->value.enumerated.name,
2261 e->texts[uinfo->value.enumerated.item]);
2262 return 0;
2263}
2264EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2265
2266/**
2267 * snd_soc_info_volsw_ext - external single mixer info callback
2268 * @kcontrol: mixer control
2269 * @uinfo: control element information
2270 *
2271 * Callback to provide information about a single external mixer control.
2272 *
2273 * Returns 0 for success.
2274 */
2275int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2276 struct snd_ctl_elem_info *uinfo)
2277{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002278 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002279
Mark Brownfd5dfad2009-04-15 21:37:46 +01002280 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002281 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2282 else
2283 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2284
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002285 uinfo->count = 1;
2286 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002287 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002288 return 0;
2289}
2290EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2291
2292/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002293 * snd_soc_info_volsw - single mixer info callback
2294 * @kcontrol: mixer control
2295 * @uinfo: control element information
2296 *
2297 * Callback to provide information about a single mixer control.
2298 *
2299 * Returns 0 for success.
2300 */
2301int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2302 struct snd_ctl_elem_info *uinfo)
2303{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002304 struct soc_mixer_control *mc =
2305 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002306 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002307 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002308 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002309
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002310 if (!mc->platform_max)
2311 mc->platform_max = mc->max;
2312 platform_max = mc->platform_max;
2313
2314 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002315 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2316 else
2317 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2318
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002319 uinfo->count = shift == rshift ? 1 : 2;
2320 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002321 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002322 return 0;
2323}
2324EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2325
2326/**
2327 * snd_soc_get_volsw - single mixer get callback
2328 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002329 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002330 *
2331 * Callback to get the value of a single mixer control.
2332 *
2333 * Returns 0 for success.
2334 */
2335int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2336 struct snd_ctl_elem_value *ucontrol)
2337{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002338 struct soc_mixer_control *mc =
2339 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002340 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002341 unsigned int reg = mc->reg;
2342 unsigned int shift = mc->shift;
2343 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002344 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002345 unsigned int mask = (1 << fls(max)) - 1;
2346 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002347
2348 ucontrol->value.integer.value[0] =
2349 (snd_soc_read(codec, reg) >> shift) & mask;
2350 if (shift != rshift)
2351 ucontrol->value.integer.value[1] =
2352 (snd_soc_read(codec, reg) >> rshift) & mask;
2353 if (invert) {
2354 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002355 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002356 if (shift != rshift)
2357 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002358 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002359 }
2360
2361 return 0;
2362}
2363EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2364
2365/**
2366 * snd_soc_put_volsw - single mixer put callback
2367 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002368 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002369 *
2370 * Callback to set the value of a single mixer control.
2371 *
2372 * Returns 0 for success.
2373 */
2374int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2375 struct snd_ctl_elem_value *ucontrol)
2376{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002377 struct soc_mixer_control *mc =
2378 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002379 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002380 unsigned int reg = mc->reg;
2381 unsigned int shift = mc->shift;
2382 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002383 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002384 unsigned int mask = (1 << fls(max)) - 1;
2385 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002386 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002387
2388 val = (ucontrol->value.integer.value[0] & mask);
2389 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002390 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002391 val_mask = mask << shift;
2392 val = val << shift;
2393 if (shift != rshift) {
2394 val2 = (ucontrol->value.integer.value[1] & mask);
2395 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002396 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002397 val_mask |= mask << rshift;
2398 val |= val2 << rshift;
2399 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002400 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002401}
2402EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2403
2404/**
2405 * snd_soc_info_volsw_2r - double mixer info callback
2406 * @kcontrol: mixer control
2407 * @uinfo: control element information
2408 *
2409 * Callback to provide information about a double mixer control that
2410 * spans 2 codec registers.
2411 *
2412 * Returns 0 for success.
2413 */
2414int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2415 struct snd_ctl_elem_info *uinfo)
2416{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002417 struct soc_mixer_control *mc =
2418 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002419 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002420
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002421 if (!mc->platform_max)
2422 mc->platform_max = mc->max;
2423 platform_max = mc->platform_max;
2424
2425 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002426 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2427 else
2428 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2429
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002430 uinfo->count = 2;
2431 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002432 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002433 return 0;
2434}
2435EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2436
2437/**
2438 * snd_soc_get_volsw_2r - double mixer get callback
2439 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002440 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002441 *
2442 * Callback to get the value of a double mixer control that spans 2 registers.
2443 *
2444 * Returns 0 for success.
2445 */
2446int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2447 struct snd_ctl_elem_value *ucontrol)
2448{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002449 struct soc_mixer_control *mc =
2450 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002451 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002452 unsigned int reg = mc->reg;
2453 unsigned int reg2 = mc->rreg;
2454 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002455 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002456 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002457 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002458
2459 ucontrol->value.integer.value[0] =
2460 (snd_soc_read(codec, reg) >> shift) & mask;
2461 ucontrol->value.integer.value[1] =
2462 (snd_soc_read(codec, reg2) >> shift) & mask;
2463 if (invert) {
2464 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002465 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002466 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002467 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002468 }
2469
2470 return 0;
2471}
2472EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2473
2474/**
2475 * snd_soc_put_volsw_2r - double mixer set callback
2476 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002477 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002478 *
2479 * Callback to set the value of a double mixer control that spans 2 registers.
2480 *
2481 * Returns 0 for success.
2482 */
2483int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2484 struct snd_ctl_elem_value *ucontrol)
2485{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002486 struct soc_mixer_control *mc =
2487 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002488 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002489 unsigned int reg = mc->reg;
2490 unsigned int reg2 = mc->rreg;
2491 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002492 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002493 unsigned int mask = (1 << fls(max)) - 1;
2494 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002495 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002496 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002497
2498 val_mask = mask << shift;
2499 val = (ucontrol->value.integer.value[0] & mask);
2500 val2 = (ucontrol->value.integer.value[1] & mask);
2501
2502 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002503 val = max - val;
2504 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002505 }
2506
2507 val = val << shift;
2508 val2 = val2 << shift;
2509
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002510 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002511 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002512 return err;
2513
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002514 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002515 return err;
2516}
2517EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2518
Mark Browne13ac2e2008-05-28 17:58:05 +01002519/**
2520 * snd_soc_info_volsw_s8 - signed mixer info callback
2521 * @kcontrol: mixer control
2522 * @uinfo: control element information
2523 *
2524 * Callback to provide information about a signed mixer control.
2525 *
2526 * Returns 0 for success.
2527 */
2528int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2529 struct snd_ctl_elem_info *uinfo)
2530{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002531 struct soc_mixer_control *mc =
2532 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002533 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002534 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002535
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002536 if (!mc->platform_max)
2537 mc->platform_max = mc->max;
2538 platform_max = mc->platform_max;
2539
Mark Browne13ac2e2008-05-28 17:58:05 +01002540 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2541 uinfo->count = 2;
2542 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002543 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002544 return 0;
2545}
2546EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2547
2548/**
2549 * snd_soc_get_volsw_s8 - signed mixer get callback
2550 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002551 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002552 *
2553 * Callback to get the value of a signed mixer control.
2554 *
2555 * Returns 0 for success.
2556 */
2557int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2558 struct snd_ctl_elem_value *ucontrol)
2559{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002560 struct soc_mixer_control *mc =
2561 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002562 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002563 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002564 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002565 int val = snd_soc_read(codec, reg);
2566
2567 ucontrol->value.integer.value[0] =
2568 ((signed char)(val & 0xff))-min;
2569 ucontrol->value.integer.value[1] =
2570 ((signed char)((val >> 8) & 0xff))-min;
2571 return 0;
2572}
2573EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2574
2575/**
2576 * snd_soc_put_volsw_sgn - signed mixer put callback
2577 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002578 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002579 *
2580 * Callback to set the value of a signed mixer control.
2581 *
2582 * Returns 0 for success.
2583 */
2584int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2585 struct snd_ctl_elem_value *ucontrol)
2586{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002587 struct soc_mixer_control *mc =
2588 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002589 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002590 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002591 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002592 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002593
2594 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2595 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2596
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002597 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002598}
2599EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2600
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002601/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002602 * snd_soc_limit_volume - Set new limit to an existing volume control.
2603 *
2604 * @codec: where to look for the control
2605 * @name: Name of the control
2606 * @max: new maximum limit
2607 *
2608 * Return 0 for success, else error.
2609 */
2610int snd_soc_limit_volume(struct snd_soc_codec *codec,
2611 const char *name, int max)
2612{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002613 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002614 struct snd_kcontrol *kctl;
2615 struct soc_mixer_control *mc;
2616 int found = 0;
2617 int ret = -EINVAL;
2618
2619 /* Sanity check for name and max */
2620 if (unlikely(!name || max <= 0))
2621 return -EINVAL;
2622
2623 list_for_each_entry(kctl, &card->controls, list) {
2624 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2625 found = 1;
2626 break;
2627 }
2628 }
2629 if (found) {
2630 mc = (struct soc_mixer_control *)kctl->private_value;
2631 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002632 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002633 ret = 0;
2634 }
2635 }
2636 return ret;
2637}
2638EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2639
2640/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002641 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2642 * mixer info callback
2643 * @kcontrol: mixer control
2644 * @uinfo: control element information
2645 *
2646 * Returns 0 for success.
2647 */
2648int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2649 struct snd_ctl_elem_info *uinfo)
2650{
2651 struct soc_mixer_control *mc =
2652 (struct soc_mixer_control *)kcontrol->private_value;
2653 int max = mc->max;
2654 int min = mc->min;
2655
2656 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2657 uinfo->count = 2;
2658 uinfo->value.integer.min = 0;
2659 uinfo->value.integer.max = max-min;
2660
2661 return 0;
2662}
2663EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2664
2665/**
2666 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2667 * mixer get callback
2668 * @kcontrol: mixer control
2669 * @uinfo: control element information
2670 *
2671 * Returns 0 for success.
2672 */
2673int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2674 struct snd_ctl_elem_value *ucontrol)
2675{
2676 struct soc_mixer_control *mc =
2677 (struct soc_mixer_control *)kcontrol->private_value;
2678 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2679 unsigned int mask = (1<<mc->shift)-1;
2680 int min = mc->min;
2681 int val = snd_soc_read(codec, mc->reg) & mask;
2682 int valr = snd_soc_read(codec, mc->rreg) & mask;
2683
Stuart Longland20630c72010-06-18 12:56:10 +10002684 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2685 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002686 return 0;
2687}
2688EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2689
2690/**
2691 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2692 * mixer put callback
2693 * @kcontrol: mixer control
2694 * @uinfo: control element information
2695 *
2696 * Returns 0 for success.
2697 */
2698int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2699 struct snd_ctl_elem_value *ucontrol)
2700{
2701 struct soc_mixer_control *mc =
2702 (struct soc_mixer_control *)kcontrol->private_value;
2703 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2704 unsigned int mask = (1<<mc->shift)-1;
2705 int min = mc->min;
2706 int ret;
2707 unsigned int val, valr, oval, ovalr;
2708
2709 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2710 val &= mask;
2711 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2712 valr &= mask;
2713
2714 oval = snd_soc_read(codec, mc->reg) & mask;
2715 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2716
2717 ret = 0;
2718 if (oval != val) {
2719 ret = snd_soc_write(codec, mc->reg, val);
2720 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002721 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002722 }
2723 if (ovalr != valr) {
2724 ret = snd_soc_write(codec, mc->rreg, valr);
2725 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002726 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002727 }
2728
2729 return 0;
2730}
2731EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2732
2733/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002734 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2735 * @dai: DAI
2736 * @clk_id: DAI specific clock ID
2737 * @freq: new clock frequency in Hz
2738 * @dir: new clock direction - input/output.
2739 *
2740 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2741 */
2742int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2743 unsigned int freq, int dir)
2744{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002745 if (dai->driver && dai->driver->ops->set_sysclk)
2746 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002747 else
2748 return -EINVAL;
2749}
2750EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2751
2752/**
2753 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2754 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002755 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002756 * @div: new clock divisor.
2757 *
2758 * Configures the clock dividers. This is used to derive the best DAI bit and
2759 * frame clocks from the system or master clock. It's best to set the DAI bit
2760 * and frame clocks as low as possible to save system power.
2761 */
2762int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2763 int div_id, int div)
2764{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002765 if (dai->driver && dai->driver->ops->set_clkdiv)
2766 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002767 else
2768 return -EINVAL;
2769}
2770EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2771
2772/**
2773 * snd_soc_dai_set_pll - configure DAI PLL.
2774 * @dai: DAI
2775 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002776 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002777 * @freq_in: PLL input clock frequency in Hz
2778 * @freq_out: requested PLL output clock frequency in Hz
2779 *
2780 * Configures and enables PLL to generate output clock based on input clock.
2781 */
Mark Brown85488032009-09-05 18:52:16 +01002782int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2783 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002784{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002785 if (dai->driver && dai->driver->ops->set_pll)
2786 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002787 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002788 else
2789 return -EINVAL;
2790}
2791EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2792
2793/**
2794 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2795 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002796 * @fmt: SND_SOC_DAIFMT_ format value.
2797 *
2798 * Configures the DAI hardware format and clocking.
2799 */
2800int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2801{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002802 if (dai->driver && dai->driver->ops->set_fmt)
2803 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002804 else
2805 return -EINVAL;
2806}
2807EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2808
2809/**
2810 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2811 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002812 * @tx_mask: bitmask representing active TX slots.
2813 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002814 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002815 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002816 *
2817 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2818 * specific.
2819 */
2820int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002821 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002822{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002823 if (dai->driver && dai->driver->ops->set_tdm_slot)
2824 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002825 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002826 else
2827 return -EINVAL;
2828}
2829EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2830
2831/**
Barry Song472df3c2009-09-12 01:16:29 +08002832 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2833 * @dai: DAI
2834 * @tx_num: how many TX channels
2835 * @tx_slot: pointer to an array which imply the TX slot number channel
2836 * 0~num-1 uses
2837 * @rx_num: how many RX channels
2838 * @rx_slot: pointer to an array which imply the RX slot number channel
2839 * 0~num-1 uses
2840 *
2841 * configure the relationship between channel number and TDM slot number.
2842 */
2843int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2844 unsigned int tx_num, unsigned int *tx_slot,
2845 unsigned int rx_num, unsigned int *rx_slot)
2846{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002847 if (dai->driver && dai->driver->ops->set_channel_map)
2848 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002849 rx_num, rx_slot);
2850 else
2851 return -EINVAL;
2852}
2853EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2854
2855/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002856 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2857 * @dai: DAI
2858 * @tristate: tristate enable
2859 *
2860 * Tristates the DAI so that others can use it.
2861 */
2862int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2863{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002864 if (dai->driver && dai->driver->ops->set_tristate)
2865 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002866 else
2867 return -EINVAL;
2868}
2869EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2870
2871/**
2872 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2873 * @dai: DAI
2874 * @mute: mute enable
2875 *
2876 * Mutes the DAI DAC.
2877 */
2878int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2879{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002880 if (dai->driver && dai->driver->ops->digital_mute)
2881 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002882 else
2883 return -EINVAL;
2884}
2885EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2886
Mark Brownc5af3a22008-11-28 13:29:45 +00002887/**
2888 * snd_soc_register_card - Register a card with the ASoC core
2889 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002890 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002891 *
2892 * Note that currently this is an internal only function: it will be
2893 * exposed to machine drivers after further backporting of ASoC v2
2894 * registration APIs.
2895 */
2896static int snd_soc_register_card(struct snd_soc_card *card)
2897{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002898 int i;
2899
Mark Brownc5af3a22008-11-28 13:29:45 +00002900 if (!card->name || !card->dev)
2901 return -EINVAL;
2902
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002903 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2904 GFP_KERNEL);
2905 if (card->rtd == NULL)
2906 return -ENOMEM;
2907
2908 for (i = 0; i < card->num_links; i++)
2909 card->rtd[i].dai_link = &card->dai_link[i];
2910
Mark Brownc5af3a22008-11-28 13:29:45 +00002911 INIT_LIST_HEAD(&card->list);
2912 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002913 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002914
2915 mutex_lock(&client_mutex);
2916 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002917 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002918 mutex_unlock(&client_mutex);
2919
2920 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2921
2922 return 0;
2923}
2924
2925/**
2926 * snd_soc_unregister_card - Unregister a card with the ASoC core
2927 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002928 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002929 *
2930 * Note that currently this is an internal only function: it will be
2931 * exposed to machine drivers after further backporting of ASoC v2
2932 * registration APIs.
2933 */
2934static int snd_soc_unregister_card(struct snd_soc_card *card)
2935{
2936 mutex_lock(&client_mutex);
2937 list_del(&card->list);
2938 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002939 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2940
2941 return 0;
2942}
2943
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002944/*
2945 * Simplify DAI link configuration by removing ".-1" from device names
2946 * and sanitizing names.
2947 */
2948static inline char *fmt_single_name(struct device *dev, int *id)
2949{
2950 char *found, name[NAME_SIZE];
2951 int id1, id2;
2952
2953 if (dev_name(dev) == NULL)
2954 return NULL;
2955
2956 strncpy(name, dev_name(dev), NAME_SIZE);
2957
2958 /* are we a "%s.%d" name (platform and SPI components) */
2959 found = strstr(name, dev->driver->name);
2960 if (found) {
2961 /* get ID */
2962 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2963
2964 /* discard ID from name if ID == -1 */
2965 if (*id == -1)
2966 found[strlen(dev->driver->name)] = '\0';
2967 }
2968
2969 } else {
2970 /* I2C component devices are named "bus-addr" */
2971 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2972 char tmp[NAME_SIZE];
2973
2974 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002975 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002976
2977 /* sanitize component name for DAI link creation */
2978 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2979 strncpy(name, tmp, NAME_SIZE);
2980 } else
2981 *id = 0;
2982 }
2983
2984 return kstrdup(name, GFP_KERNEL);
2985}
2986
2987/*
2988 * Simplify DAI link naming for single devices with multiple DAIs by removing
2989 * any ".-1" and using the DAI name (instead of device name).
2990 */
2991static inline char *fmt_multiple_name(struct device *dev,
2992 struct snd_soc_dai_driver *dai_drv)
2993{
2994 if (dai_drv->name == NULL) {
2995 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2996 dev_name(dev));
2997 return NULL;
2998 }
2999
3000 return kstrdup(dai_drv->name, GFP_KERNEL);
3001}
3002
Mark Brown91151712008-11-30 23:31:24 +00003003/**
3004 * snd_soc_register_dai - Register a DAI with the ASoC core
3005 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003006 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003007 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003008int snd_soc_register_dai(struct device *dev,
3009 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003010{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003011 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003012
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003013 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003014
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003015 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3016 if (dai == NULL)
3017 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003018
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003019 /* create DAI component name */
3020 dai->name = fmt_single_name(dev, &dai->id);
3021 if (dai->name == NULL) {
3022 kfree(dai);
3023 return -ENOMEM;
3024 }
3025
3026 dai->dev = dev;
3027 dai->driver = dai_drv;
3028 if (!dai->driver->ops)
3029 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003030
3031 mutex_lock(&client_mutex);
3032 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003033 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003034 mutex_unlock(&client_mutex);
3035
3036 pr_debug("Registered DAI '%s'\n", dai->name);
3037
3038 return 0;
3039}
3040EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3041
3042/**
3043 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3044 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003045 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003046 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003047void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003048{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003049 struct snd_soc_dai *dai;
3050
3051 list_for_each_entry(dai, &dai_list, list) {
3052 if (dev == dai->dev)
3053 goto found;
3054 }
3055 return;
3056
3057found:
Mark Brown91151712008-11-30 23:31:24 +00003058 mutex_lock(&client_mutex);
3059 list_del(&dai->list);
3060 mutex_unlock(&client_mutex);
3061
3062 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003063 kfree(dai->name);
3064 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003065}
3066EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3067
3068/**
3069 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3070 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003071 * @dai: Array of DAIs to register
3072 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003073 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003074int snd_soc_register_dais(struct device *dev,
3075 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003076{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003077 struct snd_soc_dai *dai;
3078 int i, ret = 0;
3079
Liam Girdwood720ffa42010-08-18 00:30:30 +01003080 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003081
3082 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003083
3084 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003085 if (dai == NULL) {
3086 ret = -ENOMEM;
3087 goto err;
3088 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003089
3090 /* create DAI component name */
3091 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3092 if (dai->name == NULL) {
3093 kfree(dai);
3094 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003095 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003096 }
3097
3098 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003099 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003100 if (dai->driver->id)
3101 dai->id = dai->driver->id;
3102 else
3103 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003104 if (!dai->driver->ops)
3105 dai->driver->ops = &null_dai_ops;
3106
3107 mutex_lock(&client_mutex);
3108 list_add(&dai->list, &dai_list);
3109 mutex_unlock(&client_mutex);
3110
3111 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003112 }
3113
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003114 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003115 return 0;
3116
3117err:
3118 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003119 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003120
3121 return ret;
3122}
3123EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3124
3125/**
3126 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3127 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003128 * @dai: Array of DAIs to unregister
3129 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003130 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003131void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003132{
3133 int i;
3134
3135 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003136 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003137}
3138EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3139
Mark Brown12a48a8c2008-12-03 19:40:30 +00003140/**
3141 * snd_soc_register_platform - Register a platform with the ASoC core
3142 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003143 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003144 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003145int snd_soc_register_platform(struct device *dev,
3146 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003147{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003148 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003149
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003150 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3151
3152 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3153 if (platform == NULL)
3154 return -ENOMEM;
3155
3156 /* create platform component name */
3157 platform->name = fmt_single_name(dev, &platform->id);
3158 if (platform->name == NULL) {
3159 kfree(platform);
3160 return -ENOMEM;
3161 }
3162
3163 platform->dev = dev;
3164 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003165
3166 mutex_lock(&client_mutex);
3167 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003168 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003169 mutex_unlock(&client_mutex);
3170
3171 pr_debug("Registered platform '%s'\n", platform->name);
3172
3173 return 0;
3174}
3175EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3176
3177/**
3178 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3179 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003180 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003181 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003182void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003183{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003184 struct snd_soc_platform *platform;
3185
3186 list_for_each_entry(platform, &platform_list, list) {
3187 if (dev == platform->dev)
3188 goto found;
3189 }
3190 return;
3191
3192found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003193 mutex_lock(&client_mutex);
3194 list_del(&platform->list);
3195 mutex_unlock(&client_mutex);
3196
3197 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003198 kfree(platform->name);
3199 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003200}
3201EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3202
Mark Brown151ab222009-05-09 16:22:58 +01003203static u64 codec_format_map[] = {
3204 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3205 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3206 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3207 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3208 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3209 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3210 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3211 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3212 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3213 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3214 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3215 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3216 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3217 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3218 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3219 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3220};
3221
3222/* Fix up the DAI formats for endianness: codecs don't actually see
3223 * the endianness of the data but we're using the CPU format
3224 * definitions which do need to include endianness so we ensure that
3225 * codec DAIs always have both big and little endian variants set.
3226 */
3227static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3228{
3229 int i;
3230
3231 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3232 if (stream->formats & codec_format_map[i])
3233 stream->formats |= codec_format_map[i];
3234}
3235
Mark Brown0d0cf002008-12-10 14:32:45 +00003236/**
3237 * snd_soc_register_codec - Register a codec with the ASoC core
3238 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003239 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003240 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003241int snd_soc_register_codec(struct device *dev,
3242 struct snd_soc_codec_driver *codec_drv,
3243 struct snd_soc_dai_driver *dai_drv, int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003244{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003245 struct snd_soc_codec *codec;
3246 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003247
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003248 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003249
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003250 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3251 if (codec == NULL)
3252 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003253
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003254 /* create CODEC component name */
3255 codec->name = fmt_single_name(dev, &codec->id);
3256 if (codec->name == NULL) {
3257 kfree(codec);
3258 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003259 }
3260
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003261 INIT_LIST_HEAD(&codec->dapm.widgets);
3262 INIT_LIST_HEAD(&codec->dapm.paths);
3263 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3264 codec->dapm.dev = dev;
3265 codec->dapm.codec = codec;
3266
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003267 /* allocate CODEC register cache */
3268 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3269
3270 if (codec_drv->reg_cache_default)
3271 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3272 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3273 else
3274 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3275 codec_drv->reg_word_size, GFP_KERNEL);
3276
3277 if (codec->reg_cache == NULL) {
3278 kfree(codec->name);
3279 kfree(codec);
3280 return -ENOMEM;
3281 }
3282 }
3283
3284 codec->dev = dev;
3285 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003286 codec->num_dai = num_dai;
3287 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003288
3289 for (i = 0; i < num_dai; i++) {
3290 fixup_codec_formats(&dai_drv[i].playback);
3291 fixup_codec_formats(&dai_drv[i].capture);
3292 }
3293
Mark Brown26b01cc2010-08-18 20:20:55 +01003294 /* register any DAIs */
3295 if (num_dai) {
3296 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3297 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003298 goto error;
Mark Brown26b01cc2010-08-18 20:20:55 +01003299 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003300
Mark Brown0d0cf002008-12-10 14:32:45 +00003301 mutex_lock(&client_mutex);
3302 list_add(&codec->list, &codec_list);
3303 snd_soc_instantiate_cards();
3304 mutex_unlock(&client_mutex);
3305
3306 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003307 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003308
3309error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003310 if (codec->reg_cache)
3311 kfree(codec->reg_cache);
3312 kfree(codec->name);
3313 kfree(codec);
3314 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003315}
3316EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3317
3318/**
3319 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3320 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003321 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003322 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003323void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003324{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003325 struct snd_soc_codec *codec;
3326 int i;
3327
3328 list_for_each_entry(codec, &codec_list, list) {
3329 if (dev == codec->dev)
3330 goto found;
3331 }
3332 return;
3333
3334found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003335 if (codec->num_dai)
3336 for (i = 0; i < codec->num_dai; i++)
3337 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003338
Mark Brown0d0cf002008-12-10 14:32:45 +00003339 mutex_lock(&client_mutex);
3340 list_del(&codec->list);
3341 mutex_unlock(&client_mutex);
3342
3343 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003344
3345 if (codec->reg_cache)
3346 kfree(codec->reg_cache);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003347 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003348 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003349}
3350EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3351
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003352static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003353{
Mark Brown384c89e2008-12-03 17:34:03 +00003354#ifdef CONFIG_DEBUG_FS
3355 debugfs_root = debugfs_create_dir("asoc", NULL);
3356 if (IS_ERR(debugfs_root) || !debugfs_root) {
3357 printk(KERN_WARNING
3358 "ASoC: Failed to create debugfs directory\n");
3359 debugfs_root = NULL;
3360 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003361
3362 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3363 &codec_list_fops))
3364 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3365
Mark Brownf3208782010-09-15 18:19:07 +01003366 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3367 &dai_list_fops))
3368 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003369
3370 if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
3371 &platform_list_fops))
3372 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003373#endif
3374
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003375 return platform_driver_register(&soc_driver);
3376}
Mark Brown4abe8e12010-10-12 17:41:03 +01003377module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003378
Mark Brown7d8c16a2008-11-30 22:11:24 +00003379static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003380{
Mark Brown384c89e2008-12-03 17:34:03 +00003381#ifdef CONFIG_DEBUG_FS
3382 debugfs_remove_recursive(debugfs_root);
3383#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003384 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003385}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003386module_exit(snd_soc_exit);
3387
3388/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003389MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003390MODULE_DESCRIPTION("ALSA SoC Core");
3391MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003392MODULE_ALIAS("platform:soc-audio");