blob: 72d4a2b009faaa3119582097ead72990469b5bbc [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>
Markus Pargmann741a5092013-08-19 17:05:55 +020033#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010034#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070036#include <linux/of.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020037#include <linux/gpio.h>
38#include <linux/of_gpio.h>
Marek Vasut474828a2009-07-22 13:01:03 +020039#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000041#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/pcm.h>
43#include <sound/pcm_params.h>
44#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010045#include <sound/soc-dpcm.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020046#include <sound/initval.h>
47
Mark Browna8b1d342010-11-03 18:05:58 -040048#define CREATE_TRACE_POINTS
49#include <trace/events/asoc.h>
50
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000051#define NAME_SIZE 32
52
Mark Brown384c89e2008-12-03 17:34:03 +000053#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000054struct dentry *snd_soc_debugfs_root;
55EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000056#endif
57
Mark Brownc5af3a22008-11-28 13:29:45 +000058static DEFINE_MUTEX(client_mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070061static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000062
Frank Mandarinodb2a4162006-10-06 18:31:09 +020063/*
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68static int pmdown_time = 5000;
69module_param(pmdown_time, int, 0);
70MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
Markus Pargmann741a5092013-08-19 17:05:55 +020072struct snd_ac97_reset_cfg {
73 struct pinctrl *pctl;
74 struct pinctrl_state *pstate_reset;
75 struct pinctrl_state *pstate_warm_reset;
76 struct pinctrl_state *pstate_run;
77 int gpio_sdata;
78 int gpio_sync;
79 int gpio_reset;
80};
81
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000082/* returns the minimum number of bytes needed to represent
83 * a particular given value */
84static int min_bytes_needed(unsigned long val)
85{
86 int c = 0;
87 int i;
88
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
90 if (val & (1UL << i))
91 break;
92 c = (sizeof val * 8) - c;
93 if (!c || (c % 8))
94 c = (c + 8) / 8;
95 else
96 c /= 8;
97 return c;
98}
99
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000100/* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +0000104{
Stephen Warren00b317a2011-04-01 14:50:44 -0600105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000107 int ret;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
110
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
113 WARN_ON(len > 63);
114
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
117 return -EINVAL;
118
Mark Brown25032c12011-08-01 13:52:48 +0900119 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000120 if (ret < 0) {
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
123 } else {
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
125 }
126
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
131
132 return 0;
133}
134
135/* codec register dump */
136static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
138{
139 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000140 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000141 int len;
142 size_t total = 0;
143 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000144
Stephen Warren00b317a2011-04-01 14:50:44 -0600145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000147
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000148 len = wordsize + regsize + 2 + 1;
149
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000150 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000151 return 0;
152
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
159 if (p >= pos) {
160 if (total + len >= count - 1)
161 break;
162 format_register_str(codec, i, buf + total, len);
163 total += len;
Mark Brown5164d742010-07-14 16:14:33 +0100164 }
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100165 p += len;
Mark Brown2624d5f2009-11-03 21:56:13 +0000166 }
167
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000169
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000170 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000171}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000172
Mark Brown2624d5f2009-11-03 21:56:13 +0000173static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
175{
Mark Brown36ae1a92012-01-06 17:12:45 -0800176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000177
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000179}
180
181static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
182
Mark Browndbe21402010-02-12 11:37:24 +0000183static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
185{
Mark Brown36ae1a92012-01-06 17:12:45 -0800186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000187
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000189}
190
191static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
194{
Mark Brown36ae1a92012-01-06 17:12:45 -0800195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700196 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
Jingoo Hanb785a492013-07-19 16:24:59 +0900198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700199 if (ret)
200 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000201
202 return count;
203}
204
205static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
206
Mark Brown2624d5f2009-11-03 21:56:13 +0000207#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000208static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000209 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000210{
211 ssize_t ret;
212 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000213 char *buf;
214
215 if (*ppos < 0 || !count)
216 return -EINVAL;
217
218 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000219 if (!buf)
220 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000221
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
223 if (ret >= 0) {
224 if (copy_to_user(user_buf, buf, ret)) {
225 kfree(buf);
226 return -EFAULT;
227 }
228 *ppos += ret;
229 }
230
Mark Brown2624d5f2009-11-03 21:56:13 +0000231 kfree(buf);
232 return ret;
233}
234
235static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
237{
238 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700239 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000240 char *start = buf;
241 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000242 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900243 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000244
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
247 return -EFAULT;
248 buf[buf_size] = 0;
249
Mark Brown2624d5f2009-11-03 21:56:13 +0000250 while (*start == ' ')
251 start++;
252 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000253 while (*start == ' ')
254 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900255 ret = kstrtoul(start, 16, &value);
256 if (ret)
257 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000258
259 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000261
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000262 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000263 return buf_size;
264}
265
266static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700267 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200270 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000271};
272
Russell Kinge73f3de2014-06-26 15:22:50 +0100273static struct dentry *soc_debugfs_create_dir(struct dentry *parent,
274 const char *fmt, ...)
275{
276 struct dentry *de;
277 va_list ap;
278 char *s;
279
280 va_start(ap, fmt);
281 s = kvasprintf(GFP_KERNEL, fmt, ap);
282 va_end(ap);
283
284 if (!s)
285 return NULL;
286
287 de = debugfs_create_dir(s, parent);
288 kfree(s);
289
290 return de;
291}
292
Mark Brown2624d5f2009-11-03 21:56:13 +0000293static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
294{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200295 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
296
Russell Kinge73f3de2014-06-26 15:22:50 +0100297 codec->debugfs_codec_root = soc_debugfs_create_dir(debugfs_card_root,
Mark Brown211bcc62014-06-28 13:55:22 +0100298 "codec:%s",
299 codec->component.name);
Mark Brown2624d5f2009-11-03 21:56:13 +0000300 if (!codec->debugfs_codec_root) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200301 dev_warn(codec->dev,
302 "ASoC: Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000303 return;
304 }
305
Mark Brownaaee8ef2011-01-26 20:53:50 +0000306 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
307 &codec->cache_sync);
308 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
309 &codec->cache_only);
310
Mark Brown2624d5f2009-11-03 21:56:13 +0000311 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
312 codec->debugfs_codec_root,
313 codec, &codec_reg_fops);
314 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200315 dev_warn(codec->dev,
316 "ASoC: Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000317
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200318 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000319}
320
321static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
322{
323 debugfs_remove_recursive(codec->debugfs_codec_root);
324}
325
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000326static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
327{
328 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
329
Russell Kinge73f3de2014-06-26 15:22:50 +0100330 platform->debugfs_platform_root = soc_debugfs_create_dir(debugfs_card_root,
Mark Brown211bcc62014-06-28 13:55:22 +0100331 "platform:%s",
332 platform->component.name);
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000333 if (!platform->debugfs_platform_root) {
334 dev_warn(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000335 "ASoC: Failed to create platform debugfs directory\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000336 return;
337 }
338
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +0200339 snd_soc_dapm_debugfs_init(&platform->component.dapm,
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000340 platform->debugfs_platform_root);
341}
342
343static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
344{
345 debugfs_remove_recursive(platform->debugfs_platform_root);
346}
347
Mark Brownc3c5a192010-09-15 18:15:14 +0100348static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
349 size_t count, loff_t *ppos)
350{
351 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100352 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100353 struct snd_soc_codec *codec;
354
355 if (!buf)
356 return -ENOMEM;
357
Mark Brown2b194f9d2010-10-13 10:52:16 +0100358 list_for_each_entry(codec, &codec_list, list) {
359 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200360 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100361 if (len >= 0)
362 ret += len;
363 if (ret > PAGE_SIZE) {
364 ret = PAGE_SIZE;
365 break;
366 }
367 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100368
369 if (ret >= 0)
370 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
371
372 kfree(buf);
373
374 return ret;
375}
376
377static const struct file_operations codec_list_fops = {
378 .read = codec_list_read_file,
379 .llseek = default_llseek,/* read accesses f_pos */
380};
381
Mark Brownf3208782010-09-15 18:19:07 +0100382static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
383 size_t count, loff_t *ppos)
384{
385 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100386 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100387 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100388 struct snd_soc_dai *dai;
389
390 if (!buf)
391 return -ENOMEM;
392
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100393 list_for_each_entry(component, &component_list, list) {
394 list_for_each_entry(dai, &component->dai_list, list) {
395 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
396 dai->name);
397 if (len >= 0)
398 ret += len;
399 if (ret > PAGE_SIZE) {
400 ret = PAGE_SIZE;
401 break;
402 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100403 }
404 }
Mark Brownf3208782010-09-15 18:19:07 +0100405
Mark Brown2b194f9d2010-10-13 10:52:16 +0100406 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100407
408 kfree(buf);
409
410 return ret;
411}
412
413static const struct file_operations dai_list_fops = {
414 .read = dai_list_read_file,
415 .llseek = default_llseek,/* read accesses f_pos */
416};
417
Mark Brown19c7ac22010-09-15 18:22:40 +0100418static ssize_t platform_list_read_file(struct file *file,
419 char __user *user_buf,
420 size_t count, loff_t *ppos)
421{
422 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100423 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100424 struct snd_soc_platform *platform;
425
426 if (!buf)
427 return -ENOMEM;
428
Mark Brown2b194f9d2010-10-13 10:52:16 +0100429 list_for_each_entry(platform, &platform_list, list) {
430 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200431 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100432 if (len >= 0)
433 ret += len;
434 if (ret > PAGE_SIZE) {
435 ret = PAGE_SIZE;
436 break;
437 }
438 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100439
Mark Brown2b194f9d2010-10-13 10:52:16 +0100440 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100441
442 kfree(buf);
443
444 return ret;
445}
446
447static const struct file_operations platform_list_fops = {
448 .read = platform_list_read_file,
449 .llseek = default_llseek,/* read accesses f_pos */
450};
451
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200452static void soc_init_card_debugfs(struct snd_soc_card *card)
453{
454 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000455 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200456 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200457 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100458 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200459 return;
460 }
461
462 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
463 card->debugfs_card_root,
464 &card->pop_time);
465 if (!card->debugfs_pop_time)
466 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000467 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200468}
469
470static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
471{
472 debugfs_remove_recursive(card->debugfs_card_root);
473}
474
Mark Brown2624d5f2009-11-03 21:56:13 +0000475#else
476
477static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
478{
479}
480
481static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
482{
483}
Axel Linb95fccb2010-11-09 17:06:44 +0800484
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000485static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
486{
487}
488
489static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
490{
491}
492
Axel Linb95fccb2010-11-09 17:06:44 +0800493static inline void soc_init_card_debugfs(struct snd_soc_card *card)
494{
495}
496
497static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
498{
499}
Mark Brown2624d5f2009-11-03 21:56:13 +0000500#endif
501
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100502struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
503 const char *dai_link, int stream)
504{
505 int i;
506
507 for (i = 0; i < card->num_links; i++) {
508 if (card->rtd[i].dai_link->no_pcm &&
509 !strcmp(card->rtd[i].dai_link->name, dai_link))
510 return card->rtd[i].pcm->streams[stream].substream;
511 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000512 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100513 return NULL;
514}
515EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
516
517struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
518 const char *dai_link)
519{
520 int i;
521
522 for (i = 0; i < card->num_links; i++) {
523 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
524 return &card->rtd[i];
525 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000526 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100527 return NULL;
528}
529EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
530
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531#ifdef CONFIG_SND_SOC_AC97_BUS
532/* unregister ac97 codec */
533static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
534{
535 if (codec->ac97->dev.bus)
536 device_unregister(&codec->ac97->dev);
537 return 0;
538}
539
540/* stop no dev release warning */
541static void soc_ac97_device_release(struct device *dev){}
542
543/* register ac97 codec to bus */
544static int soc_ac97_dev_register(struct snd_soc_codec *codec)
545{
546 int err;
547
548 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100549 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200550 codec->ac97->dev.release = soc_ac97_device_release;
551
Kay Sieversbb072bf2008-11-02 03:50:35 +0100552 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200553 codec->card->snd_card->number, 0, codec->component.name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554 err = device_register(&codec->ac97->dev);
555 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000556 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200557 codec->ac97->dev.bus = NULL;
558 return err;
559 }
560 return 0;
561}
562#endif
563
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100564static void codec2codec_close_delayed_work(struct work_struct *work)
565{
566 /* Currently nothing to do for c2c links
567 * Since c2c links are internal nodes in the DAPM graph and
568 * don't interface with the outside world or application layer
569 * we don't have to do any special handling on close.
570 */
571}
572
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000573#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200574/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000575int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200576{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000577 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200578 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200579 int i;
580
Daniel Macke3509ff2009-06-03 17:44:49 +0200581 /* If the initialization of this soc device failed, there is no codec
582 * associated with it. Just bail out in this case.
583 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200585 return 0;
586
Andy Green6ed25972008-06-13 16:24:05 +0100587 /* Due to the resume being scheduled into a workqueue we could
588 * suspend before that's finished - wait for it to complete.
589 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000590 snd_power_lock(card->snd_card);
591 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
592 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100593
594 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000595 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100596
Mark Browna00f90f2010-12-02 16:24:24 +0000597 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 for (i = 0; i < card->num_rtd; i++) {
599 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
600 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100601
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000602 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100603 continue;
604
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605 if (drv->ops->digital_mute && dai->playback_active)
606 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200607 }
608
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100609 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 for (i = 0; i < card->num_rtd; i++) {
611 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100612 continue;
613
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000614 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100615 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100616
Mark Brown87506542008-11-18 20:50:34 +0000617 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000618 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200619
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 for (i = 0; i < card->num_rtd; i++) {
621 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
622 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100623
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100625 continue;
626
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
628 cpu_dai->driver->suspend(cpu_dai);
629 if (platform->driver->suspend && !platform->suspended) {
630 platform->driver->suspend(cpu_dai);
631 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100632 }
633 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000635 /* close any waiting streams and save state */
636 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo43829732012-08-20 14:51:24 -0700637 flush_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200638 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100640
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000641 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642
643 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100644 continue;
645
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800646 snd_soc_dapm_stream_event(&card->rtd[i],
647 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800648 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000649
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800650 snd_soc_dapm_stream_event(&card->rtd[i],
651 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800652 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000653 }
654
Mark Browne2d32ff2012-08-31 17:38:32 -0700655 /* Recheck all analogue paths too */
656 dapm_mark_io_dirty(&card->dapm);
657 snd_soc_dapm_sync(&card->dapm);
658
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000659 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200660 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 /* If there are paths active then the CODEC will be held with
662 * bias _ON and should not be suspended. */
663 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200664 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000666 /*
667 * If the CODEC is capable of idle
668 * bias off then being in STANDBY
669 * means it's doing something,
670 * otherwise fall through.
671 */
672 if (codec->dapm.idle_bias_off) {
673 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200674 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000675 break;
676 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000677 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100678 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000679 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900680 codec->cache_sync = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200681 if (codec->component.regmap)
682 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800683 /* deactivate pins to sleep state */
684 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000685 break;
686 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200687 dev_dbg(codec->dev,
688 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000689 break;
690 }
691 }
692 }
693
694 for (i = 0; i < card->num_rtd; i++) {
695 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
696
697 if (card->rtd[i].dai_link->ignore_suspend)
698 continue;
699
700 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
701 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800702
703 /* deactivate pins to sleep state */
704 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200705 }
706
Mark Brown87506542008-11-18 20:50:34 +0000707 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000708 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200709
710 return 0;
711}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000712EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200713
Andy Green6ed25972008-06-13 16:24:05 +0100714/* deferred resume work, so resume can complete before we finished
715 * setting our codec back up, which can be very slow on I2C
716 */
717static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200718{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000719 struct snd_soc_card *card =
720 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200721 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200722 int i;
723
Andy Green6ed25972008-06-13 16:24:05 +0100724 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
725 * so userspace apps are blocked from touching us
726 */
727
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000728 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100729
Mark Brown99497882010-05-07 20:24:05 +0100730 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000731 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100732
Mark Brown87506542008-11-18 20:50:34 +0000733 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000734 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200735
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000736 /* resume AC97 DAIs */
737 for (i = 0; i < card->num_rtd; i++) {
738 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100739
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000740 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100741 continue;
742
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
744 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200745 }
746
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200747 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 /* If the CODEC was idle over suspend then it will have been
749 * left with bias OFF or STANDBY and suspended so we must now
750 * resume. Otherwise the suspend was suppressed.
751 */
752 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200753 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754 case SND_SOC_BIAS_STANDBY:
755 case SND_SOC_BIAS_OFF:
756 codec->driver->resume(codec);
757 codec->suspended = 0;
758 break;
759 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200760 dev_dbg(codec->dev,
761 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000762 break;
763 }
Mark Brown1547aba2010-05-07 21:11:40 +0100764 }
765 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200766
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000767 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100768
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000769 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100770 continue;
771
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800772 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000773 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800774 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000775
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800776 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000777 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800778 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200779 }
780
Mark Brown3ff3f642008-05-19 12:32:25 +0200781 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782 for (i = 0; i < card->num_rtd; i++) {
783 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
784 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100785
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000786 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100787 continue;
788
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000789 if (drv->ops->digital_mute && dai->playback_active)
790 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791 }
792
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000793 for (i = 0; i < card->num_rtd; i++) {
794 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
795 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100796
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000797 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100798 continue;
799
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000800 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
801 cpu_dai->driver->resume(cpu_dai);
802 if (platform->driver->resume && platform->suspended) {
803 platform->driver->resume(cpu_dai);
804 platform->suspended = 0;
805 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200806 }
807
Mark Brown87506542008-11-18 20:50:34 +0000808 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000809 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200810
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000811 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100812
813 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000814 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700815
816 /* Recheck all analogue paths too */
817 dapm_mark_io_dirty(&card->dapm);
818 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100819}
820
821/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000822int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100823{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000824 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600825 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200826
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800827 /* If the initialization of this soc device failed, there is no codec
828 * associated with it. Just bail out in this case.
829 */
830 if (list_empty(&card->codec_dev_list))
831 return 0;
832
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800833 /* activate pins from sleep state */
834 for (i = 0; i < card->num_rtd; i++) {
835 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
836 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
837 if (cpu_dai->active)
838 pinctrl_pm_select_default_state(cpu_dai->dev);
839 if (codec_dai->active)
840 pinctrl_pm_select_default_state(codec_dai->dev);
841 }
842
Mark Brown64ab9ba2009-03-31 11:27:03 +0100843 /* AC97 devices might have other drivers hanging off them so
844 * need to resume immediately. Other drivers don't have that
845 * problem and may take a substantial amount of time to resume
846 * due to I/O costs and anti-pop so handle them out of line.
847 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000848 for (i = 0; i < card->num_rtd; i++) {
849 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600850 ac97_control |= cpu_dai->driver->ac97_control;
851 }
852 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000853 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600854 soc_resume_deferred(&card->deferred_resume_work);
855 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000856 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600857 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000858 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100859 }
Andy Green6ed25972008-06-13 16:24:05 +0100860
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200861 return 0;
862}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000863EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200864#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000865#define snd_soc_suspend NULL
866#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200867#endif
868
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100869static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800870};
871
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100872static struct snd_soc_codec *soc_find_codec(const struct device_node *codec_of_node,
873 const char *codec_name)
874{
875 struct snd_soc_codec *codec;
876
877 list_for_each_entry(codec, &codec_list, list) {
878 if (codec_of_node) {
879 if (codec->dev->of_node != codec_of_node)
880 continue;
881 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200882 if (strcmp(codec->component.name, codec_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100883 continue;
884 }
885
886 return codec;
887 }
888
889 return NULL;
890}
891
892static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
893 const char *codec_dai_name)
894{
895 struct snd_soc_dai *codec_dai;
896
897 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
898 if (!strcmp(codec_dai->name, codec_dai_name)) {
899 return codec_dai;
900 }
901 }
902
903 return NULL;
904}
905
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000906static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200907{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000908 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
909 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100910 struct snd_soc_component *component;
Mark Brown435c5e22008-12-04 15:32:53 +0000911 struct snd_soc_platform *platform;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100912 struct snd_soc_dai *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100913 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200914
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000915 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000916
Mark Brownb19e6e72012-03-14 21:18:39 +0000917 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100918 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600919 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100920 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600921 continue;
922 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100923 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600924 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100925 list_for_each_entry(cpu_dai, &component->dai_list, list) {
926 if (dai_link->cpu_dai_name &&
927 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
928 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700929
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100930 rtd->cpu_dai = cpu_dai;
931 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000932 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000933
934 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000935 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000936 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000937 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000938 }
939
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100940 /* Find CODEC from registered list */
941 rtd->codec = soc_find_codec(dai_link->codec_of_node,
942 dai_link->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000943 if (!rtd->codec) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000944 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
Mark Brownb19e6e72012-03-14 21:18:39 +0000945 dai_link->codec_name);
946 return -EPROBE_DEFER;
947 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100948
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100949 /* Find CODEC DAI from registered list */
950 rtd->codec_dai = soc_find_codec_dai(rtd->codec,
951 dai_link->codec_dai_name);
952 if (!rtd->codec_dai) {
953 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
954 dai_link->codec_dai_name);
955 return -EPROBE_DEFER;
956 }
957
Mark Brown848dd8b2011-04-27 18:16:32 +0100958 /* if there's no platform we match on the empty platform */
959 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700960 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100961 platform_name = "snd-soc-dummy";
962
Mark Brownb19e6e72012-03-14 21:18:39 +0000963 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000964 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700965 if (dai_link->platform_of_node) {
966 if (platform->dev->of_node !=
967 dai_link->platform_of_node)
968 continue;
969 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200970 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700971 continue;
972 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700973
974 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000975 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000976 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000977 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000978 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000979 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000980 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000981
982 card->num_rtd++;
983
984 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000985}
986
Stephen Warrend12cd192012-06-08 12:34:22 -0600987static int soc_remove_platform(struct snd_soc_platform *platform)
988{
989 int ret;
990
991 if (platform->driver->remove) {
992 ret = platform->driver->remove(platform);
993 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000994 dev_err(platform->dev, "ASoC: failed to remove %d\n",
995 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -0600996 }
997
998 /* Make sure all DAPM widgets are freed */
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +0200999 snd_soc_dapm_free(&platform->component.dapm);
Stephen Warrend12cd192012-06-08 12:34:22 -06001000
1001 soc_cleanup_platform_debugfs(platform);
1002 platform->probed = 0;
1003 list_del(&platform->card_list);
1004 module_put(platform->dev->driver->owner);
1005
1006 return 0;
1007}
1008
Jarkko Nikula589c3562010-12-06 16:27:07 +02001009static void soc_remove_codec(struct snd_soc_codec *codec)
1010{
1011 int err;
1012
1013 if (codec->driver->remove) {
1014 err = codec->driver->remove(codec);
1015 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001016 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001017 }
1018
1019 /* Make sure all DAPM widgets are freed */
1020 snd_soc_dapm_free(&codec->dapm);
1021
1022 soc_cleanup_codec_debugfs(codec);
1023 codec->probed = 0;
1024 list_del(&codec->card_list);
1025 module_put(codec->dev->driver->owner);
1026}
1027
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001028static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1029{
1030 int err;
1031
1032 if (codec_dai && codec_dai->probed &&
1033 codec_dai->driver->remove_order == order) {
1034 if (codec_dai->driver->remove) {
1035 err = codec_dai->driver->remove(codec_dai);
1036 if (err < 0)
1037 dev_err(codec_dai->dev,
1038 "ASoC: failed to remove %s: %d\n",
1039 codec_dai->name, err);
1040 }
1041 codec_dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001042 }
1043}
1044
Stephen Warren62ae68f2012-06-08 12:34:23 -06001045static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001046{
1047 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001048 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1049 int err;
1050
1051 /* unregister the rtd device */
1052 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001053 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1054 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1055 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001056 rtd->dev_registered = 0;
1057 }
1058
1059 /* remove the CODEC DAI */
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001060 soc_remove_codec_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001061
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001062 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001063 if (cpu_dai && cpu_dai->probed &&
1064 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001065 if (cpu_dai->driver->remove) {
1066 err = cpu_dai->driver->remove(cpu_dai);
1067 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001068 dev_err(cpu_dai->dev,
1069 "ASoC: failed to remove %s: %d\n",
1070 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001071 }
1072 cpu_dai->probed = 0;
Lars-Peter Clausen9420d972014-06-16 18:13:10 +02001073 if (!cpu_dai->codec)
Stephen Warrena9db7db2012-06-08 12:34:20 -06001074 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001075 }
1076}
1077
Stephen Warren62ae68f2012-06-08 12:34:23 -06001078static void soc_remove_link_components(struct snd_soc_card *card, int num,
1079 int order)
1080{
1081 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1082 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1083 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1084 struct snd_soc_platform *platform = rtd->platform;
1085 struct snd_soc_codec *codec;
1086
1087 /* remove the platform */
1088 if (platform && platform->probed &&
1089 platform->driver->remove_order == order) {
1090 soc_remove_platform(platform);
1091 }
1092
1093 /* remove the CODEC-side CODEC */
1094 if (codec_dai) {
1095 codec = codec_dai->codec;
1096 if (codec && codec->probed &&
1097 codec->driver->remove_order == order)
1098 soc_remove_codec(codec);
1099 }
1100
1101 /* remove any CPU-side CODEC */
1102 if (cpu_dai) {
1103 codec = cpu_dai->codec;
1104 if (codec && codec->probed &&
1105 codec->driver->remove_order == order)
1106 soc_remove_codec(codec);
1107 }
1108}
1109
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001110static void soc_remove_dai_links(struct snd_soc_card *card)
1111{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001112 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001113
Liam Girdwood0168bf02011-06-07 16:08:05 +01001114 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1115 order++) {
1116 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001117 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001118 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001119
1120 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1121 order++) {
1122 for (dai = 0; dai < card->num_rtd; dai++)
1123 soc_remove_link_components(card, dai, order);
1124 }
1125
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001126 card->num_rtd = 0;
1127}
1128
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001129static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001130 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001131{
1132 int i;
1133
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001134 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001135 return;
1136
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001137 for (i = 0; i < card->num_configs; i++) {
1138 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001139 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001140 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001141 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001142 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001143 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001144 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001145 }
1146}
1147
Jarkko Nikula589c3562010-12-06 16:27:07 +02001148static int soc_probe_codec(struct snd_soc_card *card,
1149 struct snd_soc_codec *codec)
1150{
1151 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001152 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001153 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001154
1155 codec->card = card;
1156 codec->dapm.card = card;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001157 soc_set_name_prefix(card, &codec->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001158
Jarkko Nikula70d29332011-01-27 16:24:22 +02001159 if (!try_module_get(codec->dev->driver->owner))
1160 return -ENODEV;
1161
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001162 soc_init_codec_debugfs(codec);
1163
Nariman Poushinb318ad52014-04-01 13:59:33 +01001164 if (driver->dapm_widgets) {
1165 ret = snd_soc_dapm_new_controls(&codec->dapm,
1166 driver->dapm_widgets,
1167 driver->num_dapm_widgets);
1168
1169 if (ret != 0) {
1170 dev_err(codec->dev,
1171 "Failed to create new controls %d\n", ret);
1172 goto err_probe;
1173 }
1174 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001175
Mark Brown888df392012-02-16 19:37:51 -08001176 /* Create DAPM widgets for each DAI stream */
Nariman Poushin261edc72014-03-31 15:47:12 +01001177 list_for_each_entry(dai, &codec->component.dai_list, list) {
1178 ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1179
1180 if (ret != 0) {
1181 dev_err(codec->dev,
1182 "Failed to create DAI widgets %d\n", ret);
1183 goto err_probe;
1184 }
1185 }
Mark Brown888df392012-02-16 19:37:51 -08001186
Mark Brown33c5f962011-08-22 18:40:30 +01001187 codec->dapm.idle_bias_off = driver->idle_bias_off;
1188
Mark Brown89b95ac02011-03-07 16:38:44 +00001189 if (driver->probe) {
1190 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001191 if (ret < 0) {
1192 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001193 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001194 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001195 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001196 WARN(codec->dapm.idle_bias_off &&
1197 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001198 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02001199 codec->component.name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001200 }
1201
Mark Brownb7af1da2011-04-07 19:18:44 +09001202 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001203 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001204 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001205 if (driver->dapm_routes)
1206 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1207 driver->num_dapm_routes);
1208
Jarkko Nikula589c3562010-12-06 16:27:07 +02001209 /* mark codec as probed and add to card codec list */
1210 codec->probed = 1;
1211 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001212 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001213
Jarkko Nikula70d29332011-01-27 16:24:22 +02001214 return 0;
1215
1216err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001217 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001218 module_put(codec->dev->driver->owner);
1219
Jarkko Nikula589c3562010-12-06 16:27:07 +02001220 return ret;
1221}
1222
Liam Girdwood956245e2011-07-01 16:54:08 +01001223static int soc_probe_platform(struct snd_soc_card *card,
1224 struct snd_soc_platform *platform)
1225{
1226 int ret = 0;
1227 const struct snd_soc_platform_driver *driver = platform->driver;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001228 struct snd_soc_component *component;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001229 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001230
1231 platform->card = card;
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001232 platform->component.dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001233
1234 if (!try_module_get(platform->dev->driver->owner))
1235 return -ENODEV;
1236
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001237 soc_init_platform_debugfs(platform);
1238
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001239 if (driver->dapm_widgets)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001240 snd_soc_dapm_new_controls(&platform->component.dapm,
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001241 driver->dapm_widgets, driver->num_dapm_widgets);
1242
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001243 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001244 list_for_each_entry(component, &component_list, list) {
1245 if (component->dev != platform->dev)
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001246 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001247 list_for_each_entry(dai, &component->dai_list, list)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001248 snd_soc_dapm_new_dai_widgets(&platform->component.dapm,
1249 dai);
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001250 }
1251
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001252 platform->component.dapm.idle_bias_off = 1;
Stephen Warren3fec6b62012-04-05 12:28:01 -06001253
Liam Girdwood956245e2011-07-01 16:54:08 +01001254 if (driver->probe) {
1255 ret = driver->probe(platform);
1256 if (ret < 0) {
1257 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001258 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001259 goto err_probe;
1260 }
1261 }
1262
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001263 if (driver->controls)
1264 snd_soc_add_platform_controls(platform, driver->controls,
1265 driver->num_controls);
1266 if (driver->dapm_routes)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001267 snd_soc_dapm_add_routes(&platform->component.dapm,
1268 driver->dapm_routes, driver->num_dapm_routes);
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001269
Liam Girdwood956245e2011-07-01 16:54:08 +01001270 /* mark platform as probed and add to card platform list */
1271 platform->probed = 1;
1272 list_add(&platform->card_list, &card->platform_dev_list);
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001273 list_add(&platform->component.dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001274
1275 return 0;
1276
1277err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001278 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001279 module_put(platform->dev->driver->owner);
1280
1281 return ret;
1282}
1283
Mark Brown36ae1a92012-01-06 17:12:45 -08001284static void rtd_release(struct device *dev)
1285{
1286 kfree(dev);
1287}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001288
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001289static int soc_aux_dev_init(struct snd_soc_card *card,
1290 struct snd_soc_codec *codec,
1291 int num)
1292{
1293 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1294 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001295 int ret;
1296
1297 rtd->card = card;
1298
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001299 /* do machine specific initialization */
1300 if (aux_dev->init) {
1301 ret = aux_dev->init(&codec->dapm);
1302 if (ret < 0)
1303 return ret;
1304 }
1305
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001306 rtd->codec = codec;
1307
1308 return 0;
1309}
1310
1311static int soc_dai_link_init(struct snd_soc_card *card,
1312 struct snd_soc_codec *codec,
1313 int num)
1314{
1315 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1316 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001317 int ret;
1318
1319 rtd->card = card;
1320
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001321 /* do machine specific initialization */
1322 if (dai_link->init) {
1323 ret = dai_link->init(rtd);
1324 if (ret < 0)
1325 return ret;
1326 }
1327
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001328 rtd->codec = codec;
1329
1330 return 0;
1331}
1332
Jarkko Nikula589c3562010-12-06 16:27:07 +02001333static int soc_post_component_init(struct snd_soc_card *card,
1334 struct snd_soc_codec *codec,
1335 int num, int dailess)
1336{
1337 struct snd_soc_dai_link *dai_link = NULL;
1338 struct snd_soc_aux_dev *aux_dev = NULL;
1339 struct snd_soc_pcm_runtime *rtd;
Lars-Peter Clausen6479f15ad2014-03-12 15:27:40 +01001340 const char *name;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001341 int ret = 0;
1342
1343 if (!dailess) {
1344 dai_link = &card->dai_link[num];
1345 rtd = &card->rtd[num];
1346 name = dai_link->name;
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001347 ret = soc_dai_link_init(card, codec, num);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001348 } else {
1349 aux_dev = &card->aux_dev[num];
1350 rtd = &card->rtd_aux[num];
1351 name = aux_dev->name;
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001352 ret = soc_aux_dev_init(card, codec, num);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001353 }
1354
Jarkko Nikula589c3562010-12-06 16:27:07 +02001355 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001356 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001357 return ret;
1358 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001359
Jarkko Nikula589c3562010-12-06 16:27:07 +02001360 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001361 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1362 if (!rtd->dev)
1363 return -ENOMEM;
1364 device_initialize(rtd->dev);
1365 rtd->dev->parent = card->dev;
1366 rtd->dev->release = rtd_release;
1367 rtd->dev->init_name = name;
1368 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001369 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001370 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1371 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1372 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1373 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001374 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001375 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001376 /* calling put_device() here to free the rtd->dev */
1377 put_device(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001378 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001379 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001380 return ret;
1381 }
1382 rtd->dev_registered = 1;
1383
1384 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001385 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001386 if (ret < 0)
1387 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001388 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001389
1390 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001391 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001392 if (ret < 0)
1393 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001394 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001395
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001396#ifdef CONFIG_DEBUG_FS
1397 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001398 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001399 goto out;
1400
1401 ret = soc_dpcm_debugfs_add(rtd);
1402 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001403 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001404
1405out:
1406#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001407 return 0;
1408}
1409
Stephen Warren62ae68f2012-06-08 12:34:23 -06001410static int soc_probe_link_components(struct snd_soc_card *card, int num,
1411 int order)
1412{
1413 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1414 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1415 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1416 struct snd_soc_platform *platform = rtd->platform;
1417 int ret;
1418
1419 /* probe the CPU-side component, if it is a CODEC */
1420 if (cpu_dai->codec &&
1421 !cpu_dai->codec->probed &&
1422 cpu_dai->codec->driver->probe_order == order) {
1423 ret = soc_probe_codec(card, cpu_dai->codec);
1424 if (ret < 0)
1425 return ret;
1426 }
1427
1428 /* probe the CODEC-side component */
1429 if (!codec_dai->codec->probed &&
1430 codec_dai->codec->driver->probe_order == order) {
1431 ret = soc_probe_codec(card, codec_dai->codec);
1432 if (ret < 0)
1433 return ret;
1434 }
1435
1436 /* probe the platform */
1437 if (!platform->probed &&
1438 platform->driver->probe_order == order) {
1439 ret = soc_probe_platform(card, platform);
1440 if (ret < 0)
1441 return ret;
1442 }
1443
1444 return 0;
1445}
1446
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001447static int soc_probe_codec_dai(struct snd_soc_card *card,
1448 struct snd_soc_dai *codec_dai,
1449 int order)
1450{
1451 int ret;
1452
1453 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1454 if (codec_dai->driver->probe) {
1455 ret = codec_dai->driver->probe(codec_dai);
1456 if (ret < 0) {
1457 dev_err(codec_dai->dev,
1458 "ASoC: failed to probe CODEC DAI %s: %d\n",
1459 codec_dai->name, ret);
1460 return ret;
1461 }
1462 }
1463
1464 /* mark codec_dai as probed and add to card dai list */
1465 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001466 }
1467
1468 return 0;
1469}
1470
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001471static int soc_link_dai_widgets(struct snd_soc_card *card,
1472 struct snd_soc_dai_link *dai_link,
1473 struct snd_soc_dai *cpu_dai,
1474 struct snd_soc_dai *codec_dai)
1475{
1476 struct snd_soc_dapm_widget *play_w, *capture_w;
1477 int ret;
1478
1479 /* link the DAI widgets */
1480 play_w = codec_dai->playback_widget;
1481 capture_w = cpu_dai->capture_widget;
1482 if (play_w && capture_w) {
1483 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1484 capture_w, play_w);
1485 if (ret != 0) {
1486 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1487 play_w->name, capture_w->name, ret);
1488 return ret;
1489 }
1490 }
1491
1492 play_w = cpu_dai->playback_widget;
1493 capture_w = codec_dai->capture_widget;
1494 if (play_w && capture_w) {
1495 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1496 capture_w, play_w);
1497 if (ret != 0) {
1498 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1499 play_w->name, capture_w->name, ret);
1500 return ret;
1501 }
1502 }
1503
1504 return 0;
1505}
1506
Stephen Warren62ae68f2012-06-08 12:34:23 -06001507static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001508{
1509 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1510 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1511 struct snd_soc_codec *codec = rtd->codec;
1512 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001513 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1514 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001515 int ret;
1516
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001517 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001518 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001519
1520 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001521 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001522 codec_dai->card = card;
1523 cpu_dai->card = card;
1524
1525 /* set default power off timeout */
1526 rtd->pmdown_time = pmdown_time;
1527
1528 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001529 if (!cpu_dai->probed &&
1530 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001531 if (!cpu_dai->codec) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001532 if (!try_module_get(cpu_dai->dev->driver->owner))
1533 return -ENODEV;
Stephen Warrena9db7db2012-06-08 12:34:20 -06001534 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001535
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001536 if (cpu_dai->driver->probe) {
1537 ret = cpu_dai->driver->probe(cpu_dai);
1538 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001539 dev_err(cpu_dai->dev,
1540 "ASoC: failed to probe CPU DAI %s: %d\n",
1541 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001542 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001543 return ret;
1544 }
1545 }
1546 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001547 }
1548
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001549 /* probe the CODEC DAI */
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001550 ret = soc_probe_codec_dai(card, codec_dai, order);
1551 if (ret)
1552 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001553
Liam Girdwood0168bf02011-06-07 16:08:05 +01001554 /* complete DAI probe during last probe */
1555 if (order != SND_SOC_COMP_ORDER_LAST)
1556 return 0;
1557
Jarkko Nikula589c3562010-12-06 16:27:07 +02001558 ret = soc_post_component_init(card, codec, num, 0);
1559 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001560 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001561
Mark Brown36ae1a92012-01-06 17:12:45 -08001562 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001563 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001564 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1565 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001566
Namarta Kohli1245b702012-08-16 17:10:41 +05301567 if (cpu_dai->driver->compress_dai) {
1568 /*create compress_device"*/
1569 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001570 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001571 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301572 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001573 return ret;
1574 }
1575 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301576
1577 if (!dai_link->params) {
1578 /* create the pcm */
1579 ret = soc_new_pcm(rtd, num);
1580 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001581 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301582 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001583 return ret;
1584 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301585 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001586 INIT_DELAYED_WORK(&rtd->delayed_work,
1587 codec2codec_close_delayed_work);
1588
Namarta Kohli1245b702012-08-16 17:10:41 +05301589 /* link the DAI widgets */
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001590 ret = soc_link_dai_widgets(card, dai_link,
1591 cpu_dai, codec_dai);
1592 if (ret)
1593 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001594 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001595 }
1596
1597 /* add platform data for AC97 devices */
1598 if (rtd->codec_dai->driver->ac97_control)
1599 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1600
1601 return 0;
1602}
1603
1604#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001605static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1606 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001607{
1608 int ret;
1609
1610 /* Only instantiate AC97 if not already done by the adaptor
1611 * for the generic AC97 subsystem.
1612 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001613 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001614 /*
1615 * It is possible that the AC97 device is already registered to
1616 * the device subsystem. This happens when the device is created
1617 * via snd_ac97_mixer(). Currently only SoC codec that does so
1618 * is the generic AC97 glue but others migh emerge.
1619 *
1620 * In those cases we don't try to register the device again.
1621 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001622 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001623 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001624
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001625 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001626 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001627 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001628 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001629 return ret;
1630 }
1631
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001632 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001633 }
1634 return 0;
1635}
1636
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001637static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1638{
1639 return soc_register_ac97_codec(rtd->codec, rtd->codec_dai);
1640}
1641
1642static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001643{
1644 if (codec->ac97_registered) {
1645 soc_ac97_dev_unregister(codec);
1646 codec->ac97_registered = 0;
1647 }
1648}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001649
1650static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1651{
1652 soc_unregister_ac97_codec(rtd->codec);
1653}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001654#endif
1655
Lars-Peter Clausen6b0a0b32014-05-04 19:18:30 +02001656static struct snd_soc_codec *soc_find_matching_codec(struct snd_soc_card *card,
1657 int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001658{
1659 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1660 struct snd_soc_codec *codec;
1661
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001662 /* find CODEC from registered CODECs */
Mark Brownb19e6e72012-03-14 21:18:39 +00001663 list_for_each_entry(codec, &codec_list, list) {
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001664 if (aux_dev->codec_of_node &&
1665 (codec->dev->of_node != aux_dev->codec_of_node))
1666 continue;
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02001667 if (aux_dev->codec_name &&
1668 strcmp(codec->component.name, aux_dev->codec_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001669 continue;
1670 return codec;
Mark Brownb19e6e72012-03-14 21:18:39 +00001671 }
1672
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001673 return NULL;
1674}
Mark Brownfb099cb2012-08-09 18:44:37 +01001675
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001676static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1677{
1678 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1679 const char *codecname = aux_dev->codec_name;
1680 struct snd_soc_codec *codec = soc_find_matching_codec(card, num);
1681
1682 if (codec)
1683 return 0;
1684 if (aux_dev->codec_of_node)
1685 codecname = of_node_full_name(aux_dev->codec_of_node);
1686
1687 dev_err(card->dev, "ASoC: %s not registered\n", codecname);
Mark Brownb19e6e72012-03-14 21:18:39 +00001688 return -EPROBE_DEFER;
1689}
1690
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001691static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1692{
1693 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001694 const char *codecname = aux_dev->codec_name;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001695 int ret = -ENODEV;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001696 struct snd_soc_codec *codec = soc_find_matching_codec(card, num);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001697
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001698 if (!codec) {
1699 if (aux_dev->codec_of_node)
1700 codecname = of_node_full_name(aux_dev->codec_of_node);
1701
1702 /* codec not found */
1703 dev_err(card->dev, "ASoC: codec %s not found", codecname);
1704 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001705 }
1706
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001707 if (codec->probed) {
1708 dev_err(codec->dev, "ASoC: codec already probed");
1709 return -EBUSY;
1710 }
1711
Jarkko Nikula589c3562010-12-06 16:27:07 +02001712 ret = soc_probe_codec(card, codec);
1713 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001714 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001715
Jarkko Nikula589c3562010-12-06 16:27:07 +02001716 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001717
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001718 return ret;
1719}
1720
1721static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1722{
1723 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1724 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001725
1726 /* unregister the rtd device */
1727 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001728 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001729 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001730 rtd->dev_registered = 0;
1731 }
1732
Jarkko Nikula589c3562010-12-06 16:27:07 +02001733 if (codec && codec->probed)
1734 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001735}
1736
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001737static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001738{
1739 int ret;
1740
1741 if (codec->cache_init)
1742 return 0;
1743
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001744 ret = snd_soc_cache_init(codec);
1745 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001746 dev_err(codec->dev,
1747 "ASoC: Failed to set cache compression type: %d\n",
1748 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001749 return ret;
1750 }
1751 codec->cache_init = 1;
1752 return 0;
1753}
1754
Mark Brownb19e6e72012-03-14 21:18:39 +00001755static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001756{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001757 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001758 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001759 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001760
Liam Girdwood01b9d992012-03-07 10:38:25 +00001761 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001762
Mark Brownb19e6e72012-03-14 21:18:39 +00001763 /* bind DAIs */
1764 for (i = 0; i < card->num_links; i++) {
1765 ret = soc_bind_dai_link(card, i);
1766 if (ret != 0)
1767 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001768 }
1769
Mark Brownb19e6e72012-03-14 21:18:39 +00001770 /* check aux_devs too */
1771 for (i = 0; i < card->num_aux_devs; i++) {
1772 ret = soc_check_aux_dev(card, i);
1773 if (ret != 0)
1774 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001775 }
1776
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001777 /* initialize the register cache for each available codec */
1778 list_for_each_entry(codec, &codec_list, list) {
1779 if (codec->cache_init)
1780 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001781 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001782 if (ret < 0)
1783 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001784 }
1785
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001786 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001787 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001788 card->owner, 0, &card->snd_card);
1789 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001790 dev_err(card->dev,
1791 "ASoC: can't create sound card for card %s: %d\n",
1792 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001793 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001794 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001795
Mark Browne37a4972011-03-02 18:21:57 +00001796 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1797 card->dapm.dev = card->dev;
1798 card->dapm.card = card;
1799 list_add(&card->dapm.list, &card->dapm_list);
1800
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001801#ifdef CONFIG_DEBUG_FS
1802 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1803#endif
1804
Mark Brown88ee1c62011-02-02 10:43:26 +00001805#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001806 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001807 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001808#endif
Andy Green6ed25972008-06-13 16:24:05 +01001809
Mark Brown9a841eb2011-04-12 17:51:37 -07001810 if (card->dapm_widgets)
1811 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1812 card->num_dapm_widgets);
1813
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001814 /* initialise the sound card only once */
1815 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001816 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001817 if (ret < 0)
1818 goto card_probe_error;
1819 }
1820
Stephen Warren62ae68f2012-06-08 12:34:23 -06001821 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001822 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1823 order++) {
1824 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001825 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001826 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001827 dev_err(card->dev,
1828 "ASoC: failed to instantiate card %d\n",
1829 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001830 goto probe_dai_err;
1831 }
1832 }
1833 }
1834
1835 /* probe all DAI links on this card */
1836 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1837 order++) {
1838 for (i = 0; i < card->num_links; i++) {
1839 ret = soc_probe_link_dais(card, i, order);
1840 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001841 dev_err(card->dev,
1842 "ASoC: failed to instantiate card %d\n",
1843 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001844 goto probe_dai_err;
1845 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001846 }
1847 }
1848
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001849 for (i = 0; i < card->num_aux_devs; i++) {
1850 ret = soc_probe_aux_dev(card, i);
1851 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001852 dev_err(card->dev,
1853 "ASoC: failed to add auxiliary devices %d\n",
1854 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001855 goto probe_aux_dev_err;
1856 }
1857 }
1858
Mark Brown888df392012-02-16 19:37:51 -08001859 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001860 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001861
Mark Brownb7af1da2011-04-07 19:18:44 +09001862 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001863 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001864
Mark Brownb8ad29d2011-03-02 18:35:51 +00001865 if (card->dapm_routes)
1866 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1867 card->num_dapm_routes);
1868
Mark Brown75d9ac42011-09-27 16:41:01 +01001869 for (i = 0; i < card->num_links; i++) {
1870 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001871 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001872
Mark Brownf04209a2012-04-09 16:29:21 +01001873 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001874 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001875 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001876 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001877 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001878 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001879 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001880 }
1881
1882 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001883 if (dai_fmt &&
1884 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001885 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1886 dai_fmt);
1887 if (ret != 0 && ret != -ENOTSUPP)
1888 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001889 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001890 ret);
1891 } else if (dai_fmt) {
1892 /* Flip the polarity for the "CPU" end */
1893 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1894 switch (dai_link->dai_fmt &
1895 SND_SOC_DAIFMT_MASTER_MASK) {
1896 case SND_SOC_DAIFMT_CBM_CFM:
1897 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1898 break;
1899 case SND_SOC_DAIFMT_CBM_CFS:
1900 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1901 break;
1902 case SND_SOC_DAIFMT_CBS_CFM:
1903 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1904 break;
1905 case SND_SOC_DAIFMT_CBS_CFS:
1906 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1907 break;
1908 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001909
1910 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001911 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001912 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001913 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001914 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001915 ret);
1916 }
1917 }
1918
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001919 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001920 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001921 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1922 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001923 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1924 "%s", card->driver_name ? card->driver_name : card->name);
1925 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1926 switch (card->snd_card->driver[i]) {
1927 case '_':
1928 case '-':
1929 case '\0':
1930 break;
1931 default:
1932 if (!isalnum(card->snd_card->driver[i]))
1933 card->snd_card->driver[i] = '_';
1934 break;
1935 }
1936 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001937
Mark Brown28e9ad92011-03-02 18:36:34 +00001938 if (card->late_probe) {
1939 ret = card->late_probe(card);
1940 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001941 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001942 card->name, ret);
1943 goto probe_aux_dev_err;
1944 }
1945 }
1946
Stephen Warren16332812011-11-23 12:42:04 -07001947 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001948 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001949
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001950 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001951
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001952 ret = snd_card_register(card->snd_card);
1953 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001954 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1955 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001956 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001957 }
1958
1959#ifdef CONFIG_SND_SOC_AC97_BUS
1960 /* register any AC97 codecs */
1961 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001962 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1963 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001964 dev_err(card->dev,
1965 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001966 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001967 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001968 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001969 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001970 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001971#endif
1972
Mark Brown435c5e22008-12-04 15:32:53 +00001973 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001974 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001975 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001976
1977 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001978
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001979probe_aux_dev_err:
1980 for (i = 0; i < card->num_aux_devs; i++)
1981 soc_remove_aux_dev(card, i);
1982
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001983probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001984 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001985
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001986card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001987 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001988 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001989
1990 snd_card_free(card->snd_card);
1991
Mark Brownb19e6e72012-03-14 21:18:39 +00001992base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001993 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001994
Mark Brownb19e6e72012-03-14 21:18:39 +00001995 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001996}
1997
1998/* probes a new socdev */
1999static int soc_probe(struct platform_device *pdev)
2000{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002001 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002002
Vinod Koul70a7ca32011-01-14 19:22:48 +05302003 /*
2004 * no card, so machine driver should be registering card
2005 * we should not be here in that case so ret error
2006 */
2007 if (!card)
2008 return -EINVAL;
2009
Mark Brownfe4085e2012-03-02 13:07:41 +00002010 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002011 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002012 card->name);
2013
Mark Brown435c5e22008-12-04 15:32:53 +00002014 /* Bodge while we unpick instantiation */
2015 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002016
Mark Brown28d528c2012-08-09 18:45:23 +01002017 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002018}
2019
Vinod Koulb0e26482011-01-13 22:48:02 +05302020static int soc_cleanup_card_resources(struct snd_soc_card *card)
2021{
Vinod Koulb0e26482011-01-13 22:48:02 +05302022 int i;
2023
2024 /* make sure any delayed work runs */
2025 for (i = 0; i < card->num_rtd; i++) {
2026 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002027 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302028 }
2029
2030 /* remove auxiliary devices */
2031 for (i = 0; i < card->num_aux_devs; i++)
2032 soc_remove_aux_dev(card, i);
2033
2034 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002035 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302036
2037 soc_cleanup_card_debugfs(card);
2038
2039 /* remove the card */
2040 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002041 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302042
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02002043 snd_soc_dapm_free(&card->dapm);
2044
Vinod Koulb0e26482011-01-13 22:48:02 +05302045 snd_card_free(card->snd_card);
2046 return 0;
2047
2048}
2049
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002050/* removes a socdev */
2051static int soc_remove(struct platform_device *pdev)
2052{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002053 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002054
Mark Brownc5af3a22008-11-28 13:29:45 +00002055 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002056 return 0;
2057}
2058
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002059int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002060{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002061 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002062 int i;
Mark Brown51737472009-06-22 13:16:51 +01002063
2064 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002065 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002066
2067 /* Flush out pmdown_time work - we actually do want to run it
2068 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002069 for (i = 0; i < card->num_rtd; i++) {
2070 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002071 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002072 }
Mark Brown51737472009-06-22 13:16:51 +01002073
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002074 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002075
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002076 /* deactivate pins to sleep state */
2077 for (i = 0; i < card->num_rtd; i++) {
2078 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
2079 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
2080 pinctrl_pm_select_sleep_state(codec_dai->dev);
2081 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2082 }
2083
Mark Brown416356f2009-06-30 19:05:15 +01002084 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002085}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002086EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002087
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002088const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302089 .suspend = snd_soc_suspend,
2090 .resume = snd_soc_resume,
2091 .freeze = snd_soc_suspend,
2092 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002093 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302094 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002095};
Stephen Warrendeb26072011-04-05 19:35:30 -06002096EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002097
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002098/* ASoC platform driver */
2099static struct platform_driver soc_driver = {
2100 .driver = {
2101 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002102 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002103 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002104 },
2105 .probe = soc_probe,
2106 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002107};
2108
Mark Brown096e49d2009-07-05 15:12:22 +01002109/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002110 * snd_soc_new_ac97_codec - initailise AC97 device
2111 * @codec: audio codec
2112 * @ops: AC97 bus operations
2113 * @num: AC97 codec number
2114 *
2115 * Initialises AC97 codec resources for use by ad-hoc devices only.
2116 */
2117int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2118 struct snd_ac97_bus_ops *ops, int num)
2119{
2120 mutex_lock(&codec->mutex);
2121
2122 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2123 if (codec->ac97 == NULL) {
2124 mutex_unlock(&codec->mutex);
2125 return -ENOMEM;
2126 }
2127
2128 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2129 if (codec->ac97->bus == NULL) {
2130 kfree(codec->ac97);
2131 codec->ac97 = NULL;
2132 mutex_unlock(&codec->mutex);
2133 return -ENOMEM;
2134 }
2135
2136 codec->ac97->bus->ops = ops;
2137 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002138
2139 /*
2140 * Mark the AC97 device to be created by us. This way we ensure that the
2141 * device will be registered with the device subsystem later on.
2142 */
2143 codec->ac97_created = 1;
2144
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002145 mutex_unlock(&codec->mutex);
2146 return 0;
2147}
2148EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2149
Markus Pargmann741a5092013-08-19 17:05:55 +02002150static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2151
2152static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2153{
2154 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2155
2156 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2157
2158 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2159
2160 udelay(10);
2161
2162 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2163
2164 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2165 msleep(2);
2166}
2167
2168static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2169{
2170 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2171
2172 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2173
2174 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2175 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2176 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2177
2178 udelay(10);
2179
2180 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2181
2182 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2183 msleep(2);
2184}
2185
2186static int snd_soc_ac97_parse_pinctl(struct device *dev,
2187 struct snd_ac97_reset_cfg *cfg)
2188{
2189 struct pinctrl *p;
2190 struct pinctrl_state *state;
2191 int gpio;
2192 int ret;
2193
2194 p = devm_pinctrl_get(dev);
2195 if (IS_ERR(p)) {
2196 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002197 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002198 }
2199 cfg->pctl = p;
2200
2201 state = pinctrl_lookup_state(p, "ac97-reset");
2202 if (IS_ERR(state)) {
2203 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002204 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002205 }
2206 cfg->pstate_reset = state;
2207
2208 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2209 if (IS_ERR(state)) {
2210 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002211 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002212 }
2213 cfg->pstate_warm_reset = state;
2214
2215 state = pinctrl_lookup_state(p, "ac97-running");
2216 if (IS_ERR(state)) {
2217 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002218 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002219 }
2220 cfg->pstate_run = state;
2221
2222 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2223 if (gpio < 0) {
2224 dev_err(dev, "Can't find ac97-sync gpio\n");
2225 return gpio;
2226 }
2227 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2228 if (ret) {
2229 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2230 return ret;
2231 }
2232 cfg->gpio_sync = gpio;
2233
2234 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2235 if (gpio < 0) {
2236 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2237 return gpio;
2238 }
2239 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2240 if (ret) {
2241 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2242 return ret;
2243 }
2244 cfg->gpio_sdata = gpio;
2245
2246 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2247 if (gpio < 0) {
2248 dev_err(dev, "Can't find ac97-reset gpio\n");
2249 return gpio;
2250 }
2251 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2252 if (ret) {
2253 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2254 return ret;
2255 }
2256 cfg->gpio_reset = gpio;
2257
2258 return 0;
2259}
2260
Mark Brownb047e1c2013-06-26 12:45:59 +01002261struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002262EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002263
2264int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2265{
2266 if (ops == soc_ac97_ops)
2267 return 0;
2268
2269 if (soc_ac97_ops && ops)
2270 return -EBUSY;
2271
2272 soc_ac97_ops = ops;
2273
2274 return 0;
2275}
2276EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2277
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002278/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002279 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2280 *
2281 * This function sets the reset and warm_reset properties of ops and parses
2282 * the device node of pdev to get pinctrl states and gpio numbers to use.
2283 */
2284int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2285 struct platform_device *pdev)
2286{
2287 struct device *dev = &pdev->dev;
2288 struct snd_ac97_reset_cfg cfg;
2289 int ret;
2290
2291 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2292 if (ret)
2293 return ret;
2294
2295 ret = snd_soc_set_ac97_ops(ops);
2296 if (ret)
2297 return ret;
2298
2299 ops->warm_reset = snd_soc_ac97_warm_reset;
2300 ops->reset = snd_soc_ac97_reset;
2301
2302 snd_ac97_rst_cfg = cfg;
2303 return 0;
2304}
2305EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2306
2307/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002308 * snd_soc_free_ac97_codec - free AC97 codec device
2309 * @codec: audio codec
2310 *
2311 * Frees AC97 codec device resources.
2312 */
2313void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2314{
2315 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002316#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002317 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002318#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002319 kfree(codec->ac97->bus);
2320 kfree(codec->ac97);
2321 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002322 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002323 mutex_unlock(&codec->mutex);
2324}
2325EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2326
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002327/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002328 * snd_soc_cnew - create new control
2329 * @_template: control template
2330 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002331 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002332 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002333 *
2334 * Create a new mixer control from a template control.
2335 *
2336 * Returns 0 for success, else error.
2337 */
2338struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002339 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002340 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002341{
2342 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002343 struct snd_kcontrol *kcontrol;
2344 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002345
2346 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002347 template.index = 0;
2348
Mark Brownefb7ac32011-03-08 17:23:24 +00002349 if (!long_name)
2350 long_name = template.name;
2351
2352 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002353 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002354 if (!name)
2355 return NULL;
2356
Mark Brownefb7ac32011-03-08 17:23:24 +00002357 template.name = name;
2358 } else {
2359 template.name = long_name;
2360 }
2361
2362 kcontrol = snd_ctl_new1(&template, data);
2363
2364 kfree(name);
2365
2366 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002367}
2368EXPORT_SYMBOL_GPL(snd_soc_cnew);
2369
Liam Girdwood022658b2012-02-03 17:43:09 +00002370static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2371 const struct snd_kcontrol_new *controls, int num_controls,
2372 const char *prefix, void *data)
2373{
2374 int err, i;
2375
2376 for (i = 0; i < num_controls; i++) {
2377 const struct snd_kcontrol_new *control = &controls[i];
2378 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2379 control->name, prefix));
2380 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002381 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2382 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002383 return err;
2384 }
2385 }
2386
2387 return 0;
2388}
2389
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002390struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2391 const char *name)
2392{
2393 struct snd_card *card = soc_card->snd_card;
2394 struct snd_kcontrol *kctl;
2395
2396 if (unlikely(!name))
2397 return NULL;
2398
2399 list_for_each_entry(kctl, &card->controls, list)
2400 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2401 return kctl;
2402 return NULL;
2403}
2404EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2405
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002406/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002407 * snd_soc_add_codec_controls - add an array of controls to a codec.
2408 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002409 * duplicating this code.
2410 *
2411 * @codec: codec to add controls to
2412 * @controls: array of controls to add
2413 * @num_controls: number of elements in the array
2414 *
2415 * Return 0 for success, else error.
2416 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002417int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002418 const struct snd_kcontrol_new *controls, int num_controls)
2419{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002420 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002421
Liam Girdwood022658b2012-02-03 17:43:09 +00002422 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002423 codec->component.name_prefix, &codec->component);
Ian Molton3e8e1952009-01-09 00:23:21 +00002424}
Liam Girdwood022658b2012-02-03 17:43:09 +00002425EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002426
2427/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002428 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002429 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002430 *
2431 * @platform: platform to add controls to
2432 * @controls: array of controls to add
2433 * @num_controls: number of elements in the array
2434 *
2435 * Return 0 for success, else error.
2436 */
2437int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2438 const struct snd_kcontrol_new *controls, int num_controls)
2439{
2440 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002441
Liam Girdwood022658b2012-02-03 17:43:09 +00002442 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002443 NULL, &platform->component);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002444}
2445EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2446
2447/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002448 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2449 * Convenience function to add a list of controls.
2450 *
2451 * @soc_card: SoC card to add controls to
2452 * @controls: array of controls to add
2453 * @num_controls: number of elements in the array
2454 *
2455 * Return 0 for success, else error.
2456 */
2457int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2458 const struct snd_kcontrol_new *controls, int num_controls)
2459{
2460 struct snd_card *card = soc_card->snd_card;
2461
2462 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2463 NULL, soc_card);
2464}
2465EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2466
2467/**
2468 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2469 * Convienience function to add a list of controls.
2470 *
2471 * @dai: DAI to add controls to
2472 * @controls: array of controls to add
2473 * @num_controls: number of elements in the array
2474 *
2475 * Return 0 for success, else error.
2476 */
2477int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2478 const struct snd_kcontrol_new *controls, int num_controls)
2479{
2480 struct snd_card *card = dai->card->snd_card;
2481
2482 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2483 NULL, dai);
2484}
2485EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2486
2487/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002488 * snd_soc_info_enum_double - enumerated double mixer info callback
2489 * @kcontrol: mixer control
2490 * @uinfo: control element information
2491 *
2492 * Callback to provide information about a double enumerated
2493 * mixer control.
2494 *
2495 * Returns 0 for success.
2496 */
2497int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2498 struct snd_ctl_elem_info *uinfo)
2499{
2500 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2501
2502 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2503 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002504 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002505
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002506 if (uinfo->value.enumerated.item >= e->items)
2507 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002508 strlcpy(uinfo->value.enumerated.name,
2509 e->texts[uinfo->value.enumerated.item],
2510 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002511 return 0;
2512}
2513EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2514
2515/**
2516 * snd_soc_get_enum_double - enumerated double mixer get callback
2517 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002518 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002519 *
2520 * Callback to get the value of a double enumerated mixer.
2521 *
2522 * Returns 0 for success.
2523 */
2524int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2525 struct snd_ctl_elem_value *ucontrol)
2526{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002527 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002528 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002529 unsigned int val, item;
2530 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002531 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002532
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002533 ret = snd_soc_component_read(component, e->reg, &reg_val);
2534 if (ret)
2535 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002536 val = (reg_val >> e->shift_l) & e->mask;
2537 item = snd_soc_enum_val_to_item(e, val);
2538 ucontrol->value.enumerated.item[0] = item;
2539 if (e->shift_l != e->shift_r) {
2540 val = (reg_val >> e->shift_l) & e->mask;
2541 item = snd_soc_enum_val_to_item(e, val);
2542 ucontrol->value.enumerated.item[1] = item;
2543 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002544
2545 return 0;
2546}
2547EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2548
2549/**
2550 * snd_soc_put_enum_double - enumerated double mixer put callback
2551 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002552 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002553 *
2554 * Callback to set the value of a double enumerated mixer.
2555 *
2556 * Returns 0 for success.
2557 */
2558int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2559 struct snd_ctl_elem_value *ucontrol)
2560{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002561 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002562 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002563 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002564 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002565 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002566
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002567 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002568 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002569 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002570 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002571 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002572 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002573 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002574 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002575 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002576 }
2577
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002578 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002579}
2580EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2581
2582/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002583 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002584 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002585 * @reg: Register to read
2586 * @mask: Mask to use after shifting the register value
2587 * @shift: Right shift of register value
2588 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002589 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002590 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002591 * This functions reads a codec register. The register value is shifted right
2592 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2593 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002594 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002595 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002596 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002597static int snd_soc_read_signed(struct snd_soc_component *component,
2598 unsigned int reg, unsigned int mask, unsigned int shift,
2599 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002600{
Markus Pargmannf227b882014-01-16 16:02:10 +01002601 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002602 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002603
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002604 ret = snd_soc_component_read(component, reg, &val);
2605 if (ret < 0)
2606 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002607
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002608 val = (val >> shift) & mask;
2609
2610 if (!sign_bit) {
2611 *signed_val = val;
2612 return 0;
2613 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002614
2615 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002616 if (!(val & BIT(sign_bit))) {
2617 *signed_val = val;
2618 return 0;
2619 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002620
2621 ret = val;
2622
2623 /*
2624 * The register most probably does not contain a full-sized int.
2625 * Instead we have an arbitrary number of bits in a signed
2626 * representation which has to be translated into a full-sized int.
2627 * This is done by filling up all bits above the sign-bit.
2628 */
2629 ret |= ~((int)(BIT(sign_bit) - 1));
2630
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002631 *signed_val = ret;
2632
2633 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002634}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002635
2636/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002637 * snd_soc_info_volsw - single mixer info callback
2638 * @kcontrol: mixer control
2639 * @uinfo: control element information
2640 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002641 * Callback to provide information about a single mixer control, or a double
2642 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002643 *
2644 * Returns 0 for success.
2645 */
2646int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2647 struct snd_ctl_elem_info *uinfo)
2648{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002649 struct soc_mixer_control *mc =
2650 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002651 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002652
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002653 if (!mc->platform_max)
2654 mc->platform_max = mc->max;
2655 platform_max = mc->platform_max;
2656
2657 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002658 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2659 else
2660 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2661
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002662 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002663 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002664 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002665 return 0;
2666}
2667EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2668
2669/**
2670 * snd_soc_get_volsw - single mixer get callback
2671 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002672 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002673 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002674 * Callback to get the value of a single mixer control, or a double mixer
2675 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002676 *
2677 * Returns 0 for success.
2678 */
2679int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2680 struct snd_ctl_elem_value *ucontrol)
2681{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002682 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002683 struct soc_mixer_control *mc =
2684 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002685 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002686 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002687 unsigned int shift = mc->shift;
2688 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002689 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002690 int min = mc->min;
2691 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002692 unsigned int mask = (1 << fls(max)) - 1;
2693 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002694 int val;
2695 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002696
Markus Pargmannf227b882014-01-16 16:02:10 +01002697 if (sign_bit)
2698 mask = BIT(sign_bit + 1) - 1;
2699
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002700 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2701 if (ret)
2702 return ret;
2703
2704 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002705 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002706 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002707 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002708
2709 if (snd_soc_volsw_is_stereo(mc)) {
2710 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002711 ret = snd_soc_read_signed(component, reg, mask, rshift,
2712 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002713 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002714 ret = snd_soc_read_signed(component, reg2, mask, shift,
2715 sign_bit, &val);
2716 if (ret)
2717 return ret;
2718
2719 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002720 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002721 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002722 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002723 }
2724
2725 return 0;
2726}
2727EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2728
2729/**
2730 * snd_soc_put_volsw - single mixer put callback
2731 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002732 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002733 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002734 * Callback to set the value of a single mixer control, or a double mixer
2735 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002736 *
2737 * Returns 0 for success.
2738 */
2739int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2740 struct snd_ctl_elem_value *ucontrol)
2741{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002742 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002743 struct soc_mixer_control *mc =
2744 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002745 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002746 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002747 unsigned int shift = mc->shift;
2748 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002749 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002750 int min = mc->min;
2751 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002752 unsigned int mask = (1 << fls(max)) - 1;
2753 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002754 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002755 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002756 unsigned int val2 = 0;
2757 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002758
Markus Pargmannf227b882014-01-16 16:02:10 +01002759 if (sign_bit)
2760 mask = BIT(sign_bit + 1) - 1;
2761
2762 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002763 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002764 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002765 val_mask = mask << shift;
2766 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002767 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002768 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002769 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002770 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002771 if (reg == reg2) {
2772 val_mask |= mask << rshift;
2773 val |= val2 << rshift;
2774 } else {
2775 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002776 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002777 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002778 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002779 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002780 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002781 return err;
2782
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002783 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002784 err = snd_soc_component_update_bits(component, reg2, val_mask,
2785 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002786
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002787 return err;
2788}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002789EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002790
Mark Browne13ac2e2008-05-28 17:58:05 +01002791/**
Brian Austin1d99f242012-03-30 10:43:55 -05002792 * snd_soc_get_volsw_sx - single mixer get callback
2793 * @kcontrol: mixer control
2794 * @ucontrol: control element information
2795 *
2796 * Callback to get the value of a single mixer control, or a double mixer
2797 * control that spans 2 registers.
2798 *
2799 * Returns 0 for success.
2800 */
2801int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2802 struct snd_ctl_elem_value *ucontrol)
2803{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002804 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002805 struct soc_mixer_control *mc =
2806 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002807 unsigned int reg = mc->reg;
2808 unsigned int reg2 = mc->rreg;
2809 unsigned int shift = mc->shift;
2810 unsigned int rshift = mc->rshift;
2811 int max = mc->max;
2812 int min = mc->min;
2813 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002814 unsigned int val;
2815 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002816
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002817 ret = snd_soc_component_read(component, reg, &val);
2818 if (ret < 0)
2819 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002820
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002821 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2822
2823 if (snd_soc_volsw_is_stereo(mc)) {
2824 ret = snd_soc_component_read(component, reg2, &val);
2825 if (ret < 0)
2826 return ret;
2827
2828 val = ((val >> rshift) - min) & mask;
2829 ucontrol->value.integer.value[1] = val;
2830 }
Brian Austin1d99f242012-03-30 10:43:55 -05002831
2832 return 0;
2833}
2834EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2835
2836/**
2837 * snd_soc_put_volsw_sx - double mixer set callback
2838 * @kcontrol: mixer control
2839 * @uinfo: control element information
2840 *
2841 * Callback to set the value of a double mixer control that spans 2 registers.
2842 *
2843 * Returns 0 for success.
2844 */
2845int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2846 struct snd_ctl_elem_value *ucontrol)
2847{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002848 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002849 struct soc_mixer_control *mc =
2850 (struct soc_mixer_control *)kcontrol->private_value;
2851
2852 unsigned int reg = mc->reg;
2853 unsigned int reg2 = mc->rreg;
2854 unsigned int shift = mc->shift;
2855 unsigned int rshift = mc->rshift;
2856 int max = mc->max;
2857 int min = mc->min;
2858 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002859 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002860 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002861
2862 val_mask = mask << shift;
2863 val = (ucontrol->value.integer.value[0] + min) & mask;
2864 val = val << shift;
2865
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002866 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302867 if (err < 0)
2868 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002869
2870 if (snd_soc_volsw_is_stereo(mc)) {
2871 val_mask = mask << rshift;
2872 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2873 val2 = val2 << rshift;
2874
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002875 err = snd_soc_component_update_bits(component, reg2, val_mask,
2876 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002877 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002878 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002879}
2880EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2881
2882/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002883 * snd_soc_info_volsw_s8 - signed mixer info callback
2884 * @kcontrol: mixer control
2885 * @uinfo: control element information
2886 *
2887 * Callback to provide information about a signed mixer control.
2888 *
2889 * Returns 0 for success.
2890 */
2891int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2892 struct snd_ctl_elem_info *uinfo)
2893{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002894 struct soc_mixer_control *mc =
2895 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002896 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002897 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002898
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002899 if (!mc->platform_max)
2900 mc->platform_max = mc->max;
2901 platform_max = mc->platform_max;
2902
Mark Browne13ac2e2008-05-28 17:58:05 +01002903 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2904 uinfo->count = 2;
2905 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002906 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002907 return 0;
2908}
2909EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2910
2911/**
2912 * snd_soc_get_volsw_s8 - signed mixer get callback
2913 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002914 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002915 *
2916 * Callback to get the value of a signed mixer control.
2917 *
2918 * Returns 0 for success.
2919 */
2920int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2921 struct snd_ctl_elem_value *ucontrol)
2922{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002923 struct soc_mixer_control *mc =
2924 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002925 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002926 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002927 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002928 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002929 int ret;
2930
2931 ret = snd_soc_component_read(component, reg, &val);
2932 if (ret)
2933 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002934
2935 ucontrol->value.integer.value[0] =
2936 ((signed char)(val & 0xff))-min;
2937 ucontrol->value.integer.value[1] =
2938 ((signed char)((val >> 8) & 0xff))-min;
2939 return 0;
2940}
2941EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2942
2943/**
2944 * snd_soc_put_volsw_sgn - signed mixer put callback
2945 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002946 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002947 *
2948 * Callback to set the value of a signed mixer control.
2949 *
2950 * Returns 0 for success.
2951 */
2952int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2953 struct snd_ctl_elem_value *ucontrol)
2954{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002955 struct soc_mixer_control *mc =
2956 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002957 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002958 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002959 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002960 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002961
2962 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2963 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2964
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002965 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002966}
2967EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2968
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002969/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002970 * snd_soc_info_volsw_range - single mixer info callback with range.
2971 * @kcontrol: mixer control
2972 * @uinfo: control element information
2973 *
2974 * Callback to provide information, within a range, about a single
2975 * mixer control.
2976 *
2977 * returns 0 for success.
2978 */
2979int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2980 struct snd_ctl_elem_info *uinfo)
2981{
2982 struct soc_mixer_control *mc =
2983 (struct soc_mixer_control *)kcontrol->private_value;
2984 int platform_max;
2985 int min = mc->min;
2986
2987 if (!mc->platform_max)
2988 mc->platform_max = mc->max;
2989 platform_max = mc->platform_max;
2990
2991 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002992 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002993 uinfo->value.integer.min = 0;
2994 uinfo->value.integer.max = platform_max - min;
2995
2996 return 0;
2997}
2998EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2999
3000/**
3001 * snd_soc_put_volsw_range - single mixer put value callback with range.
3002 * @kcontrol: mixer control
3003 * @ucontrol: control element information
3004 *
3005 * Callback to set the value, within a range, for a single mixer control.
3006 *
3007 * Returns 0 for success.
3008 */
3009int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
3010 struct snd_ctl_elem_value *ucontrol)
3011{
3012 struct soc_mixer_control *mc =
3013 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003014 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003015 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003016 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003017 unsigned int shift = mc->shift;
3018 int min = mc->min;
3019 int max = mc->max;
3020 unsigned int mask = (1 << fls(max)) - 1;
3021 unsigned int invert = mc->invert;
3022 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003023 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003024
3025 val = ((ucontrol->value.integer.value[0] + min) & mask);
3026 if (invert)
3027 val = max - val;
3028 val_mask = mask << shift;
3029 val = val << shift;
3030
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003031 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09003032 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00003033 return ret;
3034
3035 if (snd_soc_volsw_is_stereo(mc)) {
3036 val = ((ucontrol->value.integer.value[1] + min) & mask);
3037 if (invert)
3038 val = max - val;
3039 val_mask = mask << shift;
3040 val = val << shift;
3041
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003042 ret = snd_soc_component_update_bits(component, rreg, val_mask,
3043 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00003044 }
3045
3046 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003047}
3048EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3049
3050/**
3051 * snd_soc_get_volsw_range - single mixer get callback with range
3052 * @kcontrol: mixer control
3053 * @ucontrol: control element information
3054 *
3055 * Callback to get the value, within a range, of a single mixer control.
3056 *
3057 * Returns 0 for success.
3058 */
3059int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3060 struct snd_ctl_elem_value *ucontrol)
3061{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003062 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003063 struct soc_mixer_control *mc =
3064 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003065 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003066 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003067 unsigned int shift = mc->shift;
3068 int min = mc->min;
3069 int max = mc->max;
3070 unsigned int mask = (1 << fls(max)) - 1;
3071 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003072 unsigned int val;
3073 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003074
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003075 ret = snd_soc_component_read(component, reg, &val);
3076 if (ret)
3077 return ret;
3078
3079 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003080 if (invert)
3081 ucontrol->value.integer.value[0] =
3082 max - ucontrol->value.integer.value[0];
3083 ucontrol->value.integer.value[0] =
3084 ucontrol->value.integer.value[0] - min;
3085
Mark Brown9bde4f02012-12-19 16:05:00 +00003086 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003087 ret = snd_soc_component_read(component, rreg, &val);
3088 if (ret)
3089 return ret;
3090
3091 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003092 if (invert)
3093 ucontrol->value.integer.value[1] =
3094 max - ucontrol->value.integer.value[1];
3095 ucontrol->value.integer.value[1] =
3096 ucontrol->value.integer.value[1] - min;
3097 }
3098
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003099 return 0;
3100}
3101EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3102
3103/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003104 * snd_soc_limit_volume - Set new limit to an existing volume control.
3105 *
3106 * @codec: where to look for the control
3107 * @name: Name of the control
3108 * @max: new maximum limit
3109 *
3110 * Return 0 for success, else error.
3111 */
3112int snd_soc_limit_volume(struct snd_soc_codec *codec,
3113 const char *name, int max)
3114{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003115 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003116 struct snd_kcontrol *kctl;
3117 struct soc_mixer_control *mc;
3118 int found = 0;
3119 int ret = -EINVAL;
3120
3121 /* Sanity check for name and max */
3122 if (unlikely(!name || max <= 0))
3123 return -EINVAL;
3124
3125 list_for_each_entry(kctl, &card->controls, list) {
3126 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3127 found = 1;
3128 break;
3129 }
3130 }
3131 if (found) {
3132 mc = (struct soc_mixer_control *)kctl->private_value;
3133 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003134 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003135 ret = 0;
3136 }
3137 }
3138 return ret;
3139}
3140EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3141
Mark Brown71d08512011-10-10 18:31:26 +01003142int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3143 struct snd_ctl_elem_info *uinfo)
3144{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003145 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003146 struct soc_bytes *params = (void *)kcontrol->private_value;
3147
3148 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003149 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003150
3151 return 0;
3152}
3153EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3154
3155int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3156 struct snd_ctl_elem_value *ucontrol)
3157{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003158 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003159 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003160 int ret;
3161
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003162 if (component->regmap)
3163 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003164 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003165 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003166 else
3167 ret = -EINVAL;
3168
Mark Brownf831b052012-02-17 16:20:33 -08003169 /* Hide any masked bytes to ensure consistent data reporting */
3170 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003171 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003172 case 1:
3173 ucontrol->value.bytes.data[0] &= ~params->mask;
3174 break;
3175 case 2:
3176 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003177 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003178 break;
3179 case 4:
3180 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003181 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003182 break;
3183 default:
3184 return -EINVAL;
3185 }
3186 }
3187
Mark Brown71d08512011-10-10 18:31:26 +01003188 return ret;
3189}
3190EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3191
3192int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3193 struct snd_ctl_elem_value *ucontrol)
3194{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003195 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003196 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003197 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003198 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003199 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003200
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003201 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003202 return -EINVAL;
3203
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003204 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003205
Mark Brownb5a8fe42013-01-20 21:42:22 +09003206 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3207 if (!data)
3208 return -ENOMEM;
3209
Mark Brownf831b052012-02-17 16:20:33 -08003210 /*
3211 * If we've got a mask then we need to preserve the register
3212 * bits. We shouldn't modify the incoming data so take a
3213 * copy.
3214 */
3215 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003216 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003217 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003218 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003219
3220 val &= params->mask;
3221
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003222 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003223 case 1:
3224 ((u8 *)data)[0] &= ~params->mask;
3225 ((u8 *)data)[0] |= val;
3226 break;
3227 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003228 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003229 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003230 &mask, &mask);
3231 if (ret != 0)
3232 goto out;
3233
3234 ((u16 *)data)[0] &= mask;
3235
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003236 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003237 &val, &val);
3238 if (ret != 0)
3239 goto out;
3240
3241 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003242 break;
3243 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003244 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003245 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003246 &mask, &mask);
3247 if (ret != 0)
3248 goto out;
3249
3250 ((u32 *)data)[0] &= mask;
3251
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003252 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003253 &val, &val);
3254 if (ret != 0)
3255 goto out;
3256
3257 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003258 break;
3259 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003260 ret = -EINVAL;
3261 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003262 }
3263 }
3264
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003265 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003266 data, len);
3267
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003268out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003269 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003270
3271 return ret;
3272}
3273EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3274
Vinod Kould9881202014-05-02 10:58:11 +05303275int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3276 struct snd_ctl_elem_info *ucontrol)
3277{
3278 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3279
3280 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3281 ucontrol->count = params->max;
3282
3283 return 0;
3284}
3285EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3286
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003287/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003288 * snd_soc_info_xr_sx - signed multi register info callback
3289 * @kcontrol: mreg control
3290 * @uinfo: control element information
3291 *
3292 * Callback to provide information of a control that can
3293 * span multiple codec registers which together
3294 * forms a single signed value in a MSB/LSB manner.
3295 *
3296 * Returns 0 for success.
3297 */
3298int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3299 struct snd_ctl_elem_info *uinfo)
3300{
3301 struct soc_mreg_control *mc =
3302 (struct soc_mreg_control *)kcontrol->private_value;
3303 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3304 uinfo->count = 1;
3305 uinfo->value.integer.min = mc->min;
3306 uinfo->value.integer.max = mc->max;
3307
3308 return 0;
3309}
3310EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3311
3312/**
3313 * snd_soc_get_xr_sx - signed multi register get callback
3314 * @kcontrol: mreg control
3315 * @ucontrol: control element information
3316 *
3317 * Callback to get the value of a control that can span
3318 * multiple codec registers which together forms a single
3319 * signed value in a MSB/LSB manner. The control supports
3320 * specifying total no of bits used to allow for bitfields
3321 * across the multiple codec registers.
3322 *
3323 * Returns 0 for success.
3324 */
3325int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3326 struct snd_ctl_elem_value *ucontrol)
3327{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003328 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003329 struct soc_mreg_control *mc =
3330 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003331 unsigned int regbase = mc->regbase;
3332 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003333 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003334 unsigned int regwmask = (1<<regwshift)-1;
3335 unsigned int invert = mc->invert;
3336 unsigned long mask = (1UL<<mc->nbits)-1;
3337 long min = mc->min;
3338 long max = mc->max;
3339 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003340 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003341 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003342 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003343
3344 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003345 ret = snd_soc_component_read(component, regbase+i, &regval);
3346 if (ret)
3347 return ret;
3348 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003349 }
3350 val &= mask;
3351 if (min < 0 && val > max)
3352 val |= ~mask;
3353 if (invert)
3354 val = max - val;
3355 ucontrol->value.integer.value[0] = val;
3356
3357 return 0;
3358}
3359EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3360
3361/**
3362 * snd_soc_put_xr_sx - signed multi register get callback
3363 * @kcontrol: mreg control
3364 * @ucontrol: control element information
3365 *
3366 * Callback to set the value of a control that can span
3367 * multiple codec registers which together forms a single
3368 * signed value in a MSB/LSB manner. The control supports
3369 * specifying total no of bits used to allow for bitfields
3370 * across the multiple codec registers.
3371 *
3372 * Returns 0 for success.
3373 */
3374int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3375 struct snd_ctl_elem_value *ucontrol)
3376{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003377 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003378 struct soc_mreg_control *mc =
3379 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003380 unsigned int regbase = mc->regbase;
3381 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003382 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003383 unsigned int regwmask = (1<<regwshift)-1;
3384 unsigned int invert = mc->invert;
3385 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003386 long max = mc->max;
3387 long val = ucontrol->value.integer.value[0];
3388 unsigned int i, regval, regmask;
3389 int err;
3390
3391 if (invert)
3392 val = max - val;
3393 val &= mask;
3394 for (i = 0; i < regcount; i++) {
3395 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3396 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003397 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003398 regmask, regval);
3399 if (err < 0)
3400 return err;
3401 }
3402
3403 return 0;
3404}
3405EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3406
3407/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003408 * snd_soc_get_strobe - strobe get callback
3409 * @kcontrol: mixer control
3410 * @ucontrol: control element information
3411 *
3412 * Callback get the value of a strobe mixer control.
3413 *
3414 * Returns 0 for success.
3415 */
3416int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3417 struct snd_ctl_elem_value *ucontrol)
3418{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003419 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003420 struct soc_mixer_control *mc =
3421 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003422 unsigned int reg = mc->reg;
3423 unsigned int shift = mc->shift;
3424 unsigned int mask = 1 << shift;
3425 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003426 unsigned int val;
3427 int ret;
3428
3429 ret = snd_soc_component_read(component, reg, &val);
3430 if (ret)
3431 return ret;
3432
3433 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003434
3435 if (shift != 0 && val != 0)
3436 val = val >> shift;
3437 ucontrol->value.enumerated.item[0] = val ^ invert;
3438
3439 return 0;
3440}
3441EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3442
3443/**
3444 * snd_soc_put_strobe - strobe put callback
3445 * @kcontrol: mixer control
3446 * @ucontrol: control element information
3447 *
3448 * Callback strobe a register bit to high then low (or the inverse)
3449 * in one pass of a single mixer enum control.
3450 *
3451 * Returns 1 for success.
3452 */
3453int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3454 struct snd_ctl_elem_value *ucontrol)
3455{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003456 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003457 struct soc_mixer_control *mc =
3458 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003459 unsigned int reg = mc->reg;
3460 unsigned int shift = mc->shift;
3461 unsigned int mask = 1 << shift;
3462 unsigned int invert = mc->invert != 0;
3463 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3464 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3465 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3466 int err;
3467
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003468 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003469 if (err < 0)
3470 return err;
3471
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003472 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003473}
3474EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3475
3476/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003477 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3478 * @dai: DAI
3479 * @clk_id: DAI specific clock ID
3480 * @freq: new clock frequency in Hz
3481 * @dir: new clock direction - input/output.
3482 *
3483 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3484 */
3485int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3486 unsigned int freq, int dir)
3487{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003488 if (dai->driver && dai->driver->ops->set_sysclk)
3489 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003490 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003491 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003492 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003493 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003494 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003495}
3496EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3497
3498/**
Mark Brownec4ee522011-03-07 20:58:11 +00003499 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3500 * @codec: CODEC
3501 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003502 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003503 * @freq: new clock frequency in Hz
3504 * @dir: new clock direction - input/output.
3505 *
3506 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3507 */
3508int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003509 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003510{
3511 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003512 return codec->driver->set_sysclk(codec, clk_id, source,
3513 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003514 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003515 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003516}
3517EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3518
3519/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003520 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3521 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003522 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003523 * @div: new clock divisor.
3524 *
3525 * Configures the clock dividers. This is used to derive the best DAI bit and
3526 * frame clocks from the system or master clock. It's best to set the DAI bit
3527 * and frame clocks as low as possible to save system power.
3528 */
3529int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3530 int div_id, int div)
3531{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003532 if (dai->driver && dai->driver->ops->set_clkdiv)
3533 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003534 else
3535 return -EINVAL;
3536}
3537EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3538
3539/**
3540 * snd_soc_dai_set_pll - configure DAI PLL.
3541 * @dai: DAI
3542 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003543 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003544 * @freq_in: PLL input clock frequency in Hz
3545 * @freq_out: requested PLL output clock frequency in Hz
3546 *
3547 * Configures and enables PLL to generate output clock based on input clock.
3548 */
Mark Brown85488032009-09-05 18:52:16 +01003549int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3550 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003551{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003552 if (dai->driver && dai->driver->ops->set_pll)
3553 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003554 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003555 else if (dai->codec && dai->codec->driver->set_pll)
3556 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3557 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003558 else
3559 return -EINVAL;
3560}
3561EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3562
Mark Brownec4ee522011-03-07 20:58:11 +00003563/*
3564 * snd_soc_codec_set_pll - configure codec PLL.
3565 * @codec: CODEC
3566 * @pll_id: DAI specific PLL ID
3567 * @source: DAI specific source for the PLL
3568 * @freq_in: PLL input clock frequency in Hz
3569 * @freq_out: requested PLL output clock frequency in Hz
3570 *
3571 * Configures and enables PLL to generate output clock based on input clock.
3572 */
3573int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3574 unsigned int freq_in, unsigned int freq_out)
3575{
3576 if (codec->driver->set_pll)
3577 return codec->driver->set_pll(codec, pll_id, source,
3578 freq_in, freq_out);
3579 else
3580 return -EINVAL;
3581}
3582EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3583
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003584/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003585 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3586 * @dai: DAI
3587 * @ratio Ratio of BCLK to Sample rate.
3588 *
3589 * Configures the DAI for a preset BCLK to sample rate ratio.
3590 */
3591int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3592{
3593 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3594 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3595 else
3596 return -EINVAL;
3597}
3598EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3599
3600/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003601 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3602 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003603 * @fmt: SND_SOC_DAIFMT_ format value.
3604 *
3605 * Configures the DAI hardware format and clocking.
3606 */
3607int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3608{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003609 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003610 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003611 if (dai->driver->ops->set_fmt == NULL)
3612 return -ENOTSUPP;
3613 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003614}
3615EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3616
3617/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003618 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003619 * @slots: Number of slots in use.
3620 * @tx_mask: bitmask representing active TX slots.
3621 * @rx_mask: bitmask representing active RX slots.
3622 *
3623 * Generates the TDM tx and rx slot default masks for DAI.
3624 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003625static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003626 unsigned int *tx_mask,
3627 unsigned int *rx_mask)
3628{
3629 if (*tx_mask || *rx_mask)
3630 return 0;
3631
3632 if (!slots)
3633 return -EINVAL;
3634
3635 *tx_mask = (1 << slots) - 1;
3636 *rx_mask = (1 << slots) - 1;
3637
3638 return 0;
3639}
3640
3641/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003642 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3643 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003644 * @tx_mask: bitmask representing active TX slots.
3645 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003646 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003647 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003648 *
3649 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3650 * specific.
3651 */
3652int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003653 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003654{
Xiubo Lie5c21512014-03-21 14:17:12 +08003655 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3656 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003657 &tx_mask, &rx_mask);
3658 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003659 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003660
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003661 if (dai->driver && dai->driver->ops->set_tdm_slot)
3662 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003663 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003664 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003665 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003666}
3667EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3668
3669/**
Barry Song472df3c2009-09-12 01:16:29 +08003670 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3671 * @dai: DAI
3672 * @tx_num: how many TX channels
3673 * @tx_slot: pointer to an array which imply the TX slot number channel
3674 * 0~num-1 uses
3675 * @rx_num: how many RX channels
3676 * @rx_slot: pointer to an array which imply the RX slot number channel
3677 * 0~num-1 uses
3678 *
3679 * configure the relationship between channel number and TDM slot number.
3680 */
3681int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3682 unsigned int tx_num, unsigned int *tx_slot,
3683 unsigned int rx_num, unsigned int *rx_slot)
3684{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003685 if (dai->driver && dai->driver->ops->set_channel_map)
3686 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003687 rx_num, rx_slot);
3688 else
3689 return -EINVAL;
3690}
3691EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3692
3693/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003694 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3695 * @dai: DAI
3696 * @tristate: tristate enable
3697 *
3698 * Tristates the DAI so that others can use it.
3699 */
3700int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3701{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003702 if (dai->driver && dai->driver->ops->set_tristate)
3703 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003704 else
3705 return -EINVAL;
3706}
3707EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3708
3709/**
3710 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3711 * @dai: DAI
3712 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003713 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003714 *
3715 * Mutes the DAI DAC.
3716 */
Mark Brownda183962013-02-06 15:44:07 +00003717int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3718 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003719{
Mark Brownda183962013-02-06 15:44:07 +00003720 if (!dai->driver)
3721 return -ENOTSUPP;
3722
3723 if (dai->driver->ops->mute_stream)
3724 return dai->driver->ops->mute_stream(dai, mute, direction);
3725 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3726 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003727 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003728 else
Mark Brown04570c62012-04-13 19:16:03 +01003729 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003730}
3731EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3732
Mark Brownc5af3a22008-11-28 13:29:45 +00003733/**
3734 * snd_soc_register_card - Register a card with the ASoC core
3735 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003736 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003737 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003738 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303739int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003740{
Mark Brownb19e6e72012-03-14 21:18:39 +00003741 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003742
Mark Brownc5af3a22008-11-28 13:29:45 +00003743 if (!card->name || !card->dev)
3744 return -EINVAL;
3745
Stephen Warren5a504962011-12-21 10:40:59 -07003746 for (i = 0; i < card->num_links; i++) {
3747 struct snd_soc_dai_link *link = &card->dai_link[i];
3748
3749 /*
3750 * Codec must be specified by 1 of name or OF node,
3751 * not both or neither.
3752 */
3753 if (!!link->codec_name == !!link->codec_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003754 dev_err(card->dev,
3755 "ASoC: Neither/both codec name/of_node are set for %s\n",
3756 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003757 return -EINVAL;
3758 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003759 /* Codec DAI name must be specified */
3760 if (!link->codec_dai_name) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003761 dev_err(card->dev,
3762 "ASoC: codec_dai_name not set for %s\n",
3763 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003764 return -EINVAL;
3765 }
Stephen Warren5a504962011-12-21 10:40:59 -07003766
3767 /*
3768 * Platform may be specified by either name or OF node, but
3769 * can be left unspecified, and a dummy platform will be used.
3770 */
3771 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003772 dev_err(card->dev,
3773 "ASoC: Both platform name/of_node are set for %s\n",
3774 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003775 return -EINVAL;
3776 }
3777
3778 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003779 * CPU device may be specified by either name or OF node, but
3780 * can be left unspecified, and will be matched based on DAI
3781 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003782 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003783 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003784 dev_err(card->dev,
3785 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3786 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003787 return -EINVAL;
3788 }
3789 /*
3790 * At least one of CPU DAI name or CPU device name/node must be
3791 * specified
3792 */
3793 if (!link->cpu_dai_name &&
3794 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003795 dev_err(card->dev,
3796 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3797 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003798 return -EINVAL;
3799 }
3800 }
3801
Mark Browned77cc12011-05-03 18:25:34 +01003802 dev_set_drvdata(card->dev, card);
3803
Stephen Warren111c6412011-01-28 14:26:35 -07003804 snd_soc_initialize_card_lists(card);
3805
Vinod Koul150dd2f2011-01-13 22:48:32 +05303806 soc_init_card_debugfs(card);
3807
Mark Brown181a6892012-03-14 20:18:49 +00003808 card->rtd = devm_kzalloc(card->dev,
3809 sizeof(struct snd_soc_pcm_runtime) *
3810 (card->num_links + card->num_aux_devs),
3811 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003812 if (card->rtd == NULL)
3813 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003814 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003815 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003816
3817 for (i = 0; i < card->num_links; i++)
3818 card->rtd[i].dai_link = &card->dai_link[i];
3819
Mark Browndb432b42011-10-03 21:06:40 +01003820 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003821 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003822 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003823 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003824
Mark Brownb19e6e72012-03-14 21:18:39 +00003825 ret = snd_soc_instantiate_card(card);
3826 if (ret != 0)
3827 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003828
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003829 /* deactivate pins to sleep state */
3830 for (i = 0; i < card->num_rtd; i++) {
3831 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3832 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3833 if (!codec_dai->active)
3834 pinctrl_pm_select_sleep_state(codec_dai->dev);
3835 if (!cpu_dai->active)
3836 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3837 }
3838
Mark Brownb19e6e72012-03-14 21:18:39 +00003839 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003840}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303841EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003842
3843/**
3844 * snd_soc_unregister_card - Unregister a card with the ASoC core
3845 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003846 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003847 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003848 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303849int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003850{
Vinod Koulb0e26482011-01-13 22:48:02 +05303851 if (card->instantiated)
3852 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003853 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003854
3855 return 0;
3856}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303857EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003858
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003859/*
3860 * Simplify DAI link configuration by removing ".-1" from device names
3861 * and sanitizing names.
3862 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003863static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003864{
3865 char *found, name[NAME_SIZE];
3866 int id1, id2;
3867
3868 if (dev_name(dev) == NULL)
3869 return NULL;
3870
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003871 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003872
3873 /* are we a "%s.%d" name (platform and SPI components) */
3874 found = strstr(name, dev->driver->name);
3875 if (found) {
3876 /* get ID */
3877 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3878
3879 /* discard ID from name if ID == -1 */
3880 if (*id == -1)
3881 found[strlen(dev->driver->name)] = '\0';
3882 }
3883
3884 } else {
3885 /* I2C component devices are named "bus-addr" */
3886 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3887 char tmp[NAME_SIZE];
3888
3889 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003890 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003891
3892 /* sanitize component name for DAI link creation */
3893 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003894 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003895 } else
3896 *id = 0;
3897 }
3898
3899 return kstrdup(name, GFP_KERNEL);
3900}
3901
3902/*
3903 * Simplify DAI link naming for single devices with multiple DAIs by removing
3904 * any ".-1" and using the DAI name (instead of device name).
3905 */
3906static inline char *fmt_multiple_name(struct device *dev,
3907 struct snd_soc_dai_driver *dai_drv)
3908{
3909 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003910 dev_err(dev,
3911 "ASoC: error - multiple DAI %s registered with no name\n",
3912 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003913 return NULL;
3914 }
3915
3916 return kstrdup(dai_drv->name, GFP_KERNEL);
3917}
3918
Mark Brown91151712008-11-30 23:31:24 +00003919/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003920 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003921 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003922 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003923 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003924static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003925{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003926 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003927
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003928 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003929 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3930 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003931 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003932 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003933 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003934 }
Mark Brown91151712008-11-30 23:31:24 +00003935}
Mark Brown91151712008-11-30 23:31:24 +00003936
3937/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003938 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003939 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003940 * @component: The component the DAIs are registered for
3941 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003942 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003943 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3944 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003945 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003946static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003947 struct snd_soc_dai_driver *dai_drv, size_t count,
3948 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003949{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003950 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003951 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003952 unsigned int i;
3953 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003954
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003955 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003956
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003957 component->dai_drv = dai_drv;
3958 component->num_dai = count;
3959
Mark Brown91151712008-11-30 23:31:24 +00003960 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003961
3962 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003963 if (dai == NULL) {
3964 ret = -ENOMEM;
3965 goto err;
3966 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003967
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003968 /*
3969 * Back in the old days when we still had component-less DAIs,
3970 * instead of having a static name, component-less DAIs would
3971 * inherit the name of the parent device so it is possible to
3972 * register multiple instances of the DAI. We still need to keep
3973 * the same naming style even though those DAIs are not
3974 * component-less anymore.
3975 */
3976 if (count == 1 && legacy_dai_naming) {
3977 dai->name = fmt_single_name(dev, &dai->id);
3978 } else {
3979 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3980 if (dai_drv[i].id)
3981 dai->id = dai_drv[i].id;
3982 else
3983 dai->id = i;
3984 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003985 if (dai->name == NULL) {
3986 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003987 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003988 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003989 }
3990
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003991 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003992 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003993 dai->driver = &dai_drv[i];
3994 if (!dai->driver->ops)
3995 dai->driver->ops = &null_dai_ops;
3996
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003997 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003998
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003999 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00004000 }
4001
4002 return 0;
4003
4004err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004005 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00004006
4007 return ret;
4008}
Mark Brown91151712008-11-30 23:31:24 +00004009
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004010static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
4011 enum snd_soc_dapm_type type, int subseq)
4012{
4013 struct snd_soc_component *component = dapm->component;
4014
4015 component->driver->seq_notifier(component, type, subseq);
4016}
4017
4018static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
4019 int event)
4020{
4021 struct snd_soc_component *component = dapm->component;
4022
4023 return component->driver->stream_event(component, event);
4024}
4025
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004026static int snd_soc_component_initialize(struct snd_soc_component *component,
4027 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004028{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004029 struct snd_soc_dapm_context *dapm;
4030
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004031 component->name = fmt_single_name(dev, &component->id);
4032 if (!component->name) {
4033 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004034 return -ENOMEM;
4035 }
4036
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004037 component->dev = dev;
4038 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004039
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004040 if (!component->dapm_ptr)
4041 component->dapm_ptr = &component->dapm;
4042
4043 dapm = component->dapm_ptr;
4044 dapm->dev = dev;
4045 dapm->component = component;
4046 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004047 if (driver->seq_notifier)
4048 dapm->seq_notifier = snd_soc_component_seq_notifier;
4049 if (driver->stream_event)
4050 dapm->stream_event = snd_soc_component_stream_event;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004051
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004052 INIT_LIST_HEAD(&component->dai_list);
4053 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004054
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004055 return 0;
4056}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004057
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004058static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4059{
4060 list_add(&component->list, &component_list);
4061}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004062
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004063static void snd_soc_component_add(struct snd_soc_component *component)
4064{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004065 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004066 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004067 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004068}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004069
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004070static void snd_soc_component_cleanup(struct snd_soc_component *component)
4071{
4072 snd_soc_unregister_dais(component);
4073 kfree(component->name);
4074}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004075
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004076static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4077{
4078 list_del(&component->list);
4079}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004080
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004081static void snd_soc_component_del(struct snd_soc_component *component)
4082{
4083 mutex_lock(&client_mutex);
4084 snd_soc_component_del_unlocked(component);
4085 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004086}
4087
4088int snd_soc_register_component(struct device *dev,
4089 const struct snd_soc_component_driver *cmpnt_drv,
4090 struct snd_soc_dai_driver *dai_drv,
4091 int num_dai)
4092{
4093 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004094 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004095
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004096 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004097 if (!cmpnt) {
4098 dev_err(dev, "ASoC: Failed to allocate memory\n");
4099 return -ENOMEM;
4100 }
4101
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004102 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4103 if (ret)
4104 goto err_free;
4105
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004106 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004107 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004108
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004109 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4110 if (ret < 0) {
4111 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4112 goto err_cleanup;
4113 }
4114
4115 snd_soc_component_add(cmpnt);
4116
4117 return 0;
4118
4119err_cleanup:
4120 snd_soc_component_cleanup(cmpnt);
4121err_free:
4122 kfree(cmpnt);
4123 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004124}
4125EXPORT_SYMBOL_GPL(snd_soc_register_component);
4126
4127/**
4128 * snd_soc_unregister_component - Unregister a component from the ASoC core
4129 *
4130 */
4131void snd_soc_unregister_component(struct device *dev)
4132{
4133 struct snd_soc_component *cmpnt;
4134
4135 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004136 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004137 goto found;
4138 }
4139 return;
4140
4141found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004142 snd_soc_component_del(cmpnt);
4143 snd_soc_component_cleanup(cmpnt);
4144 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004145}
4146EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4147
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004148static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4149 unsigned int reg, unsigned int val)
4150{
4151 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4152
4153 return platform->driver->write(platform, reg, val);
4154}
4155
4156static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4157 unsigned int reg, unsigned int *val)
4158{
4159 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4160
4161 *val = platform->driver->read(platform, reg);
4162
4163 return 0;
4164}
4165
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004166/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004167 * snd_soc_add_platform - Add a platform to the ASoC core
4168 * @dev: The parent device for the platform
4169 * @platform: The platform to add
4170 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004171 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004172int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57db82013-03-27 12:02:22 +01004173 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004174{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004175 int ret;
4176
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004177 ret = snd_soc_component_initialize(&platform->component,
4178 &platform_drv->component_driver, dev);
4179 if (ret)
4180 return ret;
4181
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004182 platform->dev = dev;
4183 platform->driver = platform_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004184 if (platform_drv->write)
4185 platform->component.write = snd_soc_platform_drv_write;
4186 if (platform_drv->read)
4187 platform->component.read = snd_soc_platform_drv_read;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004188
4189 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004190 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004191 list_add(&platform->list, &platform_list);
4192 mutex_unlock(&client_mutex);
4193
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004194 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4195 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004196
4197 return 0;
4198}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004199EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4200
4201/**
4202 * snd_soc_register_platform - Register a platform with the ASoC core
4203 *
4204 * @platform: platform to register
4205 */
4206int snd_soc_register_platform(struct device *dev,
4207 const struct snd_soc_platform_driver *platform_drv)
4208{
4209 struct snd_soc_platform *platform;
4210 int ret;
4211
4212 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4213
4214 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4215 if (platform == NULL)
4216 return -ENOMEM;
4217
4218 ret = snd_soc_add_platform(dev, platform, platform_drv);
4219 if (ret)
4220 kfree(platform);
4221
4222 return ret;
4223}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004224EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4225
4226/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004227 * snd_soc_remove_platform - Remove a platform from the ASoC core
4228 * @platform: the platform to remove
4229 */
4230void snd_soc_remove_platform(struct snd_soc_platform *platform)
4231{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004232
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004233 mutex_lock(&client_mutex);
4234 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004235 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004236 mutex_unlock(&client_mutex);
4237
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004238 snd_soc_component_cleanup(&platform->component);
4239
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004240 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004241 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004242}
4243EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4244
4245struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4246{
4247 struct snd_soc_platform *platform;
4248
4249 list_for_each_entry(platform, &platform_list, list) {
4250 if (dev == platform->dev)
4251 return platform;
4252 }
4253
4254 return NULL;
4255}
4256EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4257
4258/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004259 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4260 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004261 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004262 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004263void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004264{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004265 struct snd_soc_platform *platform;
4266
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004267 platform = snd_soc_lookup_platform(dev);
4268 if (!platform)
4269 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004270
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004271 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004272 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004273}
4274EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4275
Mark Brown151ab222009-05-09 16:22:58 +01004276static u64 codec_format_map[] = {
4277 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4278 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4279 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4280 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4281 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4282 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4283 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4284 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4285 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4286 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4287 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4288 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4289 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4290 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4291 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4292 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4293};
4294
4295/* Fix up the DAI formats for endianness: codecs don't actually see
4296 * the endianness of the data but we're using the CPU format
4297 * definitions which do need to include endianness so we ensure that
4298 * codec DAIs always have both big and little endian variants set.
4299 */
4300static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4301{
4302 int i;
4303
4304 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4305 if (stream->formats & codec_format_map[i])
4306 stream->formats |= codec_format_map[i];
4307}
4308
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004309static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4310 unsigned int reg, unsigned int val)
4311{
4312 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4313
4314 return codec->driver->write(codec, reg, val);
4315}
4316
4317static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4318 unsigned int reg, unsigned int *val)
4319{
4320 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4321
4322 *val = codec->driver->read(codec, reg);
4323
4324 return 0;
4325}
4326
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004327static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4328 enum snd_soc_bias_level level)
4329{
4330 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4331
4332 return codec->driver->set_bias_level(codec, level);
4333}
4334
Mark Brown0d0cf002008-12-10 14:32:45 +00004335/**
4336 * snd_soc_register_codec - Register a codec with the ASoC core
4337 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004338 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004339 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004340int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004341 const struct snd_soc_codec_driver *codec_drv,
4342 struct snd_soc_dai_driver *dai_drv,
4343 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004344{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004345 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004346 struct snd_soc_dai *dai;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004347 struct regmap *regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004348 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004349
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004350 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004351
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004352 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4353 if (codec == NULL)
4354 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004355
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004356 codec->component.dapm_ptr = &codec->dapm;
4357
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004358 ret = snd_soc_component_initialize(&codec->component,
4359 &codec_drv->component_driver, dev);
4360 if (ret)
4361 goto err_free;
4362
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004363 if (codec_drv->write)
4364 codec->component.write = snd_soc_codec_drv_write;
4365 if (codec_drv->read)
4366 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004367 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004368 codec->dapm.codec = codec;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004369 if (codec_drv->seq_notifier)
4370 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004371 if (codec_drv->set_bias_level)
4372 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004373 codec->dev = dev;
4374 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004375 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004376 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004377
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004378 if (!codec->component.write) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004379 if (codec_drv->get_regmap)
4380 regmap = codec_drv->get_regmap(dev);
4381 else
4382 regmap = dev_get_regmap(dev, NULL);
4383
4384 if (regmap) {
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004385 ret = snd_soc_component_init_io(&codec->component,
4386 regmap);
4387 if (ret) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004388 dev_err(codec->dev,
4389 "Failed to set cache I/O:%d\n",
4390 ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004391 goto err_cleanup;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004392 }
4393 }
4394 }
4395
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004396 for (i = 0; i < num_dai; i++) {
4397 fixup_codec_formats(&dai_drv[i].playback);
4398 fixup_codec_formats(&dai_drv[i].capture);
4399 }
4400
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004401 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4402 if (ret < 0) {
4403 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4404 goto err_cleanup;
4405 }
4406
4407 list_for_each_entry(dai, &codec->component.dai_list, list)
4408 dai->codec = codec;
4409
Mark Brown054880f2012-04-09 17:29:19 +01004410 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004411 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004412 list_add(&codec->list, &codec_list);
4413 mutex_unlock(&client_mutex);
4414
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004415 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4416 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004417 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004418
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004419err_cleanup:
4420 snd_soc_component_cleanup(&codec->component);
4421err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004422 kfree(codec);
4423 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004424}
4425EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4426
4427/**
4428 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4429 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004430 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004431 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004432void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004433{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004434 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004435
4436 list_for_each_entry(codec, &codec_list, list) {
4437 if (dev == codec->dev)
4438 goto found;
4439 }
4440 return;
4441
4442found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004443
Mark Brown0d0cf002008-12-10 14:32:45 +00004444 mutex_lock(&client_mutex);
4445 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004446 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004447 mutex_unlock(&client_mutex);
4448
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004449 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4450 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004451
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004452 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004453 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004454 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004455}
4456EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4457
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004458/* Retrieve a card's name from device tree */
4459int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4460 const char *propname)
4461{
4462 struct device_node *np = card->dev->of_node;
4463 int ret;
4464
4465 ret = of_property_read_string_index(np, propname, 0, &card->name);
4466 /*
4467 * EINVAL means the property does not exist. This is fine providing
4468 * card->name was previously set, which is checked later in
4469 * snd_soc_register_card.
4470 */
4471 if (ret < 0 && ret != -EINVAL) {
4472 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004473 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004474 propname, ret);
4475 return ret;
4476 }
4477
4478 return 0;
4479}
4480EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4481
Xiubo Li9a6d4862014-02-08 15:59:52 +08004482static const struct snd_soc_dapm_widget simple_widgets[] = {
4483 SND_SOC_DAPM_MIC("Microphone", NULL),
4484 SND_SOC_DAPM_LINE("Line", NULL),
4485 SND_SOC_DAPM_HP("Headphone", NULL),
4486 SND_SOC_DAPM_SPK("Speaker", NULL),
4487};
4488
4489int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4490 const char *propname)
4491{
4492 struct device_node *np = card->dev->of_node;
4493 struct snd_soc_dapm_widget *widgets;
4494 const char *template, *wname;
4495 int i, j, num_widgets, ret;
4496
4497 num_widgets = of_property_count_strings(np, propname);
4498 if (num_widgets < 0) {
4499 dev_err(card->dev,
4500 "ASoC: Property '%s' does not exist\n", propname);
4501 return -EINVAL;
4502 }
4503 if (num_widgets & 1) {
4504 dev_err(card->dev,
4505 "ASoC: Property '%s' length is not even\n", propname);
4506 return -EINVAL;
4507 }
4508
4509 num_widgets /= 2;
4510 if (!num_widgets) {
4511 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4512 propname);
4513 return -EINVAL;
4514 }
4515
4516 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4517 GFP_KERNEL);
4518 if (!widgets) {
4519 dev_err(card->dev,
4520 "ASoC: Could not allocate memory for widgets\n");
4521 return -ENOMEM;
4522 }
4523
4524 for (i = 0; i < num_widgets; i++) {
4525 ret = of_property_read_string_index(np, propname,
4526 2 * i, &template);
4527 if (ret) {
4528 dev_err(card->dev,
4529 "ASoC: Property '%s' index %d read error:%d\n",
4530 propname, 2 * i, ret);
4531 return -EINVAL;
4532 }
4533
4534 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4535 if (!strncmp(template, simple_widgets[j].name,
4536 strlen(simple_widgets[j].name))) {
4537 widgets[i] = simple_widgets[j];
4538 break;
4539 }
4540 }
4541
4542 if (j >= ARRAY_SIZE(simple_widgets)) {
4543 dev_err(card->dev,
4544 "ASoC: DAPM widget '%s' is not supported\n",
4545 template);
4546 return -EINVAL;
4547 }
4548
4549 ret = of_property_read_string_index(np, propname,
4550 (2 * i) + 1,
4551 &wname);
4552 if (ret) {
4553 dev_err(card->dev,
4554 "ASoC: Property '%s' index %d read error:%d\n",
4555 propname, (2 * i) + 1, ret);
4556 return -EINVAL;
4557 }
4558
4559 widgets[i].name = wname;
4560 }
4561
4562 card->dapm_widgets = widgets;
4563 card->num_dapm_widgets = num_widgets;
4564
4565 return 0;
4566}
4567EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4568
Xiubo Li89c67852014-02-14 09:34:35 +08004569int snd_soc_of_parse_tdm_slot(struct device_node *np,
4570 unsigned int *slots,
4571 unsigned int *slot_width)
4572{
4573 u32 val;
4574 int ret;
4575
4576 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4577 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4578 if (ret)
4579 return ret;
4580
4581 if (slots)
4582 *slots = val;
4583 }
4584
4585 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4586 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4587 if (ret)
4588 return ret;
4589
4590 if (slot_width)
4591 *slot_width = val;
4592 }
4593
4594 return 0;
4595}
4596EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4597
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004598int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4599 const char *propname)
4600{
4601 struct device_node *np = card->dev->of_node;
4602 int num_routes;
4603 struct snd_soc_dapm_route *routes;
4604 int i, ret;
4605
4606 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004607 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004608 dev_err(card->dev,
4609 "ASoC: Property '%s' does not exist or its length is not even\n",
4610 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004611 return -EINVAL;
4612 }
4613 num_routes /= 2;
4614 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004615 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004616 propname);
4617 return -EINVAL;
4618 }
4619
4620 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4621 GFP_KERNEL);
4622 if (!routes) {
4623 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004624 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004625 return -EINVAL;
4626 }
4627
4628 for (i = 0; i < num_routes; i++) {
4629 ret = of_property_read_string_index(np, propname,
4630 2 * i, &routes[i].sink);
4631 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004632 dev_err(card->dev,
4633 "ASoC: Property '%s' index %d could not be read: %d\n",
4634 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004635 return -EINVAL;
4636 }
4637 ret = of_property_read_string_index(np, propname,
4638 (2 * i) + 1, &routes[i].source);
4639 if (ret) {
4640 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004641 "ASoC: Property '%s' index %d could not be read: %d\n",
4642 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004643 return -EINVAL;
4644 }
4645 }
4646
4647 card->num_dapm_routes = num_routes;
4648 card->dapm_routes = routes;
4649
4650 return 0;
4651}
4652EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4653
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004654unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004655 const char *prefix,
4656 struct device_node **bitclkmaster,
4657 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004658{
4659 int ret, i;
4660 char prop[128];
4661 unsigned int format = 0;
4662 int bit, frame;
4663 const char *str;
4664 struct {
4665 char *name;
4666 unsigned int val;
4667 } of_fmt_table[] = {
4668 { "i2s", SND_SOC_DAIFMT_I2S },
4669 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4670 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4671 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4672 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4673 { "ac97", SND_SOC_DAIFMT_AC97 },
4674 { "pdm", SND_SOC_DAIFMT_PDM},
4675 { "msb", SND_SOC_DAIFMT_MSB },
4676 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004677 };
4678
4679 if (!prefix)
4680 prefix = "";
4681
4682 /*
4683 * check "[prefix]format = xxx"
4684 * SND_SOC_DAIFMT_FORMAT_MASK area
4685 */
4686 snprintf(prop, sizeof(prop), "%sformat", prefix);
4687 ret = of_property_read_string(np, prop, &str);
4688 if (ret == 0) {
4689 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4690 if (strcmp(str, of_fmt_table[i].name) == 0) {
4691 format |= of_fmt_table[i].val;
4692 break;
4693 }
4694 }
4695 }
4696
4697 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004698 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004699 * SND_SOC_DAIFMT_CLOCK_MASK area
4700 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004701 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4702 if (of_get_property(np, prop, NULL))
4703 format |= SND_SOC_DAIFMT_CONT;
4704 else
4705 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004706
4707 /*
4708 * check "[prefix]bitclock-inversion"
4709 * check "[prefix]frame-inversion"
4710 * SND_SOC_DAIFMT_INV_MASK area
4711 */
4712 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4713 bit = !!of_get_property(np, prop, NULL);
4714
4715 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4716 frame = !!of_get_property(np, prop, NULL);
4717
4718 switch ((bit << 4) + frame) {
4719 case 0x11:
4720 format |= SND_SOC_DAIFMT_IB_IF;
4721 break;
4722 case 0x10:
4723 format |= SND_SOC_DAIFMT_IB_NF;
4724 break;
4725 case 0x01:
4726 format |= SND_SOC_DAIFMT_NB_IF;
4727 break;
4728 default:
4729 /* SND_SOC_DAIFMT_NB_NF is default */
4730 break;
4731 }
4732
4733 /*
4734 * check "[prefix]bitclock-master"
4735 * check "[prefix]frame-master"
4736 * SND_SOC_DAIFMT_MASTER_MASK area
4737 */
4738 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4739 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004740 if (bit && bitclkmaster)
4741 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004742
4743 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4744 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004745 if (frame && framemaster)
4746 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004747
4748 switch ((bit << 4) + frame) {
4749 case 0x11:
4750 format |= SND_SOC_DAIFMT_CBM_CFM;
4751 break;
4752 case 0x10:
4753 format |= SND_SOC_DAIFMT_CBM_CFS;
4754 break;
4755 case 0x01:
4756 format |= SND_SOC_DAIFMT_CBS_CFM;
4757 break;
4758 default:
4759 format |= SND_SOC_DAIFMT_CBS_CFS;
4760 break;
4761 }
4762
4763 return format;
4764}
4765EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4766
Kuninori Morimotocb470082013-09-10 17:39:56 -07004767int snd_soc_of_get_dai_name(struct device_node *of_node,
4768 const char **dai_name)
4769{
4770 struct snd_soc_component *pos;
4771 struct of_phandle_args args;
4772 int ret;
4773
4774 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4775 "#sound-dai-cells", 0, &args);
4776 if (ret)
4777 return ret;
4778
4779 ret = -EPROBE_DEFER;
4780
4781 mutex_lock(&client_mutex);
4782 list_for_each_entry(pos, &component_list, list) {
4783 if (pos->dev->of_node != args.np)
4784 continue;
4785
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004786 if (pos->driver->of_xlate_dai_name) {
4787 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4788 } else {
4789 int id = -1;
4790
4791 switch (args.args_count) {
4792 case 0:
4793 id = 0; /* same as dai_drv[0] */
4794 break;
4795 case 1:
4796 id = args.args[0];
4797 break;
4798 default:
4799 /* not supported */
4800 break;
4801 }
4802
4803 if (id < 0 || id >= pos->num_dai) {
4804 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004805 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004806 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004807
4808 ret = 0;
4809
4810 *dai_name = pos->dai_drv[id].name;
4811 if (!*dai_name)
4812 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004813 }
4814
Kuninori Morimotocb470082013-09-10 17:39:56 -07004815 break;
4816 }
4817 mutex_unlock(&client_mutex);
4818
4819 of_node_put(args.np);
4820
4821 return ret;
4822}
4823EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4824
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004825static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004826{
Mark Brown384c89e2008-12-03 17:34:03 +00004827#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004828 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4829 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004830 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004831 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004832 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004833
Mark Brown8a9dab12011-01-10 22:25:21 +00004834 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004835 &codec_list_fops))
4836 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4837
Mark Brown8a9dab12011-01-10 22:25:21 +00004838 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004839 &dai_list_fops))
4840 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004841
Mark Brown8a9dab12011-01-10 22:25:21 +00004842 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004843 &platform_list_fops))
4844 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004845#endif
4846
Mark Brownfb257892011-04-28 10:57:54 +01004847 snd_soc_util_init();
4848
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004849 return platform_driver_register(&soc_driver);
4850}
Mark Brown4abe8e12010-10-12 17:41:03 +01004851module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004852
Mark Brown7d8c16a2008-11-30 22:11:24 +00004853static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004854{
Mark Brownfb257892011-04-28 10:57:54 +01004855 snd_soc_util_exit();
4856
Mark Brown384c89e2008-12-03 17:34:03 +00004857#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004858 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004859#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004860 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004861}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004862module_exit(snd_soc_exit);
4863
4864/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004865MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004866MODULE_DESCRIPTION("ALSA SoC Core");
4867MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004868MODULE_ALIAS("platform:soc-audio");