blob: b54ea9a0a1db4af6d1b66254416af147efb9a446 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Marek Vasut474828a2009-07-22 13:01:03 +020034#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020035#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
39#include <sound/soc-dapm.h>
40#include <sound/initval.h>
41
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000042#define NAME_SIZE 32
43
Frank Mandarinodb2a4162006-10-06 18:31:09 +020044static DEFINE_MUTEX(pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020045static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
46
Mark Brown384c89e2008-12-03 17:34:03 +000047#ifdef CONFIG_DEBUG_FS
48static struct dentry *debugfs_root;
49#endif
50
Mark Brownc5af3a22008-11-28 13:29:45 +000051static DEFINE_MUTEX(client_mutex);
52static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000053static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000054static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000055static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000056
57static int snd_soc_register_card(struct snd_soc_card *card);
58static int snd_soc_unregister_card(struct snd_soc_card *card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000059static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
Mark Brownc5af3a22008-11-28 13:29:45 +000060
Frank Mandarinodb2a4162006-10-06 18:31:09 +020061/*
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
65 */
66static int pmdown_time = 5000;
67module_param(pmdown_time, int, 0);
68MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69
Mark Brown2624d5f2009-11-03 21:56:13 +000070/* codec register dump */
71static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
72{
Mark Brown5164d742010-07-14 16:14:33 +010073 int ret, i, step = 1, count = 0;
Mark Brown2624d5f2009-11-03 21:56:13 +000074
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000075 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +000076 return 0;
77
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000078 if (codec->driver->reg_cache_step)
79 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +000080
81 count += sprintf(buf, "%s registers\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000082 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
83 if (codec->driver->readable_register && !codec->driver->readable_register(i))
Mark Brown2624d5f2009-11-03 21:56:13 +000084 continue;
85
86 count += sprintf(buf + count, "%2x: ", i);
87 if (count >= PAGE_SIZE - 1)
88 break;
89
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000090 if (codec->driver->display_register) {
91 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +000092 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +010093 } else {
94 /* If the read fails it's almost certainly due to
95 * the register being volatile and the device being
96 * powered off.
97 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000098 ret = codec->driver->read(codec, i);
Mark Brown5164d742010-07-14 16:14:33 +010099 if (ret >= 0)
100 count += snprintf(buf + count,
101 PAGE_SIZE - count,
102 "%4x", ret);
103 else
104 count += snprintf(buf + count,
105 PAGE_SIZE - count,
106 "<no data: %d>", ret);
107 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000108
109 if (count >= PAGE_SIZE - 1)
110 break;
111
112 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
113 if (count >= PAGE_SIZE - 1)
114 break;
115 }
116
117 /* Truncate count; min() would cause a warning */
118 if (count >= PAGE_SIZE)
119 count = PAGE_SIZE - 1;
120
121 return count;
122}
123static ssize_t codec_reg_show(struct device *dev,
124 struct device_attribute *attr, char *buf)
125{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000126 struct snd_soc_pcm_runtime *rtd =
127 container_of(dev, struct snd_soc_pcm_runtime, dev);
128
129 return soc_codec_reg_show(rtd->codec, buf);
Mark Brown2624d5f2009-11-03 21:56:13 +0000130}
131
132static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
133
Mark Browndbe21402010-02-12 11:37:24 +0000134static ssize_t pmdown_time_show(struct device *dev,
135 struct device_attribute *attr, char *buf)
136{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000137 struct snd_soc_pcm_runtime *rtd =
138 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Browndbe21402010-02-12 11:37:24 +0000139
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000140 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000141}
142
143static ssize_t pmdown_time_set(struct device *dev,
144 struct device_attribute *attr,
145 const char *buf, size_t count)
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);
Mark Brownc593b522010-10-27 20:11:17 -0700149 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000150
Mark Brownc593b522010-10-27 20:11:17 -0700151 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
152 if (ret)
153 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000154
155 return count;
156}
157
158static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
159
Mark Brown2624d5f2009-11-03 21:56:13 +0000160#ifdef CONFIG_DEBUG_FS
161static int codec_reg_open_file(struct inode *inode, struct file *file)
162{
163 file->private_data = inode->i_private;
164 return 0;
165}
166
167static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
168 size_t count, loff_t *ppos)
169{
170 ssize_t ret;
171 struct snd_soc_codec *codec = file->private_data;
172 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
173 if (!buf)
174 return -ENOMEM;
175 ret = soc_codec_reg_show(codec, buf);
176 if (ret >= 0)
177 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
178 kfree(buf);
179 return ret;
180}
181
182static ssize_t codec_reg_write_file(struct file *file,
183 const char __user *user_buf, size_t count, loff_t *ppos)
184{
185 char buf[32];
186 int buf_size;
187 char *start = buf;
188 unsigned long reg, value;
189 int step = 1;
190 struct snd_soc_codec *codec = file->private_data;
191
192 buf_size = min(count, (sizeof(buf)-1));
193 if (copy_from_user(buf, user_buf, buf_size))
194 return -EFAULT;
195 buf[buf_size] = 0;
196
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000197 if (codec->driver->reg_cache_step)
198 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000199
200 while (*start == ' ')
201 start++;
202 reg = simple_strtoul(start, &start, 16);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000203 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
Mark Brown2624d5f2009-11-03 21:56:13 +0000204 return -EINVAL;
205 while (*start == ' ')
206 start++;
207 if (strict_strtoul(start, 16, &value))
208 return -EINVAL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000209 codec->driver->write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000210 return buf_size;
211}
212
213static const struct file_operations codec_reg_fops = {
214 .open = codec_reg_open_file,
215 .read = codec_reg_read_file,
216 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200217 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000218};
219
220static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
221{
Mark Brown6ba6c9c2010-08-12 15:49:52 +0100222 codec->debugfs_codec_root = debugfs_create_dir(codec->name ,
Mark Brown2624d5f2009-11-03 21:56:13 +0000223 debugfs_root);
224 if (!codec->debugfs_codec_root) {
225 printk(KERN_WARNING
226 "ASoC: Failed to create codec debugfs directory\n");
227 return;
228 }
229
230 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
231 codec->debugfs_codec_root,
232 codec, &codec_reg_fops);
233 if (!codec->debugfs_reg)
234 printk(KERN_WARNING
235 "ASoC: Failed to create codec register debugfs file\n");
236
Axel Lin708fafb2010-08-27 10:34:27 +0800237 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
Mark Brown2624d5f2009-11-03 21:56:13 +0000238 codec->debugfs_codec_root,
239 &codec->pop_time);
240 if (!codec->debugfs_pop_time)
241 printk(KERN_WARNING
242 "Failed to create pop time debugfs file\n");
243
244 codec->debugfs_dapm = debugfs_create_dir("dapm",
245 codec->debugfs_codec_root);
246 if (!codec->debugfs_dapm)
247 printk(KERN_WARNING
248 "Failed to create DAPM debugfs directory\n");
249
250 snd_soc_dapm_debugfs_init(codec);
251}
252
253static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
254{
255 debugfs_remove_recursive(codec->debugfs_codec_root);
256}
257
Mark Brownc3c5a192010-09-15 18:15:14 +0100258static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
259 size_t count, loff_t *ppos)
260{
261 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100262 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100263 struct snd_soc_codec *codec;
264
265 if (!buf)
266 return -ENOMEM;
267
Mark Brown2b194f9d2010-10-13 10:52:16 +0100268 list_for_each_entry(codec, &codec_list, list) {
269 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
270 codec->name);
271 if (len >= 0)
272 ret += len;
273 if (ret > PAGE_SIZE) {
274 ret = PAGE_SIZE;
275 break;
276 }
277 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100278
279 if (ret >= 0)
280 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
281
282 kfree(buf);
283
284 return ret;
285}
286
287static const struct file_operations codec_list_fops = {
288 .read = codec_list_read_file,
289 .llseek = default_llseek,/* read accesses f_pos */
290};
291
Mark Brownf3208782010-09-15 18:19:07 +0100292static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
293 size_t count, loff_t *ppos)
294{
295 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100296 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100297 struct snd_soc_dai *dai;
298
299 if (!buf)
300 return -ENOMEM;
301
Mark Brown2b194f9d2010-10-13 10:52:16 +0100302 list_for_each_entry(dai, &dai_list, list) {
303 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
304 if (len >= 0)
305 ret += len;
306 if (ret > PAGE_SIZE) {
307 ret = PAGE_SIZE;
308 break;
309 }
310 }
Mark Brownf3208782010-09-15 18:19:07 +0100311
Mark Brown2b194f9d2010-10-13 10:52:16 +0100312 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100313
314 kfree(buf);
315
316 return ret;
317}
318
319static const struct file_operations dai_list_fops = {
320 .read = dai_list_read_file,
321 .llseek = default_llseek,/* read accesses f_pos */
322};
323
Mark Brown19c7ac22010-09-15 18:22:40 +0100324static ssize_t platform_list_read_file(struct file *file,
325 char __user *user_buf,
326 size_t count, loff_t *ppos)
327{
328 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100329 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100330 struct snd_soc_platform *platform;
331
332 if (!buf)
333 return -ENOMEM;
334
Mark Brown2b194f9d2010-10-13 10:52:16 +0100335 list_for_each_entry(platform, &platform_list, list) {
336 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
337 platform->name);
338 if (len >= 0)
339 ret += len;
340 if (ret > PAGE_SIZE) {
341 ret = PAGE_SIZE;
342 break;
343 }
344 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100345
Mark Brown2b194f9d2010-10-13 10:52:16 +0100346 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100347
348 kfree(buf);
349
350 return ret;
351}
352
353static const struct file_operations platform_list_fops = {
354 .read = platform_list_read_file,
355 .llseek = default_llseek,/* read accesses f_pos */
356};
357
Mark Brown2624d5f2009-11-03 21:56:13 +0000358#else
359
360static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
361{
362}
363
364static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
365{
366}
367#endif
368
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200369#ifdef CONFIG_SND_SOC_AC97_BUS
370/* unregister ac97 codec */
371static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
372{
373 if (codec->ac97->dev.bus)
374 device_unregister(&codec->ac97->dev);
375 return 0;
376}
377
378/* stop no dev release warning */
379static void soc_ac97_device_release(struct device *dev){}
380
381/* register ac97 codec to bus */
382static int soc_ac97_dev_register(struct snd_soc_codec *codec)
383{
384 int err;
385
386 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100387 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200388 codec->ac97->dev.release = soc_ac97_device_release;
389
Kay Sieversbb072bf2008-11-02 03:50:35 +0100390 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000391 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200392 err = device_register(&codec->ac97->dev);
393 if (err < 0) {
394 snd_printk(KERN_ERR "Can't register ac97 bus\n");
395 codec->ac97->dev.bus = NULL;
396 return err;
397 }
398 return 0;
399}
400#endif
401
Mark Brown06f409d2009-04-07 18:10:13 +0100402static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
403{
404 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000405 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
406 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100407 int ret;
408
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000409 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
410 rtd->dai_link->symmetric_rates) {
411 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
412 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100413
414 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
415 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000416 rtd->rate,
417 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100418 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000419 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100420 "Unable to apply rate symmetry constraint: %d\n", ret);
421 return ret;
422 }
423 }
424
425 return 0;
426}
427
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200428/*
429 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
430 * then initialized and any private data can be allocated. This also calls
431 * startup for the cpu DAI, platform, machine and codec DAI.
432 */
433static int soc_pcm_open(struct snd_pcm_substream *substream)
434{
435 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200436 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000437 struct snd_soc_platform *platform = rtd->platform;
438 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
439 struct snd_soc_dai *codec_dai = rtd->codec_dai;
440 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
441 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200442 int ret = 0;
443
444 mutex_lock(&pcm_mutex);
445
446 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000447 if (cpu_dai->driver->ops->startup) {
448 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200449 if (ret < 0) {
450 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100451 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200452 goto out;
453 }
454 }
455
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000456 if (platform->driver->ops->open) {
457 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200458 if (ret < 0) {
459 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
460 goto platform_err;
461 }
462 }
463
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000464 if (codec_dai->driver->ops->startup) {
465 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100466 if (ret < 0) {
467 printk(KERN_ERR "asoc: can't open codec %s\n",
468 codec_dai->name);
469 goto codec_dai_err;
470 }
471 }
472
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000473 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
474 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200475 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000476 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200477 goto machine_err;
478 }
479 }
480
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200481 /* Check that the codec and cpu DAI's are compatible */
482 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
483 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000484 max(codec_dai_drv->playback.rate_min,
485 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200486 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000487 min(codec_dai_drv->playback.rate_max,
488 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200489 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000490 max(codec_dai_drv->playback.channels_min,
491 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200492 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000493 min(codec_dai_drv->playback.channels_max,
494 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100495 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000496 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100497 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000498 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
499 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900500 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501 runtime->hw.rates |= cpu_dai_drv->playback.rates;
502 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900503 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000504 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200505 } else {
506 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000507 max(codec_dai_drv->capture.rate_min,
508 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200509 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000510 min(codec_dai_drv->capture.rate_max,
511 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200512 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000513 max(codec_dai_drv->capture.channels_min,
514 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200515 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000516 min(codec_dai_drv->capture.channels_max,
517 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100518 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100520 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000521 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
522 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900523 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000524 runtime->hw.rates |= cpu_dai_drv->capture.rates;
525 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900526 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000527 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200528 }
529
530 snd_pcm_limit_hw_rates(runtime);
531 if (!runtime->hw.rates) {
532 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100533 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900534 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200535 }
536 if (!runtime->hw.formats) {
537 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100538 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900539 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200540 }
541 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
542 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000543 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900544 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200545 }
546
Mark Brown06f409d2009-04-07 18:10:13 +0100547 /* Symmetry only applies if we've already got an active stream. */
548 if (cpu_dai->active || codec_dai->active) {
549 ret = soc_pcm_apply_symmetry(substream);
550 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900551 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100552 }
553
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 pr_debug("asoc: %s <-> %s info:\n",
555 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100556 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
557 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
558 runtime->hw.channels_max);
559 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
560 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200561
Jassi Brar14dc5732010-02-26 09:12:32 +0900562 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000563 cpu_dai->playback_active++;
564 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900565 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000566 cpu_dai->capture_active++;
567 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900568 }
569 cpu_dai->active++;
570 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200572 mutex_unlock(&pcm_mutex);
573 return 0;
574
Jassi Brarbb1c0472010-02-25 11:24:53 +0900575config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000576 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
577 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200578
Jassi Brarbb1c0472010-02-25 11:24:53 +0900579machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 if (codec_dai->driver->ops->shutdown)
581 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900582
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100583codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 if (platform->driver->ops->close)
585 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200586
587platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588 if (cpu_dai->driver->ops->shutdown)
589 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200590out:
591 mutex_unlock(&pcm_mutex);
592 return ret;
593}
594
595/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200596 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200597 * This is to ensure there are no pops or clicks in between any music tracks
598 * due to DAPM power cycling.
599 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100600static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200601{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000602 struct snd_soc_pcm_runtime *rtd =
603 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
604 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200605
606 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200607
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
609 codec_dai->driver->playback.stream_name,
610 codec_dai->playback_active ? "active" : "inactive",
611 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200612
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 /* are we waiting on this codec DAI stream */
614 if (codec_dai->pop_wait == 1) {
615 codec_dai->pop_wait = 0;
616 snd_soc_dapm_stream_event(rtd,
617 codec_dai->driver->playback.stream_name,
618 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200619 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200621 mutex_unlock(&pcm_mutex);
622}
623
624/*
625 * Called by ALSA when a PCM substream is closed. Private data can be
626 * freed here. The cpu DAI, codec DAI, machine and platform are also
627 * shutdown.
628 */
629static int soc_codec_close(struct snd_pcm_substream *substream)
630{
631 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000632 struct snd_soc_platform *platform = rtd->platform;
633 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
634 struct snd_soc_dai *codec_dai = rtd->codec_dai;
635 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200636
637 mutex_lock(&pcm_mutex);
638
Jassi Brar14dc5732010-02-26 09:12:32 +0900639 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640 cpu_dai->playback_active--;
641 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900642 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000643 cpu_dai->capture_active--;
644 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200645 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900646
647 cpu_dai->active--;
648 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200649 codec->active--;
650
Mark Brown6010b2d2008-09-06 18:33:24 +0100651 /* Muting the DAC suppresses artifacts caused during digital
652 * shutdown, for example from stopping clocks.
653 */
654 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
655 snd_soc_dai_digital_mute(codec_dai, 1);
656
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000657 if (cpu_dai->driver->ops->shutdown)
658 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200659
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660 if (codec_dai->driver->ops->shutdown)
661 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200662
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000663 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
664 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200665
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000666 if (platform->driver->ops->close)
667 platform->driver->ops->close(substream);
668 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200669
670 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
671 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100672 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 schedule_delayed_work(&rtd->delayed_work,
674 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200675 } else {
676 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000677 snd_soc_dapm_stream_event(rtd,
678 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100679 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200680 }
681
682 mutex_unlock(&pcm_mutex);
683 return 0;
684}
685
686/*
687 * Called by ALSA when the PCM substream is prepared, can set format, sample
688 * rate, etc. This function is non atomic and can be called multiple times,
689 * it can refer to the runtime info.
690 */
691static int soc_pcm_prepare(struct snd_pcm_substream *substream)
692{
693 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000694 struct snd_soc_platform *platform = rtd->platform;
695 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
696 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200697 int ret = 0;
698
699 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100700
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000701 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
702 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100703 if (ret < 0) {
704 printk(KERN_ERR "asoc: machine prepare error\n");
705 goto out;
706 }
707 }
708
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000709 if (platform->driver->ops->prepare) {
710 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200711 if (ret < 0) {
712 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200713 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200714 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200715 }
716
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 if (codec_dai->driver->ops->prepare) {
718 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200719 if (ret < 0) {
720 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200721 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200722 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723 }
724
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000725 if (cpu_dai->driver->ops->prepare) {
726 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100727 if (ret < 0) {
728 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
729 goto out;
730 }
731 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200732
Mark Brownd45f6212008-10-14 13:58:36 +0100733 /* cancel any delayed stream shutdown that is pending */
734 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
735 codec_dai->pop_wait) {
736 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000737 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100738 }
739
Mark Brown452c5ea2009-05-17 21:41:23 +0100740 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000741 snd_soc_dapm_stream_event(rtd,
742 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100743 SND_SOC_DAPM_STREAM_START);
744 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000745 snd_soc_dapm_stream_event(rtd,
746 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100747 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100748
Mark Brown452c5ea2009-05-17 21:41:23 +0100749 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750
751out:
752 mutex_unlock(&pcm_mutex);
753 return ret;
754}
755
756/*
757 * Called by ALSA when the hardware params are set by application. This
758 * function can also be called multiple times and can allocate buffers
759 * (using snd_pcm_lib_* ). It's non-atomic.
760 */
761static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
762 struct snd_pcm_hw_params *params)
763{
764 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000765 struct snd_soc_platform *platform = rtd->platform;
766 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
767 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200768 int ret = 0;
769
770 mutex_lock(&pcm_mutex);
771
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000772 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
773 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200774 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100775 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200776 goto out;
777 }
778 }
779
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000780 if (codec_dai->driver->ops->hw_params) {
781 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100782 if (ret < 0) {
783 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
784 codec_dai->name);
785 goto codec_err;
786 }
787 }
788
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000789 if (cpu_dai->driver->ops->hw_params) {
790 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200792 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100793 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200794 goto interface_err;
795 }
796 }
797
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000798 if (platform->driver->ops->hw_params) {
799 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200800 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200801 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200802 platform->name);
803 goto platform_err;
804 }
805 }
806
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000807 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100808
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200809out:
810 mutex_unlock(&pcm_mutex);
811 return ret;
812
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200813platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000814 if (cpu_dai->driver->ops->hw_free)
815 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200816
817interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000818 if (codec_dai->driver->ops->hw_free)
819 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100820
821codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000822 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
823 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200824
825 mutex_unlock(&pcm_mutex);
826 return ret;
827}
828
829/*
830 * Free's resources allocated by hw_params, can be called multiple times
831 */
832static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
833{
834 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000835 struct snd_soc_platform *platform = rtd->platform;
836 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
837 struct snd_soc_dai *codec_dai = rtd->codec_dai;
838 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200839
840 mutex_lock(&pcm_mutex);
841
842 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100843 if (!codec->active)
844 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200845
846 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000847 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
848 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849
850 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000851 if (platform->driver->ops->hw_free)
852 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200853
854 /* now free hw params for the DAI's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000855 if (codec_dai->driver->ops->hw_free)
856 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200857
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000858 if (cpu_dai->driver->ops->hw_free)
859 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200860
861 mutex_unlock(&pcm_mutex);
862 return 0;
863}
864
865static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
866{
867 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000868 struct snd_soc_platform *platform = rtd->platform;
869 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
870 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200871 int ret;
872
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000873 if (codec_dai->driver->ops->trigger) {
874 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200875 if (ret < 0)
876 return ret;
877 }
878
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000879 if (platform->driver->ops->trigger) {
880 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200881 if (ret < 0)
882 return ret;
883 }
884
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000885 if (cpu_dai->driver->ops->trigger) {
886 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200887 if (ret < 0)
888 return ret;
889 }
890 return 0;
891}
892
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200893/*
894 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200895 * If cpu_dai, codec_dai, platform driver has the delay callback, than
896 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200897 */
898static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
899{
900 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000901 struct snd_soc_platform *platform = rtd->platform;
902 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
903 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200904 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200905 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200906 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200907
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000908 if (platform->driver->ops->pointer)
909 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200910
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000911 if (cpu_dai->driver->ops->delay)
912 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200913
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000914 if (codec_dai->driver->ops->delay)
915 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200916
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000917 if (platform->driver->delay)
918 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200919
920 runtime->delay = delay;
921
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200922 return offset;
923}
924
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200925/* ASoC PCM operations */
926static struct snd_pcm_ops soc_pcm_ops = {
927 .open = soc_pcm_open,
928 .close = soc_codec_close,
929 .hw_params = soc_pcm_hw_params,
930 .hw_free = soc_pcm_hw_free,
931 .prepare = soc_pcm_prepare,
932 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200933 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200934};
935
936#ifdef CONFIG_PM
937/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100938static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200939{
Mark Brown416356f2009-06-30 19:05:15 +0100940 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000941 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200942 int i;
943
Daniel Macke3509ff2009-06-03 17:44:49 +0200944 /* If the initialization of this soc device failed, there is no codec
945 * associated with it. Just bail out in this case.
946 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000947 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200948 return 0;
949
Andy Green6ed25972008-06-13 16:24:05 +0100950 /* Due to the resume being scheduled into a workqueue we could
951 * suspend before that's finished - wait for it to complete.
952 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000953 snd_power_lock(card->snd_card);
954 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
955 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100956
957 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000958 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100959
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200960 /* mute any active DAC's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000961 for (i = 0; i < card->num_rtd; i++) {
962 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
963 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100964
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000965 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100966 continue;
967
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000968 if (drv->ops->digital_mute && dai->playback_active)
969 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200970 }
971
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100972 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000973 for (i = 0; i < card->num_rtd; i++) {
974 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100975 continue;
976
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000977 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100978 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100979
Mark Brown87506542008-11-18 20:50:34 +0000980 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100981 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200982
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000983 for (i = 0; i < card->num_rtd; i++) {
984 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
985 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100986
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000987 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100988 continue;
989
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000990 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
991 cpu_dai->driver->suspend(cpu_dai);
992 if (platform->driver->suspend && !platform->suspended) {
993 platform->driver->suspend(cpu_dai);
994 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100995 }
996 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200997
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000998 /* close any waiting streams and save state */
999 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +01001000 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001001 card->rtd[i].codec->suspend_bias_level = card->rtd[i].codec->bias_level;
1002 }
Mark Brown3efab7d2010-05-09 13:25:43 +01001003
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001004 for (i = 0; i < card->num_rtd; i++) {
1005 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1006
1007 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001008 continue;
1009
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001010 if (driver->playback.stream_name != NULL)
1011 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1012 SND_SOC_DAPM_STREAM_SUSPEND);
1013
1014 if (driver->capture.stream_name != NULL)
1015 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1016 SND_SOC_DAPM_STREAM_SUSPEND);
1017 }
1018
1019 /* suspend all CODECs */
1020 for (i = 0; i < card->num_rtd; i++) {
1021 struct snd_soc_codec *codec = card->rtd[i].codec;
1022 /* If there are paths active then the CODEC will be held with
1023 * bias _ON and should not be suspended. */
1024 if (!codec->suspended && codec->driver->suspend) {
1025 switch (codec->bias_level) {
1026 case SND_SOC_BIAS_STANDBY:
1027 case SND_SOC_BIAS_OFF:
1028 codec->driver->suspend(codec, PMSG_SUSPEND);
1029 codec->suspended = 1;
1030 break;
1031 default:
1032 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1033 break;
1034 }
1035 }
1036 }
1037
1038 for (i = 0; i < card->num_rtd; i++) {
1039 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1040
1041 if (card->rtd[i].dai_link->ignore_suspend)
1042 continue;
1043
1044 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1045 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001046 }
1047
Mark Brown87506542008-11-18 20:50:34 +00001048 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +01001049 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001050
1051 return 0;
1052}
1053
Andy Green6ed25972008-06-13 16:24:05 +01001054/* deferred resume work, so resume can complete before we finished
1055 * setting our codec back up, which can be very slow on I2C
1056 */
1057static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001058{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001059 struct snd_soc_card *card =
1060 container_of(work, struct snd_soc_card, deferred_resume_work);
1061 struct platform_device *pdev = to_platform_device(card->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001062 int i;
1063
Andy Green6ed25972008-06-13 16:24:05 +01001064 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1065 * so userspace apps are blocked from touching us
1066 */
1067
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001068 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001069
Mark Brown99497882010-05-07 20:24:05 +01001070 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001071 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +01001072
Mark Brown87506542008-11-18 20:50:34 +00001073 if (card->resume_pre)
1074 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001075
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001076 /* resume AC97 DAIs */
1077 for (i = 0; i < card->num_rtd; i++) {
1078 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001079
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001080 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001081 continue;
1082
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001083 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1084 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001085 }
1086
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001087 for (i = 0; i < card->num_rtd; i++) {
1088 struct snd_soc_codec *codec = card->rtd[i].codec;
1089 /* If the CODEC was idle over suspend then it will have been
1090 * left with bias OFF or STANDBY and suspended so we must now
1091 * resume. Otherwise the suspend was suppressed.
1092 */
1093 if (codec->driver->resume && codec->suspended) {
1094 switch (codec->bias_level) {
1095 case SND_SOC_BIAS_STANDBY:
1096 case SND_SOC_BIAS_OFF:
1097 codec->driver->resume(codec);
1098 codec->suspended = 0;
1099 break;
1100 default:
1101 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1102 break;
1103 }
Mark Brown1547aba2010-05-07 21:11:40 +01001104 }
1105 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001106
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001107 for (i = 0; i < card->num_rtd; i++) {
1108 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001109
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001110 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001111 continue;
1112
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001113 if (driver->playback.stream_name != NULL)
1114 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001115 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001116
1117 if (driver->capture.stream_name != NULL)
1118 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001119 SND_SOC_DAPM_STREAM_RESUME);
1120 }
1121
Mark Brown3ff3f642008-05-19 12:32:25 +02001122 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001123 for (i = 0; i < card->num_rtd; i++) {
1124 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1125 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001126
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001127 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001128 continue;
1129
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001130 if (drv->ops->digital_mute && dai->playback_active)
1131 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001132 }
1133
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001134 for (i = 0; i < card->num_rtd; i++) {
1135 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1136 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001137
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001138 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001139 continue;
1140
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001141 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1142 cpu_dai->driver->resume(cpu_dai);
1143 if (platform->driver->resume && platform->suspended) {
1144 platform->driver->resume(cpu_dai);
1145 platform->suspended = 0;
1146 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001147 }
1148
Mark Brown87506542008-11-18 20:50:34 +00001149 if (card->resume_post)
1150 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001151
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001152 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001153
1154 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001155 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001156}
1157
1158/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001159static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001160{
Mark Brown416356f2009-06-30 19:05:15 +01001161 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001162 struct snd_soc_card *card = platform_get_drvdata(pdev);
1163 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001164
Mark Brown64ab9ba2009-03-31 11:27:03 +01001165 /* AC97 devices might have other drivers hanging off them so
1166 * need to resume immediately. Other drivers don't have that
1167 * problem and may take a substantial amount of time to resume
1168 * due to I/O costs and anti-pop so handle them out of line.
1169 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001170 for (i = 0; i < card->num_rtd; i++) {
1171 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1172 if (cpu_dai->driver->ac97_control) {
1173 dev_dbg(dev, "Resuming AC97 immediately\n");
1174 soc_resume_deferred(&card->deferred_resume_work);
1175 } else {
1176 dev_dbg(dev, "Scheduling resume work\n");
1177 if (!schedule_work(&card->deferred_resume_work))
1178 dev_err(dev, "resume work item may be lost\n");
1179 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001180 }
Andy Green6ed25972008-06-13 16:24:05 +01001181
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001182 return 0;
1183}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001184#else
1185#define soc_suspend NULL
1186#define soc_resume NULL
1187#endif
1188
Barry Song02a06d32009-10-16 18:13:38 +08001189static struct snd_soc_dai_ops null_dai_ops = {
1190};
1191
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001192static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001193{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001194 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1195 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001196 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001197 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001198 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001199
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001200 if (rtd->complete)
1201 return 1;
1202 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001203
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001204 /* do we already have the CPU DAI for this link ? */
1205 if (rtd->cpu_dai) {
1206 goto find_codec;
1207 }
1208 /* no, then find CPU DAI from registered DAIs*/
1209 list_for_each_entry(cpu_dai, &dai_list, list) {
1210 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1211
1212 if (!try_module_get(cpu_dai->dev->driver->owner))
1213 return -ENODEV;
1214
1215 rtd->cpu_dai = cpu_dai;
1216 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001217 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001218 }
1219 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1220 dai_link->cpu_dai_name);
1221
1222find_codec:
1223 /* do we already have the CODEC for this link ? */
1224 if (rtd->codec) {
1225 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001226 }
1227
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001228 /* no, then find CODEC from registered CODECs*/
1229 list_for_each_entry(codec, &codec_list, list) {
1230 if (!strcmp(codec->name, dai_link->codec_name)) {
1231 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001232
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001233 if (!try_module_get(codec->dev->driver->owner))
1234 return -ENODEV;
Mark Brown435c5e22008-12-04 15:32:53 +00001235
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001236 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1237 list_for_each_entry(codec_dai, &dai_list, list) {
1238 if (codec->dev == codec_dai->dev &&
1239 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1240 rtd->codec_dai = codec_dai;
1241 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001242 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001243 }
1244 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1245 dai_link->codec_dai_name);
1246
1247 goto find_platform;
1248 }
1249 }
1250 dev_dbg(card->dev, "CODEC %s not registered\n",
1251 dai_link->codec_name);
1252
1253find_platform:
1254 /* do we already have the CODEC DAI for this link ? */
1255 if (rtd->platform) {
1256 goto out;
1257 }
1258 /* no, then find CPU DAI from registered DAIs*/
1259 list_for_each_entry(platform, &platform_list, list) {
1260 if (!strcmp(platform->name, dai_link->platform_name)) {
1261
1262 if (!try_module_get(platform->dev->driver->owner))
1263 return -ENODEV;
1264
1265 rtd->platform = platform;
1266 goto out;
1267 }
1268 }
1269
1270 dev_dbg(card->dev, "platform %s not registered\n",
1271 dai_link->platform_name);
1272 return 0;
1273
1274out:
1275 /* mark rtd as complete if we found all 4 of our client devices */
1276 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1277 rtd->complete = 1;
1278 card->num_rtd++;
1279 }
1280 return 1;
1281}
1282
1283static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1284{
1285 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1286 struct snd_soc_codec *codec = rtd->codec;
1287 struct snd_soc_platform *platform = rtd->platform;
1288 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1289 int err;
1290
1291 /* unregister the rtd device */
1292 if (rtd->dev_registered) {
1293 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1294 device_unregister(&rtd->dev);
1295 rtd->dev_registered = 0;
1296 }
1297
1298 /* remove the CODEC DAI */
1299 if (codec_dai && codec_dai->probed) {
1300 if (codec_dai->driver->remove) {
1301 err = codec_dai->driver->remove(codec_dai);
1302 if (err < 0)
1303 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1304 }
1305 codec_dai->probed = 0;
1306 list_del(&codec_dai->card_list);
1307 }
1308
1309 /* remove the platform */
1310 if (platform && platform->probed) {
1311 if (platform->driver->remove) {
1312 err = platform->driver->remove(platform);
1313 if (err < 0)
1314 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1315 }
1316 platform->probed = 0;
1317 list_del(&platform->card_list);
1318 module_put(platform->dev->driver->owner);
1319 }
1320
1321 /* remove the CODEC */
1322 if (codec && codec->probed) {
1323 if (codec->driver->remove) {
1324 err = codec->driver->remove(codec);
1325 if (err < 0)
1326 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1327 }
1328
1329 /* Make sure all DAPM widgets are freed */
1330 snd_soc_dapm_free(codec);
1331
1332 soc_cleanup_codec_debugfs(codec);
1333 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1334 codec->probed = 0;
1335 list_del(&codec->card_list);
1336 module_put(codec->dev->driver->owner);
1337 }
1338
1339 /* remove the cpu_dai */
1340 if (cpu_dai && cpu_dai->probed) {
1341 if (cpu_dai->driver->remove) {
1342 err = cpu_dai->driver->remove(cpu_dai);
1343 if (err < 0)
1344 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1345 }
1346 cpu_dai->probed = 0;
1347 list_del(&cpu_dai->card_list);
1348 module_put(cpu_dai->dev->driver->owner);
1349 }
1350}
1351
1352static void rtd_release(struct device *dev) {}
1353
1354static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1355{
1356 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1357 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1358 struct snd_soc_codec *codec = rtd->codec;
1359 struct snd_soc_platform *platform = rtd->platform;
1360 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1361 int ret;
1362
1363 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1364
1365 /* config components */
1366 codec_dai->codec = codec;
1367 codec->card = card;
1368 cpu_dai->platform = platform;
1369 rtd->card = card;
1370 rtd->dev.parent = card->dev;
1371 codec_dai->card = card;
1372 cpu_dai->card = card;
1373
1374 /* set default power off timeout */
1375 rtd->pmdown_time = pmdown_time;
1376
1377 /* probe the cpu_dai */
1378 if (!cpu_dai->probed) {
1379 if (cpu_dai->driver->probe) {
1380 ret = cpu_dai->driver->probe(cpu_dai);
1381 if (ret < 0) {
1382 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1383 cpu_dai->name);
1384 return ret;
1385 }
1386 }
1387 cpu_dai->probed = 1;
1388 /* mark cpu_dai as probed and add to card cpu_dai list */
1389 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1390 }
1391
1392 /* probe the CODEC */
1393 if (!codec->probed) {
1394 if (codec->driver->probe) {
1395 ret = codec->driver->probe(codec);
1396 if (ret < 0) {
1397 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1398 codec->name);
1399 return ret;
1400 }
1401 }
Mark Brown13cb61f2010-08-12 15:44:04 +01001402
1403 soc_init_codec_debugfs(codec);
1404
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001405 /* mark codec as probed and add to card codec list */
1406 codec->probed = 1;
1407 list_add(&codec->card_list, &card->codec_dev_list);
1408 }
1409
1410 /* probe the platform */
1411 if (!platform->probed) {
1412 if (platform->driver->probe) {
1413 ret = platform->driver->probe(platform);
1414 if (ret < 0) {
1415 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1416 platform->name);
1417 return ret;
1418 }
1419 }
1420 /* mark platform as probed and add to card platform list */
1421 platform->probed = 1;
1422 list_add(&platform->card_list, &card->platform_dev_list);
1423 }
1424
1425 /* probe the CODEC DAI */
1426 if (!codec_dai->probed) {
1427 if (codec_dai->driver->probe) {
1428 ret = codec_dai->driver->probe(codec_dai);
1429 if (ret < 0) {
1430 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1431 codec_dai->name);
1432 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001433 }
1434 }
1435
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001436 /* mark cpu_dai as probed and add to card cpu_dai list */
1437 codec_dai->probed = 1;
1438 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001439 }
1440
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001441 /* DAPM dai link stream work */
1442 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1443
1444 /* now that all clients have probed, initialise the DAI link */
1445 if (dai_link->init) {
1446 ret = dai_link->init(rtd);
1447 if (ret < 0) {
1448 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1449 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001450 }
1451 }
1452
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001453 /* Make sure all DAPM widgets are instantiated */
1454 snd_soc_dapm_new_widgets(codec);
1455 snd_soc_dapm_sync(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001456
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001457 /* register the rtd device */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001458 rtd->dev.release = rtd_release;
1459 rtd->dev.init_name = dai_link->name;
1460 ret = device_register(&rtd->dev);
1461 if (ret < 0) {
1462 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1463 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001464 }
1465
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001466 rtd->dev_registered = 1;
1467 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1468 if (ret < 0)
1469 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1470
1471 /* add DAPM sysfs entries for this codec */
1472 ret = snd_soc_dapm_sys_add(&rtd->dev);
1473 if (ret < 0)
1474 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1475
1476 /* add codec sysfs entries */
1477 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1478 if (ret < 0)
1479 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1480
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001481 /* create the pcm */
1482 ret = soc_new_pcm(rtd, num);
1483 if (ret < 0) {
1484 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1485 return ret;
1486 }
1487
1488 /* add platform data for AC97 devices */
1489 if (rtd->codec_dai->driver->ac97_control)
1490 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1491
1492 return 0;
1493}
1494
1495#ifdef CONFIG_SND_SOC_AC97_BUS
1496static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1497{
1498 int ret;
1499
1500 /* Only instantiate AC97 if not already done by the adaptor
1501 * for the generic AC97 subsystem.
1502 */
1503 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001504 /*
1505 * It is possible that the AC97 device is already registered to
1506 * the device subsystem. This happens when the device is created
1507 * via snd_ac97_mixer(). Currently only SoC codec that does so
1508 * is the generic AC97 glue but others migh emerge.
1509 *
1510 * In those cases we don't try to register the device again.
1511 */
1512 if (!rtd->codec->ac97_created)
1513 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001514
1515 ret = soc_ac97_dev_register(rtd->codec);
1516 if (ret < 0) {
1517 printk(KERN_ERR "asoc: AC97 device register failed\n");
1518 return ret;
1519 }
1520
1521 rtd->codec->ac97_registered = 1;
1522 }
1523 return 0;
1524}
1525
1526static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1527{
1528 if (codec->ac97_registered) {
1529 soc_ac97_dev_unregister(codec);
1530 codec->ac97_registered = 0;
1531 }
1532}
1533#endif
1534
1535static void snd_soc_instantiate_card(struct snd_soc_card *card)
1536{
1537 struct platform_device *pdev = to_platform_device(card->dev);
1538 int ret, i;
1539
1540 mutex_lock(&card->mutex);
1541
1542 if (card->instantiated) {
1543 mutex_unlock(&card->mutex);
1544 return;
1545 }
1546
1547 /* bind DAIs */
1548 for (i = 0; i < card->num_links; i++)
1549 soc_bind_dai_link(card, i);
1550
1551 /* bind completed ? */
1552 if (card->num_rtd != card->num_links) {
1553 mutex_unlock(&card->mutex);
1554 return;
1555 }
1556
1557 /* card bind complete so register a sound card */
1558 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1559 card->owner, 0, &card->snd_card);
1560 if (ret < 0) {
1561 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1562 card->name);
1563 mutex_unlock(&card->mutex);
1564 return;
1565 }
1566 card->snd_card->dev = card->dev;
1567
Randy Dunlap1301a962008-06-17 19:19:34 +01001568#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001569 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001570 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001571#endif
Andy Green6ed25972008-06-13 16:24:05 +01001572
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001573 /* initialise the sound card only once */
1574 if (card->probe) {
1575 ret = card->probe(pdev);
1576 if (ret < 0)
1577 goto card_probe_error;
1578 }
1579
Mark Brownfe3e78e2009-11-03 22:13:13 +00001580 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001581 ret = soc_probe_dai_link(card, i);
1582 if (ret < 0) {
Mark Brown321de0d2010-09-21 15:09:37 +01001583 pr_err("asoc: failed to instantiate card %s: %d\n",
1584 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001585 goto probe_dai_err;
1586 }
1587 }
1588
1589 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1590 "%s", card->name);
1591 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1592 "%s", card->name);
1593
1594 ret = snd_card_register(card->snd_card);
1595 if (ret < 0) {
1596 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1597 goto probe_dai_err;
1598 }
1599
1600#ifdef CONFIG_SND_SOC_AC97_BUS
1601 /* register any AC97 codecs */
1602 for (i = 0; i < card->num_rtd; i++) {
1603 ret = soc_register_ac97_dai_link(&card->rtd[i]);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001604 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001605 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1606 goto probe_dai_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001607 }
1608 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001609#endif
1610
Mark Brown435c5e22008-12-04 15:32:53 +00001611 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001612 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001613 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001614
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001615probe_dai_err:
1616 for (i = 0; i < card->num_links; i++)
1617 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001618
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001619card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001620 if (card->remove)
1621 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001622
1623 snd_card_free(card->snd_card);
1624
1625 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001626}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001627
Mark Brown435c5e22008-12-04 15:32:53 +00001628/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001629 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001630 * client_mutex.
1631 */
1632static void snd_soc_instantiate_cards(void)
1633{
1634 struct snd_soc_card *card;
1635 list_for_each_entry(card, &card_list, list)
1636 snd_soc_instantiate_card(card);
1637}
1638
1639/* probes a new socdev */
1640static int soc_probe(struct platform_device *pdev)
1641{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001642 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001643 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001644
1645 /* Bodge while we unpick instantiation */
1646 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001647 INIT_LIST_HEAD(&card->dai_dev_list);
1648 INIT_LIST_HEAD(&card->codec_dev_list);
1649 INIT_LIST_HEAD(&card->platform_dev_list);
1650
Mark Brown435c5e22008-12-04 15:32:53 +00001651 ret = snd_soc_register_card(card);
1652 if (ret != 0) {
1653 dev_err(&pdev->dev, "Failed to register card\n");
1654 return ret;
1655 }
1656
1657 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001658}
1659
1660/* removes a socdev */
1661static int soc_remove(struct platform_device *pdev)
1662{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001663 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001664 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001665
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001666 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001667
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001668 /* make sure any delayed work runs */
1669 for (i = 0; i < card->num_rtd; i++) {
1670 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001671 flush_delayed_work_sync(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001672 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001673
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001674 /* remove and free each DAI */
1675 for (i = 0; i < card->num_rtd; i++)
1676 soc_remove_dai_link(card, i);
1677
1678 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001679 if (card->remove)
1680 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001681
1682 kfree(card->rtd);
1683 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001684 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001685 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001686 return 0;
1687}
1688
Mark Brown416356f2009-06-30 19:05:15 +01001689static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001690{
Mark Brown416356f2009-06-30 19:05:15 +01001691 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001692 struct snd_soc_card *card = platform_get_drvdata(pdev);
1693 int i;
Mark Brown51737472009-06-22 13:16:51 +01001694
1695 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001696 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001697
1698 /* Flush out pmdown_time work - we actually do want to run it
1699 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001700 for (i = 0; i < card->num_rtd; i++) {
1701 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001702 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001703 }
Mark Brown51737472009-06-22 13:16:51 +01001704
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001705 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001706
1707 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001708}
1709
Alexey Dobriyan47145212009-12-14 18:00:08 -08001710static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001711 .suspend = soc_suspend,
1712 .resume = soc_resume,
1713 .poweroff = soc_poweroff,
1714};
1715
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001716/* ASoC platform driver */
1717static struct platform_driver soc_driver = {
1718 .driver = {
1719 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001720 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001721 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001722 },
1723 .probe = soc_probe,
1724 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001725};
1726
1727/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001728static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001729{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001730 struct snd_soc_codec *codec = rtd->codec;
1731 struct snd_soc_platform *platform = rtd->platform;
1732 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1733 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001734 struct snd_pcm *pcm;
1735 char new_name[64];
1736 int ret = 0, playback = 0, capture = 0;
1737
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001738 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001739 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001740 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001741
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001742 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001743 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001744 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001745 capture = 1;
1746
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001747 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1748 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1749 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001750 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001751 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001752 return ret;
1753 }
1754
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001755 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001756 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001757 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1758 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1759 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1760 soc_pcm_ops.copy = platform->driver->ops->copy;
1761 soc_pcm_ops.silence = platform->driver->ops->silence;
1762 soc_pcm_ops.ack = platform->driver->ops->ack;
1763 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001764
1765 if (playback)
1766 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1767
1768 if (capture)
1769 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1770
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001771 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001772 if (ret < 0) {
1773 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001774 return ret;
1775 }
1776
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001777 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001778 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1779 cpu_dai->name);
1780 return ret;
1781}
1782
Mark Brown096e49d2009-07-05 15:12:22 +01001783/**
1784 * snd_soc_codec_volatile_register: Report if a register is volatile.
1785 *
1786 * @codec: CODEC to query.
1787 * @reg: Register to query.
1788 *
1789 * Boolean function indiciating if a CODEC register is volatile.
1790 */
1791int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1792{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001793 if (codec->driver->volatile_register)
1794 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001795 else
1796 return 0;
1797}
1798EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1799
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001800/**
1801 * snd_soc_new_ac97_codec - initailise AC97 device
1802 * @codec: audio codec
1803 * @ops: AC97 bus operations
1804 * @num: AC97 codec number
1805 *
1806 * Initialises AC97 codec resources for use by ad-hoc devices only.
1807 */
1808int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1809 struct snd_ac97_bus_ops *ops, int num)
1810{
1811 mutex_lock(&codec->mutex);
1812
1813 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1814 if (codec->ac97 == NULL) {
1815 mutex_unlock(&codec->mutex);
1816 return -ENOMEM;
1817 }
1818
1819 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1820 if (codec->ac97->bus == NULL) {
1821 kfree(codec->ac97);
1822 codec->ac97 = NULL;
1823 mutex_unlock(&codec->mutex);
1824 return -ENOMEM;
1825 }
1826
1827 codec->ac97->bus->ops = ops;
1828 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001829
1830 /*
1831 * Mark the AC97 device to be created by us. This way we ensure that the
1832 * device will be registered with the device subsystem later on.
1833 */
1834 codec->ac97_created = 1;
1835
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001836 mutex_unlock(&codec->mutex);
1837 return 0;
1838}
1839EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1840
1841/**
1842 * snd_soc_free_ac97_codec - free AC97 codec device
1843 * @codec: audio codec
1844 *
1845 * Frees AC97 codec device resources.
1846 */
1847void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1848{
1849 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001850#ifdef CONFIG_SND_SOC_AC97_BUS
1851 soc_unregister_ac97_dai_link(codec);
1852#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001853 kfree(codec->ac97->bus);
1854 kfree(codec->ac97);
1855 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001856 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001857 mutex_unlock(&codec->mutex);
1858}
1859EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1860
1861/**
1862 * snd_soc_update_bits - update codec register bits
1863 * @codec: audio codec
1864 * @reg: codec register
1865 * @mask: register mask
1866 * @value: new value
1867 *
1868 * Writes new register value.
1869 *
1870 * Returns 1 for change else 0.
1871 */
1872int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001873 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001874{
1875 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001876 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001877
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001878 old = snd_soc_read(codec, reg);
1879 new = (old & ~mask) | value;
1880 change = old != new;
1881 if (change)
1882 snd_soc_write(codec, reg, new);
1883
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001884 return change;
1885}
1886EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1887
1888/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001889 * snd_soc_update_bits_locked - update codec register bits
1890 * @codec: audio codec
1891 * @reg: codec register
1892 * @mask: register mask
1893 * @value: new value
1894 *
1895 * Writes new register value, and takes the codec mutex.
1896 *
1897 * Returns 1 for change else 0.
1898 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001899int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1900 unsigned short reg, unsigned int mask,
1901 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001902{
1903 int change;
1904
1905 mutex_lock(&codec->mutex);
1906 change = snd_soc_update_bits(codec, reg, mask, value);
1907 mutex_unlock(&codec->mutex);
1908
1909 return change;
1910}
Mark Browndd1b3d52009-12-04 14:22:03 +00001911EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001912
1913/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001914 * snd_soc_test_bits - test register for change
1915 * @codec: audio codec
1916 * @reg: codec register
1917 * @mask: register mask
1918 * @value: new value
1919 *
1920 * Tests a register with a new value and checks if the new value is
1921 * different from the old value.
1922 *
1923 * Returns 1 for change else 0.
1924 */
1925int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001926 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001927{
1928 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001929 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001930
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001931 old = snd_soc_read(codec, reg);
1932 new = (old & ~mask) | value;
1933 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001934
1935 return change;
1936}
1937EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1938
1939/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001940 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1941 * @substream: the pcm substream
1942 * @hw: the hardware parameters
1943 *
1944 * Sets the substream runtime hardware parameters.
1945 */
1946int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1947 const struct snd_pcm_hardware *hw)
1948{
1949 struct snd_pcm_runtime *runtime = substream->runtime;
1950 runtime->hw.info = hw->info;
1951 runtime->hw.formats = hw->formats;
1952 runtime->hw.period_bytes_min = hw->period_bytes_min;
1953 runtime->hw.period_bytes_max = hw->period_bytes_max;
1954 runtime->hw.periods_min = hw->periods_min;
1955 runtime->hw.periods_max = hw->periods_max;
1956 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1957 runtime->hw.fifo_size = hw->fifo_size;
1958 return 0;
1959}
1960EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1961
1962/**
1963 * snd_soc_cnew - create new control
1964 * @_template: control template
1965 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001966 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001967 *
1968 * Create a new mixer control from a template control.
1969 *
1970 * Returns 0 for success, else error.
1971 */
1972struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1973 void *data, char *long_name)
1974{
1975 struct snd_kcontrol_new template;
1976
1977 memcpy(&template, _template, sizeof(template));
1978 if (long_name)
1979 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001980 template.index = 0;
1981
1982 return snd_ctl_new1(&template, data);
1983}
1984EXPORT_SYMBOL_GPL(snd_soc_cnew);
1985
1986/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001987 * snd_soc_add_controls - add an array of controls to a codec.
1988 * Convienience function to add a list of controls. Many codecs were
1989 * duplicating this code.
1990 *
1991 * @codec: codec to add controls to
1992 * @controls: array of controls to add
1993 * @num_controls: number of elements in the array
1994 *
1995 * Return 0 for success, else error.
1996 */
1997int snd_soc_add_controls(struct snd_soc_codec *codec,
1998 const struct snd_kcontrol_new *controls, int num_controls)
1999{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002000 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002001 int err, i;
2002
2003 for (i = 0; i < num_controls; i++) {
2004 const struct snd_kcontrol_new *control = &controls[i];
2005 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
2006 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002007 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2008 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002009 return err;
2010 }
2011 }
2012
2013 return 0;
2014}
2015EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2016
2017/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002018 * snd_soc_info_enum_double - enumerated double mixer info callback
2019 * @kcontrol: mixer control
2020 * @uinfo: control element information
2021 *
2022 * Callback to provide information about a double enumerated
2023 * mixer control.
2024 *
2025 * Returns 0 for success.
2026 */
2027int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2028 struct snd_ctl_elem_info *uinfo)
2029{
2030 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2031
2032 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2033 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002034 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002035
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002036 if (uinfo->value.enumerated.item > e->max - 1)
2037 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002038 strcpy(uinfo->value.enumerated.name,
2039 e->texts[uinfo->value.enumerated.item]);
2040 return 0;
2041}
2042EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2043
2044/**
2045 * snd_soc_get_enum_double - enumerated double mixer get callback
2046 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002047 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002048 *
2049 * Callback to get the value of a double enumerated mixer.
2050 *
2051 * Returns 0 for success.
2052 */
2053int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2054 struct snd_ctl_elem_value *ucontrol)
2055{
2056 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2057 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002058 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002059
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002060 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002061 ;
2062 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002063 ucontrol->value.enumerated.item[0]
2064 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002065 if (e->shift_l != e->shift_r)
2066 ucontrol->value.enumerated.item[1] =
2067 (val >> e->shift_r) & (bitmask - 1);
2068
2069 return 0;
2070}
2071EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2072
2073/**
2074 * snd_soc_put_enum_double - enumerated double mixer put callback
2075 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002076 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002077 *
2078 * Callback to set the value of a double enumerated mixer.
2079 *
2080 * Returns 0 for success.
2081 */
2082int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2083 struct snd_ctl_elem_value *ucontrol)
2084{
2085 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2086 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002087 unsigned int val;
2088 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002089
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002090 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002091 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002092 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002093 return -EINVAL;
2094 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2095 mask = (bitmask - 1) << e->shift_l;
2096 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002097 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002098 return -EINVAL;
2099 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2100 mask |= (bitmask - 1) << e->shift_r;
2101 }
2102
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002103 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002104}
2105EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2106
2107/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002108 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2109 * @kcontrol: mixer control
2110 * @ucontrol: control element information
2111 *
2112 * Callback to get the value of a double semi enumerated mixer.
2113 *
2114 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2115 * used for handling bitfield coded enumeration for example.
2116 *
2117 * Returns 0 for success.
2118 */
2119int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2120 struct snd_ctl_elem_value *ucontrol)
2121{
2122 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002123 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002124 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002125
2126 reg_val = snd_soc_read(codec, e->reg);
2127 val = (reg_val >> e->shift_l) & e->mask;
2128 for (mux = 0; mux < e->max; mux++) {
2129 if (val == e->values[mux])
2130 break;
2131 }
2132 ucontrol->value.enumerated.item[0] = mux;
2133 if (e->shift_l != e->shift_r) {
2134 val = (reg_val >> e->shift_r) & e->mask;
2135 for (mux = 0; mux < e->max; mux++) {
2136 if (val == e->values[mux])
2137 break;
2138 }
2139 ucontrol->value.enumerated.item[1] = mux;
2140 }
2141
2142 return 0;
2143}
2144EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2145
2146/**
2147 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2148 * @kcontrol: mixer control
2149 * @ucontrol: control element information
2150 *
2151 * Callback to set the value of a double semi enumerated mixer.
2152 *
2153 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2154 * used for handling bitfield coded enumeration for example.
2155 *
2156 * Returns 0 for success.
2157 */
2158int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2159 struct snd_ctl_elem_value *ucontrol)
2160{
2161 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002162 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002163 unsigned int val;
2164 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002165
2166 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2167 return -EINVAL;
2168 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2169 mask = e->mask << e->shift_l;
2170 if (e->shift_l != e->shift_r) {
2171 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2172 return -EINVAL;
2173 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2174 mask |= e->mask << e->shift_r;
2175 }
2176
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002177 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002178}
2179EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2180
2181/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002182 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2183 * @kcontrol: mixer control
2184 * @uinfo: control element information
2185 *
2186 * Callback to provide information about an external enumerated
2187 * single mixer.
2188 *
2189 * Returns 0 for success.
2190 */
2191int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2192 struct snd_ctl_elem_info *uinfo)
2193{
2194 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2195
2196 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2197 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002198 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002199
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002200 if (uinfo->value.enumerated.item > e->max - 1)
2201 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002202 strcpy(uinfo->value.enumerated.name,
2203 e->texts[uinfo->value.enumerated.item]);
2204 return 0;
2205}
2206EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2207
2208/**
2209 * snd_soc_info_volsw_ext - external single mixer info callback
2210 * @kcontrol: mixer control
2211 * @uinfo: control element information
2212 *
2213 * Callback to provide information about a single external mixer control.
2214 *
2215 * Returns 0 for success.
2216 */
2217int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2218 struct snd_ctl_elem_info *uinfo)
2219{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002220 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002221
Mark Brownfd5dfad2009-04-15 21:37:46 +01002222 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002223 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2224 else
2225 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2226
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002227 uinfo->count = 1;
2228 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002229 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002230 return 0;
2231}
2232EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2233
2234/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002235 * snd_soc_info_volsw - single mixer info callback
2236 * @kcontrol: mixer control
2237 * @uinfo: control element information
2238 *
2239 * Callback to provide information about a single mixer control.
2240 *
2241 * Returns 0 for success.
2242 */
2243int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2244 struct snd_ctl_elem_info *uinfo)
2245{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002246 struct soc_mixer_control *mc =
2247 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002248 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002249 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002250 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002251
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002252 if (!mc->platform_max)
2253 mc->platform_max = mc->max;
2254 platform_max = mc->platform_max;
2255
2256 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002257 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2258 else
2259 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2260
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002261 uinfo->count = shift == rshift ? 1 : 2;
2262 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002263 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002264 return 0;
2265}
2266EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2267
2268/**
2269 * snd_soc_get_volsw - single mixer get callback
2270 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002271 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002272 *
2273 * Callback to get the value of a single mixer control.
2274 *
2275 * Returns 0 for success.
2276 */
2277int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_value *ucontrol)
2279{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002280 struct soc_mixer_control *mc =
2281 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002282 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002283 unsigned int reg = mc->reg;
2284 unsigned int shift = mc->shift;
2285 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002286 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002287 unsigned int mask = (1 << fls(max)) - 1;
2288 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002289
2290 ucontrol->value.integer.value[0] =
2291 (snd_soc_read(codec, reg) >> shift) & mask;
2292 if (shift != rshift)
2293 ucontrol->value.integer.value[1] =
2294 (snd_soc_read(codec, reg) >> rshift) & mask;
2295 if (invert) {
2296 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002297 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002298 if (shift != rshift)
2299 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002300 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002301 }
2302
2303 return 0;
2304}
2305EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2306
2307/**
2308 * snd_soc_put_volsw - single mixer put callback
2309 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002310 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002311 *
2312 * Callback to set the value of a single mixer control.
2313 *
2314 * Returns 0 for success.
2315 */
2316int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2317 struct snd_ctl_elem_value *ucontrol)
2318{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002319 struct soc_mixer_control *mc =
2320 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002321 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002322 unsigned int reg = mc->reg;
2323 unsigned int shift = mc->shift;
2324 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002325 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002326 unsigned int mask = (1 << fls(max)) - 1;
2327 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002328 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002329
2330 val = (ucontrol->value.integer.value[0] & mask);
2331 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002332 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002333 val_mask = mask << shift;
2334 val = val << shift;
2335 if (shift != rshift) {
2336 val2 = (ucontrol->value.integer.value[1] & mask);
2337 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002338 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002339 val_mask |= mask << rshift;
2340 val |= val2 << rshift;
2341 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002342 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002343}
2344EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2345
2346/**
2347 * snd_soc_info_volsw_2r - double mixer info callback
2348 * @kcontrol: mixer control
2349 * @uinfo: control element information
2350 *
2351 * Callback to provide information about a double mixer control that
2352 * spans 2 codec registers.
2353 *
2354 * Returns 0 for success.
2355 */
2356int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2357 struct snd_ctl_elem_info *uinfo)
2358{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002359 struct soc_mixer_control *mc =
2360 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002361 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002362
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002363 if (!mc->platform_max)
2364 mc->platform_max = mc->max;
2365 platform_max = mc->platform_max;
2366
2367 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002368 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2369 else
2370 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2371
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002372 uinfo->count = 2;
2373 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002374 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002375 return 0;
2376}
2377EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2378
2379/**
2380 * snd_soc_get_volsw_2r - double mixer get callback
2381 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002382 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002383 *
2384 * Callback to get the value of a double mixer control that spans 2 registers.
2385 *
2386 * Returns 0 for success.
2387 */
2388int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2389 struct snd_ctl_elem_value *ucontrol)
2390{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002391 struct soc_mixer_control *mc =
2392 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002393 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002394 unsigned int reg = mc->reg;
2395 unsigned int reg2 = mc->rreg;
2396 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002397 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002398 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002399 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002400
2401 ucontrol->value.integer.value[0] =
2402 (snd_soc_read(codec, reg) >> shift) & mask;
2403 ucontrol->value.integer.value[1] =
2404 (snd_soc_read(codec, reg2) >> shift) & mask;
2405 if (invert) {
2406 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002407 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002408 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002409 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002410 }
2411
2412 return 0;
2413}
2414EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2415
2416/**
2417 * snd_soc_put_volsw_2r - double mixer set callback
2418 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002419 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002420 *
2421 * Callback to set the value of a double mixer control that spans 2 registers.
2422 *
2423 * Returns 0 for success.
2424 */
2425int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_value *ucontrol)
2427{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002428 struct soc_mixer_control *mc =
2429 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002430 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002431 unsigned int reg = mc->reg;
2432 unsigned int reg2 = mc->rreg;
2433 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002434 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002435 unsigned int mask = (1 << fls(max)) - 1;
2436 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002437 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002438 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002439
2440 val_mask = mask << shift;
2441 val = (ucontrol->value.integer.value[0] & mask);
2442 val2 = (ucontrol->value.integer.value[1] & mask);
2443
2444 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002445 val = max - val;
2446 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002447 }
2448
2449 val = val << shift;
2450 val2 = val2 << shift;
2451
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002452 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002453 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002454 return err;
2455
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002456 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002457 return err;
2458}
2459EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2460
Mark Browne13ac2e2008-05-28 17:58:05 +01002461/**
2462 * snd_soc_info_volsw_s8 - signed mixer info callback
2463 * @kcontrol: mixer control
2464 * @uinfo: control element information
2465 *
2466 * Callback to provide information about a signed mixer control.
2467 *
2468 * Returns 0 for success.
2469 */
2470int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2471 struct snd_ctl_elem_info *uinfo)
2472{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002473 struct soc_mixer_control *mc =
2474 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002475 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002476 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002477
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002478 if (!mc->platform_max)
2479 mc->platform_max = mc->max;
2480 platform_max = mc->platform_max;
2481
Mark Browne13ac2e2008-05-28 17:58:05 +01002482 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2483 uinfo->count = 2;
2484 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002485 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002486 return 0;
2487}
2488EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2489
2490/**
2491 * snd_soc_get_volsw_s8 - signed mixer get callback
2492 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002493 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002494 *
2495 * Callback to get the value of a signed mixer control.
2496 *
2497 * Returns 0 for success.
2498 */
2499int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2500 struct snd_ctl_elem_value *ucontrol)
2501{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002502 struct soc_mixer_control *mc =
2503 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002504 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002505 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002506 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002507 int val = snd_soc_read(codec, reg);
2508
2509 ucontrol->value.integer.value[0] =
2510 ((signed char)(val & 0xff))-min;
2511 ucontrol->value.integer.value[1] =
2512 ((signed char)((val >> 8) & 0xff))-min;
2513 return 0;
2514}
2515EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2516
2517/**
2518 * snd_soc_put_volsw_sgn - signed mixer put callback
2519 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002520 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002521 *
2522 * Callback to set the value of a signed mixer control.
2523 *
2524 * Returns 0 for success.
2525 */
2526int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2527 struct snd_ctl_elem_value *ucontrol)
2528{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002529 struct soc_mixer_control *mc =
2530 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002531 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002532 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002533 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002534 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002535
2536 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2537 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2538
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002539 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002540}
2541EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2542
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002543/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002544 * snd_soc_limit_volume - Set new limit to an existing volume control.
2545 *
2546 * @codec: where to look for the control
2547 * @name: Name of the control
2548 * @max: new maximum limit
2549 *
2550 * Return 0 for success, else error.
2551 */
2552int snd_soc_limit_volume(struct snd_soc_codec *codec,
2553 const char *name, int max)
2554{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002555 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002556 struct snd_kcontrol *kctl;
2557 struct soc_mixer_control *mc;
2558 int found = 0;
2559 int ret = -EINVAL;
2560
2561 /* Sanity check for name and max */
2562 if (unlikely(!name || max <= 0))
2563 return -EINVAL;
2564
2565 list_for_each_entry(kctl, &card->controls, list) {
2566 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2567 found = 1;
2568 break;
2569 }
2570 }
2571 if (found) {
2572 mc = (struct soc_mixer_control *)kctl->private_value;
2573 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002574 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002575 ret = 0;
2576 }
2577 }
2578 return ret;
2579}
2580EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2581
2582/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002583 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2584 * mixer info callback
2585 * @kcontrol: mixer control
2586 * @uinfo: control element information
2587 *
2588 * Returns 0 for success.
2589 */
2590int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2591 struct snd_ctl_elem_info *uinfo)
2592{
2593 struct soc_mixer_control *mc =
2594 (struct soc_mixer_control *)kcontrol->private_value;
2595 int max = mc->max;
2596 int min = mc->min;
2597
2598 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2599 uinfo->count = 2;
2600 uinfo->value.integer.min = 0;
2601 uinfo->value.integer.max = max-min;
2602
2603 return 0;
2604}
2605EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2606
2607/**
2608 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2609 * mixer get callback
2610 * @kcontrol: mixer control
2611 * @uinfo: control element information
2612 *
2613 * Returns 0 for success.
2614 */
2615int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2616 struct snd_ctl_elem_value *ucontrol)
2617{
2618 struct soc_mixer_control *mc =
2619 (struct soc_mixer_control *)kcontrol->private_value;
2620 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2621 unsigned int mask = (1<<mc->shift)-1;
2622 int min = mc->min;
2623 int val = snd_soc_read(codec, mc->reg) & mask;
2624 int valr = snd_soc_read(codec, mc->rreg) & mask;
2625
Stuart Longland20630c72010-06-18 12:56:10 +10002626 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2627 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002628 return 0;
2629}
2630EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2631
2632/**
2633 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2634 * mixer put callback
2635 * @kcontrol: mixer control
2636 * @uinfo: control element information
2637 *
2638 * Returns 0 for success.
2639 */
2640int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2641 struct snd_ctl_elem_value *ucontrol)
2642{
2643 struct soc_mixer_control *mc =
2644 (struct soc_mixer_control *)kcontrol->private_value;
2645 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2646 unsigned int mask = (1<<mc->shift)-1;
2647 int min = mc->min;
2648 int ret;
2649 unsigned int val, valr, oval, ovalr;
2650
2651 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2652 val &= mask;
2653 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2654 valr &= mask;
2655
2656 oval = snd_soc_read(codec, mc->reg) & mask;
2657 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2658
2659 ret = 0;
2660 if (oval != val) {
2661 ret = snd_soc_write(codec, mc->reg, val);
2662 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002663 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002664 }
2665 if (ovalr != valr) {
2666 ret = snd_soc_write(codec, mc->rreg, valr);
2667 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002668 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002669 }
2670
2671 return 0;
2672}
2673EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2674
2675/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002676 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2677 * @dai: DAI
2678 * @clk_id: DAI specific clock ID
2679 * @freq: new clock frequency in Hz
2680 * @dir: new clock direction - input/output.
2681 *
2682 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2683 */
2684int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2685 unsigned int freq, int dir)
2686{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002687 if (dai->driver && dai->driver->ops->set_sysclk)
2688 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002689 else
2690 return -EINVAL;
2691}
2692EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2693
2694/**
2695 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2696 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002697 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002698 * @div: new clock divisor.
2699 *
2700 * Configures the clock dividers. This is used to derive the best DAI bit and
2701 * frame clocks from the system or master clock. It's best to set the DAI bit
2702 * and frame clocks as low as possible to save system power.
2703 */
2704int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2705 int div_id, int div)
2706{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002707 if (dai->driver && dai->driver->ops->set_clkdiv)
2708 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002709 else
2710 return -EINVAL;
2711}
2712EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2713
2714/**
2715 * snd_soc_dai_set_pll - configure DAI PLL.
2716 * @dai: DAI
2717 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002718 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002719 * @freq_in: PLL input clock frequency in Hz
2720 * @freq_out: requested PLL output clock frequency in Hz
2721 *
2722 * Configures and enables PLL to generate output clock based on input clock.
2723 */
Mark Brown85488032009-09-05 18:52:16 +01002724int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2725 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002726{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002727 if (dai->driver && dai->driver->ops->set_pll)
2728 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002729 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002730 else
2731 return -EINVAL;
2732}
2733EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2734
2735/**
2736 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2737 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002738 * @fmt: SND_SOC_DAIFMT_ format value.
2739 *
2740 * Configures the DAI hardware format and clocking.
2741 */
2742int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2743{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002744 if (dai->driver && dai->driver->ops->set_fmt)
2745 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002746 else
2747 return -EINVAL;
2748}
2749EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2750
2751/**
2752 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2753 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002754 * @tx_mask: bitmask representing active TX slots.
2755 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002756 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002757 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002758 *
2759 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2760 * specific.
2761 */
2762int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002763 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002764{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002765 if (dai->driver && dai->driver->ops->set_tdm_slot)
2766 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002767 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002768 else
2769 return -EINVAL;
2770}
2771EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2772
2773/**
Barry Song472df3c2009-09-12 01:16:29 +08002774 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2775 * @dai: DAI
2776 * @tx_num: how many TX channels
2777 * @tx_slot: pointer to an array which imply the TX slot number channel
2778 * 0~num-1 uses
2779 * @rx_num: how many RX channels
2780 * @rx_slot: pointer to an array which imply the RX slot number channel
2781 * 0~num-1 uses
2782 *
2783 * configure the relationship between channel number and TDM slot number.
2784 */
2785int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2786 unsigned int tx_num, unsigned int *tx_slot,
2787 unsigned int rx_num, unsigned int *rx_slot)
2788{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002789 if (dai->driver && dai->driver->ops->set_channel_map)
2790 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002791 rx_num, rx_slot);
2792 else
2793 return -EINVAL;
2794}
2795EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2796
2797/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002798 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2799 * @dai: DAI
2800 * @tristate: tristate enable
2801 *
2802 * Tristates the DAI so that others can use it.
2803 */
2804int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2805{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002806 if (dai->driver && dai->driver->ops->set_tristate)
2807 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002808 else
2809 return -EINVAL;
2810}
2811EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2812
2813/**
2814 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2815 * @dai: DAI
2816 * @mute: mute enable
2817 *
2818 * Mutes the DAI DAC.
2819 */
2820int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2821{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002822 if (dai->driver && dai->driver->ops->digital_mute)
2823 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002824 else
2825 return -EINVAL;
2826}
2827EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2828
Mark Brownc5af3a22008-11-28 13:29:45 +00002829/**
2830 * snd_soc_register_card - Register a card with the ASoC core
2831 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002832 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002833 *
2834 * Note that currently this is an internal only function: it will be
2835 * exposed to machine drivers after further backporting of ASoC v2
2836 * registration APIs.
2837 */
2838static int snd_soc_register_card(struct snd_soc_card *card)
2839{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002840 int i;
2841
Mark Brownc5af3a22008-11-28 13:29:45 +00002842 if (!card->name || !card->dev)
2843 return -EINVAL;
2844
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002845 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2846 GFP_KERNEL);
2847 if (card->rtd == NULL)
2848 return -ENOMEM;
2849
2850 for (i = 0; i < card->num_links; i++)
2851 card->rtd[i].dai_link = &card->dai_link[i];
2852
Mark Brownc5af3a22008-11-28 13:29:45 +00002853 INIT_LIST_HEAD(&card->list);
2854 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002855 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002856
2857 mutex_lock(&client_mutex);
2858 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002859 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002860 mutex_unlock(&client_mutex);
2861
2862 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2863
2864 return 0;
2865}
2866
2867/**
2868 * snd_soc_unregister_card - Unregister a card with the ASoC core
2869 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002870 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002871 *
2872 * Note that currently this is an internal only function: it will be
2873 * exposed to machine drivers after further backporting of ASoC v2
2874 * registration APIs.
2875 */
2876static int snd_soc_unregister_card(struct snd_soc_card *card)
2877{
2878 mutex_lock(&client_mutex);
2879 list_del(&card->list);
2880 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002881 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2882
2883 return 0;
2884}
2885
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002886/*
2887 * Simplify DAI link configuration by removing ".-1" from device names
2888 * and sanitizing names.
2889 */
2890static inline char *fmt_single_name(struct device *dev, int *id)
2891{
2892 char *found, name[NAME_SIZE];
2893 int id1, id2;
2894
2895 if (dev_name(dev) == NULL)
2896 return NULL;
2897
2898 strncpy(name, dev_name(dev), NAME_SIZE);
2899
2900 /* are we a "%s.%d" name (platform and SPI components) */
2901 found = strstr(name, dev->driver->name);
2902 if (found) {
2903 /* get ID */
2904 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2905
2906 /* discard ID from name if ID == -1 */
2907 if (*id == -1)
2908 found[strlen(dev->driver->name)] = '\0';
2909 }
2910
2911 } else {
2912 /* I2C component devices are named "bus-addr" */
2913 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2914 char tmp[NAME_SIZE];
2915
2916 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002917 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002918
2919 /* sanitize component name for DAI link creation */
2920 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2921 strncpy(name, tmp, NAME_SIZE);
2922 } else
2923 *id = 0;
2924 }
2925
2926 return kstrdup(name, GFP_KERNEL);
2927}
2928
2929/*
2930 * Simplify DAI link naming for single devices with multiple DAIs by removing
2931 * any ".-1" and using the DAI name (instead of device name).
2932 */
2933static inline char *fmt_multiple_name(struct device *dev,
2934 struct snd_soc_dai_driver *dai_drv)
2935{
2936 if (dai_drv->name == NULL) {
2937 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2938 dev_name(dev));
2939 return NULL;
2940 }
2941
2942 return kstrdup(dai_drv->name, GFP_KERNEL);
2943}
2944
Mark Brown91151712008-11-30 23:31:24 +00002945/**
2946 * snd_soc_register_dai - Register a DAI with the ASoC core
2947 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002948 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002949 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002950int snd_soc_register_dai(struct device *dev,
2951 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00002952{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002953 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00002954
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002955 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00002956
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002957 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2958 if (dai == NULL)
2959 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08002960
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002961 /* create DAI component name */
2962 dai->name = fmt_single_name(dev, &dai->id);
2963 if (dai->name == NULL) {
2964 kfree(dai);
2965 return -ENOMEM;
2966 }
2967
2968 dai->dev = dev;
2969 dai->driver = dai_drv;
2970 if (!dai->driver->ops)
2971 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00002972
2973 mutex_lock(&client_mutex);
2974 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002975 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002976 mutex_unlock(&client_mutex);
2977
2978 pr_debug("Registered DAI '%s'\n", dai->name);
2979
2980 return 0;
2981}
2982EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2983
2984/**
2985 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2986 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002987 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002988 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002989void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00002990{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002991 struct snd_soc_dai *dai;
2992
2993 list_for_each_entry(dai, &dai_list, list) {
2994 if (dev == dai->dev)
2995 goto found;
2996 }
2997 return;
2998
2999found:
Mark Brown91151712008-11-30 23:31:24 +00003000 mutex_lock(&client_mutex);
3001 list_del(&dai->list);
3002 mutex_unlock(&client_mutex);
3003
3004 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003005 kfree(dai->name);
3006 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003007}
3008EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3009
3010/**
3011 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3012 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003013 * @dai: Array of DAIs to register
3014 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003015 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003016int snd_soc_register_dais(struct device *dev,
3017 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003018{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003019 struct snd_soc_dai *dai;
3020 int i, ret = 0;
3021
Liam Girdwood720ffa42010-08-18 00:30:30 +01003022 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003023
3024 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003025
3026 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003027 if (dai == NULL) {
3028 ret = -ENOMEM;
3029 goto err;
3030 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003031
3032 /* create DAI component name */
3033 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3034 if (dai->name == NULL) {
3035 kfree(dai);
3036 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003037 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003038 }
3039
3040 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003041 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003042 if (dai->driver->id)
3043 dai->id = dai->driver->id;
3044 else
3045 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003046 if (!dai->driver->ops)
3047 dai->driver->ops = &null_dai_ops;
3048
3049 mutex_lock(&client_mutex);
3050 list_add(&dai->list, &dai_list);
3051 mutex_unlock(&client_mutex);
3052
3053 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003054 }
3055
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003056 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003057 return 0;
3058
3059err:
3060 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003061 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003062
3063 return ret;
3064}
3065EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3066
3067/**
3068 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3069 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003070 * @dai: Array of DAIs to unregister
3071 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003072 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003073void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003074{
3075 int i;
3076
3077 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003078 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003079}
3080EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3081
Mark Brown12a48a8c2008-12-03 19:40:30 +00003082/**
3083 * snd_soc_register_platform - Register a platform with the ASoC core
3084 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003085 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003086 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003087int snd_soc_register_platform(struct device *dev,
3088 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003089{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003090 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003091
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003092 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3093
3094 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3095 if (platform == NULL)
3096 return -ENOMEM;
3097
3098 /* create platform component name */
3099 platform->name = fmt_single_name(dev, &platform->id);
3100 if (platform->name == NULL) {
3101 kfree(platform);
3102 return -ENOMEM;
3103 }
3104
3105 platform->dev = dev;
3106 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003107
3108 mutex_lock(&client_mutex);
3109 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003110 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003111 mutex_unlock(&client_mutex);
3112
3113 pr_debug("Registered platform '%s'\n", platform->name);
3114
3115 return 0;
3116}
3117EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3118
3119/**
3120 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3121 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003122 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003123 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003124void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003125{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003126 struct snd_soc_platform *platform;
3127
3128 list_for_each_entry(platform, &platform_list, list) {
3129 if (dev == platform->dev)
3130 goto found;
3131 }
3132 return;
3133
3134found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003135 mutex_lock(&client_mutex);
3136 list_del(&platform->list);
3137 mutex_unlock(&client_mutex);
3138
3139 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003140 kfree(platform->name);
3141 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003142}
3143EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3144
Mark Brown151ab222009-05-09 16:22:58 +01003145static u64 codec_format_map[] = {
3146 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3147 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3148 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3149 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3150 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3151 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3152 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3153 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3154 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3155 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3156 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3157 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3158 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3159 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3160 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3161 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3162};
3163
3164/* Fix up the DAI formats for endianness: codecs don't actually see
3165 * the endianness of the data but we're using the CPU format
3166 * definitions which do need to include endianness so we ensure that
3167 * codec DAIs always have both big and little endian variants set.
3168 */
3169static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3170{
3171 int i;
3172
3173 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3174 if (stream->formats & codec_format_map[i])
3175 stream->formats |= codec_format_map[i];
3176}
3177
Mark Brown0d0cf002008-12-10 14:32:45 +00003178/**
3179 * snd_soc_register_codec - Register a codec with the ASoC core
3180 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003181 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003182 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003183int snd_soc_register_codec(struct device *dev,
3184 struct snd_soc_codec_driver *codec_drv,
3185 struct snd_soc_dai_driver *dai_drv, int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003186{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003187 struct snd_soc_codec *codec;
3188 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003189
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003190 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003191
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003192 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3193 if (codec == NULL)
3194 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003195
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003196 /* create CODEC component name */
3197 codec->name = fmt_single_name(dev, &codec->id);
3198 if (codec->name == NULL) {
3199 kfree(codec);
3200 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003201 }
3202
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003203 /* allocate CODEC register cache */
3204 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3205
3206 if (codec_drv->reg_cache_default)
3207 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3208 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3209 else
3210 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3211 codec_drv->reg_word_size, GFP_KERNEL);
3212
3213 if (codec->reg_cache == NULL) {
3214 kfree(codec->name);
3215 kfree(codec);
3216 return -ENOMEM;
3217 }
3218 }
3219
3220 codec->dev = dev;
3221 codec->driver = codec_drv;
3222 codec->bias_level = SND_SOC_BIAS_OFF;
3223 codec->num_dai = num_dai;
3224 mutex_init(&codec->mutex);
3225 INIT_LIST_HEAD(&codec->dapm_widgets);
3226 INIT_LIST_HEAD(&codec->dapm_paths);
3227
3228 for (i = 0; i < num_dai; i++) {
3229 fixup_codec_formats(&dai_drv[i].playback);
3230 fixup_codec_formats(&dai_drv[i].capture);
3231 }
3232
Mark Brown26b01cc2010-08-18 20:20:55 +01003233 /* register any DAIs */
3234 if (num_dai) {
3235 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3236 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003237 goto error;
Mark Brown26b01cc2010-08-18 20:20:55 +01003238 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003239
Mark Brown0d0cf002008-12-10 14:32:45 +00003240 mutex_lock(&client_mutex);
3241 list_add(&codec->list, &codec_list);
3242 snd_soc_instantiate_cards();
3243 mutex_unlock(&client_mutex);
3244
3245 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003246 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003247
3248error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003249 if (codec->reg_cache)
3250 kfree(codec->reg_cache);
3251 kfree(codec->name);
3252 kfree(codec);
3253 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003254}
3255EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3256
3257/**
3258 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3259 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003260 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003261 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003262void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003263{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003264 struct snd_soc_codec *codec;
3265 int i;
3266
3267 list_for_each_entry(codec, &codec_list, list) {
3268 if (dev == codec->dev)
3269 goto found;
3270 }
3271 return;
3272
3273found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003274 if (codec->num_dai)
3275 for (i = 0; i < codec->num_dai; i++)
3276 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003277
Mark Brown0d0cf002008-12-10 14:32:45 +00003278 mutex_lock(&client_mutex);
3279 list_del(&codec->list);
3280 mutex_unlock(&client_mutex);
3281
3282 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003283
3284 if (codec->reg_cache)
3285 kfree(codec->reg_cache);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003286 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003287 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003288}
3289EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3290
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003291static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003292{
Mark Brown384c89e2008-12-03 17:34:03 +00003293#ifdef CONFIG_DEBUG_FS
3294 debugfs_root = debugfs_create_dir("asoc", NULL);
3295 if (IS_ERR(debugfs_root) || !debugfs_root) {
3296 printk(KERN_WARNING
3297 "ASoC: Failed to create debugfs directory\n");
3298 debugfs_root = NULL;
3299 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003300
3301 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3302 &codec_list_fops))
3303 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3304
Mark Brownf3208782010-09-15 18:19:07 +01003305 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3306 &dai_list_fops))
3307 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003308
3309 if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
3310 &platform_list_fops))
3311 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003312#endif
3313
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003314 return platform_driver_register(&soc_driver);
3315}
Mark Brown4abe8e12010-10-12 17:41:03 +01003316module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003317
Mark Brown7d8c16a2008-11-30 22:11:24 +00003318static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003319{
Mark Brown384c89e2008-12-03 17:34:03 +00003320#ifdef CONFIG_DEBUG_FS
3321 debugfs_remove_recursive(debugfs_root);
3322#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003323 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003324}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003325module_exit(snd_soc_exit);
3326
3327/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003328MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003329MODULE_DESCRIPTION("ALSA SoC Core");
3330MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003331MODULE_ALIAS("platform:soc-audio");