blob: c8bdac2db3774eaec14f070f46bbdfbd52c34663 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020033#include <linux/pinctrl/consumer.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010034#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070036#include <linux/of.h>
Markus Pargmann741a5092013-08-19 17:05:55 +020037#include <linux/gpio.h>
38#include <linux/of_gpio.h>
Marek Vasut474828a2009-07-22 13:01:03 +020039#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020040#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000041#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/pcm.h>
43#include <sound/pcm_params.h>
44#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010045#include <sound/soc-dpcm.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020046#include <sound/initval.h>
47
Mark Browna8b1d342010-11-03 18:05:58 -040048#define CREATE_TRACE_POINTS
49#include <trace/events/asoc.h>
50
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000051#define NAME_SIZE 32
52
Mark Brown384c89e2008-12-03 17:34:03 +000053#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000054struct dentry *snd_soc_debugfs_root;
55EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000056#endif
57
Mark Brownc5af3a22008-11-28 13:29:45 +000058static DEFINE_MUTEX(client_mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070061static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000062
Frank Mandarinodb2a4162006-10-06 18:31:09 +020063/*
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68static int pmdown_time = 5000;
69module_param(pmdown_time, int, 0);
70MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
Markus Pargmann741a5092013-08-19 17:05:55 +020072struct snd_ac97_reset_cfg {
73 struct pinctrl *pctl;
74 struct pinctrl_state *pstate_reset;
75 struct pinctrl_state *pstate_warm_reset;
76 struct pinctrl_state *pstate_run;
77 int gpio_sdata;
78 int gpio_sync;
79 int gpio_reset;
80};
81
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000082/* returns the minimum number of bytes needed to represent
83 * a particular given value */
84static int min_bytes_needed(unsigned long val)
85{
86 int c = 0;
87 int i;
88
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
90 if (val & (1UL << i))
91 break;
92 c = (sizeof val * 8) - c;
93 if (!c || (c % 8))
94 c = (c + 8) / 8;
95 else
96 c /= 8;
97 return c;
98}
99
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000100/* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +0000104{
Stephen Warren00b317a2011-04-01 14:50:44 -0600105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000107 int ret;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
110
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
113 WARN_ON(len > 63);
114
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
117 return -EINVAL;
118
Mark Brown25032c12011-08-01 13:52:48 +0900119 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000120 if (ret < 0) {
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
123 } else {
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
125 }
126
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
131
132 return 0;
133}
134
135/* codec register dump */
136static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
138{
139 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000140 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000141 int len;
142 size_t total = 0;
143 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000144
Stephen Warren00b317a2011-04-01 14:50:44 -0600145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000147
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000148 len = wordsize + regsize + 2 + 1;
149
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000150 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000151 return 0;
152
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100157 /* only support larger than PAGE_SIZE bytes debugfs
158 * entries for the default case */
159 if (p >= pos) {
160 if (total + len >= count - 1)
161 break;
162 format_register_str(codec, i, buf + total, len);
163 total += len;
Mark Brown5164d742010-07-14 16:14:33 +0100164 }
Lars-Peter Clausen20a0ec22014-03-18 09:02:09 +0100165 p += len;
Mark Brown2624d5f2009-11-03 21:56:13 +0000166 }
167
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000169
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000170 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000171}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000172
Mark Brown2624d5f2009-11-03 21:56:13 +0000173static ssize_t codec_reg_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
175{
Mark Brown36ae1a92012-01-06 17:12:45 -0800176 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000177
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000178 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000179}
180
181static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
182
Mark Browndbe21402010-02-12 11:37:24 +0000183static ssize_t pmdown_time_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
185{
Mark Brown36ae1a92012-01-06 17:12:45 -0800186 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000187
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000189}
190
191static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
194{
Mark Brown36ae1a92012-01-06 17:12:45 -0800195 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700196 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
Jingoo Hanb785a492013-07-19 16:24:59 +0900198 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700199 if (ret)
200 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000201
202 return count;
203}
204
205static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
206
Mark Brown2624d5f2009-11-03 21:56:13 +0000207#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000208static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000209 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000210{
211 ssize_t ret;
212 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000213 char *buf;
214
215 if (*ppos < 0 || !count)
216 return -EINVAL;
217
218 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000219 if (!buf)
220 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000221
222 ret = soc_codec_reg_show(codec, buf, count, *ppos);
223 if (ret >= 0) {
224 if (copy_to_user(user_buf, buf, ret)) {
225 kfree(buf);
226 return -EFAULT;
227 }
228 *ppos += ret;
229 }
230
Mark Brown2624d5f2009-11-03 21:56:13 +0000231 kfree(buf);
232 return ret;
233}
234
235static ssize_t codec_reg_write_file(struct file *file,
236 const char __user *user_buf, size_t count, loff_t *ppos)
237{
238 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700239 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000240 char *start = buf;
241 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000242 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900243 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000244
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
247 return -EFAULT;
248 buf[buf_size] = 0;
249
Mark Brown2624d5f2009-11-03 21:56:13 +0000250 while (*start == ' ')
251 start++;
252 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000253 while (*start == ' ')
254 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900255 ret = kstrtoul(start, 16, &value);
256 if (ret)
257 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000258
259 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030260 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000261
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000262 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000263 return buf_size;
264}
265
266static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700267 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200270 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000271};
272
Russell Kinge73f3de2014-06-26 15:22:50 +0100273static struct dentry *soc_debugfs_create_dir(struct dentry *parent,
274 const char *fmt, ...)
275{
276 struct dentry *de;
277 va_list ap;
278 char *s;
279
280 va_start(ap, fmt);
281 s = kvasprintf(GFP_KERNEL, fmt, ap);
282 va_end(ap);
283
284 if (!s)
285 return NULL;
286
287 de = debugfs_create_dir(s, parent);
288 kfree(s);
289
290 return de;
291}
292
Mark Brown2624d5f2009-11-03 21:56:13 +0000293static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
294{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200295 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
296
Russell Kinge73f3de2014-06-26 15:22:50 +0100297 codec->debugfs_codec_root = soc_debugfs_create_dir(debugfs_card_root,
Mark Brown211bcc62014-06-28 13:55:22 +0100298 "codec:%s",
299 codec->component.name);
Mark Brown2624d5f2009-11-03 21:56:13 +0000300 if (!codec->debugfs_codec_root) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200301 dev_warn(codec->dev,
302 "ASoC: Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000303 return;
304 }
305
Mark Brownaaee8ef2011-01-26 20:53:50 +0000306 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
307 &codec->cache_sync);
308 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
309 &codec->cache_only);
310
Mark Brown2624d5f2009-11-03 21:56:13 +0000311 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
312 codec->debugfs_codec_root,
313 codec, &codec_reg_fops);
314 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200315 dev_warn(codec->dev,
316 "ASoC: Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000317
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200318 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000319}
320
321static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
322{
323 debugfs_remove_recursive(codec->debugfs_codec_root);
324}
325
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000326static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
327{
328 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
329
Russell Kinge73f3de2014-06-26 15:22:50 +0100330 platform->debugfs_platform_root = soc_debugfs_create_dir(debugfs_card_root,
Mark Brown211bcc62014-06-28 13:55:22 +0100331 "platform:%s",
332 platform->component.name);
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000333 if (!platform->debugfs_platform_root) {
334 dev_warn(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000335 "ASoC: Failed to create platform debugfs directory\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000336 return;
337 }
338
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +0200339 snd_soc_dapm_debugfs_init(&platform->component.dapm,
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000340 platform->debugfs_platform_root);
341}
342
343static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
344{
345 debugfs_remove_recursive(platform->debugfs_platform_root);
346}
347
Mark Brownc3c5a192010-09-15 18:15:14 +0100348static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
349 size_t count, loff_t *ppos)
350{
351 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100352 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100353 struct snd_soc_codec *codec;
354
355 if (!buf)
356 return -ENOMEM;
357
Mark Brown2b194f9d2010-10-13 10:52:16 +0100358 list_for_each_entry(codec, &codec_list, list) {
359 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200360 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100361 if (len >= 0)
362 ret += len;
363 if (ret > PAGE_SIZE) {
364 ret = PAGE_SIZE;
365 break;
366 }
367 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100368
369 if (ret >= 0)
370 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
371
372 kfree(buf);
373
374 return ret;
375}
376
377static const struct file_operations codec_list_fops = {
378 .read = codec_list_read_file,
379 .llseek = default_llseek,/* read accesses f_pos */
380};
381
Mark Brownf3208782010-09-15 18:19:07 +0100382static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
383 size_t count, loff_t *ppos)
384{
385 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100386 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100387 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100388 struct snd_soc_dai *dai;
389
390 if (!buf)
391 return -ENOMEM;
392
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100393 list_for_each_entry(component, &component_list, list) {
394 list_for_each_entry(dai, &component->dai_list, list) {
395 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
396 dai->name);
397 if (len >= 0)
398 ret += len;
399 if (ret > PAGE_SIZE) {
400 ret = PAGE_SIZE;
401 break;
402 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100403 }
404 }
Mark Brownf3208782010-09-15 18:19:07 +0100405
Mark Brown2b194f9d2010-10-13 10:52:16 +0100406 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100407
408 kfree(buf);
409
410 return ret;
411}
412
413static const struct file_operations dai_list_fops = {
414 .read = dai_list_read_file,
415 .llseek = default_llseek,/* read accesses f_pos */
416};
417
Mark Brown19c7ac22010-09-15 18:22:40 +0100418static ssize_t platform_list_read_file(struct file *file,
419 char __user *user_buf,
420 size_t count, loff_t *ppos)
421{
422 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100423 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100424 struct snd_soc_platform *platform;
425
426 if (!buf)
427 return -ENOMEM;
428
Mark Brown2b194f9d2010-10-13 10:52:16 +0100429 list_for_each_entry(platform, &platform_list, list) {
430 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200431 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100432 if (len >= 0)
433 ret += len;
434 if (ret > PAGE_SIZE) {
435 ret = PAGE_SIZE;
436 break;
437 }
438 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100439
Mark Brown2b194f9d2010-10-13 10:52:16 +0100440 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100441
442 kfree(buf);
443
444 return ret;
445}
446
447static const struct file_operations platform_list_fops = {
448 .read = platform_list_read_file,
449 .llseek = default_llseek,/* read accesses f_pos */
450};
451
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200452static void soc_init_card_debugfs(struct snd_soc_card *card)
453{
454 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000455 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200456 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200457 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100458 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200459 return;
460 }
461
462 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
463 card->debugfs_card_root,
464 &card->pop_time);
465 if (!card->debugfs_pop_time)
466 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000467 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200468}
469
470static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
471{
472 debugfs_remove_recursive(card->debugfs_card_root);
473}
474
Mark Brown2624d5f2009-11-03 21:56:13 +0000475#else
476
477static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
478{
479}
480
481static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
482{
483}
Axel Linb95fccb2010-11-09 17:06:44 +0800484
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000485static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
486{
487}
488
489static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
490{
491}
492
Axel Linb95fccb2010-11-09 17:06:44 +0800493static inline void soc_init_card_debugfs(struct snd_soc_card *card)
494{
495}
496
497static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
498{
499}
Mark Brown2624d5f2009-11-03 21:56:13 +0000500#endif
501
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100502struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
503 const char *dai_link, int stream)
504{
505 int i;
506
507 for (i = 0; i < card->num_links; i++) {
508 if (card->rtd[i].dai_link->no_pcm &&
509 !strcmp(card->rtd[i].dai_link->name, dai_link))
510 return card->rtd[i].pcm->streams[stream].substream;
511 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000512 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100513 return NULL;
514}
515EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
516
517struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
518 const char *dai_link)
519{
520 int i;
521
522 for (i = 0; i < card->num_links; i++) {
523 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
524 return &card->rtd[i];
525 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000526 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100527 return NULL;
528}
529EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
530
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531#ifdef CONFIG_SND_SOC_AC97_BUS
532/* unregister ac97 codec */
533static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
534{
535 if (codec->ac97->dev.bus)
536 device_unregister(&codec->ac97->dev);
537 return 0;
538}
539
540/* stop no dev release warning */
541static void soc_ac97_device_release(struct device *dev){}
542
543/* register ac97 codec to bus */
544static int soc_ac97_dev_register(struct snd_soc_codec *codec)
545{
546 int err;
547
548 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100549 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200550 codec->ac97->dev.release = soc_ac97_device_release;
551
Kay Sieversbb072bf2008-11-02 03:50:35 +0100552 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200553 codec->card->snd_card->number, 0, codec->component.name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554 err = device_register(&codec->ac97->dev);
555 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000556 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200557 codec->ac97->dev.bus = NULL;
558 return err;
559 }
560 return 0;
561}
562#endif
563
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100564static void codec2codec_close_delayed_work(struct work_struct *work)
565{
566 /* Currently nothing to do for c2c links
567 * Since c2c links are internal nodes in the DAPM graph and
568 * don't interface with the outside world or application layer
569 * we don't have to do any special handling on close.
570 */
571}
572
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000573#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200574/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000575int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200576{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000577 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200578 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200579 int i;
580
Daniel Macke3509ff2009-06-03 17:44:49 +0200581 /* If the initialization of this soc device failed, there is no codec
582 * associated with it. Just bail out in this case.
583 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200585 return 0;
586
Andy Green6ed25972008-06-13 16:24:05 +0100587 /* Due to the resume being scheduled into a workqueue we could
588 * suspend before that's finished - wait for it to complete.
589 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000590 snd_power_lock(card->snd_card);
591 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
592 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100593
594 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000595 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100596
Mark Browna00f90f2010-12-02 16:24:24 +0000597 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 for (i = 0; i < card->num_rtd; i++) {
599 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
600 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100601
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000602 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100603 continue;
604
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605 if (drv->ops->digital_mute && dai->playback_active)
606 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200607 }
608
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100609 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 for (i = 0; i < card->num_rtd; i++) {
611 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100612 continue;
613
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000614 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100615 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100616
Mark Brown87506542008-11-18 20:50:34 +0000617 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000618 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200619
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 for (i = 0; i < card->num_rtd; i++) {
621 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
622 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100623
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100625 continue;
626
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
628 cpu_dai->driver->suspend(cpu_dai);
629 if (platform->driver->suspend && !platform->suspended) {
630 platform->driver->suspend(cpu_dai);
631 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100632 }
633 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000635 /* close any waiting streams and save state */
636 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo43829732012-08-20 14:51:24 -0700637 flush_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200638 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100640
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000641 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642
643 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100644 continue;
645
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800646 snd_soc_dapm_stream_event(&card->rtd[i],
647 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800648 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000649
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800650 snd_soc_dapm_stream_event(&card->rtd[i],
651 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800652 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000653 }
654
Mark Browne2d32ff2012-08-31 17:38:32 -0700655 /* Recheck all analogue paths too */
656 dapm_mark_io_dirty(&card->dapm);
657 snd_soc_dapm_sync(&card->dapm);
658
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000659 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200660 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 /* If there are paths active then the CODEC will be held with
662 * bias _ON and should not be suspended. */
663 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200664 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000666 /*
667 * If the CODEC is capable of idle
668 * bias off then being in STANDBY
669 * means it's doing something,
670 * otherwise fall through.
671 */
672 if (codec->dapm.idle_bias_off) {
673 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200674 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000675 break;
676 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000677 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100678 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000679 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900680 codec->cache_sync = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200681 if (codec->component.regmap)
682 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800683 /* deactivate pins to sleep state */
684 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000685 break;
686 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200687 dev_dbg(codec->dev,
688 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000689 break;
690 }
691 }
692 }
693
694 for (i = 0; i < card->num_rtd; i++) {
695 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
696
697 if (card->rtd[i].dai_link->ignore_suspend)
698 continue;
699
700 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
701 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800702
703 /* deactivate pins to sleep state */
704 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200705 }
706
Mark Brown87506542008-11-18 20:50:34 +0000707 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000708 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200709
710 return 0;
711}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000712EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200713
Andy Green6ed25972008-06-13 16:24:05 +0100714/* deferred resume work, so resume can complete before we finished
715 * setting our codec back up, which can be very slow on I2C
716 */
717static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200718{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000719 struct snd_soc_card *card =
720 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200721 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200722 int i;
723
Andy Green6ed25972008-06-13 16:24:05 +0100724 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
725 * so userspace apps are blocked from touching us
726 */
727
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000728 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100729
Mark Brown99497882010-05-07 20:24:05 +0100730 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000731 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100732
Mark Brown87506542008-11-18 20:50:34 +0000733 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000734 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200735
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000736 /* resume AC97 DAIs */
737 for (i = 0; i < card->num_rtd; i++) {
738 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100739
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000740 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100741 continue;
742
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
744 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200745 }
746
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200747 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 /* If the CODEC was idle over suspend then it will have been
749 * left with bias OFF or STANDBY and suspended so we must now
750 * resume. Otherwise the suspend was suppressed.
751 */
752 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200753 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754 case SND_SOC_BIAS_STANDBY:
755 case SND_SOC_BIAS_OFF:
756 codec->driver->resume(codec);
757 codec->suspended = 0;
758 break;
759 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200760 dev_dbg(codec->dev,
761 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000762 break;
763 }
Mark Brown1547aba2010-05-07 21:11:40 +0100764 }
765 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200766
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000767 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100768
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000769 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100770 continue;
771
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800772 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000773 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800774 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000775
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800776 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000777 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800778 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200779 }
780
Mark Brown3ff3f642008-05-19 12:32:25 +0200781 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782 for (i = 0; i < card->num_rtd; i++) {
783 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
784 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100785
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000786 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100787 continue;
788
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000789 if (drv->ops->digital_mute && dai->playback_active)
790 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791 }
792
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000793 for (i = 0; i < card->num_rtd; i++) {
794 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
795 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100796
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000797 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100798 continue;
799
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000800 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
801 cpu_dai->driver->resume(cpu_dai);
802 if (platform->driver->resume && platform->suspended) {
803 platform->driver->resume(cpu_dai);
804 platform->suspended = 0;
805 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200806 }
807
Mark Brown87506542008-11-18 20:50:34 +0000808 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000809 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200810
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000811 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100812
813 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000814 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700815
816 /* Recheck all analogue paths too */
817 dapm_mark_io_dirty(&card->dapm);
818 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100819}
820
821/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000822int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100823{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000824 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600825 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200826
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800827 /* If the initialization of this soc device failed, there is no codec
828 * associated with it. Just bail out in this case.
829 */
830 if (list_empty(&card->codec_dev_list))
831 return 0;
832
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800833 /* activate pins from sleep state */
834 for (i = 0; i < card->num_rtd; i++) {
835 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
836 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
837 if (cpu_dai->active)
838 pinctrl_pm_select_default_state(cpu_dai->dev);
839 if (codec_dai->active)
840 pinctrl_pm_select_default_state(codec_dai->dev);
841 }
842
Mark Brown64ab9ba2009-03-31 11:27:03 +0100843 /* AC97 devices might have other drivers hanging off them so
844 * need to resume immediately. Other drivers don't have that
845 * problem and may take a substantial amount of time to resume
846 * due to I/O costs and anti-pop so handle them out of line.
847 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000848 for (i = 0; i < card->num_rtd; i++) {
849 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600850 ac97_control |= cpu_dai->driver->ac97_control;
851 }
852 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000853 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600854 soc_resume_deferred(&card->deferred_resume_work);
855 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000856 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600857 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000858 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100859 }
Andy Green6ed25972008-06-13 16:24:05 +0100860
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200861 return 0;
862}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000863EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200864#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000865#define snd_soc_suspend NULL
866#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200867#endif
868
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100869static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800870};
871
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100872static struct snd_soc_codec *soc_find_codec(const struct device_node *codec_of_node,
873 const char *codec_name)
874{
875 struct snd_soc_codec *codec;
876
877 list_for_each_entry(codec, &codec_list, list) {
878 if (codec_of_node) {
879 if (codec->dev->of_node != codec_of_node)
880 continue;
881 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200882 if (strcmp(codec->component.name, codec_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100883 continue;
884 }
885
886 return codec;
887 }
888
889 return NULL;
890}
891
892static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
893 const char *codec_dai_name)
894{
895 struct snd_soc_dai *codec_dai;
896
897 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
898 if (!strcmp(codec_dai->name, codec_dai_name)) {
899 return codec_dai;
900 }
901 }
902
903 return NULL;
904}
905
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000906static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200907{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000908 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
909 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100910 struct snd_soc_component *component;
Mark Brown435c5e22008-12-04 15:32:53 +0000911 struct snd_soc_platform *platform;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100912 struct snd_soc_dai *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100913 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200914
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000915 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000916
Mark Brownb19e6e72012-03-14 21:18:39 +0000917 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100918 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600919 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100920 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600921 continue;
922 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100923 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600924 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100925 list_for_each_entry(cpu_dai, &component->dai_list, list) {
926 if (dai_link->cpu_dai_name &&
927 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
928 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700929
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100930 rtd->cpu_dai = cpu_dai;
931 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000932 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000933
934 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000935 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000936 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000937 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000938 }
939
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100940 /* Find CODEC from registered list */
941 rtd->codec = soc_find_codec(dai_link->codec_of_node,
942 dai_link->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000943 if (!rtd->codec) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000944 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
Mark Brownb19e6e72012-03-14 21:18:39 +0000945 dai_link->codec_name);
946 return -EPROBE_DEFER;
947 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100948
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100949 /* Find CODEC DAI from registered list */
950 rtd->codec_dai = soc_find_codec_dai(rtd->codec,
951 dai_link->codec_dai_name);
952 if (!rtd->codec_dai) {
953 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
954 dai_link->codec_dai_name);
955 return -EPROBE_DEFER;
956 }
957
Mark Brown848dd8b2011-04-27 18:16:32 +0100958 /* if there's no platform we match on the empty platform */
959 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700960 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100961 platform_name = "snd-soc-dummy";
962
Mark Brownb19e6e72012-03-14 21:18:39 +0000963 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000964 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700965 if (dai_link->platform_of_node) {
966 if (platform->dev->of_node !=
967 dai_link->platform_of_node)
968 continue;
969 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200970 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700971 continue;
972 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700973
974 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000975 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000976 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000977 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000978 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000979 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000980 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000981
982 card->num_rtd++;
983
984 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000985}
986
Stephen Warrend12cd192012-06-08 12:34:22 -0600987static int soc_remove_platform(struct snd_soc_platform *platform)
988{
989 int ret;
990
991 if (platform->driver->remove) {
992 ret = platform->driver->remove(platform);
993 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000994 dev_err(platform->dev, "ASoC: failed to remove %d\n",
995 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -0600996 }
997
998 /* Make sure all DAPM widgets are freed */
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +0200999 snd_soc_dapm_free(&platform->component.dapm);
Stephen Warrend12cd192012-06-08 12:34:22 -06001000
1001 soc_cleanup_platform_debugfs(platform);
1002 platform->probed = 0;
1003 list_del(&platform->card_list);
1004 module_put(platform->dev->driver->owner);
1005
1006 return 0;
1007}
1008
Jarkko Nikula589c3562010-12-06 16:27:07 +02001009static void soc_remove_codec(struct snd_soc_codec *codec)
1010{
1011 int err;
1012
1013 if (codec->driver->remove) {
1014 err = codec->driver->remove(codec);
1015 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001016 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001017 }
1018
1019 /* Make sure all DAPM widgets are freed */
1020 snd_soc_dapm_free(&codec->dapm);
1021
1022 soc_cleanup_codec_debugfs(codec);
1023 codec->probed = 0;
1024 list_del(&codec->card_list);
1025 module_put(codec->dev->driver->owner);
1026}
1027
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001028static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1029{
1030 int err;
1031
1032 if (codec_dai && codec_dai->probed &&
1033 codec_dai->driver->remove_order == order) {
1034 if (codec_dai->driver->remove) {
1035 err = codec_dai->driver->remove(codec_dai);
1036 if (err < 0)
1037 dev_err(codec_dai->dev,
1038 "ASoC: failed to remove %s: %d\n",
1039 codec_dai->name, err);
1040 }
1041 codec_dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001042 }
1043}
1044
Stephen Warren62ae68f2012-06-08 12:34:23 -06001045static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001046{
1047 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001048 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1049 int err;
1050
1051 /* unregister the rtd device */
1052 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001053 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1054 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1055 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001056 rtd->dev_registered = 0;
1057 }
1058
1059 /* remove the CODEC DAI */
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001060 soc_remove_codec_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001061
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001062 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001063 if (cpu_dai && cpu_dai->probed &&
1064 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001065 if (cpu_dai->driver->remove) {
1066 err = cpu_dai->driver->remove(cpu_dai);
1067 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001068 dev_err(cpu_dai->dev,
1069 "ASoC: failed to remove %s: %d\n",
1070 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001071 }
1072 cpu_dai->probed = 0;
Lars-Peter Clausen9420d972014-06-16 18:13:10 +02001073 if (!cpu_dai->codec)
Stephen Warrena9db7db2012-06-08 12:34:20 -06001074 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001075 }
1076}
1077
Stephen Warren62ae68f2012-06-08 12:34:23 -06001078static void soc_remove_link_components(struct snd_soc_card *card, int num,
1079 int order)
1080{
1081 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1082 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1083 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1084 struct snd_soc_platform *platform = rtd->platform;
1085 struct snd_soc_codec *codec;
1086
1087 /* remove the platform */
1088 if (platform && platform->probed &&
1089 platform->driver->remove_order == order) {
1090 soc_remove_platform(platform);
1091 }
1092
1093 /* remove the CODEC-side CODEC */
1094 if (codec_dai) {
1095 codec = codec_dai->codec;
1096 if (codec && codec->probed &&
1097 codec->driver->remove_order == order)
1098 soc_remove_codec(codec);
1099 }
1100
1101 /* remove any CPU-side CODEC */
1102 if (cpu_dai) {
1103 codec = cpu_dai->codec;
1104 if (codec && codec->probed &&
1105 codec->driver->remove_order == order)
1106 soc_remove_codec(codec);
1107 }
1108}
1109
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001110static void soc_remove_dai_links(struct snd_soc_card *card)
1111{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001112 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001113
Liam Girdwood0168bf02011-06-07 16:08:05 +01001114 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1115 order++) {
1116 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001117 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001118 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001119
1120 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1121 order++) {
1122 for (dai = 0; dai < card->num_rtd; dai++)
1123 soc_remove_link_components(card, dai, order);
1124 }
1125
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001126 card->num_rtd = 0;
1127}
1128
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001129static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001130 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001131{
1132 int i;
1133
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001134 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001135 return;
1136
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001137 for (i = 0; i < card->num_configs; i++) {
1138 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001139 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001140 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001141 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001142 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001143 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001144 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001145 }
1146}
1147
Jarkko Nikula589c3562010-12-06 16:27:07 +02001148static int soc_probe_codec(struct snd_soc_card *card,
1149 struct snd_soc_codec *codec)
1150{
1151 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001152 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001153 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001154
1155 codec->card = card;
1156 codec->dapm.card = card;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001157 soc_set_name_prefix(card, &codec->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001158
Jarkko Nikula70d29332011-01-27 16:24:22 +02001159 if (!try_module_get(codec->dev->driver->owner))
1160 return -ENODEV;
1161
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001162 soc_init_codec_debugfs(codec);
1163
Nariman Poushinb318ad52014-04-01 13:59:33 +01001164 if (driver->dapm_widgets) {
1165 ret = snd_soc_dapm_new_controls(&codec->dapm,
1166 driver->dapm_widgets,
1167 driver->num_dapm_widgets);
1168
1169 if (ret != 0) {
1170 dev_err(codec->dev,
1171 "Failed to create new controls %d\n", ret);
1172 goto err_probe;
1173 }
1174 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001175
Mark Brown888df392012-02-16 19:37:51 -08001176 /* Create DAPM widgets for each DAI stream */
Nariman Poushin261edc72014-03-31 15:47:12 +01001177 list_for_each_entry(dai, &codec->component.dai_list, list) {
1178 ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1179
1180 if (ret != 0) {
1181 dev_err(codec->dev,
1182 "Failed to create DAI widgets %d\n", ret);
1183 goto err_probe;
1184 }
1185 }
Mark Brown888df392012-02-16 19:37:51 -08001186
Mark Brown33c5f962011-08-22 18:40:30 +01001187 codec->dapm.idle_bias_off = driver->idle_bias_off;
1188
Mark Brown89b95ac02011-03-07 16:38:44 +00001189 if (driver->probe) {
1190 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001191 if (ret < 0) {
1192 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001193 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001194 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001195 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001196 WARN(codec->dapm.idle_bias_off &&
1197 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001198 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02001199 codec->component.name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001200 }
1201
Mark Brownb7af1da2011-04-07 19:18:44 +09001202 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001203 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001204 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001205 if (driver->dapm_routes)
1206 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1207 driver->num_dapm_routes);
1208
Jarkko Nikula589c3562010-12-06 16:27:07 +02001209 /* mark codec as probed and add to card codec list */
1210 codec->probed = 1;
1211 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001212 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001213
Jarkko Nikula70d29332011-01-27 16:24:22 +02001214 return 0;
1215
1216err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001217 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001218 module_put(codec->dev->driver->owner);
1219
Jarkko Nikula589c3562010-12-06 16:27:07 +02001220 return ret;
1221}
1222
Liam Girdwood956245e2011-07-01 16:54:08 +01001223static int soc_probe_platform(struct snd_soc_card *card,
1224 struct snd_soc_platform *platform)
1225{
1226 int ret = 0;
1227 const struct snd_soc_platform_driver *driver = platform->driver;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001228 struct snd_soc_component *component;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001229 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001230
1231 platform->card = card;
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001232 platform->component.dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001233
1234 if (!try_module_get(platform->dev->driver->owner))
1235 return -ENODEV;
1236
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001237 soc_init_platform_debugfs(platform);
1238
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001239 if (driver->dapm_widgets)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001240 snd_soc_dapm_new_controls(&platform->component.dapm,
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001241 driver->dapm_widgets, driver->num_dapm_widgets);
1242
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001243 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001244 list_for_each_entry(component, &component_list, list) {
1245 if (component->dev != platform->dev)
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001246 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001247 list_for_each_entry(dai, &component->dai_list, list)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001248 snd_soc_dapm_new_dai_widgets(&platform->component.dapm,
1249 dai);
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001250 }
1251
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001252 platform->component.dapm.idle_bias_off = 1;
Stephen Warren3fec6b62012-04-05 12:28:01 -06001253
Liam Girdwood956245e2011-07-01 16:54:08 +01001254 if (driver->probe) {
1255 ret = driver->probe(platform);
1256 if (ret < 0) {
1257 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001258 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001259 goto err_probe;
1260 }
1261 }
1262
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001263 if (driver->controls)
1264 snd_soc_add_platform_controls(platform, driver->controls,
1265 driver->num_controls);
1266 if (driver->dapm_routes)
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001267 snd_soc_dapm_add_routes(&platform->component.dapm,
1268 driver->dapm_routes, driver->num_dapm_routes);
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001269
Liam Girdwood956245e2011-07-01 16:54:08 +01001270 /* mark platform as probed and add to card platform list */
1271 platform->probed = 1;
1272 list_add(&platform->card_list, &card->platform_dev_list);
Lars-Peter Clausenbc9af9f2014-06-16 18:13:07 +02001273 list_add(&platform->component.dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001274
1275 return 0;
1276
1277err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001278 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001279 module_put(platform->dev->driver->owner);
1280
1281 return ret;
1282}
1283
Mark Brown36ae1a92012-01-06 17:12:45 -08001284static void rtd_release(struct device *dev)
1285{
1286 kfree(dev);
1287}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001288
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001289static int soc_aux_dev_init(struct snd_soc_card *card,
1290 struct snd_soc_codec *codec,
1291 int num)
1292{
1293 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1294 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001295 int ret;
1296
1297 rtd->card = card;
1298
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001299 /* do machine specific initialization */
1300 if (aux_dev->init) {
1301 ret = aux_dev->init(&codec->dapm);
1302 if (ret < 0)
1303 return ret;
1304 }
1305
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001306 rtd->codec = codec;
1307
1308 return 0;
1309}
1310
Lars-Peter Clausenb8257be2014-07-01 22:13:45 +02001311static int soc_dai_link_init(struct snd_soc_card *card, int num)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001312{
1313 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1314 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001315 int ret;
1316
1317 rtd->card = card;
1318
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001319 /* do machine specific initialization */
1320 if (dai_link->init) {
1321 ret = dai_link->init(rtd);
1322 if (ret < 0)
1323 return ret;
1324 }
1325
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001326 return 0;
1327}
1328
Jarkko Nikula589c3562010-12-06 16:27:07 +02001329static int soc_post_component_init(struct snd_soc_card *card,
1330 struct snd_soc_codec *codec,
1331 int num, int dailess)
1332{
1333 struct snd_soc_dai_link *dai_link = NULL;
1334 struct snd_soc_aux_dev *aux_dev = NULL;
1335 struct snd_soc_pcm_runtime *rtd;
Lars-Peter Clausen6479f15ad2014-03-12 15:27:40 +01001336 const char *name;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001337 int ret = 0;
1338
1339 if (!dailess) {
1340 dai_link = &card->dai_link[num];
1341 rtd = &card->rtd[num];
1342 name = dai_link->name;
Lars-Peter Clausenb8257be2014-07-01 22:13:45 +02001343 ret = soc_dai_link_init(card, num);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001344 } else {
1345 aux_dev = &card->aux_dev[num];
1346 rtd = &card->rtd_aux[num];
1347 name = aux_dev->name;
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001348 ret = soc_aux_dev_init(card, codec, num);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001349 }
1350
Jarkko Nikula589c3562010-12-06 16:27:07 +02001351 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001352 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001353 return ret;
1354 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001355
Jarkko Nikula589c3562010-12-06 16:27:07 +02001356 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001357 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1358 if (!rtd->dev)
1359 return -ENOMEM;
1360 device_initialize(rtd->dev);
1361 rtd->dev->parent = card->dev;
1362 rtd->dev->release = rtd_release;
1363 rtd->dev->init_name = name;
1364 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001365 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001366 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1367 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1368 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1369 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001370 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001371 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001372 /* calling put_device() here to free the rtd->dev */
1373 put_device(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001374 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001375 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001376 return ret;
1377 }
1378 rtd->dev_registered = 1;
1379
1380 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001381 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001382 if (ret < 0)
1383 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001384 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001385
1386 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001387 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001388 if (ret < 0)
1389 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001390 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001391
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001392#ifdef CONFIG_DEBUG_FS
1393 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001394 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001395 goto out;
1396
1397 ret = soc_dpcm_debugfs_add(rtd);
1398 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001399 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001400
1401out:
1402#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001403 return 0;
1404}
1405
Stephen Warren62ae68f2012-06-08 12:34:23 -06001406static int soc_probe_link_components(struct snd_soc_card *card, int num,
1407 int order)
1408{
1409 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1410 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1411 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1412 struct snd_soc_platform *platform = rtd->platform;
1413 int ret;
1414
1415 /* probe the CPU-side component, if it is a CODEC */
1416 if (cpu_dai->codec &&
1417 !cpu_dai->codec->probed &&
1418 cpu_dai->codec->driver->probe_order == order) {
1419 ret = soc_probe_codec(card, cpu_dai->codec);
1420 if (ret < 0)
1421 return ret;
1422 }
1423
1424 /* probe the CODEC-side component */
1425 if (!codec_dai->codec->probed &&
1426 codec_dai->codec->driver->probe_order == order) {
1427 ret = soc_probe_codec(card, codec_dai->codec);
1428 if (ret < 0)
1429 return ret;
1430 }
1431
1432 /* probe the platform */
1433 if (!platform->probed &&
1434 platform->driver->probe_order == order) {
1435 ret = soc_probe_platform(card, platform);
1436 if (ret < 0)
1437 return ret;
1438 }
1439
1440 return 0;
1441}
1442
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001443static int soc_probe_codec_dai(struct snd_soc_card *card,
1444 struct snd_soc_dai *codec_dai,
1445 int order)
1446{
1447 int ret;
1448
1449 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1450 if (codec_dai->driver->probe) {
1451 ret = codec_dai->driver->probe(codec_dai);
1452 if (ret < 0) {
1453 dev_err(codec_dai->dev,
1454 "ASoC: failed to probe CODEC DAI %s: %d\n",
1455 codec_dai->name, ret);
1456 return ret;
1457 }
1458 }
1459
1460 /* mark codec_dai as probed and add to card dai list */
1461 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001462 }
1463
1464 return 0;
1465}
1466
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001467static int soc_link_dai_widgets(struct snd_soc_card *card,
1468 struct snd_soc_dai_link *dai_link,
1469 struct snd_soc_dai *cpu_dai,
1470 struct snd_soc_dai *codec_dai)
1471{
1472 struct snd_soc_dapm_widget *play_w, *capture_w;
1473 int ret;
1474
1475 /* link the DAI widgets */
1476 play_w = codec_dai->playback_widget;
1477 capture_w = cpu_dai->capture_widget;
1478 if (play_w && capture_w) {
1479 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1480 capture_w, play_w);
1481 if (ret != 0) {
1482 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1483 play_w->name, capture_w->name, ret);
1484 return ret;
1485 }
1486 }
1487
1488 play_w = cpu_dai->playback_widget;
1489 capture_w = codec_dai->capture_widget;
1490 if (play_w && capture_w) {
1491 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1492 capture_w, play_w);
1493 if (ret != 0) {
1494 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1495 play_w->name, capture_w->name, ret);
1496 return ret;
1497 }
1498 }
1499
1500 return 0;
1501}
1502
Stephen Warren62ae68f2012-06-08 12:34:23 -06001503static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001504{
1505 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1506 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1507 struct snd_soc_codec *codec = rtd->codec;
1508 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001509 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1510 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001511 int ret;
1512
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001513 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001514 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001515
1516 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001517 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001518 codec_dai->card = card;
1519 cpu_dai->card = card;
1520
1521 /* set default power off timeout */
1522 rtd->pmdown_time = pmdown_time;
1523
1524 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001525 if (!cpu_dai->probed &&
1526 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001527 if (!cpu_dai->codec) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001528 if (!try_module_get(cpu_dai->dev->driver->owner))
1529 return -ENODEV;
Stephen Warrena9db7db2012-06-08 12:34:20 -06001530 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001531
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001532 if (cpu_dai->driver->probe) {
1533 ret = cpu_dai->driver->probe(cpu_dai);
1534 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001535 dev_err(cpu_dai->dev,
1536 "ASoC: failed to probe CPU DAI %s: %d\n",
1537 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001538 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001539 return ret;
1540 }
1541 }
1542 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001543 }
1544
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001545 /* probe the CODEC DAI */
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001546 ret = soc_probe_codec_dai(card, codec_dai, order);
1547 if (ret)
1548 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001549
Liam Girdwood0168bf02011-06-07 16:08:05 +01001550 /* complete DAI probe during last probe */
1551 if (order != SND_SOC_COMP_ORDER_LAST)
1552 return 0;
1553
Jarkko Nikula589c3562010-12-06 16:27:07 +02001554 ret = soc_post_component_init(card, codec, num, 0);
1555 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001556 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001557
Mark Brown36ae1a92012-01-06 17:12:45 -08001558 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001559 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001560 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1561 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001562
Namarta Kohli1245b702012-08-16 17:10:41 +05301563 if (cpu_dai->driver->compress_dai) {
1564 /*create compress_device"*/
1565 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001566 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001567 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301568 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001569 return ret;
1570 }
1571 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301572
1573 if (!dai_link->params) {
1574 /* create the pcm */
1575 ret = soc_new_pcm(rtd, num);
1576 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001577 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301578 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001579 return ret;
1580 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301581 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001582 INIT_DELAYED_WORK(&rtd->delayed_work,
1583 codec2codec_close_delayed_work);
1584
Namarta Kohli1245b702012-08-16 17:10:41 +05301585 /* link the DAI widgets */
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001586 ret = soc_link_dai_widgets(card, dai_link,
1587 cpu_dai, codec_dai);
1588 if (ret)
1589 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001590 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001591 }
1592
1593 /* add platform data for AC97 devices */
1594 if (rtd->codec_dai->driver->ac97_control)
1595 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1596
1597 return 0;
1598}
1599
1600#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001601static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1602 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001603{
1604 int ret;
1605
1606 /* Only instantiate AC97 if not already done by the adaptor
1607 * for the generic AC97 subsystem.
1608 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001609 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001610 /*
1611 * It is possible that the AC97 device is already registered to
1612 * the device subsystem. This happens when the device is created
1613 * via snd_ac97_mixer(). Currently only SoC codec that does so
1614 * is the generic AC97 glue but others migh emerge.
1615 *
1616 * In those cases we don't try to register the device again.
1617 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001618 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001619 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001620
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001621 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001622 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001623 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001624 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001625 return ret;
1626 }
1627
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001628 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001629 }
1630 return 0;
1631}
1632
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001633static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1634{
1635 return soc_register_ac97_codec(rtd->codec, rtd->codec_dai);
1636}
1637
1638static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001639{
1640 if (codec->ac97_registered) {
1641 soc_ac97_dev_unregister(codec);
1642 codec->ac97_registered = 0;
1643 }
1644}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001645
1646static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1647{
1648 soc_unregister_ac97_codec(rtd->codec);
1649}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001650#endif
1651
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001652static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1653{
1654 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1655 const char *codecname = aux_dev->codec_name;
Lars-Peter Clausen48f466d2014-07-01 22:13:46 +02001656 struct snd_soc_codec *codec;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001657
Lars-Peter Clausen48f466d2014-07-01 22:13:46 +02001658 codec = soc_find_codec(aux_dev->codec_of_node, aux_dev->codec_name);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001659 if (codec)
1660 return 0;
1661 if (aux_dev->codec_of_node)
1662 codecname = of_node_full_name(aux_dev->codec_of_node);
1663
1664 dev_err(card->dev, "ASoC: %s not registered\n", codecname);
Mark Brownb19e6e72012-03-14 21:18:39 +00001665 return -EPROBE_DEFER;
1666}
1667
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001668static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1669{
1670 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001671 const char *codecname = aux_dev->codec_name;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001672 int ret = -ENODEV;
Lars-Peter Clausen48f466d2014-07-01 22:13:46 +02001673 struct snd_soc_codec *codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001674
Lars-Peter Clausen48f466d2014-07-01 22:13:46 +02001675 codec = soc_find_codec(aux_dev->codec_of_node, aux_dev->codec_name);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001676 if (!codec) {
1677 if (aux_dev->codec_of_node)
1678 codecname = of_node_full_name(aux_dev->codec_of_node);
1679
1680 /* codec not found */
1681 dev_err(card->dev, "ASoC: codec %s not found", codecname);
1682 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001683 }
1684
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001685 if (codec->probed) {
1686 dev_err(codec->dev, "ASoC: codec already probed");
1687 return -EBUSY;
1688 }
1689
Jarkko Nikula589c3562010-12-06 16:27:07 +02001690 ret = soc_probe_codec(card, codec);
1691 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001692 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001693
Jarkko Nikula589c3562010-12-06 16:27:07 +02001694 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001695
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001696 return ret;
1697}
1698
1699static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1700{
1701 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1702 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001703
1704 /* unregister the rtd device */
1705 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001706 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001707 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001708 rtd->dev_registered = 0;
1709 }
1710
Jarkko Nikula589c3562010-12-06 16:27:07 +02001711 if (codec && codec->probed)
1712 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001713}
1714
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001715static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001716{
1717 int ret;
1718
1719 if (codec->cache_init)
1720 return 0;
1721
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001722 ret = snd_soc_cache_init(codec);
1723 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001724 dev_err(codec->dev,
1725 "ASoC: Failed to set cache compression type: %d\n",
1726 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001727 return ret;
1728 }
1729 codec->cache_init = 1;
1730 return 0;
1731}
1732
Mark Brownb19e6e72012-03-14 21:18:39 +00001733static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001734{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001735 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001736 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001737 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001738
Liam Girdwood01b9d992012-03-07 10:38:25 +00001739 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001740
Mark Brownb19e6e72012-03-14 21:18:39 +00001741 /* bind DAIs */
1742 for (i = 0; i < card->num_links; i++) {
1743 ret = soc_bind_dai_link(card, i);
1744 if (ret != 0)
1745 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001746 }
1747
Mark Brownb19e6e72012-03-14 21:18:39 +00001748 /* check aux_devs too */
1749 for (i = 0; i < card->num_aux_devs; i++) {
1750 ret = soc_check_aux_dev(card, i);
1751 if (ret != 0)
1752 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001753 }
1754
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001755 /* initialize the register cache for each available codec */
1756 list_for_each_entry(codec, &codec_list, list) {
1757 if (codec->cache_init)
1758 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001759 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001760 if (ret < 0)
1761 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001762 }
1763
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001764 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001765 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001766 card->owner, 0, &card->snd_card);
1767 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001768 dev_err(card->dev,
1769 "ASoC: can't create sound card for card %s: %d\n",
1770 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001771 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001772 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001773
Mark Browne37a4972011-03-02 18:21:57 +00001774 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1775 card->dapm.dev = card->dev;
1776 card->dapm.card = card;
1777 list_add(&card->dapm.list, &card->dapm_list);
1778
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001779#ifdef CONFIG_DEBUG_FS
1780 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1781#endif
1782
Mark Brown88ee1c62011-02-02 10:43:26 +00001783#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001784 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001785 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001786#endif
Andy Green6ed25972008-06-13 16:24:05 +01001787
Mark Brown9a841eb2011-04-12 17:51:37 -07001788 if (card->dapm_widgets)
1789 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1790 card->num_dapm_widgets);
1791
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001792 /* initialise the sound card only once */
1793 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001794 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001795 if (ret < 0)
1796 goto card_probe_error;
1797 }
1798
Stephen Warren62ae68f2012-06-08 12:34:23 -06001799 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001800 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1801 order++) {
1802 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001803 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001804 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001805 dev_err(card->dev,
1806 "ASoC: failed to instantiate card %d\n",
1807 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001808 goto probe_dai_err;
1809 }
1810 }
1811 }
1812
1813 /* probe all DAI links on this card */
1814 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1815 order++) {
1816 for (i = 0; i < card->num_links; i++) {
1817 ret = soc_probe_link_dais(card, i, order);
1818 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001819 dev_err(card->dev,
1820 "ASoC: failed to instantiate card %d\n",
1821 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001822 goto probe_dai_err;
1823 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001824 }
1825 }
1826
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001827 for (i = 0; i < card->num_aux_devs; i++) {
1828 ret = soc_probe_aux_dev(card, i);
1829 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001830 dev_err(card->dev,
1831 "ASoC: failed to add auxiliary devices %d\n",
1832 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001833 goto probe_aux_dev_err;
1834 }
1835 }
1836
Mark Brown888df392012-02-16 19:37:51 -08001837 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001838 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001839
Mark Brownb7af1da2011-04-07 19:18:44 +09001840 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001841 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001842
Mark Brownb8ad29d2011-03-02 18:35:51 +00001843 if (card->dapm_routes)
1844 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1845 card->num_dapm_routes);
1846
Mark Brown75d9ac42011-09-27 16:41:01 +01001847 for (i = 0; i < card->num_links; i++) {
1848 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001849 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001850
Mark Brownf04209a2012-04-09 16:29:21 +01001851 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001852 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001853 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001854 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001855 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001856 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001857 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001858 }
1859
1860 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001861 if (dai_fmt &&
1862 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001863 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1864 dai_fmt);
1865 if (ret != 0 && ret != -ENOTSUPP)
1866 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001867 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001868 ret);
1869 } else if (dai_fmt) {
1870 /* Flip the polarity for the "CPU" end */
1871 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1872 switch (dai_link->dai_fmt &
1873 SND_SOC_DAIFMT_MASTER_MASK) {
1874 case SND_SOC_DAIFMT_CBM_CFM:
1875 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1876 break;
1877 case SND_SOC_DAIFMT_CBM_CFS:
1878 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1879 break;
1880 case SND_SOC_DAIFMT_CBS_CFM:
1881 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1882 break;
1883 case SND_SOC_DAIFMT_CBS_CFS:
1884 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1885 break;
1886 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001887
1888 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001889 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001890 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001891 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001892 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001893 ret);
1894 }
1895 }
1896
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001897 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001898 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001899 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1900 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001901 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1902 "%s", card->driver_name ? card->driver_name : card->name);
1903 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1904 switch (card->snd_card->driver[i]) {
1905 case '_':
1906 case '-':
1907 case '\0':
1908 break;
1909 default:
1910 if (!isalnum(card->snd_card->driver[i]))
1911 card->snd_card->driver[i] = '_';
1912 break;
1913 }
1914 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001915
Mark Brown28e9ad92011-03-02 18:36:34 +00001916 if (card->late_probe) {
1917 ret = card->late_probe(card);
1918 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001919 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001920 card->name, ret);
1921 goto probe_aux_dev_err;
1922 }
1923 }
1924
Stephen Warren16332812011-11-23 12:42:04 -07001925 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001926 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001927
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001928 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001929
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001930 ret = snd_card_register(card->snd_card);
1931 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001932 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1933 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001934 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001935 }
1936
1937#ifdef CONFIG_SND_SOC_AC97_BUS
1938 /* register any AC97 codecs */
1939 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001940 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1941 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001942 dev_err(card->dev,
1943 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001944 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001945 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001946 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001947 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001948 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001949#endif
1950
Mark Brown435c5e22008-12-04 15:32:53 +00001951 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001952 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001953 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001954
1955 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001956
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001957probe_aux_dev_err:
1958 for (i = 0; i < card->num_aux_devs; i++)
1959 soc_remove_aux_dev(card, i);
1960
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001961probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001962 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001963
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001964card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001965 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001966 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001967
1968 snd_card_free(card->snd_card);
1969
Mark Brownb19e6e72012-03-14 21:18:39 +00001970base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001971 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001972
Mark Brownb19e6e72012-03-14 21:18:39 +00001973 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001974}
1975
1976/* probes a new socdev */
1977static int soc_probe(struct platform_device *pdev)
1978{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001979 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001980
Vinod Koul70a7ca32011-01-14 19:22:48 +05301981 /*
1982 * no card, so machine driver should be registering card
1983 * we should not be here in that case so ret error
1984 */
1985 if (!card)
1986 return -EINVAL;
1987
Mark Brownfe4085e2012-03-02 13:07:41 +00001988 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001989 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001990 card->name);
1991
Mark Brown435c5e22008-12-04 15:32:53 +00001992 /* Bodge while we unpick instantiation */
1993 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001994
Mark Brown28d528c2012-08-09 18:45:23 +01001995 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001996}
1997
Vinod Koulb0e26482011-01-13 22:48:02 +05301998static int soc_cleanup_card_resources(struct snd_soc_card *card)
1999{
Vinod Koulb0e26482011-01-13 22:48:02 +05302000 int i;
2001
2002 /* make sure any delayed work runs */
2003 for (i = 0; i < card->num_rtd; i++) {
2004 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002005 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302006 }
2007
2008 /* remove auxiliary devices */
2009 for (i = 0; i < card->num_aux_devs; i++)
2010 soc_remove_aux_dev(card, i);
2011
2012 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002013 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302014
2015 soc_cleanup_card_debugfs(card);
2016
2017 /* remove the card */
2018 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002019 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302020
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02002021 snd_soc_dapm_free(&card->dapm);
2022
Vinod Koulb0e26482011-01-13 22:48:02 +05302023 snd_card_free(card->snd_card);
2024 return 0;
2025
2026}
2027
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002028/* removes a socdev */
2029static int soc_remove(struct platform_device *pdev)
2030{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002031 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002032
Mark Brownc5af3a22008-11-28 13:29:45 +00002033 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002034 return 0;
2035}
2036
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002037int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002038{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002039 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002040 int i;
Mark Brown51737472009-06-22 13:16:51 +01002041
2042 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002043 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002044
2045 /* Flush out pmdown_time work - we actually do want to run it
2046 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002047 for (i = 0; i < card->num_rtd; i++) {
2048 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002049 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002050 }
Mark Brown51737472009-06-22 13:16:51 +01002051
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002052 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002053
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002054 /* deactivate pins to sleep state */
2055 for (i = 0; i < card->num_rtd; i++) {
2056 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
2057 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
2058 pinctrl_pm_select_sleep_state(codec_dai->dev);
2059 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2060 }
2061
Mark Brown416356f2009-06-30 19:05:15 +01002062 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002063}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002064EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002065
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002066const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302067 .suspend = snd_soc_suspend,
2068 .resume = snd_soc_resume,
2069 .freeze = snd_soc_suspend,
2070 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002071 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302072 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002073};
Stephen Warrendeb26072011-04-05 19:35:30 -06002074EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002075
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002076/* ASoC platform driver */
2077static struct platform_driver soc_driver = {
2078 .driver = {
2079 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002080 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002081 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002082 },
2083 .probe = soc_probe,
2084 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002085};
2086
Mark Brown096e49d2009-07-05 15:12:22 +01002087/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002088 * snd_soc_new_ac97_codec - initailise AC97 device
2089 * @codec: audio codec
2090 * @ops: AC97 bus operations
2091 * @num: AC97 codec number
2092 *
2093 * Initialises AC97 codec resources for use by ad-hoc devices only.
2094 */
2095int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2096 struct snd_ac97_bus_ops *ops, int num)
2097{
2098 mutex_lock(&codec->mutex);
2099
2100 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2101 if (codec->ac97 == NULL) {
2102 mutex_unlock(&codec->mutex);
2103 return -ENOMEM;
2104 }
2105
2106 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2107 if (codec->ac97->bus == NULL) {
2108 kfree(codec->ac97);
2109 codec->ac97 = NULL;
2110 mutex_unlock(&codec->mutex);
2111 return -ENOMEM;
2112 }
2113
2114 codec->ac97->bus->ops = ops;
2115 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002116
2117 /*
2118 * Mark the AC97 device to be created by us. This way we ensure that the
2119 * device will be registered with the device subsystem later on.
2120 */
2121 codec->ac97_created = 1;
2122
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002123 mutex_unlock(&codec->mutex);
2124 return 0;
2125}
2126EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2127
Markus Pargmann741a5092013-08-19 17:05:55 +02002128static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2129
2130static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2131{
2132 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2133
2134 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2135
2136 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2137
2138 udelay(10);
2139
2140 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2141
2142 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2143 msleep(2);
2144}
2145
2146static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2147{
2148 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2149
2150 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2151
2152 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2153 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2154 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2155
2156 udelay(10);
2157
2158 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2159
2160 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2161 msleep(2);
2162}
2163
2164static int snd_soc_ac97_parse_pinctl(struct device *dev,
2165 struct snd_ac97_reset_cfg *cfg)
2166{
2167 struct pinctrl *p;
2168 struct pinctrl_state *state;
2169 int gpio;
2170 int ret;
2171
2172 p = devm_pinctrl_get(dev);
2173 if (IS_ERR(p)) {
2174 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002175 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002176 }
2177 cfg->pctl = p;
2178
2179 state = pinctrl_lookup_state(p, "ac97-reset");
2180 if (IS_ERR(state)) {
2181 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002182 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002183 }
2184 cfg->pstate_reset = state;
2185
2186 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2187 if (IS_ERR(state)) {
2188 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002189 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002190 }
2191 cfg->pstate_warm_reset = state;
2192
2193 state = pinctrl_lookup_state(p, "ac97-running");
2194 if (IS_ERR(state)) {
2195 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002196 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002197 }
2198 cfg->pstate_run = state;
2199
2200 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2201 if (gpio < 0) {
2202 dev_err(dev, "Can't find ac97-sync gpio\n");
2203 return gpio;
2204 }
2205 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2206 if (ret) {
2207 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2208 return ret;
2209 }
2210 cfg->gpio_sync = gpio;
2211
2212 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2213 if (gpio < 0) {
2214 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2215 return gpio;
2216 }
2217 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2218 if (ret) {
2219 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2220 return ret;
2221 }
2222 cfg->gpio_sdata = gpio;
2223
2224 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2225 if (gpio < 0) {
2226 dev_err(dev, "Can't find ac97-reset gpio\n");
2227 return gpio;
2228 }
2229 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2230 if (ret) {
2231 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2232 return ret;
2233 }
2234 cfg->gpio_reset = gpio;
2235
2236 return 0;
2237}
2238
Mark Brownb047e1c2013-06-26 12:45:59 +01002239struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002240EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002241
2242int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2243{
2244 if (ops == soc_ac97_ops)
2245 return 0;
2246
2247 if (soc_ac97_ops && ops)
2248 return -EBUSY;
2249
2250 soc_ac97_ops = ops;
2251
2252 return 0;
2253}
2254EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2255
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002256/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002257 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2258 *
2259 * This function sets the reset and warm_reset properties of ops and parses
2260 * the device node of pdev to get pinctrl states and gpio numbers to use.
2261 */
2262int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2263 struct platform_device *pdev)
2264{
2265 struct device *dev = &pdev->dev;
2266 struct snd_ac97_reset_cfg cfg;
2267 int ret;
2268
2269 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2270 if (ret)
2271 return ret;
2272
2273 ret = snd_soc_set_ac97_ops(ops);
2274 if (ret)
2275 return ret;
2276
2277 ops->warm_reset = snd_soc_ac97_warm_reset;
2278 ops->reset = snd_soc_ac97_reset;
2279
2280 snd_ac97_rst_cfg = cfg;
2281 return 0;
2282}
2283EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2284
2285/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002286 * snd_soc_free_ac97_codec - free AC97 codec device
2287 * @codec: audio codec
2288 *
2289 * Frees AC97 codec device resources.
2290 */
2291void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2292{
2293 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002294#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002295 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002296#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002297 kfree(codec->ac97->bus);
2298 kfree(codec->ac97);
2299 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002300 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002301 mutex_unlock(&codec->mutex);
2302}
2303EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2304
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002305/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002306 * snd_soc_cnew - create new control
2307 * @_template: control template
2308 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002309 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002310 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002311 *
2312 * Create a new mixer control from a template control.
2313 *
2314 * Returns 0 for success, else error.
2315 */
2316struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002317 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002318 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002319{
2320 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002321 struct snd_kcontrol *kcontrol;
2322 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002323
2324 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002325 template.index = 0;
2326
Mark Brownefb7ac32011-03-08 17:23:24 +00002327 if (!long_name)
2328 long_name = template.name;
2329
2330 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002331 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002332 if (!name)
2333 return NULL;
2334
Mark Brownefb7ac32011-03-08 17:23:24 +00002335 template.name = name;
2336 } else {
2337 template.name = long_name;
2338 }
2339
2340 kcontrol = snd_ctl_new1(&template, data);
2341
2342 kfree(name);
2343
2344 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002345}
2346EXPORT_SYMBOL_GPL(snd_soc_cnew);
2347
Liam Girdwood022658b2012-02-03 17:43:09 +00002348static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2349 const struct snd_kcontrol_new *controls, int num_controls,
2350 const char *prefix, void *data)
2351{
2352 int err, i;
2353
2354 for (i = 0; i < num_controls; i++) {
2355 const struct snd_kcontrol_new *control = &controls[i];
2356 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2357 control->name, prefix));
2358 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002359 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2360 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002361 return err;
2362 }
2363 }
2364
2365 return 0;
2366}
2367
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002368struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2369 const char *name)
2370{
2371 struct snd_card *card = soc_card->snd_card;
2372 struct snd_kcontrol *kctl;
2373
2374 if (unlikely(!name))
2375 return NULL;
2376
2377 list_for_each_entry(kctl, &card->controls, list)
2378 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2379 return kctl;
2380 return NULL;
2381}
2382EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2383
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002384/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002385 * snd_soc_add_codec_controls - add an array of controls to a codec.
2386 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002387 * duplicating this code.
2388 *
2389 * @codec: codec to add controls to
2390 * @controls: array of controls to add
2391 * @num_controls: number of elements in the array
2392 *
2393 * Return 0 for success, else error.
2394 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002395int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002396 const struct snd_kcontrol_new *controls, int num_controls)
2397{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002398 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002399
Liam Girdwood022658b2012-02-03 17:43:09 +00002400 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002401 codec->component.name_prefix, &codec->component);
Ian Molton3e8e1952009-01-09 00:23:21 +00002402}
Liam Girdwood022658b2012-02-03 17:43:09 +00002403EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002404
2405/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002406 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002407 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002408 *
2409 * @platform: platform to add controls to
2410 * @controls: array of controls to add
2411 * @num_controls: number of elements in the array
2412 *
2413 * Return 0 for success, else error.
2414 */
2415int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2416 const struct snd_kcontrol_new *controls, int num_controls)
2417{
2418 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002419
Liam Girdwood022658b2012-02-03 17:43:09 +00002420 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002421 NULL, &platform->component);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002422}
2423EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2424
2425/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002426 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2427 * Convenience function to add a list of controls.
2428 *
2429 * @soc_card: SoC card to add controls to
2430 * @controls: array of controls to add
2431 * @num_controls: number of elements in the array
2432 *
2433 * Return 0 for success, else error.
2434 */
2435int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2436 const struct snd_kcontrol_new *controls, int num_controls)
2437{
2438 struct snd_card *card = soc_card->snd_card;
2439
2440 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2441 NULL, soc_card);
2442}
2443EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2444
2445/**
2446 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2447 * Convienience function to add a list of controls.
2448 *
2449 * @dai: DAI to add controls to
2450 * @controls: array of controls to add
2451 * @num_controls: number of elements in the array
2452 *
2453 * Return 0 for success, else error.
2454 */
2455int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2456 const struct snd_kcontrol_new *controls, int num_controls)
2457{
2458 struct snd_card *card = dai->card->snd_card;
2459
2460 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2461 NULL, dai);
2462}
2463EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2464
2465/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002466 * snd_soc_info_enum_double - enumerated double mixer info callback
2467 * @kcontrol: mixer control
2468 * @uinfo: control element information
2469 *
2470 * Callback to provide information about a double enumerated
2471 * mixer control.
2472 *
2473 * Returns 0 for success.
2474 */
2475int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2476 struct snd_ctl_elem_info *uinfo)
2477{
2478 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2479
2480 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2481 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002482 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002483
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002484 if (uinfo->value.enumerated.item >= e->items)
2485 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002486 strlcpy(uinfo->value.enumerated.name,
2487 e->texts[uinfo->value.enumerated.item],
2488 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002489 return 0;
2490}
2491EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2492
2493/**
2494 * snd_soc_get_enum_double - enumerated double mixer get callback
2495 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002496 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002497 *
2498 * Callback to get the value of a double enumerated mixer.
2499 *
2500 * Returns 0 for success.
2501 */
2502int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2503 struct snd_ctl_elem_value *ucontrol)
2504{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002505 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002506 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002507 unsigned int val, item;
2508 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002509 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002510
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002511 ret = snd_soc_component_read(component, e->reg, &reg_val);
2512 if (ret)
2513 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002514 val = (reg_val >> e->shift_l) & e->mask;
2515 item = snd_soc_enum_val_to_item(e, val);
2516 ucontrol->value.enumerated.item[0] = item;
2517 if (e->shift_l != e->shift_r) {
2518 val = (reg_val >> e->shift_l) & e->mask;
2519 item = snd_soc_enum_val_to_item(e, val);
2520 ucontrol->value.enumerated.item[1] = item;
2521 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002522
2523 return 0;
2524}
2525EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2526
2527/**
2528 * snd_soc_put_enum_double - enumerated double mixer put callback
2529 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002530 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002531 *
2532 * Callback to set the value of a double enumerated mixer.
2533 *
2534 * Returns 0 for success.
2535 */
2536int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2537 struct snd_ctl_elem_value *ucontrol)
2538{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002539 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002540 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002541 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002542 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002543 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002544
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002545 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002546 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002547 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002548 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002549 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002550 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002551 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002552 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002553 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002554 }
2555
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002556 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002557}
2558EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2559
2560/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002561 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002562 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002563 * @reg: Register to read
2564 * @mask: Mask to use after shifting the register value
2565 * @shift: Right shift of register value
2566 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002567 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002568 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002569 * This functions reads a codec register. The register value is shifted right
2570 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2571 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002572 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002573 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002574 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002575static int snd_soc_read_signed(struct snd_soc_component *component,
2576 unsigned int reg, unsigned int mask, unsigned int shift,
2577 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002578{
Markus Pargmannf227b882014-01-16 16:02:10 +01002579 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002580 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002581
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002582 ret = snd_soc_component_read(component, reg, &val);
2583 if (ret < 0)
2584 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002585
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002586 val = (val >> shift) & mask;
2587
2588 if (!sign_bit) {
2589 *signed_val = val;
2590 return 0;
2591 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002592
2593 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002594 if (!(val & BIT(sign_bit))) {
2595 *signed_val = val;
2596 return 0;
2597 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002598
2599 ret = val;
2600
2601 /*
2602 * The register most probably does not contain a full-sized int.
2603 * Instead we have an arbitrary number of bits in a signed
2604 * representation which has to be translated into a full-sized int.
2605 * This is done by filling up all bits above the sign-bit.
2606 */
2607 ret |= ~((int)(BIT(sign_bit) - 1));
2608
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002609 *signed_val = ret;
2610
2611 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002612}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002613
2614/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002615 * snd_soc_info_volsw - single mixer info callback
2616 * @kcontrol: mixer control
2617 * @uinfo: control element information
2618 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002619 * Callback to provide information about a single mixer control, or a double
2620 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002621 *
2622 * Returns 0 for success.
2623 */
2624int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2625 struct snd_ctl_elem_info *uinfo)
2626{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002627 struct soc_mixer_control *mc =
2628 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002629 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002630
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002631 if (!mc->platform_max)
2632 mc->platform_max = mc->max;
2633 platform_max = mc->platform_max;
2634
2635 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002636 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2637 else
2638 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2639
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002640 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002641 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002642 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002643 return 0;
2644}
2645EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2646
2647/**
2648 * snd_soc_get_volsw - single mixer get callback
2649 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002650 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002651 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002652 * Callback to get the value of a single mixer control, or a double mixer
2653 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002654 *
2655 * Returns 0 for success.
2656 */
2657int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2658 struct snd_ctl_elem_value *ucontrol)
2659{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002660 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002661 struct soc_mixer_control *mc =
2662 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002663 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002664 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002665 unsigned int shift = mc->shift;
2666 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002667 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002668 int min = mc->min;
2669 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002670 unsigned int mask = (1 << fls(max)) - 1;
2671 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002672 int val;
2673 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002674
Markus Pargmannf227b882014-01-16 16:02:10 +01002675 if (sign_bit)
2676 mask = BIT(sign_bit + 1) - 1;
2677
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002678 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2679 if (ret)
2680 return ret;
2681
2682 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002683 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002684 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002685 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002686
2687 if (snd_soc_volsw_is_stereo(mc)) {
2688 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002689 ret = snd_soc_read_signed(component, reg, mask, rshift,
2690 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002691 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002692 ret = snd_soc_read_signed(component, reg2, mask, shift,
2693 sign_bit, &val);
2694 if (ret)
2695 return ret;
2696
2697 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002698 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002699 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002700 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002701 }
2702
2703 return 0;
2704}
2705EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2706
2707/**
2708 * snd_soc_put_volsw - single mixer put callback
2709 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002710 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002711 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002712 * Callback to set the value of a single mixer control, or a double mixer
2713 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002714 *
2715 * Returns 0 for success.
2716 */
2717int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2718 struct snd_ctl_elem_value *ucontrol)
2719{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002720 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002721 struct soc_mixer_control *mc =
2722 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002723 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002724 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002725 unsigned int shift = mc->shift;
2726 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002727 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002728 int min = mc->min;
2729 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002730 unsigned int mask = (1 << fls(max)) - 1;
2731 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002732 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002733 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002734 unsigned int val2 = 0;
2735 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002736
Markus Pargmannf227b882014-01-16 16:02:10 +01002737 if (sign_bit)
2738 mask = BIT(sign_bit + 1) - 1;
2739
2740 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002741 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002742 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002743 val_mask = mask << shift;
2744 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002745 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002746 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002747 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002748 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002749 if (reg == reg2) {
2750 val_mask |= mask << rshift;
2751 val |= val2 << rshift;
2752 } else {
2753 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002754 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002755 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002756 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002757 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002758 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002759 return err;
2760
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002761 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002762 err = snd_soc_component_update_bits(component, reg2, val_mask,
2763 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002764
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002765 return err;
2766}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002767EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002768
Mark Browne13ac2e2008-05-28 17:58:05 +01002769/**
Brian Austin1d99f242012-03-30 10:43:55 -05002770 * snd_soc_get_volsw_sx - single mixer get callback
2771 * @kcontrol: mixer control
2772 * @ucontrol: control element information
2773 *
2774 * Callback to get the value of a single mixer control, or a double mixer
2775 * control that spans 2 registers.
2776 *
2777 * Returns 0 for success.
2778 */
2779int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2780 struct snd_ctl_elem_value *ucontrol)
2781{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002782 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002783 struct soc_mixer_control *mc =
2784 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002785 unsigned int reg = mc->reg;
2786 unsigned int reg2 = mc->rreg;
2787 unsigned int shift = mc->shift;
2788 unsigned int rshift = mc->rshift;
2789 int max = mc->max;
2790 int min = mc->min;
2791 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002792 unsigned int val;
2793 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002794
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002795 ret = snd_soc_component_read(component, reg, &val);
2796 if (ret < 0)
2797 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002798
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002799 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2800
2801 if (snd_soc_volsw_is_stereo(mc)) {
2802 ret = snd_soc_component_read(component, reg2, &val);
2803 if (ret < 0)
2804 return ret;
2805
2806 val = ((val >> rshift) - min) & mask;
2807 ucontrol->value.integer.value[1] = val;
2808 }
Brian Austin1d99f242012-03-30 10:43:55 -05002809
2810 return 0;
2811}
2812EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2813
2814/**
2815 * snd_soc_put_volsw_sx - double mixer set callback
2816 * @kcontrol: mixer control
2817 * @uinfo: control element information
2818 *
2819 * Callback to set the value of a double mixer control that spans 2 registers.
2820 *
2821 * Returns 0 for success.
2822 */
2823int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2824 struct snd_ctl_elem_value *ucontrol)
2825{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002826 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002827 struct soc_mixer_control *mc =
2828 (struct soc_mixer_control *)kcontrol->private_value;
2829
2830 unsigned int reg = mc->reg;
2831 unsigned int reg2 = mc->rreg;
2832 unsigned int shift = mc->shift;
2833 unsigned int rshift = mc->rshift;
2834 int max = mc->max;
2835 int min = mc->min;
2836 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002837 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002838 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002839
2840 val_mask = mask << shift;
2841 val = (ucontrol->value.integer.value[0] + min) & mask;
2842 val = val << shift;
2843
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002844 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302845 if (err < 0)
2846 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002847
2848 if (snd_soc_volsw_is_stereo(mc)) {
2849 val_mask = mask << rshift;
2850 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2851 val2 = val2 << rshift;
2852
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002853 err = snd_soc_component_update_bits(component, reg2, val_mask,
2854 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002855 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002856 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002857}
2858EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2859
2860/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002861 * snd_soc_info_volsw_s8 - signed mixer info callback
2862 * @kcontrol: mixer control
2863 * @uinfo: control element information
2864 *
2865 * Callback to provide information about a signed mixer control.
2866 *
2867 * Returns 0 for success.
2868 */
2869int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2870 struct snd_ctl_elem_info *uinfo)
2871{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002872 struct soc_mixer_control *mc =
2873 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002874 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002875 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002876
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002877 if (!mc->platform_max)
2878 mc->platform_max = mc->max;
2879 platform_max = mc->platform_max;
2880
Mark Browne13ac2e2008-05-28 17:58:05 +01002881 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2882 uinfo->count = 2;
2883 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002884 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002885 return 0;
2886}
2887EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2888
2889/**
2890 * snd_soc_get_volsw_s8 - signed mixer get callback
2891 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002892 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002893 *
2894 * Callback to get the value of a signed mixer control.
2895 *
2896 * Returns 0 for success.
2897 */
2898int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2899 struct snd_ctl_elem_value *ucontrol)
2900{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002901 struct soc_mixer_control *mc =
2902 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002903 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002904 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002905 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002906 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002907 int ret;
2908
2909 ret = snd_soc_component_read(component, reg, &val);
2910 if (ret)
2911 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002912
2913 ucontrol->value.integer.value[0] =
2914 ((signed char)(val & 0xff))-min;
2915 ucontrol->value.integer.value[1] =
2916 ((signed char)((val >> 8) & 0xff))-min;
2917 return 0;
2918}
2919EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2920
2921/**
2922 * snd_soc_put_volsw_sgn - signed mixer put callback
2923 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002924 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002925 *
2926 * Callback to set the value of a signed mixer control.
2927 *
2928 * Returns 0 for success.
2929 */
2930int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2931 struct snd_ctl_elem_value *ucontrol)
2932{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002933 struct soc_mixer_control *mc =
2934 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002935 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002936 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002937 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002938 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002939
2940 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2941 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2942
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002943 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002944}
2945EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2946
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002947/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002948 * snd_soc_info_volsw_range - single mixer info callback with range.
2949 * @kcontrol: mixer control
2950 * @uinfo: control element information
2951 *
2952 * Callback to provide information, within a range, about a single
2953 * mixer control.
2954 *
2955 * returns 0 for success.
2956 */
2957int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2958 struct snd_ctl_elem_info *uinfo)
2959{
2960 struct soc_mixer_control *mc =
2961 (struct soc_mixer_control *)kcontrol->private_value;
2962 int platform_max;
2963 int min = mc->min;
2964
2965 if (!mc->platform_max)
2966 mc->platform_max = mc->max;
2967 platform_max = mc->platform_max;
2968
2969 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002970 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002971 uinfo->value.integer.min = 0;
2972 uinfo->value.integer.max = platform_max - min;
2973
2974 return 0;
2975}
2976EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2977
2978/**
2979 * snd_soc_put_volsw_range - single mixer put value callback with range.
2980 * @kcontrol: mixer control
2981 * @ucontrol: control element information
2982 *
2983 * Callback to set the value, within a range, for a single mixer control.
2984 *
2985 * Returns 0 for success.
2986 */
2987int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2988 struct snd_ctl_elem_value *ucontrol)
2989{
2990 struct soc_mixer_control *mc =
2991 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002992 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002993 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002994 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002995 unsigned int shift = mc->shift;
2996 int min = mc->min;
2997 int max = mc->max;
2998 unsigned int mask = (1 << fls(max)) - 1;
2999 unsigned int invert = mc->invert;
3000 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003001 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003002
3003 val = ((ucontrol->value.integer.value[0] + min) & mask);
3004 if (invert)
3005 val = max - val;
3006 val_mask = mask << shift;
3007 val = val << shift;
3008
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003009 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09003010 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00003011 return ret;
3012
3013 if (snd_soc_volsw_is_stereo(mc)) {
3014 val = ((ucontrol->value.integer.value[1] + min) & mask);
3015 if (invert)
3016 val = max - val;
3017 val_mask = mask << shift;
3018 val = val << shift;
3019
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003020 ret = snd_soc_component_update_bits(component, rreg, val_mask,
3021 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00003022 }
3023
3024 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003025}
3026EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3027
3028/**
3029 * snd_soc_get_volsw_range - single mixer get callback with range
3030 * @kcontrol: mixer control
3031 * @ucontrol: control element information
3032 *
3033 * Callback to get the value, within a range, of a single mixer control.
3034 *
3035 * Returns 0 for success.
3036 */
3037int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3038 struct snd_ctl_elem_value *ucontrol)
3039{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003040 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003041 struct soc_mixer_control *mc =
3042 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003043 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003044 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003045 unsigned int shift = mc->shift;
3046 int min = mc->min;
3047 int max = mc->max;
3048 unsigned int mask = (1 << fls(max)) - 1;
3049 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003050 unsigned int val;
3051 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003052
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003053 ret = snd_soc_component_read(component, reg, &val);
3054 if (ret)
3055 return ret;
3056
3057 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003058 if (invert)
3059 ucontrol->value.integer.value[0] =
3060 max - ucontrol->value.integer.value[0];
3061 ucontrol->value.integer.value[0] =
3062 ucontrol->value.integer.value[0] - min;
3063
Mark Brown9bde4f02012-12-19 16:05:00 +00003064 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003065 ret = snd_soc_component_read(component, rreg, &val);
3066 if (ret)
3067 return ret;
3068
3069 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003070 if (invert)
3071 ucontrol->value.integer.value[1] =
3072 max - ucontrol->value.integer.value[1];
3073 ucontrol->value.integer.value[1] =
3074 ucontrol->value.integer.value[1] - min;
3075 }
3076
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003077 return 0;
3078}
3079EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3080
3081/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003082 * snd_soc_limit_volume - Set new limit to an existing volume control.
3083 *
3084 * @codec: where to look for the control
3085 * @name: Name of the control
3086 * @max: new maximum limit
3087 *
3088 * Return 0 for success, else error.
3089 */
3090int snd_soc_limit_volume(struct snd_soc_codec *codec,
3091 const char *name, int max)
3092{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003093 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003094 struct snd_kcontrol *kctl;
3095 struct soc_mixer_control *mc;
3096 int found = 0;
3097 int ret = -EINVAL;
3098
3099 /* Sanity check for name and max */
3100 if (unlikely(!name || max <= 0))
3101 return -EINVAL;
3102
3103 list_for_each_entry(kctl, &card->controls, list) {
3104 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3105 found = 1;
3106 break;
3107 }
3108 }
3109 if (found) {
3110 mc = (struct soc_mixer_control *)kctl->private_value;
3111 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003112 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003113 ret = 0;
3114 }
3115 }
3116 return ret;
3117}
3118EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3119
Mark Brown71d08512011-10-10 18:31:26 +01003120int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3121 struct snd_ctl_elem_info *uinfo)
3122{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003123 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003124 struct soc_bytes *params = (void *)kcontrol->private_value;
3125
3126 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003127 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003128
3129 return 0;
3130}
3131EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3132
3133int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3134 struct snd_ctl_elem_value *ucontrol)
3135{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003136 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003137 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003138 int ret;
3139
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003140 if (component->regmap)
3141 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003142 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003143 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003144 else
3145 ret = -EINVAL;
3146
Mark Brownf831b052012-02-17 16:20:33 -08003147 /* Hide any masked bytes to ensure consistent data reporting */
3148 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003149 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003150 case 1:
3151 ucontrol->value.bytes.data[0] &= ~params->mask;
3152 break;
3153 case 2:
3154 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003155 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003156 break;
3157 case 4:
3158 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003159 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003160 break;
3161 default:
3162 return -EINVAL;
3163 }
3164 }
3165
Mark Brown71d08512011-10-10 18:31:26 +01003166 return ret;
3167}
3168EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3169
3170int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3171 struct snd_ctl_elem_value *ucontrol)
3172{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003173 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003174 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003175 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003176 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003177 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003178
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003179 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003180 return -EINVAL;
3181
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003182 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003183
Mark Brownb5a8fe42013-01-20 21:42:22 +09003184 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3185 if (!data)
3186 return -ENOMEM;
3187
Mark Brownf831b052012-02-17 16:20:33 -08003188 /*
3189 * If we've got a mask then we need to preserve the register
3190 * bits. We shouldn't modify the incoming data so take a
3191 * copy.
3192 */
3193 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003194 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003195 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003196 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003197
3198 val &= params->mask;
3199
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003200 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003201 case 1:
3202 ((u8 *)data)[0] &= ~params->mask;
3203 ((u8 *)data)[0] |= val;
3204 break;
3205 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003206 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003207 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003208 &mask, &mask);
3209 if (ret != 0)
3210 goto out;
3211
3212 ((u16 *)data)[0] &= mask;
3213
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003214 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003215 &val, &val);
3216 if (ret != 0)
3217 goto out;
3218
3219 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003220 break;
3221 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003222 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003223 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003224 &mask, &mask);
3225 if (ret != 0)
3226 goto out;
3227
3228 ((u32 *)data)[0] &= mask;
3229
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003230 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003231 &val, &val);
3232 if (ret != 0)
3233 goto out;
3234
3235 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003236 break;
3237 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003238 ret = -EINVAL;
3239 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003240 }
3241 }
3242
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003243 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003244 data, len);
3245
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003246out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003247 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003248
3249 return ret;
3250}
3251EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3252
Vinod Kould9881202014-05-02 10:58:11 +05303253int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3254 struct snd_ctl_elem_info *ucontrol)
3255{
3256 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3257
3258 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3259 ucontrol->count = params->max;
3260
3261 return 0;
3262}
3263EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3264
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003265/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003266 * snd_soc_info_xr_sx - signed multi register info callback
3267 * @kcontrol: mreg control
3268 * @uinfo: control element information
3269 *
3270 * Callback to provide information of a control that can
3271 * span multiple codec registers which together
3272 * forms a single signed value in a MSB/LSB manner.
3273 *
3274 * Returns 0 for success.
3275 */
3276int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3277 struct snd_ctl_elem_info *uinfo)
3278{
3279 struct soc_mreg_control *mc =
3280 (struct soc_mreg_control *)kcontrol->private_value;
3281 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3282 uinfo->count = 1;
3283 uinfo->value.integer.min = mc->min;
3284 uinfo->value.integer.max = mc->max;
3285
3286 return 0;
3287}
3288EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3289
3290/**
3291 * snd_soc_get_xr_sx - signed multi register get callback
3292 * @kcontrol: mreg control
3293 * @ucontrol: control element information
3294 *
3295 * Callback to get the value of a control that can span
3296 * multiple codec registers which together forms a single
3297 * signed value in a MSB/LSB manner. The control supports
3298 * specifying total no of bits used to allow for bitfields
3299 * across the multiple codec registers.
3300 *
3301 * Returns 0 for success.
3302 */
3303int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3304 struct snd_ctl_elem_value *ucontrol)
3305{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003306 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003307 struct soc_mreg_control *mc =
3308 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003309 unsigned int regbase = mc->regbase;
3310 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003311 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003312 unsigned int regwmask = (1<<regwshift)-1;
3313 unsigned int invert = mc->invert;
3314 unsigned long mask = (1UL<<mc->nbits)-1;
3315 long min = mc->min;
3316 long max = mc->max;
3317 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003318 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003319 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003320 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003321
3322 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003323 ret = snd_soc_component_read(component, regbase+i, &regval);
3324 if (ret)
3325 return ret;
3326 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003327 }
3328 val &= mask;
3329 if (min < 0 && val > max)
3330 val |= ~mask;
3331 if (invert)
3332 val = max - val;
3333 ucontrol->value.integer.value[0] = val;
3334
3335 return 0;
3336}
3337EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3338
3339/**
3340 * snd_soc_put_xr_sx - signed multi register get callback
3341 * @kcontrol: mreg control
3342 * @ucontrol: control element information
3343 *
3344 * Callback to set the value of a control that can span
3345 * multiple codec registers which together forms a single
3346 * signed value in a MSB/LSB manner. The control supports
3347 * specifying total no of bits used to allow for bitfields
3348 * across the multiple codec registers.
3349 *
3350 * Returns 0 for success.
3351 */
3352int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3353 struct snd_ctl_elem_value *ucontrol)
3354{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003355 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003356 struct soc_mreg_control *mc =
3357 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003358 unsigned int regbase = mc->regbase;
3359 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003360 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003361 unsigned int regwmask = (1<<regwshift)-1;
3362 unsigned int invert = mc->invert;
3363 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003364 long max = mc->max;
3365 long val = ucontrol->value.integer.value[0];
3366 unsigned int i, regval, regmask;
3367 int err;
3368
3369 if (invert)
3370 val = max - val;
3371 val &= mask;
3372 for (i = 0; i < regcount; i++) {
3373 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3374 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003375 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003376 regmask, regval);
3377 if (err < 0)
3378 return err;
3379 }
3380
3381 return 0;
3382}
3383EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3384
3385/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003386 * snd_soc_get_strobe - strobe get callback
3387 * @kcontrol: mixer control
3388 * @ucontrol: control element information
3389 *
3390 * Callback get the value of a strobe mixer control.
3391 *
3392 * Returns 0 for success.
3393 */
3394int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3395 struct snd_ctl_elem_value *ucontrol)
3396{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003397 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003398 struct soc_mixer_control *mc =
3399 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003400 unsigned int reg = mc->reg;
3401 unsigned int shift = mc->shift;
3402 unsigned int mask = 1 << shift;
3403 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003404 unsigned int val;
3405 int ret;
3406
3407 ret = snd_soc_component_read(component, reg, &val);
3408 if (ret)
3409 return ret;
3410
3411 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003412
3413 if (shift != 0 && val != 0)
3414 val = val >> shift;
3415 ucontrol->value.enumerated.item[0] = val ^ invert;
3416
3417 return 0;
3418}
3419EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3420
3421/**
3422 * snd_soc_put_strobe - strobe put callback
3423 * @kcontrol: mixer control
3424 * @ucontrol: control element information
3425 *
3426 * Callback strobe a register bit to high then low (or the inverse)
3427 * in one pass of a single mixer enum control.
3428 *
3429 * Returns 1 for success.
3430 */
3431int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3432 struct snd_ctl_elem_value *ucontrol)
3433{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003434 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003435 struct soc_mixer_control *mc =
3436 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003437 unsigned int reg = mc->reg;
3438 unsigned int shift = mc->shift;
3439 unsigned int mask = 1 << shift;
3440 unsigned int invert = mc->invert != 0;
3441 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3442 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3443 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3444 int err;
3445
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003446 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003447 if (err < 0)
3448 return err;
3449
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003450 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003451}
3452EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3453
3454/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003455 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3456 * @dai: DAI
3457 * @clk_id: DAI specific clock ID
3458 * @freq: new clock frequency in Hz
3459 * @dir: new clock direction - input/output.
3460 *
3461 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3462 */
3463int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3464 unsigned int freq, int dir)
3465{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003466 if (dai->driver && dai->driver->ops->set_sysclk)
3467 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003468 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003469 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003470 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003471 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003472 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003473}
3474EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3475
3476/**
Mark Brownec4ee522011-03-07 20:58:11 +00003477 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3478 * @codec: CODEC
3479 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003480 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003481 * @freq: new clock frequency in Hz
3482 * @dir: new clock direction - input/output.
3483 *
3484 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3485 */
3486int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003487 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003488{
3489 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003490 return codec->driver->set_sysclk(codec, clk_id, source,
3491 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003492 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003493 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003494}
3495EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3496
3497/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003498 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3499 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003500 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003501 * @div: new clock divisor.
3502 *
3503 * Configures the clock dividers. This is used to derive the best DAI bit and
3504 * frame clocks from the system or master clock. It's best to set the DAI bit
3505 * and frame clocks as low as possible to save system power.
3506 */
3507int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3508 int div_id, int div)
3509{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003510 if (dai->driver && dai->driver->ops->set_clkdiv)
3511 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003512 else
3513 return -EINVAL;
3514}
3515EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3516
3517/**
3518 * snd_soc_dai_set_pll - configure DAI PLL.
3519 * @dai: DAI
3520 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003521 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003522 * @freq_in: PLL input clock frequency in Hz
3523 * @freq_out: requested PLL output clock frequency in Hz
3524 *
3525 * Configures and enables PLL to generate output clock based on input clock.
3526 */
Mark Brown85488032009-09-05 18:52:16 +01003527int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3528 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003529{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003530 if (dai->driver && dai->driver->ops->set_pll)
3531 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003532 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003533 else if (dai->codec && dai->codec->driver->set_pll)
3534 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3535 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003536 else
3537 return -EINVAL;
3538}
3539EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3540
Mark Brownec4ee522011-03-07 20:58:11 +00003541/*
3542 * snd_soc_codec_set_pll - configure codec PLL.
3543 * @codec: CODEC
3544 * @pll_id: DAI specific PLL ID
3545 * @source: DAI specific source for the PLL
3546 * @freq_in: PLL input clock frequency in Hz
3547 * @freq_out: requested PLL output clock frequency in Hz
3548 *
3549 * Configures and enables PLL to generate output clock based on input clock.
3550 */
3551int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3552 unsigned int freq_in, unsigned int freq_out)
3553{
3554 if (codec->driver->set_pll)
3555 return codec->driver->set_pll(codec, pll_id, source,
3556 freq_in, freq_out);
3557 else
3558 return -EINVAL;
3559}
3560EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3561
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003562/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003563 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3564 * @dai: DAI
3565 * @ratio Ratio of BCLK to Sample rate.
3566 *
3567 * Configures the DAI for a preset BCLK to sample rate ratio.
3568 */
3569int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3570{
3571 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3572 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3573 else
3574 return -EINVAL;
3575}
3576EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3577
3578/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003579 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3580 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003581 * @fmt: SND_SOC_DAIFMT_ format value.
3582 *
3583 * Configures the DAI hardware format and clocking.
3584 */
3585int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3586{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003587 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003588 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003589 if (dai->driver->ops->set_fmt == NULL)
3590 return -ENOTSUPP;
3591 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003592}
3593EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3594
3595/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003596 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003597 * @slots: Number of slots in use.
3598 * @tx_mask: bitmask representing active TX slots.
3599 * @rx_mask: bitmask representing active RX slots.
3600 *
3601 * Generates the TDM tx and rx slot default masks for DAI.
3602 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003603static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003604 unsigned int *tx_mask,
3605 unsigned int *rx_mask)
3606{
3607 if (*tx_mask || *rx_mask)
3608 return 0;
3609
3610 if (!slots)
3611 return -EINVAL;
3612
3613 *tx_mask = (1 << slots) - 1;
3614 *rx_mask = (1 << slots) - 1;
3615
3616 return 0;
3617}
3618
3619/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003620 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3621 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003622 * @tx_mask: bitmask representing active TX slots.
3623 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003624 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003625 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003626 *
3627 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3628 * specific.
3629 */
3630int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003631 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003632{
Xiubo Lie5c21512014-03-21 14:17:12 +08003633 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3634 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003635 &tx_mask, &rx_mask);
3636 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003637 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003638
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003639 if (dai->driver && dai->driver->ops->set_tdm_slot)
3640 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003641 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003642 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003643 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003644}
3645EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3646
3647/**
Barry Song472df3c2009-09-12 01:16:29 +08003648 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3649 * @dai: DAI
3650 * @tx_num: how many TX channels
3651 * @tx_slot: pointer to an array which imply the TX slot number channel
3652 * 0~num-1 uses
3653 * @rx_num: how many RX channels
3654 * @rx_slot: pointer to an array which imply the RX slot number channel
3655 * 0~num-1 uses
3656 *
3657 * configure the relationship between channel number and TDM slot number.
3658 */
3659int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3660 unsigned int tx_num, unsigned int *tx_slot,
3661 unsigned int rx_num, unsigned int *rx_slot)
3662{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003663 if (dai->driver && dai->driver->ops->set_channel_map)
3664 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003665 rx_num, rx_slot);
3666 else
3667 return -EINVAL;
3668}
3669EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3670
3671/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003672 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3673 * @dai: DAI
3674 * @tristate: tristate enable
3675 *
3676 * Tristates the DAI so that others can use it.
3677 */
3678int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3679{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003680 if (dai->driver && dai->driver->ops->set_tristate)
3681 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003682 else
3683 return -EINVAL;
3684}
3685EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3686
3687/**
3688 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3689 * @dai: DAI
3690 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003691 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003692 *
3693 * Mutes the DAI DAC.
3694 */
Mark Brownda183962013-02-06 15:44:07 +00003695int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3696 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003697{
Mark Brownda183962013-02-06 15:44:07 +00003698 if (!dai->driver)
3699 return -ENOTSUPP;
3700
3701 if (dai->driver->ops->mute_stream)
3702 return dai->driver->ops->mute_stream(dai, mute, direction);
3703 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3704 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003705 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003706 else
Mark Brown04570c62012-04-13 19:16:03 +01003707 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003708}
3709EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3710
Mark Brownc5af3a22008-11-28 13:29:45 +00003711/**
3712 * snd_soc_register_card - Register a card with the ASoC core
3713 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003714 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003715 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003716 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303717int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003718{
Mark Brownb19e6e72012-03-14 21:18:39 +00003719 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003720
Mark Brownc5af3a22008-11-28 13:29:45 +00003721 if (!card->name || !card->dev)
3722 return -EINVAL;
3723
Stephen Warren5a504962011-12-21 10:40:59 -07003724 for (i = 0; i < card->num_links; i++) {
3725 struct snd_soc_dai_link *link = &card->dai_link[i];
3726
3727 /*
3728 * Codec must be specified by 1 of name or OF node,
3729 * not both or neither.
3730 */
3731 if (!!link->codec_name == !!link->codec_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003732 dev_err(card->dev,
3733 "ASoC: Neither/both codec name/of_node are set for %s\n",
3734 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003735 return -EINVAL;
3736 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003737 /* Codec DAI name must be specified */
3738 if (!link->codec_dai_name) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003739 dev_err(card->dev,
3740 "ASoC: codec_dai_name not set for %s\n",
3741 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003742 return -EINVAL;
3743 }
Stephen Warren5a504962011-12-21 10:40:59 -07003744
3745 /*
3746 * Platform may be specified by either name or OF node, but
3747 * can be left unspecified, and a dummy platform will be used.
3748 */
3749 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003750 dev_err(card->dev,
3751 "ASoC: Both platform name/of_node are set for %s\n",
3752 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003753 return -EINVAL;
3754 }
3755
3756 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003757 * CPU device may be specified by either name or OF node, but
3758 * can be left unspecified, and will be matched based on DAI
3759 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003760 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003761 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003762 dev_err(card->dev,
3763 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3764 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003765 return -EINVAL;
3766 }
3767 /*
3768 * At least one of CPU DAI name or CPU device name/node must be
3769 * specified
3770 */
3771 if (!link->cpu_dai_name &&
3772 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003773 dev_err(card->dev,
3774 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3775 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003776 return -EINVAL;
3777 }
3778 }
3779
Mark Browned77cc12011-05-03 18:25:34 +01003780 dev_set_drvdata(card->dev, card);
3781
Stephen Warren111c6412011-01-28 14:26:35 -07003782 snd_soc_initialize_card_lists(card);
3783
Vinod Koul150dd2f2011-01-13 22:48:32 +05303784 soc_init_card_debugfs(card);
3785
Mark Brown181a6892012-03-14 20:18:49 +00003786 card->rtd = devm_kzalloc(card->dev,
3787 sizeof(struct snd_soc_pcm_runtime) *
3788 (card->num_links + card->num_aux_devs),
3789 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003790 if (card->rtd == NULL)
3791 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003792 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003793 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003794
3795 for (i = 0; i < card->num_links; i++)
3796 card->rtd[i].dai_link = &card->dai_link[i];
3797
Mark Browndb432b42011-10-03 21:06:40 +01003798 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003799 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003800 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003801 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003802
Mark Brownb19e6e72012-03-14 21:18:39 +00003803 ret = snd_soc_instantiate_card(card);
3804 if (ret != 0)
3805 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003806
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003807 /* deactivate pins to sleep state */
3808 for (i = 0; i < card->num_rtd; i++) {
3809 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3810 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3811 if (!codec_dai->active)
3812 pinctrl_pm_select_sleep_state(codec_dai->dev);
3813 if (!cpu_dai->active)
3814 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3815 }
3816
Mark Brownb19e6e72012-03-14 21:18:39 +00003817 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003818}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303819EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003820
3821/**
3822 * snd_soc_unregister_card - Unregister a card with the ASoC core
3823 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003824 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003825 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003826 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303827int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003828{
Vinod Koulb0e26482011-01-13 22:48:02 +05303829 if (card->instantiated)
3830 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003831 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003832
3833 return 0;
3834}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303835EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003836
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003837/*
3838 * Simplify DAI link configuration by removing ".-1" from device names
3839 * and sanitizing names.
3840 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003841static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003842{
3843 char *found, name[NAME_SIZE];
3844 int id1, id2;
3845
3846 if (dev_name(dev) == NULL)
3847 return NULL;
3848
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003849 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003850
3851 /* are we a "%s.%d" name (platform and SPI components) */
3852 found = strstr(name, dev->driver->name);
3853 if (found) {
3854 /* get ID */
3855 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3856
3857 /* discard ID from name if ID == -1 */
3858 if (*id == -1)
3859 found[strlen(dev->driver->name)] = '\0';
3860 }
3861
3862 } else {
3863 /* I2C component devices are named "bus-addr" */
3864 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3865 char tmp[NAME_SIZE];
3866
3867 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003868 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003869
3870 /* sanitize component name for DAI link creation */
3871 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003872 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003873 } else
3874 *id = 0;
3875 }
3876
3877 return kstrdup(name, GFP_KERNEL);
3878}
3879
3880/*
3881 * Simplify DAI link naming for single devices with multiple DAIs by removing
3882 * any ".-1" and using the DAI name (instead of device name).
3883 */
3884static inline char *fmt_multiple_name(struct device *dev,
3885 struct snd_soc_dai_driver *dai_drv)
3886{
3887 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003888 dev_err(dev,
3889 "ASoC: error - multiple DAI %s registered with no name\n",
3890 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003891 return NULL;
3892 }
3893
3894 return kstrdup(dai_drv->name, GFP_KERNEL);
3895}
3896
Mark Brown91151712008-11-30 23:31:24 +00003897/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003898 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003899 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003900 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003901 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003902static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003903{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003904 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003905
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003906 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003907 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3908 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003909 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003910 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003911 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003912 }
Mark Brown91151712008-11-30 23:31:24 +00003913}
Mark Brown91151712008-11-30 23:31:24 +00003914
3915/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003916 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003917 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003918 * @component: The component the DAIs are registered for
3919 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003920 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003921 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3922 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003923 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003924static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003925 struct snd_soc_dai_driver *dai_drv, size_t count,
3926 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003927{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003928 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003929 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003930 unsigned int i;
3931 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003932
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003933 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003934
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003935 component->dai_drv = dai_drv;
3936 component->num_dai = count;
3937
Mark Brown91151712008-11-30 23:31:24 +00003938 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003939
3940 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003941 if (dai == NULL) {
3942 ret = -ENOMEM;
3943 goto err;
3944 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003945
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003946 /*
3947 * Back in the old days when we still had component-less DAIs,
3948 * instead of having a static name, component-less DAIs would
3949 * inherit the name of the parent device so it is possible to
3950 * register multiple instances of the DAI. We still need to keep
3951 * the same naming style even though those DAIs are not
3952 * component-less anymore.
3953 */
3954 if (count == 1 && legacy_dai_naming) {
3955 dai->name = fmt_single_name(dev, &dai->id);
3956 } else {
3957 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3958 if (dai_drv[i].id)
3959 dai->id = dai_drv[i].id;
3960 else
3961 dai->id = i;
3962 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003963 if (dai->name == NULL) {
3964 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003965 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003966 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003967 }
3968
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003969 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003970 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003971 dai->driver = &dai_drv[i];
3972 if (!dai->driver->ops)
3973 dai->driver->ops = &null_dai_ops;
3974
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003975 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003976
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003977 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003978 }
3979
3980 return 0;
3981
3982err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003983 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003984
3985 return ret;
3986}
Mark Brown91151712008-11-30 23:31:24 +00003987
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003988static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3989 enum snd_soc_dapm_type type, int subseq)
3990{
3991 struct snd_soc_component *component = dapm->component;
3992
3993 component->driver->seq_notifier(component, type, subseq);
3994}
3995
3996static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3997 int event)
3998{
3999 struct snd_soc_component *component = dapm->component;
4000
4001 return component->driver->stream_event(component, event);
4002}
4003
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004004static int snd_soc_component_initialize(struct snd_soc_component *component,
4005 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004006{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004007 struct snd_soc_dapm_context *dapm;
4008
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004009 component->name = fmt_single_name(dev, &component->id);
4010 if (!component->name) {
4011 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004012 return -ENOMEM;
4013 }
4014
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004015 component->dev = dev;
4016 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004017
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004018 if (!component->dapm_ptr)
4019 component->dapm_ptr = &component->dapm;
4020
4021 dapm = component->dapm_ptr;
4022 dapm->dev = dev;
4023 dapm->component = component;
4024 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004025 if (driver->seq_notifier)
4026 dapm->seq_notifier = snd_soc_component_seq_notifier;
4027 if (driver->stream_event)
4028 dapm->stream_event = snd_soc_component_stream_event;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004029
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004030 INIT_LIST_HEAD(&component->dai_list);
4031 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004032
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004033 return 0;
4034}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004035
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004036static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4037{
4038 list_add(&component->list, &component_list);
4039}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004040
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004041static void snd_soc_component_add(struct snd_soc_component *component)
4042{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004043 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004044 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004045 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004046}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004047
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004048static void snd_soc_component_cleanup(struct snd_soc_component *component)
4049{
4050 snd_soc_unregister_dais(component);
4051 kfree(component->name);
4052}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004053
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004054static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4055{
4056 list_del(&component->list);
4057}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004058
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004059static void snd_soc_component_del(struct snd_soc_component *component)
4060{
4061 mutex_lock(&client_mutex);
4062 snd_soc_component_del_unlocked(component);
4063 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004064}
4065
4066int snd_soc_register_component(struct device *dev,
4067 const struct snd_soc_component_driver *cmpnt_drv,
4068 struct snd_soc_dai_driver *dai_drv,
4069 int num_dai)
4070{
4071 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004072 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004073
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004074 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004075 if (!cmpnt) {
4076 dev_err(dev, "ASoC: Failed to allocate memory\n");
4077 return -ENOMEM;
4078 }
4079
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004080 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4081 if (ret)
4082 goto err_free;
4083
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004084 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004085 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004086
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004087 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4088 if (ret < 0) {
4089 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4090 goto err_cleanup;
4091 }
4092
4093 snd_soc_component_add(cmpnt);
4094
4095 return 0;
4096
4097err_cleanup:
4098 snd_soc_component_cleanup(cmpnt);
4099err_free:
4100 kfree(cmpnt);
4101 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004102}
4103EXPORT_SYMBOL_GPL(snd_soc_register_component);
4104
4105/**
4106 * snd_soc_unregister_component - Unregister a component from the ASoC core
4107 *
4108 */
4109void snd_soc_unregister_component(struct device *dev)
4110{
4111 struct snd_soc_component *cmpnt;
4112
4113 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004114 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004115 goto found;
4116 }
4117 return;
4118
4119found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004120 snd_soc_component_del(cmpnt);
4121 snd_soc_component_cleanup(cmpnt);
4122 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004123}
4124EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4125
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004126static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4127 unsigned int reg, unsigned int val)
4128{
4129 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4130
4131 return platform->driver->write(platform, reg, val);
4132}
4133
4134static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4135 unsigned int reg, unsigned int *val)
4136{
4137 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4138
4139 *val = platform->driver->read(platform, reg);
4140
4141 return 0;
4142}
4143
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004144/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004145 * snd_soc_add_platform - Add a platform to the ASoC core
4146 * @dev: The parent device for the platform
4147 * @platform: The platform to add
4148 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004149 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004150int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57db82013-03-27 12:02:22 +01004151 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004152{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004153 int ret;
4154
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004155 ret = snd_soc_component_initialize(&platform->component,
4156 &platform_drv->component_driver, dev);
4157 if (ret)
4158 return ret;
4159
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004160 platform->dev = dev;
4161 platform->driver = platform_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004162 if (platform_drv->write)
4163 platform->component.write = snd_soc_platform_drv_write;
4164 if (platform_drv->read)
4165 platform->component.read = snd_soc_platform_drv_read;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004166
4167 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004168 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004169 list_add(&platform->list, &platform_list);
4170 mutex_unlock(&client_mutex);
4171
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004172 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4173 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004174
4175 return 0;
4176}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004177EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4178
4179/**
4180 * snd_soc_register_platform - Register a platform with the ASoC core
4181 *
4182 * @platform: platform to register
4183 */
4184int snd_soc_register_platform(struct device *dev,
4185 const struct snd_soc_platform_driver *platform_drv)
4186{
4187 struct snd_soc_platform *platform;
4188 int ret;
4189
4190 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4191
4192 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4193 if (platform == NULL)
4194 return -ENOMEM;
4195
4196 ret = snd_soc_add_platform(dev, platform, platform_drv);
4197 if (ret)
4198 kfree(platform);
4199
4200 return ret;
4201}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004202EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4203
4204/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004205 * snd_soc_remove_platform - Remove a platform from the ASoC core
4206 * @platform: the platform to remove
4207 */
4208void snd_soc_remove_platform(struct snd_soc_platform *platform)
4209{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004210
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004211 mutex_lock(&client_mutex);
4212 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004213 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004214 mutex_unlock(&client_mutex);
4215
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004216 snd_soc_component_cleanup(&platform->component);
4217
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004218 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004219 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004220}
4221EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4222
4223struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4224{
4225 struct snd_soc_platform *platform;
4226
4227 list_for_each_entry(platform, &platform_list, list) {
4228 if (dev == platform->dev)
4229 return platform;
4230 }
4231
4232 return NULL;
4233}
4234EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4235
4236/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004237 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4238 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004239 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004240 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004241void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004242{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004243 struct snd_soc_platform *platform;
4244
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004245 platform = snd_soc_lookup_platform(dev);
4246 if (!platform)
4247 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004248
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004249 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004250 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004251}
4252EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4253
Mark Brown151ab222009-05-09 16:22:58 +01004254static u64 codec_format_map[] = {
4255 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4256 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4257 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4258 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4259 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4260 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4261 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4262 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4263 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4264 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4265 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4266 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4267 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4268 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4269 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4270 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4271};
4272
4273/* Fix up the DAI formats for endianness: codecs don't actually see
4274 * the endianness of the data but we're using the CPU format
4275 * definitions which do need to include endianness so we ensure that
4276 * codec DAIs always have both big and little endian variants set.
4277 */
4278static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4279{
4280 int i;
4281
4282 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4283 if (stream->formats & codec_format_map[i])
4284 stream->formats |= codec_format_map[i];
4285}
4286
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004287static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4288 unsigned int reg, unsigned int val)
4289{
4290 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4291
4292 return codec->driver->write(codec, reg, val);
4293}
4294
4295static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4296 unsigned int reg, unsigned int *val)
4297{
4298 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4299
4300 *val = codec->driver->read(codec, reg);
4301
4302 return 0;
4303}
4304
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004305static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4306 enum snd_soc_bias_level level)
4307{
4308 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4309
4310 return codec->driver->set_bias_level(codec, level);
4311}
4312
Mark Brown0d0cf002008-12-10 14:32:45 +00004313/**
4314 * snd_soc_register_codec - Register a codec with the ASoC core
4315 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004316 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004317 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004318int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004319 const struct snd_soc_codec_driver *codec_drv,
4320 struct snd_soc_dai_driver *dai_drv,
4321 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004322{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004323 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004324 struct snd_soc_dai *dai;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004325 struct regmap *regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004326 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004327
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004328 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004329
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004330 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4331 if (codec == NULL)
4332 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004333
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004334 codec->component.dapm_ptr = &codec->dapm;
4335
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004336 ret = snd_soc_component_initialize(&codec->component,
4337 &codec_drv->component_driver, dev);
4338 if (ret)
4339 goto err_free;
4340
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004341 if (codec_drv->write)
4342 codec->component.write = snd_soc_codec_drv_write;
4343 if (codec_drv->read)
4344 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004345 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004346 codec->dapm.codec = codec;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004347 if (codec_drv->seq_notifier)
4348 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004349 if (codec_drv->set_bias_level)
4350 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004351 codec->dev = dev;
4352 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004353 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004354 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004355
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004356 if (!codec->component.write) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004357 if (codec_drv->get_regmap)
4358 regmap = codec_drv->get_regmap(dev);
4359 else
4360 regmap = dev_get_regmap(dev, NULL);
4361
4362 if (regmap) {
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004363 ret = snd_soc_component_init_io(&codec->component,
4364 regmap);
4365 if (ret) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004366 dev_err(codec->dev,
4367 "Failed to set cache I/O:%d\n",
4368 ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004369 goto err_cleanup;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004370 }
4371 }
4372 }
4373
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004374 for (i = 0; i < num_dai; i++) {
4375 fixup_codec_formats(&dai_drv[i].playback);
4376 fixup_codec_formats(&dai_drv[i].capture);
4377 }
4378
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004379 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4380 if (ret < 0) {
4381 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4382 goto err_cleanup;
4383 }
4384
4385 list_for_each_entry(dai, &codec->component.dai_list, list)
4386 dai->codec = codec;
4387
Mark Brown054880f2012-04-09 17:29:19 +01004388 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004389 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004390 list_add(&codec->list, &codec_list);
4391 mutex_unlock(&client_mutex);
4392
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004393 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4394 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004395 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004396
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004397err_cleanup:
4398 snd_soc_component_cleanup(&codec->component);
4399err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004400 kfree(codec);
4401 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004402}
4403EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4404
4405/**
4406 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4407 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004408 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004409 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004410void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004411{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004412 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004413
4414 list_for_each_entry(codec, &codec_list, list) {
4415 if (dev == codec->dev)
4416 goto found;
4417 }
4418 return;
4419
4420found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004421
Mark Brown0d0cf002008-12-10 14:32:45 +00004422 mutex_lock(&client_mutex);
4423 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004424 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004425 mutex_unlock(&client_mutex);
4426
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004427 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4428 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004429
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004430 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004431 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004432 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004433}
4434EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4435
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004436/* Retrieve a card's name from device tree */
4437int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4438 const char *propname)
4439{
4440 struct device_node *np = card->dev->of_node;
4441 int ret;
4442
4443 ret = of_property_read_string_index(np, propname, 0, &card->name);
4444 /*
4445 * EINVAL means the property does not exist. This is fine providing
4446 * card->name was previously set, which is checked later in
4447 * snd_soc_register_card.
4448 */
4449 if (ret < 0 && ret != -EINVAL) {
4450 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004451 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004452 propname, ret);
4453 return ret;
4454 }
4455
4456 return 0;
4457}
4458EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4459
Xiubo Li9a6d4862014-02-08 15:59:52 +08004460static const struct snd_soc_dapm_widget simple_widgets[] = {
4461 SND_SOC_DAPM_MIC("Microphone", NULL),
4462 SND_SOC_DAPM_LINE("Line", NULL),
4463 SND_SOC_DAPM_HP("Headphone", NULL),
4464 SND_SOC_DAPM_SPK("Speaker", NULL),
4465};
4466
4467int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4468 const char *propname)
4469{
4470 struct device_node *np = card->dev->of_node;
4471 struct snd_soc_dapm_widget *widgets;
4472 const char *template, *wname;
4473 int i, j, num_widgets, ret;
4474
4475 num_widgets = of_property_count_strings(np, propname);
4476 if (num_widgets < 0) {
4477 dev_err(card->dev,
4478 "ASoC: Property '%s' does not exist\n", propname);
4479 return -EINVAL;
4480 }
4481 if (num_widgets & 1) {
4482 dev_err(card->dev,
4483 "ASoC: Property '%s' length is not even\n", propname);
4484 return -EINVAL;
4485 }
4486
4487 num_widgets /= 2;
4488 if (!num_widgets) {
4489 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4490 propname);
4491 return -EINVAL;
4492 }
4493
4494 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4495 GFP_KERNEL);
4496 if (!widgets) {
4497 dev_err(card->dev,
4498 "ASoC: Could not allocate memory for widgets\n");
4499 return -ENOMEM;
4500 }
4501
4502 for (i = 0; i < num_widgets; i++) {
4503 ret = of_property_read_string_index(np, propname,
4504 2 * i, &template);
4505 if (ret) {
4506 dev_err(card->dev,
4507 "ASoC: Property '%s' index %d read error:%d\n",
4508 propname, 2 * i, ret);
4509 return -EINVAL;
4510 }
4511
4512 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4513 if (!strncmp(template, simple_widgets[j].name,
4514 strlen(simple_widgets[j].name))) {
4515 widgets[i] = simple_widgets[j];
4516 break;
4517 }
4518 }
4519
4520 if (j >= ARRAY_SIZE(simple_widgets)) {
4521 dev_err(card->dev,
4522 "ASoC: DAPM widget '%s' is not supported\n",
4523 template);
4524 return -EINVAL;
4525 }
4526
4527 ret = of_property_read_string_index(np, propname,
4528 (2 * i) + 1,
4529 &wname);
4530 if (ret) {
4531 dev_err(card->dev,
4532 "ASoC: Property '%s' index %d read error:%d\n",
4533 propname, (2 * i) + 1, ret);
4534 return -EINVAL;
4535 }
4536
4537 widgets[i].name = wname;
4538 }
4539
4540 card->dapm_widgets = widgets;
4541 card->num_dapm_widgets = num_widgets;
4542
4543 return 0;
4544}
4545EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4546
Xiubo Li89c67852014-02-14 09:34:35 +08004547int snd_soc_of_parse_tdm_slot(struct device_node *np,
4548 unsigned int *slots,
4549 unsigned int *slot_width)
4550{
4551 u32 val;
4552 int ret;
4553
4554 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4555 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4556 if (ret)
4557 return ret;
4558
4559 if (slots)
4560 *slots = val;
4561 }
4562
4563 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4564 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4565 if (ret)
4566 return ret;
4567
4568 if (slot_width)
4569 *slot_width = val;
4570 }
4571
4572 return 0;
4573}
4574EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4575
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004576int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4577 const char *propname)
4578{
4579 struct device_node *np = card->dev->of_node;
4580 int num_routes;
4581 struct snd_soc_dapm_route *routes;
4582 int i, ret;
4583
4584 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004585 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004586 dev_err(card->dev,
4587 "ASoC: Property '%s' does not exist or its length is not even\n",
4588 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004589 return -EINVAL;
4590 }
4591 num_routes /= 2;
4592 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004593 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004594 propname);
4595 return -EINVAL;
4596 }
4597
4598 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4599 GFP_KERNEL);
4600 if (!routes) {
4601 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004602 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004603 return -EINVAL;
4604 }
4605
4606 for (i = 0; i < num_routes; i++) {
4607 ret = of_property_read_string_index(np, propname,
4608 2 * i, &routes[i].sink);
4609 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004610 dev_err(card->dev,
4611 "ASoC: Property '%s' index %d could not be read: %d\n",
4612 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004613 return -EINVAL;
4614 }
4615 ret = of_property_read_string_index(np, propname,
4616 (2 * i) + 1, &routes[i].source);
4617 if (ret) {
4618 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004619 "ASoC: Property '%s' index %d could not be read: %d\n",
4620 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004621 return -EINVAL;
4622 }
4623 }
4624
4625 card->num_dapm_routes = num_routes;
4626 card->dapm_routes = routes;
4627
4628 return 0;
4629}
4630EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4631
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004632unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004633 const char *prefix,
4634 struct device_node **bitclkmaster,
4635 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004636{
4637 int ret, i;
4638 char prop[128];
4639 unsigned int format = 0;
4640 int bit, frame;
4641 const char *str;
4642 struct {
4643 char *name;
4644 unsigned int val;
4645 } of_fmt_table[] = {
4646 { "i2s", SND_SOC_DAIFMT_I2S },
4647 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4648 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4649 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4650 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4651 { "ac97", SND_SOC_DAIFMT_AC97 },
4652 { "pdm", SND_SOC_DAIFMT_PDM},
4653 { "msb", SND_SOC_DAIFMT_MSB },
4654 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004655 };
4656
4657 if (!prefix)
4658 prefix = "";
4659
4660 /*
4661 * check "[prefix]format = xxx"
4662 * SND_SOC_DAIFMT_FORMAT_MASK area
4663 */
4664 snprintf(prop, sizeof(prop), "%sformat", prefix);
4665 ret = of_property_read_string(np, prop, &str);
4666 if (ret == 0) {
4667 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4668 if (strcmp(str, of_fmt_table[i].name) == 0) {
4669 format |= of_fmt_table[i].val;
4670 break;
4671 }
4672 }
4673 }
4674
4675 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004676 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004677 * SND_SOC_DAIFMT_CLOCK_MASK area
4678 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004679 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4680 if (of_get_property(np, prop, NULL))
4681 format |= SND_SOC_DAIFMT_CONT;
4682 else
4683 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004684
4685 /*
4686 * check "[prefix]bitclock-inversion"
4687 * check "[prefix]frame-inversion"
4688 * SND_SOC_DAIFMT_INV_MASK area
4689 */
4690 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4691 bit = !!of_get_property(np, prop, NULL);
4692
4693 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4694 frame = !!of_get_property(np, prop, NULL);
4695
4696 switch ((bit << 4) + frame) {
4697 case 0x11:
4698 format |= SND_SOC_DAIFMT_IB_IF;
4699 break;
4700 case 0x10:
4701 format |= SND_SOC_DAIFMT_IB_NF;
4702 break;
4703 case 0x01:
4704 format |= SND_SOC_DAIFMT_NB_IF;
4705 break;
4706 default:
4707 /* SND_SOC_DAIFMT_NB_NF is default */
4708 break;
4709 }
4710
4711 /*
4712 * check "[prefix]bitclock-master"
4713 * check "[prefix]frame-master"
4714 * SND_SOC_DAIFMT_MASTER_MASK area
4715 */
4716 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4717 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004718 if (bit && bitclkmaster)
4719 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004720
4721 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4722 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004723 if (frame && framemaster)
4724 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004725
4726 switch ((bit << 4) + frame) {
4727 case 0x11:
4728 format |= SND_SOC_DAIFMT_CBM_CFM;
4729 break;
4730 case 0x10:
4731 format |= SND_SOC_DAIFMT_CBM_CFS;
4732 break;
4733 case 0x01:
4734 format |= SND_SOC_DAIFMT_CBS_CFM;
4735 break;
4736 default:
4737 format |= SND_SOC_DAIFMT_CBS_CFS;
4738 break;
4739 }
4740
4741 return format;
4742}
4743EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4744
Kuninori Morimotocb470082013-09-10 17:39:56 -07004745int snd_soc_of_get_dai_name(struct device_node *of_node,
4746 const char **dai_name)
4747{
4748 struct snd_soc_component *pos;
4749 struct of_phandle_args args;
4750 int ret;
4751
4752 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4753 "#sound-dai-cells", 0, &args);
4754 if (ret)
4755 return ret;
4756
4757 ret = -EPROBE_DEFER;
4758
4759 mutex_lock(&client_mutex);
4760 list_for_each_entry(pos, &component_list, list) {
4761 if (pos->dev->of_node != args.np)
4762 continue;
4763
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004764 if (pos->driver->of_xlate_dai_name) {
4765 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4766 } else {
4767 int id = -1;
4768
4769 switch (args.args_count) {
4770 case 0:
4771 id = 0; /* same as dai_drv[0] */
4772 break;
4773 case 1:
4774 id = args.args[0];
4775 break;
4776 default:
4777 /* not supported */
4778 break;
4779 }
4780
4781 if (id < 0 || id >= pos->num_dai) {
4782 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004783 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004784 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004785
4786 ret = 0;
4787
4788 *dai_name = pos->dai_drv[id].name;
4789 if (!*dai_name)
4790 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004791 }
4792
Kuninori Morimotocb470082013-09-10 17:39:56 -07004793 break;
4794 }
4795 mutex_unlock(&client_mutex);
4796
4797 of_node_put(args.np);
4798
4799 return ret;
4800}
4801EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4802
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004803static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004804{
Mark Brown384c89e2008-12-03 17:34:03 +00004805#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004806 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4807 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004808 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004809 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004810 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004811
Mark Brown8a9dab12011-01-10 22:25:21 +00004812 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004813 &codec_list_fops))
4814 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4815
Mark Brown8a9dab12011-01-10 22:25:21 +00004816 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004817 &dai_list_fops))
4818 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004819
Mark Brown8a9dab12011-01-10 22:25:21 +00004820 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004821 &platform_list_fops))
4822 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004823#endif
4824
Mark Brownfb257892011-04-28 10:57:54 +01004825 snd_soc_util_init();
4826
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004827 return platform_driver_register(&soc_driver);
4828}
Mark Brown4abe8e12010-10-12 17:41:03 +01004829module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004830
Mark Brown7d8c16a2008-11-30 22:11:24 +00004831static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004832{
Mark Brownfb257892011-04-28 10:57:54 +01004833 snd_soc_util_exit();
4834
Mark Brown384c89e2008-12-03 17:34:03 +00004835#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004836 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004837#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004838 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004839}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004840module_exit(snd_soc_exit);
4841
4842/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004843MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004844MODULE_DESCRIPTION("ALSA SoC Core");
4845MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004846MODULE_ALIAS("platform:soc-audio");