blob: 0cd36b781f4db8e6680efc37d6808345fb7a058a [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
Lars-Peter Clausen6b0a0b32014-05-04 19:18:30 +02001652static struct snd_soc_codec *soc_find_matching_codec(struct snd_soc_card *card,
1653 int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001654{
1655 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1656 struct snd_soc_codec *codec;
1657
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001658 /* find CODEC from registered CODECs */
Mark Brownb19e6e72012-03-14 21:18:39 +00001659 list_for_each_entry(codec, &codec_list, list) {
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001660 if (aux_dev->codec_of_node &&
1661 (codec->dev->of_node != aux_dev->codec_of_node))
1662 continue;
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02001663 if (aux_dev->codec_name &&
1664 strcmp(codec->component.name, aux_dev->codec_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001665 continue;
1666 return codec;
Mark Brownb19e6e72012-03-14 21:18:39 +00001667 }
1668
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001669 return NULL;
1670}
Mark Brownfb099cb2012-08-09 18:44:37 +01001671
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001672static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1673{
1674 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1675 const char *codecname = aux_dev->codec_name;
1676 struct snd_soc_codec *codec = soc_find_matching_codec(card, num);
1677
1678 if (codec)
1679 return 0;
1680 if (aux_dev->codec_of_node)
1681 codecname = of_node_full_name(aux_dev->codec_of_node);
1682
1683 dev_err(card->dev, "ASoC: %s not registered\n", codecname);
Mark Brownb19e6e72012-03-14 21:18:39 +00001684 return -EPROBE_DEFER;
1685}
1686
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001687static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1688{
1689 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001690 const char *codecname = aux_dev->codec_name;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001691 int ret = -ENODEV;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001692 struct snd_soc_codec *codec = soc_find_matching_codec(card, num);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001693
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001694 if (!codec) {
1695 if (aux_dev->codec_of_node)
1696 codecname = of_node_full_name(aux_dev->codec_of_node);
1697
1698 /* codec not found */
1699 dev_err(card->dev, "ASoC: codec %s not found", codecname);
1700 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001701 }
1702
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001703 if (codec->probed) {
1704 dev_err(codec->dev, "ASoC: codec already probed");
1705 return -EBUSY;
1706 }
1707
Jarkko Nikula589c3562010-12-06 16:27:07 +02001708 ret = soc_probe_codec(card, codec);
1709 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001710 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001711
Jarkko Nikula589c3562010-12-06 16:27:07 +02001712 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001713
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001714 return ret;
1715}
1716
1717static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1718{
1719 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1720 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001721
1722 /* unregister the rtd device */
1723 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001724 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001725 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001726 rtd->dev_registered = 0;
1727 }
1728
Jarkko Nikula589c3562010-12-06 16:27:07 +02001729 if (codec && codec->probed)
1730 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001731}
1732
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001733static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001734{
1735 int ret;
1736
1737 if (codec->cache_init)
1738 return 0;
1739
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001740 ret = snd_soc_cache_init(codec);
1741 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001742 dev_err(codec->dev,
1743 "ASoC: Failed to set cache compression type: %d\n",
1744 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001745 return ret;
1746 }
1747 codec->cache_init = 1;
1748 return 0;
1749}
1750
Mark Brownb19e6e72012-03-14 21:18:39 +00001751static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001752{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001753 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001754 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001755 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001756
Liam Girdwood01b9d992012-03-07 10:38:25 +00001757 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001758
Mark Brownb19e6e72012-03-14 21:18:39 +00001759 /* bind DAIs */
1760 for (i = 0; i < card->num_links; i++) {
1761 ret = soc_bind_dai_link(card, i);
1762 if (ret != 0)
1763 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001764 }
1765
Mark Brownb19e6e72012-03-14 21:18:39 +00001766 /* check aux_devs too */
1767 for (i = 0; i < card->num_aux_devs; i++) {
1768 ret = soc_check_aux_dev(card, i);
1769 if (ret != 0)
1770 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001771 }
1772
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001773 /* initialize the register cache for each available codec */
1774 list_for_each_entry(codec, &codec_list, list) {
1775 if (codec->cache_init)
1776 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001777 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001778 if (ret < 0)
1779 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001780 }
1781
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001782 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001783 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001784 card->owner, 0, &card->snd_card);
1785 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001786 dev_err(card->dev,
1787 "ASoC: can't create sound card for card %s: %d\n",
1788 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001789 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001790 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001791
Mark Browne37a4972011-03-02 18:21:57 +00001792 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1793 card->dapm.dev = card->dev;
1794 card->dapm.card = card;
1795 list_add(&card->dapm.list, &card->dapm_list);
1796
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001797#ifdef CONFIG_DEBUG_FS
1798 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1799#endif
1800
Mark Brown88ee1c62011-02-02 10:43:26 +00001801#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001802 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001803 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001804#endif
Andy Green6ed25972008-06-13 16:24:05 +01001805
Mark Brown9a841eb2011-04-12 17:51:37 -07001806 if (card->dapm_widgets)
1807 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1808 card->num_dapm_widgets);
1809
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001810 /* initialise the sound card only once */
1811 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001812 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001813 if (ret < 0)
1814 goto card_probe_error;
1815 }
1816
Stephen Warren62ae68f2012-06-08 12:34:23 -06001817 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001818 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1819 order++) {
1820 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001821 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001822 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001823 dev_err(card->dev,
1824 "ASoC: failed to instantiate card %d\n",
1825 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001826 goto probe_dai_err;
1827 }
1828 }
1829 }
1830
1831 /* probe all DAI links on this card */
1832 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1833 order++) {
1834 for (i = 0; i < card->num_links; i++) {
1835 ret = soc_probe_link_dais(card, i, order);
1836 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001837 dev_err(card->dev,
1838 "ASoC: failed to instantiate card %d\n",
1839 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001840 goto probe_dai_err;
1841 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001842 }
1843 }
1844
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001845 for (i = 0; i < card->num_aux_devs; i++) {
1846 ret = soc_probe_aux_dev(card, i);
1847 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001848 dev_err(card->dev,
1849 "ASoC: failed to add auxiliary devices %d\n",
1850 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001851 goto probe_aux_dev_err;
1852 }
1853 }
1854
Mark Brown888df392012-02-16 19:37:51 -08001855 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001856 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001857
Mark Brownb7af1da2011-04-07 19:18:44 +09001858 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001859 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001860
Mark Brownb8ad29d2011-03-02 18:35:51 +00001861 if (card->dapm_routes)
1862 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1863 card->num_dapm_routes);
1864
Mark Brown75d9ac42011-09-27 16:41:01 +01001865 for (i = 0; i < card->num_links; i++) {
1866 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001867 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001868
Mark Brownf04209a2012-04-09 16:29:21 +01001869 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001870 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001871 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001872 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001873 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001874 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001875 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001876 }
1877
1878 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001879 if (dai_fmt &&
1880 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001881 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1882 dai_fmt);
1883 if (ret != 0 && ret != -ENOTSUPP)
1884 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001885 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001886 ret);
1887 } else if (dai_fmt) {
1888 /* Flip the polarity for the "CPU" end */
1889 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1890 switch (dai_link->dai_fmt &
1891 SND_SOC_DAIFMT_MASTER_MASK) {
1892 case SND_SOC_DAIFMT_CBM_CFM:
1893 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1894 break;
1895 case SND_SOC_DAIFMT_CBM_CFS:
1896 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1897 break;
1898 case SND_SOC_DAIFMT_CBS_CFM:
1899 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1900 break;
1901 case SND_SOC_DAIFMT_CBS_CFS:
1902 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1903 break;
1904 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001905
1906 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001907 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001908 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001909 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001910 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001911 ret);
1912 }
1913 }
1914
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001915 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001916 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001917 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1918 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001919 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1920 "%s", card->driver_name ? card->driver_name : card->name);
1921 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1922 switch (card->snd_card->driver[i]) {
1923 case '_':
1924 case '-':
1925 case '\0':
1926 break;
1927 default:
1928 if (!isalnum(card->snd_card->driver[i]))
1929 card->snd_card->driver[i] = '_';
1930 break;
1931 }
1932 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001933
Mark Brown28e9ad92011-03-02 18:36:34 +00001934 if (card->late_probe) {
1935 ret = card->late_probe(card);
1936 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001937 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001938 card->name, ret);
1939 goto probe_aux_dev_err;
1940 }
1941 }
1942
Stephen Warren16332812011-11-23 12:42:04 -07001943 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001944 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001945
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001946 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001947
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001948 ret = snd_card_register(card->snd_card);
1949 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001950 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1951 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001952 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001953 }
1954
1955#ifdef CONFIG_SND_SOC_AC97_BUS
1956 /* register any AC97 codecs */
1957 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001958 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1959 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001960 dev_err(card->dev,
1961 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001962 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001963 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001964 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001965 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001966 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001967#endif
1968
Mark Brown435c5e22008-12-04 15:32:53 +00001969 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001970 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001971 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001972
1973 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001974
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001975probe_aux_dev_err:
1976 for (i = 0; i < card->num_aux_devs; i++)
1977 soc_remove_aux_dev(card, i);
1978
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001979probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001980 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001981
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001982card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001983 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001984 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001985
1986 snd_card_free(card->snd_card);
1987
Mark Brownb19e6e72012-03-14 21:18:39 +00001988base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001989 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001990
Mark Brownb19e6e72012-03-14 21:18:39 +00001991 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001992}
1993
1994/* probes a new socdev */
1995static int soc_probe(struct platform_device *pdev)
1996{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001997 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001998
Vinod Koul70a7ca32011-01-14 19:22:48 +05301999 /*
2000 * no card, so machine driver should be registering card
2001 * we should not be here in that case so ret error
2002 */
2003 if (!card)
2004 return -EINVAL;
2005
Mark Brownfe4085e2012-03-02 13:07:41 +00002006 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002007 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00002008 card->name);
2009
Mark Brown435c5e22008-12-04 15:32:53 +00002010 /* Bodge while we unpick instantiation */
2011 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002012
Mark Brown28d528c2012-08-09 18:45:23 +01002013 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002014}
2015
Vinod Koulb0e26482011-01-13 22:48:02 +05302016static int soc_cleanup_card_resources(struct snd_soc_card *card)
2017{
Vinod Koulb0e26482011-01-13 22:48:02 +05302018 int i;
2019
2020 /* make sure any delayed work runs */
2021 for (i = 0; i < card->num_rtd; i++) {
2022 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002023 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302024 }
2025
2026 /* remove auxiliary devices */
2027 for (i = 0; i < card->num_aux_devs; i++)
2028 soc_remove_aux_dev(card, i);
2029
2030 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002031 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302032
2033 soc_cleanup_card_debugfs(card);
2034
2035 /* remove the card */
2036 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002037 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302038
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02002039 snd_soc_dapm_free(&card->dapm);
2040
Vinod Koulb0e26482011-01-13 22:48:02 +05302041 snd_card_free(card->snd_card);
2042 return 0;
2043
2044}
2045
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002046/* removes a socdev */
2047static int soc_remove(struct platform_device *pdev)
2048{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002049 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002050
Mark Brownc5af3a22008-11-28 13:29:45 +00002051 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002052 return 0;
2053}
2054
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002055int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002056{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002057 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002058 int i;
Mark Brown51737472009-06-22 13:16:51 +01002059
2060 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002061 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002062
2063 /* Flush out pmdown_time work - we actually do want to run it
2064 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002065 for (i = 0; i < card->num_rtd; i++) {
2066 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002067 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002068 }
Mark Brown51737472009-06-22 13:16:51 +01002069
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002070 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002071
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002072 /* deactivate pins to sleep state */
2073 for (i = 0; i < card->num_rtd; i++) {
2074 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
2075 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
2076 pinctrl_pm_select_sleep_state(codec_dai->dev);
2077 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2078 }
2079
Mark Brown416356f2009-06-30 19:05:15 +01002080 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002081}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002082EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002083
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002084const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302085 .suspend = snd_soc_suspend,
2086 .resume = snd_soc_resume,
2087 .freeze = snd_soc_suspend,
2088 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002089 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302090 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002091};
Stephen Warrendeb26072011-04-05 19:35:30 -06002092EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002093
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002094/* ASoC platform driver */
2095static struct platform_driver soc_driver = {
2096 .driver = {
2097 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002098 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002099 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002100 },
2101 .probe = soc_probe,
2102 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002103};
2104
Mark Brown096e49d2009-07-05 15:12:22 +01002105/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002106 * snd_soc_new_ac97_codec - initailise AC97 device
2107 * @codec: audio codec
2108 * @ops: AC97 bus operations
2109 * @num: AC97 codec number
2110 *
2111 * Initialises AC97 codec resources for use by ad-hoc devices only.
2112 */
2113int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2114 struct snd_ac97_bus_ops *ops, int num)
2115{
2116 mutex_lock(&codec->mutex);
2117
2118 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2119 if (codec->ac97 == NULL) {
2120 mutex_unlock(&codec->mutex);
2121 return -ENOMEM;
2122 }
2123
2124 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2125 if (codec->ac97->bus == NULL) {
2126 kfree(codec->ac97);
2127 codec->ac97 = NULL;
2128 mutex_unlock(&codec->mutex);
2129 return -ENOMEM;
2130 }
2131
2132 codec->ac97->bus->ops = ops;
2133 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002134
2135 /*
2136 * Mark the AC97 device to be created by us. This way we ensure that the
2137 * device will be registered with the device subsystem later on.
2138 */
2139 codec->ac97_created = 1;
2140
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002141 mutex_unlock(&codec->mutex);
2142 return 0;
2143}
2144EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2145
Markus Pargmann741a5092013-08-19 17:05:55 +02002146static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2147
2148static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2149{
2150 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2151
2152 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2153
2154 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2155
2156 udelay(10);
2157
2158 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2159
2160 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2161 msleep(2);
2162}
2163
2164static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2165{
2166 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2167
2168 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2169
2170 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2171 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2172 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2173
2174 udelay(10);
2175
2176 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2177
2178 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2179 msleep(2);
2180}
2181
2182static int snd_soc_ac97_parse_pinctl(struct device *dev,
2183 struct snd_ac97_reset_cfg *cfg)
2184{
2185 struct pinctrl *p;
2186 struct pinctrl_state *state;
2187 int gpio;
2188 int ret;
2189
2190 p = devm_pinctrl_get(dev);
2191 if (IS_ERR(p)) {
2192 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002193 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002194 }
2195 cfg->pctl = p;
2196
2197 state = pinctrl_lookup_state(p, "ac97-reset");
2198 if (IS_ERR(state)) {
2199 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002200 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002201 }
2202 cfg->pstate_reset = state;
2203
2204 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2205 if (IS_ERR(state)) {
2206 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002207 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002208 }
2209 cfg->pstate_warm_reset = state;
2210
2211 state = pinctrl_lookup_state(p, "ac97-running");
2212 if (IS_ERR(state)) {
2213 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002214 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002215 }
2216 cfg->pstate_run = state;
2217
2218 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2219 if (gpio < 0) {
2220 dev_err(dev, "Can't find ac97-sync gpio\n");
2221 return gpio;
2222 }
2223 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2224 if (ret) {
2225 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2226 return ret;
2227 }
2228 cfg->gpio_sync = gpio;
2229
2230 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2231 if (gpio < 0) {
2232 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2233 return gpio;
2234 }
2235 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2236 if (ret) {
2237 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2238 return ret;
2239 }
2240 cfg->gpio_sdata = gpio;
2241
2242 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2243 if (gpio < 0) {
2244 dev_err(dev, "Can't find ac97-reset gpio\n");
2245 return gpio;
2246 }
2247 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2248 if (ret) {
2249 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2250 return ret;
2251 }
2252 cfg->gpio_reset = gpio;
2253
2254 return 0;
2255}
2256
Mark Brownb047e1c2013-06-26 12:45:59 +01002257struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002258EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002259
2260int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2261{
2262 if (ops == soc_ac97_ops)
2263 return 0;
2264
2265 if (soc_ac97_ops && ops)
2266 return -EBUSY;
2267
2268 soc_ac97_ops = ops;
2269
2270 return 0;
2271}
2272EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2273
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002274/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002275 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2276 *
2277 * This function sets the reset and warm_reset properties of ops and parses
2278 * the device node of pdev to get pinctrl states and gpio numbers to use.
2279 */
2280int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2281 struct platform_device *pdev)
2282{
2283 struct device *dev = &pdev->dev;
2284 struct snd_ac97_reset_cfg cfg;
2285 int ret;
2286
2287 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2288 if (ret)
2289 return ret;
2290
2291 ret = snd_soc_set_ac97_ops(ops);
2292 if (ret)
2293 return ret;
2294
2295 ops->warm_reset = snd_soc_ac97_warm_reset;
2296 ops->reset = snd_soc_ac97_reset;
2297
2298 snd_ac97_rst_cfg = cfg;
2299 return 0;
2300}
2301EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2302
2303/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002304 * snd_soc_free_ac97_codec - free AC97 codec device
2305 * @codec: audio codec
2306 *
2307 * Frees AC97 codec device resources.
2308 */
2309void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2310{
2311 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002312#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002313 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002314#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002315 kfree(codec->ac97->bus);
2316 kfree(codec->ac97);
2317 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002318 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002319 mutex_unlock(&codec->mutex);
2320}
2321EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2322
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002323/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002324 * snd_soc_cnew - create new control
2325 * @_template: control template
2326 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002327 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002328 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002329 *
2330 * Create a new mixer control from a template control.
2331 *
2332 * Returns 0 for success, else error.
2333 */
2334struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002335 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002336 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002337{
2338 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002339 struct snd_kcontrol *kcontrol;
2340 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002341
2342 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002343 template.index = 0;
2344
Mark Brownefb7ac32011-03-08 17:23:24 +00002345 if (!long_name)
2346 long_name = template.name;
2347
2348 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002349 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002350 if (!name)
2351 return NULL;
2352
Mark Brownefb7ac32011-03-08 17:23:24 +00002353 template.name = name;
2354 } else {
2355 template.name = long_name;
2356 }
2357
2358 kcontrol = snd_ctl_new1(&template, data);
2359
2360 kfree(name);
2361
2362 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002363}
2364EXPORT_SYMBOL_GPL(snd_soc_cnew);
2365
Liam Girdwood022658b2012-02-03 17:43:09 +00002366static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2367 const struct snd_kcontrol_new *controls, int num_controls,
2368 const char *prefix, void *data)
2369{
2370 int err, i;
2371
2372 for (i = 0; i < num_controls; i++) {
2373 const struct snd_kcontrol_new *control = &controls[i];
2374 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2375 control->name, prefix));
2376 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002377 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2378 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002379 return err;
2380 }
2381 }
2382
2383 return 0;
2384}
2385
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002386struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2387 const char *name)
2388{
2389 struct snd_card *card = soc_card->snd_card;
2390 struct snd_kcontrol *kctl;
2391
2392 if (unlikely(!name))
2393 return NULL;
2394
2395 list_for_each_entry(kctl, &card->controls, list)
2396 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2397 return kctl;
2398 return NULL;
2399}
2400EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2401
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002402/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002403 * snd_soc_add_codec_controls - add an array of controls to a codec.
2404 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002405 * duplicating this code.
2406 *
2407 * @codec: codec to add controls to
2408 * @controls: array of controls to add
2409 * @num_controls: number of elements in the array
2410 *
2411 * Return 0 for success, else error.
2412 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002413int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002414 const struct snd_kcontrol_new *controls, int num_controls)
2415{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002416 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002417
Liam Girdwood022658b2012-02-03 17:43:09 +00002418 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02002419 codec->component.name_prefix, &codec->component);
Ian Molton3e8e1952009-01-09 00:23:21 +00002420}
Liam Girdwood022658b2012-02-03 17:43:09 +00002421EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002422
2423/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002424 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002425 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002426 *
2427 * @platform: platform to add controls to
2428 * @controls: array of controls to add
2429 * @num_controls: number of elements in the array
2430 *
2431 * Return 0 for success, else error.
2432 */
2433int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2434 const struct snd_kcontrol_new *controls, int num_controls)
2435{
2436 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002437
Liam Girdwood022658b2012-02-03 17:43:09 +00002438 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002439 NULL, &platform->component);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002440}
2441EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2442
2443/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002444 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2445 * Convenience function to add a list of controls.
2446 *
2447 * @soc_card: SoC card to add controls to
2448 * @controls: array of controls to add
2449 * @num_controls: number of elements in the array
2450 *
2451 * Return 0 for success, else error.
2452 */
2453int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2454 const struct snd_kcontrol_new *controls, int num_controls)
2455{
2456 struct snd_card *card = soc_card->snd_card;
2457
2458 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2459 NULL, soc_card);
2460}
2461EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2462
2463/**
2464 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2465 * Convienience function to add a list of controls.
2466 *
2467 * @dai: DAI to add controls to
2468 * @controls: array of controls to add
2469 * @num_controls: number of elements in the array
2470 *
2471 * Return 0 for success, else error.
2472 */
2473int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2474 const struct snd_kcontrol_new *controls, int num_controls)
2475{
2476 struct snd_card *card = dai->card->snd_card;
2477
2478 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2479 NULL, dai);
2480}
2481EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2482
2483/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002484 * snd_soc_info_enum_double - enumerated double mixer info callback
2485 * @kcontrol: mixer control
2486 * @uinfo: control element information
2487 *
2488 * Callback to provide information about a double enumerated
2489 * mixer control.
2490 *
2491 * Returns 0 for success.
2492 */
2493int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2494 struct snd_ctl_elem_info *uinfo)
2495{
2496 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2497
2498 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2499 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002500 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002501
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002502 if (uinfo->value.enumerated.item >= e->items)
2503 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002504 strlcpy(uinfo->value.enumerated.name,
2505 e->texts[uinfo->value.enumerated.item],
2506 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002507 return 0;
2508}
2509EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2510
2511/**
2512 * snd_soc_get_enum_double - enumerated double mixer get callback
2513 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002514 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002515 *
2516 * Callback to get the value of a double enumerated mixer.
2517 *
2518 * Returns 0 for success.
2519 */
2520int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2521 struct snd_ctl_elem_value *ucontrol)
2522{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002523 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002524 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002525 unsigned int val, item;
2526 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002527 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002528
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002529 ret = snd_soc_component_read(component, e->reg, &reg_val);
2530 if (ret)
2531 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002532 val = (reg_val >> e->shift_l) & e->mask;
2533 item = snd_soc_enum_val_to_item(e, val);
2534 ucontrol->value.enumerated.item[0] = item;
2535 if (e->shift_l != e->shift_r) {
2536 val = (reg_val >> e->shift_l) & e->mask;
2537 item = snd_soc_enum_val_to_item(e, val);
2538 ucontrol->value.enumerated.item[1] = item;
2539 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002540
2541 return 0;
2542}
2543EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2544
2545/**
2546 * snd_soc_put_enum_double - enumerated double mixer put callback
2547 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002548 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002549 *
2550 * Callback to set the value of a double enumerated mixer.
2551 *
2552 * Returns 0 for success.
2553 */
2554int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2555 struct snd_ctl_elem_value *ucontrol)
2556{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002557 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002558 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002559 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002560 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002561 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002562
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002563 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002564 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002565 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002566 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002567 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002568 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002569 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002570 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002571 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002572 }
2573
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002574 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002575}
2576EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2577
2578/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002579 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002580 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002581 * @reg: Register to read
2582 * @mask: Mask to use after shifting the register value
2583 * @shift: Right shift of register value
2584 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002585 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002586 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002587 * This functions reads a codec register. The register value is shifted right
2588 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2589 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002590 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002591 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002592 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002593static int snd_soc_read_signed(struct snd_soc_component *component,
2594 unsigned int reg, unsigned int mask, unsigned int shift,
2595 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002596{
Markus Pargmannf227b882014-01-16 16:02:10 +01002597 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002598 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002599
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002600 ret = snd_soc_component_read(component, reg, &val);
2601 if (ret < 0)
2602 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002603
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002604 val = (val >> shift) & mask;
2605
2606 if (!sign_bit) {
2607 *signed_val = val;
2608 return 0;
2609 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002610
2611 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002612 if (!(val & BIT(sign_bit))) {
2613 *signed_val = val;
2614 return 0;
2615 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002616
2617 ret = val;
2618
2619 /*
2620 * The register most probably does not contain a full-sized int.
2621 * Instead we have an arbitrary number of bits in a signed
2622 * representation which has to be translated into a full-sized int.
2623 * This is done by filling up all bits above the sign-bit.
2624 */
2625 ret |= ~((int)(BIT(sign_bit) - 1));
2626
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002627 *signed_val = ret;
2628
2629 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002630}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002631
2632/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002633 * snd_soc_info_volsw - single mixer info callback
2634 * @kcontrol: mixer control
2635 * @uinfo: control element information
2636 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002637 * Callback to provide information about a single mixer control, or a double
2638 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002639 *
2640 * Returns 0 for success.
2641 */
2642int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2643 struct snd_ctl_elem_info *uinfo)
2644{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002645 struct soc_mixer_control *mc =
2646 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002647 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002648
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002649 if (!mc->platform_max)
2650 mc->platform_max = mc->max;
2651 platform_max = mc->platform_max;
2652
2653 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002654 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2655 else
2656 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2657
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002658 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002659 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002660 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002661 return 0;
2662}
2663EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2664
2665/**
2666 * snd_soc_get_volsw - single mixer get callback
2667 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002668 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002669 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002670 * Callback to get the value of a single mixer control, or a double mixer
2671 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002672 *
2673 * Returns 0 for success.
2674 */
2675int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2676 struct snd_ctl_elem_value *ucontrol)
2677{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002678 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002679 struct soc_mixer_control *mc =
2680 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002681 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002682 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002683 unsigned int shift = mc->shift;
2684 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002685 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002686 int min = mc->min;
2687 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002688 unsigned int mask = (1 << fls(max)) - 1;
2689 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002690 int val;
2691 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002692
Markus Pargmannf227b882014-01-16 16:02:10 +01002693 if (sign_bit)
2694 mask = BIT(sign_bit + 1) - 1;
2695
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002696 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2697 if (ret)
2698 return ret;
2699
2700 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002701 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002702 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002703 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002704
2705 if (snd_soc_volsw_is_stereo(mc)) {
2706 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002707 ret = snd_soc_read_signed(component, reg, mask, rshift,
2708 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002709 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002710 ret = snd_soc_read_signed(component, reg2, mask, shift,
2711 sign_bit, &val);
2712 if (ret)
2713 return ret;
2714
2715 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002716 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002717 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002718 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002719 }
2720
2721 return 0;
2722}
2723EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2724
2725/**
2726 * snd_soc_put_volsw - single mixer put callback
2727 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002728 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002729 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002730 * Callback to set the value of a single mixer control, or a double mixer
2731 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002732 *
2733 * Returns 0 for success.
2734 */
2735int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2736 struct snd_ctl_elem_value *ucontrol)
2737{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002738 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002739 struct soc_mixer_control *mc =
2740 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002741 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002742 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002743 unsigned int shift = mc->shift;
2744 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002745 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002746 int min = mc->min;
2747 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002748 unsigned int mask = (1 << fls(max)) - 1;
2749 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002750 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002751 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002752 unsigned int val2 = 0;
2753 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002754
Markus Pargmannf227b882014-01-16 16:02:10 +01002755 if (sign_bit)
2756 mask = BIT(sign_bit + 1) - 1;
2757
2758 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002759 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002760 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002761 val_mask = mask << shift;
2762 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002763 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002764 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002765 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002766 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002767 if (reg == reg2) {
2768 val_mask |= mask << rshift;
2769 val |= val2 << rshift;
2770 } else {
2771 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002772 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002773 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002774 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002775 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002776 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002777 return err;
2778
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002779 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002780 err = snd_soc_component_update_bits(component, reg2, val_mask,
2781 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002782
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002783 return err;
2784}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002785EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002786
Mark Browne13ac2e2008-05-28 17:58:05 +01002787/**
Brian Austin1d99f242012-03-30 10:43:55 -05002788 * snd_soc_get_volsw_sx - single mixer get callback
2789 * @kcontrol: mixer control
2790 * @ucontrol: control element information
2791 *
2792 * Callback to get the value of a single mixer control, or a double mixer
2793 * control that spans 2 registers.
2794 *
2795 * Returns 0 for success.
2796 */
2797int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2798 struct snd_ctl_elem_value *ucontrol)
2799{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002800 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002801 struct soc_mixer_control *mc =
2802 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002803 unsigned int reg = mc->reg;
2804 unsigned int reg2 = mc->rreg;
2805 unsigned int shift = mc->shift;
2806 unsigned int rshift = mc->rshift;
2807 int max = mc->max;
2808 int min = mc->min;
2809 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002810 unsigned int val;
2811 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002812
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002813 ret = snd_soc_component_read(component, reg, &val);
2814 if (ret < 0)
2815 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002816
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002817 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2818
2819 if (snd_soc_volsw_is_stereo(mc)) {
2820 ret = snd_soc_component_read(component, reg2, &val);
2821 if (ret < 0)
2822 return ret;
2823
2824 val = ((val >> rshift) - min) & mask;
2825 ucontrol->value.integer.value[1] = val;
2826 }
Brian Austin1d99f242012-03-30 10:43:55 -05002827
2828 return 0;
2829}
2830EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2831
2832/**
2833 * snd_soc_put_volsw_sx - double mixer set callback
2834 * @kcontrol: mixer control
2835 * @uinfo: control element information
2836 *
2837 * Callback to set the value of a double mixer control that spans 2 registers.
2838 *
2839 * Returns 0 for success.
2840 */
2841int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2842 struct snd_ctl_elem_value *ucontrol)
2843{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002844 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002845 struct soc_mixer_control *mc =
2846 (struct soc_mixer_control *)kcontrol->private_value;
2847
2848 unsigned int reg = mc->reg;
2849 unsigned int reg2 = mc->rreg;
2850 unsigned int shift = mc->shift;
2851 unsigned int rshift = mc->rshift;
2852 int max = mc->max;
2853 int min = mc->min;
2854 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002855 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002856 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002857
2858 val_mask = mask << shift;
2859 val = (ucontrol->value.integer.value[0] + min) & mask;
2860 val = val << shift;
2861
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002862 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302863 if (err < 0)
2864 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002865
2866 if (snd_soc_volsw_is_stereo(mc)) {
2867 val_mask = mask << rshift;
2868 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2869 val2 = val2 << rshift;
2870
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002871 err = snd_soc_component_update_bits(component, reg2, val_mask,
2872 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002873 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002874 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002875}
2876EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2877
2878/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002879 * snd_soc_info_volsw_s8 - signed mixer info callback
2880 * @kcontrol: mixer control
2881 * @uinfo: control element information
2882 *
2883 * Callback to provide information about a signed mixer control.
2884 *
2885 * Returns 0 for success.
2886 */
2887int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2888 struct snd_ctl_elem_info *uinfo)
2889{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002890 struct soc_mixer_control *mc =
2891 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002892 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002893 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002894
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002895 if (!mc->platform_max)
2896 mc->platform_max = mc->max;
2897 platform_max = mc->platform_max;
2898
Mark Browne13ac2e2008-05-28 17:58:05 +01002899 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2900 uinfo->count = 2;
2901 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002902 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002903 return 0;
2904}
2905EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2906
2907/**
2908 * snd_soc_get_volsw_s8 - signed mixer get callback
2909 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002910 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002911 *
2912 * Callback to get the value of a signed mixer control.
2913 *
2914 * Returns 0 for success.
2915 */
2916int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2917 struct snd_ctl_elem_value *ucontrol)
2918{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002919 struct soc_mixer_control *mc =
2920 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002921 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002922 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002923 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002924 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002925 int ret;
2926
2927 ret = snd_soc_component_read(component, reg, &val);
2928 if (ret)
2929 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002930
2931 ucontrol->value.integer.value[0] =
2932 ((signed char)(val & 0xff))-min;
2933 ucontrol->value.integer.value[1] =
2934 ((signed char)((val >> 8) & 0xff))-min;
2935 return 0;
2936}
2937EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2938
2939/**
2940 * snd_soc_put_volsw_sgn - signed mixer put callback
2941 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002942 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002943 *
2944 * Callback to set the value of a signed mixer control.
2945 *
2946 * Returns 0 for success.
2947 */
2948int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2949 struct snd_ctl_elem_value *ucontrol)
2950{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002951 struct soc_mixer_control *mc =
2952 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002953 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002954 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002955 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002956 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002957
2958 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2959 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2960
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002961 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002962}
2963EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2964
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002965/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002966 * snd_soc_info_volsw_range - single mixer info callback with range.
2967 * @kcontrol: mixer control
2968 * @uinfo: control element information
2969 *
2970 * Callback to provide information, within a range, about a single
2971 * mixer control.
2972 *
2973 * returns 0 for success.
2974 */
2975int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2976 struct snd_ctl_elem_info *uinfo)
2977{
2978 struct soc_mixer_control *mc =
2979 (struct soc_mixer_control *)kcontrol->private_value;
2980 int platform_max;
2981 int min = mc->min;
2982
2983 if (!mc->platform_max)
2984 mc->platform_max = mc->max;
2985 platform_max = mc->platform_max;
2986
2987 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002988 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002989 uinfo->value.integer.min = 0;
2990 uinfo->value.integer.max = platform_max - min;
2991
2992 return 0;
2993}
2994EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2995
2996/**
2997 * snd_soc_put_volsw_range - single mixer put value callback with range.
2998 * @kcontrol: mixer control
2999 * @ucontrol: control element information
3000 *
3001 * Callback to set the value, within a range, for a single mixer control.
3002 *
3003 * Returns 0 for success.
3004 */
3005int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
3006 struct snd_ctl_elem_value *ucontrol)
3007{
3008 struct soc_mixer_control *mc =
3009 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003010 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003011 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003012 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003013 unsigned int shift = mc->shift;
3014 int min = mc->min;
3015 int max = mc->max;
3016 unsigned int mask = (1 << fls(max)) - 1;
3017 unsigned int invert = mc->invert;
3018 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003019 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003020
3021 val = ((ucontrol->value.integer.value[0] + min) & mask);
3022 if (invert)
3023 val = max - val;
3024 val_mask = mask << shift;
3025 val = val << shift;
3026
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003027 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09003028 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00003029 return ret;
3030
3031 if (snd_soc_volsw_is_stereo(mc)) {
3032 val = ((ucontrol->value.integer.value[1] + min) & mask);
3033 if (invert)
3034 val = max - val;
3035 val_mask = mask << shift;
3036 val = val << shift;
3037
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003038 ret = snd_soc_component_update_bits(component, rreg, val_mask,
3039 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00003040 }
3041
3042 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003043}
3044EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3045
3046/**
3047 * snd_soc_get_volsw_range - single mixer get callback with range
3048 * @kcontrol: mixer control
3049 * @ucontrol: control element information
3050 *
3051 * Callback to get the value, within a range, of a single mixer control.
3052 *
3053 * Returns 0 for success.
3054 */
3055int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3056 struct snd_ctl_elem_value *ucontrol)
3057{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003058 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003059 struct soc_mixer_control *mc =
3060 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003061 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003062 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003063 unsigned int shift = mc->shift;
3064 int min = mc->min;
3065 int max = mc->max;
3066 unsigned int mask = (1 << fls(max)) - 1;
3067 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003068 unsigned int val;
3069 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003070
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003071 ret = snd_soc_component_read(component, reg, &val);
3072 if (ret)
3073 return ret;
3074
3075 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003076 if (invert)
3077 ucontrol->value.integer.value[0] =
3078 max - ucontrol->value.integer.value[0];
3079 ucontrol->value.integer.value[0] =
3080 ucontrol->value.integer.value[0] - min;
3081
Mark Brown9bde4f02012-12-19 16:05:00 +00003082 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003083 ret = snd_soc_component_read(component, rreg, &val);
3084 if (ret)
3085 return ret;
3086
3087 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003088 if (invert)
3089 ucontrol->value.integer.value[1] =
3090 max - ucontrol->value.integer.value[1];
3091 ucontrol->value.integer.value[1] =
3092 ucontrol->value.integer.value[1] - min;
3093 }
3094
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003095 return 0;
3096}
3097EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3098
3099/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003100 * snd_soc_limit_volume - Set new limit to an existing volume control.
3101 *
3102 * @codec: where to look for the control
3103 * @name: Name of the control
3104 * @max: new maximum limit
3105 *
3106 * Return 0 for success, else error.
3107 */
3108int snd_soc_limit_volume(struct snd_soc_codec *codec,
3109 const char *name, int max)
3110{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003111 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003112 struct snd_kcontrol *kctl;
3113 struct soc_mixer_control *mc;
3114 int found = 0;
3115 int ret = -EINVAL;
3116
3117 /* Sanity check for name and max */
3118 if (unlikely(!name || max <= 0))
3119 return -EINVAL;
3120
3121 list_for_each_entry(kctl, &card->controls, list) {
3122 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3123 found = 1;
3124 break;
3125 }
3126 }
3127 if (found) {
3128 mc = (struct soc_mixer_control *)kctl->private_value;
3129 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003130 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003131 ret = 0;
3132 }
3133 }
3134 return ret;
3135}
3136EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3137
Mark Brown71d08512011-10-10 18:31:26 +01003138int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3139 struct snd_ctl_elem_info *uinfo)
3140{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003141 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003142 struct soc_bytes *params = (void *)kcontrol->private_value;
3143
3144 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003145 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003146
3147 return 0;
3148}
3149EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3150
3151int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3152 struct snd_ctl_elem_value *ucontrol)
3153{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003154 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003155 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003156 int ret;
3157
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003158 if (component->regmap)
3159 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003160 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003161 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003162 else
3163 ret = -EINVAL;
3164
Mark Brownf831b052012-02-17 16:20:33 -08003165 /* Hide any masked bytes to ensure consistent data reporting */
3166 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003167 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003168 case 1:
3169 ucontrol->value.bytes.data[0] &= ~params->mask;
3170 break;
3171 case 2:
3172 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003173 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003174 break;
3175 case 4:
3176 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003177 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003178 break;
3179 default:
3180 return -EINVAL;
3181 }
3182 }
3183
Mark Brown71d08512011-10-10 18:31:26 +01003184 return ret;
3185}
3186EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3187
3188int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3189 struct snd_ctl_elem_value *ucontrol)
3190{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003191 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003192 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003193 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003194 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003195 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003196
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003197 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003198 return -EINVAL;
3199
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003200 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003201
Mark Brownb5a8fe42013-01-20 21:42:22 +09003202 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3203 if (!data)
3204 return -ENOMEM;
3205
Mark Brownf831b052012-02-17 16:20:33 -08003206 /*
3207 * If we've got a mask then we need to preserve the register
3208 * bits. We shouldn't modify the incoming data so take a
3209 * copy.
3210 */
3211 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003212 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003213 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003214 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003215
3216 val &= params->mask;
3217
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003218 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003219 case 1:
3220 ((u8 *)data)[0] &= ~params->mask;
3221 ((u8 *)data)[0] |= val;
3222 break;
3223 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003224 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003225 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003226 &mask, &mask);
3227 if (ret != 0)
3228 goto out;
3229
3230 ((u16 *)data)[0] &= mask;
3231
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003232 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003233 &val, &val);
3234 if (ret != 0)
3235 goto out;
3236
3237 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003238 break;
3239 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003240 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003241 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003242 &mask, &mask);
3243 if (ret != 0)
3244 goto out;
3245
3246 ((u32 *)data)[0] &= mask;
3247
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003248 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003249 &val, &val);
3250 if (ret != 0)
3251 goto out;
3252
3253 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003254 break;
3255 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003256 ret = -EINVAL;
3257 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003258 }
3259 }
3260
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003261 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003262 data, len);
3263
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003264out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003265 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003266
3267 return ret;
3268}
3269EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3270
Vinod Kould9881202014-05-02 10:58:11 +05303271int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3272 struct snd_ctl_elem_info *ucontrol)
3273{
3274 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3275
3276 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3277 ucontrol->count = params->max;
3278
3279 return 0;
3280}
3281EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3282
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003283/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003284 * snd_soc_info_xr_sx - signed multi register info callback
3285 * @kcontrol: mreg control
3286 * @uinfo: control element information
3287 *
3288 * Callback to provide information of a control that can
3289 * span multiple codec registers which together
3290 * forms a single signed value in a MSB/LSB manner.
3291 *
3292 * Returns 0 for success.
3293 */
3294int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3295 struct snd_ctl_elem_info *uinfo)
3296{
3297 struct soc_mreg_control *mc =
3298 (struct soc_mreg_control *)kcontrol->private_value;
3299 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3300 uinfo->count = 1;
3301 uinfo->value.integer.min = mc->min;
3302 uinfo->value.integer.max = mc->max;
3303
3304 return 0;
3305}
3306EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3307
3308/**
3309 * snd_soc_get_xr_sx - signed multi register get callback
3310 * @kcontrol: mreg control
3311 * @ucontrol: control element information
3312 *
3313 * Callback to get the value of a control that can span
3314 * multiple codec registers which together forms a single
3315 * signed value in a MSB/LSB manner. The control supports
3316 * specifying total no of bits used to allow for bitfields
3317 * across the multiple codec registers.
3318 *
3319 * Returns 0 for success.
3320 */
3321int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3322 struct snd_ctl_elem_value *ucontrol)
3323{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003324 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003325 struct soc_mreg_control *mc =
3326 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003327 unsigned int regbase = mc->regbase;
3328 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003329 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003330 unsigned int regwmask = (1<<regwshift)-1;
3331 unsigned int invert = mc->invert;
3332 unsigned long mask = (1UL<<mc->nbits)-1;
3333 long min = mc->min;
3334 long max = mc->max;
3335 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003336 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003337 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003338 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003339
3340 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003341 ret = snd_soc_component_read(component, regbase+i, &regval);
3342 if (ret)
3343 return ret;
3344 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003345 }
3346 val &= mask;
3347 if (min < 0 && val > max)
3348 val |= ~mask;
3349 if (invert)
3350 val = max - val;
3351 ucontrol->value.integer.value[0] = val;
3352
3353 return 0;
3354}
3355EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3356
3357/**
3358 * snd_soc_put_xr_sx - signed multi register get callback
3359 * @kcontrol: mreg control
3360 * @ucontrol: control element information
3361 *
3362 * Callback to set the value of a control that can span
3363 * multiple codec registers which together forms a single
3364 * signed value in a MSB/LSB manner. The control supports
3365 * specifying total no of bits used to allow for bitfields
3366 * across the multiple codec registers.
3367 *
3368 * Returns 0 for success.
3369 */
3370int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3371 struct snd_ctl_elem_value *ucontrol)
3372{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003373 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003374 struct soc_mreg_control *mc =
3375 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003376 unsigned int regbase = mc->regbase;
3377 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003378 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003379 unsigned int regwmask = (1<<regwshift)-1;
3380 unsigned int invert = mc->invert;
3381 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003382 long max = mc->max;
3383 long val = ucontrol->value.integer.value[0];
3384 unsigned int i, regval, regmask;
3385 int err;
3386
3387 if (invert)
3388 val = max - val;
3389 val &= mask;
3390 for (i = 0; i < regcount; i++) {
3391 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3392 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003393 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003394 regmask, regval);
3395 if (err < 0)
3396 return err;
3397 }
3398
3399 return 0;
3400}
3401EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3402
3403/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003404 * snd_soc_get_strobe - strobe get callback
3405 * @kcontrol: mixer control
3406 * @ucontrol: control element information
3407 *
3408 * Callback get the value of a strobe mixer control.
3409 *
3410 * Returns 0 for success.
3411 */
3412int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3413 struct snd_ctl_elem_value *ucontrol)
3414{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003415 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003416 struct soc_mixer_control *mc =
3417 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003418 unsigned int reg = mc->reg;
3419 unsigned int shift = mc->shift;
3420 unsigned int mask = 1 << shift;
3421 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003422 unsigned int val;
3423 int ret;
3424
3425 ret = snd_soc_component_read(component, reg, &val);
3426 if (ret)
3427 return ret;
3428
3429 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003430
3431 if (shift != 0 && val != 0)
3432 val = val >> shift;
3433 ucontrol->value.enumerated.item[0] = val ^ invert;
3434
3435 return 0;
3436}
3437EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3438
3439/**
3440 * snd_soc_put_strobe - strobe put callback
3441 * @kcontrol: mixer control
3442 * @ucontrol: control element information
3443 *
3444 * Callback strobe a register bit to high then low (or the inverse)
3445 * in one pass of a single mixer enum control.
3446 *
3447 * Returns 1 for success.
3448 */
3449int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3450 struct snd_ctl_elem_value *ucontrol)
3451{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003452 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003453 struct soc_mixer_control *mc =
3454 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003455 unsigned int reg = mc->reg;
3456 unsigned int shift = mc->shift;
3457 unsigned int mask = 1 << shift;
3458 unsigned int invert = mc->invert != 0;
3459 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3460 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3461 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3462 int err;
3463
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003464 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003465 if (err < 0)
3466 return err;
3467
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003468 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003469}
3470EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3471
3472/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003473 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3474 * @dai: DAI
3475 * @clk_id: DAI specific clock ID
3476 * @freq: new clock frequency in Hz
3477 * @dir: new clock direction - input/output.
3478 *
3479 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3480 */
3481int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3482 unsigned int freq, int dir)
3483{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003484 if (dai->driver && dai->driver->ops->set_sysclk)
3485 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003486 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003487 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003488 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003489 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003490 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003491}
3492EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3493
3494/**
Mark Brownec4ee522011-03-07 20:58:11 +00003495 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3496 * @codec: CODEC
3497 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003498 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003499 * @freq: new clock frequency in Hz
3500 * @dir: new clock direction - input/output.
3501 *
3502 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3503 */
3504int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003505 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003506{
3507 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003508 return codec->driver->set_sysclk(codec, clk_id, source,
3509 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003510 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003511 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003512}
3513EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3514
3515/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003516 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3517 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003518 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003519 * @div: new clock divisor.
3520 *
3521 * Configures the clock dividers. This is used to derive the best DAI bit and
3522 * frame clocks from the system or master clock. It's best to set the DAI bit
3523 * and frame clocks as low as possible to save system power.
3524 */
3525int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3526 int div_id, int div)
3527{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003528 if (dai->driver && dai->driver->ops->set_clkdiv)
3529 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003530 else
3531 return -EINVAL;
3532}
3533EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3534
3535/**
3536 * snd_soc_dai_set_pll - configure DAI PLL.
3537 * @dai: DAI
3538 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003539 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003540 * @freq_in: PLL input clock frequency in Hz
3541 * @freq_out: requested PLL output clock frequency in Hz
3542 *
3543 * Configures and enables PLL to generate output clock based on input clock.
3544 */
Mark Brown85488032009-09-05 18:52:16 +01003545int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3546 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003547{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003548 if (dai->driver && dai->driver->ops->set_pll)
3549 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003550 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003551 else if (dai->codec && dai->codec->driver->set_pll)
3552 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3553 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003554 else
3555 return -EINVAL;
3556}
3557EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3558
Mark Brownec4ee522011-03-07 20:58:11 +00003559/*
3560 * snd_soc_codec_set_pll - configure codec PLL.
3561 * @codec: CODEC
3562 * @pll_id: DAI specific PLL ID
3563 * @source: DAI specific source for the PLL
3564 * @freq_in: PLL input clock frequency in Hz
3565 * @freq_out: requested PLL output clock frequency in Hz
3566 *
3567 * Configures and enables PLL to generate output clock based on input clock.
3568 */
3569int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3570 unsigned int freq_in, unsigned int freq_out)
3571{
3572 if (codec->driver->set_pll)
3573 return codec->driver->set_pll(codec, pll_id, source,
3574 freq_in, freq_out);
3575 else
3576 return -EINVAL;
3577}
3578EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3579
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003580/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003581 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3582 * @dai: DAI
3583 * @ratio Ratio of BCLK to Sample rate.
3584 *
3585 * Configures the DAI for a preset BCLK to sample rate ratio.
3586 */
3587int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3588{
3589 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3590 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3591 else
3592 return -EINVAL;
3593}
3594EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3595
3596/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003597 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3598 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003599 * @fmt: SND_SOC_DAIFMT_ format value.
3600 *
3601 * Configures the DAI hardware format and clocking.
3602 */
3603int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3604{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003605 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003606 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003607 if (dai->driver->ops->set_fmt == NULL)
3608 return -ENOTSUPP;
3609 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003610}
3611EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3612
3613/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003614 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003615 * @slots: Number of slots in use.
3616 * @tx_mask: bitmask representing active TX slots.
3617 * @rx_mask: bitmask representing active RX slots.
3618 *
3619 * Generates the TDM tx and rx slot default masks for DAI.
3620 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003621static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003622 unsigned int *tx_mask,
3623 unsigned int *rx_mask)
3624{
3625 if (*tx_mask || *rx_mask)
3626 return 0;
3627
3628 if (!slots)
3629 return -EINVAL;
3630
3631 *tx_mask = (1 << slots) - 1;
3632 *rx_mask = (1 << slots) - 1;
3633
3634 return 0;
3635}
3636
3637/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003638 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3639 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003640 * @tx_mask: bitmask representing active TX slots.
3641 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003642 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003643 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003644 *
3645 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3646 * specific.
3647 */
3648int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003649 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003650{
Xiubo Lie5c21512014-03-21 14:17:12 +08003651 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3652 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003653 &tx_mask, &rx_mask);
3654 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003655 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003656
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003657 if (dai->driver && dai->driver->ops->set_tdm_slot)
3658 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003659 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003660 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003661 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003662}
3663EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3664
3665/**
Barry Song472df3c2009-09-12 01:16:29 +08003666 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3667 * @dai: DAI
3668 * @tx_num: how many TX channels
3669 * @tx_slot: pointer to an array which imply the TX slot number channel
3670 * 0~num-1 uses
3671 * @rx_num: how many RX channels
3672 * @rx_slot: pointer to an array which imply the RX slot number channel
3673 * 0~num-1 uses
3674 *
3675 * configure the relationship between channel number and TDM slot number.
3676 */
3677int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3678 unsigned int tx_num, unsigned int *tx_slot,
3679 unsigned int rx_num, unsigned int *rx_slot)
3680{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003681 if (dai->driver && dai->driver->ops->set_channel_map)
3682 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003683 rx_num, rx_slot);
3684 else
3685 return -EINVAL;
3686}
3687EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3688
3689/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003690 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3691 * @dai: DAI
3692 * @tristate: tristate enable
3693 *
3694 * Tristates the DAI so that others can use it.
3695 */
3696int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3697{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003698 if (dai->driver && dai->driver->ops->set_tristate)
3699 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003700 else
3701 return -EINVAL;
3702}
3703EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3704
3705/**
3706 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3707 * @dai: DAI
3708 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003709 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003710 *
3711 * Mutes the DAI DAC.
3712 */
Mark Brownda183962013-02-06 15:44:07 +00003713int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3714 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003715{
Mark Brownda183962013-02-06 15:44:07 +00003716 if (!dai->driver)
3717 return -ENOTSUPP;
3718
3719 if (dai->driver->ops->mute_stream)
3720 return dai->driver->ops->mute_stream(dai, mute, direction);
3721 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3722 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003723 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003724 else
Mark Brown04570c62012-04-13 19:16:03 +01003725 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003726}
3727EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3728
Mark Brownc5af3a22008-11-28 13:29:45 +00003729/**
3730 * snd_soc_register_card - Register a card with the ASoC core
3731 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003732 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003733 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003734 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303735int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003736{
Mark Brownb19e6e72012-03-14 21:18:39 +00003737 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003738
Mark Brownc5af3a22008-11-28 13:29:45 +00003739 if (!card->name || !card->dev)
3740 return -EINVAL;
3741
Stephen Warren5a504962011-12-21 10:40:59 -07003742 for (i = 0; i < card->num_links; i++) {
3743 struct snd_soc_dai_link *link = &card->dai_link[i];
3744
3745 /*
3746 * Codec must be specified by 1 of name or OF node,
3747 * not both or neither.
3748 */
3749 if (!!link->codec_name == !!link->codec_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003750 dev_err(card->dev,
3751 "ASoC: Neither/both codec name/of_node are set for %s\n",
3752 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003753 return -EINVAL;
3754 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003755 /* Codec DAI name must be specified */
3756 if (!link->codec_dai_name) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003757 dev_err(card->dev,
3758 "ASoC: codec_dai_name not set for %s\n",
3759 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003760 return -EINVAL;
3761 }
Stephen Warren5a504962011-12-21 10:40:59 -07003762
3763 /*
3764 * Platform may be specified by either name or OF node, but
3765 * can be left unspecified, and a dummy platform will be used.
3766 */
3767 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003768 dev_err(card->dev,
3769 "ASoC: Both platform name/of_node are set for %s\n",
3770 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003771 return -EINVAL;
3772 }
3773
3774 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003775 * CPU device may be specified by either name or OF node, but
3776 * can be left unspecified, and will be matched based on DAI
3777 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003778 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003779 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003780 dev_err(card->dev,
3781 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3782 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003783 return -EINVAL;
3784 }
3785 /*
3786 * At least one of CPU DAI name or CPU device name/node must be
3787 * specified
3788 */
3789 if (!link->cpu_dai_name &&
3790 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003791 dev_err(card->dev,
3792 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3793 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003794 return -EINVAL;
3795 }
3796 }
3797
Mark Browned77cc12011-05-03 18:25:34 +01003798 dev_set_drvdata(card->dev, card);
3799
Stephen Warren111c6412011-01-28 14:26:35 -07003800 snd_soc_initialize_card_lists(card);
3801
Vinod Koul150dd2f2011-01-13 22:48:32 +05303802 soc_init_card_debugfs(card);
3803
Mark Brown181a6892012-03-14 20:18:49 +00003804 card->rtd = devm_kzalloc(card->dev,
3805 sizeof(struct snd_soc_pcm_runtime) *
3806 (card->num_links + card->num_aux_devs),
3807 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003808 if (card->rtd == NULL)
3809 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003810 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003811 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003812
3813 for (i = 0; i < card->num_links; i++)
3814 card->rtd[i].dai_link = &card->dai_link[i];
3815
Mark Browndb432b42011-10-03 21:06:40 +01003816 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003817 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003818 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003819 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003820
Mark Brownb19e6e72012-03-14 21:18:39 +00003821 ret = snd_soc_instantiate_card(card);
3822 if (ret != 0)
3823 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003824
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003825 /* deactivate pins to sleep state */
3826 for (i = 0; i < card->num_rtd; i++) {
3827 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3828 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3829 if (!codec_dai->active)
3830 pinctrl_pm_select_sleep_state(codec_dai->dev);
3831 if (!cpu_dai->active)
3832 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3833 }
3834
Mark Brownb19e6e72012-03-14 21:18:39 +00003835 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003836}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303837EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003838
3839/**
3840 * snd_soc_unregister_card - Unregister a card with the ASoC core
3841 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003842 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003843 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003844 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303845int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003846{
Vinod Koulb0e26482011-01-13 22:48:02 +05303847 if (card->instantiated)
3848 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003849 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003850
3851 return 0;
3852}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303853EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003854
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003855/*
3856 * Simplify DAI link configuration by removing ".-1" from device names
3857 * and sanitizing names.
3858 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003859static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003860{
3861 char *found, name[NAME_SIZE];
3862 int id1, id2;
3863
3864 if (dev_name(dev) == NULL)
3865 return NULL;
3866
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003867 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003868
3869 /* are we a "%s.%d" name (platform and SPI components) */
3870 found = strstr(name, dev->driver->name);
3871 if (found) {
3872 /* get ID */
3873 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3874
3875 /* discard ID from name if ID == -1 */
3876 if (*id == -1)
3877 found[strlen(dev->driver->name)] = '\0';
3878 }
3879
3880 } else {
3881 /* I2C component devices are named "bus-addr" */
3882 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3883 char tmp[NAME_SIZE];
3884
3885 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003886 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003887
3888 /* sanitize component name for DAI link creation */
3889 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003890 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003891 } else
3892 *id = 0;
3893 }
3894
3895 return kstrdup(name, GFP_KERNEL);
3896}
3897
3898/*
3899 * Simplify DAI link naming for single devices with multiple DAIs by removing
3900 * any ".-1" and using the DAI name (instead of device name).
3901 */
3902static inline char *fmt_multiple_name(struct device *dev,
3903 struct snd_soc_dai_driver *dai_drv)
3904{
3905 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003906 dev_err(dev,
3907 "ASoC: error - multiple DAI %s registered with no name\n",
3908 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003909 return NULL;
3910 }
3911
3912 return kstrdup(dai_drv->name, GFP_KERNEL);
3913}
3914
Mark Brown91151712008-11-30 23:31:24 +00003915/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003916 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003917 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003918 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003919 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003920static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003921{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003922 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003923
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003924 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003925 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3926 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003927 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003928 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003929 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003930 }
Mark Brown91151712008-11-30 23:31:24 +00003931}
Mark Brown91151712008-11-30 23:31:24 +00003932
3933/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003934 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003935 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003936 * @component: The component the DAIs are registered for
3937 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003938 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003939 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3940 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003941 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003942static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003943 struct snd_soc_dai_driver *dai_drv, size_t count,
3944 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003945{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003946 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003947 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003948 unsigned int i;
3949 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003950
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003951 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003952
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003953 component->dai_drv = dai_drv;
3954 component->num_dai = count;
3955
Mark Brown91151712008-11-30 23:31:24 +00003956 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003957
3958 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003959 if (dai == NULL) {
3960 ret = -ENOMEM;
3961 goto err;
3962 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003963
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003964 /*
3965 * Back in the old days when we still had component-less DAIs,
3966 * instead of having a static name, component-less DAIs would
3967 * inherit the name of the parent device so it is possible to
3968 * register multiple instances of the DAI. We still need to keep
3969 * the same naming style even though those DAIs are not
3970 * component-less anymore.
3971 */
3972 if (count == 1 && legacy_dai_naming) {
3973 dai->name = fmt_single_name(dev, &dai->id);
3974 } else {
3975 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3976 if (dai_drv[i].id)
3977 dai->id = dai_drv[i].id;
3978 else
3979 dai->id = i;
3980 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003981 if (dai->name == NULL) {
3982 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003983 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003984 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003985 }
3986
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003987 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003988 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003989 dai->driver = &dai_drv[i];
3990 if (!dai->driver->ops)
3991 dai->driver->ops = &null_dai_ops;
3992
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003993 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003994
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003995 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003996 }
3997
3998 return 0;
3999
4000err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004001 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00004002
4003 return ret;
4004}
Mark Brown91151712008-11-30 23:31:24 +00004005
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004006static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
4007 enum snd_soc_dapm_type type, int subseq)
4008{
4009 struct snd_soc_component *component = dapm->component;
4010
4011 component->driver->seq_notifier(component, type, subseq);
4012}
4013
4014static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
4015 int event)
4016{
4017 struct snd_soc_component *component = dapm->component;
4018
4019 return component->driver->stream_event(component, event);
4020}
4021
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004022static int snd_soc_component_initialize(struct snd_soc_component *component,
4023 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004024{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004025 struct snd_soc_dapm_context *dapm;
4026
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004027 component->name = fmt_single_name(dev, &component->id);
4028 if (!component->name) {
4029 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004030 return -ENOMEM;
4031 }
4032
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004033 component->dev = dev;
4034 component->driver = driver;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004035
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004036 if (!component->dapm_ptr)
4037 component->dapm_ptr = &component->dapm;
4038
4039 dapm = component->dapm_ptr;
4040 dapm->dev = dev;
4041 dapm->component = component;
4042 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004043 if (driver->seq_notifier)
4044 dapm->seq_notifier = snd_soc_component_seq_notifier;
4045 if (driver->stream_event)
4046 dapm->stream_event = snd_soc_component_stream_event;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004047
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004048 INIT_LIST_HEAD(&component->dai_list);
4049 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004050
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004051 return 0;
4052}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004053
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004054static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4055{
4056 list_add(&component->list, &component_list);
4057}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004058
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004059static void snd_soc_component_add(struct snd_soc_component *component)
4060{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004061 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004062 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004063 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004064}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004065
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004066static void snd_soc_component_cleanup(struct snd_soc_component *component)
4067{
4068 snd_soc_unregister_dais(component);
4069 kfree(component->name);
4070}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004071
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004072static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4073{
4074 list_del(&component->list);
4075}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004076
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004077static void snd_soc_component_del(struct snd_soc_component *component)
4078{
4079 mutex_lock(&client_mutex);
4080 snd_soc_component_del_unlocked(component);
4081 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004082}
4083
4084int snd_soc_register_component(struct device *dev,
4085 const struct snd_soc_component_driver *cmpnt_drv,
4086 struct snd_soc_dai_driver *dai_drv,
4087 int num_dai)
4088{
4089 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004090 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004091
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004092 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004093 if (!cmpnt) {
4094 dev_err(dev, "ASoC: Failed to allocate memory\n");
4095 return -ENOMEM;
4096 }
4097
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004098 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4099 if (ret)
4100 goto err_free;
4101
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004102 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004103 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004104
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004105 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4106 if (ret < 0) {
4107 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4108 goto err_cleanup;
4109 }
4110
4111 snd_soc_component_add(cmpnt);
4112
4113 return 0;
4114
4115err_cleanup:
4116 snd_soc_component_cleanup(cmpnt);
4117err_free:
4118 kfree(cmpnt);
4119 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004120}
4121EXPORT_SYMBOL_GPL(snd_soc_register_component);
4122
4123/**
4124 * snd_soc_unregister_component - Unregister a component from the ASoC core
4125 *
4126 */
4127void snd_soc_unregister_component(struct device *dev)
4128{
4129 struct snd_soc_component *cmpnt;
4130
4131 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004132 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004133 goto found;
4134 }
4135 return;
4136
4137found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004138 snd_soc_component_del(cmpnt);
4139 snd_soc_component_cleanup(cmpnt);
4140 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004141}
4142EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4143
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004144static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4145 unsigned int reg, unsigned int val)
4146{
4147 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4148
4149 return platform->driver->write(platform, reg, val);
4150}
4151
4152static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4153 unsigned int reg, unsigned int *val)
4154{
4155 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4156
4157 *val = platform->driver->read(platform, reg);
4158
4159 return 0;
4160}
4161
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004162/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004163 * snd_soc_add_platform - Add a platform to the ASoC core
4164 * @dev: The parent device for the platform
4165 * @platform: The platform to add
4166 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004167 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004168int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57db82013-03-27 12:02:22 +01004169 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004170{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004171 int ret;
4172
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004173 ret = snd_soc_component_initialize(&platform->component,
4174 &platform_drv->component_driver, dev);
4175 if (ret)
4176 return ret;
4177
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004178 platform->dev = dev;
4179 platform->driver = platform_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004180 if (platform_drv->write)
4181 platform->component.write = snd_soc_platform_drv_write;
4182 if (platform_drv->read)
4183 platform->component.read = snd_soc_platform_drv_read;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004184
4185 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004186 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004187 list_add(&platform->list, &platform_list);
4188 mutex_unlock(&client_mutex);
4189
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004190 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4191 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004192
4193 return 0;
4194}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004195EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4196
4197/**
4198 * snd_soc_register_platform - Register a platform with the ASoC core
4199 *
4200 * @platform: platform to register
4201 */
4202int snd_soc_register_platform(struct device *dev,
4203 const struct snd_soc_platform_driver *platform_drv)
4204{
4205 struct snd_soc_platform *platform;
4206 int ret;
4207
4208 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4209
4210 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4211 if (platform == NULL)
4212 return -ENOMEM;
4213
4214 ret = snd_soc_add_platform(dev, platform, platform_drv);
4215 if (ret)
4216 kfree(platform);
4217
4218 return ret;
4219}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004220EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4221
4222/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004223 * snd_soc_remove_platform - Remove a platform from the ASoC core
4224 * @platform: the platform to remove
4225 */
4226void snd_soc_remove_platform(struct snd_soc_platform *platform)
4227{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004228
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004229 mutex_lock(&client_mutex);
4230 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004231 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004232 mutex_unlock(&client_mutex);
4233
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004234 snd_soc_component_cleanup(&platform->component);
4235
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004236 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004237 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004238}
4239EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4240
4241struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4242{
4243 struct snd_soc_platform *platform;
4244
4245 list_for_each_entry(platform, &platform_list, list) {
4246 if (dev == platform->dev)
4247 return platform;
4248 }
4249
4250 return NULL;
4251}
4252EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4253
4254/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004255 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4256 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004257 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004258 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004259void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004260{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004261 struct snd_soc_platform *platform;
4262
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004263 platform = snd_soc_lookup_platform(dev);
4264 if (!platform)
4265 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004266
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004267 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004268 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004269}
4270EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4271
Mark Brown151ab222009-05-09 16:22:58 +01004272static u64 codec_format_map[] = {
4273 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4274 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4275 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4276 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4277 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4278 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4279 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4280 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4281 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4282 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4283 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4284 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4285 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4286 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4287 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4288 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4289};
4290
4291/* Fix up the DAI formats for endianness: codecs don't actually see
4292 * the endianness of the data but we're using the CPU format
4293 * definitions which do need to include endianness so we ensure that
4294 * codec DAIs always have both big and little endian variants set.
4295 */
4296static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4297{
4298 int i;
4299
4300 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4301 if (stream->formats & codec_format_map[i])
4302 stream->formats |= codec_format_map[i];
4303}
4304
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004305static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4306 unsigned int reg, unsigned int val)
4307{
4308 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4309
4310 return codec->driver->write(codec, reg, val);
4311}
4312
4313static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4314 unsigned int reg, unsigned int *val)
4315{
4316 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4317
4318 *val = codec->driver->read(codec, reg);
4319
4320 return 0;
4321}
4322
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004323static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4324 enum snd_soc_bias_level level)
4325{
4326 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4327
4328 return codec->driver->set_bias_level(codec, level);
4329}
4330
Mark Brown0d0cf002008-12-10 14:32:45 +00004331/**
4332 * snd_soc_register_codec - Register a codec with the ASoC core
4333 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004334 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004335 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004336int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004337 const struct snd_soc_codec_driver *codec_drv,
4338 struct snd_soc_dai_driver *dai_drv,
4339 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004340{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004341 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004342 struct snd_soc_dai *dai;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004343 struct regmap *regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004344 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004345
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004346 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004347
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004348 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4349 if (codec == NULL)
4350 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004351
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004352 codec->component.dapm_ptr = &codec->dapm;
4353
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004354 ret = snd_soc_component_initialize(&codec->component,
4355 &codec_drv->component_driver, dev);
4356 if (ret)
4357 goto err_free;
4358
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004359 if (codec_drv->write)
4360 codec->component.write = snd_soc_codec_drv_write;
4361 if (codec_drv->read)
4362 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004363 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004364 codec->dapm.codec = codec;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004365 if (codec_drv->seq_notifier)
4366 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004367 if (codec_drv->set_bias_level)
4368 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004369 codec->dev = dev;
4370 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004371 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004372 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004373
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004374 if (!codec->component.write) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004375 if (codec_drv->get_regmap)
4376 regmap = codec_drv->get_regmap(dev);
4377 else
4378 regmap = dev_get_regmap(dev, NULL);
4379
4380 if (regmap) {
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004381 ret = snd_soc_component_init_io(&codec->component,
4382 regmap);
4383 if (ret) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004384 dev_err(codec->dev,
4385 "Failed to set cache I/O:%d\n",
4386 ret);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004387 goto err_cleanup;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004388 }
4389 }
4390 }
4391
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004392 for (i = 0; i < num_dai; i++) {
4393 fixup_codec_formats(&dai_drv[i].playback);
4394 fixup_codec_formats(&dai_drv[i].capture);
4395 }
4396
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004397 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4398 if (ret < 0) {
4399 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4400 goto err_cleanup;
4401 }
4402
4403 list_for_each_entry(dai, &codec->component.dai_list, list)
4404 dai->codec = codec;
4405
Mark Brown054880f2012-04-09 17:29:19 +01004406 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004407 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004408 list_add(&codec->list, &codec_list);
4409 mutex_unlock(&client_mutex);
4410
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004411 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4412 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004413 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004414
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004415err_cleanup:
4416 snd_soc_component_cleanup(&codec->component);
4417err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004418 kfree(codec);
4419 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004420}
4421EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4422
4423/**
4424 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4425 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004426 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004427 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004428void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004429{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004430 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004431
4432 list_for_each_entry(codec, &codec_list, list) {
4433 if (dev == codec->dev)
4434 goto found;
4435 }
4436 return;
4437
4438found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004439
Mark Brown0d0cf002008-12-10 14:32:45 +00004440 mutex_lock(&client_mutex);
4441 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004442 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004443 mutex_unlock(&client_mutex);
4444
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004445 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4446 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004447
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004448 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004449 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004450 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004451}
4452EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4453
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004454/* Retrieve a card's name from device tree */
4455int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4456 const char *propname)
4457{
4458 struct device_node *np = card->dev->of_node;
4459 int ret;
4460
4461 ret = of_property_read_string_index(np, propname, 0, &card->name);
4462 /*
4463 * EINVAL means the property does not exist. This is fine providing
4464 * card->name was previously set, which is checked later in
4465 * snd_soc_register_card.
4466 */
4467 if (ret < 0 && ret != -EINVAL) {
4468 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004469 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004470 propname, ret);
4471 return ret;
4472 }
4473
4474 return 0;
4475}
4476EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4477
Xiubo Li9a6d4862014-02-08 15:59:52 +08004478static const struct snd_soc_dapm_widget simple_widgets[] = {
4479 SND_SOC_DAPM_MIC("Microphone", NULL),
4480 SND_SOC_DAPM_LINE("Line", NULL),
4481 SND_SOC_DAPM_HP("Headphone", NULL),
4482 SND_SOC_DAPM_SPK("Speaker", NULL),
4483};
4484
4485int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4486 const char *propname)
4487{
4488 struct device_node *np = card->dev->of_node;
4489 struct snd_soc_dapm_widget *widgets;
4490 const char *template, *wname;
4491 int i, j, num_widgets, ret;
4492
4493 num_widgets = of_property_count_strings(np, propname);
4494 if (num_widgets < 0) {
4495 dev_err(card->dev,
4496 "ASoC: Property '%s' does not exist\n", propname);
4497 return -EINVAL;
4498 }
4499 if (num_widgets & 1) {
4500 dev_err(card->dev,
4501 "ASoC: Property '%s' length is not even\n", propname);
4502 return -EINVAL;
4503 }
4504
4505 num_widgets /= 2;
4506 if (!num_widgets) {
4507 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4508 propname);
4509 return -EINVAL;
4510 }
4511
4512 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4513 GFP_KERNEL);
4514 if (!widgets) {
4515 dev_err(card->dev,
4516 "ASoC: Could not allocate memory for widgets\n");
4517 return -ENOMEM;
4518 }
4519
4520 for (i = 0; i < num_widgets; i++) {
4521 ret = of_property_read_string_index(np, propname,
4522 2 * i, &template);
4523 if (ret) {
4524 dev_err(card->dev,
4525 "ASoC: Property '%s' index %d read error:%d\n",
4526 propname, 2 * i, ret);
4527 return -EINVAL;
4528 }
4529
4530 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4531 if (!strncmp(template, simple_widgets[j].name,
4532 strlen(simple_widgets[j].name))) {
4533 widgets[i] = simple_widgets[j];
4534 break;
4535 }
4536 }
4537
4538 if (j >= ARRAY_SIZE(simple_widgets)) {
4539 dev_err(card->dev,
4540 "ASoC: DAPM widget '%s' is not supported\n",
4541 template);
4542 return -EINVAL;
4543 }
4544
4545 ret = of_property_read_string_index(np, propname,
4546 (2 * i) + 1,
4547 &wname);
4548 if (ret) {
4549 dev_err(card->dev,
4550 "ASoC: Property '%s' index %d read error:%d\n",
4551 propname, (2 * i) + 1, ret);
4552 return -EINVAL;
4553 }
4554
4555 widgets[i].name = wname;
4556 }
4557
4558 card->dapm_widgets = widgets;
4559 card->num_dapm_widgets = num_widgets;
4560
4561 return 0;
4562}
4563EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4564
Xiubo Li89c67852014-02-14 09:34:35 +08004565int snd_soc_of_parse_tdm_slot(struct device_node *np,
4566 unsigned int *slots,
4567 unsigned int *slot_width)
4568{
4569 u32 val;
4570 int ret;
4571
4572 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4573 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4574 if (ret)
4575 return ret;
4576
4577 if (slots)
4578 *slots = val;
4579 }
4580
4581 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4582 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4583 if (ret)
4584 return ret;
4585
4586 if (slot_width)
4587 *slot_width = val;
4588 }
4589
4590 return 0;
4591}
4592EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4593
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004594int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4595 const char *propname)
4596{
4597 struct device_node *np = card->dev->of_node;
4598 int num_routes;
4599 struct snd_soc_dapm_route *routes;
4600 int i, ret;
4601
4602 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004603 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004604 dev_err(card->dev,
4605 "ASoC: Property '%s' does not exist or its length is not even\n",
4606 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004607 return -EINVAL;
4608 }
4609 num_routes /= 2;
4610 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004611 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004612 propname);
4613 return -EINVAL;
4614 }
4615
4616 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4617 GFP_KERNEL);
4618 if (!routes) {
4619 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004620 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004621 return -EINVAL;
4622 }
4623
4624 for (i = 0; i < num_routes; i++) {
4625 ret = of_property_read_string_index(np, propname,
4626 2 * i, &routes[i].sink);
4627 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004628 dev_err(card->dev,
4629 "ASoC: Property '%s' index %d could not be read: %d\n",
4630 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004631 return -EINVAL;
4632 }
4633 ret = of_property_read_string_index(np, propname,
4634 (2 * i) + 1, &routes[i].source);
4635 if (ret) {
4636 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004637 "ASoC: Property '%s' index %d could not be read: %d\n",
4638 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004639 return -EINVAL;
4640 }
4641 }
4642
4643 card->num_dapm_routes = num_routes;
4644 card->dapm_routes = routes;
4645
4646 return 0;
4647}
4648EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4649
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004650unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004651 const char *prefix,
4652 struct device_node **bitclkmaster,
4653 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004654{
4655 int ret, i;
4656 char prop[128];
4657 unsigned int format = 0;
4658 int bit, frame;
4659 const char *str;
4660 struct {
4661 char *name;
4662 unsigned int val;
4663 } of_fmt_table[] = {
4664 { "i2s", SND_SOC_DAIFMT_I2S },
4665 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4666 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4667 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4668 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4669 { "ac97", SND_SOC_DAIFMT_AC97 },
4670 { "pdm", SND_SOC_DAIFMT_PDM},
4671 { "msb", SND_SOC_DAIFMT_MSB },
4672 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004673 };
4674
4675 if (!prefix)
4676 prefix = "";
4677
4678 /*
4679 * check "[prefix]format = xxx"
4680 * SND_SOC_DAIFMT_FORMAT_MASK area
4681 */
4682 snprintf(prop, sizeof(prop), "%sformat", prefix);
4683 ret = of_property_read_string(np, prop, &str);
4684 if (ret == 0) {
4685 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4686 if (strcmp(str, of_fmt_table[i].name) == 0) {
4687 format |= of_fmt_table[i].val;
4688 break;
4689 }
4690 }
4691 }
4692
4693 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004694 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004695 * SND_SOC_DAIFMT_CLOCK_MASK area
4696 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004697 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4698 if (of_get_property(np, prop, NULL))
4699 format |= SND_SOC_DAIFMT_CONT;
4700 else
4701 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004702
4703 /*
4704 * check "[prefix]bitclock-inversion"
4705 * check "[prefix]frame-inversion"
4706 * SND_SOC_DAIFMT_INV_MASK area
4707 */
4708 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4709 bit = !!of_get_property(np, prop, NULL);
4710
4711 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4712 frame = !!of_get_property(np, prop, NULL);
4713
4714 switch ((bit << 4) + frame) {
4715 case 0x11:
4716 format |= SND_SOC_DAIFMT_IB_IF;
4717 break;
4718 case 0x10:
4719 format |= SND_SOC_DAIFMT_IB_NF;
4720 break;
4721 case 0x01:
4722 format |= SND_SOC_DAIFMT_NB_IF;
4723 break;
4724 default:
4725 /* SND_SOC_DAIFMT_NB_NF is default */
4726 break;
4727 }
4728
4729 /*
4730 * check "[prefix]bitclock-master"
4731 * check "[prefix]frame-master"
4732 * SND_SOC_DAIFMT_MASTER_MASK area
4733 */
4734 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4735 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004736 if (bit && bitclkmaster)
4737 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004738
4739 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4740 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004741 if (frame && framemaster)
4742 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004743
4744 switch ((bit << 4) + frame) {
4745 case 0x11:
4746 format |= SND_SOC_DAIFMT_CBM_CFM;
4747 break;
4748 case 0x10:
4749 format |= SND_SOC_DAIFMT_CBM_CFS;
4750 break;
4751 case 0x01:
4752 format |= SND_SOC_DAIFMT_CBS_CFM;
4753 break;
4754 default:
4755 format |= SND_SOC_DAIFMT_CBS_CFS;
4756 break;
4757 }
4758
4759 return format;
4760}
4761EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4762
Kuninori Morimotocb470082013-09-10 17:39:56 -07004763int snd_soc_of_get_dai_name(struct device_node *of_node,
4764 const char **dai_name)
4765{
4766 struct snd_soc_component *pos;
4767 struct of_phandle_args args;
4768 int ret;
4769
4770 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4771 "#sound-dai-cells", 0, &args);
4772 if (ret)
4773 return ret;
4774
4775 ret = -EPROBE_DEFER;
4776
4777 mutex_lock(&client_mutex);
4778 list_for_each_entry(pos, &component_list, list) {
4779 if (pos->dev->of_node != args.np)
4780 continue;
4781
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004782 if (pos->driver->of_xlate_dai_name) {
4783 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4784 } else {
4785 int id = -1;
4786
4787 switch (args.args_count) {
4788 case 0:
4789 id = 0; /* same as dai_drv[0] */
4790 break;
4791 case 1:
4792 id = args.args[0];
4793 break;
4794 default:
4795 /* not supported */
4796 break;
4797 }
4798
4799 if (id < 0 || id >= pos->num_dai) {
4800 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004801 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004802 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004803
4804 ret = 0;
4805
4806 *dai_name = pos->dai_drv[id].name;
4807 if (!*dai_name)
4808 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004809 }
4810
Kuninori Morimotocb470082013-09-10 17:39:56 -07004811 break;
4812 }
4813 mutex_unlock(&client_mutex);
4814
4815 of_node_put(args.np);
4816
4817 return ret;
4818}
4819EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4820
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004821static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004822{
Mark Brown384c89e2008-12-03 17:34:03 +00004823#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004824 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4825 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004826 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004827 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004828 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004829
Mark Brown8a9dab12011-01-10 22:25:21 +00004830 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004831 &codec_list_fops))
4832 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4833
Mark Brown8a9dab12011-01-10 22:25:21 +00004834 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004835 &dai_list_fops))
4836 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004837
Mark Brown8a9dab12011-01-10 22:25:21 +00004838 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004839 &platform_list_fops))
4840 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004841#endif
4842
Mark Brownfb257892011-04-28 10:57:54 +01004843 snd_soc_util_init();
4844
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004845 return platform_driver_register(&soc_driver);
4846}
Mark Brown4abe8e12010-10-12 17:41:03 +01004847module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004848
Mark Brown7d8c16a2008-11-30 22:11:24 +00004849static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004850{
Mark Brownfb257892011-04-28 10:57:54 +01004851 snd_soc_util_exit();
4852
Mark Brown384c89e2008-12-03 17:34:03 +00004853#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004854 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004855#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004856 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004857}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004858module_exit(snd_soc_exit);
4859
4860/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004861MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004862MODULE_DESCRIPTION("ALSA SoC Core");
4863MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004864MODULE_ALIAS("platform:soc-audio");