blob: a504cf42bf0a0e71bfaed0d89a36e57e394230cb [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{
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200295 struct dentry *debugfs_card_root = codec->component.card->debugfs_card_root;
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200296
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{
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200328 struct dentry *debugfs_card_root = platform->component.card->debugfs_card_root;
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000329
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;
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200549 codec->ac97->dev.parent = codec->component.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 Clausen00200102014-07-17 22:01:07 +0200553 codec->component.card->snd_card->number, 0,
554 codec->component.name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200555 err = device_register(&codec->ac97->dev);
556 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000557 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200558 codec->ac97->dev.bus = NULL;
559 return err;
560 }
561 return 0;
562}
563#endif
564
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100565static void codec2codec_close_delayed_work(struct work_struct *work)
566{
567 /* Currently nothing to do for c2c links
568 * Since c2c links are internal nodes in the DAPM graph and
569 * don't interface with the outside world or application layer
570 * we don't have to do any special handling on close.
571 */
572}
573
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000574#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200575/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000576int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200577{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000578 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200579 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200580 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200581
Daniel Macke3509ff2009-06-03 17:44:49 +0200582 /* If the initialization of this soc device failed, there is no codec
583 * associated with it. Just bail out in this case.
584 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000585 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200586 return 0;
587
Andy Green6ed25972008-06-13 16:24:05 +0100588 /* Due to the resume being scheduled into a workqueue we could
589 * suspend before that's finished - wait for it to complete.
590 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591 snd_power_lock(card->snd_card);
592 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
593 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100594
595 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100597
Mark Browna00f90f2010-12-02 16:24:24 +0000598 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100600
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100602 continue;
603
Benoit Cousson88bd8702014-07-08 23:19:34 +0200604 for (j = 0; j < card->rtd[i].num_codecs; j++) {
605 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
606 struct snd_soc_dai_driver *drv = dai->driver;
607
608 if (drv->ops->digital_mute && dai->playback_active)
609 drv->ops->digital_mute(dai, 1);
610 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200611 }
612
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100613 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000614 for (i = 0; i < card->num_rtd; i++) {
615 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100616 continue;
617
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100619 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100620
Mark Brown87506542008-11-18 20:50:34 +0000621 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000622 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200623
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624 for (i = 0; i < card->num_rtd; i++) {
625 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
626 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100627
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000628 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100629 continue;
630
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
632 cpu_dai->driver->suspend(cpu_dai);
633 if (platform->driver->suspend && !platform->suspended) {
634 platform->driver->suspend(cpu_dai);
635 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100636 }
637 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200638
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 /* close any waiting streams and save state */
640 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200641 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
Tejun Heo43829732012-08-20 14:51:24 -0700642 flush_delayed_work(&card->rtd[i].delayed_work);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200643 for (j = 0; j < card->rtd[i].num_codecs; j++) {
644 codec_dais[j]->codec->dapm.suspend_bias_level =
645 codec_dais[j]->codec->dapm.bias_level;
646 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000647 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100648
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000649 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000650
651 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100652 continue;
653
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800654 snd_soc_dapm_stream_event(&card->rtd[i],
655 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800656 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000657
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800658 snd_soc_dapm_stream_event(&card->rtd[i],
659 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800660 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 }
662
Mark Browne2d32ff2012-08-31 17:38:32 -0700663 /* Recheck all analogue paths too */
664 dapm_mark_io_dirty(&card->dapm);
665 snd_soc_dapm_sync(&card->dapm);
666
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000667 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200668 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000669 /* If there are paths active then the CODEC will be held with
670 * bias _ON and should not be suspended. */
671 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200672 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000674 /*
675 * If the CODEC is capable of idle
676 * bias off then being in STANDBY
677 * means it's doing something,
678 * otherwise fall through.
679 */
680 if (codec->dapm.idle_bias_off) {
681 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200682 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000683 break;
684 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000685 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100686 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000687 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900688 codec->cache_sync = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200689 if (codec->component.regmap)
690 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800691 /* deactivate pins to sleep state */
692 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693 break;
694 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200695 dev_dbg(codec->dev,
696 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000697 break;
698 }
699 }
700 }
701
702 for (i = 0; i < card->num_rtd; i++) {
703 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
704
705 if (card->rtd[i].dai_link->ignore_suspend)
706 continue;
707
708 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
709 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800710
711 /* deactivate pins to sleep state */
712 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200713 }
714
Mark Brown87506542008-11-18 20:50:34 +0000715 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000716 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200717
718 return 0;
719}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000720EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200721
Andy Green6ed25972008-06-13 16:24:05 +0100722/* deferred resume work, so resume can complete before we finished
723 * setting our codec back up, which can be very slow on I2C
724 */
725static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200726{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000727 struct snd_soc_card *card =
728 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200729 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200730 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200731
Andy Green6ed25972008-06-13 16:24:05 +0100732 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
733 * so userspace apps are blocked from touching us
734 */
735
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000736 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100737
Mark Brown99497882010-05-07 20:24:05 +0100738 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000739 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100740
Mark Brown87506542008-11-18 20:50:34 +0000741 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000742 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000744 /* resume AC97 DAIs */
745 for (i = 0; i < card->num_rtd; i++) {
746 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100749 continue;
750
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000751 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
752 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200753 }
754
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200755 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756 /* If the CODEC was idle over suspend then it will have been
757 * left with bias OFF or STANDBY and suspended so we must now
758 * resume. Otherwise the suspend was suppressed.
759 */
760 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200761 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000762 case SND_SOC_BIAS_STANDBY:
763 case SND_SOC_BIAS_OFF:
764 codec->driver->resume(codec);
765 codec->suspended = 0;
766 break;
767 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200768 dev_dbg(codec->dev,
769 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000770 break;
771 }
Mark Brown1547aba2010-05-07 21:11:40 +0100772 }
773 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200774
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000775 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100776
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000777 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100778 continue;
779
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800780 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000781 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800782 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000783
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800784 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000785 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800786 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200787 }
788
Mark Brown3ff3f642008-05-19 12:32:25 +0200789 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000790 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100791
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000792 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100793 continue;
794
Benoit Cousson88bd8702014-07-08 23:19:34 +0200795 for (j = 0; j < card->rtd[i].num_codecs; j++) {
796 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
797 struct snd_soc_dai_driver *drv = dai->driver;
798
799 if (drv->ops->digital_mute && dai->playback_active)
800 drv->ops->digital_mute(dai, 0);
801 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200802 }
803
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000804 for (i = 0; i < card->num_rtd; i++) {
805 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
806 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100807
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000808 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100809 continue;
810
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000811 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
812 cpu_dai->driver->resume(cpu_dai);
813 if (platform->driver->resume && platform->suspended) {
814 platform->driver->resume(cpu_dai);
815 platform->suspended = 0;
816 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200817 }
818
Mark Brown87506542008-11-18 20:50:34 +0000819 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000820 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200821
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000822 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100823
824 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000825 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700826
827 /* Recheck all analogue paths too */
828 dapm_mark_io_dirty(&card->dapm);
829 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100830}
831
832/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000833int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100834{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000835 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600836 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200837
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800838 /* If the initialization of this soc device failed, there is no codec
839 * associated with it. Just bail out in this case.
840 */
841 if (list_empty(&card->codec_dev_list))
842 return 0;
843
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800844 /* activate pins from sleep state */
845 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200846 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
847 struct snd_soc_dai **codec_dais = rtd->codec_dais;
848 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
849 int j;
850
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800851 if (cpu_dai->active)
852 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200853
854 for (j = 0; j < rtd->num_codecs; j++) {
855 struct snd_soc_dai *codec_dai = codec_dais[j];
856 if (codec_dai->active)
857 pinctrl_pm_select_default_state(codec_dai->dev);
858 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800859 }
860
Mark Brown64ab9ba2009-03-31 11:27:03 +0100861 /* AC97 devices might have other drivers hanging off them so
862 * need to resume immediately. Other drivers don't have that
863 * problem and may take a substantial amount of time to resume
864 * due to I/O costs and anti-pop so handle them out of line.
865 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000866 for (i = 0; i < card->num_rtd; i++) {
867 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600868 ac97_control |= cpu_dai->driver->ac97_control;
869 }
870 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000871 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600872 soc_resume_deferred(&card->deferred_resume_work);
873 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000874 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600875 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000876 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100877 }
Andy Green6ed25972008-06-13 16:24:05 +0100878
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200879 return 0;
880}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000881EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200882#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000883#define snd_soc_suspend NULL
884#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200885#endif
886
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100887static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800888};
889
Benoit Cousson88bd8702014-07-08 23:19:34 +0200890static struct snd_soc_codec *soc_find_codec(
891 const struct device_node *codec_of_node,
892 const char *codec_name)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100893{
894 struct snd_soc_codec *codec;
895
896 list_for_each_entry(codec, &codec_list, list) {
897 if (codec_of_node) {
898 if (codec->dev->of_node != codec_of_node)
899 continue;
900 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200901 if (strcmp(codec->component.name, codec_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100902 continue;
903 }
904
905 return codec;
906 }
907
908 return NULL;
909}
910
911static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
912 const char *codec_dai_name)
913{
914 struct snd_soc_dai *codec_dai;
915
916 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
917 if (!strcmp(codec_dai->name, codec_dai_name)) {
918 return codec_dai;
919 }
920 }
921
922 return NULL;
923}
924
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000925static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200926{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000927 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
928 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100929 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200930 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
931 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000932 struct snd_soc_platform *platform;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100933 struct snd_soc_dai *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100934 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200935 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200936
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000937 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000938
Mark Brownb19e6e72012-03-14 21:18:39 +0000939 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100940 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600941 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100942 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600943 continue;
944 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100945 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600946 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100947 list_for_each_entry(cpu_dai, &component->dai_list, list) {
948 if (dai_link->cpu_dai_name &&
949 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
950 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700951
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100952 rtd->cpu_dai = cpu_dai;
953 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000954 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000955
956 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000957 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000958 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000959 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000960 }
961
Benoit Cousson88bd8702014-07-08 23:19:34 +0200962 rtd->num_codecs = dai_link->num_codecs;
963
964 /* Find CODEC from registered CODECs */
965 for (i = 0; i < rtd->num_codecs; i++) {
966 struct snd_soc_codec *codec;
967 codec = soc_find_codec(codecs[i].of_node, codecs[i].name);
968 if (!codec) {
969 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
970 codecs[i].name);
971 return -EPROBE_DEFER;
972 }
973
974 codec_dais[i] = soc_find_codec_dai(codec, codecs[i].dai_name);
975 if (!codec_dais[i]) {
976 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
977 codecs[i].dai_name);
978 return -EPROBE_DEFER;
979 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000980 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100981
Benoit Cousson88bd8702014-07-08 23:19:34 +0200982 /* Single codec links expect codec and codec_dai in runtime data */
983 rtd->codec_dai = codec_dais[0];
984 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100985
Mark Brown848dd8b2011-04-27 18:16:32 +0100986 /* if there's no platform we match on the empty platform */
987 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700988 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100989 platform_name = "snd-soc-dummy";
990
Mark Brownb19e6e72012-03-14 21:18:39 +0000991 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000992 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700993 if (dai_link->platform_of_node) {
994 if (platform->dev->of_node !=
995 dai_link->platform_of_node)
996 continue;
997 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200998 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700999 continue;
1000 }
Stephen Warren2610ab72011-12-07 13:58:27 -07001001
1002 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001003 }
Mark Brownb19e6e72012-03-14 21:18:39 +00001004 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001005 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001006 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001007 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001008 }
Mark Brownb19e6e72012-03-14 21:18:39 +00001009
1010 card->num_rtd++;
1011
1012 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001013}
1014
Stephen Warrend12cd192012-06-08 12:34:22 -06001015static int soc_remove_platform(struct snd_soc_platform *platform)
1016{
1017 int ret;
1018
1019 if (platform->driver->remove) {
1020 ret = platform->driver->remove(platform);
1021 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001022 dev_err(platform->dev, "ASoC: failed to remove %d\n",
1023 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -06001024 }
1025
1026 /* Make sure all DAPM widgets are freed */
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001027 snd_soc_dapm_free(&platform->component.dapm);
Stephen Warrend12cd192012-06-08 12:34:22 -06001028
1029 soc_cleanup_platform_debugfs(platform);
1030 platform->probed = 0;
Stephen Warrend12cd192012-06-08 12:34:22 -06001031 module_put(platform->dev->driver->owner);
1032
1033 return 0;
1034}
1035
Jarkko Nikula589c3562010-12-06 16:27:07 +02001036static void soc_remove_codec(struct snd_soc_codec *codec)
1037{
1038 int err;
1039
1040 if (codec->driver->remove) {
1041 err = codec->driver->remove(codec);
1042 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001043 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001044 }
1045
1046 /* Make sure all DAPM widgets are freed */
1047 snd_soc_dapm_free(&codec->dapm);
1048
1049 soc_cleanup_codec_debugfs(codec);
1050 codec->probed = 0;
1051 list_del(&codec->card_list);
1052 module_put(codec->dev->driver->owner);
1053}
1054
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001055static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1056{
1057 int err;
1058
1059 if (codec_dai && codec_dai->probed &&
1060 codec_dai->driver->remove_order == order) {
1061 if (codec_dai->driver->remove) {
1062 err = codec_dai->driver->remove(codec_dai);
1063 if (err < 0)
1064 dev_err(codec_dai->dev,
1065 "ASoC: failed to remove %s: %d\n",
1066 codec_dai->name, err);
1067 }
1068 codec_dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001069 }
1070}
1071
Stephen Warren62ae68f2012-06-08 12:34:23 -06001072static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001073{
1074 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +02001075 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1076 int i, err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001077
1078 /* unregister the rtd device */
1079 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001080 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1081 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1082 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001083 rtd->dev_registered = 0;
1084 }
1085
1086 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001087 for (i = 0; i < rtd->num_codecs; i++)
1088 soc_remove_codec_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001089
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001090 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001091 if (cpu_dai && cpu_dai->probed &&
1092 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001093 if (cpu_dai->driver->remove) {
1094 err = cpu_dai->driver->remove(cpu_dai);
1095 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001096 dev_err(cpu_dai->dev,
1097 "ASoC: failed to remove %s: %d\n",
1098 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001099 }
1100 cpu_dai->probed = 0;
Lars-Peter Clausen9420d972014-06-16 18:13:10 +02001101 if (!cpu_dai->codec)
Stephen Warrena9db7db2012-06-08 12:34:20 -06001102 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001103 }
1104}
1105
Stephen Warren62ae68f2012-06-08 12:34:23 -06001106static void soc_remove_link_components(struct snd_soc_card *card, int num,
1107 int order)
1108{
1109 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1110 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001111 struct snd_soc_platform *platform = rtd->platform;
1112 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001113 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001114
1115 /* remove the platform */
1116 if (platform && platform->probed &&
1117 platform->driver->remove_order == order) {
1118 soc_remove_platform(platform);
1119 }
1120
1121 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001122 for (i = 0; i < rtd->num_codecs; i++) {
1123 codec = rtd->codec_dais[i]->codec;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001124 if (codec && codec->probed &&
1125 codec->driver->remove_order == order)
1126 soc_remove_codec(codec);
1127 }
1128
1129 /* remove any CPU-side CODEC */
1130 if (cpu_dai) {
1131 codec = cpu_dai->codec;
1132 if (codec && codec->probed &&
1133 codec->driver->remove_order == order)
1134 soc_remove_codec(codec);
1135 }
1136}
1137
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001138static void soc_remove_dai_links(struct snd_soc_card *card)
1139{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001140 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001141
Liam Girdwood0168bf02011-06-07 16:08:05 +01001142 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1143 order++) {
1144 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001145 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001146 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001147
1148 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1149 order++) {
1150 for (dai = 0; dai < card->num_rtd; dai++)
1151 soc_remove_link_components(card, dai, order);
1152 }
1153
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001154 card->num_rtd = 0;
1155}
1156
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001157static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001158 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001159{
1160 int i;
1161
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001162 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001163 return;
1164
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001165 for (i = 0; i < card->num_configs; i++) {
1166 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001167 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001168 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001169 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001170 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001171 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001172 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001173 }
1174}
1175
Jarkko Nikula589c3562010-12-06 16:27:07 +02001176static int soc_probe_codec(struct snd_soc_card *card,
1177 struct snd_soc_codec *codec)
1178{
1179 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001180 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001181 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001182
Lars-Peter Clausen00200102014-07-17 22:01:07 +02001183 codec->component.card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001184 codec->dapm.card = card;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001185 soc_set_name_prefix(card, &codec->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001186
Jarkko Nikula70d29332011-01-27 16:24:22 +02001187 if (!try_module_get(codec->dev->driver->owner))
1188 return -ENODEV;
1189
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001190 soc_init_codec_debugfs(codec);
1191
Nariman Poushinb318ad52014-04-01 13:59:33 +01001192 if (driver->dapm_widgets) {
1193 ret = snd_soc_dapm_new_controls(&codec->dapm,
1194 driver->dapm_widgets,
1195 driver->num_dapm_widgets);
1196
1197 if (ret != 0) {
1198 dev_err(codec->dev,
1199 "Failed to create new controls %d\n", ret);
1200 goto err_probe;
1201 }
1202 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001203
Mark Brown888df392012-02-16 19:37:51 -08001204 /* Create DAPM widgets for each DAI stream */
Nariman Poushin261edc72014-03-31 15:47:12 +01001205 list_for_each_entry(dai, &codec->component.dai_list, list) {
1206 ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1207
1208 if (ret != 0) {
1209 dev_err(codec->dev,
1210 "Failed to create DAI widgets %d\n", ret);
1211 goto err_probe;
1212 }
1213 }
Mark Brown888df392012-02-16 19:37:51 -08001214
Mark Brown33c5f962011-08-22 18:40:30 +01001215 codec->dapm.idle_bias_off = driver->idle_bias_off;
1216
Mark Brown89b95ac02011-03-07 16:38:44 +00001217 if (driver->probe) {
1218 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001219 if (ret < 0) {
1220 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001221 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001222 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001223 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001224 WARN(codec->dapm.idle_bias_off &&
1225 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001226 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02001227 codec->component.name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001228 }
1229
Mark Brownb7af1da2011-04-07 19:18:44 +09001230 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001231 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001232 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001233 if (driver->dapm_routes)
1234 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1235 driver->num_dapm_routes);
1236
Jarkko Nikula589c3562010-12-06 16:27:07 +02001237 /* mark codec as probed and add to card codec list */
1238 codec->probed = 1;
1239 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001240 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001241
Jarkko Nikula70d29332011-01-27 16:24:22 +02001242 return 0;
1243
1244err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001245 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001246 module_put(codec->dev->driver->owner);
1247
Jarkko Nikula589c3562010-12-06 16:27:07 +02001248 return ret;
1249}
1250
Liam Girdwood956245e2011-07-01 16:54:08 +01001251static int soc_probe_platform(struct snd_soc_card *card,
1252 struct snd_soc_platform *platform)
1253{
1254 int ret = 0;
1255 const struct snd_soc_platform_driver *driver = platform->driver;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001256 struct snd_soc_component *component;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001257 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001258
Lars-Peter Clausen00200102014-07-17 22:01:07 +02001259 platform->component.card = card;
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001260 platform->component.dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001261
1262 if (!try_module_get(platform->dev->driver->owner))
1263 return -ENODEV;
1264
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001265 soc_init_platform_debugfs(platform);
1266
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001267 if (driver->dapm_widgets)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001268 snd_soc_dapm_new_controls(&platform->component.dapm,
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001269 driver->dapm_widgets, driver->num_dapm_widgets);
1270
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001271 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001272 list_for_each_entry(component, &component_list, list) {
1273 if (component->dev != platform->dev)
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001274 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001275 list_for_each_entry(dai, &component->dai_list, list)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001276 snd_soc_dapm_new_dai_widgets(&platform->component.dapm,
1277 dai);
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001278 }
1279
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001280 platform->component.dapm.idle_bias_off = 1;
Stephen Warren3fec6b62012-04-05 12:28:01 -06001281
Liam Girdwood956245e2011-07-01 16:54:08 +01001282 if (driver->probe) {
1283 ret = driver->probe(platform);
1284 if (ret < 0) {
1285 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001286 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001287 goto err_probe;
1288 }
1289 }
1290
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001291 if (driver->controls)
1292 snd_soc_add_platform_controls(platform, driver->controls,
1293 driver->num_controls);
1294 if (driver->dapm_routes)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001295 snd_soc_dapm_add_routes(&platform->component.dapm,
1296 driver->dapm_routes, driver->num_dapm_routes);
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001297
Liam Girdwood956245e2011-07-01 16:54:08 +01001298 /* mark platform as probed and add to card platform list */
1299 platform->probed = 1;
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001300 list_add(&platform->component.dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001301
1302 return 0;
1303
1304err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001305 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001306 module_put(platform->dev->driver->owner);
1307
1308 return ret;
1309}
1310
Mark Brown36ae1a92012-01-06 17:12:45 -08001311static void rtd_release(struct device *dev)
1312{
1313 kfree(dev);
1314}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001315
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001316static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1317 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001318{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001319 int ret = 0;
1320
Jarkko Nikula589c3562010-12-06 16:27:07 +02001321 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001322 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1323 if (!rtd->dev)
1324 return -ENOMEM;
1325 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001326 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001327 rtd->dev->release = rtd_release;
1328 rtd->dev->init_name = name;
1329 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001330 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001331 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1332 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1333 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1334 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001335 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001336 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001337 /* calling put_device() here to free the rtd->dev */
1338 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001339 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001340 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001341 return ret;
1342 }
1343 rtd->dev_registered = 1;
1344
1345 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001346 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001347 if (ret < 0)
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001348 dev_err(rtd->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001349 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001350
1351 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001352 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001353 if (ret < 0)
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001354 dev_err(rtd->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001355 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001356
1357 return 0;
1358}
1359
Stephen Warren62ae68f2012-06-08 12:34:23 -06001360static int soc_probe_link_components(struct snd_soc_card *card, int num,
1361 int order)
1362{
1363 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1364 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001365 struct snd_soc_platform *platform = rtd->platform;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001366 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001367
1368 /* probe the CPU-side component, if it is a CODEC */
1369 if (cpu_dai->codec &&
1370 !cpu_dai->codec->probed &&
1371 cpu_dai->codec->driver->probe_order == order) {
1372 ret = soc_probe_codec(card, cpu_dai->codec);
1373 if (ret < 0)
1374 return ret;
1375 }
1376
Benoit Cousson88bd8702014-07-08 23:19:34 +02001377 /* probe the CODEC-side components */
1378 for (i = 0; i < rtd->num_codecs; i++) {
1379 if (!rtd->codec_dais[i]->codec->probed &&
1380 rtd->codec_dais[i]->codec->driver->probe_order == order) {
1381 ret = soc_probe_codec(card, rtd->codec_dais[i]->codec);
1382 if (ret < 0)
1383 return ret;
1384 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001385 }
1386
1387 /* probe the platform */
1388 if (!platform->probed &&
1389 platform->driver->probe_order == order) {
1390 ret = soc_probe_platform(card, platform);
1391 if (ret < 0)
1392 return ret;
1393 }
1394
1395 return 0;
1396}
1397
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001398static int soc_probe_codec_dai(struct snd_soc_card *card,
1399 struct snd_soc_dai *codec_dai,
1400 int order)
1401{
1402 int ret;
1403
1404 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1405 if (codec_dai->driver->probe) {
1406 ret = codec_dai->driver->probe(codec_dai);
1407 if (ret < 0) {
1408 dev_err(codec_dai->dev,
1409 "ASoC: failed to probe CODEC DAI %s: %d\n",
1410 codec_dai->name, ret);
1411 return ret;
1412 }
1413 }
1414
1415 /* mark codec_dai as probed and add to card dai list */
1416 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001417 }
1418
1419 return 0;
1420}
1421
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001422static int soc_link_dai_widgets(struct snd_soc_card *card,
1423 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001424 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001425{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001426 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1427 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001428 struct snd_soc_dapm_widget *play_w, *capture_w;
1429 int ret;
1430
Benoit Cousson88bd8702014-07-08 23:19:34 +02001431 if (rtd->num_codecs > 1)
1432 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1433
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001434 /* link the DAI widgets */
1435 play_w = codec_dai->playback_widget;
1436 capture_w = cpu_dai->capture_widget;
1437 if (play_w && capture_w) {
1438 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1439 capture_w, play_w);
1440 if (ret != 0) {
1441 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1442 play_w->name, capture_w->name, ret);
1443 return ret;
1444 }
1445 }
1446
1447 play_w = cpu_dai->playback_widget;
1448 capture_w = codec_dai->capture_widget;
1449 if (play_w && capture_w) {
1450 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1451 capture_w, play_w);
1452 if (ret != 0) {
1453 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1454 play_w->name, capture_w->name, ret);
1455 return ret;
1456 }
1457 }
1458
1459 return 0;
1460}
1461
Stephen Warren62ae68f2012-06-08 12:34:23 -06001462static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001463{
1464 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1465 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001466 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001467 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001468 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001469
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001470 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001471 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001472
1473 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001474 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001475 cpu_dai->card = card;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001476 for (i = 0; i < rtd->num_codecs; i++)
1477 rtd->codec_dais[i]->card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001478
1479 /* set default power off timeout */
1480 rtd->pmdown_time = pmdown_time;
1481
1482 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001483 if (!cpu_dai->probed &&
1484 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001485 if (!cpu_dai->codec) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001486 if (!try_module_get(cpu_dai->dev->driver->owner))
1487 return -ENODEV;
Stephen Warrena9db7db2012-06-08 12:34:20 -06001488 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001489
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001490 if (cpu_dai->driver->probe) {
1491 ret = cpu_dai->driver->probe(cpu_dai);
1492 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001493 dev_err(cpu_dai->dev,
1494 "ASoC: failed to probe CPU DAI %s: %d\n",
1495 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001496 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001497 return ret;
1498 }
1499 }
1500 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001501 }
1502
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001503 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001504 for (i = 0; i < rtd->num_codecs; i++) {
1505 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1506 if (ret)
1507 return ret;
1508 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001509
Liam Girdwood0168bf02011-06-07 16:08:05 +01001510 /* complete DAI probe during last probe */
1511 if (order != SND_SOC_COMP_ORDER_LAST)
1512 return 0;
1513
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001514 /* do machine specific initialization */
1515 if (dai_link->init) {
1516 ret = dai_link->init(rtd);
1517 if (ret < 0) {
1518 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1519 dai_link->name, ret);
1520 return ret;
1521 }
1522 }
1523
1524 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001525 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001526 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001527
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001528#ifdef CONFIG_DEBUG_FS
1529 /* add DPCM sysfs entries */
1530 if (dai_link->dynamic) {
1531 ret = soc_dpcm_debugfs_add(rtd);
1532 if (ret < 0) {
1533 dev_err(rtd->dev,
1534 "ASoC: failed to add dpcm sysfs entries: %d\n",
1535 ret);
1536 return ret;
1537 }
1538 }
1539#endif
1540
Mark Brown36ae1a92012-01-06 17:12:45 -08001541 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001542 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001543 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1544 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001545
Namarta Kohli1245b702012-08-16 17:10:41 +05301546 if (cpu_dai->driver->compress_dai) {
1547 /*create compress_device"*/
1548 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001549 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001550 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301551 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001552 return ret;
1553 }
1554 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301555
1556 if (!dai_link->params) {
1557 /* create the pcm */
1558 ret = soc_new_pcm(rtd, num);
1559 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001560 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301561 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001562 return ret;
1563 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301564 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001565 INIT_DELAYED_WORK(&rtd->delayed_work,
1566 codec2codec_close_delayed_work);
1567
Namarta Kohli1245b702012-08-16 17:10:41 +05301568 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001569 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001570 if (ret)
1571 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001572 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001573 }
1574
1575 /* add platform data for AC97 devices */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001576 for (i = 0; i < rtd->num_codecs; i++) {
1577 if (rtd->codec_dais[i]->driver->ac97_control)
1578 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1579 rtd->cpu_dai->ac97_pdata);
1580 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001581
1582 return 0;
1583}
1584
1585#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001586static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1587 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001588{
1589 int ret;
1590
1591 /* Only instantiate AC97 if not already done by the adaptor
1592 * for the generic AC97 subsystem.
1593 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001594 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001595 /*
1596 * It is possible that the AC97 device is already registered to
1597 * the device subsystem. This happens when the device is created
1598 * via snd_ac97_mixer(). Currently only SoC codec that does so
1599 * is the generic AC97 glue but others migh emerge.
1600 *
1601 * In those cases we don't try to register the device again.
1602 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001603 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001604 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001605
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001606 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001607 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001608 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001609 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001610 return ret;
1611 }
1612
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001613 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001614 }
1615 return 0;
1616}
1617
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001618static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001619{
1620 if (codec->ac97_registered) {
1621 soc_ac97_dev_unregister(codec);
1622 codec->ac97_registered = 0;
1623 }
1624}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001625
Benoit Cousson88bd8702014-07-08 23:19:34 +02001626static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1627{
1628 int i, ret;
1629
1630 for (i = 0; i < rtd->num_codecs; i++) {
1631 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1632
1633 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1634 if (ret) {
1635 while (--i >= 0)
1636 soc_unregister_ac97_codec(codec_dai->codec);
1637 return ret;
1638 }
1639 }
1640
1641 return 0;
1642}
1643
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001644static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1645{
Benoit Cousson88bd8702014-07-08 23:19:34 +02001646 int i;
1647
1648 for (i = 0; i < rtd->num_codecs; i++)
1649 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001650}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001651#endif
1652
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001653static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001654{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001655 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001656 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1657 const char *codecname = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001658
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001659 rtd->codec = soc_find_codec(aux_dev->codec_of_node, codecname);
1660 if (!rtd->codec) {
1661 if (aux_dev->codec_of_node)
1662 codecname = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001663
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001664 dev_err(card->dev, "ASoC: %s not registered\n", codecname);
1665 return -EPROBE_DEFER;
1666 }
1667
1668 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001669}
1670
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001671static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1672{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001673 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001674 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001675 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001676
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001677 if (rtd->codec->probed) {
1678 dev_err(rtd->codec->dev, "ASoC: codec already probed\n");
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001679 return -EBUSY;
1680 }
1681
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001682 ret = soc_probe_codec(card, rtd->codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001683 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001684 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001685
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001686 /* do machine specific initialization */
1687 if (aux_dev->init) {
1688 ret = aux_dev->init(&rtd->codec->dapm);
1689 if (ret < 0) {
1690 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1691 aux_dev->name, ret);
1692 return ret;
1693 }
1694 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001695
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001696 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001697}
1698
1699static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1700{
1701 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1702 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001703
1704 /* unregister the rtd device */
1705 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001706 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001707 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001708 rtd->dev_registered = 0;
1709 }
1710
Jarkko Nikula589c3562010-12-06 16:27:07 +02001711 if (codec && codec->probed)
1712 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001713}
1714
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001715static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001716{
1717 int ret;
1718
1719 if (codec->cache_init)
1720 return 0;
1721
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001722 ret = snd_soc_cache_init(codec);
1723 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001724 dev_err(codec->dev,
1725 "ASoC: Failed to set cache compression type: %d\n",
1726 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001727 return ret;
1728 }
1729 codec->cache_init = 1;
1730 return 0;
1731}
1732
Mark Brownb19e6e72012-03-14 21:18:39 +00001733static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001734{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001735 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001736 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001737 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001738
Liam Girdwood01b9d992012-03-07 10:38:25 +00001739 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001740
Mark Brownb19e6e72012-03-14 21:18:39 +00001741 /* bind DAIs */
1742 for (i = 0; i < card->num_links; i++) {
1743 ret = soc_bind_dai_link(card, i);
1744 if (ret != 0)
1745 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001746 }
1747
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001748 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001749 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001750 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001751 if (ret != 0)
1752 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001753 }
1754
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001755 /* initialize the register cache for each available codec */
1756 list_for_each_entry(codec, &codec_list, list) {
1757 if (codec->cache_init)
1758 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001759 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001760 if (ret < 0)
1761 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001762 }
1763
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001764 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001765 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001766 card->owner, 0, &card->snd_card);
1767 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001768 dev_err(card->dev,
1769 "ASoC: can't create sound card for card %s: %d\n",
1770 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001771 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001772 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001773
Mark Browne37a4972011-03-02 18:21:57 +00001774 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1775 card->dapm.dev = card->dev;
1776 card->dapm.card = card;
1777 list_add(&card->dapm.list, &card->dapm_list);
1778
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001779#ifdef CONFIG_DEBUG_FS
1780 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1781#endif
1782
Mark Brown88ee1c62011-02-02 10:43:26 +00001783#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001784 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001785 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001786#endif
Andy Green6ed25972008-06-13 16:24:05 +01001787
Mark Brown9a841eb2011-04-12 17:51:37 -07001788 if (card->dapm_widgets)
1789 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1790 card->num_dapm_widgets);
1791
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001792 /* initialise the sound card only once */
1793 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001794 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001795 if (ret < 0)
1796 goto card_probe_error;
1797 }
1798
Stephen Warren62ae68f2012-06-08 12:34:23 -06001799 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001800 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1801 order++) {
1802 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001803 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001804 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001805 dev_err(card->dev,
1806 "ASoC: failed to instantiate card %d\n",
1807 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001808 goto probe_dai_err;
1809 }
1810 }
1811 }
1812
1813 /* probe all DAI links on this card */
1814 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1815 order++) {
1816 for (i = 0; i < card->num_links; i++) {
1817 ret = soc_probe_link_dais(card, i, order);
1818 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001819 dev_err(card->dev,
1820 "ASoC: failed to instantiate card %d\n",
1821 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001822 goto probe_dai_err;
1823 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001824 }
1825 }
1826
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001827 for (i = 0; i < card->num_aux_devs; i++) {
1828 ret = soc_probe_aux_dev(card, i);
1829 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001830 dev_err(card->dev,
1831 "ASoC: failed to add auxiliary devices %d\n",
1832 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001833 goto probe_aux_dev_err;
1834 }
1835 }
1836
Mark Brown888df392012-02-16 19:37:51 -08001837 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001838 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001839
Mark Brownb7af1da2011-04-07 19:18:44 +09001840 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001841 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001842
Mark Brownb8ad29d2011-03-02 18:35:51 +00001843 if (card->dapm_routes)
1844 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1845 card->num_dapm_routes);
1846
Mark Brown75d9ac42011-09-27 16:41:01 +01001847 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001848 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001849 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001850 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001851
Mark Brownf04209a2012-04-09 16:29:21 +01001852 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001853 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1854 int j;
1855
1856 for (j = 0; j < rtd->num_codecs; j++) {
1857 struct snd_soc_dai *codec_dai = codec_dais[j];
1858
1859 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1860 if (ret != 0 && ret != -ENOTSUPP)
1861 dev_warn(codec_dai->dev,
1862 "ASoC: Failed to set DAI format: %d\n",
1863 ret);
1864 }
Mark Brownf04209a2012-04-09 16:29:21 +01001865 }
1866
1867 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001868 if (dai_fmt &&
1869 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001870 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1871 dai_fmt);
1872 if (ret != 0 && ret != -ENOTSUPP)
1873 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001874 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001875 ret);
1876 } else if (dai_fmt) {
1877 /* Flip the polarity for the "CPU" end */
1878 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1879 switch (dai_link->dai_fmt &
1880 SND_SOC_DAIFMT_MASTER_MASK) {
1881 case SND_SOC_DAIFMT_CBM_CFM:
1882 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1883 break;
1884 case SND_SOC_DAIFMT_CBM_CFS:
1885 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1886 break;
1887 case SND_SOC_DAIFMT_CBS_CFM:
1888 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1889 break;
1890 case SND_SOC_DAIFMT_CBS_CFS:
1891 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1892 break;
1893 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001894
1895 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001896 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001897 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001898 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001899 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001900 ret);
1901 }
1902 }
1903
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001904 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001905 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001906 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1907 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001908 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1909 "%s", card->driver_name ? card->driver_name : card->name);
1910 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1911 switch (card->snd_card->driver[i]) {
1912 case '_':
1913 case '-':
1914 case '\0':
1915 break;
1916 default:
1917 if (!isalnum(card->snd_card->driver[i]))
1918 card->snd_card->driver[i] = '_';
1919 break;
1920 }
1921 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001922
Mark Brown28e9ad92011-03-02 18:36:34 +00001923 if (card->late_probe) {
1924 ret = card->late_probe(card);
1925 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001926 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001927 card->name, ret);
1928 goto probe_aux_dev_err;
1929 }
1930 }
1931
Stephen Warren16332812011-11-23 12:42:04 -07001932 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001933 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001934
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001935 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001936
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001937 ret = snd_card_register(card->snd_card);
1938 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001939 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1940 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001941 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001942 }
1943
1944#ifdef CONFIG_SND_SOC_AC97_BUS
1945 /* register any AC97 codecs */
1946 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001947 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1948 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001949 dev_err(card->dev,
1950 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001951 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001952 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001953 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001954 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001955 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001956#endif
1957
Mark Brown435c5e22008-12-04 15:32:53 +00001958 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001959 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001960 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001961
1962 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001963
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001964probe_aux_dev_err:
1965 for (i = 0; i < card->num_aux_devs; i++)
1966 soc_remove_aux_dev(card, i);
1967
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001968probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001969 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001970
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001971card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001972 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001973 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001974
1975 snd_card_free(card->snd_card);
1976
Mark Brownb19e6e72012-03-14 21:18:39 +00001977base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001978 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001979
Mark Brownb19e6e72012-03-14 21:18:39 +00001980 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001981}
1982
1983/* probes a new socdev */
1984static int soc_probe(struct platform_device *pdev)
1985{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001986 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001987
Vinod Koul70a7ca32011-01-14 19:22:48 +05301988 /*
1989 * no card, so machine driver should be registering card
1990 * we should not be here in that case so ret error
1991 */
1992 if (!card)
1993 return -EINVAL;
1994
Mark Brownfe4085e2012-03-02 13:07:41 +00001995 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001996 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001997 card->name);
1998
Mark Brown435c5e22008-12-04 15:32:53 +00001999 /* Bodge while we unpick instantiation */
2000 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002001
Mark Brown28d528c2012-08-09 18:45:23 +01002002 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002003}
2004
Vinod Koulb0e26482011-01-13 22:48:02 +05302005static int soc_cleanup_card_resources(struct snd_soc_card *card)
2006{
Vinod Koulb0e26482011-01-13 22:48:02 +05302007 int i;
2008
2009 /* make sure any delayed work runs */
2010 for (i = 0; i < card->num_rtd; i++) {
2011 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002012 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302013 }
2014
2015 /* remove auxiliary devices */
2016 for (i = 0; i < card->num_aux_devs; i++)
2017 soc_remove_aux_dev(card, i);
2018
2019 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002020 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302021
2022 soc_cleanup_card_debugfs(card);
2023
2024 /* remove the card */
2025 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002026 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302027
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02002028 snd_soc_dapm_free(&card->dapm);
2029
Vinod Koulb0e26482011-01-13 22:48:02 +05302030 snd_card_free(card->snd_card);
2031 return 0;
2032
2033}
2034
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002035/* removes a socdev */
2036static int soc_remove(struct platform_device *pdev)
2037{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002038 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002039
Mark Brownc5af3a22008-11-28 13:29:45 +00002040 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002041 return 0;
2042}
2043
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002044int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002045{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002046 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002047 int i;
Mark Brown51737472009-06-22 13:16:51 +01002048
2049 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002050 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002051
2052 /* Flush out pmdown_time work - we actually do want to run it
2053 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002054 for (i = 0; i < card->num_rtd; i++) {
2055 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002056 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002057 }
Mark Brown51737472009-06-22 13:16:51 +01002058
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002059 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002060
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002061 /* deactivate pins to sleep state */
2062 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02002063 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2064 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2065 int j;
2066
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002067 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02002068 for (j = 0; j < rtd->num_codecs; j++) {
2069 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2070 pinctrl_pm_select_sleep_state(codec_dai->dev);
2071 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002072 }
2073
Mark Brown416356f2009-06-30 19:05:15 +01002074 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002075}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002076EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002077
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002078const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302079 .suspend = snd_soc_suspend,
2080 .resume = snd_soc_resume,
2081 .freeze = snd_soc_suspend,
2082 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002083 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302084 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002085};
Stephen Warrendeb26072011-04-05 19:35:30 -06002086EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002087
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002088/* ASoC platform driver */
2089static struct platform_driver soc_driver = {
2090 .driver = {
2091 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002092 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002093 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002094 },
2095 .probe = soc_probe,
2096 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002097};
2098
Mark Brown096e49d2009-07-05 15:12:22 +01002099/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002100 * snd_soc_new_ac97_codec - initailise AC97 device
2101 * @codec: audio codec
2102 * @ops: AC97 bus operations
2103 * @num: AC97 codec number
2104 *
2105 * Initialises AC97 codec resources for use by ad-hoc devices only.
2106 */
2107int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2108 struct snd_ac97_bus_ops *ops, int num)
2109{
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002110 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
Lars-Peter Clausene3f205a2014-09-23 00:56:28 +02002111 if (codec->ac97 == NULL)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002112 return -ENOMEM;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002113
2114 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2115 if (codec->ac97->bus == NULL) {
2116 kfree(codec->ac97);
2117 codec->ac97 = NULL;
2118 mutex_unlock(&codec->mutex);
2119 return -ENOMEM;
2120 }
2121
2122 codec->ac97->bus->ops = ops;
2123 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002124
2125 /*
2126 * Mark the AC97 device to be created by us. This way we ensure that the
2127 * device will be registered with the device subsystem later on.
2128 */
2129 codec->ac97_created = 1;
2130
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002131 return 0;
2132}
2133EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2134
Markus Pargmann741a5092013-08-19 17:05:55 +02002135static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2136
2137static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2138{
2139 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2140
2141 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2142
2143 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2144
2145 udelay(10);
2146
2147 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2148
2149 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2150 msleep(2);
2151}
2152
2153static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2154{
2155 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2156
2157 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2158
2159 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2160 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2161 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2162
2163 udelay(10);
2164
2165 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2166
2167 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2168 msleep(2);
2169}
2170
2171static int snd_soc_ac97_parse_pinctl(struct device *dev,
2172 struct snd_ac97_reset_cfg *cfg)
2173{
2174 struct pinctrl *p;
2175 struct pinctrl_state *state;
2176 int gpio;
2177 int ret;
2178
2179 p = devm_pinctrl_get(dev);
2180 if (IS_ERR(p)) {
2181 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002182 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002183 }
2184 cfg->pctl = p;
2185
2186 state = pinctrl_lookup_state(p, "ac97-reset");
2187 if (IS_ERR(state)) {
2188 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002189 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002190 }
2191 cfg->pstate_reset = state;
2192
2193 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2194 if (IS_ERR(state)) {
2195 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002196 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002197 }
2198 cfg->pstate_warm_reset = state;
2199
2200 state = pinctrl_lookup_state(p, "ac97-running");
2201 if (IS_ERR(state)) {
2202 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002203 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002204 }
2205 cfg->pstate_run = state;
2206
2207 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2208 if (gpio < 0) {
2209 dev_err(dev, "Can't find ac97-sync gpio\n");
2210 return gpio;
2211 }
2212 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2213 if (ret) {
2214 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2215 return ret;
2216 }
2217 cfg->gpio_sync = gpio;
2218
2219 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2220 if (gpio < 0) {
2221 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2222 return gpio;
2223 }
2224 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2225 if (ret) {
2226 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2227 return ret;
2228 }
2229 cfg->gpio_sdata = gpio;
2230
2231 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2232 if (gpio < 0) {
2233 dev_err(dev, "Can't find ac97-reset gpio\n");
2234 return gpio;
2235 }
2236 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2237 if (ret) {
2238 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2239 return ret;
2240 }
2241 cfg->gpio_reset = gpio;
2242
2243 return 0;
2244}
2245
Mark Brownb047e1c2013-06-26 12:45:59 +01002246struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002247EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002248
2249int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2250{
2251 if (ops == soc_ac97_ops)
2252 return 0;
2253
2254 if (soc_ac97_ops && ops)
2255 return -EBUSY;
2256
2257 soc_ac97_ops = ops;
2258
2259 return 0;
2260}
2261EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2262
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002263/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002264 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2265 *
2266 * This function sets the reset and warm_reset properties of ops and parses
2267 * the device node of pdev to get pinctrl states and gpio numbers to use.
2268 */
2269int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2270 struct platform_device *pdev)
2271{
2272 struct device *dev = &pdev->dev;
2273 struct snd_ac97_reset_cfg cfg;
2274 int ret;
2275
2276 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2277 if (ret)
2278 return ret;
2279
2280 ret = snd_soc_set_ac97_ops(ops);
2281 if (ret)
2282 return ret;
2283
2284 ops->warm_reset = snd_soc_ac97_warm_reset;
2285 ops->reset = snd_soc_ac97_reset;
2286
2287 snd_ac97_rst_cfg = cfg;
2288 return 0;
2289}
2290EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2291
2292/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002293 * snd_soc_free_ac97_codec - free AC97 codec device
2294 * @codec: audio codec
2295 *
2296 * Frees AC97 codec device resources.
2297 */
2298void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2299{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002300#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002301 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002302#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002303 kfree(codec->ac97->bus);
2304 kfree(codec->ac97);
2305 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002306 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002307}
2308EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2309
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002310/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002311 * snd_soc_cnew - create new control
2312 * @_template: control template
2313 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002314 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002315 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002316 *
2317 * Create a new mixer control from a template control.
2318 *
2319 * Returns 0 for success, else error.
2320 */
2321struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002322 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002323 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002324{
2325 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002326 struct snd_kcontrol *kcontrol;
2327 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002328
2329 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002330 template.index = 0;
2331
Mark Brownefb7ac32011-03-08 17:23:24 +00002332 if (!long_name)
2333 long_name = template.name;
2334
2335 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002336 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002337 if (!name)
2338 return NULL;
2339
Mark Brownefb7ac32011-03-08 17:23:24 +00002340 template.name = name;
2341 } else {
2342 template.name = long_name;
2343 }
2344
2345 kcontrol = snd_ctl_new1(&template, data);
2346
2347 kfree(name);
2348
2349 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002350}
2351EXPORT_SYMBOL_GPL(snd_soc_cnew);
2352
Liam Girdwood022658b2012-02-03 17:43:09 +00002353static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2354 const struct snd_kcontrol_new *controls, int num_controls,
2355 const char *prefix, void *data)
2356{
2357 int err, i;
2358
2359 for (i = 0; i < num_controls; i++) {
2360 const struct snd_kcontrol_new *control = &controls[i];
2361 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2362 control->name, prefix));
2363 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002364 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2365 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002366 return err;
2367 }
2368 }
2369
2370 return 0;
2371}
2372
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002373struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2374 const char *name)
2375{
2376 struct snd_card *card = soc_card->snd_card;
2377 struct snd_kcontrol *kctl;
2378
2379 if (unlikely(!name))
2380 return NULL;
2381
2382 list_for_each_entry(kctl, &card->controls, list)
2383 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2384 return kctl;
2385 return NULL;
2386}
2387EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2388
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002389/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002390 * snd_soc_add_component_controls - Add an array of controls to a component.
2391 *
2392 * @component: Component to add controls to
2393 * @controls: Array of controls to add
2394 * @num_controls: Number of elements in the array
2395 *
2396 * Return: 0 for success, else error.
2397 */
2398int snd_soc_add_component_controls(struct snd_soc_component *component,
2399 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2400{
2401 struct snd_card *card = component->card->snd_card;
2402
2403 return snd_soc_add_controls(card, component->dev, controls,
2404 num_controls, component->name_prefix, component);
2405}
2406EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2407
2408/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002409 * snd_soc_add_codec_controls - add an array of controls to a codec.
2410 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002411 * duplicating this code.
2412 *
2413 * @codec: codec to add controls to
2414 * @controls: array of controls to add
2415 * @num_controls: number of elements in the array
2416 *
2417 * Return 0 for success, else error.
2418 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002419int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002420 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002421{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002422 return snd_soc_add_component_controls(&codec->component, controls,
2423 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002424}
Liam Girdwood022658b2012-02-03 17:43:09 +00002425EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002426
2427/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002428 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002429 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002430 *
2431 * @platform: platform to add controls to
2432 * @controls: array of controls to add
2433 * @num_controls: number of elements in the array
2434 *
2435 * Return 0 for success, else error.
2436 */
2437int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002438 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002439{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002440 return snd_soc_add_component_controls(&platform->component, controls,
2441 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002442}
2443EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2444
2445/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002446 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2447 * Convenience function to add a list of controls.
2448 *
2449 * @soc_card: SoC card to add controls to
2450 * @controls: array of controls to add
2451 * @num_controls: number of elements in the array
2452 *
2453 * Return 0 for success, else error.
2454 */
2455int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2456 const struct snd_kcontrol_new *controls, int num_controls)
2457{
2458 struct snd_card *card = soc_card->snd_card;
2459
2460 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2461 NULL, soc_card);
2462}
2463EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2464
2465/**
2466 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2467 * Convienience function to add a list of controls.
2468 *
2469 * @dai: DAI to add controls to
2470 * @controls: array of controls to add
2471 * @num_controls: number of elements in the array
2472 *
2473 * Return 0 for success, else error.
2474 */
2475int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2476 const struct snd_kcontrol_new *controls, int num_controls)
2477{
2478 struct snd_card *card = dai->card->snd_card;
2479
2480 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2481 NULL, dai);
2482}
2483EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2484
2485/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002486 * snd_soc_info_enum_double - enumerated double mixer info callback
2487 * @kcontrol: mixer control
2488 * @uinfo: control element information
2489 *
2490 * Callback to provide information about a double enumerated
2491 * mixer control.
2492 *
2493 * Returns 0 for success.
2494 */
2495int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2496 struct snd_ctl_elem_info *uinfo)
2497{
2498 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2499
2500 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2501 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002502 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002503
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002504 if (uinfo->value.enumerated.item >= e->items)
2505 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002506 strlcpy(uinfo->value.enumerated.name,
2507 e->texts[uinfo->value.enumerated.item],
2508 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002509 return 0;
2510}
2511EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2512
2513/**
2514 * snd_soc_get_enum_double - enumerated double mixer get callback
2515 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002516 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002517 *
2518 * Callback to get the value of a double enumerated mixer.
2519 *
2520 * Returns 0 for success.
2521 */
2522int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
2524{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002525 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002526 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002527 unsigned int val, item;
2528 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002529 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002530
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002531 ret = snd_soc_component_read(component, e->reg, &reg_val);
2532 if (ret)
2533 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002534 val = (reg_val >> e->shift_l) & e->mask;
2535 item = snd_soc_enum_val_to_item(e, val);
2536 ucontrol->value.enumerated.item[0] = item;
2537 if (e->shift_l != e->shift_r) {
2538 val = (reg_val >> e->shift_l) & e->mask;
2539 item = snd_soc_enum_val_to_item(e, val);
2540 ucontrol->value.enumerated.item[1] = item;
2541 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002542
2543 return 0;
2544}
2545EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2546
2547/**
2548 * snd_soc_put_enum_double - enumerated double mixer put callback
2549 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002550 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002551 *
2552 * Callback to set the value of a double enumerated mixer.
2553 *
2554 * Returns 0 for success.
2555 */
2556int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2557 struct snd_ctl_elem_value *ucontrol)
2558{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002559 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002560 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002561 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002562 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002563 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002564
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002565 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002566 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002567 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002568 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002569 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002570 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002571 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002572 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002573 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002574 }
2575
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002576 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002577}
2578EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2579
2580/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002581 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002582 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002583 * @reg: Register to read
2584 * @mask: Mask to use after shifting the register value
2585 * @shift: Right shift of register value
2586 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002587 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002588 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002589 * This functions reads a codec register. The register value is shifted right
2590 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2591 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002592 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002593 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002594 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002595static int snd_soc_read_signed(struct snd_soc_component *component,
2596 unsigned int reg, unsigned int mask, unsigned int shift,
2597 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002598{
Markus Pargmannf227b882014-01-16 16:02:10 +01002599 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002600 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002601
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002602 ret = snd_soc_component_read(component, reg, &val);
2603 if (ret < 0)
2604 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002605
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002606 val = (val >> shift) & mask;
2607
2608 if (!sign_bit) {
2609 *signed_val = val;
2610 return 0;
2611 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002612
2613 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002614 if (!(val & BIT(sign_bit))) {
2615 *signed_val = val;
2616 return 0;
2617 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002618
2619 ret = val;
2620
2621 /*
2622 * The register most probably does not contain a full-sized int.
2623 * Instead we have an arbitrary number of bits in a signed
2624 * representation which has to be translated into a full-sized int.
2625 * This is done by filling up all bits above the sign-bit.
2626 */
2627 ret |= ~((int)(BIT(sign_bit) - 1));
2628
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002629 *signed_val = ret;
2630
2631 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002632}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002633
2634/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002635 * snd_soc_info_volsw - single mixer info callback
2636 * @kcontrol: mixer control
2637 * @uinfo: control element information
2638 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002639 * Callback to provide information about a single mixer control, or a double
2640 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002641 *
2642 * Returns 0 for success.
2643 */
2644int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2645 struct snd_ctl_elem_info *uinfo)
2646{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002647 struct soc_mixer_control *mc =
2648 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002649 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002650
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002651 if (!mc->platform_max)
2652 mc->platform_max = mc->max;
2653 platform_max = mc->platform_max;
2654
2655 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002656 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2657 else
2658 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2659
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002660 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002661 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002662 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002663 return 0;
2664}
2665EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2666
2667/**
2668 * snd_soc_get_volsw - single mixer get callback
2669 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002670 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002671 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002672 * Callback to get the value of a single mixer control, or a double mixer
2673 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002674 *
2675 * Returns 0 for success.
2676 */
2677int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2678 struct snd_ctl_elem_value *ucontrol)
2679{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002680 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002681 struct soc_mixer_control *mc =
2682 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002683 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002684 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002685 unsigned int shift = mc->shift;
2686 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002687 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002688 int min = mc->min;
2689 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002690 unsigned int mask = (1 << fls(max)) - 1;
2691 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002692 int val;
2693 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002694
Markus Pargmannf227b882014-01-16 16:02:10 +01002695 if (sign_bit)
2696 mask = BIT(sign_bit + 1) - 1;
2697
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002698 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2699 if (ret)
2700 return ret;
2701
2702 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002703 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002704 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002705 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002706
2707 if (snd_soc_volsw_is_stereo(mc)) {
2708 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002709 ret = snd_soc_read_signed(component, reg, mask, rshift,
2710 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002711 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002712 ret = snd_soc_read_signed(component, reg2, mask, shift,
2713 sign_bit, &val);
2714 if (ret)
2715 return ret;
2716
2717 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002718 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002719 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002720 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002721 }
2722
2723 return 0;
2724}
2725EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2726
2727/**
2728 * snd_soc_put_volsw - single mixer put callback
2729 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002730 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002731 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002732 * Callback to set the value of a single mixer control, or a double mixer
2733 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002734 *
2735 * Returns 0 for success.
2736 */
2737int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2738 struct snd_ctl_elem_value *ucontrol)
2739{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002740 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002741 struct soc_mixer_control *mc =
2742 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002743 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002744 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002745 unsigned int shift = mc->shift;
2746 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002747 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002748 int min = mc->min;
2749 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002750 unsigned int mask = (1 << fls(max)) - 1;
2751 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002752 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002753 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002754 unsigned int val2 = 0;
2755 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002756
Markus Pargmannf227b882014-01-16 16:02:10 +01002757 if (sign_bit)
2758 mask = BIT(sign_bit + 1) - 1;
2759
2760 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002761 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002762 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002763 val_mask = mask << shift;
2764 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002765 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002766 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002767 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002768 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002769 if (reg == reg2) {
2770 val_mask |= mask << rshift;
2771 val |= val2 << rshift;
2772 } else {
2773 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002774 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002775 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002776 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002777 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002778 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002779 return err;
2780
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002781 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002782 err = snd_soc_component_update_bits(component, reg2, val_mask,
2783 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002784
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002785 return err;
2786}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002787EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002788
Mark Browne13ac2e2008-05-28 17:58:05 +01002789/**
Brian Austin1d99f242012-03-30 10:43:55 -05002790 * snd_soc_get_volsw_sx - single mixer get callback
2791 * @kcontrol: mixer control
2792 * @ucontrol: control element information
2793 *
2794 * Callback to get the value of a single mixer control, or a double mixer
2795 * control that spans 2 registers.
2796 *
2797 * Returns 0 for success.
2798 */
2799int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2800 struct snd_ctl_elem_value *ucontrol)
2801{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002802 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002803 struct soc_mixer_control *mc =
2804 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002805 unsigned int reg = mc->reg;
2806 unsigned int reg2 = mc->rreg;
2807 unsigned int shift = mc->shift;
2808 unsigned int rshift = mc->rshift;
2809 int max = mc->max;
2810 int min = mc->min;
2811 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002812 unsigned int val;
2813 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002814
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002815 ret = snd_soc_component_read(component, reg, &val);
2816 if (ret < 0)
2817 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002818
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002819 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2820
2821 if (snd_soc_volsw_is_stereo(mc)) {
2822 ret = snd_soc_component_read(component, reg2, &val);
2823 if (ret < 0)
2824 return ret;
2825
2826 val = ((val >> rshift) - min) & mask;
2827 ucontrol->value.integer.value[1] = val;
2828 }
Brian Austin1d99f242012-03-30 10:43:55 -05002829
2830 return 0;
2831}
2832EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2833
2834/**
2835 * snd_soc_put_volsw_sx - double mixer set callback
2836 * @kcontrol: mixer control
2837 * @uinfo: control element information
2838 *
2839 * Callback to set the value of a double mixer control that spans 2 registers.
2840 *
2841 * Returns 0 for success.
2842 */
2843int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2844 struct snd_ctl_elem_value *ucontrol)
2845{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002846 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002847 struct soc_mixer_control *mc =
2848 (struct soc_mixer_control *)kcontrol->private_value;
2849
2850 unsigned int reg = mc->reg;
2851 unsigned int reg2 = mc->rreg;
2852 unsigned int shift = mc->shift;
2853 unsigned int rshift = mc->rshift;
2854 int max = mc->max;
2855 int min = mc->min;
2856 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002857 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002858 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002859
2860 val_mask = mask << shift;
2861 val = (ucontrol->value.integer.value[0] + min) & mask;
2862 val = val << shift;
2863
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002864 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302865 if (err < 0)
2866 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002867
2868 if (snd_soc_volsw_is_stereo(mc)) {
2869 val_mask = mask << rshift;
2870 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2871 val2 = val2 << rshift;
2872
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002873 err = snd_soc_component_update_bits(component, reg2, val_mask,
2874 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002875 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002876 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002877}
2878EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2879
2880/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002881 * snd_soc_info_volsw_s8 - signed mixer info callback
2882 * @kcontrol: mixer control
2883 * @uinfo: control element information
2884 *
2885 * Callback to provide information about a signed mixer control.
2886 *
2887 * Returns 0 for success.
2888 */
2889int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2890 struct snd_ctl_elem_info *uinfo)
2891{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002892 struct soc_mixer_control *mc =
2893 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002894 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002895 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002896
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002897 if (!mc->platform_max)
2898 mc->platform_max = mc->max;
2899 platform_max = mc->platform_max;
2900
Mark Browne13ac2e2008-05-28 17:58:05 +01002901 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2902 uinfo->count = 2;
2903 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002904 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002905 return 0;
2906}
2907EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2908
2909/**
2910 * snd_soc_get_volsw_s8 - signed mixer get callback
2911 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002912 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002913 *
2914 * Callback to get the value of a signed mixer control.
2915 *
2916 * Returns 0 for success.
2917 */
2918int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_value *ucontrol)
2920{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002921 struct soc_mixer_control *mc =
2922 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002923 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002924 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002925 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002926 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002927 int ret;
2928
2929 ret = snd_soc_component_read(component, reg, &val);
2930 if (ret)
2931 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002932
2933 ucontrol->value.integer.value[0] =
2934 ((signed char)(val & 0xff))-min;
2935 ucontrol->value.integer.value[1] =
2936 ((signed char)((val >> 8) & 0xff))-min;
2937 return 0;
2938}
2939EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2940
2941/**
2942 * snd_soc_put_volsw_sgn - signed mixer put callback
2943 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002944 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002945 *
2946 * Callback to set the value of a signed mixer control.
2947 *
2948 * Returns 0 for success.
2949 */
2950int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2951 struct snd_ctl_elem_value *ucontrol)
2952{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002953 struct soc_mixer_control *mc =
2954 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002955 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002956 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002957 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002958 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002959
2960 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2961 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2962
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002963 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002964}
2965EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2966
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002967/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002968 * snd_soc_info_volsw_range - single mixer info callback with range.
2969 * @kcontrol: mixer control
2970 * @uinfo: control element information
2971 *
2972 * Callback to provide information, within a range, about a single
2973 * mixer control.
2974 *
2975 * returns 0 for success.
2976 */
2977int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2978 struct snd_ctl_elem_info *uinfo)
2979{
2980 struct soc_mixer_control *mc =
2981 (struct soc_mixer_control *)kcontrol->private_value;
2982 int platform_max;
2983 int min = mc->min;
2984
2985 if (!mc->platform_max)
2986 mc->platform_max = mc->max;
2987 platform_max = mc->platform_max;
2988
2989 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002990 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002991 uinfo->value.integer.min = 0;
2992 uinfo->value.integer.max = platform_max - min;
2993
2994 return 0;
2995}
2996EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2997
2998/**
2999 * snd_soc_put_volsw_range - single mixer put value callback with range.
3000 * @kcontrol: mixer control
3001 * @ucontrol: control element information
3002 *
3003 * Callback to set the value, within a range, for a single mixer control.
3004 *
3005 * Returns 0 for success.
3006 */
3007int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
3008 struct snd_ctl_elem_value *ucontrol)
3009{
3010 struct soc_mixer_control *mc =
3011 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003012 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003013 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003014 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003015 unsigned int shift = mc->shift;
3016 int min = mc->min;
3017 int max = mc->max;
3018 unsigned int mask = (1 << fls(max)) - 1;
3019 unsigned int invert = mc->invert;
3020 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003021 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003022
3023 val = ((ucontrol->value.integer.value[0] + min) & mask);
3024 if (invert)
3025 val = max - val;
3026 val_mask = mask << shift;
3027 val = val << shift;
3028
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003029 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09003030 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00003031 return ret;
3032
3033 if (snd_soc_volsw_is_stereo(mc)) {
3034 val = ((ucontrol->value.integer.value[1] + min) & mask);
3035 if (invert)
3036 val = max - val;
3037 val_mask = mask << shift;
3038 val = val << shift;
3039
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003040 ret = snd_soc_component_update_bits(component, rreg, val_mask,
3041 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00003042 }
3043
3044 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003045}
3046EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3047
3048/**
3049 * snd_soc_get_volsw_range - single mixer get callback with range
3050 * @kcontrol: mixer control
3051 * @ucontrol: control element information
3052 *
3053 * Callback to get the value, within a range, of a single mixer control.
3054 *
3055 * Returns 0 for success.
3056 */
3057int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3058 struct snd_ctl_elem_value *ucontrol)
3059{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003060 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003061 struct soc_mixer_control *mc =
3062 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003063 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003064 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003065 unsigned int shift = mc->shift;
3066 int min = mc->min;
3067 int max = mc->max;
3068 unsigned int mask = (1 << fls(max)) - 1;
3069 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003070 unsigned int val;
3071 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003072
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003073 ret = snd_soc_component_read(component, reg, &val);
3074 if (ret)
3075 return ret;
3076
3077 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003078 if (invert)
3079 ucontrol->value.integer.value[0] =
3080 max - ucontrol->value.integer.value[0];
3081 ucontrol->value.integer.value[0] =
3082 ucontrol->value.integer.value[0] - min;
3083
Mark Brown9bde4f02012-12-19 16:05:00 +00003084 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003085 ret = snd_soc_component_read(component, rreg, &val);
3086 if (ret)
3087 return ret;
3088
3089 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003090 if (invert)
3091 ucontrol->value.integer.value[1] =
3092 max - ucontrol->value.integer.value[1];
3093 ucontrol->value.integer.value[1] =
3094 ucontrol->value.integer.value[1] - min;
3095 }
3096
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003097 return 0;
3098}
3099EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3100
3101/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003102 * snd_soc_limit_volume - Set new limit to an existing volume control.
3103 *
3104 * @codec: where to look for the control
3105 * @name: Name of the control
3106 * @max: new maximum limit
3107 *
3108 * Return 0 for success, else error.
3109 */
3110int snd_soc_limit_volume(struct snd_soc_codec *codec,
3111 const char *name, int max)
3112{
Lars-Peter Clausen00200102014-07-17 22:01:07 +02003113 struct snd_card *card = codec->component.card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003114 struct snd_kcontrol *kctl;
3115 struct soc_mixer_control *mc;
3116 int found = 0;
3117 int ret = -EINVAL;
3118
3119 /* Sanity check for name and max */
3120 if (unlikely(!name || max <= 0))
3121 return -EINVAL;
3122
3123 list_for_each_entry(kctl, &card->controls, list) {
3124 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3125 found = 1;
3126 break;
3127 }
3128 }
3129 if (found) {
3130 mc = (struct soc_mixer_control *)kctl->private_value;
3131 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003132 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003133 ret = 0;
3134 }
3135 }
3136 return ret;
3137}
3138EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3139
Mark Brown71d08512011-10-10 18:31:26 +01003140int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3141 struct snd_ctl_elem_info *uinfo)
3142{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003143 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003144 struct soc_bytes *params = (void *)kcontrol->private_value;
3145
3146 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003147 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003148
3149 return 0;
3150}
3151EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3152
3153int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3154 struct snd_ctl_elem_value *ucontrol)
3155{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003156 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003157 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003158 int ret;
3159
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003160 if (component->regmap)
3161 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003162 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003163 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003164 else
3165 ret = -EINVAL;
3166
Mark Brownf831b052012-02-17 16:20:33 -08003167 /* Hide any masked bytes to ensure consistent data reporting */
3168 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003169 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003170 case 1:
3171 ucontrol->value.bytes.data[0] &= ~params->mask;
3172 break;
3173 case 2:
3174 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003175 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003176 break;
3177 case 4:
3178 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003179 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003180 break;
3181 default:
3182 return -EINVAL;
3183 }
3184 }
3185
Mark Brown71d08512011-10-10 18:31:26 +01003186 return ret;
3187}
3188EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3189
3190int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3191 struct snd_ctl_elem_value *ucontrol)
3192{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003193 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003194 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003195 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003196 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003197 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003198
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003199 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003200 return -EINVAL;
3201
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003202 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003203
Mark Brownb5a8fe42013-01-20 21:42:22 +09003204 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3205 if (!data)
3206 return -ENOMEM;
3207
Mark Brownf831b052012-02-17 16:20:33 -08003208 /*
3209 * If we've got a mask then we need to preserve the register
3210 * bits. We shouldn't modify the incoming data so take a
3211 * copy.
3212 */
3213 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003214 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003215 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003216 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003217
3218 val &= params->mask;
3219
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003220 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003221 case 1:
3222 ((u8 *)data)[0] &= ~params->mask;
3223 ((u8 *)data)[0] |= val;
3224 break;
3225 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003226 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003227 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003228 &mask, &mask);
3229 if (ret != 0)
3230 goto out;
3231
3232 ((u16 *)data)[0] &= mask;
3233
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003234 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003235 &val, &val);
3236 if (ret != 0)
3237 goto out;
3238
3239 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003240 break;
3241 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003242 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003243 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003244 &mask, &mask);
3245 if (ret != 0)
3246 goto out;
3247
3248 ((u32 *)data)[0] &= mask;
3249
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003250 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003251 &val, &val);
3252 if (ret != 0)
3253 goto out;
3254
3255 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003256 break;
3257 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003258 ret = -EINVAL;
3259 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003260 }
3261 }
3262
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003263 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003264 data, len);
3265
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003266out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003267 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003268
3269 return ret;
3270}
3271EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3272
Vinod Kould9881202014-05-02 10:58:11 +05303273int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3274 struct snd_ctl_elem_info *ucontrol)
3275{
3276 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3277
3278 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3279 ucontrol->count = params->max;
3280
3281 return 0;
3282}
3283EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3284
Omair Mohammed Abdullah7523a272014-07-15 21:34:48 +05303285int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3286 unsigned int size, unsigned int __user *tlv)
3287{
3288 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3289 unsigned int count = size < params->max ? size : params->max;
3290 int ret = -ENXIO;
3291
3292 switch (op_flag) {
3293 case SNDRV_CTL_TLV_OP_READ:
3294 if (params->get)
3295 ret = params->get(tlv, count);
3296 break;
3297 case SNDRV_CTL_TLV_OP_WRITE:
3298 if (params->put)
3299 ret = params->put(tlv, count);
3300 break;
3301 }
3302 return ret;
3303}
3304EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3305
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003306/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003307 * snd_soc_info_xr_sx - signed multi register info callback
3308 * @kcontrol: mreg control
3309 * @uinfo: control element information
3310 *
3311 * Callback to provide information of a control that can
3312 * span multiple codec registers which together
3313 * forms a single signed value in a MSB/LSB manner.
3314 *
3315 * Returns 0 for success.
3316 */
3317int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3318 struct snd_ctl_elem_info *uinfo)
3319{
3320 struct soc_mreg_control *mc =
3321 (struct soc_mreg_control *)kcontrol->private_value;
3322 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3323 uinfo->count = 1;
3324 uinfo->value.integer.min = mc->min;
3325 uinfo->value.integer.max = mc->max;
3326
3327 return 0;
3328}
3329EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3330
3331/**
3332 * snd_soc_get_xr_sx - signed multi register get callback
3333 * @kcontrol: mreg control
3334 * @ucontrol: control element information
3335 *
3336 * Callback to get the value of a control that can span
3337 * multiple codec registers which together forms a single
3338 * signed value in a MSB/LSB manner. The control supports
3339 * specifying total no of bits used to allow for bitfields
3340 * across the multiple codec registers.
3341 *
3342 * Returns 0 for success.
3343 */
3344int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3345 struct snd_ctl_elem_value *ucontrol)
3346{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003347 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003348 struct soc_mreg_control *mc =
3349 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003350 unsigned int regbase = mc->regbase;
3351 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003352 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003353 unsigned int regwmask = (1<<regwshift)-1;
3354 unsigned int invert = mc->invert;
3355 unsigned long mask = (1UL<<mc->nbits)-1;
3356 long min = mc->min;
3357 long max = mc->max;
3358 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003359 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003360 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003361 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003362
3363 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003364 ret = snd_soc_component_read(component, regbase+i, &regval);
3365 if (ret)
3366 return ret;
3367 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003368 }
3369 val &= mask;
3370 if (min < 0 && val > max)
3371 val |= ~mask;
3372 if (invert)
3373 val = max - val;
3374 ucontrol->value.integer.value[0] = val;
3375
3376 return 0;
3377}
3378EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3379
3380/**
3381 * snd_soc_put_xr_sx - signed multi register get callback
3382 * @kcontrol: mreg control
3383 * @ucontrol: control element information
3384 *
3385 * Callback to set the value of a control that can span
3386 * multiple codec registers which together forms a single
3387 * signed value in a MSB/LSB manner. The control supports
3388 * specifying total no of bits used to allow for bitfields
3389 * across the multiple codec registers.
3390 *
3391 * Returns 0 for success.
3392 */
3393int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3394 struct snd_ctl_elem_value *ucontrol)
3395{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003396 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003397 struct soc_mreg_control *mc =
3398 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003399 unsigned int regbase = mc->regbase;
3400 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003401 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003402 unsigned int regwmask = (1<<regwshift)-1;
3403 unsigned int invert = mc->invert;
3404 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003405 long max = mc->max;
3406 long val = ucontrol->value.integer.value[0];
3407 unsigned int i, regval, regmask;
3408 int err;
3409
3410 if (invert)
3411 val = max - val;
3412 val &= mask;
3413 for (i = 0; i < regcount; i++) {
3414 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3415 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003416 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003417 regmask, regval);
3418 if (err < 0)
3419 return err;
3420 }
3421
3422 return 0;
3423}
3424EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3425
3426/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003427 * snd_soc_get_strobe - strobe get callback
3428 * @kcontrol: mixer control
3429 * @ucontrol: control element information
3430 *
3431 * Callback get the value of a strobe mixer control.
3432 *
3433 * Returns 0 for success.
3434 */
3435int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3436 struct snd_ctl_elem_value *ucontrol)
3437{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003438 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003439 struct soc_mixer_control *mc =
3440 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003441 unsigned int reg = mc->reg;
3442 unsigned int shift = mc->shift;
3443 unsigned int mask = 1 << shift;
3444 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003445 unsigned int val;
3446 int ret;
3447
3448 ret = snd_soc_component_read(component, reg, &val);
3449 if (ret)
3450 return ret;
3451
3452 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003453
3454 if (shift != 0 && val != 0)
3455 val = val >> shift;
3456 ucontrol->value.enumerated.item[0] = val ^ invert;
3457
3458 return 0;
3459}
3460EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3461
3462/**
3463 * snd_soc_put_strobe - strobe put callback
3464 * @kcontrol: mixer control
3465 * @ucontrol: control element information
3466 *
3467 * Callback strobe a register bit to high then low (or the inverse)
3468 * in one pass of a single mixer enum control.
3469 *
3470 * Returns 1 for success.
3471 */
3472int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3473 struct snd_ctl_elem_value *ucontrol)
3474{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003475 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003476 struct soc_mixer_control *mc =
3477 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003478 unsigned int reg = mc->reg;
3479 unsigned int shift = mc->shift;
3480 unsigned int mask = 1 << shift;
3481 unsigned int invert = mc->invert != 0;
3482 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3483 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3484 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3485 int err;
3486
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003487 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003488 if (err < 0)
3489 return err;
3490
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003491 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003492}
3493EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3494
3495/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003496 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3497 * @dai: DAI
3498 * @clk_id: DAI specific clock ID
3499 * @freq: new clock frequency in Hz
3500 * @dir: new clock direction - input/output.
3501 *
3502 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3503 */
3504int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3505 unsigned int freq, int dir)
3506{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003507 if (dai->driver && dai->driver->ops->set_sysclk)
3508 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003509 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003510 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003511 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003512 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003513 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003514}
3515EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3516
3517/**
Mark Brownec4ee522011-03-07 20:58:11 +00003518 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3519 * @codec: CODEC
3520 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003521 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003522 * @freq: new clock frequency in Hz
3523 * @dir: new clock direction - input/output.
3524 *
3525 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3526 */
3527int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003528 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003529{
3530 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003531 return codec->driver->set_sysclk(codec, clk_id, source,
3532 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003533 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003534 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003535}
3536EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3537
3538/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003539 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3540 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003541 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003542 * @div: new clock divisor.
3543 *
3544 * Configures the clock dividers. This is used to derive the best DAI bit and
3545 * frame clocks from the system or master clock. It's best to set the DAI bit
3546 * and frame clocks as low as possible to save system power.
3547 */
3548int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3549 int div_id, int div)
3550{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003551 if (dai->driver && dai->driver->ops->set_clkdiv)
3552 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003553 else
3554 return -EINVAL;
3555}
3556EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3557
3558/**
3559 * snd_soc_dai_set_pll - configure DAI PLL.
3560 * @dai: DAI
3561 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003562 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003563 * @freq_in: PLL input clock frequency in Hz
3564 * @freq_out: requested PLL output clock frequency in Hz
3565 *
3566 * Configures and enables PLL to generate output clock based on input clock.
3567 */
Mark Brown85488032009-09-05 18:52:16 +01003568int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3569 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003570{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003571 if (dai->driver && dai->driver->ops->set_pll)
3572 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003573 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003574 else if (dai->codec && dai->codec->driver->set_pll)
3575 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3576 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003577 else
3578 return -EINVAL;
3579}
3580EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3581
Mark Brownec4ee522011-03-07 20:58:11 +00003582/*
3583 * snd_soc_codec_set_pll - configure codec PLL.
3584 * @codec: CODEC
3585 * @pll_id: DAI specific PLL ID
3586 * @source: DAI specific source for the PLL
3587 * @freq_in: PLL input clock frequency in Hz
3588 * @freq_out: requested PLL output clock frequency in Hz
3589 *
3590 * Configures and enables PLL to generate output clock based on input clock.
3591 */
3592int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3593 unsigned int freq_in, unsigned int freq_out)
3594{
3595 if (codec->driver->set_pll)
3596 return codec->driver->set_pll(codec, pll_id, source,
3597 freq_in, freq_out);
3598 else
3599 return -EINVAL;
3600}
3601EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3602
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003603/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003604 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3605 * @dai: DAI
3606 * @ratio Ratio of BCLK to Sample rate.
3607 *
3608 * Configures the DAI for a preset BCLK to sample rate ratio.
3609 */
3610int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3611{
3612 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3613 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3614 else
3615 return -EINVAL;
3616}
3617EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3618
3619/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003620 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3621 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003622 * @fmt: SND_SOC_DAIFMT_ format value.
3623 *
3624 * Configures the DAI hardware format and clocking.
3625 */
3626int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3627{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003628 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003629 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003630 if (dai->driver->ops->set_fmt == NULL)
3631 return -ENOTSUPP;
3632 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003633}
3634EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3635
3636/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003637 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003638 * @slots: Number of slots in use.
3639 * @tx_mask: bitmask representing active TX slots.
3640 * @rx_mask: bitmask representing active RX slots.
3641 *
3642 * Generates the TDM tx and rx slot default masks for DAI.
3643 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003644static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003645 unsigned int *tx_mask,
3646 unsigned int *rx_mask)
3647{
3648 if (*tx_mask || *rx_mask)
3649 return 0;
3650
3651 if (!slots)
3652 return -EINVAL;
3653
3654 *tx_mask = (1 << slots) - 1;
3655 *rx_mask = (1 << slots) - 1;
3656
3657 return 0;
3658}
3659
3660/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003661 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3662 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003663 * @tx_mask: bitmask representing active TX slots.
3664 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003665 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003666 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003667 *
3668 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3669 * specific.
3670 */
3671int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003672 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003673{
Xiubo Lie5c21512014-03-21 14:17:12 +08003674 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3675 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003676 &tx_mask, &rx_mask);
3677 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003678 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003679
Benoit Cousson88bd8702014-07-08 23:19:34 +02003680 dai->tx_mask = tx_mask;
3681 dai->rx_mask = rx_mask;
3682
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003683 if (dai->driver && dai->driver->ops->set_tdm_slot)
3684 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003685 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003686 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003687 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003688}
3689EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3690
3691/**
Barry Song472df3c2009-09-12 01:16:29 +08003692 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3693 * @dai: DAI
3694 * @tx_num: how many TX channels
3695 * @tx_slot: pointer to an array which imply the TX slot number channel
3696 * 0~num-1 uses
3697 * @rx_num: how many RX channels
3698 * @rx_slot: pointer to an array which imply the RX slot number channel
3699 * 0~num-1 uses
3700 *
3701 * configure the relationship between channel number and TDM slot number.
3702 */
3703int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3704 unsigned int tx_num, unsigned int *tx_slot,
3705 unsigned int rx_num, unsigned int *rx_slot)
3706{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003707 if (dai->driver && dai->driver->ops->set_channel_map)
3708 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003709 rx_num, rx_slot);
3710 else
3711 return -EINVAL;
3712}
3713EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3714
3715/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003716 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3717 * @dai: DAI
3718 * @tristate: tristate enable
3719 *
3720 * Tristates the DAI so that others can use it.
3721 */
3722int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3723{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003724 if (dai->driver && dai->driver->ops->set_tristate)
3725 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003726 else
3727 return -EINVAL;
3728}
3729EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3730
3731/**
3732 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3733 * @dai: DAI
3734 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003735 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003736 *
3737 * Mutes the DAI DAC.
3738 */
Mark Brownda183962013-02-06 15:44:07 +00003739int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3740 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003741{
Mark Brownda183962013-02-06 15:44:07 +00003742 if (!dai->driver)
3743 return -ENOTSUPP;
3744
3745 if (dai->driver->ops->mute_stream)
3746 return dai->driver->ops->mute_stream(dai, mute, direction);
3747 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3748 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003749 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003750 else
Mark Brown04570c62012-04-13 19:16:03 +01003751 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003752}
3753EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3754
Benoit Cousson88bd8702014-07-08 23:19:34 +02003755static int snd_soc_init_multicodec(struct snd_soc_card *card,
3756 struct snd_soc_dai_link *dai_link)
3757{
3758 /* Legacy codec/codec_dai link is a single entry in multicodec */
3759 if (dai_link->codec_name || dai_link->codec_of_node ||
3760 dai_link->codec_dai_name) {
3761 dai_link->num_codecs = 1;
3762
3763 dai_link->codecs = devm_kzalloc(card->dev,
3764 sizeof(struct snd_soc_dai_link_component),
3765 GFP_KERNEL);
3766 if (!dai_link->codecs)
3767 return -ENOMEM;
3768
3769 dai_link->codecs[0].name = dai_link->codec_name;
3770 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3771 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3772 }
3773
3774 if (!dai_link->codecs) {
3775 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3776 return -EINVAL;
3777 }
3778
3779 return 0;
3780}
3781
Mark Brownc5af3a22008-11-28 13:29:45 +00003782/**
3783 * snd_soc_register_card - Register a card with the ASoC core
3784 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003785 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003786 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003787 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303788int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003789{
Benoit Cousson88bd8702014-07-08 23:19:34 +02003790 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003791
Mark Brownc5af3a22008-11-28 13:29:45 +00003792 if (!card->name || !card->dev)
3793 return -EINVAL;
3794
Stephen Warren5a504962011-12-21 10:40:59 -07003795 for (i = 0; i < card->num_links; i++) {
3796 struct snd_soc_dai_link *link = &card->dai_link[i];
3797
Benoit Cousson88bd8702014-07-08 23:19:34 +02003798 ret = snd_soc_init_multicodec(card, link);
3799 if (ret) {
3800 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3801 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07003802 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02003803
3804 for (j = 0; j < link->num_codecs; j++) {
3805 /*
3806 * Codec must be specified by 1 of name or OF node,
3807 * not both or neither.
3808 */
3809 if (!!link->codecs[j].name ==
3810 !!link->codecs[j].of_node) {
3811 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3812 link->name);
3813 return -EINVAL;
3814 }
3815 /* Codec DAI name must be specified */
3816 if (!link->codecs[j].dai_name) {
3817 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3818 link->name);
3819 return -EINVAL;
3820 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003821 }
Stephen Warren5a504962011-12-21 10:40:59 -07003822
3823 /*
3824 * Platform may be specified by either name or OF node, but
3825 * can be left unspecified, and a dummy platform will be used.
3826 */
3827 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003828 dev_err(card->dev,
3829 "ASoC: Both platform name/of_node are set for %s\n",
3830 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003831 return -EINVAL;
3832 }
3833
3834 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003835 * CPU device may be specified by either name or OF node, but
3836 * can be left unspecified, and will be matched based on DAI
3837 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003838 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003839 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003840 dev_err(card->dev,
3841 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3842 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003843 return -EINVAL;
3844 }
3845 /*
3846 * At least one of CPU DAI name or CPU device name/node must be
3847 * specified
3848 */
3849 if (!link->cpu_dai_name &&
3850 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003851 dev_err(card->dev,
3852 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3853 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003854 return -EINVAL;
3855 }
3856 }
3857
Mark Browned77cc12011-05-03 18:25:34 +01003858 dev_set_drvdata(card->dev, card);
3859
Stephen Warren111c6412011-01-28 14:26:35 -07003860 snd_soc_initialize_card_lists(card);
3861
Vinod Koul150dd2f2011-01-13 22:48:32 +05303862 soc_init_card_debugfs(card);
3863
Mark Brown181a6892012-03-14 20:18:49 +00003864 card->rtd = devm_kzalloc(card->dev,
3865 sizeof(struct snd_soc_pcm_runtime) *
3866 (card->num_links + card->num_aux_devs),
3867 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003868 if (card->rtd == NULL)
3869 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003870 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003871 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003872
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003873 for (i = 0; i < card->num_links; i++) {
3874 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003875 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02003876 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3877 sizeof(struct snd_soc_dai *) *
3878 (card->rtd[i].dai_link->num_codecs),
3879 GFP_KERNEL);
3880 if (card->rtd[i].codec_dais == NULL)
3881 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003882 }
3883
3884 for (i = 0; i < card->num_aux_devs; i++)
3885 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003886
Mark Browndb432b42011-10-03 21:06:40 +01003887 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003888 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003889 mutex_init(&card->mutex);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003890 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003891
Mark Brownb19e6e72012-03-14 21:18:39 +00003892 ret = snd_soc_instantiate_card(card);
3893 if (ret != 0)
3894 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003895
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003896 /* deactivate pins to sleep state */
3897 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02003898 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3899 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3900 int j;
3901
3902 for (j = 0; j < rtd->num_codecs; j++) {
3903 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3904 if (!codec_dai->active)
3905 pinctrl_pm_select_sleep_state(codec_dai->dev);
3906 }
3907
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003908 if (!cpu_dai->active)
3909 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3910 }
3911
Mark Brownb19e6e72012-03-14 21:18:39 +00003912 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003913}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303914EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003915
3916/**
3917 * snd_soc_unregister_card - Unregister a card with the ASoC core
3918 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003919 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003920 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003921 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303922int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003923{
Vinod Koulb0e26482011-01-13 22:48:02 +05303924 if (card->instantiated)
3925 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003926 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003927
3928 return 0;
3929}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303930EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003931
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003932/*
3933 * Simplify DAI link configuration by removing ".-1" from device names
3934 * and sanitizing names.
3935 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003936static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003937{
3938 char *found, name[NAME_SIZE];
3939 int id1, id2;
3940
3941 if (dev_name(dev) == NULL)
3942 return NULL;
3943
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003944 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003945
3946 /* are we a "%s.%d" name (platform and SPI components) */
3947 found = strstr(name, dev->driver->name);
3948 if (found) {
3949 /* get ID */
3950 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3951
3952 /* discard ID from name if ID == -1 */
3953 if (*id == -1)
3954 found[strlen(dev->driver->name)] = '\0';
3955 }
3956
3957 } else {
3958 /* I2C component devices are named "bus-addr" */
3959 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3960 char tmp[NAME_SIZE];
3961
3962 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003963 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003964
3965 /* sanitize component name for DAI link creation */
3966 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003967 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003968 } else
3969 *id = 0;
3970 }
3971
3972 return kstrdup(name, GFP_KERNEL);
3973}
3974
3975/*
3976 * Simplify DAI link naming for single devices with multiple DAIs by removing
3977 * any ".-1" and using the DAI name (instead of device name).
3978 */
3979static inline char *fmt_multiple_name(struct device *dev,
3980 struct snd_soc_dai_driver *dai_drv)
3981{
3982 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003983 dev_err(dev,
3984 "ASoC: error - multiple DAI %s registered with no name\n",
3985 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003986 return NULL;
3987 }
3988
3989 return kstrdup(dai_drv->name, GFP_KERNEL);
3990}
3991
Mark Brown91151712008-11-30 23:31:24 +00003992/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003993 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003994 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003995 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003996 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003997static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003998{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003999 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00004000
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01004001 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004002 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
4003 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01004004 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004005 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004006 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004007 }
Mark Brown91151712008-11-30 23:31:24 +00004008}
Mark Brown91151712008-11-30 23:31:24 +00004009
4010/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004011 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00004012 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004013 * @component: The component the DAIs are registered for
4014 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00004015 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004016 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
4017 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00004018 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004019static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004020 struct snd_soc_dai_driver *dai_drv, size_t count,
4021 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00004022{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004023 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004024 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004025 unsigned int i;
4026 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004027
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004028 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00004029
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004030 component->dai_drv = dai_drv;
4031 component->num_dai = count;
4032
Mark Brown91151712008-11-30 23:31:24 +00004033 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004034
4035 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08004036 if (dai == NULL) {
4037 ret = -ENOMEM;
4038 goto err;
4039 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004040
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004041 /*
4042 * Back in the old days when we still had component-less DAIs,
4043 * instead of having a static name, component-less DAIs would
4044 * inherit the name of the parent device so it is possible to
4045 * register multiple instances of the DAI. We still need to keep
4046 * the same naming style even though those DAIs are not
4047 * component-less anymore.
4048 */
4049 if (count == 1 && legacy_dai_naming) {
4050 dai->name = fmt_single_name(dev, &dai->id);
4051 } else {
4052 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
4053 if (dai_drv[i].id)
4054 dai->id = dai_drv[i].id;
4055 else
4056 dai->id = i;
4057 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004058 if (dai->name == NULL) {
4059 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004060 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00004061 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004062 }
4063
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004064 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004065 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004066 dai->driver = &dai_drv[i];
4067 if (!dai->driver->ops)
4068 dai->driver->ops = &null_dai_ops;
4069
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01004070 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01004071
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004072 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00004073 }
4074
4075 return 0;
4076
4077err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004078 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00004079
4080 return ret;
4081}
Mark Brown91151712008-11-30 23:31:24 +00004082
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004083static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
4084 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004085{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004086 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004087
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004088 component->driver->seq_notifier(component, type, subseq);
4089}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004090
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004091static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
4092 int event)
4093{
4094 struct snd_soc_component *component = dapm->component;
4095
4096 return component->driver->stream_event(component, event);
4097}
4098
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004099static int snd_soc_component_initialize(struct snd_soc_component *component,
4100 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004101{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004102 struct snd_soc_dapm_context *dapm;
4103
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004104 component->name = fmt_single_name(dev, &component->id);
4105 if (!component->name) {
4106 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004107 return -ENOMEM;
4108 }
4109
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004110 component->dev = dev;
4111 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004112
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004113 if (!component->dapm_ptr)
4114 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004115
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004116 dapm = component->dapm_ptr;
4117 dapm->dev = dev;
4118 dapm->component = component;
4119 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004120 if (driver->seq_notifier)
4121 dapm->seq_notifier = snd_soc_component_seq_notifier;
4122 if (driver->stream_event)
4123 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004124
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004125 INIT_LIST_HEAD(&component->dai_list);
4126 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004127
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004128 return 0;
4129}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004130
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004131static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4132{
4133 list_add(&component->list, &component_list);
4134}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004135
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004136static void snd_soc_component_add(struct snd_soc_component *component)
4137{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004138 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004139 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004140 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004141}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004142
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004143static void snd_soc_component_cleanup(struct snd_soc_component *component)
4144{
4145 snd_soc_unregister_dais(component);
4146 kfree(component->name);
4147}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004148
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004149static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4150{
4151 list_del(&component->list);
4152}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004153
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004154static void snd_soc_component_del(struct snd_soc_component *component)
4155{
4156 mutex_lock(&client_mutex);
4157 snd_soc_component_del_unlocked(component);
4158 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004159}
4160
4161int snd_soc_register_component(struct device *dev,
4162 const struct snd_soc_component_driver *cmpnt_drv,
4163 struct snd_soc_dai_driver *dai_drv,
4164 int num_dai)
4165{
4166 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004167 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004168
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004169 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004170 if (!cmpnt) {
4171 dev_err(dev, "ASoC: Failed to allocate memory\n");
4172 return -ENOMEM;
4173 }
4174
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004175 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4176 if (ret)
4177 goto err_free;
4178
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004179 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004180 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004181
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004182 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4183 if (ret < 0) {
4184 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4185 goto err_cleanup;
4186 }
4187
4188 snd_soc_component_add(cmpnt);
4189
4190 return 0;
4191
4192err_cleanup:
4193 snd_soc_component_cleanup(cmpnt);
4194err_free:
4195 kfree(cmpnt);
4196 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004197}
4198EXPORT_SYMBOL_GPL(snd_soc_register_component);
4199
4200/**
4201 * snd_soc_unregister_component - Unregister a component from the ASoC core
4202 *
4203 */
4204void snd_soc_unregister_component(struct device *dev)
4205{
4206 struct snd_soc_component *cmpnt;
4207
4208 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004209 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004210 goto found;
4211 }
4212 return;
4213
4214found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004215 snd_soc_component_del(cmpnt);
4216 snd_soc_component_cleanup(cmpnt);
4217 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004218}
4219EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4220
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004221static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4222 unsigned int reg, unsigned int val)
4223{
4224 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4225
4226 return platform->driver->write(platform, reg, val);
4227}
4228
4229static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4230 unsigned int reg, unsigned int *val)
4231{
4232 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4233
4234 *val = platform->driver->read(platform, reg);
4235
4236 return 0;
4237}
4238
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004239/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004240 * snd_soc_add_platform - Add a platform to the ASoC core
4241 * @dev: The parent device for the platform
4242 * @platform: The platform to add
4243 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004244 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004245int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004246 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004247{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004248 int ret;
4249
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004250 ret = snd_soc_component_initialize(&platform->component,
4251 &platform_drv->component_driver, dev);
4252 if (ret)
4253 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004254
4255 platform->dev = dev;
4256 platform->driver = platform_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004257 if (platform_drv->write)
4258 platform->component.write = snd_soc_platform_drv_write;
4259 if (platform_drv->read)
4260 platform->component.read = snd_soc_platform_drv_read;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004261
4262 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004263 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004264 list_add(&platform->list, &platform_list);
4265 mutex_unlock(&client_mutex);
4266
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004267 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4268 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004269
4270 return 0;
4271}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004272EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4273
4274/**
4275 * snd_soc_register_platform - Register a platform with the ASoC core
4276 *
4277 * @platform: platform to register
4278 */
4279int snd_soc_register_platform(struct device *dev,
4280 const struct snd_soc_platform_driver *platform_drv)
4281{
4282 struct snd_soc_platform *platform;
4283 int ret;
4284
4285 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4286
4287 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4288 if (platform == NULL)
4289 return -ENOMEM;
4290
4291 ret = snd_soc_add_platform(dev, platform, platform_drv);
4292 if (ret)
4293 kfree(platform);
4294
4295 return ret;
4296}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004297EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4298
4299/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004300 * snd_soc_remove_platform - Remove a platform from the ASoC core
4301 * @platform: the platform to remove
4302 */
4303void snd_soc_remove_platform(struct snd_soc_platform *platform)
4304{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004305
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004306 mutex_lock(&client_mutex);
4307 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004308 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004309 mutex_unlock(&client_mutex);
4310
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004311 snd_soc_component_cleanup(&platform->component);
4312
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004313 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004314 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004315}
4316EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4317
4318struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4319{
4320 struct snd_soc_platform *platform;
4321
4322 list_for_each_entry(platform, &platform_list, list) {
4323 if (dev == platform->dev)
4324 return platform;
4325 }
4326
4327 return NULL;
4328}
4329EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4330
4331/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004332 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4333 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004334 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004335 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004336void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004337{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004338 struct snd_soc_platform *platform;
4339
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004340 platform = snd_soc_lookup_platform(dev);
4341 if (!platform)
4342 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004343
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004344 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004345 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004346}
4347EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4348
Mark Brown151ab222009-05-09 16:22:58 +01004349static u64 codec_format_map[] = {
4350 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4351 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4352 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4353 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4354 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4355 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4356 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4357 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4358 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4359 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4360 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4361 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4362 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4363 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4364 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4365 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4366};
4367
4368/* Fix up the DAI formats for endianness: codecs don't actually see
4369 * the endianness of the data but we're using the CPU format
4370 * definitions which do need to include endianness so we ensure that
4371 * codec DAIs always have both big and little endian variants set.
4372 */
4373static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4374{
4375 int i;
4376
4377 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4378 if (stream->formats & codec_format_map[i])
4379 stream->formats |= codec_format_map[i];
4380}
4381
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004382static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4383 unsigned int reg, unsigned int val)
4384{
4385 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4386
4387 return codec->driver->write(codec, reg, val);
4388}
4389
4390static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4391 unsigned int reg, unsigned int *val)
4392{
4393 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4394
4395 *val = codec->driver->read(codec, reg);
4396
4397 return 0;
4398}
4399
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004400static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4401 enum snd_soc_bias_level level)
4402{
4403 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4404
4405 return codec->driver->set_bias_level(codec, level);
4406}
4407
Mark Brown0d0cf002008-12-10 14:32:45 +00004408/**
4409 * snd_soc_register_codec - Register a codec with the ASoC core
4410 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004411 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004412 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004413int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004414 const struct snd_soc_codec_driver *codec_drv,
4415 struct snd_soc_dai_driver *dai_drv,
4416 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004417{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004418 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004419 struct snd_soc_dai *dai;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004420 struct regmap *regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004421 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004422
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004423 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004424
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004425 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4426 if (codec == NULL)
4427 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004428
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004429 codec->component.dapm_ptr = &codec->dapm;
4430
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004431 ret = snd_soc_component_initialize(&codec->component,
4432 &codec_drv->component_driver, dev);
4433 if (ret)
4434 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01004435
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004436 if (codec_drv->write)
4437 codec->component.write = snd_soc_codec_drv_write;
4438 if (codec_drv->read)
4439 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004440 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004441 codec->dapm.codec = codec;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004442 if (codec_drv->seq_notifier)
4443 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004444 if (codec_drv->set_bias_level)
4445 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004446 codec->dev = dev;
4447 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004448 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004449 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004450
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004451 if (!codec->component.write) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004452 if (codec_drv->get_regmap)
4453 regmap = codec_drv->get_regmap(dev);
4454 else
4455 regmap = dev_get_regmap(dev, NULL);
4456
4457 if (regmap) {
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004458 ret = snd_soc_component_init_io(&codec->component,
4459 regmap);
4460 if (ret) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004461 dev_err(codec->dev,
4462 "Failed to set cache I/O:%d\n",
4463 ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004464 goto err_cleanup;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004465 }
4466 }
4467 }
4468
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004469 for (i = 0; i < num_dai; i++) {
4470 fixup_codec_formats(&dai_drv[i].playback);
4471 fixup_codec_formats(&dai_drv[i].capture);
4472 }
4473
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004474 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4475 if (ret < 0) {
4476 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4477 goto err_cleanup;
4478 }
4479
4480 list_for_each_entry(dai, &codec->component.dai_list, list)
4481 dai->codec = codec;
4482
Mark Brown054880f2012-04-09 17:29:19 +01004483 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004484 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004485 list_add(&codec->list, &codec_list);
4486 mutex_unlock(&client_mutex);
4487
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004488 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4489 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004490 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004491
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004492err_cleanup:
4493 snd_soc_component_cleanup(&codec->component);
4494err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004495 kfree(codec);
4496 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004497}
4498EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4499
4500/**
4501 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4502 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004503 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004504 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004505void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004506{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004507 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004508
4509 list_for_each_entry(codec, &codec_list, list) {
4510 if (dev == codec->dev)
4511 goto found;
4512 }
4513 return;
4514
4515found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004516
Mark Brown0d0cf002008-12-10 14:32:45 +00004517 mutex_lock(&client_mutex);
4518 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004519 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004520 mutex_unlock(&client_mutex);
4521
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004522 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4523 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004524
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004525 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004526 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004527 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004528}
4529EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4530
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004531/* Retrieve a card's name from device tree */
4532int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4533 const char *propname)
4534{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304535 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004536 int ret;
4537
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304538 if (!card->dev) {
4539 pr_err("card->dev is not set before calling %s\n", __func__);
4540 return -EINVAL;
4541 }
4542
4543 np = card->dev->of_node;
4544
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004545 ret = of_property_read_string_index(np, propname, 0, &card->name);
4546 /*
4547 * EINVAL means the property does not exist. This is fine providing
4548 * card->name was previously set, which is checked later in
4549 * snd_soc_register_card.
4550 */
4551 if (ret < 0 && ret != -EINVAL) {
4552 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004553 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004554 propname, ret);
4555 return ret;
4556 }
4557
4558 return 0;
4559}
4560EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4561
Xiubo Li9a6d4862014-02-08 15:59:52 +08004562static const struct snd_soc_dapm_widget simple_widgets[] = {
4563 SND_SOC_DAPM_MIC("Microphone", NULL),
4564 SND_SOC_DAPM_LINE("Line", NULL),
4565 SND_SOC_DAPM_HP("Headphone", NULL),
4566 SND_SOC_DAPM_SPK("Speaker", NULL),
4567};
4568
4569int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4570 const char *propname)
4571{
4572 struct device_node *np = card->dev->of_node;
4573 struct snd_soc_dapm_widget *widgets;
4574 const char *template, *wname;
4575 int i, j, num_widgets, ret;
4576
4577 num_widgets = of_property_count_strings(np, propname);
4578 if (num_widgets < 0) {
4579 dev_err(card->dev,
4580 "ASoC: Property '%s' does not exist\n", propname);
4581 return -EINVAL;
4582 }
4583 if (num_widgets & 1) {
4584 dev_err(card->dev,
4585 "ASoC: Property '%s' length is not even\n", propname);
4586 return -EINVAL;
4587 }
4588
4589 num_widgets /= 2;
4590 if (!num_widgets) {
4591 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4592 propname);
4593 return -EINVAL;
4594 }
4595
4596 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4597 GFP_KERNEL);
4598 if (!widgets) {
4599 dev_err(card->dev,
4600 "ASoC: Could not allocate memory for widgets\n");
4601 return -ENOMEM;
4602 }
4603
4604 for (i = 0; i < num_widgets; i++) {
4605 ret = of_property_read_string_index(np, propname,
4606 2 * i, &template);
4607 if (ret) {
4608 dev_err(card->dev,
4609 "ASoC: Property '%s' index %d read error:%d\n",
4610 propname, 2 * i, ret);
4611 return -EINVAL;
4612 }
4613
4614 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4615 if (!strncmp(template, simple_widgets[j].name,
4616 strlen(simple_widgets[j].name))) {
4617 widgets[i] = simple_widgets[j];
4618 break;
4619 }
4620 }
4621
4622 if (j >= ARRAY_SIZE(simple_widgets)) {
4623 dev_err(card->dev,
4624 "ASoC: DAPM widget '%s' is not supported\n",
4625 template);
4626 return -EINVAL;
4627 }
4628
4629 ret = of_property_read_string_index(np, propname,
4630 (2 * i) + 1,
4631 &wname);
4632 if (ret) {
4633 dev_err(card->dev,
4634 "ASoC: Property '%s' index %d read error:%d\n",
4635 propname, (2 * i) + 1, ret);
4636 return -EINVAL;
4637 }
4638
4639 widgets[i].name = wname;
4640 }
4641
4642 card->dapm_widgets = widgets;
4643 card->num_dapm_widgets = num_widgets;
4644
4645 return 0;
4646}
4647EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4648
Xiubo Li89c67852014-02-14 09:34:35 +08004649int snd_soc_of_parse_tdm_slot(struct device_node *np,
4650 unsigned int *slots,
4651 unsigned int *slot_width)
4652{
4653 u32 val;
4654 int ret;
4655
4656 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4657 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4658 if (ret)
4659 return ret;
4660
4661 if (slots)
4662 *slots = val;
4663 }
4664
4665 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4666 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4667 if (ret)
4668 return ret;
4669
4670 if (slot_width)
4671 *slot_width = val;
4672 }
4673
4674 return 0;
4675}
4676EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4677
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004678int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4679 const char *propname)
4680{
4681 struct device_node *np = card->dev->of_node;
4682 int num_routes;
4683 struct snd_soc_dapm_route *routes;
4684 int i, ret;
4685
4686 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004687 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004688 dev_err(card->dev,
4689 "ASoC: Property '%s' does not exist or its length is not even\n",
4690 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004691 return -EINVAL;
4692 }
4693 num_routes /= 2;
4694 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004695 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004696 propname);
4697 return -EINVAL;
4698 }
4699
4700 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4701 GFP_KERNEL);
4702 if (!routes) {
4703 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004704 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004705 return -EINVAL;
4706 }
4707
4708 for (i = 0; i < num_routes; i++) {
4709 ret = of_property_read_string_index(np, propname,
4710 2 * i, &routes[i].sink);
4711 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004712 dev_err(card->dev,
4713 "ASoC: Property '%s' index %d could not be read: %d\n",
4714 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004715 return -EINVAL;
4716 }
4717 ret = of_property_read_string_index(np, propname,
4718 (2 * i) + 1, &routes[i].source);
4719 if (ret) {
4720 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004721 "ASoC: Property '%s' index %d could not be read: %d\n",
4722 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004723 return -EINVAL;
4724 }
4725 }
4726
4727 card->num_dapm_routes = num_routes;
4728 card->dapm_routes = routes;
4729
4730 return 0;
4731}
4732EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4733
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004734unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004735 const char *prefix,
4736 struct device_node **bitclkmaster,
4737 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004738{
4739 int ret, i;
4740 char prop[128];
4741 unsigned int format = 0;
4742 int bit, frame;
4743 const char *str;
4744 struct {
4745 char *name;
4746 unsigned int val;
4747 } of_fmt_table[] = {
4748 { "i2s", SND_SOC_DAIFMT_I2S },
4749 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4750 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4751 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4752 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4753 { "ac97", SND_SOC_DAIFMT_AC97 },
4754 { "pdm", SND_SOC_DAIFMT_PDM},
4755 { "msb", SND_SOC_DAIFMT_MSB },
4756 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004757 };
4758
4759 if (!prefix)
4760 prefix = "";
4761
4762 /*
4763 * check "[prefix]format = xxx"
4764 * SND_SOC_DAIFMT_FORMAT_MASK area
4765 */
4766 snprintf(prop, sizeof(prop), "%sformat", prefix);
4767 ret = of_property_read_string(np, prop, &str);
4768 if (ret == 0) {
4769 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4770 if (strcmp(str, of_fmt_table[i].name) == 0) {
4771 format |= of_fmt_table[i].val;
4772 break;
4773 }
4774 }
4775 }
4776
4777 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004778 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004779 * SND_SOC_DAIFMT_CLOCK_MASK area
4780 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004781 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4782 if (of_get_property(np, prop, NULL))
4783 format |= SND_SOC_DAIFMT_CONT;
4784 else
4785 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004786
4787 /*
4788 * check "[prefix]bitclock-inversion"
4789 * check "[prefix]frame-inversion"
4790 * SND_SOC_DAIFMT_INV_MASK area
4791 */
4792 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4793 bit = !!of_get_property(np, prop, NULL);
4794
4795 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4796 frame = !!of_get_property(np, prop, NULL);
4797
4798 switch ((bit << 4) + frame) {
4799 case 0x11:
4800 format |= SND_SOC_DAIFMT_IB_IF;
4801 break;
4802 case 0x10:
4803 format |= SND_SOC_DAIFMT_IB_NF;
4804 break;
4805 case 0x01:
4806 format |= SND_SOC_DAIFMT_NB_IF;
4807 break;
4808 default:
4809 /* SND_SOC_DAIFMT_NB_NF is default */
4810 break;
4811 }
4812
4813 /*
4814 * check "[prefix]bitclock-master"
4815 * check "[prefix]frame-master"
4816 * SND_SOC_DAIFMT_MASTER_MASK area
4817 */
4818 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4819 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004820 if (bit && bitclkmaster)
4821 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004822
4823 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4824 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004825 if (frame && framemaster)
4826 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004827
4828 switch ((bit << 4) + frame) {
4829 case 0x11:
4830 format |= SND_SOC_DAIFMT_CBM_CFM;
4831 break;
4832 case 0x10:
4833 format |= SND_SOC_DAIFMT_CBM_CFS;
4834 break;
4835 case 0x01:
4836 format |= SND_SOC_DAIFMT_CBS_CFM;
4837 break;
4838 default:
4839 format |= SND_SOC_DAIFMT_CBS_CFS;
4840 break;
4841 }
4842
4843 return format;
4844}
4845EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4846
Kuninori Morimotocb470082013-09-10 17:39:56 -07004847int snd_soc_of_get_dai_name(struct device_node *of_node,
4848 const char **dai_name)
4849{
4850 struct snd_soc_component *pos;
4851 struct of_phandle_args args;
4852 int ret;
4853
4854 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4855 "#sound-dai-cells", 0, &args);
4856 if (ret)
4857 return ret;
4858
4859 ret = -EPROBE_DEFER;
4860
4861 mutex_lock(&client_mutex);
4862 list_for_each_entry(pos, &component_list, list) {
4863 if (pos->dev->of_node != args.np)
4864 continue;
4865
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004866 if (pos->driver->of_xlate_dai_name) {
4867 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4868 } else {
4869 int id = -1;
4870
4871 switch (args.args_count) {
4872 case 0:
4873 id = 0; /* same as dai_drv[0] */
4874 break;
4875 case 1:
4876 id = args.args[0];
4877 break;
4878 default:
4879 /* not supported */
4880 break;
4881 }
4882
4883 if (id < 0 || id >= pos->num_dai) {
4884 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004885 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004886 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004887
4888 ret = 0;
4889
4890 *dai_name = pos->dai_drv[id].name;
4891 if (!*dai_name)
4892 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004893 }
4894
Kuninori Morimotocb470082013-09-10 17:39:56 -07004895 break;
4896 }
4897 mutex_unlock(&client_mutex);
4898
4899 of_node_put(args.np);
4900
4901 return ret;
4902}
4903EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4904
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004905static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004906{
Mark Brown384c89e2008-12-03 17:34:03 +00004907#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004908 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4909 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004910 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004911 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004912 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004913
Mark Brown8a9dab12011-01-10 22:25:21 +00004914 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004915 &codec_list_fops))
4916 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4917
Mark Brown8a9dab12011-01-10 22:25:21 +00004918 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004919 &dai_list_fops))
4920 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004921
Mark Brown8a9dab12011-01-10 22:25:21 +00004922 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004923 &platform_list_fops))
4924 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004925#endif
4926
Mark Brownfb257892011-04-28 10:57:54 +01004927 snd_soc_util_init();
4928
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004929 return platform_driver_register(&soc_driver);
4930}
Mark Brown4abe8e12010-10-12 17:41:03 +01004931module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004932
Mark Brown7d8c16a2008-11-30 22:11:24 +00004933static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004934{
Mark Brownfb257892011-04-28 10:57:54 +01004935 snd_soc_util_exit();
4936
Mark Brown384c89e2008-12-03 17:34:03 +00004937#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004938 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004939#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004940 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004941}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004942module_exit(snd_soc_exit);
4943
4944/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004945MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004946MODULE_DESCRIPTION("ALSA SoC Core");
4947MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004948MODULE_ALIAS("platform:soc-audio");