blob: 51eef9b7b53f01534a619fb5d5794f91ede96937 [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>
Mark Brownf0e8ed82011-09-20 11:41:54 +010033#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070035#include <linux/of.h>
Marek Vasut474828a2009-07-22 13:01:03 +020036#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020037#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000038#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020039#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/soc.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/initval.h>
43
Mark Browna8b1d342010-11-03 18:05:58 -040044#define CREATE_TRACE_POINTS
45#include <trace/events/asoc.h>
46
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000047#define NAME_SIZE 32
48
Frank Mandarinodb2a4162006-10-06 18:31:09 +020049static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
50
Mark Brown384c89e2008-12-03 17:34:03 +000051#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000052struct dentry *snd_soc_debugfs_root;
53EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000054#endif
55
Mark Brownc5af3a22008-11-28 13:29:45 +000056static DEFINE_MUTEX(client_mutex);
57static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000058static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000061
Liam Girdwoodddee6272011-06-09 14:45:53 +010062int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
Mark Brownc5af3a22008-11-28 13:29:45 +000063
Frank Mandarinodb2a4162006-10-06 18:31:09 +020064/*
65 * This is a timeout to do a DAPM powerdown after a stream is closed().
66 * It can be used to eliminate pops between different playback streams, e.g.
67 * between two audio tracks.
68 */
69static int pmdown_time = 5000;
70module_param(pmdown_time, int, 0);
71MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000073/* returns the minimum number of bytes needed to represent
74 * a particular given value */
75static int min_bytes_needed(unsigned long val)
76{
77 int c = 0;
78 int i;
79
80 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
81 if (val & (1UL << i))
82 break;
83 c = (sizeof val * 8) - c;
84 if (!c || (c % 8))
85 c = (c + 8) / 8;
86 else
87 c /= 8;
88 return c;
89}
90
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000091/* fill buf which is 'len' bytes with a formatted
92 * string of the form 'reg: value\n' */
93static int format_register_str(struct snd_soc_codec *codec,
94 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000095{
Stephen Warren00b317a2011-04-01 14:50:44 -060096 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
97 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000098 int ret;
99 char tmpbuf[len + 1];
100 char regbuf[regsize + 1];
101
102 /* since tmpbuf is allocated on the stack, warn the callers if they
103 * try to abuse this function */
104 WARN_ON(len > 63);
105
106 /* +2 for ': ' and + 1 for '\n' */
107 if (wordsize + regsize + 2 + 1 != len)
108 return -EINVAL;
109
Mark Brown25032c12011-08-01 13:52:48 +0900110 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000111 if (ret < 0) {
112 memset(regbuf, 'X', regsize);
113 regbuf[regsize] = '\0';
114 } else {
115 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
116 }
117
118 /* prepare the buffer */
119 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
120 /* copy it back to the caller without the '\0' */
121 memcpy(buf, tmpbuf, len);
122
123 return 0;
124}
125
126/* codec register dump */
127static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
128 size_t count, loff_t pos)
129{
130 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000131 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000132 int len;
133 size_t total = 0;
134 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000135
Stephen Warren00b317a2011-04-01 14:50:44 -0600136 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
137 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000138
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000139 len = wordsize + regsize + 2 + 1;
140
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000141 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000142 return 0;
143
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000144 if (codec->driver->reg_cache_step)
145 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000146
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000147 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200148 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000149 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000150 if (codec->driver->display_register) {
151 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000152 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100153 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000154 /* only support larger than PAGE_SIZE bytes debugfs
155 * entries for the default case */
156 if (p >= pos) {
157 if (total + len >= count - 1)
158 break;
159 format_register_str(codec, i, buf + total, len);
160 total += len;
161 }
162 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100163 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000164 }
165
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000166 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000167
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000169}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000170
Mark Brown2624d5f2009-11-03 21:56:13 +0000171static ssize_t codec_reg_show(struct device *dev,
172 struct device_attribute *attr, char *buf)
173{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000174 struct snd_soc_pcm_runtime *rtd =
175 container_of(dev, struct snd_soc_pcm_runtime, dev);
176
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000177 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000178}
179
180static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
181
Mark Browndbe21402010-02-12 11:37:24 +0000182static ssize_t pmdown_time_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
184{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000185 struct snd_soc_pcm_runtime *rtd =
186 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Browndbe21402010-02-12 11:37:24 +0000187
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000189}
190
191static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
194{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000195 struct snd_soc_pcm_runtime *rtd =
196 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Brownc593b522010-10-27 20:11:17 -0700197 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000198
Mark Brownc593b522010-10-27 20:11:17 -0700199 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
200 if (ret)
201 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000202
203 return count;
204}
205
206static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
207
Mark Brown2624d5f2009-11-03 21:56:13 +0000208#ifdef CONFIG_DEBUG_FS
209static int codec_reg_open_file(struct inode *inode, struct file *file)
210{
211 file->private_data = inode->i_private;
212 return 0;
213}
214
215static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000216 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000217{
218 ssize_t ret;
219 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000220 char *buf;
221
222 if (*ppos < 0 || !count)
223 return -EINVAL;
224
225 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000226 if (!buf)
227 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000228
229 ret = soc_codec_reg_show(codec, buf, count, *ppos);
230 if (ret >= 0) {
231 if (copy_to_user(user_buf, buf, ret)) {
232 kfree(buf);
233 return -EFAULT;
234 }
235 *ppos += ret;
236 }
237
Mark Brown2624d5f2009-11-03 21:56:13 +0000238 kfree(buf);
239 return ret;
240}
241
242static ssize_t codec_reg_write_file(struct file *file,
243 const char __user *user_buf, size_t count, loff_t *ppos)
244{
245 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700246 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000247 char *start = buf;
248 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000249 struct snd_soc_codec *codec = file->private_data;
250
251 buf_size = min(count, (sizeof(buf)-1));
252 if (copy_from_user(buf, user_buf, buf_size))
253 return -EFAULT;
254 buf[buf_size] = 0;
255
Mark Brown2624d5f2009-11-03 21:56:13 +0000256 while (*start == ' ')
257 start++;
258 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000259 while (*start == ' ')
260 start++;
261 if (strict_strtoul(start, 16, &value))
262 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000263
264 /* Userspace has been fiddling around behind the kernel's back */
265 add_taint(TAINT_USER);
266
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000267 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000268 return buf_size;
269}
270
271static const struct file_operations codec_reg_fops = {
272 .open = codec_reg_open_file,
273 .read = codec_reg_read_file,
274 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200275 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000276};
277
278static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
279{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200280 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
281
282 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
283 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000284 if (!codec->debugfs_codec_root) {
285 printk(KERN_WARNING
286 "ASoC: Failed to create codec debugfs directory\n");
287 return;
288 }
289
Mark Brownaaee8ef2011-01-26 20:53:50 +0000290 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
291 &codec->cache_sync);
292 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
293 &codec->cache_only);
294
Mark Brown2624d5f2009-11-03 21:56:13 +0000295 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
296 codec->debugfs_codec_root,
297 codec, &codec_reg_fops);
298 if (!codec->debugfs_reg)
299 printk(KERN_WARNING
300 "ASoC: Failed to create codec register debugfs file\n");
301
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200302 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000303}
304
305static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
306{
307 debugfs_remove_recursive(codec->debugfs_codec_root);
308}
309
Mark Brownc3c5a192010-09-15 18:15:14 +0100310static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
311 size_t count, loff_t *ppos)
312{
313 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100314 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100315 struct snd_soc_codec *codec;
316
317 if (!buf)
318 return -ENOMEM;
319
Mark Brown2b194f9d2010-10-13 10:52:16 +0100320 list_for_each_entry(codec, &codec_list, list) {
321 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
322 codec->name);
323 if (len >= 0)
324 ret += len;
325 if (ret > PAGE_SIZE) {
326 ret = PAGE_SIZE;
327 break;
328 }
329 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100330
331 if (ret >= 0)
332 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
333
334 kfree(buf);
335
336 return ret;
337}
338
339static const struct file_operations codec_list_fops = {
340 .read = codec_list_read_file,
341 .llseek = default_llseek,/* read accesses f_pos */
342};
343
Mark Brownf3208782010-09-15 18:19:07 +0100344static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
345 size_t count, loff_t *ppos)
346{
347 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100348 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100349 struct snd_soc_dai *dai;
350
351 if (!buf)
352 return -ENOMEM;
353
Mark Brown2b194f9d2010-10-13 10:52:16 +0100354 list_for_each_entry(dai, &dai_list, list) {
355 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
356 if (len >= 0)
357 ret += len;
358 if (ret > PAGE_SIZE) {
359 ret = PAGE_SIZE;
360 break;
361 }
362 }
Mark Brownf3208782010-09-15 18:19:07 +0100363
Mark Brown2b194f9d2010-10-13 10:52:16 +0100364 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100365
366 kfree(buf);
367
368 return ret;
369}
370
371static const struct file_operations dai_list_fops = {
372 .read = dai_list_read_file,
373 .llseek = default_llseek,/* read accesses f_pos */
374};
375
Mark Brown19c7ac22010-09-15 18:22:40 +0100376static ssize_t platform_list_read_file(struct file *file,
377 char __user *user_buf,
378 size_t count, loff_t *ppos)
379{
380 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100381 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100382 struct snd_soc_platform *platform;
383
384 if (!buf)
385 return -ENOMEM;
386
Mark Brown2b194f9d2010-10-13 10:52:16 +0100387 list_for_each_entry(platform, &platform_list, list) {
388 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
389 platform->name);
390 if (len >= 0)
391 ret += len;
392 if (ret > PAGE_SIZE) {
393 ret = PAGE_SIZE;
394 break;
395 }
396 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100397
Mark Brown2b194f9d2010-10-13 10:52:16 +0100398 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100399
400 kfree(buf);
401
402 return ret;
403}
404
405static const struct file_operations platform_list_fops = {
406 .read = platform_list_read_file,
407 .llseek = default_llseek,/* read accesses f_pos */
408};
409
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200410static void soc_init_card_debugfs(struct snd_soc_card *card)
411{
412 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000413 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200414 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200415 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100416 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200417 return;
418 }
419
420 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
421 card->debugfs_card_root,
422 &card->pop_time);
423 if (!card->debugfs_pop_time)
424 dev_warn(card->dev,
425 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200426}
427
428static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
429{
430 debugfs_remove_recursive(card->debugfs_card_root);
431}
432
Mark Brown2624d5f2009-11-03 21:56:13 +0000433#else
434
435static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
436{
437}
438
439static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
440{
441}
Axel Linb95fccb2010-11-09 17:06:44 +0800442
443static inline void soc_init_card_debugfs(struct snd_soc_card *card)
444{
445}
446
447static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
448{
449}
Mark Brown2624d5f2009-11-03 21:56:13 +0000450#endif
451
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200452#ifdef CONFIG_SND_SOC_AC97_BUS
453/* unregister ac97 codec */
454static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
455{
456 if (codec->ac97->dev.bus)
457 device_unregister(&codec->ac97->dev);
458 return 0;
459}
460
461/* stop no dev release warning */
462static void soc_ac97_device_release(struct device *dev){}
463
464/* register ac97 codec to bus */
465static int soc_ac97_dev_register(struct snd_soc_codec *codec)
466{
467 int err;
468
469 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100470 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200471 codec->ac97->dev.release = soc_ac97_device_release;
472
Kay Sieversbb072bf2008-11-02 03:50:35 +0100473 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000474 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200475 err = device_register(&codec->ac97->dev);
476 if (err < 0) {
477 snd_printk(KERN_ERR "Can't register ac97 bus\n");
478 codec->ac97->dev.bus = NULL;
479 return err;
480 }
481 return 0;
482}
483#endif
484
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000485#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200486/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000487int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200488{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000489 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200490 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200491 int i;
492
Daniel Macke3509ff2009-06-03 17:44:49 +0200493 /* If the initialization of this soc device failed, there is no codec
494 * associated with it. Just bail out in this case.
495 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000496 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200497 return 0;
498
Andy Green6ed25972008-06-13 16:24:05 +0100499 /* Due to the resume being scheduled into a workqueue we could
500 * suspend before that's finished - wait for it to complete.
501 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000502 snd_power_lock(card->snd_card);
503 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
504 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100505
506 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000507 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100508
Mark Browna00f90f2010-12-02 16:24:24 +0000509 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000510 for (i = 0; i < card->num_rtd; i++) {
511 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
512 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100513
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000514 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100515 continue;
516
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000517 if (drv->ops->digital_mute && dai->playback_active)
518 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200519 }
520
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100521 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 for (i = 0; i < card->num_rtd; i++) {
523 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100524 continue;
525
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000526 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100527 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100528
Mark Brown87506542008-11-18 20:50:34 +0000529 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000530 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532 for (i = 0; i < card->num_rtd; i++) {
533 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
534 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100535
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000536 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100537 continue;
538
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000539 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
540 cpu_dai->driver->suspend(cpu_dai);
541 if (platform->driver->suspend && !platform->suspended) {
542 platform->driver->suspend(cpu_dai);
543 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100544 }
545 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200546
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547 /* close any waiting streams and save state */
548 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100549 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200550 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000551 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100552
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 for (i = 0; i < card->num_rtd; i++) {
554 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
555
556 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100557 continue;
558
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559 if (driver->playback.stream_name != NULL)
560 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
561 SND_SOC_DAPM_STREAM_SUSPEND);
562
563 if (driver->capture.stream_name != NULL)
564 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
565 SND_SOC_DAPM_STREAM_SUSPEND);
566 }
567
568 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200569 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000570 /* If there are paths active then the CODEC will be held with
571 * bias _ON and should not be suspended. */
572 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200573 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 case SND_SOC_BIAS_STANDBY:
575 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100576 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900578 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000579 break;
580 default:
581 dev_dbg(codec->dev, "CODEC is on over suspend\n");
582 break;
583 }
584 }
585 }
586
587 for (i = 0; i < card->num_rtd; i++) {
588 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
589
590 if (card->rtd[i].dai_link->ignore_suspend)
591 continue;
592
593 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
594 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200595 }
596
Mark Brown87506542008-11-18 20:50:34 +0000597 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000598 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200599
600 return 0;
601}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000602EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200603
Andy Green6ed25972008-06-13 16:24:05 +0100604/* deferred resume work, so resume can complete before we finished
605 * setting our codec back up, which can be very slow on I2C
606 */
607static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200608{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609 struct snd_soc_card *card =
610 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200611 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200612 int i;
613
Andy Green6ed25972008-06-13 16:24:05 +0100614 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
615 * so userspace apps are blocked from touching us
616 */
617
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100619
Mark Brown99497882010-05-07 20:24:05 +0100620 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100622
Mark Brown87506542008-11-18 20:50:34 +0000623 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000624 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200625
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626 /* resume AC97 DAIs */
627 for (i = 0; i < card->num_rtd; i++) {
628 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100629
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100631 continue;
632
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
634 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200635 }
636
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200637 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000638 /* If the CODEC was idle over suspend then it will have been
639 * left with bias OFF or STANDBY and suspended so we must now
640 * resume. Otherwise the suspend was suppressed.
641 */
642 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200643 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644 case SND_SOC_BIAS_STANDBY:
645 case SND_SOC_BIAS_OFF:
646 codec->driver->resume(codec);
647 codec->suspended = 0;
648 break;
649 default:
650 dev_dbg(codec->dev, "CODEC was on over suspend\n");
651 break;
652 }
Mark Brown1547aba2010-05-07 21:11:40 +0100653 }
654 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200655
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656 for (i = 0; i < card->num_rtd; i++) {
657 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100658
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000659 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100660 continue;
661
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000662 if (driver->playback.stream_name != NULL)
663 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200664 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665
666 if (driver->capture.stream_name != NULL)
667 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200668 SND_SOC_DAPM_STREAM_RESUME);
669 }
670
Mark Brown3ff3f642008-05-19 12:32:25 +0200671 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000672 for (i = 0; i < card->num_rtd; i++) {
673 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
674 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100675
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000676 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100677 continue;
678
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000679 if (drv->ops->digital_mute && dai->playback_active)
680 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200681 }
682
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000683 for (i = 0; i < card->num_rtd; i++) {
684 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
685 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100686
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000687 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100688 continue;
689
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
691 cpu_dai->driver->resume(cpu_dai);
692 if (platform->driver->resume && platform->suspended) {
693 platform->driver->resume(cpu_dai);
694 platform->suspended = 0;
695 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200696 }
697
Mark Brown87506542008-11-18 20:50:34 +0000698 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000699 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000701 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100702
703 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000704 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100705}
706
707/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000708int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100709{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000710 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600711 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200712
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800713 /* If the initialization of this soc device failed, there is no codec
714 * associated with it. Just bail out in this case.
715 */
716 if (list_empty(&card->codec_dev_list))
717 return 0;
718
Mark Brown64ab9ba2009-03-31 11:27:03 +0100719 /* AC97 devices might have other drivers hanging off them so
720 * need to resume immediately. Other drivers don't have that
721 * problem and may take a substantial amount of time to resume
722 * due to I/O costs and anti-pop so handle them out of line.
723 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 for (i = 0; i < card->num_rtd; i++) {
725 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600726 ac97_control |= cpu_dai->driver->ac97_control;
727 }
728 if (ac97_control) {
729 dev_dbg(dev, "Resuming AC97 immediately\n");
730 soc_resume_deferred(&card->deferred_resume_work);
731 } else {
732 dev_dbg(dev, "Scheduling resume work\n");
733 if (!schedule_work(&card->deferred_resume_work))
734 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100735 }
Andy Green6ed25972008-06-13 16:24:05 +0100736
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200737 return 0;
738}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000739EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200740#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000741#define snd_soc_suspend NULL
742#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200743#endif
744
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100745static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800746};
747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200749{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
751 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000752 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000753 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100755 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200756
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000757 if (rtd->complete)
758 return 1;
759 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000760
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000761 /* do we already have the CPU DAI for this link ? */
762 if (rtd->cpu_dai) {
763 goto find_codec;
764 }
765 /* no, then find CPU DAI from registered DAIs*/
766 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warren2610ab72011-12-07 13:58:27 -0700767 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
768 continue;
769
770 rtd->cpu_dai = cpu_dai;
771 goto find_codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000772 }
773 dev_dbg(card->dev, "CPU DAI %s not registered\n",
774 dai_link->cpu_dai_name);
775
776find_codec:
777 /* do we already have the CODEC for this link ? */
778 if (rtd->codec) {
779 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +0000780 }
781
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782 /* no, then find CODEC from registered CODECs*/
783 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren2610ab72011-12-07 13:58:27 -0700784 if (strcmp(codec->name, dai_link->codec_name))
785 continue;
Mark Brown6b05eda2008-12-08 19:26:48 +0000786
Stephen Warren2610ab72011-12-07 13:58:27 -0700787 rtd->codec = codec;
788
789 /*
790 * CODEC found, so find CODEC DAI from registered DAIs from
791 * this CODEC
792 */
793 list_for_each_entry(codec_dai, &dai_list, list) {
794 if (codec->dev == codec_dai->dev &&
795 !strcmp(codec_dai->name,
796 dai_link->codec_dai_name)) {
797
798 rtd->codec_dai = codec_dai;
799 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000800 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000801 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700802 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
803 dai_link->codec_dai_name);
804
805 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000806 }
807 dev_dbg(card->dev, "CODEC %s not registered\n",
808 dai_link->codec_name);
809
810find_platform:
Mark Brown848dd8b2011-04-27 18:16:32 +0100811 /* do we need a platform? */
812 if (rtd->platform)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000813 goto out;
Mark Brown848dd8b2011-04-27 18:16:32 +0100814
815 /* if there's no platform we match on the empty platform */
816 platform_name = dai_link->platform_name;
817 if (!platform_name)
818 platform_name = "snd-soc-dummy";
819
820 /* no, then find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000821 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren2610ab72011-12-07 13:58:27 -0700822 if (strcmp(platform->name, platform_name))
823 continue;
824
825 rtd->platform = platform;
826 goto out;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000827 }
828
829 dev_dbg(card->dev, "platform %s not registered\n",
830 dai_link->platform_name);
831 return 0;
832
833out:
834 /* mark rtd as complete if we found all 4 of our client devices */
835 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
836 rtd->complete = 1;
837 card->num_rtd++;
838 }
839 return 1;
840}
841
Jarkko Nikula589c3562010-12-06 16:27:07 +0200842static void soc_remove_codec(struct snd_soc_codec *codec)
843{
844 int err;
845
846 if (codec->driver->remove) {
847 err = codec->driver->remove(codec);
848 if (err < 0)
849 dev_err(codec->dev,
850 "asoc: failed to remove %s: %d\n",
851 codec->name, err);
852 }
853
854 /* Make sure all DAPM widgets are freed */
855 snd_soc_dapm_free(&codec->dapm);
856
857 soc_cleanup_codec_debugfs(codec);
858 codec->probed = 0;
859 list_del(&codec->card_list);
860 module_put(codec->dev->driver->owner);
861}
862
Liam Girdwood0168bf02011-06-07 16:08:05 +0100863static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000864{
865 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
866 struct snd_soc_codec *codec = rtd->codec;
867 struct snd_soc_platform *platform = rtd->platform;
868 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
869 int err;
870
871 /* unregister the rtd device */
872 if (rtd->dev_registered) {
873 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200874 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000875 device_unregister(&rtd->dev);
876 rtd->dev_registered = 0;
877 }
878
879 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100880 if (codec_dai && codec_dai->probed &&
881 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000882 if (codec_dai->driver->remove) {
883 err = codec_dai->driver->remove(codec_dai);
884 if (err < 0)
885 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
886 }
887 codec_dai->probed = 0;
888 list_del(&codec_dai->card_list);
889 }
890
891 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100892 if (platform && platform->probed &&
893 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000894 if (platform->driver->remove) {
895 err = platform->driver->remove(platform);
896 if (err < 0)
897 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
898 }
899 platform->probed = 0;
900 list_del(&platform->card_list);
901 module_put(platform->dev->driver->owner);
902 }
903
904 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100905 if (codec && codec->probed &&
906 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200907 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000908
909 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100910 if (cpu_dai && cpu_dai->probed &&
911 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000912 if (cpu_dai->driver->remove) {
913 err = cpu_dai->driver->remove(cpu_dai);
914 if (err < 0)
915 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
916 }
917 cpu_dai->probed = 0;
918 list_del(&cpu_dai->card_list);
919 module_put(cpu_dai->dev->driver->owner);
920 }
921}
922
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900923static void soc_remove_dai_links(struct snd_soc_card *card)
924{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100925 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900926
Liam Girdwood0168bf02011-06-07 16:08:05 +0100927 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
928 order++) {
929 for (dai = 0; dai < card->num_rtd; dai++)
930 soc_remove_dai_link(card, dai, order);
931 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900932 card->num_rtd = 0;
933}
934
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200935static void soc_set_name_prefix(struct snd_soc_card *card,
936 struct snd_soc_codec *codec)
937{
938 int i;
939
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000940 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200941 return;
942
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000943 for (i = 0; i < card->num_configs; i++) {
944 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200945 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
946 codec->name_prefix = map->name_prefix;
947 break;
948 }
949 }
950}
951
Jarkko Nikula589c3562010-12-06 16:27:07 +0200952static int soc_probe_codec(struct snd_soc_card *card,
953 struct snd_soc_codec *codec)
954{
955 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +0000956 const struct snd_soc_codec_driver *driver = codec->driver;
Jarkko Nikula589c3562010-12-06 16:27:07 +0200957
958 codec->card = card;
959 codec->dapm.card = card;
960 soc_set_name_prefix(card, codec);
961
Jarkko Nikula70d29332011-01-27 16:24:22 +0200962 if (!try_module_get(codec->dev->driver->owner))
963 return -ENODEV;
964
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +0200965 soc_init_codec_debugfs(codec);
966
Lars-Peter Clausen77530152011-05-05 16:59:09 +0200967 if (driver->dapm_widgets)
968 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
969 driver->num_dapm_widgets);
970
Mark Brown33c5f962011-08-22 18:40:30 +0100971 codec->dapm.idle_bias_off = driver->idle_bias_off;
972
Mark Brown89b95ac02011-03-07 16:38:44 +0000973 if (driver->probe) {
974 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200975 if (ret < 0) {
976 dev_err(codec->dev,
977 "asoc: failed to probe CODEC %s: %d\n",
978 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +0200979 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +0200980 }
981 }
982
Mark Brownb7af1da2011-04-07 19:18:44 +0900983 if (driver->controls)
984 snd_soc_add_controls(codec, driver->controls,
985 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +0000986 if (driver->dapm_routes)
987 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
988 driver->num_dapm_routes);
989
Jarkko Nikula589c3562010-12-06 16:27:07 +0200990 /* mark codec as probed and add to card codec list */
991 codec->probed = 1;
992 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200993 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200994
Jarkko Nikula70d29332011-01-27 16:24:22 +0200995 return 0;
996
997err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +0200998 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +0200999 module_put(codec->dev->driver->owner);
1000
Jarkko Nikula589c3562010-12-06 16:27:07 +02001001 return ret;
1002}
1003
Liam Girdwood956245e2011-07-01 16:54:08 +01001004static int soc_probe_platform(struct snd_soc_card *card,
1005 struct snd_soc_platform *platform)
1006{
1007 int ret = 0;
1008 const struct snd_soc_platform_driver *driver = platform->driver;
1009
1010 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001011 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001012
1013 if (!try_module_get(platform->dev->driver->owner))
1014 return -ENODEV;
1015
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001016 if (driver->dapm_widgets)
1017 snd_soc_dapm_new_controls(&platform->dapm,
1018 driver->dapm_widgets, driver->num_dapm_widgets);
1019
Liam Girdwood956245e2011-07-01 16:54:08 +01001020 if (driver->probe) {
1021 ret = driver->probe(platform);
1022 if (ret < 0) {
1023 dev_err(platform->dev,
1024 "asoc: failed to probe platform %s: %d\n",
1025 platform->name, ret);
1026 goto err_probe;
1027 }
1028 }
1029
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001030 if (driver->controls)
1031 snd_soc_add_platform_controls(platform, driver->controls,
1032 driver->num_controls);
1033 if (driver->dapm_routes)
1034 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1035 driver->num_dapm_routes);
1036
Liam Girdwood956245e2011-07-01 16:54:08 +01001037 /* mark platform as probed and add to card platform list */
1038 platform->probed = 1;
1039 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001040 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001041
1042 return 0;
1043
1044err_probe:
1045 module_put(platform->dev->driver->owner);
1046
1047 return ret;
1048}
1049
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001050static void rtd_release(struct device *dev) {}
1051
Jarkko Nikula589c3562010-12-06 16:27:07 +02001052static int soc_post_component_init(struct snd_soc_card *card,
1053 struct snd_soc_codec *codec,
1054 int num, int dailess)
1055{
1056 struct snd_soc_dai_link *dai_link = NULL;
1057 struct snd_soc_aux_dev *aux_dev = NULL;
1058 struct snd_soc_pcm_runtime *rtd;
1059 const char *temp, *name;
1060 int ret = 0;
1061
1062 if (!dailess) {
1063 dai_link = &card->dai_link[num];
1064 rtd = &card->rtd[num];
1065 name = dai_link->name;
1066 } else {
1067 aux_dev = &card->aux_dev[num];
1068 rtd = &card->rtd_aux[num];
1069 name = aux_dev->name;
1070 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001071 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001072
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001073 /* Make sure all DAPM widgets are instantiated */
1074 snd_soc_dapm_new_widgets(&codec->dapm);
1075
Jarkko Nikula589c3562010-12-06 16:27:07 +02001076 /* machine controls, routes and widgets are not prefixed */
1077 temp = codec->name_prefix;
1078 codec->name_prefix = NULL;
1079
1080 /* do machine specific initialization */
1081 if (!dailess && dai_link->init)
1082 ret = dai_link->init(rtd);
1083 else if (dailess && aux_dev->init)
1084 ret = aux_dev->init(&codec->dapm);
1085 if (ret < 0) {
1086 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1087 return ret;
1088 }
1089 codec->name_prefix = temp;
1090
Jarkko Nikula589c3562010-12-06 16:27:07 +02001091 /* register the rtd device */
1092 rtd->codec = codec;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001093 rtd->dev.parent = card->dev;
1094 rtd->dev.release = rtd_release;
1095 rtd->dev.init_name = name;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001096 mutex_init(&rtd->pcm_mutex);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001097 ret = device_register(&rtd->dev);
1098 if (ret < 0) {
1099 dev_err(card->dev,
1100 "asoc: failed to register runtime device: %d\n", ret);
1101 return ret;
1102 }
1103 rtd->dev_registered = 1;
1104
1105 /* add DAPM sysfs entries for this codec */
1106 ret = snd_soc_dapm_sys_add(&rtd->dev);
1107 if (ret < 0)
1108 dev_err(codec->dev,
1109 "asoc: failed to add codec dapm sysfs entries: %d\n",
1110 ret);
1111
1112 /* add codec sysfs entries */
1113 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1114 if (ret < 0)
1115 dev_err(codec->dev,
1116 "asoc: failed to add codec sysfs files: %d\n", ret);
1117
1118 return 0;
1119}
1120
Liam Girdwood0168bf02011-06-07 16:08:05 +01001121static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001122{
1123 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1124 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1125 struct snd_soc_codec *codec = rtd->codec;
1126 struct snd_soc_platform *platform = rtd->platform;
1127 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1128 int ret;
1129
Liam Girdwood0168bf02011-06-07 16:08:05 +01001130 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1131 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001132
1133 /* config components */
1134 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001135 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001136 codec_dai->card = card;
1137 cpu_dai->card = card;
1138
1139 /* set default power off timeout */
1140 rtd->pmdown_time = pmdown_time;
1141
1142 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001143 if (!cpu_dai->probed &&
1144 cpu_dai->driver->probe_order == order) {
Liam Girdwood61b61e32011-05-24 13:57:43 +01001145 if (!try_module_get(cpu_dai->dev->driver->owner))
1146 return -ENODEV;
1147
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001148 if (cpu_dai->driver->probe) {
1149 ret = cpu_dai->driver->probe(cpu_dai);
1150 if (ret < 0) {
1151 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1152 cpu_dai->name);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001153 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001154 return ret;
1155 }
1156 }
1157 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001158 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001159 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1160 }
1161
1162 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001163 if (!codec->probed &&
1164 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001165 ret = soc_probe_codec(card, codec);
1166 if (ret < 0)
1167 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001168 }
1169
1170 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001171 if (!platform->probed &&
1172 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001173 ret = soc_probe_platform(card, platform);
1174 if (ret < 0)
1175 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001176 }
1177
1178 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001179 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001180 if (codec_dai->driver->probe) {
1181 ret = codec_dai->driver->probe(codec_dai);
1182 if (ret < 0) {
1183 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1184 codec_dai->name);
1185 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001186 }
1187 }
1188
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001189 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001190 codec_dai->probed = 1;
1191 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001192 }
1193
Liam Girdwood0168bf02011-06-07 16:08:05 +01001194 /* complete DAI probe during last probe */
1195 if (order != SND_SOC_COMP_ORDER_LAST)
1196 return 0;
1197
Jarkko Nikula589c3562010-12-06 16:27:07 +02001198 ret = soc_post_component_init(card, codec, num, 0);
1199 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001200 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001201
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001202 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1203 if (ret < 0)
1204 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1205
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001206 /* create the pcm */
1207 ret = soc_new_pcm(rtd, num);
1208 if (ret < 0) {
1209 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1210 return ret;
1211 }
1212
1213 /* add platform data for AC97 devices */
1214 if (rtd->codec_dai->driver->ac97_control)
1215 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1216
1217 return 0;
1218}
1219
1220#ifdef CONFIG_SND_SOC_AC97_BUS
1221static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1222{
1223 int ret;
1224
1225 /* Only instantiate AC97 if not already done by the adaptor
1226 * for the generic AC97 subsystem.
1227 */
1228 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001229 /*
1230 * It is possible that the AC97 device is already registered to
1231 * the device subsystem. This happens when the device is created
1232 * via snd_ac97_mixer(). Currently only SoC codec that does so
1233 * is the generic AC97 glue but others migh emerge.
1234 *
1235 * In those cases we don't try to register the device again.
1236 */
1237 if (!rtd->codec->ac97_created)
1238 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001239
1240 ret = soc_ac97_dev_register(rtd->codec);
1241 if (ret < 0) {
1242 printk(KERN_ERR "asoc: AC97 device register failed\n");
1243 return ret;
1244 }
1245
1246 rtd->codec->ac97_registered = 1;
1247 }
1248 return 0;
1249}
1250
1251static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1252{
1253 if (codec->ac97_registered) {
1254 soc_ac97_dev_unregister(codec);
1255 codec->ac97_registered = 0;
1256 }
1257}
1258#endif
1259
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001260static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1261{
1262 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001263 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001264 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001265
1266 /* find CODEC from registered CODECs*/
1267 list_for_each_entry(codec, &codec_list, list) {
1268 if (!strcmp(codec->name, aux_dev->codec_name)) {
1269 if (codec->probed) {
1270 dev_err(codec->dev,
1271 "asoc: codec already probed");
1272 ret = -EBUSY;
1273 goto out;
1274 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001275 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001276 }
1277 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001278 /* codec not found */
1279 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1280 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001281
Jarkko Nikula676ad982010-12-03 09:18:22 +02001282found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001283 ret = soc_probe_codec(card, codec);
1284 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001285 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001286
Jarkko Nikula589c3562010-12-06 16:27:07 +02001287 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001288
1289out:
1290 return ret;
1291}
1292
1293static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1294{
1295 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1296 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001297
1298 /* unregister the rtd device */
1299 if (rtd->dev_registered) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001300 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001301 device_unregister(&rtd->dev);
1302 rtd->dev_registered = 0;
1303 }
1304
Jarkko Nikula589c3562010-12-06 16:27:07 +02001305 if (codec && codec->probed)
1306 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001307}
1308
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001309static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1310 enum snd_soc_compress_type compress_type)
1311{
1312 int ret;
1313
1314 if (codec->cache_init)
1315 return 0;
1316
1317 /* override the compress_type if necessary */
1318 if (compress_type && codec->compress_type != compress_type)
1319 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001320 ret = snd_soc_cache_init(codec);
1321 if (ret < 0) {
1322 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1323 ret);
1324 return ret;
1325 }
1326 codec->cache_init = 1;
1327 return 0;
1328}
1329
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001330static void snd_soc_instantiate_card(struct snd_soc_card *card)
1331{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001332 struct snd_soc_codec *codec;
1333 struct snd_soc_codec_conf *codec_conf;
1334 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001335 struct snd_soc_dai_link *dai_link;
Liam Girdwood0168bf02011-06-07 16:08:05 +01001336 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001337
1338 mutex_lock(&card->mutex);
1339
1340 if (card->instantiated) {
1341 mutex_unlock(&card->mutex);
1342 return;
1343 }
1344
1345 /* bind DAIs */
1346 for (i = 0; i < card->num_links; i++)
1347 soc_bind_dai_link(card, i);
1348
1349 /* bind completed ? */
1350 if (card->num_rtd != card->num_links) {
1351 mutex_unlock(&card->mutex);
1352 return;
1353 }
1354
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001355 /* initialize the register cache for each available codec */
1356 list_for_each_entry(codec, &codec_list, list) {
1357 if (codec->cache_init)
1358 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001359 /* by default we don't override the compress_type */
1360 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001361 /* check to see if we need to override the compress_type */
1362 for (i = 0; i < card->num_configs; ++i) {
1363 codec_conf = &card->codec_conf[i];
1364 if (!strcmp(codec->name, codec_conf->dev_name)) {
1365 compress_type = codec_conf->compress_type;
1366 if (compress_type && compress_type
1367 != codec->compress_type)
1368 break;
1369 }
1370 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001371 ret = snd_soc_init_codec_cache(codec, compress_type);
1372 if (ret < 0) {
1373 mutex_unlock(&card->mutex);
1374 return;
1375 }
1376 }
1377
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001378 /* card bind complete so register a sound card */
1379 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1380 card->owner, 0, &card->snd_card);
1381 if (ret < 0) {
1382 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1383 card->name);
1384 mutex_unlock(&card->mutex);
1385 return;
1386 }
1387 card->snd_card->dev = card->dev;
1388
Mark Browne37a4972011-03-02 18:21:57 +00001389 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1390 card->dapm.dev = card->dev;
1391 card->dapm.card = card;
1392 list_add(&card->dapm.list, &card->dapm_list);
1393
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001394#ifdef CONFIG_DEBUG_FS
1395 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1396#endif
1397
Mark Brown88ee1c62011-02-02 10:43:26 +00001398#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001399 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001400 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001401#endif
Andy Green6ed25972008-06-13 16:24:05 +01001402
Mark Brown9a841eb2011-04-12 17:51:37 -07001403 if (card->dapm_widgets)
1404 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1405 card->num_dapm_widgets);
1406
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001407 /* initialise the sound card only once */
1408 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001409 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001410 if (ret < 0)
1411 goto card_probe_error;
1412 }
1413
Liam Girdwood0168bf02011-06-07 16:08:05 +01001414 /* early DAI link probe */
1415 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1416 order++) {
1417 for (i = 0; i < card->num_links; i++) {
1418 ret = soc_probe_dai_link(card, i, order);
1419 if (ret < 0) {
1420 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001421 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001422 goto probe_dai_err;
1423 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001424 }
1425 }
1426
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001427 for (i = 0; i < card->num_aux_devs; i++) {
1428 ret = soc_probe_aux_dev(card, i);
1429 if (ret < 0) {
1430 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1431 card->name, ret);
1432 goto probe_aux_dev_err;
1433 }
1434 }
1435
Mark Brownb7af1da2011-04-07 19:18:44 +09001436 /* We should have a non-codec control add function but we don't */
1437 if (card->controls)
1438 snd_soc_add_controls(list_first_entry(&card->codec_dev_list,
1439 struct snd_soc_codec,
1440 card_list),
1441 card->controls,
1442 card->num_controls);
1443
Mark Brownb8ad29d2011-03-02 18:35:51 +00001444 if (card->dapm_routes)
1445 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1446 card->num_dapm_routes);
1447
Mark Brownb90d2f92011-10-10 13:38:06 +01001448 snd_soc_dapm_new_widgets(&card->dapm);
1449
Mark Brown75d9ac42011-09-27 16:41:01 +01001450 for (i = 0; i < card->num_links; i++) {
1451 dai_link = &card->dai_link[i];
1452
1453 if (dai_link->dai_fmt) {
1454 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1455 dai_link->dai_fmt);
1456 if (ret != 0)
1457 dev_warn(card->rtd[i].codec_dai->dev,
1458 "Failed to set DAI format: %d\n",
1459 ret);
1460
1461 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1462 dai_link->dai_fmt);
1463 if (ret != 0)
1464 dev_warn(card->rtd[i].cpu_dai->dev,
1465 "Failed to set DAI format: %d\n",
1466 ret);
1467 }
1468 }
1469
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001470 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001471 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001472 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1473 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001474 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1475 "%s", card->driver_name ? card->driver_name : card->name);
1476 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1477 switch (card->snd_card->driver[i]) {
1478 case '_':
1479 case '-':
1480 case '\0':
1481 break;
1482 default:
1483 if (!isalnum(card->snd_card->driver[i]))
1484 card->snd_card->driver[i] = '_';
1485 break;
1486 }
1487 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001488
Mark Brown28e9ad92011-03-02 18:36:34 +00001489 if (card->late_probe) {
1490 ret = card->late_probe(card);
1491 if (ret < 0) {
1492 dev_err(card->dev, "%s late_probe() failed: %d\n",
1493 card->name, ret);
1494 goto probe_aux_dev_err;
1495 }
1496 }
1497
Mark Brown2dc00212011-10-08 13:59:44 +01001498 snd_soc_dapm_new_widgets(&card->dapm);
1499
Stephen Warren16332812011-11-23 12:42:04 -07001500 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001501 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001502 snd_soc_dapm_auto_nc_codec_pins(codec);
1503
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001504 ret = snd_card_register(card->snd_card);
1505 if (ret < 0) {
1506 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
Axel Lin6b3ed782010-12-07 16:12:29 +08001507 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001508 }
1509
1510#ifdef CONFIG_SND_SOC_AC97_BUS
1511 /* register any AC97 codecs */
1512 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001513 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1514 if (ret < 0) {
1515 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1516 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001517 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001518 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001519 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001520 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001521#endif
1522
Mark Brown435c5e22008-12-04 15:32:53 +00001523 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001524 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001525 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001526 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001527
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001528probe_aux_dev_err:
1529 for (i = 0; i < card->num_aux_devs; i++)
1530 soc_remove_aux_dev(card, i);
1531
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001532probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001533 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001534
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001535card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001536 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001537 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001538
1539 snd_card_free(card->snd_card);
1540
1541 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001542}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001543
Mark Brown435c5e22008-12-04 15:32:53 +00001544/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001545 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001546 * client_mutex.
1547 */
1548static void snd_soc_instantiate_cards(void)
1549{
1550 struct snd_soc_card *card;
1551 list_for_each_entry(card, &card_list, list)
1552 snd_soc_instantiate_card(card);
1553}
1554
1555/* probes a new socdev */
1556static int soc_probe(struct platform_device *pdev)
1557{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001558 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001559 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001560
Vinod Koul70a7ca32011-01-14 19:22:48 +05301561 /*
1562 * no card, so machine driver should be registering card
1563 * we should not be here in that case so ret error
1564 */
1565 if (!card)
1566 return -EINVAL;
1567
Mark Brown435c5e22008-12-04 15:32:53 +00001568 /* Bodge while we unpick instantiation */
1569 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001570
Mark Brown435c5e22008-12-04 15:32:53 +00001571 ret = snd_soc_register_card(card);
1572 if (ret != 0) {
1573 dev_err(&pdev->dev, "Failed to register card\n");
1574 return ret;
1575 }
1576
1577 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001578}
1579
Vinod Koulb0e26482011-01-13 22:48:02 +05301580static int soc_cleanup_card_resources(struct snd_soc_card *card)
1581{
Vinod Koulb0e26482011-01-13 22:48:02 +05301582 int i;
1583
1584 /* make sure any delayed work runs */
1585 for (i = 0; i < card->num_rtd; i++) {
1586 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1587 flush_delayed_work_sync(&rtd->delayed_work);
1588 }
1589
1590 /* remove auxiliary devices */
1591 for (i = 0; i < card->num_aux_devs; i++)
1592 soc_remove_aux_dev(card, i);
1593
1594 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001595 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301596
1597 soc_cleanup_card_debugfs(card);
1598
1599 /* remove the card */
1600 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001601 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301602
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001603 snd_soc_dapm_free(&card->dapm);
1604
Vinod Koulb0e26482011-01-13 22:48:02 +05301605 kfree(card->rtd);
1606 snd_card_free(card->snd_card);
1607 return 0;
1608
1609}
1610
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001611/* removes a socdev */
1612static int soc_remove(struct platform_device *pdev)
1613{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001614 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001615
Mark Brownc5af3a22008-11-28 13:29:45 +00001616 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001617 return 0;
1618}
1619
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001620int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001621{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001622 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001623 int i;
Mark Brown51737472009-06-22 13:16:51 +01001624
1625 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001626 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001627
1628 /* Flush out pmdown_time work - we actually do want to run it
1629 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001630 for (i = 0; i < card->num_rtd; i++) {
1631 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001632 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001633 }
Mark Brown51737472009-06-22 13:16:51 +01001634
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001635 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001636
1637 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001638}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001639EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001640
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001641const struct dev_pm_ops snd_soc_pm_ops = {
1642 .suspend = snd_soc_suspend,
1643 .resume = snd_soc_resume,
1644 .poweroff = snd_soc_poweroff,
Mark Brown416356f2009-06-30 19:05:15 +01001645};
Stephen Warrendeb26072011-04-05 19:35:30 -06001646EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001647
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001648/* ASoC platform driver */
1649static struct platform_driver soc_driver = {
1650 .driver = {
1651 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001652 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001653 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001654 },
1655 .probe = soc_probe,
1656 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001657};
1658
Mark Brown096e49d2009-07-05 15:12:22 +01001659/**
1660 * snd_soc_codec_volatile_register: Report if a register is volatile.
1661 *
1662 * @codec: CODEC to query.
1663 * @reg: Register to query.
1664 *
1665 * Boolean function indiciating if a CODEC register is volatile.
1666 */
Mark Brown181e0552011-01-24 14:05:25 +00001667int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1668 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001669{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001670 if (codec->volatile_register)
1671 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001672 else
1673 return 0;
1674}
1675EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1676
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001677/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001678 * snd_soc_codec_readable_register: Report if a register is readable.
1679 *
1680 * @codec: CODEC to query.
1681 * @reg: Register to query.
1682 *
1683 * Boolean function indicating if a CODEC register is readable.
1684 */
1685int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1686 unsigned int reg)
1687{
1688 if (codec->readable_register)
1689 return codec->readable_register(codec, reg);
1690 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001691 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001692}
1693EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1694
1695/**
1696 * snd_soc_codec_writable_register: Report if a register is writable.
1697 *
1698 * @codec: CODEC to query.
1699 * @reg: Register to query.
1700 *
1701 * Boolean function indicating if a CODEC register is writable.
1702 */
1703int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1704 unsigned int reg)
1705{
1706 if (codec->writable_register)
1707 return codec->writable_register(codec, reg);
1708 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001709 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001710}
1711EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1712
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001713int snd_soc_platform_read(struct snd_soc_platform *platform,
1714 unsigned int reg)
1715{
1716 unsigned int ret;
1717
1718 if (!platform->driver->read) {
1719 dev_err(platform->dev, "platform has no read back\n");
1720 return -1;
1721 }
1722
1723 ret = platform->driver->read(platform, reg);
1724 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001725 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001726
1727 return ret;
1728}
1729EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1730
1731int snd_soc_platform_write(struct snd_soc_platform *platform,
1732 unsigned int reg, unsigned int val)
1733{
1734 if (!platform->driver->write) {
1735 dev_err(platform->dev, "platform has no write back\n");
1736 return -1;
1737 }
1738
1739 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001740 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001741 return platform->driver->write(platform, reg, val);
1742}
1743EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1744
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001745/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001746 * snd_soc_new_ac97_codec - initailise AC97 device
1747 * @codec: audio codec
1748 * @ops: AC97 bus operations
1749 * @num: AC97 codec number
1750 *
1751 * Initialises AC97 codec resources for use by ad-hoc devices only.
1752 */
1753int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1754 struct snd_ac97_bus_ops *ops, int num)
1755{
1756 mutex_lock(&codec->mutex);
1757
1758 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1759 if (codec->ac97 == NULL) {
1760 mutex_unlock(&codec->mutex);
1761 return -ENOMEM;
1762 }
1763
1764 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1765 if (codec->ac97->bus == NULL) {
1766 kfree(codec->ac97);
1767 codec->ac97 = NULL;
1768 mutex_unlock(&codec->mutex);
1769 return -ENOMEM;
1770 }
1771
1772 codec->ac97->bus->ops = ops;
1773 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001774
1775 /*
1776 * Mark the AC97 device to be created by us. This way we ensure that the
1777 * device will be registered with the device subsystem later on.
1778 */
1779 codec->ac97_created = 1;
1780
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001781 mutex_unlock(&codec->mutex);
1782 return 0;
1783}
1784EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1785
1786/**
1787 * snd_soc_free_ac97_codec - free AC97 codec device
1788 * @codec: audio codec
1789 *
1790 * Frees AC97 codec device resources.
1791 */
1792void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1793{
1794 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001795#ifdef CONFIG_SND_SOC_AC97_BUS
1796 soc_unregister_ac97_dai_link(codec);
1797#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001798 kfree(codec->ac97->bus);
1799 kfree(codec->ac97);
1800 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001801 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001802 mutex_unlock(&codec->mutex);
1803}
1804EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1805
Mark Brownc3753702010-11-01 15:41:57 -04001806unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1807{
1808 unsigned int ret;
1809
Mark Brownc3acec22010-12-02 16:15:29 +00001810 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04001811 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04001812 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04001813
1814 return ret;
1815}
1816EXPORT_SYMBOL_GPL(snd_soc_read);
1817
1818unsigned int snd_soc_write(struct snd_soc_codec *codec,
1819 unsigned int reg, unsigned int val)
1820{
1821 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04001822 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00001823 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04001824}
1825EXPORT_SYMBOL_GPL(snd_soc_write);
1826
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00001827unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1828 unsigned int reg, const void *data, size_t len)
1829{
1830 return codec->bulk_write_raw(codec, reg, data, len);
1831}
1832EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1833
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001834/**
1835 * snd_soc_update_bits - update codec register bits
1836 * @codec: audio codec
1837 * @reg: codec register
1838 * @mask: register mask
1839 * @value: new value
1840 *
1841 * Writes new register value.
1842 *
Timur Tabi180c3292011-01-10 15:58:13 -06001843 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001844 */
1845int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001846 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001847{
1848 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001849 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06001850 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001851
Timur Tabi180c3292011-01-10 15:58:13 -06001852 ret = snd_soc_read(codec, reg);
1853 if (ret < 0)
1854 return ret;
1855
1856 old = ret;
Mark Brown78bf3c92011-06-03 17:09:14 +01001857 new = (old & ~mask) | (value & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001858 change = old != new;
Timur Tabi180c3292011-01-10 15:58:13 -06001859 if (change) {
1860 ret = snd_soc_write(codec, reg, new);
1861 if (ret < 0)
1862 return ret;
1863 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001864
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001865 return change;
1866}
1867EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1868
1869/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001870 * snd_soc_update_bits_locked - update codec register bits
1871 * @codec: audio codec
1872 * @reg: codec register
1873 * @mask: register mask
1874 * @value: new value
1875 *
1876 * Writes new register value, and takes the codec mutex.
1877 *
1878 * Returns 1 for change else 0.
1879 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001880int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1881 unsigned short reg, unsigned int mask,
1882 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001883{
1884 int change;
1885
1886 mutex_lock(&codec->mutex);
1887 change = snd_soc_update_bits(codec, reg, mask, value);
1888 mutex_unlock(&codec->mutex);
1889
1890 return change;
1891}
Mark Browndd1b3d52009-12-04 14:22:03 +00001892EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001893
1894/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001895 * snd_soc_test_bits - test register for change
1896 * @codec: audio codec
1897 * @reg: codec register
1898 * @mask: register mask
1899 * @value: new value
1900 *
1901 * Tests a register with a new value and checks if the new value is
1902 * different from the old value.
1903 *
1904 * Returns 1 for change else 0.
1905 */
1906int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001907 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001908{
1909 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001910 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001911
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001912 old = snd_soc_read(codec, reg);
1913 new = (old & ~mask) | value;
1914 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001915
1916 return change;
1917}
1918EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1919
1920/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001921 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1922 * @substream: the pcm substream
1923 * @hw: the hardware parameters
1924 *
1925 * Sets the substream runtime hardware parameters.
1926 */
1927int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1928 const struct snd_pcm_hardware *hw)
1929{
1930 struct snd_pcm_runtime *runtime = substream->runtime;
1931 runtime->hw.info = hw->info;
1932 runtime->hw.formats = hw->formats;
1933 runtime->hw.period_bytes_min = hw->period_bytes_min;
1934 runtime->hw.period_bytes_max = hw->period_bytes_max;
1935 runtime->hw.periods_min = hw->periods_min;
1936 runtime->hw.periods_max = hw->periods_max;
1937 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1938 runtime->hw.fifo_size = hw->fifo_size;
1939 return 0;
1940}
1941EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1942
1943/**
1944 * snd_soc_cnew - create new control
1945 * @_template: control template
1946 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001947 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00001948 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001949 *
1950 * Create a new mixer control from a template control.
1951 *
1952 * Returns 0 for success, else error.
1953 */
1954struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brownefb7ac32011-03-08 17:23:24 +00001955 void *data, char *long_name,
1956 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001957{
1958 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00001959 struct snd_kcontrol *kcontrol;
1960 char *name = NULL;
1961 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001962
1963 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001964 template.index = 0;
1965
Mark Brownefb7ac32011-03-08 17:23:24 +00001966 if (!long_name)
1967 long_name = template.name;
1968
1969 if (prefix) {
1970 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08001971 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00001972 if (!name)
1973 return NULL;
1974
1975 snprintf(name, name_len, "%s %s", prefix, long_name);
1976
1977 template.name = name;
1978 } else {
1979 template.name = long_name;
1980 }
1981
1982 kcontrol = snd_ctl_new1(&template, data);
1983
1984 kfree(name);
1985
1986 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001987}
1988EXPORT_SYMBOL_GPL(snd_soc_cnew);
1989
1990/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001991 * snd_soc_add_controls - add an array of controls to a codec.
1992 * Convienience function to add a list of controls. Many codecs were
1993 * duplicating this code.
1994 *
1995 * @codec: codec to add controls to
1996 * @controls: array of controls to add
1997 * @num_controls: number of elements in the array
1998 *
1999 * Return 0 for success, else error.
2000 */
2001int snd_soc_add_controls(struct snd_soc_codec *codec,
2002 const struct snd_kcontrol_new *controls, int num_controls)
2003{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002004 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002005 int err, i;
2006
2007 for (i = 0; i < num_controls; i++) {
2008 const struct snd_kcontrol_new *control = &controls[i];
Mark Brownefb7ac32011-03-08 17:23:24 +00002009 err = snd_ctl_add(card, snd_soc_cnew(control, codec,
2010 control->name,
2011 codec->name_prefix));
Ian Molton3e8e1952009-01-09 00:23:21 +00002012 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002013 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
Mark Brownefb7ac32011-03-08 17:23:24 +00002014 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002015 return err;
2016 }
2017 }
2018
2019 return 0;
2020}
2021EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2022
2023/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002024 * snd_soc_add_platform_controls - add an array of controls to a platform.
2025 * Convienience function to add a list of controls.
2026 *
2027 * @platform: platform to add controls to
2028 * @controls: array of controls to add
2029 * @num_controls: number of elements in the array
2030 *
2031 * Return 0 for success, else error.
2032 */
2033int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2034 const struct snd_kcontrol_new *controls, int num_controls)
2035{
2036 struct snd_card *card = platform->card->snd_card;
2037 int err, i;
2038
2039 for (i = 0; i < num_controls; i++) {
2040 const struct snd_kcontrol_new *control = &controls[i];
2041 err = snd_ctl_add(card, snd_soc_cnew(control, platform,
2042 control->name, NULL));
2043 if (err < 0) {
2044 dev_err(platform->dev, "Failed to add %s %d\n",control->name, err);
2045 return err;
2046 }
2047 }
2048
2049 return 0;
2050}
2051EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2052
2053/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002054 * snd_soc_info_enum_double - enumerated double mixer info callback
2055 * @kcontrol: mixer control
2056 * @uinfo: control element information
2057 *
2058 * Callback to provide information about a double enumerated
2059 * mixer control.
2060 *
2061 * Returns 0 for success.
2062 */
2063int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_info *uinfo)
2065{
2066 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2067
2068 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2069 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002070 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002071
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002072 if (uinfo->value.enumerated.item > e->max - 1)
2073 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002074 strcpy(uinfo->value.enumerated.name,
2075 e->texts[uinfo->value.enumerated.item]);
2076 return 0;
2077}
2078EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2079
2080/**
2081 * snd_soc_get_enum_double - enumerated double mixer get callback
2082 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002083 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002084 *
2085 * Callback to get the value of a double enumerated mixer.
2086 *
2087 * Returns 0 for success.
2088 */
2089int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2090 struct snd_ctl_elem_value *ucontrol)
2091{
2092 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2093 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002094 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002095
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002096 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002097 ;
2098 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002099 ucontrol->value.enumerated.item[0]
2100 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002101 if (e->shift_l != e->shift_r)
2102 ucontrol->value.enumerated.item[1] =
2103 (val >> e->shift_r) & (bitmask - 1);
2104
2105 return 0;
2106}
2107EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2108
2109/**
2110 * snd_soc_put_enum_double - enumerated double mixer put callback
2111 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002112 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002113 *
2114 * Callback to set the value of a double enumerated mixer.
2115 *
2116 * Returns 0 for success.
2117 */
2118int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2119 struct snd_ctl_elem_value *ucontrol)
2120{
2121 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2122 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002123 unsigned int val;
2124 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002125
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002126 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002127 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002128 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002129 return -EINVAL;
2130 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2131 mask = (bitmask - 1) << e->shift_l;
2132 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002133 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002134 return -EINVAL;
2135 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2136 mask |= (bitmask - 1) << e->shift_r;
2137 }
2138
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002139 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002140}
2141EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2142
2143/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002144 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2145 * @kcontrol: mixer control
2146 * @ucontrol: control element information
2147 *
2148 * Callback to get the value of a double semi enumerated mixer.
2149 *
2150 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2151 * used for handling bitfield coded enumeration for example.
2152 *
2153 * Returns 0 for success.
2154 */
2155int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2156 struct snd_ctl_elem_value *ucontrol)
2157{
2158 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002159 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002160 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002161
2162 reg_val = snd_soc_read(codec, e->reg);
2163 val = (reg_val >> e->shift_l) & e->mask;
2164 for (mux = 0; mux < e->max; mux++) {
2165 if (val == e->values[mux])
2166 break;
2167 }
2168 ucontrol->value.enumerated.item[0] = mux;
2169 if (e->shift_l != e->shift_r) {
2170 val = (reg_val >> e->shift_r) & e->mask;
2171 for (mux = 0; mux < e->max; mux++) {
2172 if (val == e->values[mux])
2173 break;
2174 }
2175 ucontrol->value.enumerated.item[1] = mux;
2176 }
2177
2178 return 0;
2179}
2180EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2181
2182/**
2183 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2184 * @kcontrol: mixer control
2185 * @ucontrol: control element information
2186 *
2187 * Callback to set the value of a double semi enumerated mixer.
2188 *
2189 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2190 * used for handling bitfield coded enumeration for example.
2191 *
2192 * Returns 0 for success.
2193 */
2194int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2195 struct snd_ctl_elem_value *ucontrol)
2196{
2197 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002198 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002199 unsigned int val;
2200 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002201
2202 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2203 return -EINVAL;
2204 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2205 mask = e->mask << e->shift_l;
2206 if (e->shift_l != e->shift_r) {
2207 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2208 return -EINVAL;
2209 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2210 mask |= e->mask << e->shift_r;
2211 }
2212
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002213 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002214}
2215EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2216
2217/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002218 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2219 * @kcontrol: mixer control
2220 * @uinfo: control element information
2221 *
2222 * Callback to provide information about an external enumerated
2223 * single mixer.
2224 *
2225 * Returns 0 for success.
2226 */
2227int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2228 struct snd_ctl_elem_info *uinfo)
2229{
2230 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2231
2232 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2233 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002234 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002235
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002236 if (uinfo->value.enumerated.item > e->max - 1)
2237 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002238 strcpy(uinfo->value.enumerated.name,
2239 e->texts[uinfo->value.enumerated.item]);
2240 return 0;
2241}
2242EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2243
2244/**
2245 * snd_soc_info_volsw_ext - external single mixer info callback
2246 * @kcontrol: mixer control
2247 * @uinfo: control element information
2248 *
2249 * Callback to provide information about a single external mixer control.
2250 *
2251 * Returns 0 for success.
2252 */
2253int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2254 struct snd_ctl_elem_info *uinfo)
2255{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002256 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002257
Mark Brownfd5dfad2009-04-15 21:37:46 +01002258 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002259 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2260 else
2261 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2262
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002263 uinfo->count = 1;
2264 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002265 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002266 return 0;
2267}
2268EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2269
2270/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002271 * snd_soc_info_volsw - single mixer info callback
2272 * @kcontrol: mixer control
2273 * @uinfo: control element information
2274 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002275 * Callback to provide information about a single mixer control, or a double
2276 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002277 *
2278 * Returns 0 for success.
2279 */
2280int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2281 struct snd_ctl_elem_info *uinfo)
2282{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002283 struct soc_mixer_control *mc =
2284 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002285 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002286
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002287 if (!mc->platform_max)
2288 mc->platform_max = mc->max;
2289 platform_max = mc->platform_max;
2290
2291 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002292 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2293 else
2294 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2295
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002296 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002297 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002298 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002299 return 0;
2300}
2301EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2302
2303/**
2304 * snd_soc_get_volsw - single mixer get callback
2305 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002306 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002307 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002308 * Callback to get the value of a single mixer control, or a double mixer
2309 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002310 *
2311 * Returns 0 for success.
2312 */
2313int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2314 struct snd_ctl_elem_value *ucontrol)
2315{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002316 struct soc_mixer_control *mc =
2317 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002318 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002319 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002320 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002321 unsigned int shift = mc->shift;
2322 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002323 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002324 unsigned int mask = (1 << fls(max)) - 1;
2325 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002326
2327 ucontrol->value.integer.value[0] =
2328 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002329 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002330 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002331 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002332
2333 if (snd_soc_volsw_is_stereo(mc)) {
2334 if (reg == reg2)
2335 ucontrol->value.integer.value[1] =
2336 (snd_soc_read(codec, reg) >> rshift) & mask;
2337 else
2338 ucontrol->value.integer.value[1] =
2339 (snd_soc_read(codec, reg2) >> shift) & mask;
2340 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002341 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002342 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002343 }
2344
2345 return 0;
2346}
2347EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2348
2349/**
2350 * snd_soc_put_volsw - single mixer put callback
2351 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002352 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002353 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002354 * Callback to set the value of a single mixer control, or a double mixer
2355 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002356 *
2357 * Returns 0 for success.
2358 */
2359int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2360 struct snd_ctl_elem_value *ucontrol)
2361{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002362 struct soc_mixer_control *mc =
2363 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002364 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002365 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002366 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002367 unsigned int shift = mc->shift;
2368 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002369 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002370 unsigned int mask = (1 << fls(max)) - 1;
2371 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002372 int err;
2373 bool type_2r = 0;
2374 unsigned int val2 = 0;
2375 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002376
2377 val = (ucontrol->value.integer.value[0] & mask);
2378 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002379 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002380 val_mask = mask << shift;
2381 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002382 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002383 val2 = (ucontrol->value.integer.value[1] & mask);
2384 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002385 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002386 if (reg == reg2) {
2387 val_mask |= mask << rshift;
2388 val |= val2 << rshift;
2389 } else {
2390 val2 = val2 << shift;
2391 type_2r = 1;
2392 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002393 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002394 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002395 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002396 return err;
2397
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002398 if (type_2r)
2399 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2400
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002401 return err;
2402}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002403EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002404
Mark Browne13ac2e2008-05-28 17:58:05 +01002405/**
2406 * snd_soc_info_volsw_s8 - signed mixer info callback
2407 * @kcontrol: mixer control
2408 * @uinfo: control element information
2409 *
2410 * Callback to provide information about a signed mixer control.
2411 *
2412 * Returns 0 for success.
2413 */
2414int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2415 struct snd_ctl_elem_info *uinfo)
2416{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002417 struct soc_mixer_control *mc =
2418 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002419 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002420 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002421
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002422 if (!mc->platform_max)
2423 mc->platform_max = mc->max;
2424 platform_max = mc->platform_max;
2425
Mark Browne13ac2e2008-05-28 17:58:05 +01002426 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2427 uinfo->count = 2;
2428 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002429 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002430 return 0;
2431}
2432EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2433
2434/**
2435 * snd_soc_get_volsw_s8 - signed mixer get callback
2436 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002437 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002438 *
2439 * Callback to get the value of a signed mixer control.
2440 *
2441 * Returns 0 for success.
2442 */
2443int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2444 struct snd_ctl_elem_value *ucontrol)
2445{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002446 struct soc_mixer_control *mc =
2447 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002448 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002449 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002450 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002451 int val = snd_soc_read(codec, reg);
2452
2453 ucontrol->value.integer.value[0] =
2454 ((signed char)(val & 0xff))-min;
2455 ucontrol->value.integer.value[1] =
2456 ((signed char)((val >> 8) & 0xff))-min;
2457 return 0;
2458}
2459EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2460
2461/**
2462 * snd_soc_put_volsw_sgn - signed mixer put callback
2463 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002464 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002465 *
2466 * Callback to set the value of a signed mixer control.
2467 *
2468 * Returns 0 for success.
2469 */
2470int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2471 struct snd_ctl_elem_value *ucontrol)
2472{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002473 struct soc_mixer_control *mc =
2474 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002475 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002476 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002477 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002478 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002479
2480 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2481 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2482
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002483 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002484}
2485EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2486
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002487/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002488 * snd_soc_limit_volume - Set new limit to an existing volume control.
2489 *
2490 * @codec: where to look for the control
2491 * @name: Name of the control
2492 * @max: new maximum limit
2493 *
2494 * Return 0 for success, else error.
2495 */
2496int snd_soc_limit_volume(struct snd_soc_codec *codec,
2497 const char *name, int max)
2498{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002499 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002500 struct snd_kcontrol *kctl;
2501 struct soc_mixer_control *mc;
2502 int found = 0;
2503 int ret = -EINVAL;
2504
2505 /* Sanity check for name and max */
2506 if (unlikely(!name || max <= 0))
2507 return -EINVAL;
2508
2509 list_for_each_entry(kctl, &card->controls, list) {
2510 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2511 found = 1;
2512 break;
2513 }
2514 }
2515 if (found) {
2516 mc = (struct soc_mixer_control *)kctl->private_value;
2517 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002518 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002519 ret = 0;
2520 }
2521 }
2522 return ret;
2523}
2524EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2525
2526/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002527 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2528 * mixer info callback
2529 * @kcontrol: mixer control
2530 * @uinfo: control element information
2531 *
2532 * Returns 0 for success.
2533 */
2534int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2535 struct snd_ctl_elem_info *uinfo)
2536{
2537 struct soc_mixer_control *mc =
2538 (struct soc_mixer_control *)kcontrol->private_value;
2539 int max = mc->max;
2540 int min = mc->min;
2541
2542 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2543 uinfo->count = 2;
2544 uinfo->value.integer.min = 0;
2545 uinfo->value.integer.max = max-min;
2546
2547 return 0;
2548}
2549EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2550
2551/**
2552 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2553 * mixer get callback
2554 * @kcontrol: mixer control
2555 * @uinfo: control element information
2556 *
2557 * Returns 0 for success.
2558 */
2559int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2560 struct snd_ctl_elem_value *ucontrol)
2561{
2562 struct soc_mixer_control *mc =
2563 (struct soc_mixer_control *)kcontrol->private_value;
2564 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2565 unsigned int mask = (1<<mc->shift)-1;
2566 int min = mc->min;
2567 int val = snd_soc_read(codec, mc->reg) & mask;
2568 int valr = snd_soc_read(codec, mc->rreg) & mask;
2569
Stuart Longland20630c72010-06-18 12:56:10 +10002570 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2571 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002572 return 0;
2573}
2574EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2575
2576/**
2577 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2578 * mixer put callback
2579 * @kcontrol: mixer control
2580 * @uinfo: control element information
2581 *
2582 * Returns 0 for success.
2583 */
2584int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2585 struct snd_ctl_elem_value *ucontrol)
2586{
2587 struct soc_mixer_control *mc =
2588 (struct soc_mixer_control *)kcontrol->private_value;
2589 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2590 unsigned int mask = (1<<mc->shift)-1;
2591 int min = mc->min;
2592 int ret;
2593 unsigned int val, valr, oval, ovalr;
2594
2595 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2596 val &= mask;
2597 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2598 valr &= mask;
2599
2600 oval = snd_soc_read(codec, mc->reg) & mask;
2601 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2602
2603 ret = 0;
2604 if (oval != val) {
2605 ret = snd_soc_write(codec, mc->reg, val);
2606 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002607 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002608 }
2609 if (ovalr != valr) {
2610 ret = snd_soc_write(codec, mc->rreg, valr);
2611 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002612 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002613 }
2614
2615 return 0;
2616}
2617EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2618
2619/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002620 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2621 * @dai: DAI
2622 * @clk_id: DAI specific clock ID
2623 * @freq: new clock frequency in Hz
2624 * @dir: new clock direction - input/output.
2625 *
2626 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2627 */
2628int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2629 unsigned int freq, int dir)
2630{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002631 if (dai->driver && dai->driver->ops->set_sysclk)
2632 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002633 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002634 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002635 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002636 else
2637 return -EINVAL;
2638}
2639EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2640
2641/**
Mark Brownec4ee522011-03-07 20:58:11 +00002642 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2643 * @codec: CODEC
2644 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002645 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002646 * @freq: new clock frequency in Hz
2647 * @dir: new clock direction - input/output.
2648 *
2649 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2650 */
2651int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002652 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002653{
2654 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002655 return codec->driver->set_sysclk(codec, clk_id, source,
2656 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002657 else
2658 return -EINVAL;
2659}
2660EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2661
2662/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002663 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2664 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002665 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002666 * @div: new clock divisor.
2667 *
2668 * Configures the clock dividers. This is used to derive the best DAI bit and
2669 * frame clocks from the system or master clock. It's best to set the DAI bit
2670 * and frame clocks as low as possible to save system power.
2671 */
2672int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2673 int div_id, int div)
2674{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002675 if (dai->driver && dai->driver->ops->set_clkdiv)
2676 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002677 else
2678 return -EINVAL;
2679}
2680EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2681
2682/**
2683 * snd_soc_dai_set_pll - configure DAI PLL.
2684 * @dai: DAI
2685 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002686 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002687 * @freq_in: PLL input clock frequency in Hz
2688 * @freq_out: requested PLL output clock frequency in Hz
2689 *
2690 * Configures and enables PLL to generate output clock based on input clock.
2691 */
Mark Brown85488032009-09-05 18:52:16 +01002692int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2693 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002694{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002695 if (dai->driver && dai->driver->ops->set_pll)
2696 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002697 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002698 else if (dai->codec && dai->codec->driver->set_pll)
2699 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2700 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002701 else
2702 return -EINVAL;
2703}
2704EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2705
Mark Brownec4ee522011-03-07 20:58:11 +00002706/*
2707 * snd_soc_codec_set_pll - configure codec PLL.
2708 * @codec: CODEC
2709 * @pll_id: DAI specific PLL ID
2710 * @source: DAI specific source for the PLL
2711 * @freq_in: PLL input clock frequency in Hz
2712 * @freq_out: requested PLL output clock frequency in Hz
2713 *
2714 * Configures and enables PLL to generate output clock based on input clock.
2715 */
2716int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2717 unsigned int freq_in, unsigned int freq_out)
2718{
2719 if (codec->driver->set_pll)
2720 return codec->driver->set_pll(codec, pll_id, source,
2721 freq_in, freq_out);
2722 else
2723 return -EINVAL;
2724}
2725EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2726
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002727/**
2728 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2729 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002730 * @fmt: SND_SOC_DAIFMT_ format value.
2731 *
2732 * Configures the DAI hardware format and clocking.
2733 */
2734int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2735{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002736 if (dai->driver && dai->driver->ops->set_fmt)
2737 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002738 else
2739 return -EINVAL;
2740}
2741EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2742
2743/**
2744 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2745 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002746 * @tx_mask: bitmask representing active TX slots.
2747 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002748 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002749 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002750 *
2751 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2752 * specific.
2753 */
2754int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002755 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002756{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002757 if (dai->driver && dai->driver->ops->set_tdm_slot)
2758 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002759 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002760 else
2761 return -EINVAL;
2762}
2763EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2764
2765/**
Barry Song472df3c2009-09-12 01:16:29 +08002766 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2767 * @dai: DAI
2768 * @tx_num: how many TX channels
2769 * @tx_slot: pointer to an array which imply the TX slot number channel
2770 * 0~num-1 uses
2771 * @rx_num: how many RX channels
2772 * @rx_slot: pointer to an array which imply the RX slot number channel
2773 * 0~num-1 uses
2774 *
2775 * configure the relationship between channel number and TDM slot number.
2776 */
2777int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2778 unsigned int tx_num, unsigned int *tx_slot,
2779 unsigned int rx_num, unsigned int *rx_slot)
2780{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002781 if (dai->driver && dai->driver->ops->set_channel_map)
2782 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002783 rx_num, rx_slot);
2784 else
2785 return -EINVAL;
2786}
2787EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2788
2789/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002790 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2791 * @dai: DAI
2792 * @tristate: tristate enable
2793 *
2794 * Tristates the DAI so that others can use it.
2795 */
2796int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2797{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002798 if (dai->driver && dai->driver->ops->set_tristate)
2799 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002800 else
2801 return -EINVAL;
2802}
2803EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2804
2805/**
2806 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2807 * @dai: DAI
2808 * @mute: mute enable
2809 *
2810 * Mutes the DAI DAC.
2811 */
2812int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2813{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002814 if (dai->driver && dai->driver->ops->digital_mute)
2815 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002816 else
2817 return -EINVAL;
2818}
2819EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2820
Mark Brownc5af3a22008-11-28 13:29:45 +00002821/**
2822 * snd_soc_register_card - Register a card with the ASoC core
2823 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002824 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002825 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002826 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302827int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002828{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002829 int i;
2830
Mark Brownc5af3a22008-11-28 13:29:45 +00002831 if (!card->name || !card->dev)
2832 return -EINVAL;
2833
Mark Browned77cc12011-05-03 18:25:34 +01002834 dev_set_drvdata(card->dev, card);
2835
Stephen Warren111c6412011-01-28 14:26:35 -07002836 snd_soc_initialize_card_lists(card);
2837
Vinod Koul150dd2f2011-01-13 22:48:32 +05302838 soc_init_card_debugfs(card);
2839
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002840 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
2841 (card->num_links + card->num_aux_devs),
2842 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002843 if (card->rtd == NULL)
2844 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002845 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002846
2847 for (i = 0; i < card->num_links; i++)
2848 card->rtd[i].dai_link = &card->dai_link[i];
2849
Mark Brownc5af3a22008-11-28 13:29:45 +00002850 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01002851 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00002852 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002853 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002854
2855 mutex_lock(&client_mutex);
2856 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002857 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002858 mutex_unlock(&client_mutex);
2859
2860 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2861
2862 return 0;
2863}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302864EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002865
2866/**
2867 * snd_soc_unregister_card - Unregister a card with the ASoC core
2868 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002869 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002870 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002871 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302872int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002873{
Vinod Koulb0e26482011-01-13 22:48:02 +05302874 if (card->instantiated)
2875 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002876 mutex_lock(&client_mutex);
2877 list_del(&card->list);
2878 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002879 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2880
2881 return 0;
2882}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302883EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002884
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002885/*
2886 * Simplify DAI link configuration by removing ".-1" from device names
2887 * and sanitizing names.
2888 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002889static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002890{
2891 char *found, name[NAME_SIZE];
2892 int id1, id2;
2893
2894 if (dev_name(dev) == NULL)
2895 return NULL;
2896
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002897 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002898
2899 /* are we a "%s.%d" name (platform and SPI components) */
2900 found = strstr(name, dev->driver->name);
2901 if (found) {
2902 /* get ID */
2903 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2904
2905 /* discard ID from name if ID == -1 */
2906 if (*id == -1)
2907 found[strlen(dev->driver->name)] = '\0';
2908 }
2909
2910 } else {
2911 /* I2C component devices are named "bus-addr" */
2912 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2913 char tmp[NAME_SIZE];
2914
2915 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002916 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002917
2918 /* sanitize component name for DAI link creation */
2919 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002920 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002921 } else
2922 *id = 0;
2923 }
2924
2925 return kstrdup(name, GFP_KERNEL);
2926}
2927
2928/*
2929 * Simplify DAI link naming for single devices with multiple DAIs by removing
2930 * any ".-1" and using the DAI name (instead of device name).
2931 */
2932static inline char *fmt_multiple_name(struct device *dev,
2933 struct snd_soc_dai_driver *dai_drv)
2934{
2935 if (dai_drv->name == NULL) {
2936 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2937 dev_name(dev));
2938 return NULL;
2939 }
2940
2941 return kstrdup(dai_drv->name, GFP_KERNEL);
2942}
2943
Mark Brown91151712008-11-30 23:31:24 +00002944/**
2945 * snd_soc_register_dai - Register a DAI with the ASoC core
2946 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002947 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002948 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002949int snd_soc_register_dai(struct device *dev,
2950 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00002951{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002952 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00002953
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002954 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00002955
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002956 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2957 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08002958 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08002959
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002960 /* create DAI component name */
2961 dai->name = fmt_single_name(dev, &dai->id);
2962 if (dai->name == NULL) {
2963 kfree(dai);
2964 return -ENOMEM;
2965 }
2966
2967 dai->dev = dev;
2968 dai->driver = dai_drv;
2969 if (!dai->driver->ops)
2970 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00002971
2972 mutex_lock(&client_mutex);
2973 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002974 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002975 mutex_unlock(&client_mutex);
2976
2977 pr_debug("Registered DAI '%s'\n", dai->name);
2978
2979 return 0;
2980}
2981EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2982
2983/**
2984 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2985 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002986 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002987 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002988void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00002989{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002990 struct snd_soc_dai *dai;
2991
2992 list_for_each_entry(dai, &dai_list, list) {
2993 if (dev == dai->dev)
2994 goto found;
2995 }
2996 return;
2997
2998found:
Mark Brown91151712008-11-30 23:31:24 +00002999 mutex_lock(&client_mutex);
3000 list_del(&dai->list);
3001 mutex_unlock(&client_mutex);
3002
3003 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003004 kfree(dai->name);
3005 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003006}
3007EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3008
3009/**
3010 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3011 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003012 * @dai: Array of DAIs to register
3013 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003014 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003015int snd_soc_register_dais(struct device *dev,
3016 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003017{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003018 struct snd_soc_dai *dai;
3019 int i, ret = 0;
3020
Liam Girdwood720ffa42010-08-18 00:30:30 +01003021 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003022
3023 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003024
3025 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003026 if (dai == NULL) {
3027 ret = -ENOMEM;
3028 goto err;
3029 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003030
3031 /* create DAI component name */
3032 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3033 if (dai->name == NULL) {
3034 kfree(dai);
3035 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003036 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003037 }
3038
3039 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003040 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003041 if (dai->driver->id)
3042 dai->id = dai->driver->id;
3043 else
3044 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003045 if (!dai->driver->ops)
3046 dai->driver->ops = &null_dai_ops;
3047
3048 mutex_lock(&client_mutex);
3049 list_add(&dai->list, &dai_list);
3050 mutex_unlock(&client_mutex);
3051
3052 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003053 }
3054
Axel Lin1dcb4f32010-12-06 16:48:03 +08003055 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003056 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08003057 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00003058 return 0;
3059
3060err:
3061 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003062 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003063
3064 return ret;
3065}
3066EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3067
3068/**
3069 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3070 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003071 * @dai: Array of DAIs to unregister
3072 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003073 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003074void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003075{
3076 int i;
3077
3078 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003079 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003080}
3081EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3082
Mark Brown12a48a8c2008-12-03 19:40:30 +00003083/**
3084 * snd_soc_register_platform - Register a platform with the ASoC core
3085 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003086 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003087 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003088int snd_soc_register_platform(struct device *dev,
3089 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003090{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003091 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003092
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003093 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3094
3095 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3096 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003097 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003098
3099 /* create platform component name */
3100 platform->name = fmt_single_name(dev, &platform->id);
3101 if (platform->name == NULL) {
3102 kfree(platform);
3103 return -ENOMEM;
3104 }
3105
3106 platform->dev = dev;
3107 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003108 platform->dapm.dev = dev;
3109 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003110 platform->dapm.stream_event = platform_drv->stream_event;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003111
3112 mutex_lock(&client_mutex);
3113 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003114 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003115 mutex_unlock(&client_mutex);
3116
3117 pr_debug("Registered platform '%s'\n", platform->name);
3118
3119 return 0;
3120}
3121EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3122
3123/**
3124 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3125 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003126 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003127 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003128void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003129{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003130 struct snd_soc_platform *platform;
3131
3132 list_for_each_entry(platform, &platform_list, list) {
3133 if (dev == platform->dev)
3134 goto found;
3135 }
3136 return;
3137
3138found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003139 mutex_lock(&client_mutex);
3140 list_del(&platform->list);
3141 mutex_unlock(&client_mutex);
3142
3143 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003144 kfree(platform->name);
3145 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003146}
3147EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3148
Mark Brown151ab222009-05-09 16:22:58 +01003149static u64 codec_format_map[] = {
3150 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3151 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3152 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3153 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3154 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3155 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3156 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3157 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3158 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3159 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3160 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3161 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3162 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3163 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3164 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3165 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3166};
3167
3168/* Fix up the DAI formats for endianness: codecs don't actually see
3169 * the endianness of the data but we're using the CPU format
3170 * definitions which do need to include endianness so we ensure that
3171 * codec DAIs always have both big and little endian variants set.
3172 */
3173static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3174{
3175 int i;
3176
3177 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3178 if (stream->formats & codec_format_map[i])
3179 stream->formats |= codec_format_map[i];
3180}
3181
Mark Brown0d0cf002008-12-10 14:32:45 +00003182/**
3183 * snd_soc_register_codec - Register a codec with the ASoC core
3184 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003185 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003186 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003187int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003188 const struct snd_soc_codec_driver *codec_drv,
3189 struct snd_soc_dai_driver *dai_drv,
3190 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003191{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003192 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003193 struct snd_soc_codec *codec;
3194 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003195
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003196 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003197
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003198 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3199 if (codec == NULL)
3200 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003201
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003202 /* create CODEC component name */
3203 codec->name = fmt_single_name(dev, &codec->id);
3204 if (codec->name == NULL) {
3205 kfree(codec);
3206 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003207 }
3208
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003209 if (codec_drv->compress_type)
3210 codec->compress_type = codec_drv->compress_type;
3211 else
3212 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3213
Mark Brownc3acec22010-12-02 16:15:29 +00003214 codec->write = codec_drv->write;
3215 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003216 codec->volatile_register = codec_drv->volatile_register;
3217 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003218 codec->writable_register = codec_drv->writable_register;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003219 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3220 codec->dapm.dev = dev;
3221 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003222 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003223 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003224 codec->dev = dev;
3225 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003226 codec->num_dai = num_dai;
3227 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003228
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003229 /* allocate CODEC register cache */
3230 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003231 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003232 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003233 /* it is necessary to make a copy of the default register cache
3234 * because in the case of using a compression type that requires
3235 * the default register cache to be marked as __devinitconst the
3236 * kernel might have freed the array by the time we initialize
3237 * the cache.
3238 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003239 if (codec_drv->reg_cache_default) {
3240 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3241 reg_size, GFP_KERNEL);
3242 if (!codec->reg_def_copy) {
3243 ret = -ENOMEM;
3244 goto fail;
3245 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003246 }
3247 }
3248
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003249 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3250 if (!codec->volatile_register)
3251 codec->volatile_register = snd_soc_default_volatile_register;
3252 if (!codec->readable_register)
3253 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003254 if (!codec->writable_register)
3255 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003256 }
3257
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003258 for (i = 0; i < num_dai; i++) {
3259 fixup_codec_formats(&dai_drv[i].playback);
3260 fixup_codec_formats(&dai_drv[i].capture);
3261 }
3262
Mark Brown26b01cc2010-08-18 20:20:55 +01003263 /* register any DAIs */
3264 if (num_dai) {
3265 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3266 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003267 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003268 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003269
Mark Brown0d0cf002008-12-10 14:32:45 +00003270 mutex_lock(&client_mutex);
3271 list_add(&codec->list, &codec_list);
3272 snd_soc_instantiate_cards();
3273 mutex_unlock(&client_mutex);
3274
3275 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003276 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003277
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003278fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003279 kfree(codec->reg_def_copy);
3280 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003281 kfree(codec->name);
3282 kfree(codec);
3283 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003284}
3285EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3286
3287/**
3288 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3289 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003290 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003291 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003292void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003293{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003294 struct snd_soc_codec *codec;
3295 int i;
3296
3297 list_for_each_entry(codec, &codec_list, list) {
3298 if (dev == codec->dev)
3299 goto found;
3300 }
3301 return;
3302
3303found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003304 if (codec->num_dai)
3305 for (i = 0; i < codec->num_dai; i++)
3306 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003307
Mark Brown0d0cf002008-12-10 14:32:45 +00003308 mutex_lock(&client_mutex);
3309 list_del(&codec->list);
3310 mutex_unlock(&client_mutex);
3311
3312 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003313
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003314 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003315 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003316 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003317 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003318}
3319EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3320
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003321/* Retrieve a card's name from device tree */
3322int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3323 const char *propname)
3324{
3325 struct device_node *np = card->dev->of_node;
3326 int ret;
3327
3328 ret = of_property_read_string_index(np, propname, 0, &card->name);
3329 /*
3330 * EINVAL means the property does not exist. This is fine providing
3331 * card->name was previously set, which is checked later in
3332 * snd_soc_register_card.
3333 */
3334 if (ret < 0 && ret != -EINVAL) {
3335 dev_err(card->dev,
3336 "Property '%s' could not be read: %d\n",
3337 propname, ret);
3338 return ret;
3339 }
3340
3341 return 0;
3342}
3343EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3344
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003345static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003346{
Mark Brown384c89e2008-12-03 17:34:03 +00003347#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003348 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3349 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Mark Brown384c89e2008-12-03 17:34:03 +00003350 printk(KERN_WARNING
3351 "ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003352 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003353 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003354
Mark Brown8a9dab12011-01-10 22:25:21 +00003355 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003356 &codec_list_fops))
3357 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3358
Mark Brown8a9dab12011-01-10 22:25:21 +00003359 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003360 &dai_list_fops))
3361 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003362
Mark Brown8a9dab12011-01-10 22:25:21 +00003363 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003364 &platform_list_fops))
3365 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003366#endif
3367
Mark Brownfb257892011-04-28 10:57:54 +01003368 snd_soc_util_init();
3369
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003370 return platform_driver_register(&soc_driver);
3371}
Mark Brown4abe8e12010-10-12 17:41:03 +01003372module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003373
Mark Brown7d8c16a2008-11-30 22:11:24 +00003374static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003375{
Mark Brownfb257892011-04-28 10:57:54 +01003376 snd_soc_util_exit();
3377
Mark Brown384c89e2008-12-03 17:34:03 +00003378#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003379 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003380#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003381 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003382}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003383module_exit(snd_soc_exit);
3384
3385/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003386MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003387MODULE_DESCRIPTION("ALSA SoC Core");
3388MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003389MODULE_ALIAS("platform:soc-audio");