blob: 9dfbb8fcb7654e09a5f6f08ea97c037beae9dc95 [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>
Mark Brown3028eb82010-12-05 12:22:46 +000036#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020037#include <sound/pcm.h>
38#include <sound/pcm_params.h>
39#include <sound/soc.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040#include <sound/initval.h>
41
Mark Browna8b1d342010-11-03 18:05:58 -040042#define CREATE_TRACE_POINTS
43#include <trace/events/asoc.h>
44
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000045#define NAME_SIZE 32
46
Frank Mandarinodb2a4162006-10-06 18:31:09 +020047static DEFINE_MUTEX(pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020048static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
49
Mark Brown384c89e2008-12-03 17:34:03 +000050#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000051struct dentry *snd_soc_debugfs_root;
52EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000053#endif
54
Mark Brownc5af3a22008-11-28 13:29:45 +000055static DEFINE_MUTEX(client_mutex);
56static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000057static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000058static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000059static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000060
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000061static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
Mark Brownc5af3a22008-11-28 13:29:45 +000062
Frank Mandarinodb2a4162006-10-06 18:31:09 +020063/*
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68static int pmdown_time = 5000;
69module_param(pmdown_time, int, 0);
70MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
Mark Brown2624d5f2009-11-03 21:56:13 +000072/* codec register dump */
73static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
74{
Mark Brown5164d742010-07-14 16:14:33 +010075 int ret, i, step = 1, count = 0;
Mark Brown2624d5f2009-11-03 21:56:13 +000076
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000077 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +000078 return 0;
79
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000080 if (codec->driver->reg_cache_step)
81 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +000082
83 count += sprintf(buf, "%s registers\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000084 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +000085 if (codec->readable_register && !codec->readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +000086 continue;
87
88 count += sprintf(buf + count, "%2x: ", i);
89 if (count >= PAGE_SIZE - 1)
90 break;
91
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000092 if (codec->driver->display_register) {
93 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +000094 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +010095 } else {
96 /* If the read fails it's almost certainly due to
97 * the register being volatile and the device being
98 * powered off.
99 */
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000100 ret = snd_soc_read(codec, i);
Mark Brown5164d742010-07-14 16:14:33 +0100101 if (ret >= 0)
102 count += snprintf(buf + count,
103 PAGE_SIZE - count,
104 "%4x", ret);
105 else
106 count += snprintf(buf + count,
107 PAGE_SIZE - count,
108 "<no data: %d>", ret);
109 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000110
111 if (count >= PAGE_SIZE - 1)
112 break;
113
114 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
115 if (count >= PAGE_SIZE - 1)
116 break;
117 }
118
119 /* Truncate count; min() would cause a warning */
120 if (count >= PAGE_SIZE)
121 count = PAGE_SIZE - 1;
122
123 return count;
124}
125static ssize_t codec_reg_show(struct device *dev,
126 struct device_attribute *attr, char *buf)
127{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000128 struct snd_soc_pcm_runtime *rtd =
129 container_of(dev, struct snd_soc_pcm_runtime, dev);
130
131 return soc_codec_reg_show(rtd->codec, buf);
Mark Brown2624d5f2009-11-03 21:56:13 +0000132}
133
134static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
135
Mark Browndbe21402010-02-12 11:37:24 +0000136static ssize_t pmdown_time_show(struct device *dev,
137 struct device_attribute *attr, char *buf)
138{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000139 struct snd_soc_pcm_runtime *rtd =
140 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Browndbe21402010-02-12 11:37:24 +0000141
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000142 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000143}
144
145static ssize_t pmdown_time_set(struct device *dev,
146 struct device_attribute *attr,
147 const char *buf, size_t count)
148{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000149 struct snd_soc_pcm_runtime *rtd =
150 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Brownc593b522010-10-27 20:11:17 -0700151 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000152
Mark Brownc593b522010-10-27 20:11:17 -0700153 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
154 if (ret)
155 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000156
157 return count;
158}
159
160static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
161
Mark Brown2624d5f2009-11-03 21:56:13 +0000162#ifdef CONFIG_DEBUG_FS
163static int codec_reg_open_file(struct inode *inode, struct file *file)
164{
165 file->private_data = inode->i_private;
166 return 0;
167}
168
169static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
170 size_t count, loff_t *ppos)
171{
172 ssize_t ret;
173 struct snd_soc_codec *codec = file->private_data;
174 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
175 if (!buf)
176 return -ENOMEM;
177 ret = soc_codec_reg_show(codec, buf);
178 if (ret >= 0)
179 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
180 kfree(buf);
181 return ret;
182}
183
184static ssize_t codec_reg_write_file(struct file *file,
185 const char __user *user_buf, size_t count, loff_t *ppos)
186{
187 char buf[32];
188 int buf_size;
189 char *start = buf;
190 unsigned long reg, value;
191 int step = 1;
192 struct snd_soc_codec *codec = file->private_data;
193
194 buf_size = min(count, (sizeof(buf)-1));
195 if (copy_from_user(buf, user_buf, buf_size))
196 return -EFAULT;
197 buf[buf_size] = 0;
198
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000199 if (codec->driver->reg_cache_step)
200 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000201
202 while (*start == ' ')
203 start++;
204 reg = simple_strtoul(start, &start, 16);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000205 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
Mark Brown2624d5f2009-11-03 21:56:13 +0000206 return -EINVAL;
207 while (*start == ' ')
208 start++;
209 if (strict_strtoul(start, 16, &value))
210 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000211
212 /* Userspace has been fiddling around behind the kernel's back */
213 add_taint(TAINT_USER);
214
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000215 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000216 return buf_size;
217}
218
219static const struct file_operations codec_reg_fops = {
220 .open = codec_reg_open_file,
221 .read = codec_reg_read_file,
222 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200223 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000224};
225
226static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
227{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200228 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
229
230 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
231 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000232 if (!codec->debugfs_codec_root) {
233 printk(KERN_WARNING
234 "ASoC: Failed to create codec debugfs directory\n");
235 return;
236 }
237
Mark Brownaaee8ef2011-01-26 20:53:50 +0000238 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
239 &codec->cache_sync);
240 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
241 &codec->cache_only);
242
Mark Brown2624d5f2009-11-03 21:56:13 +0000243 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
244 codec->debugfs_codec_root,
245 codec, &codec_reg_fops);
246 if (!codec->debugfs_reg)
247 printk(KERN_WARNING
248 "ASoC: Failed to create codec register debugfs file\n");
249
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200250 codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
Mark Brown2624d5f2009-11-03 21:56:13 +0000251 codec->debugfs_codec_root);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200252 if (!codec->dapm.debugfs_dapm)
Mark Brown2624d5f2009-11-03 21:56:13 +0000253 printk(KERN_WARNING
254 "Failed to create DAPM debugfs directory\n");
255
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200256 snd_soc_dapm_debugfs_init(&codec->dapm);
Mark Brown2624d5f2009-11-03 21:56:13 +0000257}
258
259static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
260{
261 debugfs_remove_recursive(codec->debugfs_codec_root);
262}
263
Mark Brownc3c5a192010-09-15 18:15:14 +0100264static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
265 size_t count, loff_t *ppos)
266{
267 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100268 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100269 struct snd_soc_codec *codec;
270
271 if (!buf)
272 return -ENOMEM;
273
Mark Brown2b194f9d2010-10-13 10:52:16 +0100274 list_for_each_entry(codec, &codec_list, list) {
275 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
276 codec->name);
277 if (len >= 0)
278 ret += len;
279 if (ret > PAGE_SIZE) {
280 ret = PAGE_SIZE;
281 break;
282 }
283 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100284
285 if (ret >= 0)
286 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
287
288 kfree(buf);
289
290 return ret;
291}
292
293static const struct file_operations codec_list_fops = {
294 .read = codec_list_read_file,
295 .llseek = default_llseek,/* read accesses f_pos */
296};
297
Mark Brownf3208782010-09-15 18:19:07 +0100298static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
299 size_t count, loff_t *ppos)
300{
301 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100302 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100303 struct snd_soc_dai *dai;
304
305 if (!buf)
306 return -ENOMEM;
307
Mark Brown2b194f9d2010-10-13 10:52:16 +0100308 list_for_each_entry(dai, &dai_list, list) {
309 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
310 if (len >= 0)
311 ret += len;
312 if (ret > PAGE_SIZE) {
313 ret = PAGE_SIZE;
314 break;
315 }
316 }
Mark Brownf3208782010-09-15 18:19:07 +0100317
Mark Brown2b194f9d2010-10-13 10:52:16 +0100318 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100319
320 kfree(buf);
321
322 return ret;
323}
324
325static const struct file_operations dai_list_fops = {
326 .read = dai_list_read_file,
327 .llseek = default_llseek,/* read accesses f_pos */
328};
329
Mark Brown19c7ac22010-09-15 18:22:40 +0100330static ssize_t platform_list_read_file(struct file *file,
331 char __user *user_buf,
332 size_t count, loff_t *ppos)
333{
334 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100335 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100336 struct snd_soc_platform *platform;
337
338 if (!buf)
339 return -ENOMEM;
340
Mark Brown2b194f9d2010-10-13 10:52:16 +0100341 list_for_each_entry(platform, &platform_list, list) {
342 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
343 platform->name);
344 if (len >= 0)
345 ret += len;
346 if (ret > PAGE_SIZE) {
347 ret = PAGE_SIZE;
348 break;
349 }
350 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100351
Mark Brown2b194f9d2010-10-13 10:52:16 +0100352 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100353
354 kfree(buf);
355
356 return ret;
357}
358
359static const struct file_operations platform_list_fops = {
360 .read = platform_list_read_file,
361 .llseek = default_llseek,/* read accesses f_pos */
362};
363
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200364static void soc_init_card_debugfs(struct snd_soc_card *card)
365{
366 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000367 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200368 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200369 dev_warn(card->dev,
370 "ASoC: Failed to create codec debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200371 return;
372 }
373
374 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
375 card->debugfs_card_root,
376 &card->pop_time);
377 if (!card->debugfs_pop_time)
378 dev_warn(card->dev,
379 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200380}
381
382static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
383{
384 debugfs_remove_recursive(card->debugfs_card_root);
385}
386
Mark Brown2624d5f2009-11-03 21:56:13 +0000387#else
388
389static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
390{
391}
392
393static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
394{
395}
Axel Linb95fccb2010-11-09 17:06:44 +0800396
397static inline void soc_init_card_debugfs(struct snd_soc_card *card)
398{
399}
400
401static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
402{
403}
Mark Brown2624d5f2009-11-03 21:56:13 +0000404#endif
405
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200406#ifdef CONFIG_SND_SOC_AC97_BUS
407/* unregister ac97 codec */
408static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
409{
410 if (codec->ac97->dev.bus)
411 device_unregister(&codec->ac97->dev);
412 return 0;
413}
414
415/* stop no dev release warning */
416static void soc_ac97_device_release(struct device *dev){}
417
418/* register ac97 codec to bus */
419static int soc_ac97_dev_register(struct snd_soc_codec *codec)
420{
421 int err;
422
423 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100424 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200425 codec->ac97->dev.release = soc_ac97_device_release;
426
Kay Sieversbb072bf2008-11-02 03:50:35 +0100427 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000428 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200429 err = device_register(&codec->ac97->dev);
430 if (err < 0) {
431 snd_printk(KERN_ERR "Can't register ac97 bus\n");
432 codec->ac97->dev.bus = NULL;
433 return err;
434 }
435 return 0;
436}
437#endif
438
Mark Brown06f409d2009-04-07 18:10:13 +0100439static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
440{
441 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000442 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
443 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100444 int ret;
445
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000446 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
447 rtd->dai_link->symmetric_rates) {
448 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
449 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100450
451 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
452 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000453 rtd->rate,
454 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100455 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000456 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100457 "Unable to apply rate symmetry constraint: %d\n", ret);
458 return ret;
459 }
460 }
461
462 return 0;
463}
464
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200465/*
466 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
467 * then initialized and any private data can be allocated. This also calls
468 * startup for the cpu DAI, platform, machine and codec DAI.
469 */
470static int soc_pcm_open(struct snd_pcm_substream *substream)
471{
472 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200473 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000474 struct snd_soc_platform *platform = rtd->platform;
475 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
476 struct snd_soc_dai *codec_dai = rtd->codec_dai;
477 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
478 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200479 int ret = 0;
480
481 mutex_lock(&pcm_mutex);
482
483 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000484 if (cpu_dai->driver->ops->startup) {
485 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200486 if (ret < 0) {
487 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100488 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200489 goto out;
490 }
491 }
492
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000493 if (platform->driver->ops->open) {
494 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200495 if (ret < 0) {
496 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
497 goto platform_err;
498 }
499 }
500
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501 if (codec_dai->driver->ops->startup) {
502 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100503 if (ret < 0) {
504 printk(KERN_ERR "asoc: can't open codec %s\n",
505 codec_dai->name);
506 goto codec_dai_err;
507 }
508 }
509
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000510 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
511 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200512 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000513 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200514 goto machine_err;
515 }
516 }
517
Mark Browna00f90f2010-12-02 16:24:24 +0000518 /* Check that the codec and cpu DAIs are compatible */
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200519 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
520 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000521 max(codec_dai_drv->playback.rate_min,
522 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200523 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000524 min(codec_dai_drv->playback.rate_max,
525 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200526 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000527 max(codec_dai_drv->playback.channels_min,
528 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200529 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000530 min(codec_dai_drv->playback.channels_max,
531 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100532 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000533 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100534 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000535 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
536 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900537 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000538 runtime->hw.rates |= cpu_dai_drv->playback.rates;
539 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900540 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200542 } else {
543 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000544 max(codec_dai_drv->capture.rate_min,
545 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200546 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547 min(codec_dai_drv->capture.rate_max,
548 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200549 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 max(codec_dai_drv->capture.channels_min,
551 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200552 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 min(codec_dai_drv->capture.channels_max,
554 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100555 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000556 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100557 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
559 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900560 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 runtime->hw.rates |= cpu_dai_drv->capture.rates;
562 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900563 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200565 }
566
567 snd_pcm_limit_hw_rates(runtime);
568 if (!runtime->hw.rates) {
569 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100570 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900571 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200572 }
573 if (!runtime->hw.formats) {
574 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100575 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900576 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200577 }
578 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
579 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900581 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200582 }
583
Mark Brown06f409d2009-04-07 18:10:13 +0100584 /* Symmetry only applies if we've already got an active stream. */
585 if (cpu_dai->active || codec_dai->active) {
586 ret = soc_pcm_apply_symmetry(substream);
587 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900588 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100589 }
590
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 pr_debug("asoc: %s <-> %s info:\n",
592 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100593 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
594 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
595 runtime->hw.channels_max);
596 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
597 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200598
Jassi Brar14dc5732010-02-26 09:12:32 +0900599 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600 cpu_dai->playback_active++;
601 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900602 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000603 cpu_dai->capture_active++;
604 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900605 }
606 cpu_dai->active++;
607 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200609 mutex_unlock(&pcm_mutex);
610 return 0;
611
Jassi Brarbb1c0472010-02-25 11:24:53 +0900612config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
614 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200615
Jassi Brarbb1c0472010-02-25 11:24:53 +0900616machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 if (codec_dai->driver->ops->shutdown)
618 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900619
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100620codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 if (platform->driver->ops->close)
622 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200623
624platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000625 if (cpu_dai->driver->ops->shutdown)
626 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200627out:
628 mutex_unlock(&pcm_mutex);
629 return ret;
630}
631
632/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200633 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634 * This is to ensure there are no pops or clicks in between any music tracks
635 * due to DAPM power cycling.
636 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100637static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200638{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 struct snd_soc_pcm_runtime *rtd =
640 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
641 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200642
643 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200644
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000645 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
646 codec_dai->driver->playback.stream_name,
647 codec_dai->playback_active ? "active" : "inactive",
648 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200649
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000650 /* are we waiting on this codec DAI stream */
651 if (codec_dai->pop_wait == 1) {
652 codec_dai->pop_wait = 0;
653 snd_soc_dapm_stream_event(rtd,
654 codec_dai->driver->playback.stream_name,
655 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200656 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000657
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200658 mutex_unlock(&pcm_mutex);
659}
660
661/*
662 * Called by ALSA when a PCM substream is closed. Private data can be
663 * freed here. The cpu DAI, codec DAI, machine and platform are also
664 * shutdown.
665 */
666static int soc_codec_close(struct snd_pcm_substream *substream)
667{
668 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000669 struct snd_soc_platform *platform = rtd->platform;
670 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
671 struct snd_soc_dai *codec_dai = rtd->codec_dai;
672 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200673
674 mutex_lock(&pcm_mutex);
675
Jassi Brar14dc5732010-02-26 09:12:32 +0900676 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000677 cpu_dai->playback_active--;
678 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900679 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000680 cpu_dai->capture_active--;
681 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200682 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900683
684 cpu_dai->active--;
685 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200686 codec->active--;
687
Mark Brown6010b2d2008-09-06 18:33:24 +0100688 /* Muting the DAC suppresses artifacts caused during digital
689 * shutdown, for example from stopping clocks.
690 */
691 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
692 snd_soc_dai_digital_mute(codec_dai, 1);
693
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000694 if (cpu_dai->driver->ops->shutdown)
695 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200696
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000697 if (codec_dai->driver->ops->shutdown)
698 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
701 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200702
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000703 if (platform->driver->ops->close)
704 platform->driver->ops->close(substream);
705 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200706
707 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
708 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100709 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000710 schedule_delayed_work(&rtd->delayed_work,
711 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200712 } else {
713 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714 snd_soc_dapm_stream_event(rtd,
715 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100716 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200717 }
718
719 mutex_unlock(&pcm_mutex);
720 return 0;
721}
722
723/*
724 * Called by ALSA when the PCM substream is prepared, can set format, sample
725 * rate, etc. This function is non atomic and can be called multiple times,
726 * it can refer to the runtime info.
727 */
728static int soc_pcm_prepare(struct snd_pcm_substream *substream)
729{
730 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000731 struct snd_soc_platform *platform = rtd->platform;
732 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
733 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200734 int ret = 0;
735
736 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100737
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000738 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
739 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100740 if (ret < 0) {
741 printk(KERN_ERR "asoc: machine prepare error\n");
742 goto out;
743 }
744 }
745
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000746 if (platform->driver->ops->prepare) {
747 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200748 if (ret < 0) {
749 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200751 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200752 }
753
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754 if (codec_dai->driver->ops->prepare) {
755 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200756 if (ret < 0) {
757 printk(KERN_ERR "asoc: codec DAI 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 (cpu_dai->driver->ops->prepare) {
763 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100764 if (ret < 0) {
765 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
766 goto out;
767 }
768 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200769
Mark Brownd45f6212008-10-14 13:58:36 +0100770 /* cancel any delayed stream shutdown that is pending */
771 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
772 codec_dai->pop_wait) {
773 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000774 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100775 }
776
Mark Brown452c5ea2009-05-17 21:41:23 +0100777 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778 snd_soc_dapm_stream_event(rtd,
779 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100780 SND_SOC_DAPM_STREAM_START);
781 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782 snd_soc_dapm_stream_event(rtd,
783 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100784 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100785
Mark Brown452c5ea2009-05-17 21:41:23 +0100786 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200787
788out:
789 mutex_unlock(&pcm_mutex);
790 return ret;
791}
792
793/*
794 * Called by ALSA when the hardware params are set by application. This
795 * function can also be called multiple times and can allocate buffers
796 * (using snd_pcm_lib_* ). It's non-atomic.
797 */
798static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
799 struct snd_pcm_hw_params *params)
800{
801 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000802 struct snd_soc_platform *platform = rtd->platform;
803 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
804 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200805 int ret = 0;
806
807 mutex_lock(&pcm_mutex);
808
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000809 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
810 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200811 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100812 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200813 goto out;
814 }
815 }
816
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000817 if (codec_dai->driver->ops->hw_params) {
818 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100819 if (ret < 0) {
820 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
821 codec_dai->name);
822 goto codec_err;
823 }
824 }
825
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 if (cpu_dai->driver->ops->hw_params) {
827 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200828 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200829 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100830 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200831 goto interface_err;
832 }
833 }
834
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000835 if (platform->driver->ops->hw_params) {
836 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200837 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200838 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200839 platform->name);
840 goto platform_err;
841 }
842 }
843
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000844 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100845
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200846out:
847 mutex_unlock(&pcm_mutex);
848 return ret;
849
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200850platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000851 if (cpu_dai->driver->ops->hw_free)
852 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200853
854interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000855 if (codec_dai->driver->ops->hw_free)
856 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100857
858codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000859 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
860 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200861
862 mutex_unlock(&pcm_mutex);
863 return ret;
864}
865
866/*
Mark Browna00f90f2010-12-02 16:24:24 +0000867 * Frees resources allocated by hw_params, can be called multiple times
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200868 */
869static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
870{
871 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000872 struct snd_soc_platform *platform = rtd->platform;
873 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
874 struct snd_soc_dai *codec_dai = rtd->codec_dai;
875 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200876
877 mutex_lock(&pcm_mutex);
878
879 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100880 if (!codec->active)
881 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200882
883 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000884 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
885 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200886
887 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000888 if (platform->driver->ops->hw_free)
889 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200890
Mark Browna00f90f2010-12-02 16:24:24 +0000891 /* now free hw params for the DAIs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000892 if (codec_dai->driver->ops->hw_free)
893 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200894
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000895 if (cpu_dai->driver->ops->hw_free)
896 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200897
898 mutex_unlock(&pcm_mutex);
899 return 0;
900}
901
902static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
903{
904 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000905 struct snd_soc_platform *platform = rtd->platform;
906 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
907 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200908 int ret;
909
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000910 if (codec_dai->driver->ops->trigger) {
911 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200912 if (ret < 0)
913 return ret;
914 }
915
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000916 if (platform->driver->ops->trigger) {
917 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200918 if (ret < 0)
919 return ret;
920 }
921
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000922 if (cpu_dai->driver->ops->trigger) {
923 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200924 if (ret < 0)
925 return ret;
926 }
927 return 0;
928}
929
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200930/*
931 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200932 * If cpu_dai, codec_dai, platform driver has the delay callback, than
933 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200934 */
935static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
936{
937 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000938 struct snd_soc_platform *platform = rtd->platform;
939 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
940 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200941 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200942 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200943 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200944
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000945 if (platform->driver->ops->pointer)
946 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200947
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000948 if (cpu_dai->driver->ops->delay)
949 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200950
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000951 if (codec_dai->driver->ops->delay)
952 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200953
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000954 if (platform->driver->delay)
955 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200956
957 runtime->delay = delay;
958
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200959 return offset;
960}
961
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200962/* ASoC PCM operations */
963static struct snd_pcm_ops soc_pcm_ops = {
964 .open = soc_pcm_open,
965 .close = soc_codec_close,
966 .hw_params = soc_pcm_hw_params,
967 .hw_free = soc_pcm_hw_free,
968 .prepare = soc_pcm_prepare,
969 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200970 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200971};
972
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000973#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200974/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000975int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200976{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000977 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200978 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200979 int i;
980
Daniel Macke3509ff2009-06-03 17:44:49 +0200981 /* If the initialization of this soc device failed, there is no codec
982 * associated with it. Just bail out in this case.
983 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000984 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200985 return 0;
986
Andy Green6ed25972008-06-13 16:24:05 +0100987 /* Due to the resume being scheduled into a workqueue we could
988 * suspend before that's finished - wait for it to complete.
989 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000990 snd_power_lock(card->snd_card);
991 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
992 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100993
994 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000995 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100996
Mark Browna00f90f2010-12-02 16:24:24 +0000997 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000998 for (i = 0; i < card->num_rtd; i++) {
999 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1000 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001001
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001003 continue;
1004
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001005 if (drv->ops->digital_mute && dai->playback_active)
1006 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001007 }
1008
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001009 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001010 for (i = 0; i < card->num_rtd; i++) {
1011 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001012 continue;
1013
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001014 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +01001015 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001016
Mark Brown87506542008-11-18 20:50:34 +00001017 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +00001018 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001019
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001020 for (i = 0; i < card->num_rtd; i++) {
1021 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1022 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001023
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001024 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001025 continue;
1026
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001027 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1028 cpu_dai->driver->suspend(cpu_dai);
1029 if (platform->driver->suspend && !platform->suspended) {
1030 platform->driver->suspend(cpu_dai);
1031 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +01001032 }
1033 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001034
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001035 /* close any waiting streams and save state */
1036 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +01001037 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001038 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001039 }
Mark Brown3efab7d2010-05-09 13:25:43 +01001040
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001041 for (i = 0; i < card->num_rtd; i++) {
1042 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1043
1044 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001045 continue;
1046
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001047 if (driver->playback.stream_name != NULL)
1048 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1049 SND_SOC_DAPM_STREAM_SUSPEND);
1050
1051 if (driver->capture.stream_name != NULL)
1052 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1053 SND_SOC_DAPM_STREAM_SUSPEND);
1054 }
1055
1056 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001057 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001058 /* If there are paths active then the CODEC will be held with
1059 * bias _ON and should not be suspended. */
1060 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001061 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001062 case SND_SOC_BIAS_STANDBY:
1063 case SND_SOC_BIAS_OFF:
1064 codec->driver->suspend(codec, PMSG_SUSPEND);
1065 codec->suspended = 1;
1066 break;
1067 default:
1068 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1069 break;
1070 }
1071 }
1072 }
1073
1074 for (i = 0; i < card->num_rtd; i++) {
1075 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1076
1077 if (card->rtd[i].dai_link->ignore_suspend)
1078 continue;
1079
1080 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1081 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001082 }
1083
Mark Brown87506542008-11-18 20:50:34 +00001084 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +00001085 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001086
1087 return 0;
1088}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001089EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001090
Andy Green6ed25972008-06-13 16:24:05 +01001091/* deferred resume work, so resume can complete before we finished
1092 * setting our codec back up, which can be very slow on I2C
1093 */
1094static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001095{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001096 struct snd_soc_card *card =
1097 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001098 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001099 int i;
1100
Andy Green6ed25972008-06-13 16:24:05 +01001101 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1102 * so userspace apps are blocked from touching us
1103 */
1104
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001105 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001106
Mark Brown99497882010-05-07 20:24:05 +01001107 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001108 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +01001109
Mark Brown87506542008-11-18 20:50:34 +00001110 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +00001111 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001112
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001113 /* resume AC97 DAIs */
1114 for (i = 0; i < card->num_rtd; i++) {
1115 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001116
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001117 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001118 continue;
1119
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001120 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1121 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001122 }
1123
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001124 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001125 /* If the CODEC was idle over suspend then it will have been
1126 * left with bias OFF or STANDBY and suspended so we must now
1127 * resume. Otherwise the suspend was suppressed.
1128 */
1129 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001130 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001131 case SND_SOC_BIAS_STANDBY:
1132 case SND_SOC_BIAS_OFF:
1133 codec->driver->resume(codec);
1134 codec->suspended = 0;
1135 break;
1136 default:
1137 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1138 break;
1139 }
Mark Brown1547aba2010-05-07 21:11:40 +01001140 }
1141 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001142
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001143 for (i = 0; i < card->num_rtd; i++) {
1144 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001145
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001146 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001147 continue;
1148
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001149 if (driver->playback.stream_name != NULL)
1150 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001151 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001152
1153 if (driver->capture.stream_name != NULL)
1154 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001155 SND_SOC_DAPM_STREAM_RESUME);
1156 }
1157
Mark Brown3ff3f642008-05-19 12:32:25 +02001158 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001159 for (i = 0; i < card->num_rtd; i++) {
1160 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1161 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001162
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001163 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001164 continue;
1165
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001166 if (drv->ops->digital_mute && dai->playback_active)
1167 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001168 }
1169
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001170 for (i = 0; i < card->num_rtd; i++) {
1171 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1172 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001173
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001174 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001175 continue;
1176
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001177 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1178 cpu_dai->driver->resume(cpu_dai);
1179 if (platform->driver->resume && platform->suspended) {
1180 platform->driver->resume(cpu_dai);
1181 platform->suspended = 0;
1182 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001183 }
1184
Mark Brown87506542008-11-18 20:50:34 +00001185 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +00001186 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001187
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001188 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001189
1190 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001191 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001192}
1193
1194/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001195int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001196{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001197 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001198 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001199
Mark Brown64ab9ba2009-03-31 11:27:03 +01001200 /* AC97 devices might have other drivers hanging off them so
1201 * need to resume immediately. Other drivers don't have that
1202 * problem and may take a substantial amount of time to resume
1203 * due to I/O costs and anti-pop so handle them out of line.
1204 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001205 for (i = 0; i < card->num_rtd; i++) {
1206 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1207 if (cpu_dai->driver->ac97_control) {
1208 dev_dbg(dev, "Resuming AC97 immediately\n");
1209 soc_resume_deferred(&card->deferred_resume_work);
1210 } else {
1211 dev_dbg(dev, "Scheduling resume work\n");
1212 if (!schedule_work(&card->deferred_resume_work))
1213 dev_err(dev, "resume work item may be lost\n");
1214 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001215 }
Andy Green6ed25972008-06-13 16:24:05 +01001216
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001217 return 0;
1218}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001219EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001220#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001221#define snd_soc_suspend NULL
1222#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001223#endif
1224
Barry Song02a06d32009-10-16 18:13:38 +08001225static struct snd_soc_dai_ops null_dai_ops = {
1226};
1227
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001228static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001229{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001230 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1231 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001232 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001233 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001234 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001235
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001236 if (rtd->complete)
1237 return 1;
1238 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001239
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001240 /* do we already have the CPU DAI for this link ? */
1241 if (rtd->cpu_dai) {
1242 goto find_codec;
1243 }
1244 /* no, then find CPU DAI from registered DAIs*/
1245 list_for_each_entry(cpu_dai, &dai_list, list) {
1246 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1247
1248 if (!try_module_get(cpu_dai->dev->driver->owner))
1249 return -ENODEV;
1250
1251 rtd->cpu_dai = cpu_dai;
1252 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001253 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001254 }
1255 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1256 dai_link->cpu_dai_name);
1257
1258find_codec:
1259 /* do we already have the CODEC for this link ? */
1260 if (rtd->codec) {
1261 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001262 }
1263
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001264 /* no, then find CODEC from registered CODECs*/
1265 list_for_each_entry(codec, &codec_list, list) {
1266 if (!strcmp(codec->name, dai_link->codec_name)) {
1267 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001268
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001269 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1270 list_for_each_entry(codec_dai, &dai_list, list) {
1271 if (codec->dev == codec_dai->dev &&
1272 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1273 rtd->codec_dai = codec_dai;
1274 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001275 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001276 }
1277 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1278 dai_link->codec_dai_name);
1279
1280 goto find_platform;
1281 }
1282 }
1283 dev_dbg(card->dev, "CODEC %s not registered\n",
1284 dai_link->codec_name);
1285
1286find_platform:
1287 /* do we already have the CODEC DAI for this link ? */
1288 if (rtd->platform) {
1289 goto out;
1290 }
1291 /* no, then find CPU DAI from registered DAIs*/
1292 list_for_each_entry(platform, &platform_list, list) {
1293 if (!strcmp(platform->name, dai_link->platform_name)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001294 rtd->platform = platform;
1295 goto out;
1296 }
1297 }
1298
1299 dev_dbg(card->dev, "platform %s not registered\n",
1300 dai_link->platform_name);
1301 return 0;
1302
1303out:
1304 /* mark rtd as complete if we found all 4 of our client devices */
1305 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1306 rtd->complete = 1;
1307 card->num_rtd++;
1308 }
1309 return 1;
1310}
1311
Jarkko Nikula589c3562010-12-06 16:27:07 +02001312static void soc_remove_codec(struct snd_soc_codec *codec)
1313{
1314 int err;
1315
1316 if (codec->driver->remove) {
1317 err = codec->driver->remove(codec);
1318 if (err < 0)
1319 dev_err(codec->dev,
1320 "asoc: failed to remove %s: %d\n",
1321 codec->name, err);
1322 }
1323
1324 /* Make sure all DAPM widgets are freed */
1325 snd_soc_dapm_free(&codec->dapm);
1326
1327 soc_cleanup_codec_debugfs(codec);
1328 codec->probed = 0;
1329 list_del(&codec->card_list);
1330 module_put(codec->dev->driver->owner);
1331}
1332
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001333static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1334{
1335 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1336 struct snd_soc_codec *codec = rtd->codec;
1337 struct snd_soc_platform *platform = rtd->platform;
1338 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1339 int err;
1340
1341 /* unregister the rtd device */
1342 if (rtd->dev_registered) {
1343 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001344 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001345 device_unregister(&rtd->dev);
1346 rtd->dev_registered = 0;
1347 }
1348
1349 /* remove the CODEC DAI */
1350 if (codec_dai && codec_dai->probed) {
1351 if (codec_dai->driver->remove) {
1352 err = codec_dai->driver->remove(codec_dai);
1353 if (err < 0)
1354 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1355 }
1356 codec_dai->probed = 0;
1357 list_del(&codec_dai->card_list);
1358 }
1359
1360 /* remove the platform */
1361 if (platform && platform->probed) {
1362 if (platform->driver->remove) {
1363 err = platform->driver->remove(platform);
1364 if (err < 0)
1365 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1366 }
1367 platform->probed = 0;
1368 list_del(&platform->card_list);
1369 module_put(platform->dev->driver->owner);
1370 }
1371
1372 /* remove the CODEC */
Jarkko Nikula589c3562010-12-06 16:27:07 +02001373 if (codec && codec->probed)
1374 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001375
1376 /* remove the cpu_dai */
1377 if (cpu_dai && cpu_dai->probed) {
1378 if (cpu_dai->driver->remove) {
1379 err = cpu_dai->driver->remove(cpu_dai);
1380 if (err < 0)
1381 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1382 }
1383 cpu_dai->probed = 0;
1384 list_del(&cpu_dai->card_list);
1385 module_put(cpu_dai->dev->driver->owner);
1386 }
1387}
1388
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001389static void soc_set_name_prefix(struct snd_soc_card *card,
1390 struct snd_soc_codec *codec)
1391{
1392 int i;
1393
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001394 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001395 return;
1396
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001397 for (i = 0; i < card->num_configs; i++) {
1398 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001399 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1400 codec->name_prefix = map->name_prefix;
1401 break;
1402 }
1403 }
1404}
1405
Jarkko Nikula589c3562010-12-06 16:27:07 +02001406static int soc_probe_codec(struct snd_soc_card *card,
1407 struct snd_soc_codec *codec)
1408{
1409 int ret = 0;
1410
1411 codec->card = card;
1412 codec->dapm.card = card;
1413 soc_set_name_prefix(card, codec);
1414
Jarkko Nikula70d29332011-01-27 16:24:22 +02001415 if (!try_module_get(codec->dev->driver->owner))
1416 return -ENODEV;
1417
Jarkko Nikula589c3562010-12-06 16:27:07 +02001418 if (codec->driver->probe) {
1419 ret = codec->driver->probe(codec);
1420 if (ret < 0) {
1421 dev_err(codec->dev,
1422 "asoc: failed to probe CODEC %s: %d\n",
1423 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001424 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001425 }
1426 }
1427
1428 soc_init_codec_debugfs(codec);
1429
1430 /* mark codec as probed and add to card codec list */
1431 codec->probed = 1;
1432 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001433 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001434
Jarkko Nikula70d29332011-01-27 16:24:22 +02001435 return 0;
1436
1437err_probe:
1438 module_put(codec->dev->driver->owner);
1439
Jarkko Nikula589c3562010-12-06 16:27:07 +02001440 return ret;
1441}
1442
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001443static void rtd_release(struct device *dev) {}
1444
Jarkko Nikula589c3562010-12-06 16:27:07 +02001445static int soc_post_component_init(struct snd_soc_card *card,
1446 struct snd_soc_codec *codec,
1447 int num, int dailess)
1448{
1449 struct snd_soc_dai_link *dai_link = NULL;
1450 struct snd_soc_aux_dev *aux_dev = NULL;
1451 struct snd_soc_pcm_runtime *rtd;
1452 const char *temp, *name;
1453 int ret = 0;
1454
1455 if (!dailess) {
1456 dai_link = &card->dai_link[num];
1457 rtd = &card->rtd[num];
1458 name = dai_link->name;
1459 } else {
1460 aux_dev = &card->aux_dev[num];
1461 rtd = &card->rtd_aux[num];
1462 name = aux_dev->name;
1463 }
1464
1465 /* machine controls, routes and widgets are not prefixed */
1466 temp = codec->name_prefix;
1467 codec->name_prefix = NULL;
1468
1469 /* do machine specific initialization */
1470 if (!dailess && dai_link->init)
1471 ret = dai_link->init(rtd);
1472 else if (dailess && aux_dev->init)
1473 ret = aux_dev->init(&codec->dapm);
1474 if (ret < 0) {
1475 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1476 return ret;
1477 }
1478 codec->name_prefix = temp;
1479
1480 /* Make sure all DAPM widgets are instantiated */
1481 snd_soc_dapm_new_widgets(&codec->dapm);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001482
1483 /* register the rtd device */
1484 rtd->codec = codec;
1485 rtd->card = card;
1486 rtd->dev.parent = card->dev;
1487 rtd->dev.release = rtd_release;
1488 rtd->dev.init_name = name;
1489 ret = device_register(&rtd->dev);
1490 if (ret < 0) {
1491 dev_err(card->dev,
1492 "asoc: failed to register runtime device: %d\n", ret);
1493 return ret;
1494 }
1495 rtd->dev_registered = 1;
1496
1497 /* add DAPM sysfs entries for this codec */
1498 ret = snd_soc_dapm_sys_add(&rtd->dev);
1499 if (ret < 0)
1500 dev_err(codec->dev,
1501 "asoc: failed to add codec dapm sysfs entries: %d\n",
1502 ret);
1503
1504 /* add codec sysfs entries */
1505 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1506 if (ret < 0)
1507 dev_err(codec->dev,
1508 "asoc: failed to add codec sysfs files: %d\n", ret);
1509
1510 return 0;
1511}
1512
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001513static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1514{
1515 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1516 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1517 struct snd_soc_codec *codec = rtd->codec;
1518 struct snd_soc_platform *platform = rtd->platform;
1519 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1520 int ret;
1521
1522 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1523
1524 /* config components */
1525 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001526 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001527 codec_dai->card = card;
1528 cpu_dai->card = card;
1529
1530 /* set default power off timeout */
1531 rtd->pmdown_time = pmdown_time;
1532
1533 /* probe the cpu_dai */
1534 if (!cpu_dai->probed) {
1535 if (cpu_dai->driver->probe) {
1536 ret = cpu_dai->driver->probe(cpu_dai);
1537 if (ret < 0) {
1538 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1539 cpu_dai->name);
1540 return ret;
1541 }
1542 }
1543 cpu_dai->probed = 1;
1544 /* mark cpu_dai as probed and add to card cpu_dai list */
1545 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1546 }
1547
1548 /* probe the CODEC */
1549 if (!codec->probed) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001550 ret = soc_probe_codec(card, codec);
1551 if (ret < 0)
1552 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001553 }
1554
1555 /* probe the platform */
1556 if (!platform->probed) {
Jarkko Nikula70d29332011-01-27 16:24:22 +02001557 if (!try_module_get(platform->dev->driver->owner))
1558 return -ENODEV;
1559
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001560 if (platform->driver->probe) {
1561 ret = platform->driver->probe(platform);
1562 if (ret < 0) {
1563 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1564 platform->name);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001565 module_put(platform->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001566 return ret;
1567 }
1568 }
1569 /* mark platform as probed and add to card platform list */
1570 platform->probed = 1;
1571 list_add(&platform->card_list, &card->platform_dev_list);
1572 }
1573
1574 /* probe the CODEC DAI */
1575 if (!codec_dai->probed) {
1576 if (codec_dai->driver->probe) {
1577 ret = codec_dai->driver->probe(codec_dai);
1578 if (ret < 0) {
1579 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1580 codec_dai->name);
1581 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001582 }
1583 }
1584
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001585 /* mark cpu_dai as probed and add to card cpu_dai list */
1586 codec_dai->probed = 1;
1587 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001588 }
1589
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001590 /* DAPM dai link stream work */
1591 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1592
Jarkko Nikula589c3562010-12-06 16:27:07 +02001593 ret = soc_post_component_init(card, codec, num, 0);
1594 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001595 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001596
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001597 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1598 if (ret < 0)
1599 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1600
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001601 /* create the pcm */
1602 ret = soc_new_pcm(rtd, num);
1603 if (ret < 0) {
1604 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1605 return ret;
1606 }
1607
1608 /* add platform data for AC97 devices */
1609 if (rtd->codec_dai->driver->ac97_control)
1610 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1611
1612 return 0;
1613}
1614
1615#ifdef CONFIG_SND_SOC_AC97_BUS
1616static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1617{
1618 int ret;
1619
1620 /* Only instantiate AC97 if not already done by the adaptor
1621 * for the generic AC97 subsystem.
1622 */
1623 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001624 /*
1625 * It is possible that the AC97 device is already registered to
1626 * the device subsystem. This happens when the device is created
1627 * via snd_ac97_mixer(). Currently only SoC codec that does so
1628 * is the generic AC97 glue but others migh emerge.
1629 *
1630 * In those cases we don't try to register the device again.
1631 */
1632 if (!rtd->codec->ac97_created)
1633 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001634
1635 ret = soc_ac97_dev_register(rtd->codec);
1636 if (ret < 0) {
1637 printk(KERN_ERR "asoc: AC97 device register failed\n");
1638 return ret;
1639 }
1640
1641 rtd->codec->ac97_registered = 1;
1642 }
1643 return 0;
1644}
1645
1646static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1647{
1648 if (codec->ac97_registered) {
1649 soc_ac97_dev_unregister(codec);
1650 codec->ac97_registered = 0;
1651 }
1652}
1653#endif
1654
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001655static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1656{
1657 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001658 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001659 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001660
1661 /* find CODEC from registered CODECs*/
1662 list_for_each_entry(codec, &codec_list, list) {
1663 if (!strcmp(codec->name, aux_dev->codec_name)) {
1664 if (codec->probed) {
1665 dev_err(codec->dev,
1666 "asoc: codec already probed");
1667 ret = -EBUSY;
1668 goto out;
1669 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001670 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001671 }
1672 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001673 /* codec not found */
1674 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1675 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001676
Jarkko Nikula676ad982010-12-03 09:18:22 +02001677found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001678 ret = soc_probe_codec(card, codec);
1679 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001680 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001681
Jarkko Nikula589c3562010-12-06 16:27:07 +02001682 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001683
1684out:
1685 return ret;
1686}
1687
1688static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1689{
1690 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1691 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001692
1693 /* unregister the rtd device */
1694 if (rtd->dev_registered) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001695 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001696 device_unregister(&rtd->dev);
1697 rtd->dev_registered = 0;
1698 }
1699
Jarkko Nikula589c3562010-12-06 16:27:07 +02001700 if (codec && codec->probed)
1701 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001702}
1703
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001704static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1705 enum snd_soc_compress_type compress_type)
1706{
1707 int ret;
1708
1709 if (codec->cache_init)
1710 return 0;
1711
1712 /* override the compress_type if necessary */
1713 if (compress_type && codec->compress_type != compress_type)
1714 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001715 ret = snd_soc_cache_init(codec);
1716 if (ret < 0) {
1717 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1718 ret);
1719 return ret;
1720 }
1721 codec->cache_init = 1;
1722 return 0;
1723}
1724
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001725static void snd_soc_instantiate_card(struct snd_soc_card *card)
1726{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001727 struct snd_soc_codec *codec;
1728 struct snd_soc_codec_conf *codec_conf;
1729 enum snd_soc_compress_type compress_type;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001730 int ret, i;
1731
1732 mutex_lock(&card->mutex);
1733
1734 if (card->instantiated) {
1735 mutex_unlock(&card->mutex);
1736 return;
1737 }
1738
1739 /* bind DAIs */
1740 for (i = 0; i < card->num_links; i++)
1741 soc_bind_dai_link(card, i);
1742
1743 /* bind completed ? */
1744 if (card->num_rtd != card->num_links) {
1745 mutex_unlock(&card->mutex);
1746 return;
1747 }
1748
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001749 /* initialize the register cache for each available codec */
1750 list_for_each_entry(codec, &codec_list, list) {
1751 if (codec->cache_init)
1752 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001753 /* by default we don't override the compress_type */
1754 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001755 /* check to see if we need to override the compress_type */
1756 for (i = 0; i < card->num_configs; ++i) {
1757 codec_conf = &card->codec_conf[i];
1758 if (!strcmp(codec->name, codec_conf->dev_name)) {
1759 compress_type = codec_conf->compress_type;
1760 if (compress_type && compress_type
1761 != codec->compress_type)
1762 break;
1763 }
1764 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001765 ret = snd_soc_init_codec_cache(codec, compress_type);
1766 if (ret < 0) {
1767 mutex_unlock(&card->mutex);
1768 return;
1769 }
1770 }
1771
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001772 /* card bind complete so register a sound card */
1773 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1774 card->owner, 0, &card->snd_card);
1775 if (ret < 0) {
1776 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1777 card->name);
1778 mutex_unlock(&card->mutex);
1779 return;
1780 }
1781 card->snd_card->dev = card->dev;
1782
Randy Dunlap1301a962008-06-17 19:19:34 +01001783#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001784 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001785 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001786#endif
Andy Green6ed25972008-06-13 16:24:05 +01001787
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001788 /* initialise the sound card only once */
1789 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001790 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001791 if (ret < 0)
1792 goto card_probe_error;
1793 }
1794
Mark Brownfe3e78e2009-11-03 22:13:13 +00001795 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001796 ret = soc_probe_dai_link(card, i);
1797 if (ret < 0) {
Mark Brown321de0d2010-09-21 15:09:37 +01001798 pr_err("asoc: failed to instantiate card %s: %d\n",
1799 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001800 goto probe_dai_err;
1801 }
1802 }
1803
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001804 for (i = 0; i < card->num_aux_devs; i++) {
1805 ret = soc_probe_aux_dev(card, i);
1806 if (ret < 0) {
1807 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1808 card->name, ret);
1809 goto probe_aux_dev_err;
1810 }
1811 }
1812
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001813 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1814 "%s", card->name);
1815 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1816 "%s", card->name);
1817
1818 ret = snd_card_register(card->snd_card);
1819 if (ret < 0) {
1820 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
Axel Lin6b3ed782010-12-07 16:12:29 +08001821 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001822 }
1823
1824#ifdef CONFIG_SND_SOC_AC97_BUS
1825 /* register any AC97 codecs */
1826 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001827 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1828 if (ret < 0) {
1829 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1830 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001831 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001832 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001833 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001834 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001835#endif
1836
Mark Brown435c5e22008-12-04 15:32:53 +00001837 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001838 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001839 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001840
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001841probe_aux_dev_err:
1842 for (i = 0; i < card->num_aux_devs; i++)
1843 soc_remove_aux_dev(card, i);
1844
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001845probe_dai_err:
1846 for (i = 0; i < card->num_links; i++)
1847 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001848
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001849card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001850 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001851 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001852
1853 snd_card_free(card->snd_card);
1854
1855 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001856}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001857
Mark Brown435c5e22008-12-04 15:32:53 +00001858/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001859 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001860 * client_mutex.
1861 */
1862static void snd_soc_instantiate_cards(void)
1863{
1864 struct snd_soc_card *card;
1865 list_for_each_entry(card, &card_list, list)
1866 snd_soc_instantiate_card(card);
1867}
1868
1869/* probes a new socdev */
1870static int soc_probe(struct platform_device *pdev)
1871{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001872 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001873 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001874
Vinod Koul70a7ca32011-01-14 19:22:48 +05301875 /*
1876 * no card, so machine driver should be registering card
1877 * we should not be here in that case so ret error
1878 */
1879 if (!card)
1880 return -EINVAL;
1881
Mark Brown435c5e22008-12-04 15:32:53 +00001882 /* Bodge while we unpick instantiation */
1883 card->dev = &pdev->dev;
Vinod Koul4e10bda2011-01-13 22:48:52 +05301884 snd_soc_initialize_card_lists(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001885
Mark Brown435c5e22008-12-04 15:32:53 +00001886 ret = snd_soc_register_card(card);
1887 if (ret != 0) {
1888 dev_err(&pdev->dev, "Failed to register card\n");
1889 return ret;
1890 }
1891
1892 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001893}
1894
Vinod Koulb0e26482011-01-13 22:48:02 +05301895static int soc_cleanup_card_resources(struct snd_soc_card *card)
1896{
Vinod Koulb0e26482011-01-13 22:48:02 +05301897 int i;
1898
1899 /* make sure any delayed work runs */
1900 for (i = 0; i < card->num_rtd; i++) {
1901 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1902 flush_delayed_work_sync(&rtd->delayed_work);
1903 }
1904
1905 /* remove auxiliary devices */
1906 for (i = 0; i < card->num_aux_devs; i++)
1907 soc_remove_aux_dev(card, i);
1908
1909 /* remove and free each DAI */
1910 for (i = 0; i < card->num_rtd; i++)
1911 soc_remove_dai_link(card, i);
1912
1913 soc_cleanup_card_debugfs(card);
1914
1915 /* remove the card */
1916 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001917 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301918
1919 kfree(card->rtd);
1920 snd_card_free(card->snd_card);
1921 return 0;
1922
1923}
1924
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001925/* removes a socdev */
1926static int soc_remove(struct platform_device *pdev)
1927{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001928 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001929
Mark Brownc5af3a22008-11-28 13:29:45 +00001930 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001931 return 0;
1932}
1933
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001934int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001935{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001936 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001937 int i;
Mark Brown51737472009-06-22 13:16:51 +01001938
1939 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001940 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001941
1942 /* Flush out pmdown_time work - we actually do want to run it
1943 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001944 for (i = 0; i < card->num_rtd; i++) {
1945 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001946 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001947 }
Mark Brown51737472009-06-22 13:16:51 +01001948
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001949 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001950
1951 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001952}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001953EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001954
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001955const struct dev_pm_ops snd_soc_pm_ops = {
1956 .suspend = snd_soc_suspend,
1957 .resume = snd_soc_resume,
1958 .poweroff = snd_soc_poweroff,
Mark Brown416356f2009-06-30 19:05:15 +01001959};
1960
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001961/* ASoC platform driver */
1962static struct platform_driver soc_driver = {
1963 .driver = {
1964 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001965 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001966 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001967 },
1968 .probe = soc_probe,
1969 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001970};
1971
1972/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001973static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001974{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001975 struct snd_soc_codec *codec = rtd->codec;
1976 struct snd_soc_platform *platform = rtd->platform;
1977 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1978 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001979 struct snd_pcm *pcm;
1980 char new_name[64];
1981 int ret = 0, playback = 0, capture = 0;
1982
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001983 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001984 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001985 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001986
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001987 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001988 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001989 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001990 capture = 1;
1991
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001992 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1993 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1994 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001995 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001996 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001997 return ret;
1998 }
1999
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002000 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002001 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002002 soc_pcm_ops.mmap = platform->driver->ops->mmap;
2003 soc_pcm_ops.pointer = platform->driver->ops->pointer;
2004 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
2005 soc_pcm_ops.copy = platform->driver->ops->copy;
2006 soc_pcm_ops.silence = platform->driver->ops->silence;
2007 soc_pcm_ops.ack = platform->driver->ops->ack;
2008 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002009
2010 if (playback)
2011 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
2012
2013 if (capture)
2014 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
2015
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002016 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002017 if (ret < 0) {
2018 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002019 return ret;
2020 }
2021
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002022 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002023 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2024 cpu_dai->name);
2025 return ret;
2026}
2027
Mark Brown096e49d2009-07-05 15:12:22 +01002028/**
2029 * snd_soc_codec_volatile_register: Report if a register is volatile.
2030 *
2031 * @codec: CODEC to query.
2032 * @reg: Register to query.
2033 *
2034 * Boolean function indiciating if a CODEC register is volatile.
2035 */
Mark Brown181e0552011-01-24 14:05:25 +00002036int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
2037 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01002038{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00002039 if (codec->volatile_register)
2040 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01002041 else
2042 return 0;
2043}
2044EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2045
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002046/**
2047 * snd_soc_new_ac97_codec - initailise AC97 device
2048 * @codec: audio codec
2049 * @ops: AC97 bus operations
2050 * @num: AC97 codec number
2051 *
2052 * Initialises AC97 codec resources for use by ad-hoc devices only.
2053 */
2054int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2055 struct snd_ac97_bus_ops *ops, int num)
2056{
2057 mutex_lock(&codec->mutex);
2058
2059 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2060 if (codec->ac97 == NULL) {
2061 mutex_unlock(&codec->mutex);
2062 return -ENOMEM;
2063 }
2064
2065 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2066 if (codec->ac97->bus == NULL) {
2067 kfree(codec->ac97);
2068 codec->ac97 = NULL;
2069 mutex_unlock(&codec->mutex);
2070 return -ENOMEM;
2071 }
2072
2073 codec->ac97->bus->ops = ops;
2074 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002075
2076 /*
2077 * Mark the AC97 device to be created by us. This way we ensure that the
2078 * device will be registered with the device subsystem later on.
2079 */
2080 codec->ac97_created = 1;
2081
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002082 mutex_unlock(&codec->mutex);
2083 return 0;
2084}
2085EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2086
2087/**
2088 * snd_soc_free_ac97_codec - free AC97 codec device
2089 * @codec: audio codec
2090 *
2091 * Frees AC97 codec device resources.
2092 */
2093void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2094{
2095 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002096#ifdef CONFIG_SND_SOC_AC97_BUS
2097 soc_unregister_ac97_dai_link(codec);
2098#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002099 kfree(codec->ac97->bus);
2100 kfree(codec->ac97);
2101 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002102 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002103 mutex_unlock(&codec->mutex);
2104}
2105EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2106
Mark Brownc3753702010-11-01 15:41:57 -04002107unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2108{
2109 unsigned int ret;
2110
Mark Brownc3acec22010-12-02 16:15:29 +00002111 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002112 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002113 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002114
2115 return ret;
2116}
2117EXPORT_SYMBOL_GPL(snd_soc_read);
2118
2119unsigned int snd_soc_write(struct snd_soc_codec *codec,
2120 unsigned int reg, unsigned int val)
2121{
2122 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002123 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002124 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002125}
2126EXPORT_SYMBOL_GPL(snd_soc_write);
2127
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002128/**
2129 * snd_soc_update_bits - update codec register bits
2130 * @codec: audio codec
2131 * @reg: codec register
2132 * @mask: register mask
2133 * @value: new value
2134 *
2135 * Writes new register value.
2136 *
Timur Tabi180c3292011-01-10 15:58:13 -06002137 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002138 */
2139int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002140 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002141{
2142 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002143 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002144 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002145
Timur Tabi180c3292011-01-10 15:58:13 -06002146 ret = snd_soc_read(codec, reg);
2147 if (ret < 0)
2148 return ret;
2149
2150 old = ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002151 new = (old & ~mask) | value;
2152 change = old != new;
Timur Tabi180c3292011-01-10 15:58:13 -06002153 if (change) {
2154 ret = snd_soc_write(codec, reg, new);
2155 if (ret < 0)
2156 return ret;
2157 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002158
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002159 return change;
2160}
2161EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2162
2163/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002164 * snd_soc_update_bits_locked - update codec register bits
2165 * @codec: audio codec
2166 * @reg: codec register
2167 * @mask: register mask
2168 * @value: new value
2169 *
2170 * Writes new register value, and takes the codec mutex.
2171 *
2172 * Returns 1 for change else 0.
2173 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002174int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2175 unsigned short reg, unsigned int mask,
2176 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002177{
2178 int change;
2179
2180 mutex_lock(&codec->mutex);
2181 change = snd_soc_update_bits(codec, reg, mask, value);
2182 mutex_unlock(&codec->mutex);
2183
2184 return change;
2185}
Mark Browndd1b3d52009-12-04 14:22:03 +00002186EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002187
2188/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002189 * snd_soc_test_bits - test register for change
2190 * @codec: audio codec
2191 * @reg: codec register
2192 * @mask: register mask
2193 * @value: new value
2194 *
2195 * Tests a register with a new value and checks if the new value is
2196 * different from the old value.
2197 *
2198 * Returns 1 for change else 0.
2199 */
2200int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002201 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002202{
2203 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002204 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002205
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002206 old = snd_soc_read(codec, reg);
2207 new = (old & ~mask) | value;
2208 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002209
2210 return change;
2211}
2212EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2213
2214/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002215 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2216 * @substream: the pcm substream
2217 * @hw: the hardware parameters
2218 *
2219 * Sets the substream runtime hardware parameters.
2220 */
2221int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2222 const struct snd_pcm_hardware *hw)
2223{
2224 struct snd_pcm_runtime *runtime = substream->runtime;
2225 runtime->hw.info = hw->info;
2226 runtime->hw.formats = hw->formats;
2227 runtime->hw.period_bytes_min = hw->period_bytes_min;
2228 runtime->hw.period_bytes_max = hw->period_bytes_max;
2229 runtime->hw.periods_min = hw->periods_min;
2230 runtime->hw.periods_max = hw->periods_max;
2231 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2232 runtime->hw.fifo_size = hw->fifo_size;
2233 return 0;
2234}
2235EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2236
2237/**
2238 * snd_soc_cnew - create new control
2239 * @_template: control template
2240 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002241 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002242 *
2243 * Create a new mixer control from a template control.
2244 *
2245 * Returns 0 for success, else error.
2246 */
2247struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2248 void *data, char *long_name)
2249{
2250 struct snd_kcontrol_new template;
2251
2252 memcpy(&template, _template, sizeof(template));
2253 if (long_name)
2254 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002255 template.index = 0;
2256
2257 return snd_ctl_new1(&template, data);
2258}
2259EXPORT_SYMBOL_GPL(snd_soc_cnew);
2260
2261/**
Ian Molton3e8e1952009-01-09 00:23:21 +00002262 * snd_soc_add_controls - add an array of controls to a codec.
2263 * Convienience function to add a list of controls. Many codecs were
2264 * duplicating this code.
2265 *
2266 * @codec: codec to add controls to
2267 * @controls: array of controls to add
2268 * @num_controls: number of elements in the array
2269 *
2270 * Return 0 for success, else error.
2271 */
2272int snd_soc_add_controls(struct snd_soc_codec *codec,
2273 const struct snd_kcontrol_new *controls, int num_controls)
2274{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002275 struct snd_card *card = codec->card->snd_card;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002276 char prefixed_name[44], *name;
Ian Molton3e8e1952009-01-09 00:23:21 +00002277 int err, i;
2278
2279 for (i = 0; i < num_controls; i++) {
2280 const struct snd_kcontrol_new *control = &controls[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002281 if (codec->name_prefix) {
2282 snprintf(prefixed_name, sizeof(prefixed_name), "%s %s",
2283 codec->name_prefix, control->name);
2284 name = prefixed_name;
2285 } else {
2286 name = control->name;
2287 }
2288 err = snd_ctl_add(card, snd_soc_cnew(control, codec, name));
Ian Molton3e8e1952009-01-09 00:23:21 +00002289 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002290 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002291 codec->name, name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002292 return err;
2293 }
2294 }
2295
2296 return 0;
2297}
2298EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2299
2300/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002301 * snd_soc_info_enum_double - enumerated double mixer info callback
2302 * @kcontrol: mixer control
2303 * @uinfo: control element information
2304 *
2305 * Callback to provide information about a double enumerated
2306 * mixer control.
2307 *
2308 * Returns 0 for success.
2309 */
2310int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2311 struct snd_ctl_elem_info *uinfo)
2312{
2313 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2314
2315 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2316 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002317 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002318
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002319 if (uinfo->value.enumerated.item > e->max - 1)
2320 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002321 strcpy(uinfo->value.enumerated.name,
2322 e->texts[uinfo->value.enumerated.item]);
2323 return 0;
2324}
2325EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2326
2327/**
2328 * snd_soc_get_enum_double - enumerated double mixer get callback
2329 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002330 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002331 *
2332 * Callback to get the value of a double enumerated mixer.
2333 *
2334 * Returns 0 for success.
2335 */
2336int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2337 struct snd_ctl_elem_value *ucontrol)
2338{
2339 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2340 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002341 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002342
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002343 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002344 ;
2345 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002346 ucontrol->value.enumerated.item[0]
2347 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002348 if (e->shift_l != e->shift_r)
2349 ucontrol->value.enumerated.item[1] =
2350 (val >> e->shift_r) & (bitmask - 1);
2351
2352 return 0;
2353}
2354EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2355
2356/**
2357 * snd_soc_put_enum_double - enumerated double mixer put callback
2358 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002359 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002360 *
2361 * Callback to set the value of a double enumerated mixer.
2362 *
2363 * Returns 0 for success.
2364 */
2365int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2366 struct snd_ctl_elem_value *ucontrol)
2367{
2368 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2369 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002370 unsigned int val;
2371 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002372
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002373 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002374 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002375 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002376 return -EINVAL;
2377 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2378 mask = (bitmask - 1) << e->shift_l;
2379 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002380 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002381 return -EINVAL;
2382 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2383 mask |= (bitmask - 1) << e->shift_r;
2384 }
2385
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002386 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002387}
2388EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2389
2390/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002391 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2392 * @kcontrol: mixer control
2393 * @ucontrol: control element information
2394 *
2395 * Callback to get the value of a double semi enumerated mixer.
2396 *
2397 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2398 * used for handling bitfield coded enumeration for example.
2399 *
2400 * Returns 0 for success.
2401 */
2402int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2403 struct snd_ctl_elem_value *ucontrol)
2404{
2405 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002406 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002407 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002408
2409 reg_val = snd_soc_read(codec, e->reg);
2410 val = (reg_val >> e->shift_l) & e->mask;
2411 for (mux = 0; mux < e->max; mux++) {
2412 if (val == e->values[mux])
2413 break;
2414 }
2415 ucontrol->value.enumerated.item[0] = mux;
2416 if (e->shift_l != e->shift_r) {
2417 val = (reg_val >> e->shift_r) & e->mask;
2418 for (mux = 0; mux < e->max; mux++) {
2419 if (val == e->values[mux])
2420 break;
2421 }
2422 ucontrol->value.enumerated.item[1] = mux;
2423 }
2424
2425 return 0;
2426}
2427EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2428
2429/**
2430 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2431 * @kcontrol: mixer control
2432 * @ucontrol: control element information
2433 *
2434 * Callback to set the value of a double semi enumerated mixer.
2435 *
2436 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2437 * used for handling bitfield coded enumeration for example.
2438 *
2439 * Returns 0 for success.
2440 */
2441int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2442 struct snd_ctl_elem_value *ucontrol)
2443{
2444 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002445 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002446 unsigned int val;
2447 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002448
2449 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2450 return -EINVAL;
2451 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2452 mask = e->mask << e->shift_l;
2453 if (e->shift_l != e->shift_r) {
2454 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2455 return -EINVAL;
2456 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2457 mask |= e->mask << e->shift_r;
2458 }
2459
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002460 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002461}
2462EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2463
2464/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002465 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2466 * @kcontrol: mixer control
2467 * @uinfo: control element information
2468 *
2469 * Callback to provide information about an external enumerated
2470 * single mixer.
2471 *
2472 * Returns 0 for success.
2473 */
2474int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2475 struct snd_ctl_elem_info *uinfo)
2476{
2477 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2478
2479 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2480 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002481 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002482
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002483 if (uinfo->value.enumerated.item > e->max - 1)
2484 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002485 strcpy(uinfo->value.enumerated.name,
2486 e->texts[uinfo->value.enumerated.item]);
2487 return 0;
2488}
2489EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2490
2491/**
2492 * snd_soc_info_volsw_ext - external single mixer info callback
2493 * @kcontrol: mixer control
2494 * @uinfo: control element information
2495 *
2496 * Callback to provide information about a single external mixer control.
2497 *
2498 * Returns 0 for success.
2499 */
2500int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2501 struct snd_ctl_elem_info *uinfo)
2502{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002503 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002504
Mark Brownfd5dfad2009-04-15 21:37:46 +01002505 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002506 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2507 else
2508 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2509
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002510 uinfo->count = 1;
2511 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002512 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002513 return 0;
2514}
2515EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2516
2517/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002518 * snd_soc_info_volsw - single mixer info callback
2519 * @kcontrol: mixer control
2520 * @uinfo: control element information
2521 *
2522 * Callback to provide information about a single mixer control.
2523 *
2524 * Returns 0 for success.
2525 */
2526int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2527 struct snd_ctl_elem_info *uinfo)
2528{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002529 struct soc_mixer_control *mc =
2530 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002531 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002532 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002533 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002534
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002535 if (!mc->platform_max)
2536 mc->platform_max = mc->max;
2537 platform_max = mc->platform_max;
2538
2539 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002540 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2541 else
2542 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2543
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002544 uinfo->count = shift == rshift ? 1 : 2;
2545 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002546 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002547 return 0;
2548}
2549EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2550
2551/**
2552 * snd_soc_get_volsw - single mixer get callback
2553 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002554 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002555 *
2556 * Callback to get the value of a single mixer control.
2557 *
2558 * Returns 0 for success.
2559 */
2560int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2561 struct snd_ctl_elem_value *ucontrol)
2562{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002563 struct soc_mixer_control *mc =
2564 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002565 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002566 unsigned int reg = mc->reg;
2567 unsigned int shift = mc->shift;
2568 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002569 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002570 unsigned int mask = (1 << fls(max)) - 1;
2571 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002572
2573 ucontrol->value.integer.value[0] =
2574 (snd_soc_read(codec, reg) >> shift) & mask;
2575 if (shift != rshift)
2576 ucontrol->value.integer.value[1] =
2577 (snd_soc_read(codec, reg) >> rshift) & mask;
2578 if (invert) {
2579 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002580 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002581 if (shift != rshift)
2582 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002583 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002584 }
2585
2586 return 0;
2587}
2588EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2589
2590/**
2591 * snd_soc_put_volsw - single mixer put callback
2592 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002593 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002594 *
2595 * Callback to set the value of a single mixer control.
2596 *
2597 * Returns 0 for success.
2598 */
2599int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2600 struct snd_ctl_elem_value *ucontrol)
2601{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002602 struct soc_mixer_control *mc =
2603 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002604 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002605 unsigned int reg = mc->reg;
2606 unsigned int shift = mc->shift;
2607 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002608 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002609 unsigned int mask = (1 << fls(max)) - 1;
2610 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002611 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002612
2613 val = (ucontrol->value.integer.value[0] & mask);
2614 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002615 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002616 val_mask = mask << shift;
2617 val = val << shift;
2618 if (shift != rshift) {
2619 val2 = (ucontrol->value.integer.value[1] & mask);
2620 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002621 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002622 val_mask |= mask << rshift;
2623 val |= val2 << rshift;
2624 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002625 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002626}
2627EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2628
2629/**
2630 * snd_soc_info_volsw_2r - double mixer info callback
2631 * @kcontrol: mixer control
2632 * @uinfo: control element information
2633 *
2634 * Callback to provide information about a double mixer control that
2635 * spans 2 codec registers.
2636 *
2637 * Returns 0 for success.
2638 */
2639int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2640 struct snd_ctl_elem_info *uinfo)
2641{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002642 struct soc_mixer_control *mc =
2643 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002644 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002645
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002646 if (!mc->platform_max)
2647 mc->platform_max = mc->max;
2648 platform_max = mc->platform_max;
2649
2650 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002651 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2652 else
2653 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2654
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002655 uinfo->count = 2;
2656 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002657 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002658 return 0;
2659}
2660EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2661
2662/**
2663 * snd_soc_get_volsw_2r - double mixer get callback
2664 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002665 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002666 *
2667 * Callback to get the value of a double mixer control that spans 2 registers.
2668 *
2669 * Returns 0 for success.
2670 */
2671int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2672 struct snd_ctl_elem_value *ucontrol)
2673{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002674 struct soc_mixer_control *mc =
2675 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002676 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002677 unsigned int reg = mc->reg;
2678 unsigned int reg2 = mc->rreg;
2679 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002680 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002681 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002682 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002683
2684 ucontrol->value.integer.value[0] =
2685 (snd_soc_read(codec, reg) >> shift) & mask;
2686 ucontrol->value.integer.value[1] =
2687 (snd_soc_read(codec, reg2) >> shift) & mask;
2688 if (invert) {
2689 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002690 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002691 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002692 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002693 }
2694
2695 return 0;
2696}
2697EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2698
2699/**
2700 * snd_soc_put_volsw_2r - double mixer set callback
2701 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002702 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002703 *
2704 * Callback to set the value of a double mixer control that spans 2 registers.
2705 *
2706 * Returns 0 for success.
2707 */
2708int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2709 struct snd_ctl_elem_value *ucontrol)
2710{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002711 struct soc_mixer_control *mc =
2712 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002713 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002714 unsigned int reg = mc->reg;
2715 unsigned int reg2 = mc->rreg;
2716 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002717 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002718 unsigned int mask = (1 << fls(max)) - 1;
2719 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002720 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002721 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002722
2723 val_mask = mask << shift;
2724 val = (ucontrol->value.integer.value[0] & mask);
2725 val2 = (ucontrol->value.integer.value[1] & mask);
2726
2727 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002728 val = max - val;
2729 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002730 }
2731
2732 val = val << shift;
2733 val2 = val2 << shift;
2734
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002735 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002736 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002737 return err;
2738
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002739 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002740 return err;
2741}
2742EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2743
Mark Browne13ac2e2008-05-28 17:58:05 +01002744/**
2745 * snd_soc_info_volsw_s8 - signed mixer info callback
2746 * @kcontrol: mixer control
2747 * @uinfo: control element information
2748 *
2749 * Callback to provide information about a signed mixer control.
2750 *
2751 * Returns 0 for success.
2752 */
2753int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2754 struct snd_ctl_elem_info *uinfo)
2755{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002756 struct soc_mixer_control *mc =
2757 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002758 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002759 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002760
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002761 if (!mc->platform_max)
2762 mc->platform_max = mc->max;
2763 platform_max = mc->platform_max;
2764
Mark Browne13ac2e2008-05-28 17:58:05 +01002765 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2766 uinfo->count = 2;
2767 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002768 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002769 return 0;
2770}
2771EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2772
2773/**
2774 * snd_soc_get_volsw_s8 - signed mixer get callback
2775 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002776 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002777 *
2778 * Callback to get the value of a signed mixer control.
2779 *
2780 * Returns 0 for success.
2781 */
2782int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2783 struct snd_ctl_elem_value *ucontrol)
2784{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002785 struct soc_mixer_control *mc =
2786 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002787 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002788 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002789 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002790 int val = snd_soc_read(codec, reg);
2791
2792 ucontrol->value.integer.value[0] =
2793 ((signed char)(val & 0xff))-min;
2794 ucontrol->value.integer.value[1] =
2795 ((signed char)((val >> 8) & 0xff))-min;
2796 return 0;
2797}
2798EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2799
2800/**
2801 * snd_soc_put_volsw_sgn - signed mixer put callback
2802 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002803 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002804 *
2805 * Callback to set the value of a signed mixer control.
2806 *
2807 * Returns 0 for success.
2808 */
2809int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2810 struct snd_ctl_elem_value *ucontrol)
2811{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002812 struct soc_mixer_control *mc =
2813 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002814 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002815 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002816 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002817 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002818
2819 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2820 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2821
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002822 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002823}
2824EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2825
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002826/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002827 * snd_soc_limit_volume - Set new limit to an existing volume control.
2828 *
2829 * @codec: where to look for the control
2830 * @name: Name of the control
2831 * @max: new maximum limit
2832 *
2833 * Return 0 for success, else error.
2834 */
2835int snd_soc_limit_volume(struct snd_soc_codec *codec,
2836 const char *name, int max)
2837{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002838 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002839 struct snd_kcontrol *kctl;
2840 struct soc_mixer_control *mc;
2841 int found = 0;
2842 int ret = -EINVAL;
2843
2844 /* Sanity check for name and max */
2845 if (unlikely(!name || max <= 0))
2846 return -EINVAL;
2847
2848 list_for_each_entry(kctl, &card->controls, list) {
2849 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2850 found = 1;
2851 break;
2852 }
2853 }
2854 if (found) {
2855 mc = (struct soc_mixer_control *)kctl->private_value;
2856 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002857 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002858 ret = 0;
2859 }
2860 }
2861 return ret;
2862}
2863EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2864
2865/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002866 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2867 * mixer info callback
2868 * @kcontrol: mixer control
2869 * @uinfo: control element information
2870 *
2871 * Returns 0 for success.
2872 */
2873int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2874 struct snd_ctl_elem_info *uinfo)
2875{
2876 struct soc_mixer_control *mc =
2877 (struct soc_mixer_control *)kcontrol->private_value;
2878 int max = mc->max;
2879 int min = mc->min;
2880
2881 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2882 uinfo->count = 2;
2883 uinfo->value.integer.min = 0;
2884 uinfo->value.integer.max = max-min;
2885
2886 return 0;
2887}
2888EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2889
2890/**
2891 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2892 * mixer get callback
2893 * @kcontrol: mixer control
2894 * @uinfo: control element information
2895 *
2896 * Returns 0 for success.
2897 */
2898int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2899 struct snd_ctl_elem_value *ucontrol)
2900{
2901 struct soc_mixer_control *mc =
2902 (struct soc_mixer_control *)kcontrol->private_value;
2903 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2904 unsigned int mask = (1<<mc->shift)-1;
2905 int min = mc->min;
2906 int val = snd_soc_read(codec, mc->reg) & mask;
2907 int valr = snd_soc_read(codec, mc->rreg) & mask;
2908
Stuart Longland20630c72010-06-18 12:56:10 +10002909 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2910 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002911 return 0;
2912}
2913EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2914
2915/**
2916 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2917 * mixer put callback
2918 * @kcontrol: mixer control
2919 * @uinfo: control element information
2920 *
2921 * Returns 0 for success.
2922 */
2923int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2924 struct snd_ctl_elem_value *ucontrol)
2925{
2926 struct soc_mixer_control *mc =
2927 (struct soc_mixer_control *)kcontrol->private_value;
2928 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2929 unsigned int mask = (1<<mc->shift)-1;
2930 int min = mc->min;
2931 int ret;
2932 unsigned int val, valr, oval, ovalr;
2933
2934 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2935 val &= mask;
2936 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2937 valr &= mask;
2938
2939 oval = snd_soc_read(codec, mc->reg) & mask;
2940 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2941
2942 ret = 0;
2943 if (oval != val) {
2944 ret = snd_soc_write(codec, mc->reg, val);
2945 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002946 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002947 }
2948 if (ovalr != valr) {
2949 ret = snd_soc_write(codec, mc->rreg, valr);
2950 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002951 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002952 }
2953
2954 return 0;
2955}
2956EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2957
2958/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002959 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2960 * @dai: DAI
2961 * @clk_id: DAI specific clock ID
2962 * @freq: new clock frequency in Hz
2963 * @dir: new clock direction - input/output.
2964 *
2965 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2966 */
2967int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2968 unsigned int freq, int dir)
2969{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002970 if (dai->driver && dai->driver->ops->set_sysclk)
2971 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002972 else
2973 return -EINVAL;
2974}
2975EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2976
2977/**
2978 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2979 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002980 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002981 * @div: new clock divisor.
2982 *
2983 * Configures the clock dividers. This is used to derive the best DAI bit and
2984 * frame clocks from the system or master clock. It's best to set the DAI bit
2985 * and frame clocks as low as possible to save system power.
2986 */
2987int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2988 int div_id, int div)
2989{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002990 if (dai->driver && dai->driver->ops->set_clkdiv)
2991 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002992 else
2993 return -EINVAL;
2994}
2995EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2996
2997/**
2998 * snd_soc_dai_set_pll - configure DAI PLL.
2999 * @dai: DAI
3000 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003001 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003002 * @freq_in: PLL input clock frequency in Hz
3003 * @freq_out: requested PLL output clock frequency in Hz
3004 *
3005 * Configures and enables PLL to generate output clock based on input clock.
3006 */
Mark Brown85488032009-09-05 18:52:16 +01003007int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3008 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003009{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003010 if (dai->driver && dai->driver->ops->set_pll)
3011 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003012 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003013 else
3014 return -EINVAL;
3015}
3016EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3017
3018/**
3019 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3020 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003021 * @fmt: SND_SOC_DAIFMT_ format value.
3022 *
3023 * Configures the DAI hardware format and clocking.
3024 */
3025int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3026{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003027 if (dai->driver && dai->driver->ops->set_fmt)
3028 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003029 else
3030 return -EINVAL;
3031}
3032EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3033
3034/**
3035 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3036 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003037 * @tx_mask: bitmask representing active TX slots.
3038 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003039 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003040 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003041 *
3042 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3043 * specific.
3044 */
3045int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003046 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003047{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003048 if (dai->driver && dai->driver->ops->set_tdm_slot)
3049 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003050 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003051 else
3052 return -EINVAL;
3053}
3054EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3055
3056/**
Barry Song472df3c2009-09-12 01:16:29 +08003057 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3058 * @dai: DAI
3059 * @tx_num: how many TX channels
3060 * @tx_slot: pointer to an array which imply the TX slot number channel
3061 * 0~num-1 uses
3062 * @rx_num: how many RX channels
3063 * @rx_slot: pointer to an array which imply the RX slot number channel
3064 * 0~num-1 uses
3065 *
3066 * configure the relationship between channel number and TDM slot number.
3067 */
3068int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3069 unsigned int tx_num, unsigned int *tx_slot,
3070 unsigned int rx_num, unsigned int *rx_slot)
3071{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003072 if (dai->driver && dai->driver->ops->set_channel_map)
3073 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003074 rx_num, rx_slot);
3075 else
3076 return -EINVAL;
3077}
3078EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3079
3080/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003081 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3082 * @dai: DAI
3083 * @tristate: tristate enable
3084 *
3085 * Tristates the DAI so that others can use it.
3086 */
3087int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3088{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003089 if (dai->driver && dai->driver->ops->set_tristate)
3090 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003091 else
3092 return -EINVAL;
3093}
3094EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3095
3096/**
3097 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3098 * @dai: DAI
3099 * @mute: mute enable
3100 *
3101 * Mutes the DAI DAC.
3102 */
3103int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3104{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003105 if (dai->driver && dai->driver->ops->digital_mute)
3106 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003107 else
3108 return -EINVAL;
3109}
3110EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3111
Mark Brownc5af3a22008-11-28 13:29:45 +00003112/**
3113 * snd_soc_register_card - Register a card with the ASoC core
3114 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003115 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003116 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003117 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303118int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003119{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003120 int i;
3121
Mark Brownc5af3a22008-11-28 13:29:45 +00003122 if (!card->name || !card->dev)
3123 return -EINVAL;
3124
Vinod Koul150dd2f2011-01-13 22:48:32 +05303125 soc_init_card_debugfs(card);
3126
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003127 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
3128 (card->num_links + card->num_aux_devs),
3129 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003130 if (card->rtd == NULL)
3131 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003132 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003133
3134 for (i = 0; i < card->num_links; i++)
3135 card->rtd[i].dai_link = &card->dai_link[i];
3136
Mark Brownc5af3a22008-11-28 13:29:45 +00003137 INIT_LIST_HEAD(&card->list);
3138 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003139 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003140
3141 mutex_lock(&client_mutex);
3142 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003143 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00003144 mutex_unlock(&client_mutex);
3145
3146 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3147
3148 return 0;
3149}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303150EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003151
3152/**
3153 * snd_soc_unregister_card - Unregister a card with the ASoC core
3154 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003155 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003156 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003157 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303158int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003159{
Vinod Koulb0e26482011-01-13 22:48:02 +05303160 if (card->instantiated)
3161 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003162 mutex_lock(&client_mutex);
3163 list_del(&card->list);
3164 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003165 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3166
3167 return 0;
3168}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303169EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003170
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003171/*
3172 * Simplify DAI link configuration by removing ".-1" from device names
3173 * and sanitizing names.
3174 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003175static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003176{
3177 char *found, name[NAME_SIZE];
3178 int id1, id2;
3179
3180 if (dev_name(dev) == NULL)
3181 return NULL;
3182
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003183 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003184
3185 /* are we a "%s.%d" name (platform and SPI components) */
3186 found = strstr(name, dev->driver->name);
3187 if (found) {
3188 /* get ID */
3189 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3190
3191 /* discard ID from name if ID == -1 */
3192 if (*id == -1)
3193 found[strlen(dev->driver->name)] = '\0';
3194 }
3195
3196 } else {
3197 /* I2C component devices are named "bus-addr" */
3198 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3199 char tmp[NAME_SIZE];
3200
3201 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003202 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003203
3204 /* sanitize component name for DAI link creation */
3205 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003206 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003207 } else
3208 *id = 0;
3209 }
3210
3211 return kstrdup(name, GFP_KERNEL);
3212}
3213
3214/*
3215 * Simplify DAI link naming for single devices with multiple DAIs by removing
3216 * any ".-1" and using the DAI name (instead of device name).
3217 */
3218static inline char *fmt_multiple_name(struct device *dev,
3219 struct snd_soc_dai_driver *dai_drv)
3220{
3221 if (dai_drv->name == NULL) {
3222 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3223 dev_name(dev));
3224 return NULL;
3225 }
3226
3227 return kstrdup(dai_drv->name, GFP_KERNEL);
3228}
3229
Mark Brown91151712008-11-30 23:31:24 +00003230/**
3231 * snd_soc_register_dai - Register a DAI with the ASoC core
3232 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003233 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003234 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003235int snd_soc_register_dai(struct device *dev,
3236 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003237{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003238 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003239
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003240 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003241
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003242 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3243 if (dai == NULL)
3244 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003245
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003246 /* create DAI component name */
3247 dai->name = fmt_single_name(dev, &dai->id);
3248 if (dai->name == NULL) {
3249 kfree(dai);
3250 return -ENOMEM;
3251 }
3252
3253 dai->dev = dev;
3254 dai->driver = dai_drv;
3255 if (!dai->driver->ops)
3256 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003257
3258 mutex_lock(&client_mutex);
3259 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003260 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003261 mutex_unlock(&client_mutex);
3262
3263 pr_debug("Registered DAI '%s'\n", dai->name);
3264
3265 return 0;
3266}
3267EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3268
3269/**
3270 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3271 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003272 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003273 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003274void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003275{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003276 struct snd_soc_dai *dai;
3277
3278 list_for_each_entry(dai, &dai_list, list) {
3279 if (dev == dai->dev)
3280 goto found;
3281 }
3282 return;
3283
3284found:
Mark Brown91151712008-11-30 23:31:24 +00003285 mutex_lock(&client_mutex);
3286 list_del(&dai->list);
3287 mutex_unlock(&client_mutex);
3288
3289 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003290 kfree(dai->name);
3291 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003292}
3293EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3294
3295/**
3296 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3297 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003298 * @dai: Array of DAIs to register
3299 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003300 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003301int snd_soc_register_dais(struct device *dev,
3302 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003303{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003304 struct snd_soc_dai *dai;
3305 int i, ret = 0;
3306
Liam Girdwood720ffa42010-08-18 00:30:30 +01003307 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003308
3309 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003310
3311 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003312 if (dai == NULL) {
3313 ret = -ENOMEM;
3314 goto err;
3315 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003316
3317 /* create DAI component name */
3318 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3319 if (dai->name == NULL) {
3320 kfree(dai);
3321 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003322 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003323 }
3324
3325 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003326 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003327 if (dai->driver->id)
3328 dai->id = dai->driver->id;
3329 else
3330 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003331 if (!dai->driver->ops)
3332 dai->driver->ops = &null_dai_ops;
3333
3334 mutex_lock(&client_mutex);
3335 list_add(&dai->list, &dai_list);
3336 mutex_unlock(&client_mutex);
3337
3338 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003339 }
3340
Axel Lin1dcb4f32010-12-06 16:48:03 +08003341 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003342 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08003343 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00003344 return 0;
3345
3346err:
3347 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003348 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003349
3350 return ret;
3351}
3352EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3353
3354/**
3355 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3356 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003357 * @dai: Array of DAIs to unregister
3358 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003359 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003360void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003361{
3362 int i;
3363
3364 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003365 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003366}
3367EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3368
Mark Brown12a48a8c2008-12-03 19:40:30 +00003369/**
3370 * snd_soc_register_platform - Register a platform with the ASoC core
3371 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003372 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003373 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003374int snd_soc_register_platform(struct device *dev,
3375 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003376{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003377 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003378
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003379 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3380
3381 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3382 if (platform == NULL)
3383 return -ENOMEM;
3384
3385 /* create platform component name */
3386 platform->name = fmt_single_name(dev, &platform->id);
3387 if (platform->name == NULL) {
3388 kfree(platform);
3389 return -ENOMEM;
3390 }
3391
3392 platform->dev = dev;
3393 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003394
3395 mutex_lock(&client_mutex);
3396 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003397 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003398 mutex_unlock(&client_mutex);
3399
3400 pr_debug("Registered platform '%s'\n", platform->name);
3401
3402 return 0;
3403}
3404EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3405
3406/**
3407 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3408 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003409 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003410 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003411void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003412{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003413 struct snd_soc_platform *platform;
3414
3415 list_for_each_entry(platform, &platform_list, list) {
3416 if (dev == platform->dev)
3417 goto found;
3418 }
3419 return;
3420
3421found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003422 mutex_lock(&client_mutex);
3423 list_del(&platform->list);
3424 mutex_unlock(&client_mutex);
3425
3426 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003427 kfree(platform->name);
3428 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003429}
3430EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3431
Mark Brown151ab222009-05-09 16:22:58 +01003432static u64 codec_format_map[] = {
3433 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3434 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3435 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3436 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3437 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3438 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3439 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3440 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3441 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3442 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3443 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3444 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3445 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3446 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3447 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3448 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3449};
3450
3451/* Fix up the DAI formats for endianness: codecs don't actually see
3452 * the endianness of the data but we're using the CPU format
3453 * definitions which do need to include endianness so we ensure that
3454 * codec DAIs always have both big and little endian variants set.
3455 */
3456static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3457{
3458 int i;
3459
3460 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3461 if (stream->formats & codec_format_map[i])
3462 stream->formats |= codec_format_map[i];
3463}
3464
Mark Brown0d0cf002008-12-10 14:32:45 +00003465/**
3466 * snd_soc_register_codec - Register a codec with the ASoC core
3467 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003468 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003469 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003470int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003471 const struct snd_soc_codec_driver *codec_drv,
3472 struct snd_soc_dai_driver *dai_drv,
3473 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003474{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003475 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003476 struct snd_soc_codec *codec;
3477 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003478
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003479 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003480
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003481 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3482 if (codec == NULL)
3483 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003484
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003485 /* create CODEC component name */
3486 codec->name = fmt_single_name(dev, &codec->id);
3487 if (codec->name == NULL) {
3488 kfree(codec);
3489 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003490 }
3491
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003492 if (codec_drv->compress_type)
3493 codec->compress_type = codec_drv->compress_type;
3494 else
3495 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3496
Mark Brownc3acec22010-12-02 16:15:29 +00003497 codec->write = codec_drv->write;
3498 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003499 codec->volatile_register = codec_drv->volatile_register;
3500 codec->readable_register = codec_drv->readable_register;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003501 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3502 codec->dapm.dev = dev;
3503 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003504 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003505 codec->dev = dev;
3506 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003507 codec->num_dai = num_dai;
3508 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003509
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003510 /* allocate CODEC register cache */
3511 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003512 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003513 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003514 /* it is necessary to make a copy of the default register cache
3515 * because in the case of using a compression type that requires
3516 * the default register cache to be marked as __devinitconst the
3517 * kernel might have freed the array by the time we initialize
3518 * the cache.
3519 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003520 if (codec_drv->reg_cache_default) {
3521 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3522 reg_size, GFP_KERNEL);
3523 if (!codec->reg_def_copy) {
3524 ret = -ENOMEM;
3525 goto fail;
3526 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003527 }
3528 }
3529
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003530 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3531 if (!codec->volatile_register)
3532 codec->volatile_register = snd_soc_default_volatile_register;
3533 if (!codec->readable_register)
3534 codec->readable_register = snd_soc_default_readable_register;
3535 }
3536
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003537 for (i = 0; i < num_dai; i++) {
3538 fixup_codec_formats(&dai_drv[i].playback);
3539 fixup_codec_formats(&dai_drv[i].capture);
3540 }
3541
Mark Brown26b01cc2010-08-18 20:20:55 +01003542 /* register any DAIs */
3543 if (num_dai) {
3544 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3545 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003546 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003547 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003548
Mark Brown0d0cf002008-12-10 14:32:45 +00003549 mutex_lock(&client_mutex);
3550 list_add(&codec->list, &codec_list);
3551 snd_soc_instantiate_cards();
3552 mutex_unlock(&client_mutex);
3553
3554 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003555 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003556
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003557fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003558 kfree(codec->reg_def_copy);
3559 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003560 kfree(codec->name);
3561 kfree(codec);
3562 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003563}
3564EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3565
3566/**
3567 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3568 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003569 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003570 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003571void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003572{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003573 struct snd_soc_codec *codec;
3574 int i;
3575
3576 list_for_each_entry(codec, &codec_list, list) {
3577 if (dev == codec->dev)
3578 goto found;
3579 }
3580 return;
3581
3582found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003583 if (codec->num_dai)
3584 for (i = 0; i < codec->num_dai; i++)
3585 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003586
Mark Brown0d0cf002008-12-10 14:32:45 +00003587 mutex_lock(&client_mutex);
3588 list_del(&codec->list);
3589 mutex_unlock(&client_mutex);
3590
3591 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003592
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003593 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003594 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003595 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003596 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003597}
3598EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3599
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003600static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003601{
Mark Brown384c89e2008-12-03 17:34:03 +00003602#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003603 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3604 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Mark Brown384c89e2008-12-03 17:34:03 +00003605 printk(KERN_WARNING
3606 "ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003607 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003608 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003609
Mark Brown8a9dab12011-01-10 22:25:21 +00003610 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003611 &codec_list_fops))
3612 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3613
Mark Brown8a9dab12011-01-10 22:25:21 +00003614 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003615 &dai_list_fops))
3616 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003617
Mark Brown8a9dab12011-01-10 22:25:21 +00003618 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003619 &platform_list_fops))
3620 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003621#endif
3622
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003623 return platform_driver_register(&soc_driver);
3624}
Mark Brown4abe8e12010-10-12 17:41:03 +01003625module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003626
Mark Brown7d8c16a2008-11-30 22:11:24 +00003627static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003628{
Mark Brown384c89e2008-12-03 17:34:03 +00003629#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003630 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003631#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003632 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003633}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003634module_exit(snd_soc_exit);
3635
3636/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003637MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003638MODULE_DESCRIPTION("ALSA SoC Core");
3639MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003640MODULE_ALIAS("platform:soc-audio");