blob: a3a47cdaac8fa22c28a3371e579d1560b6bcf128 [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
Frank Mandarinodb2a4162006-10-06 18:31:09 +020062/*
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
66 */
67static int pmdown_time = 5000;
68module_param(pmdown_time, int, 0);
69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000071/* returns the minimum number of bytes needed to represent
72 * a particular given value */
73static int min_bytes_needed(unsigned long val)
74{
75 int c = 0;
76 int i;
77
78 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
79 if (val & (1UL << i))
80 break;
81 c = (sizeof val * 8) - c;
82 if (!c || (c % 8))
83 c = (c + 8) / 8;
84 else
85 c /= 8;
86 return c;
87}
88
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000089/* fill buf which is 'len' bytes with a formatted
90 * string of the form 'reg: value\n' */
91static int format_register_str(struct snd_soc_codec *codec,
92 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000093{
Stephen Warren00b317a2011-04-01 14:50:44 -060094 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
95 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000096 int ret;
97 char tmpbuf[len + 1];
98 char regbuf[regsize + 1];
99
100 /* since tmpbuf is allocated on the stack, warn the callers if they
101 * try to abuse this function */
102 WARN_ON(len > 63);
103
104 /* +2 for ': ' and + 1 for '\n' */
105 if (wordsize + regsize + 2 + 1 != len)
106 return -EINVAL;
107
Mark Brown25032c12011-08-01 13:52:48 +0900108 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000109 if (ret < 0) {
110 memset(regbuf, 'X', regsize);
111 regbuf[regsize] = '\0';
112 } else {
113 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
114 }
115
116 /* prepare the buffer */
117 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
118 /* copy it back to the caller without the '\0' */
119 memcpy(buf, tmpbuf, len);
120
121 return 0;
122}
123
124/* codec register dump */
125static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
126 size_t count, loff_t pos)
127{
128 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000129 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000130 int len;
131 size_t total = 0;
132 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000133
Stephen Warren00b317a2011-04-01 14:50:44 -0600134 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
135 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000136
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000137 len = wordsize + regsize + 2 + 1;
138
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000139 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000140 return 0;
141
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000142 if (codec->driver->reg_cache_step)
143 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000144
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000145 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200146 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000147 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000148 if (codec->driver->display_register) {
149 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000150 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100151 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000152 /* only support larger than PAGE_SIZE bytes debugfs
153 * entries for the default case */
154 if (p >= pos) {
155 if (total + len >= count - 1)
156 break;
157 format_register_str(codec, i, buf + total, len);
158 total += len;
159 }
160 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100161 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000162 }
163
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000164 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000165
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000166 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000167}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168
Mark Brown2624d5f2009-11-03 21:56:13 +0000169static ssize_t codec_reg_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
171{
Mark Brown36ae1a92012-01-06 17:12:45 -0800172 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000173
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000174 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000175}
176
177static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
178
Mark Browndbe21402010-02-12 11:37:24 +0000179static ssize_t pmdown_time_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
Mark Brown36ae1a92012-01-06 17:12:45 -0800182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000183
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000185}
186
187static ssize_t pmdown_time_set(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
190{
Mark Brown36ae1a92012-01-06 17:12:45 -0800191 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700192 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000193
Mark Brownc593b522010-10-27 20:11:17 -0700194 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
195 if (ret)
196 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
198 return count;
199}
200
201static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202
Mark Brown2624d5f2009-11-03 21:56:13 +0000203#ifdef CONFIG_DEBUG_FS
204static int codec_reg_open_file(struct inode *inode, struct file *file)
205{
206 file->private_data = inode->i_private;
207 return 0;
208}
209
210static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000211 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000212{
213 ssize_t ret;
214 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000215 char *buf;
216
217 if (*ppos < 0 || !count)
218 return -EINVAL;
219
220 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000221 if (!buf)
222 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000223
224 ret = soc_codec_reg_show(codec, buf, count, *ppos);
225 if (ret >= 0) {
226 if (copy_to_user(user_buf, buf, ret)) {
227 kfree(buf);
228 return -EFAULT;
229 }
230 *ppos += ret;
231 }
232
Mark Brown2624d5f2009-11-03 21:56:13 +0000233 kfree(buf);
234 return ret;
235}
236
237static ssize_t codec_reg_write_file(struct file *file,
238 const char __user *user_buf, size_t count, loff_t *ppos)
239{
240 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700241 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000242 char *start = buf;
243 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000244 struct snd_soc_codec *codec = file->private_data;
245
246 buf_size = min(count, (sizeof(buf)-1));
247 if (copy_from_user(buf, user_buf, buf_size))
248 return -EFAULT;
249 buf[buf_size] = 0;
250
Mark Brown2624d5f2009-11-03 21:56:13 +0000251 while (*start == ' ')
252 start++;
253 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000254 while (*start == ' ')
255 start++;
256 if (strict_strtoul(start, 16, &value))
257 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000258
259 /* Userspace has been fiddling around behind the kernel's back */
260 add_taint(TAINT_USER);
261
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000262 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000263 return buf_size;
264}
265
266static const struct file_operations codec_reg_fops = {
267 .open = codec_reg_open_file,
268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200270 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000271};
272
273static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
274{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200275 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
276
277 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
278 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000279 if (!codec->debugfs_codec_root) {
280 printk(KERN_WARNING
281 "ASoC: Failed to create codec debugfs directory\n");
282 return;
283 }
284
Mark Brownaaee8ef2011-01-26 20:53:50 +0000285 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
286 &codec->cache_sync);
287 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
288 &codec->cache_only);
289
Mark Brown2624d5f2009-11-03 21:56:13 +0000290 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
291 codec->debugfs_codec_root,
292 codec, &codec_reg_fops);
293 if (!codec->debugfs_reg)
294 printk(KERN_WARNING
295 "ASoC: Failed to create codec register debugfs file\n");
296
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200297 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000298}
299
300static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
301{
302 debugfs_remove_recursive(codec->debugfs_codec_root);
303}
304
Mark Brownc3c5a192010-09-15 18:15:14 +0100305static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
306 size_t count, loff_t *ppos)
307{
308 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100309 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100310 struct snd_soc_codec *codec;
311
312 if (!buf)
313 return -ENOMEM;
314
Mark Brown2b194f9d2010-10-13 10:52:16 +0100315 list_for_each_entry(codec, &codec_list, list) {
316 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
317 codec->name);
318 if (len >= 0)
319 ret += len;
320 if (ret > PAGE_SIZE) {
321 ret = PAGE_SIZE;
322 break;
323 }
324 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100325
326 if (ret >= 0)
327 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
328
329 kfree(buf);
330
331 return ret;
332}
333
334static const struct file_operations codec_list_fops = {
335 .read = codec_list_read_file,
336 .llseek = default_llseek,/* read accesses f_pos */
337};
338
Mark Brownf3208782010-09-15 18:19:07 +0100339static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
340 size_t count, loff_t *ppos)
341{
342 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100343 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100344 struct snd_soc_dai *dai;
345
346 if (!buf)
347 return -ENOMEM;
348
Mark Brown2b194f9d2010-10-13 10:52:16 +0100349 list_for_each_entry(dai, &dai_list, list) {
350 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
351 if (len >= 0)
352 ret += len;
353 if (ret > PAGE_SIZE) {
354 ret = PAGE_SIZE;
355 break;
356 }
357 }
Mark Brownf3208782010-09-15 18:19:07 +0100358
Mark Brown2b194f9d2010-10-13 10:52:16 +0100359 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100360
361 kfree(buf);
362
363 return ret;
364}
365
366static const struct file_operations dai_list_fops = {
367 .read = dai_list_read_file,
368 .llseek = default_llseek,/* read accesses f_pos */
369};
370
Mark Brown19c7ac22010-09-15 18:22:40 +0100371static ssize_t platform_list_read_file(struct file *file,
372 char __user *user_buf,
373 size_t count, loff_t *ppos)
374{
375 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100376 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100377 struct snd_soc_platform *platform;
378
379 if (!buf)
380 return -ENOMEM;
381
Mark Brown2b194f9d2010-10-13 10:52:16 +0100382 list_for_each_entry(platform, &platform_list, list) {
383 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
384 platform->name);
385 if (len >= 0)
386 ret += len;
387 if (ret > PAGE_SIZE) {
388 ret = PAGE_SIZE;
389 break;
390 }
391 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100392
Mark Brown2b194f9d2010-10-13 10:52:16 +0100393 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100394
395 kfree(buf);
396
397 return ret;
398}
399
400static const struct file_operations platform_list_fops = {
401 .read = platform_list_read_file,
402 .llseek = default_llseek,/* read accesses f_pos */
403};
404
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200405static void soc_init_card_debugfs(struct snd_soc_card *card)
406{
407 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000408 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200409 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200410 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100411 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200412 return;
413 }
414
415 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
416 card->debugfs_card_root,
417 &card->pop_time);
418 if (!card->debugfs_pop_time)
419 dev_warn(card->dev,
420 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200421}
422
423static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
424{
425 debugfs_remove_recursive(card->debugfs_card_root);
426}
427
Mark Brown2624d5f2009-11-03 21:56:13 +0000428#else
429
430static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
431{
432}
433
434static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
435{
436}
Axel Linb95fccb2010-11-09 17:06:44 +0800437
438static inline void soc_init_card_debugfs(struct snd_soc_card *card)
439{
440}
441
442static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
443{
444}
Mark Brown2624d5f2009-11-03 21:56:13 +0000445#endif
446
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200447#ifdef CONFIG_SND_SOC_AC97_BUS
448/* unregister ac97 codec */
449static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
450{
451 if (codec->ac97->dev.bus)
452 device_unregister(&codec->ac97->dev);
453 return 0;
454}
455
456/* stop no dev release warning */
457static void soc_ac97_device_release(struct device *dev){}
458
459/* register ac97 codec to bus */
460static int soc_ac97_dev_register(struct snd_soc_codec *codec)
461{
462 int err;
463
464 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100465 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200466 codec->ac97->dev.release = soc_ac97_device_release;
467
Kay Sieversbb072bf2008-11-02 03:50:35 +0100468 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000469 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200470 err = device_register(&codec->ac97->dev);
471 if (err < 0) {
472 snd_printk(KERN_ERR "Can't register ac97 bus\n");
473 codec->ac97->dev.bus = NULL;
474 return err;
475 }
476 return 0;
477}
478#endif
479
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000480#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200481/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000482int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200483{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000484 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200485 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200486 int i;
487
Daniel Macke3509ff2009-06-03 17:44:49 +0200488 /* If the initialization of this soc device failed, there is no codec
489 * associated with it. Just bail out in this case.
490 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000491 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200492 return 0;
493
Andy Green6ed25972008-06-13 16:24:05 +0100494 /* Due to the resume being scheduled into a workqueue we could
495 * suspend before that's finished - wait for it to complete.
496 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000497 snd_power_lock(card->snd_card);
498 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
499 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100500
501 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000502 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100503
Mark Browna00f90f2010-12-02 16:24:24 +0000504 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000505 for (i = 0; i < card->num_rtd; i++) {
506 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
507 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100508
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000509 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100510 continue;
511
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000512 if (drv->ops->digital_mute && dai->playback_active)
513 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200514 }
515
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100516 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000517 for (i = 0; i < card->num_rtd; i++) {
518 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100519 continue;
520
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000521 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100522 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100523
Mark Brown87506542008-11-18 20:50:34 +0000524 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000525 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200526
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000527 for (i = 0; i < card->num_rtd; i++) {
528 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
529 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100530
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100532 continue;
533
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000534 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
535 cpu_dai->driver->suspend(cpu_dai);
536 if (platform->driver->suspend && !platform->suspended) {
537 platform->driver->suspend(cpu_dai);
538 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100539 }
540 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200541
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000542 /* close any waiting streams and save state */
543 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100544 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200545 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000546 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100547
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548 for (i = 0; i < card->num_rtd; i++) {
549 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
550
551 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100552 continue;
553
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 if (driver->playback.stream_name != NULL)
555 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
556 SND_SOC_DAPM_STREAM_SUSPEND);
557
558 if (driver->capture.stream_name != NULL)
559 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
560 SND_SOC_DAPM_STREAM_SUSPEND);
561 }
562
563 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200564 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000565 /* If there are paths active then the CODEC will be held with
566 * bias _ON and should not be suspended. */
567 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200568 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 case SND_SOC_BIAS_STANDBY:
570 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100571 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900573 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 break;
575 default:
576 dev_dbg(codec->dev, "CODEC is on over suspend\n");
577 break;
578 }
579 }
580 }
581
582 for (i = 0; i < card->num_rtd; i++) {
583 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
584
585 if (card->rtd[i].dai_link->ignore_suspend)
586 continue;
587
588 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
589 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200590 }
591
Mark Brown87506542008-11-18 20:50:34 +0000592 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000593 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200594
595 return 0;
596}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000597EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200598
Andy Green6ed25972008-06-13 16:24:05 +0100599/* deferred resume work, so resume can complete before we finished
600 * setting our codec back up, which can be very slow on I2C
601 */
602static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200603{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000604 struct snd_soc_card *card =
605 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200606 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200607 int i;
608
Andy Green6ed25972008-06-13 16:24:05 +0100609 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
610 * so userspace apps are blocked from touching us
611 */
612
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100614
Mark Brown99497882010-05-07 20:24:05 +0100615 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000616 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100617
Mark Brown87506542008-11-18 20:50:34 +0000618 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000619 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200620
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 /* resume AC97 DAIs */
622 for (i = 0; i < card->num_rtd; i++) {
623 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100624
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000625 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100626 continue;
627
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000628 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
629 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200630 }
631
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200632 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633 /* If the CODEC was idle over suspend then it will have been
634 * left with bias OFF or STANDBY and suspended so we must now
635 * resume. Otherwise the suspend was suppressed.
636 */
637 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200638 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 case SND_SOC_BIAS_STANDBY:
640 case SND_SOC_BIAS_OFF:
641 codec->driver->resume(codec);
642 codec->suspended = 0;
643 break;
644 default:
645 dev_dbg(codec->dev, "CODEC was on over suspend\n");
646 break;
647 }
Mark Brown1547aba2010-05-07 21:11:40 +0100648 }
649 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200650
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000651 for (i = 0; i < card->num_rtd; i++) {
652 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100653
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000654 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100655 continue;
656
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000657 if (driver->playback.stream_name != NULL)
658 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200659 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660
661 if (driver->capture.stream_name != NULL)
662 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200663 SND_SOC_DAPM_STREAM_RESUME);
664 }
665
Mark Brown3ff3f642008-05-19 12:32:25 +0200666 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000667 for (i = 0; i < card->num_rtd; i++) {
668 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
669 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100670
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000671 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100672 continue;
673
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000674 if (drv->ops->digital_mute && dai->playback_active)
675 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200676 }
677
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000678 for (i = 0; i < card->num_rtd; i++) {
679 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
680 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100681
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000682 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100683 continue;
684
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000685 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
686 cpu_dai->driver->resume(cpu_dai);
687 if (platform->driver->resume && platform->suspended) {
688 platform->driver->resume(cpu_dai);
689 platform->suspended = 0;
690 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200691 }
692
Mark Brown87506542008-11-18 20:50:34 +0000693 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000694 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200695
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100697
698 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000699 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100700}
701
702/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000703int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100704{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000705 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600706 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200707
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800708 /* If the initialization of this soc device failed, there is no codec
709 * associated with it. Just bail out in this case.
710 */
711 if (list_empty(&card->codec_dev_list))
712 return 0;
713
Mark Brown64ab9ba2009-03-31 11:27:03 +0100714 /* AC97 devices might have other drivers hanging off them so
715 * need to resume immediately. Other drivers don't have that
716 * problem and may take a substantial amount of time to resume
717 * due to I/O costs and anti-pop so handle them out of line.
718 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000719 for (i = 0; i < card->num_rtd; i++) {
720 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600721 ac97_control |= cpu_dai->driver->ac97_control;
722 }
723 if (ac97_control) {
724 dev_dbg(dev, "Resuming AC97 immediately\n");
725 soc_resume_deferred(&card->deferred_resume_work);
726 } else {
727 dev_dbg(dev, "Scheduling resume work\n");
728 if (!schedule_work(&card->deferred_resume_work))
729 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100730 }
Andy Green6ed25972008-06-13 16:24:05 +0100731
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200732 return 0;
733}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000734EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200735#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000736#define snd_soc_suspend NULL
737#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200738#endif
739
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100740static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800741};
742
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200744{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000745 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
746 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000747 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000748 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000749 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100750 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200751
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000752 if (rtd->complete)
753 return 1;
754 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000755
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756 /* do we already have the CPU DAI for this link ? */
757 if (rtd->cpu_dai) {
758 goto find_codec;
759 }
760 /* no, then find CPU DAI from registered DAIs*/
761 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700762 if (dai_link->cpu_dai_of_node) {
763 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
764 continue;
765 } else {
766 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
767 continue;
768 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700769
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 Warren5a504962011-12-21 10:40:59 -0700784 if (dai_link->codec_of_node) {
785 if (codec->dev->of_node != dai_link->codec_of_node)
786 continue;
787 } else {
788 if (strcmp(codec->name, dai_link->codec_name))
789 continue;
790 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000791
Stephen Warren2610ab72011-12-07 13:58:27 -0700792 rtd->codec = codec;
793
794 /*
795 * CODEC found, so find CODEC DAI from registered DAIs from
796 * this CODEC
797 */
798 list_for_each_entry(codec_dai, &dai_list, list) {
799 if (codec->dev == codec_dai->dev &&
800 !strcmp(codec_dai->name,
801 dai_link->codec_dai_name)) {
802
803 rtd->codec_dai = codec_dai;
804 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000805 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000806 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700807 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
808 dai_link->codec_dai_name);
809
810 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000811 }
812 dev_dbg(card->dev, "CODEC %s not registered\n",
813 dai_link->codec_name);
814
815find_platform:
Mark Brown848dd8b2011-04-27 18:16:32 +0100816 /* do we need a platform? */
817 if (rtd->platform)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000818 goto out;
Mark Brown848dd8b2011-04-27 18:16:32 +0100819
820 /* if there's no platform we match on the empty platform */
821 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700822 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100823 platform_name = "snd-soc-dummy";
824
825 /* no, then find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700827 if (dai_link->platform_of_node) {
828 if (platform->dev->of_node !=
829 dai_link->platform_of_node)
830 continue;
831 } else {
832 if (strcmp(platform->name, platform_name))
833 continue;
834 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700835
836 rtd->platform = platform;
837 goto out;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000838 }
839
840 dev_dbg(card->dev, "platform %s not registered\n",
841 dai_link->platform_name);
842 return 0;
843
844out:
845 /* mark rtd as complete if we found all 4 of our client devices */
846 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
847 rtd->complete = 1;
848 card->num_rtd++;
849 }
850 return 1;
851}
852
Jarkko Nikula589c3562010-12-06 16:27:07 +0200853static void soc_remove_codec(struct snd_soc_codec *codec)
854{
855 int err;
856
857 if (codec->driver->remove) {
858 err = codec->driver->remove(codec);
859 if (err < 0)
860 dev_err(codec->dev,
861 "asoc: failed to remove %s: %d\n",
862 codec->name, err);
863 }
864
865 /* Make sure all DAPM widgets are freed */
866 snd_soc_dapm_free(&codec->dapm);
867
868 soc_cleanup_codec_debugfs(codec);
869 codec->probed = 0;
870 list_del(&codec->card_list);
871 module_put(codec->dev->driver->owner);
872}
873
Liam Girdwood0168bf02011-06-07 16:08:05 +0100874static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000875{
876 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
877 struct snd_soc_codec *codec = rtd->codec;
878 struct snd_soc_platform *platform = rtd->platform;
879 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
880 int err;
881
882 /* unregister the rtd device */
883 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800884 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
885 device_remove_file(rtd->dev, &dev_attr_codec_reg);
886 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000887 rtd->dev_registered = 0;
888 }
889
890 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100891 if (codec_dai && codec_dai->probed &&
892 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000893 if (codec_dai->driver->remove) {
894 err = codec_dai->driver->remove(codec_dai);
895 if (err < 0)
896 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
897 }
898 codec_dai->probed = 0;
899 list_del(&codec_dai->card_list);
900 }
901
902 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100903 if (platform && platform->probed &&
904 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000905 if (platform->driver->remove) {
906 err = platform->driver->remove(platform);
907 if (err < 0)
908 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
909 }
Liam Girdwood675c4962012-01-16 15:25:37 +0000910
911 /* Make sure all DAPM widgets are freed */
912 snd_soc_dapm_free(&platform->dapm);
913
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000914 platform->probed = 0;
915 list_del(&platform->card_list);
916 module_put(platform->dev->driver->owner);
917 }
918
919 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100920 if (codec && codec->probed &&
921 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200922 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000923
924 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100925 if (cpu_dai && cpu_dai->probed &&
926 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000927 if (cpu_dai->driver->remove) {
928 err = cpu_dai->driver->remove(cpu_dai);
929 if (err < 0)
930 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
931 }
932 cpu_dai->probed = 0;
933 list_del(&cpu_dai->card_list);
934 module_put(cpu_dai->dev->driver->owner);
935 }
936}
937
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900938static void soc_remove_dai_links(struct snd_soc_card *card)
939{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100940 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900941
Liam Girdwood0168bf02011-06-07 16:08:05 +0100942 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
943 order++) {
944 for (dai = 0; dai < card->num_rtd; dai++)
945 soc_remove_dai_link(card, dai, order);
946 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900947 card->num_rtd = 0;
948}
949
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200950static void soc_set_name_prefix(struct snd_soc_card *card,
951 struct snd_soc_codec *codec)
952{
953 int i;
954
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000955 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200956 return;
957
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000958 for (i = 0; i < card->num_configs; i++) {
959 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200960 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
961 codec->name_prefix = map->name_prefix;
962 break;
963 }
964 }
965}
966
Jarkko Nikula589c3562010-12-06 16:27:07 +0200967static int soc_probe_codec(struct snd_soc_card *card,
968 struct snd_soc_codec *codec)
969{
970 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +0000971 const struct snd_soc_codec_driver *driver = codec->driver;
Jarkko Nikula589c3562010-12-06 16:27:07 +0200972
973 codec->card = card;
974 codec->dapm.card = card;
975 soc_set_name_prefix(card, codec);
976
Jarkko Nikula70d29332011-01-27 16:24:22 +0200977 if (!try_module_get(codec->dev->driver->owner))
978 return -ENODEV;
979
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +0200980 soc_init_codec_debugfs(codec);
981
Lars-Peter Clausen77530152011-05-05 16:59:09 +0200982 if (driver->dapm_widgets)
983 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
984 driver->num_dapm_widgets);
985
Mark Brown33c5f962011-08-22 18:40:30 +0100986 codec->dapm.idle_bias_off = driver->idle_bias_off;
987
Mark Brown89b95ac02011-03-07 16:38:44 +0000988 if (driver->probe) {
989 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200990 if (ret < 0) {
991 dev_err(codec->dev,
992 "asoc: failed to probe CODEC %s: %d\n",
993 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +0200994 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +0200995 }
996 }
997
Mark Brownb7af1da2011-04-07 19:18:44 +0900998 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +0000999 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001000 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001001 if (driver->dapm_routes)
1002 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1003 driver->num_dapm_routes);
1004
Jarkko Nikula589c3562010-12-06 16:27:07 +02001005 /* mark codec as probed and add to card codec list */
1006 codec->probed = 1;
1007 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001008 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001009
Jarkko Nikula70d29332011-01-27 16:24:22 +02001010 return 0;
1011
1012err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001013 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001014 module_put(codec->dev->driver->owner);
1015
Jarkko Nikula589c3562010-12-06 16:27:07 +02001016 return ret;
1017}
1018
Liam Girdwood956245e2011-07-01 16:54:08 +01001019static int soc_probe_platform(struct snd_soc_card *card,
1020 struct snd_soc_platform *platform)
1021{
1022 int ret = 0;
1023 const struct snd_soc_platform_driver *driver = platform->driver;
1024
1025 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001026 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001027
1028 if (!try_module_get(platform->dev->driver->owner))
1029 return -ENODEV;
1030
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001031 if (driver->dapm_widgets)
1032 snd_soc_dapm_new_controls(&platform->dapm,
1033 driver->dapm_widgets, driver->num_dapm_widgets);
1034
Liam Girdwood956245e2011-07-01 16:54:08 +01001035 if (driver->probe) {
1036 ret = driver->probe(platform);
1037 if (ret < 0) {
1038 dev_err(platform->dev,
1039 "asoc: failed to probe platform %s: %d\n",
1040 platform->name, ret);
1041 goto err_probe;
1042 }
1043 }
1044
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001045 if (driver->controls)
1046 snd_soc_add_platform_controls(platform, driver->controls,
1047 driver->num_controls);
1048 if (driver->dapm_routes)
1049 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1050 driver->num_dapm_routes);
1051
Liam Girdwood956245e2011-07-01 16:54:08 +01001052 /* mark platform as probed and add to card platform list */
1053 platform->probed = 1;
1054 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001055 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001056
1057 return 0;
1058
1059err_probe:
1060 module_put(platform->dev->driver->owner);
1061
1062 return ret;
1063}
1064
Mark Brown36ae1a92012-01-06 17:12:45 -08001065static void rtd_release(struct device *dev)
1066{
1067 kfree(dev);
1068}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001069
Jarkko Nikula589c3562010-12-06 16:27:07 +02001070static int soc_post_component_init(struct snd_soc_card *card,
1071 struct snd_soc_codec *codec,
1072 int num, int dailess)
1073{
1074 struct snd_soc_dai_link *dai_link = NULL;
1075 struct snd_soc_aux_dev *aux_dev = NULL;
1076 struct snd_soc_pcm_runtime *rtd;
1077 const char *temp, *name;
1078 int ret = 0;
1079
1080 if (!dailess) {
1081 dai_link = &card->dai_link[num];
1082 rtd = &card->rtd[num];
1083 name = dai_link->name;
1084 } else {
1085 aux_dev = &card->aux_dev[num];
1086 rtd = &card->rtd_aux[num];
1087 name = aux_dev->name;
1088 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001089 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001090
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001091 /* Make sure all DAPM widgets are instantiated */
1092 snd_soc_dapm_new_widgets(&codec->dapm);
1093
Jarkko Nikula589c3562010-12-06 16:27:07 +02001094 /* machine controls, routes and widgets are not prefixed */
1095 temp = codec->name_prefix;
1096 codec->name_prefix = NULL;
1097
1098 /* do machine specific initialization */
1099 if (!dailess && dai_link->init)
1100 ret = dai_link->init(rtd);
1101 else if (dailess && aux_dev->init)
1102 ret = aux_dev->init(&codec->dapm);
1103 if (ret < 0) {
1104 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1105 return ret;
1106 }
1107 codec->name_prefix = temp;
1108
Jarkko Nikula589c3562010-12-06 16:27:07 +02001109 /* register the rtd device */
1110 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001111
1112 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1113 if (!rtd->dev)
1114 return -ENOMEM;
1115 device_initialize(rtd->dev);
1116 rtd->dev->parent = card->dev;
1117 rtd->dev->release = rtd_release;
1118 rtd->dev->init_name = name;
1119 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001120 mutex_init(&rtd->pcm_mutex);
Mark Brown36ae1a92012-01-06 17:12:45 -08001121 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001122 if (ret < 0) {
1123 dev_err(card->dev,
1124 "asoc: failed to register runtime device: %d\n", ret);
1125 return ret;
1126 }
1127 rtd->dev_registered = 1;
1128
1129 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001130 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001131 if (ret < 0)
1132 dev_err(codec->dev,
1133 "asoc: failed to add codec dapm sysfs entries: %d\n",
1134 ret);
1135
1136 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001137 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001138 if (ret < 0)
1139 dev_err(codec->dev,
1140 "asoc: failed to add codec sysfs files: %d\n", ret);
1141
1142 return 0;
1143}
1144
Liam Girdwood0168bf02011-06-07 16:08:05 +01001145static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001146{
1147 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1148 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1149 struct snd_soc_codec *codec = rtd->codec;
1150 struct snd_soc_platform *platform = rtd->platform;
1151 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1152 int ret;
1153
Liam Girdwood0168bf02011-06-07 16:08:05 +01001154 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1155 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001156
1157 /* config components */
1158 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001159 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001160 codec_dai->card = card;
1161 cpu_dai->card = card;
1162
1163 /* set default power off timeout */
1164 rtd->pmdown_time = pmdown_time;
1165
1166 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001167 if (!cpu_dai->probed &&
1168 cpu_dai->driver->probe_order == order) {
Liam Girdwood61b61e32011-05-24 13:57:43 +01001169 if (!try_module_get(cpu_dai->dev->driver->owner))
1170 return -ENODEV;
1171
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001172 if (cpu_dai->driver->probe) {
1173 ret = cpu_dai->driver->probe(cpu_dai);
1174 if (ret < 0) {
1175 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1176 cpu_dai->name);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001177 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001178 return ret;
1179 }
1180 }
1181 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001182 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001183 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1184 }
1185
1186 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001187 if (!codec->probed &&
1188 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001189 ret = soc_probe_codec(card, codec);
1190 if (ret < 0)
1191 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001192 }
1193
1194 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001195 if (!platform->probed &&
1196 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001197 ret = soc_probe_platform(card, platform);
1198 if (ret < 0)
1199 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001200 }
1201
1202 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001203 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001204 if (codec_dai->driver->probe) {
1205 ret = codec_dai->driver->probe(codec_dai);
1206 if (ret < 0) {
1207 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1208 codec_dai->name);
1209 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001210 }
1211 }
1212
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001213 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001214 codec_dai->probed = 1;
1215 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001216 }
1217
Liam Girdwood0168bf02011-06-07 16:08:05 +01001218 /* complete DAI probe during last probe */
1219 if (order != SND_SOC_COMP_ORDER_LAST)
1220 return 0;
1221
Jarkko Nikula589c3562010-12-06 16:27:07 +02001222 ret = soc_post_component_init(card, codec, num, 0);
1223 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001224 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001225
Mark Brown36ae1a92012-01-06 17:12:45 -08001226 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001227 if (ret < 0)
1228 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1229
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001230 /* create the pcm */
1231 ret = soc_new_pcm(rtd, num);
1232 if (ret < 0) {
1233 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1234 return ret;
1235 }
1236
1237 /* add platform data for AC97 devices */
1238 if (rtd->codec_dai->driver->ac97_control)
1239 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1240
1241 return 0;
1242}
1243
1244#ifdef CONFIG_SND_SOC_AC97_BUS
1245static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1246{
1247 int ret;
1248
1249 /* Only instantiate AC97 if not already done by the adaptor
1250 * for the generic AC97 subsystem.
1251 */
1252 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001253 /*
1254 * It is possible that the AC97 device is already registered to
1255 * the device subsystem. This happens when the device is created
1256 * via snd_ac97_mixer(). Currently only SoC codec that does so
1257 * is the generic AC97 glue but others migh emerge.
1258 *
1259 * In those cases we don't try to register the device again.
1260 */
1261 if (!rtd->codec->ac97_created)
1262 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001263
1264 ret = soc_ac97_dev_register(rtd->codec);
1265 if (ret < 0) {
1266 printk(KERN_ERR "asoc: AC97 device register failed\n");
1267 return ret;
1268 }
1269
1270 rtd->codec->ac97_registered = 1;
1271 }
1272 return 0;
1273}
1274
1275static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1276{
1277 if (codec->ac97_registered) {
1278 soc_ac97_dev_unregister(codec);
1279 codec->ac97_registered = 0;
1280 }
1281}
1282#endif
1283
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001284static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1285{
1286 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001287 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001288 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001289
1290 /* find CODEC from registered CODECs*/
1291 list_for_each_entry(codec, &codec_list, list) {
1292 if (!strcmp(codec->name, aux_dev->codec_name)) {
1293 if (codec->probed) {
1294 dev_err(codec->dev,
1295 "asoc: codec already probed");
1296 ret = -EBUSY;
1297 goto out;
1298 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001299 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001300 }
1301 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001302 /* codec not found */
1303 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1304 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001305
Jarkko Nikula676ad982010-12-03 09:18:22 +02001306found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001307 ret = soc_probe_codec(card, codec);
1308 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001309 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001310
Jarkko Nikula589c3562010-12-06 16:27:07 +02001311 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001312
1313out:
1314 return ret;
1315}
1316
1317static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1318{
1319 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1320 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001321
1322 /* unregister the rtd device */
1323 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001324 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1325 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001326 rtd->dev_registered = 0;
1327 }
1328
Jarkko Nikula589c3562010-12-06 16:27:07 +02001329 if (codec && codec->probed)
1330 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001331}
1332
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001333static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1334 enum snd_soc_compress_type compress_type)
1335{
1336 int ret;
1337
1338 if (codec->cache_init)
1339 return 0;
1340
1341 /* override the compress_type if necessary */
1342 if (compress_type && codec->compress_type != compress_type)
1343 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001344 ret = snd_soc_cache_init(codec);
1345 if (ret < 0) {
1346 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1347 ret);
1348 return ret;
1349 }
1350 codec->cache_init = 1;
1351 return 0;
1352}
1353
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001354static void snd_soc_instantiate_card(struct snd_soc_card *card)
1355{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001356 struct snd_soc_codec *codec;
1357 struct snd_soc_codec_conf *codec_conf;
1358 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001359 struct snd_soc_dai_link *dai_link;
Liam Girdwood0168bf02011-06-07 16:08:05 +01001360 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001361
1362 mutex_lock(&card->mutex);
1363
1364 if (card->instantiated) {
1365 mutex_unlock(&card->mutex);
1366 return;
1367 }
1368
1369 /* bind DAIs */
1370 for (i = 0; i < card->num_links; i++)
1371 soc_bind_dai_link(card, i);
1372
1373 /* bind completed ? */
1374 if (card->num_rtd != card->num_links) {
1375 mutex_unlock(&card->mutex);
1376 return;
1377 }
1378
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001379 /* initialize the register cache for each available codec */
1380 list_for_each_entry(codec, &codec_list, list) {
1381 if (codec->cache_init)
1382 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001383 /* by default we don't override the compress_type */
1384 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001385 /* check to see if we need to override the compress_type */
1386 for (i = 0; i < card->num_configs; ++i) {
1387 codec_conf = &card->codec_conf[i];
1388 if (!strcmp(codec->name, codec_conf->dev_name)) {
1389 compress_type = codec_conf->compress_type;
1390 if (compress_type && compress_type
1391 != codec->compress_type)
1392 break;
1393 }
1394 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001395 ret = snd_soc_init_codec_cache(codec, compress_type);
1396 if (ret < 0) {
1397 mutex_unlock(&card->mutex);
1398 return;
1399 }
1400 }
1401
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001402 /* card bind complete so register a sound card */
1403 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1404 card->owner, 0, &card->snd_card);
1405 if (ret < 0) {
1406 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1407 card->name);
1408 mutex_unlock(&card->mutex);
1409 return;
1410 }
1411 card->snd_card->dev = card->dev;
1412
Mark Browne37a4972011-03-02 18:21:57 +00001413 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1414 card->dapm.dev = card->dev;
1415 card->dapm.card = card;
1416 list_add(&card->dapm.list, &card->dapm_list);
1417
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001418#ifdef CONFIG_DEBUG_FS
1419 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1420#endif
1421
Mark Brown88ee1c62011-02-02 10:43:26 +00001422#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001423 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001424 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001425#endif
Andy Green6ed25972008-06-13 16:24:05 +01001426
Mark Brown9a841eb2011-04-12 17:51:37 -07001427 if (card->dapm_widgets)
1428 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1429 card->num_dapm_widgets);
1430
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001431 /* initialise the sound card only once */
1432 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001433 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001434 if (ret < 0)
1435 goto card_probe_error;
1436 }
1437
Liam Girdwood0168bf02011-06-07 16:08:05 +01001438 /* early DAI link probe */
1439 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1440 order++) {
1441 for (i = 0; i < card->num_links; i++) {
1442 ret = soc_probe_dai_link(card, i, order);
1443 if (ret < 0) {
1444 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001445 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001446 goto probe_dai_err;
1447 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001448 }
1449 }
1450
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001451 for (i = 0; i < card->num_aux_devs; i++) {
1452 ret = soc_probe_aux_dev(card, i);
1453 if (ret < 0) {
1454 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1455 card->name, ret);
1456 goto probe_aux_dev_err;
1457 }
1458 }
1459
Mark Brownb7af1da2011-04-07 19:18:44 +09001460 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001461 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001462
Mark Brownb8ad29d2011-03-02 18:35:51 +00001463 if (card->dapm_routes)
1464 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1465 card->num_dapm_routes);
1466
Mark Brownb90d2f92011-10-10 13:38:06 +01001467 snd_soc_dapm_new_widgets(&card->dapm);
1468
Mark Brown75d9ac42011-09-27 16:41:01 +01001469 for (i = 0; i < card->num_links; i++) {
1470 dai_link = &card->dai_link[i];
1471
1472 if (dai_link->dai_fmt) {
1473 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1474 dai_link->dai_fmt);
1475 if (ret != 0)
1476 dev_warn(card->rtd[i].codec_dai->dev,
1477 "Failed to set DAI format: %d\n",
1478 ret);
1479
1480 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1481 dai_link->dai_fmt);
1482 if (ret != 0)
1483 dev_warn(card->rtd[i].cpu_dai->dev,
1484 "Failed to set DAI format: %d\n",
1485 ret);
1486 }
1487 }
1488
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001489 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001490 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001491 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1492 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001493 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1494 "%s", card->driver_name ? card->driver_name : card->name);
1495 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1496 switch (card->snd_card->driver[i]) {
1497 case '_':
1498 case '-':
1499 case '\0':
1500 break;
1501 default:
1502 if (!isalnum(card->snd_card->driver[i]))
1503 card->snd_card->driver[i] = '_';
1504 break;
1505 }
1506 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001507
Mark Brown28e9ad92011-03-02 18:36:34 +00001508 if (card->late_probe) {
1509 ret = card->late_probe(card);
1510 if (ret < 0) {
1511 dev_err(card->dev, "%s late_probe() failed: %d\n",
1512 card->name, ret);
1513 goto probe_aux_dev_err;
1514 }
1515 }
1516
Mark Brown2dc00212011-10-08 13:59:44 +01001517 snd_soc_dapm_new_widgets(&card->dapm);
1518
Stephen Warren16332812011-11-23 12:42:04 -07001519 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001520 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001521 snd_soc_dapm_auto_nc_codec_pins(codec);
1522
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001523 ret = snd_card_register(card->snd_card);
1524 if (ret < 0) {
1525 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
Axel Lin6b3ed782010-12-07 16:12:29 +08001526 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001527 }
1528
1529#ifdef CONFIG_SND_SOC_AC97_BUS
1530 /* register any AC97 codecs */
1531 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001532 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1533 if (ret < 0) {
1534 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1535 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001536 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001537 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001538 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001539 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001540#endif
1541
Mark Brown435c5e22008-12-04 15:32:53 +00001542 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001543 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001544 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001545 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001546
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001547probe_aux_dev_err:
1548 for (i = 0; i < card->num_aux_devs; i++)
1549 soc_remove_aux_dev(card, i);
1550
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001551probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001552 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001553
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001554card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001555 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001556 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001557
1558 snd_card_free(card->snd_card);
1559
1560 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001561}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001562
Mark Brown435c5e22008-12-04 15:32:53 +00001563/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001564 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001565 * client_mutex.
1566 */
1567static void snd_soc_instantiate_cards(void)
1568{
1569 struct snd_soc_card *card;
1570 list_for_each_entry(card, &card_list, list)
1571 snd_soc_instantiate_card(card);
1572}
1573
1574/* probes a new socdev */
1575static int soc_probe(struct platform_device *pdev)
1576{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001577 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001578 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001579
Vinod Koul70a7ca32011-01-14 19:22:48 +05301580 /*
1581 * no card, so machine driver should be registering card
1582 * we should not be here in that case so ret error
1583 */
1584 if (!card)
1585 return -EINVAL;
1586
Mark Brown435c5e22008-12-04 15:32:53 +00001587 /* Bodge while we unpick instantiation */
1588 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001589
Mark Brown435c5e22008-12-04 15:32:53 +00001590 ret = snd_soc_register_card(card);
1591 if (ret != 0) {
1592 dev_err(&pdev->dev, "Failed to register card\n");
1593 return ret;
1594 }
1595
1596 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001597}
1598
Vinod Koulb0e26482011-01-13 22:48:02 +05301599static int soc_cleanup_card_resources(struct snd_soc_card *card)
1600{
Vinod Koulb0e26482011-01-13 22:48:02 +05301601 int i;
1602
1603 /* make sure any delayed work runs */
1604 for (i = 0; i < card->num_rtd; i++) {
1605 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1606 flush_delayed_work_sync(&rtd->delayed_work);
1607 }
1608
1609 /* remove auxiliary devices */
1610 for (i = 0; i < card->num_aux_devs; i++)
1611 soc_remove_aux_dev(card, i);
1612
1613 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001614 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301615
1616 soc_cleanup_card_debugfs(card);
1617
1618 /* remove the card */
1619 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001620 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301621
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001622 snd_soc_dapm_free(&card->dapm);
1623
Vinod Koulb0e26482011-01-13 22:48:02 +05301624 kfree(card->rtd);
1625 snd_card_free(card->snd_card);
1626 return 0;
1627
1628}
1629
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001630/* removes a socdev */
1631static int soc_remove(struct platform_device *pdev)
1632{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001633 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001634
Mark Brownc5af3a22008-11-28 13:29:45 +00001635 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001636 return 0;
1637}
1638
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001639int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001640{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001641 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001642 int i;
Mark Brown51737472009-06-22 13:16:51 +01001643
1644 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001645 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001646
1647 /* Flush out pmdown_time work - we actually do want to run it
1648 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001649 for (i = 0; i < card->num_rtd; i++) {
1650 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001651 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001652 }
Mark Brown51737472009-06-22 13:16:51 +01001653
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001654 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001655
1656 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001657}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001658EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001659
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001660const struct dev_pm_ops snd_soc_pm_ops = {
Mark Brown5aa44b12012-01-31 15:30:28 +00001661 SET_SYSTEM_SLEEP_PM_OPS(snd_soc_suspend, snd_soc_resume)
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001662 .poweroff = snd_soc_poweroff,
Mark Brown416356f2009-06-30 19:05:15 +01001663};
Stephen Warrendeb26072011-04-05 19:35:30 -06001664EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001665
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001666/* ASoC platform driver */
1667static struct platform_driver soc_driver = {
1668 .driver = {
1669 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001670 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001671 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001672 },
1673 .probe = soc_probe,
1674 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001675};
1676
Mark Brown096e49d2009-07-05 15:12:22 +01001677/**
1678 * snd_soc_codec_volatile_register: Report if a register is volatile.
1679 *
1680 * @codec: CODEC to query.
1681 * @reg: Register to query.
1682 *
1683 * Boolean function indiciating if a CODEC register is volatile.
1684 */
Mark Brown181e0552011-01-24 14:05:25 +00001685int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1686 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001687{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001688 if (codec->volatile_register)
1689 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001690 else
1691 return 0;
1692}
1693EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1694
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001695/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001696 * snd_soc_codec_readable_register: Report if a register is readable.
1697 *
1698 * @codec: CODEC to query.
1699 * @reg: Register to query.
1700 *
1701 * Boolean function indicating if a CODEC register is readable.
1702 */
1703int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1704 unsigned int reg)
1705{
1706 if (codec->readable_register)
1707 return codec->readable_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_readable_register);
1712
1713/**
1714 * snd_soc_codec_writable_register: Report if a register is writable.
1715 *
1716 * @codec: CODEC to query.
1717 * @reg: Register to query.
1718 *
1719 * Boolean function indicating if a CODEC register is writable.
1720 */
1721int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1722 unsigned int reg)
1723{
1724 if (codec->writable_register)
1725 return codec->writable_register(codec, reg);
1726 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001727 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001728}
1729EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1730
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001731int snd_soc_platform_read(struct snd_soc_platform *platform,
1732 unsigned int reg)
1733{
1734 unsigned int ret;
1735
1736 if (!platform->driver->read) {
1737 dev_err(platform->dev, "platform has no read back\n");
1738 return -1;
1739 }
1740
1741 ret = platform->driver->read(platform, reg);
1742 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001743 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001744
1745 return ret;
1746}
1747EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1748
1749int snd_soc_platform_write(struct snd_soc_platform *platform,
1750 unsigned int reg, unsigned int val)
1751{
1752 if (!platform->driver->write) {
1753 dev_err(platform->dev, "platform has no write back\n");
1754 return -1;
1755 }
1756
1757 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001758 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001759 return platform->driver->write(platform, reg, val);
1760}
1761EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1762
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001763/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001764 * snd_soc_new_ac97_codec - initailise AC97 device
1765 * @codec: audio codec
1766 * @ops: AC97 bus operations
1767 * @num: AC97 codec number
1768 *
1769 * Initialises AC97 codec resources for use by ad-hoc devices only.
1770 */
1771int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1772 struct snd_ac97_bus_ops *ops, int num)
1773{
1774 mutex_lock(&codec->mutex);
1775
1776 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1777 if (codec->ac97 == NULL) {
1778 mutex_unlock(&codec->mutex);
1779 return -ENOMEM;
1780 }
1781
1782 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1783 if (codec->ac97->bus == NULL) {
1784 kfree(codec->ac97);
1785 codec->ac97 = NULL;
1786 mutex_unlock(&codec->mutex);
1787 return -ENOMEM;
1788 }
1789
1790 codec->ac97->bus->ops = ops;
1791 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001792
1793 /*
1794 * Mark the AC97 device to be created by us. This way we ensure that the
1795 * device will be registered with the device subsystem later on.
1796 */
1797 codec->ac97_created = 1;
1798
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001799 mutex_unlock(&codec->mutex);
1800 return 0;
1801}
1802EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1803
1804/**
1805 * snd_soc_free_ac97_codec - free AC97 codec device
1806 * @codec: audio codec
1807 *
1808 * Frees AC97 codec device resources.
1809 */
1810void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1811{
1812 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001813#ifdef CONFIG_SND_SOC_AC97_BUS
1814 soc_unregister_ac97_dai_link(codec);
1815#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001816 kfree(codec->ac97->bus);
1817 kfree(codec->ac97);
1818 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001819 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001820 mutex_unlock(&codec->mutex);
1821}
1822EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1823
Mark Brownc3753702010-11-01 15:41:57 -04001824unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1825{
1826 unsigned int ret;
1827
Mark Brownc3acec22010-12-02 16:15:29 +00001828 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04001829 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04001830 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04001831
1832 return ret;
1833}
1834EXPORT_SYMBOL_GPL(snd_soc_read);
1835
1836unsigned int snd_soc_write(struct snd_soc_codec *codec,
1837 unsigned int reg, unsigned int val)
1838{
1839 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04001840 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00001841 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04001842}
1843EXPORT_SYMBOL_GPL(snd_soc_write);
1844
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00001845unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1846 unsigned int reg, const void *data, size_t len)
1847{
1848 return codec->bulk_write_raw(codec, reg, data, len);
1849}
1850EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1851
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001852/**
1853 * snd_soc_update_bits - update codec register bits
1854 * @codec: audio codec
1855 * @reg: codec register
1856 * @mask: register mask
1857 * @value: new value
1858 *
1859 * Writes new register value.
1860 *
Timur Tabi180c3292011-01-10 15:58:13 -06001861 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001862 */
1863int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001864 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001865{
Mark Brown8a713da2011-12-03 12:33:55 +00001866 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001867 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06001868 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001869
Mark Brown8a713da2011-12-03 12:33:55 +00001870 if (codec->using_regmap) {
1871 ret = regmap_update_bits_check(codec->control_data, reg,
1872 mask, value, &change);
1873 } else {
1874 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06001875 if (ret < 0)
1876 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00001877
1878 old = ret;
1879 new = (old & ~mask) | (value & mask);
1880 change = old != new;
1881 if (change)
1882 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06001883 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001884
Mark Brown8a713da2011-12-03 12:33:55 +00001885 if (ret < 0)
1886 return ret;
1887
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001888 return change;
1889}
1890EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1891
1892/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001893 * snd_soc_update_bits_locked - update codec register bits
1894 * @codec: audio codec
1895 * @reg: codec register
1896 * @mask: register mask
1897 * @value: new value
1898 *
1899 * Writes new register value, and takes the codec mutex.
1900 *
1901 * Returns 1 for change else 0.
1902 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001903int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1904 unsigned short reg, unsigned int mask,
1905 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001906{
1907 int change;
1908
1909 mutex_lock(&codec->mutex);
1910 change = snd_soc_update_bits(codec, reg, mask, value);
1911 mutex_unlock(&codec->mutex);
1912
1913 return change;
1914}
Mark Browndd1b3d52009-12-04 14:22:03 +00001915EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001916
1917/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001918 * snd_soc_test_bits - test register for change
1919 * @codec: audio codec
1920 * @reg: codec register
1921 * @mask: register mask
1922 * @value: new value
1923 *
1924 * Tests a register with a new value and checks if the new value is
1925 * different from the old value.
1926 *
1927 * Returns 1 for change else 0.
1928 */
1929int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001930 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001931{
1932 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001933 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001934
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001935 old = snd_soc_read(codec, reg);
1936 new = (old & ~mask) | value;
1937 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001938
1939 return change;
1940}
1941EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1942
1943/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001944 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1945 * @substream: the pcm substream
1946 * @hw: the hardware parameters
1947 *
1948 * Sets the substream runtime hardware parameters.
1949 */
1950int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1951 const struct snd_pcm_hardware *hw)
1952{
1953 struct snd_pcm_runtime *runtime = substream->runtime;
1954 runtime->hw.info = hw->info;
1955 runtime->hw.formats = hw->formats;
1956 runtime->hw.period_bytes_min = hw->period_bytes_min;
1957 runtime->hw.period_bytes_max = hw->period_bytes_max;
1958 runtime->hw.periods_min = hw->periods_min;
1959 runtime->hw.periods_max = hw->periods_max;
1960 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1961 runtime->hw.fifo_size = hw->fifo_size;
1962 return 0;
1963}
1964EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1965
1966/**
1967 * snd_soc_cnew - create new control
1968 * @_template: control template
1969 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001970 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00001971 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001972 *
1973 * Create a new mixer control from a template control.
1974 *
1975 * Returns 0 for success, else error.
1976 */
1977struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brownefb7ac32011-03-08 17:23:24 +00001978 void *data, char *long_name,
1979 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001980{
1981 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00001982 struct snd_kcontrol *kcontrol;
1983 char *name = NULL;
1984 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001985
1986 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001987 template.index = 0;
1988
Mark Brownefb7ac32011-03-08 17:23:24 +00001989 if (!long_name)
1990 long_name = template.name;
1991
1992 if (prefix) {
1993 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08001994 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00001995 if (!name)
1996 return NULL;
1997
1998 snprintf(name, name_len, "%s %s", prefix, long_name);
1999
2000 template.name = name;
2001 } else {
2002 template.name = long_name;
2003 }
2004
2005 kcontrol = snd_ctl_new1(&template, data);
2006
2007 kfree(name);
2008
2009 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002010}
2011EXPORT_SYMBOL_GPL(snd_soc_cnew);
2012
Liam Girdwood022658b2012-02-03 17:43:09 +00002013static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2014 const struct snd_kcontrol_new *controls, int num_controls,
2015 const char *prefix, void *data)
2016{
2017 int err, i;
2018
2019 for (i = 0; i < num_controls; i++) {
2020 const struct snd_kcontrol_new *control = &controls[i];
2021 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2022 control->name, prefix));
2023 if (err < 0) {
2024 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2025 return err;
2026 }
2027 }
2028
2029 return 0;
2030}
2031
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002032/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002033 * snd_soc_add_codec_controls - add an array of controls to a codec.
2034 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002035 * duplicating this code.
2036 *
2037 * @codec: codec to add controls to
2038 * @controls: array of controls to add
2039 * @num_controls: number of elements in the array
2040 *
2041 * Return 0 for success, else error.
2042 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002043int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002044 const struct snd_kcontrol_new *controls, int num_controls)
2045{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002046 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002047
Liam Girdwood022658b2012-02-03 17:43:09 +00002048 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2049 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002050}
Liam Girdwood022658b2012-02-03 17:43:09 +00002051EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002052
2053/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002054 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002055 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002056 *
2057 * @platform: platform to add controls to
2058 * @controls: array of controls to add
2059 * @num_controls: number of elements in the array
2060 *
2061 * Return 0 for success, else error.
2062 */
2063int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2064 const struct snd_kcontrol_new *controls, int num_controls)
2065{
2066 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002067
Liam Girdwood022658b2012-02-03 17:43:09 +00002068 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2069 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002070}
2071EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2072
2073/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002074 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2075 * Convenience function to add a list of controls.
2076 *
2077 * @soc_card: SoC card to add controls to
2078 * @controls: array of controls to add
2079 * @num_controls: number of elements in the array
2080 *
2081 * Return 0 for success, else error.
2082 */
2083int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2084 const struct snd_kcontrol_new *controls, int num_controls)
2085{
2086 struct snd_card *card = soc_card->snd_card;
2087
2088 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2089 NULL, soc_card);
2090}
2091EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2092
2093/**
2094 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2095 * Convienience function to add a list of controls.
2096 *
2097 * @dai: DAI to add controls to
2098 * @controls: array of controls to add
2099 * @num_controls: number of elements in the array
2100 *
2101 * Return 0 for success, else error.
2102 */
2103int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2104 const struct snd_kcontrol_new *controls, int num_controls)
2105{
2106 struct snd_card *card = dai->card->snd_card;
2107
2108 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2109 NULL, dai);
2110}
2111EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2112
2113/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002114 * snd_soc_info_enum_double - enumerated double mixer info callback
2115 * @kcontrol: mixer control
2116 * @uinfo: control element information
2117 *
2118 * Callback to provide information about a double enumerated
2119 * mixer control.
2120 *
2121 * Returns 0 for success.
2122 */
2123int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2124 struct snd_ctl_elem_info *uinfo)
2125{
2126 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2127
2128 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2129 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002130 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002131
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002132 if (uinfo->value.enumerated.item > e->max - 1)
2133 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002134 strcpy(uinfo->value.enumerated.name,
2135 e->texts[uinfo->value.enumerated.item]);
2136 return 0;
2137}
2138EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2139
2140/**
2141 * snd_soc_get_enum_double - enumerated double mixer get callback
2142 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002143 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002144 *
2145 * Callback to get the value of a double enumerated mixer.
2146 *
2147 * Returns 0 for success.
2148 */
2149int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2150 struct snd_ctl_elem_value *ucontrol)
2151{
2152 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2153 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002154 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002155
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002156 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002157 ;
2158 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002159 ucontrol->value.enumerated.item[0]
2160 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002161 if (e->shift_l != e->shift_r)
2162 ucontrol->value.enumerated.item[1] =
2163 (val >> e->shift_r) & (bitmask - 1);
2164
2165 return 0;
2166}
2167EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2168
2169/**
2170 * snd_soc_put_enum_double - enumerated double mixer put callback
2171 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002172 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002173 *
2174 * Callback to set the value of a double enumerated mixer.
2175 *
2176 * Returns 0 for success.
2177 */
2178int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2179 struct snd_ctl_elem_value *ucontrol)
2180{
2181 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2182 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002183 unsigned int val;
2184 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002185
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002186 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002187 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002188 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002189 return -EINVAL;
2190 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2191 mask = (bitmask - 1) << e->shift_l;
2192 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002193 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002194 return -EINVAL;
2195 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2196 mask |= (bitmask - 1) << e->shift_r;
2197 }
2198
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002199 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002200}
2201EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2202
2203/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002204 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2205 * @kcontrol: mixer control
2206 * @ucontrol: control element information
2207 *
2208 * Callback to get the value of a double semi enumerated mixer.
2209 *
2210 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2211 * used for handling bitfield coded enumeration for example.
2212 *
2213 * Returns 0 for success.
2214 */
2215int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2216 struct snd_ctl_elem_value *ucontrol)
2217{
2218 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002219 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002220 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002221
2222 reg_val = snd_soc_read(codec, e->reg);
2223 val = (reg_val >> e->shift_l) & e->mask;
2224 for (mux = 0; mux < e->max; mux++) {
2225 if (val == e->values[mux])
2226 break;
2227 }
2228 ucontrol->value.enumerated.item[0] = mux;
2229 if (e->shift_l != e->shift_r) {
2230 val = (reg_val >> e->shift_r) & e->mask;
2231 for (mux = 0; mux < e->max; mux++) {
2232 if (val == e->values[mux])
2233 break;
2234 }
2235 ucontrol->value.enumerated.item[1] = mux;
2236 }
2237
2238 return 0;
2239}
2240EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2241
2242/**
2243 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2244 * @kcontrol: mixer control
2245 * @ucontrol: control element information
2246 *
2247 * Callback to set the value of a double semi enumerated mixer.
2248 *
2249 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2250 * used for handling bitfield coded enumeration for example.
2251 *
2252 * Returns 0 for success.
2253 */
2254int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2255 struct snd_ctl_elem_value *ucontrol)
2256{
2257 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002258 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002259 unsigned int val;
2260 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002261
2262 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2263 return -EINVAL;
2264 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2265 mask = e->mask << e->shift_l;
2266 if (e->shift_l != e->shift_r) {
2267 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2268 return -EINVAL;
2269 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2270 mask |= e->mask << e->shift_r;
2271 }
2272
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002273 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002274}
2275EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2276
2277/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002278 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2279 * @kcontrol: mixer control
2280 * @uinfo: control element information
2281 *
2282 * Callback to provide information about an external enumerated
2283 * single mixer.
2284 *
2285 * Returns 0 for success.
2286 */
2287int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2288 struct snd_ctl_elem_info *uinfo)
2289{
2290 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2291
2292 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2293 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002294 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002295
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002296 if (uinfo->value.enumerated.item > e->max - 1)
2297 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002298 strcpy(uinfo->value.enumerated.name,
2299 e->texts[uinfo->value.enumerated.item]);
2300 return 0;
2301}
2302EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2303
2304/**
2305 * snd_soc_info_volsw_ext - external single mixer info callback
2306 * @kcontrol: mixer control
2307 * @uinfo: control element information
2308 *
2309 * Callback to provide information about a single external mixer control.
2310 *
2311 * Returns 0 for success.
2312 */
2313int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2314 struct snd_ctl_elem_info *uinfo)
2315{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002316 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002317
Mark Brownfd5dfad2009-04-15 21:37:46 +01002318 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002319 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2320 else
2321 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2322
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002323 uinfo->count = 1;
2324 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002325 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002326 return 0;
2327}
2328EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2329
2330/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002331 * snd_soc_info_volsw - single mixer info callback
2332 * @kcontrol: mixer control
2333 * @uinfo: control element information
2334 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002335 * Callback to provide information about a single mixer control, or a double
2336 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002337 *
2338 * Returns 0 for success.
2339 */
2340int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2341 struct snd_ctl_elem_info *uinfo)
2342{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002343 struct soc_mixer_control *mc =
2344 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002345 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002346
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002347 if (!mc->platform_max)
2348 mc->platform_max = mc->max;
2349 platform_max = mc->platform_max;
2350
2351 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002352 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2353 else
2354 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2355
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002356 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002357 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002358 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002359 return 0;
2360}
2361EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2362
2363/**
2364 * snd_soc_get_volsw - single mixer get callback
2365 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002366 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002367 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002368 * Callback to get the value of a single mixer control, or a double mixer
2369 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002370 *
2371 * Returns 0 for success.
2372 */
2373int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2374 struct snd_ctl_elem_value *ucontrol)
2375{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002376 struct soc_mixer_control *mc =
2377 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002378 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002379 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002380 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002381 unsigned int shift = mc->shift;
2382 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002383 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002384 unsigned int mask = (1 << fls(max)) - 1;
2385 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002386
2387 ucontrol->value.integer.value[0] =
2388 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002389 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002390 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002391 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002392
2393 if (snd_soc_volsw_is_stereo(mc)) {
2394 if (reg == reg2)
2395 ucontrol->value.integer.value[1] =
2396 (snd_soc_read(codec, reg) >> rshift) & mask;
2397 else
2398 ucontrol->value.integer.value[1] =
2399 (snd_soc_read(codec, reg2) >> shift) & mask;
2400 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002401 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002402 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002403 }
2404
2405 return 0;
2406}
2407EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2408
2409/**
2410 * snd_soc_put_volsw - single mixer put callback
2411 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002412 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002413 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002414 * Callback to set the value of a single mixer control, or a double mixer
2415 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002416 *
2417 * Returns 0 for success.
2418 */
2419int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2420 struct snd_ctl_elem_value *ucontrol)
2421{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002422 struct soc_mixer_control *mc =
2423 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002424 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002425 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002426 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002427 unsigned int shift = mc->shift;
2428 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002429 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002430 unsigned int mask = (1 << fls(max)) - 1;
2431 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002432 int err;
2433 bool type_2r = 0;
2434 unsigned int val2 = 0;
2435 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002436
2437 val = (ucontrol->value.integer.value[0] & mask);
2438 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002439 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002440 val_mask = mask << shift;
2441 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002442 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002443 val2 = (ucontrol->value.integer.value[1] & mask);
2444 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002445 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002446 if (reg == reg2) {
2447 val_mask |= mask << rshift;
2448 val |= val2 << rshift;
2449 } else {
2450 val2 = val2 << shift;
2451 type_2r = 1;
2452 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002453 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002454 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002455 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002456 return err;
2457
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002458 if (type_2r)
2459 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2460
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002461 return err;
2462}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002463EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002464
Mark Browne13ac2e2008-05-28 17:58:05 +01002465/**
2466 * snd_soc_info_volsw_s8 - signed mixer info callback
2467 * @kcontrol: mixer control
2468 * @uinfo: control element information
2469 *
2470 * Callback to provide information about a signed mixer control.
2471 *
2472 * Returns 0 for success.
2473 */
2474int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2475 struct snd_ctl_elem_info *uinfo)
2476{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002477 struct soc_mixer_control *mc =
2478 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002479 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002480 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002481
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002482 if (!mc->platform_max)
2483 mc->platform_max = mc->max;
2484 platform_max = mc->platform_max;
2485
Mark Browne13ac2e2008-05-28 17:58:05 +01002486 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2487 uinfo->count = 2;
2488 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002489 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002490 return 0;
2491}
2492EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2493
2494/**
2495 * snd_soc_get_volsw_s8 - signed mixer get callback
2496 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002497 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002498 *
2499 * Callback to get the value of a signed mixer control.
2500 *
2501 * Returns 0 for success.
2502 */
2503int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2504 struct snd_ctl_elem_value *ucontrol)
2505{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002506 struct soc_mixer_control *mc =
2507 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002508 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002509 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002510 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002511 int val = snd_soc_read(codec, reg);
2512
2513 ucontrol->value.integer.value[0] =
2514 ((signed char)(val & 0xff))-min;
2515 ucontrol->value.integer.value[1] =
2516 ((signed char)((val >> 8) & 0xff))-min;
2517 return 0;
2518}
2519EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2520
2521/**
2522 * snd_soc_put_volsw_sgn - signed mixer put callback
2523 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002524 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002525 *
2526 * Callback to set the value of a signed mixer control.
2527 *
2528 * Returns 0 for success.
2529 */
2530int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2531 struct snd_ctl_elem_value *ucontrol)
2532{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002533 struct soc_mixer_control *mc =
2534 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002535 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002536 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002537 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002538 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002539
2540 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2541 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2542
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002543 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002544}
2545EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2546
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002547/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002548 * snd_soc_limit_volume - Set new limit to an existing volume control.
2549 *
2550 * @codec: where to look for the control
2551 * @name: Name of the control
2552 * @max: new maximum limit
2553 *
2554 * Return 0 for success, else error.
2555 */
2556int snd_soc_limit_volume(struct snd_soc_codec *codec,
2557 const char *name, int max)
2558{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002559 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002560 struct snd_kcontrol *kctl;
2561 struct soc_mixer_control *mc;
2562 int found = 0;
2563 int ret = -EINVAL;
2564
2565 /* Sanity check for name and max */
2566 if (unlikely(!name || max <= 0))
2567 return -EINVAL;
2568
2569 list_for_each_entry(kctl, &card->controls, list) {
2570 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2571 found = 1;
2572 break;
2573 }
2574 }
2575 if (found) {
2576 mc = (struct soc_mixer_control *)kctl->private_value;
2577 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002578 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002579 ret = 0;
2580 }
2581 }
2582 return ret;
2583}
2584EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2585
2586/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002587 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2588 * mixer info callback
2589 * @kcontrol: mixer control
2590 * @uinfo: control element information
2591 *
2592 * Returns 0 for success.
2593 */
2594int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2595 struct snd_ctl_elem_info *uinfo)
2596{
2597 struct soc_mixer_control *mc =
2598 (struct soc_mixer_control *)kcontrol->private_value;
2599 int max = mc->max;
2600 int min = mc->min;
2601
2602 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2603 uinfo->count = 2;
2604 uinfo->value.integer.min = 0;
2605 uinfo->value.integer.max = max-min;
2606
2607 return 0;
2608}
2609EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2610
2611/**
2612 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2613 * mixer get callback
2614 * @kcontrol: mixer control
2615 * @uinfo: control element information
2616 *
2617 * Returns 0 for success.
2618 */
2619int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2620 struct snd_ctl_elem_value *ucontrol)
2621{
2622 struct soc_mixer_control *mc =
2623 (struct soc_mixer_control *)kcontrol->private_value;
2624 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2625 unsigned int mask = (1<<mc->shift)-1;
2626 int min = mc->min;
2627 int val = snd_soc_read(codec, mc->reg) & mask;
2628 int valr = snd_soc_read(codec, mc->rreg) & mask;
2629
Stuart Longland20630c72010-06-18 12:56:10 +10002630 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2631 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002632 return 0;
2633}
2634EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2635
2636/**
2637 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2638 * mixer put callback
2639 * @kcontrol: mixer control
2640 * @uinfo: control element information
2641 *
2642 * Returns 0 for success.
2643 */
2644int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2645 struct snd_ctl_elem_value *ucontrol)
2646{
2647 struct soc_mixer_control *mc =
2648 (struct soc_mixer_control *)kcontrol->private_value;
2649 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2650 unsigned int mask = (1<<mc->shift)-1;
2651 int min = mc->min;
2652 int ret;
2653 unsigned int val, valr, oval, ovalr;
2654
2655 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2656 val &= mask;
2657 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2658 valr &= mask;
2659
2660 oval = snd_soc_read(codec, mc->reg) & mask;
2661 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2662
2663 ret = 0;
2664 if (oval != val) {
2665 ret = snd_soc_write(codec, mc->reg, val);
2666 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002667 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002668 }
2669 if (ovalr != valr) {
2670 ret = snd_soc_write(codec, mc->rreg, valr);
2671 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002672 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002673 }
2674
2675 return 0;
2676}
2677EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2678
2679/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002680 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2681 * @dai: DAI
2682 * @clk_id: DAI specific clock ID
2683 * @freq: new clock frequency in Hz
2684 * @dir: new clock direction - input/output.
2685 *
2686 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2687 */
2688int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2689 unsigned int freq, int dir)
2690{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002691 if (dai->driver && dai->driver->ops->set_sysclk)
2692 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002693 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002694 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002695 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002696 else
2697 return -EINVAL;
2698}
2699EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2700
2701/**
Mark Brownec4ee522011-03-07 20:58:11 +00002702 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2703 * @codec: CODEC
2704 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002705 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002706 * @freq: new clock frequency in Hz
2707 * @dir: new clock direction - input/output.
2708 *
2709 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2710 */
2711int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002712 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002713{
2714 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002715 return codec->driver->set_sysclk(codec, clk_id, source,
2716 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002717 else
2718 return -EINVAL;
2719}
2720EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2721
2722/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002723 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2724 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002725 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002726 * @div: new clock divisor.
2727 *
2728 * Configures the clock dividers. This is used to derive the best DAI bit and
2729 * frame clocks from the system or master clock. It's best to set the DAI bit
2730 * and frame clocks as low as possible to save system power.
2731 */
2732int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2733 int div_id, int div)
2734{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002735 if (dai->driver && dai->driver->ops->set_clkdiv)
2736 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002737 else
2738 return -EINVAL;
2739}
2740EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2741
2742/**
2743 * snd_soc_dai_set_pll - configure DAI PLL.
2744 * @dai: DAI
2745 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002746 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002747 * @freq_in: PLL input clock frequency in Hz
2748 * @freq_out: requested PLL output clock frequency in Hz
2749 *
2750 * Configures and enables PLL to generate output clock based on input clock.
2751 */
Mark Brown85488032009-09-05 18:52:16 +01002752int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2753 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002754{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002755 if (dai->driver && dai->driver->ops->set_pll)
2756 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002757 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002758 else if (dai->codec && dai->codec->driver->set_pll)
2759 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2760 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002761 else
2762 return -EINVAL;
2763}
2764EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2765
Mark Brownec4ee522011-03-07 20:58:11 +00002766/*
2767 * snd_soc_codec_set_pll - configure codec PLL.
2768 * @codec: CODEC
2769 * @pll_id: DAI specific PLL ID
2770 * @source: DAI specific source for the PLL
2771 * @freq_in: PLL input clock frequency in Hz
2772 * @freq_out: requested PLL output clock frequency in Hz
2773 *
2774 * Configures and enables PLL to generate output clock based on input clock.
2775 */
2776int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2777 unsigned int freq_in, unsigned int freq_out)
2778{
2779 if (codec->driver->set_pll)
2780 return codec->driver->set_pll(codec, pll_id, source,
2781 freq_in, freq_out);
2782 else
2783 return -EINVAL;
2784}
2785EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2786
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002787/**
2788 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2789 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002790 * @fmt: SND_SOC_DAIFMT_ format value.
2791 *
2792 * Configures the DAI hardware format and clocking.
2793 */
2794int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2795{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002796 if (dai->driver && dai->driver->ops->set_fmt)
2797 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002798 else
2799 return -EINVAL;
2800}
2801EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2802
2803/**
2804 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2805 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002806 * @tx_mask: bitmask representing active TX slots.
2807 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002808 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002809 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002810 *
2811 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2812 * specific.
2813 */
2814int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002815 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002816{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002817 if (dai->driver && dai->driver->ops->set_tdm_slot)
2818 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002819 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002820 else
2821 return -EINVAL;
2822}
2823EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2824
2825/**
Barry Song472df3c2009-09-12 01:16:29 +08002826 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2827 * @dai: DAI
2828 * @tx_num: how many TX channels
2829 * @tx_slot: pointer to an array which imply the TX slot number channel
2830 * 0~num-1 uses
2831 * @rx_num: how many RX channels
2832 * @rx_slot: pointer to an array which imply the RX slot number channel
2833 * 0~num-1 uses
2834 *
2835 * configure the relationship between channel number and TDM slot number.
2836 */
2837int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2838 unsigned int tx_num, unsigned int *tx_slot,
2839 unsigned int rx_num, unsigned int *rx_slot)
2840{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002841 if (dai->driver && dai->driver->ops->set_channel_map)
2842 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002843 rx_num, rx_slot);
2844 else
2845 return -EINVAL;
2846}
2847EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2848
2849/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002850 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2851 * @dai: DAI
2852 * @tristate: tristate enable
2853 *
2854 * Tristates the DAI so that others can use it.
2855 */
2856int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2857{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002858 if (dai->driver && dai->driver->ops->set_tristate)
2859 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002860 else
2861 return -EINVAL;
2862}
2863EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2864
2865/**
2866 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2867 * @dai: DAI
2868 * @mute: mute enable
2869 *
2870 * Mutes the DAI DAC.
2871 */
2872int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2873{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002874 if (dai->driver && dai->driver->ops->digital_mute)
2875 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002876 else
2877 return -EINVAL;
2878}
2879EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2880
Mark Brownc5af3a22008-11-28 13:29:45 +00002881/**
2882 * snd_soc_register_card - Register a card with the ASoC core
2883 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002884 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002885 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002886 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302887int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002888{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002889 int i;
2890
Mark Brownc5af3a22008-11-28 13:29:45 +00002891 if (!card->name || !card->dev)
2892 return -EINVAL;
2893
Stephen Warren5a504962011-12-21 10:40:59 -07002894 for (i = 0; i < card->num_links; i++) {
2895 struct snd_soc_dai_link *link = &card->dai_link[i];
2896
2897 /*
2898 * Codec must be specified by 1 of name or OF node,
2899 * not both or neither.
2900 */
2901 if (!!link->codec_name == !!link->codec_of_node) {
2902 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00002903 "Neither/both codec name/of_node are set for %s\n",
2904 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07002905 return -EINVAL;
2906 }
2907
2908 /*
2909 * Platform may be specified by either name or OF node, but
2910 * can be left unspecified, and a dummy platform will be used.
2911 */
2912 if (link->platform_name && link->platform_of_node) {
2913 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00002914 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07002915 return -EINVAL;
2916 }
2917
2918 /*
2919 * CPU DAI must be specified by 1 of name or OF node,
2920 * not both or neither.
2921 */
2922 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
2923 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00002924 "Neither/both cpu_dai name/of_node are set for %s\n",
2925 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07002926 return -EINVAL;
2927 }
2928 }
2929
Mark Browned77cc12011-05-03 18:25:34 +01002930 dev_set_drvdata(card->dev, card);
2931
Stephen Warren111c6412011-01-28 14:26:35 -07002932 snd_soc_initialize_card_lists(card);
2933
Vinod Koul150dd2f2011-01-13 22:48:32 +05302934 soc_init_card_debugfs(card);
2935
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002936 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
2937 (card->num_links + card->num_aux_devs),
2938 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002939 if (card->rtd == NULL)
2940 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002941 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002942
2943 for (i = 0; i < card->num_links; i++)
2944 card->rtd[i].dai_link = &card->dai_link[i];
2945
Mark Brownc5af3a22008-11-28 13:29:45 +00002946 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01002947 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00002948 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002949 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002950
2951 mutex_lock(&client_mutex);
2952 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002953 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002954 mutex_unlock(&client_mutex);
2955
2956 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2957
2958 return 0;
2959}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302960EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002961
2962/**
2963 * snd_soc_unregister_card - Unregister a card with the ASoC core
2964 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002965 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002966 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002967 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302968int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002969{
Vinod Koulb0e26482011-01-13 22:48:02 +05302970 if (card->instantiated)
2971 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002972 mutex_lock(&client_mutex);
2973 list_del(&card->list);
2974 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002975 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2976
2977 return 0;
2978}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302979EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002980
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002981/*
2982 * Simplify DAI link configuration by removing ".-1" from device names
2983 * and sanitizing names.
2984 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002985static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002986{
2987 char *found, name[NAME_SIZE];
2988 int id1, id2;
2989
2990 if (dev_name(dev) == NULL)
2991 return NULL;
2992
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002993 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002994
2995 /* are we a "%s.%d" name (platform and SPI components) */
2996 found = strstr(name, dev->driver->name);
2997 if (found) {
2998 /* get ID */
2999 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3000
3001 /* discard ID from name if ID == -1 */
3002 if (*id == -1)
3003 found[strlen(dev->driver->name)] = '\0';
3004 }
3005
3006 } else {
3007 /* I2C component devices are named "bus-addr" */
3008 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3009 char tmp[NAME_SIZE];
3010
3011 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003012 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003013
3014 /* sanitize component name for DAI link creation */
3015 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003016 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003017 } else
3018 *id = 0;
3019 }
3020
3021 return kstrdup(name, GFP_KERNEL);
3022}
3023
3024/*
3025 * Simplify DAI link naming for single devices with multiple DAIs by removing
3026 * any ".-1" and using the DAI name (instead of device name).
3027 */
3028static inline char *fmt_multiple_name(struct device *dev,
3029 struct snd_soc_dai_driver *dai_drv)
3030{
3031 if (dai_drv->name == NULL) {
3032 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3033 dev_name(dev));
3034 return NULL;
3035 }
3036
3037 return kstrdup(dai_drv->name, GFP_KERNEL);
3038}
3039
Mark Brown91151712008-11-30 23:31:24 +00003040/**
3041 * snd_soc_register_dai - Register a DAI with the ASoC core
3042 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003043 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003044 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003045int snd_soc_register_dai(struct device *dev,
3046 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003047{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003048 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003049
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003050 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003051
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003052 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3053 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003054 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003055
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003056 /* create DAI component name */
3057 dai->name = fmt_single_name(dev, &dai->id);
3058 if (dai->name == NULL) {
3059 kfree(dai);
3060 return -ENOMEM;
3061 }
3062
3063 dai->dev = dev;
3064 dai->driver = dai_drv;
3065 if (!dai->driver->ops)
3066 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003067
3068 mutex_lock(&client_mutex);
3069 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003070 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003071 mutex_unlock(&client_mutex);
3072
3073 pr_debug("Registered DAI '%s'\n", dai->name);
3074
3075 return 0;
3076}
3077EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3078
3079/**
3080 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3081 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003082 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003083 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003084void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003085{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003086 struct snd_soc_dai *dai;
3087
3088 list_for_each_entry(dai, &dai_list, list) {
3089 if (dev == dai->dev)
3090 goto found;
3091 }
3092 return;
3093
3094found:
Mark Brown91151712008-11-30 23:31:24 +00003095 mutex_lock(&client_mutex);
3096 list_del(&dai->list);
3097 mutex_unlock(&client_mutex);
3098
3099 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003100 kfree(dai->name);
3101 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003102}
3103EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3104
3105/**
3106 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3107 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003108 * @dai: Array of DAIs to register
3109 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003110 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003111int snd_soc_register_dais(struct device *dev,
3112 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003113{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003114 struct snd_soc_dai *dai;
3115 int i, ret = 0;
3116
Liam Girdwood720ffa42010-08-18 00:30:30 +01003117 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003118
3119 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003120
3121 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003122 if (dai == NULL) {
3123 ret = -ENOMEM;
3124 goto err;
3125 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003126
3127 /* create DAI component name */
3128 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3129 if (dai->name == NULL) {
3130 kfree(dai);
3131 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003132 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003133 }
3134
3135 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003136 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003137 if (dai->driver->id)
3138 dai->id = dai->driver->id;
3139 else
3140 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003141 if (!dai->driver->ops)
3142 dai->driver->ops = &null_dai_ops;
3143
3144 mutex_lock(&client_mutex);
3145 list_add(&dai->list, &dai_list);
3146 mutex_unlock(&client_mutex);
3147
3148 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003149 }
3150
Axel Lin1dcb4f32010-12-06 16:48:03 +08003151 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003152 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08003153 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00003154 return 0;
3155
3156err:
3157 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003158 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003159
3160 return ret;
3161}
3162EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3163
3164/**
3165 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3166 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003167 * @dai: Array of DAIs to unregister
3168 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003169 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003170void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003171{
3172 int i;
3173
3174 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003175 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003176}
3177EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3178
Mark Brown12a48a8c2008-12-03 19:40:30 +00003179/**
3180 * snd_soc_register_platform - Register a platform with the ASoC core
3181 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003182 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003183 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003184int snd_soc_register_platform(struct device *dev,
3185 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003186{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003187 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003188
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003189 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3190
3191 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3192 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003193 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003194
3195 /* create platform component name */
3196 platform->name = fmt_single_name(dev, &platform->id);
3197 if (platform->name == NULL) {
3198 kfree(platform);
3199 return -ENOMEM;
3200 }
3201
3202 platform->dev = dev;
3203 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003204 platform->dapm.dev = dev;
3205 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003206 platform->dapm.stream_event = platform_drv->stream_event;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003207
3208 mutex_lock(&client_mutex);
3209 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003210 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003211 mutex_unlock(&client_mutex);
3212
3213 pr_debug("Registered platform '%s'\n", platform->name);
3214
3215 return 0;
3216}
3217EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3218
3219/**
3220 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3221 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003222 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003223 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003224void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003225{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003226 struct snd_soc_platform *platform;
3227
3228 list_for_each_entry(platform, &platform_list, list) {
3229 if (dev == platform->dev)
3230 goto found;
3231 }
3232 return;
3233
3234found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003235 mutex_lock(&client_mutex);
3236 list_del(&platform->list);
3237 mutex_unlock(&client_mutex);
3238
3239 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003240 kfree(platform->name);
3241 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003242}
3243EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3244
Mark Brown151ab222009-05-09 16:22:58 +01003245static u64 codec_format_map[] = {
3246 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3247 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3248 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3249 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3250 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3251 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3252 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3253 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3254 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3255 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3256 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3257 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3258 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3259 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3260 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3261 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3262};
3263
3264/* Fix up the DAI formats for endianness: codecs don't actually see
3265 * the endianness of the data but we're using the CPU format
3266 * definitions which do need to include endianness so we ensure that
3267 * codec DAIs always have both big and little endian variants set.
3268 */
3269static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3270{
3271 int i;
3272
3273 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3274 if (stream->formats & codec_format_map[i])
3275 stream->formats |= codec_format_map[i];
3276}
3277
Mark Brown0d0cf002008-12-10 14:32:45 +00003278/**
3279 * snd_soc_register_codec - Register a codec with the ASoC core
3280 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003281 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003282 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003283int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003284 const struct snd_soc_codec_driver *codec_drv,
3285 struct snd_soc_dai_driver *dai_drv,
3286 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003287{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003288 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003289 struct snd_soc_codec *codec;
3290 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003291
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003292 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003293
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003294 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3295 if (codec == NULL)
3296 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003297
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003298 /* create CODEC component name */
3299 codec->name = fmt_single_name(dev, &codec->id);
3300 if (codec->name == NULL) {
3301 kfree(codec);
3302 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003303 }
3304
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003305 if (codec_drv->compress_type)
3306 codec->compress_type = codec_drv->compress_type;
3307 else
3308 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3309
Mark Brownc3acec22010-12-02 16:15:29 +00003310 codec->write = codec_drv->write;
3311 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003312 codec->volatile_register = codec_drv->volatile_register;
3313 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003314 codec->writable_register = codec_drv->writable_register;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003315 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3316 codec->dapm.dev = dev;
3317 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003318 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003319 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003320 codec->dev = dev;
3321 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003322 codec->num_dai = num_dai;
3323 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003324
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003325 /* allocate CODEC register cache */
3326 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003327 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003328 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003329 /* it is necessary to make a copy of the default register cache
3330 * because in the case of using a compression type that requires
3331 * the default register cache to be marked as __devinitconst the
3332 * kernel might have freed the array by the time we initialize
3333 * the cache.
3334 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003335 if (codec_drv->reg_cache_default) {
3336 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3337 reg_size, GFP_KERNEL);
3338 if (!codec->reg_def_copy) {
3339 ret = -ENOMEM;
3340 goto fail;
3341 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003342 }
3343 }
3344
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003345 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3346 if (!codec->volatile_register)
3347 codec->volatile_register = snd_soc_default_volatile_register;
3348 if (!codec->readable_register)
3349 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003350 if (!codec->writable_register)
3351 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003352 }
3353
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003354 for (i = 0; i < num_dai; i++) {
3355 fixup_codec_formats(&dai_drv[i].playback);
3356 fixup_codec_formats(&dai_drv[i].capture);
3357 }
3358
Mark Brown26b01cc2010-08-18 20:20:55 +01003359 /* register any DAIs */
3360 if (num_dai) {
3361 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3362 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003363 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003364 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003365
Mark Brown0d0cf002008-12-10 14:32:45 +00003366 mutex_lock(&client_mutex);
3367 list_add(&codec->list, &codec_list);
3368 snd_soc_instantiate_cards();
3369 mutex_unlock(&client_mutex);
3370
3371 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003372 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003373
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003374fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003375 kfree(codec->reg_def_copy);
3376 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003377 kfree(codec->name);
3378 kfree(codec);
3379 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003380}
3381EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3382
3383/**
3384 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3385 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003386 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003387 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003388void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003389{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003390 struct snd_soc_codec *codec;
3391 int i;
3392
3393 list_for_each_entry(codec, &codec_list, list) {
3394 if (dev == codec->dev)
3395 goto found;
3396 }
3397 return;
3398
3399found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003400 if (codec->num_dai)
3401 for (i = 0; i < codec->num_dai; i++)
3402 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003403
Mark Brown0d0cf002008-12-10 14:32:45 +00003404 mutex_lock(&client_mutex);
3405 list_del(&codec->list);
3406 mutex_unlock(&client_mutex);
3407
3408 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003409
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003410 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003411 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003412 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003413 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003414}
3415EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3416
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003417/* Retrieve a card's name from device tree */
3418int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3419 const char *propname)
3420{
3421 struct device_node *np = card->dev->of_node;
3422 int ret;
3423
3424 ret = of_property_read_string_index(np, propname, 0, &card->name);
3425 /*
3426 * EINVAL means the property does not exist. This is fine providing
3427 * card->name was previously set, which is checked later in
3428 * snd_soc_register_card.
3429 */
3430 if (ret < 0 && ret != -EINVAL) {
3431 dev_err(card->dev,
3432 "Property '%s' could not be read: %d\n",
3433 propname, ret);
3434 return ret;
3435 }
3436
3437 return 0;
3438}
3439EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3440
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003441int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3442 const char *propname)
3443{
3444 struct device_node *np = card->dev->of_node;
3445 int num_routes;
3446 struct snd_soc_dapm_route *routes;
3447 int i, ret;
3448
3449 num_routes = of_property_count_strings(np, propname);
3450 if (num_routes & 1) {
3451 dev_err(card->dev,
3452 "Property '%s's length is not even\n",
3453 propname);
3454 return -EINVAL;
3455 }
3456 num_routes /= 2;
3457 if (!num_routes) {
3458 dev_err(card->dev,
3459 "Property '%s's length is zero\n",
3460 propname);
3461 return -EINVAL;
3462 }
3463
3464 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3465 GFP_KERNEL);
3466 if (!routes) {
3467 dev_err(card->dev,
3468 "Could not allocate DAPM route table\n");
3469 return -EINVAL;
3470 }
3471
3472 for (i = 0; i < num_routes; i++) {
3473 ret = of_property_read_string_index(np, propname,
3474 2 * i, &routes[i].sink);
3475 if (ret) {
3476 dev_err(card->dev,
3477 "Property '%s' index %d could not be read: %d\n",
3478 propname, 2 * i, ret);
3479 return -EINVAL;
3480 }
3481 ret = of_property_read_string_index(np, propname,
3482 (2 * i) + 1, &routes[i].source);
3483 if (ret) {
3484 dev_err(card->dev,
3485 "Property '%s' index %d could not be read: %d\n",
3486 propname, (2 * i) + 1, ret);
3487 return -EINVAL;
3488 }
3489 }
3490
3491 card->num_dapm_routes = num_routes;
3492 card->dapm_routes = routes;
3493
3494 return 0;
3495}
3496EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3497
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003498static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003499{
Mark Brown384c89e2008-12-03 17:34:03 +00003500#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003501 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3502 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Mark Brown384c89e2008-12-03 17:34:03 +00003503 printk(KERN_WARNING
3504 "ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003505 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003506 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003507
Mark Brown8a9dab12011-01-10 22:25:21 +00003508 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003509 &codec_list_fops))
3510 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3511
Mark Brown8a9dab12011-01-10 22:25:21 +00003512 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003513 &dai_list_fops))
3514 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003515
Mark Brown8a9dab12011-01-10 22:25:21 +00003516 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003517 &platform_list_fops))
3518 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003519#endif
3520
Mark Brownfb257892011-04-28 10:57:54 +01003521 snd_soc_util_init();
3522
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003523 return platform_driver_register(&soc_driver);
3524}
Mark Brown4abe8e12010-10-12 17:41:03 +01003525module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003526
Mark Brown7d8c16a2008-11-30 22:11:24 +00003527static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003528{
Mark Brownfb257892011-04-28 10:57:54 +01003529 snd_soc_util_exit();
3530
Mark Brown384c89e2008-12-03 17:34:03 +00003531#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003532 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003533#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003534 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003535}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003536module_exit(snd_soc_exit);
3537
3538/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003539MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003540MODULE_DESCRIPTION("ALSA SoC Core");
3541MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003542MODULE_ALIAS("platform:soc-audio");