blob: 92cee24ed2dcce7eb7b7e8de3a87f09d0ffe5138 [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:
Mark Brown125a25d2012-01-31 15:49:10 +0000570 /*
571 * If the CODEC is capable of idle
572 * bias off then being in STANDBY
573 * means it's doing something,
574 * otherwise fall through.
575 */
576 if (codec->dapm.idle_bias_off) {
577 dev_dbg(codec->dev,
578 "idle_bias_off CODEC on over suspend\n");
579 break;
580 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100582 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000583 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900584 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000585 break;
586 default:
587 dev_dbg(codec->dev, "CODEC is on over suspend\n");
588 break;
589 }
590 }
591 }
592
593 for (i = 0; i < card->num_rtd; i++) {
594 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
595
596 if (card->rtd[i].dai_link->ignore_suspend)
597 continue;
598
599 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
600 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200601 }
602
Mark Brown87506542008-11-18 20:50:34 +0000603 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000604 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200605
606 return 0;
607}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000608EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200609
Andy Green6ed25972008-06-13 16:24:05 +0100610/* deferred resume work, so resume can complete before we finished
611 * setting our codec back up, which can be very slow on I2C
612 */
613static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200614{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000615 struct snd_soc_card *card =
616 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200617 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200618 int i;
619
Andy Green6ed25972008-06-13 16:24:05 +0100620 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
621 * so userspace apps are blocked from touching us
622 */
623
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100625
Mark Brown99497882010-05-07 20:24:05 +0100626 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100628
Mark Brown87506542008-11-18 20:50:34 +0000629 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000630 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200631
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000632 /* resume AC97 DAIs */
633 for (i = 0; i < card->num_rtd; i++) {
634 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100635
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000636 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100637 continue;
638
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
640 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200641 }
642
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200643 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644 /* If the CODEC was idle over suspend then it will have been
645 * left with bias OFF or STANDBY and suspended so we must now
646 * resume. Otherwise the suspend was suppressed.
647 */
648 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200649 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000650 case SND_SOC_BIAS_STANDBY:
651 case SND_SOC_BIAS_OFF:
652 codec->driver->resume(codec);
653 codec->suspended = 0;
654 break;
655 default:
656 dev_dbg(codec->dev, "CODEC was on over suspend\n");
657 break;
658 }
Mark Brown1547aba2010-05-07 21:11:40 +0100659 }
660 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200661
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000662 for (i = 0; i < card->num_rtd; i++) {
663 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100664
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100666 continue;
667
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 if (driver->playback.stream_name != NULL)
669 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200670 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000671
672 if (driver->capture.stream_name != NULL)
673 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200674 SND_SOC_DAPM_STREAM_RESUME);
675 }
676
Mark Brown3ff3f642008-05-19 12:32:25 +0200677 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000678 for (i = 0; i < card->num_rtd; i++) {
679 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
680 struct snd_soc_dai_driver *drv = dai->driver;
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 (drv->ops->digital_mute && dai->playback_active)
686 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200687 }
688
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000689 for (i = 0; i < card->num_rtd; i++) {
690 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
691 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100692
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100694 continue;
695
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
697 cpu_dai->driver->resume(cpu_dai);
698 if (platform->driver->resume && platform->suspended) {
699 platform->driver->resume(cpu_dai);
700 platform->suspended = 0;
701 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200702 }
703
Mark Brown87506542008-11-18 20:50:34 +0000704 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000705 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200706
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000707 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100708
709 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000710 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100711}
712
713/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000714int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100715{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000716 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600717 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200718
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800719 /* If the initialization of this soc device failed, there is no codec
720 * associated with it. Just bail out in this case.
721 */
722 if (list_empty(&card->codec_dev_list))
723 return 0;
724
Mark Brown64ab9ba2009-03-31 11:27:03 +0100725 /* AC97 devices might have other drivers hanging off them so
726 * need to resume immediately. Other drivers don't have that
727 * problem and may take a substantial amount of time to resume
728 * due to I/O costs and anti-pop so handle them out of line.
729 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000730 for (i = 0; i < card->num_rtd; i++) {
731 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600732 ac97_control |= cpu_dai->driver->ac97_control;
733 }
734 if (ac97_control) {
735 dev_dbg(dev, "Resuming AC97 immediately\n");
736 soc_resume_deferred(&card->deferred_resume_work);
737 } else {
738 dev_dbg(dev, "Scheduling resume work\n");
739 if (!schedule_work(&card->deferred_resume_work))
740 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100741 }
Andy Green6ed25972008-06-13 16:24:05 +0100742
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200743 return 0;
744}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000745EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200746#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000747#define snd_soc_suspend NULL
748#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200749#endif
750
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100751static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800752};
753
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200755{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
757 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000758 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000759 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000760 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100761 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200762
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000763 if (rtd->complete)
764 return 1;
765 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000766
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000767 /* do we already have the CPU DAI for this link ? */
768 if (rtd->cpu_dai) {
769 goto find_codec;
770 }
771 /* no, then find CPU DAI from registered DAIs*/
772 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700773 if (dai_link->cpu_dai_of_node) {
774 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
775 continue;
776 } else {
777 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
778 continue;
779 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700780
781 rtd->cpu_dai = cpu_dai;
782 goto find_codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000783 }
784 dev_dbg(card->dev, "CPU DAI %s not registered\n",
785 dai_link->cpu_dai_name);
786
787find_codec:
788 /* do we already have the CODEC for this link ? */
789 if (rtd->codec) {
790 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +0000791 }
792
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000793 /* no, then find CODEC from registered CODECs*/
794 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700795 if (dai_link->codec_of_node) {
796 if (codec->dev->of_node != dai_link->codec_of_node)
797 continue;
798 } else {
799 if (strcmp(codec->name, dai_link->codec_name))
800 continue;
801 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000802
Stephen Warren2610ab72011-12-07 13:58:27 -0700803 rtd->codec = codec;
804
805 /*
806 * CODEC found, so find CODEC DAI from registered DAIs from
807 * this CODEC
808 */
809 list_for_each_entry(codec_dai, &dai_list, list) {
810 if (codec->dev == codec_dai->dev &&
811 !strcmp(codec_dai->name,
812 dai_link->codec_dai_name)) {
813
814 rtd->codec_dai = codec_dai;
815 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000816 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000817 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700818 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
819 dai_link->codec_dai_name);
820
821 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000822 }
823 dev_dbg(card->dev, "CODEC %s not registered\n",
824 dai_link->codec_name);
825
826find_platform:
Mark Brown848dd8b2011-04-27 18:16:32 +0100827 /* do we need a platform? */
828 if (rtd->platform)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000829 goto out;
Mark Brown848dd8b2011-04-27 18:16:32 +0100830
831 /* if there's no platform we match on the empty platform */
832 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700833 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100834 platform_name = "snd-soc-dummy";
835
836 /* no, then find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000837 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700838 if (dai_link->platform_of_node) {
839 if (platform->dev->of_node !=
840 dai_link->platform_of_node)
841 continue;
842 } else {
843 if (strcmp(platform->name, platform_name))
844 continue;
845 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700846
847 rtd->platform = platform;
848 goto out;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000849 }
850
851 dev_dbg(card->dev, "platform %s not registered\n",
852 dai_link->platform_name);
853 return 0;
854
855out:
856 /* mark rtd as complete if we found all 4 of our client devices */
857 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
858 rtd->complete = 1;
859 card->num_rtd++;
860 }
861 return 1;
862}
863
Jarkko Nikula589c3562010-12-06 16:27:07 +0200864static void soc_remove_codec(struct snd_soc_codec *codec)
865{
866 int err;
867
868 if (codec->driver->remove) {
869 err = codec->driver->remove(codec);
870 if (err < 0)
871 dev_err(codec->dev,
872 "asoc: failed to remove %s: %d\n",
873 codec->name, err);
874 }
875
876 /* Make sure all DAPM widgets are freed */
877 snd_soc_dapm_free(&codec->dapm);
878
879 soc_cleanup_codec_debugfs(codec);
880 codec->probed = 0;
881 list_del(&codec->card_list);
882 module_put(codec->dev->driver->owner);
883}
884
Liam Girdwood0168bf02011-06-07 16:08:05 +0100885static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000886{
887 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
888 struct snd_soc_codec *codec = rtd->codec;
889 struct snd_soc_platform *platform = rtd->platform;
890 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
891 int err;
892
893 /* unregister the rtd device */
894 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800895 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
896 device_remove_file(rtd->dev, &dev_attr_codec_reg);
897 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000898 rtd->dev_registered = 0;
899 }
900
901 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100902 if (codec_dai && codec_dai->probed &&
903 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000904 if (codec_dai->driver->remove) {
905 err = codec_dai->driver->remove(codec_dai);
906 if (err < 0)
907 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
908 }
909 codec_dai->probed = 0;
910 list_del(&codec_dai->card_list);
911 }
912
913 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100914 if (platform && platform->probed &&
915 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000916 if (platform->driver->remove) {
917 err = platform->driver->remove(platform);
918 if (err < 0)
919 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
920 }
Liam Girdwood675c4962012-01-16 15:25:37 +0000921
922 /* Make sure all DAPM widgets are freed */
923 snd_soc_dapm_free(&platform->dapm);
924
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000925 platform->probed = 0;
926 list_del(&platform->card_list);
927 module_put(platform->dev->driver->owner);
928 }
929
930 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100931 if (codec && codec->probed &&
932 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200933 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000934
935 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100936 if (cpu_dai && cpu_dai->probed &&
937 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000938 if (cpu_dai->driver->remove) {
939 err = cpu_dai->driver->remove(cpu_dai);
940 if (err < 0)
941 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
942 }
943 cpu_dai->probed = 0;
944 list_del(&cpu_dai->card_list);
945 module_put(cpu_dai->dev->driver->owner);
946 }
947}
948
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900949static void soc_remove_dai_links(struct snd_soc_card *card)
950{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100951 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900952
Liam Girdwood0168bf02011-06-07 16:08:05 +0100953 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
954 order++) {
955 for (dai = 0; dai < card->num_rtd; dai++)
956 soc_remove_dai_link(card, dai, order);
957 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900958 card->num_rtd = 0;
959}
960
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200961static void soc_set_name_prefix(struct snd_soc_card *card,
962 struct snd_soc_codec *codec)
963{
964 int i;
965
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000966 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200967 return;
968
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000969 for (i = 0; i < card->num_configs; i++) {
970 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200971 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
972 codec->name_prefix = map->name_prefix;
973 break;
974 }
975 }
976}
977
Jarkko Nikula589c3562010-12-06 16:27:07 +0200978static int soc_probe_codec(struct snd_soc_card *card,
979 struct snd_soc_codec *codec)
980{
981 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +0000982 const struct snd_soc_codec_driver *driver = codec->driver;
Jarkko Nikula589c3562010-12-06 16:27:07 +0200983
984 codec->card = card;
985 codec->dapm.card = card;
986 soc_set_name_prefix(card, codec);
987
Jarkko Nikula70d29332011-01-27 16:24:22 +0200988 if (!try_module_get(codec->dev->driver->owner))
989 return -ENODEV;
990
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +0200991 soc_init_codec_debugfs(codec);
992
Lars-Peter Clausen77530152011-05-05 16:59:09 +0200993 if (driver->dapm_widgets)
994 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
995 driver->num_dapm_widgets);
996
Mark Brown33c5f962011-08-22 18:40:30 +0100997 codec->dapm.idle_bias_off = driver->idle_bias_off;
998
Mark Brown89b95ac02011-03-07 16:38:44 +0000999 if (driver->probe) {
1000 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001001 if (ret < 0) {
1002 dev_err(codec->dev,
1003 "asoc: failed to probe CODEC %s: %d\n",
1004 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001005 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001006 }
1007 }
1008
Mark Brownb7af1da2011-04-07 19:18:44 +09001009 if (driver->controls)
1010 snd_soc_add_controls(codec, driver->controls,
1011 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001012 if (driver->dapm_routes)
1013 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1014 driver->num_dapm_routes);
1015
Jarkko Nikula589c3562010-12-06 16:27:07 +02001016 /* mark codec as probed and add to card codec list */
1017 codec->probed = 1;
1018 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001019 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001020
Jarkko Nikula70d29332011-01-27 16:24:22 +02001021 return 0;
1022
1023err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001024 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001025 module_put(codec->dev->driver->owner);
1026
Jarkko Nikula589c3562010-12-06 16:27:07 +02001027 return ret;
1028}
1029
Liam Girdwood956245e2011-07-01 16:54:08 +01001030static int soc_probe_platform(struct snd_soc_card *card,
1031 struct snd_soc_platform *platform)
1032{
1033 int ret = 0;
1034 const struct snd_soc_platform_driver *driver = platform->driver;
1035
1036 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001037 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001038
1039 if (!try_module_get(platform->dev->driver->owner))
1040 return -ENODEV;
1041
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001042 if (driver->dapm_widgets)
1043 snd_soc_dapm_new_controls(&platform->dapm,
1044 driver->dapm_widgets, driver->num_dapm_widgets);
1045
Liam Girdwood956245e2011-07-01 16:54:08 +01001046 if (driver->probe) {
1047 ret = driver->probe(platform);
1048 if (ret < 0) {
1049 dev_err(platform->dev,
1050 "asoc: failed to probe platform %s: %d\n",
1051 platform->name, ret);
1052 goto err_probe;
1053 }
1054 }
1055
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001056 if (driver->controls)
1057 snd_soc_add_platform_controls(platform, driver->controls,
1058 driver->num_controls);
1059 if (driver->dapm_routes)
1060 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1061 driver->num_dapm_routes);
1062
Liam Girdwood956245e2011-07-01 16:54:08 +01001063 /* mark platform as probed and add to card platform list */
1064 platform->probed = 1;
1065 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001066 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001067
1068 return 0;
1069
1070err_probe:
1071 module_put(platform->dev->driver->owner);
1072
1073 return ret;
1074}
1075
Mark Brown36ae1a92012-01-06 17:12:45 -08001076static void rtd_release(struct device *dev)
1077{
1078 kfree(dev);
1079}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001080
Jarkko Nikula589c3562010-12-06 16:27:07 +02001081static int soc_post_component_init(struct snd_soc_card *card,
1082 struct snd_soc_codec *codec,
1083 int num, int dailess)
1084{
1085 struct snd_soc_dai_link *dai_link = NULL;
1086 struct snd_soc_aux_dev *aux_dev = NULL;
1087 struct snd_soc_pcm_runtime *rtd;
1088 const char *temp, *name;
1089 int ret = 0;
1090
1091 if (!dailess) {
1092 dai_link = &card->dai_link[num];
1093 rtd = &card->rtd[num];
1094 name = dai_link->name;
1095 } else {
1096 aux_dev = &card->aux_dev[num];
1097 rtd = &card->rtd_aux[num];
1098 name = aux_dev->name;
1099 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001100 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001101
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001102 /* Make sure all DAPM widgets are instantiated */
1103 snd_soc_dapm_new_widgets(&codec->dapm);
1104
Jarkko Nikula589c3562010-12-06 16:27:07 +02001105 /* machine controls, routes and widgets are not prefixed */
1106 temp = codec->name_prefix;
1107 codec->name_prefix = NULL;
1108
1109 /* do machine specific initialization */
1110 if (!dailess && dai_link->init)
1111 ret = dai_link->init(rtd);
1112 else if (dailess && aux_dev->init)
1113 ret = aux_dev->init(&codec->dapm);
1114 if (ret < 0) {
1115 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1116 return ret;
1117 }
1118 codec->name_prefix = temp;
1119
Jarkko Nikula589c3562010-12-06 16:27:07 +02001120 /* register the rtd device */
1121 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001122
1123 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1124 if (!rtd->dev)
1125 return -ENOMEM;
1126 device_initialize(rtd->dev);
1127 rtd->dev->parent = card->dev;
1128 rtd->dev->release = rtd_release;
1129 rtd->dev->init_name = name;
1130 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001131 mutex_init(&rtd->pcm_mutex);
Mark Brown36ae1a92012-01-06 17:12:45 -08001132 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001133 if (ret < 0) {
1134 dev_err(card->dev,
1135 "asoc: failed to register runtime device: %d\n", ret);
1136 return ret;
1137 }
1138 rtd->dev_registered = 1;
1139
1140 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001141 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001142 if (ret < 0)
1143 dev_err(codec->dev,
1144 "asoc: failed to add codec dapm sysfs entries: %d\n",
1145 ret);
1146
1147 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001148 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001149 if (ret < 0)
1150 dev_err(codec->dev,
1151 "asoc: failed to add codec sysfs files: %d\n", ret);
1152
1153 return 0;
1154}
1155
Liam Girdwood0168bf02011-06-07 16:08:05 +01001156static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001157{
1158 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1159 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1160 struct snd_soc_codec *codec = rtd->codec;
1161 struct snd_soc_platform *platform = rtd->platform;
1162 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1163 int ret;
1164
Liam Girdwood0168bf02011-06-07 16:08:05 +01001165 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1166 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001167
1168 /* config components */
1169 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001170 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001171 codec_dai->card = card;
1172 cpu_dai->card = card;
1173
1174 /* set default power off timeout */
1175 rtd->pmdown_time = pmdown_time;
1176
1177 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001178 if (!cpu_dai->probed &&
1179 cpu_dai->driver->probe_order == order) {
Liam Girdwood61b61e32011-05-24 13:57:43 +01001180 if (!try_module_get(cpu_dai->dev->driver->owner))
1181 return -ENODEV;
1182
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001183 if (cpu_dai->driver->probe) {
1184 ret = cpu_dai->driver->probe(cpu_dai);
1185 if (ret < 0) {
1186 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1187 cpu_dai->name);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001188 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001189 return ret;
1190 }
1191 }
1192 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001193 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001194 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1195 }
1196
1197 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001198 if (!codec->probed &&
1199 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001200 ret = soc_probe_codec(card, codec);
1201 if (ret < 0)
1202 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001203 }
1204
1205 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001206 if (!platform->probed &&
1207 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001208 ret = soc_probe_platform(card, platform);
1209 if (ret < 0)
1210 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001211 }
1212
1213 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001214 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001215 if (codec_dai->driver->probe) {
1216 ret = codec_dai->driver->probe(codec_dai);
1217 if (ret < 0) {
1218 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1219 codec_dai->name);
1220 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001221 }
1222 }
1223
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001224 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001225 codec_dai->probed = 1;
1226 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001227 }
1228
Liam Girdwood0168bf02011-06-07 16:08:05 +01001229 /* complete DAI probe during last probe */
1230 if (order != SND_SOC_COMP_ORDER_LAST)
1231 return 0;
1232
Jarkko Nikula589c3562010-12-06 16:27:07 +02001233 ret = soc_post_component_init(card, codec, num, 0);
1234 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001235 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001236
Mark Brown36ae1a92012-01-06 17:12:45 -08001237 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001238 if (ret < 0)
1239 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1240
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001241 /* create the pcm */
1242 ret = soc_new_pcm(rtd, num);
1243 if (ret < 0) {
1244 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1245 return ret;
1246 }
1247
1248 /* add platform data for AC97 devices */
1249 if (rtd->codec_dai->driver->ac97_control)
1250 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1251
1252 return 0;
1253}
1254
1255#ifdef CONFIG_SND_SOC_AC97_BUS
1256static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1257{
1258 int ret;
1259
1260 /* Only instantiate AC97 if not already done by the adaptor
1261 * for the generic AC97 subsystem.
1262 */
1263 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001264 /*
1265 * It is possible that the AC97 device is already registered to
1266 * the device subsystem. This happens when the device is created
1267 * via snd_ac97_mixer(). Currently only SoC codec that does so
1268 * is the generic AC97 glue but others migh emerge.
1269 *
1270 * In those cases we don't try to register the device again.
1271 */
1272 if (!rtd->codec->ac97_created)
1273 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001274
1275 ret = soc_ac97_dev_register(rtd->codec);
1276 if (ret < 0) {
1277 printk(KERN_ERR "asoc: AC97 device register failed\n");
1278 return ret;
1279 }
1280
1281 rtd->codec->ac97_registered = 1;
1282 }
1283 return 0;
1284}
1285
1286static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1287{
1288 if (codec->ac97_registered) {
1289 soc_ac97_dev_unregister(codec);
1290 codec->ac97_registered = 0;
1291 }
1292}
1293#endif
1294
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001295static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1296{
1297 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001298 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001299 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001300
1301 /* find CODEC from registered CODECs*/
1302 list_for_each_entry(codec, &codec_list, list) {
1303 if (!strcmp(codec->name, aux_dev->codec_name)) {
1304 if (codec->probed) {
1305 dev_err(codec->dev,
1306 "asoc: codec already probed");
1307 ret = -EBUSY;
1308 goto out;
1309 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001310 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001311 }
1312 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001313 /* codec not found */
1314 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1315 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001316
Jarkko Nikula676ad982010-12-03 09:18:22 +02001317found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001318 ret = soc_probe_codec(card, codec);
1319 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001320 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001321
Jarkko Nikula589c3562010-12-06 16:27:07 +02001322 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001323
1324out:
1325 return ret;
1326}
1327
1328static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1329{
1330 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1331 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001332
1333 /* unregister the rtd device */
1334 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001335 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1336 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001337 rtd->dev_registered = 0;
1338 }
1339
Jarkko Nikula589c3562010-12-06 16:27:07 +02001340 if (codec && codec->probed)
1341 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001342}
1343
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001344static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1345 enum snd_soc_compress_type compress_type)
1346{
1347 int ret;
1348
1349 if (codec->cache_init)
1350 return 0;
1351
1352 /* override the compress_type if necessary */
1353 if (compress_type && codec->compress_type != compress_type)
1354 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001355 ret = snd_soc_cache_init(codec);
1356 if (ret < 0) {
1357 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1358 ret);
1359 return ret;
1360 }
1361 codec->cache_init = 1;
1362 return 0;
1363}
1364
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001365static void snd_soc_instantiate_card(struct snd_soc_card *card)
1366{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001367 struct snd_soc_codec *codec;
1368 struct snd_soc_codec_conf *codec_conf;
1369 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001370 struct snd_soc_dai_link *dai_link;
Liam Girdwood0168bf02011-06-07 16:08:05 +01001371 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001372
1373 mutex_lock(&card->mutex);
1374
1375 if (card->instantiated) {
1376 mutex_unlock(&card->mutex);
1377 return;
1378 }
1379
1380 /* bind DAIs */
1381 for (i = 0; i < card->num_links; i++)
1382 soc_bind_dai_link(card, i);
1383
1384 /* bind completed ? */
1385 if (card->num_rtd != card->num_links) {
1386 mutex_unlock(&card->mutex);
1387 return;
1388 }
1389
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001390 /* initialize the register cache for each available codec */
1391 list_for_each_entry(codec, &codec_list, list) {
1392 if (codec->cache_init)
1393 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001394 /* by default we don't override the compress_type */
1395 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001396 /* check to see if we need to override the compress_type */
1397 for (i = 0; i < card->num_configs; ++i) {
1398 codec_conf = &card->codec_conf[i];
1399 if (!strcmp(codec->name, codec_conf->dev_name)) {
1400 compress_type = codec_conf->compress_type;
1401 if (compress_type && compress_type
1402 != codec->compress_type)
1403 break;
1404 }
1405 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001406 ret = snd_soc_init_codec_cache(codec, compress_type);
1407 if (ret < 0) {
1408 mutex_unlock(&card->mutex);
1409 return;
1410 }
1411 }
1412
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001413 /* card bind complete so register a sound card */
1414 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1415 card->owner, 0, &card->snd_card);
1416 if (ret < 0) {
1417 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1418 card->name);
1419 mutex_unlock(&card->mutex);
1420 return;
1421 }
1422 card->snd_card->dev = card->dev;
1423
Mark Browne37a4972011-03-02 18:21:57 +00001424 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1425 card->dapm.dev = card->dev;
1426 card->dapm.card = card;
1427 list_add(&card->dapm.list, &card->dapm_list);
1428
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001429#ifdef CONFIG_DEBUG_FS
1430 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1431#endif
1432
Mark Brown88ee1c62011-02-02 10:43:26 +00001433#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001434 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001435 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001436#endif
Andy Green6ed25972008-06-13 16:24:05 +01001437
Mark Brown9a841eb2011-04-12 17:51:37 -07001438 if (card->dapm_widgets)
1439 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1440 card->num_dapm_widgets);
1441
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001442 /* initialise the sound card only once */
1443 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001444 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001445 if (ret < 0)
1446 goto card_probe_error;
1447 }
1448
Liam Girdwood0168bf02011-06-07 16:08:05 +01001449 /* early DAI link probe */
1450 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1451 order++) {
1452 for (i = 0; i < card->num_links; i++) {
1453 ret = soc_probe_dai_link(card, i, order);
1454 if (ret < 0) {
1455 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001456 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001457 goto probe_dai_err;
1458 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001459 }
1460 }
1461
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001462 for (i = 0; i < card->num_aux_devs; i++) {
1463 ret = soc_probe_aux_dev(card, i);
1464 if (ret < 0) {
1465 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1466 card->name, ret);
1467 goto probe_aux_dev_err;
1468 }
1469 }
1470
Mark Brownb7af1da2011-04-07 19:18:44 +09001471 /* We should have a non-codec control add function but we don't */
1472 if (card->controls)
1473 snd_soc_add_controls(list_first_entry(&card->codec_dev_list,
1474 struct snd_soc_codec,
1475 card_list),
1476 card->controls,
1477 card->num_controls);
1478
Mark Brownb8ad29d2011-03-02 18:35:51 +00001479 if (card->dapm_routes)
1480 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1481 card->num_dapm_routes);
1482
Mark Brownb90d2f92011-10-10 13:38:06 +01001483 snd_soc_dapm_new_widgets(&card->dapm);
1484
Mark Brown75d9ac42011-09-27 16:41:01 +01001485 for (i = 0; i < card->num_links; i++) {
1486 dai_link = &card->dai_link[i];
1487
1488 if (dai_link->dai_fmt) {
1489 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1490 dai_link->dai_fmt);
1491 if (ret != 0)
1492 dev_warn(card->rtd[i].codec_dai->dev,
1493 "Failed to set DAI format: %d\n",
1494 ret);
1495
1496 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1497 dai_link->dai_fmt);
1498 if (ret != 0)
1499 dev_warn(card->rtd[i].cpu_dai->dev,
1500 "Failed to set DAI format: %d\n",
1501 ret);
1502 }
1503 }
1504
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001505 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001506 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001507 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1508 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001509 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1510 "%s", card->driver_name ? card->driver_name : card->name);
1511 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1512 switch (card->snd_card->driver[i]) {
1513 case '_':
1514 case '-':
1515 case '\0':
1516 break;
1517 default:
1518 if (!isalnum(card->snd_card->driver[i]))
1519 card->snd_card->driver[i] = '_';
1520 break;
1521 }
1522 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001523
Mark Brown28e9ad92011-03-02 18:36:34 +00001524 if (card->late_probe) {
1525 ret = card->late_probe(card);
1526 if (ret < 0) {
1527 dev_err(card->dev, "%s late_probe() failed: %d\n",
1528 card->name, ret);
1529 goto probe_aux_dev_err;
1530 }
1531 }
1532
Mark Brown2dc00212011-10-08 13:59:44 +01001533 snd_soc_dapm_new_widgets(&card->dapm);
1534
Stephen Warren16332812011-11-23 12:42:04 -07001535 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001536 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001537 snd_soc_dapm_auto_nc_codec_pins(codec);
1538
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001539 ret = snd_card_register(card->snd_card);
1540 if (ret < 0) {
1541 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
Axel Lin6b3ed782010-12-07 16:12:29 +08001542 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001543 }
1544
1545#ifdef CONFIG_SND_SOC_AC97_BUS
1546 /* register any AC97 codecs */
1547 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001548 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1549 if (ret < 0) {
1550 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1551 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001552 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001553 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001554 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001555 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001556#endif
1557
Mark Brown435c5e22008-12-04 15:32:53 +00001558 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001559 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001560 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001561 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001562
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001563probe_aux_dev_err:
1564 for (i = 0; i < card->num_aux_devs; i++)
1565 soc_remove_aux_dev(card, i);
1566
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001567probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001568 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001569
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001570card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001571 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001572 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001573
1574 snd_card_free(card->snd_card);
1575
1576 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001577}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001578
Mark Brown435c5e22008-12-04 15:32:53 +00001579/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001580 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001581 * client_mutex.
1582 */
1583static void snd_soc_instantiate_cards(void)
1584{
1585 struct snd_soc_card *card;
1586 list_for_each_entry(card, &card_list, list)
1587 snd_soc_instantiate_card(card);
1588}
1589
1590/* probes a new socdev */
1591static int soc_probe(struct platform_device *pdev)
1592{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001593 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001594 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001595
Vinod Koul70a7ca32011-01-14 19:22:48 +05301596 /*
1597 * no card, so machine driver should be registering card
1598 * we should not be here in that case so ret error
1599 */
1600 if (!card)
1601 return -EINVAL;
1602
Mark Brown435c5e22008-12-04 15:32:53 +00001603 /* Bodge while we unpick instantiation */
1604 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001605
Mark Brown435c5e22008-12-04 15:32:53 +00001606 ret = snd_soc_register_card(card);
1607 if (ret != 0) {
1608 dev_err(&pdev->dev, "Failed to register card\n");
1609 return ret;
1610 }
1611
1612 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001613}
1614
Vinod Koulb0e26482011-01-13 22:48:02 +05301615static int soc_cleanup_card_resources(struct snd_soc_card *card)
1616{
Vinod Koulb0e26482011-01-13 22:48:02 +05301617 int i;
1618
1619 /* make sure any delayed work runs */
1620 for (i = 0; i < card->num_rtd; i++) {
1621 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1622 flush_delayed_work_sync(&rtd->delayed_work);
1623 }
1624
1625 /* remove auxiliary devices */
1626 for (i = 0; i < card->num_aux_devs; i++)
1627 soc_remove_aux_dev(card, i);
1628
1629 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001630 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301631
1632 soc_cleanup_card_debugfs(card);
1633
1634 /* remove the card */
1635 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001636 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301637
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001638 snd_soc_dapm_free(&card->dapm);
1639
Vinod Koulb0e26482011-01-13 22:48:02 +05301640 kfree(card->rtd);
1641 snd_card_free(card->snd_card);
1642 return 0;
1643
1644}
1645
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001646/* removes a socdev */
1647static int soc_remove(struct platform_device *pdev)
1648{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001649 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001650
Mark Brownc5af3a22008-11-28 13:29:45 +00001651 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001652 return 0;
1653}
1654
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001655int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001656{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001657 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001658 int i;
Mark Brown51737472009-06-22 13:16:51 +01001659
1660 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001661 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001662
1663 /* Flush out pmdown_time work - we actually do want to run it
1664 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001665 for (i = 0; i < card->num_rtd; i++) {
1666 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001667 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001668 }
Mark Brown51737472009-06-22 13:16:51 +01001669
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001670 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001671
1672 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001673}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001674EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001675
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001676const struct dev_pm_ops snd_soc_pm_ops = {
1677 .suspend = snd_soc_suspend,
1678 .resume = snd_soc_resume,
1679 .poweroff = snd_soc_poweroff,
Mark Brown416356f2009-06-30 19:05:15 +01001680};
Stephen Warrendeb26072011-04-05 19:35:30 -06001681EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001682
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001683/* ASoC platform driver */
1684static struct platform_driver soc_driver = {
1685 .driver = {
1686 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001687 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001688 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001689 },
1690 .probe = soc_probe,
1691 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001692};
1693
Mark Brown096e49d2009-07-05 15:12:22 +01001694/**
1695 * snd_soc_codec_volatile_register: Report if a register is volatile.
1696 *
1697 * @codec: CODEC to query.
1698 * @reg: Register to query.
1699 *
1700 * Boolean function indiciating if a CODEC register is volatile.
1701 */
Mark Brown181e0552011-01-24 14:05:25 +00001702int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1703 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001704{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001705 if (codec->volatile_register)
1706 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001707 else
1708 return 0;
1709}
1710EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1711
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001712/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001713 * snd_soc_codec_readable_register: Report if a register is readable.
1714 *
1715 * @codec: CODEC to query.
1716 * @reg: Register to query.
1717 *
1718 * Boolean function indicating if a CODEC register is readable.
1719 */
1720int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1721 unsigned int reg)
1722{
1723 if (codec->readable_register)
1724 return codec->readable_register(codec, reg);
1725 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001726 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001727}
1728EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1729
1730/**
1731 * snd_soc_codec_writable_register: Report if a register is writable.
1732 *
1733 * @codec: CODEC to query.
1734 * @reg: Register to query.
1735 *
1736 * Boolean function indicating if a CODEC register is writable.
1737 */
1738int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1739 unsigned int reg)
1740{
1741 if (codec->writable_register)
1742 return codec->writable_register(codec, reg);
1743 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001744 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001745}
1746EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1747
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001748int snd_soc_platform_read(struct snd_soc_platform *platform,
1749 unsigned int reg)
1750{
1751 unsigned int ret;
1752
1753 if (!platform->driver->read) {
1754 dev_err(platform->dev, "platform has no read back\n");
1755 return -1;
1756 }
1757
1758 ret = platform->driver->read(platform, reg);
1759 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001760 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001761
1762 return ret;
1763}
1764EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1765
1766int snd_soc_platform_write(struct snd_soc_platform *platform,
1767 unsigned int reg, unsigned int val)
1768{
1769 if (!platform->driver->write) {
1770 dev_err(platform->dev, "platform has no write back\n");
1771 return -1;
1772 }
1773
1774 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001775 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001776 return platform->driver->write(platform, reg, val);
1777}
1778EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1779
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001780/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001781 * snd_soc_new_ac97_codec - initailise AC97 device
1782 * @codec: audio codec
1783 * @ops: AC97 bus operations
1784 * @num: AC97 codec number
1785 *
1786 * Initialises AC97 codec resources for use by ad-hoc devices only.
1787 */
1788int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1789 struct snd_ac97_bus_ops *ops, int num)
1790{
1791 mutex_lock(&codec->mutex);
1792
1793 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1794 if (codec->ac97 == NULL) {
1795 mutex_unlock(&codec->mutex);
1796 return -ENOMEM;
1797 }
1798
1799 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1800 if (codec->ac97->bus == NULL) {
1801 kfree(codec->ac97);
1802 codec->ac97 = NULL;
1803 mutex_unlock(&codec->mutex);
1804 return -ENOMEM;
1805 }
1806
1807 codec->ac97->bus->ops = ops;
1808 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001809
1810 /*
1811 * Mark the AC97 device to be created by us. This way we ensure that the
1812 * device will be registered with the device subsystem later on.
1813 */
1814 codec->ac97_created = 1;
1815
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001816 mutex_unlock(&codec->mutex);
1817 return 0;
1818}
1819EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1820
1821/**
1822 * snd_soc_free_ac97_codec - free AC97 codec device
1823 * @codec: audio codec
1824 *
1825 * Frees AC97 codec device resources.
1826 */
1827void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1828{
1829 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001830#ifdef CONFIG_SND_SOC_AC97_BUS
1831 soc_unregister_ac97_dai_link(codec);
1832#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001833 kfree(codec->ac97->bus);
1834 kfree(codec->ac97);
1835 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001836 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001837 mutex_unlock(&codec->mutex);
1838}
1839EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1840
Mark Brownc3753702010-11-01 15:41:57 -04001841unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1842{
1843 unsigned int ret;
1844
Mark Brownc3acec22010-12-02 16:15:29 +00001845 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04001846 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04001847 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04001848
1849 return ret;
1850}
1851EXPORT_SYMBOL_GPL(snd_soc_read);
1852
1853unsigned int snd_soc_write(struct snd_soc_codec *codec,
1854 unsigned int reg, unsigned int val)
1855{
1856 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04001857 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00001858 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04001859}
1860EXPORT_SYMBOL_GPL(snd_soc_write);
1861
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00001862unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1863 unsigned int reg, const void *data, size_t len)
1864{
1865 return codec->bulk_write_raw(codec, reg, data, len);
1866}
1867EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1868
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001869/**
1870 * snd_soc_update_bits - update codec register bits
1871 * @codec: audio codec
1872 * @reg: codec register
1873 * @mask: register mask
1874 * @value: new value
1875 *
1876 * Writes new register value.
1877 *
Timur Tabi180c3292011-01-10 15:58:13 -06001878 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001879 */
1880int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001881 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001882{
1883 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001884 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06001885 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001886
Timur Tabi180c3292011-01-10 15:58:13 -06001887 ret = snd_soc_read(codec, reg);
1888 if (ret < 0)
1889 return ret;
1890
1891 old = ret;
Mark Brown78bf3c92011-06-03 17:09:14 +01001892 new = (old & ~mask) | (value & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001893 change = old != new;
Timur Tabi180c3292011-01-10 15:58:13 -06001894 if (change) {
1895 ret = snd_soc_write(codec, reg, new);
1896 if (ret < 0)
1897 return ret;
1898 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001899
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001900 return change;
1901}
1902EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1903
1904/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001905 * snd_soc_update_bits_locked - update codec register bits
1906 * @codec: audio codec
1907 * @reg: codec register
1908 * @mask: register mask
1909 * @value: new value
1910 *
1911 * Writes new register value, and takes the codec mutex.
1912 *
1913 * Returns 1 for change else 0.
1914 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001915int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1916 unsigned short reg, unsigned int mask,
1917 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001918{
1919 int change;
1920
1921 mutex_lock(&codec->mutex);
1922 change = snd_soc_update_bits(codec, reg, mask, value);
1923 mutex_unlock(&codec->mutex);
1924
1925 return change;
1926}
Mark Browndd1b3d52009-12-04 14:22:03 +00001927EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001928
1929/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001930 * snd_soc_test_bits - test register for change
1931 * @codec: audio codec
1932 * @reg: codec register
1933 * @mask: register mask
1934 * @value: new value
1935 *
1936 * Tests a register with a new value and checks if the new value is
1937 * different from the old value.
1938 *
1939 * Returns 1 for change else 0.
1940 */
1941int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001942 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001943{
1944 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001945 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001946
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001947 old = snd_soc_read(codec, reg);
1948 new = (old & ~mask) | value;
1949 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001950
1951 return change;
1952}
1953EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1954
1955/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001956 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1957 * @substream: the pcm substream
1958 * @hw: the hardware parameters
1959 *
1960 * Sets the substream runtime hardware parameters.
1961 */
1962int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1963 const struct snd_pcm_hardware *hw)
1964{
1965 struct snd_pcm_runtime *runtime = substream->runtime;
1966 runtime->hw.info = hw->info;
1967 runtime->hw.formats = hw->formats;
1968 runtime->hw.period_bytes_min = hw->period_bytes_min;
1969 runtime->hw.period_bytes_max = hw->period_bytes_max;
1970 runtime->hw.periods_min = hw->periods_min;
1971 runtime->hw.periods_max = hw->periods_max;
1972 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1973 runtime->hw.fifo_size = hw->fifo_size;
1974 return 0;
1975}
1976EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1977
1978/**
1979 * snd_soc_cnew - create new control
1980 * @_template: control template
1981 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001982 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00001983 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001984 *
1985 * Create a new mixer control from a template control.
1986 *
1987 * Returns 0 for success, else error.
1988 */
1989struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brownefb7ac32011-03-08 17:23:24 +00001990 void *data, char *long_name,
1991 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001992{
1993 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00001994 struct snd_kcontrol *kcontrol;
1995 char *name = NULL;
1996 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001997
1998 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001999 template.index = 0;
2000
Mark Brownefb7ac32011-03-08 17:23:24 +00002001 if (!long_name)
2002 long_name = template.name;
2003
2004 if (prefix) {
2005 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002006 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002007 if (!name)
2008 return NULL;
2009
2010 snprintf(name, name_len, "%s %s", prefix, long_name);
2011
2012 template.name = name;
2013 } else {
2014 template.name = long_name;
2015 }
2016
2017 kcontrol = snd_ctl_new1(&template, data);
2018
2019 kfree(name);
2020
2021 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002022}
2023EXPORT_SYMBOL_GPL(snd_soc_cnew);
2024
2025/**
Ian Molton3e8e1952009-01-09 00:23:21 +00002026 * snd_soc_add_controls - add an array of controls to a codec.
2027 * Convienience function to add a list of controls. Many codecs were
2028 * duplicating this code.
2029 *
2030 * @codec: codec to add controls to
2031 * @controls: array of controls to add
2032 * @num_controls: number of elements in the array
2033 *
2034 * Return 0 for success, else error.
2035 */
2036int snd_soc_add_controls(struct snd_soc_codec *codec,
2037 const struct snd_kcontrol_new *controls, int num_controls)
2038{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002039 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002040 int err, i;
2041
2042 for (i = 0; i < num_controls; i++) {
2043 const struct snd_kcontrol_new *control = &controls[i];
Mark Brownefb7ac32011-03-08 17:23:24 +00002044 err = snd_ctl_add(card, snd_soc_cnew(control, codec,
2045 control->name,
2046 codec->name_prefix));
Ian Molton3e8e1952009-01-09 00:23:21 +00002047 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002048 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
Mark Brownefb7ac32011-03-08 17:23:24 +00002049 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002050 return err;
2051 }
2052 }
2053
2054 return 0;
2055}
2056EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2057
2058/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002059 * snd_soc_add_platform_controls - add an array of controls to a platform.
2060 * Convienience function to add a list of controls.
2061 *
2062 * @platform: platform to add controls to
2063 * @controls: array of controls to add
2064 * @num_controls: number of elements in the array
2065 *
2066 * Return 0 for success, else error.
2067 */
2068int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2069 const struct snd_kcontrol_new *controls, int num_controls)
2070{
2071 struct snd_card *card = platform->card->snd_card;
2072 int err, i;
2073
2074 for (i = 0; i < num_controls; i++) {
2075 const struct snd_kcontrol_new *control = &controls[i];
2076 err = snd_ctl_add(card, snd_soc_cnew(control, platform,
2077 control->name, NULL));
2078 if (err < 0) {
2079 dev_err(platform->dev, "Failed to add %s %d\n",control->name, err);
2080 return err;
2081 }
2082 }
2083
2084 return 0;
2085}
2086EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2087
2088/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002089 * snd_soc_info_enum_double - enumerated double mixer info callback
2090 * @kcontrol: mixer control
2091 * @uinfo: control element information
2092 *
2093 * Callback to provide information about a double enumerated
2094 * mixer control.
2095 *
2096 * Returns 0 for success.
2097 */
2098int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2099 struct snd_ctl_elem_info *uinfo)
2100{
2101 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2102
2103 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2104 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002105 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002106
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002107 if (uinfo->value.enumerated.item > e->max - 1)
2108 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002109 strcpy(uinfo->value.enumerated.name,
2110 e->texts[uinfo->value.enumerated.item]);
2111 return 0;
2112}
2113EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2114
2115/**
2116 * snd_soc_get_enum_double - enumerated double mixer get callback
2117 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002118 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002119 *
2120 * Callback to get the value of a double enumerated mixer.
2121 *
2122 * Returns 0 for success.
2123 */
2124int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2125 struct snd_ctl_elem_value *ucontrol)
2126{
2127 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2128 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002129 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002130
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002131 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002132 ;
2133 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002134 ucontrol->value.enumerated.item[0]
2135 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002136 if (e->shift_l != e->shift_r)
2137 ucontrol->value.enumerated.item[1] =
2138 (val >> e->shift_r) & (bitmask - 1);
2139
2140 return 0;
2141}
2142EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2143
2144/**
2145 * snd_soc_put_enum_double - enumerated double mixer put callback
2146 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002147 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002148 *
2149 * Callback to set the value of a double enumerated mixer.
2150 *
2151 * Returns 0 for success.
2152 */
2153int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2154 struct snd_ctl_elem_value *ucontrol)
2155{
2156 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2157 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002158 unsigned int val;
2159 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002160
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002161 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002162 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002163 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002164 return -EINVAL;
2165 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2166 mask = (bitmask - 1) << e->shift_l;
2167 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002168 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002169 return -EINVAL;
2170 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2171 mask |= (bitmask - 1) << e->shift_r;
2172 }
2173
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002174 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002175}
2176EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2177
2178/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002179 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2180 * @kcontrol: mixer control
2181 * @ucontrol: control element information
2182 *
2183 * Callback to get the value of a double semi enumerated mixer.
2184 *
2185 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2186 * used for handling bitfield coded enumeration for example.
2187 *
2188 * Returns 0 for success.
2189 */
2190int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2191 struct snd_ctl_elem_value *ucontrol)
2192{
2193 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002194 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002195 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002196
2197 reg_val = snd_soc_read(codec, e->reg);
2198 val = (reg_val >> e->shift_l) & e->mask;
2199 for (mux = 0; mux < e->max; mux++) {
2200 if (val == e->values[mux])
2201 break;
2202 }
2203 ucontrol->value.enumerated.item[0] = mux;
2204 if (e->shift_l != e->shift_r) {
2205 val = (reg_val >> e->shift_r) & e->mask;
2206 for (mux = 0; mux < e->max; mux++) {
2207 if (val == e->values[mux])
2208 break;
2209 }
2210 ucontrol->value.enumerated.item[1] = mux;
2211 }
2212
2213 return 0;
2214}
2215EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2216
2217/**
2218 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2219 * @kcontrol: mixer control
2220 * @ucontrol: control element information
2221 *
2222 * Callback to set the value of a double semi enumerated mixer.
2223 *
2224 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2225 * used for handling bitfield coded enumeration for example.
2226 *
2227 * Returns 0 for success.
2228 */
2229int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2230 struct snd_ctl_elem_value *ucontrol)
2231{
2232 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002233 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002234 unsigned int val;
2235 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002236
2237 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2238 return -EINVAL;
2239 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2240 mask = e->mask << e->shift_l;
2241 if (e->shift_l != e->shift_r) {
2242 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2243 return -EINVAL;
2244 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2245 mask |= e->mask << e->shift_r;
2246 }
2247
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002248 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002249}
2250EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2251
2252/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002253 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2254 * @kcontrol: mixer control
2255 * @uinfo: control element information
2256 *
2257 * Callback to provide information about an external enumerated
2258 * single mixer.
2259 *
2260 * Returns 0 for success.
2261 */
2262int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2263 struct snd_ctl_elem_info *uinfo)
2264{
2265 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2266
2267 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2268 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002269 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002270
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002271 if (uinfo->value.enumerated.item > e->max - 1)
2272 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002273 strcpy(uinfo->value.enumerated.name,
2274 e->texts[uinfo->value.enumerated.item]);
2275 return 0;
2276}
2277EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2278
2279/**
2280 * snd_soc_info_volsw_ext - external single mixer info callback
2281 * @kcontrol: mixer control
2282 * @uinfo: control element information
2283 *
2284 * Callback to provide information about a single external mixer control.
2285 *
2286 * Returns 0 for success.
2287 */
2288int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2289 struct snd_ctl_elem_info *uinfo)
2290{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002291 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002292
Mark Brownfd5dfad2009-04-15 21:37:46 +01002293 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002294 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2295 else
2296 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2297
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002298 uinfo->count = 1;
2299 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002300 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002301 return 0;
2302}
2303EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2304
2305/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002306 * snd_soc_info_volsw - single mixer info callback
2307 * @kcontrol: mixer control
2308 * @uinfo: control element information
2309 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002310 * Callback to provide information about a single mixer control, or a double
2311 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002312 *
2313 * Returns 0 for success.
2314 */
2315int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2316 struct snd_ctl_elem_info *uinfo)
2317{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002318 struct soc_mixer_control *mc =
2319 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002320 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002321
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002322 if (!mc->platform_max)
2323 mc->platform_max = mc->max;
2324 platform_max = mc->platform_max;
2325
2326 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002327 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2328 else
2329 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2330
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002331 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002332 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002333 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002334 return 0;
2335}
2336EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2337
2338/**
2339 * snd_soc_get_volsw - single mixer get callback
2340 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002341 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002342 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002343 * Callback to get the value of a single mixer control, or a double mixer
2344 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002345 *
2346 * Returns 0 for success.
2347 */
2348int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2349 struct snd_ctl_elem_value *ucontrol)
2350{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002351 struct soc_mixer_control *mc =
2352 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002353 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002354 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002355 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002356 unsigned int shift = mc->shift;
2357 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002358 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002359 unsigned int mask = (1 << fls(max)) - 1;
2360 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002361
2362 ucontrol->value.integer.value[0] =
2363 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002364 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002365 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002366 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002367
2368 if (snd_soc_volsw_is_stereo(mc)) {
2369 if (reg == reg2)
2370 ucontrol->value.integer.value[1] =
2371 (snd_soc_read(codec, reg) >> rshift) & mask;
2372 else
2373 ucontrol->value.integer.value[1] =
2374 (snd_soc_read(codec, reg2) >> shift) & mask;
2375 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002376 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002377 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002378 }
2379
2380 return 0;
2381}
2382EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2383
2384/**
2385 * snd_soc_put_volsw - single mixer put callback
2386 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002387 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002388 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002389 * Callback to set the value of a single mixer control, or a double mixer
2390 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002391 *
2392 * Returns 0 for success.
2393 */
2394int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2395 struct snd_ctl_elem_value *ucontrol)
2396{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002397 struct soc_mixer_control *mc =
2398 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002399 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002400 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002401 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002402 unsigned int shift = mc->shift;
2403 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002404 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002405 unsigned int mask = (1 << fls(max)) - 1;
2406 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002407 int err;
2408 bool type_2r = 0;
2409 unsigned int val2 = 0;
2410 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002411
2412 val = (ucontrol->value.integer.value[0] & mask);
2413 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002414 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002415 val_mask = mask << shift;
2416 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002417 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002418 val2 = (ucontrol->value.integer.value[1] & mask);
2419 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002420 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002421 if (reg == reg2) {
2422 val_mask |= mask << rshift;
2423 val |= val2 << rshift;
2424 } else {
2425 val2 = val2 << shift;
2426 type_2r = 1;
2427 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002428 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002429 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002430 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002431 return err;
2432
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002433 if (type_2r)
2434 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2435
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002436 return err;
2437}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002438EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002439
Mark Browne13ac2e2008-05-28 17:58:05 +01002440/**
2441 * snd_soc_info_volsw_s8 - signed mixer info callback
2442 * @kcontrol: mixer control
2443 * @uinfo: control element information
2444 *
2445 * Callback to provide information about a signed mixer control.
2446 *
2447 * Returns 0 for success.
2448 */
2449int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2450 struct snd_ctl_elem_info *uinfo)
2451{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002452 struct soc_mixer_control *mc =
2453 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002454 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002455 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002456
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002457 if (!mc->platform_max)
2458 mc->platform_max = mc->max;
2459 platform_max = mc->platform_max;
2460
Mark Browne13ac2e2008-05-28 17:58:05 +01002461 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2462 uinfo->count = 2;
2463 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002464 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002465 return 0;
2466}
2467EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2468
2469/**
2470 * snd_soc_get_volsw_s8 - signed mixer get callback
2471 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002472 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002473 *
2474 * Callback to get the value of a signed mixer control.
2475 *
2476 * Returns 0 for success.
2477 */
2478int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2479 struct snd_ctl_elem_value *ucontrol)
2480{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002481 struct soc_mixer_control *mc =
2482 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002483 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002484 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002485 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002486 int val = snd_soc_read(codec, reg);
2487
2488 ucontrol->value.integer.value[0] =
2489 ((signed char)(val & 0xff))-min;
2490 ucontrol->value.integer.value[1] =
2491 ((signed char)((val >> 8) & 0xff))-min;
2492 return 0;
2493}
2494EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2495
2496/**
2497 * snd_soc_put_volsw_sgn - signed mixer put callback
2498 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002499 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002500 *
2501 * Callback to set the value of a signed mixer control.
2502 *
2503 * Returns 0 for success.
2504 */
2505int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2506 struct snd_ctl_elem_value *ucontrol)
2507{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002508 struct soc_mixer_control *mc =
2509 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002510 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002511 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002512 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002513 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002514
2515 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2516 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2517
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002518 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002519}
2520EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2521
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002522/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002523 * snd_soc_limit_volume - Set new limit to an existing volume control.
2524 *
2525 * @codec: where to look for the control
2526 * @name: Name of the control
2527 * @max: new maximum limit
2528 *
2529 * Return 0 for success, else error.
2530 */
2531int snd_soc_limit_volume(struct snd_soc_codec *codec,
2532 const char *name, int max)
2533{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002534 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002535 struct snd_kcontrol *kctl;
2536 struct soc_mixer_control *mc;
2537 int found = 0;
2538 int ret = -EINVAL;
2539
2540 /* Sanity check for name and max */
2541 if (unlikely(!name || max <= 0))
2542 return -EINVAL;
2543
2544 list_for_each_entry(kctl, &card->controls, list) {
2545 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2546 found = 1;
2547 break;
2548 }
2549 }
2550 if (found) {
2551 mc = (struct soc_mixer_control *)kctl->private_value;
2552 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002553 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002554 ret = 0;
2555 }
2556 }
2557 return ret;
2558}
2559EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2560
2561/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002562 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2563 * mixer info callback
2564 * @kcontrol: mixer control
2565 * @uinfo: control element information
2566 *
2567 * Returns 0 for success.
2568 */
2569int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2570 struct snd_ctl_elem_info *uinfo)
2571{
2572 struct soc_mixer_control *mc =
2573 (struct soc_mixer_control *)kcontrol->private_value;
2574 int max = mc->max;
2575 int min = mc->min;
2576
2577 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2578 uinfo->count = 2;
2579 uinfo->value.integer.min = 0;
2580 uinfo->value.integer.max = max-min;
2581
2582 return 0;
2583}
2584EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2585
2586/**
2587 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2588 * mixer get callback
2589 * @kcontrol: mixer control
2590 * @uinfo: control element information
2591 *
2592 * Returns 0 for success.
2593 */
2594int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2595 struct snd_ctl_elem_value *ucontrol)
2596{
2597 struct soc_mixer_control *mc =
2598 (struct soc_mixer_control *)kcontrol->private_value;
2599 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2600 unsigned int mask = (1<<mc->shift)-1;
2601 int min = mc->min;
2602 int val = snd_soc_read(codec, mc->reg) & mask;
2603 int valr = snd_soc_read(codec, mc->rreg) & mask;
2604
Stuart Longland20630c72010-06-18 12:56:10 +10002605 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2606 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002607 return 0;
2608}
2609EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2610
2611/**
2612 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2613 * mixer put callback
2614 * @kcontrol: mixer control
2615 * @uinfo: control element information
2616 *
2617 * Returns 0 for success.
2618 */
2619int snd_soc_put_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 ret;
2628 unsigned int val, valr, oval, ovalr;
2629
2630 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2631 val &= mask;
2632 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2633 valr &= mask;
2634
2635 oval = snd_soc_read(codec, mc->reg) & mask;
2636 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2637
2638 ret = 0;
2639 if (oval != val) {
2640 ret = snd_soc_write(codec, mc->reg, val);
2641 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002642 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002643 }
2644 if (ovalr != valr) {
2645 ret = snd_soc_write(codec, mc->rreg, valr);
2646 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002647 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002648 }
2649
2650 return 0;
2651}
2652EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2653
2654/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002655 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2656 * @dai: DAI
2657 * @clk_id: DAI specific clock ID
2658 * @freq: new clock frequency in Hz
2659 * @dir: new clock direction - input/output.
2660 *
2661 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2662 */
2663int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2664 unsigned int freq, int dir)
2665{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002666 if (dai->driver && dai->driver->ops->set_sysclk)
2667 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002668 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002669 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002670 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002671 else
2672 return -EINVAL;
2673}
2674EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2675
2676/**
Mark Brownec4ee522011-03-07 20:58:11 +00002677 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2678 * @codec: CODEC
2679 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002680 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002681 * @freq: new clock frequency in Hz
2682 * @dir: new clock direction - input/output.
2683 *
2684 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2685 */
2686int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002687 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002688{
2689 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002690 return codec->driver->set_sysclk(codec, clk_id, source,
2691 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002692 else
2693 return -EINVAL;
2694}
2695EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2696
2697/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002698 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2699 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002700 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002701 * @div: new clock divisor.
2702 *
2703 * Configures the clock dividers. This is used to derive the best DAI bit and
2704 * frame clocks from the system or master clock. It's best to set the DAI bit
2705 * and frame clocks as low as possible to save system power.
2706 */
2707int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2708 int div_id, int div)
2709{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002710 if (dai->driver && dai->driver->ops->set_clkdiv)
2711 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002712 else
2713 return -EINVAL;
2714}
2715EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2716
2717/**
2718 * snd_soc_dai_set_pll - configure DAI PLL.
2719 * @dai: DAI
2720 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002721 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002722 * @freq_in: PLL input clock frequency in Hz
2723 * @freq_out: requested PLL output clock frequency in Hz
2724 *
2725 * Configures and enables PLL to generate output clock based on input clock.
2726 */
Mark Brown85488032009-09-05 18:52:16 +01002727int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2728 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002729{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002730 if (dai->driver && dai->driver->ops->set_pll)
2731 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002732 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002733 else if (dai->codec && dai->codec->driver->set_pll)
2734 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2735 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002736 else
2737 return -EINVAL;
2738}
2739EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2740
Mark Brownec4ee522011-03-07 20:58:11 +00002741/*
2742 * snd_soc_codec_set_pll - configure codec PLL.
2743 * @codec: CODEC
2744 * @pll_id: DAI specific PLL ID
2745 * @source: DAI specific source for the PLL
2746 * @freq_in: PLL input clock frequency in Hz
2747 * @freq_out: requested PLL output clock frequency in Hz
2748 *
2749 * Configures and enables PLL to generate output clock based on input clock.
2750 */
2751int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2752 unsigned int freq_in, unsigned int freq_out)
2753{
2754 if (codec->driver->set_pll)
2755 return codec->driver->set_pll(codec, pll_id, source,
2756 freq_in, freq_out);
2757 else
2758 return -EINVAL;
2759}
2760EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2761
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002762/**
2763 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2764 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002765 * @fmt: SND_SOC_DAIFMT_ format value.
2766 *
2767 * Configures the DAI hardware format and clocking.
2768 */
2769int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2770{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002771 if (dai->driver && dai->driver->ops->set_fmt)
2772 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002773 else
2774 return -EINVAL;
2775}
2776EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2777
2778/**
2779 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2780 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002781 * @tx_mask: bitmask representing active TX slots.
2782 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002783 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002784 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002785 *
2786 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2787 * specific.
2788 */
2789int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002790 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002791{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002792 if (dai->driver && dai->driver->ops->set_tdm_slot)
2793 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002794 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002795 else
2796 return -EINVAL;
2797}
2798EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2799
2800/**
Barry Song472df3c2009-09-12 01:16:29 +08002801 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2802 * @dai: DAI
2803 * @tx_num: how many TX channels
2804 * @tx_slot: pointer to an array which imply the TX slot number channel
2805 * 0~num-1 uses
2806 * @rx_num: how many RX channels
2807 * @rx_slot: pointer to an array which imply the RX slot number channel
2808 * 0~num-1 uses
2809 *
2810 * configure the relationship between channel number and TDM slot number.
2811 */
2812int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2813 unsigned int tx_num, unsigned int *tx_slot,
2814 unsigned int rx_num, unsigned int *rx_slot)
2815{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002816 if (dai->driver && dai->driver->ops->set_channel_map)
2817 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002818 rx_num, rx_slot);
2819 else
2820 return -EINVAL;
2821}
2822EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2823
2824/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002825 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2826 * @dai: DAI
2827 * @tristate: tristate enable
2828 *
2829 * Tristates the DAI so that others can use it.
2830 */
2831int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2832{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002833 if (dai->driver && dai->driver->ops->set_tristate)
2834 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002835 else
2836 return -EINVAL;
2837}
2838EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2839
2840/**
2841 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2842 * @dai: DAI
2843 * @mute: mute enable
2844 *
2845 * Mutes the DAI DAC.
2846 */
2847int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2848{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002849 if (dai->driver && dai->driver->ops->digital_mute)
2850 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002851 else
2852 return -EINVAL;
2853}
2854EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2855
Mark Brownc5af3a22008-11-28 13:29:45 +00002856/**
2857 * snd_soc_register_card - Register a card with the ASoC core
2858 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002859 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002860 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002861 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302862int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002863{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002864 int i;
2865
Mark Brownc5af3a22008-11-28 13:29:45 +00002866 if (!card->name || !card->dev)
2867 return -EINVAL;
2868
Stephen Warren5a504962011-12-21 10:40:59 -07002869 for (i = 0; i < card->num_links; i++) {
2870 struct snd_soc_dai_link *link = &card->dai_link[i];
2871
2872 /*
2873 * Codec must be specified by 1 of name or OF node,
2874 * not both or neither.
2875 */
2876 if (!!link->codec_name == !!link->codec_of_node) {
2877 dev_err(card->dev,
2878 "Neither/both codec name/of_node are set\n");
2879 return -EINVAL;
2880 }
2881
2882 /*
2883 * Platform may be specified by either name or OF node, but
2884 * can be left unspecified, and a dummy platform will be used.
2885 */
2886 if (link->platform_name && link->platform_of_node) {
2887 dev_err(card->dev,
2888 "Both platform name/of_node are set\n");
2889 return -EINVAL;
2890 }
2891
2892 /*
2893 * CPU DAI must be specified by 1 of name or OF node,
2894 * not both or neither.
2895 */
2896 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
2897 dev_err(card->dev,
2898 "Neither/both cpu_dai name/of_node are set\n");
2899 return -EINVAL;
2900 }
2901 }
2902
Mark Browned77cc12011-05-03 18:25:34 +01002903 dev_set_drvdata(card->dev, card);
2904
Stephen Warren111c6412011-01-28 14:26:35 -07002905 snd_soc_initialize_card_lists(card);
2906
Vinod Koul150dd2f2011-01-13 22:48:32 +05302907 soc_init_card_debugfs(card);
2908
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002909 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
2910 (card->num_links + card->num_aux_devs),
2911 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002912 if (card->rtd == NULL)
2913 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002914 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002915
2916 for (i = 0; i < card->num_links; i++)
2917 card->rtd[i].dai_link = &card->dai_link[i];
2918
Mark Brownc5af3a22008-11-28 13:29:45 +00002919 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01002920 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00002921 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002922 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002923
2924 mutex_lock(&client_mutex);
2925 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002926 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002927 mutex_unlock(&client_mutex);
2928
2929 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2930
2931 return 0;
2932}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302933EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002934
2935/**
2936 * snd_soc_unregister_card - Unregister a card with the ASoC core
2937 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002938 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002939 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002940 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302941int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002942{
Vinod Koulb0e26482011-01-13 22:48:02 +05302943 if (card->instantiated)
2944 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002945 mutex_lock(&client_mutex);
2946 list_del(&card->list);
2947 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002948 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2949
2950 return 0;
2951}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302952EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002953
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002954/*
2955 * Simplify DAI link configuration by removing ".-1" from device names
2956 * and sanitizing names.
2957 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002958static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002959{
2960 char *found, name[NAME_SIZE];
2961 int id1, id2;
2962
2963 if (dev_name(dev) == NULL)
2964 return NULL;
2965
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002966 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002967
2968 /* are we a "%s.%d" name (platform and SPI components) */
2969 found = strstr(name, dev->driver->name);
2970 if (found) {
2971 /* get ID */
2972 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2973
2974 /* discard ID from name if ID == -1 */
2975 if (*id == -1)
2976 found[strlen(dev->driver->name)] = '\0';
2977 }
2978
2979 } else {
2980 /* I2C component devices are named "bus-addr" */
2981 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2982 char tmp[NAME_SIZE];
2983
2984 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002985 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002986
2987 /* sanitize component name for DAI link creation */
2988 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002989 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002990 } else
2991 *id = 0;
2992 }
2993
2994 return kstrdup(name, GFP_KERNEL);
2995}
2996
2997/*
2998 * Simplify DAI link naming for single devices with multiple DAIs by removing
2999 * any ".-1" and using the DAI name (instead of device name).
3000 */
3001static inline char *fmt_multiple_name(struct device *dev,
3002 struct snd_soc_dai_driver *dai_drv)
3003{
3004 if (dai_drv->name == NULL) {
3005 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3006 dev_name(dev));
3007 return NULL;
3008 }
3009
3010 return kstrdup(dai_drv->name, GFP_KERNEL);
3011}
3012
Mark Brown91151712008-11-30 23:31:24 +00003013/**
3014 * snd_soc_register_dai - Register a DAI with the ASoC core
3015 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003016 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003017 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003018int snd_soc_register_dai(struct device *dev,
3019 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003020{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003021 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003022
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003023 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003024
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003025 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3026 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003027 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003028
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003029 /* create DAI component name */
3030 dai->name = fmt_single_name(dev, &dai->id);
3031 if (dai->name == NULL) {
3032 kfree(dai);
3033 return -ENOMEM;
3034 }
3035
3036 dai->dev = dev;
3037 dai->driver = dai_drv;
3038 if (!dai->driver->ops)
3039 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003040
3041 mutex_lock(&client_mutex);
3042 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003043 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003044 mutex_unlock(&client_mutex);
3045
3046 pr_debug("Registered DAI '%s'\n", dai->name);
3047
3048 return 0;
3049}
3050EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3051
3052/**
3053 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3054 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003055 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003056 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003057void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003058{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003059 struct snd_soc_dai *dai;
3060
3061 list_for_each_entry(dai, &dai_list, list) {
3062 if (dev == dai->dev)
3063 goto found;
3064 }
3065 return;
3066
3067found:
Mark Brown91151712008-11-30 23:31:24 +00003068 mutex_lock(&client_mutex);
3069 list_del(&dai->list);
3070 mutex_unlock(&client_mutex);
3071
3072 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003073 kfree(dai->name);
3074 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003075}
3076EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3077
3078/**
3079 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3080 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003081 * @dai: Array of DAIs to register
3082 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003083 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003084int snd_soc_register_dais(struct device *dev,
3085 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003086{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003087 struct snd_soc_dai *dai;
3088 int i, ret = 0;
3089
Liam Girdwood720ffa42010-08-18 00:30:30 +01003090 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003091
3092 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003093
3094 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003095 if (dai == NULL) {
3096 ret = -ENOMEM;
3097 goto err;
3098 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003099
3100 /* create DAI component name */
3101 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3102 if (dai->name == NULL) {
3103 kfree(dai);
3104 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003105 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003106 }
3107
3108 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003109 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003110 if (dai->driver->id)
3111 dai->id = dai->driver->id;
3112 else
3113 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003114 if (!dai->driver->ops)
3115 dai->driver->ops = &null_dai_ops;
3116
3117 mutex_lock(&client_mutex);
3118 list_add(&dai->list, &dai_list);
3119 mutex_unlock(&client_mutex);
3120
3121 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003122 }
3123
Axel Lin1dcb4f32010-12-06 16:48:03 +08003124 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003125 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08003126 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00003127 return 0;
3128
3129err:
3130 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003131 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003132
3133 return ret;
3134}
3135EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3136
3137/**
3138 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3139 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003140 * @dai: Array of DAIs to unregister
3141 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003142 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003143void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003144{
3145 int i;
3146
3147 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003148 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003149}
3150EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3151
Mark Brown12a48a8c2008-12-03 19:40:30 +00003152/**
3153 * snd_soc_register_platform - Register a platform with the ASoC core
3154 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003155 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003156 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003157int snd_soc_register_platform(struct device *dev,
3158 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003159{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003160 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003161
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003162 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3163
3164 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3165 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003166 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003167
3168 /* create platform component name */
3169 platform->name = fmt_single_name(dev, &platform->id);
3170 if (platform->name == NULL) {
3171 kfree(platform);
3172 return -ENOMEM;
3173 }
3174
3175 platform->dev = dev;
3176 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003177 platform->dapm.dev = dev;
3178 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003179 platform->dapm.stream_event = platform_drv->stream_event;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003180
3181 mutex_lock(&client_mutex);
3182 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003183 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003184 mutex_unlock(&client_mutex);
3185
3186 pr_debug("Registered platform '%s'\n", platform->name);
3187
3188 return 0;
3189}
3190EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3191
3192/**
3193 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3194 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003195 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003196 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003197void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003198{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003199 struct snd_soc_platform *platform;
3200
3201 list_for_each_entry(platform, &platform_list, list) {
3202 if (dev == platform->dev)
3203 goto found;
3204 }
3205 return;
3206
3207found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003208 mutex_lock(&client_mutex);
3209 list_del(&platform->list);
3210 mutex_unlock(&client_mutex);
3211
3212 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003213 kfree(platform->name);
3214 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003215}
3216EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3217
Mark Brown151ab222009-05-09 16:22:58 +01003218static u64 codec_format_map[] = {
3219 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3220 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3221 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3222 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3223 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3224 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3225 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3226 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3227 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3228 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3229 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3230 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3231 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3232 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3233 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3234 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3235};
3236
3237/* Fix up the DAI formats for endianness: codecs don't actually see
3238 * the endianness of the data but we're using the CPU format
3239 * definitions which do need to include endianness so we ensure that
3240 * codec DAIs always have both big and little endian variants set.
3241 */
3242static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3243{
3244 int i;
3245
3246 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3247 if (stream->formats & codec_format_map[i])
3248 stream->formats |= codec_format_map[i];
3249}
3250
Mark Brown0d0cf002008-12-10 14:32:45 +00003251/**
3252 * snd_soc_register_codec - Register a codec with the ASoC core
3253 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003254 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003255 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003256int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003257 const struct snd_soc_codec_driver *codec_drv,
3258 struct snd_soc_dai_driver *dai_drv,
3259 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003260{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003261 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003262 struct snd_soc_codec *codec;
3263 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003264
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003265 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003266
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003267 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3268 if (codec == NULL)
3269 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003270
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003271 /* create CODEC component name */
3272 codec->name = fmt_single_name(dev, &codec->id);
3273 if (codec->name == NULL) {
3274 kfree(codec);
3275 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003276 }
3277
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003278 if (codec_drv->compress_type)
3279 codec->compress_type = codec_drv->compress_type;
3280 else
3281 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3282
Mark Brownc3acec22010-12-02 16:15:29 +00003283 codec->write = codec_drv->write;
3284 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003285 codec->volatile_register = codec_drv->volatile_register;
3286 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003287 codec->writable_register = codec_drv->writable_register;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003288 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3289 codec->dapm.dev = dev;
3290 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003291 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003292 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003293 codec->dev = dev;
3294 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003295 codec->num_dai = num_dai;
3296 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003297
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003298 /* allocate CODEC register cache */
3299 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003300 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003301 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003302 /* it is necessary to make a copy of the default register cache
3303 * because in the case of using a compression type that requires
3304 * the default register cache to be marked as __devinitconst the
3305 * kernel might have freed the array by the time we initialize
3306 * the cache.
3307 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003308 if (codec_drv->reg_cache_default) {
3309 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3310 reg_size, GFP_KERNEL);
3311 if (!codec->reg_def_copy) {
3312 ret = -ENOMEM;
3313 goto fail;
3314 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003315 }
3316 }
3317
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003318 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3319 if (!codec->volatile_register)
3320 codec->volatile_register = snd_soc_default_volatile_register;
3321 if (!codec->readable_register)
3322 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003323 if (!codec->writable_register)
3324 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003325 }
3326
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003327 for (i = 0; i < num_dai; i++) {
3328 fixup_codec_formats(&dai_drv[i].playback);
3329 fixup_codec_formats(&dai_drv[i].capture);
3330 }
3331
Mark Brown26b01cc2010-08-18 20:20:55 +01003332 /* register any DAIs */
3333 if (num_dai) {
3334 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3335 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003336 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003337 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003338
Mark Brown0d0cf002008-12-10 14:32:45 +00003339 mutex_lock(&client_mutex);
3340 list_add(&codec->list, &codec_list);
3341 snd_soc_instantiate_cards();
3342 mutex_unlock(&client_mutex);
3343
3344 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003345 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003346
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003347fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003348 kfree(codec->reg_def_copy);
3349 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003350 kfree(codec->name);
3351 kfree(codec);
3352 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003353}
3354EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3355
3356/**
3357 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3358 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003359 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003360 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003361void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003362{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003363 struct snd_soc_codec *codec;
3364 int i;
3365
3366 list_for_each_entry(codec, &codec_list, list) {
3367 if (dev == codec->dev)
3368 goto found;
3369 }
3370 return;
3371
3372found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003373 if (codec->num_dai)
3374 for (i = 0; i < codec->num_dai; i++)
3375 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003376
Mark Brown0d0cf002008-12-10 14:32:45 +00003377 mutex_lock(&client_mutex);
3378 list_del(&codec->list);
3379 mutex_unlock(&client_mutex);
3380
3381 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003382
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003383 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003384 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003385 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003386 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003387}
3388EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3389
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003390/* Retrieve a card's name from device tree */
3391int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3392 const char *propname)
3393{
3394 struct device_node *np = card->dev->of_node;
3395 int ret;
3396
3397 ret = of_property_read_string_index(np, propname, 0, &card->name);
3398 /*
3399 * EINVAL means the property does not exist. This is fine providing
3400 * card->name was previously set, which is checked later in
3401 * snd_soc_register_card.
3402 */
3403 if (ret < 0 && ret != -EINVAL) {
3404 dev_err(card->dev,
3405 "Property '%s' could not be read: %d\n",
3406 propname, ret);
3407 return ret;
3408 }
3409
3410 return 0;
3411}
3412EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3413
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003414int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3415 const char *propname)
3416{
3417 struct device_node *np = card->dev->of_node;
3418 int num_routes;
3419 struct snd_soc_dapm_route *routes;
3420 int i, ret;
3421
3422 num_routes = of_property_count_strings(np, propname);
3423 if (num_routes & 1) {
3424 dev_err(card->dev,
3425 "Property '%s's length is not even\n",
3426 propname);
3427 return -EINVAL;
3428 }
3429 num_routes /= 2;
3430 if (!num_routes) {
3431 dev_err(card->dev,
3432 "Property '%s's length is zero\n",
3433 propname);
3434 return -EINVAL;
3435 }
3436
3437 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3438 GFP_KERNEL);
3439 if (!routes) {
3440 dev_err(card->dev,
3441 "Could not allocate DAPM route table\n");
3442 return -EINVAL;
3443 }
3444
3445 for (i = 0; i < num_routes; i++) {
3446 ret = of_property_read_string_index(np, propname,
3447 2 * i, &routes[i].sink);
3448 if (ret) {
3449 dev_err(card->dev,
3450 "Property '%s' index %d could not be read: %d\n",
3451 propname, 2 * i, ret);
3452 return -EINVAL;
3453 }
3454 ret = of_property_read_string_index(np, propname,
3455 (2 * i) + 1, &routes[i].source);
3456 if (ret) {
3457 dev_err(card->dev,
3458 "Property '%s' index %d could not be read: %d\n",
3459 propname, (2 * i) + 1, ret);
3460 return -EINVAL;
3461 }
3462 }
3463
3464 card->num_dapm_routes = num_routes;
3465 card->dapm_routes = routes;
3466
3467 return 0;
3468}
3469EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3470
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003471static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003472{
Mark Brown384c89e2008-12-03 17:34:03 +00003473#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003474 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3475 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Mark Brown384c89e2008-12-03 17:34:03 +00003476 printk(KERN_WARNING
3477 "ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003478 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003479 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003480
Mark Brown8a9dab12011-01-10 22:25:21 +00003481 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003482 &codec_list_fops))
3483 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3484
Mark Brown8a9dab12011-01-10 22:25:21 +00003485 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003486 &dai_list_fops))
3487 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003488
Mark Brown8a9dab12011-01-10 22:25:21 +00003489 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003490 &platform_list_fops))
3491 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003492#endif
3493
Mark Brownfb257892011-04-28 10:57:54 +01003494 snd_soc_util_init();
3495
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003496 return platform_driver_register(&soc_driver);
3497}
Mark Brown4abe8e12010-10-12 17:41:03 +01003498module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003499
Mark Brown7d8c16a2008-11-30 22:11:24 +00003500static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003501{
Mark Brownfb257892011-04-28 10:57:54 +01003502 snd_soc_util_exit();
3503
Mark Brown384c89e2008-12-03 17:34:03 +00003504#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003505 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003506#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003507 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003508}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003509module_exit(snd_soc_exit);
3510
3511/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003512MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003513MODULE_DESCRIPTION("ALSA SoC Core");
3514MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003515MODULE_ALIAS("platform:soc-audio");