blob: e9aa9ce59c06d3b2a605148e4812dfb520c952e9 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Marek Vasut474828a2009-07-22 13:01:03 +020034#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020035#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020039#include <sound/initval.h>
40
Mark Browna8b1d342010-11-03 18:05:58 -040041#define CREATE_TRACE_POINTS
42#include <trace/events/asoc.h>
43
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000044#define NAME_SIZE 32
45
Frank Mandarinodb2a4162006-10-06 18:31:09 +020046static DEFINE_MUTEX(pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020047static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
48
Mark Brown384c89e2008-12-03 17:34:03 +000049#ifdef CONFIG_DEBUG_FS
50static struct dentry *debugfs_root;
51#endif
52
Mark Brownc5af3a22008-11-28 13:29:45 +000053static DEFINE_MUTEX(client_mutex);
54static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000055static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000056static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000057static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000058
59static int snd_soc_register_card(struct snd_soc_card *card);
60static int snd_soc_unregister_card(struct snd_soc_card *card);
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
Liam Girdwood965ac422007-01-31 14:14:57 +010072/*
73 * This function forces any delayed work to be queued and run.
74 */
75static int run_delayed_work(struct delayed_work *dwork)
76{
77 int ret;
78
79 /* cancel any work waiting to be queued. */
80 ret = cancel_delayed_work(dwork);
81
82 /* if there was any work waiting then we run it now and
83 * wait for it's completion */
84 if (ret) {
85 schedule_delayed_work(dwork, 0);
86 flush_scheduled_work();
87 }
88 return ret;
89}
90
Mark Brown2624d5f2009-11-03 21:56:13 +000091/* codec register dump */
92static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
93{
Mark Brown5164d742010-07-14 16:14:33 +010094 int ret, i, step = 1, count = 0;
Mark Brown2624d5f2009-11-03 21:56:13 +000095
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000096 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +000097 return 0;
98
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000099 if (codec->driver->reg_cache_step)
100 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000101
102 count += sprintf(buf, "%s registers\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000103 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
104 if (codec->driver->readable_register && !codec->driver->readable_register(i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000105 continue;
106
107 count += sprintf(buf + count, "%2x: ", i);
108 if (count >= PAGE_SIZE - 1)
109 break;
110
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000111 if (codec->driver->display_register) {
112 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000113 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100114 } else {
115 /* If the read fails it's almost certainly due to
116 * the register being volatile and the device being
117 * powered off.
118 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000119 ret = codec->driver->read(codec, i);
Mark Brown5164d742010-07-14 16:14:33 +0100120 if (ret >= 0)
121 count += snprintf(buf + count,
122 PAGE_SIZE - count,
123 "%4x", ret);
124 else
125 count += snprintf(buf + count,
126 PAGE_SIZE - count,
127 "<no data: %d>", ret);
128 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000129
130 if (count >= PAGE_SIZE - 1)
131 break;
132
133 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
134 if (count >= PAGE_SIZE - 1)
135 break;
136 }
137
138 /* Truncate count; min() would cause a warning */
139 if (count >= PAGE_SIZE)
140 count = PAGE_SIZE - 1;
141
142 return count;
143}
144static ssize_t codec_reg_show(struct device *dev,
145 struct device_attribute *attr, char *buf)
146{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000147 struct snd_soc_pcm_runtime *rtd =
148 container_of(dev, struct snd_soc_pcm_runtime, dev);
149
150 return soc_codec_reg_show(rtd->codec, buf);
Mark Brown2624d5f2009-11-03 21:56:13 +0000151}
152
153static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
154
Mark Browndbe21402010-02-12 11:37:24 +0000155static ssize_t pmdown_time_show(struct device *dev,
156 struct device_attribute *attr, char *buf)
157{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000158 struct snd_soc_pcm_runtime *rtd =
159 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Browndbe21402010-02-12 11:37:24 +0000160
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000161 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000162}
163
164static ssize_t pmdown_time_set(struct device *dev,
165 struct device_attribute *attr,
166 const char *buf, size_t count)
167{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000168 struct snd_soc_pcm_runtime *rtd =
169 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Brownc593b522010-10-27 20:11:17 -0700170 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000171
Mark Brownc593b522010-10-27 20:11:17 -0700172 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
173 if (ret)
174 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000175
176 return count;
177}
178
179static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
180
Mark Brown2624d5f2009-11-03 21:56:13 +0000181#ifdef CONFIG_DEBUG_FS
182static int codec_reg_open_file(struct inode *inode, struct file *file)
183{
184 file->private_data = inode->i_private;
185 return 0;
186}
187
188static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
189 size_t count, loff_t *ppos)
190{
191 ssize_t ret;
192 struct snd_soc_codec *codec = file->private_data;
193 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
194 if (!buf)
195 return -ENOMEM;
196 ret = soc_codec_reg_show(codec, buf);
197 if (ret >= 0)
198 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
199 kfree(buf);
200 return ret;
201}
202
203static ssize_t codec_reg_write_file(struct file *file,
204 const char __user *user_buf, size_t count, loff_t *ppos)
205{
206 char buf[32];
207 int buf_size;
208 char *start = buf;
209 unsigned long reg, value;
210 int step = 1;
211 struct snd_soc_codec *codec = file->private_data;
212
213 buf_size = min(count, (sizeof(buf)-1));
214 if (copy_from_user(buf, user_buf, buf_size))
215 return -EFAULT;
216 buf[buf_size] = 0;
217
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000218 if (codec->driver->reg_cache_step)
219 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000220
221 while (*start == ' ')
222 start++;
223 reg = simple_strtoul(start, &start, 16);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000224 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
Mark Brown2624d5f2009-11-03 21:56:13 +0000225 return -EINVAL;
226 while (*start == ' ')
227 start++;
228 if (strict_strtoul(start, 16, &value))
229 return -EINVAL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000230 codec->driver->write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000231 return buf_size;
232}
233
234static const struct file_operations codec_reg_fops = {
235 .open = codec_reg_open_file,
236 .read = codec_reg_read_file,
237 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200238 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000239};
240
241static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
242{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200243 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
244
245 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
246 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000247 if (!codec->debugfs_codec_root) {
248 printk(KERN_WARNING
249 "ASoC: Failed to create codec debugfs directory\n");
250 return;
251 }
252
253 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
254 codec->debugfs_codec_root,
255 codec, &codec_reg_fops);
256 if (!codec->debugfs_reg)
257 printk(KERN_WARNING
258 "ASoC: Failed to create codec register debugfs file\n");
259
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200260 codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
Mark Brown2624d5f2009-11-03 21:56:13 +0000261 codec->debugfs_codec_root);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200262 if (!codec->dapm.debugfs_dapm)
Mark Brown2624d5f2009-11-03 21:56:13 +0000263 printk(KERN_WARNING
264 "Failed to create DAPM debugfs directory\n");
265
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200266 snd_soc_dapm_debugfs_init(&codec->dapm);
Mark Brown2624d5f2009-11-03 21:56:13 +0000267}
268
269static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
270{
271 debugfs_remove_recursive(codec->debugfs_codec_root);
272}
273
Mark Brownc3c5a192010-09-15 18:15:14 +0100274static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
275 size_t count, loff_t *ppos)
276{
277 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100278 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100279 struct snd_soc_codec *codec;
280
281 if (!buf)
282 return -ENOMEM;
283
Mark Brown2b194f9d2010-10-13 10:52:16 +0100284 list_for_each_entry(codec, &codec_list, list) {
285 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
286 codec->name);
287 if (len >= 0)
288 ret += len;
289 if (ret > PAGE_SIZE) {
290 ret = PAGE_SIZE;
291 break;
292 }
293 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100294
295 if (ret >= 0)
296 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
297
298 kfree(buf);
299
300 return ret;
301}
302
303static const struct file_operations codec_list_fops = {
304 .read = codec_list_read_file,
305 .llseek = default_llseek,/* read accesses f_pos */
306};
307
Mark Brownf3208782010-09-15 18:19:07 +0100308static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
309 size_t count, loff_t *ppos)
310{
311 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100312 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100313 struct snd_soc_dai *dai;
314
315 if (!buf)
316 return -ENOMEM;
317
Mark Brown2b194f9d2010-10-13 10:52:16 +0100318 list_for_each_entry(dai, &dai_list, list) {
319 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
320 if (len >= 0)
321 ret += len;
322 if (ret > PAGE_SIZE) {
323 ret = PAGE_SIZE;
324 break;
325 }
326 }
Mark Brownf3208782010-09-15 18:19:07 +0100327
Mark Brown2b194f9d2010-10-13 10:52:16 +0100328 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100329
330 kfree(buf);
331
332 return ret;
333}
334
335static const struct file_operations dai_list_fops = {
336 .read = dai_list_read_file,
337 .llseek = default_llseek,/* read accesses f_pos */
338};
339
Mark Brown19c7ac22010-09-15 18:22:40 +0100340static ssize_t platform_list_read_file(struct file *file,
341 char __user *user_buf,
342 size_t count, loff_t *ppos)
343{
344 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100345 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100346 struct snd_soc_platform *platform;
347
348 if (!buf)
349 return -ENOMEM;
350
Mark Brown2b194f9d2010-10-13 10:52:16 +0100351 list_for_each_entry(platform, &platform_list, list) {
352 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
353 platform->name);
354 if (len >= 0)
355 ret += len;
356 if (ret > PAGE_SIZE) {
357 ret = PAGE_SIZE;
358 break;
359 }
360 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100361
Mark Brown2b194f9d2010-10-13 10:52:16 +0100362 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100363
364 kfree(buf);
365
366 return ret;
367}
368
369static const struct file_operations platform_list_fops = {
370 .read = platform_list_read_file,
371 .llseek = default_llseek,/* read accesses f_pos */
372};
373
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200374static void soc_init_card_debugfs(struct snd_soc_card *card)
375{
376 card->debugfs_card_root = debugfs_create_dir(card->name,
377 debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200378 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200379 dev_warn(card->dev,
380 "ASoC: Failed to create codec debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200381 return;
382 }
383
384 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
385 card->debugfs_card_root,
386 &card->pop_time);
387 if (!card->debugfs_pop_time)
388 dev_warn(card->dev,
389 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200390}
391
392static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
393{
394 debugfs_remove_recursive(card->debugfs_card_root);
395}
396
Mark Brown2624d5f2009-11-03 21:56:13 +0000397#else
398
399static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
400{
401}
402
403static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
404{
405}
Axel Linb95fccb2010-11-09 17:06:44 +0800406
407static inline void soc_init_card_debugfs(struct snd_soc_card *card)
408{
409}
410
411static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
412{
413}
Mark Brown2624d5f2009-11-03 21:56:13 +0000414#endif
415
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200416#ifdef CONFIG_SND_SOC_AC97_BUS
417/* unregister ac97 codec */
418static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
419{
420 if (codec->ac97->dev.bus)
421 device_unregister(&codec->ac97->dev);
422 return 0;
423}
424
425/* stop no dev release warning */
426static void soc_ac97_device_release(struct device *dev){}
427
428/* register ac97 codec to bus */
429static int soc_ac97_dev_register(struct snd_soc_codec *codec)
430{
431 int err;
432
433 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100434 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200435 codec->ac97->dev.release = soc_ac97_device_release;
436
Kay Sieversbb072bf2008-11-02 03:50:35 +0100437 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000438 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200439 err = device_register(&codec->ac97->dev);
440 if (err < 0) {
441 snd_printk(KERN_ERR "Can't register ac97 bus\n");
442 codec->ac97->dev.bus = NULL;
443 return err;
444 }
445 return 0;
446}
447#endif
448
Mark Brown06f409d2009-04-07 18:10:13 +0100449static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
450{
451 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000452 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
453 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100454 int ret;
455
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000456 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
457 rtd->dai_link->symmetric_rates) {
458 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
459 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100460
461 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
462 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000463 rtd->rate,
464 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100465 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000466 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100467 "Unable to apply rate symmetry constraint: %d\n", ret);
468 return ret;
469 }
470 }
471
472 return 0;
473}
474
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200475/*
476 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
477 * then initialized and any private data can be allocated. This also calls
478 * startup for the cpu DAI, platform, machine and codec DAI.
479 */
480static int soc_pcm_open(struct snd_pcm_substream *substream)
481{
482 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200483 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000484 struct snd_soc_platform *platform = rtd->platform;
485 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
486 struct snd_soc_dai *codec_dai = rtd->codec_dai;
487 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
488 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200489 int ret = 0;
490
491 mutex_lock(&pcm_mutex);
492
493 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000494 if (cpu_dai->driver->ops->startup) {
495 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200496 if (ret < 0) {
497 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100498 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200499 goto out;
500 }
501 }
502
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000503 if (platform->driver->ops->open) {
504 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200505 if (ret < 0) {
506 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
507 goto platform_err;
508 }
509 }
510
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000511 if (codec_dai->driver->ops->startup) {
512 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100513 if (ret < 0) {
514 printk(KERN_ERR "asoc: can't open codec %s\n",
515 codec_dai->name);
516 goto codec_dai_err;
517 }
518 }
519
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000520 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
521 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200522 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000523 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200524 goto machine_err;
525 }
526 }
527
Mark Browna00f90f2010-12-02 16:24:24 +0000528 /* Check that the codec and cpu DAIs are compatible */
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200529 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
530 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531 max(codec_dai_drv->playback.rate_min,
532 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200533 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000534 min(codec_dai_drv->playback.rate_max,
535 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000537 max(codec_dai_drv->playback.channels_min,
538 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200539 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000540 min(codec_dai_drv->playback.channels_max,
541 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100542 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000543 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100544 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000545 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
546 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900547 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548 runtime->hw.rates |= cpu_dai_drv->playback.rates;
549 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900550 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000551 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200552 } else {
553 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 max(codec_dai_drv->capture.rate_min,
555 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200556 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000557 min(codec_dai_drv->capture.rate_max,
558 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200559 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000560 max(codec_dai_drv->capture.channels_min,
561 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200562 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000563 min(codec_dai_drv->capture.channels_max,
564 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100565 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000566 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100567 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000568 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
569 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900570 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571 runtime->hw.rates |= cpu_dai_drv->capture.rates;
572 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900573 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200575 }
576
577 snd_pcm_limit_hw_rates(runtime);
578 if (!runtime->hw.rates) {
579 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100580 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 if (!runtime->hw.formats) {
584 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100585 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900586 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200587 }
588 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
589 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000590 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900591 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592 }
593
Mark Brown06f409d2009-04-07 18:10:13 +0100594 /* Symmetry only applies if we've already got an active stream. */
595 if (cpu_dai->active || codec_dai->active) {
596 ret = soc_pcm_apply_symmetry(substream);
597 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900598 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100599 }
600
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 pr_debug("asoc: %s <-> %s info:\n",
602 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100603 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
604 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
605 runtime->hw.channels_max);
606 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
607 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200608
Jassi Brar14dc5732010-02-26 09:12:32 +0900609 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 cpu_dai->playback_active++;
611 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900612 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 cpu_dai->capture_active++;
614 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900615 }
616 cpu_dai->active++;
617 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200619 mutex_unlock(&pcm_mutex);
620 return 0;
621
Jassi Brarbb1c0472010-02-25 11:24:53 +0900622config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
624 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200625
Jassi Brarbb1c0472010-02-25 11:24:53 +0900626machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627 if (codec_dai->driver->ops->shutdown)
628 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900629
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100630codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631 if (platform->driver->ops->close)
632 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200633
634platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000635 if (cpu_dai->driver->ops->shutdown)
636 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200637out:
638 mutex_unlock(&pcm_mutex);
639 return ret;
640}
641
642/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200643 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200644 * This is to ensure there are no pops or clicks in between any music tracks
645 * due to DAPM power cycling.
646 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100647static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200648{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000649 struct snd_soc_pcm_runtime *rtd =
650 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
651 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200652
653 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000655 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
656 codec_dai->driver->playback.stream_name,
657 codec_dai->playback_active ? "active" : "inactive",
658 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200659
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660 /* are we waiting on this codec DAI stream */
661 if (codec_dai->pop_wait == 1) {
662 codec_dai->pop_wait = 0;
663 snd_soc_dapm_stream_event(rtd,
664 codec_dai->driver->playback.stream_name,
665 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200666 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000667
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200668 mutex_unlock(&pcm_mutex);
669}
670
671/*
672 * Called by ALSA when a PCM substream is closed. Private data can be
673 * freed here. The cpu DAI, codec DAI, machine and platform are also
674 * shutdown.
675 */
676static int soc_codec_close(struct snd_pcm_substream *substream)
677{
678 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000679 struct snd_soc_platform *platform = rtd->platform;
680 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
681 struct snd_soc_dai *codec_dai = rtd->codec_dai;
682 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200683
684 mutex_lock(&pcm_mutex);
685
Jassi Brar14dc5732010-02-26 09:12:32 +0900686 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000687 cpu_dai->playback_active--;
688 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900689 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690 cpu_dai->capture_active--;
691 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200692 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900693
694 cpu_dai->active--;
695 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200696 codec->active--;
697
Mark Brown6010b2d2008-09-06 18:33:24 +0100698 /* Muting the DAC suppresses artifacts caused during digital
699 * shutdown, for example from stopping clocks.
700 */
701 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
702 snd_soc_dai_digital_mute(codec_dai, 1);
703
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000704 if (cpu_dai->driver->ops->shutdown)
705 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200706
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000707 if (codec_dai->driver->ops->shutdown)
708 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200709
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000710 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
711 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713 if (platform->driver->ops->close)
714 platform->driver->ops->close(substream);
715 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716
717 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
718 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100719 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000720 schedule_delayed_work(&rtd->delayed_work,
721 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200722 } else {
723 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 snd_soc_dapm_stream_event(rtd,
725 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100726 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200727 }
728
729 mutex_unlock(&pcm_mutex);
730 return 0;
731}
732
733/*
734 * Called by ALSA when the PCM substream is prepared, can set format, sample
735 * rate, etc. This function is non atomic and can be called multiple times,
736 * it can refer to the runtime info.
737 */
738static int soc_pcm_prepare(struct snd_pcm_substream *substream)
739{
740 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000741 struct snd_soc_platform *platform = rtd->platform;
742 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
743 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200744 int ret = 0;
745
746 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
749 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100750 if (ret < 0) {
751 printk(KERN_ERR "asoc: machine prepare error\n");
752 goto out;
753 }
754 }
755
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756 if (platform->driver->ops->prepare) {
757 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200758 if (ret < 0) {
759 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200760 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200761 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200762 }
763
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000764 if (codec_dai->driver->ops->prepare) {
765 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200766 if (ret < 0) {
767 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200768 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200769 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200770 }
771
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000772 if (cpu_dai->driver->ops->prepare) {
773 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100774 if (ret < 0) {
775 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
776 goto out;
777 }
778 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200779
Mark Brownd45f6212008-10-14 13:58:36 +0100780 /* cancel any delayed stream shutdown that is pending */
781 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
782 codec_dai->pop_wait) {
783 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000784 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100785 }
786
Mark Brown452c5ea2009-05-17 21:41:23 +0100787 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000788 snd_soc_dapm_stream_event(rtd,
789 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100790 SND_SOC_DAPM_STREAM_START);
791 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000792 snd_soc_dapm_stream_event(rtd,
793 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100794 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100795
Mark Brown452c5ea2009-05-17 21:41:23 +0100796 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200797
798out:
799 mutex_unlock(&pcm_mutex);
800 return ret;
801}
802
803/*
804 * Called by ALSA when the hardware params are set by application. This
805 * function can also be called multiple times and can allocate buffers
806 * (using snd_pcm_lib_* ). It's non-atomic.
807 */
808static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
809 struct snd_pcm_hw_params *params)
810{
811 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000812 struct snd_soc_platform *platform = rtd->platform;
813 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
814 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200815 int ret = 0;
816
817 mutex_lock(&pcm_mutex);
818
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000819 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
820 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200821 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100822 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200823 goto out;
824 }
825 }
826
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000827 if (codec_dai->driver->ops->hw_params) {
828 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100829 if (ret < 0) {
830 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
831 codec_dai->name);
832 goto codec_err;
833 }
834 }
835
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000836 if (cpu_dai->driver->ops->hw_params) {
837 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200838 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200839 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100840 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200841 goto interface_err;
842 }
843 }
844
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000845 if (platform->driver->ops->hw_params) {
846 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200847 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200848 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849 platform->name);
850 goto platform_err;
851 }
852 }
853
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000854 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100855
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200856out:
857 mutex_unlock(&pcm_mutex);
858 return ret;
859
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200860platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000861 if (cpu_dai->driver->ops->hw_free)
862 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200863
864interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000865 if (codec_dai->driver->ops->hw_free)
866 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100867
868codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000869 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
870 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200871
872 mutex_unlock(&pcm_mutex);
873 return ret;
874}
875
876/*
Mark Browna00f90f2010-12-02 16:24:24 +0000877 * Frees resources allocated by hw_params, can be called multiple times
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200878 */
879static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
880{
881 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000882 struct snd_soc_platform *platform = rtd->platform;
883 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
884 struct snd_soc_dai *codec_dai = rtd->codec_dai;
885 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200886
887 mutex_lock(&pcm_mutex);
888
889 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100890 if (!codec->active)
891 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200892
893 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000894 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
895 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200896
897 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000898 if (platform->driver->ops->hw_free)
899 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200900
Mark Browna00f90f2010-12-02 16:24:24 +0000901 /* now free hw params for the DAIs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000902 if (codec_dai->driver->ops->hw_free)
903 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200904
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000905 if (cpu_dai->driver->ops->hw_free)
906 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200907
908 mutex_unlock(&pcm_mutex);
909 return 0;
910}
911
912static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
913{
914 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000915 struct snd_soc_platform *platform = rtd->platform;
916 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
917 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200918 int ret;
919
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000920 if (codec_dai->driver->ops->trigger) {
921 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200922 if (ret < 0)
923 return ret;
924 }
925
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000926 if (platform->driver->ops->trigger) {
927 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200928 if (ret < 0)
929 return ret;
930 }
931
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000932 if (cpu_dai->driver->ops->trigger) {
933 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200934 if (ret < 0)
935 return ret;
936 }
937 return 0;
938}
939
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200940/*
941 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200942 * If cpu_dai, codec_dai, platform driver has the delay callback, than
943 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200944 */
945static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
946{
947 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000948 struct snd_soc_platform *platform = rtd->platform;
949 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
950 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200951 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200952 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200953 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200954
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000955 if (platform->driver->ops->pointer)
956 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200957
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000958 if (cpu_dai->driver->ops->delay)
959 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200960
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000961 if (codec_dai->driver->ops->delay)
962 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200963
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000964 if (platform->driver->delay)
965 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200966
967 runtime->delay = delay;
968
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200969 return offset;
970}
971
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200972/* ASoC PCM operations */
973static struct snd_pcm_ops soc_pcm_ops = {
974 .open = soc_pcm_open,
975 .close = soc_codec_close,
976 .hw_params = soc_pcm_hw_params,
977 .hw_free = soc_pcm_hw_free,
978 .prepare = soc_pcm_prepare,
979 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200980 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200981};
982
983#ifdef CONFIG_PM
984/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100985static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200986{
Mark Brown416356f2009-06-30 19:05:15 +0100987 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000988 struct snd_soc_card *card = platform_get_drvdata(pdev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200989 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200990 int i;
991
Daniel Macke3509ff2009-06-03 17:44:49 +0200992 /* If the initialization of this soc device failed, there is no codec
993 * associated with it. Just bail out in this case.
994 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000995 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200996 return 0;
997
Andy Green6ed25972008-06-13 16:24:05 +0100998 /* Due to the resume being scheduled into a workqueue we could
999 * suspend before that's finished - wait for it to complete.
1000 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001001 snd_power_lock(card->snd_card);
1002 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
1003 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +01001004
1005 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001006 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +01001007
Mark Browna00f90f2010-12-02 16:24:24 +00001008 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001009 for (i = 0; i < card->num_rtd; i++) {
1010 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1011 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001012
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001013 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001014 continue;
1015
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001016 if (drv->ops->digital_mute && dai->playback_active)
1017 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001018 }
1019
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001020 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001021 for (i = 0; i < card->num_rtd; i++) {
1022 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001023 continue;
1024
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001025 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +01001026 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001027
Mark Brown87506542008-11-18 20:50:34 +00001028 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +01001029 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001030
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001031 for (i = 0; i < card->num_rtd; i++) {
1032 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1033 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001034
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001035 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001036 continue;
1037
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001038 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1039 cpu_dai->driver->suspend(cpu_dai);
1040 if (platform->driver->suspend && !platform->suspended) {
1041 platform->driver->suspend(cpu_dai);
1042 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +01001043 }
1044 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001045
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001046 /* close any waiting streams and save state */
1047 for (i = 0; i < card->num_rtd; i++) {
1048 run_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001049 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001050 }
Mark Brown3efab7d2010-05-09 13:25:43 +01001051
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001052 for (i = 0; i < card->num_rtd; i++) {
1053 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1054
1055 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001056 continue;
1057
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001058 if (driver->playback.stream_name != NULL)
1059 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1060 SND_SOC_DAPM_STREAM_SUSPEND);
1061
1062 if (driver->capture.stream_name != NULL)
1063 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1064 SND_SOC_DAPM_STREAM_SUSPEND);
1065 }
1066
1067 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001068 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001069 /* If there are paths active then the CODEC will be held with
1070 * bias _ON and should not be suspended. */
1071 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001072 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001073 case SND_SOC_BIAS_STANDBY:
1074 case SND_SOC_BIAS_OFF:
1075 codec->driver->suspend(codec, PMSG_SUSPEND);
1076 codec->suspended = 1;
1077 break;
1078 default:
1079 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1080 break;
1081 }
1082 }
1083 }
1084
1085 for (i = 0; i < card->num_rtd; i++) {
1086 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1087
1088 if (card->rtd[i].dai_link->ignore_suspend)
1089 continue;
1090
1091 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1092 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001093 }
1094
Mark Brown87506542008-11-18 20:50:34 +00001095 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +01001096 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001097
1098 return 0;
1099}
1100
Andy Green6ed25972008-06-13 16:24:05 +01001101/* deferred resume work, so resume can complete before we finished
1102 * setting our codec back up, which can be very slow on I2C
1103 */
1104static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001105{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001106 struct snd_soc_card *card =
1107 container_of(work, struct snd_soc_card, deferred_resume_work);
1108 struct platform_device *pdev = to_platform_device(card->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001109 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001110 int i;
1111
Andy Green6ed25972008-06-13 16:24:05 +01001112 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1113 * so userspace apps are blocked from touching us
1114 */
1115
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001116 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001117
Mark Brown99497882010-05-07 20:24:05 +01001118 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001119 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +01001120
Mark Brown87506542008-11-18 20:50:34 +00001121 if (card->resume_pre)
1122 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001123
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001124 /* resume AC97 DAIs */
1125 for (i = 0; i < card->num_rtd; i++) {
1126 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001127
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001128 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001129 continue;
1130
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001131 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1132 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001133 }
1134
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001135 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001136 /* If the CODEC was idle over suspend then it will have been
1137 * left with bias OFF or STANDBY and suspended so we must now
1138 * resume. Otherwise the suspend was suppressed.
1139 */
1140 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001141 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001142 case SND_SOC_BIAS_STANDBY:
1143 case SND_SOC_BIAS_OFF:
1144 codec->driver->resume(codec);
1145 codec->suspended = 0;
1146 break;
1147 default:
1148 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1149 break;
1150 }
Mark Brown1547aba2010-05-07 21:11:40 +01001151 }
1152 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001153
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001154 for (i = 0; i < card->num_rtd; i++) {
1155 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001156
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001157 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001158 continue;
1159
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001160 if (driver->playback.stream_name != NULL)
1161 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001162 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001163
1164 if (driver->capture.stream_name != NULL)
1165 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001166 SND_SOC_DAPM_STREAM_RESUME);
1167 }
1168
Mark Brown3ff3f642008-05-19 12:32:25 +02001169 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001170 for (i = 0; i < card->num_rtd; i++) {
1171 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1172 struct snd_soc_dai_driver *drv = dai->driver;
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 (drv->ops->digital_mute && dai->playback_active)
1178 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001179 }
1180
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001181 for (i = 0; i < card->num_rtd; i++) {
1182 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1183 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001184
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001185 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001186 continue;
1187
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001188 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1189 cpu_dai->driver->resume(cpu_dai);
1190 if (platform->driver->resume && platform->suspended) {
1191 platform->driver->resume(cpu_dai);
1192 platform->suspended = 0;
1193 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001194 }
1195
Mark Brown87506542008-11-18 20:50:34 +00001196 if (card->resume_post)
1197 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001198
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001199 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001200
1201 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001202 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001203}
1204
1205/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001206static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001207{
Mark Brown416356f2009-06-30 19:05:15 +01001208 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001209 struct snd_soc_card *card = platform_get_drvdata(pdev);
1210 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001211
Mark Brown64ab9ba2009-03-31 11:27:03 +01001212 /* AC97 devices might have other drivers hanging off them so
1213 * need to resume immediately. Other drivers don't have that
1214 * problem and may take a substantial amount of time to resume
1215 * due to I/O costs and anti-pop so handle them out of line.
1216 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001217 for (i = 0; i < card->num_rtd; i++) {
1218 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1219 if (cpu_dai->driver->ac97_control) {
1220 dev_dbg(dev, "Resuming AC97 immediately\n");
1221 soc_resume_deferred(&card->deferred_resume_work);
1222 } else {
1223 dev_dbg(dev, "Scheduling resume work\n");
1224 if (!schedule_work(&card->deferred_resume_work))
1225 dev_err(dev, "resume work item may be lost\n");
1226 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001227 }
Andy Green6ed25972008-06-13 16:24:05 +01001228
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001229 return 0;
1230}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001231#else
1232#define soc_suspend NULL
1233#define soc_resume NULL
1234#endif
1235
Barry Song02a06d32009-10-16 18:13:38 +08001236static struct snd_soc_dai_ops null_dai_ops = {
1237};
1238
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001239static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001240{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001241 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1242 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001243 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001244 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001245 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001246
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001247 if (rtd->complete)
1248 return 1;
1249 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001250
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001251 /* do we already have the CPU DAI for this link ? */
1252 if (rtd->cpu_dai) {
1253 goto find_codec;
1254 }
1255 /* no, then find CPU DAI from registered DAIs*/
1256 list_for_each_entry(cpu_dai, &dai_list, list) {
1257 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1258
1259 if (!try_module_get(cpu_dai->dev->driver->owner))
1260 return -ENODEV;
1261
1262 rtd->cpu_dai = cpu_dai;
1263 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001264 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001265 }
1266 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1267 dai_link->cpu_dai_name);
1268
1269find_codec:
1270 /* do we already have the CODEC for this link ? */
1271 if (rtd->codec) {
1272 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001273 }
1274
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001275 /* no, then find CODEC from registered CODECs*/
1276 list_for_each_entry(codec, &codec_list, list) {
1277 if (!strcmp(codec->name, dai_link->codec_name)) {
1278 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001279
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001280 if (!try_module_get(codec->dev->driver->owner))
1281 return -ENODEV;
Mark Brown435c5e22008-12-04 15:32:53 +00001282
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001283 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1284 list_for_each_entry(codec_dai, &dai_list, list) {
1285 if (codec->dev == codec_dai->dev &&
1286 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1287 rtd->codec_dai = codec_dai;
1288 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001289 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001290 }
1291 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1292 dai_link->codec_dai_name);
1293
1294 goto find_platform;
1295 }
1296 }
1297 dev_dbg(card->dev, "CODEC %s not registered\n",
1298 dai_link->codec_name);
1299
1300find_platform:
1301 /* do we already have the CODEC DAI for this link ? */
1302 if (rtd->platform) {
1303 goto out;
1304 }
1305 /* no, then find CPU DAI from registered DAIs*/
1306 list_for_each_entry(platform, &platform_list, list) {
1307 if (!strcmp(platform->name, dai_link->platform_name)) {
1308
1309 if (!try_module_get(platform->dev->driver->owner))
1310 return -ENODEV;
1311
1312 rtd->platform = platform;
1313 goto out;
1314 }
1315 }
1316
1317 dev_dbg(card->dev, "platform %s not registered\n",
1318 dai_link->platform_name);
1319 return 0;
1320
1321out:
1322 /* mark rtd as complete if we found all 4 of our client devices */
1323 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1324 rtd->complete = 1;
1325 card->num_rtd++;
1326 }
1327 return 1;
1328}
1329
1330static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1331{
1332 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1333 struct snd_soc_codec *codec = rtd->codec;
1334 struct snd_soc_platform *platform = rtd->platform;
1335 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1336 int err;
1337
1338 /* unregister the rtd device */
1339 if (rtd->dev_registered) {
1340 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1341 device_unregister(&rtd->dev);
1342 rtd->dev_registered = 0;
1343 }
1344
1345 /* remove the CODEC DAI */
1346 if (codec_dai && codec_dai->probed) {
1347 if (codec_dai->driver->remove) {
1348 err = codec_dai->driver->remove(codec_dai);
1349 if (err < 0)
1350 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1351 }
1352 codec_dai->probed = 0;
1353 list_del(&codec_dai->card_list);
1354 }
1355
1356 /* remove the platform */
1357 if (platform && platform->probed) {
1358 if (platform->driver->remove) {
1359 err = platform->driver->remove(platform);
1360 if (err < 0)
1361 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1362 }
1363 platform->probed = 0;
1364 list_del(&platform->card_list);
1365 module_put(platform->dev->driver->owner);
1366 }
1367
1368 /* remove the CODEC */
1369 if (codec && codec->probed) {
1370 if (codec->driver->remove) {
1371 err = codec->driver->remove(codec);
1372 if (err < 0)
1373 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1374 }
1375
1376 /* Make sure all DAPM widgets are freed */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001377 snd_soc_dapm_free(&codec->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001378
1379 soc_cleanup_codec_debugfs(codec);
1380 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1381 codec->probed = 0;
1382 list_del(&codec->card_list);
1383 module_put(codec->dev->driver->owner);
1384 }
1385
1386 /* remove the cpu_dai */
1387 if (cpu_dai && cpu_dai->probed) {
1388 if (cpu_dai->driver->remove) {
1389 err = cpu_dai->driver->remove(cpu_dai);
1390 if (err < 0)
1391 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1392 }
1393 cpu_dai->probed = 0;
1394 list_del(&cpu_dai->card_list);
1395 module_put(cpu_dai->dev->driver->owner);
1396 }
1397}
1398
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001399static void soc_set_name_prefix(struct snd_soc_card *card,
1400 struct snd_soc_codec *codec)
1401{
1402 int i;
1403
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001404 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001405 return;
1406
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001407 for (i = 0; i < card->num_configs; i++) {
1408 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001409 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1410 codec->name_prefix = map->name_prefix;
1411 break;
1412 }
1413 }
1414}
1415
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001416static void rtd_release(struct device *dev) {}
1417
1418static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1419{
1420 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1421 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1422 struct snd_soc_codec *codec = rtd->codec;
1423 struct snd_soc_platform *platform = rtd->platform;
1424 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001425 const char *temp;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001426 int ret;
1427
1428 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1429
1430 /* config components */
1431 codec_dai->codec = codec;
1432 codec->card = card;
1433 cpu_dai->platform = platform;
1434 rtd->card = card;
1435 rtd->dev.parent = card->dev;
1436 codec_dai->card = card;
1437 cpu_dai->card = card;
1438
1439 /* set default power off timeout */
1440 rtd->pmdown_time = pmdown_time;
1441
1442 /* probe the cpu_dai */
1443 if (!cpu_dai->probed) {
1444 if (cpu_dai->driver->probe) {
1445 ret = cpu_dai->driver->probe(cpu_dai);
1446 if (ret < 0) {
1447 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1448 cpu_dai->name);
1449 return ret;
1450 }
1451 }
1452 cpu_dai->probed = 1;
1453 /* mark cpu_dai as probed and add to card cpu_dai list */
1454 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1455 }
1456
1457 /* probe the CODEC */
1458 if (!codec->probed) {
Jarkko Nikula3a45b862010-11-05 20:35:21 +02001459 codec->dapm.card = card;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001460 soc_set_name_prefix(card, codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001461 if (codec->driver->probe) {
1462 ret = codec->driver->probe(codec);
1463 if (ret < 0) {
1464 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1465 codec->name);
1466 return ret;
1467 }
1468 }
Mark Brown13cb61f2010-08-12 15:44:04 +01001469
1470 soc_init_codec_debugfs(codec);
1471
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001472 /* mark codec as probed and add to card codec list */
1473 codec->probed = 1;
1474 list_add(&codec->card_list, &card->codec_dev_list);
1475 }
1476
1477 /* probe the platform */
1478 if (!platform->probed) {
1479 if (platform->driver->probe) {
1480 ret = platform->driver->probe(platform);
1481 if (ret < 0) {
1482 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1483 platform->name);
1484 return ret;
1485 }
1486 }
1487 /* mark platform as probed and add to card platform list */
1488 platform->probed = 1;
1489 list_add(&platform->card_list, &card->platform_dev_list);
1490 }
1491
1492 /* probe the CODEC DAI */
1493 if (!codec_dai->probed) {
1494 if (codec_dai->driver->probe) {
1495 ret = codec_dai->driver->probe(codec_dai);
1496 if (ret < 0) {
1497 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1498 codec_dai->name);
1499 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001500 }
1501 }
1502
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001503 /* mark cpu_dai as probed and add to card cpu_dai list */
1504 codec_dai->probed = 1;
1505 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001506 }
1507
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001508 /* DAPM dai link stream work */
1509 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1510
1511 /* now that all clients have probed, initialise the DAI link */
1512 if (dai_link->init) {
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001513 /* machine controls, routes and widgets are not prefixed */
1514 temp = rtd->codec->name_prefix;
1515 rtd->codec->name_prefix = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001516 ret = dai_link->init(rtd);
1517 if (ret < 0) {
1518 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1519 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001520 }
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001521 rtd->codec->name_prefix = temp;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001522 }
1523
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001524 /* Make sure all DAPM widgets are instantiated */
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001525 snd_soc_dapm_new_widgets(&codec->dapm);
1526 snd_soc_dapm_sync(&codec->dapm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001527
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001528 /* register the rtd device */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001529 rtd->dev.release = rtd_release;
1530 rtd->dev.init_name = dai_link->name;
1531 ret = device_register(&rtd->dev);
1532 if (ret < 0) {
1533 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1534 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001535 }
1536
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001537 rtd->dev_registered = 1;
1538 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1539 if (ret < 0)
1540 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1541
1542 /* add DAPM sysfs entries for this codec */
1543 ret = snd_soc_dapm_sys_add(&rtd->dev);
1544 if (ret < 0)
1545 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1546
1547 /* add codec sysfs entries */
1548 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1549 if (ret < 0)
1550 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1551
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001552 /* create the pcm */
1553 ret = soc_new_pcm(rtd, num);
1554 if (ret < 0) {
1555 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1556 return ret;
1557 }
1558
1559 /* add platform data for AC97 devices */
1560 if (rtd->codec_dai->driver->ac97_control)
1561 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1562
1563 return 0;
1564}
1565
1566#ifdef CONFIG_SND_SOC_AC97_BUS
1567static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1568{
1569 int ret;
1570
1571 /* Only instantiate AC97 if not already done by the adaptor
1572 * for the generic AC97 subsystem.
1573 */
1574 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001575 /*
1576 * It is possible that the AC97 device is already registered to
1577 * the device subsystem. This happens when the device is created
1578 * via snd_ac97_mixer(). Currently only SoC codec that does so
1579 * is the generic AC97 glue but others migh emerge.
1580 *
1581 * In those cases we don't try to register the device again.
1582 */
1583 if (!rtd->codec->ac97_created)
1584 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001585
1586 ret = soc_ac97_dev_register(rtd->codec);
1587 if (ret < 0) {
1588 printk(KERN_ERR "asoc: AC97 device register failed\n");
1589 return ret;
1590 }
1591
1592 rtd->codec->ac97_registered = 1;
1593 }
1594 return 0;
1595}
1596
1597static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1598{
1599 if (codec->ac97_registered) {
1600 soc_ac97_dev_unregister(codec);
1601 codec->ac97_registered = 0;
1602 }
1603}
1604#endif
1605
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001606static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1607{
1608 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1609 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1610 struct snd_soc_codec *codec;
1611 const char *temp;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001612 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001613
1614 /* find CODEC from registered CODECs*/
1615 list_for_each_entry(codec, &codec_list, list) {
1616 if (!strcmp(codec->name, aux_dev->codec_name)) {
1617 if (codec->probed) {
1618 dev_err(codec->dev,
1619 "asoc: codec already probed");
1620 ret = -EBUSY;
1621 goto out;
1622 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001623 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001624 }
1625 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001626 /* codec not found */
1627 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1628 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001629
Jarkko Nikula676ad982010-12-03 09:18:22 +02001630found:
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001631 if (!try_module_get(codec->dev->driver->owner))
1632 return -ENODEV;
1633
1634 codec->card = card;
1635 codec->dapm.card = card;
1636
1637 soc_set_name_prefix(card, codec);
1638 if (codec->driver->probe) {
1639 ret = codec->driver->probe(codec);
1640 if (ret < 0) {
1641 dev_err(codec->dev, "asoc: failed to probe CODEC");
1642 return ret;
1643 }
1644 }
1645
1646 soc_init_codec_debugfs(codec);
1647
1648 /* mark codec as probed and add to card codec list */
1649 codec->probed = 1;
1650 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001651
1652 /* now that all clients have probed, initialise the DAI link */
1653 if (aux_dev->init) {
1654 /* machine controls, routes and widgets are not prefixed */
1655 temp = codec->name_prefix;
1656 codec->name_prefix = NULL;
1657 ret = aux_dev->init(&codec->dapm);
1658 if (ret < 0) {
1659 dev_err(codec->dev,
1660 "asoc: failed to init %s\n", aux_dev->name);
1661 return ret;
1662 }
1663 codec->name_prefix = temp;
1664 }
1665
1666 /* Make sure all DAPM widgets are instantiated */
1667 snd_soc_dapm_new_widgets(&codec->dapm);
1668 snd_soc_dapm_sync(&codec->dapm);
1669
1670 /* register the rtd device */
1671 rtd->codec = codec;
1672 rtd->card = card;
1673 rtd->dev.parent = card->dev;
1674 rtd->dev.release = rtd_release;
1675 rtd->dev.init_name = aux_dev->name;
1676 ret = device_register(&rtd->dev);
1677 if (ret < 0) {
1678 dev_err(codec->dev,
1679 "asoc: failed to register aux runtime device %d\n",
1680 ret);
1681 return ret;
1682 }
1683 rtd->dev_registered = 1;
1684
1685 /* add DAPM sysfs entries for this codec */
1686 ret = snd_soc_dapm_sys_add(&rtd->dev);
1687 if (ret < 0)
1688 dev_err(codec->dev,
1689 "asoc: failed to add codec dapm sysfs entries\n");
1690
1691 /* add codec sysfs entries */
1692 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1693 if (ret < 0)
1694 dev_err(codec->dev, "asoc: failed to add codec sysfs files\n");
1695
1696out:
1697 return ret;
1698}
1699
1700static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1701{
1702 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1703 struct snd_soc_codec *codec = rtd->codec;
1704 int err;
1705
1706 /* unregister the rtd device */
1707 if (rtd->dev_registered) {
1708 device_unregister(&rtd->dev);
1709 rtd->dev_registered = 0;
1710 }
1711
1712 /* remove the CODEC */
1713 if (codec && codec->probed) {
1714 if (codec->driver->remove) {
1715 err = codec->driver->remove(codec);
1716 if (err < 0)
1717 dev_err(codec->dev,
1718 "asoc: failed to remove %s\n",
1719 codec->name);
1720 }
1721
1722 /* Make sure all DAPM widgets are freed */
1723 snd_soc_dapm_free(&codec->dapm);
1724
1725 soc_cleanup_codec_debugfs(codec);
1726 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1727 codec->probed = 0;
1728 list_del(&codec->card_list);
1729 module_put(codec->dev->driver->owner);
1730 }
1731}
1732
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001733static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1734 enum snd_soc_compress_type compress_type)
1735{
1736 int ret;
1737
1738 if (codec->cache_init)
1739 return 0;
1740
1741 /* override the compress_type if necessary */
1742 if (compress_type && codec->compress_type != compress_type)
1743 codec->compress_type = compress_type;
1744 dev_dbg(codec->dev, "Cache compress_type for %s is %d\n",
1745 codec->name, codec->compress_type);
1746 ret = snd_soc_cache_init(codec);
1747 if (ret < 0) {
1748 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1749 ret);
1750 return ret;
1751 }
1752 codec->cache_init = 1;
1753 return 0;
1754}
1755
1756
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001757static void snd_soc_instantiate_card(struct snd_soc_card *card)
1758{
1759 struct platform_device *pdev = to_platform_device(card->dev);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001760 struct snd_soc_codec *codec;
1761 struct snd_soc_codec_conf *codec_conf;
1762 enum snd_soc_compress_type compress_type;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001763 int ret, i;
1764
1765 mutex_lock(&card->mutex);
1766
1767 if (card->instantiated) {
1768 mutex_unlock(&card->mutex);
1769 return;
1770 }
1771
1772 /* bind DAIs */
1773 for (i = 0; i < card->num_links; i++)
1774 soc_bind_dai_link(card, i);
1775
1776 /* bind completed ? */
1777 if (card->num_rtd != card->num_links) {
1778 mutex_unlock(&card->mutex);
1779 return;
1780 }
1781
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001782 /* initialize the register cache for each available codec */
1783 list_for_each_entry(codec, &codec_list, list) {
1784 if (codec->cache_init)
1785 continue;
1786 /* check to see if we need to override the compress_type */
1787 for (i = 0; i < card->num_configs; ++i) {
1788 codec_conf = &card->codec_conf[i];
1789 if (!strcmp(codec->name, codec_conf->dev_name)) {
1790 compress_type = codec_conf->compress_type;
1791 if (compress_type && compress_type
1792 != codec->compress_type)
1793 break;
1794 }
1795 }
1796 if (i == card->num_configs) {
1797 /* no need to override the compress_type so
1798 * go ahead and do the standard thing */
1799 ret = snd_soc_init_codec_cache(codec, 0);
1800 if (ret < 0) {
1801 mutex_unlock(&card->mutex);
1802 return;
1803 }
1804 continue;
1805 }
1806 /* override the compress_type with the one supplied in
1807 * the machine driver */
1808 ret = snd_soc_init_codec_cache(codec, compress_type);
1809 if (ret < 0) {
1810 mutex_unlock(&card->mutex);
1811 return;
1812 }
1813 }
1814
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001815 /* card bind complete so register a sound card */
1816 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1817 card->owner, 0, &card->snd_card);
1818 if (ret < 0) {
1819 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1820 card->name);
1821 mutex_unlock(&card->mutex);
1822 return;
1823 }
1824 card->snd_card->dev = card->dev;
1825
Randy Dunlap1301a962008-06-17 19:19:34 +01001826#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001827 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001828 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001829#endif
Andy Green6ed25972008-06-13 16:24:05 +01001830
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001831 /* initialise the sound card only once */
1832 if (card->probe) {
1833 ret = card->probe(pdev);
1834 if (ret < 0)
1835 goto card_probe_error;
1836 }
1837
Mark Brownfe3e78e2009-11-03 22:13:13 +00001838 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001839 ret = soc_probe_dai_link(card, i);
1840 if (ret < 0) {
Mark Brown321de0d2010-09-21 15:09:37 +01001841 pr_err("asoc: failed to instantiate card %s: %d\n",
1842 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001843 goto probe_dai_err;
1844 }
1845 }
1846
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001847 for (i = 0; i < card->num_aux_devs; i++) {
1848 ret = soc_probe_aux_dev(card, i);
1849 if (ret < 0) {
1850 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1851 card->name, ret);
1852 goto probe_aux_dev_err;
1853 }
1854 }
1855
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001856 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1857 "%s", card->name);
1858 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1859 "%s", card->name);
1860
1861 ret = snd_card_register(card->snd_card);
1862 if (ret < 0) {
1863 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1864 goto probe_dai_err;
1865 }
1866
1867#ifdef CONFIG_SND_SOC_AC97_BUS
1868 /* register any AC97 codecs */
1869 for (i = 0; i < card->num_rtd; i++) {
1870 ret = soc_register_ac97_dai_link(&card->rtd[i]);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001871 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001872 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1873 goto probe_dai_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001874 }
1875 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001876#endif
1877
Mark Brown435c5e22008-12-04 15:32:53 +00001878 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001879 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001880 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001881
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001882probe_aux_dev_err:
1883 for (i = 0; i < card->num_aux_devs; i++)
1884 soc_remove_aux_dev(card, i);
1885
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001886probe_dai_err:
1887 for (i = 0; i < card->num_links; i++)
1888 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001889
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001890card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001891 if (card->remove)
1892 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001893
1894 snd_card_free(card->snd_card);
1895
1896 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001897}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001898
Mark Brown435c5e22008-12-04 15:32:53 +00001899/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001900 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001901 * client_mutex.
1902 */
1903static void snd_soc_instantiate_cards(void)
1904{
1905 struct snd_soc_card *card;
1906 list_for_each_entry(card, &card_list, list)
1907 snd_soc_instantiate_card(card);
1908}
1909
1910/* probes a new socdev */
1911static int soc_probe(struct platform_device *pdev)
1912{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001913 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001914 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001915
1916 /* Bodge while we unpick instantiation */
1917 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001918 INIT_LIST_HEAD(&card->dai_dev_list);
1919 INIT_LIST_HEAD(&card->codec_dev_list);
1920 INIT_LIST_HEAD(&card->platform_dev_list);
1921
Jarkko Nikulaa6052152010-11-05 20:35:19 +02001922 soc_init_card_debugfs(card);
1923
Mark Brown435c5e22008-12-04 15:32:53 +00001924 ret = snd_soc_register_card(card);
1925 if (ret != 0) {
1926 dev_err(&pdev->dev, "Failed to register card\n");
1927 return ret;
1928 }
1929
1930 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001931}
1932
1933/* removes a socdev */
1934static int soc_remove(struct platform_device *pdev)
1935{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001936 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001937 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001938
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001939 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001940
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001941 /* make sure any delayed work runs */
1942 for (i = 0; i < card->num_rtd; i++) {
1943 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1944 run_delayed_work(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001945 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001946
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001947 /* remove auxiliary devices */
1948 for (i = 0; i < card->num_aux_devs; i++)
1949 soc_remove_aux_dev(card, i);
1950
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001951 /* remove and free each DAI */
1952 for (i = 0; i < card->num_rtd; i++)
1953 soc_remove_dai_link(card, i);
1954
Jarkko Nikulaa6052152010-11-05 20:35:19 +02001955 soc_cleanup_card_debugfs(card);
1956
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001957 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001958 if (card->remove)
1959 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001960
1961 kfree(card->rtd);
1962 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001963 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001964 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001965 return 0;
1966}
1967
Mark Brown416356f2009-06-30 19:05:15 +01001968static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001969{
Mark Brown416356f2009-06-30 19:05:15 +01001970 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001971 struct snd_soc_card *card = platform_get_drvdata(pdev);
1972 int i;
Mark Brown51737472009-06-22 13:16:51 +01001973
1974 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001975 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001976
1977 /* Flush out pmdown_time work - we actually do want to run it
1978 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001979 for (i = 0; i < card->num_rtd; i++) {
1980 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1981 run_delayed_work(&rtd->delayed_work);
1982 }
Mark Brown51737472009-06-22 13:16:51 +01001983
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001984 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001985
1986 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001987}
1988
Alexey Dobriyan47145212009-12-14 18:00:08 -08001989static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001990 .suspend = soc_suspend,
1991 .resume = soc_resume,
1992 .poweroff = soc_poweroff,
1993};
1994
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001995/* ASoC platform driver */
1996static struct platform_driver soc_driver = {
1997 .driver = {
1998 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001999 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01002000 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002001 },
2002 .probe = soc_probe,
2003 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002004};
2005
2006/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002007static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002008{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002009 struct snd_soc_codec *codec = rtd->codec;
2010 struct snd_soc_platform *platform = rtd->platform;
2011 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2012 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002013 struct snd_pcm *pcm;
2014 char new_name[64];
2015 int ret = 0, playback = 0, capture = 0;
2016
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002017 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00002018 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002019 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002020
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002021 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002022 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002023 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002024 capture = 1;
2025
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002026 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
2027 ret = snd_pcm_new(rtd->card->snd_card, new_name,
2028 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002029 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002030 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002031 return ret;
2032 }
2033
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002034 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002035 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002036 soc_pcm_ops.mmap = platform->driver->ops->mmap;
2037 soc_pcm_ops.pointer = platform->driver->ops->pointer;
2038 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
2039 soc_pcm_ops.copy = platform->driver->ops->copy;
2040 soc_pcm_ops.silence = platform->driver->ops->silence;
2041 soc_pcm_ops.ack = platform->driver->ops->ack;
2042 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002043
2044 if (playback)
2045 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
2046
2047 if (capture)
2048 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
2049
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002050 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002051 if (ret < 0) {
2052 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002053 return ret;
2054 }
2055
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002056 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002057 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2058 cpu_dai->name);
2059 return ret;
2060}
2061
Mark Brown096e49d2009-07-05 15:12:22 +01002062/**
2063 * snd_soc_codec_volatile_register: Report if a register is volatile.
2064 *
2065 * @codec: CODEC to query.
2066 * @reg: Register to query.
2067 *
2068 * Boolean function indiciating if a CODEC register is volatile.
2069 */
2070int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
2071{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002072 if (codec->driver->volatile_register)
2073 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01002074 else
2075 return 0;
2076}
2077EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2078
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002079/**
2080 * snd_soc_new_ac97_codec - initailise AC97 device
2081 * @codec: audio codec
2082 * @ops: AC97 bus operations
2083 * @num: AC97 codec number
2084 *
2085 * Initialises AC97 codec resources for use by ad-hoc devices only.
2086 */
2087int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2088 struct snd_ac97_bus_ops *ops, int num)
2089{
2090 mutex_lock(&codec->mutex);
2091
2092 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2093 if (codec->ac97 == NULL) {
2094 mutex_unlock(&codec->mutex);
2095 return -ENOMEM;
2096 }
2097
2098 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2099 if (codec->ac97->bus == NULL) {
2100 kfree(codec->ac97);
2101 codec->ac97 = NULL;
2102 mutex_unlock(&codec->mutex);
2103 return -ENOMEM;
2104 }
2105
2106 codec->ac97->bus->ops = ops;
2107 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002108
2109 /*
2110 * Mark the AC97 device to be created by us. This way we ensure that the
2111 * device will be registered with the device subsystem later on.
2112 */
2113 codec->ac97_created = 1;
2114
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002115 mutex_unlock(&codec->mutex);
2116 return 0;
2117}
2118EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2119
2120/**
2121 * snd_soc_free_ac97_codec - free AC97 codec device
2122 * @codec: audio codec
2123 *
2124 * Frees AC97 codec device resources.
2125 */
2126void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2127{
2128 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002129#ifdef CONFIG_SND_SOC_AC97_BUS
2130 soc_unregister_ac97_dai_link(codec);
2131#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002132 kfree(codec->ac97->bus);
2133 kfree(codec->ac97);
2134 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002135 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002136 mutex_unlock(&codec->mutex);
2137}
2138EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2139
Mark Brownc3753702010-11-01 15:41:57 -04002140unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2141{
2142 unsigned int ret;
2143
Mark Brownc3acec22010-12-02 16:15:29 +00002144 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002145 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002146 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002147
2148 return ret;
2149}
2150EXPORT_SYMBOL_GPL(snd_soc_read);
2151
2152unsigned int snd_soc_write(struct snd_soc_codec *codec,
2153 unsigned int reg, unsigned int val)
2154{
2155 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002156 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002157 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002158}
2159EXPORT_SYMBOL_GPL(snd_soc_write);
2160
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002161/**
2162 * snd_soc_update_bits - update codec register bits
2163 * @codec: audio codec
2164 * @reg: codec register
2165 * @mask: register mask
2166 * @value: new value
2167 *
2168 * Writes new register value.
2169 *
2170 * Returns 1 for change else 0.
2171 */
2172int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002173 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002174{
2175 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002176 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002177
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002178 old = snd_soc_read(codec, reg);
2179 new = (old & ~mask) | value;
2180 change = old != new;
2181 if (change)
2182 snd_soc_write(codec, reg, new);
2183
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002184 return change;
2185}
2186EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2187
2188/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002189 * snd_soc_update_bits_locked - update codec register bits
2190 * @codec: audio codec
2191 * @reg: codec register
2192 * @mask: register mask
2193 * @value: new value
2194 *
2195 * Writes new register value, and takes the codec mutex.
2196 *
2197 * Returns 1 for change else 0.
2198 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002199int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2200 unsigned short reg, unsigned int mask,
2201 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002202{
2203 int change;
2204
2205 mutex_lock(&codec->mutex);
2206 change = snd_soc_update_bits(codec, reg, mask, value);
2207 mutex_unlock(&codec->mutex);
2208
2209 return change;
2210}
Mark Browndd1b3d52009-12-04 14:22:03 +00002211EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002212
2213/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002214 * snd_soc_test_bits - test register for change
2215 * @codec: audio codec
2216 * @reg: codec register
2217 * @mask: register mask
2218 * @value: new value
2219 *
2220 * Tests a register with a new value and checks if the new value is
2221 * different from the old value.
2222 *
2223 * Returns 1 for change else 0.
2224 */
2225int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002226 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002227{
2228 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002229 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002230
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002231 old = snd_soc_read(codec, reg);
2232 new = (old & ~mask) | value;
2233 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002234
2235 return change;
2236}
2237EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2238
2239/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002240 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2241 * @substream: the pcm substream
2242 * @hw: the hardware parameters
2243 *
2244 * Sets the substream runtime hardware parameters.
2245 */
2246int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2247 const struct snd_pcm_hardware *hw)
2248{
2249 struct snd_pcm_runtime *runtime = substream->runtime;
2250 runtime->hw.info = hw->info;
2251 runtime->hw.formats = hw->formats;
2252 runtime->hw.period_bytes_min = hw->period_bytes_min;
2253 runtime->hw.period_bytes_max = hw->period_bytes_max;
2254 runtime->hw.periods_min = hw->periods_min;
2255 runtime->hw.periods_max = hw->periods_max;
2256 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2257 runtime->hw.fifo_size = hw->fifo_size;
2258 return 0;
2259}
2260EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2261
2262/**
2263 * snd_soc_cnew - create new control
2264 * @_template: control template
2265 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002266 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002267 *
2268 * Create a new mixer control from a template control.
2269 *
2270 * Returns 0 for success, else error.
2271 */
2272struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2273 void *data, char *long_name)
2274{
2275 struct snd_kcontrol_new template;
2276
2277 memcpy(&template, _template, sizeof(template));
2278 if (long_name)
2279 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002280 template.index = 0;
2281
2282 return snd_ctl_new1(&template, data);
2283}
2284EXPORT_SYMBOL_GPL(snd_soc_cnew);
2285
2286/**
Ian Molton3e8e1952009-01-09 00:23:21 +00002287 * snd_soc_add_controls - add an array of controls to a codec.
2288 * Convienience function to add a list of controls. Many codecs were
2289 * duplicating this code.
2290 *
2291 * @codec: codec to add controls to
2292 * @controls: array of controls to add
2293 * @num_controls: number of elements in the array
2294 *
2295 * Return 0 for success, else error.
2296 */
2297int snd_soc_add_controls(struct snd_soc_codec *codec,
2298 const struct snd_kcontrol_new *controls, int num_controls)
2299{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002300 struct snd_card *card = codec->card->snd_card;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002301 char prefixed_name[44], *name;
Ian Molton3e8e1952009-01-09 00:23:21 +00002302 int err, i;
2303
2304 for (i = 0; i < num_controls; i++) {
2305 const struct snd_kcontrol_new *control = &controls[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002306 if (codec->name_prefix) {
2307 snprintf(prefixed_name, sizeof(prefixed_name), "%s %s",
2308 codec->name_prefix, control->name);
2309 name = prefixed_name;
2310 } else {
2311 name = control->name;
2312 }
2313 err = snd_ctl_add(card, snd_soc_cnew(control, codec, name));
Ian Molton3e8e1952009-01-09 00:23:21 +00002314 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002315 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
Jarkko Nikulaead9b912010-11-13 20:40:44 +02002316 codec->name, name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002317 return err;
2318 }
2319 }
2320
2321 return 0;
2322}
2323EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2324
2325/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002326 * snd_soc_info_enum_double - enumerated double mixer info callback
2327 * @kcontrol: mixer control
2328 * @uinfo: control element information
2329 *
2330 * Callback to provide information about a double enumerated
2331 * mixer control.
2332 *
2333 * Returns 0 for success.
2334 */
2335int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2336 struct snd_ctl_elem_info *uinfo)
2337{
2338 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2339
2340 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2341 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002342 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002343
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002344 if (uinfo->value.enumerated.item > e->max - 1)
2345 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002346 strcpy(uinfo->value.enumerated.name,
2347 e->texts[uinfo->value.enumerated.item]);
2348 return 0;
2349}
2350EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2351
2352/**
2353 * snd_soc_get_enum_double - enumerated double mixer get callback
2354 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002355 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002356 *
2357 * Callback to get the value of a double enumerated mixer.
2358 *
2359 * Returns 0 for success.
2360 */
2361int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2362 struct snd_ctl_elem_value *ucontrol)
2363{
2364 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2365 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002366 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002367
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002368 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002369 ;
2370 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002371 ucontrol->value.enumerated.item[0]
2372 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002373 if (e->shift_l != e->shift_r)
2374 ucontrol->value.enumerated.item[1] =
2375 (val >> e->shift_r) & (bitmask - 1);
2376
2377 return 0;
2378}
2379EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2380
2381/**
2382 * snd_soc_put_enum_double - enumerated double mixer put callback
2383 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002384 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002385 *
2386 * Callback to set the value of a double enumerated mixer.
2387 *
2388 * Returns 0 for success.
2389 */
2390int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2391 struct snd_ctl_elem_value *ucontrol)
2392{
2393 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2394 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002395 unsigned int val;
2396 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002397
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002398 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002399 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002400 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002401 return -EINVAL;
2402 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2403 mask = (bitmask - 1) << e->shift_l;
2404 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002405 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002406 return -EINVAL;
2407 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2408 mask |= (bitmask - 1) << e->shift_r;
2409 }
2410
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002411 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002412}
2413EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2414
2415/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002416 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2417 * @kcontrol: mixer control
2418 * @ucontrol: control element information
2419 *
2420 * Callback to get the value of a double semi enumerated mixer.
2421 *
2422 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2423 * used for handling bitfield coded enumeration for example.
2424 *
2425 * Returns 0 for success.
2426 */
2427int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2428 struct snd_ctl_elem_value *ucontrol)
2429{
2430 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002431 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002432 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002433
2434 reg_val = snd_soc_read(codec, e->reg);
2435 val = (reg_val >> e->shift_l) & e->mask;
2436 for (mux = 0; mux < e->max; mux++) {
2437 if (val == e->values[mux])
2438 break;
2439 }
2440 ucontrol->value.enumerated.item[0] = mux;
2441 if (e->shift_l != e->shift_r) {
2442 val = (reg_val >> e->shift_r) & e->mask;
2443 for (mux = 0; mux < e->max; mux++) {
2444 if (val == e->values[mux])
2445 break;
2446 }
2447 ucontrol->value.enumerated.item[1] = mux;
2448 }
2449
2450 return 0;
2451}
2452EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2453
2454/**
2455 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2456 * @kcontrol: mixer control
2457 * @ucontrol: control element information
2458 *
2459 * Callback to set the value of a double semi enumerated mixer.
2460 *
2461 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2462 * used for handling bitfield coded enumeration for example.
2463 *
2464 * Returns 0 for success.
2465 */
2466int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2467 struct snd_ctl_elem_value *ucontrol)
2468{
2469 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002470 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002471 unsigned int val;
2472 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002473
2474 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2475 return -EINVAL;
2476 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2477 mask = e->mask << e->shift_l;
2478 if (e->shift_l != e->shift_r) {
2479 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2480 return -EINVAL;
2481 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2482 mask |= e->mask << e->shift_r;
2483 }
2484
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002485 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002486}
2487EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2488
2489/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002490 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2491 * @kcontrol: mixer control
2492 * @uinfo: control element information
2493 *
2494 * Callback to provide information about an external enumerated
2495 * single mixer.
2496 *
2497 * Returns 0 for success.
2498 */
2499int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2500 struct snd_ctl_elem_info *uinfo)
2501{
2502 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2503
2504 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2505 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002506 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002507
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002508 if (uinfo->value.enumerated.item > e->max - 1)
2509 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002510 strcpy(uinfo->value.enumerated.name,
2511 e->texts[uinfo->value.enumerated.item]);
2512 return 0;
2513}
2514EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2515
2516/**
2517 * snd_soc_info_volsw_ext - external single mixer info callback
2518 * @kcontrol: mixer control
2519 * @uinfo: control element information
2520 *
2521 * Callback to provide information about a single external mixer control.
2522 *
2523 * Returns 0 for success.
2524 */
2525int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2526 struct snd_ctl_elem_info *uinfo)
2527{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002528 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002529
Mark Brownfd5dfad2009-04-15 21:37:46 +01002530 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002531 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2532 else
2533 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2534
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002535 uinfo->count = 1;
2536 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002537 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002538 return 0;
2539}
2540EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2541
2542/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002543 * snd_soc_info_volsw - single mixer info callback
2544 * @kcontrol: mixer control
2545 * @uinfo: control element information
2546 *
2547 * Callback to provide information about a single mixer control.
2548 *
2549 * Returns 0 for success.
2550 */
2551int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2552 struct snd_ctl_elem_info *uinfo)
2553{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002554 struct soc_mixer_control *mc =
2555 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002556 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002557 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002558 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002559
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002560 if (!mc->platform_max)
2561 mc->platform_max = mc->max;
2562 platform_max = mc->platform_max;
2563
2564 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002565 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2566 else
2567 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2568
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002569 uinfo->count = shift == rshift ? 1 : 2;
2570 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002571 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002572 return 0;
2573}
2574EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2575
2576/**
2577 * snd_soc_get_volsw - single mixer get callback
2578 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002579 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002580 *
2581 * Callback to get the value of a single mixer control.
2582 *
2583 * Returns 0 for success.
2584 */
2585int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2586 struct snd_ctl_elem_value *ucontrol)
2587{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002588 struct soc_mixer_control *mc =
2589 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002590 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002591 unsigned int reg = mc->reg;
2592 unsigned int shift = mc->shift;
2593 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002594 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002595 unsigned int mask = (1 << fls(max)) - 1;
2596 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002597
2598 ucontrol->value.integer.value[0] =
2599 (snd_soc_read(codec, reg) >> shift) & mask;
2600 if (shift != rshift)
2601 ucontrol->value.integer.value[1] =
2602 (snd_soc_read(codec, reg) >> rshift) & mask;
2603 if (invert) {
2604 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002605 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002606 if (shift != rshift)
2607 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002608 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002609 }
2610
2611 return 0;
2612}
2613EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2614
2615/**
2616 * snd_soc_put_volsw - single mixer put callback
2617 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002618 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002619 *
2620 * Callback to set the value of a single mixer control.
2621 *
2622 * Returns 0 for success.
2623 */
2624int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2625 struct snd_ctl_elem_value *ucontrol)
2626{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002627 struct soc_mixer_control *mc =
2628 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002629 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002630 unsigned int reg = mc->reg;
2631 unsigned int shift = mc->shift;
2632 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002633 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002634 unsigned int mask = (1 << fls(max)) - 1;
2635 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002636 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002637
2638 val = (ucontrol->value.integer.value[0] & mask);
2639 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002640 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002641 val_mask = mask << shift;
2642 val = val << shift;
2643 if (shift != rshift) {
2644 val2 = (ucontrol->value.integer.value[1] & mask);
2645 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002646 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002647 val_mask |= mask << rshift;
2648 val |= val2 << rshift;
2649 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002650 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002651}
2652EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2653
2654/**
2655 * snd_soc_info_volsw_2r - double mixer info callback
2656 * @kcontrol: mixer control
2657 * @uinfo: control element information
2658 *
2659 * Callback to provide information about a double mixer control that
2660 * spans 2 codec registers.
2661 *
2662 * Returns 0 for success.
2663 */
2664int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2665 struct snd_ctl_elem_info *uinfo)
2666{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002667 struct soc_mixer_control *mc =
2668 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002669 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002670
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002671 if (!mc->platform_max)
2672 mc->platform_max = mc->max;
2673 platform_max = mc->platform_max;
2674
2675 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002676 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2677 else
2678 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2679
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002680 uinfo->count = 2;
2681 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002682 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002683 return 0;
2684}
2685EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2686
2687/**
2688 * snd_soc_get_volsw_2r - double mixer get callback
2689 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002690 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002691 *
2692 * Callback to get the value of a double mixer control that spans 2 registers.
2693 *
2694 * Returns 0 for success.
2695 */
2696int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2697 struct snd_ctl_elem_value *ucontrol)
2698{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002699 struct soc_mixer_control *mc =
2700 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002701 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002702 unsigned int reg = mc->reg;
2703 unsigned int reg2 = mc->rreg;
2704 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002705 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002706 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002707 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002708
2709 ucontrol->value.integer.value[0] =
2710 (snd_soc_read(codec, reg) >> shift) & mask;
2711 ucontrol->value.integer.value[1] =
2712 (snd_soc_read(codec, reg2) >> shift) & mask;
2713 if (invert) {
2714 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002715 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002716 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002717 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002718 }
2719
2720 return 0;
2721}
2722EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2723
2724/**
2725 * snd_soc_put_volsw_2r - double mixer set callback
2726 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002727 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002728 *
2729 * Callback to set the value of a double mixer control that spans 2 registers.
2730 *
2731 * Returns 0 for success.
2732 */
2733int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2734 struct snd_ctl_elem_value *ucontrol)
2735{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002736 struct soc_mixer_control *mc =
2737 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002738 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002739 unsigned int reg = mc->reg;
2740 unsigned int reg2 = mc->rreg;
2741 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002742 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002743 unsigned int mask = (1 << fls(max)) - 1;
2744 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002745 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002746 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002747
2748 val_mask = mask << shift;
2749 val = (ucontrol->value.integer.value[0] & mask);
2750 val2 = (ucontrol->value.integer.value[1] & mask);
2751
2752 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002753 val = max - val;
2754 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002755 }
2756
2757 val = val << shift;
2758 val2 = val2 << shift;
2759
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002760 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002761 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002762 return err;
2763
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002764 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002765 return err;
2766}
2767EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2768
Mark Browne13ac2e2008-05-28 17:58:05 +01002769/**
2770 * snd_soc_info_volsw_s8 - signed mixer info callback
2771 * @kcontrol: mixer control
2772 * @uinfo: control element information
2773 *
2774 * Callback to provide information about a signed mixer control.
2775 *
2776 * Returns 0 for success.
2777 */
2778int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2779 struct snd_ctl_elem_info *uinfo)
2780{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002781 struct soc_mixer_control *mc =
2782 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002783 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002784 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002785
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002786 if (!mc->platform_max)
2787 mc->platform_max = mc->max;
2788 platform_max = mc->platform_max;
2789
Mark Browne13ac2e2008-05-28 17:58:05 +01002790 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2791 uinfo->count = 2;
2792 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002793 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002794 return 0;
2795}
2796EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2797
2798/**
2799 * snd_soc_get_volsw_s8 - signed mixer get callback
2800 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002801 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002802 *
2803 * Callback to get the value of a signed mixer control.
2804 *
2805 * Returns 0 for success.
2806 */
2807int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2808 struct snd_ctl_elem_value *ucontrol)
2809{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002810 struct soc_mixer_control *mc =
2811 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002812 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002813 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002814 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002815 int val = snd_soc_read(codec, reg);
2816
2817 ucontrol->value.integer.value[0] =
2818 ((signed char)(val & 0xff))-min;
2819 ucontrol->value.integer.value[1] =
2820 ((signed char)((val >> 8) & 0xff))-min;
2821 return 0;
2822}
2823EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2824
2825/**
2826 * snd_soc_put_volsw_sgn - signed mixer put callback
2827 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002828 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002829 *
2830 * Callback to set the value of a signed mixer control.
2831 *
2832 * Returns 0 for success.
2833 */
2834int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2835 struct snd_ctl_elem_value *ucontrol)
2836{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002837 struct soc_mixer_control *mc =
2838 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002839 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002840 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002841 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002842 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002843
2844 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2845 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2846
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002847 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002848}
2849EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2850
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002851/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002852 * snd_soc_limit_volume - Set new limit to an existing volume control.
2853 *
2854 * @codec: where to look for the control
2855 * @name: Name of the control
2856 * @max: new maximum limit
2857 *
2858 * Return 0 for success, else error.
2859 */
2860int snd_soc_limit_volume(struct snd_soc_codec *codec,
2861 const char *name, int max)
2862{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002863 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002864 struct snd_kcontrol *kctl;
2865 struct soc_mixer_control *mc;
2866 int found = 0;
2867 int ret = -EINVAL;
2868
2869 /* Sanity check for name and max */
2870 if (unlikely(!name || max <= 0))
2871 return -EINVAL;
2872
2873 list_for_each_entry(kctl, &card->controls, list) {
2874 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2875 found = 1;
2876 break;
2877 }
2878 }
2879 if (found) {
2880 mc = (struct soc_mixer_control *)kctl->private_value;
2881 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002882 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002883 ret = 0;
2884 }
2885 }
2886 return ret;
2887}
2888EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2889
2890/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002891 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2892 * mixer info callback
2893 * @kcontrol: mixer control
2894 * @uinfo: control element information
2895 *
2896 * Returns 0 for success.
2897 */
2898int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2899 struct snd_ctl_elem_info *uinfo)
2900{
2901 struct soc_mixer_control *mc =
2902 (struct soc_mixer_control *)kcontrol->private_value;
2903 int max = mc->max;
2904 int min = mc->min;
2905
2906 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2907 uinfo->count = 2;
2908 uinfo->value.integer.min = 0;
2909 uinfo->value.integer.max = max-min;
2910
2911 return 0;
2912}
2913EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2914
2915/**
2916 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2917 * mixer get callback
2918 * @kcontrol: mixer control
2919 * @uinfo: control element information
2920 *
2921 * Returns 0 for success.
2922 */
2923int snd_soc_get_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 val = snd_soc_read(codec, mc->reg) & mask;
2932 int valr = snd_soc_read(codec, mc->rreg) & mask;
2933
Stuart Longland20630c72010-06-18 12:56:10 +10002934 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2935 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002936 return 0;
2937}
2938EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2939
2940/**
2941 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2942 * mixer put callback
2943 * @kcontrol: mixer control
2944 * @uinfo: control element information
2945 *
2946 * Returns 0 for success.
2947 */
2948int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2949 struct snd_ctl_elem_value *ucontrol)
2950{
2951 struct soc_mixer_control *mc =
2952 (struct soc_mixer_control *)kcontrol->private_value;
2953 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2954 unsigned int mask = (1<<mc->shift)-1;
2955 int min = mc->min;
2956 int ret;
2957 unsigned int val, valr, oval, ovalr;
2958
2959 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2960 val &= mask;
2961 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2962 valr &= mask;
2963
2964 oval = snd_soc_read(codec, mc->reg) & mask;
2965 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2966
2967 ret = 0;
2968 if (oval != val) {
2969 ret = snd_soc_write(codec, mc->reg, val);
2970 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002971 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002972 }
2973 if (ovalr != valr) {
2974 ret = snd_soc_write(codec, mc->rreg, valr);
2975 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002976 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002977 }
2978
2979 return 0;
2980}
2981EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2982
2983/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002984 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2985 * @dai: DAI
2986 * @clk_id: DAI specific clock ID
2987 * @freq: new clock frequency in Hz
2988 * @dir: new clock direction - input/output.
2989 *
2990 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2991 */
2992int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2993 unsigned int freq, int dir)
2994{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002995 if (dai->driver && dai->driver->ops->set_sysclk)
2996 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002997 else
2998 return -EINVAL;
2999}
3000EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3001
3002/**
3003 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3004 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003005 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003006 * @div: new clock divisor.
3007 *
3008 * Configures the clock dividers. This is used to derive the best DAI bit and
3009 * frame clocks from the system or master clock. It's best to set the DAI bit
3010 * and frame clocks as low as possible to save system power.
3011 */
3012int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3013 int div_id, int div)
3014{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003015 if (dai->driver && dai->driver->ops->set_clkdiv)
3016 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003017 else
3018 return -EINVAL;
3019}
3020EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3021
3022/**
3023 * snd_soc_dai_set_pll - configure DAI PLL.
3024 * @dai: DAI
3025 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003026 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003027 * @freq_in: PLL input clock frequency in Hz
3028 * @freq_out: requested PLL output clock frequency in Hz
3029 *
3030 * Configures and enables PLL to generate output clock based on input clock.
3031 */
Mark Brown85488032009-09-05 18:52:16 +01003032int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3033 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003034{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003035 if (dai->driver && dai->driver->ops->set_pll)
3036 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003037 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003038 else
3039 return -EINVAL;
3040}
3041EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3042
3043/**
3044 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3045 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003046 * @fmt: SND_SOC_DAIFMT_ format value.
3047 *
3048 * Configures the DAI hardware format and clocking.
3049 */
3050int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3051{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003052 if (dai->driver && dai->driver->ops->set_fmt)
3053 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003054 else
3055 return -EINVAL;
3056}
3057EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3058
3059/**
3060 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3061 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003062 * @tx_mask: bitmask representing active TX slots.
3063 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003064 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003065 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003066 *
3067 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3068 * specific.
3069 */
3070int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003071 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003072{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003073 if (dai->driver && dai->driver->ops->set_tdm_slot)
3074 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003075 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003076 else
3077 return -EINVAL;
3078}
3079EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3080
3081/**
Barry Song472df3c2009-09-12 01:16:29 +08003082 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3083 * @dai: DAI
3084 * @tx_num: how many TX channels
3085 * @tx_slot: pointer to an array which imply the TX slot number channel
3086 * 0~num-1 uses
3087 * @rx_num: how many RX channels
3088 * @rx_slot: pointer to an array which imply the RX slot number channel
3089 * 0~num-1 uses
3090 *
3091 * configure the relationship between channel number and TDM slot number.
3092 */
3093int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3094 unsigned int tx_num, unsigned int *tx_slot,
3095 unsigned int rx_num, unsigned int *rx_slot)
3096{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003097 if (dai->driver && dai->driver->ops->set_channel_map)
3098 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003099 rx_num, rx_slot);
3100 else
3101 return -EINVAL;
3102}
3103EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3104
3105/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003106 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3107 * @dai: DAI
3108 * @tristate: tristate enable
3109 *
3110 * Tristates the DAI so that others can use it.
3111 */
3112int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3113{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003114 if (dai->driver && dai->driver->ops->set_tristate)
3115 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003116 else
3117 return -EINVAL;
3118}
3119EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3120
3121/**
3122 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3123 * @dai: DAI
3124 * @mute: mute enable
3125 *
3126 * Mutes the DAI DAC.
3127 */
3128int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3129{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003130 if (dai->driver && dai->driver->ops->digital_mute)
3131 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003132 else
3133 return -EINVAL;
3134}
3135EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3136
Mark Brownc5af3a22008-11-28 13:29:45 +00003137/**
3138 * snd_soc_register_card - Register a card with the ASoC core
3139 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003140 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003141 *
3142 * Note that currently this is an internal only function: it will be
3143 * exposed to machine drivers after further backporting of ASoC v2
3144 * registration APIs.
3145 */
3146static int snd_soc_register_card(struct snd_soc_card *card)
3147{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003148 int i;
3149
Mark Brownc5af3a22008-11-28 13:29:45 +00003150 if (!card->name || !card->dev)
3151 return -EINVAL;
3152
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003153 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
3154 (card->num_links + card->num_aux_devs),
3155 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003156 if (card->rtd == NULL)
3157 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003158 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003159
3160 for (i = 0; i < card->num_links; i++)
3161 card->rtd[i].dai_link = &card->dai_link[i];
3162
Mark Brownc5af3a22008-11-28 13:29:45 +00003163 INIT_LIST_HEAD(&card->list);
3164 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003165 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003166
3167 mutex_lock(&client_mutex);
3168 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003169 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00003170 mutex_unlock(&client_mutex);
3171
3172 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3173
3174 return 0;
3175}
3176
3177/**
3178 * snd_soc_unregister_card - Unregister a card with the ASoC core
3179 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003180 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003181 *
3182 * Note that currently this is an internal only function: it will be
3183 * exposed to machine drivers after further backporting of ASoC v2
3184 * registration APIs.
3185 */
3186static int snd_soc_unregister_card(struct snd_soc_card *card)
3187{
3188 mutex_lock(&client_mutex);
3189 list_del(&card->list);
3190 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003191 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3192
3193 return 0;
3194}
3195
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003196/*
3197 * Simplify DAI link configuration by removing ".-1" from device names
3198 * and sanitizing names.
3199 */
3200static inline char *fmt_single_name(struct device *dev, int *id)
3201{
3202 char *found, name[NAME_SIZE];
3203 int id1, id2;
3204
3205 if (dev_name(dev) == NULL)
3206 return NULL;
3207
3208 strncpy(name, dev_name(dev), NAME_SIZE);
3209
3210 /* are we a "%s.%d" name (platform and SPI components) */
3211 found = strstr(name, dev->driver->name);
3212 if (found) {
3213 /* get ID */
3214 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3215
3216 /* discard ID from name if ID == -1 */
3217 if (*id == -1)
3218 found[strlen(dev->driver->name)] = '\0';
3219 }
3220
3221 } else {
3222 /* I2C component devices are named "bus-addr" */
3223 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3224 char tmp[NAME_SIZE];
3225
3226 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003227 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003228
3229 /* sanitize component name for DAI link creation */
3230 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3231 strncpy(name, tmp, NAME_SIZE);
3232 } else
3233 *id = 0;
3234 }
3235
3236 return kstrdup(name, GFP_KERNEL);
3237}
3238
3239/*
3240 * Simplify DAI link naming for single devices with multiple DAIs by removing
3241 * any ".-1" and using the DAI name (instead of device name).
3242 */
3243static inline char *fmt_multiple_name(struct device *dev,
3244 struct snd_soc_dai_driver *dai_drv)
3245{
3246 if (dai_drv->name == NULL) {
3247 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3248 dev_name(dev));
3249 return NULL;
3250 }
3251
3252 return kstrdup(dai_drv->name, GFP_KERNEL);
3253}
3254
Mark Brown91151712008-11-30 23:31:24 +00003255/**
3256 * snd_soc_register_dai - Register a DAI with the ASoC core
3257 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003258 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003259 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003260int snd_soc_register_dai(struct device *dev,
3261 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003262{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003263 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003264
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003265 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003266
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003267 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3268 if (dai == NULL)
3269 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003270
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003271 /* create DAI component name */
3272 dai->name = fmt_single_name(dev, &dai->id);
3273 if (dai->name == NULL) {
3274 kfree(dai);
3275 return -ENOMEM;
3276 }
3277
3278 dai->dev = dev;
3279 dai->driver = dai_drv;
3280 if (!dai->driver->ops)
3281 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003282
3283 mutex_lock(&client_mutex);
3284 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003285 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003286 mutex_unlock(&client_mutex);
3287
3288 pr_debug("Registered DAI '%s'\n", dai->name);
3289
3290 return 0;
3291}
3292EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3293
3294/**
3295 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3296 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003297 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003298 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003299void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003300{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003301 struct snd_soc_dai *dai;
3302
3303 list_for_each_entry(dai, &dai_list, list) {
3304 if (dev == dai->dev)
3305 goto found;
3306 }
3307 return;
3308
3309found:
Mark Brown91151712008-11-30 23:31:24 +00003310 mutex_lock(&client_mutex);
3311 list_del(&dai->list);
3312 mutex_unlock(&client_mutex);
3313
3314 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003315 kfree(dai->name);
3316 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003317}
3318EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3319
3320/**
3321 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3322 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003323 * @dai: Array of DAIs to register
3324 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003325 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003326int snd_soc_register_dais(struct device *dev,
3327 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003328{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003329 struct snd_soc_dai *dai;
3330 int i, ret = 0;
3331
Liam Girdwood720ffa42010-08-18 00:30:30 +01003332 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003333
3334 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003335
3336 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003337 if (dai == NULL) {
3338 ret = -ENOMEM;
3339 goto err;
3340 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003341
3342 /* create DAI component name */
3343 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3344 if (dai->name == NULL) {
3345 kfree(dai);
3346 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003347 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003348 }
3349
3350 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003351 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003352 if (dai->driver->id)
3353 dai->id = dai->driver->id;
3354 else
3355 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003356 if (!dai->driver->ops)
3357 dai->driver->ops = &null_dai_ops;
3358
3359 mutex_lock(&client_mutex);
3360 list_add(&dai->list, &dai_list);
3361 mutex_unlock(&client_mutex);
3362
3363 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003364 }
3365
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003366 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003367 return 0;
3368
3369err:
3370 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003371 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003372
3373 return ret;
3374}
3375EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3376
3377/**
3378 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3379 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003380 * @dai: Array of DAIs to unregister
3381 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003382 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003383void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003384{
3385 int i;
3386
3387 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003388 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003389}
3390EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3391
Mark Brown12a48a8c2008-12-03 19:40:30 +00003392/**
3393 * snd_soc_register_platform - Register a platform with the ASoC core
3394 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003395 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003396 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003397int snd_soc_register_platform(struct device *dev,
3398 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003399{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003400 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003401
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003402 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3403
3404 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3405 if (platform == NULL)
3406 return -ENOMEM;
3407
3408 /* create platform component name */
3409 platform->name = fmt_single_name(dev, &platform->id);
3410 if (platform->name == NULL) {
3411 kfree(platform);
3412 return -ENOMEM;
3413 }
3414
3415 platform->dev = dev;
3416 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003417
3418 mutex_lock(&client_mutex);
3419 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003420 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003421 mutex_unlock(&client_mutex);
3422
3423 pr_debug("Registered platform '%s'\n", platform->name);
3424
3425 return 0;
3426}
3427EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3428
3429/**
3430 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3431 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003432 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003433 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003434void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003435{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003436 struct snd_soc_platform *platform;
3437
3438 list_for_each_entry(platform, &platform_list, list) {
3439 if (dev == platform->dev)
3440 goto found;
3441 }
3442 return;
3443
3444found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003445 mutex_lock(&client_mutex);
3446 list_del(&platform->list);
3447 mutex_unlock(&client_mutex);
3448
3449 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003450 kfree(platform->name);
3451 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003452}
3453EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3454
Mark Brown151ab222009-05-09 16:22:58 +01003455static u64 codec_format_map[] = {
3456 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3457 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3458 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3459 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3460 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3461 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3462 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3463 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3464 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3465 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3466 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3467 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3468 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3469 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3470 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3471 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3472};
3473
3474/* Fix up the DAI formats for endianness: codecs don't actually see
3475 * the endianness of the data but we're using the CPU format
3476 * definitions which do need to include endianness so we ensure that
3477 * codec DAIs always have both big and little endian variants set.
3478 */
3479static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3480{
3481 int i;
3482
3483 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3484 if (stream->formats & codec_format_map[i])
3485 stream->formats |= codec_format_map[i];
3486}
3487
Mark Brown0d0cf002008-12-10 14:32:45 +00003488/**
3489 * snd_soc_register_codec - Register a codec with the ASoC core
3490 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003491 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003492 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003493int snd_soc_register_codec(struct device *dev,
3494 struct snd_soc_codec_driver *codec_drv,
3495 struct snd_soc_dai_driver *dai_drv, int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003496{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003497 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003498 struct snd_soc_codec *codec;
3499 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003500
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003501 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003502
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003503 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3504 if (codec == NULL)
3505 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003506
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003507 /* create CODEC component name */
3508 codec->name = fmt_single_name(dev, &codec->id);
3509 if (codec->name == NULL) {
3510 kfree(codec);
3511 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003512 }
3513
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003514 if (codec_drv->compress_type)
3515 codec->compress_type = codec_drv->compress_type;
3516 else
3517 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3518
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003519 INIT_LIST_HEAD(&codec->dapm.widgets);
3520 INIT_LIST_HEAD(&codec->dapm.paths);
Mark Brownc3acec22010-12-02 16:15:29 +00003521 codec->write = codec_drv->write;
3522 codec->read = codec_drv->read;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003523 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3524 codec->dapm.dev = dev;
3525 codec->dapm.codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003526 codec->dev = dev;
3527 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003528 codec->num_dai = num_dai;
3529 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003530
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003531 /* allocate CODEC register cache */
3532 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003533 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3534 /* it is necessary to make a copy of the default register cache
3535 * because in the case of using a compression type that requires
3536 * the default register cache to be marked as __devinitconst the
3537 * kernel might have freed the array by the time we initialize
3538 * the cache.
3539 */
3540 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3541 reg_size, GFP_KERNEL);
3542 if (!codec->reg_def_copy) {
3543 ret = -ENOMEM;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003544 goto fail;
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003545 }
3546 }
3547
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003548 for (i = 0; i < num_dai; i++) {
3549 fixup_codec_formats(&dai_drv[i].playback);
3550 fixup_codec_formats(&dai_drv[i].capture);
3551 }
3552
Mark Brown26b01cc2010-08-18 20:20:55 +01003553 /* register any DAIs */
3554 if (num_dai) {
3555 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3556 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003557 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003558 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003559
Mark Brown0d0cf002008-12-10 14:32:45 +00003560 mutex_lock(&client_mutex);
3561 list_add(&codec->list, &codec_list);
3562 snd_soc_instantiate_cards();
3563 mutex_unlock(&client_mutex);
3564
3565 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003566 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003567
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003568fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003569 kfree(codec->reg_def_copy);
3570 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003571 kfree(codec->name);
3572 kfree(codec);
3573 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003574}
3575EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3576
3577/**
3578 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3579 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003580 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003581 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003582void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003583{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003584 struct snd_soc_codec *codec;
3585 int i;
3586
3587 list_for_each_entry(codec, &codec_list, list) {
3588 if (dev == codec->dev)
3589 goto found;
3590 }
3591 return;
3592
3593found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003594 if (codec->num_dai)
3595 for (i = 0; i < codec->num_dai; i++)
3596 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003597
Mark Brown0d0cf002008-12-10 14:32:45 +00003598 mutex_lock(&client_mutex);
3599 list_del(&codec->list);
3600 mutex_unlock(&client_mutex);
3601
3602 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003603
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003604 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003605 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003606 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003607 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003608}
3609EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3610
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003611static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003612{
Mark Brown384c89e2008-12-03 17:34:03 +00003613#ifdef CONFIG_DEBUG_FS
3614 debugfs_root = debugfs_create_dir("asoc", NULL);
3615 if (IS_ERR(debugfs_root) || !debugfs_root) {
3616 printk(KERN_WARNING
3617 "ASoC: Failed to create debugfs directory\n");
3618 debugfs_root = NULL;
3619 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003620
3621 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3622 &codec_list_fops))
3623 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3624
Mark Brownf3208782010-09-15 18:19:07 +01003625 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3626 &dai_list_fops))
3627 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003628
3629 if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
3630 &platform_list_fops))
3631 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003632#endif
3633
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003634 return platform_driver_register(&soc_driver);
3635}
Mark Brown4abe8e12010-10-12 17:41:03 +01003636module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003637
Mark Brown7d8c16a2008-11-30 22:11:24 +00003638static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003639{
Mark Brown384c89e2008-12-03 17:34:03 +00003640#ifdef CONFIG_DEBUG_FS
3641 debugfs_remove_recursive(debugfs_root);
3642#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003643 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003644}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003645module_exit(snd_soc_exit);
3646
3647/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003648MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003649MODULE_DESCRIPTION("ALSA SoC Core");
3650MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003651MODULE_ALIAS("platform:soc-audio");