blob: c18ce1dc51b8a48ed913e32093767854043559ed [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
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200258 codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
Mark Brown2624d5f2009-11-03 21:56:13 +0000259 codec->debugfs_codec_root);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200260 if (!codec->dapm.debugfs_dapm)
Mark Brown2624d5f2009-11-03 21:56:13 +0000261 printk(KERN_WARNING
262 "Failed to create DAPM debugfs directory\n");
263
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200264 snd_soc_dapm_debugfs_init(&codec->dapm);
Mark Brown2624d5f2009-11-03 21:56:13 +0000265}
266
267static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
268{
269 debugfs_remove_recursive(codec->debugfs_codec_root);
270}
271
Mark Brownc3c5a192010-09-15 18:15:14 +0100272static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
273 size_t count, loff_t *ppos)
274{
275 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100276 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100277 struct snd_soc_codec *codec;
278
279 if (!buf)
280 return -ENOMEM;
281
Mark Brown2b194f9d2010-10-13 10:52:16 +0100282 list_for_each_entry(codec, &codec_list, list) {
283 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
284 codec->name);
285 if (len >= 0)
286 ret += len;
287 if (ret > PAGE_SIZE) {
288 ret = PAGE_SIZE;
289 break;
290 }
291 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100292
293 if (ret >= 0)
294 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
295
296 kfree(buf);
297
298 return ret;
299}
300
301static const struct file_operations codec_list_fops = {
302 .read = codec_list_read_file,
303 .llseek = default_llseek,/* read accesses f_pos */
304};
305
Mark Brownf3208782010-09-15 18:19:07 +0100306static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
307 size_t count, loff_t *ppos)
308{
309 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100310 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100311 struct snd_soc_dai *dai;
312
313 if (!buf)
314 return -ENOMEM;
315
Mark Brown2b194f9d2010-10-13 10:52:16 +0100316 list_for_each_entry(dai, &dai_list, list) {
317 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
318 if (len >= 0)
319 ret += len;
320 if (ret > PAGE_SIZE) {
321 ret = PAGE_SIZE;
322 break;
323 }
324 }
Mark Brownf3208782010-09-15 18:19:07 +0100325
Mark Brown2b194f9d2010-10-13 10:52:16 +0100326 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100327
328 kfree(buf);
329
330 return ret;
331}
332
333static const struct file_operations dai_list_fops = {
334 .read = dai_list_read_file,
335 .llseek = default_llseek,/* read accesses f_pos */
336};
337
Mark Brown19c7ac22010-09-15 18:22:40 +0100338static ssize_t platform_list_read_file(struct file *file,
339 char __user *user_buf,
340 size_t count, loff_t *ppos)
341{
342 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100343 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100344 struct snd_soc_platform *platform;
345
346 if (!buf)
347 return -ENOMEM;
348
Mark Brown2b194f9d2010-10-13 10:52:16 +0100349 list_for_each_entry(platform, &platform_list, list) {
350 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
351 platform->name);
352 if (len >= 0)
353 ret += len;
354 if (ret > PAGE_SIZE) {
355 ret = PAGE_SIZE;
356 break;
357 }
358 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100359
Mark Brown2b194f9d2010-10-13 10:52:16 +0100360 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100361
362 kfree(buf);
363
364 return ret;
365}
366
367static const struct file_operations platform_list_fops = {
368 .read = platform_list_read_file,
369 .llseek = default_llseek,/* read accesses f_pos */
370};
371
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200372static void soc_init_card_debugfs(struct snd_soc_card *card)
373{
374 card->debugfs_card_root = debugfs_create_dir(card->name,
375 debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200376 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200377 dev_warn(card->dev,
378 "ASoC: Failed to create codec debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200379 return;
380 }
381
382 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
383 card->debugfs_card_root,
384 &card->pop_time);
385 if (!card->debugfs_pop_time)
386 dev_warn(card->dev,
387 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200388}
389
390static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
391{
392 debugfs_remove_recursive(card->debugfs_card_root);
393}
394
Mark Brown2624d5f2009-11-03 21:56:13 +0000395#else
396
397static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
398{
399}
400
401static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
402{
403}
Axel Linb95fccb2010-11-09 17:06:44 +0800404
405static inline void soc_init_card_debugfs(struct snd_soc_card *card)
406{
407}
408
409static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
410{
411}
Mark Brown2624d5f2009-11-03 21:56:13 +0000412#endif
413
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200414#ifdef CONFIG_SND_SOC_AC97_BUS
415/* unregister ac97 codec */
416static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
417{
418 if (codec->ac97->dev.bus)
419 device_unregister(&codec->ac97->dev);
420 return 0;
421}
422
423/* stop no dev release warning */
424static void soc_ac97_device_release(struct device *dev){}
425
426/* register ac97 codec to bus */
427static int soc_ac97_dev_register(struct snd_soc_codec *codec)
428{
429 int err;
430
431 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100432 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200433 codec->ac97->dev.release = soc_ac97_device_release;
434
Kay Sieversbb072bf2008-11-02 03:50:35 +0100435 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000436 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200437 err = device_register(&codec->ac97->dev);
438 if (err < 0) {
439 snd_printk(KERN_ERR "Can't register ac97 bus\n");
440 codec->ac97->dev.bus = NULL;
441 return err;
442 }
443 return 0;
444}
445#endif
446
Mark Brown06f409d2009-04-07 18:10:13 +0100447static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
448{
449 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000450 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
451 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100452 int ret;
453
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000454 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
455 rtd->dai_link->symmetric_rates) {
456 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
457 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100458
459 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
460 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000461 rtd->rate,
462 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100463 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000464 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100465 "Unable to apply rate symmetry constraint: %d\n", ret);
466 return ret;
467 }
468 }
469
470 return 0;
471}
472
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200473/*
474 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
475 * then initialized and any private data can be allocated. This also calls
476 * startup for the cpu DAI, platform, machine and codec DAI.
477 */
478static int soc_pcm_open(struct snd_pcm_substream *substream)
479{
480 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200481 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000482 struct snd_soc_platform *platform = rtd->platform;
483 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
484 struct snd_soc_dai *codec_dai = rtd->codec_dai;
485 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
486 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200487 int ret = 0;
488
489 mutex_lock(&pcm_mutex);
490
491 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000492 if (cpu_dai->driver->ops->startup) {
493 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200494 if (ret < 0) {
495 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100496 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200497 goto out;
498 }
499 }
500
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501 if (platform->driver->ops->open) {
502 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200503 if (ret < 0) {
504 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
505 goto platform_err;
506 }
507 }
508
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000509 if (codec_dai->driver->ops->startup) {
510 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100511 if (ret < 0) {
512 printk(KERN_ERR "asoc: can't open codec %s\n",
513 codec_dai->name);
514 goto codec_dai_err;
515 }
516 }
517
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
519 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200520 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000521 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200522 goto machine_err;
523 }
524 }
525
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200526 /* Check that the codec and cpu DAI's are compatible */
527 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
528 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000529 max(codec_dai_drv->playback.rate_min,
530 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532 min(codec_dai_drv->playback.rate_max,
533 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200534 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000535 max(codec_dai_drv->playback.channels_min,
536 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200537 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000538 min(codec_dai_drv->playback.channels_max,
539 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100540 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100542 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000543 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
544 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900545 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000546 runtime->hw.rates |= cpu_dai_drv->playback.rates;
547 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900548 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000549 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200550 } else {
551 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 max(codec_dai_drv->capture.rate_min,
553 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 min(codec_dai_drv->capture.rate_max,
556 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200557 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 max(codec_dai_drv->capture.channels_min,
559 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200560 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 min(codec_dai_drv->capture.channels_max,
562 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100563 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100565 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000566 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
567 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900568 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 runtime->hw.rates |= cpu_dai_drv->capture.rates;
570 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900571 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200573 }
574
575 snd_pcm_limit_hw_rates(runtime);
576 if (!runtime->hw.rates) {
577 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100578 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 if (!runtime->hw.formats) {
582 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100583 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900584 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200585 }
586 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
587 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900589 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200590 }
591
Mark Brown06f409d2009-04-07 18:10:13 +0100592 /* Symmetry only applies if we've already got an active stream. */
593 if (cpu_dai->active || codec_dai->active) {
594 ret = soc_pcm_apply_symmetry(substream);
595 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900596 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100597 }
598
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 pr_debug("asoc: %s <-> %s info:\n",
600 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100601 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
602 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
603 runtime->hw.channels_max);
604 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
605 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200606
Jassi Brar14dc5732010-02-26 09:12:32 +0900607 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 cpu_dai->playback_active++;
609 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900610 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000611 cpu_dai->capture_active++;
612 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900613 }
614 cpu_dai->active++;
615 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000616 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200617 mutex_unlock(&pcm_mutex);
618 return 0;
619
Jassi Brarbb1c0472010-02-25 11:24:53 +0900620config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
622 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200623
Jassi Brarbb1c0472010-02-25 11:24:53 +0900624machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000625 if (codec_dai->driver->ops->shutdown)
626 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900627
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100628codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000629 if (platform->driver->ops->close)
630 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200631
632platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633 if (cpu_dai->driver->ops->shutdown)
634 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200635out:
636 mutex_unlock(&pcm_mutex);
637 return ret;
638}
639
640/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200641 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200642 * This is to ensure there are no pops or clicks in between any music tracks
643 * due to DAPM power cycling.
644 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100645static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200646{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000647 struct snd_soc_pcm_runtime *rtd =
648 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
649 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200650
651 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200652
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000653 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
654 codec_dai->driver->playback.stream_name,
655 codec_dai->playback_active ? "active" : "inactive",
656 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200657
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 /* are we waiting on this codec DAI stream */
659 if (codec_dai->pop_wait == 1) {
660 codec_dai->pop_wait = 0;
661 snd_soc_dapm_stream_event(rtd,
662 codec_dai->driver->playback.stream_name,
663 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200664 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200666 mutex_unlock(&pcm_mutex);
667}
668
669/*
670 * Called by ALSA when a PCM substream is closed. Private data can be
671 * freed here. The cpu DAI, codec DAI, machine and platform are also
672 * shutdown.
673 */
674static int soc_codec_close(struct snd_pcm_substream *substream)
675{
676 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000677 struct snd_soc_platform *platform = rtd->platform;
678 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
679 struct snd_soc_dai *codec_dai = rtd->codec_dai;
680 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200681
682 mutex_lock(&pcm_mutex);
683
Jassi Brar14dc5732010-02-26 09:12:32 +0900684 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000685 cpu_dai->playback_active--;
686 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900687 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000688 cpu_dai->capture_active--;
689 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900691
692 cpu_dai->active--;
693 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200694 codec->active--;
695
Mark Brown6010b2d2008-09-06 18:33:24 +0100696 /* Muting the DAC suppresses artifacts caused during digital
697 * shutdown, for example from stopping clocks.
698 */
699 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
700 snd_soc_dai_digital_mute(codec_dai, 1);
701
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000702 if (cpu_dai->driver->ops->shutdown)
703 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200704
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000705 if (codec_dai->driver->ops->shutdown)
706 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200707
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000708 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
709 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200710
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000711 if (platform->driver->ops->close)
712 platform->driver->ops->close(substream);
713 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200714
715 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
716 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100717 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000718 schedule_delayed_work(&rtd->delayed_work,
719 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200720 } else {
721 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000722 snd_soc_dapm_stream_event(rtd,
723 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100724 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200725 }
726
727 mutex_unlock(&pcm_mutex);
728 return 0;
729}
730
731/*
732 * Called by ALSA when the PCM substream is prepared, can set format, sample
733 * rate, etc. This function is non atomic and can be called multiple times,
734 * it can refer to the runtime info.
735 */
736static int soc_pcm_prepare(struct snd_pcm_substream *substream)
737{
738 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000739 struct snd_soc_platform *platform = rtd->platform;
740 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
741 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200742 int ret = 0;
743
744 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100745
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000746 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
747 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100748 if (ret < 0) {
749 printk(KERN_ERR "asoc: machine prepare error\n");
750 goto out;
751 }
752 }
753
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754 if (platform->driver->ops->prepare) {
755 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200756 if (ret < 0) {
757 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200758 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200759 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200760 }
761
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000762 if (codec_dai->driver->ops->prepare) {
763 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200764 if (ret < 0) {
765 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200766 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200767 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200768 }
769
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000770 if (cpu_dai->driver->ops->prepare) {
771 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100772 if (ret < 0) {
773 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
774 goto out;
775 }
776 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200777
Mark Brownd45f6212008-10-14 13:58:36 +0100778 /* cancel any delayed stream shutdown that is pending */
779 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
780 codec_dai->pop_wait) {
781 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100783 }
784
Mark Brown452c5ea2009-05-17 21:41:23 +0100785 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000786 snd_soc_dapm_stream_event(rtd,
787 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100788 SND_SOC_DAPM_STREAM_START);
789 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000790 snd_soc_dapm_stream_event(rtd,
791 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100792 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100793
Mark Brown452c5ea2009-05-17 21:41:23 +0100794 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200795
796out:
797 mutex_unlock(&pcm_mutex);
798 return ret;
799}
800
801/*
802 * Called by ALSA when the hardware params are set by application. This
803 * function can also be called multiple times and can allocate buffers
804 * (using snd_pcm_lib_* ). It's non-atomic.
805 */
806static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
807 struct snd_pcm_hw_params *params)
808{
809 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000810 struct snd_soc_platform *platform = rtd->platform;
811 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
812 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200813 int ret = 0;
814
815 mutex_lock(&pcm_mutex);
816
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000817 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
818 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200819 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100820 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200821 goto out;
822 }
823 }
824
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000825 if (codec_dai->driver->ops->hw_params) {
826 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100827 if (ret < 0) {
828 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
829 codec_dai->name);
830 goto codec_err;
831 }
832 }
833
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000834 if (cpu_dai->driver->ops->hw_params) {
835 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200836 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200837 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100838 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200839 goto interface_err;
840 }
841 }
842
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000843 if (platform->driver->ops->hw_params) {
844 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200845 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200846 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200847 platform->name);
848 goto platform_err;
849 }
850 }
851
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000852 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100853
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200854out:
855 mutex_unlock(&pcm_mutex);
856 return ret;
857
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000859 if (cpu_dai->driver->ops->hw_free)
860 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200861
862interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000863 if (codec_dai->driver->ops->hw_free)
864 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100865
866codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000867 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
868 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200869
870 mutex_unlock(&pcm_mutex);
871 return ret;
872}
873
874/*
875 * Free's resources allocated by hw_params, can be called multiple times
876 */
877static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
878{
879 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000880 struct snd_soc_platform *platform = rtd->platform;
881 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
882 struct snd_soc_dai *codec_dai = rtd->codec_dai;
883 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200884
885 mutex_lock(&pcm_mutex);
886
887 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100888 if (!codec->active)
889 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200890
891 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000892 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
893 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200894
895 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000896 if (platform->driver->ops->hw_free)
897 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200898
899 /* now free hw params for the DAI's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000900 if (codec_dai->driver->ops->hw_free)
901 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200902
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000903 if (cpu_dai->driver->ops->hw_free)
904 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200905
906 mutex_unlock(&pcm_mutex);
907 return 0;
908}
909
910static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
911{
912 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000913 struct snd_soc_platform *platform = rtd->platform;
914 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
915 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200916 int ret;
917
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000918 if (codec_dai->driver->ops->trigger) {
919 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200920 if (ret < 0)
921 return ret;
922 }
923
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000924 if (platform->driver->ops->trigger) {
925 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200926 if (ret < 0)
927 return ret;
928 }
929
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000930 if (cpu_dai->driver->ops->trigger) {
931 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200932 if (ret < 0)
933 return ret;
934 }
935 return 0;
936}
937
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200938/*
939 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200940 * If cpu_dai, codec_dai, platform driver has the delay callback, than
941 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200942 */
943static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
944{
945 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000946 struct snd_soc_platform *platform = rtd->platform;
947 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
948 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200949 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200950 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200951 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200952
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000953 if (platform->driver->ops->pointer)
954 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200955
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000956 if (cpu_dai->driver->ops->delay)
957 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200958
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000959 if (codec_dai->driver->ops->delay)
960 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200961
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000962 if (platform->driver->delay)
963 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200964
965 runtime->delay = delay;
966
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200967 return offset;
968}
969
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200970/* ASoC PCM operations */
971static struct snd_pcm_ops soc_pcm_ops = {
972 .open = soc_pcm_open,
973 .close = soc_codec_close,
974 .hw_params = soc_pcm_hw_params,
975 .hw_free = soc_pcm_hw_free,
976 .prepare = soc_pcm_prepare,
977 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200978 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200979};
980
981#ifdef CONFIG_PM
982/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100983static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200984{
Mark Brown416356f2009-06-30 19:05:15 +0100985 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000986 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200987 int i;
988
Daniel Macke3509ff2009-06-03 17:44:49 +0200989 /* If the initialization of this soc device failed, there is no codec
990 * associated with it. Just bail out in this case.
991 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000992 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200993 return 0;
994
Andy Green6ed25972008-06-13 16:24:05 +0100995 /* Due to the resume being scheduled into a workqueue we could
996 * suspend before that's finished - wait for it to complete.
997 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000998 snd_power_lock(card->snd_card);
999 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
1000 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +01001001
1002 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001003 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +01001004
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001005 /* mute any active DAC's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001006 for (i = 0; i < card->num_rtd; i++) {
1007 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1008 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001009
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001010 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001011 continue;
1012
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001013 if (drv->ops->digital_mute && dai->playback_active)
1014 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001015 }
1016
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001017 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001018 for (i = 0; i < card->num_rtd; i++) {
1019 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001020 continue;
1021
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001022 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +01001023 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001024
Mark Brown87506542008-11-18 20:50:34 +00001025 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +01001026 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001027
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001028 for (i = 0; i < card->num_rtd; i++) {
1029 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1030 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001031
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001032 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001033 continue;
1034
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001035 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1036 cpu_dai->driver->suspend(cpu_dai);
1037 if (platform->driver->suspend && !platform->suspended) {
1038 platform->driver->suspend(cpu_dai);
1039 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +01001040 }
1041 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001042
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001043 /* close any waiting streams and save state */
1044 for (i = 0; i < card->num_rtd; i++) {
1045 run_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001046 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001047 }
Mark Brown3efab7d2010-05-09 13:25:43 +01001048
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001049 for (i = 0; i < card->num_rtd; i++) {
1050 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1051
1052 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001053 continue;
1054
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001055 if (driver->playback.stream_name != NULL)
1056 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1057 SND_SOC_DAPM_STREAM_SUSPEND);
1058
1059 if (driver->capture.stream_name != NULL)
1060 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1061 SND_SOC_DAPM_STREAM_SUSPEND);
1062 }
1063
1064 /* suspend all CODECs */
1065 for (i = 0; i < card->num_rtd; i++) {
1066 struct snd_soc_codec *codec = card->rtd[i].codec;
1067 /* If there are paths active then the CODEC will be held with
1068 * bias _ON and should not be suspended. */
1069 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001070 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001071 case SND_SOC_BIAS_STANDBY:
1072 case SND_SOC_BIAS_OFF:
1073 codec->driver->suspend(codec, PMSG_SUSPEND);
1074 codec->suspended = 1;
1075 break;
1076 default:
1077 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1078 break;
1079 }
1080 }
1081 }
1082
1083 for (i = 0; i < card->num_rtd; i++) {
1084 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1085
1086 if (card->rtd[i].dai_link->ignore_suspend)
1087 continue;
1088
1089 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1090 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001091 }
1092
Mark Brown87506542008-11-18 20:50:34 +00001093 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +01001094 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001095
1096 return 0;
1097}
1098
Andy Green6ed25972008-06-13 16:24:05 +01001099/* deferred resume work, so resume can complete before we finished
1100 * setting our codec back up, which can be very slow on I2C
1101 */
1102static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001103{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001104 struct snd_soc_card *card =
1105 container_of(work, struct snd_soc_card, deferred_resume_work);
1106 struct platform_device *pdev = to_platform_device(card->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001107 int i;
1108
Andy Green6ed25972008-06-13 16:24:05 +01001109 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1110 * so userspace apps are blocked from touching us
1111 */
1112
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001113 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001114
Mark Brown99497882010-05-07 20:24:05 +01001115 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001116 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +01001117
Mark Brown87506542008-11-18 20:50:34 +00001118 if (card->resume_pre)
1119 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001120
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001121 /* resume AC97 DAIs */
1122 for (i = 0; i < card->num_rtd; i++) {
1123 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001124
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001125 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001126 continue;
1127
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001128 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1129 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001130 }
1131
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001132 for (i = 0; i < card->num_rtd; i++) {
1133 struct snd_soc_codec *codec = card->rtd[i].codec;
1134 /* If the CODEC was idle over suspend then it will have been
1135 * left with bias OFF or STANDBY and suspended so we must now
1136 * resume. Otherwise the suspend was suppressed.
1137 */
1138 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001139 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001140 case SND_SOC_BIAS_STANDBY:
1141 case SND_SOC_BIAS_OFF:
1142 codec->driver->resume(codec);
1143 codec->suspended = 0;
1144 break;
1145 default:
1146 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1147 break;
1148 }
Mark Brown1547aba2010-05-07 21:11:40 +01001149 }
1150 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001151
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001152 for (i = 0; i < card->num_rtd; i++) {
1153 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001154
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001155 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001156 continue;
1157
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001158 if (driver->playback.stream_name != NULL)
1159 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001160 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001161
1162 if (driver->capture.stream_name != NULL)
1163 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001164 SND_SOC_DAPM_STREAM_RESUME);
1165 }
1166
Mark Brown3ff3f642008-05-19 12:32:25 +02001167 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001168 for (i = 0; i < card->num_rtd; i++) {
1169 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1170 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001171
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001172 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001173 continue;
1174
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001175 if (drv->ops->digital_mute && dai->playback_active)
1176 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001177 }
1178
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001179 for (i = 0; i < card->num_rtd; i++) {
1180 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1181 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001182
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001183 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001184 continue;
1185
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001186 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1187 cpu_dai->driver->resume(cpu_dai);
1188 if (platform->driver->resume && platform->suspended) {
1189 platform->driver->resume(cpu_dai);
1190 platform->suspended = 0;
1191 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001192 }
1193
Mark Brown87506542008-11-18 20:50:34 +00001194 if (card->resume_post)
1195 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001196
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001197 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001198
1199 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001200 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001201}
1202
1203/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001204static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001205{
Mark Brown416356f2009-06-30 19:05:15 +01001206 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001207 struct snd_soc_card *card = platform_get_drvdata(pdev);
1208 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001209
Mark Brown64ab9ba2009-03-31 11:27:03 +01001210 /* AC97 devices might have other drivers hanging off them so
1211 * need to resume immediately. Other drivers don't have that
1212 * problem and may take a substantial amount of time to resume
1213 * due to I/O costs and anti-pop so handle them out of line.
1214 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001215 for (i = 0; i < card->num_rtd; i++) {
1216 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1217 if (cpu_dai->driver->ac97_control) {
1218 dev_dbg(dev, "Resuming AC97 immediately\n");
1219 soc_resume_deferred(&card->deferred_resume_work);
1220 } else {
1221 dev_dbg(dev, "Scheduling resume work\n");
1222 if (!schedule_work(&card->deferred_resume_work))
1223 dev_err(dev, "resume work item may be lost\n");
1224 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001225 }
Andy Green6ed25972008-06-13 16:24:05 +01001226
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001227 return 0;
1228}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001229#else
1230#define soc_suspend NULL
1231#define soc_resume NULL
1232#endif
1233
Barry Song02a06d32009-10-16 18:13:38 +08001234static struct snd_soc_dai_ops null_dai_ops = {
1235};
1236
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001237static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001238{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001239 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1240 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001241 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001242 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001243 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001244
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001245 if (rtd->complete)
1246 return 1;
1247 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001248
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001249 /* do we already have the CPU DAI for this link ? */
1250 if (rtd->cpu_dai) {
1251 goto find_codec;
1252 }
1253 /* no, then find CPU DAI from registered DAIs*/
1254 list_for_each_entry(cpu_dai, &dai_list, list) {
1255 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1256
1257 if (!try_module_get(cpu_dai->dev->driver->owner))
1258 return -ENODEV;
1259
1260 rtd->cpu_dai = cpu_dai;
1261 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001262 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001263 }
1264 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1265 dai_link->cpu_dai_name);
1266
1267find_codec:
1268 /* do we already have the CODEC for this link ? */
1269 if (rtd->codec) {
1270 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001271 }
1272
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001273 /* no, then find CODEC from registered CODECs*/
1274 list_for_each_entry(codec, &codec_list, list) {
1275 if (!strcmp(codec->name, dai_link->codec_name)) {
1276 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001277
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001278 if (!try_module_get(codec->dev->driver->owner))
1279 return -ENODEV;
Mark Brown435c5e22008-12-04 15:32:53 +00001280
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001281 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1282 list_for_each_entry(codec_dai, &dai_list, list) {
1283 if (codec->dev == codec_dai->dev &&
1284 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1285 rtd->codec_dai = codec_dai;
1286 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001287 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001288 }
1289 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1290 dai_link->codec_dai_name);
1291
1292 goto find_platform;
1293 }
1294 }
1295 dev_dbg(card->dev, "CODEC %s not registered\n",
1296 dai_link->codec_name);
1297
1298find_platform:
1299 /* do we already have the CODEC DAI for this link ? */
1300 if (rtd->platform) {
1301 goto out;
1302 }
1303 /* no, then find CPU DAI from registered DAIs*/
1304 list_for_each_entry(platform, &platform_list, list) {
1305 if (!strcmp(platform->name, dai_link->platform_name)) {
1306
1307 if (!try_module_get(platform->dev->driver->owner))
1308 return -ENODEV;
1309
1310 rtd->platform = platform;
1311 goto out;
1312 }
1313 }
1314
1315 dev_dbg(card->dev, "platform %s not registered\n",
1316 dai_link->platform_name);
1317 return 0;
1318
1319out:
1320 /* mark rtd as complete if we found all 4 of our client devices */
1321 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1322 rtd->complete = 1;
1323 card->num_rtd++;
1324 }
1325 return 1;
1326}
1327
1328static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1329{
1330 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1331 struct snd_soc_codec *codec = rtd->codec;
1332 struct snd_soc_platform *platform = rtd->platform;
1333 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1334 int err;
1335
1336 /* unregister the rtd device */
1337 if (rtd->dev_registered) {
1338 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1339 device_unregister(&rtd->dev);
1340 rtd->dev_registered = 0;
1341 }
1342
1343 /* remove the CODEC DAI */
1344 if (codec_dai && codec_dai->probed) {
1345 if (codec_dai->driver->remove) {
1346 err = codec_dai->driver->remove(codec_dai);
1347 if (err < 0)
1348 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1349 }
1350 codec_dai->probed = 0;
1351 list_del(&codec_dai->card_list);
1352 }
1353
1354 /* remove the platform */
1355 if (platform && platform->probed) {
1356 if (platform->driver->remove) {
1357 err = platform->driver->remove(platform);
1358 if (err < 0)
1359 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1360 }
1361 platform->probed = 0;
1362 list_del(&platform->card_list);
1363 module_put(platform->dev->driver->owner);
1364 }
1365
1366 /* remove the CODEC */
1367 if (codec && codec->probed) {
1368 if (codec->driver->remove) {
1369 err = codec->driver->remove(codec);
1370 if (err < 0)
1371 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1372 }
1373
1374 /* Make sure all DAPM widgets are freed */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001375 snd_soc_dapm_free(&codec->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001376
1377 soc_cleanup_codec_debugfs(codec);
1378 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1379 codec->probed = 0;
1380 list_del(&codec->card_list);
1381 module_put(codec->dev->driver->owner);
1382 }
1383
1384 /* remove the cpu_dai */
1385 if (cpu_dai && cpu_dai->probed) {
1386 if (cpu_dai->driver->remove) {
1387 err = cpu_dai->driver->remove(cpu_dai);
1388 if (err < 0)
1389 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1390 }
1391 cpu_dai->probed = 0;
1392 list_del(&cpu_dai->card_list);
1393 module_put(cpu_dai->dev->driver->owner);
1394 }
1395}
1396
1397static void rtd_release(struct device *dev) {}
1398
1399static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1400{
1401 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1402 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1403 struct snd_soc_codec *codec = rtd->codec;
1404 struct snd_soc_platform *platform = rtd->platform;
1405 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1406 int ret;
1407
1408 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1409
1410 /* config components */
1411 codec_dai->codec = codec;
1412 codec->card = card;
1413 cpu_dai->platform = platform;
1414 rtd->card = card;
1415 rtd->dev.parent = card->dev;
1416 codec_dai->card = card;
1417 cpu_dai->card = card;
1418
1419 /* set default power off timeout */
1420 rtd->pmdown_time = pmdown_time;
1421
1422 /* probe the cpu_dai */
1423 if (!cpu_dai->probed) {
1424 if (cpu_dai->driver->probe) {
1425 ret = cpu_dai->driver->probe(cpu_dai);
1426 if (ret < 0) {
1427 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1428 cpu_dai->name);
1429 return ret;
1430 }
1431 }
1432 cpu_dai->probed = 1;
1433 /* mark cpu_dai as probed and add to card cpu_dai list */
1434 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1435 }
1436
1437 /* probe the CODEC */
1438 if (!codec->probed) {
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001439 codec->dapm.card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001440 if (codec->driver->probe) {
1441 ret = codec->driver->probe(codec);
1442 if (ret < 0) {
1443 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1444 codec->name);
1445 return ret;
1446 }
1447 }
Mark Brown13cb61f2010-08-12 15:44:04 +01001448
1449 soc_init_codec_debugfs(codec);
1450
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001451 /* mark codec as probed and add to card codec list */
1452 codec->probed = 1;
1453 list_add(&codec->card_list, &card->codec_dev_list);
1454 }
1455
1456 /* probe the platform */
1457 if (!platform->probed) {
1458 if (platform->driver->probe) {
1459 ret = platform->driver->probe(platform);
1460 if (ret < 0) {
1461 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1462 platform->name);
1463 return ret;
1464 }
1465 }
1466 /* mark platform as probed and add to card platform list */
1467 platform->probed = 1;
1468 list_add(&platform->card_list, &card->platform_dev_list);
1469 }
1470
1471 /* probe the CODEC DAI */
1472 if (!codec_dai->probed) {
1473 if (codec_dai->driver->probe) {
1474 ret = codec_dai->driver->probe(codec_dai);
1475 if (ret < 0) {
1476 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1477 codec_dai->name);
1478 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001479 }
1480 }
1481
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001482 /* mark cpu_dai as probed and add to card cpu_dai list */
1483 codec_dai->probed = 1;
1484 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001485 }
1486
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001487 /* DAPM dai link stream work */
1488 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1489
1490 /* now that all clients have probed, initialise the DAI link */
1491 if (dai_link->init) {
1492 ret = dai_link->init(rtd);
1493 if (ret < 0) {
1494 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1495 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001496 }
1497 }
1498
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001499 /* Make sure all DAPM widgets are instantiated */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001500 snd_soc_dapm_new_widgets(&codec->dapm);
1501 snd_soc_dapm_sync(&codec->dapm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001502
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001503 /* register the rtd device */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001504 rtd->dev.release = rtd_release;
1505 rtd->dev.init_name = dai_link->name;
1506 ret = device_register(&rtd->dev);
1507 if (ret < 0) {
1508 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1509 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001510 }
1511
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001512 rtd->dev_registered = 1;
1513 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1514 if (ret < 0)
1515 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1516
1517 /* add DAPM sysfs entries for this codec */
1518 ret = snd_soc_dapm_sys_add(&rtd->dev);
1519 if (ret < 0)
1520 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1521
1522 /* add codec sysfs entries */
1523 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1524 if (ret < 0)
1525 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1526
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001527 /* create the pcm */
1528 ret = soc_new_pcm(rtd, num);
1529 if (ret < 0) {
1530 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1531 return ret;
1532 }
1533
1534 /* add platform data for AC97 devices */
1535 if (rtd->codec_dai->driver->ac97_control)
1536 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1537
1538 return 0;
1539}
1540
1541#ifdef CONFIG_SND_SOC_AC97_BUS
1542static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1543{
1544 int ret;
1545
1546 /* Only instantiate AC97 if not already done by the adaptor
1547 * for the generic AC97 subsystem.
1548 */
1549 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001550 /*
1551 * It is possible that the AC97 device is already registered to
1552 * the device subsystem. This happens when the device is created
1553 * via snd_ac97_mixer(). Currently only SoC codec that does so
1554 * is the generic AC97 glue but others migh emerge.
1555 *
1556 * In those cases we don't try to register the device again.
1557 */
1558 if (!rtd->codec->ac97_created)
1559 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001560
1561 ret = soc_ac97_dev_register(rtd->codec);
1562 if (ret < 0) {
1563 printk(KERN_ERR "asoc: AC97 device register failed\n");
1564 return ret;
1565 }
1566
1567 rtd->codec->ac97_registered = 1;
1568 }
1569 return 0;
1570}
1571
1572static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1573{
1574 if (codec->ac97_registered) {
1575 soc_ac97_dev_unregister(codec);
1576 codec->ac97_registered = 0;
1577 }
1578}
1579#endif
1580
1581static void snd_soc_instantiate_card(struct snd_soc_card *card)
1582{
1583 struct platform_device *pdev = to_platform_device(card->dev);
1584 int ret, i;
1585
1586 mutex_lock(&card->mutex);
1587
1588 if (card->instantiated) {
1589 mutex_unlock(&card->mutex);
1590 return;
1591 }
1592
1593 /* bind DAIs */
1594 for (i = 0; i < card->num_links; i++)
1595 soc_bind_dai_link(card, i);
1596
1597 /* bind completed ? */
1598 if (card->num_rtd != card->num_links) {
1599 mutex_unlock(&card->mutex);
1600 return;
1601 }
1602
1603 /* card bind complete so register a sound card */
1604 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1605 card->owner, 0, &card->snd_card);
1606 if (ret < 0) {
1607 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1608 card->name);
1609 mutex_unlock(&card->mutex);
1610 return;
1611 }
1612 card->snd_card->dev = card->dev;
1613
Randy Dunlap1301a962008-06-17 19:19:34 +01001614#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001615 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001616 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001617#endif
Andy Green6ed25972008-06-13 16:24:05 +01001618
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001619 /* initialise the sound card only once */
1620 if (card->probe) {
1621 ret = card->probe(pdev);
1622 if (ret < 0)
1623 goto card_probe_error;
1624 }
1625
Mark Brownfe3e78e2009-11-03 22:13:13 +00001626 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001627 ret = soc_probe_dai_link(card, i);
1628 if (ret < 0) {
Mark Brown321de0d2010-09-21 15:09:37 +01001629 pr_err("asoc: failed to instantiate card %s: %d\n",
1630 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001631 goto probe_dai_err;
1632 }
1633 }
1634
1635 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1636 "%s", card->name);
1637 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1638 "%s", card->name);
1639
1640 ret = snd_card_register(card->snd_card);
1641 if (ret < 0) {
1642 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1643 goto probe_dai_err;
1644 }
1645
1646#ifdef CONFIG_SND_SOC_AC97_BUS
1647 /* register any AC97 codecs */
1648 for (i = 0; i < card->num_rtd; i++) {
1649 ret = soc_register_ac97_dai_link(&card->rtd[i]);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001650 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001651 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1652 goto probe_dai_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001653 }
1654 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001655#endif
1656
Mark Brown435c5e22008-12-04 15:32:53 +00001657 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001658 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001659 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001660
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001661probe_dai_err:
1662 for (i = 0; i < card->num_links; i++)
1663 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001664
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001665card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001666 if (card->remove)
1667 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001668
1669 snd_card_free(card->snd_card);
1670
1671 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001672}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001673
Mark Brown435c5e22008-12-04 15:32:53 +00001674/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001675 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001676 * client_mutex.
1677 */
1678static void snd_soc_instantiate_cards(void)
1679{
1680 struct snd_soc_card *card;
1681 list_for_each_entry(card, &card_list, list)
1682 snd_soc_instantiate_card(card);
1683}
1684
1685/* probes a new socdev */
1686static int soc_probe(struct platform_device *pdev)
1687{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001688 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001689 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001690
1691 /* Bodge while we unpick instantiation */
1692 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001693 INIT_LIST_HEAD(&card->dai_dev_list);
1694 INIT_LIST_HEAD(&card->codec_dev_list);
1695 INIT_LIST_HEAD(&card->platform_dev_list);
1696
Jarkko Nikulaa6052152010-11-05 20:35:19 +02001697 soc_init_card_debugfs(card);
1698
Mark Brown435c5e22008-12-04 15:32:53 +00001699 ret = snd_soc_register_card(card);
1700 if (ret != 0) {
1701 dev_err(&pdev->dev, "Failed to register card\n");
1702 return ret;
1703 }
1704
1705 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001706}
1707
1708/* removes a socdev */
1709static int soc_remove(struct platform_device *pdev)
1710{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001711 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001712 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001714 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001715
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001716 /* make sure any delayed work runs */
1717 for (i = 0; i < card->num_rtd; i++) {
1718 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1719 run_delayed_work(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001720 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001721
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001722 /* remove and free each DAI */
1723 for (i = 0; i < card->num_rtd; i++)
1724 soc_remove_dai_link(card, i);
1725
Jarkko Nikulaa6052152010-11-05 20:35:19 +02001726 soc_cleanup_card_debugfs(card);
1727
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001728 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001729 if (card->remove)
1730 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001731
1732 kfree(card->rtd);
1733 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001734 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001735 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001736 return 0;
1737}
1738
Mark Brown416356f2009-06-30 19:05:15 +01001739static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001740{
Mark Brown416356f2009-06-30 19:05:15 +01001741 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001742 struct snd_soc_card *card = platform_get_drvdata(pdev);
1743 int i;
Mark Brown51737472009-06-22 13:16:51 +01001744
1745 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001746 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001747
1748 /* Flush out pmdown_time work - we actually do want to run it
1749 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001750 for (i = 0; i < card->num_rtd; i++) {
1751 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1752 run_delayed_work(&rtd->delayed_work);
1753 }
Mark Brown51737472009-06-22 13:16:51 +01001754
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001755 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001756
1757 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001758}
1759
Alexey Dobriyan47145212009-12-14 18:00:08 -08001760static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001761 .suspend = soc_suspend,
1762 .resume = soc_resume,
1763 .poweroff = soc_poweroff,
1764};
1765
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001766/* ASoC platform driver */
1767static struct platform_driver soc_driver = {
1768 .driver = {
1769 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001770 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001771 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001772 },
1773 .probe = soc_probe,
1774 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001775};
1776
1777/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001778static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001779{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001780 struct snd_soc_codec *codec = rtd->codec;
1781 struct snd_soc_platform *platform = rtd->platform;
1782 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1783 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001784 struct snd_pcm *pcm;
1785 char new_name[64];
1786 int ret = 0, playback = 0, capture = 0;
1787
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001788 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001789 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001790 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001791
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001792 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001793 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001794 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001795 capture = 1;
1796
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001797 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1798 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1799 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001800 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001801 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001802 return ret;
1803 }
1804
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001805 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001806 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001807 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1808 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1809 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1810 soc_pcm_ops.copy = platform->driver->ops->copy;
1811 soc_pcm_ops.silence = platform->driver->ops->silence;
1812 soc_pcm_ops.ack = platform->driver->ops->ack;
1813 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001814
1815 if (playback)
1816 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1817
1818 if (capture)
1819 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1820
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001821 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001822 if (ret < 0) {
1823 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001824 return ret;
1825 }
1826
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001827 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001828 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1829 cpu_dai->name);
1830 return ret;
1831}
1832
Mark Brown096e49d2009-07-05 15:12:22 +01001833/**
1834 * snd_soc_codec_volatile_register: Report if a register is volatile.
1835 *
1836 * @codec: CODEC to query.
1837 * @reg: Register to query.
1838 *
1839 * Boolean function indiciating if a CODEC register is volatile.
1840 */
1841int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1842{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001843 if (codec->driver->volatile_register)
1844 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001845 else
1846 return 0;
1847}
1848EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1849
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001850/**
1851 * snd_soc_new_ac97_codec - initailise AC97 device
1852 * @codec: audio codec
1853 * @ops: AC97 bus operations
1854 * @num: AC97 codec number
1855 *
1856 * Initialises AC97 codec resources for use by ad-hoc devices only.
1857 */
1858int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1859 struct snd_ac97_bus_ops *ops, int num)
1860{
1861 mutex_lock(&codec->mutex);
1862
1863 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1864 if (codec->ac97 == NULL) {
1865 mutex_unlock(&codec->mutex);
1866 return -ENOMEM;
1867 }
1868
1869 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1870 if (codec->ac97->bus == NULL) {
1871 kfree(codec->ac97);
1872 codec->ac97 = NULL;
1873 mutex_unlock(&codec->mutex);
1874 return -ENOMEM;
1875 }
1876
1877 codec->ac97->bus->ops = ops;
1878 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001879
1880 /*
1881 * Mark the AC97 device to be created by us. This way we ensure that the
1882 * device will be registered with the device subsystem later on.
1883 */
1884 codec->ac97_created = 1;
1885
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001886 mutex_unlock(&codec->mutex);
1887 return 0;
1888}
1889EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1890
1891/**
1892 * snd_soc_free_ac97_codec - free AC97 codec device
1893 * @codec: audio codec
1894 *
1895 * Frees AC97 codec device resources.
1896 */
1897void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1898{
1899 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001900#ifdef CONFIG_SND_SOC_AC97_BUS
1901 soc_unregister_ac97_dai_link(codec);
1902#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001903 kfree(codec->ac97->bus);
1904 kfree(codec->ac97);
1905 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001906 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001907 mutex_unlock(&codec->mutex);
1908}
1909EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1910
Mark Brownc3753702010-11-01 15:41:57 -04001911unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1912{
1913 unsigned int ret;
1914
1915 ret = codec->driver->read(codec, reg);
1916 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1917
1918 return ret;
1919}
1920EXPORT_SYMBOL_GPL(snd_soc_read);
1921
1922unsigned int snd_soc_write(struct snd_soc_codec *codec,
1923 unsigned int reg, unsigned int val)
1924{
1925 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1926 return codec->driver->write(codec, reg, val);
1927}
1928EXPORT_SYMBOL_GPL(snd_soc_write);
1929
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001930/**
1931 * snd_soc_update_bits - update codec register bits
1932 * @codec: audio codec
1933 * @reg: codec register
1934 * @mask: register mask
1935 * @value: new value
1936 *
1937 * Writes new register value.
1938 *
1939 * Returns 1 for change else 0.
1940 */
1941int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001942 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001943{
1944 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001945 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001946
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001947 old = snd_soc_read(codec, reg);
1948 new = (old & ~mask) | value;
1949 change = old != new;
1950 if (change)
1951 snd_soc_write(codec, reg, new);
1952
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001953 return change;
1954}
1955EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1956
1957/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001958 * snd_soc_update_bits_locked - update codec register bits
1959 * @codec: audio codec
1960 * @reg: codec register
1961 * @mask: register mask
1962 * @value: new value
1963 *
1964 * Writes new register value, and takes the codec mutex.
1965 *
1966 * Returns 1 for change else 0.
1967 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001968int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1969 unsigned short reg, unsigned int mask,
1970 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001971{
1972 int change;
1973
1974 mutex_lock(&codec->mutex);
1975 change = snd_soc_update_bits(codec, reg, mask, value);
1976 mutex_unlock(&codec->mutex);
1977
1978 return change;
1979}
Mark Browndd1b3d52009-12-04 14:22:03 +00001980EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001981
1982/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001983 * snd_soc_test_bits - test register for change
1984 * @codec: audio codec
1985 * @reg: codec register
1986 * @mask: register mask
1987 * @value: new value
1988 *
1989 * Tests a register with a new value and checks if the new value is
1990 * different from the old value.
1991 *
1992 * Returns 1 for change else 0.
1993 */
1994int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001995 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001996{
1997 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001998 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001999
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002000 old = snd_soc_read(codec, reg);
2001 new = (old & ~mask) | value;
2002 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002003
2004 return change;
2005}
2006EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2007
2008/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002009 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2010 * @substream: the pcm substream
2011 * @hw: the hardware parameters
2012 *
2013 * Sets the substream runtime hardware parameters.
2014 */
2015int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2016 const struct snd_pcm_hardware *hw)
2017{
2018 struct snd_pcm_runtime *runtime = substream->runtime;
2019 runtime->hw.info = hw->info;
2020 runtime->hw.formats = hw->formats;
2021 runtime->hw.period_bytes_min = hw->period_bytes_min;
2022 runtime->hw.period_bytes_max = hw->period_bytes_max;
2023 runtime->hw.periods_min = hw->periods_min;
2024 runtime->hw.periods_max = hw->periods_max;
2025 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2026 runtime->hw.fifo_size = hw->fifo_size;
2027 return 0;
2028}
2029EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2030
2031/**
2032 * snd_soc_cnew - create new control
2033 * @_template: control template
2034 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002035 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002036 *
2037 * Create a new mixer control from a template control.
2038 *
2039 * Returns 0 for success, else error.
2040 */
2041struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2042 void *data, char *long_name)
2043{
2044 struct snd_kcontrol_new template;
2045
2046 memcpy(&template, _template, sizeof(template));
2047 if (long_name)
2048 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002049 template.index = 0;
2050
2051 return snd_ctl_new1(&template, data);
2052}
2053EXPORT_SYMBOL_GPL(snd_soc_cnew);
2054
2055/**
Ian Molton3e8e1952009-01-09 00:23:21 +00002056 * snd_soc_add_controls - add an array of controls to a codec.
2057 * Convienience function to add a list of controls. Many codecs were
2058 * duplicating this code.
2059 *
2060 * @codec: codec to add controls to
2061 * @controls: array of controls to add
2062 * @num_controls: number of elements in the array
2063 *
2064 * Return 0 for success, else error.
2065 */
2066int snd_soc_add_controls(struct snd_soc_codec *codec,
2067 const struct snd_kcontrol_new *controls, int num_controls)
2068{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002069 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002070 int err, i;
2071
2072 for (i = 0; i < num_controls; i++) {
2073 const struct snd_kcontrol_new *control = &controls[i];
2074 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
2075 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002076 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2077 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002078 return err;
2079 }
2080 }
2081
2082 return 0;
2083}
2084EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2085
2086/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002087 * snd_soc_info_enum_double - enumerated double mixer info callback
2088 * @kcontrol: mixer control
2089 * @uinfo: control element information
2090 *
2091 * Callback to provide information about a double enumerated
2092 * mixer control.
2093 *
2094 * Returns 0 for success.
2095 */
2096int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2097 struct snd_ctl_elem_info *uinfo)
2098{
2099 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2100
2101 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2102 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002103 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002104
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002105 if (uinfo->value.enumerated.item > e->max - 1)
2106 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002107 strcpy(uinfo->value.enumerated.name,
2108 e->texts[uinfo->value.enumerated.item]);
2109 return 0;
2110}
2111EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2112
2113/**
2114 * snd_soc_get_enum_double - enumerated double mixer get callback
2115 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002116 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002117 *
2118 * Callback to get the value of a double enumerated mixer.
2119 *
2120 * Returns 0 for success.
2121 */
2122int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2123 struct snd_ctl_elem_value *ucontrol)
2124{
2125 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2126 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002127 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002128
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002129 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002130 ;
2131 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002132 ucontrol->value.enumerated.item[0]
2133 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002134 if (e->shift_l != e->shift_r)
2135 ucontrol->value.enumerated.item[1] =
2136 (val >> e->shift_r) & (bitmask - 1);
2137
2138 return 0;
2139}
2140EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2141
2142/**
2143 * snd_soc_put_enum_double - enumerated double mixer put callback
2144 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002145 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002146 *
2147 * Callback to set the value of a double enumerated mixer.
2148 *
2149 * Returns 0 for success.
2150 */
2151int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2152 struct snd_ctl_elem_value *ucontrol)
2153{
2154 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2155 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002156 unsigned int val;
2157 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002158
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002159 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002160 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002161 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002162 return -EINVAL;
2163 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2164 mask = (bitmask - 1) << e->shift_l;
2165 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002166 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002167 return -EINVAL;
2168 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2169 mask |= (bitmask - 1) << e->shift_r;
2170 }
2171
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002172 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002173}
2174EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2175
2176/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002177 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2178 * @kcontrol: mixer control
2179 * @ucontrol: control element information
2180 *
2181 * Callback to get the value of a double semi enumerated mixer.
2182 *
2183 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2184 * used for handling bitfield coded enumeration for example.
2185 *
2186 * Returns 0 for success.
2187 */
2188int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2189 struct snd_ctl_elem_value *ucontrol)
2190{
2191 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002192 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002193 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002194
2195 reg_val = snd_soc_read(codec, e->reg);
2196 val = (reg_val >> e->shift_l) & e->mask;
2197 for (mux = 0; mux < e->max; mux++) {
2198 if (val == e->values[mux])
2199 break;
2200 }
2201 ucontrol->value.enumerated.item[0] = mux;
2202 if (e->shift_l != e->shift_r) {
2203 val = (reg_val >> e->shift_r) & e->mask;
2204 for (mux = 0; mux < e->max; mux++) {
2205 if (val == e->values[mux])
2206 break;
2207 }
2208 ucontrol->value.enumerated.item[1] = mux;
2209 }
2210
2211 return 0;
2212}
2213EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2214
2215/**
2216 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2217 * @kcontrol: mixer control
2218 * @ucontrol: control element information
2219 *
2220 * Callback to set the value of a double semi enumerated mixer.
2221 *
2222 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2223 * used for handling bitfield coded enumeration for example.
2224 *
2225 * Returns 0 for success.
2226 */
2227int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2228 struct snd_ctl_elem_value *ucontrol)
2229{
2230 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002231 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002232 unsigned int val;
2233 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002234
2235 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2236 return -EINVAL;
2237 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2238 mask = e->mask << e->shift_l;
2239 if (e->shift_l != e->shift_r) {
2240 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2241 return -EINVAL;
2242 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2243 mask |= e->mask << e->shift_r;
2244 }
2245
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002246 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002247}
2248EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2249
2250/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002251 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2252 * @kcontrol: mixer control
2253 * @uinfo: control element information
2254 *
2255 * Callback to provide information about an external enumerated
2256 * single mixer.
2257 *
2258 * Returns 0 for success.
2259 */
2260int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2261 struct snd_ctl_elem_info *uinfo)
2262{
2263 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2264
2265 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2266 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002267 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002268
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002269 if (uinfo->value.enumerated.item > e->max - 1)
2270 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002271 strcpy(uinfo->value.enumerated.name,
2272 e->texts[uinfo->value.enumerated.item]);
2273 return 0;
2274}
2275EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2276
2277/**
2278 * snd_soc_info_volsw_ext - external single mixer info callback
2279 * @kcontrol: mixer control
2280 * @uinfo: control element information
2281 *
2282 * Callback to provide information about a single external mixer control.
2283 *
2284 * Returns 0 for success.
2285 */
2286int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2287 struct snd_ctl_elem_info *uinfo)
2288{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002289 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002290
Mark Brownfd5dfad2009-04-15 21:37:46 +01002291 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002292 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2293 else
2294 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2295
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002296 uinfo->count = 1;
2297 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002298 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002299 return 0;
2300}
2301EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2302
2303/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002304 * snd_soc_info_volsw - single mixer info callback
2305 * @kcontrol: mixer control
2306 * @uinfo: control element information
2307 *
2308 * Callback to provide information about a single mixer control.
2309 *
2310 * Returns 0 for success.
2311 */
2312int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2313 struct snd_ctl_elem_info *uinfo)
2314{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002315 struct soc_mixer_control *mc =
2316 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002317 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002318 unsigned int shift = mc->shift;
Jon Smirl815ecf82008-07-29 10:22:24 -04002319 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002320
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002321 if (!mc->platform_max)
2322 mc->platform_max = mc->max;
2323 platform_max = mc->platform_max;
2324
2325 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002326 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2327 else
2328 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2329
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002330 uinfo->count = shift == rshift ? 1 : 2;
2331 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002332 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002333 return 0;
2334}
2335EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2336
2337/**
2338 * snd_soc_get_volsw - single mixer get callback
2339 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002340 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002341 *
2342 * Callback to get the value of a single mixer control.
2343 *
2344 * Returns 0 for success.
2345 */
2346int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2347 struct snd_ctl_elem_value *ucontrol)
2348{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002349 struct soc_mixer_control *mc =
2350 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002351 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002352 unsigned int reg = mc->reg;
2353 unsigned int shift = mc->shift;
2354 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002355 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002356 unsigned int mask = (1 << fls(max)) - 1;
2357 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002358
2359 ucontrol->value.integer.value[0] =
2360 (snd_soc_read(codec, reg) >> shift) & mask;
2361 if (shift != rshift)
2362 ucontrol->value.integer.value[1] =
2363 (snd_soc_read(codec, reg) >> rshift) & mask;
2364 if (invert) {
2365 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002366 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002367 if (shift != rshift)
2368 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002369 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002370 }
2371
2372 return 0;
2373}
2374EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2375
2376/**
2377 * snd_soc_put_volsw - single mixer put callback
2378 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002379 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002380 *
2381 * Callback to set the value of a single mixer control.
2382 *
2383 * Returns 0 for success.
2384 */
2385int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_value *ucontrol)
2387{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002388 struct soc_mixer_control *mc =
2389 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002390 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002391 unsigned int reg = mc->reg;
2392 unsigned int shift = mc->shift;
2393 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002394 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002395 unsigned int mask = (1 << fls(max)) - 1;
2396 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002397 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002398
2399 val = (ucontrol->value.integer.value[0] & mask);
2400 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002401 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002402 val_mask = mask << shift;
2403 val = val << shift;
2404 if (shift != rshift) {
2405 val2 = (ucontrol->value.integer.value[1] & mask);
2406 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002407 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002408 val_mask |= mask << rshift;
2409 val |= val2 << rshift;
2410 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002411 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002412}
2413EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2414
2415/**
2416 * snd_soc_info_volsw_2r - double mixer info callback
2417 * @kcontrol: mixer control
2418 * @uinfo: control element information
2419 *
2420 * Callback to provide information about a double mixer control that
2421 * spans 2 codec registers.
2422 *
2423 * Returns 0 for success.
2424 */
2425int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_info *uinfo)
2427{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002428 struct soc_mixer_control *mc =
2429 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002430 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002431
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002432 if (!mc->platform_max)
2433 mc->platform_max = mc->max;
2434 platform_max = mc->platform_max;
2435
2436 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002437 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2438 else
2439 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2440
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002441 uinfo->count = 2;
2442 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002443 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002444 return 0;
2445}
2446EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2447
2448/**
2449 * snd_soc_get_volsw_2r - double mixer get callback
2450 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002451 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002452 *
2453 * Callback to get the value of a double mixer control that spans 2 registers.
2454 *
2455 * Returns 0 for success.
2456 */
2457int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2458 struct snd_ctl_elem_value *ucontrol)
2459{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002460 struct soc_mixer_control *mc =
2461 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002462 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002463 unsigned int reg = mc->reg;
2464 unsigned int reg2 = mc->rreg;
2465 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002466 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002467 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf82008-07-29 10:22:24 -04002468 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002469
2470 ucontrol->value.integer.value[0] =
2471 (snd_soc_read(codec, reg) >> shift) & mask;
2472 ucontrol->value.integer.value[1] =
2473 (snd_soc_read(codec, reg2) >> shift) & mask;
2474 if (invert) {
2475 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002476 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002477 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002478 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002479 }
2480
2481 return 0;
2482}
2483EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2484
2485/**
2486 * snd_soc_put_volsw_2r - double mixer set callback
2487 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002488 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002489 *
2490 * Callback to set the value of a double mixer control that spans 2 registers.
2491 *
2492 * Returns 0 for success.
2493 */
2494int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2495 struct snd_ctl_elem_value *ucontrol)
2496{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002497 struct soc_mixer_control *mc =
2498 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002499 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002500 unsigned int reg = mc->reg;
2501 unsigned int reg2 = mc->rreg;
2502 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002503 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002504 unsigned int mask = (1 << fls(max)) - 1;
2505 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002506 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002507 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002508
2509 val_mask = mask << shift;
2510 val = (ucontrol->value.integer.value[0] & mask);
2511 val2 = (ucontrol->value.integer.value[1] & mask);
2512
2513 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002514 val = max - val;
2515 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002516 }
2517
2518 val = val << shift;
2519 val2 = val2 << shift;
2520
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002521 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002522 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002523 return err;
2524
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002525 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002526 return err;
2527}
2528EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2529
Mark Browne13ac2e2008-05-28 17:58:05 +01002530/**
2531 * snd_soc_info_volsw_s8 - signed mixer info callback
2532 * @kcontrol: mixer control
2533 * @uinfo: control element information
2534 *
2535 * Callback to provide information about a signed mixer control.
2536 *
2537 * Returns 0 for success.
2538 */
2539int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2540 struct snd_ctl_elem_info *uinfo)
2541{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002542 struct soc_mixer_control *mc =
2543 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002544 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002545 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002546
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002547 if (!mc->platform_max)
2548 mc->platform_max = mc->max;
2549 platform_max = mc->platform_max;
2550
Mark Browne13ac2e2008-05-28 17:58:05 +01002551 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2552 uinfo->count = 2;
2553 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002554 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002555 return 0;
2556}
2557EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2558
2559/**
2560 * snd_soc_get_volsw_s8 - signed mixer get callback
2561 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002562 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002563 *
2564 * Callback to get the value of a signed mixer control.
2565 *
2566 * Returns 0 for success.
2567 */
2568int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2569 struct snd_ctl_elem_value *ucontrol)
2570{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002571 struct soc_mixer_control *mc =
2572 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002573 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002574 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002575 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002576 int val = snd_soc_read(codec, reg);
2577
2578 ucontrol->value.integer.value[0] =
2579 ((signed char)(val & 0xff))-min;
2580 ucontrol->value.integer.value[1] =
2581 ((signed char)((val >> 8) & 0xff))-min;
2582 return 0;
2583}
2584EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2585
2586/**
2587 * snd_soc_put_volsw_sgn - signed mixer put callback
2588 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002589 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002590 *
2591 * Callback to set the value of a signed mixer control.
2592 *
2593 * Returns 0 for success.
2594 */
2595int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2596 struct snd_ctl_elem_value *ucontrol)
2597{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002598 struct soc_mixer_control *mc =
2599 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002600 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002601 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002602 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002603 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002604
2605 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2606 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2607
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002608 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002609}
2610EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2611
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002612/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002613 * snd_soc_limit_volume - Set new limit to an existing volume control.
2614 *
2615 * @codec: where to look for the control
2616 * @name: Name of the control
2617 * @max: new maximum limit
2618 *
2619 * Return 0 for success, else error.
2620 */
2621int snd_soc_limit_volume(struct snd_soc_codec *codec,
2622 const char *name, int max)
2623{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002624 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002625 struct snd_kcontrol *kctl;
2626 struct soc_mixer_control *mc;
2627 int found = 0;
2628 int ret = -EINVAL;
2629
2630 /* Sanity check for name and max */
2631 if (unlikely(!name || max <= 0))
2632 return -EINVAL;
2633
2634 list_for_each_entry(kctl, &card->controls, list) {
2635 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2636 found = 1;
2637 break;
2638 }
2639 }
2640 if (found) {
2641 mc = (struct soc_mixer_control *)kctl->private_value;
2642 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002643 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002644 ret = 0;
2645 }
2646 }
2647 return ret;
2648}
2649EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2650
2651/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002652 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2653 * mixer info callback
2654 * @kcontrol: mixer control
2655 * @uinfo: control element information
2656 *
2657 * Returns 0 for success.
2658 */
2659int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_info *uinfo)
2661{
2662 struct soc_mixer_control *mc =
2663 (struct soc_mixer_control *)kcontrol->private_value;
2664 int max = mc->max;
2665 int min = mc->min;
2666
2667 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2668 uinfo->count = 2;
2669 uinfo->value.integer.min = 0;
2670 uinfo->value.integer.max = max-min;
2671
2672 return 0;
2673}
2674EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2675
2676/**
2677 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2678 * mixer get callback
2679 * @kcontrol: mixer control
2680 * @uinfo: control element information
2681 *
2682 * Returns 0 for success.
2683 */
2684int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2685 struct snd_ctl_elem_value *ucontrol)
2686{
2687 struct soc_mixer_control *mc =
2688 (struct soc_mixer_control *)kcontrol->private_value;
2689 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2690 unsigned int mask = (1<<mc->shift)-1;
2691 int min = mc->min;
2692 int val = snd_soc_read(codec, mc->reg) & mask;
2693 int valr = snd_soc_read(codec, mc->rreg) & mask;
2694
Stuart Longland20630c72010-06-18 12:56:10 +10002695 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2696 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002697 return 0;
2698}
2699EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2700
2701/**
2702 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2703 * mixer put callback
2704 * @kcontrol: mixer control
2705 * @uinfo: control element information
2706 *
2707 * Returns 0 for success.
2708 */
2709int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2710 struct snd_ctl_elem_value *ucontrol)
2711{
2712 struct soc_mixer_control *mc =
2713 (struct soc_mixer_control *)kcontrol->private_value;
2714 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2715 unsigned int mask = (1<<mc->shift)-1;
2716 int min = mc->min;
2717 int ret;
2718 unsigned int val, valr, oval, ovalr;
2719
2720 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2721 val &= mask;
2722 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2723 valr &= mask;
2724
2725 oval = snd_soc_read(codec, mc->reg) & mask;
2726 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2727
2728 ret = 0;
2729 if (oval != val) {
2730 ret = snd_soc_write(codec, mc->reg, val);
2731 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002732 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002733 }
2734 if (ovalr != valr) {
2735 ret = snd_soc_write(codec, mc->rreg, valr);
2736 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002737 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002738 }
2739
2740 return 0;
2741}
2742EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2743
2744/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002745 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2746 * @dai: DAI
2747 * @clk_id: DAI specific clock ID
2748 * @freq: new clock frequency in Hz
2749 * @dir: new clock direction - input/output.
2750 *
2751 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2752 */
2753int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2754 unsigned int freq, int dir)
2755{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002756 if (dai->driver && dai->driver->ops->set_sysclk)
2757 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002758 else
2759 return -EINVAL;
2760}
2761EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2762
2763/**
2764 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2765 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002766 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002767 * @div: new clock divisor.
2768 *
2769 * Configures the clock dividers. This is used to derive the best DAI bit and
2770 * frame clocks from the system or master clock. It's best to set the DAI bit
2771 * and frame clocks as low as possible to save system power.
2772 */
2773int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2774 int div_id, int div)
2775{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002776 if (dai->driver && dai->driver->ops->set_clkdiv)
2777 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002778 else
2779 return -EINVAL;
2780}
2781EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2782
2783/**
2784 * snd_soc_dai_set_pll - configure DAI PLL.
2785 * @dai: DAI
2786 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002787 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002788 * @freq_in: PLL input clock frequency in Hz
2789 * @freq_out: requested PLL output clock frequency in Hz
2790 *
2791 * Configures and enables PLL to generate output clock based on input clock.
2792 */
Mark Brown85488032009-09-05 18:52:16 +01002793int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2794 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002795{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002796 if (dai->driver && dai->driver->ops->set_pll)
2797 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002798 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002799 else
2800 return -EINVAL;
2801}
2802EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2803
2804/**
2805 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2806 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002807 * @fmt: SND_SOC_DAIFMT_ format value.
2808 *
2809 * Configures the DAI hardware format and clocking.
2810 */
2811int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2812{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002813 if (dai->driver && dai->driver->ops->set_fmt)
2814 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002815 else
2816 return -EINVAL;
2817}
2818EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2819
2820/**
2821 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2822 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002823 * @tx_mask: bitmask representing active TX slots.
2824 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002825 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002826 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002827 *
2828 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2829 * specific.
2830 */
2831int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002832 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002833{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002834 if (dai->driver && dai->driver->ops->set_tdm_slot)
2835 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002836 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002837 else
2838 return -EINVAL;
2839}
2840EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2841
2842/**
Barry Song472df3c2009-09-12 01:16:29 +08002843 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2844 * @dai: DAI
2845 * @tx_num: how many TX channels
2846 * @tx_slot: pointer to an array which imply the TX slot number channel
2847 * 0~num-1 uses
2848 * @rx_num: how many RX channels
2849 * @rx_slot: pointer to an array which imply the RX slot number channel
2850 * 0~num-1 uses
2851 *
2852 * configure the relationship between channel number and TDM slot number.
2853 */
2854int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2855 unsigned int tx_num, unsigned int *tx_slot,
2856 unsigned int rx_num, unsigned int *rx_slot)
2857{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002858 if (dai->driver && dai->driver->ops->set_channel_map)
2859 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002860 rx_num, rx_slot);
2861 else
2862 return -EINVAL;
2863}
2864EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2865
2866/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002867 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2868 * @dai: DAI
2869 * @tristate: tristate enable
2870 *
2871 * Tristates the DAI so that others can use it.
2872 */
2873int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2874{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002875 if (dai->driver && dai->driver->ops->set_tristate)
2876 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002877 else
2878 return -EINVAL;
2879}
2880EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2881
2882/**
2883 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2884 * @dai: DAI
2885 * @mute: mute enable
2886 *
2887 * Mutes the DAI DAC.
2888 */
2889int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2890{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002891 if (dai->driver && dai->driver->ops->digital_mute)
2892 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002893 else
2894 return -EINVAL;
2895}
2896EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2897
Mark Brownc5af3a22008-11-28 13:29:45 +00002898/**
2899 * snd_soc_register_card - Register a card with the ASoC core
2900 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002901 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002902 *
2903 * Note that currently this is an internal only function: it will be
2904 * exposed to machine drivers after further backporting of ASoC v2
2905 * registration APIs.
2906 */
2907static int snd_soc_register_card(struct snd_soc_card *card)
2908{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002909 int i;
2910
Mark Brownc5af3a22008-11-28 13:29:45 +00002911 if (!card->name || !card->dev)
2912 return -EINVAL;
2913
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002914 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2915 GFP_KERNEL);
2916 if (card->rtd == NULL)
2917 return -ENOMEM;
2918
2919 for (i = 0; i < card->num_links; i++)
2920 card->rtd[i].dai_link = &card->dai_link[i];
2921
Mark Brownc5af3a22008-11-28 13:29:45 +00002922 INIT_LIST_HEAD(&card->list);
2923 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002924 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002925
2926 mutex_lock(&client_mutex);
2927 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002928 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002929 mutex_unlock(&client_mutex);
2930
2931 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2932
2933 return 0;
2934}
2935
2936/**
2937 * snd_soc_unregister_card - Unregister a card with the ASoC core
2938 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002939 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002940 *
2941 * Note that currently this is an internal only function: it will be
2942 * exposed to machine drivers after further backporting of ASoC v2
2943 * registration APIs.
2944 */
2945static int snd_soc_unregister_card(struct snd_soc_card *card)
2946{
2947 mutex_lock(&client_mutex);
2948 list_del(&card->list);
2949 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002950 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2951
2952 return 0;
2953}
2954
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002955/*
2956 * Simplify DAI link configuration by removing ".-1" from device names
2957 * and sanitizing names.
2958 */
2959static inline char *fmt_single_name(struct device *dev, int *id)
2960{
2961 char *found, name[NAME_SIZE];
2962 int id1, id2;
2963
2964 if (dev_name(dev) == NULL)
2965 return NULL;
2966
2967 strncpy(name, dev_name(dev), NAME_SIZE);
2968
2969 /* are we a "%s.%d" name (platform and SPI components) */
2970 found = strstr(name, dev->driver->name);
2971 if (found) {
2972 /* get ID */
2973 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2974
2975 /* discard ID from name if ID == -1 */
2976 if (*id == -1)
2977 found[strlen(dev->driver->name)] = '\0';
2978 }
2979
2980 } else {
2981 /* I2C component devices are named "bus-addr" */
2982 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2983 char tmp[NAME_SIZE];
2984
2985 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002986 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002987
2988 /* sanitize component name for DAI link creation */
2989 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2990 strncpy(name, tmp, NAME_SIZE);
2991 } else
2992 *id = 0;
2993 }
2994
2995 return kstrdup(name, GFP_KERNEL);
2996}
2997
2998/*
2999 * Simplify DAI link naming for single devices with multiple DAIs by removing
3000 * any ".-1" and using the DAI name (instead of device name).
3001 */
3002static inline char *fmt_multiple_name(struct device *dev,
3003 struct snd_soc_dai_driver *dai_drv)
3004{
3005 if (dai_drv->name == NULL) {
3006 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3007 dev_name(dev));
3008 return NULL;
3009 }
3010
3011 return kstrdup(dai_drv->name, GFP_KERNEL);
3012}
3013
Mark Brown91151712008-11-30 23:31:24 +00003014/**
3015 * snd_soc_register_dai - Register a DAI with the ASoC core
3016 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003017 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003018 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003019int snd_soc_register_dai(struct device *dev,
3020 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003021{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003022 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003023
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003024 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003025
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003026 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3027 if (dai == NULL)
3028 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003029
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003030 /* create DAI component name */
3031 dai->name = fmt_single_name(dev, &dai->id);
3032 if (dai->name == NULL) {
3033 kfree(dai);
3034 return -ENOMEM;
3035 }
3036
3037 dai->dev = dev;
3038 dai->driver = dai_drv;
3039 if (!dai->driver->ops)
3040 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003041
3042 mutex_lock(&client_mutex);
3043 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003044 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003045 mutex_unlock(&client_mutex);
3046
3047 pr_debug("Registered DAI '%s'\n", dai->name);
3048
3049 return 0;
3050}
3051EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3052
3053/**
3054 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3055 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003056 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003057 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003058void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003059{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003060 struct snd_soc_dai *dai;
3061
3062 list_for_each_entry(dai, &dai_list, list) {
3063 if (dev == dai->dev)
3064 goto found;
3065 }
3066 return;
3067
3068found:
Mark Brown91151712008-11-30 23:31:24 +00003069 mutex_lock(&client_mutex);
3070 list_del(&dai->list);
3071 mutex_unlock(&client_mutex);
3072
3073 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003074 kfree(dai->name);
3075 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003076}
3077EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3078
3079/**
3080 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3081 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003082 * @dai: Array of DAIs to register
3083 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003084 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003085int snd_soc_register_dais(struct device *dev,
3086 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003087{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003088 struct snd_soc_dai *dai;
3089 int i, ret = 0;
3090
Liam Girdwood720ffa42010-08-18 00:30:30 +01003091 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003092
3093 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003094
3095 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003096 if (dai == NULL) {
3097 ret = -ENOMEM;
3098 goto err;
3099 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003100
3101 /* create DAI component name */
3102 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3103 if (dai->name == NULL) {
3104 kfree(dai);
3105 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003106 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003107 }
3108
3109 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003110 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003111 if (dai->driver->id)
3112 dai->id = dai->driver->id;
3113 else
3114 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003115 if (!dai->driver->ops)
3116 dai->driver->ops = &null_dai_ops;
3117
3118 mutex_lock(&client_mutex);
3119 list_add(&dai->list, &dai_list);
3120 mutex_unlock(&client_mutex);
3121
3122 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003123 }
3124
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003125 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003126 return 0;
3127
3128err:
3129 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003130 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003131
3132 return ret;
3133}
3134EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3135
3136/**
3137 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3138 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003139 * @dai: Array of DAIs to unregister
3140 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003141 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003142void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003143{
3144 int i;
3145
3146 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003147 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003148}
3149EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3150
Mark Brown12a48a8c2008-12-03 19:40:30 +00003151/**
3152 * snd_soc_register_platform - Register a platform with the ASoC core
3153 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003154 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003155 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003156int snd_soc_register_platform(struct device *dev,
3157 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003158{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003159 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003160
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003161 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3162
3163 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3164 if (platform == NULL)
3165 return -ENOMEM;
3166
3167 /* create platform component name */
3168 platform->name = fmt_single_name(dev, &platform->id);
3169 if (platform->name == NULL) {
3170 kfree(platform);
3171 return -ENOMEM;
3172 }
3173
3174 platform->dev = dev;
3175 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003176
3177 mutex_lock(&client_mutex);
3178 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003179 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003180 mutex_unlock(&client_mutex);
3181
3182 pr_debug("Registered platform '%s'\n", platform->name);
3183
3184 return 0;
3185}
3186EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3187
3188/**
3189 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3190 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003191 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003192 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003193void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003194{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003195 struct snd_soc_platform *platform;
3196
3197 list_for_each_entry(platform, &platform_list, list) {
3198 if (dev == platform->dev)
3199 goto found;
3200 }
3201 return;
3202
3203found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003204 mutex_lock(&client_mutex);
3205 list_del(&platform->list);
3206 mutex_unlock(&client_mutex);
3207
3208 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003209 kfree(platform->name);
3210 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003211}
3212EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3213
Mark Brown151ab222009-05-09 16:22:58 +01003214static u64 codec_format_map[] = {
3215 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3216 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3217 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3218 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3219 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3220 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3221 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3222 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3223 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3224 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3225 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3226 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3227 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3228 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3229 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3230 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3231};
3232
3233/* Fix up the DAI formats for endianness: codecs don't actually see
3234 * the endianness of the data but we're using the CPU format
3235 * definitions which do need to include endianness so we ensure that
3236 * codec DAIs always have both big and little endian variants set.
3237 */
3238static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3239{
3240 int i;
3241
3242 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3243 if (stream->formats & codec_format_map[i])
3244 stream->formats |= codec_format_map[i];
3245}
3246
Mark Brown0d0cf002008-12-10 14:32:45 +00003247/**
3248 * snd_soc_register_codec - Register a codec with the ASoC core
3249 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003250 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003251 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003252int snd_soc_register_codec(struct device *dev,
3253 struct snd_soc_codec_driver *codec_drv,
3254 struct snd_soc_dai_driver *dai_drv, int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003255{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003256 struct snd_soc_codec *codec;
3257 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003258
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003259 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003260
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003261 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3262 if (codec == NULL)
3263 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003264
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003265 /* create CODEC component name */
3266 codec->name = fmt_single_name(dev, &codec->id);
3267 if (codec->name == NULL) {
3268 kfree(codec);
3269 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003270 }
3271
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003272 INIT_LIST_HEAD(&codec->dapm.widgets);
3273 INIT_LIST_HEAD(&codec->dapm.paths);
3274 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3275 codec->dapm.dev = dev;
3276 codec->dapm.codec = codec;
3277
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003278 /* allocate CODEC register cache */
3279 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3280
3281 if (codec_drv->reg_cache_default)
3282 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3283 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3284 else
3285 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3286 codec_drv->reg_word_size, GFP_KERNEL);
3287
3288 if (codec->reg_cache == NULL) {
3289 kfree(codec->name);
3290 kfree(codec);
3291 return -ENOMEM;
3292 }
3293 }
3294
3295 codec->dev = dev;
3296 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003297 codec->num_dai = num_dai;
3298 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003299
3300 for (i = 0; i < num_dai; i++) {
3301 fixup_codec_formats(&dai_drv[i].playback);
3302 fixup_codec_formats(&dai_drv[i].capture);
3303 }
3304
Mark Brown26b01cc2010-08-18 20:20:55 +01003305 /* register any DAIs */
3306 if (num_dai) {
3307 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3308 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003309 goto error;
Mark Brown26b01cc2010-08-18 20:20:55 +01003310 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003311
Mark Brown0d0cf002008-12-10 14:32:45 +00003312 mutex_lock(&client_mutex);
3313 list_add(&codec->list, &codec_list);
3314 snd_soc_instantiate_cards();
3315 mutex_unlock(&client_mutex);
3316
3317 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003318 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003319
3320error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003321 if (codec->reg_cache)
3322 kfree(codec->reg_cache);
3323 kfree(codec->name);
3324 kfree(codec);
3325 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003326}
3327EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3328
3329/**
3330 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3331 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003332 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003333 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003334void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003335{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003336 struct snd_soc_codec *codec;
3337 int i;
3338
3339 list_for_each_entry(codec, &codec_list, list) {
3340 if (dev == codec->dev)
3341 goto found;
3342 }
3343 return;
3344
3345found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003346 if (codec->num_dai)
3347 for (i = 0; i < codec->num_dai; i++)
3348 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003349
Mark Brown0d0cf002008-12-10 14:32:45 +00003350 mutex_lock(&client_mutex);
3351 list_del(&codec->list);
3352 mutex_unlock(&client_mutex);
3353
3354 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003355
3356 if (codec->reg_cache)
3357 kfree(codec->reg_cache);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003358 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003359 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003360}
3361EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3362
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003363static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003364{
Mark Brown384c89e2008-12-03 17:34:03 +00003365#ifdef CONFIG_DEBUG_FS
3366 debugfs_root = debugfs_create_dir("asoc", NULL);
3367 if (IS_ERR(debugfs_root) || !debugfs_root) {
3368 printk(KERN_WARNING
3369 "ASoC: Failed to create debugfs directory\n");
3370 debugfs_root = NULL;
3371 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003372
3373 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3374 &codec_list_fops))
3375 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3376
Mark Brownf3208782010-09-15 18:19:07 +01003377 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3378 &dai_list_fops))
3379 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003380
3381 if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
3382 &platform_list_fops))
3383 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003384#endif
3385
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003386 return platform_driver_register(&soc_driver);
3387}
Mark Brown4abe8e12010-10-12 17:41:03 +01003388module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003389
Mark Brown7d8c16a2008-11-30 22:11:24 +00003390static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003391{
Mark Brown384c89e2008-12-03 17:34:03 +00003392#ifdef CONFIG_DEBUG_FS
3393 debugfs_remove_recursive(debugfs_root);
3394#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003395 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003396}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003397module_exit(snd_soc_exit);
3398
3399/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003400MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003401MODULE_DESCRIPTION("ALSA SoC Core");
3402MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003403MODULE_ALIAS("platform:soc-audio");