blob: a5edb31ddfffd7b513947047e6216f70ab063f7b [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;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200579 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200580
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++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100599
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100601 continue;
602
Benoit Cousson88bd8702014-07-08 23:19:34 +0200603 for (j = 0; j < card->rtd[i].num_codecs; j++) {
604 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
605 struct snd_soc_dai_driver *drv = dai->driver;
606
607 if (drv->ops->digital_mute && dai->playback_active)
608 drv->ops->digital_mute(dai, 1);
609 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200610 }
611
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100612 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 for (i = 0; i < card->num_rtd; i++) {
614 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100615 continue;
616
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100618 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100619
Mark Brown87506542008-11-18 20:50:34 +0000620 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000621 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200622
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623 for (i = 0; i < card->num_rtd; i++) {
624 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
625 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100626
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100628 continue;
629
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
631 cpu_dai->driver->suspend(cpu_dai);
632 if (platform->driver->suspend && !platform->suspended) {
633 platform->driver->suspend(cpu_dai);
634 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100635 }
636 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200637
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000638 /* close any waiting streams and save state */
639 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200640 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
Tejun Heo43829732012-08-20 14:51:24 -0700641 flush_delayed_work(&card->rtd[i].delayed_work);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200642 for (j = 0; j < card->rtd[i].num_codecs; j++) {
643 codec_dais[j]->codec->dapm.suspend_bias_level =
644 codec_dais[j]->codec->dapm.bias_level;
645 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100647
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000648 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000649
650 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100651 continue;
652
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800653 snd_soc_dapm_stream_event(&card->rtd[i],
654 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800655 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800657 snd_soc_dapm_stream_event(&card->rtd[i],
658 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800659 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660 }
661
Mark Browne2d32ff2012-08-31 17:38:32 -0700662 /* Recheck all analogue paths too */
663 dapm_mark_io_dirty(&card->dapm);
664 snd_soc_dapm_sync(&card->dapm);
665
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000666 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200667 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 /* If there are paths active then the CODEC will be held with
669 * bias _ON and should not be suspended. */
670 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200671 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000672 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000673 /*
674 * If the CODEC is capable of idle
675 * bias off then being in STANDBY
676 * means it's doing something,
677 * otherwise fall through.
678 */
679 if (codec->dapm.idle_bias_off) {
680 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200681 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000682 break;
683 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000684 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100685 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000686 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900687 codec->cache_sync = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200688 if (codec->component.regmap)
689 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800690 /* deactivate pins to sleep state */
691 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000692 break;
693 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200694 dev_dbg(codec->dev,
695 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 break;
697 }
698 }
699 }
700
701 for (i = 0; i < card->num_rtd; i++) {
702 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
703
704 if (card->rtd[i].dai_link->ignore_suspend)
705 continue;
706
707 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
708 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800709
710 /* deactivate pins to sleep state */
711 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200712 }
713
Mark Brown87506542008-11-18 20:50:34 +0000714 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000715 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716
717 return 0;
718}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000719EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200720
Andy Green6ed25972008-06-13 16:24:05 +0100721/* deferred resume work, so resume can complete before we finished
722 * setting our codec back up, which can be very slow on I2C
723 */
724static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200725{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000726 struct snd_soc_card *card =
727 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200728 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200729 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200730
Andy Green6ed25972008-06-13 16:24:05 +0100731 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
732 * so userspace apps are blocked from touching us
733 */
734
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000735 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100736
Mark Brown99497882010-05-07 20:24:05 +0100737 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000738 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100739
Mark Brown87506542008-11-18 20:50:34 +0000740 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000741 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200742
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743 /* resume AC97 DAIs */
744 for (i = 0; i < card->num_rtd; i++) {
745 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100746
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000747 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100748 continue;
749
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
751 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200752 }
753
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200754 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000755 /* If the CODEC was idle over suspend then it will have been
756 * left with bias OFF or STANDBY and suspended so we must now
757 * resume. Otherwise the suspend was suppressed.
758 */
759 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200760 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000761 case SND_SOC_BIAS_STANDBY:
762 case SND_SOC_BIAS_OFF:
763 codec->driver->resume(codec);
764 codec->suspended = 0;
765 break;
766 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200767 dev_dbg(codec->dev,
768 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000769 break;
770 }
Mark Brown1547aba2010-05-07 21:11:40 +0100771 }
772 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200773
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000774 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100775
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000776 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100777 continue;
778
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800779 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000780 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800781 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800783 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000784 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800785 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200786 }
787
Mark Brown3ff3f642008-05-19 12:32:25 +0200788 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000789 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100790
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000791 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100792 continue;
793
Benoit Cousson88bd8702014-07-08 23:19:34 +0200794 for (j = 0; j < card->rtd[i].num_codecs; j++) {
795 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
796 struct snd_soc_dai_driver *drv = dai->driver;
797
798 if (drv->ops->digital_mute && dai->playback_active)
799 drv->ops->digital_mute(dai, 0);
800 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200801 }
802
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000803 for (i = 0; i < card->num_rtd; i++) {
804 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
805 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100806
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000807 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100808 continue;
809
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000810 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
811 cpu_dai->driver->resume(cpu_dai);
812 if (platform->driver->resume && platform->suspended) {
813 platform->driver->resume(cpu_dai);
814 platform->suspended = 0;
815 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200816 }
817
Mark Brown87506542008-11-18 20:50:34 +0000818 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000819 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200820
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000821 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100822
823 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000824 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700825
826 /* Recheck all analogue paths too */
827 dapm_mark_io_dirty(&card->dapm);
828 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100829}
830
831/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000832int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100833{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000834 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600835 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200836
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800837 /* If the initialization of this soc device failed, there is no codec
838 * associated with it. Just bail out in this case.
839 */
840 if (list_empty(&card->codec_dev_list))
841 return 0;
842
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800843 /* activate pins from sleep state */
844 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200845 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
846 struct snd_soc_dai **codec_dais = rtd->codec_dais;
847 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
848 int j;
849
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800850 if (cpu_dai->active)
851 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200852
853 for (j = 0; j < rtd->num_codecs; j++) {
854 struct snd_soc_dai *codec_dai = codec_dais[j];
855 if (codec_dai->active)
856 pinctrl_pm_select_default_state(codec_dai->dev);
857 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800858 }
859
Mark Brown64ab9ba2009-03-31 11:27:03 +0100860 /* AC97 devices might have other drivers hanging off them so
861 * need to resume immediately. Other drivers don't have that
862 * problem and may take a substantial amount of time to resume
863 * due to I/O costs and anti-pop so handle them out of line.
864 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000865 for (i = 0; i < card->num_rtd; i++) {
866 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600867 ac97_control |= cpu_dai->driver->ac97_control;
868 }
869 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000870 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600871 soc_resume_deferred(&card->deferred_resume_work);
872 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000873 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600874 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000875 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100876 }
Andy Green6ed25972008-06-13 16:24:05 +0100877
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200878 return 0;
879}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000880EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200881#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000882#define snd_soc_suspend NULL
883#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200884#endif
885
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100886static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800887};
888
Benoit Cousson88bd8702014-07-08 23:19:34 +0200889static struct snd_soc_codec *soc_find_codec(
890 const struct device_node *codec_of_node,
891 const char *codec_name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100892{
893 struct snd_soc_codec *codec;
894
895 list_for_each_entry(codec, &codec_list, list) {
896 if (codec_of_node) {
897 if (codec->dev->of_node != codec_of_node)
898 continue;
899 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200900 if (strcmp(codec->component.name, codec_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100901 continue;
902 }
903
904 return codec;
905 }
906
907 return NULL;
908}
909
910static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
911 const char *codec_dai_name)
912{
913 struct snd_soc_dai *codec_dai;
914
915 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
916 if (!strcmp(codec_dai->name, codec_dai_name)) {
917 return codec_dai;
918 }
919 }
920
921 return NULL;
922}
923
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000924static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200925{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000926 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
927 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100928 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200929 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
930 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000931 struct snd_soc_platform *platform;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100932 struct snd_soc_dai *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100933 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200934 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200935
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000936 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000937
Mark Brownb19e6e72012-03-14 21:18:39 +0000938 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100939 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600940 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100941 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600942 continue;
943 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100944 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600945 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100946 list_for_each_entry(cpu_dai, &component->dai_list, list) {
947 if (dai_link->cpu_dai_name &&
948 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
949 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700950
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100951 rtd->cpu_dai = cpu_dai;
952 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000953 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000954
955 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000956 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000957 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000958 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000959 }
960
Benoit Cousson88bd8702014-07-08 23:19:34 +0200961 rtd->num_codecs = dai_link->num_codecs;
962
963 /* Find CODEC from registered CODECs */
964 for (i = 0; i < rtd->num_codecs; i++) {
965 struct snd_soc_codec *codec;
966 codec = soc_find_codec(codecs[i].of_node, codecs[i].name);
967 if (!codec) {
968 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
969 codecs[i].name);
970 return -EPROBE_DEFER;
971 }
972
973 codec_dais[i] = soc_find_codec_dai(codec, codecs[i].dai_name);
974 if (!codec_dais[i]) {
975 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
976 codecs[i].dai_name);
977 return -EPROBE_DEFER;
978 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000979 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100980
Benoit Cousson88bd8702014-07-08 23:19:34 +0200981 /* Single codec links expect codec and codec_dai in runtime data */
982 rtd->codec_dai = codec_dais[0];
983 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100984
Mark Brown848dd8b2011-04-27 18:16:32 +0100985 /* if there's no platform we match on the empty platform */
986 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700987 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100988 platform_name = "snd-soc-dummy";
989
Mark Brownb19e6e72012-03-14 21:18:39 +0000990 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000991 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700992 if (dai_link->platform_of_node) {
993 if (platform->dev->of_node !=
994 dai_link->platform_of_node)
995 continue;
996 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200997 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700998 continue;
999 }
Stephen Warren2610ab72011-12-07 13:58:27 -07001000
1001 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002 }
Mark Brownb19e6e72012-03-14 21:18:39 +00001003 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001004 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001005 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001006 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001007 }
Mark Brownb19e6e72012-03-14 21:18:39 +00001008
1009 card->num_rtd++;
1010
1011 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001012}
1013
Stephen Warrend12cd192012-06-08 12:34:22 -06001014static int soc_remove_platform(struct snd_soc_platform *platform)
1015{
1016 int ret;
1017
1018 if (platform->driver->remove) {
1019 ret = platform->driver->remove(platform);
1020 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001021 dev_err(platform->dev, "ASoC: failed to remove %d\n",
1022 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -06001023 }
1024
1025 /* Make sure all DAPM widgets are freed */
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001026 snd_soc_dapm_free(&platform->component.dapm);
Stephen Warrend12cd192012-06-08 12:34:22 -06001027
1028 soc_cleanup_platform_debugfs(platform);
1029 platform->probed = 0;
Stephen Warrend12cd192012-06-08 12:34:22 -06001030 module_put(platform->dev->driver->owner);
1031
1032 return 0;
1033}
1034
Jarkko Nikula589c3562010-12-06 16:27:07 +02001035static void soc_remove_codec(struct snd_soc_codec *codec)
1036{
1037 int err;
1038
1039 if (codec->driver->remove) {
1040 err = codec->driver->remove(codec);
1041 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001042 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001043 }
1044
1045 /* Make sure all DAPM widgets are freed */
1046 snd_soc_dapm_free(&codec->dapm);
1047
1048 soc_cleanup_codec_debugfs(codec);
1049 codec->probed = 0;
1050 list_del(&codec->card_list);
1051 module_put(codec->dev->driver->owner);
1052}
1053
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001054static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1055{
1056 int err;
1057
1058 if (codec_dai && codec_dai->probed &&
1059 codec_dai->driver->remove_order == order) {
1060 if (codec_dai->driver->remove) {
1061 err = codec_dai->driver->remove(codec_dai);
1062 if (err < 0)
1063 dev_err(codec_dai->dev,
1064 "ASoC: failed to remove %s: %d\n",
1065 codec_dai->name, err);
1066 }
1067 codec_dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001068 }
1069}
1070
Stephen Warren62ae68f2012-06-08 12:34:23 -06001071static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001072{
1073 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +02001074 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1075 int i, err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001076
1077 /* unregister the rtd device */
1078 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001079 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1080 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1081 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001082 rtd->dev_registered = 0;
1083 }
1084
1085 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001086 for (i = 0; i < rtd->num_codecs; i++)
1087 soc_remove_codec_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001088
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001089 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001090 if (cpu_dai && cpu_dai->probed &&
1091 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001092 if (cpu_dai->driver->remove) {
1093 err = cpu_dai->driver->remove(cpu_dai);
1094 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001095 dev_err(cpu_dai->dev,
1096 "ASoC: failed to remove %s: %d\n",
1097 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001098 }
1099 cpu_dai->probed = 0;
Lars-Peter Clausen9420d972014-06-16 18:13:10 +02001100 if (!cpu_dai->codec)
Stephen Warrena9db7db2012-06-08 12:34:20 -06001101 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001102 }
1103}
1104
Stephen Warren62ae68f2012-06-08 12:34:23 -06001105static void soc_remove_link_components(struct snd_soc_card *card, int num,
1106 int order)
1107{
1108 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1109 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001110 struct snd_soc_platform *platform = rtd->platform;
1111 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001112 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001113
1114 /* remove the platform */
1115 if (platform && platform->probed &&
1116 platform->driver->remove_order == order) {
1117 soc_remove_platform(platform);
1118 }
1119
1120 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001121 for (i = 0; i < rtd->num_codecs; i++) {
1122 codec = rtd->codec_dais[i]->codec;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001123 if (codec && codec->probed &&
1124 codec->driver->remove_order == order)
1125 soc_remove_codec(codec);
1126 }
1127
1128 /* remove any CPU-side CODEC */
1129 if (cpu_dai) {
1130 codec = cpu_dai->codec;
1131 if (codec && codec->probed &&
1132 codec->driver->remove_order == order)
1133 soc_remove_codec(codec);
1134 }
1135}
1136
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001137static void soc_remove_dai_links(struct snd_soc_card *card)
1138{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001139 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001140
Liam Girdwood0168bf02011-06-07 16:08:05 +01001141 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1142 order++) {
1143 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001144 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001145 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001146
1147 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1148 order++) {
1149 for (dai = 0; dai < card->num_rtd; dai++)
1150 soc_remove_link_components(card, dai, order);
1151 }
1152
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001153 card->num_rtd = 0;
1154}
1155
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001156static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001157 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001158{
1159 int i;
1160
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001161 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001162 return;
1163
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001164 for (i = 0; i < card->num_configs; i++) {
1165 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001166 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001167 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001168 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001169 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001170 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001171 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001172 }
1173}
1174
Jarkko Nikula589c3562010-12-06 16:27:07 +02001175static int soc_probe_codec(struct snd_soc_card *card,
1176 struct snd_soc_codec *codec)
1177{
1178 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001179 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001180 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001181
1182 codec->card = card;
1183 codec->dapm.card = card;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001184 soc_set_name_prefix(card, &codec->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001185
Jarkko Nikula70d29332011-01-27 16:24:22 +02001186 if (!try_module_get(codec->dev->driver->owner))
1187 return -ENODEV;
1188
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001189 soc_init_codec_debugfs(codec);
1190
Nariman Poushinb318ad52014-04-01 13:59:33 +01001191 if (driver->dapm_widgets) {
1192 ret = snd_soc_dapm_new_controls(&codec->dapm,
1193 driver->dapm_widgets,
1194 driver->num_dapm_widgets);
1195
1196 if (ret != 0) {
1197 dev_err(codec->dev,
1198 "Failed to create new controls %d\n", ret);
1199 goto err_probe;
1200 }
1201 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001202
Mark Brown888df392012-02-16 19:37:51 -08001203 /* Create DAPM widgets for each DAI stream */
Nariman Poushin261edc72014-03-31 15:47:12 +01001204 list_for_each_entry(dai, &codec->component.dai_list, list) {
1205 ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1206
1207 if (ret != 0) {
1208 dev_err(codec->dev,
1209 "Failed to create DAI widgets %d\n", ret);
1210 goto err_probe;
1211 }
1212 }
Mark Brown888df392012-02-16 19:37:51 -08001213
Mark Brown33c5f962011-08-22 18:40:30 +01001214 codec->dapm.idle_bias_off = driver->idle_bias_off;
1215
Mark Brown89b95ac02011-03-07 16:38:44 +00001216 if (driver->probe) {
1217 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001218 if (ret < 0) {
1219 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001220 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001221 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001222 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001223 WARN(codec->dapm.idle_bias_off &&
1224 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001225 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02001226 codec->component.name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001227 }
1228
Mark Brownb7af1da2011-04-07 19:18:44 +09001229 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001230 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001231 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001232 if (driver->dapm_routes)
1233 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1234 driver->num_dapm_routes);
1235
Jarkko Nikula589c3562010-12-06 16:27:07 +02001236 /* mark codec as probed and add to card codec list */
1237 codec->probed = 1;
1238 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001239 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001240
Jarkko Nikula70d29332011-01-27 16:24:22 +02001241 return 0;
1242
1243err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001244 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001245 module_put(codec->dev->driver->owner);
1246
Jarkko Nikula589c3562010-12-06 16:27:07 +02001247 return ret;
1248}
1249
Liam Girdwood956245e2011-07-01 16:54:08 +01001250static int soc_probe_platform(struct snd_soc_card *card,
1251 struct snd_soc_platform *platform)
1252{
1253 int ret = 0;
1254 const struct snd_soc_platform_driver *driver = platform->driver;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001255 struct snd_soc_component *component;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001256 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001257
1258 platform->card = card;
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001259 platform->component.dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001260
1261 if (!try_module_get(platform->dev->driver->owner))
1262 return -ENODEV;
1263
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001264 soc_init_platform_debugfs(platform);
1265
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001266 if (driver->dapm_widgets)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001267 snd_soc_dapm_new_controls(&platform->component.dapm,
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001268 driver->dapm_widgets, driver->num_dapm_widgets);
1269
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001270 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001271 list_for_each_entry(component, &component_list, list) {
1272 if (component->dev != platform->dev)
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001273 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001274 list_for_each_entry(dai, &component->dai_list, list)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001275 snd_soc_dapm_new_dai_widgets(&platform->component.dapm,
1276 dai);
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001277 }
1278
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001279 platform->component.dapm.idle_bias_off = 1;
Stephen Warren3fec6b62012-04-05 12:28:01 -06001280
Liam Girdwood956245e2011-07-01 16:54:08 +01001281 if (driver->probe) {
1282 ret = driver->probe(platform);
1283 if (ret < 0) {
1284 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001285 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001286 goto err_probe;
1287 }
1288 }
1289
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001290 if (driver->controls)
1291 snd_soc_add_platform_controls(platform, driver->controls,
1292 driver->num_controls);
1293 if (driver->dapm_routes)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001294 snd_soc_dapm_add_routes(&platform->component.dapm,
1295 driver->dapm_routes, driver->num_dapm_routes);
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001296
Liam Girdwood956245e2011-07-01 16:54:08 +01001297 /* mark platform as probed and add to card platform list */
1298 platform->probed = 1;
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001299 list_add(&platform->component.dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001300
1301 return 0;
1302
1303err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001304 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001305 module_put(platform->dev->driver->owner);
1306
1307 return ret;
1308}
1309
Mark Brown36ae1a92012-01-06 17:12:45 -08001310static void rtd_release(struct device *dev)
1311{
1312 kfree(dev);
1313}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001314
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001315static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1316 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001317{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001318 int ret = 0;
1319
Jarkko Nikula589c3562010-12-06 16:27:07 +02001320 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001321 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1322 if (!rtd->dev)
1323 return -ENOMEM;
1324 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001325 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001326 rtd->dev->release = rtd_release;
1327 rtd->dev->init_name = name;
1328 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001329 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001330 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1331 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1332 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1333 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001334 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001335 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001336 /* calling put_device() here to free the rtd->dev */
1337 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001338 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001339 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001340 return ret;
1341 }
1342 rtd->dev_registered = 1;
1343
1344 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001345 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001346 if (ret < 0)
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001347 dev_err(rtd->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001348 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001349
1350 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001351 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001352 if (ret < 0)
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001353 dev_err(rtd->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001354 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001355
1356 return 0;
1357}
1358
Stephen Warren62ae68f2012-06-08 12:34:23 -06001359static int soc_probe_link_components(struct snd_soc_card *card, int num,
1360 int order)
1361{
1362 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1363 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001364 struct snd_soc_platform *platform = rtd->platform;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001365 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001366
1367 /* probe the CPU-side component, if it is a CODEC */
1368 if (cpu_dai->codec &&
1369 !cpu_dai->codec->probed &&
1370 cpu_dai->codec->driver->probe_order == order) {
1371 ret = soc_probe_codec(card, cpu_dai->codec);
1372 if (ret < 0)
1373 return ret;
1374 }
1375
Benoit Cousson88bd8702014-07-08 23:19:34 +02001376 /* probe the CODEC-side components */
1377 for (i = 0; i < rtd->num_codecs; i++) {
1378 if (!rtd->codec_dais[i]->codec->probed &&
1379 rtd->codec_dais[i]->codec->driver->probe_order == order) {
1380 ret = soc_probe_codec(card, rtd->codec_dais[i]->codec);
1381 if (ret < 0)
1382 return ret;
1383 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001384 }
1385
1386 /* probe the platform */
1387 if (!platform->probed &&
1388 platform->driver->probe_order == order) {
1389 ret = soc_probe_platform(card, platform);
1390 if (ret < 0)
1391 return ret;
1392 }
1393
1394 return 0;
1395}
1396
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001397static int soc_probe_codec_dai(struct snd_soc_card *card,
1398 struct snd_soc_dai *codec_dai,
1399 int order)
1400{
1401 int ret;
1402
1403 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1404 if (codec_dai->driver->probe) {
1405 ret = codec_dai->driver->probe(codec_dai);
1406 if (ret < 0) {
1407 dev_err(codec_dai->dev,
1408 "ASoC: failed to probe CODEC DAI %s: %d\n",
1409 codec_dai->name, ret);
1410 return ret;
1411 }
1412 }
1413
1414 /* mark codec_dai as probed and add to card dai list */
1415 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001416 }
1417
1418 return 0;
1419}
1420
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001421static int soc_link_dai_widgets(struct snd_soc_card *card,
1422 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001423 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001424{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001425 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1426 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001427 struct snd_soc_dapm_widget *play_w, *capture_w;
1428 int ret;
1429
Benoit Cousson88bd8702014-07-08 23:19:34 +02001430 if (rtd->num_codecs > 1)
1431 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1432
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001433 /* link the DAI widgets */
1434 play_w = codec_dai->playback_widget;
1435 capture_w = cpu_dai->capture_widget;
1436 if (play_w && capture_w) {
1437 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1438 capture_w, play_w);
1439 if (ret != 0) {
1440 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1441 play_w->name, capture_w->name, ret);
1442 return ret;
1443 }
1444 }
1445
1446 play_w = cpu_dai->playback_widget;
1447 capture_w = codec_dai->capture_widget;
1448 if (play_w && capture_w) {
1449 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1450 capture_w, play_w);
1451 if (ret != 0) {
1452 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1453 play_w->name, capture_w->name, ret);
1454 return ret;
1455 }
1456 }
1457
1458 return 0;
1459}
1460
Stephen Warren62ae68f2012-06-08 12:34:23 -06001461static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001462{
1463 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1464 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001465 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001466 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001467 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001468
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001469 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001470 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001471
1472 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001473 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001474 cpu_dai->card = card;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001475 for (i = 0; i < rtd->num_codecs; i++)
1476 rtd->codec_dais[i]->card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001477
1478 /* set default power off timeout */
1479 rtd->pmdown_time = pmdown_time;
1480
1481 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001482 if (!cpu_dai->probed &&
1483 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001484 if (!cpu_dai->codec) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001485 if (!try_module_get(cpu_dai->dev->driver->owner))
1486 return -ENODEV;
Stephen Warrena9db7db2012-06-08 12:34:20 -06001487 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001488
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001489 if (cpu_dai->driver->probe) {
1490 ret = cpu_dai->driver->probe(cpu_dai);
1491 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001492 dev_err(cpu_dai->dev,
1493 "ASoC: failed to probe CPU DAI %s: %d\n",
1494 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001495 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496 return ret;
1497 }
1498 }
1499 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001500 }
1501
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001502 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001503 for (i = 0; i < rtd->num_codecs; i++) {
1504 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1505 if (ret)
1506 return ret;
1507 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001508
Liam Girdwood0168bf02011-06-07 16:08:05 +01001509 /* complete DAI probe during last probe */
1510 if (order != SND_SOC_COMP_ORDER_LAST)
1511 return 0;
1512
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001513 /* do machine specific initialization */
1514 if (dai_link->init) {
1515 ret = dai_link->init(rtd);
1516 if (ret < 0) {
1517 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1518 dai_link->name, ret);
1519 return ret;
1520 }
1521 }
1522
1523 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001524 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001525 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001526
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001527#ifdef CONFIG_DEBUG_FS
1528 /* add DPCM sysfs entries */
1529 if (dai_link->dynamic) {
1530 ret = soc_dpcm_debugfs_add(rtd);
1531 if (ret < 0) {
1532 dev_err(rtd->dev,
1533 "ASoC: failed to add dpcm sysfs entries: %d\n",
1534 ret);
1535 return ret;
1536 }
1537 }
1538#endif
1539
Mark Brown36ae1a92012-01-06 17:12:45 -08001540 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001541 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001542 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1543 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001544
Namarta Kohli1245b702012-08-16 17:10:41 +05301545 if (cpu_dai->driver->compress_dai) {
1546 /*create compress_device"*/
1547 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001548 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001549 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301550 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001551 return ret;
1552 }
1553 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301554
1555 if (!dai_link->params) {
1556 /* create the pcm */
1557 ret = soc_new_pcm(rtd, num);
1558 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001559 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301560 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001561 return ret;
1562 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301563 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001564 INIT_DELAYED_WORK(&rtd->delayed_work,
1565 codec2codec_close_delayed_work);
1566
Namarta Kohli1245b702012-08-16 17:10:41 +05301567 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001568 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001569 if (ret)
1570 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001571 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001572 }
1573
1574 /* add platform data for AC97 devices */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001575 for (i = 0; i < rtd->num_codecs; i++) {
1576 if (rtd->codec_dais[i]->driver->ac97_control)
1577 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1578 rtd->cpu_dai->ac97_pdata);
1579 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001580
1581 return 0;
1582}
1583
1584#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001585static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1586 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001587{
1588 int ret;
1589
1590 /* Only instantiate AC97 if not already done by the adaptor
1591 * for the generic AC97 subsystem.
1592 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001593 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001594 /*
1595 * It is possible that the AC97 device is already registered to
1596 * the device subsystem. This happens when the device is created
1597 * via snd_ac97_mixer(). Currently only SoC codec that does so
1598 * is the generic AC97 glue but others migh emerge.
1599 *
1600 * In those cases we don't try to register the device again.
1601 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001602 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001603 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001604
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001605 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001606 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001607 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001608 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001609 return ret;
1610 }
1611
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001612 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001613 }
1614 return 0;
1615}
1616
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001617static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001618{
1619 if (codec->ac97_registered) {
1620 soc_ac97_dev_unregister(codec);
1621 codec->ac97_registered = 0;
1622 }
1623}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001624
Benoit Cousson88bd8702014-07-08 23:19:34 +02001625static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1626{
1627 int i, ret;
1628
1629 for (i = 0; i < rtd->num_codecs; i++) {
1630 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1631
1632 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1633 if (ret) {
1634 while (--i >= 0)
1635 soc_unregister_ac97_codec(codec_dai->codec);
1636 return ret;
1637 }
1638 }
1639
1640 return 0;
1641}
1642
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001643static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1644{
Benoit Cousson88bd8702014-07-08 23:19:34 +02001645 int i;
1646
1647 for (i = 0; i < rtd->num_codecs; i++)
1648 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001649}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001650#endif
1651
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001652static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001653{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001654 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001655 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1656 const char *codecname = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001657
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001658 rtd->codec = soc_find_codec(aux_dev->codec_of_node, codecname);
1659 if (!rtd->codec) {
1660 if (aux_dev->codec_of_node)
1661 codecname = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001662
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001663 dev_err(card->dev, "ASoC: %s not registered\n", codecname);
1664 return -EPROBE_DEFER;
1665 }
1666
1667 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001668}
1669
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001670static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1671{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001672 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001673 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001674 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001675
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001676 if (rtd->codec->probed) {
1677 dev_err(rtd->codec->dev, "ASoC: codec already probed\n");
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001678 return -EBUSY;
1679 }
1680
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001681 ret = soc_probe_codec(card, rtd->codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001682 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001683 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001684
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001685 /* do machine specific initialization */
1686 if (aux_dev->init) {
1687 ret = aux_dev->init(&rtd->codec->dapm);
1688 if (ret < 0) {
1689 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1690 aux_dev->name, ret);
1691 return ret;
1692 }
1693 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001694
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001695 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001696}
1697
1698static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1699{
1700 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1701 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001702
1703 /* unregister the rtd device */
1704 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001705 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001706 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001707 rtd->dev_registered = 0;
1708 }
1709
Jarkko Nikula589c3562010-12-06 16:27:07 +02001710 if (codec && codec->probed)
1711 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001712}
1713
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001714static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001715{
1716 int ret;
1717
1718 if (codec->cache_init)
1719 return 0;
1720
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001721 ret = snd_soc_cache_init(codec);
1722 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001723 dev_err(codec->dev,
1724 "ASoC: Failed to set cache compression type: %d\n",
1725 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001726 return ret;
1727 }
1728 codec->cache_init = 1;
1729 return 0;
1730}
1731
Mark Brownb19e6e72012-03-14 21:18:39 +00001732static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001733{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001734 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001735 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001736 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001737
Liam Girdwood01b9d992012-03-07 10:38:25 +00001738 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001739
Mark Brownb19e6e72012-03-14 21:18:39 +00001740 /* bind DAIs */
1741 for (i = 0; i < card->num_links; i++) {
1742 ret = soc_bind_dai_link(card, i);
1743 if (ret != 0)
1744 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001745 }
1746
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001747 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001748 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001749 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001750 if (ret != 0)
1751 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001752 }
1753
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001754 /* initialize the register cache for each available codec */
1755 list_for_each_entry(codec, &codec_list, list) {
1756 if (codec->cache_init)
1757 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001758 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001759 if (ret < 0)
1760 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001761 }
1762
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001763 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001764 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001765 card->owner, 0, &card->snd_card);
1766 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001767 dev_err(card->dev,
1768 "ASoC: can't create sound card for card %s: %d\n",
1769 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001770 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001771 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001772
Mark Browne37a4972011-03-02 18:21:57 +00001773 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1774 card->dapm.dev = card->dev;
1775 card->dapm.card = card;
1776 list_add(&card->dapm.list, &card->dapm_list);
1777
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001778#ifdef CONFIG_DEBUG_FS
1779 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1780#endif
1781
Mark Brown88ee1c62011-02-02 10:43:26 +00001782#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001783 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001784 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001785#endif
Andy Green6ed25972008-06-13 16:24:05 +01001786
Mark Brown9a841eb2011-04-12 17:51:37 -07001787 if (card->dapm_widgets)
1788 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1789 card->num_dapm_widgets);
1790
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001791 /* initialise the sound card only once */
1792 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001793 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001794 if (ret < 0)
1795 goto card_probe_error;
1796 }
1797
Stephen Warren62ae68f2012-06-08 12:34:23 -06001798 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001799 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1800 order++) {
1801 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001802 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001803 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001804 dev_err(card->dev,
1805 "ASoC: failed to instantiate card %d\n",
1806 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001807 goto probe_dai_err;
1808 }
1809 }
1810 }
1811
1812 /* probe all DAI links on this card */
1813 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1814 order++) {
1815 for (i = 0; i < card->num_links; i++) {
1816 ret = soc_probe_link_dais(card, i, order);
1817 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001818 dev_err(card->dev,
1819 "ASoC: failed to instantiate card %d\n",
1820 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001821 goto probe_dai_err;
1822 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001823 }
1824 }
1825
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001826 for (i = 0; i < card->num_aux_devs; i++) {
1827 ret = soc_probe_aux_dev(card, i);
1828 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001829 dev_err(card->dev,
1830 "ASoC: failed to add auxiliary devices %d\n",
1831 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001832 goto probe_aux_dev_err;
1833 }
1834 }
1835
Mark Brown888df392012-02-16 19:37:51 -08001836 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001837 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001838
Mark Brownb7af1da2011-04-07 19:18:44 +09001839 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001840 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001841
Mark Brownb8ad29d2011-03-02 18:35:51 +00001842 if (card->dapm_routes)
1843 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1844 card->num_dapm_routes);
1845
Mark Brown75d9ac42011-09-27 16:41:01 +01001846 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001847 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001848 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001849 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001850
Mark Brownf04209a2012-04-09 16:29:21 +01001851 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001852 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1853 int j;
1854
1855 for (j = 0; j < rtd->num_codecs; j++) {
1856 struct snd_soc_dai *codec_dai = codec_dais[j];
1857
1858 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1859 if (ret != 0 && ret != -ENOTSUPP)
1860 dev_warn(codec_dai->dev,
1861 "ASoC: Failed to set DAI format: %d\n",
1862 ret);
1863 }
Mark Brownf04209a2012-04-09 16:29:21 +01001864 }
1865
1866 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001867 if (dai_fmt &&
1868 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001869 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1870 dai_fmt);
1871 if (ret != 0 && ret != -ENOTSUPP)
1872 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001873 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001874 ret);
1875 } else if (dai_fmt) {
1876 /* Flip the polarity for the "CPU" end */
1877 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1878 switch (dai_link->dai_fmt &
1879 SND_SOC_DAIFMT_MASTER_MASK) {
1880 case SND_SOC_DAIFMT_CBM_CFM:
1881 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1882 break;
1883 case SND_SOC_DAIFMT_CBM_CFS:
1884 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1885 break;
1886 case SND_SOC_DAIFMT_CBS_CFM:
1887 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1888 break;
1889 case SND_SOC_DAIFMT_CBS_CFS:
1890 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1891 break;
1892 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001893
1894 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001895 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001896 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001897 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001898 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001899 ret);
1900 }
1901 }
1902
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001903 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001904 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001905 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1906 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001907 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1908 "%s", card->driver_name ? card->driver_name : card->name);
1909 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1910 switch (card->snd_card->driver[i]) {
1911 case '_':
1912 case '-':
1913 case '\0':
1914 break;
1915 default:
1916 if (!isalnum(card->snd_card->driver[i]))
1917 card->snd_card->driver[i] = '_';
1918 break;
1919 }
1920 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001921
Mark Brown28e9ad92011-03-02 18:36:34 +00001922 if (card->late_probe) {
1923 ret = card->late_probe(card);
1924 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001925 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001926 card->name, ret);
1927 goto probe_aux_dev_err;
1928 }
1929 }
1930
Stephen Warren16332812011-11-23 12:42:04 -07001931 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001932 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001933
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001934 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001935
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001936 ret = snd_card_register(card->snd_card);
1937 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001938 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1939 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001940 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001941 }
1942
1943#ifdef CONFIG_SND_SOC_AC97_BUS
1944 /* register any AC97 codecs */
1945 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001946 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1947 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001948 dev_err(card->dev,
1949 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001950 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001951 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001952 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001953 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001954 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001955#endif
1956
Mark Brown435c5e22008-12-04 15:32:53 +00001957 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001958 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001959 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001960
1961 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001962
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001963probe_aux_dev_err:
1964 for (i = 0; i < card->num_aux_devs; i++)
1965 soc_remove_aux_dev(card, i);
1966
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001967probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001968 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001969
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001970card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001971 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001972 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001973
1974 snd_card_free(card->snd_card);
1975
Mark Brownb19e6e72012-03-14 21:18:39 +00001976base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001977 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001978
Mark Brownb19e6e72012-03-14 21:18:39 +00001979 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001980}
1981
1982/* probes a new socdev */
1983static int soc_probe(struct platform_device *pdev)
1984{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001985 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001986
Vinod Koul70a7ca32011-01-14 19:22:48 +05301987 /*
1988 * no card, so machine driver should be registering card
1989 * we should not be here in that case so ret error
1990 */
1991 if (!card)
1992 return -EINVAL;
1993
Mark Brownfe4085e2012-03-02 13:07:41 +00001994 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001995 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001996 card->name);
1997
Mark Brown435c5e22008-12-04 15:32:53 +00001998 /* Bodge while we unpick instantiation */
1999 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002000
Mark Brown28d528c2012-08-09 18:45:23 +01002001 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002002}
2003
Vinod Koulb0e26482011-01-13 22:48:02 +05302004static int soc_cleanup_card_resources(struct snd_soc_card *card)
2005{
Vinod Koulb0e26482011-01-13 22:48:02 +05302006 int i;
2007
2008 /* make sure any delayed work runs */
2009 for (i = 0; i < card->num_rtd; i++) {
2010 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002011 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302012 }
2013
2014 /* remove auxiliary devices */
2015 for (i = 0; i < card->num_aux_devs; i++)
2016 soc_remove_aux_dev(card, i);
2017
2018 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002019 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302020
2021 soc_cleanup_card_debugfs(card);
2022
2023 /* remove the card */
2024 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002025 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302026
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02002027 snd_soc_dapm_free(&card->dapm);
2028
Vinod Koulb0e26482011-01-13 22:48:02 +05302029 snd_card_free(card->snd_card);
2030 return 0;
2031
2032}
2033
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002034/* removes a socdev */
2035static int soc_remove(struct platform_device *pdev)
2036{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002037 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002038
Mark Brownc5af3a22008-11-28 13:29:45 +00002039 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002040 return 0;
2041}
2042
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002043int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002044{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002045 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002046 int i;
Mark Brown51737472009-06-22 13:16:51 +01002047
2048 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002049 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002050
2051 /* Flush out pmdown_time work - we actually do want to run it
2052 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002053 for (i = 0; i < card->num_rtd; i++) {
2054 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002055 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002056 }
Mark Brown51737472009-06-22 13:16:51 +01002057
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002058 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002059
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002060 /* deactivate pins to sleep state */
2061 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002062 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2063 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2064 int j;
2065
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002066 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002067 for (j = 0; j < rtd->num_codecs; j++) {
2068 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2069 pinctrl_pm_select_sleep_state(codec_dai->dev);
2070 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002071 }
2072
Mark Brown416356f2009-06-30 19:05:15 +01002073 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002074}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002075EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002076
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002077const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302078 .suspend = snd_soc_suspend,
2079 .resume = snd_soc_resume,
2080 .freeze = snd_soc_suspend,
2081 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002082 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302083 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002084};
Stephen Warrendeb26072011-04-05 19:35:30 -06002085EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002086
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002087/* ASoC platform driver */
2088static struct platform_driver soc_driver = {
2089 .driver = {
2090 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002091 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002092 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002093 },
2094 .probe = soc_probe,
2095 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002096};
2097
Mark Brown096e49d2009-07-05 15:12:22 +01002098/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002099 * snd_soc_new_ac97_codec - initailise AC97 device
2100 * @codec: audio codec
2101 * @ops: AC97 bus operations
2102 * @num: AC97 codec number
2103 *
2104 * Initialises AC97 codec resources for use by ad-hoc devices only.
2105 */
2106int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2107 struct snd_ac97_bus_ops *ops, int num)
2108{
2109 mutex_lock(&codec->mutex);
2110
2111 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2112 if (codec->ac97 == NULL) {
2113 mutex_unlock(&codec->mutex);
2114 return -ENOMEM;
2115 }
2116
2117 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2118 if (codec->ac97->bus == NULL) {
2119 kfree(codec->ac97);
2120 codec->ac97 = NULL;
2121 mutex_unlock(&codec->mutex);
2122 return -ENOMEM;
2123 }
2124
2125 codec->ac97->bus->ops = ops;
2126 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002127
2128 /*
2129 * Mark the AC97 device to be created by us. This way we ensure that the
2130 * device will be registered with the device subsystem later on.
2131 */
2132 codec->ac97_created = 1;
2133
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002134 mutex_unlock(&codec->mutex);
2135 return 0;
2136}
2137EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2138
Markus Pargmann741a5092013-08-19 17:05:55 +02002139static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2140
2141static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2142{
2143 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2144
2145 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2146
2147 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2148
2149 udelay(10);
2150
2151 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2152
2153 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2154 msleep(2);
2155}
2156
2157static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2158{
2159 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2160
2161 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2162
2163 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2164 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2165 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2166
2167 udelay(10);
2168
2169 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2170
2171 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2172 msleep(2);
2173}
2174
2175static int snd_soc_ac97_parse_pinctl(struct device *dev,
2176 struct snd_ac97_reset_cfg *cfg)
2177{
2178 struct pinctrl *p;
2179 struct pinctrl_state *state;
2180 int gpio;
2181 int ret;
2182
2183 p = devm_pinctrl_get(dev);
2184 if (IS_ERR(p)) {
2185 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002186 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002187 }
2188 cfg->pctl = p;
2189
2190 state = pinctrl_lookup_state(p, "ac97-reset");
2191 if (IS_ERR(state)) {
2192 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002193 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002194 }
2195 cfg->pstate_reset = state;
2196
2197 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2198 if (IS_ERR(state)) {
2199 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002200 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002201 }
2202 cfg->pstate_warm_reset = state;
2203
2204 state = pinctrl_lookup_state(p, "ac97-running");
2205 if (IS_ERR(state)) {
2206 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002207 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002208 }
2209 cfg->pstate_run = state;
2210
2211 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2212 if (gpio < 0) {
2213 dev_err(dev, "Can't find ac97-sync gpio\n");
2214 return gpio;
2215 }
2216 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2217 if (ret) {
2218 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2219 return ret;
2220 }
2221 cfg->gpio_sync = gpio;
2222
2223 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2224 if (gpio < 0) {
2225 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2226 return gpio;
2227 }
2228 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2229 if (ret) {
2230 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2231 return ret;
2232 }
2233 cfg->gpio_sdata = gpio;
2234
2235 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2236 if (gpio < 0) {
2237 dev_err(dev, "Can't find ac97-reset gpio\n");
2238 return gpio;
2239 }
2240 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2241 if (ret) {
2242 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2243 return ret;
2244 }
2245 cfg->gpio_reset = gpio;
2246
2247 return 0;
2248}
2249
Mark Brownb047e1c2013-06-26 12:45:59 +01002250struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002251EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002252
2253int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2254{
2255 if (ops == soc_ac97_ops)
2256 return 0;
2257
2258 if (soc_ac97_ops && ops)
2259 return -EBUSY;
2260
2261 soc_ac97_ops = ops;
2262
2263 return 0;
2264}
2265EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2266
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002267/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002268 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2269 *
2270 * This function sets the reset and warm_reset properties of ops and parses
2271 * the device node of pdev to get pinctrl states and gpio numbers to use.
2272 */
2273int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2274 struct platform_device *pdev)
2275{
2276 struct device *dev = &pdev->dev;
2277 struct snd_ac97_reset_cfg cfg;
2278 int ret;
2279
2280 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2281 if (ret)
2282 return ret;
2283
2284 ret = snd_soc_set_ac97_ops(ops);
2285 if (ret)
2286 return ret;
2287
2288 ops->warm_reset = snd_soc_ac97_warm_reset;
2289 ops->reset = snd_soc_ac97_reset;
2290
2291 snd_ac97_rst_cfg = cfg;
2292 return 0;
2293}
2294EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2295
2296/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002297 * snd_soc_free_ac97_codec - free AC97 codec device
2298 * @codec: audio codec
2299 *
2300 * Frees AC97 codec device resources.
2301 */
2302void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2303{
2304 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002305#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002306 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002307#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002308 kfree(codec->ac97->bus);
2309 kfree(codec->ac97);
2310 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002311 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002312 mutex_unlock(&codec->mutex);
2313}
2314EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2315
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002316/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002317 * snd_soc_cnew - create new control
2318 * @_template: control template
2319 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002320 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002321 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002322 *
2323 * Create a new mixer control from a template control.
2324 *
2325 * Returns 0 for success, else error.
2326 */
2327struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002328 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002329 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002330{
2331 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002332 struct snd_kcontrol *kcontrol;
2333 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002334
2335 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002336 template.index = 0;
2337
Mark Brownefb7ac32011-03-08 17:23:24 +00002338 if (!long_name)
2339 long_name = template.name;
2340
2341 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002342 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002343 if (!name)
2344 return NULL;
2345
Mark Brownefb7ac32011-03-08 17:23:24 +00002346 template.name = name;
2347 } else {
2348 template.name = long_name;
2349 }
2350
2351 kcontrol = snd_ctl_new1(&template, data);
2352
2353 kfree(name);
2354
2355 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002356}
2357EXPORT_SYMBOL_GPL(snd_soc_cnew);
2358
Liam Girdwood022658b2012-02-03 17:43:09 +00002359static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2360 const struct snd_kcontrol_new *controls, int num_controls,
2361 const char *prefix, void *data)
2362{
2363 int err, i;
2364
2365 for (i = 0; i < num_controls; i++) {
2366 const struct snd_kcontrol_new *control = &controls[i];
2367 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2368 control->name, prefix));
2369 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002370 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2371 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002372 return err;
2373 }
2374 }
2375
2376 return 0;
2377}
2378
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002379struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2380 const char *name)
2381{
2382 struct snd_card *card = soc_card->snd_card;
2383 struct snd_kcontrol *kctl;
2384
2385 if (unlikely(!name))
2386 return NULL;
2387
2388 list_for_each_entry(kctl, &card->controls, list)
2389 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2390 return kctl;
2391 return NULL;
2392}
2393EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2394
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002395/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002396 * snd_soc_add_codec_controls - add an array of controls to a codec.
2397 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002398 * duplicating this code.
2399 *
2400 * @codec: codec to add controls to
2401 * @controls: array of controls to add
2402 * @num_controls: number of elements in the array
2403 *
2404 * Return 0 for success, else error.
2405 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002406int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002407 const struct snd_kcontrol_new *controls, int num_controls)
2408{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002409 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002410
Liam Girdwood022658b2012-02-03 17:43:09 +00002411 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002412 codec->component.name_prefix, &codec->component);
Ian Molton3e8e1952009-01-09 00:23:21 +00002413}
Liam Girdwood022658b2012-02-03 17:43:09 +00002414EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002415
2416/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002417 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002418 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002419 *
2420 * @platform: platform to add controls to
2421 * @controls: array of controls to add
2422 * @num_controls: number of elements in the array
2423 *
2424 * Return 0 for success, else error.
2425 */
2426int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2427 const struct snd_kcontrol_new *controls, int num_controls)
2428{
2429 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002430
Liam Girdwood022658b2012-02-03 17:43:09 +00002431 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002432 NULL, &platform->component);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002433}
2434EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2435
2436/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002437 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2438 * Convenience function to add a list of controls.
2439 *
2440 * @soc_card: SoC card to add controls to
2441 * @controls: array of controls to add
2442 * @num_controls: number of elements in the array
2443 *
2444 * Return 0 for success, else error.
2445 */
2446int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2447 const struct snd_kcontrol_new *controls, int num_controls)
2448{
2449 struct snd_card *card = soc_card->snd_card;
2450
2451 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2452 NULL, soc_card);
2453}
2454EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2455
2456/**
2457 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2458 * Convienience function to add a list of controls.
2459 *
2460 * @dai: DAI to add controls to
2461 * @controls: array of controls to add
2462 * @num_controls: number of elements in the array
2463 *
2464 * Return 0 for success, else error.
2465 */
2466int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2467 const struct snd_kcontrol_new *controls, int num_controls)
2468{
2469 struct snd_card *card = dai->card->snd_card;
2470
2471 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2472 NULL, dai);
2473}
2474EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2475
2476/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002477 * snd_soc_info_enum_double - enumerated double mixer info callback
2478 * @kcontrol: mixer control
2479 * @uinfo: control element information
2480 *
2481 * Callback to provide information about a double enumerated
2482 * mixer control.
2483 *
2484 * Returns 0 for success.
2485 */
2486int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2487 struct snd_ctl_elem_info *uinfo)
2488{
2489 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2490
2491 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2492 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002493 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002494
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002495 if (uinfo->value.enumerated.item >= e->items)
2496 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002497 strlcpy(uinfo->value.enumerated.name,
2498 e->texts[uinfo->value.enumerated.item],
2499 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002500 return 0;
2501}
2502EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2503
2504/**
2505 * snd_soc_get_enum_double - enumerated double mixer get callback
2506 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002507 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002508 *
2509 * Callback to get the value of a double enumerated mixer.
2510 *
2511 * Returns 0 for success.
2512 */
2513int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2514 struct snd_ctl_elem_value *ucontrol)
2515{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002516 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002517 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002518 unsigned int val, item;
2519 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002520 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002521
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002522 ret = snd_soc_component_read(component, e->reg, &reg_val);
2523 if (ret)
2524 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002525 val = (reg_val >> e->shift_l) & e->mask;
2526 item = snd_soc_enum_val_to_item(e, val);
2527 ucontrol->value.enumerated.item[0] = item;
2528 if (e->shift_l != e->shift_r) {
2529 val = (reg_val >> e->shift_l) & e->mask;
2530 item = snd_soc_enum_val_to_item(e, val);
2531 ucontrol->value.enumerated.item[1] = item;
2532 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002533
2534 return 0;
2535}
2536EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2537
2538/**
2539 * snd_soc_put_enum_double - enumerated double mixer put callback
2540 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002541 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002542 *
2543 * Callback to set the value of a double enumerated mixer.
2544 *
2545 * Returns 0 for success.
2546 */
2547int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2548 struct snd_ctl_elem_value *ucontrol)
2549{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002550 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002551 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002552 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002553 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002554 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002555
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002556 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002557 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002558 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002559 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002560 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002561 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002562 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002563 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002564 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002565 }
2566
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002567 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002568}
2569EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2570
2571/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002572 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002573 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002574 * @reg: Register to read
2575 * @mask: Mask to use after shifting the register value
2576 * @shift: Right shift of register value
2577 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002578 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002579 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002580 * This functions reads a codec register. The register value is shifted right
2581 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2582 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002583 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002584 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002585 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002586static int snd_soc_read_signed(struct snd_soc_component *component,
2587 unsigned int reg, unsigned int mask, unsigned int shift,
2588 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002589{
Markus Pargmannf227b882014-01-16 16:02:10 +01002590 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002591 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002592
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002593 ret = snd_soc_component_read(component, reg, &val);
2594 if (ret < 0)
2595 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002596
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002597 val = (val >> shift) & mask;
2598
2599 if (!sign_bit) {
2600 *signed_val = val;
2601 return 0;
2602 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002603
2604 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002605 if (!(val & BIT(sign_bit))) {
2606 *signed_val = val;
2607 return 0;
2608 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002609
2610 ret = val;
2611
2612 /*
2613 * The register most probably does not contain a full-sized int.
2614 * Instead we have an arbitrary number of bits in a signed
2615 * representation which has to be translated into a full-sized int.
2616 * This is done by filling up all bits above the sign-bit.
2617 */
2618 ret |= ~((int)(BIT(sign_bit) - 1));
2619
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002620 *signed_val = ret;
2621
2622 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002623}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002624
2625/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002626 * snd_soc_info_volsw - single mixer info callback
2627 * @kcontrol: mixer control
2628 * @uinfo: control element information
2629 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002630 * Callback to provide information about a single mixer control, or a double
2631 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002632 *
2633 * Returns 0 for success.
2634 */
2635int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2636 struct snd_ctl_elem_info *uinfo)
2637{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002638 struct soc_mixer_control *mc =
2639 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002640 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002641
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002642 if (!mc->platform_max)
2643 mc->platform_max = mc->max;
2644 platform_max = mc->platform_max;
2645
2646 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002647 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2648 else
2649 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2650
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002651 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002652 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002653 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002654 return 0;
2655}
2656EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2657
2658/**
2659 * snd_soc_get_volsw - single mixer get callback
2660 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002661 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002662 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002663 * Callback to get the value of a single mixer control, or a double mixer
2664 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002665 *
2666 * Returns 0 for success.
2667 */
2668int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2669 struct snd_ctl_elem_value *ucontrol)
2670{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002671 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002672 struct soc_mixer_control *mc =
2673 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002674 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002675 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002676 unsigned int shift = mc->shift;
2677 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002678 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002679 int min = mc->min;
2680 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002681 unsigned int mask = (1 << fls(max)) - 1;
2682 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002683 int val;
2684 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002685
Markus Pargmannf227b882014-01-16 16:02:10 +01002686 if (sign_bit)
2687 mask = BIT(sign_bit + 1) - 1;
2688
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002689 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2690 if (ret)
2691 return ret;
2692
2693 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002694 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002695 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002696 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002697
2698 if (snd_soc_volsw_is_stereo(mc)) {
2699 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002700 ret = snd_soc_read_signed(component, reg, mask, rshift,
2701 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002702 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002703 ret = snd_soc_read_signed(component, reg2, mask, shift,
2704 sign_bit, &val);
2705 if (ret)
2706 return ret;
2707
2708 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002709 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002710 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002711 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002712 }
2713
2714 return 0;
2715}
2716EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2717
2718/**
2719 * snd_soc_put_volsw - single mixer put callback
2720 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002721 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002722 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002723 * Callback to set the value of a single mixer control, or a double mixer
2724 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002725 *
2726 * Returns 0 for success.
2727 */
2728int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2729 struct snd_ctl_elem_value *ucontrol)
2730{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002731 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002732 struct soc_mixer_control *mc =
2733 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002734 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002735 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002736 unsigned int shift = mc->shift;
2737 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002738 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002739 int min = mc->min;
2740 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002741 unsigned int mask = (1 << fls(max)) - 1;
2742 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002743 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002744 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002745 unsigned int val2 = 0;
2746 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002747
Markus Pargmannf227b882014-01-16 16:02:10 +01002748 if (sign_bit)
2749 mask = BIT(sign_bit + 1) - 1;
2750
2751 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002752 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002753 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002754 val_mask = mask << shift;
2755 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002756 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002757 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002758 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002759 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002760 if (reg == reg2) {
2761 val_mask |= mask << rshift;
2762 val |= val2 << rshift;
2763 } else {
2764 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002765 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002766 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002767 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002768 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002769 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002770 return err;
2771
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002772 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002773 err = snd_soc_component_update_bits(component, reg2, val_mask,
2774 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002775
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002776 return err;
2777}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002778EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002779
Mark Browne13ac2e2008-05-28 17:58:05 +01002780/**
Brian Austin1d99f242012-03-30 10:43:55 -05002781 * snd_soc_get_volsw_sx - single mixer get callback
2782 * @kcontrol: mixer control
2783 * @ucontrol: control element information
2784 *
2785 * Callback to get the value of a single mixer control, or a double mixer
2786 * control that spans 2 registers.
2787 *
2788 * Returns 0 for success.
2789 */
2790int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2791 struct snd_ctl_elem_value *ucontrol)
2792{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002793 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002794 struct soc_mixer_control *mc =
2795 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002796 unsigned int reg = mc->reg;
2797 unsigned int reg2 = mc->rreg;
2798 unsigned int shift = mc->shift;
2799 unsigned int rshift = mc->rshift;
2800 int max = mc->max;
2801 int min = mc->min;
2802 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002803 unsigned int val;
2804 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002805
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002806 ret = snd_soc_component_read(component, reg, &val);
2807 if (ret < 0)
2808 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002809
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002810 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2811
2812 if (snd_soc_volsw_is_stereo(mc)) {
2813 ret = snd_soc_component_read(component, reg2, &val);
2814 if (ret < 0)
2815 return ret;
2816
2817 val = ((val >> rshift) - min) & mask;
2818 ucontrol->value.integer.value[1] = val;
2819 }
Brian Austin1d99f242012-03-30 10:43:55 -05002820
2821 return 0;
2822}
2823EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2824
2825/**
2826 * snd_soc_put_volsw_sx - double mixer set callback
2827 * @kcontrol: mixer control
2828 * @uinfo: control element information
2829 *
2830 * Callback to set the value of a double mixer control that spans 2 registers.
2831 *
2832 * Returns 0 for success.
2833 */
2834int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2835 struct snd_ctl_elem_value *ucontrol)
2836{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002837 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002838 struct soc_mixer_control *mc =
2839 (struct soc_mixer_control *)kcontrol->private_value;
2840
2841 unsigned int reg = mc->reg;
2842 unsigned int reg2 = mc->rreg;
2843 unsigned int shift = mc->shift;
2844 unsigned int rshift = mc->rshift;
2845 int max = mc->max;
2846 int min = mc->min;
2847 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002848 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002849 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002850
2851 val_mask = mask << shift;
2852 val = (ucontrol->value.integer.value[0] + min) & mask;
2853 val = val << shift;
2854
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002855 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302856 if (err < 0)
2857 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002858
2859 if (snd_soc_volsw_is_stereo(mc)) {
2860 val_mask = mask << rshift;
2861 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2862 val2 = val2 << rshift;
2863
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002864 err = snd_soc_component_update_bits(component, reg2, val_mask,
2865 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002866 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002867 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002868}
2869EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2870
2871/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002872 * snd_soc_info_volsw_s8 - signed mixer info callback
2873 * @kcontrol: mixer control
2874 * @uinfo: control element information
2875 *
2876 * Callback to provide information about a signed mixer control.
2877 *
2878 * Returns 0 for success.
2879 */
2880int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2881 struct snd_ctl_elem_info *uinfo)
2882{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002883 struct soc_mixer_control *mc =
2884 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002885 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002886 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002887
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002888 if (!mc->platform_max)
2889 mc->platform_max = mc->max;
2890 platform_max = mc->platform_max;
2891
Mark Browne13ac2e2008-05-28 17:58:05 +01002892 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2893 uinfo->count = 2;
2894 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002895 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002896 return 0;
2897}
2898EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2899
2900/**
2901 * snd_soc_get_volsw_s8 - signed mixer get callback
2902 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002903 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002904 *
2905 * Callback to get the value of a signed mixer control.
2906 *
2907 * Returns 0 for success.
2908 */
2909int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2910 struct snd_ctl_elem_value *ucontrol)
2911{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002912 struct soc_mixer_control *mc =
2913 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002914 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002915 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002916 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002917 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002918 int ret;
2919
2920 ret = snd_soc_component_read(component, reg, &val);
2921 if (ret)
2922 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002923
2924 ucontrol->value.integer.value[0] =
2925 ((signed char)(val & 0xff))-min;
2926 ucontrol->value.integer.value[1] =
2927 ((signed char)((val >> 8) & 0xff))-min;
2928 return 0;
2929}
2930EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2931
2932/**
2933 * snd_soc_put_volsw_sgn - signed mixer put callback
2934 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002935 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002936 *
2937 * Callback to set the value of a signed mixer control.
2938 *
2939 * Returns 0 for success.
2940 */
2941int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2942 struct snd_ctl_elem_value *ucontrol)
2943{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002944 struct soc_mixer_control *mc =
2945 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002946 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002947 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002948 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002949 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002950
2951 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2952 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2953
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002954 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002955}
2956EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2957
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002958/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002959 * snd_soc_info_volsw_range - single mixer info callback with range.
2960 * @kcontrol: mixer control
2961 * @uinfo: control element information
2962 *
2963 * Callback to provide information, within a range, about a single
2964 * mixer control.
2965 *
2966 * returns 0 for success.
2967 */
2968int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2969 struct snd_ctl_elem_info *uinfo)
2970{
2971 struct soc_mixer_control *mc =
2972 (struct soc_mixer_control *)kcontrol->private_value;
2973 int platform_max;
2974 int min = mc->min;
2975
2976 if (!mc->platform_max)
2977 mc->platform_max = mc->max;
2978 platform_max = mc->platform_max;
2979
2980 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002981 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002982 uinfo->value.integer.min = 0;
2983 uinfo->value.integer.max = platform_max - min;
2984
2985 return 0;
2986}
2987EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2988
2989/**
2990 * snd_soc_put_volsw_range - single mixer put value callback with range.
2991 * @kcontrol: mixer control
2992 * @ucontrol: control element information
2993 *
2994 * Callback to set the value, within a range, for a single mixer control.
2995 *
2996 * Returns 0 for success.
2997 */
2998int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2999 struct snd_ctl_elem_value *ucontrol)
3000{
3001 struct soc_mixer_control *mc =
3002 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003003 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003004 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003005 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003006 unsigned int shift = mc->shift;
3007 int min = mc->min;
3008 int max = mc->max;
3009 unsigned int mask = (1 << fls(max)) - 1;
3010 unsigned int invert = mc->invert;
3011 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003012 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003013
3014 val = ((ucontrol->value.integer.value[0] + min) & mask);
3015 if (invert)
3016 val = max - val;
3017 val_mask = mask << shift;
3018 val = val << shift;
3019
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003020 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09003021 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00003022 return ret;
3023
3024 if (snd_soc_volsw_is_stereo(mc)) {
3025 val = ((ucontrol->value.integer.value[1] + 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, rreg, val_mask,
3032 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00003033 }
3034
3035 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003036}
3037EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3038
3039/**
3040 * snd_soc_get_volsw_range - single mixer get callback with range
3041 * @kcontrol: mixer control
3042 * @ucontrol: control element information
3043 *
3044 * Callback to get the value, within a range, of a single mixer control.
3045 *
3046 * Returns 0 for success.
3047 */
3048int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3049 struct snd_ctl_elem_value *ucontrol)
3050{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003051 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003052 struct soc_mixer_control *mc =
3053 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003054 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003055 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003056 unsigned int shift = mc->shift;
3057 int min = mc->min;
3058 int max = mc->max;
3059 unsigned int mask = (1 << fls(max)) - 1;
3060 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003061 unsigned int val;
3062 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003063
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003064 ret = snd_soc_component_read(component, reg, &val);
3065 if (ret)
3066 return ret;
3067
3068 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003069 if (invert)
3070 ucontrol->value.integer.value[0] =
3071 max - ucontrol->value.integer.value[0];
3072 ucontrol->value.integer.value[0] =
3073 ucontrol->value.integer.value[0] - min;
3074
Mark Brown9bde4f02012-12-19 16:05:00 +00003075 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003076 ret = snd_soc_component_read(component, rreg, &val);
3077 if (ret)
3078 return ret;
3079
3080 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003081 if (invert)
3082 ucontrol->value.integer.value[1] =
3083 max - ucontrol->value.integer.value[1];
3084 ucontrol->value.integer.value[1] =
3085 ucontrol->value.integer.value[1] - min;
3086 }
3087
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003088 return 0;
3089}
3090EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3091
3092/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003093 * snd_soc_limit_volume - Set new limit to an existing volume control.
3094 *
3095 * @codec: where to look for the control
3096 * @name: Name of the control
3097 * @max: new maximum limit
3098 *
3099 * Return 0 for success, else error.
3100 */
3101int snd_soc_limit_volume(struct snd_soc_codec *codec,
3102 const char *name, int max)
3103{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003104 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003105 struct snd_kcontrol *kctl;
3106 struct soc_mixer_control *mc;
3107 int found = 0;
3108 int ret = -EINVAL;
3109
3110 /* Sanity check for name and max */
3111 if (unlikely(!name || max <= 0))
3112 return -EINVAL;
3113
3114 list_for_each_entry(kctl, &card->controls, list) {
3115 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3116 found = 1;
3117 break;
3118 }
3119 }
3120 if (found) {
3121 mc = (struct soc_mixer_control *)kctl->private_value;
3122 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003123 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003124 ret = 0;
3125 }
3126 }
3127 return ret;
3128}
3129EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3130
Mark Brown71d08512011-10-10 18:31:26 +01003131int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3132 struct snd_ctl_elem_info *uinfo)
3133{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003134 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003135 struct soc_bytes *params = (void *)kcontrol->private_value;
3136
3137 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003138 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003139
3140 return 0;
3141}
3142EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3143
3144int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3145 struct snd_ctl_elem_value *ucontrol)
3146{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003147 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003148 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003149 int ret;
3150
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003151 if (component->regmap)
3152 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003153 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003154 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003155 else
3156 ret = -EINVAL;
3157
Mark Brownf831b052012-02-17 16:20:33 -08003158 /* Hide any masked bytes to ensure consistent data reporting */
3159 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003160 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003161 case 1:
3162 ucontrol->value.bytes.data[0] &= ~params->mask;
3163 break;
3164 case 2:
3165 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003166 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003167 break;
3168 case 4:
3169 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003170 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003171 break;
3172 default:
3173 return -EINVAL;
3174 }
3175 }
3176
Mark Brown71d08512011-10-10 18:31:26 +01003177 return ret;
3178}
3179EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3180
3181int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3182 struct snd_ctl_elem_value *ucontrol)
3183{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003184 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003185 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003186 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003187 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003188 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003189
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003190 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003191 return -EINVAL;
3192
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003193 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003194
Mark Brownb5a8fe42013-01-20 21:42:22 +09003195 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3196 if (!data)
3197 return -ENOMEM;
3198
Mark Brownf831b052012-02-17 16:20:33 -08003199 /*
3200 * If we've got a mask then we need to preserve the register
3201 * bits. We shouldn't modify the incoming data so take a
3202 * copy.
3203 */
3204 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003205 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003206 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003207 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003208
3209 val &= params->mask;
3210
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003211 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003212 case 1:
3213 ((u8 *)data)[0] &= ~params->mask;
3214 ((u8 *)data)[0] |= val;
3215 break;
3216 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003217 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003218 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003219 &mask, &mask);
3220 if (ret != 0)
3221 goto out;
3222
3223 ((u16 *)data)[0] &= mask;
3224
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003225 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003226 &val, &val);
3227 if (ret != 0)
3228 goto out;
3229
3230 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003231 break;
3232 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003233 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003234 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003235 &mask, &mask);
3236 if (ret != 0)
3237 goto out;
3238
3239 ((u32 *)data)[0] &= mask;
3240
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003241 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003242 &val, &val);
3243 if (ret != 0)
3244 goto out;
3245
3246 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003247 break;
3248 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003249 ret = -EINVAL;
3250 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003251 }
3252 }
3253
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003254 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003255 data, len);
3256
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003257out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003258 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003259
3260 return ret;
3261}
3262EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3263
Vinod Kould9881202014-05-02 10:58:11 +05303264int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3265 struct snd_ctl_elem_info *ucontrol)
3266{
3267 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3268
3269 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3270 ucontrol->count = params->max;
3271
3272 return 0;
3273}
3274EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3275
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003276/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003277 * snd_soc_info_xr_sx - signed multi register info callback
3278 * @kcontrol: mreg control
3279 * @uinfo: control element information
3280 *
3281 * Callback to provide information of a control that can
3282 * span multiple codec registers which together
3283 * forms a single signed value in a MSB/LSB manner.
3284 *
3285 * Returns 0 for success.
3286 */
3287int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3288 struct snd_ctl_elem_info *uinfo)
3289{
3290 struct soc_mreg_control *mc =
3291 (struct soc_mreg_control *)kcontrol->private_value;
3292 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3293 uinfo->count = 1;
3294 uinfo->value.integer.min = mc->min;
3295 uinfo->value.integer.max = mc->max;
3296
3297 return 0;
3298}
3299EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3300
3301/**
3302 * snd_soc_get_xr_sx - signed multi register get callback
3303 * @kcontrol: mreg control
3304 * @ucontrol: control element information
3305 *
3306 * Callback to get the value of a control that can span
3307 * multiple codec registers which together forms a single
3308 * signed value in a MSB/LSB manner. The control supports
3309 * specifying total no of bits used to allow for bitfields
3310 * across the multiple codec registers.
3311 *
3312 * Returns 0 for success.
3313 */
3314int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3315 struct snd_ctl_elem_value *ucontrol)
3316{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003317 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003318 struct soc_mreg_control *mc =
3319 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003320 unsigned int regbase = mc->regbase;
3321 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003322 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003323 unsigned int regwmask = (1<<regwshift)-1;
3324 unsigned int invert = mc->invert;
3325 unsigned long mask = (1UL<<mc->nbits)-1;
3326 long min = mc->min;
3327 long max = mc->max;
3328 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003329 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003330 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003331 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003332
3333 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003334 ret = snd_soc_component_read(component, regbase+i, &regval);
3335 if (ret)
3336 return ret;
3337 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003338 }
3339 val &= mask;
3340 if (min < 0 && val > max)
3341 val |= ~mask;
3342 if (invert)
3343 val = max - val;
3344 ucontrol->value.integer.value[0] = val;
3345
3346 return 0;
3347}
3348EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3349
3350/**
3351 * snd_soc_put_xr_sx - signed multi register get callback
3352 * @kcontrol: mreg control
3353 * @ucontrol: control element information
3354 *
3355 * Callback to set the value of a control that can span
3356 * multiple codec registers which together forms a single
3357 * signed value in a MSB/LSB manner. The control supports
3358 * specifying total no of bits used to allow for bitfields
3359 * across the multiple codec registers.
3360 *
3361 * Returns 0 for success.
3362 */
3363int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3364 struct snd_ctl_elem_value *ucontrol)
3365{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003366 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003367 struct soc_mreg_control *mc =
3368 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003369 unsigned int regbase = mc->regbase;
3370 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003371 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003372 unsigned int regwmask = (1<<regwshift)-1;
3373 unsigned int invert = mc->invert;
3374 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003375 long max = mc->max;
3376 long val = ucontrol->value.integer.value[0];
3377 unsigned int i, regval, regmask;
3378 int err;
3379
3380 if (invert)
3381 val = max - val;
3382 val &= mask;
3383 for (i = 0; i < regcount; i++) {
3384 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3385 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003386 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003387 regmask, regval);
3388 if (err < 0)
3389 return err;
3390 }
3391
3392 return 0;
3393}
3394EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3395
3396/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003397 * snd_soc_get_strobe - strobe get callback
3398 * @kcontrol: mixer control
3399 * @ucontrol: control element information
3400 *
3401 * Callback get the value of a strobe mixer control.
3402 *
3403 * Returns 0 for success.
3404 */
3405int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3406 struct snd_ctl_elem_value *ucontrol)
3407{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003408 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003409 struct soc_mixer_control *mc =
3410 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003411 unsigned int reg = mc->reg;
3412 unsigned int shift = mc->shift;
3413 unsigned int mask = 1 << shift;
3414 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003415 unsigned int val;
3416 int ret;
3417
3418 ret = snd_soc_component_read(component, reg, &val);
3419 if (ret)
3420 return ret;
3421
3422 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003423
3424 if (shift != 0 && val != 0)
3425 val = val >> shift;
3426 ucontrol->value.enumerated.item[0] = val ^ invert;
3427
3428 return 0;
3429}
3430EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3431
3432/**
3433 * snd_soc_put_strobe - strobe put callback
3434 * @kcontrol: mixer control
3435 * @ucontrol: control element information
3436 *
3437 * Callback strobe a register bit to high then low (or the inverse)
3438 * in one pass of a single mixer enum control.
3439 *
3440 * Returns 1 for success.
3441 */
3442int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3443 struct snd_ctl_elem_value *ucontrol)
3444{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003445 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003446 struct soc_mixer_control *mc =
3447 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003448 unsigned int reg = mc->reg;
3449 unsigned int shift = mc->shift;
3450 unsigned int mask = 1 << shift;
3451 unsigned int invert = mc->invert != 0;
3452 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3453 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3454 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3455 int err;
3456
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003457 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003458 if (err < 0)
3459 return err;
3460
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003461 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003462}
3463EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3464
3465/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003466 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3467 * @dai: DAI
3468 * @clk_id: DAI specific clock ID
3469 * @freq: new clock frequency in Hz
3470 * @dir: new clock direction - input/output.
3471 *
3472 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3473 */
3474int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3475 unsigned int freq, int dir)
3476{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003477 if (dai->driver && dai->driver->ops->set_sysclk)
3478 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003479 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003480 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003481 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003482 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003483 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003484}
3485EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3486
3487/**
Mark Brownec4ee522011-03-07 20:58:11 +00003488 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3489 * @codec: CODEC
3490 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003491 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003492 * @freq: new clock frequency in Hz
3493 * @dir: new clock direction - input/output.
3494 *
3495 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3496 */
3497int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003498 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003499{
3500 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003501 return codec->driver->set_sysclk(codec, clk_id, source,
3502 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003503 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003504 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003505}
3506EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3507
3508/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003509 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3510 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003511 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003512 * @div: new clock divisor.
3513 *
3514 * Configures the clock dividers. This is used to derive the best DAI bit and
3515 * frame clocks from the system or master clock. It's best to set the DAI bit
3516 * and frame clocks as low as possible to save system power.
3517 */
3518int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3519 int div_id, int div)
3520{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003521 if (dai->driver && dai->driver->ops->set_clkdiv)
3522 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003523 else
3524 return -EINVAL;
3525}
3526EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3527
3528/**
3529 * snd_soc_dai_set_pll - configure DAI PLL.
3530 * @dai: DAI
3531 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003532 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003533 * @freq_in: PLL input clock frequency in Hz
3534 * @freq_out: requested PLL output clock frequency in Hz
3535 *
3536 * Configures and enables PLL to generate output clock based on input clock.
3537 */
Mark Brown85488032009-09-05 18:52:16 +01003538int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3539 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003540{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003541 if (dai->driver && dai->driver->ops->set_pll)
3542 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003543 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003544 else if (dai->codec && dai->codec->driver->set_pll)
3545 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3546 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003547 else
3548 return -EINVAL;
3549}
3550EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3551
Mark Brownec4ee522011-03-07 20:58:11 +00003552/*
3553 * snd_soc_codec_set_pll - configure codec PLL.
3554 * @codec: CODEC
3555 * @pll_id: DAI specific PLL ID
3556 * @source: DAI specific source for the PLL
3557 * @freq_in: PLL input clock frequency in Hz
3558 * @freq_out: requested PLL output clock frequency in Hz
3559 *
3560 * Configures and enables PLL to generate output clock based on input clock.
3561 */
3562int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3563 unsigned int freq_in, unsigned int freq_out)
3564{
3565 if (codec->driver->set_pll)
3566 return codec->driver->set_pll(codec, pll_id, source,
3567 freq_in, freq_out);
3568 else
3569 return -EINVAL;
3570}
3571EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3572
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003573/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003574 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3575 * @dai: DAI
3576 * @ratio Ratio of BCLK to Sample rate.
3577 *
3578 * Configures the DAI for a preset BCLK to sample rate ratio.
3579 */
3580int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3581{
3582 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3583 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3584 else
3585 return -EINVAL;
3586}
3587EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3588
3589/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003590 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3591 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003592 * @fmt: SND_SOC_DAIFMT_ format value.
3593 *
3594 * Configures the DAI hardware format and clocking.
3595 */
3596int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3597{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003598 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003599 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003600 if (dai->driver->ops->set_fmt == NULL)
3601 return -ENOTSUPP;
3602 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003603}
3604EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3605
3606/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003607 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003608 * @slots: Number of slots in use.
3609 * @tx_mask: bitmask representing active TX slots.
3610 * @rx_mask: bitmask representing active RX slots.
3611 *
3612 * Generates the TDM tx and rx slot default masks for DAI.
3613 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003614static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003615 unsigned int *tx_mask,
3616 unsigned int *rx_mask)
3617{
3618 if (*tx_mask || *rx_mask)
3619 return 0;
3620
3621 if (!slots)
3622 return -EINVAL;
3623
3624 *tx_mask = (1 << slots) - 1;
3625 *rx_mask = (1 << slots) - 1;
3626
3627 return 0;
3628}
3629
3630/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003631 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3632 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003633 * @tx_mask: bitmask representing active TX slots.
3634 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003635 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003636 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003637 *
3638 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3639 * specific.
3640 */
3641int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003642 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003643{
Xiubo Lie5c21512014-03-21 14:17:12 +08003644 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3645 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003646 &tx_mask, &rx_mask);
3647 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003648 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003649
Benoit Cousson88bd8702014-07-08 23:19:34 +02003650 dai->tx_mask = tx_mask;
3651 dai->rx_mask = rx_mask;
3652
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003653 if (dai->driver && dai->driver->ops->set_tdm_slot)
3654 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003655 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003656 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003657 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003658}
3659EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3660
3661/**
Barry Song472df3c2009-09-12 01:16:29 +08003662 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3663 * @dai: DAI
3664 * @tx_num: how many TX channels
3665 * @tx_slot: pointer to an array which imply the TX slot number channel
3666 * 0~num-1 uses
3667 * @rx_num: how many RX channels
3668 * @rx_slot: pointer to an array which imply the RX slot number channel
3669 * 0~num-1 uses
3670 *
3671 * configure the relationship between channel number and TDM slot number.
3672 */
3673int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3674 unsigned int tx_num, unsigned int *tx_slot,
3675 unsigned int rx_num, unsigned int *rx_slot)
3676{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003677 if (dai->driver && dai->driver->ops->set_channel_map)
3678 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003679 rx_num, rx_slot);
3680 else
3681 return -EINVAL;
3682}
3683EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3684
3685/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003686 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3687 * @dai: DAI
3688 * @tristate: tristate enable
3689 *
3690 * Tristates the DAI so that others can use it.
3691 */
3692int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3693{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003694 if (dai->driver && dai->driver->ops->set_tristate)
3695 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003696 else
3697 return -EINVAL;
3698}
3699EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3700
3701/**
3702 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3703 * @dai: DAI
3704 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003705 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003706 *
3707 * Mutes the DAI DAC.
3708 */
Mark Brownda183962013-02-06 15:44:07 +00003709int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3710 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003711{
Mark Brownda183962013-02-06 15:44:07 +00003712 if (!dai->driver)
3713 return -ENOTSUPP;
3714
3715 if (dai->driver->ops->mute_stream)
3716 return dai->driver->ops->mute_stream(dai, mute, direction);
3717 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3718 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003719 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003720 else
Mark Brown04570c62012-04-13 19:16:03 +01003721 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003722}
3723EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3724
Benoit Cousson88bd8702014-07-08 23:19:34 +02003725static int snd_soc_init_multicodec(struct snd_soc_card *card,
3726 struct snd_soc_dai_link *dai_link)
3727{
3728 /* Legacy codec/codec_dai link is a single entry in multicodec */
3729 if (dai_link->codec_name || dai_link->codec_of_node ||
3730 dai_link->codec_dai_name) {
3731 dai_link->num_codecs = 1;
3732
3733 dai_link->codecs = devm_kzalloc(card->dev,
3734 sizeof(struct snd_soc_dai_link_component),
3735 GFP_KERNEL);
3736 if (!dai_link->codecs)
3737 return -ENOMEM;
3738
3739 dai_link->codecs[0].name = dai_link->codec_name;
3740 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3741 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3742 }
3743
3744 if (!dai_link->codecs) {
3745 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3746 return -EINVAL;
3747 }
3748
3749 return 0;
3750}
3751
Mark Brownc5af3a22008-11-28 13:29:45 +00003752/**
3753 * snd_soc_register_card - Register a card with the ASoC core
3754 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003755 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003756 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003757 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303758int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003759{
Benoit Cousson88bd8702014-07-08 23:19:34 +02003760 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003761
Mark Brownc5af3a22008-11-28 13:29:45 +00003762 if (!card->name || !card->dev)
3763 return -EINVAL;
3764
Stephen Warren5a504962011-12-21 10:40:59 -07003765 for (i = 0; i < card->num_links; i++) {
3766 struct snd_soc_dai_link *link = &card->dai_link[i];
3767
Benoit Cousson88bd8702014-07-08 23:19:34 +02003768 ret = snd_soc_init_multicodec(card, link);
3769 if (ret) {
3770 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3771 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07003772 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02003773
3774 for (j = 0; j < link->num_codecs; j++) {
3775 /*
3776 * Codec must be specified by 1 of name or OF node,
3777 * not both or neither.
3778 */
3779 if (!!link->codecs[j].name ==
3780 !!link->codecs[j].of_node) {
3781 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3782 link->name);
3783 return -EINVAL;
3784 }
3785 /* Codec DAI name must be specified */
3786 if (!link->codecs[j].dai_name) {
3787 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3788 link->name);
3789 return -EINVAL;
3790 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003791 }
Stephen Warren5a504962011-12-21 10:40:59 -07003792
3793 /*
3794 * Platform may be specified by either name or OF node, but
3795 * can be left unspecified, and a dummy platform will be used.
3796 */
3797 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003798 dev_err(card->dev,
3799 "ASoC: Both platform name/of_node are set for %s\n",
3800 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003801 return -EINVAL;
3802 }
3803
3804 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003805 * CPU device may be specified by either name or OF node, but
3806 * can be left unspecified, and will be matched based on DAI
3807 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003808 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003809 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003810 dev_err(card->dev,
3811 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3812 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003813 return -EINVAL;
3814 }
3815 /*
3816 * At least one of CPU DAI name or CPU device name/node must be
3817 * specified
3818 */
3819 if (!link->cpu_dai_name &&
3820 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003821 dev_err(card->dev,
3822 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3823 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003824 return -EINVAL;
3825 }
3826 }
3827
Mark Browned77cc12011-05-03 18:25:34 +01003828 dev_set_drvdata(card->dev, card);
3829
Stephen Warren111c6412011-01-28 14:26:35 -07003830 snd_soc_initialize_card_lists(card);
3831
Vinod Koul150dd2f2011-01-13 22:48:32 +05303832 soc_init_card_debugfs(card);
3833
Mark Brown181a6892012-03-14 20:18:49 +00003834 card->rtd = devm_kzalloc(card->dev,
3835 sizeof(struct snd_soc_pcm_runtime) *
3836 (card->num_links + card->num_aux_devs),
3837 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003838 if (card->rtd == NULL)
3839 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003840 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003841 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003842
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003843 for (i = 0; i < card->num_links; i++) {
3844 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003845 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02003846 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3847 sizeof(struct snd_soc_dai *) *
3848 (card->rtd[i].dai_link->num_codecs),
3849 GFP_KERNEL);
3850 if (card->rtd[i].codec_dais == NULL)
3851 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003852 }
3853
3854 for (i = 0; i < card->num_aux_devs; i++)
3855 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003856
Mark Browndb432b42011-10-03 21:06:40 +01003857 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003858 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003859 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003860 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003861
Mark Brownb19e6e72012-03-14 21:18:39 +00003862 ret = snd_soc_instantiate_card(card);
3863 if (ret != 0)
3864 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003865
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003866 /* deactivate pins to sleep state */
3867 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02003868 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3869 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3870 int j;
3871
3872 for (j = 0; j < rtd->num_codecs; j++) {
3873 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3874 if (!codec_dai->active)
3875 pinctrl_pm_select_sleep_state(codec_dai->dev);
3876 }
3877
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003878 if (!cpu_dai->active)
3879 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3880 }
3881
Mark Brownb19e6e72012-03-14 21:18:39 +00003882 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003883}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303884EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003885
3886/**
3887 * snd_soc_unregister_card - Unregister a card with the ASoC core
3888 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003889 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003890 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003891 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303892int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003893{
Vinod Koulb0e26482011-01-13 22:48:02 +05303894 if (card->instantiated)
3895 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003896 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003897
3898 return 0;
3899}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303900EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003901
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003902/*
3903 * Simplify DAI link configuration by removing ".-1" from device names
3904 * and sanitizing names.
3905 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003906static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003907{
3908 char *found, name[NAME_SIZE];
3909 int id1, id2;
3910
3911 if (dev_name(dev) == NULL)
3912 return NULL;
3913
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003914 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003915
3916 /* are we a "%s.%d" name (platform and SPI components) */
3917 found = strstr(name, dev->driver->name);
3918 if (found) {
3919 /* get ID */
3920 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3921
3922 /* discard ID from name if ID == -1 */
3923 if (*id == -1)
3924 found[strlen(dev->driver->name)] = '\0';
3925 }
3926
3927 } else {
3928 /* I2C component devices are named "bus-addr" */
3929 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3930 char tmp[NAME_SIZE];
3931
3932 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003933 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003934
3935 /* sanitize component name for DAI link creation */
3936 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003937 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003938 } else
3939 *id = 0;
3940 }
3941
3942 return kstrdup(name, GFP_KERNEL);
3943}
3944
3945/*
3946 * Simplify DAI link naming for single devices with multiple DAIs by removing
3947 * any ".-1" and using the DAI name (instead of device name).
3948 */
3949static inline char *fmt_multiple_name(struct device *dev,
3950 struct snd_soc_dai_driver *dai_drv)
3951{
3952 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003953 dev_err(dev,
3954 "ASoC: error - multiple DAI %s registered with no name\n",
3955 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003956 return NULL;
3957 }
3958
3959 return kstrdup(dai_drv->name, GFP_KERNEL);
3960}
3961
Mark Brown91151712008-11-30 23:31:24 +00003962/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003963 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003964 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003965 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003966 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003967static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003968{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003969 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003970
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003971 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003972 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3973 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003974 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003975 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003976 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003977 }
Mark Brown91151712008-11-30 23:31:24 +00003978}
Mark Brown91151712008-11-30 23:31:24 +00003979
3980/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003981 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003982 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003983 * @component: The component the DAIs are registered for
3984 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003985 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003986 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3987 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003988 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003989static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003990 struct snd_soc_dai_driver *dai_drv, size_t count,
3991 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003992{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003993 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003994 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003995 unsigned int i;
3996 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003997
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003998 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003999
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004000 component->dai_drv = dai_drv;
4001 component->num_dai = count;
4002
Mark Brown91151712008-11-30 23:31:24 +00004003 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004004
4005 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08004006 if (dai == NULL) {
4007 ret = -ENOMEM;
4008 goto err;
4009 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004010
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004011 /*
4012 * Back in the old days when we still had component-less DAIs,
4013 * instead of having a static name, component-less DAIs would
4014 * inherit the name of the parent device so it is possible to
4015 * register multiple instances of the DAI. We still need to keep
4016 * the same naming style even though those DAIs are not
4017 * component-less anymore.
4018 */
4019 if (count == 1 && legacy_dai_naming) {
4020 dai->name = fmt_single_name(dev, &dai->id);
4021 } else {
4022 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
4023 if (dai_drv[i].id)
4024 dai->id = dai_drv[i].id;
4025 else
4026 dai->id = i;
4027 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004028 if (dai->name == NULL) {
4029 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004030 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00004031 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004032 }
4033
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004034 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004035 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004036 dai->driver = &dai_drv[i];
4037 if (!dai->driver->ops)
4038 dai->driver->ops = &null_dai_ops;
4039
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01004040 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01004041
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004042 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00004043 }
4044
4045 return 0;
4046
4047err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004048 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00004049
4050 return ret;
4051}
Mark Brown91151712008-11-30 23:31:24 +00004052
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004053static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
4054 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004055{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004056 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004057
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004058 component->driver->seq_notifier(component, type, subseq);
4059}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004060
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004061static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
4062 int event)
4063{
4064 struct snd_soc_component *component = dapm->component;
4065
4066 return component->driver->stream_event(component, event);
4067}
4068
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004069static int snd_soc_component_initialize(struct snd_soc_component *component,
4070 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004071{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004072 struct snd_soc_dapm_context *dapm;
4073
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004074 component->name = fmt_single_name(dev, &component->id);
4075 if (!component->name) {
4076 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004077 return -ENOMEM;
4078 }
4079
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004080 component->dev = dev;
4081 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004082
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004083 if (!component->dapm_ptr)
4084 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004085
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004086 dapm = component->dapm_ptr;
4087 dapm->dev = dev;
4088 dapm->component = component;
4089 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004090 if (driver->seq_notifier)
4091 dapm->seq_notifier = snd_soc_component_seq_notifier;
4092 if (driver->stream_event)
4093 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004094
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004095 INIT_LIST_HEAD(&component->dai_list);
4096 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004097
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004098 return 0;
4099}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004100
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004101static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4102{
4103 list_add(&component->list, &component_list);
4104}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004105
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004106static void snd_soc_component_add(struct snd_soc_component *component)
4107{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004108 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004109 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004110 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004111}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004112
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004113static void snd_soc_component_cleanup(struct snd_soc_component *component)
4114{
4115 snd_soc_unregister_dais(component);
4116 kfree(component->name);
4117}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004118
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004119static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4120{
4121 list_del(&component->list);
4122}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004123
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004124static void snd_soc_component_del(struct snd_soc_component *component)
4125{
4126 mutex_lock(&client_mutex);
4127 snd_soc_component_del_unlocked(component);
4128 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004129}
4130
4131int snd_soc_register_component(struct device *dev,
4132 const struct snd_soc_component_driver *cmpnt_drv,
4133 struct snd_soc_dai_driver *dai_drv,
4134 int num_dai)
4135{
4136 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004137 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004138
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004139 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004140 if (!cmpnt) {
4141 dev_err(dev, "ASoC: Failed to allocate memory\n");
4142 return -ENOMEM;
4143 }
4144
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004145 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4146 if (ret)
4147 goto err_free;
4148
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004149 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004150 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004151
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004152 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4153 if (ret < 0) {
4154 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4155 goto err_cleanup;
4156 }
4157
4158 snd_soc_component_add(cmpnt);
4159
4160 return 0;
4161
4162err_cleanup:
4163 snd_soc_component_cleanup(cmpnt);
4164err_free:
4165 kfree(cmpnt);
4166 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004167}
4168EXPORT_SYMBOL_GPL(snd_soc_register_component);
4169
4170/**
4171 * snd_soc_unregister_component - Unregister a component from the ASoC core
4172 *
4173 */
4174void snd_soc_unregister_component(struct device *dev)
4175{
4176 struct snd_soc_component *cmpnt;
4177
4178 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004179 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004180 goto found;
4181 }
4182 return;
4183
4184found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004185 snd_soc_component_del(cmpnt);
4186 snd_soc_component_cleanup(cmpnt);
4187 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004188}
4189EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4190
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004191static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4192 unsigned int reg, unsigned int val)
4193{
4194 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4195
4196 return platform->driver->write(platform, reg, val);
4197}
4198
4199static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4200 unsigned int reg, unsigned int *val)
4201{
4202 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4203
4204 *val = platform->driver->read(platform, reg);
4205
4206 return 0;
4207}
4208
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004209/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004210 * snd_soc_add_platform - Add a platform to the ASoC core
4211 * @dev: The parent device for the platform
4212 * @platform: The platform to add
4213 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004214 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004215int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57db82013-03-27 12:02:22 +01004216 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004217{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004218 int ret;
4219
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004220 ret = snd_soc_component_initialize(&platform->component,
4221 &platform_drv->component_driver, dev);
4222 if (ret)
4223 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004224
4225 platform->dev = dev;
4226 platform->driver = platform_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004227 if (platform_drv->write)
4228 platform->component.write = snd_soc_platform_drv_write;
4229 if (platform_drv->read)
4230 platform->component.read = snd_soc_platform_drv_read;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004231
4232 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004233 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004234 list_add(&platform->list, &platform_list);
4235 mutex_unlock(&client_mutex);
4236
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004237 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4238 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004239
4240 return 0;
4241}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004242EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4243
4244/**
4245 * snd_soc_register_platform - Register a platform with the ASoC core
4246 *
4247 * @platform: platform to register
4248 */
4249int snd_soc_register_platform(struct device *dev,
4250 const struct snd_soc_platform_driver *platform_drv)
4251{
4252 struct snd_soc_platform *platform;
4253 int ret;
4254
4255 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4256
4257 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4258 if (platform == NULL)
4259 return -ENOMEM;
4260
4261 ret = snd_soc_add_platform(dev, platform, platform_drv);
4262 if (ret)
4263 kfree(platform);
4264
4265 return ret;
4266}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004267EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4268
4269/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004270 * snd_soc_remove_platform - Remove a platform from the ASoC core
4271 * @platform: the platform to remove
4272 */
4273void snd_soc_remove_platform(struct snd_soc_platform *platform)
4274{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004275
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004276 mutex_lock(&client_mutex);
4277 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004278 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004279 mutex_unlock(&client_mutex);
4280
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004281 snd_soc_component_cleanup(&platform->component);
4282
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004283 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004284 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004285}
4286EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4287
4288struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4289{
4290 struct snd_soc_platform *platform;
4291
4292 list_for_each_entry(platform, &platform_list, list) {
4293 if (dev == platform->dev)
4294 return platform;
4295 }
4296
4297 return NULL;
4298}
4299EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4300
4301/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004302 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4303 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004304 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004305 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004306void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004307{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004308 struct snd_soc_platform *platform;
4309
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004310 platform = snd_soc_lookup_platform(dev);
4311 if (!platform)
4312 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004313
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004314 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004315 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004316}
4317EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4318
Mark Brown151ab222009-05-09 16:22:58 +01004319static u64 codec_format_map[] = {
4320 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4321 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4322 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4323 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4324 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4325 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4326 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4327 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4328 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4329 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4330 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4331 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4332 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4333 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4334 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4335 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4336};
4337
4338/* Fix up the DAI formats for endianness: codecs don't actually see
4339 * the endianness of the data but we're using the CPU format
4340 * definitions which do need to include endianness so we ensure that
4341 * codec DAIs always have both big and little endian variants set.
4342 */
4343static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4344{
4345 int i;
4346
4347 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4348 if (stream->formats & codec_format_map[i])
4349 stream->formats |= codec_format_map[i];
4350}
4351
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004352static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4353 unsigned int reg, unsigned int val)
4354{
4355 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4356
4357 return codec->driver->write(codec, reg, val);
4358}
4359
4360static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4361 unsigned int reg, unsigned int *val)
4362{
4363 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4364
4365 *val = codec->driver->read(codec, reg);
4366
4367 return 0;
4368}
4369
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004370static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4371 enum snd_soc_bias_level level)
4372{
4373 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4374
4375 return codec->driver->set_bias_level(codec, level);
4376}
4377
Mark Brown0d0cf002008-12-10 14:32:45 +00004378/**
4379 * snd_soc_register_codec - Register a codec with the ASoC core
4380 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004381 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004382 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004383int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004384 const struct snd_soc_codec_driver *codec_drv,
4385 struct snd_soc_dai_driver *dai_drv,
4386 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004387{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004388 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004389 struct snd_soc_dai *dai;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004390 struct regmap *regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004391 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004392
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004393 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004394
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004395 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4396 if (codec == NULL)
4397 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004398
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004399 codec->component.dapm_ptr = &codec->dapm;
4400
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004401 ret = snd_soc_component_initialize(&codec->component,
4402 &codec_drv->component_driver, dev);
4403 if (ret)
4404 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01004405
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004406 if (codec_drv->write)
4407 codec->component.write = snd_soc_codec_drv_write;
4408 if (codec_drv->read)
4409 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004410 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004411 codec->dapm.codec = codec;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004412 if (codec_drv->seq_notifier)
4413 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004414 if (codec_drv->set_bias_level)
4415 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004416 codec->dev = dev;
4417 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004418 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004419 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004420
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004421 if (!codec->component.write) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004422 if (codec_drv->get_regmap)
4423 regmap = codec_drv->get_regmap(dev);
4424 else
4425 regmap = dev_get_regmap(dev, NULL);
4426
4427 if (regmap) {
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004428 ret = snd_soc_component_init_io(&codec->component,
4429 regmap);
4430 if (ret) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004431 dev_err(codec->dev,
4432 "Failed to set cache I/O:%d\n",
4433 ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004434 goto err_cleanup;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004435 }
4436 }
4437 }
4438
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004439 for (i = 0; i < num_dai; i++) {
4440 fixup_codec_formats(&dai_drv[i].playback);
4441 fixup_codec_formats(&dai_drv[i].capture);
4442 }
4443
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004444 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4445 if (ret < 0) {
4446 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4447 goto err_cleanup;
4448 }
4449
4450 list_for_each_entry(dai, &codec->component.dai_list, list)
4451 dai->codec = codec;
4452
Mark Brown054880f2012-04-09 17:29:19 +01004453 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004454 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004455 list_add(&codec->list, &codec_list);
4456 mutex_unlock(&client_mutex);
4457
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004458 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4459 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004460 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004461
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004462err_cleanup:
4463 snd_soc_component_cleanup(&codec->component);
4464err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004465 kfree(codec);
4466 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004467}
4468EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4469
4470/**
4471 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4472 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004473 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004474 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004475void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004476{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004477 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004478
4479 list_for_each_entry(codec, &codec_list, list) {
4480 if (dev == codec->dev)
4481 goto found;
4482 }
4483 return;
4484
4485found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004486
Mark Brown0d0cf002008-12-10 14:32:45 +00004487 mutex_lock(&client_mutex);
4488 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004489 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004490 mutex_unlock(&client_mutex);
4491
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004492 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4493 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004494
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004495 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004496 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004497 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004498}
4499EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4500
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004501/* Retrieve a card's name from device tree */
4502int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4503 const char *propname)
4504{
4505 struct device_node *np = card->dev->of_node;
4506 int ret;
4507
4508 ret = of_property_read_string_index(np, propname, 0, &card->name);
4509 /*
4510 * EINVAL means the property does not exist. This is fine providing
4511 * card->name was previously set, which is checked later in
4512 * snd_soc_register_card.
4513 */
4514 if (ret < 0 && ret != -EINVAL) {
4515 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004516 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004517 propname, ret);
4518 return ret;
4519 }
4520
4521 return 0;
4522}
4523EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4524
Xiubo Li9a6d4862014-02-08 15:59:52 +08004525static const struct snd_soc_dapm_widget simple_widgets[] = {
4526 SND_SOC_DAPM_MIC("Microphone", NULL),
4527 SND_SOC_DAPM_LINE("Line", NULL),
4528 SND_SOC_DAPM_HP("Headphone", NULL),
4529 SND_SOC_DAPM_SPK("Speaker", NULL),
4530};
4531
4532int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4533 const char *propname)
4534{
4535 struct device_node *np = card->dev->of_node;
4536 struct snd_soc_dapm_widget *widgets;
4537 const char *template, *wname;
4538 int i, j, num_widgets, ret;
4539
4540 num_widgets = of_property_count_strings(np, propname);
4541 if (num_widgets < 0) {
4542 dev_err(card->dev,
4543 "ASoC: Property '%s' does not exist\n", propname);
4544 return -EINVAL;
4545 }
4546 if (num_widgets & 1) {
4547 dev_err(card->dev,
4548 "ASoC: Property '%s' length is not even\n", propname);
4549 return -EINVAL;
4550 }
4551
4552 num_widgets /= 2;
4553 if (!num_widgets) {
4554 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4555 propname);
4556 return -EINVAL;
4557 }
4558
4559 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4560 GFP_KERNEL);
4561 if (!widgets) {
4562 dev_err(card->dev,
4563 "ASoC: Could not allocate memory for widgets\n");
4564 return -ENOMEM;
4565 }
4566
4567 for (i = 0; i < num_widgets; i++) {
4568 ret = of_property_read_string_index(np, propname,
4569 2 * i, &template);
4570 if (ret) {
4571 dev_err(card->dev,
4572 "ASoC: Property '%s' index %d read error:%d\n",
4573 propname, 2 * i, ret);
4574 return -EINVAL;
4575 }
4576
4577 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4578 if (!strncmp(template, simple_widgets[j].name,
4579 strlen(simple_widgets[j].name))) {
4580 widgets[i] = simple_widgets[j];
4581 break;
4582 }
4583 }
4584
4585 if (j >= ARRAY_SIZE(simple_widgets)) {
4586 dev_err(card->dev,
4587 "ASoC: DAPM widget '%s' is not supported\n",
4588 template);
4589 return -EINVAL;
4590 }
4591
4592 ret = of_property_read_string_index(np, propname,
4593 (2 * i) + 1,
4594 &wname);
4595 if (ret) {
4596 dev_err(card->dev,
4597 "ASoC: Property '%s' index %d read error:%d\n",
4598 propname, (2 * i) + 1, ret);
4599 return -EINVAL;
4600 }
4601
4602 widgets[i].name = wname;
4603 }
4604
4605 card->dapm_widgets = widgets;
4606 card->num_dapm_widgets = num_widgets;
4607
4608 return 0;
4609}
4610EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4611
Xiubo Li89c67852014-02-14 09:34:35 +08004612int snd_soc_of_parse_tdm_slot(struct device_node *np,
4613 unsigned int *slots,
4614 unsigned int *slot_width)
4615{
4616 u32 val;
4617 int ret;
4618
4619 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4620 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4621 if (ret)
4622 return ret;
4623
4624 if (slots)
4625 *slots = val;
4626 }
4627
4628 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4629 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4630 if (ret)
4631 return ret;
4632
4633 if (slot_width)
4634 *slot_width = val;
4635 }
4636
4637 return 0;
4638}
4639EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4640
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004641int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4642 const char *propname)
4643{
4644 struct device_node *np = card->dev->of_node;
4645 int num_routes;
4646 struct snd_soc_dapm_route *routes;
4647 int i, ret;
4648
4649 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004650 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004651 dev_err(card->dev,
4652 "ASoC: Property '%s' does not exist or its length is not even\n",
4653 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004654 return -EINVAL;
4655 }
4656 num_routes /= 2;
4657 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004658 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004659 propname);
4660 return -EINVAL;
4661 }
4662
4663 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4664 GFP_KERNEL);
4665 if (!routes) {
4666 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004667 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004668 return -EINVAL;
4669 }
4670
4671 for (i = 0; i < num_routes; i++) {
4672 ret = of_property_read_string_index(np, propname,
4673 2 * i, &routes[i].sink);
4674 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004675 dev_err(card->dev,
4676 "ASoC: Property '%s' index %d could not be read: %d\n",
4677 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004678 return -EINVAL;
4679 }
4680 ret = of_property_read_string_index(np, propname,
4681 (2 * i) + 1, &routes[i].source);
4682 if (ret) {
4683 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004684 "ASoC: Property '%s' index %d could not be read: %d\n",
4685 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004686 return -EINVAL;
4687 }
4688 }
4689
4690 card->num_dapm_routes = num_routes;
4691 card->dapm_routes = routes;
4692
4693 return 0;
4694}
4695EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4696
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004697unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004698 const char *prefix,
4699 struct device_node **bitclkmaster,
4700 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004701{
4702 int ret, i;
4703 char prop[128];
4704 unsigned int format = 0;
4705 int bit, frame;
4706 const char *str;
4707 struct {
4708 char *name;
4709 unsigned int val;
4710 } of_fmt_table[] = {
4711 { "i2s", SND_SOC_DAIFMT_I2S },
4712 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4713 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4714 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4715 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4716 { "ac97", SND_SOC_DAIFMT_AC97 },
4717 { "pdm", SND_SOC_DAIFMT_PDM},
4718 { "msb", SND_SOC_DAIFMT_MSB },
4719 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004720 };
4721
4722 if (!prefix)
4723 prefix = "";
4724
4725 /*
4726 * check "[prefix]format = xxx"
4727 * SND_SOC_DAIFMT_FORMAT_MASK area
4728 */
4729 snprintf(prop, sizeof(prop), "%sformat", prefix);
4730 ret = of_property_read_string(np, prop, &str);
4731 if (ret == 0) {
4732 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4733 if (strcmp(str, of_fmt_table[i].name) == 0) {
4734 format |= of_fmt_table[i].val;
4735 break;
4736 }
4737 }
4738 }
4739
4740 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004741 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004742 * SND_SOC_DAIFMT_CLOCK_MASK area
4743 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004744 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4745 if (of_get_property(np, prop, NULL))
4746 format |= SND_SOC_DAIFMT_CONT;
4747 else
4748 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004749
4750 /*
4751 * check "[prefix]bitclock-inversion"
4752 * check "[prefix]frame-inversion"
4753 * SND_SOC_DAIFMT_INV_MASK area
4754 */
4755 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4756 bit = !!of_get_property(np, prop, NULL);
4757
4758 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4759 frame = !!of_get_property(np, prop, NULL);
4760
4761 switch ((bit << 4) + frame) {
4762 case 0x11:
4763 format |= SND_SOC_DAIFMT_IB_IF;
4764 break;
4765 case 0x10:
4766 format |= SND_SOC_DAIFMT_IB_NF;
4767 break;
4768 case 0x01:
4769 format |= SND_SOC_DAIFMT_NB_IF;
4770 break;
4771 default:
4772 /* SND_SOC_DAIFMT_NB_NF is default */
4773 break;
4774 }
4775
4776 /*
4777 * check "[prefix]bitclock-master"
4778 * check "[prefix]frame-master"
4779 * SND_SOC_DAIFMT_MASTER_MASK area
4780 */
4781 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4782 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004783 if (bit && bitclkmaster)
4784 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004785
4786 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4787 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004788 if (frame && framemaster)
4789 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004790
4791 switch ((bit << 4) + frame) {
4792 case 0x11:
4793 format |= SND_SOC_DAIFMT_CBM_CFM;
4794 break;
4795 case 0x10:
4796 format |= SND_SOC_DAIFMT_CBM_CFS;
4797 break;
4798 case 0x01:
4799 format |= SND_SOC_DAIFMT_CBS_CFM;
4800 break;
4801 default:
4802 format |= SND_SOC_DAIFMT_CBS_CFS;
4803 break;
4804 }
4805
4806 return format;
4807}
4808EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4809
Kuninori Morimotocb470082013-09-10 17:39:56 -07004810int snd_soc_of_get_dai_name(struct device_node *of_node,
4811 const char **dai_name)
4812{
4813 struct snd_soc_component *pos;
4814 struct of_phandle_args args;
4815 int ret;
4816
4817 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4818 "#sound-dai-cells", 0, &args);
4819 if (ret)
4820 return ret;
4821
4822 ret = -EPROBE_DEFER;
4823
4824 mutex_lock(&client_mutex);
4825 list_for_each_entry(pos, &component_list, list) {
4826 if (pos->dev->of_node != args.np)
4827 continue;
4828
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004829 if (pos->driver->of_xlate_dai_name) {
4830 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4831 } else {
4832 int id = -1;
4833
4834 switch (args.args_count) {
4835 case 0:
4836 id = 0; /* same as dai_drv[0] */
4837 break;
4838 case 1:
4839 id = args.args[0];
4840 break;
4841 default:
4842 /* not supported */
4843 break;
4844 }
4845
4846 if (id < 0 || id >= pos->num_dai) {
4847 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004848 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004849 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004850
4851 ret = 0;
4852
4853 *dai_name = pos->dai_drv[id].name;
4854 if (!*dai_name)
4855 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004856 }
4857
Kuninori Morimotocb470082013-09-10 17:39:56 -07004858 break;
4859 }
4860 mutex_unlock(&client_mutex);
4861
4862 of_node_put(args.np);
4863
4864 return ret;
4865}
4866EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4867
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004868static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004869{
Mark Brown384c89e2008-12-03 17:34:03 +00004870#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004871 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4872 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004873 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004874 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004875 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004876
Mark Brown8a9dab12011-01-10 22:25:21 +00004877 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004878 &codec_list_fops))
4879 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4880
Mark Brown8a9dab12011-01-10 22:25:21 +00004881 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004882 &dai_list_fops))
4883 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004884
Mark Brown8a9dab12011-01-10 22:25:21 +00004885 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004886 &platform_list_fops))
4887 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004888#endif
4889
Mark Brownfb257892011-04-28 10:57:54 +01004890 snd_soc_util_init();
4891
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004892 return platform_driver_register(&soc_driver);
4893}
Mark Brown4abe8e12010-10-12 17:41:03 +01004894module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004895
Mark Brown7d8c16a2008-11-30 22:11:24 +00004896static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004897{
Mark Brownfb257892011-04-28 10:57:54 +01004898 snd_soc_util_exit();
4899
Mark Brown384c89e2008-12-03 17:34:03 +00004900#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004901 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004902#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004903 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004904}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004905module_exit(snd_soc_exit);
4906
4907/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004908MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004909MODULE_DESCRIPTION("ALSA SoC Core");
4910MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004911MODULE_ALIAS("platform:soc-audio");