blob: 2bdf9a4ac2b4c25d59b0a26a9eab67e9e34b5347 [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
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200273static void soc_init_component_debugfs(struct snd_soc_component *component)
Russell Kinge73f3de2014-06-26 15:22:50 +0100274{
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200275 if (component->debugfs_prefix) {
276 char *name;
Russell Kinge73f3de2014-06-26 15:22:50 +0100277
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200278 name = kasprintf(GFP_KERNEL, "%s:%s",
279 component->debugfs_prefix, component->name);
280 if (name) {
281 component->debugfs_root = debugfs_create_dir(name,
282 component->card->debugfs_card_root);
283 kfree(name);
284 }
285 } else {
286 component->debugfs_root = debugfs_create_dir(component->name,
287 component->card->debugfs_card_root);
288 }
Russell Kinge73f3de2014-06-26 15:22:50 +0100289
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200290 if (!component->debugfs_root) {
291 dev_warn(component->dev,
292 "ASoC: Failed to create component debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000293 return;
294 }
295
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200296 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
297 component->debugfs_root);
298
299 if (component->init_debugfs)
300 component->init_debugfs(component);
301}
302
303static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
304{
305 debugfs_remove_recursive(component->debugfs_root);
306}
307
308static void soc_init_codec_debugfs(struct snd_soc_component *component)
309{
310 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
311
312 debugfs_create_bool("cache_sync", 0444, codec->component.debugfs_root,
Mark Brownaaee8ef2011-01-26 20:53:50 +0000313 &codec->cache_sync);
Mark Brownaaee8ef2011-01-26 20:53:50 +0000314
Mark Brown2624d5f2009-11-03 21:56:13 +0000315 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200316 codec->component.debugfs_root,
Mark Brown2624d5f2009-11-03 21:56:13 +0000317 codec, &codec_reg_fops);
318 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200319 dev_warn(codec->dev,
320 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000321}
322
Mark Brownc3c5a192010-09-15 18:15:14 +0100323static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
324 size_t count, loff_t *ppos)
325{
326 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100327 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100328 struct snd_soc_codec *codec;
329
330 if (!buf)
331 return -ENOMEM;
332
Mark Brown2b194f9d2010-10-13 10:52:16 +0100333 list_for_each_entry(codec, &codec_list, list) {
334 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200335 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100336 if (len >= 0)
337 ret += len;
338 if (ret > PAGE_SIZE) {
339 ret = PAGE_SIZE;
340 break;
341 }
342 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100343
344 if (ret >= 0)
345 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
346
347 kfree(buf);
348
349 return ret;
350}
351
352static const struct file_operations codec_list_fops = {
353 .read = codec_list_read_file,
354 .llseek = default_llseek,/* read accesses f_pos */
355};
356
Mark Brownf3208782010-09-15 18:19:07 +0100357static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
358 size_t count, loff_t *ppos)
359{
360 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100361 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100362 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100363 struct snd_soc_dai *dai;
364
365 if (!buf)
366 return -ENOMEM;
367
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100368 list_for_each_entry(component, &component_list, list) {
369 list_for_each_entry(dai, &component->dai_list, list) {
370 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
371 dai->name);
372 if (len >= 0)
373 ret += len;
374 if (ret > PAGE_SIZE) {
375 ret = PAGE_SIZE;
376 break;
377 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100378 }
379 }
Mark Brownf3208782010-09-15 18:19:07 +0100380
Mark Brown2b194f9d2010-10-13 10:52:16 +0100381 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100382
383 kfree(buf);
384
385 return ret;
386}
387
388static const struct file_operations dai_list_fops = {
389 .read = dai_list_read_file,
390 .llseek = default_llseek,/* read accesses f_pos */
391};
392
Mark Brown19c7ac22010-09-15 18:22:40 +0100393static ssize_t platform_list_read_file(struct file *file,
394 char __user *user_buf,
395 size_t count, loff_t *ppos)
396{
397 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100398 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100399 struct snd_soc_platform *platform;
400
401 if (!buf)
402 return -ENOMEM;
403
Mark Brown2b194f9d2010-10-13 10:52:16 +0100404 list_for_each_entry(platform, &platform_list, list) {
405 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200406 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100407 if (len >= 0)
408 ret += len;
409 if (ret > PAGE_SIZE) {
410 ret = PAGE_SIZE;
411 break;
412 }
413 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100414
Mark Brown2b194f9d2010-10-13 10:52:16 +0100415 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100416
417 kfree(buf);
418
419 return ret;
420}
421
422static const struct file_operations platform_list_fops = {
423 .read = platform_list_read_file,
424 .llseek = default_llseek,/* read accesses f_pos */
425};
426
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200427static void soc_init_card_debugfs(struct snd_soc_card *card)
428{
429 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000430 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200431 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200432 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100433 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200434 return;
435 }
436
437 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
438 card->debugfs_card_root,
439 &card->pop_time);
440 if (!card->debugfs_pop_time)
441 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000442 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200443}
444
445static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
446{
447 debugfs_remove_recursive(card->debugfs_card_root);
448}
449
Mark Brown2624d5f2009-11-03 21:56:13 +0000450#else
451
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200452#define soc_init_codec_debugfs NULL
453
454static inline void soc_init_component_debugfs(
455 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000456{
457}
458
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200459static inline void soc_cleanup_component_debugfs(
460 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000461{
462}
463
Axel Linb95fccb2010-11-09 17:06:44 +0800464static inline void soc_init_card_debugfs(struct snd_soc_card *card)
465{
466}
467
468static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
469{
470}
Mark Brown2624d5f2009-11-03 21:56:13 +0000471#endif
472
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100473struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
474 const char *dai_link, int stream)
475{
476 int i;
477
478 for (i = 0; i < card->num_links; i++) {
479 if (card->rtd[i].dai_link->no_pcm &&
480 !strcmp(card->rtd[i].dai_link->name, dai_link))
481 return card->rtd[i].pcm->streams[stream].substream;
482 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000483 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100484 return NULL;
485}
486EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
487
488struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
489 const char *dai_link)
490{
491 int i;
492
493 for (i = 0; i < card->num_links; i++) {
494 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
495 return &card->rtd[i];
496 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000497 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100498 return NULL;
499}
500EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
501
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200502#ifdef CONFIG_SND_SOC_AC97_BUS
503/* unregister ac97 codec */
504static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
505{
506 if (codec->ac97->dev.bus)
507 device_unregister(&codec->ac97->dev);
508 return 0;
509}
510
511/* stop no dev release warning */
512static void soc_ac97_device_release(struct device *dev){}
513
514/* register ac97 codec to bus */
515static int soc_ac97_dev_register(struct snd_soc_codec *codec)
516{
517 int err;
518
519 codec->ac97->dev.bus = &ac97_bus_type;
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200520 codec->ac97->dev.parent = codec->component.card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200521 codec->ac97->dev.release = soc_ac97_device_release;
522
Kay Sieversbb072bf2008-11-02 03:50:35 +0100523 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200524 codec->component.card->snd_card->number, 0,
525 codec->component.name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200526 err = device_register(&codec->ac97->dev);
527 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000528 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200529 codec->ac97->dev.bus = NULL;
530 return err;
531 }
532 return 0;
533}
534#endif
535
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100536static void codec2codec_close_delayed_work(struct work_struct *work)
537{
538 /* Currently nothing to do for c2c links
539 * Since c2c links are internal nodes in the DAPM graph and
540 * don't interface with the outside world or application layer
541 * we don't have to do any special handling on close.
542 */
543}
544
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000545#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200546/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000547int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000549 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200550 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200551 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200552
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200553 /* If the card is not initialized yet there is nothing to do */
554 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200555 return 0;
556
Andy Green6ed25972008-06-13 16:24:05 +0100557 /* Due to the resume being scheduled into a workqueue we could
558 * suspend before that's finished - wait for it to complete.
559 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000560 snd_power_lock(card->snd_card);
561 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
562 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100563
564 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000565 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100566
Mark Browna00f90f2010-12-02 16:24:24 +0000567 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000568 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100569
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000570 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100571 continue;
572
Benoit Cousson88bd8702014-07-08 23:19:34 +0200573 for (j = 0; j < card->rtd[i].num_codecs; j++) {
574 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
575 struct snd_soc_dai_driver *drv = dai->driver;
576
577 if (drv->ops->digital_mute && dai->playback_active)
578 drv->ops->digital_mute(dai, 1);
579 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200580 }
581
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100582 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000583 for (i = 0; i < card->num_rtd; i++) {
584 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100585 continue;
586
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100588 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100589
Mark Brown87506542008-11-18 20:50:34 +0000590 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000591 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000593 for (i = 0; i < card->num_rtd; i++) {
594 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
595 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100596
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000597 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100598 continue;
599
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
601 cpu_dai->driver->suspend(cpu_dai);
602 if (platform->driver->suspend && !platform->suspended) {
603 platform->driver->suspend(cpu_dai);
604 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100605 }
606 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200607
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 /* close any waiting streams and save state */
609 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200610 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
Tejun Heo43829732012-08-20 14:51:24 -0700611 flush_delayed_work(&card->rtd[i].delayed_work);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200612 for (j = 0; j < card->rtd[i].num_codecs; j++) {
613 codec_dais[j]->codec->dapm.suspend_bias_level =
614 codec_dais[j]->codec->dapm.bias_level;
615 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000616 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100617
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000619
620 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100621 continue;
622
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800623 snd_soc_dapm_stream_event(&card->rtd[i],
624 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800625 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800627 snd_soc_dapm_stream_event(&card->rtd[i],
628 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800629 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630 }
631
Mark Browne2d32ff2012-08-31 17:38:32 -0700632 /* Recheck all analogue paths too */
633 dapm_mark_io_dirty(&card->dapm);
634 snd_soc_dapm_sync(&card->dapm);
635
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000636 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200637 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000638 /* If there are paths active then the CODEC will be held with
639 * bias _ON and should not be suspended. */
640 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200641 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000643 /*
644 * If the CODEC is capable of idle
645 * bias off then being in STANDBY
646 * means it's doing something,
647 * otherwise fall through.
648 */
649 if (codec->dapm.idle_bias_off) {
650 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200651 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000652 break;
653 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000654 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100655 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900657 codec->cache_sync = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200658 if (codec->component.regmap)
659 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800660 /* deactivate pins to sleep state */
661 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000662 break;
663 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200664 dev_dbg(codec->dev,
665 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000666 break;
667 }
668 }
669 }
670
671 for (i = 0; i < card->num_rtd; i++) {
672 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
673
674 if (card->rtd[i].dai_link->ignore_suspend)
675 continue;
676
677 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
678 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800679
680 /* deactivate pins to sleep state */
681 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200682 }
683
Mark Brown87506542008-11-18 20:50:34 +0000684 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000685 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200686
687 return 0;
688}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000689EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690
Andy Green6ed25972008-06-13 16:24:05 +0100691/* deferred resume work, so resume can complete before we finished
692 * setting our codec back up, which can be very slow on I2C
693 */
694static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200695{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 struct snd_soc_card *card =
697 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200698 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200699 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700
Andy Green6ed25972008-06-13 16:24:05 +0100701 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
702 * so userspace apps are blocked from touching us
703 */
704
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000705 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100706
Mark Brown99497882010-05-07 20:24:05 +0100707 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000708 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100709
Mark Brown87506542008-11-18 20:50:34 +0000710 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000711 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713 /* resume AC97 DAIs */
714 for (i = 0; i < card->num_rtd; i++) {
715 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100716
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100718 continue;
719
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000720 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
721 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200722 }
723
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200724 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000725 /* If the CODEC was idle over suspend then it will have been
726 * left with bias OFF or STANDBY and suspended so we must now
727 * resume. Otherwise the suspend was suppressed.
728 */
729 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200730 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000731 case SND_SOC_BIAS_STANDBY:
732 case SND_SOC_BIAS_OFF:
733 codec->driver->resume(codec);
734 codec->suspended = 0;
735 break;
736 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200737 dev_dbg(codec->dev,
738 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000739 break;
740 }
Mark Brown1547aba2010-05-07 21:11:40 +0100741 }
742 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000744 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100745
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000746 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100747 continue;
748
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800749 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000750 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800751 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000752
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800753 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000754 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800755 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200756 }
757
Mark Brown3ff3f642008-05-19 12:32:25 +0200758 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000759 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100760
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000761 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100762 continue;
763
Benoit Cousson88bd8702014-07-08 23:19:34 +0200764 for (j = 0; j < card->rtd[i].num_codecs; j++) {
765 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
766 struct snd_soc_dai_driver *drv = dai->driver;
767
768 if (drv->ops->digital_mute && dai->playback_active)
769 drv->ops->digital_mute(dai, 0);
770 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200771 }
772
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000773 for (i = 0; i < card->num_rtd; i++) {
774 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
775 struct snd_soc_platform *platform = card->rtd[i].platform;
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
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000780 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
781 cpu_dai->driver->resume(cpu_dai);
782 if (platform->driver->resume && platform->suspended) {
783 platform->driver->resume(cpu_dai);
784 platform->suspended = 0;
785 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200786 }
787
Mark Brown87506542008-11-18 20:50:34 +0000788 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000789 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000791 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100792
793 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000794 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700795
796 /* Recheck all analogue paths too */
797 dapm_mark_io_dirty(&card->dapm);
798 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100799}
800
801/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000802int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100803{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000804 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600805 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200806
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200807 /* If the card is not initialized yet there is nothing to do */
808 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800809 return 0;
810
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800811 /* activate pins from sleep state */
812 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200813 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
814 struct snd_soc_dai **codec_dais = rtd->codec_dais;
815 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
816 int j;
817
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800818 if (cpu_dai->active)
819 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200820
821 for (j = 0; j < rtd->num_codecs; j++) {
822 struct snd_soc_dai *codec_dai = codec_dais[j];
823 if (codec_dai->active)
824 pinctrl_pm_select_default_state(codec_dai->dev);
825 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800826 }
827
Mark Brown64ab9ba2009-03-31 11:27:03 +0100828 /* AC97 devices might have other drivers hanging off them so
829 * need to resume immediately. Other drivers don't have that
830 * problem and may take a substantial amount of time to resume
831 * due to I/O costs and anti-pop so handle them out of line.
832 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000833 for (i = 0; i < card->num_rtd; i++) {
834 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600835 ac97_control |= cpu_dai->driver->ac97_control;
836 }
837 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000838 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600839 soc_resume_deferred(&card->deferred_resume_work);
840 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000841 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600842 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000843 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100844 }
Andy Green6ed25972008-06-13 16:24:05 +0100845
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200846 return 0;
847}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000848EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000850#define snd_soc_suspend NULL
851#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200852#endif
853
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100854static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800855};
856
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200857static struct snd_soc_component *soc_find_component(
858 const struct device_node *of_node, const char *name)
859{
860 struct snd_soc_component *component;
861
862 list_for_each_entry(component, &component_list, list) {
863 if (of_node) {
864 if (component->dev->of_node == of_node)
865 return component;
866 } else if (strcmp(component->name, name) == 0) {
867 return component;
868 }
869 }
870
871 return NULL;
872}
873
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200874static struct snd_soc_dai *snd_soc_find_dai(
875 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100876{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200877 struct snd_soc_component *component;
878 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100879
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200880 /* Find CPU DAI from registered DAIs*/
881 list_for_each_entry(component, &component_list, list) {
882 if (dlc->of_node && component->dev->of_node != dlc->of_node)
883 continue;
884 if (dlc->name && strcmp(dev_name(component->dev), dlc->name))
885 continue;
886 list_for_each_entry(dai, &component->dai_list, list) {
887 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100888 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100889
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200890 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100891 }
892 }
893
894 return NULL;
895}
896
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000897static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200898{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000899 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
900 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200901 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200902 struct snd_soc_dai_link_component cpu_dai_component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200903 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000904 struct snd_soc_platform *platform;
Mark Brown848dd8b2011-04-27 18:16:32 +0100905 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200906 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200907
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000908 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000909
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200910 cpu_dai_component.name = dai_link->cpu_name;
911 cpu_dai_component.of_node = dai_link->cpu_of_node;
912 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
913 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000914 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000915 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000916 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000917 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000918 }
919
Benoit Cousson88bd8702014-07-08 23:19:34 +0200920 rtd->num_codecs = dai_link->num_codecs;
921
922 /* Find CODEC from registered CODECs */
923 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200924 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200925 if (!codec_dais[i]) {
926 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
927 codecs[i].dai_name);
928 return -EPROBE_DEFER;
929 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000930 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100931
Benoit Cousson88bd8702014-07-08 23:19:34 +0200932 /* Single codec links expect codec and codec_dai in runtime data */
933 rtd->codec_dai = codec_dais[0];
934 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100935
Mark Brown848dd8b2011-04-27 18:16:32 +0100936 /* if there's no platform we match on the empty platform */
937 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700938 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100939 platform_name = "snd-soc-dummy";
940
Mark Brownb19e6e72012-03-14 21:18:39 +0000941 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000942 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700943 if (dai_link->platform_of_node) {
944 if (platform->dev->of_node !=
945 dai_link->platform_of_node)
946 continue;
947 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200948 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700949 continue;
950 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700951
952 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000953 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000954 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000955 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000956 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000957 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000958 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000959
960 card->num_rtd++;
961
962 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000963}
964
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200965static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600966{
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200967 if (!component->probed)
968 return;
969
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200970 /* This is a HACK and will be removed soon */
971 if (component->codec)
972 list_del(&component->codec->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600973
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200974 if (component->remove)
975 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600976
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200977 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600978
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200979 soc_cleanup_component_debugfs(component);
980 component->probed = 0;
981 module_put(component->dev->driver->owner);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200982}
983
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200984static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100985{
986 int err;
987
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200988 if (dai && dai->probed &&
989 dai->driver->remove_order == order) {
990 if (dai->driver->remove) {
991 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100992 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200993 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100994 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200995 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100996 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200997 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100998 }
999}
1000
Stephen Warren62ae68f2012-06-08 12:34:23 -06001001static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002{
1003 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001004 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001005
1006 /* unregister the rtd device */
1007 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001008 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1009 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1010 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001011 rtd->dev_registered = 0;
1012 }
1013
1014 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001015 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001016 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001017
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001018 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001019}
1020
Stephen Warren62ae68f2012-06-08 12:34:23 -06001021static void soc_remove_link_components(struct snd_soc_card *card, int num,
1022 int order)
1023{
1024 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1025 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001026 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001027 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001028 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001029
1030 /* remove the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001031 if (platform && platform->component.driver->remove_order == order)
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001032 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001033
1034 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001035 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001036 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001037 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001038 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001039 }
1040
1041 /* remove any CPU-side CODEC */
1042 if (cpu_dai) {
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001043 if (cpu_dai->component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001044 soc_remove_component(cpu_dai->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001045 }
1046}
1047
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001048static void soc_remove_dai_links(struct snd_soc_card *card)
1049{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001050 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001051
Liam Girdwood0168bf02011-06-07 16:08:05 +01001052 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1053 order++) {
1054 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001055 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001056 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001057
1058 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1059 order++) {
1060 for (dai = 0; dai < card->num_rtd; dai++)
1061 soc_remove_link_components(card, dai, order);
1062 }
1063
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001064 card->num_rtd = 0;
1065}
1066
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001067static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001068 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001069{
1070 int i;
1071
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001072 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001073 return;
1074
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001075 for (i = 0; i < card->num_configs; i++) {
1076 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001077 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001078 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001079 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001080 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001081 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001082 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001083 }
1084}
1085
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001086static int soc_probe_component(struct snd_soc_card *card,
1087 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001088{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001089 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1090 struct snd_soc_component *dai_component, *component2;
Mark Brown888df392012-02-16 19:37:51 -08001091 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001092 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001093
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001094 if (component->probed)
1095 return 0;
1096
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001097 component->card = card;
1098 dapm->card = card;
1099 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001100
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001101 if (!try_module_get(component->dev->driver->owner))
Jarkko Nikula70d29332011-01-27 16:24:22 +02001102 return -ENODEV;
1103
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001104 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001105
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001106 if (component->dapm_widgets) {
1107 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1108 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001109
1110 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001111 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001112 "Failed to create new controls %d\n", ret);
1113 goto err_probe;
1114 }
1115 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001116
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001117 /*
1118 * This is rather ugly, but certain platforms expect that the DAPM
1119 * widgets for the DAIs for components with the same parent device are
1120 * created in the platforms DAPM context. Until that is fixed we need to
1121 * keep this.
1122 */
1123 if (component->steal_sibling_dai_widgets) {
1124 dai_component = NULL;
1125 list_for_each_entry(component2, &component_list, list) {
1126 if (component == component2)
1127 continue;
Nariman Poushin261edc72014-03-31 15:47:12 +01001128
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001129 if (component2->dev == component->dev &&
1130 !list_empty(&component2->dai_list)) {
1131 dai_component = component2;
1132 break;
1133 }
1134 }
1135 } else {
1136 dai_component = component;
1137 list_for_each_entry(component2, &component_list, list) {
1138 if (component2->dev == component->dev &&
1139 component2->steal_sibling_dai_widgets) {
1140 dai_component = NULL;
1141 break;
1142 }
Nariman Poushin261edc72014-03-31 15:47:12 +01001143 }
1144 }
Mark Brown888df392012-02-16 19:37:51 -08001145
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001146 if (dai_component) {
1147 list_for_each_entry(dai, &dai_component->dai_list, list) {
1148 snd_soc_dapm_new_dai_widgets(dapm, dai);
1149 if (ret != 0) {
1150 dev_err(component->dev,
1151 "Failed to create DAI widgets %d\n",
1152 ret);
1153 goto err_probe;
1154 }
1155 }
1156 }
Mark Brown33c5f962011-08-22 18:40:30 +01001157
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001158 if (component->probe) {
1159 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001160 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001161 dev_err(component->dev,
1162 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001163 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001164 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001165
1166 WARN(dapm->idle_bias_off &&
1167 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001168 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001169 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001170 }
1171
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001172 if (component->controls)
1173 snd_soc_add_component_controls(component, component->controls,
1174 component->num_controls);
1175 if (component->dapm_routes)
1176 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1177 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001178
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001179 component->probed = 1;
1180 list_add(&dapm->list, &card->dapm_list);
1181
1182 /* This is a HACK and will be removed soon */
1183 if (component->codec)
1184 list_add(&component->codec->card_list, &card->codec_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001185
Jarkko Nikula70d29332011-01-27 16:24:22 +02001186 return 0;
1187
1188err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001189 soc_cleanup_component_debugfs(component);
1190 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001191
1192 return ret;
1193}
1194
Mark Brown36ae1a92012-01-06 17:12:45 -08001195static void rtd_release(struct device *dev)
1196{
1197 kfree(dev);
1198}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001199
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001200static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1201 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001202{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001203 int ret = 0;
1204
Jarkko Nikula589c3562010-12-06 16:27:07 +02001205 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001206 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1207 if (!rtd->dev)
1208 return -ENOMEM;
1209 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001210 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001211 rtd->dev->release = rtd_release;
1212 rtd->dev->init_name = name;
1213 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001214 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001215 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1216 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1217 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1218 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001219 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001220 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001221 /* calling put_device() here to free the rtd->dev */
1222 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001223 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001224 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001225 return ret;
1226 }
1227 rtd->dev_registered = 1;
1228
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001229 if (rtd->codec) {
1230 /* add DAPM sysfs entries for this codec */
1231 ret = snd_soc_dapm_sys_add(rtd->dev);
1232 if (ret < 0)
1233 dev_err(rtd->dev,
1234 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1235 ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001236
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001237 /* add codec sysfs entries */
1238 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1239 if (ret < 0)
1240 dev_err(rtd->dev,
1241 "ASoC: failed to add codec sysfs files: %d\n",
1242 ret);
1243 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001244
1245 return 0;
1246}
1247
Stephen Warren62ae68f2012-06-08 12:34:23 -06001248static int soc_probe_link_components(struct snd_soc_card *card, int num,
1249 int order)
1250{
1251 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Stephen Warren62ae68f2012-06-08 12:34:23 -06001252 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001253 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001254 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001255
1256 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001257 component = rtd->cpu_dai->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001258 if (component->driver->probe_order == order) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001259 ret = soc_probe_component(card, component);
1260 if (ret < 0)
1261 return ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001262 }
1263
Benoit Cousson88bd8702014-07-08 23:19:34 +02001264 /* probe the CODEC-side components */
1265 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001266 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001267 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001268 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001269 if (ret < 0)
1270 return ret;
1271 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001272 }
1273
1274 /* probe the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001275 if (platform->component.driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001276 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001277 if (ret < 0)
1278 return ret;
1279 }
1280
1281 return 0;
1282}
1283
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001284static int soc_probe_codec_dai(struct snd_soc_card *card,
1285 struct snd_soc_dai *codec_dai,
1286 int order)
1287{
1288 int ret;
1289
1290 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1291 if (codec_dai->driver->probe) {
1292 ret = codec_dai->driver->probe(codec_dai);
1293 if (ret < 0) {
1294 dev_err(codec_dai->dev,
1295 "ASoC: failed to probe CODEC DAI %s: %d\n",
1296 codec_dai->name, ret);
1297 return ret;
1298 }
1299 }
1300
1301 /* mark codec_dai as probed and add to card dai list */
1302 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001303 }
1304
1305 return 0;
1306}
1307
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001308static int soc_link_dai_widgets(struct snd_soc_card *card,
1309 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001310 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001311{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001312 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1313 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001314 struct snd_soc_dapm_widget *play_w, *capture_w;
1315 int ret;
1316
Benoit Cousson88bd8702014-07-08 23:19:34 +02001317 if (rtd->num_codecs > 1)
1318 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1319
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001320 /* link the DAI widgets */
1321 play_w = codec_dai->playback_widget;
1322 capture_w = cpu_dai->capture_widget;
1323 if (play_w && capture_w) {
1324 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1325 capture_w, play_w);
1326 if (ret != 0) {
1327 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1328 play_w->name, capture_w->name, ret);
1329 return ret;
1330 }
1331 }
1332
1333 play_w = cpu_dai->playback_widget;
1334 capture_w = codec_dai->capture_widget;
1335 if (play_w && capture_w) {
1336 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1337 capture_w, play_w);
1338 if (ret != 0) {
1339 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1340 play_w->name, capture_w->name, ret);
1341 return ret;
1342 }
1343 }
1344
1345 return 0;
1346}
1347
Stephen Warren62ae68f2012-06-08 12:34:23 -06001348static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001349{
1350 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1351 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001352 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001353 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001354 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001355
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001356 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001357 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001358
1359 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001360 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001361 cpu_dai->card = card;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001362 for (i = 0; i < rtd->num_codecs; i++)
1363 rtd->codec_dais[i]->card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001364
1365 /* set default power off timeout */
1366 rtd->pmdown_time = pmdown_time;
1367
1368 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001369 if (!cpu_dai->probed &&
1370 cpu_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001371 if (cpu_dai->driver->probe) {
1372 ret = cpu_dai->driver->probe(cpu_dai);
1373 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001374 dev_err(cpu_dai->dev,
1375 "ASoC: failed to probe CPU DAI %s: %d\n",
1376 cpu_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001377 return ret;
1378 }
1379 }
1380 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001381 }
1382
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001383 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001384 for (i = 0; i < rtd->num_codecs; i++) {
1385 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1386 if (ret)
1387 return ret;
1388 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001389
Liam Girdwood0168bf02011-06-07 16:08:05 +01001390 /* complete DAI probe during last probe */
1391 if (order != SND_SOC_COMP_ORDER_LAST)
1392 return 0;
1393
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001394 /* do machine specific initialization */
1395 if (dai_link->init) {
1396 ret = dai_link->init(rtd);
1397 if (ret < 0) {
1398 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1399 dai_link->name, ret);
1400 return ret;
1401 }
1402 }
1403
1404 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001405 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001406 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001407
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001408#ifdef CONFIG_DEBUG_FS
1409 /* add DPCM sysfs entries */
1410 if (dai_link->dynamic) {
1411 ret = soc_dpcm_debugfs_add(rtd);
1412 if (ret < 0) {
1413 dev_err(rtd->dev,
1414 "ASoC: failed to add dpcm sysfs entries: %d\n",
1415 ret);
1416 return ret;
1417 }
1418 }
1419#endif
1420
Mark Brown36ae1a92012-01-06 17:12:45 -08001421 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001422 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001423 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1424 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001425
Namarta Kohli1245b702012-08-16 17:10:41 +05301426 if (cpu_dai->driver->compress_dai) {
1427 /*create compress_device"*/
1428 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001429 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001430 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301431 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001432 return ret;
1433 }
1434 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301435
1436 if (!dai_link->params) {
1437 /* create the pcm */
1438 ret = soc_new_pcm(rtd, num);
1439 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001440 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301441 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001442 return ret;
1443 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301444 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001445 INIT_DELAYED_WORK(&rtd->delayed_work,
1446 codec2codec_close_delayed_work);
1447
Namarta Kohli1245b702012-08-16 17:10:41 +05301448 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001449 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001450 if (ret)
1451 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001452 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001453 }
1454
1455 /* add platform data for AC97 devices */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001456 for (i = 0; i < rtd->num_codecs; i++) {
1457 if (rtd->codec_dais[i]->driver->ac97_control)
1458 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1459 rtd->cpu_dai->ac97_pdata);
1460 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001461
1462 return 0;
1463}
1464
1465#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001466static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1467 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001468{
1469 int ret;
1470
1471 /* Only instantiate AC97 if not already done by the adaptor
1472 * for the generic AC97 subsystem.
1473 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001474 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001475 /*
1476 * It is possible that the AC97 device is already registered to
1477 * the device subsystem. This happens when the device is created
1478 * via snd_ac97_mixer(). Currently only SoC codec that does so
1479 * is the generic AC97 glue but others migh emerge.
1480 *
1481 * In those cases we don't try to register the device again.
1482 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001483 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001484 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001485
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001486 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001487 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001488 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001489 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001490 return ret;
1491 }
1492
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001493 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001494 }
1495 return 0;
1496}
1497
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001498static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001499{
1500 if (codec->ac97_registered) {
1501 soc_ac97_dev_unregister(codec);
1502 codec->ac97_registered = 0;
1503 }
1504}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001505
Benoit Cousson88bd8702014-07-08 23:19:34 +02001506static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1507{
1508 int i, ret;
1509
1510 for (i = 0; i < rtd->num_codecs; i++) {
1511 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1512
1513 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1514 if (ret) {
1515 while (--i >= 0)
1516 soc_unregister_ac97_codec(codec_dai->codec);
1517 return ret;
1518 }
1519 }
1520
1521 return 0;
1522}
1523
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001524static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1525{
Benoit Cousson88bd8702014-07-08 23:19:34 +02001526 int i;
1527
1528 for (i = 0; i < rtd->num_codecs; i++)
1529 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001530}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001531#endif
1532
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001533static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001534{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001535 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001536 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001537 const char *name = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001538
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001539 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1540 if (!rtd->component) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001541 if (aux_dev->codec_of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001542 name = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001543
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001544 dev_err(card->dev, "ASoC: %s not registered\n", name);
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001545 return -EPROBE_DEFER;
1546 }
1547
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001548 /*
1549 * Some places still reference rtd->codec, so we have to keep that
1550 * initialized if the component is a CODEC. Once all those references
1551 * have been removed, this code can be removed as well.
1552 */
1553 rtd->codec = rtd->component->codec;
1554
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001555 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001556}
1557
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001558static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1559{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001560 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001561 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001562 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001563
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001564 ret = soc_probe_component(card, rtd->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001565 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001566 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001567
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001568 /* do machine specific initialization */
1569 if (aux_dev->init) {
Lars-Peter Clausen57bf7722014-08-19 15:51:23 +02001570 ret = aux_dev->init(rtd->component);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001571 if (ret < 0) {
1572 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1573 aux_dev->name, ret);
1574 return ret;
1575 }
1576 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001577
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001578 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001579}
1580
1581static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1582{
1583 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001584 struct snd_soc_component *component = rtd->component;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001585
1586 /* unregister the rtd device */
1587 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001588 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001589 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001590 rtd->dev_registered = 0;
1591 }
1592
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001593 if (component && component->probed)
1594 soc_remove_component(component);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001595}
1596
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001597static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001598{
1599 int ret;
1600
1601 if (codec->cache_init)
1602 return 0;
1603
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001604 ret = snd_soc_cache_init(codec);
1605 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001606 dev_err(codec->dev,
1607 "ASoC: Failed to set cache compression type: %d\n",
1608 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001609 return ret;
1610 }
1611 codec->cache_init = 1;
1612 return 0;
1613}
1614
Mark Brownb19e6e72012-03-14 21:18:39 +00001615static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001616{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001617 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001618 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001619 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001620
Liam Girdwood01b9d992012-03-07 10:38:25 +00001621 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001622
Mark Brownb19e6e72012-03-14 21:18:39 +00001623 /* bind DAIs */
1624 for (i = 0; i < card->num_links; i++) {
1625 ret = soc_bind_dai_link(card, i);
1626 if (ret != 0)
1627 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001628 }
1629
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001630 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001631 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001632 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001633 if (ret != 0)
1634 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001635 }
1636
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001637 /* initialize the register cache for each available codec */
1638 list_for_each_entry(codec, &codec_list, list) {
1639 if (codec->cache_init)
1640 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001641 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001642 if (ret < 0)
1643 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001644 }
1645
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001646 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001647 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001648 card->owner, 0, &card->snd_card);
1649 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001650 dev_err(card->dev,
1651 "ASoC: can't create sound card for card %s: %d\n",
1652 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001653 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001654 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001655
Mark Browne37a4972011-03-02 18:21:57 +00001656 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1657 card->dapm.dev = card->dev;
1658 card->dapm.card = card;
1659 list_add(&card->dapm.list, &card->dapm_list);
1660
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001661#ifdef CONFIG_DEBUG_FS
1662 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1663#endif
1664
Mark Brown88ee1c62011-02-02 10:43:26 +00001665#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001666 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001667 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001668#endif
Andy Green6ed25972008-06-13 16:24:05 +01001669
Mark Brown9a841eb2011-04-12 17:51:37 -07001670 if (card->dapm_widgets)
1671 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1672 card->num_dapm_widgets);
1673
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001674 /* initialise the sound card only once */
1675 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001676 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001677 if (ret < 0)
1678 goto card_probe_error;
1679 }
1680
Stephen Warren62ae68f2012-06-08 12:34:23 -06001681 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001682 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1683 order++) {
1684 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001685 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001686 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001687 dev_err(card->dev,
1688 "ASoC: failed to instantiate card %d\n",
1689 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001690 goto probe_dai_err;
1691 }
1692 }
1693 }
1694
1695 /* probe all DAI links on this card */
1696 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1697 order++) {
1698 for (i = 0; i < card->num_links; i++) {
1699 ret = soc_probe_link_dais(card, i, order);
1700 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001701 dev_err(card->dev,
1702 "ASoC: failed to instantiate card %d\n",
1703 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001704 goto probe_dai_err;
1705 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001706 }
1707 }
1708
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001709 for (i = 0; i < card->num_aux_devs; i++) {
1710 ret = soc_probe_aux_dev(card, i);
1711 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001712 dev_err(card->dev,
1713 "ASoC: failed to add auxiliary devices %d\n",
1714 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001715 goto probe_aux_dev_err;
1716 }
1717 }
1718
Mark Brown888df392012-02-16 19:37:51 -08001719 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001720 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001721
Mark Brownb7af1da2011-04-07 19:18:44 +09001722 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001723 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001724
Mark Brownb8ad29d2011-03-02 18:35:51 +00001725 if (card->dapm_routes)
1726 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1727 card->num_dapm_routes);
1728
Mark Brown75d9ac42011-09-27 16:41:01 +01001729 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001730 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001731 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001732 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001733
Mark Brownf04209a2012-04-09 16:29:21 +01001734 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001735 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1736 int j;
1737
1738 for (j = 0; j < rtd->num_codecs; j++) {
1739 struct snd_soc_dai *codec_dai = codec_dais[j];
1740
1741 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1742 if (ret != 0 && ret != -ENOTSUPP)
1743 dev_warn(codec_dai->dev,
1744 "ASoC: Failed to set DAI format: %d\n",
1745 ret);
1746 }
Mark Brownf04209a2012-04-09 16:29:21 +01001747 }
1748
1749 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001750 if (dai_fmt &&
1751 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001752 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1753 dai_fmt);
1754 if (ret != 0 && ret != -ENOTSUPP)
1755 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001756 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001757 ret);
1758 } else if (dai_fmt) {
1759 /* Flip the polarity for the "CPU" end */
1760 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1761 switch (dai_link->dai_fmt &
1762 SND_SOC_DAIFMT_MASTER_MASK) {
1763 case SND_SOC_DAIFMT_CBM_CFM:
1764 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1765 break;
1766 case SND_SOC_DAIFMT_CBM_CFS:
1767 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1768 break;
1769 case SND_SOC_DAIFMT_CBS_CFM:
1770 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1771 break;
1772 case SND_SOC_DAIFMT_CBS_CFS:
1773 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1774 break;
1775 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001776
1777 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001778 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001779 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001780 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001781 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001782 ret);
1783 }
1784 }
1785
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001786 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001787 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001788 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1789 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001790 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1791 "%s", card->driver_name ? card->driver_name : card->name);
1792 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1793 switch (card->snd_card->driver[i]) {
1794 case '_':
1795 case '-':
1796 case '\0':
1797 break;
1798 default:
1799 if (!isalnum(card->snd_card->driver[i]))
1800 card->snd_card->driver[i] = '_';
1801 break;
1802 }
1803 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001804
Mark Brown28e9ad92011-03-02 18:36:34 +00001805 if (card->late_probe) {
1806 ret = card->late_probe(card);
1807 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001808 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001809 card->name, ret);
1810 goto probe_aux_dev_err;
1811 }
1812 }
1813
Stephen Warren16332812011-11-23 12:42:04 -07001814 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001815 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001816
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001817 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001818
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001819 ret = snd_card_register(card->snd_card);
1820 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001821 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1822 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001823 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001824 }
1825
1826#ifdef CONFIG_SND_SOC_AC97_BUS
1827 /* register any AC97 codecs */
1828 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001829 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1830 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001831 dev_err(card->dev,
1832 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001833 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001834 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001835 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001836 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001837 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001838#endif
1839
Mark Brown435c5e22008-12-04 15:32:53 +00001840 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001841 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001842 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001843
1844 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001845
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001846probe_aux_dev_err:
1847 for (i = 0; i < card->num_aux_devs; i++)
1848 soc_remove_aux_dev(card, i);
1849
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001850probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001851 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001852
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001853card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001854 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001855 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001856
1857 snd_card_free(card->snd_card);
1858
Mark Brownb19e6e72012-03-14 21:18:39 +00001859base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001860 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001861
Mark Brownb19e6e72012-03-14 21:18:39 +00001862 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001863}
1864
1865/* probes a new socdev */
1866static int soc_probe(struct platform_device *pdev)
1867{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001868 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001869
Vinod Koul70a7ca32011-01-14 19:22:48 +05301870 /*
1871 * no card, so machine driver should be registering card
1872 * we should not be here in that case so ret error
1873 */
1874 if (!card)
1875 return -EINVAL;
1876
Mark Brownfe4085e2012-03-02 13:07:41 +00001877 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001878 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001879 card->name);
1880
Mark Brown435c5e22008-12-04 15:32:53 +00001881 /* Bodge while we unpick instantiation */
1882 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001883
Mark Brown28d528c2012-08-09 18:45:23 +01001884 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001885}
1886
Vinod Koulb0e26482011-01-13 22:48:02 +05301887static int soc_cleanup_card_resources(struct snd_soc_card *card)
1888{
Vinod Koulb0e26482011-01-13 22:48:02 +05301889 int i;
1890
1891 /* make sure any delayed work runs */
1892 for (i = 0; i < card->num_rtd; i++) {
1893 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001894 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301895 }
1896
1897 /* remove auxiliary devices */
1898 for (i = 0; i < card->num_aux_devs; i++)
1899 soc_remove_aux_dev(card, i);
1900
1901 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001902 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301903
1904 soc_cleanup_card_debugfs(card);
1905
1906 /* remove the card */
1907 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001908 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301909
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001910 snd_soc_dapm_free(&card->dapm);
1911
Vinod Koulb0e26482011-01-13 22:48:02 +05301912 snd_card_free(card->snd_card);
1913 return 0;
1914
1915}
1916
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001917/* removes a socdev */
1918static int soc_remove(struct platform_device *pdev)
1919{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001920 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001921
Mark Brownc5af3a22008-11-28 13:29:45 +00001922 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001923 return 0;
1924}
1925
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001926int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001927{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001928 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001929 int i;
Mark Brown51737472009-06-22 13:16:51 +01001930
1931 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001932 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001933
1934 /* Flush out pmdown_time work - we actually do want to run it
1935 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001936 for (i = 0; i < card->num_rtd; i++) {
1937 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001938 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001939 }
Mark Brown51737472009-06-22 13:16:51 +01001940
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001941 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001942
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001943 /* deactivate pins to sleep state */
1944 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001945 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1946 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1947 int j;
1948
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001949 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001950 for (j = 0; j < rtd->num_codecs; j++) {
1951 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1952 pinctrl_pm_select_sleep_state(codec_dai->dev);
1953 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001954 }
1955
Mark Brown416356f2009-06-30 19:05:15 +01001956 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001957}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001958EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001959
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001960const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301961 .suspend = snd_soc_suspend,
1962 .resume = snd_soc_resume,
1963 .freeze = snd_soc_suspend,
1964 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001965 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301966 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001967};
Stephen Warrendeb26072011-04-05 19:35:30 -06001968EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001969
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001970/* ASoC platform driver */
1971static struct platform_driver soc_driver = {
1972 .driver = {
1973 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001974 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001975 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001976 },
1977 .probe = soc_probe,
1978 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001979};
1980
Mark Brown096e49d2009-07-05 15:12:22 +01001981/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001982 * snd_soc_new_ac97_codec - initailise AC97 device
1983 * @codec: audio codec
1984 * @ops: AC97 bus operations
1985 * @num: AC97 codec number
1986 *
1987 * Initialises AC97 codec resources for use by ad-hoc devices only.
1988 */
1989int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1990 struct snd_ac97_bus_ops *ops, int num)
1991{
1992 mutex_lock(&codec->mutex);
1993
1994 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1995 if (codec->ac97 == NULL) {
1996 mutex_unlock(&codec->mutex);
1997 return -ENOMEM;
1998 }
1999
2000 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2001 if (codec->ac97->bus == NULL) {
2002 kfree(codec->ac97);
2003 codec->ac97 = NULL;
2004 mutex_unlock(&codec->mutex);
2005 return -ENOMEM;
2006 }
2007
2008 codec->ac97->bus->ops = ops;
2009 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002010
2011 /*
2012 * Mark the AC97 device to be created by us. This way we ensure that the
2013 * device will be registered with the device subsystem later on.
2014 */
2015 codec->ac97_created = 1;
2016
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002017 mutex_unlock(&codec->mutex);
2018 return 0;
2019}
2020EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2021
Markus Pargmann741a5092013-08-19 17:05:55 +02002022static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2023
2024static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2025{
2026 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2027
2028 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2029
2030 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2031
2032 udelay(10);
2033
2034 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2035
2036 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2037 msleep(2);
2038}
2039
2040static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2041{
2042 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2043
2044 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2045
2046 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2047 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2048 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2049
2050 udelay(10);
2051
2052 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2053
2054 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2055 msleep(2);
2056}
2057
2058static int snd_soc_ac97_parse_pinctl(struct device *dev,
2059 struct snd_ac97_reset_cfg *cfg)
2060{
2061 struct pinctrl *p;
2062 struct pinctrl_state *state;
2063 int gpio;
2064 int ret;
2065
2066 p = devm_pinctrl_get(dev);
2067 if (IS_ERR(p)) {
2068 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002069 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002070 }
2071 cfg->pctl = p;
2072
2073 state = pinctrl_lookup_state(p, "ac97-reset");
2074 if (IS_ERR(state)) {
2075 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002076 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002077 }
2078 cfg->pstate_reset = state;
2079
2080 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2081 if (IS_ERR(state)) {
2082 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002083 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002084 }
2085 cfg->pstate_warm_reset = state;
2086
2087 state = pinctrl_lookup_state(p, "ac97-running");
2088 if (IS_ERR(state)) {
2089 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002090 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002091 }
2092 cfg->pstate_run = state;
2093
2094 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2095 if (gpio < 0) {
2096 dev_err(dev, "Can't find ac97-sync gpio\n");
2097 return gpio;
2098 }
2099 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2100 if (ret) {
2101 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2102 return ret;
2103 }
2104 cfg->gpio_sync = gpio;
2105
2106 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2107 if (gpio < 0) {
2108 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2109 return gpio;
2110 }
2111 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2112 if (ret) {
2113 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2114 return ret;
2115 }
2116 cfg->gpio_sdata = gpio;
2117
2118 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2119 if (gpio < 0) {
2120 dev_err(dev, "Can't find ac97-reset gpio\n");
2121 return gpio;
2122 }
2123 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2124 if (ret) {
2125 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2126 return ret;
2127 }
2128 cfg->gpio_reset = gpio;
2129
2130 return 0;
2131}
2132
Mark Brownb047e1c2013-06-26 12:45:59 +01002133struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002134EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002135
2136int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2137{
2138 if (ops == soc_ac97_ops)
2139 return 0;
2140
2141 if (soc_ac97_ops && ops)
2142 return -EBUSY;
2143
2144 soc_ac97_ops = ops;
2145
2146 return 0;
2147}
2148EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2149
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002150/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002151 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2152 *
2153 * This function sets the reset and warm_reset properties of ops and parses
2154 * the device node of pdev to get pinctrl states and gpio numbers to use.
2155 */
2156int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2157 struct platform_device *pdev)
2158{
2159 struct device *dev = &pdev->dev;
2160 struct snd_ac97_reset_cfg cfg;
2161 int ret;
2162
2163 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2164 if (ret)
2165 return ret;
2166
2167 ret = snd_soc_set_ac97_ops(ops);
2168 if (ret)
2169 return ret;
2170
2171 ops->warm_reset = snd_soc_ac97_warm_reset;
2172 ops->reset = snd_soc_ac97_reset;
2173
2174 snd_ac97_rst_cfg = cfg;
2175 return 0;
2176}
2177EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2178
2179/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002180 * snd_soc_free_ac97_codec - free AC97 codec device
2181 * @codec: audio codec
2182 *
2183 * Frees AC97 codec device resources.
2184 */
2185void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2186{
2187 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002188#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002189 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002190#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002191 kfree(codec->ac97->bus);
2192 kfree(codec->ac97);
2193 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002194 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002195 mutex_unlock(&codec->mutex);
2196}
2197EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2198
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002199/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002200 * snd_soc_cnew - create new control
2201 * @_template: control template
2202 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002203 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002204 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002205 *
2206 * Create a new mixer control from a template control.
2207 *
2208 * Returns 0 for success, else error.
2209 */
2210struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002211 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002212 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002213{
2214 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002215 struct snd_kcontrol *kcontrol;
2216 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002217
2218 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002219 template.index = 0;
2220
Mark Brownefb7ac32011-03-08 17:23:24 +00002221 if (!long_name)
2222 long_name = template.name;
2223
2224 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002225 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002226 if (!name)
2227 return NULL;
2228
Mark Brownefb7ac32011-03-08 17:23:24 +00002229 template.name = name;
2230 } else {
2231 template.name = long_name;
2232 }
2233
2234 kcontrol = snd_ctl_new1(&template, data);
2235
2236 kfree(name);
2237
2238 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002239}
2240EXPORT_SYMBOL_GPL(snd_soc_cnew);
2241
Liam Girdwood022658b2012-02-03 17:43:09 +00002242static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2243 const struct snd_kcontrol_new *controls, int num_controls,
2244 const char *prefix, void *data)
2245{
2246 int err, i;
2247
2248 for (i = 0; i < num_controls; i++) {
2249 const struct snd_kcontrol_new *control = &controls[i];
2250 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2251 control->name, prefix));
2252 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002253 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2254 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002255 return err;
2256 }
2257 }
2258
2259 return 0;
2260}
2261
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002262struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2263 const char *name)
2264{
2265 struct snd_card *card = soc_card->snd_card;
2266 struct snd_kcontrol *kctl;
2267
2268 if (unlikely(!name))
2269 return NULL;
2270
2271 list_for_each_entry(kctl, &card->controls, list)
2272 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2273 return kctl;
2274 return NULL;
2275}
2276EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2277
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002278/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002279 * snd_soc_add_component_controls - Add an array of controls to a component.
2280 *
2281 * @component: Component to add controls to
2282 * @controls: Array of controls to add
2283 * @num_controls: Number of elements in the array
2284 *
2285 * Return: 0 for success, else error.
2286 */
2287int snd_soc_add_component_controls(struct snd_soc_component *component,
2288 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2289{
2290 struct snd_card *card = component->card->snd_card;
2291
2292 return snd_soc_add_controls(card, component->dev, controls,
2293 num_controls, component->name_prefix, component);
2294}
2295EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2296
2297/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002298 * snd_soc_add_codec_controls - add an array of controls to a codec.
2299 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002300 * duplicating this code.
2301 *
2302 * @codec: codec to add controls to
2303 * @controls: array of controls to add
2304 * @num_controls: number of elements in the array
2305 *
2306 * Return 0 for success, else error.
2307 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002308int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002309 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002310{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002311 return snd_soc_add_component_controls(&codec->component, controls,
2312 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002313}
Liam Girdwood022658b2012-02-03 17:43:09 +00002314EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002315
2316/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002317 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002318 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002319 *
2320 * @platform: platform to add controls to
2321 * @controls: array of controls to add
2322 * @num_controls: number of elements in the array
2323 *
2324 * Return 0 for success, else error.
2325 */
2326int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002327 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002328{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002329 return snd_soc_add_component_controls(&platform->component, controls,
2330 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002331}
2332EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2333
2334/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002335 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2336 * Convenience function to add a list of controls.
2337 *
2338 * @soc_card: SoC card to add controls to
2339 * @controls: array of controls to add
2340 * @num_controls: number of elements in the array
2341 *
2342 * Return 0 for success, else error.
2343 */
2344int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2345 const struct snd_kcontrol_new *controls, int num_controls)
2346{
2347 struct snd_card *card = soc_card->snd_card;
2348
2349 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2350 NULL, soc_card);
2351}
2352EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2353
2354/**
2355 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2356 * Convienience function to add a list of controls.
2357 *
2358 * @dai: DAI to add controls to
2359 * @controls: array of controls to add
2360 * @num_controls: number of elements in the array
2361 *
2362 * Return 0 for success, else error.
2363 */
2364int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2365 const struct snd_kcontrol_new *controls, int num_controls)
2366{
2367 struct snd_card *card = dai->card->snd_card;
2368
2369 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2370 NULL, dai);
2371}
2372EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2373
2374/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002375 * snd_soc_info_enum_double - enumerated double mixer info callback
2376 * @kcontrol: mixer control
2377 * @uinfo: control element information
2378 *
2379 * Callback to provide information about a double enumerated
2380 * mixer control.
2381 *
2382 * Returns 0 for success.
2383 */
2384int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2385 struct snd_ctl_elem_info *uinfo)
2386{
2387 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2388
2389 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2390 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002391 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002392
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002393 if (uinfo->value.enumerated.item >= e->items)
2394 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002395 strlcpy(uinfo->value.enumerated.name,
2396 e->texts[uinfo->value.enumerated.item],
2397 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002398 return 0;
2399}
2400EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2401
2402/**
2403 * snd_soc_get_enum_double - enumerated double mixer get callback
2404 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002405 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002406 *
2407 * Callback to get the value of a double enumerated mixer.
2408 *
2409 * Returns 0 for success.
2410 */
2411int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2412 struct snd_ctl_elem_value *ucontrol)
2413{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002414 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002415 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002416 unsigned int val, item;
2417 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002418 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002419
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002420 ret = snd_soc_component_read(component, e->reg, &reg_val);
2421 if (ret)
2422 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002423 val = (reg_val >> e->shift_l) & e->mask;
2424 item = snd_soc_enum_val_to_item(e, val);
2425 ucontrol->value.enumerated.item[0] = item;
2426 if (e->shift_l != e->shift_r) {
2427 val = (reg_val >> e->shift_l) & e->mask;
2428 item = snd_soc_enum_val_to_item(e, val);
2429 ucontrol->value.enumerated.item[1] = item;
2430 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002431
2432 return 0;
2433}
2434EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2435
2436/**
2437 * snd_soc_put_enum_double - enumerated double mixer put callback
2438 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002439 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002440 *
2441 * Callback to set the value of a double enumerated mixer.
2442 *
2443 * Returns 0 for success.
2444 */
2445int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2446 struct snd_ctl_elem_value *ucontrol)
2447{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002448 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002449 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002450 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002451 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002452 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002453
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002454 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002455 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002456 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002457 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002458 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002459 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002460 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002461 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002462 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002463 }
2464
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002465 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002466}
2467EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2468
2469/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002470 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002471 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002472 * @reg: Register to read
2473 * @mask: Mask to use after shifting the register value
2474 * @shift: Right shift of register value
2475 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002476 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002477 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002478 * This functions reads a codec register. The register value is shifted right
2479 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2480 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002481 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002482 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002483 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002484static int snd_soc_read_signed(struct snd_soc_component *component,
2485 unsigned int reg, unsigned int mask, unsigned int shift,
2486 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002487{
Markus Pargmannf227b882014-01-16 16:02:10 +01002488 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002489 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002490
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002491 ret = snd_soc_component_read(component, reg, &val);
2492 if (ret < 0)
2493 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002494
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002495 val = (val >> shift) & mask;
2496
2497 if (!sign_bit) {
2498 *signed_val = val;
2499 return 0;
2500 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002501
2502 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002503 if (!(val & BIT(sign_bit))) {
2504 *signed_val = val;
2505 return 0;
2506 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002507
2508 ret = val;
2509
2510 /*
2511 * The register most probably does not contain a full-sized int.
2512 * Instead we have an arbitrary number of bits in a signed
2513 * representation which has to be translated into a full-sized int.
2514 * This is done by filling up all bits above the sign-bit.
2515 */
2516 ret |= ~((int)(BIT(sign_bit) - 1));
2517
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002518 *signed_val = ret;
2519
2520 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002521}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002522
2523/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002524 * snd_soc_info_volsw - single mixer info callback
2525 * @kcontrol: mixer control
2526 * @uinfo: control element information
2527 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002528 * Callback to provide information about a single mixer control, or a double
2529 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002530 *
2531 * Returns 0 for success.
2532 */
2533int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2534 struct snd_ctl_elem_info *uinfo)
2535{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002536 struct soc_mixer_control *mc =
2537 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002538 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002539
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002540 if (!mc->platform_max)
2541 mc->platform_max = mc->max;
2542 platform_max = mc->platform_max;
2543
2544 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002545 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2546 else
2547 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2548
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002549 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002550 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002551 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002552 return 0;
2553}
2554EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2555
2556/**
2557 * snd_soc_get_volsw - single mixer get callback
2558 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002559 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002560 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002561 * Callback to get the value of a single mixer control, or a double mixer
2562 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002563 *
2564 * Returns 0 for success.
2565 */
2566int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2567 struct snd_ctl_elem_value *ucontrol)
2568{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002569 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002570 struct soc_mixer_control *mc =
2571 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002572 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002573 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002574 unsigned int shift = mc->shift;
2575 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002576 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002577 int min = mc->min;
2578 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002579 unsigned int mask = (1 << fls(max)) - 1;
2580 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002581 int val;
2582 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002583
Markus Pargmannf227b882014-01-16 16:02:10 +01002584 if (sign_bit)
2585 mask = BIT(sign_bit + 1) - 1;
2586
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002587 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2588 if (ret)
2589 return ret;
2590
2591 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002592 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002593 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002594 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002595
2596 if (snd_soc_volsw_is_stereo(mc)) {
2597 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002598 ret = snd_soc_read_signed(component, reg, mask, rshift,
2599 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002600 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002601 ret = snd_soc_read_signed(component, reg2, mask, shift,
2602 sign_bit, &val);
2603 if (ret)
2604 return ret;
2605
2606 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002607 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002608 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002609 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002610 }
2611
2612 return 0;
2613}
2614EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2615
2616/**
2617 * snd_soc_put_volsw - single mixer put callback
2618 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002619 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002620 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002621 * Callback to set the value of a single mixer control, or a double mixer
2622 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002623 *
2624 * Returns 0 for success.
2625 */
2626int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2627 struct snd_ctl_elem_value *ucontrol)
2628{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002629 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002630 struct soc_mixer_control *mc =
2631 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002632 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002633 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002634 unsigned int shift = mc->shift;
2635 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002636 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002637 int min = mc->min;
2638 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002639 unsigned int mask = (1 << fls(max)) - 1;
2640 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002641 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002642 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002643 unsigned int val2 = 0;
2644 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002645
Markus Pargmannf227b882014-01-16 16:02:10 +01002646 if (sign_bit)
2647 mask = BIT(sign_bit + 1) - 1;
2648
2649 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002650 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002651 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002652 val_mask = mask << shift;
2653 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002654 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002655 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002656 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002657 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002658 if (reg == reg2) {
2659 val_mask |= mask << rshift;
2660 val |= val2 << rshift;
2661 } else {
2662 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002663 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002664 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002665 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002666 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002667 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002668 return err;
2669
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002670 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002671 err = snd_soc_component_update_bits(component, reg2, val_mask,
2672 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002673
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002674 return err;
2675}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002676EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002677
Mark Browne13ac2e2008-05-28 17:58:05 +01002678/**
Brian Austin1d99f242012-03-30 10:43:55 -05002679 * snd_soc_get_volsw_sx - single mixer get callback
2680 * @kcontrol: mixer control
2681 * @ucontrol: control element information
2682 *
2683 * Callback to get the value of a single mixer control, or a double mixer
2684 * control that spans 2 registers.
2685 *
2686 * Returns 0 for success.
2687 */
2688int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2689 struct snd_ctl_elem_value *ucontrol)
2690{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002691 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002692 struct soc_mixer_control *mc =
2693 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002694 unsigned int reg = mc->reg;
2695 unsigned int reg2 = mc->rreg;
2696 unsigned int shift = mc->shift;
2697 unsigned int rshift = mc->rshift;
2698 int max = mc->max;
2699 int min = mc->min;
2700 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002701 unsigned int val;
2702 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002703
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002704 ret = snd_soc_component_read(component, reg, &val);
2705 if (ret < 0)
2706 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002707
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002708 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2709
2710 if (snd_soc_volsw_is_stereo(mc)) {
2711 ret = snd_soc_component_read(component, reg2, &val);
2712 if (ret < 0)
2713 return ret;
2714
2715 val = ((val >> rshift) - min) & mask;
2716 ucontrol->value.integer.value[1] = val;
2717 }
Brian Austin1d99f242012-03-30 10:43:55 -05002718
2719 return 0;
2720}
2721EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2722
2723/**
2724 * snd_soc_put_volsw_sx - double mixer set callback
2725 * @kcontrol: mixer control
2726 * @uinfo: control element information
2727 *
2728 * Callback to set the value of a double mixer control that spans 2 registers.
2729 *
2730 * Returns 0 for success.
2731 */
2732int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2733 struct snd_ctl_elem_value *ucontrol)
2734{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002735 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002736 struct soc_mixer_control *mc =
2737 (struct soc_mixer_control *)kcontrol->private_value;
2738
2739 unsigned int reg = mc->reg;
2740 unsigned int reg2 = mc->rreg;
2741 unsigned int shift = mc->shift;
2742 unsigned int rshift = mc->rshift;
2743 int max = mc->max;
2744 int min = mc->min;
2745 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002746 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002747 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002748
2749 val_mask = mask << shift;
2750 val = (ucontrol->value.integer.value[0] + min) & mask;
2751 val = val << shift;
2752
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002753 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302754 if (err < 0)
2755 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002756
2757 if (snd_soc_volsw_is_stereo(mc)) {
2758 val_mask = mask << rshift;
2759 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2760 val2 = val2 << rshift;
2761
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002762 err = snd_soc_component_update_bits(component, reg2, val_mask,
2763 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002764 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002765 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002766}
2767EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2768
2769/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002770 * snd_soc_info_volsw_s8 - signed mixer info callback
2771 * @kcontrol: mixer control
2772 * @uinfo: control element information
2773 *
2774 * Callback to provide information about a signed mixer control.
2775 *
2776 * Returns 0 for success.
2777 */
2778int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2779 struct snd_ctl_elem_info *uinfo)
2780{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002781 struct soc_mixer_control *mc =
2782 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002783 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002784 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002785
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002786 if (!mc->platform_max)
2787 mc->platform_max = mc->max;
2788 platform_max = mc->platform_max;
2789
Mark Browne13ac2e2008-05-28 17:58:05 +01002790 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2791 uinfo->count = 2;
2792 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002793 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002794 return 0;
2795}
2796EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2797
2798/**
2799 * snd_soc_get_volsw_s8 - signed mixer get callback
2800 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002801 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002802 *
2803 * Callback to get the value of a signed mixer control.
2804 *
2805 * Returns 0 for success.
2806 */
2807int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2808 struct snd_ctl_elem_value *ucontrol)
2809{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002810 struct soc_mixer_control *mc =
2811 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002812 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002813 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002814 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002815 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002816 int ret;
2817
2818 ret = snd_soc_component_read(component, reg, &val);
2819 if (ret)
2820 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002821
2822 ucontrol->value.integer.value[0] =
2823 ((signed char)(val & 0xff))-min;
2824 ucontrol->value.integer.value[1] =
2825 ((signed char)((val >> 8) & 0xff))-min;
2826 return 0;
2827}
2828EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2829
2830/**
2831 * snd_soc_put_volsw_sgn - signed mixer put callback
2832 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002833 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002834 *
2835 * Callback to set the value of a signed mixer control.
2836 *
2837 * Returns 0 for success.
2838 */
2839int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2840 struct snd_ctl_elem_value *ucontrol)
2841{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002842 struct soc_mixer_control *mc =
2843 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002844 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002845 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002846 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002847 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002848
2849 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2850 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2851
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002852 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002853}
2854EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2855
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002856/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002857 * snd_soc_info_volsw_range - single mixer info callback with range.
2858 * @kcontrol: mixer control
2859 * @uinfo: control element information
2860 *
2861 * Callback to provide information, within a range, about a single
2862 * mixer control.
2863 *
2864 * returns 0 for success.
2865 */
2866int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2867 struct snd_ctl_elem_info *uinfo)
2868{
2869 struct soc_mixer_control *mc =
2870 (struct soc_mixer_control *)kcontrol->private_value;
2871 int platform_max;
2872 int min = mc->min;
2873
2874 if (!mc->platform_max)
2875 mc->platform_max = mc->max;
2876 platform_max = mc->platform_max;
2877
2878 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002879 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002880 uinfo->value.integer.min = 0;
2881 uinfo->value.integer.max = platform_max - min;
2882
2883 return 0;
2884}
2885EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2886
2887/**
2888 * snd_soc_put_volsw_range - single mixer put value callback with range.
2889 * @kcontrol: mixer control
2890 * @ucontrol: control element information
2891 *
2892 * Callback to set the value, within a range, for a single mixer control.
2893 *
2894 * Returns 0 for success.
2895 */
2896int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2897 struct snd_ctl_elem_value *ucontrol)
2898{
2899 struct soc_mixer_control *mc =
2900 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002901 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002902 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002903 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002904 unsigned int shift = mc->shift;
2905 int min = mc->min;
2906 int max = mc->max;
2907 unsigned int mask = (1 << fls(max)) - 1;
2908 unsigned int invert = mc->invert;
2909 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002910 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002911
2912 val = ((ucontrol->value.integer.value[0] + min) & mask);
2913 if (invert)
2914 val = max - val;
2915 val_mask = mask << shift;
2916 val = val << shift;
2917
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002918 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002919 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002920 return ret;
2921
2922 if (snd_soc_volsw_is_stereo(mc)) {
2923 val = ((ucontrol->value.integer.value[1] + min) & mask);
2924 if (invert)
2925 val = max - val;
2926 val_mask = mask << shift;
2927 val = val << shift;
2928
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002929 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2930 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00002931 }
2932
2933 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002934}
2935EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2936
2937/**
2938 * snd_soc_get_volsw_range - single mixer get callback with range
2939 * @kcontrol: mixer control
2940 * @ucontrol: control element information
2941 *
2942 * Callback to get the value, within a range, of a single mixer control.
2943 *
2944 * Returns 0 for success.
2945 */
2946int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2947 struct snd_ctl_elem_value *ucontrol)
2948{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002949 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002950 struct soc_mixer_control *mc =
2951 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002952 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002953 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002954 unsigned int shift = mc->shift;
2955 int min = mc->min;
2956 int max = mc->max;
2957 unsigned int mask = (1 << fls(max)) - 1;
2958 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002959 unsigned int val;
2960 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002961
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002962 ret = snd_soc_component_read(component, reg, &val);
2963 if (ret)
2964 return ret;
2965
2966 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002967 if (invert)
2968 ucontrol->value.integer.value[0] =
2969 max - ucontrol->value.integer.value[0];
2970 ucontrol->value.integer.value[0] =
2971 ucontrol->value.integer.value[0] - min;
2972
Mark Brown9bde4f02012-12-19 16:05:00 +00002973 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002974 ret = snd_soc_component_read(component, rreg, &val);
2975 if (ret)
2976 return ret;
2977
2978 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002979 if (invert)
2980 ucontrol->value.integer.value[1] =
2981 max - ucontrol->value.integer.value[1];
2982 ucontrol->value.integer.value[1] =
2983 ucontrol->value.integer.value[1] - min;
2984 }
2985
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002986 return 0;
2987}
2988EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2989
2990/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002991 * snd_soc_limit_volume - Set new limit to an existing volume control.
2992 *
2993 * @codec: where to look for the control
2994 * @name: Name of the control
2995 * @max: new maximum limit
2996 *
2997 * Return 0 for success, else error.
2998 */
2999int snd_soc_limit_volume(struct snd_soc_codec *codec,
3000 const char *name, int max)
3001{
Lars-Peter Clausen00200102014-07-17 22:01:07 +02003002 struct snd_card *card = codec->component.card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003003 struct snd_kcontrol *kctl;
3004 struct soc_mixer_control *mc;
3005 int found = 0;
3006 int ret = -EINVAL;
3007
3008 /* Sanity check for name and max */
3009 if (unlikely(!name || max <= 0))
3010 return -EINVAL;
3011
3012 list_for_each_entry(kctl, &card->controls, list) {
3013 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3014 found = 1;
3015 break;
3016 }
3017 }
3018 if (found) {
3019 mc = (struct soc_mixer_control *)kctl->private_value;
3020 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003021 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003022 ret = 0;
3023 }
3024 }
3025 return ret;
3026}
3027EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3028
Mark Brown71d08512011-10-10 18:31:26 +01003029int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3030 struct snd_ctl_elem_info *uinfo)
3031{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003032 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003033 struct soc_bytes *params = (void *)kcontrol->private_value;
3034
3035 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003036 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003037
3038 return 0;
3039}
3040EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3041
3042int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3043 struct snd_ctl_elem_value *ucontrol)
3044{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003045 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003046 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003047 int ret;
3048
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003049 if (component->regmap)
3050 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003051 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003052 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003053 else
3054 ret = -EINVAL;
3055
Mark Brownf831b052012-02-17 16:20:33 -08003056 /* Hide any masked bytes to ensure consistent data reporting */
3057 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003058 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003059 case 1:
3060 ucontrol->value.bytes.data[0] &= ~params->mask;
3061 break;
3062 case 2:
3063 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003064 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003065 break;
3066 case 4:
3067 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003068 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003069 break;
3070 default:
3071 return -EINVAL;
3072 }
3073 }
3074
Mark Brown71d08512011-10-10 18:31:26 +01003075 return ret;
3076}
3077EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3078
3079int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3080 struct snd_ctl_elem_value *ucontrol)
3081{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003082 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003083 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003084 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003085 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003086 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003087
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003088 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003089 return -EINVAL;
3090
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003091 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003092
Mark Brownb5a8fe42013-01-20 21:42:22 +09003093 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3094 if (!data)
3095 return -ENOMEM;
3096
Mark Brownf831b052012-02-17 16:20:33 -08003097 /*
3098 * If we've got a mask then we need to preserve the register
3099 * bits. We shouldn't modify the incoming data so take a
3100 * copy.
3101 */
3102 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003103 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003104 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003105 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003106
3107 val &= params->mask;
3108
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003109 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003110 case 1:
3111 ((u8 *)data)[0] &= ~params->mask;
3112 ((u8 *)data)[0] |= val;
3113 break;
3114 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003115 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003116 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003117 &mask, &mask);
3118 if (ret != 0)
3119 goto out;
3120
3121 ((u16 *)data)[0] &= mask;
3122
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003123 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003124 &val, &val);
3125 if (ret != 0)
3126 goto out;
3127
3128 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003129 break;
3130 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003131 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003132 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003133 &mask, &mask);
3134 if (ret != 0)
3135 goto out;
3136
3137 ((u32 *)data)[0] &= mask;
3138
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003139 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003140 &val, &val);
3141 if (ret != 0)
3142 goto out;
3143
3144 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003145 break;
3146 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003147 ret = -EINVAL;
3148 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003149 }
3150 }
3151
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003152 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003153 data, len);
3154
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003155out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003156 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003157
3158 return ret;
3159}
3160EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3161
Vinod Kould9881202014-05-02 10:58:11 +05303162int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3163 struct snd_ctl_elem_info *ucontrol)
3164{
3165 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3166
3167 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3168 ucontrol->count = params->max;
3169
3170 return 0;
3171}
3172EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3173
Omair Mohammed Abdullah7523a272014-07-15 21:34:48 +05303174int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3175 unsigned int size, unsigned int __user *tlv)
3176{
3177 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3178 unsigned int count = size < params->max ? size : params->max;
3179 int ret = -ENXIO;
3180
3181 switch (op_flag) {
3182 case SNDRV_CTL_TLV_OP_READ:
3183 if (params->get)
3184 ret = params->get(tlv, count);
3185 break;
3186 case SNDRV_CTL_TLV_OP_WRITE:
3187 if (params->put)
3188 ret = params->put(tlv, count);
3189 break;
3190 }
3191 return ret;
3192}
3193EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3194
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003195/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003196 * snd_soc_info_xr_sx - signed multi register info callback
3197 * @kcontrol: mreg control
3198 * @uinfo: control element information
3199 *
3200 * Callback to provide information of a control that can
3201 * span multiple codec registers which together
3202 * forms a single signed value in a MSB/LSB manner.
3203 *
3204 * Returns 0 for success.
3205 */
3206int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3207 struct snd_ctl_elem_info *uinfo)
3208{
3209 struct soc_mreg_control *mc =
3210 (struct soc_mreg_control *)kcontrol->private_value;
3211 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3212 uinfo->count = 1;
3213 uinfo->value.integer.min = mc->min;
3214 uinfo->value.integer.max = mc->max;
3215
3216 return 0;
3217}
3218EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3219
3220/**
3221 * snd_soc_get_xr_sx - signed multi register get callback
3222 * @kcontrol: mreg control
3223 * @ucontrol: control element information
3224 *
3225 * Callback to get the value of a control that can span
3226 * multiple codec registers which together forms a single
3227 * signed value in a MSB/LSB manner. The control supports
3228 * specifying total no of bits used to allow for bitfields
3229 * across the multiple codec registers.
3230 *
3231 * Returns 0 for success.
3232 */
3233int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3234 struct snd_ctl_elem_value *ucontrol)
3235{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003236 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003237 struct soc_mreg_control *mc =
3238 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003239 unsigned int regbase = mc->regbase;
3240 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003241 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003242 unsigned int regwmask = (1<<regwshift)-1;
3243 unsigned int invert = mc->invert;
3244 unsigned long mask = (1UL<<mc->nbits)-1;
3245 long min = mc->min;
3246 long max = mc->max;
3247 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003248 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003249 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003250 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003251
3252 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003253 ret = snd_soc_component_read(component, regbase+i, &regval);
3254 if (ret)
3255 return ret;
3256 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003257 }
3258 val &= mask;
3259 if (min < 0 && val > max)
3260 val |= ~mask;
3261 if (invert)
3262 val = max - val;
3263 ucontrol->value.integer.value[0] = val;
3264
3265 return 0;
3266}
3267EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3268
3269/**
3270 * snd_soc_put_xr_sx - signed multi register get callback
3271 * @kcontrol: mreg control
3272 * @ucontrol: control element information
3273 *
3274 * Callback to set the value of a control that can span
3275 * multiple codec registers which together forms a single
3276 * signed value in a MSB/LSB manner. The control supports
3277 * specifying total no of bits used to allow for bitfields
3278 * across the multiple codec registers.
3279 *
3280 * Returns 0 for success.
3281 */
3282int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3283 struct snd_ctl_elem_value *ucontrol)
3284{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003285 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003286 struct soc_mreg_control *mc =
3287 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003288 unsigned int regbase = mc->regbase;
3289 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003290 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003291 unsigned int regwmask = (1<<regwshift)-1;
3292 unsigned int invert = mc->invert;
3293 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003294 long max = mc->max;
3295 long val = ucontrol->value.integer.value[0];
3296 unsigned int i, regval, regmask;
3297 int err;
3298
3299 if (invert)
3300 val = max - val;
3301 val &= mask;
3302 for (i = 0; i < regcount; i++) {
3303 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3304 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003305 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003306 regmask, regval);
3307 if (err < 0)
3308 return err;
3309 }
3310
3311 return 0;
3312}
3313EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3314
3315/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003316 * snd_soc_get_strobe - strobe get callback
3317 * @kcontrol: mixer control
3318 * @ucontrol: control element information
3319 *
3320 * Callback get the value of a strobe mixer control.
3321 *
3322 * Returns 0 for success.
3323 */
3324int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3325 struct snd_ctl_elem_value *ucontrol)
3326{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003327 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003328 struct soc_mixer_control *mc =
3329 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003330 unsigned int reg = mc->reg;
3331 unsigned int shift = mc->shift;
3332 unsigned int mask = 1 << shift;
3333 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003334 unsigned int val;
3335 int ret;
3336
3337 ret = snd_soc_component_read(component, reg, &val);
3338 if (ret)
3339 return ret;
3340
3341 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003342
3343 if (shift != 0 && val != 0)
3344 val = val >> shift;
3345 ucontrol->value.enumerated.item[0] = val ^ invert;
3346
3347 return 0;
3348}
3349EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3350
3351/**
3352 * snd_soc_put_strobe - strobe put callback
3353 * @kcontrol: mixer control
3354 * @ucontrol: control element information
3355 *
3356 * Callback strobe a register bit to high then low (or the inverse)
3357 * in one pass of a single mixer enum control.
3358 *
3359 * Returns 1 for success.
3360 */
3361int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3362 struct snd_ctl_elem_value *ucontrol)
3363{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003364 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003365 struct soc_mixer_control *mc =
3366 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003367 unsigned int reg = mc->reg;
3368 unsigned int shift = mc->shift;
3369 unsigned int mask = 1 << shift;
3370 unsigned int invert = mc->invert != 0;
3371 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3372 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3373 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3374 int err;
3375
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003376 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003377 if (err < 0)
3378 return err;
3379
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003380 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003381}
3382EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3383
3384/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003385 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3386 * @dai: DAI
3387 * @clk_id: DAI specific clock ID
3388 * @freq: new clock frequency in Hz
3389 * @dir: new clock direction - input/output.
3390 *
3391 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3392 */
3393int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3394 unsigned int freq, int dir)
3395{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003396 if (dai->driver && dai->driver->ops->set_sysclk)
3397 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003398 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003399 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003400 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003401 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003402 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003403}
3404EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3405
3406/**
Mark Brownec4ee522011-03-07 20:58:11 +00003407 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3408 * @codec: CODEC
3409 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003410 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003411 * @freq: new clock frequency in Hz
3412 * @dir: new clock direction - input/output.
3413 *
3414 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3415 */
3416int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003417 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003418{
3419 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003420 return codec->driver->set_sysclk(codec, clk_id, source,
3421 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003422 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003423 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003424}
3425EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3426
3427/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003428 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3429 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003430 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003431 * @div: new clock divisor.
3432 *
3433 * Configures the clock dividers. This is used to derive the best DAI bit and
3434 * frame clocks from the system or master clock. It's best to set the DAI bit
3435 * and frame clocks as low as possible to save system power.
3436 */
3437int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3438 int div_id, int div)
3439{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003440 if (dai->driver && dai->driver->ops->set_clkdiv)
3441 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003442 else
3443 return -EINVAL;
3444}
3445EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3446
3447/**
3448 * snd_soc_dai_set_pll - configure DAI PLL.
3449 * @dai: DAI
3450 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003451 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003452 * @freq_in: PLL input clock frequency in Hz
3453 * @freq_out: requested PLL output clock frequency in Hz
3454 *
3455 * Configures and enables PLL to generate output clock based on input clock.
3456 */
Mark Brown85488032009-09-05 18:52:16 +01003457int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3458 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003459{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003460 if (dai->driver && dai->driver->ops->set_pll)
3461 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003462 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003463 else if (dai->codec && dai->codec->driver->set_pll)
3464 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3465 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003466 else
3467 return -EINVAL;
3468}
3469EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3470
Mark Brownec4ee522011-03-07 20:58:11 +00003471/*
3472 * snd_soc_codec_set_pll - configure codec PLL.
3473 * @codec: CODEC
3474 * @pll_id: DAI specific PLL ID
3475 * @source: DAI specific source for the PLL
3476 * @freq_in: PLL input clock frequency in Hz
3477 * @freq_out: requested PLL output clock frequency in Hz
3478 *
3479 * Configures and enables PLL to generate output clock based on input clock.
3480 */
3481int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3482 unsigned int freq_in, unsigned int freq_out)
3483{
3484 if (codec->driver->set_pll)
3485 return codec->driver->set_pll(codec, pll_id, source,
3486 freq_in, freq_out);
3487 else
3488 return -EINVAL;
3489}
3490EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3491
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003492/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003493 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3494 * @dai: DAI
3495 * @ratio Ratio of BCLK to Sample rate.
3496 *
3497 * Configures the DAI for a preset BCLK to sample rate ratio.
3498 */
3499int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3500{
3501 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3502 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3503 else
3504 return -EINVAL;
3505}
3506EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3507
3508/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003509 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3510 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003511 * @fmt: SND_SOC_DAIFMT_ format value.
3512 *
3513 * Configures the DAI hardware format and clocking.
3514 */
3515int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3516{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003517 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003518 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003519 if (dai->driver->ops->set_fmt == NULL)
3520 return -ENOTSUPP;
3521 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003522}
3523EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3524
3525/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003526 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003527 * @slots: Number of slots in use.
3528 * @tx_mask: bitmask representing active TX slots.
3529 * @rx_mask: bitmask representing active RX slots.
3530 *
3531 * Generates the TDM tx and rx slot default masks for DAI.
3532 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003533static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003534 unsigned int *tx_mask,
3535 unsigned int *rx_mask)
3536{
3537 if (*tx_mask || *rx_mask)
3538 return 0;
3539
3540 if (!slots)
3541 return -EINVAL;
3542
3543 *tx_mask = (1 << slots) - 1;
3544 *rx_mask = (1 << slots) - 1;
3545
3546 return 0;
3547}
3548
3549/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003550 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3551 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003552 * @tx_mask: bitmask representing active TX slots.
3553 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003554 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003555 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003556 *
3557 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3558 * specific.
3559 */
3560int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003561 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003562{
Xiubo Lie5c21512014-03-21 14:17:12 +08003563 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3564 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003565 &tx_mask, &rx_mask);
3566 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003567 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003568
Benoit Cousson88bd8702014-07-08 23:19:34 +02003569 dai->tx_mask = tx_mask;
3570 dai->rx_mask = rx_mask;
3571
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003572 if (dai->driver && dai->driver->ops->set_tdm_slot)
3573 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003574 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003575 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003576 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003577}
3578EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3579
3580/**
Barry Song472df3c2009-09-12 01:16:29 +08003581 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3582 * @dai: DAI
3583 * @tx_num: how many TX channels
3584 * @tx_slot: pointer to an array which imply the TX slot number channel
3585 * 0~num-1 uses
3586 * @rx_num: how many RX channels
3587 * @rx_slot: pointer to an array which imply the RX slot number channel
3588 * 0~num-1 uses
3589 *
3590 * configure the relationship between channel number and TDM slot number.
3591 */
3592int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3593 unsigned int tx_num, unsigned int *tx_slot,
3594 unsigned int rx_num, unsigned int *rx_slot)
3595{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003596 if (dai->driver && dai->driver->ops->set_channel_map)
3597 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003598 rx_num, rx_slot);
3599 else
3600 return -EINVAL;
3601}
3602EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3603
3604/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003605 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3606 * @dai: DAI
3607 * @tristate: tristate enable
3608 *
3609 * Tristates the DAI so that others can use it.
3610 */
3611int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3612{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003613 if (dai->driver && dai->driver->ops->set_tristate)
3614 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003615 else
3616 return -EINVAL;
3617}
3618EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3619
3620/**
3621 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3622 * @dai: DAI
3623 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003624 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003625 *
3626 * Mutes the DAI DAC.
3627 */
Mark Brownda183962013-02-06 15:44:07 +00003628int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3629 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003630{
Mark Brownda183962013-02-06 15:44:07 +00003631 if (!dai->driver)
3632 return -ENOTSUPP;
3633
3634 if (dai->driver->ops->mute_stream)
3635 return dai->driver->ops->mute_stream(dai, mute, direction);
3636 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3637 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003638 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003639 else
Mark Brown04570c62012-04-13 19:16:03 +01003640 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003641}
3642EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3643
Benoit Cousson88bd8702014-07-08 23:19:34 +02003644static int snd_soc_init_multicodec(struct snd_soc_card *card,
3645 struct snd_soc_dai_link *dai_link)
3646{
3647 /* Legacy codec/codec_dai link is a single entry in multicodec */
3648 if (dai_link->codec_name || dai_link->codec_of_node ||
3649 dai_link->codec_dai_name) {
3650 dai_link->num_codecs = 1;
3651
3652 dai_link->codecs = devm_kzalloc(card->dev,
3653 sizeof(struct snd_soc_dai_link_component),
3654 GFP_KERNEL);
3655 if (!dai_link->codecs)
3656 return -ENOMEM;
3657
3658 dai_link->codecs[0].name = dai_link->codec_name;
3659 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3660 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3661 }
3662
3663 if (!dai_link->codecs) {
3664 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3665 return -EINVAL;
3666 }
3667
3668 return 0;
3669}
3670
Mark Brownc5af3a22008-11-28 13:29:45 +00003671/**
3672 * snd_soc_register_card - Register a card with the ASoC core
3673 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003674 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003675 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003676 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303677int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003678{
Benoit Cousson88bd8702014-07-08 23:19:34 +02003679 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003680
Mark Brownc5af3a22008-11-28 13:29:45 +00003681 if (!card->name || !card->dev)
3682 return -EINVAL;
3683
Stephen Warren5a504962011-12-21 10:40:59 -07003684 for (i = 0; i < card->num_links; i++) {
3685 struct snd_soc_dai_link *link = &card->dai_link[i];
3686
Benoit Cousson88bd8702014-07-08 23:19:34 +02003687 ret = snd_soc_init_multicodec(card, link);
3688 if (ret) {
3689 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3690 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07003691 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02003692
3693 for (j = 0; j < link->num_codecs; j++) {
3694 /*
3695 * Codec must be specified by 1 of name or OF node,
3696 * not both or neither.
3697 */
3698 if (!!link->codecs[j].name ==
3699 !!link->codecs[j].of_node) {
3700 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3701 link->name);
3702 return -EINVAL;
3703 }
3704 /* Codec DAI name must be specified */
3705 if (!link->codecs[j].dai_name) {
3706 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3707 link->name);
3708 return -EINVAL;
3709 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003710 }
Stephen Warren5a504962011-12-21 10:40:59 -07003711
3712 /*
3713 * Platform may be specified by either name or OF node, but
3714 * can be left unspecified, and a dummy platform will be used.
3715 */
3716 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003717 dev_err(card->dev,
3718 "ASoC: Both platform name/of_node are set for %s\n",
3719 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003720 return -EINVAL;
3721 }
3722
3723 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003724 * CPU device may be specified by either name or OF node, but
3725 * can be left unspecified, and will be matched based on DAI
3726 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003727 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003728 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003729 dev_err(card->dev,
3730 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3731 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003732 return -EINVAL;
3733 }
3734 /*
3735 * At least one of CPU DAI name or CPU device name/node must be
3736 * specified
3737 */
3738 if (!link->cpu_dai_name &&
3739 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003740 dev_err(card->dev,
3741 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3742 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003743 return -EINVAL;
3744 }
3745 }
3746
Mark Browned77cc12011-05-03 18:25:34 +01003747 dev_set_drvdata(card->dev, card);
3748
Stephen Warren111c6412011-01-28 14:26:35 -07003749 snd_soc_initialize_card_lists(card);
3750
Vinod Koul150dd2f2011-01-13 22:48:32 +05303751 soc_init_card_debugfs(card);
3752
Mark Brown181a6892012-03-14 20:18:49 +00003753 card->rtd = devm_kzalloc(card->dev,
3754 sizeof(struct snd_soc_pcm_runtime) *
3755 (card->num_links + card->num_aux_devs),
3756 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003757 if (card->rtd == NULL)
3758 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003759 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003760 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003761
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003762 for (i = 0; i < card->num_links; i++) {
3763 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003764 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02003765 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3766 sizeof(struct snd_soc_dai *) *
3767 (card->rtd[i].dai_link->num_codecs),
3768 GFP_KERNEL);
3769 if (card->rtd[i].codec_dais == NULL)
3770 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003771 }
3772
3773 for (i = 0; i < card->num_aux_devs; i++)
3774 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003775
Mark Browndb432b42011-10-03 21:06:40 +01003776 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003777 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003778 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003779 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003780
Mark Brownb19e6e72012-03-14 21:18:39 +00003781 ret = snd_soc_instantiate_card(card);
3782 if (ret != 0)
3783 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003784
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003785 /* deactivate pins to sleep state */
3786 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02003787 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3788 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3789 int j;
3790
3791 for (j = 0; j < rtd->num_codecs; j++) {
3792 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3793 if (!codec_dai->active)
3794 pinctrl_pm_select_sleep_state(codec_dai->dev);
3795 }
3796
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003797 if (!cpu_dai->active)
3798 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3799 }
3800
Mark Brownb19e6e72012-03-14 21:18:39 +00003801 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003802}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303803EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003804
3805/**
3806 * snd_soc_unregister_card - Unregister a card with the ASoC core
3807 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003808 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003809 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003810 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303811int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003812{
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02003813 if (card->instantiated) {
3814 card->instantiated = false;
Lars-Peter Clausen1c325f72014-09-04 19:44:05 +02003815 snd_soc_dapm_shutdown(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05303816 soc_cleanup_card_resources(card);
Lars-Peter Clausen01e0df62014-09-04 19:44:04 +02003817 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003818 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003819
3820 return 0;
3821}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303822EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003823
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003824/*
3825 * Simplify DAI link configuration by removing ".-1" from device names
3826 * and sanitizing names.
3827 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003828static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003829{
3830 char *found, name[NAME_SIZE];
3831 int id1, id2;
3832
3833 if (dev_name(dev) == NULL)
3834 return NULL;
3835
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003836 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003837
3838 /* are we a "%s.%d" name (platform and SPI components) */
3839 found = strstr(name, dev->driver->name);
3840 if (found) {
3841 /* get ID */
3842 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3843
3844 /* discard ID from name if ID == -1 */
3845 if (*id == -1)
3846 found[strlen(dev->driver->name)] = '\0';
3847 }
3848
3849 } else {
3850 /* I2C component devices are named "bus-addr" */
3851 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3852 char tmp[NAME_SIZE];
3853
3854 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003855 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003856
3857 /* sanitize component name for DAI link creation */
3858 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003859 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003860 } else
3861 *id = 0;
3862 }
3863
3864 return kstrdup(name, GFP_KERNEL);
3865}
3866
3867/*
3868 * Simplify DAI link naming for single devices with multiple DAIs by removing
3869 * any ".-1" and using the DAI name (instead of device name).
3870 */
3871static inline char *fmt_multiple_name(struct device *dev,
3872 struct snd_soc_dai_driver *dai_drv)
3873{
3874 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003875 dev_err(dev,
3876 "ASoC: error - multiple DAI %s registered with no name\n",
3877 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003878 return NULL;
3879 }
3880
3881 return kstrdup(dai_drv->name, GFP_KERNEL);
3882}
3883
Mark Brown91151712008-11-30 23:31:24 +00003884/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003885 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003886 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003887 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003888 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003889static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003890{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003891 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003892
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003893 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003894 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3895 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003896 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003897 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003898 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003899 }
Mark Brown91151712008-11-30 23:31:24 +00003900}
Mark Brown91151712008-11-30 23:31:24 +00003901
3902/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003903 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003904 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003905 * @component: The component the DAIs are registered for
3906 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003907 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003908 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3909 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003910 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003911static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003912 struct snd_soc_dai_driver *dai_drv, size_t count,
3913 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003914{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003915 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003916 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003917 unsigned int i;
3918 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003919
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003920 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003921
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003922 component->dai_drv = dai_drv;
3923 component->num_dai = count;
3924
Mark Brown91151712008-11-30 23:31:24 +00003925 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003926
3927 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003928 if (dai == NULL) {
3929 ret = -ENOMEM;
3930 goto err;
3931 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003932
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003933 /*
3934 * Back in the old days when we still had component-less DAIs,
3935 * instead of having a static name, component-less DAIs would
3936 * inherit the name of the parent device so it is possible to
3937 * register multiple instances of the DAI. We still need to keep
3938 * the same naming style even though those DAIs are not
3939 * component-less anymore.
3940 */
3941 if (count == 1 && legacy_dai_naming) {
3942 dai->name = fmt_single_name(dev, &dai->id);
3943 } else {
3944 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3945 if (dai_drv[i].id)
3946 dai->id = dai_drv[i].id;
3947 else
3948 dai->id = i;
3949 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003950 if (dai->name == NULL) {
3951 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003952 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003953 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003954 }
3955
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003956 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003957 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003958 dai->driver = &dai_drv[i];
3959 if (!dai->driver->ops)
3960 dai->driver->ops = &null_dai_ops;
3961
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003962 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003963
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003964 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003965 }
3966
3967 return 0;
3968
3969err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003970 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003971
3972 return ret;
3973}
Mark Brown91151712008-11-30 23:31:24 +00003974
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003975static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3976 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003977{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003978 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003979
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003980 component->driver->seq_notifier(component, type, subseq);
3981}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003982
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003983static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3984 int event)
3985{
3986 struct snd_soc_component *component = dapm->component;
3987
3988 return component->driver->stream_event(component, event);
3989}
3990
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003991static int snd_soc_component_initialize(struct snd_soc_component *component,
3992 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003993{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003994 struct snd_soc_dapm_context *dapm;
3995
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003996 component->name = fmt_single_name(dev, &component->id);
3997 if (!component->name) {
3998 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003999 return -ENOMEM;
4000 }
4001
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004002 component->dev = dev;
4003 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02004004 component->probe = component->driver->probe;
4005 component->remove = component->driver->remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004006
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004007 if (!component->dapm_ptr)
4008 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004009
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004010 dapm = component->dapm_ptr;
4011 dapm->dev = dev;
4012 dapm->component = component;
4013 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02004014 dapm->idle_bias_off = true;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004015 if (driver->seq_notifier)
4016 dapm->seq_notifier = snd_soc_component_seq_notifier;
4017 if (driver->stream_event)
4018 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004019
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02004020 component->controls = driver->controls;
4021 component->num_controls = driver->num_controls;
4022 component->dapm_widgets = driver->dapm_widgets;
4023 component->num_dapm_widgets = driver->num_dapm_widgets;
4024 component->dapm_routes = driver->dapm_routes;
4025 component->num_dapm_routes = driver->num_dapm_routes;
4026
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004027 INIT_LIST_HEAD(&component->dai_list);
4028 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004029
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004030 return 0;
4031}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004032
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02004033static void snd_soc_component_init_regmap(struct snd_soc_component *component)
4034{
4035 if (!component->regmap)
4036 component->regmap = dev_get_regmap(component->dev, NULL);
4037 if (component->regmap) {
4038 int val_bytes = regmap_get_val_bytes(component->regmap);
4039 /* Errors are legitimate for non-integer byte multiples */
4040 if (val_bytes > 0)
4041 component->val_bytes = val_bytes;
4042 }
4043}
4044
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004045static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4046{
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02004047 if (!component->write && !component->read)
4048 snd_soc_component_init_regmap(component);
4049
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004050 list_add(&component->list, &component_list);
4051}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004052
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004053static void snd_soc_component_add(struct snd_soc_component *component)
4054{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004055 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004056 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004057 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004058}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004059
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004060static void snd_soc_component_cleanup(struct snd_soc_component *component)
4061{
4062 snd_soc_unregister_dais(component);
4063 kfree(component->name);
4064}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004065
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004066static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4067{
4068 list_del(&component->list);
4069}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004070
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004071static void snd_soc_component_del(struct snd_soc_component *component)
4072{
4073 mutex_lock(&client_mutex);
4074 snd_soc_component_del_unlocked(component);
4075 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004076}
4077
4078int snd_soc_register_component(struct device *dev,
4079 const struct snd_soc_component_driver *cmpnt_drv,
4080 struct snd_soc_dai_driver *dai_drv,
4081 int num_dai)
4082{
4083 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004084 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004085
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004086 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004087 if (!cmpnt) {
4088 dev_err(dev, "ASoC: Failed to allocate memory\n");
4089 return -ENOMEM;
4090 }
4091
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004092 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4093 if (ret)
4094 goto err_free;
4095
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004096 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004097 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004098
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004099 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4100 if (ret < 0) {
4101 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4102 goto err_cleanup;
4103 }
4104
4105 snd_soc_component_add(cmpnt);
4106
4107 return 0;
4108
4109err_cleanup:
4110 snd_soc_component_cleanup(cmpnt);
4111err_free:
4112 kfree(cmpnt);
4113 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004114}
4115EXPORT_SYMBOL_GPL(snd_soc_register_component);
4116
4117/**
4118 * snd_soc_unregister_component - Unregister a component from the ASoC core
4119 *
4120 */
4121void snd_soc_unregister_component(struct device *dev)
4122{
4123 struct snd_soc_component *cmpnt;
4124
4125 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004126 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004127 goto found;
4128 }
4129 return;
4130
4131found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004132 snd_soc_component_del(cmpnt);
4133 snd_soc_component_cleanup(cmpnt);
4134 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004135}
4136EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4137
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004138static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4139{
4140 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4141
4142 return platform->driver->probe(platform);
4143}
4144
4145static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4146{
4147 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4148
4149 platform->driver->remove(platform);
4150}
4151
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004152/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004153 * snd_soc_add_platform - Add a platform to the ASoC core
4154 * @dev: The parent device for the platform
4155 * @platform: The platform to add
4156 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004157 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004158int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004159 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004160{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004161 int ret;
4162
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004163 ret = snd_soc_component_initialize(&platform->component,
4164 &platform_drv->component_driver, dev);
4165 if (ret)
4166 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004167
4168 platform->dev = dev;
4169 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004170 if (platform_drv->controls) {
4171 platform->component.controls = platform_drv->controls;
4172 platform->component.num_controls = platform_drv->num_controls;
4173 }
4174 if (platform_drv->dapm_widgets) {
4175 platform->component.dapm_widgets = platform_drv->dapm_widgets;
4176 platform->component.num_dapm_widgets = platform_drv->num_dapm_widgets;
4177 platform->component.steal_sibling_dai_widgets = true;
4178 }
4179 if (platform_drv->dapm_routes) {
4180 platform->component.dapm_routes = platform_drv->dapm_routes;
4181 platform->component.num_dapm_routes = platform_drv->num_dapm_routes;
4182 }
4183
4184 if (platform_drv->probe)
4185 platform->component.probe = snd_soc_platform_drv_probe;
4186 if (platform_drv->remove)
4187 platform->component.remove = snd_soc_platform_drv_remove;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004188
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004189#ifdef CONFIG_DEBUG_FS
4190 platform->component.debugfs_prefix = "platform";
4191#endif
4192
Mark Brown12a48a8c2008-12-03 19:40:30 +00004193 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004194 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004195 list_add(&platform->list, &platform_list);
4196 mutex_unlock(&client_mutex);
4197
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004198 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4199 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004200
4201 return 0;
4202}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004203EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4204
4205/**
4206 * snd_soc_register_platform - Register a platform with the ASoC core
4207 *
4208 * @platform: platform to register
4209 */
4210int snd_soc_register_platform(struct device *dev,
4211 const struct snd_soc_platform_driver *platform_drv)
4212{
4213 struct snd_soc_platform *platform;
4214 int ret;
4215
4216 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4217
4218 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4219 if (platform == NULL)
4220 return -ENOMEM;
4221
4222 ret = snd_soc_add_platform(dev, platform, platform_drv);
4223 if (ret)
4224 kfree(platform);
4225
4226 return ret;
4227}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004228EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4229
4230/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004231 * snd_soc_remove_platform - Remove a platform from the ASoC core
4232 * @platform: the platform to remove
4233 */
4234void snd_soc_remove_platform(struct snd_soc_platform *platform)
4235{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004236
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004237 mutex_lock(&client_mutex);
4238 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004239 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004240 mutex_unlock(&client_mutex);
4241
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004242 snd_soc_component_cleanup(&platform->component);
4243
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004244 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004245 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004246}
4247EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4248
4249struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4250{
4251 struct snd_soc_platform *platform;
4252
4253 list_for_each_entry(platform, &platform_list, list) {
4254 if (dev == platform->dev)
4255 return platform;
4256 }
4257
4258 return NULL;
4259}
4260EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4261
4262/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004263 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4264 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004265 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004266 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004267void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004268{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004269 struct snd_soc_platform *platform;
4270
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004271 platform = snd_soc_lookup_platform(dev);
4272 if (!platform)
4273 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004274
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004275 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004276 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004277}
4278EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4279
Mark Brown151ab222009-05-09 16:22:58 +01004280static u64 codec_format_map[] = {
4281 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4282 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4283 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4284 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4285 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4286 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4287 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4288 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4289 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4290 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4291 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4292 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4293 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4294 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4295 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4296 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4297};
4298
4299/* Fix up the DAI formats for endianness: codecs don't actually see
4300 * the endianness of the data but we're using the CPU format
4301 * definitions which do need to include endianness so we ensure that
4302 * codec DAIs always have both big and little endian variants set.
4303 */
4304static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4305{
4306 int i;
4307
4308 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4309 if (stream->formats & codec_format_map[i])
4310 stream->formats |= codec_format_map[i];
4311}
4312
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004313static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4314{
4315 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4316
4317 return codec->driver->probe(codec);
4318}
4319
4320static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4321{
4322 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4323
4324 codec->driver->remove(codec);
4325}
4326
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004327static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4328 unsigned int reg, unsigned int val)
4329{
4330 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4331
4332 return codec->driver->write(codec, reg, val);
4333}
4334
4335static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4336 unsigned int reg, unsigned int *val)
4337{
4338 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4339
4340 *val = codec->driver->read(codec, reg);
4341
4342 return 0;
4343}
4344
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004345static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4346 enum snd_soc_bias_level level)
4347{
4348 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4349
4350 return codec->driver->set_bias_level(codec, level);
4351}
4352
Mark Brown0d0cf002008-12-10 14:32:45 +00004353/**
4354 * snd_soc_register_codec - Register a codec with the ASoC core
4355 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004356 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004357 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004358int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004359 const struct snd_soc_codec_driver *codec_drv,
4360 struct snd_soc_dai_driver *dai_drv,
4361 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004362{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004363 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004364 struct snd_soc_dai *dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004365 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004366
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004367 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004368
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004369 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4370 if (codec == NULL)
4371 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004372
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004373 codec->component.dapm_ptr = &codec->dapm;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004374 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004375
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004376 ret = snd_soc_component_initialize(&codec->component,
4377 &codec_drv->component_driver, dev);
4378 if (ret)
4379 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01004380
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004381 if (codec_drv->controls) {
4382 codec->component.controls = codec_drv->controls;
4383 codec->component.num_controls = codec_drv->num_controls;
4384 }
4385 if (codec_drv->dapm_widgets) {
4386 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4387 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4388 }
4389 if (codec_drv->dapm_routes) {
4390 codec->component.dapm_routes = codec_drv->dapm_routes;
4391 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4392 }
4393
4394 if (codec_drv->probe)
4395 codec->component.probe = snd_soc_codec_drv_probe;
4396 if (codec_drv->remove)
4397 codec->component.remove = snd_soc_codec_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004398 if (codec_drv->write)
4399 codec->component.write = snd_soc_codec_drv_write;
4400 if (codec_drv->read)
4401 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004402 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004403 codec->dapm.codec = codec;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02004404 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
Lars-Peter Clausen86dbf2a2014-09-04 19:44:06 +02004405 codec->dapm.suspend_bias_off = codec_drv->suspend_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004406 if (codec_drv->seq_notifier)
4407 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004408 if (codec_drv->set_bias_level)
4409 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004410 codec->dev = dev;
4411 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004412 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004413 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004414
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004415#ifdef CONFIG_DEBUG_FS
4416 codec->component.init_debugfs = soc_init_codec_debugfs;
4417 codec->component.debugfs_prefix = "codec";
4418#endif
4419
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02004420 if (codec_drv->get_regmap)
4421 codec->component.regmap = codec_drv->get_regmap(dev);
Xiubo Lia39f75f2014-03-26 13:40:23 +08004422
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004423 for (i = 0; i < num_dai; i++) {
4424 fixup_codec_formats(&dai_drv[i].playback);
4425 fixup_codec_formats(&dai_drv[i].capture);
4426 }
4427
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004428 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4429 if (ret < 0) {
4430 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4431 goto err_cleanup;
4432 }
4433
4434 list_for_each_entry(dai, &codec->component.dai_list, list)
4435 dai->codec = codec;
4436
Mark Brown054880f2012-04-09 17:29:19 +01004437 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004438 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004439 list_add(&codec->list, &codec_list);
4440 mutex_unlock(&client_mutex);
4441
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004442 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4443 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004444 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004445
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004446err_cleanup:
4447 snd_soc_component_cleanup(&codec->component);
4448err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004449 kfree(codec);
4450 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004451}
4452EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4453
4454/**
4455 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4456 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004457 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004458 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004459void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004460{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004461 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004462
4463 list_for_each_entry(codec, &codec_list, list) {
4464 if (dev == codec->dev)
4465 goto found;
4466 }
4467 return;
4468
4469found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004470
Mark Brown0d0cf002008-12-10 14:32:45 +00004471 mutex_lock(&client_mutex);
4472 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004473 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004474 mutex_unlock(&client_mutex);
4475
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004476 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4477 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004478
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004479 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004480 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004481 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004482}
4483EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4484
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004485/* Retrieve a card's name from device tree */
4486int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4487 const char *propname)
4488{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304489 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004490 int ret;
4491
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304492 if (!card->dev) {
4493 pr_err("card->dev is not set before calling %s\n", __func__);
4494 return -EINVAL;
4495 }
4496
4497 np = card->dev->of_node;
4498
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004499 ret = of_property_read_string_index(np, propname, 0, &card->name);
4500 /*
4501 * EINVAL means the property does not exist. This is fine providing
4502 * card->name was previously set, which is checked later in
4503 * snd_soc_register_card.
4504 */
4505 if (ret < 0 && ret != -EINVAL) {
4506 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004507 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004508 propname, ret);
4509 return ret;
4510 }
4511
4512 return 0;
4513}
4514EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4515
Xiubo Li9a6d4862014-02-08 15:59:52 +08004516static const struct snd_soc_dapm_widget simple_widgets[] = {
4517 SND_SOC_DAPM_MIC("Microphone", NULL),
4518 SND_SOC_DAPM_LINE("Line", NULL),
4519 SND_SOC_DAPM_HP("Headphone", NULL),
4520 SND_SOC_DAPM_SPK("Speaker", NULL),
4521};
4522
4523int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4524 const char *propname)
4525{
4526 struct device_node *np = card->dev->of_node;
4527 struct snd_soc_dapm_widget *widgets;
4528 const char *template, *wname;
4529 int i, j, num_widgets, ret;
4530
4531 num_widgets = of_property_count_strings(np, propname);
4532 if (num_widgets < 0) {
4533 dev_err(card->dev,
4534 "ASoC: Property '%s' does not exist\n", propname);
4535 return -EINVAL;
4536 }
4537 if (num_widgets & 1) {
4538 dev_err(card->dev,
4539 "ASoC: Property '%s' length is not even\n", propname);
4540 return -EINVAL;
4541 }
4542
4543 num_widgets /= 2;
4544 if (!num_widgets) {
4545 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4546 propname);
4547 return -EINVAL;
4548 }
4549
4550 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4551 GFP_KERNEL);
4552 if (!widgets) {
4553 dev_err(card->dev,
4554 "ASoC: Could not allocate memory for widgets\n");
4555 return -ENOMEM;
4556 }
4557
4558 for (i = 0; i < num_widgets; i++) {
4559 ret = of_property_read_string_index(np, propname,
4560 2 * i, &template);
4561 if (ret) {
4562 dev_err(card->dev,
4563 "ASoC: Property '%s' index %d read error:%d\n",
4564 propname, 2 * i, ret);
4565 return -EINVAL;
4566 }
4567
4568 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4569 if (!strncmp(template, simple_widgets[j].name,
4570 strlen(simple_widgets[j].name))) {
4571 widgets[i] = simple_widgets[j];
4572 break;
4573 }
4574 }
4575
4576 if (j >= ARRAY_SIZE(simple_widgets)) {
4577 dev_err(card->dev,
4578 "ASoC: DAPM widget '%s' is not supported\n",
4579 template);
4580 return -EINVAL;
4581 }
4582
4583 ret = of_property_read_string_index(np, propname,
4584 (2 * i) + 1,
4585 &wname);
4586 if (ret) {
4587 dev_err(card->dev,
4588 "ASoC: Property '%s' index %d read error:%d\n",
4589 propname, (2 * i) + 1, ret);
4590 return -EINVAL;
4591 }
4592
4593 widgets[i].name = wname;
4594 }
4595
4596 card->dapm_widgets = widgets;
4597 card->num_dapm_widgets = num_widgets;
4598
4599 return 0;
4600}
4601EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4602
Xiubo Li89c67852014-02-14 09:34:35 +08004603int snd_soc_of_parse_tdm_slot(struct device_node *np,
4604 unsigned int *slots,
4605 unsigned int *slot_width)
4606{
4607 u32 val;
4608 int ret;
4609
4610 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4611 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4612 if (ret)
4613 return ret;
4614
4615 if (slots)
4616 *slots = val;
4617 }
4618
4619 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4620 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4621 if (ret)
4622 return ret;
4623
4624 if (slot_width)
4625 *slot_width = val;
4626 }
4627
4628 return 0;
4629}
4630EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4631
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004632int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4633 const char *propname)
4634{
4635 struct device_node *np = card->dev->of_node;
4636 int num_routes;
4637 struct snd_soc_dapm_route *routes;
4638 int i, ret;
4639
4640 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004641 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004642 dev_err(card->dev,
4643 "ASoC: Property '%s' does not exist or its length is not even\n",
4644 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004645 return -EINVAL;
4646 }
4647 num_routes /= 2;
4648 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004649 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004650 propname);
4651 return -EINVAL;
4652 }
4653
4654 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4655 GFP_KERNEL);
4656 if (!routes) {
4657 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004658 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004659 return -EINVAL;
4660 }
4661
4662 for (i = 0; i < num_routes; i++) {
4663 ret = of_property_read_string_index(np, propname,
4664 2 * i, &routes[i].sink);
4665 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004666 dev_err(card->dev,
4667 "ASoC: Property '%s' index %d could not be read: %d\n",
4668 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004669 return -EINVAL;
4670 }
4671 ret = of_property_read_string_index(np, propname,
4672 (2 * i) + 1, &routes[i].source);
4673 if (ret) {
4674 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004675 "ASoC: Property '%s' index %d could not be read: %d\n",
4676 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004677 return -EINVAL;
4678 }
4679 }
4680
4681 card->num_dapm_routes = num_routes;
4682 card->dapm_routes = routes;
4683
4684 return 0;
4685}
4686EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4687
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004688unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004689 const char *prefix,
4690 struct device_node **bitclkmaster,
4691 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004692{
4693 int ret, i;
4694 char prop[128];
4695 unsigned int format = 0;
4696 int bit, frame;
4697 const char *str;
4698 struct {
4699 char *name;
4700 unsigned int val;
4701 } of_fmt_table[] = {
4702 { "i2s", SND_SOC_DAIFMT_I2S },
4703 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4704 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4705 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4706 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4707 { "ac97", SND_SOC_DAIFMT_AC97 },
4708 { "pdm", SND_SOC_DAIFMT_PDM},
4709 { "msb", SND_SOC_DAIFMT_MSB },
4710 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004711 };
4712
4713 if (!prefix)
4714 prefix = "";
4715
4716 /*
4717 * check "[prefix]format = xxx"
4718 * SND_SOC_DAIFMT_FORMAT_MASK area
4719 */
4720 snprintf(prop, sizeof(prop), "%sformat", prefix);
4721 ret = of_property_read_string(np, prop, &str);
4722 if (ret == 0) {
4723 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4724 if (strcmp(str, of_fmt_table[i].name) == 0) {
4725 format |= of_fmt_table[i].val;
4726 break;
4727 }
4728 }
4729 }
4730
4731 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004732 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004733 * SND_SOC_DAIFMT_CLOCK_MASK area
4734 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004735 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4736 if (of_get_property(np, prop, NULL))
4737 format |= SND_SOC_DAIFMT_CONT;
4738 else
4739 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004740
4741 /*
4742 * check "[prefix]bitclock-inversion"
4743 * check "[prefix]frame-inversion"
4744 * SND_SOC_DAIFMT_INV_MASK area
4745 */
4746 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4747 bit = !!of_get_property(np, prop, NULL);
4748
4749 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4750 frame = !!of_get_property(np, prop, NULL);
4751
4752 switch ((bit << 4) + frame) {
4753 case 0x11:
4754 format |= SND_SOC_DAIFMT_IB_IF;
4755 break;
4756 case 0x10:
4757 format |= SND_SOC_DAIFMT_IB_NF;
4758 break;
4759 case 0x01:
4760 format |= SND_SOC_DAIFMT_NB_IF;
4761 break;
4762 default:
4763 /* SND_SOC_DAIFMT_NB_NF is default */
4764 break;
4765 }
4766
4767 /*
4768 * check "[prefix]bitclock-master"
4769 * check "[prefix]frame-master"
4770 * SND_SOC_DAIFMT_MASTER_MASK area
4771 */
4772 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4773 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004774 if (bit && bitclkmaster)
4775 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004776
4777 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4778 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004779 if (frame && framemaster)
4780 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004781
4782 switch ((bit << 4) + frame) {
4783 case 0x11:
4784 format |= SND_SOC_DAIFMT_CBM_CFM;
4785 break;
4786 case 0x10:
4787 format |= SND_SOC_DAIFMT_CBM_CFS;
4788 break;
4789 case 0x01:
4790 format |= SND_SOC_DAIFMT_CBS_CFM;
4791 break;
4792 default:
4793 format |= SND_SOC_DAIFMT_CBS_CFS;
4794 break;
4795 }
4796
4797 return format;
4798}
4799EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4800
Kuninori Morimotocb470082013-09-10 17:39:56 -07004801int snd_soc_of_get_dai_name(struct device_node *of_node,
4802 const char **dai_name)
4803{
4804 struct snd_soc_component *pos;
4805 struct of_phandle_args args;
4806 int ret;
4807
4808 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4809 "#sound-dai-cells", 0, &args);
4810 if (ret)
4811 return ret;
4812
4813 ret = -EPROBE_DEFER;
4814
4815 mutex_lock(&client_mutex);
4816 list_for_each_entry(pos, &component_list, list) {
4817 if (pos->dev->of_node != args.np)
4818 continue;
4819
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004820 if (pos->driver->of_xlate_dai_name) {
4821 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4822 } else {
4823 int id = -1;
4824
4825 switch (args.args_count) {
4826 case 0:
4827 id = 0; /* same as dai_drv[0] */
4828 break;
4829 case 1:
4830 id = args.args[0];
4831 break;
4832 default:
4833 /* not supported */
4834 break;
4835 }
4836
4837 if (id < 0 || id >= pos->num_dai) {
4838 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004839 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004840 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004841
4842 ret = 0;
4843
4844 *dai_name = pos->dai_drv[id].name;
4845 if (!*dai_name)
4846 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004847 }
4848
Kuninori Morimotocb470082013-09-10 17:39:56 -07004849 break;
4850 }
4851 mutex_unlock(&client_mutex);
4852
4853 of_node_put(args.np);
4854
4855 return ret;
4856}
4857EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4858
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004859static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004860{
Mark Brown384c89e2008-12-03 17:34:03 +00004861#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004862 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4863 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004864 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004865 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004866 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004867
Mark Brown8a9dab12011-01-10 22:25:21 +00004868 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004869 &codec_list_fops))
4870 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4871
Mark Brown8a9dab12011-01-10 22:25:21 +00004872 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004873 &dai_list_fops))
4874 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004875
Mark Brown8a9dab12011-01-10 22:25:21 +00004876 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004877 &platform_list_fops))
4878 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004879#endif
4880
Mark Brownfb257892011-04-28 10:57:54 +01004881 snd_soc_util_init();
4882
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004883 return platform_driver_register(&soc_driver);
4884}
Mark Brown4abe8e12010-10-12 17:41:03 +01004885module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004886
Mark Brown7d8c16a2008-11-30 22:11:24 +00004887static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004888{
Mark Brownfb257892011-04-28 10:57:54 +01004889 snd_soc_util_exit();
4890
Mark Brown384c89e2008-12-03 17:34:03 +00004891#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004892 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004893#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004894 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004895}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004896module_exit(snd_soc_exit);
4897
4898/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004899MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004900MODULE_DESCRIPTION("ALSA SoC Core");
4901MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004902MODULE_ALIAS("platform:soc-audio");