blob: 8bd999530a76b18d6f1ab1032691fab95bc39a46 [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 Brown89b95ac2011-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 Nikula70d293312011-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 Brown89b95ac2011-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 Nikula70d293312011-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)
Liam Girdwood022658b2012-02-03 17:43:09 +00001010 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001011 driver->num_controls);
Mark Brown89b95ac2011-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 Nikula70d293312011-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 Nikula70d293312011-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 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001472 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001473
Mark Brownb8ad29d2011-03-02 18:35:51 +00001474 if (card->dapm_routes)
1475 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1476 card->num_dapm_routes);
1477
Mark Brownb90d2f92011-10-10 13:38:06 +01001478 snd_soc_dapm_new_widgets(&card->dapm);
1479
Mark Brown75d9ac42011-09-27 16:41:01 +01001480 for (i = 0; i < card->num_links; i++) {
1481 dai_link = &card->dai_link[i];
1482
1483 if (dai_link->dai_fmt) {
1484 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1485 dai_link->dai_fmt);
1486 if (ret != 0)
1487 dev_warn(card->rtd[i].codec_dai->dev,
1488 "Failed to set DAI format: %d\n",
1489 ret);
1490
1491 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1492 dai_link->dai_fmt);
1493 if (ret != 0)
1494 dev_warn(card->rtd[i].cpu_dai->dev,
1495 "Failed to set DAI format: %d\n",
1496 ret);
1497 }
1498 }
1499
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001500 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001501 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001502 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1503 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001504 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1505 "%s", card->driver_name ? card->driver_name : card->name);
1506 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1507 switch (card->snd_card->driver[i]) {
1508 case '_':
1509 case '-':
1510 case '\0':
1511 break;
1512 default:
1513 if (!isalnum(card->snd_card->driver[i]))
1514 card->snd_card->driver[i] = '_';
1515 break;
1516 }
1517 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001518
Mark Brown28e9ad92011-03-02 18:36:34 +00001519 if (card->late_probe) {
1520 ret = card->late_probe(card);
1521 if (ret < 0) {
1522 dev_err(card->dev, "%s late_probe() failed: %d\n",
1523 card->name, ret);
1524 goto probe_aux_dev_err;
1525 }
1526 }
1527
Mark Brown2dc00212011-10-08 13:59:44 +01001528 snd_soc_dapm_new_widgets(&card->dapm);
1529
Stephen Warren16332812011-11-23 12:42:04 -07001530 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001531 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001532 snd_soc_dapm_auto_nc_codec_pins(codec);
1533
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001534 ret = snd_card_register(card->snd_card);
1535 if (ret < 0) {
1536 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
Axel Lin6b3ed782010-12-07 16:12:29 +08001537 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001538 }
1539
1540#ifdef CONFIG_SND_SOC_AC97_BUS
1541 /* register any AC97 codecs */
1542 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001543 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1544 if (ret < 0) {
1545 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1546 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001547 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001548 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001549 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001550 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001551#endif
1552
Mark Brown435c5e22008-12-04 15:32:53 +00001553 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001554 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001555 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001556 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001557
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001558probe_aux_dev_err:
1559 for (i = 0; i < card->num_aux_devs; i++)
1560 soc_remove_aux_dev(card, i);
1561
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001562probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001563 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001564
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001565card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001566 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001567 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001568
1569 snd_card_free(card->snd_card);
1570
1571 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001572}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001573
Mark Brown435c5e22008-12-04 15:32:53 +00001574/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001575 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001576 * client_mutex.
1577 */
1578static void snd_soc_instantiate_cards(void)
1579{
1580 struct snd_soc_card *card;
1581 list_for_each_entry(card, &card_list, list)
1582 snd_soc_instantiate_card(card);
1583}
1584
1585/* probes a new socdev */
1586static int soc_probe(struct platform_device *pdev)
1587{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001588 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001589 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001590
Vinod Koul70a7ca32011-01-14 19:22:48 +05301591 /*
1592 * no card, so machine driver should be registering card
1593 * we should not be here in that case so ret error
1594 */
1595 if (!card)
1596 return -EINVAL;
1597
Mark Brown435c5e22008-12-04 15:32:53 +00001598 /* Bodge while we unpick instantiation */
1599 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001600
Mark Brown435c5e22008-12-04 15:32:53 +00001601 ret = snd_soc_register_card(card);
1602 if (ret != 0) {
1603 dev_err(&pdev->dev, "Failed to register card\n");
1604 return ret;
1605 }
1606
1607 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001608}
1609
Vinod Koulb0e26482011-01-13 22:48:02 +05301610static int soc_cleanup_card_resources(struct snd_soc_card *card)
1611{
Vinod Koulb0e26482011-01-13 22:48:02 +05301612 int i;
1613
1614 /* make sure any delayed work runs */
1615 for (i = 0; i < card->num_rtd; i++) {
1616 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1617 flush_delayed_work_sync(&rtd->delayed_work);
1618 }
1619
1620 /* remove auxiliary devices */
1621 for (i = 0; i < card->num_aux_devs; i++)
1622 soc_remove_aux_dev(card, i);
1623
1624 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001625 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301626
1627 soc_cleanup_card_debugfs(card);
1628
1629 /* remove the card */
1630 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001631 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301632
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001633 snd_soc_dapm_free(&card->dapm);
1634
Vinod Koulb0e26482011-01-13 22:48:02 +05301635 kfree(card->rtd);
1636 snd_card_free(card->snd_card);
1637 return 0;
1638
1639}
1640
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001641/* removes a socdev */
1642static int soc_remove(struct platform_device *pdev)
1643{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001644 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001645
Mark Brownc5af3a22008-11-28 13:29:45 +00001646 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001647 return 0;
1648}
1649
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001650int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001651{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001652 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001653 int i;
Mark Brown51737472009-06-22 13:16:51 +01001654
1655 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001656 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001657
1658 /* Flush out pmdown_time work - we actually do want to run it
1659 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001660 for (i = 0; i < card->num_rtd; i++) {
1661 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001662 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001663 }
Mark Brown51737472009-06-22 13:16:51 +01001664
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001665 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001666
1667 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001668}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001669EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001670
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001671const struct dev_pm_ops snd_soc_pm_ops = {
Mark Brown5aa44b12012-01-31 15:30:28 +00001672 SET_SYSTEM_SLEEP_PM_OPS(snd_soc_suspend, snd_soc_resume)
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001673 .poweroff = snd_soc_poweroff,
Mark Brown416356f2009-06-30 19:05:15 +01001674};
Stephen Warrendeb26072011-04-05 19:35:30 -06001675EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001676
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001677/* ASoC platform driver */
1678static struct platform_driver soc_driver = {
1679 .driver = {
1680 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001681 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001682 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001683 },
1684 .probe = soc_probe,
1685 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001686};
1687
Mark Brown096e49d2009-07-05 15:12:22 +01001688/**
1689 * snd_soc_codec_volatile_register: Report if a register is volatile.
1690 *
1691 * @codec: CODEC to query.
1692 * @reg: Register to query.
1693 *
1694 * Boolean function indiciating if a CODEC register is volatile.
1695 */
Mark Brown181e0552011-01-24 14:05:25 +00001696int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1697 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001698{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001699 if (codec->volatile_register)
1700 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001701 else
1702 return 0;
1703}
1704EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1705
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001706/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001707 * snd_soc_codec_readable_register: Report if a register is readable.
1708 *
1709 * @codec: CODEC to query.
1710 * @reg: Register to query.
1711 *
1712 * Boolean function indicating if a CODEC register is readable.
1713 */
1714int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1715 unsigned int reg)
1716{
1717 if (codec->readable_register)
1718 return codec->readable_register(codec, reg);
1719 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001720 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001721}
1722EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1723
1724/**
1725 * snd_soc_codec_writable_register: Report if a register is writable.
1726 *
1727 * @codec: CODEC to query.
1728 * @reg: Register to query.
1729 *
1730 * Boolean function indicating if a CODEC register is writable.
1731 */
1732int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1733 unsigned int reg)
1734{
1735 if (codec->writable_register)
1736 return codec->writable_register(codec, reg);
1737 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001738 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001739}
1740EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1741
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001742int snd_soc_platform_read(struct snd_soc_platform *platform,
1743 unsigned int reg)
1744{
1745 unsigned int ret;
1746
1747 if (!platform->driver->read) {
1748 dev_err(platform->dev, "platform has no read back\n");
1749 return -1;
1750 }
1751
1752 ret = platform->driver->read(platform, reg);
1753 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001754 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001755
1756 return ret;
1757}
1758EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1759
1760int snd_soc_platform_write(struct snd_soc_platform *platform,
1761 unsigned int reg, unsigned int val)
1762{
1763 if (!platform->driver->write) {
1764 dev_err(platform->dev, "platform has no write back\n");
1765 return -1;
1766 }
1767
1768 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001769 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001770 return platform->driver->write(platform, reg, val);
1771}
1772EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1773
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001774/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001775 * snd_soc_new_ac97_codec - initailise AC97 device
1776 * @codec: audio codec
1777 * @ops: AC97 bus operations
1778 * @num: AC97 codec number
1779 *
1780 * Initialises AC97 codec resources for use by ad-hoc devices only.
1781 */
1782int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1783 struct snd_ac97_bus_ops *ops, int num)
1784{
1785 mutex_lock(&codec->mutex);
1786
1787 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1788 if (codec->ac97 == NULL) {
1789 mutex_unlock(&codec->mutex);
1790 return -ENOMEM;
1791 }
1792
1793 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1794 if (codec->ac97->bus == NULL) {
1795 kfree(codec->ac97);
1796 codec->ac97 = NULL;
1797 mutex_unlock(&codec->mutex);
1798 return -ENOMEM;
1799 }
1800
1801 codec->ac97->bus->ops = ops;
1802 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001803
1804 /*
1805 * Mark the AC97 device to be created by us. This way we ensure that the
1806 * device will be registered with the device subsystem later on.
1807 */
1808 codec->ac97_created = 1;
1809
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001810 mutex_unlock(&codec->mutex);
1811 return 0;
1812}
1813EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1814
1815/**
1816 * snd_soc_free_ac97_codec - free AC97 codec device
1817 * @codec: audio codec
1818 *
1819 * Frees AC97 codec device resources.
1820 */
1821void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1822{
1823 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001824#ifdef CONFIG_SND_SOC_AC97_BUS
1825 soc_unregister_ac97_dai_link(codec);
1826#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001827 kfree(codec->ac97->bus);
1828 kfree(codec->ac97);
1829 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001830 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001831 mutex_unlock(&codec->mutex);
1832}
1833EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1834
Mark Brownc3753702010-11-01 15:41:57 -04001835unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1836{
1837 unsigned int ret;
1838
Mark Brownc3acec22010-12-02 16:15:29 +00001839 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04001840 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04001841 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04001842
1843 return ret;
1844}
1845EXPORT_SYMBOL_GPL(snd_soc_read);
1846
1847unsigned int snd_soc_write(struct snd_soc_codec *codec,
1848 unsigned int reg, unsigned int val)
1849{
1850 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04001851 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00001852 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04001853}
1854EXPORT_SYMBOL_GPL(snd_soc_write);
1855
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00001856unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1857 unsigned int reg, const void *data, size_t len)
1858{
1859 return codec->bulk_write_raw(codec, reg, data, len);
1860}
1861EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1862
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001863/**
1864 * snd_soc_update_bits - update codec register bits
1865 * @codec: audio codec
1866 * @reg: codec register
1867 * @mask: register mask
1868 * @value: new value
1869 *
1870 * Writes new register value.
1871 *
Timur Tabi180c3292011-01-10 15:58:13 -06001872 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001873 */
1874int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001875 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001876{
Mark Brown8a713da2011-12-03 12:33:55 +00001877 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001878 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06001879 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001880
Mark Brown8a713da2011-12-03 12:33:55 +00001881 if (codec->using_regmap) {
1882 ret = regmap_update_bits_check(codec->control_data, reg,
1883 mask, value, &change);
1884 } else {
1885 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06001886 if (ret < 0)
1887 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00001888
1889 old = ret;
1890 new = (old & ~mask) | (value & mask);
1891 change = old != new;
1892 if (change)
1893 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06001894 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001895
Mark Brown8a713da2011-12-03 12:33:55 +00001896 if (ret < 0)
1897 return ret;
1898
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001899 return change;
1900}
1901EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1902
1903/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001904 * snd_soc_update_bits_locked - update codec register bits
1905 * @codec: audio codec
1906 * @reg: codec register
1907 * @mask: register mask
1908 * @value: new value
1909 *
1910 * Writes new register value, and takes the codec mutex.
1911 *
1912 * Returns 1 for change else 0.
1913 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001914int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1915 unsigned short reg, unsigned int mask,
1916 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001917{
1918 int change;
1919
1920 mutex_lock(&codec->mutex);
1921 change = snd_soc_update_bits(codec, reg, mask, value);
1922 mutex_unlock(&codec->mutex);
1923
1924 return change;
1925}
Mark Browndd1b3d52009-12-04 14:22:03 +00001926EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001927
1928/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001929 * snd_soc_test_bits - test register for change
1930 * @codec: audio codec
1931 * @reg: codec register
1932 * @mask: register mask
1933 * @value: new value
1934 *
1935 * Tests a register with a new value and checks if the new value is
1936 * different from the old value.
1937 *
1938 * Returns 1 for change else 0.
1939 */
1940int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001941 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001942{
1943 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001944 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001945
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001946 old = snd_soc_read(codec, reg);
1947 new = (old & ~mask) | value;
1948 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001949
1950 return change;
1951}
1952EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1953
1954/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001955 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1956 * @substream: the pcm substream
1957 * @hw: the hardware parameters
1958 *
1959 * Sets the substream runtime hardware parameters.
1960 */
1961int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1962 const struct snd_pcm_hardware *hw)
1963{
1964 struct snd_pcm_runtime *runtime = substream->runtime;
1965 runtime->hw.info = hw->info;
1966 runtime->hw.formats = hw->formats;
1967 runtime->hw.period_bytes_min = hw->period_bytes_min;
1968 runtime->hw.period_bytes_max = hw->period_bytes_max;
1969 runtime->hw.periods_min = hw->periods_min;
1970 runtime->hw.periods_max = hw->periods_max;
1971 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1972 runtime->hw.fifo_size = hw->fifo_size;
1973 return 0;
1974}
1975EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1976
1977/**
1978 * snd_soc_cnew - create new control
1979 * @_template: control template
1980 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001981 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00001982 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001983 *
1984 * Create a new mixer control from a template control.
1985 *
1986 * Returns 0 for success, else error.
1987 */
1988struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brownefb7ac32011-03-08 17:23:24 +00001989 void *data, char *long_name,
1990 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001991{
1992 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00001993 struct snd_kcontrol *kcontrol;
1994 char *name = NULL;
1995 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001996
1997 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001998 template.index = 0;
1999
Mark Brownefb7ac32011-03-08 17:23:24 +00002000 if (!long_name)
2001 long_name = template.name;
2002
2003 if (prefix) {
2004 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002005 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002006 if (!name)
2007 return NULL;
2008
2009 snprintf(name, name_len, "%s %s", prefix, long_name);
2010
2011 template.name = name;
2012 } else {
2013 template.name = long_name;
2014 }
2015
2016 kcontrol = snd_ctl_new1(&template, data);
2017
2018 kfree(name);
2019
2020 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002021}
2022EXPORT_SYMBOL_GPL(snd_soc_cnew);
2023
Liam Girdwood022658b2012-02-03 17:43:09 +00002024static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2025 const struct snd_kcontrol_new *controls, int num_controls,
2026 const char *prefix, void *data)
2027{
2028 int err, i;
2029
2030 for (i = 0; i < num_controls; i++) {
2031 const struct snd_kcontrol_new *control = &controls[i];
2032 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2033 control->name, prefix));
2034 if (err < 0) {
2035 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2036 return err;
2037 }
2038 }
2039
2040 return 0;
2041}
2042
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002043/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002044 * snd_soc_add_codec_controls - add an array of controls to a codec.
2045 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002046 * duplicating this code.
2047 *
2048 * @codec: codec to add controls to
2049 * @controls: array of controls to add
2050 * @num_controls: number of elements in the array
2051 *
2052 * Return 0 for success, else error.
2053 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002054int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002055 const struct snd_kcontrol_new *controls, int num_controls)
2056{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002057 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002058
Liam Girdwood022658b2012-02-03 17:43:09 +00002059 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2060 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002061}
Liam Girdwood022658b2012-02-03 17:43:09 +00002062EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002063
2064/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002065 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002066 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002067 *
2068 * @platform: platform to add controls to
2069 * @controls: array of controls to add
2070 * @num_controls: number of elements in the array
2071 *
2072 * Return 0 for success, else error.
2073 */
2074int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2075 const struct snd_kcontrol_new *controls, int num_controls)
2076{
2077 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002078
Liam Girdwood022658b2012-02-03 17:43:09 +00002079 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2080 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002081}
2082EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2083
2084/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002085 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2086 * Convenience function to add a list of controls.
2087 *
2088 * @soc_card: SoC card to add controls to
2089 * @controls: array of controls to add
2090 * @num_controls: number of elements in the array
2091 *
2092 * Return 0 for success, else error.
2093 */
2094int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2095 const struct snd_kcontrol_new *controls, int num_controls)
2096{
2097 struct snd_card *card = soc_card->snd_card;
2098
2099 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2100 NULL, soc_card);
2101}
2102EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2103
2104/**
2105 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2106 * Convienience function to add a list of controls.
2107 *
2108 * @dai: DAI to add controls to
2109 * @controls: array of controls to add
2110 * @num_controls: number of elements in the array
2111 *
2112 * Return 0 for success, else error.
2113 */
2114int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2115 const struct snd_kcontrol_new *controls, int num_controls)
2116{
2117 struct snd_card *card = dai->card->snd_card;
2118
2119 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2120 NULL, dai);
2121}
2122EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2123
2124/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002125 * snd_soc_info_enum_double - enumerated double mixer info callback
2126 * @kcontrol: mixer control
2127 * @uinfo: control element information
2128 *
2129 * Callback to provide information about a double enumerated
2130 * mixer control.
2131 *
2132 * Returns 0 for success.
2133 */
2134int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2135 struct snd_ctl_elem_info *uinfo)
2136{
2137 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2138
2139 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2140 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002141 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002142
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002143 if (uinfo->value.enumerated.item > e->max - 1)
2144 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002145 strcpy(uinfo->value.enumerated.name,
2146 e->texts[uinfo->value.enumerated.item]);
2147 return 0;
2148}
2149EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2150
2151/**
2152 * snd_soc_get_enum_double - enumerated double mixer get callback
2153 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002154 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002155 *
2156 * Callback to get the value of a double enumerated mixer.
2157 *
2158 * Returns 0 for success.
2159 */
2160int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2161 struct snd_ctl_elem_value *ucontrol)
2162{
2163 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2164 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002165 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002166
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002167 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002168 ;
2169 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002170 ucontrol->value.enumerated.item[0]
2171 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002172 if (e->shift_l != e->shift_r)
2173 ucontrol->value.enumerated.item[1] =
2174 (val >> e->shift_r) & (bitmask - 1);
2175
2176 return 0;
2177}
2178EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2179
2180/**
2181 * snd_soc_put_enum_double - enumerated double mixer put callback
2182 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002183 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002184 *
2185 * Callback to set the value of a double enumerated mixer.
2186 *
2187 * Returns 0 for success.
2188 */
2189int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2190 struct snd_ctl_elem_value *ucontrol)
2191{
2192 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2193 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002194 unsigned int val;
2195 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002196
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002197 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002198 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002199 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002200 return -EINVAL;
2201 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2202 mask = (bitmask - 1) << e->shift_l;
2203 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002204 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002205 return -EINVAL;
2206 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2207 mask |= (bitmask - 1) << e->shift_r;
2208 }
2209
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002210 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002211}
2212EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2213
2214/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002215 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2216 * @kcontrol: mixer control
2217 * @ucontrol: control element information
2218 *
2219 * Callback to get the value of a double semi enumerated mixer.
2220 *
2221 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2222 * used for handling bitfield coded enumeration for example.
2223 *
2224 * Returns 0 for success.
2225 */
2226int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2227 struct snd_ctl_elem_value *ucontrol)
2228{
2229 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002230 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002231 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002232
2233 reg_val = snd_soc_read(codec, e->reg);
2234 val = (reg_val >> e->shift_l) & e->mask;
2235 for (mux = 0; mux < e->max; mux++) {
2236 if (val == e->values[mux])
2237 break;
2238 }
2239 ucontrol->value.enumerated.item[0] = mux;
2240 if (e->shift_l != e->shift_r) {
2241 val = (reg_val >> e->shift_r) & e->mask;
2242 for (mux = 0; mux < e->max; mux++) {
2243 if (val == e->values[mux])
2244 break;
2245 }
2246 ucontrol->value.enumerated.item[1] = mux;
2247 }
2248
2249 return 0;
2250}
2251EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2252
2253/**
2254 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2255 * @kcontrol: mixer control
2256 * @ucontrol: control element information
2257 *
2258 * Callback to set the value of a double semi enumerated mixer.
2259 *
2260 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2261 * used for handling bitfield coded enumeration for example.
2262 *
2263 * Returns 0 for success.
2264 */
2265int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2266 struct snd_ctl_elem_value *ucontrol)
2267{
2268 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002269 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002270 unsigned int val;
2271 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002272
2273 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2274 return -EINVAL;
2275 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2276 mask = e->mask << e->shift_l;
2277 if (e->shift_l != e->shift_r) {
2278 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2279 return -EINVAL;
2280 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2281 mask |= e->mask << e->shift_r;
2282 }
2283
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002284 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002285}
2286EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2287
2288/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002289 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2290 * @kcontrol: mixer control
2291 * @uinfo: control element information
2292 *
2293 * Callback to provide information about an external enumerated
2294 * single mixer.
2295 *
2296 * Returns 0 for success.
2297 */
2298int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2299 struct snd_ctl_elem_info *uinfo)
2300{
2301 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2302
2303 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2304 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002305 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002306
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002307 if (uinfo->value.enumerated.item > e->max - 1)
2308 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002309 strcpy(uinfo->value.enumerated.name,
2310 e->texts[uinfo->value.enumerated.item]);
2311 return 0;
2312}
2313EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2314
2315/**
2316 * snd_soc_info_volsw_ext - external single mixer info callback
2317 * @kcontrol: mixer control
2318 * @uinfo: control element information
2319 *
2320 * Callback to provide information about a single external mixer control.
2321 *
2322 * Returns 0 for success.
2323 */
2324int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2325 struct snd_ctl_elem_info *uinfo)
2326{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002327 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002328
Mark Brownfd5dfad2009-04-15 21:37:46 +01002329 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002330 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2331 else
2332 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2333
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002334 uinfo->count = 1;
2335 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002336 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002337 return 0;
2338}
2339EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2340
2341/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002342 * snd_soc_info_volsw - single mixer info callback
2343 * @kcontrol: mixer control
2344 * @uinfo: control element information
2345 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002346 * Callback to provide information about a single mixer control, or a double
2347 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002348 *
2349 * Returns 0 for success.
2350 */
2351int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2352 struct snd_ctl_elem_info *uinfo)
2353{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002354 struct soc_mixer_control *mc =
2355 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002356 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002357
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002358 if (!mc->platform_max)
2359 mc->platform_max = mc->max;
2360 platform_max = mc->platform_max;
2361
2362 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002363 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2364 else
2365 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2366
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002367 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002368 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002369 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002370 return 0;
2371}
2372EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2373
2374/**
2375 * snd_soc_get_volsw - single mixer get callback
2376 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002377 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002378 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002379 * Callback to get the value of a single mixer control, or a double mixer
2380 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002381 *
2382 * Returns 0 for success.
2383 */
2384int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2385 struct snd_ctl_elem_value *ucontrol)
2386{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002387 struct soc_mixer_control *mc =
2388 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002389 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002390 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002391 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf82008-07-29 10:22:24 -04002392 unsigned int shift = mc->shift;
2393 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002394 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002395 unsigned int mask = (1 << fls(max)) - 1;
2396 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002397
2398 ucontrol->value.integer.value[0] =
2399 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002400 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002401 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002402 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002403
2404 if (snd_soc_volsw_is_stereo(mc)) {
2405 if (reg == reg2)
2406 ucontrol->value.integer.value[1] =
2407 (snd_soc_read(codec, reg) >> rshift) & mask;
2408 else
2409 ucontrol->value.integer.value[1] =
2410 (snd_soc_read(codec, reg2) >> shift) & mask;
2411 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002412 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002413 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002414 }
2415
2416 return 0;
2417}
2418EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2419
2420/**
2421 * snd_soc_put_volsw - single mixer put callback
2422 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002423 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002424 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002425 * Callback to set the value of a single mixer control, or a double mixer
2426 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002427 *
2428 * Returns 0 for success.
2429 */
2430int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2431 struct snd_ctl_elem_value *ucontrol)
2432{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002433 struct soc_mixer_control *mc =
2434 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002435 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002436 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002437 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf82008-07-29 10:22:24 -04002438 unsigned int shift = mc->shift;
2439 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002440 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002441 unsigned int mask = (1 << fls(max)) - 1;
2442 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002443 int err;
2444 bool type_2r = 0;
2445 unsigned int val2 = 0;
2446 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002447
2448 val = (ucontrol->value.integer.value[0] & mask);
2449 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002450 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002451 val_mask = mask << shift;
2452 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002453 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002454 val2 = (ucontrol->value.integer.value[1] & mask);
2455 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002456 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002457 if (reg == reg2) {
2458 val_mask |= mask << rshift;
2459 val |= val2 << rshift;
2460 } else {
2461 val2 = val2 << shift;
2462 type_2r = 1;
2463 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002464 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002465 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002466 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002467 return err;
2468
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002469 if (type_2r)
2470 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2471
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002472 return err;
2473}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002474EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002475
Mark Browne13ac2e2008-05-28 17:58:05 +01002476/**
2477 * snd_soc_info_volsw_s8 - signed mixer info callback
2478 * @kcontrol: mixer control
2479 * @uinfo: control element information
2480 *
2481 * Callback to provide information about a signed mixer control.
2482 *
2483 * Returns 0 for success.
2484 */
2485int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2486 struct snd_ctl_elem_info *uinfo)
2487{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002488 struct soc_mixer_control *mc =
2489 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002490 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002491 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002492
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002493 if (!mc->platform_max)
2494 mc->platform_max = mc->max;
2495 platform_max = mc->platform_max;
2496
Mark Browne13ac2e2008-05-28 17:58:05 +01002497 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2498 uinfo->count = 2;
2499 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002500 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002501 return 0;
2502}
2503EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2504
2505/**
2506 * snd_soc_get_volsw_s8 - signed mixer get callback
2507 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002508 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002509 *
2510 * Callback to get the value of a signed mixer control.
2511 *
2512 * Returns 0 for success.
2513 */
2514int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2515 struct snd_ctl_elem_value *ucontrol)
2516{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002517 struct soc_mixer_control *mc =
2518 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002519 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002520 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002521 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002522 int val = snd_soc_read(codec, reg);
2523
2524 ucontrol->value.integer.value[0] =
2525 ((signed char)(val & 0xff))-min;
2526 ucontrol->value.integer.value[1] =
2527 ((signed char)((val >> 8) & 0xff))-min;
2528 return 0;
2529}
2530EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2531
2532/**
2533 * snd_soc_put_volsw_sgn - signed mixer put callback
2534 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002535 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002536 *
2537 * Callback to set the value of a signed mixer control.
2538 *
2539 * Returns 0 for success.
2540 */
2541int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2542 struct snd_ctl_elem_value *ucontrol)
2543{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002544 struct soc_mixer_control *mc =
2545 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002546 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002547 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002548 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002549 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002550
2551 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2552 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2553
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002554 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002555}
2556EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2557
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002558/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002559 * snd_soc_limit_volume - Set new limit to an existing volume control.
2560 *
2561 * @codec: where to look for the control
2562 * @name: Name of the control
2563 * @max: new maximum limit
2564 *
2565 * Return 0 for success, else error.
2566 */
2567int snd_soc_limit_volume(struct snd_soc_codec *codec,
2568 const char *name, int max)
2569{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002570 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002571 struct snd_kcontrol *kctl;
2572 struct soc_mixer_control *mc;
2573 int found = 0;
2574 int ret = -EINVAL;
2575
2576 /* Sanity check for name and max */
2577 if (unlikely(!name || max <= 0))
2578 return -EINVAL;
2579
2580 list_for_each_entry(kctl, &card->controls, list) {
2581 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2582 found = 1;
2583 break;
2584 }
2585 }
2586 if (found) {
2587 mc = (struct soc_mixer_control *)kctl->private_value;
2588 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002589 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002590 ret = 0;
2591 }
2592 }
2593 return ret;
2594}
2595EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2596
2597/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002598 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2599 * mixer info callback
2600 * @kcontrol: mixer control
2601 * @uinfo: control element information
2602 *
2603 * Returns 0 for success.
2604 */
2605int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2606 struct snd_ctl_elem_info *uinfo)
2607{
2608 struct soc_mixer_control *mc =
2609 (struct soc_mixer_control *)kcontrol->private_value;
2610 int max = mc->max;
2611 int min = mc->min;
2612
2613 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2614 uinfo->count = 2;
2615 uinfo->value.integer.min = 0;
2616 uinfo->value.integer.max = max-min;
2617
2618 return 0;
2619}
2620EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2621
2622/**
2623 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2624 * mixer get callback
2625 * @kcontrol: mixer control
2626 * @uinfo: control element information
2627 *
2628 * Returns 0 for success.
2629 */
2630int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2631 struct snd_ctl_elem_value *ucontrol)
2632{
2633 struct soc_mixer_control *mc =
2634 (struct soc_mixer_control *)kcontrol->private_value;
2635 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2636 unsigned int mask = (1<<mc->shift)-1;
2637 int min = mc->min;
2638 int val = snd_soc_read(codec, mc->reg) & mask;
2639 int valr = snd_soc_read(codec, mc->rreg) & mask;
2640
Stuart Longland20630c72010-06-18 12:56:10 +10002641 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2642 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002643 return 0;
2644}
2645EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2646
2647/**
2648 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2649 * mixer put callback
2650 * @kcontrol: mixer control
2651 * @uinfo: control element information
2652 *
2653 * Returns 0 for success.
2654 */
2655int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2656 struct snd_ctl_elem_value *ucontrol)
2657{
2658 struct soc_mixer_control *mc =
2659 (struct soc_mixer_control *)kcontrol->private_value;
2660 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2661 unsigned int mask = (1<<mc->shift)-1;
2662 int min = mc->min;
2663 int ret;
2664 unsigned int val, valr, oval, ovalr;
2665
2666 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2667 val &= mask;
2668 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2669 valr &= mask;
2670
2671 oval = snd_soc_read(codec, mc->reg) & mask;
2672 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2673
2674 ret = 0;
2675 if (oval != val) {
2676 ret = snd_soc_write(codec, mc->reg, val);
2677 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002678 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002679 }
2680 if (ovalr != valr) {
2681 ret = snd_soc_write(codec, mc->rreg, valr);
2682 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002683 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002684 }
2685
2686 return 0;
2687}
2688EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2689
2690/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002691 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2692 * @dai: DAI
2693 * @clk_id: DAI specific clock ID
2694 * @freq: new clock frequency in Hz
2695 * @dir: new clock direction - input/output.
2696 *
2697 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2698 */
2699int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2700 unsigned int freq, int dir)
2701{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002702 if (dai->driver && dai->driver->ops->set_sysclk)
2703 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002704 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002705 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002706 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002707 else
2708 return -EINVAL;
2709}
2710EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2711
2712/**
Mark Brownec4ee522011-03-07 20:58:11 +00002713 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2714 * @codec: CODEC
2715 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002716 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002717 * @freq: new clock frequency in Hz
2718 * @dir: new clock direction - input/output.
2719 *
2720 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2721 */
2722int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002723 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002724{
2725 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002726 return codec->driver->set_sysclk(codec, clk_id, source,
2727 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002728 else
2729 return -EINVAL;
2730}
2731EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2732
2733/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002734 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2735 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002736 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002737 * @div: new clock divisor.
2738 *
2739 * Configures the clock dividers. This is used to derive the best DAI bit and
2740 * frame clocks from the system or master clock. It's best to set the DAI bit
2741 * and frame clocks as low as possible to save system power.
2742 */
2743int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2744 int div_id, int div)
2745{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002746 if (dai->driver && dai->driver->ops->set_clkdiv)
2747 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002748 else
2749 return -EINVAL;
2750}
2751EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2752
2753/**
2754 * snd_soc_dai_set_pll - configure DAI PLL.
2755 * @dai: DAI
2756 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002757 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002758 * @freq_in: PLL input clock frequency in Hz
2759 * @freq_out: requested PLL output clock frequency in Hz
2760 *
2761 * Configures and enables PLL to generate output clock based on input clock.
2762 */
Mark Brown85488032009-09-05 18:52:16 +01002763int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2764 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002765{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002766 if (dai->driver && dai->driver->ops->set_pll)
2767 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002768 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002769 else if (dai->codec && dai->codec->driver->set_pll)
2770 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2771 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002772 else
2773 return -EINVAL;
2774}
2775EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2776
Mark Brownec4ee522011-03-07 20:58:11 +00002777/*
2778 * snd_soc_codec_set_pll - configure codec PLL.
2779 * @codec: CODEC
2780 * @pll_id: DAI specific PLL ID
2781 * @source: DAI specific source for the PLL
2782 * @freq_in: PLL input clock frequency in Hz
2783 * @freq_out: requested PLL output clock frequency in Hz
2784 *
2785 * Configures and enables PLL to generate output clock based on input clock.
2786 */
2787int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2788 unsigned int freq_in, unsigned int freq_out)
2789{
2790 if (codec->driver->set_pll)
2791 return codec->driver->set_pll(codec, pll_id, source,
2792 freq_in, freq_out);
2793 else
2794 return -EINVAL;
2795}
2796EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2797
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002798/**
2799 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2800 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002801 * @fmt: SND_SOC_DAIFMT_ format value.
2802 *
2803 * Configures the DAI hardware format and clocking.
2804 */
2805int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2806{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002807 if (dai->driver && dai->driver->ops->set_fmt)
2808 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002809 else
2810 return -EINVAL;
2811}
2812EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2813
2814/**
2815 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2816 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002817 * @tx_mask: bitmask representing active TX slots.
2818 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002819 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002820 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002821 *
2822 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2823 * specific.
2824 */
2825int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002826 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002827{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002828 if (dai->driver && dai->driver->ops->set_tdm_slot)
2829 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002830 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002831 else
2832 return -EINVAL;
2833}
2834EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2835
2836/**
Barry Song472df3c2009-09-12 01:16:29 +08002837 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2838 * @dai: DAI
2839 * @tx_num: how many TX channels
2840 * @tx_slot: pointer to an array which imply the TX slot number channel
2841 * 0~num-1 uses
2842 * @rx_num: how many RX channels
2843 * @rx_slot: pointer to an array which imply the RX slot number channel
2844 * 0~num-1 uses
2845 *
2846 * configure the relationship between channel number and TDM slot number.
2847 */
2848int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2849 unsigned int tx_num, unsigned int *tx_slot,
2850 unsigned int rx_num, unsigned int *rx_slot)
2851{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002852 if (dai->driver && dai->driver->ops->set_channel_map)
2853 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002854 rx_num, rx_slot);
2855 else
2856 return -EINVAL;
2857}
2858EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2859
2860/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002861 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2862 * @dai: DAI
2863 * @tristate: tristate enable
2864 *
2865 * Tristates the DAI so that others can use it.
2866 */
2867int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2868{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002869 if (dai->driver && dai->driver->ops->set_tristate)
2870 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002871 else
2872 return -EINVAL;
2873}
2874EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2875
2876/**
2877 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2878 * @dai: DAI
2879 * @mute: mute enable
2880 *
2881 * Mutes the DAI DAC.
2882 */
2883int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2884{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002885 if (dai->driver && dai->driver->ops->digital_mute)
2886 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002887 else
2888 return -EINVAL;
2889}
2890EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2891
Mark Brownc5af3a22008-11-28 13:29:45 +00002892/**
2893 * snd_soc_register_card - Register a card with the ASoC core
2894 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002895 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002896 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002897 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302898int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002899{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002900 int i;
2901
Mark Brownc5af3a22008-11-28 13:29:45 +00002902 if (!card->name || !card->dev)
2903 return -EINVAL;
2904
Stephen Warren5a504962011-12-21 10:40:59 -07002905 for (i = 0; i < card->num_links; i++) {
2906 struct snd_soc_dai_link *link = &card->dai_link[i];
2907
2908 /*
2909 * Codec must be specified by 1 of name or OF node,
2910 * not both or neither.
2911 */
2912 if (!!link->codec_name == !!link->codec_of_node) {
2913 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00002914 "Neither/both codec name/of_node are set for %s\n",
2915 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07002916 return -EINVAL;
2917 }
2918
2919 /*
2920 * Platform may be specified by either name or OF node, but
2921 * can be left unspecified, and a dummy platform will be used.
2922 */
2923 if (link->platform_name && link->platform_of_node) {
2924 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00002925 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07002926 return -EINVAL;
2927 }
2928
2929 /*
2930 * CPU DAI must be specified by 1 of name or OF node,
2931 * not both or neither.
2932 */
2933 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
2934 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00002935 "Neither/both cpu_dai name/of_node are set for %s\n",
2936 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07002937 return -EINVAL;
2938 }
2939 }
2940
Mark Browned77cc12011-05-03 18:25:34 +01002941 dev_set_drvdata(card->dev, card);
2942
Stephen Warren111c6412011-01-28 14:26:35 -07002943 snd_soc_initialize_card_lists(card);
2944
Vinod Koul150dd2f2011-01-13 22:48:32 +05302945 soc_init_card_debugfs(card);
2946
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002947 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
2948 (card->num_links + card->num_aux_devs),
2949 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002950 if (card->rtd == NULL)
2951 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002952 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002953
2954 for (i = 0; i < card->num_links; i++)
2955 card->rtd[i].dai_link = &card->dai_link[i];
2956
Mark Brownc5af3a22008-11-28 13:29:45 +00002957 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01002958 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00002959 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002960 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002961
2962 mutex_lock(&client_mutex);
2963 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002964 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002965 mutex_unlock(&client_mutex);
2966
2967 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2968
2969 return 0;
2970}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302971EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002972
2973/**
2974 * snd_soc_unregister_card - Unregister a card with the ASoC core
2975 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002976 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002977 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002978 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302979int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002980{
Vinod Koulb0e26482011-01-13 22:48:02 +05302981 if (card->instantiated)
2982 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002983 mutex_lock(&client_mutex);
2984 list_del(&card->list);
2985 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002986 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2987
2988 return 0;
2989}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302990EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002991
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002992/*
2993 * Simplify DAI link configuration by removing ".-1" from device names
2994 * and sanitizing names.
2995 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002996static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002997{
2998 char *found, name[NAME_SIZE];
2999 int id1, id2;
3000
3001 if (dev_name(dev) == NULL)
3002 return NULL;
3003
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003004 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003005
3006 /* are we a "%s.%d" name (platform and SPI components) */
3007 found = strstr(name, dev->driver->name);
3008 if (found) {
3009 /* get ID */
3010 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3011
3012 /* discard ID from name if ID == -1 */
3013 if (*id == -1)
3014 found[strlen(dev->driver->name)] = '\0';
3015 }
3016
3017 } else {
3018 /* I2C component devices are named "bus-addr" */
3019 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3020 char tmp[NAME_SIZE];
3021
3022 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003023 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003024
3025 /* sanitize component name for DAI link creation */
3026 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003027 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003028 } else
3029 *id = 0;
3030 }
3031
3032 return kstrdup(name, GFP_KERNEL);
3033}
3034
3035/*
3036 * Simplify DAI link naming for single devices with multiple DAIs by removing
3037 * any ".-1" and using the DAI name (instead of device name).
3038 */
3039static inline char *fmt_multiple_name(struct device *dev,
3040 struct snd_soc_dai_driver *dai_drv)
3041{
3042 if (dai_drv->name == NULL) {
3043 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3044 dev_name(dev));
3045 return NULL;
3046 }
3047
3048 return kstrdup(dai_drv->name, GFP_KERNEL);
3049}
3050
Mark Brown91151712008-11-30 23:31:24 +00003051/**
3052 * snd_soc_register_dai - Register a DAI with the ASoC core
3053 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003054 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003055 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003056int snd_soc_register_dai(struct device *dev,
3057 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003058{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003059 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003060
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003061 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003062
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003063 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3064 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003065 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003066
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003067 /* create DAI component name */
3068 dai->name = fmt_single_name(dev, &dai->id);
3069 if (dai->name == NULL) {
3070 kfree(dai);
3071 return -ENOMEM;
3072 }
3073
3074 dai->dev = dev;
3075 dai->driver = dai_drv;
3076 if (!dai->driver->ops)
3077 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003078
3079 mutex_lock(&client_mutex);
3080 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003081 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003082 mutex_unlock(&client_mutex);
3083
3084 pr_debug("Registered DAI '%s'\n", dai->name);
3085
3086 return 0;
3087}
3088EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3089
3090/**
3091 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3092 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003093 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003094 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003095void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003096{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003097 struct snd_soc_dai *dai;
3098
3099 list_for_each_entry(dai, &dai_list, list) {
3100 if (dev == dai->dev)
3101 goto found;
3102 }
3103 return;
3104
3105found:
Mark Brown91151712008-11-30 23:31:24 +00003106 mutex_lock(&client_mutex);
3107 list_del(&dai->list);
3108 mutex_unlock(&client_mutex);
3109
3110 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003111 kfree(dai->name);
3112 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003113}
3114EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3115
3116/**
3117 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3118 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003119 * @dai: Array of DAIs to register
3120 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003121 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003122int snd_soc_register_dais(struct device *dev,
3123 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003124{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003125 struct snd_soc_dai *dai;
3126 int i, ret = 0;
3127
Liam Girdwood720ffa42010-08-18 00:30:30 +01003128 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003129
3130 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003131
3132 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003133 if (dai == NULL) {
3134 ret = -ENOMEM;
3135 goto err;
3136 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003137
3138 /* create DAI component name */
3139 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3140 if (dai->name == NULL) {
3141 kfree(dai);
3142 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003143 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003144 }
3145
3146 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003147 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003148 if (dai->driver->id)
3149 dai->id = dai->driver->id;
3150 else
3151 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003152 if (!dai->driver->ops)
3153 dai->driver->ops = &null_dai_ops;
3154
3155 mutex_lock(&client_mutex);
3156 list_add(&dai->list, &dai_list);
3157 mutex_unlock(&client_mutex);
3158
3159 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003160 }
3161
Axel Lin1dcb4f32010-12-06 16:48:03 +08003162 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003163 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08003164 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00003165 return 0;
3166
3167err:
3168 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003169 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003170
3171 return ret;
3172}
3173EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3174
3175/**
3176 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3177 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003178 * @dai: Array of DAIs to unregister
3179 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003180 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003181void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003182{
3183 int i;
3184
3185 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003186 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003187}
3188EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3189
Mark Brown12a48a8c2008-12-03 19:40:30 +00003190/**
3191 * snd_soc_register_platform - Register a platform with the ASoC core
3192 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003193 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003194 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003195int snd_soc_register_platform(struct device *dev,
3196 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003197{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003198 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003199
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003200 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3201
3202 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3203 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003204 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003205
3206 /* create platform component name */
3207 platform->name = fmt_single_name(dev, &platform->id);
3208 if (platform->name == NULL) {
3209 kfree(platform);
3210 return -ENOMEM;
3211 }
3212
3213 platform->dev = dev;
3214 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003215 platform->dapm.dev = dev;
3216 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003217 platform->dapm.stream_event = platform_drv->stream_event;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003218
3219 mutex_lock(&client_mutex);
3220 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003221 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003222 mutex_unlock(&client_mutex);
3223
3224 pr_debug("Registered platform '%s'\n", platform->name);
3225
3226 return 0;
3227}
3228EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3229
3230/**
3231 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3232 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003233 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003234 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003235void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003236{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003237 struct snd_soc_platform *platform;
3238
3239 list_for_each_entry(platform, &platform_list, list) {
3240 if (dev == platform->dev)
3241 goto found;
3242 }
3243 return;
3244
3245found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003246 mutex_lock(&client_mutex);
3247 list_del(&platform->list);
3248 mutex_unlock(&client_mutex);
3249
3250 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003251 kfree(platform->name);
3252 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003253}
3254EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3255
Mark Brown151ab222009-05-09 16:22:58 +01003256static u64 codec_format_map[] = {
3257 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3258 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3259 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3260 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3261 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3262 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3263 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3264 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3265 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3266 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3267 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3268 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3269 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3270 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3271 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3272 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3273};
3274
3275/* Fix up the DAI formats for endianness: codecs don't actually see
3276 * the endianness of the data but we're using the CPU format
3277 * definitions which do need to include endianness so we ensure that
3278 * codec DAIs always have both big and little endian variants set.
3279 */
3280static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3281{
3282 int i;
3283
3284 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3285 if (stream->formats & codec_format_map[i])
3286 stream->formats |= codec_format_map[i];
3287}
3288
Mark Brown0d0cf002008-12-10 14:32:45 +00003289/**
3290 * snd_soc_register_codec - Register a codec with the ASoC core
3291 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003292 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003293 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003294int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003295 const struct snd_soc_codec_driver *codec_drv,
3296 struct snd_soc_dai_driver *dai_drv,
3297 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003298{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003299 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003300 struct snd_soc_codec *codec;
3301 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003302
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003303 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003304
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003305 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3306 if (codec == NULL)
3307 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003308
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003309 /* create CODEC component name */
3310 codec->name = fmt_single_name(dev, &codec->id);
3311 if (codec->name == NULL) {
3312 kfree(codec);
3313 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003314 }
3315
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003316 if (codec_drv->compress_type)
3317 codec->compress_type = codec_drv->compress_type;
3318 else
3319 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3320
Mark Brownc3acec22010-12-02 16:15:29 +00003321 codec->write = codec_drv->write;
3322 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003323 codec->volatile_register = codec_drv->volatile_register;
3324 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003325 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00003326 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003327 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3328 codec->dapm.dev = dev;
3329 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003330 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003331 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003332 codec->dev = dev;
3333 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003334 codec->num_dai = num_dai;
3335 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003336
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003337 /* allocate CODEC register cache */
3338 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003339 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003340 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003341 /* it is necessary to make a copy of the default register cache
3342 * because in the case of using a compression type that requires
3343 * the default register cache to be marked as __devinitconst the
3344 * kernel might have freed the array by the time we initialize
3345 * the cache.
3346 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003347 if (codec_drv->reg_cache_default) {
3348 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3349 reg_size, GFP_KERNEL);
3350 if (!codec->reg_def_copy) {
3351 ret = -ENOMEM;
3352 goto fail;
3353 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003354 }
3355 }
3356
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003357 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3358 if (!codec->volatile_register)
3359 codec->volatile_register = snd_soc_default_volatile_register;
3360 if (!codec->readable_register)
3361 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003362 if (!codec->writable_register)
3363 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003364 }
3365
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003366 for (i = 0; i < num_dai; i++) {
3367 fixup_codec_formats(&dai_drv[i].playback);
3368 fixup_codec_formats(&dai_drv[i].capture);
3369 }
3370
Mark Brown26b01cc2010-08-18 20:20:55 +01003371 /* register any DAIs */
3372 if (num_dai) {
3373 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3374 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003375 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003376 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003377
Mark Brown0d0cf002008-12-10 14:32:45 +00003378 mutex_lock(&client_mutex);
3379 list_add(&codec->list, &codec_list);
3380 snd_soc_instantiate_cards();
3381 mutex_unlock(&client_mutex);
3382
3383 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003384 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003385
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003386fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003387 kfree(codec->reg_def_copy);
3388 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003389 kfree(codec->name);
3390 kfree(codec);
3391 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003392}
3393EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3394
3395/**
3396 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3397 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003398 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003399 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003400void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003401{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003402 struct snd_soc_codec *codec;
3403 int i;
3404
3405 list_for_each_entry(codec, &codec_list, list) {
3406 if (dev == codec->dev)
3407 goto found;
3408 }
3409 return;
3410
3411found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003412 if (codec->num_dai)
3413 for (i = 0; i < codec->num_dai; i++)
3414 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003415
Mark Brown0d0cf002008-12-10 14:32:45 +00003416 mutex_lock(&client_mutex);
3417 list_del(&codec->list);
3418 mutex_unlock(&client_mutex);
3419
3420 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003421
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003422 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003423 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003424 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003425 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003426}
3427EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3428
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003429/* Retrieve a card's name from device tree */
3430int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3431 const char *propname)
3432{
3433 struct device_node *np = card->dev->of_node;
3434 int ret;
3435
3436 ret = of_property_read_string_index(np, propname, 0, &card->name);
3437 /*
3438 * EINVAL means the property does not exist. This is fine providing
3439 * card->name was previously set, which is checked later in
3440 * snd_soc_register_card.
3441 */
3442 if (ret < 0 && ret != -EINVAL) {
3443 dev_err(card->dev,
3444 "Property '%s' could not be read: %d\n",
3445 propname, ret);
3446 return ret;
3447 }
3448
3449 return 0;
3450}
3451EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3452
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003453int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3454 const char *propname)
3455{
3456 struct device_node *np = card->dev->of_node;
3457 int num_routes;
3458 struct snd_soc_dapm_route *routes;
3459 int i, ret;
3460
3461 num_routes = of_property_count_strings(np, propname);
3462 if (num_routes & 1) {
3463 dev_err(card->dev,
3464 "Property '%s's length is not even\n",
3465 propname);
3466 return -EINVAL;
3467 }
3468 num_routes /= 2;
3469 if (!num_routes) {
3470 dev_err(card->dev,
3471 "Property '%s's length is zero\n",
3472 propname);
3473 return -EINVAL;
3474 }
3475
3476 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3477 GFP_KERNEL);
3478 if (!routes) {
3479 dev_err(card->dev,
3480 "Could not allocate DAPM route table\n");
3481 return -EINVAL;
3482 }
3483
3484 for (i = 0; i < num_routes; i++) {
3485 ret = of_property_read_string_index(np, propname,
3486 2 * i, &routes[i].sink);
3487 if (ret) {
3488 dev_err(card->dev,
3489 "Property '%s' index %d could not be read: %d\n",
3490 propname, 2 * i, ret);
3491 return -EINVAL;
3492 }
3493 ret = of_property_read_string_index(np, propname,
3494 (2 * i) + 1, &routes[i].source);
3495 if (ret) {
3496 dev_err(card->dev,
3497 "Property '%s' index %d could not be read: %d\n",
3498 propname, (2 * i) + 1, ret);
3499 return -EINVAL;
3500 }
3501 }
3502
3503 card->num_dapm_routes = num_routes;
3504 card->dapm_routes = routes;
3505
3506 return 0;
3507}
3508EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3509
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003510static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003511{
Mark Brown384c89e2008-12-03 17:34:03 +00003512#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003513 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3514 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Mark Brown384c89e2008-12-03 17:34:03 +00003515 printk(KERN_WARNING
3516 "ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003517 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003518 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003519
Mark Brown8a9dab12011-01-10 22:25:21 +00003520 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003521 &codec_list_fops))
3522 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3523
Mark Brown8a9dab12011-01-10 22:25:21 +00003524 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003525 &dai_list_fops))
3526 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003527
Mark Brown8a9dab12011-01-10 22:25:21 +00003528 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003529 &platform_list_fops))
3530 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003531#endif
3532
Mark Brownfb257892011-04-28 10:57:54 +01003533 snd_soc_util_init();
3534
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003535 return platform_driver_register(&soc_driver);
3536}
Mark Brown4abe8e12010-10-12 17:41:03 +01003537module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003538
Mark Brown7d8c16a2008-11-30 22:11:24 +00003539static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003540{
Mark Brownfb257892011-04-28 10:57:54 +01003541 snd_soc_util_exit();
3542
Mark Brown384c89e2008-12-03 17:34:03 +00003543#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003544 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003545#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003546 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003547}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003548module_exit(snd_soc_exit);
3549
3550/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003551MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003552MODULE_DESCRIPTION("ALSA SoC Core");
3553MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003554MODULE_ALIAS("platform:soc-audio");