blob: ed3c253066b1b6e6bedcb07310f379234754df2e [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>
Liam Girdwood01d75842012-04-25 12:12:49 +010042#include <sound/soc-dpcm.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020043#include <sound/initval.h>
44
Mark Browna8b1d342010-11-03 18:05:58 -040045#define CREATE_TRACE_POINTS
46#include <trace/events/asoc.h>
47
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000048#define NAME_SIZE 32
49
Mark Brown384c89e2008-12-03 17:34:03 +000050#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000051struct dentry *snd_soc_debugfs_root;
52EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000053#endif
54
Mark Brownc5af3a22008-11-28 13:29:45 +000055static DEFINE_MUTEX(client_mutex);
Mark Brown91151712008-11-30 23:31:24 +000056static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000057static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000058static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070059static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000060
Frank Mandarinodb2a4162006-10-06 18:31:09 +020061/*
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
65 */
66static int pmdown_time = 5000;
67module_param(pmdown_time, int, 0);
68MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000070/* returns the minimum number of bytes needed to represent
71 * a particular given value */
72static int min_bytes_needed(unsigned long val)
73{
74 int c = 0;
75 int i;
76
77 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
78 if (val & (1UL << i))
79 break;
80 c = (sizeof val * 8) - c;
81 if (!c || (c % 8))
82 c = (c + 8) / 8;
83 else
84 c /= 8;
85 return c;
86}
87
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000088/* fill buf which is 'len' bytes with a formatted
89 * string of the form 'reg: value\n' */
90static int format_register_str(struct snd_soc_codec *codec,
91 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000092{
Stephen Warren00b317a2011-04-01 14:50:44 -060093 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
94 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000095 int ret;
96 char tmpbuf[len + 1];
97 char regbuf[regsize + 1];
98
99 /* since tmpbuf is allocated on the stack, warn the callers if they
100 * try to abuse this function */
101 WARN_ON(len > 63);
102
103 /* +2 for ': ' and + 1 for '\n' */
104 if (wordsize + regsize + 2 + 1 != len)
105 return -EINVAL;
106
Mark Brown25032c12011-08-01 13:52:48 +0900107 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000108 if (ret < 0) {
109 memset(regbuf, 'X', regsize);
110 regbuf[regsize] = '\0';
111 } else {
112 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
113 }
114
115 /* prepare the buffer */
116 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
117 /* copy it back to the caller without the '\0' */
118 memcpy(buf, tmpbuf, len);
119
120 return 0;
121}
122
123/* codec register dump */
124static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
125 size_t count, loff_t pos)
126{
127 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000128 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000129 int len;
130 size_t total = 0;
131 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000132
Stephen Warren00b317a2011-04-01 14:50:44 -0600133 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
134 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000135
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000136 len = wordsize + regsize + 2 + 1;
137
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000138 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000139 return 0;
140
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000141 if (codec->driver->reg_cache_step)
142 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000143
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000144 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200145 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000146 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000147 if (codec->driver->display_register) {
148 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000149 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100150 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000151 /* only support larger than PAGE_SIZE bytes debugfs
152 * entries for the default case */
153 if (p >= pos) {
154 if (total + len >= count - 1)
155 break;
156 format_register_str(codec, i, buf + total, len);
157 total += len;
158 }
159 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100160 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000161 }
162
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000163 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000164
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000165 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000166}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000167
Mark Brown2624d5f2009-11-03 21:56:13 +0000168static ssize_t codec_reg_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
170{
Mark Brown36ae1a92012-01-06 17:12:45 -0800171 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000172
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000173 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000174}
175
176static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
177
Mark Browndbe21402010-02-12 11:37:24 +0000178static ssize_t pmdown_time_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
180{
Mark Brown36ae1a92012-01-06 17:12:45 -0800181 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000182
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000183 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000184}
185
186static ssize_t pmdown_time_set(struct device *dev,
187 struct device_attribute *attr,
188 const char *buf, size_t count)
189{
Mark Brown36ae1a92012-01-06 17:12:45 -0800190 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700191 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000192
Mark Brownc593b522010-10-27 20:11:17 -0700193 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
194 if (ret)
195 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000196
197 return count;
198}
199
200static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
201
Mark Brown2624d5f2009-11-03 21:56:13 +0000202#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000203static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000204 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000205{
206 ssize_t ret;
207 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000208 char *buf;
209
210 if (*ppos < 0 || !count)
211 return -EINVAL;
212
213 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000214 if (!buf)
215 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000216
217 ret = soc_codec_reg_show(codec, buf, count, *ppos);
218 if (ret >= 0) {
219 if (copy_to_user(user_buf, buf, ret)) {
220 kfree(buf);
221 return -EFAULT;
222 }
223 *ppos += ret;
224 }
225
Mark Brown2624d5f2009-11-03 21:56:13 +0000226 kfree(buf);
227 return ret;
228}
229
230static ssize_t codec_reg_write_file(struct file *file,
231 const char __user *user_buf, size_t count, loff_t *ppos)
232{
233 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700234 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000235 char *start = buf;
236 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000237 struct snd_soc_codec *codec = file->private_data;
238
239 buf_size = min(count, (sizeof(buf)-1));
240 if (copy_from_user(buf, user_buf, buf_size))
241 return -EFAULT;
242 buf[buf_size] = 0;
243
Mark Brown2624d5f2009-11-03 21:56:13 +0000244 while (*start == ' ')
245 start++;
246 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000247 while (*start == ' ')
248 start++;
249 if (strict_strtoul(start, 16, &value))
250 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000251
252 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030253 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000254
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000255 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000256 return buf_size;
257}
258
259static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700260 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000261 .read = codec_reg_read_file,
262 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200263 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000264};
265
266static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
267{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200268 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
269
270 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
271 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000272 if (!codec->debugfs_codec_root) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200273 dev_warn(codec->dev,
274 "ASoC: Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000275 return;
276 }
277
Mark Brownaaee8ef2011-01-26 20:53:50 +0000278 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
279 &codec->cache_sync);
280 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
281 &codec->cache_only);
282
Mark Brown2624d5f2009-11-03 21:56:13 +0000283 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
284 codec->debugfs_codec_root,
285 codec, &codec_reg_fops);
286 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200287 dev_warn(codec->dev,
288 "ASoC: Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000289
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200290 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000291}
292
293static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
294{
295 debugfs_remove_recursive(codec->debugfs_codec_root);
296}
297
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000298static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
299{
300 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
301
302 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
303 debugfs_card_root);
304 if (!platform->debugfs_platform_root) {
305 dev_warn(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000306 "ASoC: Failed to create platform debugfs directory\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000307 return;
308 }
309
310 snd_soc_dapm_debugfs_init(&platform->dapm,
311 platform->debugfs_platform_root);
312}
313
314static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
315{
316 debugfs_remove_recursive(platform->debugfs_platform_root);
317}
318
Mark Brownc3c5a192010-09-15 18:15:14 +0100319static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
320 size_t count, loff_t *ppos)
321{
322 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100323 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100324 struct snd_soc_codec *codec;
325
326 if (!buf)
327 return -ENOMEM;
328
Mark Brown2b194f9d2010-10-13 10:52:16 +0100329 list_for_each_entry(codec, &codec_list, list) {
330 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
331 codec->name);
332 if (len >= 0)
333 ret += len;
334 if (ret > PAGE_SIZE) {
335 ret = PAGE_SIZE;
336 break;
337 }
338 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100339
340 if (ret >= 0)
341 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
342
343 kfree(buf);
344
345 return ret;
346}
347
348static const struct file_operations codec_list_fops = {
349 .read = codec_list_read_file,
350 .llseek = default_llseek,/* read accesses f_pos */
351};
352
Mark Brownf3208782010-09-15 18:19:07 +0100353static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
354 size_t count, loff_t *ppos)
355{
356 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100357 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100358 struct snd_soc_dai *dai;
359
360 if (!buf)
361 return -ENOMEM;
362
Mark Brown2b194f9d2010-10-13 10:52:16 +0100363 list_for_each_entry(dai, &dai_list, list) {
364 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
365 if (len >= 0)
366 ret += len;
367 if (ret > PAGE_SIZE) {
368 ret = PAGE_SIZE;
369 break;
370 }
371 }
Mark Brownf3208782010-09-15 18:19:07 +0100372
Mark Brown2b194f9d2010-10-13 10:52:16 +0100373 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100374
375 kfree(buf);
376
377 return ret;
378}
379
380static const struct file_operations dai_list_fops = {
381 .read = dai_list_read_file,
382 .llseek = default_llseek,/* read accesses f_pos */
383};
384
Mark Brown19c7ac22010-09-15 18:22:40 +0100385static ssize_t platform_list_read_file(struct file *file,
386 char __user *user_buf,
387 size_t count, loff_t *ppos)
388{
389 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100390 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100391 struct snd_soc_platform *platform;
392
393 if (!buf)
394 return -ENOMEM;
395
Mark Brown2b194f9d2010-10-13 10:52:16 +0100396 list_for_each_entry(platform, &platform_list, list) {
397 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
398 platform->name);
399 if (len >= 0)
400 ret += len;
401 if (ret > PAGE_SIZE) {
402 ret = PAGE_SIZE;
403 break;
404 }
405 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100406
Mark Brown2b194f9d2010-10-13 10:52:16 +0100407 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100408
409 kfree(buf);
410
411 return ret;
412}
413
414static const struct file_operations platform_list_fops = {
415 .read = platform_list_read_file,
416 .llseek = default_llseek,/* read accesses f_pos */
417};
418
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200419static void soc_init_card_debugfs(struct snd_soc_card *card)
420{
421 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000422 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200423 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200424 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100425 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200426 return;
427 }
428
429 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
430 card->debugfs_card_root,
431 &card->pop_time);
432 if (!card->debugfs_pop_time)
433 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000434 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200435}
436
437static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
438{
439 debugfs_remove_recursive(card->debugfs_card_root);
440}
441
Mark Brown2624d5f2009-11-03 21:56:13 +0000442#else
443
444static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
445{
446}
447
448static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
449{
450}
Axel Linb95fccb2010-11-09 17:06:44 +0800451
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000452static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
453{
454}
455
456static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
457{
458}
459
Axel Linb95fccb2010-11-09 17:06:44 +0800460static inline void soc_init_card_debugfs(struct snd_soc_card *card)
461{
462}
463
464static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
465{
466}
Mark Brown2624d5f2009-11-03 21:56:13 +0000467#endif
468
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100469struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
470 const char *dai_link, int stream)
471{
472 int i;
473
474 for (i = 0; i < card->num_links; i++) {
475 if (card->rtd[i].dai_link->no_pcm &&
476 !strcmp(card->rtd[i].dai_link->name, dai_link))
477 return card->rtd[i].pcm->streams[stream].substream;
478 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000479 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100480 return NULL;
481}
482EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
483
484struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
485 const char *dai_link)
486{
487 int i;
488
489 for (i = 0; i < card->num_links; i++) {
490 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
491 return &card->rtd[i];
492 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000493 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100494 return NULL;
495}
496EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
497
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200498#ifdef CONFIG_SND_SOC_AC97_BUS
499/* unregister ac97 codec */
500static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
501{
502 if (codec->ac97->dev.bus)
503 device_unregister(&codec->ac97->dev);
504 return 0;
505}
506
507/* stop no dev release warning */
508static void soc_ac97_device_release(struct device *dev){}
509
510/* register ac97 codec to bus */
511static int soc_ac97_dev_register(struct snd_soc_codec *codec)
512{
513 int err;
514
515 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100516 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200517 codec->ac97->dev.release = soc_ac97_device_release;
518
Kay Sieversbb072bf2008-11-02 03:50:35 +0100519 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000520 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200521 err = device_register(&codec->ac97->dev);
522 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000523 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200524 codec->ac97->dev.bus = NULL;
525 return err;
526 }
527 return 0;
528}
529#endif
530
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100531static void codec2codec_close_delayed_work(struct work_struct *work)
532{
533 /* Currently nothing to do for c2c links
534 * Since c2c links are internal nodes in the DAPM graph and
535 * don't interface with the outside world or application layer
536 * we don't have to do any special handling on close.
537 */
538}
539
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000540#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200541/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000542int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200543{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000544 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200545 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200546 int i;
547
Daniel Macke3509ff2009-06-03 17:44:49 +0200548 /* If the initialization of this soc device failed, there is no codec
549 * associated with it. Just bail out in this case.
550 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000551 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200552 return 0;
553
Andy Green6ed25972008-06-13 16:24:05 +0100554 /* Due to the resume being scheduled into a workqueue we could
555 * suspend before that's finished - wait for it to complete.
556 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000557 snd_power_lock(card->snd_card);
558 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
559 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100560
561 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100563
Mark Browna00f90f2010-12-02 16:24:24 +0000564 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000565 for (i = 0; i < card->num_rtd; i++) {
566 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
567 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100568
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100570 continue;
571
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 if (drv->ops->digital_mute && dai->playback_active)
573 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200574 }
575
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100576 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577 for (i = 0; i < card->num_rtd; i++) {
578 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100579 continue;
580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100582 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100583
Mark Brown87506542008-11-18 20:50:34 +0000584 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000585 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200586
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 for (i = 0; i < card->num_rtd; i++) {
588 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
589 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100590
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100592 continue;
593
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000594 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
595 cpu_dai->driver->suspend(cpu_dai);
596 if (platform->driver->suspend && !platform->suspended) {
597 platform->driver->suspend(cpu_dai);
598 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100599 }
600 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200601
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000602 /* close any waiting streams and save state */
603 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo43829732012-08-20 14:51:24 -0700604 flush_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200605 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000606 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100607
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609
610 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100611 continue;
612
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800613 snd_soc_dapm_stream_event(&card->rtd[i],
614 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800615 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000616
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800617 snd_soc_dapm_stream_event(&card->rtd[i],
618 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800619 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 }
621
Mark Browne2d32ff2012-08-31 17:38:32 -0700622 /* Recheck all analogue paths too */
623 dapm_mark_io_dirty(&card->dapm);
624 snd_soc_dapm_sync(&card->dapm);
625
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200627 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000628 /* If there are paths active then the CODEC will be held with
629 * bias _ON and should not be suspended. */
630 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200631 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000632 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000633 /*
634 * If the CODEC is capable of idle
635 * bias off then being in STANDBY
636 * means it's doing something,
637 * otherwise fall through.
638 */
639 if (codec->dapm.idle_bias_off) {
640 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200641 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000642 break;
643 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100645 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900647 codec->cache_sync = 1;
Mark Brownda8b8e02012-09-12 12:21:52 +0800648 if (codec->using_regmap)
649 regcache_mark_dirty(codec->control_data);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000650 break;
651 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200652 dev_dbg(codec->dev,
653 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000654 break;
655 }
656 }
657 }
658
659 for (i = 0; i < card->num_rtd; i++) {
660 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
661
662 if (card->rtd[i].dai_link->ignore_suspend)
663 continue;
664
665 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
666 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200667 }
668
Mark Brown87506542008-11-18 20:50:34 +0000669 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000670 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200671
672 return 0;
673}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000674EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200675
Andy Green6ed25972008-06-13 16:24:05 +0100676/* deferred resume work, so resume can complete before we finished
677 * setting our codec back up, which can be very slow on I2C
678 */
679static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200680{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000681 struct snd_soc_card *card =
682 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200683 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200684 int i;
685
Andy Green6ed25972008-06-13 16:24:05 +0100686 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
687 * so userspace apps are blocked from touching us
688 */
689
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000690 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100691
Mark Brown99497882010-05-07 20:24:05 +0100692 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100694
Mark Brown87506542008-11-18 20:50:34 +0000695 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000696 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200697
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000698 /* resume AC97 DAIs */
699 for (i = 0; i < card->num_rtd; i++) {
700 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100701
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000702 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100703 continue;
704
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000705 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
706 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200707 }
708
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200709 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000710 /* If the CODEC was idle over suspend then it will have been
711 * left with bias OFF or STANDBY and suspended so we must now
712 * resume. Otherwise the suspend was suppressed.
713 */
714 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200715 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000716 case SND_SOC_BIAS_STANDBY:
717 case SND_SOC_BIAS_OFF:
718 codec->driver->resume(codec);
719 codec->suspended = 0;
720 break;
721 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200722 dev_dbg(codec->dev,
723 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 break;
725 }
Mark Brown1547aba2010-05-07 21:11:40 +0100726 }
727 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200728
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000729 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100730
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000731 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100732 continue;
733
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800734 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000735 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800736 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000737
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800738 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000739 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800740 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200741 }
742
Mark Brown3ff3f642008-05-19 12:32:25 +0200743 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000744 for (i = 0; i < card->num_rtd; i++) {
745 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
746 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100749 continue;
750
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000751 if (drv->ops->digital_mute && dai->playback_active)
752 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200753 }
754
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000755 for (i = 0; i < card->num_rtd; i++) {
756 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
757 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100758
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000759 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100760 continue;
761
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000762 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
763 cpu_dai->driver->resume(cpu_dai);
764 if (platform->driver->resume && platform->suspended) {
765 platform->driver->resume(cpu_dai);
766 platform->suspended = 0;
767 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200768 }
769
Mark Brown87506542008-11-18 20:50:34 +0000770 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000771 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200772
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000773 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100774
775 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000776 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700777
778 /* Recheck all analogue paths too */
779 dapm_mark_io_dirty(&card->dapm);
780 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100781}
782
783/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000784int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100785{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000786 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600787 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200788
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800789 /* If the initialization of this soc device failed, there is no codec
790 * associated with it. Just bail out in this case.
791 */
792 if (list_empty(&card->codec_dev_list))
793 return 0;
794
Mark Brown64ab9ba2009-03-31 11:27:03 +0100795 /* AC97 devices might have other drivers hanging off them so
796 * need to resume immediately. Other drivers don't have that
797 * problem and may take a substantial amount of time to resume
798 * due to I/O costs and anti-pop so handle them out of line.
799 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000800 for (i = 0; i < card->num_rtd; i++) {
801 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600802 ac97_control |= cpu_dai->driver->ac97_control;
803 }
804 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000805 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600806 soc_resume_deferred(&card->deferred_resume_work);
807 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000808 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600809 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000810 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100811 }
Andy Green6ed25972008-06-13 16:24:05 +0100812
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200813 return 0;
814}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000815EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200816#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000817#define snd_soc_suspend NULL
818#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200819#endif
820
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100821static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800822};
823
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000824static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200825{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
827 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000828 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000829 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000830 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100831 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200832
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000833 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000834
Mark Brownb19e6e72012-03-14 21:18:39 +0000835 /* Find CPU DAI from registered DAIs*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000836 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600837 if (dai_link->cpu_of_node &&
838 (cpu_dai->dev->of_node != dai_link->cpu_of_node))
839 continue;
840 if (dai_link->cpu_name &&
841 strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
842 continue;
843 if (dai_link->cpu_dai_name &&
844 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
845 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700846
847 rtd->cpu_dai = cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000848 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000849
850 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000851 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000852 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000853 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000854 }
855
Mark Brownb19e6e72012-03-14 21:18:39 +0000856 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700858 if (dai_link->codec_of_node) {
859 if (codec->dev->of_node != dai_link->codec_of_node)
860 continue;
861 } else {
862 if (strcmp(codec->name, dai_link->codec_name))
863 continue;
864 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000865
Stephen Warren2610ab72011-12-07 13:58:27 -0700866 rtd->codec = codec;
867
868 /*
869 * CODEC found, so find CODEC DAI from registered DAIs from
870 * this CODEC
871 */
872 list_for_each_entry(codec_dai, &dai_list, list) {
873 if (codec->dev == codec_dai->dev &&
874 !strcmp(codec_dai->name,
875 dai_link->codec_dai_name)) {
876
877 rtd->codec_dai = codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000878 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000879 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000880
881 if (!rtd->codec_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000882 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700883 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000884 return -EPROBE_DEFER;
885 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000886 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000887
Mark Brownb19e6e72012-03-14 21:18:39 +0000888 if (!rtd->codec) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000889 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
Mark Brownb19e6e72012-03-14 21:18:39 +0000890 dai_link->codec_name);
891 return -EPROBE_DEFER;
892 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100893
894 /* if there's no platform we match on the empty platform */
895 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700896 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100897 platform_name = "snd-soc-dummy";
898
Mark Brownb19e6e72012-03-14 21:18:39 +0000899 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000900 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700901 if (dai_link->platform_of_node) {
902 if (platform->dev->of_node !=
903 dai_link->platform_of_node)
904 continue;
905 } else {
906 if (strcmp(platform->name, platform_name))
907 continue;
908 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700909
910 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000911 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000912 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000913 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000914 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000915 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000916 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000917
918 card->num_rtd++;
919
920 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000921}
922
Stephen Warrend12cd192012-06-08 12:34:22 -0600923static int soc_remove_platform(struct snd_soc_platform *platform)
924{
925 int ret;
926
927 if (platform->driver->remove) {
928 ret = platform->driver->remove(platform);
929 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000930 dev_err(platform->dev, "ASoC: failed to remove %d\n",
931 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -0600932 }
933
934 /* Make sure all DAPM widgets are freed */
935 snd_soc_dapm_free(&platform->dapm);
936
937 soc_cleanup_platform_debugfs(platform);
938 platform->probed = 0;
939 list_del(&platform->card_list);
940 module_put(platform->dev->driver->owner);
941
942 return 0;
943}
944
Jarkko Nikula589c3562010-12-06 16:27:07 +0200945static void soc_remove_codec(struct snd_soc_codec *codec)
946{
947 int err;
948
949 if (codec->driver->remove) {
950 err = codec->driver->remove(codec);
951 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000952 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200953 }
954
955 /* Make sure all DAPM widgets are freed */
956 snd_soc_dapm_free(&codec->dapm);
957
958 soc_cleanup_codec_debugfs(codec);
959 codec->probed = 0;
960 list_del(&codec->card_list);
961 module_put(codec->dev->driver->owner);
962}
963
Stephen Warren62ae68f2012-06-08 12:34:23 -0600964static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000965{
966 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000967 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
968 int err;
969
970 /* unregister the rtd device */
971 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800972 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
973 device_remove_file(rtd->dev, &dev_attr_codec_reg);
974 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000975 rtd->dev_registered = 0;
976 }
977
978 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100979 if (codec_dai && codec_dai->probed &&
980 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000981 if (codec_dai->driver->remove) {
982 err = codec_dai->driver->remove(codec_dai);
983 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000984 dev_err(codec_dai->dev,
985 "ASoC: failed to remove %s: %d\n",
986 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000987 }
988 codec_dai->probed = 0;
989 list_del(&codec_dai->card_list);
990 }
991
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000992 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100993 if (cpu_dai && cpu_dai->probed &&
994 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000995 if (cpu_dai->driver->remove) {
996 err = cpu_dai->driver->remove(cpu_dai);
997 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000998 dev_err(cpu_dai->dev,
999 "ASoC: failed to remove %s: %d\n",
1000 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001001 }
1002 cpu_dai->probed = 0;
1003 list_del(&cpu_dai->card_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001004
Stephen Warren18d75642012-06-08 12:34:21 -06001005 if (!cpu_dai->codec) {
1006 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001007 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -06001008 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001009 }
1010}
1011
Stephen Warren62ae68f2012-06-08 12:34:23 -06001012static void soc_remove_link_components(struct snd_soc_card *card, int num,
1013 int order)
1014{
1015 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1016 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1017 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1018 struct snd_soc_platform *platform = rtd->platform;
1019 struct snd_soc_codec *codec;
1020
1021 /* remove the platform */
1022 if (platform && platform->probed &&
1023 platform->driver->remove_order == order) {
1024 soc_remove_platform(platform);
1025 }
1026
1027 /* remove the CODEC-side CODEC */
1028 if (codec_dai) {
1029 codec = codec_dai->codec;
1030 if (codec && codec->probed &&
1031 codec->driver->remove_order == order)
1032 soc_remove_codec(codec);
1033 }
1034
1035 /* remove any CPU-side CODEC */
1036 if (cpu_dai) {
1037 codec = cpu_dai->codec;
1038 if (codec && codec->probed &&
1039 codec->driver->remove_order == order)
1040 soc_remove_codec(codec);
1041 }
1042}
1043
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001044static void soc_remove_dai_links(struct snd_soc_card *card)
1045{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001046 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001047
Liam Girdwood0168bf02011-06-07 16:08:05 +01001048 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1049 order++) {
1050 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001051 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001052 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001053
1054 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1055 order++) {
1056 for (dai = 0; dai < card->num_rtd; dai++)
1057 soc_remove_link_components(card, dai, order);
1058 }
1059
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001060 card->num_rtd = 0;
1061}
1062
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001063static void soc_set_name_prefix(struct snd_soc_card *card,
1064 struct snd_soc_codec *codec)
1065{
1066 int i;
1067
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001068 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001069 return;
1070
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001071 for (i = 0; i < card->num_configs; i++) {
1072 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001073 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1074 codec->name_prefix = map->name_prefix;
1075 break;
1076 }
1077 }
1078}
1079
Jarkko Nikula589c3562010-12-06 16:27:07 +02001080static int soc_probe_codec(struct snd_soc_card *card,
1081 struct snd_soc_codec *codec)
1082{
1083 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001084 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001085 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001086
1087 codec->card = card;
1088 codec->dapm.card = card;
1089 soc_set_name_prefix(card, codec);
1090
Jarkko Nikula70d29332011-01-27 16:24:22 +02001091 if (!try_module_get(codec->dev->driver->owner))
1092 return -ENODEV;
1093
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001094 soc_init_codec_debugfs(codec);
1095
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001096 if (driver->dapm_widgets)
1097 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1098 driver->num_dapm_widgets);
1099
Mark Brown888df392012-02-16 19:37:51 -08001100 /* Create DAPM widgets for each DAI stream */
1101 list_for_each_entry(dai, &dai_list, list) {
1102 if (dai->dev != codec->dev)
1103 continue;
1104
1105 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1106 }
1107
Mark Brown33c5f962011-08-22 18:40:30 +01001108 codec->dapm.idle_bias_off = driver->idle_bias_off;
1109
Mark Brown89b95ac02011-03-07 16:38:44 +00001110 if (driver->probe) {
1111 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001112 if (ret < 0) {
1113 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001114 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001115 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001116 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001117 WARN(codec->dapm.idle_bias_off &&
1118 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001119 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1120 codec->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001121 }
1122
Mark Brown38cbf952012-06-22 13:04:02 +01001123 /* If the driver didn't set I/O up try regmap */
Mark Brown98d30882012-08-01 20:05:47 +01001124 if (!codec->write && dev_get_regmap(codec->dev, NULL))
Mark Brown38cbf952012-06-22 13:04:02 +01001125 snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1126
Mark Brownb7af1da2011-04-07 19:18:44 +09001127 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001128 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001129 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001130 if (driver->dapm_routes)
1131 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1132 driver->num_dapm_routes);
1133
Jarkko Nikula589c3562010-12-06 16:27:07 +02001134 /* mark codec as probed and add to card codec list */
1135 codec->probed = 1;
1136 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001137 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001138
Jarkko Nikula70d29332011-01-27 16:24:22 +02001139 return 0;
1140
1141err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001142 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001143 module_put(codec->dev->driver->owner);
1144
Jarkko Nikula589c3562010-12-06 16:27:07 +02001145 return ret;
1146}
1147
Liam Girdwood956245e2011-07-01 16:54:08 +01001148static int soc_probe_platform(struct snd_soc_card *card,
1149 struct snd_soc_platform *platform)
1150{
1151 int ret = 0;
1152 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001153 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001154
1155 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001156 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001157
1158 if (!try_module_get(platform->dev->driver->owner))
1159 return -ENODEV;
1160
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001161 soc_init_platform_debugfs(platform);
1162
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001163 if (driver->dapm_widgets)
1164 snd_soc_dapm_new_controls(&platform->dapm,
1165 driver->dapm_widgets, driver->num_dapm_widgets);
1166
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001167 /* Create DAPM widgets for each DAI stream */
1168 list_for_each_entry(dai, &dai_list, list) {
1169 if (dai->dev != platform->dev)
1170 continue;
1171
1172 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1173 }
1174
Stephen Warren3fec6b62012-04-05 12:28:01 -06001175 platform->dapm.idle_bias_off = 1;
1176
Liam Girdwood956245e2011-07-01 16:54:08 +01001177 if (driver->probe) {
1178 ret = driver->probe(platform);
1179 if (ret < 0) {
1180 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001181 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001182 goto err_probe;
1183 }
1184 }
1185
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001186 if (driver->controls)
1187 snd_soc_add_platform_controls(platform, driver->controls,
1188 driver->num_controls);
1189 if (driver->dapm_routes)
1190 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1191 driver->num_dapm_routes);
1192
Liam Girdwood956245e2011-07-01 16:54:08 +01001193 /* mark platform as probed and add to card platform list */
1194 platform->probed = 1;
1195 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001196 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001197
1198 return 0;
1199
1200err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001201 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001202 module_put(platform->dev->driver->owner);
1203
1204 return ret;
1205}
1206
Mark Brown36ae1a92012-01-06 17:12:45 -08001207static void rtd_release(struct device *dev)
1208{
1209 kfree(dev);
1210}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001211
Jarkko Nikula589c3562010-12-06 16:27:07 +02001212static int soc_post_component_init(struct snd_soc_card *card,
1213 struct snd_soc_codec *codec,
1214 int num, int dailess)
1215{
1216 struct snd_soc_dai_link *dai_link = NULL;
1217 struct snd_soc_aux_dev *aux_dev = NULL;
1218 struct snd_soc_pcm_runtime *rtd;
1219 const char *temp, *name;
1220 int ret = 0;
1221
1222 if (!dailess) {
1223 dai_link = &card->dai_link[num];
1224 rtd = &card->rtd[num];
1225 name = dai_link->name;
1226 } else {
1227 aux_dev = &card->aux_dev[num];
1228 rtd = &card->rtd_aux[num];
1229 name = aux_dev->name;
1230 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001231 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001232
1233 /* machine controls, routes and widgets are not prefixed */
1234 temp = codec->name_prefix;
1235 codec->name_prefix = NULL;
1236
1237 /* do machine specific initialization */
1238 if (!dailess && dai_link->init)
1239 ret = dai_link->init(rtd);
1240 else if (dailess && aux_dev->init)
1241 ret = aux_dev->init(&codec->dapm);
1242 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001243 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001244 return ret;
1245 }
1246 codec->name_prefix = temp;
1247
Jarkko Nikula589c3562010-12-06 16:27:07 +02001248 /* register the rtd device */
1249 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001250
1251 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1252 if (!rtd->dev)
1253 return -ENOMEM;
1254 device_initialize(rtd->dev);
1255 rtd->dev->parent = card->dev;
1256 rtd->dev->release = rtd_release;
1257 rtd->dev->init_name = name;
1258 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001259 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001260 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1261 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1262 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1263 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001264 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001265 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001266 /* calling put_device() here to free the rtd->dev */
1267 put_device(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001268 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001269 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001270 return ret;
1271 }
1272 rtd->dev_registered = 1;
1273
1274 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001275 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001276 if (ret < 0)
1277 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001278 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001279
1280 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001281 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001282 if (ret < 0)
1283 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001284 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001285
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001286#ifdef CONFIG_DEBUG_FS
1287 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001288 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001289 goto out;
1290
1291 ret = soc_dpcm_debugfs_add(rtd);
1292 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001293 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001294
1295out:
1296#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001297 return 0;
1298}
1299
Stephen Warren62ae68f2012-06-08 12:34:23 -06001300static int soc_probe_link_components(struct snd_soc_card *card, int num,
1301 int order)
1302{
1303 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1304 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1305 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1306 struct snd_soc_platform *platform = rtd->platform;
1307 int ret;
1308
1309 /* probe the CPU-side component, if it is a CODEC */
1310 if (cpu_dai->codec &&
1311 !cpu_dai->codec->probed &&
1312 cpu_dai->codec->driver->probe_order == order) {
1313 ret = soc_probe_codec(card, cpu_dai->codec);
1314 if (ret < 0)
1315 return ret;
1316 }
1317
1318 /* probe the CODEC-side component */
1319 if (!codec_dai->codec->probed &&
1320 codec_dai->codec->driver->probe_order == order) {
1321 ret = soc_probe_codec(card, codec_dai->codec);
1322 if (ret < 0)
1323 return ret;
1324 }
1325
1326 /* probe the platform */
1327 if (!platform->probed &&
1328 platform->driver->probe_order == order) {
1329 ret = soc_probe_platform(card, platform);
1330 if (ret < 0)
1331 return ret;
1332 }
1333
1334 return 0;
1335}
1336
1337static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001338{
1339 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1340 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1341 struct snd_soc_codec *codec = rtd->codec;
1342 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001343 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1344 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1345 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001346 int ret;
1347
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001348 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001349 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001350
1351 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001352 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001353 codec_dai->card = card;
1354 cpu_dai->card = card;
1355
1356 /* set default power off timeout */
1357 rtd->pmdown_time = pmdown_time;
1358
1359 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001360 if (!cpu_dai->probed &&
1361 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001362 if (!cpu_dai->codec) {
1363 cpu_dai->dapm.card = card;
1364 if (!try_module_get(cpu_dai->dev->driver->owner))
1365 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001366
Stephen Warren18d75642012-06-08 12:34:21 -06001367 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001368 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1369 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001370
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001371 if (cpu_dai->driver->probe) {
1372 ret = cpu_dai->driver->probe(cpu_dai);
1373 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001374 dev_err(cpu_dai->dev,
1375 "ASoC: failed to probe CPU DAI %s: %d\n",
1376 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001377 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001378 return ret;
1379 }
1380 }
1381 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001382 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001383 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1384 }
1385
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001386 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001387 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001388 if (codec_dai->driver->probe) {
1389 ret = codec_dai->driver->probe(codec_dai);
1390 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001391 dev_err(codec_dai->dev,
1392 "ASoC: failed to probe CODEC DAI %s: %d\n",
1393 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001394 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001395 }
1396 }
1397
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001398 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001399 codec_dai->probed = 1;
1400 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001401 }
1402
Liam Girdwood0168bf02011-06-07 16:08:05 +01001403 /* complete DAI probe during last probe */
1404 if (order != SND_SOC_COMP_ORDER_LAST)
1405 return 0;
1406
Jarkko Nikula589c3562010-12-06 16:27:07 +02001407 ret = soc_post_component_init(card, codec, num, 0);
1408 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001409 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001410
Mark Brown36ae1a92012-01-06 17:12:45 -08001411 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001412 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001413 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1414 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001415
Namarta Kohli1245b702012-08-16 17:10:41 +05301416 if (cpu_dai->driver->compress_dai) {
1417 /*create compress_device"*/
1418 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001419 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001420 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301421 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001422 return ret;
1423 }
1424 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301425
1426 if (!dai_link->params) {
1427 /* create the pcm */
1428 ret = soc_new_pcm(rtd, num);
1429 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001430 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301431 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001432 return ret;
1433 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301434 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001435 INIT_DELAYED_WORK(&rtd->delayed_work,
1436 codec2codec_close_delayed_work);
1437
Namarta Kohli1245b702012-08-16 17:10:41 +05301438 /* link the DAI widgets */
1439 play_w = codec_dai->playback_widget;
1440 capture_w = cpu_dai->capture_widget;
1441 if (play_w && capture_w) {
1442 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Mark Brownc74184e2012-04-04 22:12:09 +01001443 capture_w, play_w);
Namarta Kohli1245b702012-08-16 17:10:41 +05301444 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001445 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301446 play_w->name, capture_w->name, ret);
1447 return ret;
1448 }
1449 }
1450
1451 play_w = cpu_dai->playback_widget;
1452 capture_w = codec_dai->capture_widget;
1453 if (play_w && capture_w) {
1454 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1455 capture_w, play_w);
1456 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001457 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301458 play_w->name, capture_w->name, ret);
1459 return ret;
1460 }
Mark Brownc74184e2012-04-04 22:12:09 +01001461 }
1462 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001463 }
1464
1465 /* add platform data for AC97 devices */
1466 if (rtd->codec_dai->driver->ac97_control)
1467 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1468
1469 return 0;
1470}
1471
1472#ifdef CONFIG_SND_SOC_AC97_BUS
1473static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1474{
1475 int ret;
1476
1477 /* Only instantiate AC97 if not already done by the adaptor
1478 * for the generic AC97 subsystem.
1479 */
1480 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001481 /*
1482 * It is possible that the AC97 device is already registered to
1483 * the device subsystem. This happens when the device is created
1484 * via snd_ac97_mixer(). Currently only SoC codec that does so
1485 * is the generic AC97 glue but others migh emerge.
1486 *
1487 * In those cases we don't try to register the device again.
1488 */
1489 if (!rtd->codec->ac97_created)
1490 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001491
1492 ret = soc_ac97_dev_register(rtd->codec);
1493 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001494 dev_err(rtd->codec->dev,
1495 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496 return ret;
1497 }
1498
1499 rtd->codec->ac97_registered = 1;
1500 }
1501 return 0;
1502}
1503
1504static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1505{
1506 if (codec->ac97_registered) {
1507 soc_ac97_dev_unregister(codec);
1508 codec->ac97_registered = 0;
1509 }
1510}
1511#endif
1512
Mark Brownb19e6e72012-03-14 21:18:39 +00001513static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1514{
1515 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1516 struct snd_soc_codec *codec;
1517
1518 /* find CODEC from registered CODECs*/
1519 list_for_each_entry(codec, &codec_list, list) {
1520 if (!strcmp(codec->name, aux_dev->codec_name))
1521 return 0;
1522 }
1523
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001524 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
Mark Brownfb099cb2012-08-09 18:44:37 +01001525
Mark Brownb19e6e72012-03-14 21:18:39 +00001526 return -EPROBE_DEFER;
1527}
1528
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001529static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1530{
1531 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001532 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001533 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001534
1535 /* find CODEC from registered CODECs*/
1536 list_for_each_entry(codec, &codec_list, list) {
1537 if (!strcmp(codec->name, aux_dev->codec_name)) {
1538 if (codec->probed) {
1539 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001540 "ASoC: codec already probed");
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001541 ret = -EBUSY;
1542 goto out;
1543 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001544 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001545 }
1546 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001547 /* codec not found */
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001548 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001549 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001550
Jarkko Nikula676ad982010-12-03 09:18:22 +02001551found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001552 ret = soc_probe_codec(card, codec);
1553 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001554 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001555
Jarkko Nikula589c3562010-12-06 16:27:07 +02001556 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001557
1558out:
1559 return ret;
1560}
1561
1562static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1563{
1564 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1565 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001566
1567 /* unregister the rtd device */
1568 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001569 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001570 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001571 rtd->dev_registered = 0;
1572 }
1573
Jarkko Nikula589c3562010-12-06 16:27:07 +02001574 if (codec && codec->probed)
1575 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001576}
1577
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001578static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1579 enum snd_soc_compress_type compress_type)
1580{
1581 int ret;
1582
1583 if (codec->cache_init)
1584 return 0;
1585
1586 /* override the compress_type if necessary */
1587 if (compress_type && codec->compress_type != compress_type)
1588 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001589 ret = snd_soc_cache_init(codec);
1590 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001591 dev_err(codec->dev,
1592 "ASoC: Failed to set cache compression type: %d\n",
1593 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001594 return ret;
1595 }
1596 codec->cache_init = 1;
1597 return 0;
1598}
1599
Mark Brownb19e6e72012-03-14 21:18:39 +00001600static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001601{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001602 struct snd_soc_codec *codec;
1603 struct snd_soc_codec_conf *codec_conf;
1604 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001605 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001606 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001607
Liam Girdwood01b9d992012-03-07 10:38:25 +00001608 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001609
Mark Brownb19e6e72012-03-14 21:18:39 +00001610 /* bind DAIs */
1611 for (i = 0; i < card->num_links; i++) {
1612 ret = soc_bind_dai_link(card, i);
1613 if (ret != 0)
1614 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001615 }
1616
Mark Brownb19e6e72012-03-14 21:18:39 +00001617 /* check aux_devs too */
1618 for (i = 0; i < card->num_aux_devs; i++) {
1619 ret = soc_check_aux_dev(card, i);
1620 if (ret != 0)
1621 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001622 }
1623
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001624 /* initialize the register cache for each available codec */
1625 list_for_each_entry(codec, &codec_list, list) {
1626 if (codec->cache_init)
1627 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001628 /* by default we don't override the compress_type */
1629 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001630 /* check to see if we need to override the compress_type */
1631 for (i = 0; i < card->num_configs; ++i) {
1632 codec_conf = &card->codec_conf[i];
1633 if (!strcmp(codec->name, codec_conf->dev_name)) {
1634 compress_type = codec_conf->compress_type;
1635 if (compress_type && compress_type
1636 != codec->compress_type)
1637 break;
1638 }
1639 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001640 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001641 if (ret < 0)
1642 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001643 }
1644
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001645 /* card bind complete so register a sound card */
1646 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1647 card->owner, 0, &card->snd_card);
1648 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001649 dev_err(card->dev,
1650 "ASoC: can't create sound card for card %s: %d\n",
1651 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001652 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001653 }
1654 card->snd_card->dev = card->dev;
1655
Mark Browne37a4972011-03-02 18:21:57 +00001656 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1657 card->dapm.dev = card->dev;
1658 card->dapm.card = card;
1659 list_add(&card->dapm.list, &card->dapm_list);
1660
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001661#ifdef CONFIG_DEBUG_FS
1662 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1663#endif
1664
Mark Brown88ee1c62011-02-02 10:43:26 +00001665#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001666 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001667 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001668#endif
Andy Green6ed25972008-06-13 16:24:05 +01001669
Mark Brown9a841eb2011-04-12 17:51:37 -07001670 if (card->dapm_widgets)
1671 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1672 card->num_dapm_widgets);
1673
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001674 /* initialise the sound card only once */
1675 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001676 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001677 if (ret < 0)
1678 goto card_probe_error;
1679 }
1680
Stephen Warren62ae68f2012-06-08 12:34:23 -06001681 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001682 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1683 order++) {
1684 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001685 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001686 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001687 dev_err(card->dev,
1688 "ASoC: failed to instantiate card %d\n",
1689 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001690 goto probe_dai_err;
1691 }
1692 }
1693 }
1694
1695 /* probe all DAI links on this card */
1696 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1697 order++) {
1698 for (i = 0; i < card->num_links; i++) {
1699 ret = soc_probe_link_dais(card, i, order);
1700 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001701 dev_err(card->dev,
1702 "ASoC: failed to instantiate card %d\n",
1703 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001704 goto probe_dai_err;
1705 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001706 }
1707 }
1708
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001709 for (i = 0; i < card->num_aux_devs; i++) {
1710 ret = soc_probe_aux_dev(card, i);
1711 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001712 dev_err(card->dev,
1713 "ASoC: failed to add auxiliary devices %d\n",
1714 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001715 goto probe_aux_dev_err;
1716 }
1717 }
1718
Mark Brown888df392012-02-16 19:37:51 -08001719 snd_soc_dapm_link_dai_widgets(card);
1720
Mark Brownb7af1da2011-04-07 19:18:44 +09001721 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001722 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001723
Mark Brownb8ad29d2011-03-02 18:35:51 +00001724 if (card->dapm_routes)
1725 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1726 card->num_dapm_routes);
1727
Mark Brown75d9ac42011-09-27 16:41:01 +01001728 for (i = 0; i < card->num_links; i++) {
1729 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001730 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001731
Mark Brownf04209a2012-04-09 16:29:21 +01001732 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001733 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001734 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001735 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001736 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001737 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001738 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001739 }
1740
1741 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001742 if (dai_fmt &&
1743 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001744 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1745 dai_fmt);
1746 if (ret != 0 && ret != -ENOTSUPP)
1747 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001748 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001749 ret);
1750 } else if (dai_fmt) {
1751 /* Flip the polarity for the "CPU" end */
1752 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1753 switch (dai_link->dai_fmt &
1754 SND_SOC_DAIFMT_MASTER_MASK) {
1755 case SND_SOC_DAIFMT_CBM_CFM:
1756 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1757 break;
1758 case SND_SOC_DAIFMT_CBM_CFS:
1759 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1760 break;
1761 case SND_SOC_DAIFMT_CBS_CFM:
1762 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1763 break;
1764 case SND_SOC_DAIFMT_CBS_CFS:
1765 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1766 break;
1767 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001768
1769 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001770 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001771 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001772 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001773 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001774 ret);
1775 }
1776 }
1777
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001778 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001779 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001780 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1781 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001782 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1783 "%s", card->driver_name ? card->driver_name : card->name);
1784 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1785 switch (card->snd_card->driver[i]) {
1786 case '_':
1787 case '-':
1788 case '\0':
1789 break;
1790 default:
1791 if (!isalnum(card->snd_card->driver[i]))
1792 card->snd_card->driver[i] = '_';
1793 break;
1794 }
1795 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001796
Mark Brown28e9ad92011-03-02 18:36:34 +00001797 if (card->late_probe) {
1798 ret = card->late_probe(card);
1799 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001800 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001801 card->name, ret);
1802 goto probe_aux_dev_err;
1803 }
1804 }
1805
Stephen Warren16332812011-11-23 12:42:04 -07001806 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001807 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001808 snd_soc_dapm_auto_nc_codec_pins(codec);
1809
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001810 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001811
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001812 ret = snd_card_register(card->snd_card);
1813 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001814 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1815 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001816 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001817 }
1818
1819#ifdef CONFIG_SND_SOC_AC97_BUS
1820 /* register any AC97 codecs */
1821 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001822 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1823 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001824 dev_err(card->dev,
1825 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001826 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001827 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001828 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001829 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001830 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001831#endif
1832
Mark Brown435c5e22008-12-04 15:32:53 +00001833 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001834 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001835 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001836
1837 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001838
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001839probe_aux_dev_err:
1840 for (i = 0; i < card->num_aux_devs; i++)
1841 soc_remove_aux_dev(card, i);
1842
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001843probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001844 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001845
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001846card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001847 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001848 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001849
1850 snd_card_free(card->snd_card);
1851
Mark Brownb19e6e72012-03-14 21:18:39 +00001852base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001853 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001854
Mark Brownb19e6e72012-03-14 21:18:39 +00001855 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001856}
1857
1858/* probes a new socdev */
1859static int soc_probe(struct platform_device *pdev)
1860{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001861 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001862
Vinod Koul70a7ca32011-01-14 19:22:48 +05301863 /*
1864 * no card, so machine driver should be registering card
1865 * we should not be here in that case so ret error
1866 */
1867 if (!card)
1868 return -EINVAL;
1869
Mark Brownfe4085e2012-03-02 13:07:41 +00001870 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001871 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001872 card->name);
1873
Mark Brown435c5e22008-12-04 15:32:53 +00001874 /* Bodge while we unpick instantiation */
1875 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001876
Mark Brown28d528c2012-08-09 18:45:23 +01001877 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001878}
1879
Vinod Koulb0e26482011-01-13 22:48:02 +05301880static int soc_cleanup_card_resources(struct snd_soc_card *card)
1881{
Vinod Koulb0e26482011-01-13 22:48:02 +05301882 int i;
1883
1884 /* make sure any delayed work runs */
1885 for (i = 0; i < card->num_rtd; i++) {
1886 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001887 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301888 }
1889
1890 /* remove auxiliary devices */
1891 for (i = 0; i < card->num_aux_devs; i++)
1892 soc_remove_aux_dev(card, i);
1893
1894 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001895 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301896
1897 soc_cleanup_card_debugfs(card);
1898
1899 /* remove the card */
1900 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001901 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301902
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001903 snd_soc_dapm_free(&card->dapm);
1904
Vinod Koulb0e26482011-01-13 22:48:02 +05301905 snd_card_free(card->snd_card);
1906 return 0;
1907
1908}
1909
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001910/* removes a socdev */
1911static int soc_remove(struct platform_device *pdev)
1912{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001913 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001914
Mark Brownc5af3a22008-11-28 13:29:45 +00001915 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001916 return 0;
1917}
1918
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001919int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001920{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001921 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001922 int i;
Mark Brown51737472009-06-22 13:16:51 +01001923
1924 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001925 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001926
1927 /* Flush out pmdown_time work - we actually do want to run it
1928 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001929 for (i = 0; i < card->num_rtd; i++) {
1930 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001931 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001932 }
Mark Brown51737472009-06-22 13:16:51 +01001933
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001934 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001935
1936 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001937}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001938EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001939
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001940const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301941 .suspend = snd_soc_suspend,
1942 .resume = snd_soc_resume,
1943 .freeze = snd_soc_suspend,
1944 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001945 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301946 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001947};
Stephen Warrendeb26072011-04-05 19:35:30 -06001948EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001949
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001950/* ASoC platform driver */
1951static struct platform_driver soc_driver = {
1952 .driver = {
1953 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001954 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001955 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001956 },
1957 .probe = soc_probe,
1958 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001959};
1960
Mark Brown096e49d2009-07-05 15:12:22 +01001961/**
1962 * snd_soc_codec_volatile_register: Report if a register is volatile.
1963 *
1964 * @codec: CODEC to query.
1965 * @reg: Register to query.
1966 *
1967 * Boolean function indiciating if a CODEC register is volatile.
1968 */
Mark Brown181e0552011-01-24 14:05:25 +00001969int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1970 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001971{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001972 if (codec->volatile_register)
1973 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001974 else
1975 return 0;
1976}
1977EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1978
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001979/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001980 * snd_soc_codec_readable_register: Report if a register is readable.
1981 *
1982 * @codec: CODEC to query.
1983 * @reg: Register to query.
1984 *
1985 * Boolean function indicating if a CODEC register is readable.
1986 */
1987int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1988 unsigned int reg)
1989{
1990 if (codec->readable_register)
1991 return codec->readable_register(codec, reg);
1992 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001993 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001994}
1995EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1996
1997/**
1998 * snd_soc_codec_writable_register: Report if a register is writable.
1999 *
2000 * @codec: CODEC to query.
2001 * @reg: Register to query.
2002 *
2003 * Boolean function indicating if a CODEC register is writable.
2004 */
2005int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2006 unsigned int reg)
2007{
2008 if (codec->writable_register)
2009 return codec->writable_register(codec, reg);
2010 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02002011 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002012}
2013EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2014
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002015int snd_soc_platform_read(struct snd_soc_platform *platform,
2016 unsigned int reg)
2017{
2018 unsigned int ret;
2019
2020 if (!platform->driver->read) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002021 dev_err(platform->dev, "ASoC: platform has no read back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002022 return -1;
2023 }
2024
2025 ret = platform->driver->read(platform, reg);
2026 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002027 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002028
2029 return ret;
2030}
2031EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2032
2033int snd_soc_platform_write(struct snd_soc_platform *platform,
2034 unsigned int reg, unsigned int val)
2035{
2036 if (!platform->driver->write) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002037 dev_err(platform->dev, "ASoC: platform has no write back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002038 return -1;
2039 }
2040
2041 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002042 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002043 return platform->driver->write(platform, reg, val);
2044}
2045EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2046
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002047/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002048 * snd_soc_new_ac97_codec - initailise AC97 device
2049 * @codec: audio codec
2050 * @ops: AC97 bus operations
2051 * @num: AC97 codec number
2052 *
2053 * Initialises AC97 codec resources for use by ad-hoc devices only.
2054 */
2055int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2056 struct snd_ac97_bus_ops *ops, int num)
2057{
2058 mutex_lock(&codec->mutex);
2059
2060 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2061 if (codec->ac97 == NULL) {
2062 mutex_unlock(&codec->mutex);
2063 return -ENOMEM;
2064 }
2065
2066 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2067 if (codec->ac97->bus == NULL) {
2068 kfree(codec->ac97);
2069 codec->ac97 = NULL;
2070 mutex_unlock(&codec->mutex);
2071 return -ENOMEM;
2072 }
2073
2074 codec->ac97->bus->ops = ops;
2075 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002076
2077 /*
2078 * Mark the AC97 device to be created by us. This way we ensure that the
2079 * device will be registered with the device subsystem later on.
2080 */
2081 codec->ac97_created = 1;
2082
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002083 mutex_unlock(&codec->mutex);
2084 return 0;
2085}
2086EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2087
Mark Brownb047e1c2013-06-26 12:45:59 +01002088struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002089EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002090
2091int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2092{
2093 if (ops == soc_ac97_ops)
2094 return 0;
2095
2096 if (soc_ac97_ops && ops)
2097 return -EBUSY;
2098
2099 soc_ac97_ops = ops;
2100
2101 return 0;
2102}
2103EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2104
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002105/**
2106 * snd_soc_free_ac97_codec - free AC97 codec device
2107 * @codec: audio codec
2108 *
2109 * Frees AC97 codec device resources.
2110 */
2111void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2112{
2113 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002114#ifdef CONFIG_SND_SOC_AC97_BUS
2115 soc_unregister_ac97_dai_link(codec);
2116#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002117 kfree(codec->ac97->bus);
2118 kfree(codec->ac97);
2119 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002120 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002121 mutex_unlock(&codec->mutex);
2122}
2123EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2124
Mark Brownc3753702010-11-01 15:41:57 -04002125unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2126{
2127 unsigned int ret;
2128
Mark Brownc3acec22010-12-02 16:15:29 +00002129 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002130 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002131 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002132
2133 return ret;
2134}
2135EXPORT_SYMBOL_GPL(snd_soc_read);
2136
2137unsigned int snd_soc_write(struct snd_soc_codec *codec,
2138 unsigned int reg, unsigned int val)
2139{
2140 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002141 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002142 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002143}
2144EXPORT_SYMBOL_GPL(snd_soc_write);
2145
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002146unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2147 unsigned int reg, const void *data, size_t len)
2148{
2149 return codec->bulk_write_raw(codec, reg, data, len);
2150}
2151EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2152
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002153/**
2154 * snd_soc_update_bits - update codec register bits
2155 * @codec: audio codec
2156 * @reg: codec register
2157 * @mask: register mask
2158 * @value: new value
2159 *
2160 * Writes new register value.
2161 *
Timur Tabi180c3292011-01-10 15:58:13 -06002162 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002163 */
2164int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002165 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002166{
Mark Brown8a713da2011-12-03 12:33:55 +00002167 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002168 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002169 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002170
Mark Brown8a713da2011-12-03 12:33:55 +00002171 if (codec->using_regmap) {
2172 ret = regmap_update_bits_check(codec->control_data, reg,
2173 mask, value, &change);
2174 } else {
2175 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002176 if (ret < 0)
2177 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002178
2179 old = ret;
2180 new = (old & ~mask) | (value & mask);
2181 change = old != new;
2182 if (change)
2183 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002184 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002185
Mark Brown8a713da2011-12-03 12:33:55 +00002186 if (ret < 0)
2187 return ret;
2188
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002189 return change;
2190}
2191EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2192
2193/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002194 * snd_soc_update_bits_locked - update codec register bits
2195 * @codec: audio codec
2196 * @reg: codec register
2197 * @mask: register mask
2198 * @value: new value
2199 *
2200 * Writes new register value, and takes the codec mutex.
2201 *
2202 * Returns 1 for change else 0.
2203 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002204int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2205 unsigned short reg, unsigned int mask,
2206 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002207{
2208 int change;
2209
2210 mutex_lock(&codec->mutex);
2211 change = snd_soc_update_bits(codec, reg, mask, value);
2212 mutex_unlock(&codec->mutex);
2213
2214 return change;
2215}
Mark Browndd1b3d52009-12-04 14:22:03 +00002216EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002217
2218/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002219 * snd_soc_test_bits - test register for change
2220 * @codec: audio codec
2221 * @reg: codec register
2222 * @mask: register mask
2223 * @value: new value
2224 *
2225 * Tests a register with a new value and checks if the new value is
2226 * different from the old value.
2227 *
2228 * Returns 1 for change else 0.
2229 */
2230int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002231 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002232{
2233 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002234 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002235
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002236 old = snd_soc_read(codec, reg);
2237 new = (old & ~mask) | value;
2238 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002239
2240 return change;
2241}
2242EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2243
2244/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002245 * snd_soc_cnew - create new control
2246 * @_template: control template
2247 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002248 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002249 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002250 *
2251 * Create a new mixer control from a template control.
2252 *
2253 * Returns 0 for success, else error.
2254 */
2255struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002256 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002257 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002258{
2259 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002260 struct snd_kcontrol *kcontrol;
2261 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002262
2263 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002264 template.index = 0;
2265
Mark Brownefb7ac32011-03-08 17:23:24 +00002266 if (!long_name)
2267 long_name = template.name;
2268
2269 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002270 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002271 if (!name)
2272 return NULL;
2273
Mark Brownefb7ac32011-03-08 17:23:24 +00002274 template.name = name;
2275 } else {
2276 template.name = long_name;
2277 }
2278
2279 kcontrol = snd_ctl_new1(&template, data);
2280
2281 kfree(name);
2282
2283 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002284}
2285EXPORT_SYMBOL_GPL(snd_soc_cnew);
2286
Liam Girdwood022658b2012-02-03 17:43:09 +00002287static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2288 const struct snd_kcontrol_new *controls, int num_controls,
2289 const char *prefix, void *data)
2290{
2291 int err, i;
2292
2293 for (i = 0; i < num_controls; i++) {
2294 const struct snd_kcontrol_new *control = &controls[i];
2295 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2296 control->name, prefix));
2297 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002298 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2299 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002300 return err;
2301 }
2302 }
2303
2304 return 0;
2305}
2306
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002307/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002308 * snd_soc_add_codec_controls - add an array of controls to a codec.
2309 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002310 * duplicating this code.
2311 *
2312 * @codec: codec to add controls to
2313 * @controls: array of controls to add
2314 * @num_controls: number of elements in the array
2315 *
2316 * Return 0 for success, else error.
2317 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002318int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002319 const struct snd_kcontrol_new *controls, int num_controls)
2320{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002321 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002322
Liam Girdwood022658b2012-02-03 17:43:09 +00002323 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2324 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002325}
Liam Girdwood022658b2012-02-03 17:43:09 +00002326EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002327
2328/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002329 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002330 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002331 *
2332 * @platform: platform to add controls to
2333 * @controls: array of controls to add
2334 * @num_controls: number of elements in the array
2335 *
2336 * Return 0 for success, else error.
2337 */
2338int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2339 const struct snd_kcontrol_new *controls, int num_controls)
2340{
2341 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002342
Liam Girdwood022658b2012-02-03 17:43:09 +00002343 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2344 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002345}
2346EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2347
2348/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002349 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2350 * Convenience function to add a list of controls.
2351 *
2352 * @soc_card: SoC card to add controls to
2353 * @controls: array of controls to add
2354 * @num_controls: number of elements in the array
2355 *
2356 * Return 0 for success, else error.
2357 */
2358int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2359 const struct snd_kcontrol_new *controls, int num_controls)
2360{
2361 struct snd_card *card = soc_card->snd_card;
2362
2363 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2364 NULL, soc_card);
2365}
2366EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2367
2368/**
2369 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2370 * Convienience function to add a list of controls.
2371 *
2372 * @dai: DAI to add controls to
2373 * @controls: array of controls to add
2374 * @num_controls: number of elements in the array
2375 *
2376 * Return 0 for success, else error.
2377 */
2378int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2379 const struct snd_kcontrol_new *controls, int num_controls)
2380{
2381 struct snd_card *card = dai->card->snd_card;
2382
2383 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2384 NULL, dai);
2385}
2386EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2387
2388/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002389 * snd_soc_info_enum_double - enumerated double mixer info callback
2390 * @kcontrol: mixer control
2391 * @uinfo: control element information
2392 *
2393 * Callback to provide information about a double enumerated
2394 * mixer control.
2395 *
2396 * Returns 0 for success.
2397 */
2398int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2399 struct snd_ctl_elem_info *uinfo)
2400{
2401 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2402
2403 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2404 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002405 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002406
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002407 if (uinfo->value.enumerated.item > e->max - 1)
2408 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002409 strcpy(uinfo->value.enumerated.name,
2410 e->texts[uinfo->value.enumerated.item]);
2411 return 0;
2412}
2413EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2414
2415/**
2416 * snd_soc_get_enum_double - enumerated double mixer get callback
2417 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002418 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002419 *
2420 * Callback to get the value of a double enumerated mixer.
2421 *
2422 * Returns 0 for success.
2423 */
2424int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2425 struct snd_ctl_elem_value *ucontrol)
2426{
2427 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2428 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002429 unsigned int val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002430
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002431 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002432 ucontrol->value.enumerated.item[0]
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002433 = (val >> e->shift_l) & e->mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002434 if (e->shift_l != e->shift_r)
2435 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002436 (val >> e->shift_r) & e->mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002437
2438 return 0;
2439}
2440EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2441
2442/**
2443 * snd_soc_put_enum_double - enumerated double mixer put callback
2444 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002445 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002446 *
2447 * Callback to set the value of a double enumerated mixer.
2448 *
2449 * Returns 0 for success.
2450 */
2451int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2452 struct snd_ctl_elem_value *ucontrol)
2453{
2454 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2455 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002456 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002457 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002458
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002459 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002460 return -EINVAL;
2461 val = ucontrol->value.enumerated.item[0] << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002462 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002463 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002464 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002465 return -EINVAL;
2466 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002467 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002468 }
2469
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002470 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002471}
2472EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2473
2474/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002475 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2476 * @kcontrol: mixer control
2477 * @ucontrol: control element information
2478 *
2479 * Callback to get the value of a double semi enumerated mixer.
2480 *
2481 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2482 * used for handling bitfield coded enumeration for example.
2483 *
2484 * Returns 0 for success.
2485 */
2486int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2487 struct snd_ctl_elem_value *ucontrol)
2488{
2489 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002490 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002491 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002492
2493 reg_val = snd_soc_read(codec, e->reg);
2494 val = (reg_val >> e->shift_l) & e->mask;
2495 for (mux = 0; mux < e->max; mux++) {
2496 if (val == e->values[mux])
2497 break;
2498 }
2499 ucontrol->value.enumerated.item[0] = mux;
2500 if (e->shift_l != e->shift_r) {
2501 val = (reg_val >> e->shift_r) & e->mask;
2502 for (mux = 0; mux < e->max; mux++) {
2503 if (val == e->values[mux])
2504 break;
2505 }
2506 ucontrol->value.enumerated.item[1] = mux;
2507 }
2508
2509 return 0;
2510}
2511EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2512
2513/**
2514 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2515 * @kcontrol: mixer control
2516 * @ucontrol: control element information
2517 *
2518 * Callback to set the value of a double semi enumerated mixer.
2519 *
2520 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2521 * used for handling bitfield coded enumeration for example.
2522 *
2523 * Returns 0 for success.
2524 */
2525int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2526 struct snd_ctl_elem_value *ucontrol)
2527{
2528 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002529 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002530 unsigned int val;
2531 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002532
2533 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2534 return -EINVAL;
2535 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2536 mask = e->mask << e->shift_l;
2537 if (e->shift_l != e->shift_r) {
2538 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2539 return -EINVAL;
2540 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2541 mask |= e->mask << e->shift_r;
2542 }
2543
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002544 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002545}
2546EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2547
2548/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002549 * snd_soc_info_volsw - single mixer info callback
2550 * @kcontrol: mixer control
2551 * @uinfo: control element information
2552 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002553 * Callback to provide information about a single mixer control, or a double
2554 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002555 *
2556 * Returns 0 for success.
2557 */
2558int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2559 struct snd_ctl_elem_info *uinfo)
2560{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002561 struct soc_mixer_control *mc =
2562 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002563 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002564
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002565 if (!mc->platform_max)
2566 mc->platform_max = mc->max;
2567 platform_max = mc->platform_max;
2568
2569 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002570 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2571 else
2572 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2573
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002574 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002575 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002576 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002577 return 0;
2578}
2579EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2580
2581/**
2582 * snd_soc_get_volsw - single mixer get callback
2583 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002584 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002585 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002586 * Callback to get the value of a single mixer control, or a double mixer
2587 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002588 *
2589 * Returns 0 for success.
2590 */
2591int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2592 struct snd_ctl_elem_value *ucontrol)
2593{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002594 struct soc_mixer_control *mc =
2595 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002596 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002597 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002598 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002599 unsigned int shift = mc->shift;
2600 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002601 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002602 unsigned int mask = (1 << fls(max)) - 1;
2603 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002604
2605 ucontrol->value.integer.value[0] =
2606 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002607 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002608 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002609 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002610
2611 if (snd_soc_volsw_is_stereo(mc)) {
2612 if (reg == reg2)
2613 ucontrol->value.integer.value[1] =
2614 (snd_soc_read(codec, reg) >> rshift) & mask;
2615 else
2616 ucontrol->value.integer.value[1] =
2617 (snd_soc_read(codec, reg2) >> shift) & mask;
2618 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002619 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002620 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002621 }
2622
2623 return 0;
2624}
2625EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2626
2627/**
2628 * snd_soc_put_volsw - single mixer put callback
2629 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002630 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002631 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002632 * Callback to set the value of a single mixer control, or a double mixer
2633 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002634 *
2635 * Returns 0 for success.
2636 */
2637int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2638 struct snd_ctl_elem_value *ucontrol)
2639{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002640 struct soc_mixer_control *mc =
2641 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002642 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002643 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002644 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002645 unsigned int shift = mc->shift;
2646 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002647 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002648 unsigned int mask = (1 << fls(max)) - 1;
2649 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002650 int err;
2651 bool type_2r = 0;
2652 unsigned int val2 = 0;
2653 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002654
2655 val = (ucontrol->value.integer.value[0] & mask);
2656 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002657 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002658 val_mask = mask << shift;
2659 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002660 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002661 val2 = (ucontrol->value.integer.value[1] & mask);
2662 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002663 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002664 if (reg == reg2) {
2665 val_mask |= mask << rshift;
2666 val |= val2 << rshift;
2667 } else {
2668 val2 = val2 << shift;
2669 type_2r = 1;
2670 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002671 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002672 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002673 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002674 return err;
2675
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002676 if (type_2r)
2677 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2678
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002679 return err;
2680}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002681EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002682
Mark Browne13ac2e2008-05-28 17:58:05 +01002683/**
Brian Austin1d99f242012-03-30 10:43:55 -05002684 * snd_soc_get_volsw_sx - single mixer get callback
2685 * @kcontrol: mixer control
2686 * @ucontrol: control element information
2687 *
2688 * Callback to get the value of a single mixer control, or a double mixer
2689 * control that spans 2 registers.
2690 *
2691 * Returns 0 for success.
2692 */
2693int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2694 struct snd_ctl_elem_value *ucontrol)
2695{
2696 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2697 struct soc_mixer_control *mc =
2698 (struct soc_mixer_control *)kcontrol->private_value;
2699
2700 unsigned int reg = mc->reg;
2701 unsigned int reg2 = mc->rreg;
2702 unsigned int shift = mc->shift;
2703 unsigned int rshift = mc->rshift;
2704 int max = mc->max;
2705 int min = mc->min;
2706 int mask = (1 << (fls(min + max) - 1)) - 1;
2707
2708 ucontrol->value.integer.value[0] =
2709 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2710
2711 if (snd_soc_volsw_is_stereo(mc))
2712 ucontrol->value.integer.value[1] =
2713 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2714
2715 return 0;
2716}
2717EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2718
2719/**
2720 * snd_soc_put_volsw_sx - double mixer set callback
2721 * @kcontrol: mixer control
2722 * @uinfo: control element information
2723 *
2724 * Callback to set the value of a double mixer control that spans 2 registers.
2725 *
2726 * Returns 0 for success.
2727 */
2728int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2729 struct snd_ctl_elem_value *ucontrol)
2730{
2731 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2732 struct soc_mixer_control *mc =
2733 (struct soc_mixer_control *)kcontrol->private_value;
2734
2735 unsigned int reg = mc->reg;
2736 unsigned int reg2 = mc->rreg;
2737 unsigned int shift = mc->shift;
2738 unsigned int rshift = mc->rshift;
2739 int max = mc->max;
2740 int min = mc->min;
2741 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002742 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002743 unsigned short val, val_mask, val2 = 0;
2744
2745 val_mask = mask << shift;
2746 val = (ucontrol->value.integer.value[0] + min) & mask;
2747 val = val << shift;
2748
Mukund Navadad0558522012-11-09 11:53:40 +05302749 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2750 if (err < 0)
2751 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002752
2753 if (snd_soc_volsw_is_stereo(mc)) {
2754 val_mask = mask << rshift;
2755 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2756 val2 = val2 << rshift;
2757
2758 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2759 return err;
2760 }
2761 return 0;
2762}
2763EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2764
2765/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002766 * snd_soc_info_volsw_s8 - signed mixer info callback
2767 * @kcontrol: mixer control
2768 * @uinfo: control element information
2769 *
2770 * Callback to provide information about a signed mixer control.
2771 *
2772 * Returns 0 for success.
2773 */
2774int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2775 struct snd_ctl_elem_info *uinfo)
2776{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002777 struct soc_mixer_control *mc =
2778 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002779 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002780 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002781
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002782 if (!mc->platform_max)
2783 mc->platform_max = mc->max;
2784 platform_max = mc->platform_max;
2785
Mark Browne13ac2e2008-05-28 17:58:05 +01002786 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2787 uinfo->count = 2;
2788 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002789 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002790 return 0;
2791}
2792EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2793
2794/**
2795 * snd_soc_get_volsw_s8 - signed mixer get callback
2796 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002797 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002798 *
2799 * Callback to get the value of a signed mixer control.
2800 *
2801 * Returns 0 for success.
2802 */
2803int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2804 struct snd_ctl_elem_value *ucontrol)
2805{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002806 struct soc_mixer_control *mc =
2807 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002808 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002809 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002810 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002811 int val = snd_soc_read(codec, reg);
2812
2813 ucontrol->value.integer.value[0] =
2814 ((signed char)(val & 0xff))-min;
2815 ucontrol->value.integer.value[1] =
2816 ((signed char)((val >> 8) & 0xff))-min;
2817 return 0;
2818}
2819EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2820
2821/**
2822 * snd_soc_put_volsw_sgn - signed mixer put callback
2823 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002824 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002825 *
2826 * Callback to set the value of a signed mixer control.
2827 *
2828 * Returns 0 for success.
2829 */
2830int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2831 struct snd_ctl_elem_value *ucontrol)
2832{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002833 struct soc_mixer_control *mc =
2834 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002835 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002836 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002837 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002838 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002839
2840 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2841 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2842
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002843 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002844}
2845EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2846
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002847/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002848 * snd_soc_info_volsw_range - single mixer info callback with range.
2849 * @kcontrol: mixer control
2850 * @uinfo: control element information
2851 *
2852 * Callback to provide information, within a range, about a single
2853 * mixer control.
2854 *
2855 * returns 0 for success.
2856 */
2857int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2858 struct snd_ctl_elem_info *uinfo)
2859{
2860 struct soc_mixer_control *mc =
2861 (struct soc_mixer_control *)kcontrol->private_value;
2862 int platform_max;
2863 int min = mc->min;
2864
2865 if (!mc->platform_max)
2866 mc->platform_max = mc->max;
2867 platform_max = mc->platform_max;
2868
2869 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002870 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002871 uinfo->value.integer.min = 0;
2872 uinfo->value.integer.max = platform_max - min;
2873
2874 return 0;
2875}
2876EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2877
2878/**
2879 * snd_soc_put_volsw_range - single mixer put value callback with range.
2880 * @kcontrol: mixer control
2881 * @ucontrol: control element information
2882 *
2883 * Callback to set the value, within a range, for a single mixer control.
2884 *
2885 * Returns 0 for success.
2886 */
2887int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2888 struct snd_ctl_elem_value *ucontrol)
2889{
2890 struct soc_mixer_control *mc =
2891 (struct soc_mixer_control *)kcontrol->private_value;
2892 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2893 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002894 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002895 unsigned int shift = mc->shift;
2896 int min = mc->min;
2897 int max = mc->max;
2898 unsigned int mask = (1 << fls(max)) - 1;
2899 unsigned int invert = mc->invert;
2900 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002901 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002902
2903 val = ((ucontrol->value.integer.value[0] + min) & mask);
2904 if (invert)
2905 val = max - val;
2906 val_mask = mask << shift;
2907 val = val << shift;
2908
Mark Brown9bde4f02012-12-19 16:05:00 +00002909 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002910 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002911 return ret;
2912
2913 if (snd_soc_volsw_is_stereo(mc)) {
2914 val = ((ucontrol->value.integer.value[1] + min) & mask);
2915 if (invert)
2916 val = max - val;
2917 val_mask = mask << shift;
2918 val = val << shift;
2919
2920 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
2921 }
2922
2923 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002924}
2925EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2926
2927/**
2928 * snd_soc_get_volsw_range - single mixer get callback with range
2929 * @kcontrol: mixer control
2930 * @ucontrol: control element information
2931 *
2932 * Callback to get the value, within a range, of a single mixer control.
2933 *
2934 * Returns 0 for success.
2935 */
2936int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2937 struct snd_ctl_elem_value *ucontrol)
2938{
2939 struct soc_mixer_control *mc =
2940 (struct soc_mixer_control *)kcontrol->private_value;
2941 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2942 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002943 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002944 unsigned int shift = mc->shift;
2945 int min = mc->min;
2946 int max = mc->max;
2947 unsigned int mask = (1 << fls(max)) - 1;
2948 unsigned int invert = mc->invert;
2949
2950 ucontrol->value.integer.value[0] =
2951 (snd_soc_read(codec, reg) >> shift) & mask;
2952 if (invert)
2953 ucontrol->value.integer.value[0] =
2954 max - ucontrol->value.integer.value[0];
2955 ucontrol->value.integer.value[0] =
2956 ucontrol->value.integer.value[0] - min;
2957
Mark Brown9bde4f02012-12-19 16:05:00 +00002958 if (snd_soc_volsw_is_stereo(mc)) {
2959 ucontrol->value.integer.value[1] =
2960 (snd_soc_read(codec, rreg) >> shift) & mask;
2961 if (invert)
2962 ucontrol->value.integer.value[1] =
2963 max - ucontrol->value.integer.value[1];
2964 ucontrol->value.integer.value[1] =
2965 ucontrol->value.integer.value[1] - min;
2966 }
2967
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002968 return 0;
2969}
2970EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2971
2972/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002973 * snd_soc_limit_volume - Set new limit to an existing volume control.
2974 *
2975 * @codec: where to look for the control
2976 * @name: Name of the control
2977 * @max: new maximum limit
2978 *
2979 * Return 0 for success, else error.
2980 */
2981int snd_soc_limit_volume(struct snd_soc_codec *codec,
2982 const char *name, int max)
2983{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002984 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002985 struct snd_kcontrol *kctl;
2986 struct soc_mixer_control *mc;
2987 int found = 0;
2988 int ret = -EINVAL;
2989
2990 /* Sanity check for name and max */
2991 if (unlikely(!name || max <= 0))
2992 return -EINVAL;
2993
2994 list_for_each_entry(kctl, &card->controls, list) {
2995 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2996 found = 1;
2997 break;
2998 }
2999 }
3000 if (found) {
3001 mc = (struct soc_mixer_control *)kctl->private_value;
3002 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003003 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003004 ret = 0;
3005 }
3006 }
3007 return ret;
3008}
3009EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3010
Mark Brown71d08512011-10-10 18:31:26 +01003011int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3012 struct snd_ctl_elem_info *uinfo)
3013{
3014 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3015 struct soc_bytes *params = (void *)kcontrol->private_value;
3016
3017 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3018 uinfo->count = params->num_regs * codec->val_bytes;
3019
3020 return 0;
3021}
3022EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3023
3024int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3025 struct snd_ctl_elem_value *ucontrol)
3026{
3027 struct soc_bytes *params = (void *)kcontrol->private_value;
3028 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3029 int ret;
3030
3031 if (codec->using_regmap)
3032 ret = regmap_raw_read(codec->control_data, params->base,
3033 ucontrol->value.bytes.data,
3034 params->num_regs * codec->val_bytes);
3035 else
3036 ret = -EINVAL;
3037
Mark Brownf831b052012-02-17 16:20:33 -08003038 /* Hide any masked bytes to ensure consistent data reporting */
3039 if (ret == 0 && params->mask) {
3040 switch (codec->val_bytes) {
3041 case 1:
3042 ucontrol->value.bytes.data[0] &= ~params->mask;
3043 break;
3044 case 2:
3045 ((u16 *)(&ucontrol->value.bytes.data))[0]
3046 &= ~params->mask;
3047 break;
3048 case 4:
3049 ((u32 *)(&ucontrol->value.bytes.data))[0]
3050 &= ~params->mask;
3051 break;
3052 default:
3053 return -EINVAL;
3054 }
3055 }
3056
Mark Brown71d08512011-10-10 18:31:26 +01003057 return ret;
3058}
3059EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3060
3061int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3062 struct snd_ctl_elem_value *ucontrol)
3063{
3064 struct soc_bytes *params = (void *)kcontrol->private_value;
3065 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003066 int ret, len;
3067 unsigned int val;
3068 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003069
Mark Brownf831b052012-02-17 16:20:33 -08003070 if (!codec->using_regmap)
3071 return -EINVAL;
3072
Mark Brownf831b052012-02-17 16:20:33 -08003073 len = params->num_regs * codec->val_bytes;
3074
Mark Brownb5a8fe42013-01-20 21:42:22 +09003075 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3076 if (!data)
3077 return -ENOMEM;
3078
Mark Brownf831b052012-02-17 16:20:33 -08003079 /*
3080 * If we've got a mask then we need to preserve the register
3081 * bits. We shouldn't modify the incoming data so take a
3082 * copy.
3083 */
3084 if (params->mask) {
3085 ret = regmap_read(codec->control_data, params->base, &val);
3086 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003087 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003088
3089 val &= params->mask;
3090
Mark Brownf831b052012-02-17 16:20:33 -08003091 switch (codec->val_bytes) {
3092 case 1:
3093 ((u8 *)data)[0] &= ~params->mask;
3094 ((u8 *)data)[0] |= val;
3095 break;
3096 case 2:
3097 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3098 ((u16 *)data)[0] |= cpu_to_be16(val);
3099 break;
3100 case 4:
3101 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3102 ((u32 *)data)[0] |= cpu_to_be32(val);
3103 break;
3104 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003105 ret = -EINVAL;
3106 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003107 }
3108 }
3109
3110 ret = regmap_raw_write(codec->control_data, params->base,
3111 data, len);
3112
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003113out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003114 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003115
3116 return ret;
3117}
3118EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3119
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003120/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003121 * snd_soc_info_xr_sx - signed multi register info callback
3122 * @kcontrol: mreg control
3123 * @uinfo: control element information
3124 *
3125 * Callback to provide information of a control that can
3126 * span multiple codec registers which together
3127 * forms a single signed value in a MSB/LSB manner.
3128 *
3129 * Returns 0 for success.
3130 */
3131int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3132 struct snd_ctl_elem_info *uinfo)
3133{
3134 struct soc_mreg_control *mc =
3135 (struct soc_mreg_control *)kcontrol->private_value;
3136 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3137 uinfo->count = 1;
3138 uinfo->value.integer.min = mc->min;
3139 uinfo->value.integer.max = mc->max;
3140
3141 return 0;
3142}
3143EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3144
3145/**
3146 * snd_soc_get_xr_sx - signed multi register get callback
3147 * @kcontrol: mreg control
3148 * @ucontrol: control element information
3149 *
3150 * Callback to get the value of a control that can span
3151 * multiple codec registers which together forms a single
3152 * signed value in a MSB/LSB manner. The control supports
3153 * specifying total no of bits used to allow for bitfields
3154 * across the multiple codec registers.
3155 *
3156 * Returns 0 for success.
3157 */
3158int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3159 struct snd_ctl_elem_value *ucontrol)
3160{
3161 struct soc_mreg_control *mc =
3162 (struct soc_mreg_control *)kcontrol->private_value;
3163 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3164 unsigned int regbase = mc->regbase;
3165 unsigned int regcount = mc->regcount;
3166 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3167 unsigned int regwmask = (1<<regwshift)-1;
3168 unsigned int invert = mc->invert;
3169 unsigned long mask = (1UL<<mc->nbits)-1;
3170 long min = mc->min;
3171 long max = mc->max;
3172 long val = 0;
3173 unsigned long regval;
3174 unsigned int i;
3175
3176 for (i = 0; i < regcount; i++) {
3177 regval = snd_soc_read(codec, regbase+i) & regwmask;
3178 val |= regval << (regwshift*(regcount-i-1));
3179 }
3180 val &= mask;
3181 if (min < 0 && val > max)
3182 val |= ~mask;
3183 if (invert)
3184 val = max - val;
3185 ucontrol->value.integer.value[0] = val;
3186
3187 return 0;
3188}
3189EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3190
3191/**
3192 * snd_soc_put_xr_sx - signed multi register get callback
3193 * @kcontrol: mreg control
3194 * @ucontrol: control element information
3195 *
3196 * Callback to set the value of a control that can span
3197 * multiple codec registers which together forms a single
3198 * signed value in a MSB/LSB manner. The control supports
3199 * specifying total no of bits used to allow for bitfields
3200 * across the multiple codec registers.
3201 *
3202 * Returns 0 for success.
3203 */
3204int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3205 struct snd_ctl_elem_value *ucontrol)
3206{
3207 struct soc_mreg_control *mc =
3208 (struct soc_mreg_control *)kcontrol->private_value;
3209 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3210 unsigned int regbase = mc->regbase;
3211 unsigned int regcount = mc->regcount;
3212 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3213 unsigned int regwmask = (1<<regwshift)-1;
3214 unsigned int invert = mc->invert;
3215 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003216 long max = mc->max;
3217 long val = ucontrol->value.integer.value[0];
3218 unsigned int i, regval, regmask;
3219 int err;
3220
3221 if (invert)
3222 val = max - val;
3223 val &= mask;
3224 for (i = 0; i < regcount; i++) {
3225 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3226 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3227 err = snd_soc_update_bits_locked(codec, regbase+i,
3228 regmask, regval);
3229 if (err < 0)
3230 return err;
3231 }
3232
3233 return 0;
3234}
3235EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3236
3237/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003238 * snd_soc_get_strobe - strobe get callback
3239 * @kcontrol: mixer control
3240 * @ucontrol: control element information
3241 *
3242 * Callback get the value of a strobe mixer control.
3243 *
3244 * Returns 0 for success.
3245 */
3246int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3247 struct snd_ctl_elem_value *ucontrol)
3248{
3249 struct soc_mixer_control *mc =
3250 (struct soc_mixer_control *)kcontrol->private_value;
3251 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3252 unsigned int reg = mc->reg;
3253 unsigned int shift = mc->shift;
3254 unsigned int mask = 1 << shift;
3255 unsigned int invert = mc->invert != 0;
3256 unsigned int val = snd_soc_read(codec, reg) & mask;
3257
3258 if (shift != 0 && val != 0)
3259 val = val >> shift;
3260 ucontrol->value.enumerated.item[0] = val ^ invert;
3261
3262 return 0;
3263}
3264EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3265
3266/**
3267 * snd_soc_put_strobe - strobe put callback
3268 * @kcontrol: mixer control
3269 * @ucontrol: control element information
3270 *
3271 * Callback strobe a register bit to high then low (or the inverse)
3272 * in one pass of a single mixer enum control.
3273 *
3274 * Returns 1 for success.
3275 */
3276int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3277 struct snd_ctl_elem_value *ucontrol)
3278{
3279 struct soc_mixer_control *mc =
3280 (struct soc_mixer_control *)kcontrol->private_value;
3281 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3282 unsigned int reg = mc->reg;
3283 unsigned int shift = mc->shift;
3284 unsigned int mask = 1 << shift;
3285 unsigned int invert = mc->invert != 0;
3286 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3287 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3288 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3289 int err;
3290
3291 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3292 if (err < 0)
3293 return err;
3294
3295 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3296 return err;
3297}
3298EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3299
3300/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003301 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3302 * @dai: DAI
3303 * @clk_id: DAI specific clock ID
3304 * @freq: new clock frequency in Hz
3305 * @dir: new clock direction - input/output.
3306 *
3307 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3308 */
3309int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3310 unsigned int freq, int dir)
3311{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003312 if (dai->driver && dai->driver->ops->set_sysclk)
3313 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003314 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003315 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003316 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003317 else
3318 return -EINVAL;
3319}
3320EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3321
3322/**
Mark Brownec4ee522011-03-07 20:58:11 +00003323 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3324 * @codec: CODEC
3325 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003326 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003327 * @freq: new clock frequency in Hz
3328 * @dir: new clock direction - input/output.
3329 *
3330 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3331 */
3332int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003333 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003334{
3335 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003336 return codec->driver->set_sysclk(codec, clk_id, source,
3337 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003338 else
3339 return -EINVAL;
3340}
3341EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3342
3343/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003344 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3345 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003346 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003347 * @div: new clock divisor.
3348 *
3349 * Configures the clock dividers. This is used to derive the best DAI bit and
3350 * frame clocks from the system or master clock. It's best to set the DAI bit
3351 * and frame clocks as low as possible to save system power.
3352 */
3353int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3354 int div_id, int div)
3355{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003356 if (dai->driver && dai->driver->ops->set_clkdiv)
3357 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003358 else
3359 return -EINVAL;
3360}
3361EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3362
3363/**
3364 * snd_soc_dai_set_pll - configure DAI PLL.
3365 * @dai: DAI
3366 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003367 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003368 * @freq_in: PLL input clock frequency in Hz
3369 * @freq_out: requested PLL output clock frequency in Hz
3370 *
3371 * Configures and enables PLL to generate output clock based on input clock.
3372 */
Mark Brown85488032009-09-05 18:52:16 +01003373int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3374 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003375{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003376 if (dai->driver && dai->driver->ops->set_pll)
3377 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003378 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003379 else if (dai->codec && dai->codec->driver->set_pll)
3380 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3381 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003382 else
3383 return -EINVAL;
3384}
3385EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3386
Mark Brownec4ee522011-03-07 20:58:11 +00003387/*
3388 * snd_soc_codec_set_pll - configure codec PLL.
3389 * @codec: CODEC
3390 * @pll_id: DAI specific PLL ID
3391 * @source: DAI specific source for the PLL
3392 * @freq_in: PLL input clock frequency in Hz
3393 * @freq_out: requested PLL output clock frequency in Hz
3394 *
3395 * Configures and enables PLL to generate output clock based on input clock.
3396 */
3397int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3398 unsigned int freq_in, unsigned int freq_out)
3399{
3400 if (codec->driver->set_pll)
3401 return codec->driver->set_pll(codec, pll_id, source,
3402 freq_in, freq_out);
3403 else
3404 return -EINVAL;
3405}
3406EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3407
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003408/**
3409 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3410 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003411 * @fmt: SND_SOC_DAIFMT_ format value.
3412 *
3413 * Configures the DAI hardware format and clocking.
3414 */
3415int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3416{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003417 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003418 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003419 if (dai->driver->ops->set_fmt == NULL)
3420 return -ENOTSUPP;
3421 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003422}
3423EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3424
3425/**
3426 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3427 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003428 * @tx_mask: bitmask representing active TX slots.
3429 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003430 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003431 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003432 *
3433 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3434 * specific.
3435 */
3436int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003437 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003438{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003439 if (dai->driver && dai->driver->ops->set_tdm_slot)
3440 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003441 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003442 else
3443 return -EINVAL;
3444}
3445EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3446
3447/**
Barry Song472df3c2009-09-12 01:16:29 +08003448 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3449 * @dai: DAI
3450 * @tx_num: how many TX channels
3451 * @tx_slot: pointer to an array which imply the TX slot number channel
3452 * 0~num-1 uses
3453 * @rx_num: how many RX channels
3454 * @rx_slot: pointer to an array which imply the RX slot number channel
3455 * 0~num-1 uses
3456 *
3457 * configure the relationship between channel number and TDM slot number.
3458 */
3459int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3460 unsigned int tx_num, unsigned int *tx_slot,
3461 unsigned int rx_num, unsigned int *rx_slot)
3462{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003463 if (dai->driver && dai->driver->ops->set_channel_map)
3464 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003465 rx_num, rx_slot);
3466 else
3467 return -EINVAL;
3468}
3469EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3470
3471/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003472 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3473 * @dai: DAI
3474 * @tristate: tristate enable
3475 *
3476 * Tristates the DAI so that others can use it.
3477 */
3478int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3479{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003480 if (dai->driver && dai->driver->ops->set_tristate)
3481 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003482 else
3483 return -EINVAL;
3484}
3485EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3486
3487/**
3488 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3489 * @dai: DAI
3490 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003491 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003492 *
3493 * Mutes the DAI DAC.
3494 */
Mark Brownda183962013-02-06 15:44:07 +00003495int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3496 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003497{
Mark Brownda183962013-02-06 15:44:07 +00003498 if (!dai->driver)
3499 return -ENOTSUPP;
3500
3501 if (dai->driver->ops->mute_stream)
3502 return dai->driver->ops->mute_stream(dai, mute, direction);
3503 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3504 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003505 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003506 else
Mark Brown04570c62012-04-13 19:16:03 +01003507 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003508}
3509EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3510
Mark Brownc5af3a22008-11-28 13:29:45 +00003511/**
3512 * snd_soc_register_card - Register a card with the ASoC core
3513 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003514 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003515 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003516 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303517int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003518{
Mark Brownb19e6e72012-03-14 21:18:39 +00003519 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003520
Mark Brownc5af3a22008-11-28 13:29:45 +00003521 if (!card->name || !card->dev)
3522 return -EINVAL;
3523
Stephen Warren5a504962011-12-21 10:40:59 -07003524 for (i = 0; i < card->num_links; i++) {
3525 struct snd_soc_dai_link *link = &card->dai_link[i];
3526
3527 /*
3528 * Codec must be specified by 1 of name or OF node,
3529 * not both or neither.
3530 */
3531 if (!!link->codec_name == !!link->codec_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003532 dev_err(card->dev,
3533 "ASoC: Neither/both codec name/of_node are set for %s\n",
3534 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003535 return -EINVAL;
3536 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003537 /* Codec DAI name must be specified */
3538 if (!link->codec_dai_name) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003539 dev_err(card->dev,
3540 "ASoC: codec_dai_name not set for %s\n",
3541 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003542 return -EINVAL;
3543 }
Stephen Warren5a504962011-12-21 10:40:59 -07003544
3545 /*
3546 * Platform may be specified by either name or OF node, but
3547 * can be left unspecified, and a dummy platform will be used.
3548 */
3549 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003550 dev_err(card->dev,
3551 "ASoC: Both platform name/of_node are set for %s\n",
3552 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003553 return -EINVAL;
3554 }
3555
3556 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003557 * CPU device may be specified by either name or OF node, but
3558 * can be left unspecified, and will be matched based on DAI
3559 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003560 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003561 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003562 dev_err(card->dev,
3563 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3564 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003565 return -EINVAL;
3566 }
3567 /*
3568 * At least one of CPU DAI name or CPU device name/node must be
3569 * specified
3570 */
3571 if (!link->cpu_dai_name &&
3572 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003573 dev_err(card->dev,
3574 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3575 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003576 return -EINVAL;
3577 }
3578 }
3579
Mark Browned77cc12011-05-03 18:25:34 +01003580 dev_set_drvdata(card->dev, card);
3581
Stephen Warren111c6412011-01-28 14:26:35 -07003582 snd_soc_initialize_card_lists(card);
3583
Vinod Koul150dd2f2011-01-13 22:48:32 +05303584 soc_init_card_debugfs(card);
3585
Mark Brown181a6892012-03-14 20:18:49 +00003586 card->rtd = devm_kzalloc(card->dev,
3587 sizeof(struct snd_soc_pcm_runtime) *
3588 (card->num_links + card->num_aux_devs),
3589 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003590 if (card->rtd == NULL)
3591 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003592 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003593 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003594
3595 for (i = 0; i < card->num_links; i++)
3596 card->rtd[i].dai_link = &card->dai_link[i];
3597
Mark Brownc5af3a22008-11-28 13:29:45 +00003598 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003599 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003600 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003601 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003602 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003603
Mark Brownb19e6e72012-03-14 21:18:39 +00003604 ret = snd_soc_instantiate_card(card);
3605 if (ret != 0)
3606 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003607
Mark Brownb19e6e72012-03-14 21:18:39 +00003608 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003609}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303610EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003611
3612/**
3613 * snd_soc_unregister_card - Unregister a card with the ASoC core
3614 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003615 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003616 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003617 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303618int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003619{
Vinod Koulb0e26482011-01-13 22:48:02 +05303620 if (card->instantiated)
3621 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003622 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003623
3624 return 0;
3625}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303626EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003627
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003628/*
3629 * Simplify DAI link configuration by removing ".-1" from device names
3630 * and sanitizing names.
3631 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003632static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003633{
3634 char *found, name[NAME_SIZE];
3635 int id1, id2;
3636
3637 if (dev_name(dev) == NULL)
3638 return NULL;
3639
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003640 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003641
3642 /* are we a "%s.%d" name (platform and SPI components) */
3643 found = strstr(name, dev->driver->name);
3644 if (found) {
3645 /* get ID */
3646 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3647
3648 /* discard ID from name if ID == -1 */
3649 if (*id == -1)
3650 found[strlen(dev->driver->name)] = '\0';
3651 }
3652
3653 } else {
3654 /* I2C component devices are named "bus-addr" */
3655 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3656 char tmp[NAME_SIZE];
3657
3658 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003659 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003660
3661 /* sanitize component name for DAI link creation */
3662 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003663 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003664 } else
3665 *id = 0;
3666 }
3667
3668 return kstrdup(name, GFP_KERNEL);
3669}
3670
3671/*
3672 * Simplify DAI link naming for single devices with multiple DAIs by removing
3673 * any ".-1" and using the DAI name (instead of device name).
3674 */
3675static inline char *fmt_multiple_name(struct device *dev,
3676 struct snd_soc_dai_driver *dai_drv)
3677{
3678 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003679 dev_err(dev,
3680 "ASoC: error - multiple DAI %s registered with no name\n",
3681 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003682 return NULL;
3683 }
3684
3685 return kstrdup(dai_drv->name, GFP_KERNEL);
3686}
3687
Mark Brown91151712008-11-30 23:31:24 +00003688/**
3689 * snd_soc_register_dai - Register a DAI with the ASoC core
3690 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003691 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003692 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003693static int snd_soc_register_dai(struct device *dev,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003694 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003695{
Mark Brown054880f2012-04-09 17:29:19 +01003696 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003697 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003698
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003699 dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003700
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003701 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3702 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003703 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003704
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003705 /* create DAI component name */
3706 dai->name = fmt_single_name(dev, &dai->id);
3707 if (dai->name == NULL) {
3708 kfree(dai);
3709 return -ENOMEM;
3710 }
3711
3712 dai->dev = dev;
3713 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003714 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003715 if (!dai->driver->ops)
3716 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003717
3718 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003719
3720 list_for_each_entry(codec, &codec_list, list) {
3721 if (codec->dev == dev) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003722 dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
Mark Brown054880f2012-04-09 17:29:19 +01003723 dai->name, codec->name);
3724 dai->codec = codec;
3725 break;
3726 }
3727 }
3728
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003729 if (!dai->codec)
3730 dai->dapm.idle_bias_off = 1;
3731
Mark Brown91151712008-11-30 23:31:24 +00003732 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003733
Mark Brown91151712008-11-30 23:31:24 +00003734 mutex_unlock(&client_mutex);
3735
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003736 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003737
3738 return 0;
3739}
Mark Brown91151712008-11-30 23:31:24 +00003740
3741/**
3742 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3743 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003744 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003745 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003746static void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003747{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003748 struct snd_soc_dai *dai;
3749
3750 list_for_each_entry(dai, &dai_list, list) {
3751 if (dev == dai->dev)
3752 goto found;
3753 }
3754 return;
3755
3756found:
Mark Brown91151712008-11-30 23:31:24 +00003757 mutex_lock(&client_mutex);
3758 list_del(&dai->list);
3759 mutex_unlock(&client_mutex);
3760
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003761 dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003762 kfree(dai->name);
3763 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003764}
Mark Brown91151712008-11-30 23:31:24 +00003765
3766/**
3767 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3768 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003769 * @dai: Array of DAIs to register
3770 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003771 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003772static int snd_soc_register_dais(struct device *dev,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003773 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003774{
Mark Brown054880f2012-04-09 17:29:19 +01003775 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003776 struct snd_soc_dai *dai;
3777 int i, ret = 0;
3778
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003779 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003780
3781 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003782
3783 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003784 if (dai == NULL) {
3785 ret = -ENOMEM;
3786 goto err;
3787 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003788
3789 /* create DAI component name */
3790 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3791 if (dai->name == NULL) {
3792 kfree(dai);
3793 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003794 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003795 }
3796
3797 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003798 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003799 if (dai->driver->id)
3800 dai->id = dai->driver->id;
3801 else
3802 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003803 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003804 if (!dai->driver->ops)
3805 dai->driver->ops = &null_dai_ops;
3806
3807 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003808
3809 list_for_each_entry(codec, &codec_list, list) {
3810 if (codec->dev == dev) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003811 dev_dbg(dev,
3812 "ASoC: Mapped DAI %s to CODEC %s\n",
3813 dai->name, codec->name);
Mark Brown054880f2012-04-09 17:29:19 +01003814 dai->codec = codec;
3815 break;
3816 }
3817 }
3818
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003819 if (!dai->codec)
3820 dai->dapm.idle_bias_off = 1;
3821
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003822 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003823
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003824 mutex_unlock(&client_mutex);
3825
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003826 dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003827 }
3828
3829 return 0;
3830
3831err:
3832 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003833 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003834
3835 return ret;
3836}
Mark Brown91151712008-11-30 23:31:24 +00003837
3838/**
3839 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3840 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003841 * @dai: Array of DAIs to unregister
3842 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003843 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003844static void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003845{
3846 int i;
3847
3848 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003849 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003850}
Mark Brown91151712008-11-30 23:31:24 +00003851
Mark Brown12a48a8c2008-12-03 19:40:30 +00003852/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003853 * snd_soc_add_platform - Add a platform to the ASoC core
3854 * @dev: The parent device for the platform
3855 * @platform: The platform to add
3856 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00003857 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003858int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57db82013-03-27 12:02:22 +01003859 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003860{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003861 /* create platform component name */
3862 platform->name = fmt_single_name(dev, &platform->id);
Dan Carpenterb5c745f2013-07-22 09:56:54 +03003863 if (platform->name == NULL)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003864 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003865
3866 platform->dev = dev;
3867 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003868 platform->dapm.dev = dev;
3869 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003870 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003871 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003872
3873 mutex_lock(&client_mutex);
3874 list_add(&platform->list, &platform_list);
3875 mutex_unlock(&client_mutex);
3876
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003877 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003878
3879 return 0;
3880}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003881EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3882
3883/**
3884 * snd_soc_register_platform - Register a platform with the ASoC core
3885 *
3886 * @platform: platform to register
3887 */
3888int snd_soc_register_platform(struct device *dev,
3889 const struct snd_soc_platform_driver *platform_drv)
3890{
3891 struct snd_soc_platform *platform;
3892 int ret;
3893
3894 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3895
3896 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3897 if (platform == NULL)
3898 return -ENOMEM;
3899
3900 ret = snd_soc_add_platform(dev, platform, platform_drv);
3901 if (ret)
3902 kfree(platform);
3903
3904 return ret;
3905}
Mark Brown12a48a8c2008-12-03 19:40:30 +00003906EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3907
3908/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003909 * snd_soc_remove_platform - Remove a platform from the ASoC core
3910 * @platform: the platform to remove
3911 */
3912void snd_soc_remove_platform(struct snd_soc_platform *platform)
3913{
3914 mutex_lock(&client_mutex);
3915 list_del(&platform->list);
3916 mutex_unlock(&client_mutex);
3917
3918 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3919 platform->name);
3920 kfree(platform->name);
3921}
3922EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3923
3924struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3925{
3926 struct snd_soc_platform *platform;
3927
3928 list_for_each_entry(platform, &platform_list, list) {
3929 if (dev == platform->dev)
3930 return platform;
3931 }
3932
3933 return NULL;
3934}
3935EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3936
3937/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00003938 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3939 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003940 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003941 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003942void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003943{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003944 struct snd_soc_platform *platform;
3945
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003946 platform = snd_soc_lookup_platform(dev);
3947 if (!platform)
3948 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003949
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003950 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003951 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003952}
3953EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3954
Mark Brown151ab222009-05-09 16:22:58 +01003955static u64 codec_format_map[] = {
3956 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3957 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3958 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3959 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3960 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3961 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3962 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3963 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3964 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3965 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3966 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3967 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3968 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3969 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3970 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3971 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3972};
3973
3974/* Fix up the DAI formats for endianness: codecs don't actually see
3975 * the endianness of the data but we're using the CPU format
3976 * definitions which do need to include endianness so we ensure that
3977 * codec DAIs always have both big and little endian variants set.
3978 */
3979static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3980{
3981 int i;
3982
3983 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3984 if (stream->formats & codec_format_map[i])
3985 stream->formats |= codec_format_map[i];
3986}
3987
Mark Brown0d0cf002008-12-10 14:32:45 +00003988/**
3989 * snd_soc_register_codec - Register a codec with the ASoC core
3990 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003991 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003992 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003993int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003994 const struct snd_soc_codec_driver *codec_drv,
3995 struct snd_soc_dai_driver *dai_drv,
3996 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003997{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003998 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003999 struct snd_soc_codec *codec;
4000 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004001
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004002 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004003
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004004 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4005 if (codec == NULL)
4006 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004007
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004008 /* create CODEC component name */
4009 codec->name = fmt_single_name(dev, &codec->id);
4010 if (codec->name == NULL) {
Kuninori Morimotof790b942013-02-25 00:40:09 -08004011 ret = -ENOMEM;
4012 goto fail_codec;
Mark Brown151ab222009-05-09 16:22:58 +01004013 }
4014
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00004015 if (codec_drv->compress_type)
4016 codec->compress_type = codec_drv->compress_type;
4017 else
4018 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
4019
Mark Brownc3acec22010-12-02 16:15:29 +00004020 codec->write = codec_drv->write;
4021 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004022 codec->volatile_register = codec_drv->volatile_register;
4023 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004024 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00004025 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004026 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4027 codec->dapm.dev = dev;
4028 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004029 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004030 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004031 codec->dev = dev;
4032 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004033 codec->num_dai = num_dai;
4034 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004035
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004036 /* allocate CODEC register cache */
4037 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004038 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00004039 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004040 /* it is necessary to make a copy of the default register cache
4041 * because in the case of using a compression type that requires
Bill Pembertonf6e65742012-11-19 13:25:33 -05004042 * the default register cache to be marked as the
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004043 * kernel might have freed the array by the time we initialize
4044 * the cache.
4045 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004046 if (codec_drv->reg_cache_default) {
4047 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4048 reg_size, GFP_KERNEL);
4049 if (!codec->reg_def_copy) {
4050 ret = -ENOMEM;
Kuninori Morimotof790b942013-02-25 00:40:09 -08004051 goto fail_codec_name;
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004052 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004053 }
4054 }
4055
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004056 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4057 if (!codec->volatile_register)
4058 codec->volatile_register = snd_soc_default_volatile_register;
4059 if (!codec->readable_register)
4060 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004061 if (!codec->writable_register)
4062 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004063 }
4064
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004065 for (i = 0; i < num_dai; i++) {
4066 fixup_codec_formats(&dai_drv[i].playback);
4067 fixup_codec_formats(&dai_drv[i].capture);
4068 }
4069
Mark Brown054880f2012-04-09 17:29:19 +01004070 mutex_lock(&client_mutex);
4071 list_add(&codec->list, &codec_list);
4072 mutex_unlock(&client_mutex);
4073
Mark Brown26b01cc2010-08-18 20:20:55 +01004074 /* register any DAIs */
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004075 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4076 if (ret < 0) {
4077 dev_err(codec->dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4078 goto fail_codec_name;
Mark Brown26b01cc2010-08-18 20:20:55 +01004079 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004080
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004081 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004082 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004083
Kuninori Morimotof790b942013-02-25 00:40:09 -08004084fail_codec_name:
Kuninori Morimotoe1328a82013-03-07 17:42:33 -08004085 mutex_lock(&client_mutex);
4086 list_del(&codec->list);
4087 mutex_unlock(&client_mutex);
4088
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004089 kfree(codec->name);
Kuninori Morimotof790b942013-02-25 00:40:09 -08004090fail_codec:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004091 kfree(codec);
4092 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004093}
4094EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4095
4096/**
4097 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4098 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004099 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004100 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004101void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004102{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004103 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004104
4105 list_for_each_entry(codec, &codec_list, list) {
4106 if (dev == codec->dev)
4107 goto found;
4108 }
4109 return;
4110
4111found:
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004112 snd_soc_unregister_dais(dev, codec->num_dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004113
Mark Brown0d0cf002008-12-10 14:32:45 +00004114 mutex_lock(&client_mutex);
4115 list_del(&codec->list);
4116 mutex_unlock(&client_mutex);
4117
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004118 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004119
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004120 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004121 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004122 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004123 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004124}
4125EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4126
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004127
4128/**
4129 * snd_soc_register_component - Register a component with the ASoC core
4130 *
4131 */
4132int snd_soc_register_component(struct device *dev,
4133 const struct snd_soc_component_driver *cmpnt_drv,
4134 struct snd_soc_dai_driver *dai_drv,
4135 int num_dai)
4136{
4137 struct snd_soc_component *cmpnt;
4138 int ret;
4139
4140 dev_dbg(dev, "component register %s\n", dev_name(dev));
4141
4142 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4143 if (!cmpnt) {
4144 dev_err(dev, "ASoC: Failed to allocate memory\n");
4145 return -ENOMEM;
4146 }
4147
4148 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4149 if (!cmpnt->name) {
4150 dev_err(dev, "ASoC: Failed to simplifying name\n");
4151 return -ENOMEM;
4152 }
4153
4154 cmpnt->dev = dev;
4155 cmpnt->driver = cmpnt_drv;
4156 cmpnt->num_dai = num_dai;
4157
Kuninori Morimotoa1422b82013-03-21 03:27:13 -07004158 /*
4159 * snd_soc_register_dai() uses fmt_single_name(), and
4160 * snd_soc_register_dais() uses fmt_multiple_name()
4161 * for dai->name which is used for name based matching
4162 */
4163 if (1 == num_dai)
4164 ret = snd_soc_register_dai(dev, dai_drv);
4165 else
4166 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004167 if (ret < 0) {
4168 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4169 goto error_component_name;
4170 }
4171
4172 mutex_lock(&client_mutex);
4173 list_add(&cmpnt->list, &component_list);
4174 mutex_unlock(&client_mutex);
4175
4176 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4177
4178 return ret;
4179
4180error_component_name:
4181 kfree(cmpnt->name);
4182
4183 return ret;
4184}
Stephen Warren2e1cc192013-03-26 16:38:19 -06004185EXPORT_SYMBOL_GPL(snd_soc_register_component);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004186
4187/**
4188 * snd_soc_unregister_component - Unregister a component from the ASoC core
4189 *
4190 */
4191void snd_soc_unregister_component(struct device *dev)
4192{
4193 struct snd_soc_component *cmpnt;
4194
4195 list_for_each_entry(cmpnt, &component_list, list) {
4196 if (dev == cmpnt->dev)
4197 goto found;
4198 }
4199 return;
4200
4201found:
4202 snd_soc_unregister_dais(dev, cmpnt->num_dai);
4203
4204 mutex_lock(&client_mutex);
4205 list_del(&cmpnt->list);
4206 mutex_unlock(&client_mutex);
4207
4208 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4209 kfree(cmpnt->name);
4210}
Stephen Warren2e1cc192013-03-26 16:38:19 -06004211EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004212
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004213/* Retrieve a card's name from device tree */
4214int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4215 const char *propname)
4216{
4217 struct device_node *np = card->dev->of_node;
4218 int ret;
4219
4220 ret = of_property_read_string_index(np, propname, 0, &card->name);
4221 /*
4222 * EINVAL means the property does not exist. This is fine providing
4223 * card->name was previously set, which is checked later in
4224 * snd_soc_register_card.
4225 */
4226 if (ret < 0 && ret != -EINVAL) {
4227 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004228 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004229 propname, ret);
4230 return ret;
4231 }
4232
4233 return 0;
4234}
4235EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4236
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004237int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4238 const char *propname)
4239{
4240 struct device_node *np = card->dev->of_node;
4241 int num_routes;
4242 struct snd_soc_dapm_route *routes;
4243 int i, ret;
4244
4245 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004246 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004247 dev_err(card->dev,
4248 "ASoC: Property '%s' does not exist or its length is not even\n",
4249 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004250 return -EINVAL;
4251 }
4252 num_routes /= 2;
4253 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004254 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004255 propname);
4256 return -EINVAL;
4257 }
4258
4259 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4260 GFP_KERNEL);
4261 if (!routes) {
4262 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004263 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004264 return -EINVAL;
4265 }
4266
4267 for (i = 0; i < num_routes; i++) {
4268 ret = of_property_read_string_index(np, propname,
4269 2 * i, &routes[i].sink);
4270 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004271 dev_err(card->dev,
4272 "ASoC: Property '%s' index %d could not be read: %d\n",
4273 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004274 return -EINVAL;
4275 }
4276 ret = of_property_read_string_index(np, propname,
4277 (2 * i) + 1, &routes[i].source);
4278 if (ret) {
4279 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004280 "ASoC: Property '%s' index %d could not be read: %d\n",
4281 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004282 return -EINVAL;
4283 }
4284 }
4285
4286 card->num_dapm_routes = num_routes;
4287 card->dapm_routes = routes;
4288
4289 return 0;
4290}
4291EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4292
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004293unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4294 const char *prefix)
4295{
4296 int ret, i;
4297 char prop[128];
4298 unsigned int format = 0;
4299 int bit, frame;
4300 const char *str;
4301 struct {
4302 char *name;
4303 unsigned int val;
4304 } of_fmt_table[] = {
4305 { "i2s", SND_SOC_DAIFMT_I2S },
4306 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4307 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4308 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4309 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4310 { "ac97", SND_SOC_DAIFMT_AC97 },
4311 { "pdm", SND_SOC_DAIFMT_PDM},
4312 { "msb", SND_SOC_DAIFMT_MSB },
4313 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004314 };
4315
4316 if (!prefix)
4317 prefix = "";
4318
4319 /*
4320 * check "[prefix]format = xxx"
4321 * SND_SOC_DAIFMT_FORMAT_MASK area
4322 */
4323 snprintf(prop, sizeof(prop), "%sformat", prefix);
4324 ret = of_property_read_string(np, prop, &str);
4325 if (ret == 0) {
4326 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4327 if (strcmp(str, of_fmt_table[i].name) == 0) {
4328 format |= of_fmt_table[i].val;
4329 break;
4330 }
4331 }
4332 }
4333
4334 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004335 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004336 * SND_SOC_DAIFMT_CLOCK_MASK area
4337 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004338 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4339 if (of_get_property(np, prop, NULL))
4340 format |= SND_SOC_DAIFMT_CONT;
4341 else
4342 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004343
4344 /*
4345 * check "[prefix]bitclock-inversion"
4346 * check "[prefix]frame-inversion"
4347 * SND_SOC_DAIFMT_INV_MASK area
4348 */
4349 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4350 bit = !!of_get_property(np, prop, NULL);
4351
4352 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4353 frame = !!of_get_property(np, prop, NULL);
4354
4355 switch ((bit << 4) + frame) {
4356 case 0x11:
4357 format |= SND_SOC_DAIFMT_IB_IF;
4358 break;
4359 case 0x10:
4360 format |= SND_SOC_DAIFMT_IB_NF;
4361 break;
4362 case 0x01:
4363 format |= SND_SOC_DAIFMT_NB_IF;
4364 break;
4365 default:
4366 /* SND_SOC_DAIFMT_NB_NF is default */
4367 break;
4368 }
4369
4370 /*
4371 * check "[prefix]bitclock-master"
4372 * check "[prefix]frame-master"
4373 * SND_SOC_DAIFMT_MASTER_MASK area
4374 */
4375 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4376 bit = !!of_get_property(np, prop, NULL);
4377
4378 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4379 frame = !!of_get_property(np, prop, NULL);
4380
4381 switch ((bit << 4) + frame) {
4382 case 0x11:
4383 format |= SND_SOC_DAIFMT_CBM_CFM;
4384 break;
4385 case 0x10:
4386 format |= SND_SOC_DAIFMT_CBM_CFS;
4387 break;
4388 case 0x01:
4389 format |= SND_SOC_DAIFMT_CBS_CFM;
4390 break;
4391 default:
4392 format |= SND_SOC_DAIFMT_CBS_CFS;
4393 break;
4394 }
4395
4396 return format;
4397}
4398EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4399
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004400static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004401{
Mark Brown384c89e2008-12-03 17:34:03 +00004402#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004403 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4404 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004405 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004406 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004407 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004408
Mark Brown8a9dab12011-01-10 22:25:21 +00004409 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004410 &codec_list_fops))
4411 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4412
Mark Brown8a9dab12011-01-10 22:25:21 +00004413 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004414 &dai_list_fops))
4415 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004416
Mark Brown8a9dab12011-01-10 22:25:21 +00004417 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004418 &platform_list_fops))
4419 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004420#endif
4421
Mark Brownfb257892011-04-28 10:57:54 +01004422 snd_soc_util_init();
4423
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004424 return platform_driver_register(&soc_driver);
4425}
Mark Brown4abe8e12010-10-12 17:41:03 +01004426module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004427
Mark Brown7d8c16a2008-11-30 22:11:24 +00004428static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004429{
Mark Brownfb257892011-04-28 10:57:54 +01004430 snd_soc_util_exit();
4431
Mark Brown384c89e2008-12-03 17:34:03 +00004432#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004433 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004434#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004435 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004436}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004437module_exit(snd_soc_exit);
4438
4439/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004440MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004441MODULE_DESCRIPTION("ALSA SoC Core");
4442MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004443MODULE_ALIAS("platform:soc-audio");