blob: 85b7d548f1675f14c1a2065bbcffcf7d229a2e27 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Marek Vasut474828a2009-07-22 13:01:03 +020034#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020035#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
39#include <sound/soc-dapm.h>
40#include <sound/initval.h>
41
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000042#define NAME_SIZE 32
43
Frank Mandarinodb2a4162006-10-06 18:31:09 +020044static DEFINE_MUTEX(pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +020045static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
46
Mark Brown384c89e2008-12-03 17:34:03 +000047#ifdef CONFIG_DEBUG_FS
48static struct dentry *debugfs_root;
49#endif
50
Mark Brownc5af3a22008-11-28 13:29:45 +000051static DEFINE_MUTEX(client_mutex);
52static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000053static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000054static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000055static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000056
57static int snd_soc_register_card(struct snd_soc_card *card);
58static int snd_soc_unregister_card(struct snd_soc_card *card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000059static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
Mark Brownc5af3a22008-11-28 13:29:45 +000060
Frank Mandarinodb2a4162006-10-06 18:31:09 +020061/*
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
65 */
66static int pmdown_time = 5000;
67module_param(pmdown_time, int, 0);
68MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69
Liam Girdwood965ac422007-01-31 14:14:57 +010070/*
71 * This function forces any delayed work to be queued and run.
72 */
73static int run_delayed_work(struct delayed_work *dwork)
74{
75 int ret;
76
77 /* cancel any work waiting to be queued. */
78 ret = cancel_delayed_work(dwork);
79
80 /* if there was any work waiting then we run it now and
81 * wait for it's completion */
82 if (ret) {
83 schedule_delayed_work(dwork, 0);
84 flush_scheduled_work();
85 }
86 return ret;
87}
88
Mark Brown2624d5f2009-11-03 21:56:13 +000089/* codec register dump */
90static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
91{
Mark Brown5164d742010-07-14 16:14:33 +010092 int ret, i, step = 1, count = 0;
Mark Brown2624d5f2009-11-03 21:56:13 +000093
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000094 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +000095 return 0;
96
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000097 if (codec->driver->reg_cache_step)
98 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +000099
100 count += sprintf(buf, "%s registers\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000101 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
102 if (codec->driver->readable_register && !codec->driver->readable_register(i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000103 continue;
104
105 count += sprintf(buf + count, "%2x: ", i);
106 if (count >= PAGE_SIZE - 1)
107 break;
108
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000109 if (codec->driver->display_register) {
110 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000111 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100112 } else {
113 /* If the read fails it's almost certainly due to
114 * the register being volatile and the device being
115 * powered off.
116 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000117 ret = codec->driver->read(codec, i);
Mark Brown5164d742010-07-14 16:14:33 +0100118 if (ret >= 0)
119 count += snprintf(buf + count,
120 PAGE_SIZE - count,
121 "%4x", ret);
122 else
123 count += snprintf(buf + count,
124 PAGE_SIZE - count,
125 "<no data: %d>", ret);
126 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000127
128 if (count >= PAGE_SIZE - 1)
129 break;
130
131 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
132 if (count >= PAGE_SIZE - 1)
133 break;
134 }
135
136 /* Truncate count; min() would cause a warning */
137 if (count >= PAGE_SIZE)
138 count = PAGE_SIZE - 1;
139
140 return count;
141}
142static ssize_t codec_reg_show(struct device *dev,
143 struct device_attribute *attr, char *buf)
144{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000145 struct snd_soc_pcm_runtime *rtd =
146 container_of(dev, struct snd_soc_pcm_runtime, dev);
147
148 return soc_codec_reg_show(rtd->codec, buf);
Mark Brown2624d5f2009-11-03 21:56:13 +0000149}
150
151static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
152
Mark Browndbe21402010-02-12 11:37:24 +0000153static ssize_t pmdown_time_show(struct device *dev,
154 struct device_attribute *attr, char *buf)
155{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000156 struct snd_soc_pcm_runtime *rtd =
157 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Browndbe21402010-02-12 11:37:24 +0000158
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000159 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000160}
161
162static ssize_t pmdown_time_set(struct device *dev,
163 struct device_attribute *attr,
164 const char *buf, size_t count)
165{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000166 struct snd_soc_pcm_runtime *rtd =
167 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Brownc593b522010-10-27 20:11:17 -0700168 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000169
Mark Brownc593b522010-10-27 20:11:17 -0700170 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
171 if (ret)
172 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000173
174 return count;
175}
176
177static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
178
Mark Brown2624d5f2009-11-03 21:56:13 +0000179#ifdef CONFIG_DEBUG_FS
180static int codec_reg_open_file(struct inode *inode, struct file *file)
181{
182 file->private_data = inode->i_private;
183 return 0;
184}
185
186static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
187 size_t count, loff_t *ppos)
188{
189 ssize_t ret;
190 struct snd_soc_codec *codec = file->private_data;
191 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
192 if (!buf)
193 return -ENOMEM;
194 ret = soc_codec_reg_show(codec, buf);
195 if (ret >= 0)
196 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
197 kfree(buf);
198 return ret;
199}
200
201static ssize_t codec_reg_write_file(struct file *file,
202 const char __user *user_buf, size_t count, loff_t *ppos)
203{
204 char buf[32];
205 int buf_size;
206 char *start = buf;
207 unsigned long reg, value;
208 int step = 1;
209 struct snd_soc_codec *codec = file->private_data;
210
211 buf_size = min(count, (sizeof(buf)-1));
212 if (copy_from_user(buf, user_buf, buf_size))
213 return -EFAULT;
214 buf[buf_size] = 0;
215
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000216 if (codec->driver->reg_cache_step)
217 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000218
219 while (*start == ' ')
220 start++;
221 reg = simple_strtoul(start, &start, 16);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000222 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
Mark Brown2624d5f2009-11-03 21:56:13 +0000223 return -EINVAL;
224 while (*start == ' ')
225 start++;
226 if (strict_strtoul(start, 16, &value))
227 return -EINVAL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000228 codec->driver->write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000229 return buf_size;
230}
231
232static const struct file_operations codec_reg_fops = {
233 .open = codec_reg_open_file,
234 .read = codec_reg_read_file,
235 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200236 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000237};
238
239static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
240{
Mark Brown6ba6c9c2010-08-12 15:49:52 +0100241 codec->debugfs_codec_root = debugfs_create_dir(codec->name ,
Mark Brown2624d5f2009-11-03 21:56:13 +0000242 debugfs_root);
243 if (!codec->debugfs_codec_root) {
244 printk(KERN_WARNING
245 "ASoC: Failed to create codec debugfs directory\n");
246 return;
247 }
248
249 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
250 codec->debugfs_codec_root,
251 codec, &codec_reg_fops);
252 if (!codec->debugfs_reg)
253 printk(KERN_WARNING
254 "ASoC: Failed to create codec register debugfs file\n");
255
Axel Lin708fafb2010-08-27 10:34:27 +0800256 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
Mark Brown2624d5f2009-11-03 21:56:13 +0000257 codec->debugfs_codec_root,
258 &codec->pop_time);
259 if (!codec->debugfs_pop_time)
260 printk(KERN_WARNING
261 "Failed to create pop time debugfs file\n");
262
263 codec->debugfs_dapm = debugfs_create_dir("dapm",
264 codec->debugfs_codec_root);
265 if (!codec->debugfs_dapm)
266 printk(KERN_WARNING
267 "Failed to create DAPM debugfs directory\n");
268
269 snd_soc_dapm_debugfs_init(codec);
270}
271
272static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
273{
274 debugfs_remove_recursive(codec->debugfs_codec_root);
275}
276
Mark Brownc3c5a192010-09-15 18:15:14 +0100277static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
278 size_t count, loff_t *ppos)
279{
280 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100281 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100282 struct snd_soc_codec *codec;
283
284 if (!buf)
285 return -ENOMEM;
286
Mark Brown2b194f9d2010-10-13 10:52:16 +0100287 list_for_each_entry(codec, &codec_list, list) {
288 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
289 codec->name);
290 if (len >= 0)
291 ret += len;
292 if (ret > PAGE_SIZE) {
293 ret = PAGE_SIZE;
294 break;
295 }
296 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100297
298 if (ret >= 0)
299 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
300
301 kfree(buf);
302
303 return ret;
304}
305
306static const struct file_operations codec_list_fops = {
307 .read = codec_list_read_file,
308 .llseek = default_llseek,/* read accesses f_pos */
309};
310
Mark Brownf3208782010-09-15 18:19:07 +0100311static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
312 size_t count, loff_t *ppos)
313{
314 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100315 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100316 struct snd_soc_dai *dai;
317
318 if (!buf)
319 return -ENOMEM;
320
Mark Brown2b194f9d2010-10-13 10:52:16 +0100321 list_for_each_entry(dai, &dai_list, list) {
322 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
323 if (len >= 0)
324 ret += len;
325 if (ret > PAGE_SIZE) {
326 ret = PAGE_SIZE;
327 break;
328 }
329 }
Mark Brownf3208782010-09-15 18:19:07 +0100330
Mark Brown2b194f9d2010-10-13 10:52:16 +0100331 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100332
333 kfree(buf);
334
335 return ret;
336}
337
338static const struct file_operations dai_list_fops = {
339 .read = dai_list_read_file,
340 .llseek = default_llseek,/* read accesses f_pos */
341};
342
Mark Brown19c7ac22010-09-15 18:22:40 +0100343static ssize_t platform_list_read_file(struct file *file,
344 char __user *user_buf,
345 size_t count, loff_t *ppos)
346{
347 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100348 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100349 struct snd_soc_platform *platform;
350
351 if (!buf)
352 return -ENOMEM;
353
Mark Brown2b194f9d2010-10-13 10:52:16 +0100354 list_for_each_entry(platform, &platform_list, list) {
355 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
356 platform->name);
357 if (len >= 0)
358 ret += len;
359 if (ret > PAGE_SIZE) {
360 ret = PAGE_SIZE;
361 break;
362 }
363 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100364
Mark Brown2b194f9d2010-10-13 10:52:16 +0100365 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100366
367 kfree(buf);
368
369 return ret;
370}
371
372static const struct file_operations platform_list_fops = {
373 .read = platform_list_read_file,
374 .llseek = default_llseek,/* read accesses f_pos */
375};
376
Mark Brown2624d5f2009-11-03 21:56:13 +0000377#else
378
379static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
380{
381}
382
383static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
384{
385}
386#endif
387
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200388#ifdef CONFIG_SND_SOC_AC97_BUS
389/* unregister ac97 codec */
390static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
391{
392 if (codec->ac97->dev.bus)
393 device_unregister(&codec->ac97->dev);
394 return 0;
395}
396
397/* stop no dev release warning */
398static void soc_ac97_device_release(struct device *dev){}
399
400/* register ac97 codec to bus */
401static int soc_ac97_dev_register(struct snd_soc_codec *codec)
402{
403 int err;
404
405 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100406 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200407 codec->ac97->dev.release = soc_ac97_device_release;
408
Kay Sieversbb072bf2008-11-02 03:50:35 +0100409 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000410 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200411 err = device_register(&codec->ac97->dev);
412 if (err < 0) {
413 snd_printk(KERN_ERR "Can't register ac97 bus\n");
414 codec->ac97->dev.bus = NULL;
415 return err;
416 }
417 return 0;
418}
419#endif
420
Mark Brown06f409d2009-04-07 18:10:13 +0100421static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
422{
423 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000424 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
425 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100426 int ret;
427
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000428 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
429 rtd->dai_link->symmetric_rates) {
430 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
431 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100432
433 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
434 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000435 rtd->rate,
436 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100437 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000438 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100439 "Unable to apply rate symmetry constraint: %d\n", ret);
440 return ret;
441 }
442 }
443
444 return 0;
445}
446
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200447/*
448 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
449 * then initialized and any private data can be allocated. This also calls
450 * startup for the cpu DAI, platform, machine and codec DAI.
451 */
452static int soc_pcm_open(struct snd_pcm_substream *substream)
453{
454 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200455 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000456 struct snd_soc_platform *platform = rtd->platform;
457 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
458 struct snd_soc_dai *codec_dai = rtd->codec_dai;
459 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
460 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200461 int ret = 0;
462
463 mutex_lock(&pcm_mutex);
464
465 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000466 if (cpu_dai->driver->ops->startup) {
467 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200468 if (ret < 0) {
469 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100470 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200471 goto out;
472 }
473 }
474
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000475 if (platform->driver->ops->open) {
476 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200477 if (ret < 0) {
478 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
479 goto platform_err;
480 }
481 }
482
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000483 if (codec_dai->driver->ops->startup) {
484 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100485 if (ret < 0) {
486 printk(KERN_ERR "asoc: can't open codec %s\n",
487 codec_dai->name);
488 goto codec_dai_err;
489 }
490 }
491
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000492 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
493 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200494 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000495 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200496 goto machine_err;
497 }
498 }
499
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200500 /* Check that the codec and cpu DAI's are compatible */
501 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
502 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000503 max(codec_dai_drv->playback.rate_min,
504 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200505 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000506 min(codec_dai_drv->playback.rate_max,
507 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200508 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000509 max(codec_dai_drv->playback.channels_min,
510 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200511 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000512 min(codec_dai_drv->playback.channels_max,
513 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100514 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000515 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100516 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000517 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
518 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900519 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000520 runtime->hw.rates |= cpu_dai_drv->playback.rates;
521 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900522 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000523 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200524 } else {
525 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000526 max(codec_dai_drv->capture.rate_min,
527 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200528 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000529 min(codec_dai_drv->capture.rate_max,
530 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532 max(codec_dai_drv->capture.channels_min,
533 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200534 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000535 min(codec_dai_drv->capture.channels_max,
536 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100537 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000538 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100539 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000540 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
541 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900542 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000543 runtime->hw.rates |= cpu_dai_drv->capture.rates;
544 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900545 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000546 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200547 }
548
549 snd_pcm_limit_hw_rates(runtime);
550 if (!runtime->hw.rates) {
551 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100552 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900553 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554 }
555 if (!runtime->hw.formats) {
556 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100557 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900558 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200559 }
560 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
561 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900563 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564 }
565
Mark Brown06f409d2009-04-07 18:10:13 +0100566 /* Symmetry only applies if we've already got an active stream. */
567 if (cpu_dai->active || codec_dai->active) {
568 ret = soc_pcm_apply_symmetry(substream);
569 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900570 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100571 }
572
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000573 pr_debug("asoc: %s <-> %s info:\n",
574 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100575 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
576 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
577 runtime->hw.channels_max);
578 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
579 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200580
Jassi Brar14dc5732010-02-26 09:12:32 +0900581 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000582 cpu_dai->playback_active++;
583 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900584 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000585 cpu_dai->capture_active++;
586 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900587 }
588 cpu_dai->active++;
589 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000590 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200591 mutex_unlock(&pcm_mutex);
592 return 0;
593
Jassi Brarbb1c0472010-02-25 11:24:53 +0900594config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000595 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
596 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200597
Jassi Brarbb1c0472010-02-25 11:24:53 +0900598machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 if (codec_dai->driver->ops->shutdown)
600 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900601
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100602codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000603 if (platform->driver->ops->close)
604 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200605
606platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000607 if (cpu_dai->driver->ops->shutdown)
608 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200609out:
610 mutex_unlock(&pcm_mutex);
611 return ret;
612}
613
614/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200615 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200616 * This is to ensure there are no pops or clicks in between any music tracks
617 * due to DAPM power cycling.
618 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100619static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200620{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 struct snd_soc_pcm_runtime *rtd =
622 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
623 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200624
625 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200626
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
628 codec_dai->driver->playback.stream_name,
629 codec_dai->playback_active ? "active" : "inactive",
630 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200631
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000632 /* are we waiting on this codec DAI stream */
633 if (codec_dai->pop_wait == 1) {
634 codec_dai->pop_wait = 0;
635 snd_soc_dapm_stream_event(rtd,
636 codec_dai->driver->playback.stream_name,
637 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200638 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200640 mutex_unlock(&pcm_mutex);
641}
642
643/*
644 * Called by ALSA when a PCM substream is closed. Private data can be
645 * freed here. The cpu DAI, codec DAI, machine and platform are also
646 * shutdown.
647 */
648static int soc_codec_close(struct snd_pcm_substream *substream)
649{
650 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000651 struct snd_soc_platform *platform = rtd->platform;
652 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
653 struct snd_soc_dai *codec_dai = rtd->codec_dai;
654 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200655
656 mutex_lock(&pcm_mutex);
657
Jassi Brar14dc5732010-02-26 09:12:32 +0900658 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000659 cpu_dai->playback_active--;
660 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900661 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000662 cpu_dai->capture_active--;
663 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200664 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900665
666 cpu_dai->active--;
667 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200668 codec->active--;
669
Mark Brown6010b2d2008-09-06 18:33:24 +0100670 /* Muting the DAC suppresses artifacts caused during digital
671 * shutdown, for example from stopping clocks.
672 */
673 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
674 snd_soc_dai_digital_mute(codec_dai, 1);
675
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000676 if (cpu_dai->driver->ops->shutdown)
677 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200678
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000679 if (codec_dai->driver->ops->shutdown)
680 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200681
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000682 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
683 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200684
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000685 if (platform->driver->ops->close)
686 platform->driver->ops->close(substream);
687 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200688
689 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
690 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100691 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000692 schedule_delayed_work(&rtd->delayed_work,
693 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200694 } else {
695 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 snd_soc_dapm_stream_event(rtd,
697 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100698 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699 }
700
701 mutex_unlock(&pcm_mutex);
702 return 0;
703}
704
705/*
706 * Called by ALSA when the PCM substream is prepared, can set format, sample
707 * rate, etc. This function is non atomic and can be called multiple times,
708 * it can refer to the runtime info.
709 */
710static int soc_pcm_prepare(struct snd_pcm_substream *substream)
711{
712 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713 struct snd_soc_platform *platform = rtd->platform;
714 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
715 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716 int ret = 0;
717
718 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100719
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000720 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
721 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100722 if (ret < 0) {
723 printk(KERN_ERR "asoc: machine prepare error\n");
724 goto out;
725 }
726 }
727
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000728 if (platform->driver->ops->prepare) {
729 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200730 if (ret < 0) {
731 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200732 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200733 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200734 }
735
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000736 if (codec_dai->driver->ops->prepare) {
737 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200738 if (ret < 0) {
739 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200740 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200741 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200742 }
743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000744 if (cpu_dai->driver->ops->prepare) {
745 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100746 if (ret < 0) {
747 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
748 goto out;
749 }
750 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200751
Mark Brownd45f6212008-10-14 13:58:36 +0100752 /* cancel any delayed stream shutdown that is pending */
753 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
754 codec_dai->pop_wait) {
755 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100757 }
758
Mark Brown452c5ea2009-05-17 21:41:23 +0100759 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000760 snd_soc_dapm_stream_event(rtd,
761 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100762 SND_SOC_DAPM_STREAM_START);
763 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000764 snd_soc_dapm_stream_event(rtd,
765 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100766 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100767
Mark Brown452c5ea2009-05-17 21:41:23 +0100768 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200769
770out:
771 mutex_unlock(&pcm_mutex);
772 return ret;
773}
774
775/*
776 * Called by ALSA when the hardware params are set by application. This
777 * function can also be called multiple times and can allocate buffers
778 * (using snd_pcm_lib_* ). It's non-atomic.
779 */
780static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
781 struct snd_pcm_hw_params *params)
782{
783 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000784 struct snd_soc_platform *platform = rtd->platform;
785 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
786 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200787 int ret = 0;
788
789 mutex_lock(&pcm_mutex);
790
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000791 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
792 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200793 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100794 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200795 goto out;
796 }
797 }
798
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000799 if (codec_dai->driver->ops->hw_params) {
800 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100801 if (ret < 0) {
802 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
803 codec_dai->name);
804 goto codec_err;
805 }
806 }
807
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000808 if (cpu_dai->driver->ops->hw_params) {
809 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200810 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200811 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100812 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200813 goto interface_err;
814 }
815 }
816
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000817 if (platform->driver->ops->hw_params) {
818 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200819 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200820 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200821 platform->name);
822 goto platform_err;
823 }
824 }
825
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100827
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200828out:
829 mutex_unlock(&pcm_mutex);
830 return ret;
831
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200832platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000833 if (cpu_dai->driver->ops->hw_free)
834 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200835
836interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000837 if (codec_dai->driver->ops->hw_free)
838 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100839
840codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000841 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
842 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200843
844 mutex_unlock(&pcm_mutex);
845 return ret;
846}
847
848/*
849 * Free's resources allocated by hw_params, can be called multiple times
850 */
851static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
852{
853 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000854 struct snd_soc_platform *platform = rtd->platform;
855 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
856 struct snd_soc_dai *codec_dai = rtd->codec_dai;
857 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858
859 mutex_lock(&pcm_mutex);
860
861 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100862 if (!codec->active)
863 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200864
865 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000866 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
867 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200868
869 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000870 if (platform->driver->ops->hw_free)
871 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200872
873 /* now free hw params for the DAI's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000874 if (codec_dai->driver->ops->hw_free)
875 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200876
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000877 if (cpu_dai->driver->ops->hw_free)
878 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200879
880 mutex_unlock(&pcm_mutex);
881 return 0;
882}
883
884static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
885{
886 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000887 struct snd_soc_platform *platform = rtd->platform;
888 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
889 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200890 int ret;
891
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000892 if (codec_dai->driver->ops->trigger) {
893 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200894 if (ret < 0)
895 return ret;
896 }
897
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000898 if (platform->driver->ops->trigger) {
899 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200900 if (ret < 0)
901 return ret;
902 }
903
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000904 if (cpu_dai->driver->ops->trigger) {
905 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200906 if (ret < 0)
907 return ret;
908 }
909 return 0;
910}
911
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200912/*
913 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200914 * If cpu_dai, codec_dai, platform driver has the delay callback, than
915 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200916 */
917static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
918{
919 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000920 struct snd_soc_platform *platform = rtd->platform;
921 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
922 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200923 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200924 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200925 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200926
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000927 if (platform->driver->ops->pointer)
928 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200929
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000930 if (cpu_dai->driver->ops->delay)
931 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200932
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000933 if (codec_dai->driver->ops->delay)
934 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200935
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000936 if (platform->driver->delay)
937 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200938
939 runtime->delay = delay;
940
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200941 return offset;
942}
943
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200944/* ASoC PCM operations */
945static struct snd_pcm_ops soc_pcm_ops = {
946 .open = soc_pcm_open,
947 .close = soc_codec_close,
948 .hw_params = soc_pcm_hw_params,
949 .hw_free = soc_pcm_hw_free,
950 .prepare = soc_pcm_prepare,
951 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200952 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200953};
954
955#ifdef CONFIG_PM
956/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100957static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200958{
Mark Brown416356f2009-06-30 19:05:15 +0100959 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000960 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200961 int i;
962
Daniel Macke3509ff2009-06-03 17:44:49 +0200963 /* If the initialization of this soc device failed, there is no codec
964 * associated with it. Just bail out in this case.
965 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000966 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200967 return 0;
968
Andy Green6ed25972008-06-13 16:24:05 +0100969 /* Due to the resume being scheduled into a workqueue we could
970 * suspend before that's finished - wait for it to complete.
971 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000972 snd_power_lock(card->snd_card);
973 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
974 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100975
976 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000977 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100978
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200979 /* mute any active DAC's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000980 for (i = 0; i < card->num_rtd; i++) {
981 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
982 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100983
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000984 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100985 continue;
986
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000987 if (drv->ops->digital_mute && dai->playback_active)
988 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200989 }
990
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100991 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000992 for (i = 0; i < card->num_rtd; i++) {
993 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100994 continue;
995
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000996 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100997 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100998
Mark Brown87506542008-11-18 20:50:34 +0000999 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +01001000 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001001
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002 for (i = 0; i < card->num_rtd; i++) {
1003 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1004 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001005
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001006 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001007 continue;
1008
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001009 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1010 cpu_dai->driver->suspend(cpu_dai);
1011 if (platform->driver->suspend && !platform->suspended) {
1012 platform->driver->suspend(cpu_dai);
1013 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +01001014 }
1015 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001016
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001017 /* close any waiting streams and save state */
1018 for (i = 0; i < card->num_rtd; i++) {
1019 run_delayed_work(&card->rtd[i].delayed_work);
1020 card->rtd[i].codec->suspend_bias_level = card->rtd[i].codec->bias_level;
1021 }
Mark Brown3efab7d2010-05-09 13:25:43 +01001022
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001023 for (i = 0; i < card->num_rtd; i++) {
1024 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1025
1026 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001027 continue;
1028
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001029 if (driver->playback.stream_name != NULL)
1030 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1031 SND_SOC_DAPM_STREAM_SUSPEND);
1032
1033 if (driver->capture.stream_name != NULL)
1034 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1035 SND_SOC_DAPM_STREAM_SUSPEND);
1036 }
1037
1038 /* suspend all CODECs */
1039 for (i = 0; i < card->num_rtd; i++) {
1040 struct snd_soc_codec *codec = card->rtd[i].codec;
1041 /* If there are paths active then the CODEC will be held with
1042 * bias _ON and should not be suspended. */
1043 if (!codec->suspended && codec->driver->suspend) {
1044 switch (codec->bias_level) {
1045 case SND_SOC_BIAS_STANDBY:
1046 case SND_SOC_BIAS_OFF:
1047 codec->driver->suspend(codec, PMSG_SUSPEND);
1048 codec->suspended = 1;
1049 break;
1050 default:
1051 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1052 break;
1053 }
1054 }
1055 }
1056
1057 for (i = 0; i < card->num_rtd; i++) {
1058 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1059
1060 if (card->rtd[i].dai_link->ignore_suspend)
1061 continue;
1062
1063 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1064 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001065 }
1066
Mark Brown87506542008-11-18 20:50:34 +00001067 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +01001068 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001069
1070 return 0;
1071}
1072
Andy Green6ed25972008-06-13 16:24:05 +01001073/* deferred resume work, so resume can complete before we finished
1074 * setting our codec back up, which can be very slow on I2C
1075 */
1076static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001077{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001078 struct snd_soc_card *card =
1079 container_of(work, struct snd_soc_card, deferred_resume_work);
1080 struct platform_device *pdev = to_platform_device(card->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001081 int i;
1082
Andy Green6ed25972008-06-13 16:24:05 +01001083 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1084 * so userspace apps are blocked from touching us
1085 */
1086
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001087 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001088
Mark Brown9949788b2010-05-07 20:24:05 +01001089 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001090 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +01001091
Mark Brown87506542008-11-18 20:50:34 +00001092 if (card->resume_pre)
1093 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001094
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001095 /* resume AC97 DAIs */
1096 for (i = 0; i < card->num_rtd; i++) {
1097 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001098
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001099 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001100 continue;
1101
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001102 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1103 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001104 }
1105
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001106 for (i = 0; i < card->num_rtd; i++) {
1107 struct snd_soc_codec *codec = card->rtd[i].codec;
1108 /* If the CODEC was idle over suspend then it will have been
1109 * left with bias OFF or STANDBY and suspended so we must now
1110 * resume. Otherwise the suspend was suppressed.
1111 */
1112 if (codec->driver->resume && codec->suspended) {
1113 switch (codec->bias_level) {
1114 case SND_SOC_BIAS_STANDBY:
1115 case SND_SOC_BIAS_OFF:
1116 codec->driver->resume(codec);
1117 codec->suspended = 0;
1118 break;
1119 default:
1120 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1121 break;
1122 }
Mark Brown1547aba2010-05-07 21:11:40 +01001123 }
1124 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001125
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001126 for (i = 0; i < card->num_rtd; i++) {
1127 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001128
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001129 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001130 continue;
1131
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001132 if (driver->playback.stream_name != NULL)
1133 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001134 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001135
1136 if (driver->capture.stream_name != NULL)
1137 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001138 SND_SOC_DAPM_STREAM_RESUME);
1139 }
1140
Mark Brown3ff3f642008-05-19 12:32:25 +02001141 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001142 for (i = 0; i < card->num_rtd; i++) {
1143 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1144 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001145
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001146 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001147 continue;
1148
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001149 if (drv->ops->digital_mute && dai->playback_active)
1150 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001151 }
1152
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001153 for (i = 0; i < card->num_rtd; i++) {
1154 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1155 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001156
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001157 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001158 continue;
1159
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001160 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1161 cpu_dai->driver->resume(cpu_dai);
1162 if (platform->driver->resume && platform->suspended) {
1163 platform->driver->resume(cpu_dai);
1164 platform->suspended = 0;
1165 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001166 }
1167
Mark Brown87506542008-11-18 20:50:34 +00001168 if (card->resume_post)
1169 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001170
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001171 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001172
1173 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001174 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001175}
1176
1177/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001178static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001179{
Mark Brown416356f2009-06-30 19:05:15 +01001180 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001181 struct snd_soc_card *card = platform_get_drvdata(pdev);
1182 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001183
Mark Brown64ab9ba2009-03-31 11:27:03 +01001184 /* AC97 devices might have other drivers hanging off them so
1185 * need to resume immediately. Other drivers don't have that
1186 * problem and may take a substantial amount of time to resume
1187 * due to I/O costs and anti-pop so handle them out of line.
1188 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001189 for (i = 0; i < card->num_rtd; i++) {
1190 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1191 if (cpu_dai->driver->ac97_control) {
1192 dev_dbg(dev, "Resuming AC97 immediately\n");
1193 soc_resume_deferred(&card->deferred_resume_work);
1194 } else {
1195 dev_dbg(dev, "Scheduling resume work\n");
1196 if (!schedule_work(&card->deferred_resume_work))
1197 dev_err(dev, "resume work item may be lost\n");
1198 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001199 }
Andy Green6ed25972008-06-13 16:24:05 +01001200
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001201 return 0;
1202}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001203#else
1204#define soc_suspend NULL
1205#define soc_resume NULL
1206#endif
1207
Barry Song02a06d32009-10-16 18:13:38 +08001208static struct snd_soc_dai_ops null_dai_ops = {
1209};
1210
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001211static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001212{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001213 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1214 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001215 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001216 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001217 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001218
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001219 if (rtd->complete)
1220 return 1;
1221 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001222
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001223 /* do we already have the CPU DAI for this link ? */
1224 if (rtd->cpu_dai) {
1225 goto find_codec;
1226 }
1227 /* no, then find CPU DAI from registered DAIs*/
1228 list_for_each_entry(cpu_dai, &dai_list, list) {
1229 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1230
1231 if (!try_module_get(cpu_dai->dev->driver->owner))
1232 return -ENODEV;
1233
1234 rtd->cpu_dai = cpu_dai;
1235 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001236 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001237 }
1238 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1239 dai_link->cpu_dai_name);
1240
1241find_codec:
1242 /* do we already have the CODEC for this link ? */
1243 if (rtd->codec) {
1244 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001245 }
1246
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001247 /* no, then find CODEC from registered CODECs*/
1248 list_for_each_entry(codec, &codec_list, list) {
1249 if (!strcmp(codec->name, dai_link->codec_name)) {
1250 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001251
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001252 if (!try_module_get(codec->dev->driver->owner))
1253 return -ENODEV;
Mark Brown435c5e22008-12-04 15:32:53 +00001254
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001255 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1256 list_for_each_entry(codec_dai, &dai_list, list) {
1257 if (codec->dev == codec_dai->dev &&
1258 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1259 rtd->codec_dai = codec_dai;
1260 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001261 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001262 }
1263 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1264 dai_link->codec_dai_name);
1265
1266 goto find_platform;
1267 }
1268 }
1269 dev_dbg(card->dev, "CODEC %s not registered\n",
1270 dai_link->codec_name);
1271
1272find_platform:
1273 /* do we already have the CODEC DAI for this link ? */
1274 if (rtd->platform) {
1275 goto out;
1276 }
1277 /* no, then find CPU DAI from registered DAIs*/
1278 list_for_each_entry(platform, &platform_list, list) {
1279 if (!strcmp(platform->name, dai_link->platform_name)) {
1280
1281 if (!try_module_get(platform->dev->driver->owner))
1282 return -ENODEV;
1283
1284 rtd->platform = platform;
1285 goto out;
1286 }
1287 }
1288
1289 dev_dbg(card->dev, "platform %s not registered\n",
1290 dai_link->platform_name);
1291 return 0;
1292
1293out:
1294 /* mark rtd as complete if we found all 4 of our client devices */
1295 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1296 rtd->complete = 1;
1297 card->num_rtd++;
1298 }
1299 return 1;
1300}
1301
1302static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1303{
1304 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1305 struct snd_soc_codec *codec = rtd->codec;
1306 struct snd_soc_platform *platform = rtd->platform;
1307 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1308 int err;
1309
1310 /* unregister the rtd device */
1311 if (rtd->dev_registered) {
1312 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1313 device_unregister(&rtd->dev);
1314 rtd->dev_registered = 0;
1315 }
1316
1317 /* remove the CODEC DAI */
1318 if (codec_dai && codec_dai->probed) {
1319 if (codec_dai->driver->remove) {
1320 err = codec_dai->driver->remove(codec_dai);
1321 if (err < 0)
1322 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1323 }
1324 codec_dai->probed = 0;
1325 list_del(&codec_dai->card_list);
1326 }
1327
1328 /* remove the platform */
1329 if (platform && platform->probed) {
1330 if (platform->driver->remove) {
1331 err = platform->driver->remove(platform);
1332 if (err < 0)
1333 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1334 }
1335 platform->probed = 0;
1336 list_del(&platform->card_list);
1337 module_put(platform->dev->driver->owner);
1338 }
1339
1340 /* remove the CODEC */
1341 if (codec && codec->probed) {
1342 if (codec->driver->remove) {
1343 err = codec->driver->remove(codec);
1344 if (err < 0)
1345 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1346 }
1347
1348 /* Make sure all DAPM widgets are freed */
1349 snd_soc_dapm_free(codec);
1350
1351 soc_cleanup_codec_debugfs(codec);
1352 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1353 codec->probed = 0;
1354 list_del(&codec->card_list);
1355 module_put(codec->dev->driver->owner);
1356 }
1357
1358 /* remove the cpu_dai */
1359 if (cpu_dai && cpu_dai->probed) {
1360 if (cpu_dai->driver->remove) {
1361 err = cpu_dai->driver->remove(cpu_dai);
1362 if (err < 0)
1363 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1364 }
1365 cpu_dai->probed = 0;
1366 list_del(&cpu_dai->card_list);
1367 module_put(cpu_dai->dev->driver->owner);
1368 }
1369}
1370
1371static void rtd_release(struct device *dev) {}
1372
1373static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1374{
1375 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1376 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1377 struct snd_soc_codec *codec = rtd->codec;
1378 struct snd_soc_platform *platform = rtd->platform;
1379 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1380 int ret;
1381
1382 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1383
1384 /* config components */
1385 codec_dai->codec = codec;
1386 codec->card = card;
1387 cpu_dai->platform = platform;
1388 rtd->card = card;
1389 rtd->dev.parent = card->dev;
1390 codec_dai->card = card;
1391 cpu_dai->card = card;
1392
1393 /* set default power off timeout */
1394 rtd->pmdown_time = pmdown_time;
1395
1396 /* probe the cpu_dai */
1397 if (!cpu_dai->probed) {
1398 if (cpu_dai->driver->probe) {
1399 ret = cpu_dai->driver->probe(cpu_dai);
1400 if (ret < 0) {
1401 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1402 cpu_dai->name);
1403 return ret;
1404 }
1405 }
1406 cpu_dai->probed = 1;
1407 /* mark cpu_dai as probed and add to card cpu_dai list */
1408 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1409 }
1410
1411 /* probe the CODEC */
1412 if (!codec->probed) {
1413 if (codec->driver->probe) {
1414 ret = codec->driver->probe(codec);
1415 if (ret < 0) {
1416 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1417 codec->name);
1418 return ret;
1419 }
1420 }
Mark Brown13cb61f2010-08-12 15:44:04 +01001421
1422 soc_init_codec_debugfs(codec);
1423
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001424 /* mark codec as probed and add to card codec list */
1425 codec->probed = 1;
1426 list_add(&codec->card_list, &card->codec_dev_list);
1427 }
1428
1429 /* probe the platform */
1430 if (!platform->probed) {
1431 if (platform->driver->probe) {
1432 ret = platform->driver->probe(platform);
1433 if (ret < 0) {
1434 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1435 platform->name);
1436 return ret;
1437 }
1438 }
1439 /* mark platform as probed and add to card platform list */
1440 platform->probed = 1;
1441 list_add(&platform->card_list, &card->platform_dev_list);
1442 }
1443
1444 /* probe the CODEC DAI */
1445 if (!codec_dai->probed) {
1446 if (codec_dai->driver->probe) {
1447 ret = codec_dai->driver->probe(codec_dai);
1448 if (ret < 0) {
1449 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1450 codec_dai->name);
1451 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001452 }
1453 }
1454
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001455 /* mark cpu_dai as probed and add to card cpu_dai list */
1456 codec_dai->probed = 1;
1457 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001458 }
1459
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001460 /* DAPM dai link stream work */
1461 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1462
1463 /* now that all clients have probed, initialise the DAI link */
1464 if (dai_link->init) {
1465 ret = dai_link->init(rtd);
1466 if (ret < 0) {
1467 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1468 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001469 }
1470 }
1471
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001472 /* Make sure all DAPM widgets are instantiated */
1473 snd_soc_dapm_new_widgets(codec);
1474 snd_soc_dapm_sync(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001475
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001476 /* register the rtd device */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001477 rtd->dev.release = rtd_release;
1478 rtd->dev.init_name = dai_link->name;
1479 ret = device_register(&rtd->dev);
1480 if (ret < 0) {
1481 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1482 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001483 }
1484
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001485 rtd->dev_registered = 1;
1486 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1487 if (ret < 0)
1488 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1489
1490 /* add DAPM sysfs entries for this codec */
1491 ret = snd_soc_dapm_sys_add(&rtd->dev);
1492 if (ret < 0)
1493 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1494
1495 /* add codec sysfs entries */
1496 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1497 if (ret < 0)
1498 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1499
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001500 /* create the pcm */
1501 ret = soc_new_pcm(rtd, num);
1502 if (ret < 0) {
1503 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1504 return ret;
1505 }
1506
1507 /* add platform data for AC97 devices */
1508 if (rtd->codec_dai->driver->ac97_control)
1509 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1510
1511 return 0;
1512}
1513
1514#ifdef CONFIG_SND_SOC_AC97_BUS
1515static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1516{
1517 int ret;
1518
1519 /* Only instantiate AC97 if not already done by the adaptor
1520 * for the generic AC97 subsystem.
1521 */
1522 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001523 /*
1524 * It is possible that the AC97 device is already registered to
1525 * the device subsystem. This happens when the device is created
1526 * via snd_ac97_mixer(). Currently only SoC codec that does so
1527 * is the generic AC97 glue but others migh emerge.
1528 *
1529 * In those cases we don't try to register the device again.
1530 */
1531 if (!rtd->codec->ac97_created)
1532 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001533
1534 ret = soc_ac97_dev_register(rtd->codec);
1535 if (ret < 0) {
1536 printk(KERN_ERR "asoc: AC97 device register failed\n");
1537 return ret;
1538 }
1539
1540 rtd->codec->ac97_registered = 1;
1541 }
1542 return 0;
1543}
1544
1545static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1546{
1547 if (codec->ac97_registered) {
1548 soc_ac97_dev_unregister(codec);
1549 codec->ac97_registered = 0;
1550 }
1551}
1552#endif
1553
1554static void snd_soc_instantiate_card(struct snd_soc_card *card)
1555{
1556 struct platform_device *pdev = to_platform_device(card->dev);
1557 int ret, i;
1558
1559 mutex_lock(&card->mutex);
1560
1561 if (card->instantiated) {
1562 mutex_unlock(&card->mutex);
1563 return;
1564 }
1565
1566 /* bind DAIs */
1567 for (i = 0; i < card->num_links; i++)
1568 soc_bind_dai_link(card, i);
1569
1570 /* bind completed ? */
1571 if (card->num_rtd != card->num_links) {
1572 mutex_unlock(&card->mutex);
1573 return;
1574 }
1575
1576 /* card bind complete so register a sound card */
1577 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1578 card->owner, 0, &card->snd_card);
1579 if (ret < 0) {
1580 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1581 card->name);
1582 mutex_unlock(&card->mutex);
1583 return;
1584 }
1585 card->snd_card->dev = card->dev;
1586
Randy Dunlap1301a962008-06-17 19:19:34 +01001587#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001588 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001589 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001590#endif
Andy Green6ed25972008-06-13 16:24:05 +01001591
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001592 /* initialise the sound card only once */
1593 if (card->probe) {
1594 ret = card->probe(pdev);
1595 if (ret < 0)
1596 goto card_probe_error;
1597 }
1598
Mark Brownfe3e78e2009-11-03 22:13:13 +00001599 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001600 ret = soc_probe_dai_link(card, i);
1601 if (ret < 0) {
Mark Brown321de0d2010-09-21 15:09:37 +01001602 pr_err("asoc: failed to instantiate card %s: %d\n",
1603 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001604 goto probe_dai_err;
1605 }
1606 }
1607
1608 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1609 "%s", card->name);
1610 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1611 "%s", card->name);
1612
1613 ret = snd_card_register(card->snd_card);
1614 if (ret < 0) {
1615 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1616 goto probe_dai_err;
1617 }
1618
1619#ifdef CONFIG_SND_SOC_AC97_BUS
1620 /* register any AC97 codecs */
1621 for (i = 0; i < card->num_rtd; i++) {
Axel Lin681e3692010-12-07 20:56:30 +08001622 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1623 if (ret < 0) {
1624 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1625 while (--i >= 0)
1626 soc_unregister_ac97_dai_link(&card->rtd[i]);
1627 goto probe_dai_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001628 }
Axel Lin681e3692010-12-07 20:56:30 +08001629 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001630#endif
1631
Mark Brown435c5e22008-12-04 15:32:53 +00001632 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001633 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001634 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001635
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001636probe_dai_err:
1637 for (i = 0; i < card->num_links; i++)
1638 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001639
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001640card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001641 if (card->remove)
1642 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001643
1644 snd_card_free(card->snd_card);
1645
1646 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001647}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001648
Mark Brown435c5e22008-12-04 15:32:53 +00001649/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001650 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001651 * client_mutex.
1652 */
1653static void snd_soc_instantiate_cards(void)
1654{
1655 struct snd_soc_card *card;
1656 list_for_each_entry(card, &card_list, list)
1657 snd_soc_instantiate_card(card);
1658}
1659
1660/* probes a new socdev */
1661static int soc_probe(struct platform_device *pdev)
1662{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001663 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001664 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001665
1666 /* Bodge while we unpick instantiation */
1667 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001668 INIT_LIST_HEAD(&card->dai_dev_list);
1669 INIT_LIST_HEAD(&card->codec_dev_list);
1670 INIT_LIST_HEAD(&card->platform_dev_list);
1671
Mark Brown435c5e22008-12-04 15:32:53 +00001672 ret = snd_soc_register_card(card);
1673 if (ret != 0) {
1674 dev_err(&pdev->dev, "Failed to register card\n");
1675 return ret;
1676 }
1677
1678 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001679}
1680
1681/* removes a socdev */
1682static int soc_remove(struct platform_device *pdev)
1683{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001684 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001685 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001686
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001687 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001688
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001689 /* make sure any delayed work runs */
1690 for (i = 0; i < card->num_rtd; i++) {
1691 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1692 run_delayed_work(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001693 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001694
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001695 /* remove and free each DAI */
1696 for (i = 0; i < card->num_rtd; i++)
1697 soc_remove_dai_link(card, i);
1698
1699 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001700 if (card->remove)
1701 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001702
1703 kfree(card->rtd);
1704 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001705 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001706 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001707 return 0;
1708}
1709
Mark Brown416356f2009-06-30 19:05:15 +01001710static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001711{
Mark Brown416356f2009-06-30 19:05:15 +01001712 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001713 struct snd_soc_card *card = platform_get_drvdata(pdev);
1714 int i;
Mark Brown51737472009-06-22 13:16:51 +01001715
1716 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001717 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001718
1719 /* Flush out pmdown_time work - we actually do want to run it
1720 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001721 for (i = 0; i < card->num_rtd; i++) {
1722 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1723 run_delayed_work(&rtd->delayed_work);
1724 }
Mark Brown51737472009-06-22 13:16:51 +01001725
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001726 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001727
1728 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001729}
1730
Alexey Dobriyan47145212009-12-14 18:00:08 -08001731static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001732 .suspend = soc_suspend,
1733 .resume = soc_resume,
1734 .poweroff = soc_poweroff,
1735};
1736
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001737/* ASoC platform driver */
1738static struct platform_driver soc_driver = {
1739 .driver = {
1740 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001741 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001742 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001743 },
1744 .probe = soc_probe,
1745 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001746};
1747
1748/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001749static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001750{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001751 struct snd_soc_codec *codec = rtd->codec;
1752 struct snd_soc_platform *platform = rtd->platform;
1753 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1754 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001755 struct snd_pcm *pcm;
1756 char new_name[64];
1757 int ret = 0, playback = 0, capture = 0;
1758
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001759 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001760 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001761 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001762
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001763 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001764 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001765 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001766 capture = 1;
1767
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001768 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1769 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1770 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001771 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001772 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001773 return ret;
1774 }
1775
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001776 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001777 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001778 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1779 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1780 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1781 soc_pcm_ops.copy = platform->driver->ops->copy;
1782 soc_pcm_ops.silence = platform->driver->ops->silence;
1783 soc_pcm_ops.ack = platform->driver->ops->ack;
1784 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001785
1786 if (playback)
1787 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1788
1789 if (capture)
1790 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1791
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001792 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001793 if (ret < 0) {
1794 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001795 return ret;
1796 }
1797
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001798 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001799 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1800 cpu_dai->name);
1801 return ret;
1802}
1803
Mark Brown096e49d2009-07-05 15:12:22 +01001804/**
1805 * snd_soc_codec_volatile_register: Report if a register is volatile.
1806 *
1807 * @codec: CODEC to query.
1808 * @reg: Register to query.
1809 *
1810 * Boolean function indiciating if a CODEC register is volatile.
1811 */
1812int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1813{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001814 if (codec->driver->volatile_register)
1815 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001816 else
1817 return 0;
1818}
1819EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1820
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001821/**
1822 * snd_soc_new_ac97_codec - initailise AC97 device
1823 * @codec: audio codec
1824 * @ops: AC97 bus operations
1825 * @num: AC97 codec number
1826 *
1827 * Initialises AC97 codec resources for use by ad-hoc devices only.
1828 */
1829int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1830 struct snd_ac97_bus_ops *ops, int num)
1831{
1832 mutex_lock(&codec->mutex);
1833
1834 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1835 if (codec->ac97 == NULL) {
1836 mutex_unlock(&codec->mutex);
1837 return -ENOMEM;
1838 }
1839
1840 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1841 if (codec->ac97->bus == NULL) {
1842 kfree(codec->ac97);
1843 codec->ac97 = NULL;
1844 mutex_unlock(&codec->mutex);
1845 return -ENOMEM;
1846 }
1847
1848 codec->ac97->bus->ops = ops;
1849 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001850
1851 /*
1852 * Mark the AC97 device to be created by us. This way we ensure that the
1853 * device will be registered with the device subsystem later on.
1854 */
1855 codec->ac97_created = 1;
1856
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001857 mutex_unlock(&codec->mutex);
1858 return 0;
1859}
1860EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1861
1862/**
1863 * snd_soc_free_ac97_codec - free AC97 codec device
1864 * @codec: audio codec
1865 *
1866 * Frees AC97 codec device resources.
1867 */
1868void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1869{
1870 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001871#ifdef CONFIG_SND_SOC_AC97_BUS
1872 soc_unregister_ac97_dai_link(codec);
1873#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001874 kfree(codec->ac97->bus);
1875 kfree(codec->ac97);
1876 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001877 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001878 mutex_unlock(&codec->mutex);
1879}
1880EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1881
1882/**
1883 * snd_soc_update_bits - update codec register bits
1884 * @codec: audio codec
1885 * @reg: codec register
1886 * @mask: register mask
1887 * @value: new value
1888 *
1889 * Writes new register value.
1890 *
1891 * Returns 1 for change else 0.
1892 */
1893int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001894 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001895{
1896 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001897 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001898
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001899 old = snd_soc_read(codec, reg);
1900 new = (old & ~mask) | value;
1901 change = old != new;
1902 if (change)
1903 snd_soc_write(codec, reg, new);
1904
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001905 return change;
1906}
1907EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1908
1909/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001910 * snd_soc_update_bits_locked - update codec register bits
1911 * @codec: audio codec
1912 * @reg: codec register
1913 * @mask: register mask
1914 * @value: new value
1915 *
1916 * Writes new register value, and takes the codec mutex.
1917 *
1918 * Returns 1 for change else 0.
1919 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001920int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1921 unsigned short reg, unsigned int mask,
1922 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001923{
1924 int change;
1925
1926 mutex_lock(&codec->mutex);
1927 change = snd_soc_update_bits(codec, reg, mask, value);
1928 mutex_unlock(&codec->mutex);
1929
1930 return change;
1931}
Mark Browndd1b3d52009-12-04 14:22:03 +00001932EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001933
1934/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001935 * snd_soc_test_bits - test register for change
1936 * @codec: audio codec
1937 * @reg: codec register
1938 * @mask: register mask
1939 * @value: new value
1940 *
1941 * Tests a register with a new value and checks if the new value is
1942 * different from the old value.
1943 *
1944 * Returns 1 for change else 0.
1945 */
1946int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001947 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001948{
1949 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001950 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001951
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001952 old = snd_soc_read(codec, reg);
1953 new = (old & ~mask) | value;
1954 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001955
1956 return change;
1957}
1958EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1959
1960/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001961 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1962 * @substream: the pcm substream
1963 * @hw: the hardware parameters
1964 *
1965 * Sets the substream runtime hardware parameters.
1966 */
1967int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1968 const struct snd_pcm_hardware *hw)
1969{
1970 struct snd_pcm_runtime *runtime = substream->runtime;
1971 runtime->hw.info = hw->info;
1972 runtime->hw.formats = hw->formats;
1973 runtime->hw.period_bytes_min = hw->period_bytes_min;
1974 runtime->hw.period_bytes_max = hw->period_bytes_max;
1975 runtime->hw.periods_min = hw->periods_min;
1976 runtime->hw.periods_max = hw->periods_max;
1977 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1978 runtime->hw.fifo_size = hw->fifo_size;
1979 return 0;
1980}
1981EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1982
1983/**
1984 * snd_soc_cnew - create new control
1985 * @_template: control template
1986 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001987 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001988 *
1989 * Create a new mixer control from a template control.
1990 *
1991 * Returns 0 for success, else error.
1992 */
1993struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1994 void *data, char *long_name)
1995{
1996 struct snd_kcontrol_new template;
1997
1998 memcpy(&template, _template, sizeof(template));
1999 if (long_name)
2000 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002001 template.index = 0;
2002
2003 return snd_ctl_new1(&template, data);
2004}
2005EXPORT_SYMBOL_GPL(snd_soc_cnew);
2006
2007/**
Ian Molton3e8e1952009-01-09 00:23:21 +00002008 * snd_soc_add_controls - add an array of controls to a codec.
2009 * Convienience function to add a list of controls. Many codecs were
2010 * duplicating this code.
2011 *
2012 * @codec: codec to add controls to
2013 * @controls: array of controls to add
2014 * @num_controls: number of elements in the array
2015 *
2016 * Return 0 for success, else error.
2017 */
2018int snd_soc_add_controls(struct snd_soc_codec *codec,
2019 const struct snd_kcontrol_new *controls, int num_controls)
2020{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002021 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002022 int err, i;
2023
2024 for (i = 0; i < num_controls; i++) {
2025 const struct snd_kcontrol_new *control = &controls[i];
2026 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
2027 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002028 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2029 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002030 return err;
2031 }
2032 }
2033
2034 return 0;
2035}
2036EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2037
2038/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002039 * snd_soc_info_enum_double - enumerated double mixer info callback
2040 * @kcontrol: mixer control
2041 * @uinfo: control element information
2042 *
2043 * Callback to provide information about a double enumerated
2044 * mixer control.
2045 *
2046 * Returns 0 for success.
2047 */
2048int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2049 struct snd_ctl_elem_info *uinfo)
2050{
2051 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2052
2053 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2054 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002055 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002056
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002057 if (uinfo->value.enumerated.item > e->max - 1)
2058 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002059 strcpy(uinfo->value.enumerated.name,
2060 e->texts[uinfo->value.enumerated.item]);
2061 return 0;
2062}
2063EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2064
2065/**
2066 * snd_soc_get_enum_double - enumerated double mixer get callback
2067 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002068 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002069 *
2070 * Callback to get the value of a double enumerated mixer.
2071 *
2072 * Returns 0 for success.
2073 */
2074int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2075 struct snd_ctl_elem_value *ucontrol)
2076{
2077 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2078 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002079 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002080
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002081 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002082 ;
2083 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002084 ucontrol->value.enumerated.item[0]
2085 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002086 if (e->shift_l != e->shift_r)
2087 ucontrol->value.enumerated.item[1] =
2088 (val >> e->shift_r) & (bitmask - 1);
2089
2090 return 0;
2091}
2092EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2093
2094/**
2095 * snd_soc_put_enum_double - enumerated double mixer put callback
2096 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002097 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002098 *
2099 * Callback to set the value of a double enumerated mixer.
2100 *
2101 * Returns 0 for success.
2102 */
2103int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2104 struct snd_ctl_elem_value *ucontrol)
2105{
2106 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2107 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002108 unsigned int val;
2109 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002110
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002111 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002112 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002113 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002114 return -EINVAL;
2115 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2116 mask = (bitmask - 1) << e->shift_l;
2117 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002118 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002119 return -EINVAL;
2120 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2121 mask |= (bitmask - 1) << e->shift_r;
2122 }
2123
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002124 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002125}
2126EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2127
2128/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002129 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2130 * @kcontrol: mixer control
2131 * @ucontrol: control element information
2132 *
2133 * Callback to get the value of a double semi enumerated mixer.
2134 *
2135 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2136 * used for handling bitfield coded enumeration for example.
2137 *
2138 * Returns 0 for success.
2139 */
2140int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2141 struct snd_ctl_elem_value *ucontrol)
2142{
2143 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002144 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002145 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002146
2147 reg_val = snd_soc_read(codec, e->reg);
2148 val = (reg_val >> e->shift_l) & e->mask;
2149 for (mux = 0; mux < e->max; mux++) {
2150 if (val == e->values[mux])
2151 break;
2152 }
2153 ucontrol->value.enumerated.item[0] = mux;
2154 if (e->shift_l != e->shift_r) {
2155 val = (reg_val >> e->shift_r) & e->mask;
2156 for (mux = 0; mux < e->max; mux++) {
2157 if (val == e->values[mux])
2158 break;
2159 }
2160 ucontrol->value.enumerated.item[1] = mux;
2161 }
2162
2163 return 0;
2164}
2165EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2166
2167/**
2168 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2169 * @kcontrol: mixer control
2170 * @ucontrol: control element information
2171 *
2172 * Callback to set the value of a double semi enumerated mixer.
2173 *
2174 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2175 * used for handling bitfield coded enumeration for example.
2176 *
2177 * Returns 0 for success.
2178 */
2179int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2180 struct snd_ctl_elem_value *ucontrol)
2181{
2182 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002183 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002184 unsigned int val;
2185 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002186
2187 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2188 return -EINVAL;
2189 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2190 mask = e->mask << e->shift_l;
2191 if (e->shift_l != e->shift_r) {
2192 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2193 return -EINVAL;
2194 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2195 mask |= e->mask << e->shift_r;
2196 }
2197
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002198 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002199}
2200EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2201
2202/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002203 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2204 * @kcontrol: mixer control
2205 * @uinfo: control element information
2206 *
2207 * Callback to provide information about an external enumerated
2208 * single mixer.
2209 *
2210 * Returns 0 for success.
2211 */
2212int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2213 struct snd_ctl_elem_info *uinfo)
2214{
2215 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2216
2217 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2218 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002219 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002220
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002221 if (uinfo->value.enumerated.item > e->max - 1)
2222 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002223 strcpy(uinfo->value.enumerated.name,
2224 e->texts[uinfo->value.enumerated.item]);
2225 return 0;
2226}
2227EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2228
2229/**
2230 * snd_soc_info_volsw_ext - external single mixer info callback
2231 * @kcontrol: mixer control
2232 * @uinfo: control element information
2233 *
2234 * Callback to provide information about a single external mixer control.
2235 *
2236 * Returns 0 for success.
2237 */
2238int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2239 struct snd_ctl_elem_info *uinfo)
2240{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002241 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002242
Mark Brownfd5dfad2009-04-15 21:37:46 +01002243 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002244 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2245 else
2246 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2247
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002248 uinfo->count = 1;
2249 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002250 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002251 return 0;
2252}
2253EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2254
2255/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002256 * snd_soc_info_volsw - single mixer info callback
2257 * @kcontrol: mixer control
2258 * @uinfo: control element information
2259 *
2260 * Callback to provide information about a single mixer control.
2261 *
2262 * Returns 0 for success.
2263 */
2264int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2265 struct snd_ctl_elem_info *uinfo)
2266{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002267 struct soc_mixer_control *mc =
2268 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002269 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002270 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002271 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002272
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002273 if (!mc->platform_max)
2274 mc->platform_max = mc->max;
2275 platform_max = mc->platform_max;
2276
2277 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002278 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2279 else
2280 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2281
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002282 uinfo->count = shift == rshift ? 1 : 2;
2283 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002284 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002285 return 0;
2286}
2287EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2288
2289/**
2290 * snd_soc_get_volsw - single mixer get callback
2291 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002292 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002293 *
2294 * Callback to get the value of a single mixer control.
2295 *
2296 * Returns 0 for success.
2297 */
2298int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2299 struct snd_ctl_elem_value *ucontrol)
2300{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002301 struct soc_mixer_control *mc =
2302 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002303 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002304 unsigned int reg = mc->reg;
2305 unsigned int shift = mc->shift;
2306 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002307 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002308 unsigned int mask = (1 << fls(max)) - 1;
2309 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002310
2311 ucontrol->value.integer.value[0] =
2312 (snd_soc_read(codec, reg) >> shift) & mask;
2313 if (shift != rshift)
2314 ucontrol->value.integer.value[1] =
2315 (snd_soc_read(codec, reg) >> rshift) & mask;
2316 if (invert) {
2317 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002318 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002319 if (shift != rshift)
2320 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002321 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002322 }
2323
2324 return 0;
2325}
2326EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2327
2328/**
2329 * snd_soc_put_volsw - single mixer put callback
2330 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002331 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002332 *
2333 * Callback to set the value of a single mixer control.
2334 *
2335 * Returns 0 for success.
2336 */
2337int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2338 struct snd_ctl_elem_value *ucontrol)
2339{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002340 struct soc_mixer_control *mc =
2341 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002342 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002343 unsigned int reg = mc->reg;
2344 unsigned int shift = mc->shift;
2345 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002346 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002347 unsigned int mask = (1 << fls(max)) - 1;
2348 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002349 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002350
2351 val = (ucontrol->value.integer.value[0] & mask);
2352 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002353 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002354 val_mask = mask << shift;
2355 val = val << shift;
2356 if (shift != rshift) {
2357 val2 = (ucontrol->value.integer.value[1] & mask);
2358 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002359 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002360 val_mask |= mask << rshift;
2361 val |= val2 << rshift;
2362 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002363 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002364}
2365EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2366
2367/**
2368 * snd_soc_info_volsw_2r - double mixer info callback
2369 * @kcontrol: mixer control
2370 * @uinfo: control element information
2371 *
2372 * Callback to provide information about a double mixer control that
2373 * spans 2 codec registers.
2374 *
2375 * Returns 0 for success.
2376 */
2377int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2378 struct snd_ctl_elem_info *uinfo)
2379{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002380 struct soc_mixer_control *mc =
2381 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002382 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002383
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002384 if (!mc->platform_max)
2385 mc->platform_max = mc->max;
2386 platform_max = mc->platform_max;
2387
2388 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002389 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2390 else
2391 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2392
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002393 uinfo->count = 2;
2394 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002395 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002396 return 0;
2397}
2398EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2399
2400/**
2401 * snd_soc_get_volsw_2r - double mixer get callback
2402 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002403 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002404 *
2405 * Callback to get the value of a double mixer control that spans 2 registers.
2406 *
2407 * Returns 0 for success.
2408 */
2409int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2410 struct snd_ctl_elem_value *ucontrol)
2411{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002412 struct soc_mixer_control *mc =
2413 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002414 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002415 unsigned int reg = mc->reg;
2416 unsigned int reg2 = mc->rreg;
2417 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002418 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002419 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002420 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002421
2422 ucontrol->value.integer.value[0] =
2423 (snd_soc_read(codec, reg) >> shift) & mask;
2424 ucontrol->value.integer.value[1] =
2425 (snd_soc_read(codec, reg2) >> shift) & mask;
2426 if (invert) {
2427 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002428 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002429 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002430 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002431 }
2432
2433 return 0;
2434}
2435EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2436
2437/**
2438 * snd_soc_put_volsw_2r - double mixer set callback
2439 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002440 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002441 *
2442 * Callback to set the value of a double mixer control that spans 2 registers.
2443 *
2444 * Returns 0 for success.
2445 */
2446int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2447 struct snd_ctl_elem_value *ucontrol)
2448{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002449 struct soc_mixer_control *mc =
2450 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002451 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002452 unsigned int reg = mc->reg;
2453 unsigned int reg2 = mc->rreg;
2454 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002455 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002456 unsigned int mask = (1 << fls(max)) - 1;
2457 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002458 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002459 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002460
2461 val_mask = mask << shift;
2462 val = (ucontrol->value.integer.value[0] & mask);
2463 val2 = (ucontrol->value.integer.value[1] & mask);
2464
2465 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002466 val = max - val;
2467 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002468 }
2469
2470 val = val << shift;
2471 val2 = val2 << shift;
2472
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002473 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002474 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002475 return err;
2476
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002477 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002478 return err;
2479}
2480EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2481
Mark Browne13ac2e2008-05-28 17:58:05 +01002482/**
2483 * snd_soc_info_volsw_s8 - signed mixer info callback
2484 * @kcontrol: mixer control
2485 * @uinfo: control element information
2486 *
2487 * Callback to provide information about a signed mixer control.
2488 *
2489 * Returns 0 for success.
2490 */
2491int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2492 struct snd_ctl_elem_info *uinfo)
2493{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002494 struct soc_mixer_control *mc =
2495 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002496 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002497 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002498
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002499 if (!mc->platform_max)
2500 mc->platform_max = mc->max;
2501 platform_max = mc->platform_max;
2502
Mark Browne13ac2e2008-05-28 17:58:05 +01002503 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2504 uinfo->count = 2;
2505 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002506 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002507 return 0;
2508}
2509EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2510
2511/**
2512 * snd_soc_get_volsw_s8 - signed mixer get callback
2513 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002514 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002515 *
2516 * Callback to get the value of a signed mixer control.
2517 *
2518 * Returns 0 for success.
2519 */
2520int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2521 struct snd_ctl_elem_value *ucontrol)
2522{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002523 struct soc_mixer_control *mc =
2524 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002525 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002526 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002527 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002528 int val = snd_soc_read(codec, reg);
2529
2530 ucontrol->value.integer.value[0] =
2531 ((signed char)(val & 0xff))-min;
2532 ucontrol->value.integer.value[1] =
2533 ((signed char)((val >> 8) & 0xff))-min;
2534 return 0;
2535}
2536EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2537
2538/**
2539 * snd_soc_put_volsw_sgn - signed mixer put callback
2540 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002541 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002542 *
2543 * Callback to set the value of a signed mixer control.
2544 *
2545 * Returns 0 for success.
2546 */
2547int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2548 struct snd_ctl_elem_value *ucontrol)
2549{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002550 struct soc_mixer_control *mc =
2551 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002552 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002553 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002554 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002555 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002556
2557 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2558 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2559
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002560 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002561}
2562EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2563
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002564/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002565 * snd_soc_limit_volume - Set new limit to an existing volume control.
2566 *
2567 * @codec: where to look for the control
2568 * @name: Name of the control
2569 * @max: new maximum limit
2570 *
2571 * Return 0 for success, else error.
2572 */
2573int snd_soc_limit_volume(struct snd_soc_codec *codec,
2574 const char *name, int max)
2575{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002576 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002577 struct snd_kcontrol *kctl;
2578 struct soc_mixer_control *mc;
2579 int found = 0;
2580 int ret = -EINVAL;
2581
2582 /* Sanity check for name and max */
2583 if (unlikely(!name || max <= 0))
2584 return -EINVAL;
2585
2586 list_for_each_entry(kctl, &card->controls, list) {
2587 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2588 found = 1;
2589 break;
2590 }
2591 }
2592 if (found) {
2593 mc = (struct soc_mixer_control *)kctl->private_value;
2594 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002595 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002596 ret = 0;
2597 }
2598 }
2599 return ret;
2600}
2601EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2602
2603/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002604 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2605 * mixer info callback
2606 * @kcontrol: mixer control
2607 * @uinfo: control element information
2608 *
2609 * Returns 0 for success.
2610 */
2611int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2612 struct snd_ctl_elem_info *uinfo)
2613{
2614 struct soc_mixer_control *mc =
2615 (struct soc_mixer_control *)kcontrol->private_value;
2616 int max = mc->max;
2617 int min = mc->min;
2618
2619 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2620 uinfo->count = 2;
2621 uinfo->value.integer.min = 0;
2622 uinfo->value.integer.max = max-min;
2623
2624 return 0;
2625}
2626EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2627
2628/**
2629 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2630 * mixer get callback
2631 * @kcontrol: mixer control
2632 * @uinfo: control element information
2633 *
2634 * Returns 0 for success.
2635 */
2636int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2637 struct snd_ctl_elem_value *ucontrol)
2638{
2639 struct soc_mixer_control *mc =
2640 (struct soc_mixer_control *)kcontrol->private_value;
2641 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2642 unsigned int mask = (1<<mc->shift)-1;
2643 int min = mc->min;
2644 int val = snd_soc_read(codec, mc->reg) & mask;
2645 int valr = snd_soc_read(codec, mc->rreg) & mask;
2646
Stuart Longland20630c72010-06-18 12:56:10 +10002647 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2648 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002649 return 0;
2650}
2651EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2652
2653/**
2654 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2655 * mixer put callback
2656 * @kcontrol: mixer control
2657 * @uinfo: control element information
2658 *
2659 * Returns 0 for success.
2660 */
2661int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2662 struct snd_ctl_elem_value *ucontrol)
2663{
2664 struct soc_mixer_control *mc =
2665 (struct soc_mixer_control *)kcontrol->private_value;
2666 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2667 unsigned int mask = (1<<mc->shift)-1;
2668 int min = mc->min;
2669 int ret;
2670 unsigned int val, valr, oval, ovalr;
2671
2672 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2673 val &= mask;
2674 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2675 valr &= mask;
2676
2677 oval = snd_soc_read(codec, mc->reg) & mask;
2678 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2679
2680 ret = 0;
2681 if (oval != val) {
2682 ret = snd_soc_write(codec, mc->reg, val);
2683 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002684 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002685 }
2686 if (ovalr != valr) {
2687 ret = snd_soc_write(codec, mc->rreg, valr);
2688 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002689 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002690 }
2691
2692 return 0;
2693}
2694EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2695
2696/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002697 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2698 * @dai: DAI
2699 * @clk_id: DAI specific clock ID
2700 * @freq: new clock frequency in Hz
2701 * @dir: new clock direction - input/output.
2702 *
2703 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2704 */
2705int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2706 unsigned int freq, int dir)
2707{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002708 if (dai->driver && dai->driver->ops->set_sysclk)
2709 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002710 else
2711 return -EINVAL;
2712}
2713EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2714
2715/**
2716 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2717 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002718 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002719 * @div: new clock divisor.
2720 *
2721 * Configures the clock dividers. This is used to derive the best DAI bit and
2722 * frame clocks from the system or master clock. It's best to set the DAI bit
2723 * and frame clocks as low as possible to save system power.
2724 */
2725int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2726 int div_id, int div)
2727{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002728 if (dai->driver && dai->driver->ops->set_clkdiv)
2729 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002730 else
2731 return -EINVAL;
2732}
2733EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2734
2735/**
2736 * snd_soc_dai_set_pll - configure DAI PLL.
2737 * @dai: DAI
2738 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002739 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002740 * @freq_in: PLL input clock frequency in Hz
2741 * @freq_out: requested PLL output clock frequency in Hz
2742 *
2743 * Configures and enables PLL to generate output clock based on input clock.
2744 */
Mark Brown85488032009-09-05 18:52:16 +01002745int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2746 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002747{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002748 if (dai->driver && dai->driver->ops->set_pll)
2749 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002750 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002751 else
2752 return -EINVAL;
2753}
2754EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2755
2756/**
2757 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2758 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002759 * @fmt: SND_SOC_DAIFMT_ format value.
2760 *
2761 * Configures the DAI hardware format and clocking.
2762 */
2763int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2764{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002765 if (dai->driver && dai->driver->ops->set_fmt)
2766 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002767 else
2768 return -EINVAL;
2769}
2770EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2771
2772/**
2773 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2774 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002775 * @tx_mask: bitmask representing active TX slots.
2776 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002777 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002778 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002779 *
2780 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2781 * specific.
2782 */
2783int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002784 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002785{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002786 if (dai->driver && dai->driver->ops->set_tdm_slot)
2787 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002788 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002789 else
2790 return -EINVAL;
2791}
2792EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2793
2794/**
Barry Song472df3c2009-09-12 01:16:29 +08002795 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2796 * @dai: DAI
2797 * @tx_num: how many TX channels
2798 * @tx_slot: pointer to an array which imply the TX slot number channel
2799 * 0~num-1 uses
2800 * @rx_num: how many RX channels
2801 * @rx_slot: pointer to an array which imply the RX slot number channel
2802 * 0~num-1 uses
2803 *
2804 * configure the relationship between channel number and TDM slot number.
2805 */
2806int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2807 unsigned int tx_num, unsigned int *tx_slot,
2808 unsigned int rx_num, unsigned int *rx_slot)
2809{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002810 if (dai->driver && dai->driver->ops->set_channel_map)
2811 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002812 rx_num, rx_slot);
2813 else
2814 return -EINVAL;
2815}
2816EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2817
2818/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002819 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2820 * @dai: DAI
2821 * @tristate: tristate enable
2822 *
2823 * Tristates the DAI so that others can use it.
2824 */
2825int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2826{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002827 if (dai->driver && dai->driver->ops->set_tristate)
2828 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002829 else
2830 return -EINVAL;
2831}
2832EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2833
2834/**
2835 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2836 * @dai: DAI
2837 * @mute: mute enable
2838 *
2839 * Mutes the DAI DAC.
2840 */
2841int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2842{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002843 if (dai->driver && dai->driver->ops->digital_mute)
2844 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002845 else
2846 return -EINVAL;
2847}
2848EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2849
Mark Brownc5af3a22008-11-28 13:29:45 +00002850/**
2851 * snd_soc_register_card - Register a card with the ASoC core
2852 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002853 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002854 *
2855 * Note that currently this is an internal only function: it will be
2856 * exposed to machine drivers after further backporting of ASoC v2
2857 * registration APIs.
2858 */
2859static int snd_soc_register_card(struct snd_soc_card *card)
2860{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002861 int i;
2862
Mark Brownc5af3a22008-11-28 13:29:45 +00002863 if (!card->name || !card->dev)
2864 return -EINVAL;
2865
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002866 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2867 GFP_KERNEL);
2868 if (card->rtd == NULL)
2869 return -ENOMEM;
2870
2871 for (i = 0; i < card->num_links; i++)
2872 card->rtd[i].dai_link = &card->dai_link[i];
2873
Mark Brownc5af3a22008-11-28 13:29:45 +00002874 INIT_LIST_HEAD(&card->list);
2875 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002876 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002877
2878 mutex_lock(&client_mutex);
2879 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002880 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002881 mutex_unlock(&client_mutex);
2882
2883 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2884
2885 return 0;
2886}
2887
2888/**
2889 * snd_soc_unregister_card - Unregister a card with the ASoC core
2890 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002891 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002892 *
2893 * Note that currently this is an internal only function: it will be
2894 * exposed to machine drivers after further backporting of ASoC v2
2895 * registration APIs.
2896 */
2897static int snd_soc_unregister_card(struct snd_soc_card *card)
2898{
2899 mutex_lock(&client_mutex);
2900 list_del(&card->list);
2901 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002902 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2903
2904 return 0;
2905}
2906
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002907/*
2908 * Simplify DAI link configuration by removing ".-1" from device names
2909 * and sanitizing names.
2910 */
2911static inline char *fmt_single_name(struct device *dev, int *id)
2912{
2913 char *found, name[NAME_SIZE];
2914 int id1, id2;
2915
2916 if (dev_name(dev) == NULL)
2917 return NULL;
2918
2919 strncpy(name, dev_name(dev), NAME_SIZE);
2920
2921 /* are we a "%s.%d" name (platform and SPI components) */
2922 found = strstr(name, dev->driver->name);
2923 if (found) {
2924 /* get ID */
2925 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2926
2927 /* discard ID from name if ID == -1 */
2928 if (*id == -1)
2929 found[strlen(dev->driver->name)] = '\0';
2930 }
2931
2932 } else {
2933 /* I2C component devices are named "bus-addr" */
2934 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2935 char tmp[NAME_SIZE];
2936
2937 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002938 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002939
2940 /* sanitize component name for DAI link creation */
2941 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2942 strncpy(name, tmp, NAME_SIZE);
2943 } else
2944 *id = 0;
2945 }
2946
2947 return kstrdup(name, GFP_KERNEL);
2948}
2949
2950/*
2951 * Simplify DAI link naming for single devices with multiple DAIs by removing
2952 * any ".-1" and using the DAI name (instead of device name).
2953 */
2954static inline char *fmt_multiple_name(struct device *dev,
2955 struct snd_soc_dai_driver *dai_drv)
2956{
2957 if (dai_drv->name == NULL) {
2958 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2959 dev_name(dev));
2960 return NULL;
2961 }
2962
2963 return kstrdup(dai_drv->name, GFP_KERNEL);
2964}
2965
Mark Brown91151712008-11-30 23:31:24 +00002966/**
2967 * snd_soc_register_dai - Register a DAI with the ASoC core
2968 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002969 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002970 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002971int snd_soc_register_dai(struct device *dev,
2972 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00002973{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002974 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00002975
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002976 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00002977
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002978 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2979 if (dai == NULL)
2980 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08002981
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002982 /* create DAI component name */
2983 dai->name = fmt_single_name(dev, &dai->id);
2984 if (dai->name == NULL) {
2985 kfree(dai);
2986 return -ENOMEM;
2987 }
2988
2989 dai->dev = dev;
2990 dai->driver = dai_drv;
2991 if (!dai->driver->ops)
2992 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00002993
2994 mutex_lock(&client_mutex);
2995 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002996 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002997 mutex_unlock(&client_mutex);
2998
2999 pr_debug("Registered DAI '%s'\n", dai->name);
3000
3001 return 0;
3002}
3003EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3004
3005/**
3006 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3007 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003008 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003009 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003010void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003011{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003012 struct snd_soc_dai *dai;
3013
3014 list_for_each_entry(dai, &dai_list, list) {
3015 if (dev == dai->dev)
3016 goto found;
3017 }
3018 return;
3019
3020found:
Mark Brown91151712008-11-30 23:31:24 +00003021 mutex_lock(&client_mutex);
3022 list_del(&dai->list);
3023 mutex_unlock(&client_mutex);
3024
3025 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003026 kfree(dai->name);
3027 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003028}
3029EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3030
3031/**
3032 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3033 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003034 * @dai: Array of DAIs to register
3035 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003036 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003037int snd_soc_register_dais(struct device *dev,
3038 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003039{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003040 struct snd_soc_dai *dai;
3041 int i, ret = 0;
3042
Liam Girdwood720ffa42010-08-18 00:30:30 +01003043 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003044
3045 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003046
3047 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003048 if (dai == NULL) {
3049 ret = -ENOMEM;
3050 goto err;
3051 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003052
3053 /* create DAI component name */
3054 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3055 if (dai->name == NULL) {
3056 kfree(dai);
3057 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003058 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003059 }
3060
3061 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003062 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003063 if (dai->driver->id)
3064 dai->id = dai->driver->id;
3065 else
3066 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003067 if (!dai->driver->ops)
3068 dai->driver->ops = &null_dai_ops;
3069
3070 mutex_lock(&client_mutex);
3071 list_add(&dai->list, &dai_list);
3072 mutex_unlock(&client_mutex);
3073
3074 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003075 }
3076
Axel Lin1dcb4f32010-12-06 16:48:03 +08003077 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003078 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08003079 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00003080 return 0;
3081
3082err:
3083 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003084 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003085
3086 return ret;
3087}
3088EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3089
3090/**
3091 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3092 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003093 * @dai: Array of DAIs to unregister
3094 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003095 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003096void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003097{
3098 int i;
3099
3100 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003101 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003102}
3103EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3104
Mark Brown12a48a8c2008-12-03 19:40:30 +00003105/**
3106 * snd_soc_register_platform - Register a platform with the ASoC core
3107 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003108 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003109 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003110int snd_soc_register_platform(struct device *dev,
3111 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003112{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003113 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003114
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003115 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3116
3117 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3118 if (platform == NULL)
3119 return -ENOMEM;
3120
3121 /* create platform component name */
3122 platform->name = fmt_single_name(dev, &platform->id);
3123 if (platform->name == NULL) {
3124 kfree(platform);
3125 return -ENOMEM;
3126 }
3127
3128 platform->dev = dev;
3129 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003130
3131 mutex_lock(&client_mutex);
3132 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003133 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003134 mutex_unlock(&client_mutex);
3135
3136 pr_debug("Registered platform '%s'\n", platform->name);
3137
3138 return 0;
3139}
3140EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3141
3142/**
3143 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3144 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003145 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003146 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003147void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003148{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003149 struct snd_soc_platform *platform;
3150
3151 list_for_each_entry(platform, &platform_list, list) {
3152 if (dev == platform->dev)
3153 goto found;
3154 }
3155 return;
3156
3157found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003158 mutex_lock(&client_mutex);
3159 list_del(&platform->list);
3160 mutex_unlock(&client_mutex);
3161
3162 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003163 kfree(platform->name);
3164 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003165}
3166EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3167
Mark Brown151ab222009-05-09 16:22:58 +01003168static u64 codec_format_map[] = {
3169 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3170 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3171 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3172 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3173 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3174 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3175 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3176 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3177 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3178 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3179 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3180 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3181 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3182 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3183 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3184 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3185};
3186
3187/* Fix up the DAI formats for endianness: codecs don't actually see
3188 * the endianness of the data but we're using the CPU format
3189 * definitions which do need to include endianness so we ensure that
3190 * codec DAIs always have both big and little endian variants set.
3191 */
3192static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3193{
3194 int i;
3195
3196 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3197 if (stream->formats & codec_format_map[i])
3198 stream->formats |= codec_format_map[i];
3199}
3200
Mark Brown0d0cf002008-12-10 14:32:45 +00003201/**
3202 * snd_soc_register_codec - Register a codec with the ASoC core
3203 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003204 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003205 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003206int snd_soc_register_codec(struct device *dev,
3207 struct snd_soc_codec_driver *codec_drv,
3208 struct snd_soc_dai_driver *dai_drv, int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003209{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003210 struct snd_soc_codec *codec;
3211 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003212
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003213 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003214
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003215 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3216 if (codec == NULL)
3217 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003218
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003219 /* create CODEC component name */
3220 codec->name = fmt_single_name(dev, &codec->id);
3221 if (codec->name == NULL) {
3222 kfree(codec);
3223 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003224 }
3225
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003226 /* allocate CODEC register cache */
3227 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3228
3229 if (codec_drv->reg_cache_default)
3230 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3231 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3232 else
3233 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3234 codec_drv->reg_word_size, GFP_KERNEL);
3235
3236 if (codec->reg_cache == NULL) {
3237 kfree(codec->name);
3238 kfree(codec);
3239 return -ENOMEM;
3240 }
3241 }
3242
3243 codec->dev = dev;
3244 codec->driver = codec_drv;
3245 codec->bias_level = SND_SOC_BIAS_OFF;
3246 codec->num_dai = num_dai;
3247 mutex_init(&codec->mutex);
3248 INIT_LIST_HEAD(&codec->dapm_widgets);
3249 INIT_LIST_HEAD(&codec->dapm_paths);
3250
3251 for (i = 0; i < num_dai; i++) {
3252 fixup_codec_formats(&dai_drv[i].playback);
3253 fixup_codec_formats(&dai_drv[i].capture);
3254 }
3255
Mark Brown26b01cc2010-08-18 20:20:55 +01003256 /* register any DAIs */
3257 if (num_dai) {
3258 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3259 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003260 goto error;
Mark Brown26b01cc2010-08-18 20:20:55 +01003261 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003262
Mark Brown0d0cf002008-12-10 14:32:45 +00003263 mutex_lock(&client_mutex);
3264 list_add(&codec->list, &codec_list);
3265 snd_soc_instantiate_cards();
3266 mutex_unlock(&client_mutex);
3267
3268 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003269 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003270
3271error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003272 if (codec->reg_cache)
3273 kfree(codec->reg_cache);
3274 kfree(codec->name);
3275 kfree(codec);
3276 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003277}
3278EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3279
3280/**
3281 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3282 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003283 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003284 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003285void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003286{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003287 struct snd_soc_codec *codec;
3288 int i;
3289
3290 list_for_each_entry(codec, &codec_list, list) {
3291 if (dev == codec->dev)
3292 goto found;
3293 }
3294 return;
3295
3296found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003297 if (codec->num_dai)
3298 for (i = 0; i < codec->num_dai; i++)
3299 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003300
Mark Brown0d0cf002008-12-10 14:32:45 +00003301 mutex_lock(&client_mutex);
3302 list_del(&codec->list);
3303 mutex_unlock(&client_mutex);
3304
3305 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003306
3307 if (codec->reg_cache)
3308 kfree(codec->reg_cache);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003309 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003310 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003311}
3312EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3313
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003314static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003315{
Mark Brown384c89e2008-12-03 17:34:03 +00003316#ifdef CONFIG_DEBUG_FS
3317 debugfs_root = debugfs_create_dir("asoc", NULL);
3318 if (IS_ERR(debugfs_root) || !debugfs_root) {
3319 printk(KERN_WARNING
3320 "ASoC: Failed to create debugfs directory\n");
3321 debugfs_root = NULL;
3322 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003323
3324 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3325 &codec_list_fops))
3326 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3327
Mark Brownf3208782010-09-15 18:19:07 +01003328 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3329 &dai_list_fops))
3330 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003331
3332 if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
3333 &platform_list_fops))
3334 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003335#endif
3336
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003337 return platform_driver_register(&soc_driver);
3338}
Mark Brown4abe8e12010-10-12 17:41:03 +01003339module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003340
Mark Brown7d8c16a2008-11-30 22:11:24 +00003341static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003342{
Mark Brown384c89e2008-12-03 17:34:03 +00003343#ifdef CONFIG_DEBUG_FS
3344 debugfs_remove_recursive(debugfs_root);
3345#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003346 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003347}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003348module_exit(snd_soc_exit);
3349
3350/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003351MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003352MODULE_DESCRIPTION("ALSA SoC Core");
3353MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003354MODULE_ALIAS("platform:soc-audio");