blob: 862b1af5fbdf8dde9f70510265872a4298b96eca [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 Browndbe21402010-02-12 11:37:24 +0000168
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000169 strict_strtol(buf, 10, &rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000170
171 return count;
172}
173
174static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
175
Mark Brown2624d5f2009-11-03 21:56:13 +0000176#ifdef CONFIG_DEBUG_FS
177static int codec_reg_open_file(struct inode *inode, struct file *file)
178{
179 file->private_data = inode->i_private;
180 return 0;
181}
182
183static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
184 size_t count, loff_t *ppos)
185{
186 ssize_t ret;
187 struct snd_soc_codec *codec = file->private_data;
188 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
189 if (!buf)
190 return -ENOMEM;
191 ret = soc_codec_reg_show(codec, buf);
192 if (ret >= 0)
193 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
194 kfree(buf);
195 return ret;
196}
197
198static ssize_t codec_reg_write_file(struct file *file,
199 const char __user *user_buf, size_t count, loff_t *ppos)
200{
201 char buf[32];
202 int buf_size;
203 char *start = buf;
204 unsigned long reg, value;
205 int step = 1;
206 struct snd_soc_codec *codec = file->private_data;
207
208 buf_size = min(count, (sizeof(buf)-1));
209 if (copy_from_user(buf, user_buf, buf_size))
210 return -EFAULT;
211 buf[buf_size] = 0;
212
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000213 if (codec->driver->reg_cache_step)
214 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000215
216 while (*start == ' ')
217 start++;
218 reg = simple_strtoul(start, &start, 16);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000219 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
Mark Brown2624d5f2009-11-03 21:56:13 +0000220 return -EINVAL;
221 while (*start == ' ')
222 start++;
223 if (strict_strtoul(start, 16, &value))
224 return -EINVAL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000225 codec->driver->write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000226 return buf_size;
227}
228
229static const struct file_operations codec_reg_fops = {
230 .open = codec_reg_open_file,
231 .read = codec_reg_read_file,
232 .write = codec_reg_write_file,
233};
234
235static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
236{
Mark Brown6ba6c9c2010-08-12 15:49:52 +0100237 codec->debugfs_codec_root = debugfs_create_dir(codec->name ,
Mark Brown2624d5f2009-11-03 21:56:13 +0000238 debugfs_root);
239 if (!codec->debugfs_codec_root) {
240 printk(KERN_WARNING
241 "ASoC: Failed to create codec debugfs directory\n");
242 return;
243 }
244
245 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
246 codec->debugfs_codec_root,
247 codec, &codec_reg_fops);
248 if (!codec->debugfs_reg)
249 printk(KERN_WARNING
250 "ASoC: Failed to create codec register debugfs file\n");
251
Axel Lin708fafb2010-08-27 10:34:27 +0800252 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
Mark Brown2624d5f2009-11-03 21:56:13 +0000253 codec->debugfs_codec_root,
254 &codec->pop_time);
255 if (!codec->debugfs_pop_time)
256 printk(KERN_WARNING
257 "Failed to create pop time debugfs file\n");
258
259 codec->debugfs_dapm = debugfs_create_dir("dapm",
260 codec->debugfs_codec_root);
261 if (!codec->debugfs_dapm)
262 printk(KERN_WARNING
263 "Failed to create DAPM debugfs directory\n");
264
265 snd_soc_dapm_debugfs_init(codec);
266}
267
268static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
269{
270 debugfs_remove_recursive(codec->debugfs_codec_root);
271}
272
Mark Brownc3c5a192010-09-15 18:15:14 +0100273static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
274 size_t count, loff_t *ppos)
275{
276 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100277 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100278 struct snd_soc_codec *codec;
279
280 if (!buf)
281 return -ENOMEM;
282
Mark Brown2b194f9d2010-10-13 10:52:16 +0100283 list_for_each_entry(codec, &codec_list, list) {
284 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
285 codec->name);
286 if (len >= 0)
287 ret += len;
288 if (ret > PAGE_SIZE) {
289 ret = PAGE_SIZE;
290 break;
291 }
292 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100293
294 if (ret >= 0)
295 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
296
297 kfree(buf);
298
299 return ret;
300}
301
302static const struct file_operations codec_list_fops = {
303 .read = codec_list_read_file,
304 .llseek = default_llseek,/* read accesses f_pos */
305};
306
Mark Brownf3208782010-09-15 18:19:07 +0100307static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
308 size_t count, loff_t *ppos)
309{
310 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100311 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100312 struct snd_soc_dai *dai;
313
314 if (!buf)
315 return -ENOMEM;
316
Mark Brown2b194f9d2010-10-13 10:52:16 +0100317 list_for_each_entry(dai, &dai_list, list) {
318 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
319 if (len >= 0)
320 ret += len;
321 if (ret > PAGE_SIZE) {
322 ret = PAGE_SIZE;
323 break;
324 }
325 }
Mark Brownf3208782010-09-15 18:19:07 +0100326
Mark Brown2b194f9d2010-10-13 10:52:16 +0100327 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100328
329 kfree(buf);
330
331 return ret;
332}
333
334static const struct file_operations dai_list_fops = {
335 .read = dai_list_read_file,
336 .llseek = default_llseek,/* read accesses f_pos */
337};
338
Mark Brown19c7ac22010-09-15 18:22:40 +0100339static ssize_t platform_list_read_file(struct file *file,
340 char __user *user_buf,
341 size_t count, loff_t *ppos)
342{
343 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100344 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100345 struct snd_soc_platform *platform;
346
347 if (!buf)
348 return -ENOMEM;
349
Mark Brown2b194f9d2010-10-13 10:52:16 +0100350 list_for_each_entry(platform, &platform_list, list) {
351 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
352 platform->name);
353 if (len >= 0)
354 ret += len;
355 if (ret > PAGE_SIZE) {
356 ret = PAGE_SIZE;
357 break;
358 }
359 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100360
Mark Brown2b194f9d2010-10-13 10:52:16 +0100361 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100362
363 kfree(buf);
364
365 return ret;
366}
367
368static const struct file_operations platform_list_fops = {
369 .read = platform_list_read_file,
370 .llseek = default_llseek,/* read accesses f_pos */
371};
372
Mark Brown2624d5f2009-11-03 21:56:13 +0000373#else
374
375static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
376{
377}
378
379static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
380{
381}
382#endif
383
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200384#ifdef CONFIG_SND_SOC_AC97_BUS
385/* unregister ac97 codec */
386static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
387{
388 if (codec->ac97->dev.bus)
389 device_unregister(&codec->ac97->dev);
390 return 0;
391}
392
393/* stop no dev release warning */
394static void soc_ac97_device_release(struct device *dev){}
395
396/* register ac97 codec to bus */
397static int soc_ac97_dev_register(struct snd_soc_codec *codec)
398{
399 int err;
400
401 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100402 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200403 codec->ac97->dev.release = soc_ac97_device_release;
404
Kay Sieversbb072bf2008-11-02 03:50:35 +0100405 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000406 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200407 err = device_register(&codec->ac97->dev);
408 if (err < 0) {
409 snd_printk(KERN_ERR "Can't register ac97 bus\n");
410 codec->ac97->dev.bus = NULL;
411 return err;
412 }
413 return 0;
414}
415#endif
416
Mark Brown06f409d2009-04-07 18:10:13 +0100417static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
418{
419 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000420 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
421 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100422 int ret;
423
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000424 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
425 rtd->dai_link->symmetric_rates) {
426 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
427 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100428
429 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
430 SNDRV_PCM_HW_PARAM_RATE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000431 rtd->rate,
432 rtd->rate);
Mark Brown06f409d2009-04-07 18:10:13 +0100433 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000434 dev_err(&rtd->dev,
Mark Brown06f409d2009-04-07 18:10:13 +0100435 "Unable to apply rate symmetry constraint: %d\n", ret);
436 return ret;
437 }
438 }
439
440 return 0;
441}
442
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200443/*
444 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
445 * then initialized and any private data can be allocated. This also calls
446 * startup for the cpu DAI, platform, machine and codec DAI.
447 */
448static int soc_pcm_open(struct snd_pcm_substream *substream)
449{
450 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200451 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000452 struct snd_soc_platform *platform = rtd->platform;
453 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
454 struct snd_soc_dai *codec_dai = rtd->codec_dai;
455 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
456 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200457 int ret = 0;
458
459 mutex_lock(&pcm_mutex);
460
461 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000462 if (cpu_dai->driver->ops->startup) {
463 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200464 if (ret < 0) {
465 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100466 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200467 goto out;
468 }
469 }
470
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000471 if (platform->driver->ops->open) {
472 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200473 if (ret < 0) {
474 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
475 goto platform_err;
476 }
477 }
478
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000479 if (codec_dai->driver->ops->startup) {
480 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100481 if (ret < 0) {
482 printk(KERN_ERR "asoc: can't open codec %s\n",
483 codec_dai->name);
484 goto codec_dai_err;
485 }
486 }
487
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000488 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
489 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200490 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000491 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200492 goto machine_err;
493 }
494 }
495
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200496 /* Check that the codec and cpu DAI's are compatible */
497 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
498 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000499 max(codec_dai_drv->playback.rate_min,
500 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200501 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000502 min(codec_dai_drv->playback.rate_max,
503 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200504 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000505 max(codec_dai_drv->playback.channels_min,
506 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200507 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000508 min(codec_dai_drv->playback.channels_max,
509 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100510 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000511 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100512 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000513 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
514 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900515 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000516 runtime->hw.rates |= cpu_dai_drv->playback.rates;
517 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900518 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200520 } else {
521 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 max(codec_dai_drv->capture.rate_min,
523 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200524 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000525 min(codec_dai_drv->capture.rate_max,
526 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200527 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000528 max(codec_dai_drv->capture.channels_min,
529 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200530 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531 min(codec_dai_drv->capture.channels_max,
532 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100533 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000534 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100535 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000536 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
537 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900538 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000539 runtime->hw.rates |= cpu_dai_drv->capture.rates;
540 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900541 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000542 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200543 }
544
545 snd_pcm_limit_hw_rates(runtime);
546 if (!runtime->hw.rates) {
547 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100548 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900549 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200550 }
551 if (!runtime->hw.formats) {
552 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100553 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900554 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200555 }
556 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
557 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900559 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200560 }
561
Mark Brown06f409d2009-04-07 18:10:13 +0100562 /* Symmetry only applies if we've already got an active stream. */
563 if (cpu_dai->active || codec_dai->active) {
564 ret = soc_pcm_apply_symmetry(substream);
565 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900566 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100567 }
568
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 pr_debug("asoc: %s <-> %s info:\n",
570 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100571 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
572 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
573 runtime->hw.channels_max);
574 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
575 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200576
Jassi Brar14dc5732010-02-26 09:12:32 +0900577 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000578 cpu_dai->playback_active++;
579 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900580 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 cpu_dai->capture_active++;
582 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900583 }
584 cpu_dai->active++;
585 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000586 rtd->codec->active++;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200587 mutex_unlock(&pcm_mutex);
588 return 0;
589
Jassi Brarbb1c0472010-02-25 11:24:53 +0900590config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
592 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200593
Jassi Brarbb1c0472010-02-25 11:24:53 +0900594machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000595 if (codec_dai->driver->ops->shutdown)
596 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900597
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100598codec_dai_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 if (platform->driver->ops->close)
600 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200601
602platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000603 if (cpu_dai->driver->ops->shutdown)
604 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200605out:
606 mutex_unlock(&pcm_mutex);
607 return ret;
608}
609
610/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200611 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200612 * This is to ensure there are no pops or clicks in between any music tracks
613 * due to DAPM power cycling.
614 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100615static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200616{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 struct snd_soc_pcm_runtime *rtd =
618 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
619 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200620
621 mutex_lock(&pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200622
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
624 codec_dai->driver->playback.stream_name,
625 codec_dai->playback_active ? "active" : "inactive",
626 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200627
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000628 /* are we waiting on this codec DAI stream */
629 if (codec_dai->pop_wait == 1) {
630 codec_dai->pop_wait = 0;
631 snd_soc_dapm_stream_event(rtd,
632 codec_dai->driver->playback.stream_name,
633 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000635
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200636 mutex_unlock(&pcm_mutex);
637}
638
639/*
640 * Called by ALSA when a PCM substream is closed. Private data can be
641 * freed here. The cpu DAI, codec DAI, machine and platform are also
642 * shutdown.
643 */
644static int soc_codec_close(struct snd_pcm_substream *substream)
645{
646 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000647 struct snd_soc_platform *platform = rtd->platform;
648 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
649 struct snd_soc_dai *codec_dai = rtd->codec_dai;
650 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200651
652 mutex_lock(&pcm_mutex);
653
Jassi Brar14dc5732010-02-26 09:12:32 +0900654 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000655 cpu_dai->playback_active--;
656 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900657 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 cpu_dai->capture_active--;
659 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200660 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900661
662 cpu_dai->active--;
663 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200664 codec->active--;
665
Mark Brown6010b2d2008-09-06 18:33:24 +0100666 /* Muting the DAC suppresses artifacts caused during digital
667 * shutdown, for example from stopping clocks.
668 */
669 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
670 snd_soc_dai_digital_mute(codec_dai, 1);
671
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000672 if (cpu_dai->driver->ops->shutdown)
673 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200674
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000675 if (codec_dai->driver->ops->shutdown)
676 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200677
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000678 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
679 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200680
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000681 if (platform->driver->ops->close)
682 platform->driver->ops->close(substream);
683 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200684
685 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
686 /* start delayed pop wq here for playback streams */
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100687 codec_dai->pop_wait = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000688 schedule_delayed_work(&rtd->delayed_work,
689 msecs_to_jiffies(rtd->pmdown_time));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690 } else {
691 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000692 snd_soc_dapm_stream_event(rtd,
693 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100694 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200695 }
696
697 mutex_unlock(&pcm_mutex);
698 return 0;
699}
700
701/*
702 * Called by ALSA when the PCM substream is prepared, can set format, sample
703 * rate, etc. This function is non atomic and can be called multiple times,
704 * it can refer to the runtime info.
705 */
706static int soc_pcm_prepare(struct snd_pcm_substream *substream)
707{
708 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000709 struct snd_soc_platform *platform = rtd->platform;
710 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
711 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200712 int ret = 0;
713
714 mutex_lock(&pcm_mutex);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100715
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000716 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
717 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100718 if (ret < 0) {
719 printk(KERN_ERR "asoc: machine prepare error\n");
720 goto out;
721 }
722 }
723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 if (platform->driver->ops->prepare) {
725 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200726 if (ret < 0) {
727 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200728 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200729 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200730 }
731
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000732 if (codec_dai->driver->ops->prepare) {
733 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200734 if (ret < 0) {
735 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200736 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200737 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200738 }
739
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000740 if (cpu_dai->driver->ops->prepare) {
741 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100742 if (ret < 0) {
743 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
744 goto out;
745 }
746 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200747
Mark Brownd45f6212008-10-14 13:58:36 +0100748 /* cancel any delayed stream shutdown that is pending */
749 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
750 codec_dai->pop_wait) {
751 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000752 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100753 }
754
Mark Brown452c5ea2009-05-17 21:41:23 +0100755 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756 snd_soc_dapm_stream_event(rtd,
757 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100758 SND_SOC_DAPM_STREAM_START);
759 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000760 snd_soc_dapm_stream_event(rtd,
761 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100762 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100763
Mark Brown452c5ea2009-05-17 21:41:23 +0100764 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200765
766out:
767 mutex_unlock(&pcm_mutex);
768 return ret;
769}
770
771/*
772 * Called by ALSA when the hardware params are set by application. This
773 * function can also be called multiple times and can allocate buffers
774 * (using snd_pcm_lib_* ). It's non-atomic.
775 */
776static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
777 struct snd_pcm_hw_params *params)
778{
779 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000780 struct snd_soc_platform *platform = rtd->platform;
781 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
782 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200783 int ret = 0;
784
785 mutex_lock(&pcm_mutex);
786
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000787 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
788 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200789 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100790 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791 goto out;
792 }
793 }
794
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000795 if (codec_dai->driver->ops->hw_params) {
796 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100797 if (ret < 0) {
798 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
799 codec_dai->name);
800 goto codec_err;
801 }
802 }
803
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000804 if (cpu_dai->driver->ops->hw_params) {
805 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200806 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200807 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100808 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200809 goto interface_err;
810 }
811 }
812
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000813 if (platform->driver->ops->hw_params) {
814 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200815 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200816 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200817 platform->name);
818 goto platform_err;
819 }
820 }
821
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000822 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100823
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200824out:
825 mutex_unlock(&pcm_mutex);
826 return ret;
827
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200828platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000829 if (cpu_dai->driver->ops->hw_free)
830 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200831
832interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000833 if (codec_dai->driver->ops->hw_free)
834 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100835
836codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000837 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
838 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200839
840 mutex_unlock(&pcm_mutex);
841 return ret;
842}
843
844/*
845 * Free's resources allocated by hw_params, can be called multiple times
846 */
847static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
848{
849 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000850 struct snd_soc_platform *platform = rtd->platform;
851 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
852 struct snd_soc_dai *codec_dai = rtd->codec_dai;
853 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200854
855 mutex_lock(&pcm_mutex);
856
857 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100858 if (!codec->active)
859 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200860
861 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000862 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
863 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200864
865 /* free any DMA resources */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000866 if (platform->driver->ops->hw_free)
867 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200868
869 /* now free hw params for the DAI's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000870 if (codec_dai->driver->ops->hw_free)
871 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200872
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000873 if (cpu_dai->driver->ops->hw_free)
874 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200875
876 mutex_unlock(&pcm_mutex);
877 return 0;
878}
879
880static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
881{
882 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000883 struct snd_soc_platform *platform = rtd->platform;
884 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
885 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200886 int ret;
887
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000888 if (codec_dai->driver->ops->trigger) {
889 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200890 if (ret < 0)
891 return ret;
892 }
893
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000894 if (platform->driver->ops->trigger) {
895 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200896 if (ret < 0)
897 return ret;
898 }
899
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000900 if (cpu_dai->driver->ops->trigger) {
901 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200902 if (ret < 0)
903 return ret;
904 }
905 return 0;
906}
907
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200908/*
909 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200910 * If cpu_dai, codec_dai, platform driver has the delay callback, than
911 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200912 */
913static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
914{
915 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000916 struct snd_soc_platform *platform = rtd->platform;
917 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
918 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200919 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200920 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200921 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200922
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000923 if (platform->driver->ops->pointer)
924 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200925
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000926 if (cpu_dai->driver->ops->delay)
927 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200928
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000929 if (codec_dai->driver->ops->delay)
930 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200931
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000932 if (platform->driver->delay)
933 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200934
935 runtime->delay = delay;
936
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200937 return offset;
938}
939
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200940/* ASoC PCM operations */
941static struct snd_pcm_ops soc_pcm_ops = {
942 .open = soc_pcm_open,
943 .close = soc_codec_close,
944 .hw_params = soc_pcm_hw_params,
945 .hw_free = soc_pcm_hw_free,
946 .prepare = soc_pcm_prepare,
947 .trigger = soc_pcm_trigger,
Peter Ujfalusi377b6f62010-03-03 15:08:06 +0200948 .pointer = soc_pcm_pointer,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200949};
950
951#ifdef CONFIG_PM
952/* powers down audio subsystem for suspend */
Mark Brown416356f2009-06-30 19:05:15 +0100953static int soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200954{
Mark Brown416356f2009-06-30 19:05:15 +0100955 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000956 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200957 int i;
958
Daniel Macke3509ff2009-06-03 17:44:49 +0200959 /* If the initialization of this soc device failed, there is no codec
960 * associated with it. Just bail out in this case.
961 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000962 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200963 return 0;
964
Andy Green6ed25972008-06-13 16:24:05 +0100965 /* Due to the resume being scheduled into a workqueue we could
966 * suspend before that's finished - wait for it to complete.
967 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000968 snd_power_lock(card->snd_card);
969 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
970 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100971
972 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000973 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100974
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200975 /* mute any active DAC's */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000976 for (i = 0; i < card->num_rtd; i++) {
977 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
978 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100979
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000980 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100981 continue;
982
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000983 if (drv->ops->digital_mute && dai->playback_active)
984 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200985 }
986
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100987 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000988 for (i = 0; i < card->num_rtd; i++) {
989 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100990 continue;
991
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000992 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100993 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100994
Mark Brown87506542008-11-18 20:50:34 +0000995 if (card->suspend_pre)
Mark Brown416356f2009-06-30 19:05:15 +0100996 card->suspend_pre(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200997
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000998 for (i = 0; i < card->num_rtd; i++) {
999 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1000 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001001
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001003 continue;
1004
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001005 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1006 cpu_dai->driver->suspend(cpu_dai);
1007 if (platform->driver->suspend && !platform->suspended) {
1008 platform->driver->suspend(cpu_dai);
1009 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +01001010 }
1011 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001012
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001013 /* close any waiting streams and save state */
1014 for (i = 0; i < card->num_rtd; i++) {
1015 run_delayed_work(&card->rtd[i].delayed_work);
1016 card->rtd[i].codec->suspend_bias_level = card->rtd[i].codec->bias_level;
1017 }
Mark Brown3efab7d2010-05-09 13:25:43 +01001018
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001019 for (i = 0; i < card->num_rtd; i++) {
1020 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1021
1022 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001023 continue;
1024
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001025 if (driver->playback.stream_name != NULL)
1026 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1027 SND_SOC_DAPM_STREAM_SUSPEND);
1028
1029 if (driver->capture.stream_name != NULL)
1030 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1031 SND_SOC_DAPM_STREAM_SUSPEND);
1032 }
1033
1034 /* suspend all CODECs */
1035 for (i = 0; i < card->num_rtd; i++) {
1036 struct snd_soc_codec *codec = card->rtd[i].codec;
1037 /* If there are paths active then the CODEC will be held with
1038 * bias _ON and should not be suspended. */
1039 if (!codec->suspended && codec->driver->suspend) {
1040 switch (codec->bias_level) {
1041 case SND_SOC_BIAS_STANDBY:
1042 case SND_SOC_BIAS_OFF:
1043 codec->driver->suspend(codec, PMSG_SUSPEND);
1044 codec->suspended = 1;
1045 break;
1046 default:
1047 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1048 break;
1049 }
1050 }
1051 }
1052
1053 for (i = 0; i < card->num_rtd; i++) {
1054 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1055
1056 if (card->rtd[i].dai_link->ignore_suspend)
1057 continue;
1058
1059 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1060 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001061 }
1062
Mark Brown87506542008-11-18 20:50:34 +00001063 if (card->suspend_post)
Mark Brown416356f2009-06-30 19:05:15 +01001064 card->suspend_post(pdev, PMSG_SUSPEND);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001065
1066 return 0;
1067}
1068
Andy Green6ed25972008-06-13 16:24:05 +01001069/* deferred resume work, so resume can complete before we finished
1070 * setting our codec back up, which can be very slow on I2C
1071 */
1072static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001073{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001074 struct snd_soc_card *card =
1075 container_of(work, struct snd_soc_card, deferred_resume_work);
1076 struct platform_device *pdev = to_platform_device(card->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001077 int i;
1078
Andy Green6ed25972008-06-13 16:24:05 +01001079 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1080 * so userspace apps are blocked from touching us
1081 */
1082
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001083 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001084
Mark Brown99497882010-05-07 20:24:05 +01001085 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001086 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +01001087
Mark Brown87506542008-11-18 20:50:34 +00001088 if (card->resume_pre)
1089 card->resume_pre(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001090
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001091 /* resume AC97 DAIs */
1092 for (i = 0; i < card->num_rtd; i++) {
1093 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001094
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001095 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001096 continue;
1097
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001098 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1099 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001100 }
1101
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001102 for (i = 0; i < card->num_rtd; i++) {
1103 struct snd_soc_codec *codec = card->rtd[i].codec;
1104 /* If the CODEC was idle over suspend then it will have been
1105 * left with bias OFF or STANDBY and suspended so we must now
1106 * resume. Otherwise the suspend was suppressed.
1107 */
1108 if (codec->driver->resume && codec->suspended) {
1109 switch (codec->bias_level) {
1110 case SND_SOC_BIAS_STANDBY:
1111 case SND_SOC_BIAS_OFF:
1112 codec->driver->resume(codec);
1113 codec->suspended = 0;
1114 break;
1115 default:
1116 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1117 break;
1118 }
Mark Brown1547aba2010-05-07 21:11:40 +01001119 }
1120 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001121
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001122 for (i = 0; i < card->num_rtd; i++) {
1123 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001124
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001125 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001126 continue;
1127
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001128 if (driver->playback.stream_name != NULL)
1129 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001130 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001131
1132 if (driver->capture.stream_name != NULL)
1133 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001134 SND_SOC_DAPM_STREAM_RESUME);
1135 }
1136
Mark Brown3ff3f642008-05-19 12:32:25 +02001137 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001138 for (i = 0; i < card->num_rtd; i++) {
1139 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1140 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001141
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001142 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001143 continue;
1144
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001145 if (drv->ops->digital_mute && dai->playback_active)
1146 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001147 }
1148
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001149 for (i = 0; i < card->num_rtd; i++) {
1150 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1151 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001152
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001153 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +01001154 continue;
1155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001156 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1157 cpu_dai->driver->resume(cpu_dai);
1158 if (platform->driver->resume && platform->suspended) {
1159 platform->driver->resume(cpu_dai);
1160 platform->suspended = 0;
1161 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001162 }
1163
Mark Brown87506542008-11-18 20:50:34 +00001164 if (card->resume_post)
1165 card->resume_post(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001166
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001167 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001168
1169 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001170 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001171}
1172
1173/* powers up audio subsystem after a suspend */
Mark Brown416356f2009-06-30 19:05:15 +01001174static int soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001175{
Mark Brown416356f2009-06-30 19:05:15 +01001176 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001177 struct snd_soc_card *card = platform_get_drvdata(pdev);
1178 int i;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001179
Mark Brown64ab9ba2009-03-31 11:27:03 +01001180 /* AC97 devices might have other drivers hanging off them so
1181 * need to resume immediately. Other drivers don't have that
1182 * problem and may take a substantial amount of time to resume
1183 * due to I/O costs and anti-pop so handle them out of line.
1184 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001185 for (i = 0; i < card->num_rtd; i++) {
1186 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1187 if (cpu_dai->driver->ac97_control) {
1188 dev_dbg(dev, "Resuming AC97 immediately\n");
1189 soc_resume_deferred(&card->deferred_resume_work);
1190 } else {
1191 dev_dbg(dev, "Scheduling resume work\n");
1192 if (!schedule_work(&card->deferred_resume_work))
1193 dev_err(dev, "resume work item may be lost\n");
1194 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001195 }
Andy Green6ed25972008-06-13 16:24:05 +01001196
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001197 return 0;
1198}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001199#else
1200#define soc_suspend NULL
1201#define soc_resume NULL
1202#endif
1203
Barry Song02a06d32009-10-16 18:13:38 +08001204static struct snd_soc_dai_ops null_dai_ops = {
1205};
1206
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001207static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001208{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001209 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1210 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001211 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001212 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001213 struct snd_soc_dai *codec_dai, *cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001214
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001215 if (rtd->complete)
1216 return 1;
1217 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001218
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001219 /* do we already have the CPU DAI for this link ? */
1220 if (rtd->cpu_dai) {
1221 goto find_codec;
1222 }
1223 /* no, then find CPU DAI from registered DAIs*/
1224 list_for_each_entry(cpu_dai, &dai_list, list) {
1225 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1226
1227 if (!try_module_get(cpu_dai->dev->driver->owner))
1228 return -ENODEV;
1229
1230 rtd->cpu_dai = cpu_dai;
1231 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001232 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001233 }
1234 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1235 dai_link->cpu_dai_name);
1236
1237find_codec:
1238 /* do we already have the CODEC for this link ? */
1239 if (rtd->codec) {
1240 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001241 }
1242
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001243 /* no, then find CODEC from registered CODECs*/
1244 list_for_each_entry(codec, &codec_list, list) {
1245 if (!strcmp(codec->name, dai_link->codec_name)) {
1246 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001247
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001248 if (!try_module_get(codec->dev->driver->owner))
1249 return -ENODEV;
Mark Brown435c5e22008-12-04 15:32:53 +00001250
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001251 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1252 list_for_each_entry(codec_dai, &dai_list, list) {
1253 if (codec->dev == codec_dai->dev &&
1254 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1255 rtd->codec_dai = codec_dai;
1256 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001257 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001258 }
1259 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1260 dai_link->codec_dai_name);
1261
1262 goto find_platform;
1263 }
1264 }
1265 dev_dbg(card->dev, "CODEC %s not registered\n",
1266 dai_link->codec_name);
1267
1268find_platform:
1269 /* do we already have the CODEC DAI for this link ? */
1270 if (rtd->platform) {
1271 goto out;
1272 }
1273 /* no, then find CPU DAI from registered DAIs*/
1274 list_for_each_entry(platform, &platform_list, list) {
1275 if (!strcmp(platform->name, dai_link->platform_name)) {
1276
1277 if (!try_module_get(platform->dev->driver->owner))
1278 return -ENODEV;
1279
1280 rtd->platform = platform;
1281 goto out;
1282 }
1283 }
1284
1285 dev_dbg(card->dev, "platform %s not registered\n",
1286 dai_link->platform_name);
1287 return 0;
1288
1289out:
1290 /* mark rtd as complete if we found all 4 of our client devices */
1291 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1292 rtd->complete = 1;
1293 card->num_rtd++;
1294 }
1295 return 1;
1296}
1297
1298static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1299{
1300 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1301 struct snd_soc_codec *codec = rtd->codec;
1302 struct snd_soc_platform *platform = rtd->platform;
1303 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1304 int err;
1305
1306 /* unregister the rtd device */
1307 if (rtd->dev_registered) {
1308 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1309 device_unregister(&rtd->dev);
1310 rtd->dev_registered = 0;
1311 }
1312
1313 /* remove the CODEC DAI */
1314 if (codec_dai && codec_dai->probed) {
1315 if (codec_dai->driver->remove) {
1316 err = codec_dai->driver->remove(codec_dai);
1317 if (err < 0)
1318 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1319 }
1320 codec_dai->probed = 0;
1321 list_del(&codec_dai->card_list);
1322 }
1323
1324 /* remove the platform */
1325 if (platform && platform->probed) {
1326 if (platform->driver->remove) {
1327 err = platform->driver->remove(platform);
1328 if (err < 0)
1329 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1330 }
1331 platform->probed = 0;
1332 list_del(&platform->card_list);
1333 module_put(platform->dev->driver->owner);
1334 }
1335
1336 /* remove the CODEC */
1337 if (codec && codec->probed) {
1338 if (codec->driver->remove) {
1339 err = codec->driver->remove(codec);
1340 if (err < 0)
1341 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1342 }
1343
1344 /* Make sure all DAPM widgets are freed */
1345 snd_soc_dapm_free(codec);
1346
1347 soc_cleanup_codec_debugfs(codec);
1348 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1349 codec->probed = 0;
1350 list_del(&codec->card_list);
1351 module_put(codec->dev->driver->owner);
1352 }
1353
1354 /* remove the cpu_dai */
1355 if (cpu_dai && cpu_dai->probed) {
1356 if (cpu_dai->driver->remove) {
1357 err = cpu_dai->driver->remove(cpu_dai);
1358 if (err < 0)
1359 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1360 }
1361 cpu_dai->probed = 0;
1362 list_del(&cpu_dai->card_list);
1363 module_put(cpu_dai->dev->driver->owner);
1364 }
1365}
1366
1367static void rtd_release(struct device *dev) {}
1368
1369static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1370{
1371 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1372 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1373 struct snd_soc_codec *codec = rtd->codec;
1374 struct snd_soc_platform *platform = rtd->platform;
1375 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1376 int ret;
1377
1378 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1379
1380 /* config components */
1381 codec_dai->codec = codec;
1382 codec->card = card;
1383 cpu_dai->platform = platform;
1384 rtd->card = card;
1385 rtd->dev.parent = card->dev;
1386 codec_dai->card = card;
1387 cpu_dai->card = card;
1388
1389 /* set default power off timeout */
1390 rtd->pmdown_time = pmdown_time;
1391
1392 /* probe the cpu_dai */
1393 if (!cpu_dai->probed) {
1394 if (cpu_dai->driver->probe) {
1395 ret = cpu_dai->driver->probe(cpu_dai);
1396 if (ret < 0) {
1397 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1398 cpu_dai->name);
1399 return ret;
1400 }
1401 }
1402 cpu_dai->probed = 1;
1403 /* mark cpu_dai as probed and add to card cpu_dai list */
1404 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1405 }
1406
1407 /* probe the CODEC */
1408 if (!codec->probed) {
1409 if (codec->driver->probe) {
1410 ret = codec->driver->probe(codec);
1411 if (ret < 0) {
1412 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1413 codec->name);
1414 return ret;
1415 }
1416 }
Mark Brown13cb61f2010-08-12 15:44:04 +01001417
1418 soc_init_codec_debugfs(codec);
1419
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001420 /* mark codec as probed and add to card codec list */
1421 codec->probed = 1;
1422 list_add(&codec->card_list, &card->codec_dev_list);
1423 }
1424
1425 /* probe the platform */
1426 if (!platform->probed) {
1427 if (platform->driver->probe) {
1428 ret = platform->driver->probe(platform);
1429 if (ret < 0) {
1430 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1431 platform->name);
1432 return ret;
1433 }
1434 }
1435 /* mark platform as probed and add to card platform list */
1436 platform->probed = 1;
1437 list_add(&platform->card_list, &card->platform_dev_list);
1438 }
1439
1440 /* probe the CODEC DAI */
1441 if (!codec_dai->probed) {
1442 if (codec_dai->driver->probe) {
1443 ret = codec_dai->driver->probe(codec_dai);
1444 if (ret < 0) {
1445 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1446 codec_dai->name);
1447 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001448 }
1449 }
1450
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001451 /* mark cpu_dai as probed and add to card cpu_dai list */
1452 codec_dai->probed = 1;
1453 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001454 }
1455
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001456 /* DAPM dai link stream work */
1457 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1458
1459 /* now that all clients have probed, initialise the DAI link */
1460 if (dai_link->init) {
1461 ret = dai_link->init(rtd);
1462 if (ret < 0) {
1463 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1464 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001465 }
1466 }
1467
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001468 /* Make sure all DAPM widgets are instantiated */
1469 snd_soc_dapm_new_widgets(codec);
1470 snd_soc_dapm_sync(codec);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001471
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001472 /* register the rtd device */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001473 rtd->dev.release = rtd_release;
1474 rtd->dev.init_name = dai_link->name;
1475 ret = device_register(&rtd->dev);
1476 if (ret < 0) {
1477 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1478 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001479 }
1480
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001481 rtd->dev_registered = 1;
1482 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1483 if (ret < 0)
1484 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1485
1486 /* add DAPM sysfs entries for this codec */
1487 ret = snd_soc_dapm_sys_add(&rtd->dev);
1488 if (ret < 0)
1489 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1490
1491 /* add codec sysfs entries */
1492 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1493 if (ret < 0)
1494 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1495
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496 /* create the pcm */
1497 ret = soc_new_pcm(rtd, num);
1498 if (ret < 0) {
1499 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1500 return ret;
1501 }
1502
1503 /* add platform data for AC97 devices */
1504 if (rtd->codec_dai->driver->ac97_control)
1505 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1506
1507 return 0;
1508}
1509
1510#ifdef CONFIG_SND_SOC_AC97_BUS
1511static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1512{
1513 int ret;
1514
1515 /* Only instantiate AC97 if not already done by the adaptor
1516 * for the generic AC97 subsystem.
1517 */
1518 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001519 /*
1520 * It is possible that the AC97 device is already registered to
1521 * the device subsystem. This happens when the device is created
1522 * via snd_ac97_mixer(). Currently only SoC codec that does so
1523 * is the generic AC97 glue but others migh emerge.
1524 *
1525 * In those cases we don't try to register the device again.
1526 */
1527 if (!rtd->codec->ac97_created)
1528 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001529
1530 ret = soc_ac97_dev_register(rtd->codec);
1531 if (ret < 0) {
1532 printk(KERN_ERR "asoc: AC97 device register failed\n");
1533 return ret;
1534 }
1535
1536 rtd->codec->ac97_registered = 1;
1537 }
1538 return 0;
1539}
1540
1541static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1542{
1543 if (codec->ac97_registered) {
1544 soc_ac97_dev_unregister(codec);
1545 codec->ac97_registered = 0;
1546 }
1547}
1548#endif
1549
1550static void snd_soc_instantiate_card(struct snd_soc_card *card)
1551{
1552 struct platform_device *pdev = to_platform_device(card->dev);
1553 int ret, i;
1554
1555 mutex_lock(&card->mutex);
1556
1557 if (card->instantiated) {
1558 mutex_unlock(&card->mutex);
1559 return;
1560 }
1561
1562 /* bind DAIs */
1563 for (i = 0; i < card->num_links; i++)
1564 soc_bind_dai_link(card, i);
1565
1566 /* bind completed ? */
1567 if (card->num_rtd != card->num_links) {
1568 mutex_unlock(&card->mutex);
1569 return;
1570 }
1571
1572 /* card bind complete so register a sound card */
1573 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1574 card->owner, 0, &card->snd_card);
1575 if (ret < 0) {
1576 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1577 card->name);
1578 mutex_unlock(&card->mutex);
1579 return;
1580 }
1581 card->snd_card->dev = card->dev;
1582
Randy Dunlap1301a962008-06-17 19:19:34 +01001583#ifdef CONFIG_PM
Andy Green6ed25972008-06-13 16:24:05 +01001584 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001585 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001586#endif
Andy Green6ed25972008-06-13 16:24:05 +01001587
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001588 /* initialise the sound card only once */
1589 if (card->probe) {
1590 ret = card->probe(pdev);
1591 if (ret < 0)
1592 goto card_probe_error;
1593 }
1594
Mark Brownfe3e78e2009-11-03 22:13:13 +00001595 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001596 ret = soc_probe_dai_link(card, i);
1597 if (ret < 0) {
Mark Brown321de0d2010-09-21 15:09:37 +01001598 pr_err("asoc: failed to instantiate card %s: %d\n",
1599 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001600 goto probe_dai_err;
1601 }
1602 }
1603
1604 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1605 "%s", card->name);
1606 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1607 "%s", card->name);
1608
1609 ret = snd_card_register(card->snd_card);
1610 if (ret < 0) {
1611 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1612 goto probe_dai_err;
1613 }
1614
1615#ifdef CONFIG_SND_SOC_AC97_BUS
1616 /* register any AC97 codecs */
1617 for (i = 0; i < card->num_rtd; i++) {
1618 ret = soc_register_ac97_dai_link(&card->rtd[i]);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001619 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001620 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1621 goto probe_dai_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001622 }
1623 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001624#endif
1625
Mark Brown435c5e22008-12-04 15:32:53 +00001626 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001627 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001628 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001629
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001630probe_dai_err:
1631 for (i = 0; i < card->num_links; i++)
1632 soc_remove_dai_link(card, i);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001633
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001634card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001635 if (card->remove)
1636 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001637
1638 snd_card_free(card->snd_card);
1639
1640 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001641}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001642
Mark Brown435c5e22008-12-04 15:32:53 +00001643/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001644 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001645 * client_mutex.
1646 */
1647static void snd_soc_instantiate_cards(void)
1648{
1649 struct snd_soc_card *card;
1650 list_for_each_entry(card, &card_list, list)
1651 snd_soc_instantiate_card(card);
1652}
1653
1654/* probes a new socdev */
1655static int soc_probe(struct platform_device *pdev)
1656{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001657 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001658 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001659
1660 /* Bodge while we unpick instantiation */
1661 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001662 INIT_LIST_HEAD(&card->dai_dev_list);
1663 INIT_LIST_HEAD(&card->codec_dev_list);
1664 INIT_LIST_HEAD(&card->platform_dev_list);
1665
Mark Brown435c5e22008-12-04 15:32:53 +00001666 ret = snd_soc_register_card(card);
1667 if (ret != 0) {
1668 dev_err(&pdev->dev, "Failed to register card\n");
1669 return ret;
1670 }
1671
1672 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001673}
1674
1675/* removes a socdev */
1676static int soc_remove(struct platform_device *pdev)
1677{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001678 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001679 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001680
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001681 if (card->instantiated) {
Mike Rapoport914dc182009-05-11 13:04:55 +03001682
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001683 /* make sure any delayed work runs */
1684 for (i = 0; i < card->num_rtd; i++) {
1685 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1686 run_delayed_work(&rtd->delayed_work);
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001687 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001688
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001689 /* remove and free each DAI */
1690 for (i = 0; i < card->num_rtd; i++)
1691 soc_remove_dai_link(card, i);
1692
1693 /* remove the card */
Guennadi Liakhovetskib2dfa622010-03-18 08:23:33 +01001694 if (card->remove)
1695 card->remove(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001696
1697 kfree(card->rtd);
1698 snd_card_free(card->snd_card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001699 }
Mark Brownc5af3a22008-11-28 13:29:45 +00001700 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001701 return 0;
1702}
1703
Mark Brown416356f2009-06-30 19:05:15 +01001704static int soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001705{
Mark Brown416356f2009-06-30 19:05:15 +01001706 struct platform_device *pdev = to_platform_device(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001707 struct snd_soc_card *card = platform_get_drvdata(pdev);
1708 int i;
Mark Brown51737472009-06-22 13:16:51 +01001709
1710 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001711 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001712
1713 /* Flush out pmdown_time work - we actually do want to run it
1714 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001715 for (i = 0; i < card->num_rtd; i++) {
1716 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1717 run_delayed_work(&rtd->delayed_work);
1718 }
Mark Brown51737472009-06-22 13:16:51 +01001719
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001720 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001721
1722 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001723}
1724
Alexey Dobriyan47145212009-12-14 18:00:08 -08001725static const struct dev_pm_ops soc_pm_ops = {
Mark Brown416356f2009-06-30 19:05:15 +01001726 .suspend = soc_suspend,
1727 .resume = soc_resume,
1728 .poweroff = soc_poweroff,
1729};
1730
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001731/* ASoC platform driver */
1732static struct platform_driver soc_driver = {
1733 .driver = {
1734 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001735 .owner = THIS_MODULE,
Mark Brown416356f2009-06-30 19:05:15 +01001736 .pm = &soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001737 },
1738 .probe = soc_probe,
1739 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001740};
1741
1742/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001743static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001744{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001745 struct snd_soc_codec *codec = rtd->codec;
1746 struct snd_soc_platform *platform = rtd->platform;
1747 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1748 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001749 struct snd_pcm *pcm;
1750 char new_name[64];
1751 int ret = 0, playback = 0, capture = 0;
1752
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001753 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00001754 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001755 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001756
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001757 if (codec_dai->driver->playback.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001758 playback = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001759 if (codec_dai->driver->capture.channels_min)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001760 capture = 1;
1761
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001762 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1763 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1764 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001765 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001766 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001767 return ret;
1768 }
1769
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001770 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001771 pcm->private_data = rtd;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001772 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1773 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1774 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1775 soc_pcm_ops.copy = platform->driver->ops->copy;
1776 soc_pcm_ops.silence = platform->driver->ops->silence;
1777 soc_pcm_ops.ack = platform->driver->ops->ack;
1778 soc_pcm_ops.page = platform->driver->ops->page;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001779
1780 if (playback)
1781 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1782
1783 if (capture)
1784 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1785
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001786 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001787 if (ret < 0) {
1788 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001789 return ret;
1790 }
1791
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001792 pcm->private_free = platform->driver->pcm_free;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001793 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1794 cpu_dai->name);
1795 return ret;
1796}
1797
Mark Brown096e49d2009-07-05 15:12:22 +01001798/**
1799 * snd_soc_codec_volatile_register: Report if a register is volatile.
1800 *
1801 * @codec: CODEC to query.
1802 * @reg: Register to query.
1803 *
1804 * Boolean function indiciating if a CODEC register is volatile.
1805 */
1806int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1807{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001808 if (codec->driver->volatile_register)
1809 return codec->driver->volatile_register(reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001810 else
1811 return 0;
1812}
1813EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1814
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001815/**
1816 * snd_soc_new_ac97_codec - initailise AC97 device
1817 * @codec: audio codec
1818 * @ops: AC97 bus operations
1819 * @num: AC97 codec number
1820 *
1821 * Initialises AC97 codec resources for use by ad-hoc devices only.
1822 */
1823int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1824 struct snd_ac97_bus_ops *ops, int num)
1825{
1826 mutex_lock(&codec->mutex);
1827
1828 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1829 if (codec->ac97 == NULL) {
1830 mutex_unlock(&codec->mutex);
1831 return -ENOMEM;
1832 }
1833
1834 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1835 if (codec->ac97->bus == NULL) {
1836 kfree(codec->ac97);
1837 codec->ac97 = NULL;
1838 mutex_unlock(&codec->mutex);
1839 return -ENOMEM;
1840 }
1841
1842 codec->ac97->bus->ops = ops;
1843 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001844
1845 /*
1846 * Mark the AC97 device to be created by us. This way we ensure that the
1847 * device will be registered with the device subsystem later on.
1848 */
1849 codec->ac97_created = 1;
1850
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001851 mutex_unlock(&codec->mutex);
1852 return 0;
1853}
1854EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1855
1856/**
1857 * snd_soc_free_ac97_codec - free AC97 codec device
1858 * @codec: audio codec
1859 *
1860 * Frees AC97 codec device resources.
1861 */
1862void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1863{
1864 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001865#ifdef CONFIG_SND_SOC_AC97_BUS
1866 soc_unregister_ac97_dai_link(codec);
1867#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001868 kfree(codec->ac97->bus);
1869 kfree(codec->ac97);
1870 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001871 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001872 mutex_unlock(&codec->mutex);
1873}
1874EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1875
1876/**
1877 * snd_soc_update_bits - update codec register bits
1878 * @codec: audio codec
1879 * @reg: codec register
1880 * @mask: register mask
1881 * @value: new value
1882 *
1883 * Writes new register value.
1884 *
1885 * Returns 1 for change else 0.
1886 */
1887int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001888 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001889{
1890 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001891 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001892
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001893 old = snd_soc_read(codec, reg);
1894 new = (old & ~mask) | value;
1895 change = old != new;
1896 if (change)
1897 snd_soc_write(codec, reg, new);
1898
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001899 return change;
1900}
1901EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1902
1903/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001904 * snd_soc_update_bits_locked - update codec register bits
1905 * @codec: audio codec
1906 * @reg: codec register
1907 * @mask: register mask
1908 * @value: new value
1909 *
1910 * Writes new register value, and takes the codec mutex.
1911 *
1912 * Returns 1 for change else 0.
1913 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001914int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1915 unsigned short reg, unsigned int mask,
1916 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001917{
1918 int change;
1919
1920 mutex_lock(&codec->mutex);
1921 change = snd_soc_update_bits(codec, reg, mask, value);
1922 mutex_unlock(&codec->mutex);
1923
1924 return change;
1925}
Mark Browndd1b3d52009-12-04 14:22:03 +00001926EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001927
1928/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001929 * snd_soc_test_bits - test register for change
1930 * @codec: audio codec
1931 * @reg: codec register
1932 * @mask: register mask
1933 * @value: new value
1934 *
1935 * Tests a register with a new value and checks if the new value is
1936 * different from the old value.
1937 *
1938 * Returns 1 for change else 0.
1939 */
1940int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001941 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001942{
1943 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001944 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001945
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001946 old = snd_soc_read(codec, reg);
1947 new = (old & ~mask) | value;
1948 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001949
1950 return change;
1951}
1952EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1953
1954/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001955 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1956 * @substream: the pcm substream
1957 * @hw: the hardware parameters
1958 *
1959 * Sets the substream runtime hardware parameters.
1960 */
1961int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1962 const struct snd_pcm_hardware *hw)
1963{
1964 struct snd_pcm_runtime *runtime = substream->runtime;
1965 runtime->hw.info = hw->info;
1966 runtime->hw.formats = hw->formats;
1967 runtime->hw.period_bytes_min = hw->period_bytes_min;
1968 runtime->hw.period_bytes_max = hw->period_bytes_max;
1969 runtime->hw.periods_min = hw->periods_min;
1970 runtime->hw.periods_max = hw->periods_max;
1971 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1972 runtime->hw.fifo_size = hw->fifo_size;
1973 return 0;
1974}
1975EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1976
1977/**
1978 * snd_soc_cnew - create new control
1979 * @_template: control template
1980 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001981 * @long_name: control long name
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001982 *
1983 * Create a new mixer control from a template control.
1984 *
1985 * Returns 0 for success, else error.
1986 */
1987struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1988 void *data, char *long_name)
1989{
1990 struct snd_kcontrol_new template;
1991
1992 memcpy(&template, _template, sizeof(template));
1993 if (long_name)
1994 template.name = long_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001995 template.index = 0;
1996
1997 return snd_ctl_new1(&template, data);
1998}
1999EXPORT_SYMBOL_GPL(snd_soc_cnew);
2000
2001/**
Ian Molton3e8e1952009-01-09 00:23:21 +00002002 * snd_soc_add_controls - add an array of controls to a codec.
2003 * Convienience function to add a list of controls. Many codecs were
2004 * duplicating this code.
2005 *
2006 * @codec: codec to add controls to
2007 * @controls: array of controls to add
2008 * @num_controls: number of elements in the array
2009 *
2010 * Return 0 for success, else error.
2011 */
2012int snd_soc_add_controls(struct snd_soc_codec *codec,
2013 const struct snd_kcontrol_new *controls, int num_controls)
2014{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002015 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002016 int err, i;
2017
2018 for (i = 0; i < num_controls; i++) {
2019 const struct snd_kcontrol_new *control = &controls[i];
2020 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
2021 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002022 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2023 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002024 return err;
2025 }
2026 }
2027
2028 return 0;
2029}
2030EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2031
2032/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002033 * snd_soc_info_enum_double - enumerated double mixer info callback
2034 * @kcontrol: mixer control
2035 * @uinfo: control element information
2036 *
2037 * Callback to provide information about a double enumerated
2038 * mixer control.
2039 *
2040 * Returns 0 for success.
2041 */
2042int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2043 struct snd_ctl_elem_info *uinfo)
2044{
2045 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2046
2047 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2048 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002049 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002050
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002051 if (uinfo->value.enumerated.item > e->max - 1)
2052 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002053 strcpy(uinfo->value.enumerated.name,
2054 e->texts[uinfo->value.enumerated.item]);
2055 return 0;
2056}
2057EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2058
2059/**
2060 * snd_soc_get_enum_double - enumerated double mixer get callback
2061 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002062 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002063 *
2064 * Callback to get the value of a double enumerated mixer.
2065 *
2066 * Returns 0 for success.
2067 */
2068int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2069 struct snd_ctl_elem_value *ucontrol)
2070{
2071 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2072 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002073 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002074
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002075 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002076 ;
2077 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002078 ucontrol->value.enumerated.item[0]
2079 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002080 if (e->shift_l != e->shift_r)
2081 ucontrol->value.enumerated.item[1] =
2082 (val >> e->shift_r) & (bitmask - 1);
2083
2084 return 0;
2085}
2086EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2087
2088/**
2089 * snd_soc_put_enum_double - enumerated double mixer put callback
2090 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002091 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002092 *
2093 * Callback to set the value of a double enumerated mixer.
2094 *
2095 * Returns 0 for success.
2096 */
2097int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2098 struct snd_ctl_elem_value *ucontrol)
2099{
2100 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2101 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002102 unsigned int val;
2103 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002104
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002105 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002106 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002107 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002108 return -EINVAL;
2109 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2110 mask = (bitmask - 1) << e->shift_l;
2111 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002112 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002113 return -EINVAL;
2114 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2115 mask |= (bitmask - 1) << e->shift_r;
2116 }
2117
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002118 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002119}
2120EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2121
2122/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002123 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2124 * @kcontrol: mixer control
2125 * @ucontrol: control element information
2126 *
2127 * Callback to get the value of a double semi enumerated mixer.
2128 *
2129 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2130 * used for handling bitfield coded enumeration for example.
2131 *
2132 * Returns 0 for success.
2133 */
2134int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2135 struct snd_ctl_elem_value *ucontrol)
2136{
2137 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002138 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002139 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002140
2141 reg_val = snd_soc_read(codec, e->reg);
2142 val = (reg_val >> e->shift_l) & e->mask;
2143 for (mux = 0; mux < e->max; mux++) {
2144 if (val == e->values[mux])
2145 break;
2146 }
2147 ucontrol->value.enumerated.item[0] = mux;
2148 if (e->shift_l != e->shift_r) {
2149 val = (reg_val >> e->shift_r) & e->mask;
2150 for (mux = 0; mux < e->max; mux++) {
2151 if (val == e->values[mux])
2152 break;
2153 }
2154 ucontrol->value.enumerated.item[1] = mux;
2155 }
2156
2157 return 0;
2158}
2159EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2160
2161/**
2162 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2163 * @kcontrol: mixer control
2164 * @ucontrol: control element information
2165 *
2166 * Callback to set the value of a double semi enumerated mixer.
2167 *
2168 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2169 * used for handling bitfield coded enumeration for example.
2170 *
2171 * Returns 0 for success.
2172 */
2173int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2174 struct snd_ctl_elem_value *ucontrol)
2175{
2176 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002177 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002178 unsigned int val;
2179 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002180
2181 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2182 return -EINVAL;
2183 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2184 mask = e->mask << e->shift_l;
2185 if (e->shift_l != e->shift_r) {
2186 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2187 return -EINVAL;
2188 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2189 mask |= e->mask << e->shift_r;
2190 }
2191
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002192 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002193}
2194EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2195
2196/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002197 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2198 * @kcontrol: mixer control
2199 * @uinfo: control element information
2200 *
2201 * Callback to provide information about an external enumerated
2202 * single mixer.
2203 *
2204 * Returns 0 for success.
2205 */
2206int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2207 struct snd_ctl_elem_info *uinfo)
2208{
2209 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2210
2211 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2212 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002213 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002214
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002215 if (uinfo->value.enumerated.item > e->max - 1)
2216 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002217 strcpy(uinfo->value.enumerated.name,
2218 e->texts[uinfo->value.enumerated.item]);
2219 return 0;
2220}
2221EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2222
2223/**
2224 * snd_soc_info_volsw_ext - external single mixer info callback
2225 * @kcontrol: mixer control
2226 * @uinfo: control element information
2227 *
2228 * Callback to provide information about a single external mixer control.
2229 *
2230 * Returns 0 for success.
2231 */
2232int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2233 struct snd_ctl_elem_info *uinfo)
2234{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002235 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002236
Mark Brownfd5dfad2009-04-15 21:37:46 +01002237 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002238 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2239 else
2240 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2241
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002242 uinfo->count = 1;
2243 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002244 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002245 return 0;
2246}
2247EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2248
2249/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002250 * snd_soc_info_volsw - single mixer info callback
2251 * @kcontrol: mixer control
2252 * @uinfo: control element information
2253 *
2254 * Callback to provide information about a single mixer control.
2255 *
2256 * Returns 0 for success.
2257 */
2258int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2259 struct snd_ctl_elem_info *uinfo)
2260{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002261 struct soc_mixer_control *mc =
2262 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002263 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00002264 unsigned int shift = mc->shift;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002265 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002266
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002267 if (!mc->platform_max)
2268 mc->platform_max = mc->max;
2269 platform_max = mc->platform_max;
2270
2271 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002272 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2273 else
2274 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2275
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002276 uinfo->count = shift == rshift ? 1 : 2;
2277 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002278 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002279 return 0;
2280}
2281EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2282
2283/**
2284 * snd_soc_get_volsw - single mixer get callback
2285 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002286 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002287 *
2288 * Callback to get the value of a single mixer control.
2289 *
2290 * Returns 0 for success.
2291 */
2292int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2293 struct snd_ctl_elem_value *ucontrol)
2294{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002295 struct soc_mixer_control *mc =
2296 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002297 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002298 unsigned int reg = mc->reg;
2299 unsigned int shift = mc->shift;
2300 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002301 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002302 unsigned int mask = (1 << fls(max)) - 1;
2303 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002304
2305 ucontrol->value.integer.value[0] =
2306 (snd_soc_read(codec, reg) >> shift) & mask;
2307 if (shift != rshift)
2308 ucontrol->value.integer.value[1] =
2309 (snd_soc_read(codec, reg) >> rshift) & mask;
2310 if (invert) {
2311 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002312 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002313 if (shift != rshift)
2314 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002315 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002316 }
2317
2318 return 0;
2319}
2320EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2321
2322/**
2323 * snd_soc_put_volsw - single mixer put callback
2324 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002325 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002326 *
2327 * Callback to set the value of a single mixer control.
2328 *
2329 * Returns 0 for success.
2330 */
2331int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2332 struct snd_ctl_elem_value *ucontrol)
2333{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002334 struct soc_mixer_control *mc =
2335 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002336 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002337 unsigned int reg = mc->reg;
2338 unsigned int shift = mc->shift;
2339 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002340 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002341 unsigned int mask = (1 << fls(max)) - 1;
2342 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002343 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002344
2345 val = (ucontrol->value.integer.value[0] & mask);
2346 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002347 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002348 val_mask = mask << shift;
2349 val = val << shift;
2350 if (shift != rshift) {
2351 val2 = (ucontrol->value.integer.value[1] & mask);
2352 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002353 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002354 val_mask |= mask << rshift;
2355 val |= val2 << rshift;
2356 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002357 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002358}
2359EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2360
2361/**
2362 * snd_soc_info_volsw_2r - double mixer info callback
2363 * @kcontrol: mixer control
2364 * @uinfo: control element information
2365 *
2366 * Callback to provide information about a double mixer control that
2367 * spans 2 codec registers.
2368 *
2369 * Returns 0 for success.
2370 */
2371int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2372 struct snd_ctl_elem_info *uinfo)
2373{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002374 struct soc_mixer_control *mc =
2375 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002376 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002377
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002378 if (!mc->platform_max)
2379 mc->platform_max = mc->max;
2380 platform_max = mc->platform_max;
2381
2382 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002383 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2384 else
2385 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2386
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002387 uinfo->count = 2;
2388 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002389 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002390 return 0;
2391}
2392EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2393
2394/**
2395 * snd_soc_get_volsw_2r - double mixer get callback
2396 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002397 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002398 *
2399 * Callback to get the value of a double mixer control that spans 2 registers.
2400 *
2401 * Returns 0 for success.
2402 */
2403int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_value *ucontrol)
2405{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002406 struct soc_mixer_control *mc =
2407 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002408 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002409 unsigned int reg = mc->reg;
2410 unsigned int reg2 = mc->rreg;
2411 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002412 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002413 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002414 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002415
2416 ucontrol->value.integer.value[0] =
2417 (snd_soc_read(codec, reg) >> shift) & mask;
2418 ucontrol->value.integer.value[1] =
2419 (snd_soc_read(codec, reg2) >> shift) & mask;
2420 if (invert) {
2421 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002422 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002423 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002424 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002425 }
2426
2427 return 0;
2428}
2429EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2430
2431/**
2432 * snd_soc_put_volsw_2r - double mixer set callback
2433 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002434 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002435 *
2436 * Callback to set the value of a double mixer control that spans 2 registers.
2437 *
2438 * Returns 0 for success.
2439 */
2440int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2441 struct snd_ctl_elem_value *ucontrol)
2442{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002443 struct soc_mixer_control *mc =
2444 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002445 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002446 unsigned int reg = mc->reg;
2447 unsigned int reg2 = mc->rreg;
2448 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002449 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002450 unsigned int mask = (1 << fls(max)) - 1;
2451 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002452 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002453 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002454
2455 val_mask = mask << shift;
2456 val = (ucontrol->value.integer.value[0] & mask);
2457 val2 = (ucontrol->value.integer.value[1] & mask);
2458
2459 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002460 val = max - val;
2461 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002462 }
2463
2464 val = val << shift;
2465 val2 = val2 << shift;
2466
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002467 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002468 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002469 return err;
2470
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002471 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002472 return err;
2473}
2474EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2475
Mark Browne13ac2e2008-05-28 17:58:05 +01002476/**
2477 * snd_soc_info_volsw_s8 - signed mixer info callback
2478 * @kcontrol: mixer control
2479 * @uinfo: control element information
2480 *
2481 * Callback to provide information about a signed mixer control.
2482 *
2483 * Returns 0 for success.
2484 */
2485int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2486 struct snd_ctl_elem_info *uinfo)
2487{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002488 struct soc_mixer_control *mc =
2489 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002490 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002491 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002492
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002493 if (!mc->platform_max)
2494 mc->platform_max = mc->max;
2495 platform_max = mc->platform_max;
2496
Mark Browne13ac2e2008-05-28 17:58:05 +01002497 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2498 uinfo->count = 2;
2499 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002500 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002501 return 0;
2502}
2503EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2504
2505/**
2506 * snd_soc_get_volsw_s8 - signed mixer get callback
2507 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002508 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002509 *
2510 * Callback to get the value of a signed mixer control.
2511 *
2512 * Returns 0 for success.
2513 */
2514int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2515 struct snd_ctl_elem_value *ucontrol)
2516{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002517 struct soc_mixer_control *mc =
2518 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002519 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002520 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002521 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002522 int val = snd_soc_read(codec, reg);
2523
2524 ucontrol->value.integer.value[0] =
2525 ((signed char)(val & 0xff))-min;
2526 ucontrol->value.integer.value[1] =
2527 ((signed char)((val >> 8) & 0xff))-min;
2528 return 0;
2529}
2530EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2531
2532/**
2533 * snd_soc_put_volsw_sgn - signed mixer put callback
2534 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002535 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002536 *
2537 * Callback to set the value of a signed mixer control.
2538 *
2539 * Returns 0 for success.
2540 */
2541int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2542 struct snd_ctl_elem_value *ucontrol)
2543{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002544 struct soc_mixer_control *mc =
2545 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002546 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002547 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002548 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002549 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002550
2551 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2552 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2553
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002554 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002555}
2556EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2557
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002558/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002559 * snd_soc_limit_volume - Set new limit to an existing volume control.
2560 *
2561 * @codec: where to look for the control
2562 * @name: Name of the control
2563 * @max: new maximum limit
2564 *
2565 * Return 0 for success, else error.
2566 */
2567int snd_soc_limit_volume(struct snd_soc_codec *codec,
2568 const char *name, int max)
2569{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002570 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002571 struct snd_kcontrol *kctl;
2572 struct soc_mixer_control *mc;
2573 int found = 0;
2574 int ret = -EINVAL;
2575
2576 /* Sanity check for name and max */
2577 if (unlikely(!name || max <= 0))
2578 return -EINVAL;
2579
2580 list_for_each_entry(kctl, &card->controls, list) {
2581 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2582 found = 1;
2583 break;
2584 }
2585 }
2586 if (found) {
2587 mc = (struct soc_mixer_control *)kctl->private_value;
2588 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002589 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002590 ret = 0;
2591 }
2592 }
2593 return ret;
2594}
2595EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2596
2597/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002598 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2599 * mixer info callback
2600 * @kcontrol: mixer control
2601 * @uinfo: control element information
2602 *
2603 * Returns 0 for success.
2604 */
2605int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2606 struct snd_ctl_elem_info *uinfo)
2607{
2608 struct soc_mixer_control *mc =
2609 (struct soc_mixer_control *)kcontrol->private_value;
2610 int max = mc->max;
2611 int min = mc->min;
2612
2613 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2614 uinfo->count = 2;
2615 uinfo->value.integer.min = 0;
2616 uinfo->value.integer.max = max-min;
2617
2618 return 0;
2619}
2620EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2621
2622/**
2623 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2624 * mixer get callback
2625 * @kcontrol: mixer control
2626 * @uinfo: control element information
2627 *
2628 * Returns 0 for success.
2629 */
2630int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2631 struct snd_ctl_elem_value *ucontrol)
2632{
2633 struct soc_mixer_control *mc =
2634 (struct soc_mixer_control *)kcontrol->private_value;
2635 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2636 unsigned int mask = (1<<mc->shift)-1;
2637 int min = mc->min;
2638 int val = snd_soc_read(codec, mc->reg) & mask;
2639 int valr = snd_soc_read(codec, mc->rreg) & mask;
2640
Stuart Longland20630c72010-06-18 12:56:10 +10002641 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2642 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002643 return 0;
2644}
2645EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2646
2647/**
2648 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2649 * mixer put callback
2650 * @kcontrol: mixer control
2651 * @uinfo: control element information
2652 *
2653 * Returns 0 for success.
2654 */
2655int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2656 struct snd_ctl_elem_value *ucontrol)
2657{
2658 struct soc_mixer_control *mc =
2659 (struct soc_mixer_control *)kcontrol->private_value;
2660 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2661 unsigned int mask = (1<<mc->shift)-1;
2662 int min = mc->min;
2663 int ret;
2664 unsigned int val, valr, oval, ovalr;
2665
2666 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2667 val &= mask;
2668 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2669 valr &= mask;
2670
2671 oval = snd_soc_read(codec, mc->reg) & mask;
2672 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2673
2674 ret = 0;
2675 if (oval != val) {
2676 ret = snd_soc_write(codec, mc->reg, val);
2677 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002678 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002679 }
2680 if (ovalr != valr) {
2681 ret = snd_soc_write(codec, mc->rreg, valr);
2682 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002683 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002684 }
2685
2686 return 0;
2687}
2688EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2689
2690/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002691 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2692 * @dai: DAI
2693 * @clk_id: DAI specific clock ID
2694 * @freq: new clock frequency in Hz
2695 * @dir: new clock direction - input/output.
2696 *
2697 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2698 */
2699int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2700 unsigned int freq, int dir)
2701{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002702 if (dai->driver && dai->driver->ops->set_sysclk)
2703 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002704 else
2705 return -EINVAL;
2706}
2707EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2708
2709/**
2710 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2711 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002712 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002713 * @div: new clock divisor.
2714 *
2715 * Configures the clock dividers. This is used to derive the best DAI bit and
2716 * frame clocks from the system or master clock. It's best to set the DAI bit
2717 * and frame clocks as low as possible to save system power.
2718 */
2719int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2720 int div_id, int div)
2721{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002722 if (dai->driver && dai->driver->ops->set_clkdiv)
2723 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002724 else
2725 return -EINVAL;
2726}
2727EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2728
2729/**
2730 * snd_soc_dai_set_pll - configure DAI PLL.
2731 * @dai: DAI
2732 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002733 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002734 * @freq_in: PLL input clock frequency in Hz
2735 * @freq_out: requested PLL output clock frequency in Hz
2736 *
2737 * Configures and enables PLL to generate output clock based on input clock.
2738 */
Mark Brown85488032009-09-05 18:52:16 +01002739int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2740 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002741{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002742 if (dai->driver && dai->driver->ops->set_pll)
2743 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002744 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002745 else
2746 return -EINVAL;
2747}
2748EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2749
2750/**
2751 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2752 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002753 * @fmt: SND_SOC_DAIFMT_ format value.
2754 *
2755 * Configures the DAI hardware format and clocking.
2756 */
2757int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2758{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002759 if (dai->driver && dai->driver->ops->set_fmt)
2760 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002761 else
2762 return -EINVAL;
2763}
2764EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2765
2766/**
2767 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2768 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002769 * @tx_mask: bitmask representing active TX slots.
2770 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002771 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002772 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002773 *
2774 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2775 * specific.
2776 */
2777int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002778 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002779{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002780 if (dai->driver && dai->driver->ops->set_tdm_slot)
2781 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002782 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002783 else
2784 return -EINVAL;
2785}
2786EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2787
2788/**
Barry Song472df3c2009-09-12 01:16:29 +08002789 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2790 * @dai: DAI
2791 * @tx_num: how many TX channels
2792 * @tx_slot: pointer to an array which imply the TX slot number channel
2793 * 0~num-1 uses
2794 * @rx_num: how many RX channels
2795 * @rx_slot: pointer to an array which imply the RX slot number channel
2796 * 0~num-1 uses
2797 *
2798 * configure the relationship between channel number and TDM slot number.
2799 */
2800int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2801 unsigned int tx_num, unsigned int *tx_slot,
2802 unsigned int rx_num, unsigned int *rx_slot)
2803{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002804 if (dai->driver && dai->driver->ops->set_channel_map)
2805 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002806 rx_num, rx_slot);
2807 else
2808 return -EINVAL;
2809}
2810EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2811
2812/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002813 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2814 * @dai: DAI
2815 * @tristate: tristate enable
2816 *
2817 * Tristates the DAI so that others can use it.
2818 */
2819int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2820{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002821 if (dai->driver && dai->driver->ops->set_tristate)
2822 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002823 else
2824 return -EINVAL;
2825}
2826EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2827
2828/**
2829 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2830 * @dai: DAI
2831 * @mute: mute enable
2832 *
2833 * Mutes the DAI DAC.
2834 */
2835int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2836{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002837 if (dai->driver && dai->driver->ops->digital_mute)
2838 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002839 else
2840 return -EINVAL;
2841}
2842EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2843
Mark Brownc5af3a22008-11-28 13:29:45 +00002844/**
2845 * snd_soc_register_card - Register a card with the ASoC core
2846 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002847 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002848 *
2849 * Note that currently this is an internal only function: it will be
2850 * exposed to machine drivers after further backporting of ASoC v2
2851 * registration APIs.
2852 */
2853static int snd_soc_register_card(struct snd_soc_card *card)
2854{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002855 int i;
2856
Mark Brownc5af3a22008-11-28 13:29:45 +00002857 if (!card->name || !card->dev)
2858 return -EINVAL;
2859
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002860 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2861 GFP_KERNEL);
2862 if (card->rtd == NULL)
2863 return -ENOMEM;
2864
2865 for (i = 0; i < card->num_links; i++)
2866 card->rtd[i].dai_link = &card->dai_link[i];
2867
Mark Brownc5af3a22008-11-28 13:29:45 +00002868 INIT_LIST_HEAD(&card->list);
2869 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002870 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002871
2872 mutex_lock(&client_mutex);
2873 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002874 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002875 mutex_unlock(&client_mutex);
2876
2877 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2878
2879 return 0;
2880}
2881
2882/**
2883 * snd_soc_unregister_card - Unregister a card with the ASoC core
2884 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002885 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002886 *
2887 * Note that currently this is an internal only function: it will be
2888 * exposed to machine drivers after further backporting of ASoC v2
2889 * registration APIs.
2890 */
2891static int snd_soc_unregister_card(struct snd_soc_card *card)
2892{
2893 mutex_lock(&client_mutex);
2894 list_del(&card->list);
2895 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002896 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2897
2898 return 0;
2899}
2900
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002901/*
2902 * Simplify DAI link configuration by removing ".-1" from device names
2903 * and sanitizing names.
2904 */
2905static inline char *fmt_single_name(struct device *dev, int *id)
2906{
2907 char *found, name[NAME_SIZE];
2908 int id1, id2;
2909
2910 if (dev_name(dev) == NULL)
2911 return NULL;
2912
2913 strncpy(name, dev_name(dev), NAME_SIZE);
2914
2915 /* are we a "%s.%d" name (platform and SPI components) */
2916 found = strstr(name, dev->driver->name);
2917 if (found) {
2918 /* get ID */
2919 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2920
2921 /* discard ID from name if ID == -1 */
2922 if (*id == -1)
2923 found[strlen(dev->driver->name)] = '\0';
2924 }
2925
2926 } else {
2927 /* I2C component devices are named "bus-addr" */
2928 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2929 char tmp[NAME_SIZE];
2930
2931 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002932 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002933
2934 /* sanitize component name for DAI link creation */
2935 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2936 strncpy(name, tmp, NAME_SIZE);
2937 } else
2938 *id = 0;
2939 }
2940
2941 return kstrdup(name, GFP_KERNEL);
2942}
2943
2944/*
2945 * Simplify DAI link naming for single devices with multiple DAIs by removing
2946 * any ".-1" and using the DAI name (instead of device name).
2947 */
2948static inline char *fmt_multiple_name(struct device *dev,
2949 struct snd_soc_dai_driver *dai_drv)
2950{
2951 if (dai_drv->name == NULL) {
2952 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2953 dev_name(dev));
2954 return NULL;
2955 }
2956
2957 return kstrdup(dai_drv->name, GFP_KERNEL);
2958}
2959
Mark Brown91151712008-11-30 23:31:24 +00002960/**
2961 * snd_soc_register_dai - Register a DAI with the ASoC core
2962 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002963 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002964 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002965int snd_soc_register_dai(struct device *dev,
2966 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00002967{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002968 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00002969
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002970 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00002971
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002972 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2973 if (dai == NULL)
2974 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08002975
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002976 /* create DAI component name */
2977 dai->name = fmt_single_name(dev, &dai->id);
2978 if (dai->name == NULL) {
2979 kfree(dai);
2980 return -ENOMEM;
2981 }
2982
2983 dai->dev = dev;
2984 dai->driver = dai_drv;
2985 if (!dai->driver->ops)
2986 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00002987
2988 mutex_lock(&client_mutex);
2989 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002990 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002991 mutex_unlock(&client_mutex);
2992
2993 pr_debug("Registered DAI '%s'\n", dai->name);
2994
2995 return 0;
2996}
2997EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2998
2999/**
3000 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3001 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003002 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003003 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003004void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003005{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003006 struct snd_soc_dai *dai;
3007
3008 list_for_each_entry(dai, &dai_list, list) {
3009 if (dev == dai->dev)
3010 goto found;
3011 }
3012 return;
3013
3014found:
Mark Brown91151712008-11-30 23:31:24 +00003015 mutex_lock(&client_mutex);
3016 list_del(&dai->list);
3017 mutex_unlock(&client_mutex);
3018
3019 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003020 kfree(dai->name);
3021 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003022}
3023EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3024
3025/**
3026 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3027 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003028 * @dai: Array of DAIs to register
3029 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003030 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003031int snd_soc_register_dais(struct device *dev,
3032 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003033{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003034 struct snd_soc_dai *dai;
3035 int i, ret = 0;
3036
Liam Girdwood720ffa42010-08-18 00:30:30 +01003037 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003038
3039 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003040
3041 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3042 if (dai == NULL)
3043 return -ENOMEM;
3044
3045 /* create DAI component name */
3046 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3047 if (dai->name == NULL) {
3048 kfree(dai);
3049 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003050 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003051 }
3052
3053 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003054 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003055 if (dai->driver->id)
3056 dai->id = dai->driver->id;
3057 else
3058 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003059 if (!dai->driver->ops)
3060 dai->driver->ops = &null_dai_ops;
3061
3062 mutex_lock(&client_mutex);
3063 list_add(&dai->list, &dai_list);
3064 mutex_unlock(&client_mutex);
3065
3066 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003067 }
3068
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003069 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003070 return 0;
3071
3072err:
3073 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003074 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003075
3076 return ret;
3077}
3078EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3079
3080/**
3081 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3082 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003083 * @dai: Array of DAIs to unregister
3084 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003085 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003086void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003087{
3088 int i;
3089
3090 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003091 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003092}
3093EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3094
Mark Brown12a48a8c2008-12-03 19:40:30 +00003095/**
3096 * snd_soc_register_platform - Register a platform with the ASoC core
3097 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003098 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003099 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003100int snd_soc_register_platform(struct device *dev,
3101 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003102{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003103 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003104
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003105 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3106
3107 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3108 if (platform == NULL)
3109 return -ENOMEM;
3110
3111 /* create platform component name */
3112 platform->name = fmt_single_name(dev, &platform->id);
3113 if (platform->name == NULL) {
3114 kfree(platform);
3115 return -ENOMEM;
3116 }
3117
3118 platform->dev = dev;
3119 platform->driver = platform_drv;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003120
3121 mutex_lock(&client_mutex);
3122 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003123 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003124 mutex_unlock(&client_mutex);
3125
3126 pr_debug("Registered platform '%s'\n", platform->name);
3127
3128 return 0;
3129}
3130EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3131
3132/**
3133 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3134 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003135 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003136 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003137void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003138{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003139 struct snd_soc_platform *platform;
3140
3141 list_for_each_entry(platform, &platform_list, list) {
3142 if (dev == platform->dev)
3143 goto found;
3144 }
3145 return;
3146
3147found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003148 mutex_lock(&client_mutex);
3149 list_del(&platform->list);
3150 mutex_unlock(&client_mutex);
3151
3152 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003153 kfree(platform->name);
3154 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003155}
3156EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3157
Mark Brown151ab222009-05-09 16:22:58 +01003158static u64 codec_format_map[] = {
3159 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3160 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3161 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3162 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3163 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3164 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3165 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3166 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3167 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3168 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3169 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3170 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3171 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3172 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3173 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3174 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3175};
3176
3177/* Fix up the DAI formats for endianness: codecs don't actually see
3178 * the endianness of the data but we're using the CPU format
3179 * definitions which do need to include endianness so we ensure that
3180 * codec DAIs always have both big and little endian variants set.
3181 */
3182static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3183{
3184 int i;
3185
3186 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3187 if (stream->formats & codec_format_map[i])
3188 stream->formats |= codec_format_map[i];
3189}
3190
Mark Brown0d0cf002008-12-10 14:32:45 +00003191/**
3192 * snd_soc_register_codec - Register a codec with the ASoC core
3193 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003194 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003195 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003196int snd_soc_register_codec(struct device *dev,
3197 struct snd_soc_codec_driver *codec_drv,
3198 struct snd_soc_dai_driver *dai_drv, int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003199{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003200 struct snd_soc_codec *codec;
3201 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003202
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003203 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003204
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003205 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3206 if (codec == NULL)
3207 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003208
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003209 /* create CODEC component name */
3210 codec->name = fmt_single_name(dev, &codec->id);
3211 if (codec->name == NULL) {
3212 kfree(codec);
3213 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003214 }
3215
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003216 /* allocate CODEC register cache */
3217 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3218
3219 if (codec_drv->reg_cache_default)
3220 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3221 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3222 else
3223 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3224 codec_drv->reg_word_size, GFP_KERNEL);
3225
3226 if (codec->reg_cache == NULL) {
3227 kfree(codec->name);
3228 kfree(codec);
3229 return -ENOMEM;
3230 }
3231 }
3232
3233 codec->dev = dev;
3234 codec->driver = codec_drv;
3235 codec->bias_level = SND_SOC_BIAS_OFF;
3236 codec->num_dai = num_dai;
3237 mutex_init(&codec->mutex);
3238 INIT_LIST_HEAD(&codec->dapm_widgets);
3239 INIT_LIST_HEAD(&codec->dapm_paths);
3240
3241 for (i = 0; i < num_dai; i++) {
3242 fixup_codec_formats(&dai_drv[i].playback);
3243 fixup_codec_formats(&dai_drv[i].capture);
3244 }
3245
Mark Brown26b01cc2010-08-18 20:20:55 +01003246 /* register any DAIs */
3247 if (num_dai) {
3248 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3249 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003250 goto error;
Mark Brown26b01cc2010-08-18 20:20:55 +01003251 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003252
Mark Brown0d0cf002008-12-10 14:32:45 +00003253 mutex_lock(&client_mutex);
3254 list_add(&codec->list, &codec_list);
3255 snd_soc_instantiate_cards();
3256 mutex_unlock(&client_mutex);
3257
3258 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003259 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003260
3261error:
3262 for (i--; i >= 0; i--)
3263 snd_soc_unregister_dai(dev);
3264
3265 if (codec->reg_cache)
3266 kfree(codec->reg_cache);
3267 kfree(codec->name);
3268 kfree(codec);
3269 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003270}
3271EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3272
3273/**
3274 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3275 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003276 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003277 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003278void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003279{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003280 struct snd_soc_codec *codec;
3281 int i;
3282
3283 list_for_each_entry(codec, &codec_list, list) {
3284 if (dev == codec->dev)
3285 goto found;
3286 }
3287 return;
3288
3289found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003290 if (codec->num_dai)
3291 for (i = 0; i < codec->num_dai; i++)
3292 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003293
Mark Brown0d0cf002008-12-10 14:32:45 +00003294 mutex_lock(&client_mutex);
3295 list_del(&codec->list);
3296 mutex_unlock(&client_mutex);
3297
3298 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003299
3300 if (codec->reg_cache)
3301 kfree(codec->reg_cache);
3302 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003303}
3304EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3305
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003306static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003307{
Mark Brown384c89e2008-12-03 17:34:03 +00003308#ifdef CONFIG_DEBUG_FS
3309 debugfs_root = debugfs_create_dir("asoc", NULL);
3310 if (IS_ERR(debugfs_root) || !debugfs_root) {
3311 printk(KERN_WARNING
3312 "ASoC: Failed to create debugfs directory\n");
3313 debugfs_root = NULL;
3314 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003315
3316 if (!debugfs_create_file("codecs", 0444, debugfs_root, NULL,
3317 &codec_list_fops))
3318 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3319
Mark Brownf3208782010-09-15 18:19:07 +01003320 if (!debugfs_create_file("dais", 0444, debugfs_root, NULL,
3321 &dai_list_fops))
3322 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003323
3324 if (!debugfs_create_file("platforms", 0444, debugfs_root, NULL,
3325 &platform_list_fops))
3326 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003327#endif
3328
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003329 return platform_driver_register(&soc_driver);
3330}
Mark Brown4abe8e12010-10-12 17:41:03 +01003331module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003332
Mark Brown7d8c16a2008-11-30 22:11:24 +00003333static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003334{
Mark Brown384c89e2008-12-03 17:34:03 +00003335#ifdef CONFIG_DEBUG_FS
3336 debugfs_remove_recursive(debugfs_root);
3337#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003338 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003339}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003340module_exit(snd_soc_exit);
3341
3342/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003343MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003344MODULE_DESCRIPTION("ALSA SoC Core");
3345MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003346MODULE_ALIAS("platform:soc-audio");