blob: 7fa3b4a04bd3678a63deb7442c7aac788229b020 [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
273static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
274{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200275 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
276
277 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
278 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000279 if (!codec->debugfs_codec_root) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200280 dev_warn(codec->dev,
281 "ASoC: Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000282 return;
283 }
284
Mark Brownaaee8ef2011-01-26 20:53:50 +0000285 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
286 &codec->cache_sync);
287 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
288 &codec->cache_only);
289
Mark Brown2624d5f2009-11-03 21:56:13 +0000290 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
291 codec->debugfs_codec_root,
292 codec, &codec_reg_fops);
293 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200294 dev_warn(codec->dev,
295 "ASoC: Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000296
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200297 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000298}
299
300static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
301{
302 debugfs_remove_recursive(codec->debugfs_codec_root);
303}
304
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000305static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
306{
307 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
308
309 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
310 debugfs_card_root);
311 if (!platform->debugfs_platform_root) {
312 dev_warn(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000313 "ASoC: Failed to create platform debugfs directory\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000314 return;
315 }
316
317 snd_soc_dapm_debugfs_init(&platform->dapm,
318 platform->debugfs_platform_root);
319}
320
321static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
322{
323 debugfs_remove_recursive(platform->debugfs_platform_root);
324}
325
Mark Brownc3c5a192010-09-15 18:15:14 +0100326static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
327 size_t count, loff_t *ppos)
328{
329 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100330 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100331 struct snd_soc_codec *codec;
332
333 if (!buf)
334 return -ENOMEM;
335
Mark Brown2b194f9d2010-10-13 10:52:16 +0100336 list_for_each_entry(codec, &codec_list, list) {
337 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
338 codec->name);
339 if (len >= 0)
340 ret += len;
341 if (ret > PAGE_SIZE) {
342 ret = PAGE_SIZE;
343 break;
344 }
345 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100346
347 if (ret >= 0)
348 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
349
350 kfree(buf);
351
352 return ret;
353}
354
355static const struct file_operations codec_list_fops = {
356 .read = codec_list_read_file,
357 .llseek = default_llseek,/* read accesses f_pos */
358};
359
Mark Brownf3208782010-09-15 18:19:07 +0100360static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
361 size_t count, loff_t *ppos)
362{
363 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100364 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100365 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100366 struct snd_soc_dai *dai;
367
368 if (!buf)
369 return -ENOMEM;
370
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100371 list_for_each_entry(component, &component_list, list) {
372 list_for_each_entry(dai, &component->dai_list, list) {
373 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
374 dai->name);
375 if (len >= 0)
376 ret += len;
377 if (ret > PAGE_SIZE) {
378 ret = PAGE_SIZE;
379 break;
380 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100381 }
382 }
Mark Brownf3208782010-09-15 18:19:07 +0100383
Mark Brown2b194f9d2010-10-13 10:52:16 +0100384 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100385
386 kfree(buf);
387
388 return ret;
389}
390
391static const struct file_operations dai_list_fops = {
392 .read = dai_list_read_file,
393 .llseek = default_llseek,/* read accesses f_pos */
394};
395
Mark Brown19c7ac22010-09-15 18:22:40 +0100396static ssize_t platform_list_read_file(struct file *file,
397 char __user *user_buf,
398 size_t count, loff_t *ppos)
399{
400 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100401 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100402 struct snd_soc_platform *platform;
403
404 if (!buf)
405 return -ENOMEM;
406
Mark Brown2b194f9d2010-10-13 10:52:16 +0100407 list_for_each_entry(platform, &platform_list, list) {
408 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
409 platform->name);
410 if (len >= 0)
411 ret += len;
412 if (ret > PAGE_SIZE) {
413 ret = PAGE_SIZE;
414 break;
415 }
416 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100417
Mark Brown2b194f9d2010-10-13 10:52:16 +0100418 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100419
420 kfree(buf);
421
422 return ret;
423}
424
425static const struct file_operations platform_list_fops = {
426 .read = platform_list_read_file,
427 .llseek = default_llseek,/* read accesses f_pos */
428};
429
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200430static void soc_init_card_debugfs(struct snd_soc_card *card)
431{
432 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000433 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200434 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200435 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100436 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200437 return;
438 }
439
440 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
441 card->debugfs_card_root,
442 &card->pop_time);
443 if (!card->debugfs_pop_time)
444 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000445 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200446}
447
448static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
449{
450 debugfs_remove_recursive(card->debugfs_card_root);
451}
452
Mark Brown2624d5f2009-11-03 21:56:13 +0000453#else
454
455static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
456{
457}
458
459static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
460{
461}
Axel Linb95fccb2010-11-09 17:06:44 +0800462
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000463static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
464{
465}
466
467static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
468{
469}
470
Axel Linb95fccb2010-11-09 17:06:44 +0800471static inline void soc_init_card_debugfs(struct snd_soc_card *card)
472{
473}
474
475static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
476{
477}
Mark Brown2624d5f2009-11-03 21:56:13 +0000478#endif
479
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100480struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
481 const char *dai_link, int stream)
482{
483 int i;
484
485 for (i = 0; i < card->num_links; i++) {
486 if (card->rtd[i].dai_link->no_pcm &&
487 !strcmp(card->rtd[i].dai_link->name, dai_link))
488 return card->rtd[i].pcm->streams[stream].substream;
489 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000490 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100491 return NULL;
492}
493EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
494
495struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
496 const char *dai_link)
497{
498 int i;
499
500 for (i = 0; i < card->num_links; i++) {
501 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
502 return &card->rtd[i];
503 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000504 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100505 return NULL;
506}
507EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
508
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200509#ifdef CONFIG_SND_SOC_AC97_BUS
510/* unregister ac97 codec */
511static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
512{
513 if (codec->ac97->dev.bus)
514 device_unregister(&codec->ac97->dev);
515 return 0;
516}
517
518/* stop no dev release warning */
519static void soc_ac97_device_release(struct device *dev){}
520
521/* register ac97 codec to bus */
522static int soc_ac97_dev_register(struct snd_soc_codec *codec)
523{
524 int err;
525
526 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100527 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200528 codec->ac97->dev.release = soc_ac97_device_release;
529
Kay Sieversbb072bf2008-11-02 03:50:35 +0100530 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200532 err = device_register(&codec->ac97->dev);
533 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000534 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200535 codec->ac97->dev.bus = NULL;
536 return err;
537 }
538 return 0;
539}
540#endif
541
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100542static void codec2codec_close_delayed_work(struct work_struct *work)
543{
544 /* Currently nothing to do for c2c links
545 * Since c2c links are internal nodes in the DAPM graph and
546 * don't interface with the outside world or application layer
547 * we don't have to do any special handling on close.
548 */
549}
550
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000551#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200552/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000553int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000555 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200556 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200557 int i;
558
Daniel Macke3509ff2009-06-03 17:44:49 +0200559 /* If the initialization of this soc device failed, there is no codec
560 * associated with it. Just bail out in this case.
561 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200563 return 0;
564
Andy Green6ed25972008-06-13 16:24:05 +0100565 /* Due to the resume being scheduled into a workqueue we could
566 * suspend before that's finished - wait for it to complete.
567 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000568 snd_power_lock(card->snd_card);
569 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
570 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100571
572 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000573 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100574
Mark Browna00f90f2010-12-02 16:24:24 +0000575 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000576 for (i = 0; i < card->num_rtd; i++) {
577 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
578 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100579
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100581 continue;
582
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000583 if (drv->ops->digital_mute && dai->playback_active)
584 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200585 }
586
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100587 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588 for (i = 0; i < card->num_rtd; i++) {
589 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100590 continue;
591
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000592 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100593 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100594
Mark Brown87506542008-11-18 20:50:34 +0000595 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000596 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200597
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 for (i = 0; i < card->num_rtd; i++) {
599 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
600 struct snd_soc_platform *platform = card->rtd[i].platform;
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 (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
606 cpu_dai->driver->suspend(cpu_dai);
607 if (platform->driver->suspend && !platform->suspended) {
608 platform->driver->suspend(cpu_dai);
609 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100610 }
611 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200612
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 /* close any waiting streams and save state */
614 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo43829732012-08-20 14:51:24 -0700615 flush_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200616 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100618
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000619 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620
621 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100622 continue;
623
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800624 snd_soc_dapm_stream_event(&card->rtd[i],
625 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800626 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800628 snd_soc_dapm_stream_event(&card->rtd[i],
629 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800630 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631 }
632
Mark Browne2d32ff2012-08-31 17:38:32 -0700633 /* Recheck all analogue paths too */
634 dapm_mark_io_dirty(&card->dapm);
635 snd_soc_dapm_sync(&card->dapm);
636
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000637 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200638 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 /* If there are paths active then the CODEC will be held with
640 * bias _ON and should not be suspended. */
641 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200642 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000643 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000644 /*
645 * If the CODEC is capable of idle
646 * bias off then being in STANDBY
647 * means it's doing something,
648 * otherwise fall through.
649 */
650 if (codec->dapm.idle_bias_off) {
651 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200652 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000653 break;
654 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000655 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100656 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000657 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900658 codec->cache_sync = 1;
Mark Brownda8b8e02012-09-12 12:21:52 +0800659 if (codec->using_regmap)
660 regcache_mark_dirty(codec->control_data);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800661 /* deactivate pins to sleep state */
662 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000663 break;
664 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200665 dev_dbg(codec->dev,
666 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000667 break;
668 }
669 }
670 }
671
672 for (i = 0; i < card->num_rtd; i++) {
673 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
674
675 if (card->rtd[i].dai_link->ignore_suspend)
676 continue;
677
678 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
679 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800680
681 /* deactivate pins to sleep state */
682 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200683 }
684
Mark Brown87506542008-11-18 20:50:34 +0000685 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000686 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200687
688 return 0;
689}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000690EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200691
Andy Green6ed25972008-06-13 16:24:05 +0100692/* deferred resume work, so resume can complete before we finished
693 * setting our codec back up, which can be very slow on I2C
694 */
695static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200696{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000697 struct snd_soc_card *card =
698 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200699 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700 int i;
701
Andy Green6ed25972008-06-13 16:24:05 +0100702 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
703 * so userspace apps are blocked from touching us
704 */
705
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000706 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100707
Mark Brown99497882010-05-07 20:24:05 +0100708 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000709 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100710
Mark Brown87506542008-11-18 20:50:34 +0000711 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000712 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714 /* resume AC97 DAIs */
715 for (i = 0; i < card->num_rtd; i++) {
716 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100717
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000718 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100719 continue;
720
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000721 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
722 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723 }
724
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200725 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000726 /* If the CODEC was idle over suspend then it will have been
727 * left with bias OFF or STANDBY and suspended so we must now
728 * resume. Otherwise the suspend was suppressed.
729 */
730 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200731 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000732 case SND_SOC_BIAS_STANDBY:
733 case SND_SOC_BIAS_OFF:
734 codec->driver->resume(codec);
735 codec->suspended = 0;
736 break;
737 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200738 dev_dbg(codec->dev,
739 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000740 break;
741 }
Mark Brown1547aba2010-05-07 21:11:40 +0100742 }
743 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200744
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000745 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100746
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000747 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100748 continue;
749
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800750 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000751 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800752 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000753
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800754 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000755 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800756 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200757 }
758
Mark Brown3ff3f642008-05-19 12:32:25 +0200759 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000760 for (i = 0; i < card->num_rtd; i++) {
761 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
762 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100763
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000764 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100765 continue;
766
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000767 if (drv->ops->digital_mute && dai->playback_active)
768 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200769 }
770
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000771 for (i = 0; i < card->num_rtd; i++) {
772 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
773 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100774
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000775 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100776 continue;
777
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
779 cpu_dai->driver->resume(cpu_dai);
780 if (platform->driver->resume && platform->suspended) {
781 platform->driver->resume(cpu_dai);
782 platform->suspended = 0;
783 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200784 }
785
Mark Brown87506542008-11-18 20:50:34 +0000786 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000787 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200788
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000789 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100790
791 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000792 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700793
794 /* Recheck all analogue paths too */
795 dapm_mark_io_dirty(&card->dapm);
796 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100797}
798
799/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000800int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100801{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000802 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600803 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200804
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800805 /* If the initialization of this soc device failed, there is no codec
806 * associated with it. Just bail out in this case.
807 */
808 if (list_empty(&card->codec_dev_list))
809 return 0;
810
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800811 /* activate pins from sleep state */
812 for (i = 0; i < card->num_rtd; i++) {
813 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
814 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
815 if (cpu_dai->active)
816 pinctrl_pm_select_default_state(cpu_dai->dev);
817 if (codec_dai->active)
818 pinctrl_pm_select_default_state(codec_dai->dev);
819 }
820
Mark Brown64ab9ba2009-03-31 11:27:03 +0100821 /* AC97 devices might have other drivers hanging off them so
822 * need to resume immediately. Other drivers don't have that
823 * problem and may take a substantial amount of time to resume
824 * due to I/O costs and anti-pop so handle them out of line.
825 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 for (i = 0; i < card->num_rtd; i++) {
827 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600828 ac97_control |= cpu_dai->driver->ac97_control;
829 }
830 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000831 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600832 soc_resume_deferred(&card->deferred_resume_work);
833 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000834 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600835 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000836 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100837 }
Andy Green6ed25972008-06-13 16:24:05 +0100838
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200839 return 0;
840}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000841EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200842#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000843#define snd_soc_suspend NULL
844#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200845#endif
846
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100847static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800848};
849
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000850static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200851{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000852 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
853 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100854 struct snd_soc_component *component;
Mark Brownfe3e78e2009-11-03 22:13:13 +0000855 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000856 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100858 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200859
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000860 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000861
Mark Brownb19e6e72012-03-14 21:18:39 +0000862 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100863 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600864 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100865 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600866 continue;
867 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100868 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600869 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100870 list_for_each_entry(cpu_dai, &component->dai_list, list) {
871 if (dai_link->cpu_dai_name &&
872 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
873 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700874
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100875 rtd->cpu_dai = cpu_dai;
876 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000877 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000878
879 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000880 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000881 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000882 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000883 }
884
Mark Brownb19e6e72012-03-14 21:18:39 +0000885 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000886 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700887 if (dai_link->codec_of_node) {
888 if (codec->dev->of_node != dai_link->codec_of_node)
889 continue;
890 } else {
891 if (strcmp(codec->name, dai_link->codec_name))
892 continue;
893 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000894
Stephen Warren2610ab72011-12-07 13:58:27 -0700895 rtd->codec = codec;
896
897 /*
898 * CODEC found, so find CODEC DAI from registered DAIs from
899 * this CODEC
900 */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100901 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
902 if (!strcmp(codec_dai->name, dai_link->codec_dai_name)) {
Stephen Warren2610ab72011-12-07 13:58:27 -0700903 rtd->codec_dai = codec_dai;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100904 break;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000905 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000906 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000907
908 if (!rtd->codec_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000909 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700910 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000911 return -EPROBE_DEFER;
912 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000913 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000914
Mark Brownb19e6e72012-03-14 21:18:39 +0000915 if (!rtd->codec) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000916 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
Mark Brownb19e6e72012-03-14 21:18:39 +0000917 dai_link->codec_name);
918 return -EPROBE_DEFER;
919 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100920
921 /* if there's no platform we match on the empty platform */
922 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700923 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100924 platform_name = "snd-soc-dummy";
925
Mark Brownb19e6e72012-03-14 21:18:39 +0000926 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000927 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700928 if (dai_link->platform_of_node) {
929 if (platform->dev->of_node !=
930 dai_link->platform_of_node)
931 continue;
932 } else {
933 if (strcmp(platform->name, platform_name))
934 continue;
935 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700936
937 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000938 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000939 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000940 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000941 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000942 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000943 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000944
945 card->num_rtd++;
946
947 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000948}
949
Stephen Warrend12cd192012-06-08 12:34:22 -0600950static int soc_remove_platform(struct snd_soc_platform *platform)
951{
952 int ret;
953
954 if (platform->driver->remove) {
955 ret = platform->driver->remove(platform);
956 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000957 dev_err(platform->dev, "ASoC: failed to remove %d\n",
958 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -0600959 }
960
961 /* Make sure all DAPM widgets are freed */
962 snd_soc_dapm_free(&platform->dapm);
963
964 soc_cleanup_platform_debugfs(platform);
965 platform->probed = 0;
966 list_del(&platform->card_list);
967 module_put(platform->dev->driver->owner);
968
969 return 0;
970}
971
Jarkko Nikula589c3562010-12-06 16:27:07 +0200972static void soc_remove_codec(struct snd_soc_codec *codec)
973{
974 int err;
975
976 if (codec->driver->remove) {
977 err = codec->driver->remove(codec);
978 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000979 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200980 }
981
982 /* Make sure all DAPM widgets are freed */
983 snd_soc_dapm_free(&codec->dapm);
984
985 soc_cleanup_codec_debugfs(codec);
986 codec->probed = 0;
987 list_del(&codec->card_list);
988 module_put(codec->dev->driver->owner);
989}
990
Stephen Warren62ae68f2012-06-08 12:34:23 -0600991static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000992{
993 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000994 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
995 int err;
996
997 /* unregister the rtd device */
998 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800999 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1000 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1001 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002 rtd->dev_registered = 0;
1003 }
1004
1005 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001006 if (codec_dai && codec_dai->probed &&
1007 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001008 if (codec_dai->driver->remove) {
1009 err = codec_dai->driver->remove(codec_dai);
1010 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001011 dev_err(codec_dai->dev,
1012 "ASoC: failed to remove %s: %d\n",
1013 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001014 }
1015 codec_dai->probed = 0;
1016 list_del(&codec_dai->card_list);
1017 }
1018
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001019 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001020 if (cpu_dai && cpu_dai->probed &&
1021 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001022 if (cpu_dai->driver->remove) {
1023 err = cpu_dai->driver->remove(cpu_dai);
1024 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001025 dev_err(cpu_dai->dev,
1026 "ASoC: failed to remove %s: %d\n",
1027 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001028 }
1029 cpu_dai->probed = 0;
1030 list_del(&cpu_dai->card_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001031
Stephen Warren18d75642012-06-08 12:34:21 -06001032 if (!cpu_dai->codec) {
1033 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001034 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -06001035 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001036 }
1037}
1038
Stephen Warren62ae68f2012-06-08 12:34:23 -06001039static void soc_remove_link_components(struct snd_soc_card *card, int num,
1040 int order)
1041{
1042 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1043 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1044 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1045 struct snd_soc_platform *platform = rtd->platform;
1046 struct snd_soc_codec *codec;
1047
1048 /* remove the platform */
1049 if (platform && platform->probed &&
1050 platform->driver->remove_order == order) {
1051 soc_remove_platform(platform);
1052 }
1053
1054 /* remove the CODEC-side CODEC */
1055 if (codec_dai) {
1056 codec = codec_dai->codec;
1057 if (codec && codec->probed &&
1058 codec->driver->remove_order == order)
1059 soc_remove_codec(codec);
1060 }
1061
1062 /* remove any CPU-side CODEC */
1063 if (cpu_dai) {
1064 codec = cpu_dai->codec;
1065 if (codec && codec->probed &&
1066 codec->driver->remove_order == order)
1067 soc_remove_codec(codec);
1068 }
1069}
1070
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001071static void soc_remove_dai_links(struct snd_soc_card *card)
1072{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001073 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001074
Liam Girdwood0168bf02011-06-07 16:08:05 +01001075 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1076 order++) {
1077 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001078 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001079 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001080
1081 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1082 order++) {
1083 for (dai = 0; dai < card->num_rtd; dai++)
1084 soc_remove_link_components(card, dai, order);
1085 }
1086
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001087 card->num_rtd = 0;
1088}
1089
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001090static void soc_set_name_prefix(struct snd_soc_card *card,
1091 struct snd_soc_codec *codec)
1092{
1093 int i;
1094
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001095 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001096 return;
1097
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001098 for (i = 0; i < card->num_configs; i++) {
1099 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001100 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1101 codec->name_prefix = map->name_prefix;
1102 break;
1103 }
1104 }
1105}
1106
Jarkko Nikula589c3562010-12-06 16:27:07 +02001107static int soc_probe_codec(struct snd_soc_card *card,
1108 struct snd_soc_codec *codec)
1109{
1110 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001111 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001112 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001113
1114 codec->card = card;
1115 codec->dapm.card = card;
1116 soc_set_name_prefix(card, codec);
1117
Jarkko Nikula70d29332011-01-27 16:24:22 +02001118 if (!try_module_get(codec->dev->driver->owner))
1119 return -ENODEV;
1120
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001121 soc_init_codec_debugfs(codec);
1122
Nariman Poushinb318ad52014-04-01 13:59:33 +01001123 if (driver->dapm_widgets) {
1124 ret = snd_soc_dapm_new_controls(&codec->dapm,
1125 driver->dapm_widgets,
1126 driver->num_dapm_widgets);
1127
1128 if (ret != 0) {
1129 dev_err(codec->dev,
1130 "Failed to create new controls %d\n", ret);
1131 goto err_probe;
1132 }
1133 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001134
Mark Brown888df392012-02-16 19:37:51 -08001135 /* Create DAPM widgets for each DAI stream */
Nariman Poushin261edc72014-03-31 15:47:12 +01001136 list_for_each_entry(dai, &codec->component.dai_list, list) {
1137 ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1138
1139 if (ret != 0) {
1140 dev_err(codec->dev,
1141 "Failed to create DAI widgets %d\n", ret);
1142 goto err_probe;
1143 }
1144 }
Mark Brown888df392012-02-16 19:37:51 -08001145
Mark Brown33c5f962011-08-22 18:40:30 +01001146 codec->dapm.idle_bias_off = driver->idle_bias_off;
1147
Xiubo Lia32c17b2014-03-11 12:43:22 +08001148 if (!codec->write && dev_get_regmap(codec->dev, NULL)) {
1149 /* Set the default I/O up try regmap */
1150 ret = snd_soc_codec_set_cache_io(codec, NULL);
1151 if (ret < 0) {
1152 dev_err(codec->dev,
1153 "Failed to set cache I/O: %d\n", ret);
1154 goto err_probe;
1155 }
1156 }
Xiubo Life2265e2014-02-27 17:49:52 +08001157
Mark Brown89b95ac02011-03-07 16:38:44 +00001158 if (driver->probe) {
1159 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001160 if (ret < 0) {
1161 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001162 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001163 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001164 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001165 WARN(codec->dapm.idle_bias_off &&
1166 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001167 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1168 codec->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001169 }
1170
Mark Brownb7af1da2011-04-07 19:18:44 +09001171 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001172 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001173 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001174 if (driver->dapm_routes)
1175 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1176 driver->num_dapm_routes);
1177
Jarkko Nikula589c3562010-12-06 16:27:07 +02001178 /* mark codec as probed and add to card codec list */
1179 codec->probed = 1;
1180 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001181 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001182
Jarkko Nikula70d29332011-01-27 16:24:22 +02001183 return 0;
1184
1185err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001186 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001187 module_put(codec->dev->driver->owner);
1188
Jarkko Nikula589c3562010-12-06 16:27:07 +02001189 return ret;
1190}
1191
Liam Girdwood956245e2011-07-01 16:54:08 +01001192static int soc_probe_platform(struct snd_soc_card *card,
1193 struct snd_soc_platform *platform)
1194{
1195 int ret = 0;
1196 const struct snd_soc_platform_driver *driver = platform->driver;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001197 struct snd_soc_component *component;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001198 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001199
1200 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001201 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001202
1203 if (!try_module_get(platform->dev->driver->owner))
1204 return -ENODEV;
1205
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001206 soc_init_platform_debugfs(platform);
1207
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001208 if (driver->dapm_widgets)
1209 snd_soc_dapm_new_controls(&platform->dapm,
1210 driver->dapm_widgets, driver->num_dapm_widgets);
1211
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001212 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001213 list_for_each_entry(component, &component_list, list) {
1214 if (component->dev != platform->dev)
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001215 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001216 list_for_each_entry(dai, &component->dai_list, list)
1217 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001218 }
1219
Stephen Warren3fec6b62012-04-05 12:28:01 -06001220 platform->dapm.idle_bias_off = 1;
1221
Liam Girdwood956245e2011-07-01 16:54:08 +01001222 if (driver->probe) {
1223 ret = driver->probe(platform);
1224 if (ret < 0) {
1225 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001226 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001227 goto err_probe;
1228 }
1229 }
1230
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001231 if (driver->controls)
1232 snd_soc_add_platform_controls(platform, driver->controls,
1233 driver->num_controls);
1234 if (driver->dapm_routes)
1235 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1236 driver->num_dapm_routes);
1237
Liam Girdwood956245e2011-07-01 16:54:08 +01001238 /* mark platform as probed and add to card platform list */
1239 platform->probed = 1;
1240 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001241 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001242
1243 return 0;
1244
1245err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001246 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001247 module_put(platform->dev->driver->owner);
1248
1249 return ret;
1250}
1251
Mark Brown36ae1a92012-01-06 17:12:45 -08001252static void rtd_release(struct device *dev)
1253{
1254 kfree(dev);
1255}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001256
Jarkko Nikula589c3562010-12-06 16:27:07 +02001257static int soc_post_component_init(struct snd_soc_card *card,
1258 struct snd_soc_codec *codec,
1259 int num, int dailess)
1260{
1261 struct snd_soc_dai_link *dai_link = NULL;
1262 struct snd_soc_aux_dev *aux_dev = NULL;
1263 struct snd_soc_pcm_runtime *rtd;
Lars-Peter Clausen6479f15ad2014-03-12 15:27:40 +01001264 const char *name;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001265 int ret = 0;
1266
1267 if (!dailess) {
1268 dai_link = &card->dai_link[num];
1269 rtd = &card->rtd[num];
1270 name = dai_link->name;
1271 } else {
1272 aux_dev = &card->aux_dev[num];
1273 rtd = &card->rtd_aux[num];
1274 name = aux_dev->name;
1275 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001276 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001277
Jarkko Nikula589c3562010-12-06 16:27:07 +02001278 /* do machine specific initialization */
1279 if (!dailess && dai_link->init)
1280 ret = dai_link->init(rtd);
1281 else if (dailess && aux_dev->init)
1282 ret = aux_dev->init(&codec->dapm);
1283 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001284 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001285 return ret;
1286 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001287
Jarkko Nikula589c3562010-12-06 16:27:07 +02001288 /* register the rtd device */
1289 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001290
1291 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1292 if (!rtd->dev)
1293 return -ENOMEM;
1294 device_initialize(rtd->dev);
1295 rtd->dev->parent = card->dev;
1296 rtd->dev->release = rtd_release;
1297 rtd->dev->init_name = name;
1298 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001299 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001300 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1301 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1302 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1303 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001304 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001305 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001306 /* calling put_device() here to free the rtd->dev */
1307 put_device(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001308 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001309 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001310 return ret;
1311 }
1312 rtd->dev_registered = 1;
1313
1314 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001315 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001316 if (ret < 0)
1317 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001318 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001319
1320 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001321 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001322 if (ret < 0)
1323 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001324 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001325
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001326#ifdef CONFIG_DEBUG_FS
1327 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001328 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001329 goto out;
1330
1331 ret = soc_dpcm_debugfs_add(rtd);
1332 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001333 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001334
1335out:
1336#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001337 return 0;
1338}
1339
Stephen Warren62ae68f2012-06-08 12:34:23 -06001340static int soc_probe_link_components(struct snd_soc_card *card, int num,
1341 int order)
1342{
1343 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1344 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1345 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1346 struct snd_soc_platform *platform = rtd->platform;
1347 int ret;
1348
1349 /* probe the CPU-side component, if it is a CODEC */
1350 if (cpu_dai->codec &&
1351 !cpu_dai->codec->probed &&
1352 cpu_dai->codec->driver->probe_order == order) {
1353 ret = soc_probe_codec(card, cpu_dai->codec);
1354 if (ret < 0)
1355 return ret;
1356 }
1357
1358 /* probe the CODEC-side component */
1359 if (!codec_dai->codec->probed &&
1360 codec_dai->codec->driver->probe_order == order) {
1361 ret = soc_probe_codec(card, codec_dai->codec);
1362 if (ret < 0)
1363 return ret;
1364 }
1365
1366 /* probe the platform */
1367 if (!platform->probed &&
1368 platform->driver->probe_order == order) {
1369 ret = soc_probe_platform(card, platform);
1370 if (ret < 0)
1371 return ret;
1372 }
1373
1374 return 0;
1375}
1376
1377static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001378{
1379 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1380 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1381 struct snd_soc_codec *codec = rtd->codec;
1382 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001383 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1384 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1385 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001386 int ret;
1387
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001388 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001389 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001390
1391 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001392 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001393 codec_dai->card = card;
1394 cpu_dai->card = card;
1395
1396 /* set default power off timeout */
1397 rtd->pmdown_time = pmdown_time;
1398
1399 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001400 if (!cpu_dai->probed &&
1401 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001402 if (!cpu_dai->codec) {
1403 cpu_dai->dapm.card = card;
1404 if (!try_module_get(cpu_dai->dev->driver->owner))
1405 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001406
Stephen Warren18d75642012-06-08 12:34:21 -06001407 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001408 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001409
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001410 if (cpu_dai->driver->probe) {
1411 ret = cpu_dai->driver->probe(cpu_dai);
1412 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001413 dev_err(cpu_dai->dev,
1414 "ASoC: failed to probe CPU DAI %s: %d\n",
1415 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001416 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001417 return ret;
1418 }
1419 }
1420 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001421 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001422 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1423 }
1424
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001425 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001426 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001427 if (codec_dai->driver->probe) {
1428 ret = codec_dai->driver->probe(codec_dai);
1429 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001430 dev_err(codec_dai->dev,
1431 "ASoC: failed to probe CODEC DAI %s: %d\n",
1432 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001433 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001434 }
1435 }
1436
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001437 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001438 codec_dai->probed = 1;
1439 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001440 }
1441
Liam Girdwood0168bf02011-06-07 16:08:05 +01001442 /* complete DAI probe during last probe */
1443 if (order != SND_SOC_COMP_ORDER_LAST)
1444 return 0;
1445
Jarkko Nikula589c3562010-12-06 16:27:07 +02001446 ret = soc_post_component_init(card, codec, num, 0);
1447 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001448 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001449
Mark Brown36ae1a92012-01-06 17:12:45 -08001450 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001451 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001452 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1453 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001454
Namarta Kohli1245b702012-08-16 17:10:41 +05301455 if (cpu_dai->driver->compress_dai) {
1456 /*create compress_device"*/
1457 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001458 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001459 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301460 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001461 return ret;
1462 }
1463 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301464
1465 if (!dai_link->params) {
1466 /* create the pcm */
1467 ret = soc_new_pcm(rtd, num);
1468 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001469 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301470 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001471 return ret;
1472 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301473 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001474 INIT_DELAYED_WORK(&rtd->delayed_work,
1475 codec2codec_close_delayed_work);
1476
Namarta Kohli1245b702012-08-16 17:10:41 +05301477 /* link the DAI widgets */
1478 play_w = codec_dai->playback_widget;
1479 capture_w = cpu_dai->capture_widget;
1480 if (play_w && capture_w) {
1481 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Mark Brownc74184e2012-04-04 22:12:09 +01001482 capture_w, play_w);
Namarta Kohli1245b702012-08-16 17:10:41 +05301483 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001484 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301485 play_w->name, capture_w->name, ret);
1486 return ret;
1487 }
1488 }
1489
1490 play_w = cpu_dai->playback_widget;
1491 capture_w = codec_dai->capture_widget;
1492 if (play_w && capture_w) {
1493 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1494 capture_w, play_w);
1495 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001496 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301497 play_w->name, capture_w->name, ret);
1498 return ret;
1499 }
Mark Brownc74184e2012-04-04 22:12:09 +01001500 }
1501 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001502 }
1503
1504 /* add platform data for AC97 devices */
1505 if (rtd->codec_dai->driver->ac97_control)
1506 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1507
1508 return 0;
1509}
1510
1511#ifdef CONFIG_SND_SOC_AC97_BUS
1512static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1513{
1514 int ret;
1515
1516 /* Only instantiate AC97 if not already done by the adaptor
1517 * for the generic AC97 subsystem.
1518 */
1519 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001520 /*
1521 * It is possible that the AC97 device is already registered to
1522 * the device subsystem. This happens when the device is created
1523 * via snd_ac97_mixer(). Currently only SoC codec that does so
1524 * is the generic AC97 glue but others migh emerge.
1525 *
1526 * In those cases we don't try to register the device again.
1527 */
1528 if (!rtd->codec->ac97_created)
1529 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001530
1531 ret = soc_ac97_dev_register(rtd->codec);
1532 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001533 dev_err(rtd->codec->dev,
1534 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001535 return ret;
1536 }
1537
1538 rtd->codec->ac97_registered = 1;
1539 }
1540 return 0;
1541}
1542
1543static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1544{
1545 if (codec->ac97_registered) {
1546 soc_ac97_dev_unregister(codec);
1547 codec->ac97_registered = 0;
1548 }
1549}
1550#endif
1551
Mark Brownb19e6e72012-03-14 21:18:39 +00001552static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1553{
1554 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1555 struct snd_soc_codec *codec;
1556
1557 /* find CODEC from registered CODECs*/
1558 list_for_each_entry(codec, &codec_list, list) {
1559 if (!strcmp(codec->name, aux_dev->codec_name))
1560 return 0;
1561 }
1562
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001563 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
Mark Brownfb099cb2012-08-09 18:44:37 +01001564
Mark Brownb19e6e72012-03-14 21:18:39 +00001565 return -EPROBE_DEFER;
1566}
1567
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001568static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1569{
1570 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001571 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001572 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001573
1574 /* find CODEC from registered CODECs*/
1575 list_for_each_entry(codec, &codec_list, list) {
1576 if (!strcmp(codec->name, aux_dev->codec_name)) {
1577 if (codec->probed) {
1578 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001579 "ASoC: codec already probed");
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001580 ret = -EBUSY;
1581 goto out;
1582 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001583 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001584 }
1585 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001586 /* codec not found */
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001587 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001588 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001589
Jarkko Nikula676ad982010-12-03 09:18:22 +02001590found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001591 ret = soc_probe_codec(card, codec);
1592 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001593 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001594
Jarkko Nikula589c3562010-12-06 16:27:07 +02001595 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001596
1597out:
1598 return ret;
1599}
1600
1601static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1602{
1603 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1604 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001605
1606 /* unregister the rtd device */
1607 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001608 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001609 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001610 rtd->dev_registered = 0;
1611 }
1612
Jarkko Nikula589c3562010-12-06 16:27:07 +02001613 if (codec && codec->probed)
1614 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001615}
1616
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001617static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001618{
1619 int ret;
1620
1621 if (codec->cache_init)
1622 return 0;
1623
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001624 ret = snd_soc_cache_init(codec);
1625 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001626 dev_err(codec->dev,
1627 "ASoC: Failed to set cache compression type: %d\n",
1628 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001629 return ret;
1630 }
1631 codec->cache_init = 1;
1632 return 0;
1633}
1634
Mark Brownb19e6e72012-03-14 21:18:39 +00001635static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001636{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001637 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001638 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001639 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001640
Liam Girdwood01b9d992012-03-07 10:38:25 +00001641 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001642
Mark Brownb19e6e72012-03-14 21:18:39 +00001643 /* bind DAIs */
1644 for (i = 0; i < card->num_links; i++) {
1645 ret = soc_bind_dai_link(card, i);
1646 if (ret != 0)
1647 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001648 }
1649
Mark Brownb19e6e72012-03-14 21:18:39 +00001650 /* check aux_devs too */
1651 for (i = 0; i < card->num_aux_devs; i++) {
1652 ret = soc_check_aux_dev(card, i);
1653 if (ret != 0)
1654 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001655 }
1656
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001657 /* initialize the register cache for each available codec */
1658 list_for_each_entry(codec, &codec_list, list) {
1659 if (codec->cache_init)
1660 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001661 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001662 if (ret < 0)
1663 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001664 }
1665
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001666 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001667 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001668 card->owner, 0, &card->snd_card);
1669 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001670 dev_err(card->dev,
1671 "ASoC: can't create sound card for card %s: %d\n",
1672 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001673 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001674 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001675
Mark Browne37a4972011-03-02 18:21:57 +00001676 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1677 card->dapm.dev = card->dev;
1678 card->dapm.card = card;
1679 list_add(&card->dapm.list, &card->dapm_list);
1680
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001681#ifdef CONFIG_DEBUG_FS
1682 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1683#endif
1684
Mark Brown88ee1c62011-02-02 10:43:26 +00001685#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001686 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001687 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001688#endif
Andy Green6ed25972008-06-13 16:24:05 +01001689
Mark Brown9a841eb2011-04-12 17:51:37 -07001690 if (card->dapm_widgets)
1691 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1692 card->num_dapm_widgets);
1693
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001694 /* initialise the sound card only once */
1695 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001696 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001697 if (ret < 0)
1698 goto card_probe_error;
1699 }
1700
Stephen Warren62ae68f2012-06-08 12:34:23 -06001701 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001702 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1703 order++) {
1704 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001705 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001706 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001707 dev_err(card->dev,
1708 "ASoC: failed to instantiate card %d\n",
1709 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001710 goto probe_dai_err;
1711 }
1712 }
1713 }
1714
1715 /* probe all DAI links on this card */
1716 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1717 order++) {
1718 for (i = 0; i < card->num_links; i++) {
1719 ret = soc_probe_link_dais(card, i, order);
1720 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001721 dev_err(card->dev,
1722 "ASoC: failed to instantiate card %d\n",
1723 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001724 goto probe_dai_err;
1725 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001726 }
1727 }
1728
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001729 for (i = 0; i < card->num_aux_devs; i++) {
1730 ret = soc_probe_aux_dev(card, i);
1731 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001732 dev_err(card->dev,
1733 "ASoC: failed to add auxiliary devices %d\n",
1734 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001735 goto probe_aux_dev_err;
1736 }
1737 }
1738
Mark Brown888df392012-02-16 19:37:51 -08001739 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001740 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001741
Mark Brownb7af1da2011-04-07 19:18:44 +09001742 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001743 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001744
Mark Brownb8ad29d2011-03-02 18:35:51 +00001745 if (card->dapm_routes)
1746 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1747 card->num_dapm_routes);
1748
Mark Brown75d9ac42011-09-27 16:41:01 +01001749 for (i = 0; i < card->num_links; i++) {
1750 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001751 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001752
Mark Brownf04209a2012-04-09 16:29:21 +01001753 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001754 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001755 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001756 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001757 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001758 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001759 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001760 }
1761
1762 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001763 if (dai_fmt &&
1764 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001765 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1766 dai_fmt);
1767 if (ret != 0 && ret != -ENOTSUPP)
1768 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001769 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001770 ret);
1771 } else if (dai_fmt) {
1772 /* Flip the polarity for the "CPU" end */
1773 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1774 switch (dai_link->dai_fmt &
1775 SND_SOC_DAIFMT_MASTER_MASK) {
1776 case SND_SOC_DAIFMT_CBM_CFM:
1777 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1778 break;
1779 case SND_SOC_DAIFMT_CBM_CFS:
1780 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1781 break;
1782 case SND_SOC_DAIFMT_CBS_CFM:
1783 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1784 break;
1785 case SND_SOC_DAIFMT_CBS_CFS:
1786 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1787 break;
1788 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001789
1790 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001791 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001792 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001793 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001794 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001795 ret);
1796 }
1797 }
1798
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001799 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001800 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001801 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1802 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001803 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1804 "%s", card->driver_name ? card->driver_name : card->name);
1805 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1806 switch (card->snd_card->driver[i]) {
1807 case '_':
1808 case '-':
1809 case '\0':
1810 break;
1811 default:
1812 if (!isalnum(card->snd_card->driver[i]))
1813 card->snd_card->driver[i] = '_';
1814 break;
1815 }
1816 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001817
Mark Brown28e9ad92011-03-02 18:36:34 +00001818 if (card->late_probe) {
1819 ret = card->late_probe(card);
1820 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001821 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001822 card->name, ret);
1823 goto probe_aux_dev_err;
1824 }
1825 }
1826
Stephen Warren16332812011-11-23 12:42:04 -07001827 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001828 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001829 snd_soc_dapm_auto_nc_codec_pins(codec);
1830
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001831 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001832
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001833 ret = snd_card_register(card->snd_card);
1834 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001835 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1836 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001837 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001838 }
1839
1840#ifdef CONFIG_SND_SOC_AC97_BUS
1841 /* register any AC97 codecs */
1842 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001843 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1844 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001845 dev_err(card->dev,
1846 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001847 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001848 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001849 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001850 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001851 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001852#endif
1853
Mark Brown435c5e22008-12-04 15:32:53 +00001854 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001855 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001856 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001857
1858 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001859
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001860probe_aux_dev_err:
1861 for (i = 0; i < card->num_aux_devs; i++)
1862 soc_remove_aux_dev(card, i);
1863
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001864probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001865 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001866
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001867card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001868 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001869 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001870
1871 snd_card_free(card->snd_card);
1872
Mark Brownb19e6e72012-03-14 21:18:39 +00001873base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001874 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001875
Mark Brownb19e6e72012-03-14 21:18:39 +00001876 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001877}
1878
1879/* probes a new socdev */
1880static int soc_probe(struct platform_device *pdev)
1881{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001882 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001883
Vinod Koul70a7ca32011-01-14 19:22:48 +05301884 /*
1885 * no card, so machine driver should be registering card
1886 * we should not be here in that case so ret error
1887 */
1888 if (!card)
1889 return -EINVAL;
1890
Mark Brownfe4085e2012-03-02 13:07:41 +00001891 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001892 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001893 card->name);
1894
Mark Brown435c5e22008-12-04 15:32:53 +00001895 /* Bodge while we unpick instantiation */
1896 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001897
Mark Brown28d528c2012-08-09 18:45:23 +01001898 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001899}
1900
Vinod Koulb0e26482011-01-13 22:48:02 +05301901static int soc_cleanup_card_resources(struct snd_soc_card *card)
1902{
Vinod Koulb0e26482011-01-13 22:48:02 +05301903 int i;
1904
1905 /* make sure any delayed work runs */
1906 for (i = 0; i < card->num_rtd; i++) {
1907 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001908 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301909 }
1910
1911 /* remove auxiliary devices */
1912 for (i = 0; i < card->num_aux_devs; i++)
1913 soc_remove_aux_dev(card, i);
1914
1915 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001916 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301917
1918 soc_cleanup_card_debugfs(card);
1919
1920 /* remove the card */
1921 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001922 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301923
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001924 snd_soc_dapm_free(&card->dapm);
1925
Vinod Koulb0e26482011-01-13 22:48:02 +05301926 snd_card_free(card->snd_card);
1927 return 0;
1928
1929}
1930
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001931/* removes a socdev */
1932static int soc_remove(struct platform_device *pdev)
1933{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001934 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001935
Mark Brownc5af3a22008-11-28 13:29:45 +00001936 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001937 return 0;
1938}
1939
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001940int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001941{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001942 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001943 int i;
Mark Brown51737472009-06-22 13:16:51 +01001944
1945 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001946 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001947
1948 /* Flush out pmdown_time work - we actually do want to run it
1949 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001950 for (i = 0; i < card->num_rtd; i++) {
1951 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001952 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001953 }
Mark Brown51737472009-06-22 13:16:51 +01001954
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001955 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001956
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001957 /* deactivate pins to sleep state */
1958 for (i = 0; i < card->num_rtd; i++) {
1959 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1960 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
1961 pinctrl_pm_select_sleep_state(codec_dai->dev);
1962 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1963 }
1964
Mark Brown416356f2009-06-30 19:05:15 +01001965 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001966}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001967EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001968
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001969const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301970 .suspend = snd_soc_suspend,
1971 .resume = snd_soc_resume,
1972 .freeze = snd_soc_suspend,
1973 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001974 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301975 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001976};
Stephen Warrendeb26072011-04-05 19:35:30 -06001977EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001978
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001979/* ASoC platform driver */
1980static struct platform_driver soc_driver = {
1981 .driver = {
1982 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001983 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001984 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001985 },
1986 .probe = soc_probe,
1987 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001988};
1989
Mark Brown096e49d2009-07-05 15:12:22 +01001990/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001991 * snd_soc_new_ac97_codec - initailise AC97 device
1992 * @codec: audio codec
1993 * @ops: AC97 bus operations
1994 * @num: AC97 codec number
1995 *
1996 * Initialises AC97 codec resources for use by ad-hoc devices only.
1997 */
1998int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1999 struct snd_ac97_bus_ops *ops, int num)
2000{
2001 mutex_lock(&codec->mutex);
2002
2003 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2004 if (codec->ac97 == NULL) {
2005 mutex_unlock(&codec->mutex);
2006 return -ENOMEM;
2007 }
2008
2009 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2010 if (codec->ac97->bus == NULL) {
2011 kfree(codec->ac97);
2012 codec->ac97 = NULL;
2013 mutex_unlock(&codec->mutex);
2014 return -ENOMEM;
2015 }
2016
2017 codec->ac97->bus->ops = ops;
2018 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002019
2020 /*
2021 * Mark the AC97 device to be created by us. This way we ensure that the
2022 * device will be registered with the device subsystem later on.
2023 */
2024 codec->ac97_created = 1;
2025
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002026 mutex_unlock(&codec->mutex);
2027 return 0;
2028}
2029EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2030
Markus Pargmann741a5092013-08-19 17:05:55 +02002031static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2032
2033static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2034{
2035 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2036
2037 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2038
2039 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2040
2041 udelay(10);
2042
2043 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2044
2045 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2046 msleep(2);
2047}
2048
2049static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2050{
2051 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2052
2053 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2054
2055 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2056 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2057 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2058
2059 udelay(10);
2060
2061 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2062
2063 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2064 msleep(2);
2065}
2066
2067static int snd_soc_ac97_parse_pinctl(struct device *dev,
2068 struct snd_ac97_reset_cfg *cfg)
2069{
2070 struct pinctrl *p;
2071 struct pinctrl_state *state;
2072 int gpio;
2073 int ret;
2074
2075 p = devm_pinctrl_get(dev);
2076 if (IS_ERR(p)) {
2077 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002078 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002079 }
2080 cfg->pctl = p;
2081
2082 state = pinctrl_lookup_state(p, "ac97-reset");
2083 if (IS_ERR(state)) {
2084 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002085 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002086 }
2087 cfg->pstate_reset = state;
2088
2089 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2090 if (IS_ERR(state)) {
2091 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002092 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002093 }
2094 cfg->pstate_warm_reset = state;
2095
2096 state = pinctrl_lookup_state(p, "ac97-running");
2097 if (IS_ERR(state)) {
2098 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002099 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002100 }
2101 cfg->pstate_run = state;
2102
2103 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2104 if (gpio < 0) {
2105 dev_err(dev, "Can't find ac97-sync gpio\n");
2106 return gpio;
2107 }
2108 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2109 if (ret) {
2110 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2111 return ret;
2112 }
2113 cfg->gpio_sync = gpio;
2114
2115 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2116 if (gpio < 0) {
2117 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2118 return gpio;
2119 }
2120 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2121 if (ret) {
2122 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2123 return ret;
2124 }
2125 cfg->gpio_sdata = gpio;
2126
2127 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2128 if (gpio < 0) {
2129 dev_err(dev, "Can't find ac97-reset gpio\n");
2130 return gpio;
2131 }
2132 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2133 if (ret) {
2134 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2135 return ret;
2136 }
2137 cfg->gpio_reset = gpio;
2138
2139 return 0;
2140}
2141
Mark Brownb047e1c2013-06-26 12:45:59 +01002142struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002143EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002144
2145int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2146{
2147 if (ops == soc_ac97_ops)
2148 return 0;
2149
2150 if (soc_ac97_ops && ops)
2151 return -EBUSY;
2152
2153 soc_ac97_ops = ops;
2154
2155 return 0;
2156}
2157EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2158
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002159/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002160 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2161 *
2162 * This function sets the reset and warm_reset properties of ops and parses
2163 * the device node of pdev to get pinctrl states and gpio numbers to use.
2164 */
2165int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2166 struct platform_device *pdev)
2167{
2168 struct device *dev = &pdev->dev;
2169 struct snd_ac97_reset_cfg cfg;
2170 int ret;
2171
2172 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2173 if (ret)
2174 return ret;
2175
2176 ret = snd_soc_set_ac97_ops(ops);
2177 if (ret)
2178 return ret;
2179
2180 ops->warm_reset = snd_soc_ac97_warm_reset;
2181 ops->reset = snd_soc_ac97_reset;
2182
2183 snd_ac97_rst_cfg = cfg;
2184 return 0;
2185}
2186EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2187
2188/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002189 * snd_soc_free_ac97_codec - free AC97 codec device
2190 * @codec: audio codec
2191 *
2192 * Frees AC97 codec device resources.
2193 */
2194void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2195{
2196 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002197#ifdef CONFIG_SND_SOC_AC97_BUS
2198 soc_unregister_ac97_dai_link(codec);
2199#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002200 kfree(codec->ac97->bus);
2201 kfree(codec->ac97);
2202 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002203 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002204 mutex_unlock(&codec->mutex);
2205}
2206EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2207
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002208/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002209 * snd_soc_cnew - create new control
2210 * @_template: control template
2211 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002212 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002213 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002214 *
2215 * Create a new mixer control from a template control.
2216 *
2217 * Returns 0 for success, else error.
2218 */
2219struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002220 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002221 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002222{
2223 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002224 struct snd_kcontrol *kcontrol;
2225 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002226
2227 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002228 template.index = 0;
2229
Mark Brownefb7ac32011-03-08 17:23:24 +00002230 if (!long_name)
2231 long_name = template.name;
2232
2233 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002234 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002235 if (!name)
2236 return NULL;
2237
Mark Brownefb7ac32011-03-08 17:23:24 +00002238 template.name = name;
2239 } else {
2240 template.name = long_name;
2241 }
2242
2243 kcontrol = snd_ctl_new1(&template, data);
2244
2245 kfree(name);
2246
2247 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002248}
2249EXPORT_SYMBOL_GPL(snd_soc_cnew);
2250
Liam Girdwood022658b2012-02-03 17:43:09 +00002251static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2252 const struct snd_kcontrol_new *controls, int num_controls,
2253 const char *prefix, void *data)
2254{
2255 int err, i;
2256
2257 for (i = 0; i < num_controls; i++) {
2258 const struct snd_kcontrol_new *control = &controls[i];
2259 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2260 control->name, prefix));
2261 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002262 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2263 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002264 return err;
2265 }
2266 }
2267
2268 return 0;
2269}
2270
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002271struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2272 const char *name)
2273{
2274 struct snd_card *card = soc_card->snd_card;
2275 struct snd_kcontrol *kctl;
2276
2277 if (unlikely(!name))
2278 return NULL;
2279
2280 list_for_each_entry(kctl, &card->controls, list)
2281 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2282 return kctl;
2283 return NULL;
2284}
2285EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2286
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002287/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002288 * snd_soc_add_codec_controls - add an array of controls to a codec.
2289 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002290 * duplicating this code.
2291 *
2292 * @codec: codec to add controls to
2293 * @controls: array of controls to add
2294 * @num_controls: number of elements in the array
2295 *
2296 * Return 0 for success, else error.
2297 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002298int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002299 const struct snd_kcontrol_new *controls, int num_controls)
2300{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002301 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002302
Liam Girdwood022658b2012-02-03 17:43:09 +00002303 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2304 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002305}
Liam Girdwood022658b2012-02-03 17:43:09 +00002306EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002307
2308/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002309 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002310 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002311 *
2312 * @platform: platform to add controls to
2313 * @controls: array of controls to add
2314 * @num_controls: number of elements in the array
2315 *
2316 * Return 0 for success, else error.
2317 */
2318int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2319 const struct snd_kcontrol_new *controls, int num_controls)
2320{
2321 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002322
Liam Girdwood022658b2012-02-03 17:43:09 +00002323 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2324 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002325}
2326EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2327
2328/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002329 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2330 * Convenience function to add a list of controls.
2331 *
2332 * @soc_card: SoC card to add controls to
2333 * @controls: array of controls to add
2334 * @num_controls: number of elements in the array
2335 *
2336 * Return 0 for success, else error.
2337 */
2338int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2339 const struct snd_kcontrol_new *controls, int num_controls)
2340{
2341 struct snd_card *card = soc_card->snd_card;
2342
2343 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2344 NULL, soc_card);
2345}
2346EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2347
2348/**
2349 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2350 * Convienience function to add a list of controls.
2351 *
2352 * @dai: DAI to add controls to
2353 * @controls: array of controls to add
2354 * @num_controls: number of elements in the array
2355 *
2356 * Return 0 for success, else error.
2357 */
2358int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2359 const struct snd_kcontrol_new *controls, int num_controls)
2360{
2361 struct snd_card *card = dai->card->snd_card;
2362
2363 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2364 NULL, dai);
2365}
2366EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2367
2368/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002369 * snd_soc_info_enum_double - enumerated double mixer info callback
2370 * @kcontrol: mixer control
2371 * @uinfo: control element information
2372 *
2373 * Callback to provide information about a double enumerated
2374 * mixer control.
2375 *
2376 * Returns 0 for success.
2377 */
2378int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_info *uinfo)
2380{
2381 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2382
2383 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2384 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002385 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002386
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002387 if (uinfo->value.enumerated.item >= e->items)
2388 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002389 strlcpy(uinfo->value.enumerated.name,
2390 e->texts[uinfo->value.enumerated.item],
2391 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002392 return 0;
2393}
2394EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2395
2396/**
2397 * snd_soc_get_enum_double - enumerated double mixer get callback
2398 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002399 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002400 *
2401 * Callback to get the value of a double enumerated mixer.
2402 *
2403 * Returns 0 for success.
2404 */
2405int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2406 struct snd_ctl_elem_value *ucontrol)
2407{
2408 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2409 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002410 unsigned int val, item;
2411 unsigned int reg_val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002412
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002413 reg_val = snd_soc_read(codec, e->reg);
2414 val = (reg_val >> e->shift_l) & e->mask;
2415 item = snd_soc_enum_val_to_item(e, val);
2416 ucontrol->value.enumerated.item[0] = item;
2417 if (e->shift_l != e->shift_r) {
2418 val = (reg_val >> e->shift_l) & e->mask;
2419 item = snd_soc_enum_val_to_item(e, val);
2420 ucontrol->value.enumerated.item[1] = item;
2421 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002422
2423 return 0;
2424}
2425EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2426
2427/**
2428 * snd_soc_put_enum_double - enumerated double mixer put callback
2429 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002430 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002431 *
2432 * Callback to set the value of a double enumerated mixer.
2433 *
2434 * Returns 0 for success.
2435 */
2436int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2437 struct snd_ctl_elem_value *ucontrol)
2438{
2439 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2440 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002441 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002442 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002443 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002444
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002445 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002446 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002447 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002448 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002449 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002450 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002451 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002452 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002453 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002454 }
2455
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002456 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002457}
2458EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2459
2460/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002461 * snd_soc_read_signed - Read a codec register and interprete as signed value
2462 * @codec: codec
2463 * @reg: Register to read
2464 * @mask: Mask to use after shifting the register value
2465 * @shift: Right shift of register value
2466 * @sign_bit: Bit that describes if a number is negative or not.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002467 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002468 * This functions reads a codec register. The register value is shifted right
2469 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2470 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002471 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002472 * Returns the register value as signed int.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002473 */
Markus Pargmannf227b882014-01-16 16:02:10 +01002474static int snd_soc_read_signed(struct snd_soc_codec *codec, unsigned int reg,
2475 unsigned int mask, unsigned int shift, unsigned int sign_bit)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002476{
Markus Pargmannf227b882014-01-16 16:02:10 +01002477 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002478 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002479
Markus Pargmannf227b882014-01-16 16:02:10 +01002480 val = (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002481
Markus Pargmannf227b882014-01-16 16:02:10 +01002482 if (!sign_bit)
2483 return val;
2484
2485 /* non-negative number */
2486 if (!(val & BIT(sign_bit)))
2487 return val;
2488
2489 ret = val;
2490
2491 /*
2492 * The register most probably does not contain a full-sized int.
2493 * Instead we have an arbitrary number of bits in a signed
2494 * representation which has to be translated into a full-sized int.
2495 * This is done by filling up all bits above the sign-bit.
2496 */
2497 ret |= ~((int)(BIT(sign_bit) - 1));
2498
2499 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002500}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002501
2502/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002503 * snd_soc_info_volsw - single mixer info callback
2504 * @kcontrol: mixer control
2505 * @uinfo: control element information
2506 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002507 * Callback to provide information about a single mixer control, or a double
2508 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002509 *
2510 * Returns 0 for success.
2511 */
2512int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2513 struct snd_ctl_elem_info *uinfo)
2514{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002515 struct soc_mixer_control *mc =
2516 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002517 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002518
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002519 if (!mc->platform_max)
2520 mc->platform_max = mc->max;
2521 platform_max = mc->platform_max;
2522
2523 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002524 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2525 else
2526 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2527
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002528 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002529 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002530 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002531 return 0;
2532}
2533EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2534
2535/**
2536 * snd_soc_get_volsw - single mixer get callback
2537 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002538 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002539 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002540 * Callback to get the value of a single mixer control, or a double mixer
2541 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002542 *
2543 * Returns 0 for success.
2544 */
2545int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2546 struct snd_ctl_elem_value *ucontrol)
2547{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002548 struct soc_mixer_control *mc =
2549 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002550 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002551 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002552 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002553 unsigned int shift = mc->shift;
2554 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002555 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002556 int min = mc->min;
2557 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002558 unsigned int mask = (1 << fls(max)) - 1;
2559 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002560
Markus Pargmannf227b882014-01-16 16:02:10 +01002561 if (sign_bit)
2562 mask = BIT(sign_bit + 1) - 1;
2563
2564 ucontrol->value.integer.value[0] = snd_soc_read_signed(codec, reg, mask,
2565 shift, sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002566 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002567 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002568 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002569
2570 if (snd_soc_volsw_is_stereo(mc)) {
2571 if (reg == reg2)
2572 ucontrol->value.integer.value[1] =
Markus Pargmannf227b882014-01-16 16:02:10 +01002573 snd_soc_read_signed(codec, reg, mask, rshift,
2574 sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002575 else
2576 ucontrol->value.integer.value[1] =
Markus Pargmannf227b882014-01-16 16:02:10 +01002577 snd_soc_read_signed(codec, reg2, mask, shift,
2578 sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002579 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002580 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002581 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002582 }
2583
2584 return 0;
2585}
2586EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2587
2588/**
2589 * snd_soc_put_volsw - single mixer put callback
2590 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002591 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002592 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002593 * Callback to set the value of a single mixer control, or a double mixer
2594 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002595 *
2596 * Returns 0 for success.
2597 */
2598int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_value *ucontrol)
2600{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002601 struct soc_mixer_control *mc =
2602 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002603 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002604 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002605 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002606 unsigned int shift = mc->shift;
2607 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002608 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002609 int min = mc->min;
2610 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002611 unsigned int mask = (1 << fls(max)) - 1;
2612 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002613 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002614 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002615 unsigned int val2 = 0;
2616 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002617
Markus Pargmannf227b882014-01-16 16:02:10 +01002618 if (sign_bit)
2619 mask = BIT(sign_bit + 1) - 1;
2620
2621 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002622 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002623 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002624 val_mask = mask << shift;
2625 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002626 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002627 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002628 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002629 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002630 if (reg == reg2) {
2631 val_mask |= mask << rshift;
2632 val |= val2 << rshift;
2633 } else {
2634 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002635 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002636 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002637 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002638 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002639 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002640 return err;
2641
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002642 if (type_2r)
2643 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2644
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002645 return err;
2646}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002647EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002648
Mark Browne13ac2e2008-05-28 17:58:05 +01002649/**
Brian Austin1d99f242012-03-30 10:43:55 -05002650 * snd_soc_get_volsw_sx - single mixer get callback
2651 * @kcontrol: mixer control
2652 * @ucontrol: control element information
2653 *
2654 * Callback to get the value of a single mixer control, or a double mixer
2655 * control that spans 2 registers.
2656 *
2657 * Returns 0 for success.
2658 */
2659int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_value *ucontrol)
2661{
2662 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2663 struct soc_mixer_control *mc =
2664 (struct soc_mixer_control *)kcontrol->private_value;
2665
2666 unsigned int reg = mc->reg;
2667 unsigned int reg2 = mc->rreg;
2668 unsigned int shift = mc->shift;
2669 unsigned int rshift = mc->rshift;
2670 int max = mc->max;
2671 int min = mc->min;
2672 int mask = (1 << (fls(min + max) - 1)) - 1;
2673
2674 ucontrol->value.integer.value[0] =
2675 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2676
2677 if (snd_soc_volsw_is_stereo(mc))
2678 ucontrol->value.integer.value[1] =
2679 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2680
2681 return 0;
2682}
2683EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2684
2685/**
2686 * snd_soc_put_volsw_sx - double mixer set callback
2687 * @kcontrol: mixer control
2688 * @uinfo: control element information
2689 *
2690 * Callback to set the value of a double mixer control that spans 2 registers.
2691 *
2692 * Returns 0 for success.
2693 */
2694int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2695 struct snd_ctl_elem_value *ucontrol)
2696{
2697 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2698 struct soc_mixer_control *mc =
2699 (struct soc_mixer_control *)kcontrol->private_value;
2700
2701 unsigned int reg = mc->reg;
2702 unsigned int reg2 = mc->rreg;
2703 unsigned int shift = mc->shift;
2704 unsigned int rshift = mc->rshift;
2705 int max = mc->max;
2706 int min = mc->min;
2707 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002708 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002709 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002710
2711 val_mask = mask << shift;
2712 val = (ucontrol->value.integer.value[0] + min) & mask;
2713 val = val << shift;
2714
Mukund Navadad0558522012-11-09 11:53:40 +05302715 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2716 if (err < 0)
2717 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002718
2719 if (snd_soc_volsw_is_stereo(mc)) {
2720 val_mask = mask << rshift;
2721 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2722 val2 = val2 << rshift;
2723
2724 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2725 return err;
2726 }
2727 return 0;
2728}
2729EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2730
2731/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002732 * snd_soc_info_volsw_s8 - signed mixer info callback
2733 * @kcontrol: mixer control
2734 * @uinfo: control element information
2735 *
2736 * Callback to provide information about a signed mixer control.
2737 *
2738 * Returns 0 for success.
2739 */
2740int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2741 struct snd_ctl_elem_info *uinfo)
2742{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002743 struct soc_mixer_control *mc =
2744 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002745 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002746 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002747
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002748 if (!mc->platform_max)
2749 mc->platform_max = mc->max;
2750 platform_max = mc->platform_max;
2751
Mark Browne13ac2e2008-05-28 17:58:05 +01002752 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2753 uinfo->count = 2;
2754 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002755 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002756 return 0;
2757}
2758EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2759
2760/**
2761 * snd_soc_get_volsw_s8 - signed mixer get callback
2762 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002763 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002764 *
2765 * Callback to get the value of a signed mixer control.
2766 *
2767 * Returns 0 for success.
2768 */
2769int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2770 struct snd_ctl_elem_value *ucontrol)
2771{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002772 struct soc_mixer_control *mc =
2773 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002774 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002775 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002776 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002777 int val = snd_soc_read(codec, reg);
2778
2779 ucontrol->value.integer.value[0] =
2780 ((signed char)(val & 0xff))-min;
2781 ucontrol->value.integer.value[1] =
2782 ((signed char)((val >> 8) & 0xff))-min;
2783 return 0;
2784}
2785EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2786
2787/**
2788 * snd_soc_put_volsw_sgn - signed mixer put callback
2789 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002790 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002791 *
2792 * Callback to set the value of a signed mixer control.
2793 *
2794 * Returns 0 for success.
2795 */
2796int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2797 struct snd_ctl_elem_value *ucontrol)
2798{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002799 struct soc_mixer_control *mc =
2800 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002801 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002802 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002803 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002804 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002805
2806 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2807 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2808
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002809 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002810}
2811EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2812
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002813/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002814 * snd_soc_info_volsw_range - single mixer info callback with range.
2815 * @kcontrol: mixer control
2816 * @uinfo: control element information
2817 *
2818 * Callback to provide information, within a range, about a single
2819 * mixer control.
2820 *
2821 * returns 0 for success.
2822 */
2823int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2824 struct snd_ctl_elem_info *uinfo)
2825{
2826 struct soc_mixer_control *mc =
2827 (struct soc_mixer_control *)kcontrol->private_value;
2828 int platform_max;
2829 int min = mc->min;
2830
2831 if (!mc->platform_max)
2832 mc->platform_max = mc->max;
2833 platform_max = mc->platform_max;
2834
2835 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002836 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002837 uinfo->value.integer.min = 0;
2838 uinfo->value.integer.max = platform_max - min;
2839
2840 return 0;
2841}
2842EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2843
2844/**
2845 * snd_soc_put_volsw_range - single mixer put value callback with range.
2846 * @kcontrol: mixer control
2847 * @ucontrol: control element information
2848 *
2849 * Callback to set the value, within a range, for a single mixer control.
2850 *
2851 * Returns 0 for success.
2852 */
2853int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
2855{
2856 struct soc_mixer_control *mc =
2857 (struct soc_mixer_control *)kcontrol->private_value;
2858 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2859 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002860 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002861 unsigned int shift = mc->shift;
2862 int min = mc->min;
2863 int max = mc->max;
2864 unsigned int mask = (1 << fls(max)) - 1;
2865 unsigned int invert = mc->invert;
2866 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002867 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002868
2869 val = ((ucontrol->value.integer.value[0] + min) & mask);
2870 if (invert)
2871 val = max - val;
2872 val_mask = mask << shift;
2873 val = val << shift;
2874
Mark Brown9bde4f02012-12-19 16:05:00 +00002875 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002876 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002877 return ret;
2878
2879 if (snd_soc_volsw_is_stereo(mc)) {
2880 val = ((ucontrol->value.integer.value[1] + min) & mask);
2881 if (invert)
2882 val = max - val;
2883 val_mask = mask << shift;
2884 val = val << shift;
2885
2886 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
2887 }
2888
2889 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002890}
2891EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2892
2893/**
2894 * snd_soc_get_volsw_range - single mixer get callback with range
2895 * @kcontrol: mixer control
2896 * @ucontrol: control element information
2897 *
2898 * Callback to get the value, within a range, of a single mixer control.
2899 *
2900 * Returns 0 for success.
2901 */
2902int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2903 struct snd_ctl_elem_value *ucontrol)
2904{
2905 struct soc_mixer_control *mc =
2906 (struct soc_mixer_control *)kcontrol->private_value;
2907 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2908 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002909 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002910 unsigned int shift = mc->shift;
2911 int min = mc->min;
2912 int max = mc->max;
2913 unsigned int mask = (1 << fls(max)) - 1;
2914 unsigned int invert = mc->invert;
2915
2916 ucontrol->value.integer.value[0] =
2917 (snd_soc_read(codec, reg) >> shift) & mask;
2918 if (invert)
2919 ucontrol->value.integer.value[0] =
2920 max - ucontrol->value.integer.value[0];
2921 ucontrol->value.integer.value[0] =
2922 ucontrol->value.integer.value[0] - min;
2923
Mark Brown9bde4f02012-12-19 16:05:00 +00002924 if (snd_soc_volsw_is_stereo(mc)) {
2925 ucontrol->value.integer.value[1] =
2926 (snd_soc_read(codec, rreg) >> shift) & mask;
2927 if (invert)
2928 ucontrol->value.integer.value[1] =
2929 max - ucontrol->value.integer.value[1];
2930 ucontrol->value.integer.value[1] =
2931 ucontrol->value.integer.value[1] - min;
2932 }
2933
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002934 return 0;
2935}
2936EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2937
2938/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002939 * snd_soc_limit_volume - Set new limit to an existing volume control.
2940 *
2941 * @codec: where to look for the control
2942 * @name: Name of the control
2943 * @max: new maximum limit
2944 *
2945 * Return 0 for success, else error.
2946 */
2947int snd_soc_limit_volume(struct snd_soc_codec *codec,
2948 const char *name, int max)
2949{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002950 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002951 struct snd_kcontrol *kctl;
2952 struct soc_mixer_control *mc;
2953 int found = 0;
2954 int ret = -EINVAL;
2955
2956 /* Sanity check for name and max */
2957 if (unlikely(!name || max <= 0))
2958 return -EINVAL;
2959
2960 list_for_each_entry(kctl, &card->controls, list) {
2961 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2962 found = 1;
2963 break;
2964 }
2965 }
2966 if (found) {
2967 mc = (struct soc_mixer_control *)kctl->private_value;
2968 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002969 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002970 ret = 0;
2971 }
2972 }
2973 return ret;
2974}
2975EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2976
Mark Brown71d08512011-10-10 18:31:26 +01002977int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2978 struct snd_ctl_elem_info *uinfo)
2979{
2980 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2981 struct soc_bytes *params = (void *)kcontrol->private_value;
2982
2983 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2984 uinfo->count = params->num_regs * codec->val_bytes;
2985
2986 return 0;
2987}
2988EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2989
2990int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2991 struct snd_ctl_elem_value *ucontrol)
2992{
2993 struct soc_bytes *params = (void *)kcontrol->private_value;
2994 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2995 int ret;
2996
2997 if (codec->using_regmap)
2998 ret = regmap_raw_read(codec->control_data, params->base,
2999 ucontrol->value.bytes.data,
3000 params->num_regs * codec->val_bytes);
3001 else
3002 ret = -EINVAL;
3003
Mark Brownf831b052012-02-17 16:20:33 -08003004 /* Hide any masked bytes to ensure consistent data reporting */
3005 if (ret == 0 && params->mask) {
3006 switch (codec->val_bytes) {
3007 case 1:
3008 ucontrol->value.bytes.data[0] &= ~params->mask;
3009 break;
3010 case 2:
3011 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003012 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003013 break;
3014 case 4:
3015 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003016 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003017 break;
3018 default:
3019 return -EINVAL;
3020 }
3021 }
3022
Mark Brown71d08512011-10-10 18:31:26 +01003023 return ret;
3024}
3025EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3026
3027int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3028 struct snd_ctl_elem_value *ucontrol)
3029{
3030 struct soc_bytes *params = (void *)kcontrol->private_value;
3031 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003032 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003033 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003034 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003035
Mark Brownf831b052012-02-17 16:20:33 -08003036 if (!codec->using_regmap)
3037 return -EINVAL;
3038
Mark Brownf831b052012-02-17 16:20:33 -08003039 len = params->num_regs * codec->val_bytes;
3040
Mark Brownb5a8fe42013-01-20 21:42:22 +09003041 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3042 if (!data)
3043 return -ENOMEM;
3044
Mark Brownf831b052012-02-17 16:20:33 -08003045 /*
3046 * If we've got a mask then we need to preserve the register
3047 * bits. We shouldn't modify the incoming data so take a
3048 * copy.
3049 */
3050 if (params->mask) {
3051 ret = regmap_read(codec->control_data, params->base, &val);
3052 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003053 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003054
3055 val &= params->mask;
3056
Mark Brownf831b052012-02-17 16:20:33 -08003057 switch (codec->val_bytes) {
3058 case 1:
3059 ((u8 *)data)[0] &= ~params->mask;
3060 ((u8 *)data)[0] |= val;
3061 break;
3062 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003063 mask = ~params->mask;
3064 ret = regmap_parse_val(codec->control_data,
3065 &mask, &mask);
3066 if (ret != 0)
3067 goto out;
3068
3069 ((u16 *)data)[0] &= mask;
3070
3071 ret = regmap_parse_val(codec->control_data,
3072 &val, &val);
3073 if (ret != 0)
3074 goto out;
3075
3076 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003077 break;
3078 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003079 mask = ~params->mask;
3080 ret = regmap_parse_val(codec->control_data,
3081 &mask, &mask);
3082 if (ret != 0)
3083 goto out;
3084
3085 ((u32 *)data)[0] &= mask;
3086
3087 ret = regmap_parse_val(codec->control_data,
3088 &val, &val);
3089 if (ret != 0)
3090 goto out;
3091
3092 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003093 break;
3094 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003095 ret = -EINVAL;
3096 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003097 }
3098 }
3099
3100 ret = regmap_raw_write(codec->control_data, params->base,
3101 data, len);
3102
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003103out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003104 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003105
3106 return ret;
3107}
3108EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3109
Vinod Kould9881202014-05-02 10:58:11 +05303110int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_info *ucontrol)
3112{
3113 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3114
3115 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3116 ucontrol->count = params->max;
3117
3118 return 0;
3119}
3120EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3121
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003122/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003123 * snd_soc_info_xr_sx - signed multi register info callback
3124 * @kcontrol: mreg control
3125 * @uinfo: control element information
3126 *
3127 * Callback to provide information of a control that can
3128 * span multiple codec registers which together
3129 * forms a single signed value in a MSB/LSB manner.
3130 *
3131 * Returns 0 for success.
3132 */
3133int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3134 struct snd_ctl_elem_info *uinfo)
3135{
3136 struct soc_mreg_control *mc =
3137 (struct soc_mreg_control *)kcontrol->private_value;
3138 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3139 uinfo->count = 1;
3140 uinfo->value.integer.min = mc->min;
3141 uinfo->value.integer.max = mc->max;
3142
3143 return 0;
3144}
3145EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3146
3147/**
3148 * snd_soc_get_xr_sx - signed multi register get callback
3149 * @kcontrol: mreg control
3150 * @ucontrol: control element information
3151 *
3152 * Callback to get the value of a control that can span
3153 * multiple codec registers which together forms a single
3154 * signed value in a MSB/LSB manner. The control supports
3155 * specifying total no of bits used to allow for bitfields
3156 * across the multiple codec registers.
3157 *
3158 * Returns 0 for success.
3159 */
3160int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3161 struct snd_ctl_elem_value *ucontrol)
3162{
3163 struct soc_mreg_control *mc =
3164 (struct soc_mreg_control *)kcontrol->private_value;
3165 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3166 unsigned int regbase = mc->regbase;
3167 unsigned int regcount = mc->regcount;
Lars-Peter Clausen6137a5c2014-03-18 09:02:06 +01003168 unsigned int regwshift = codec->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003169 unsigned int regwmask = (1<<regwshift)-1;
3170 unsigned int invert = mc->invert;
3171 unsigned long mask = (1UL<<mc->nbits)-1;
3172 long min = mc->min;
3173 long max = mc->max;
3174 long val = 0;
3175 unsigned long regval;
3176 unsigned int i;
3177
3178 for (i = 0; i < regcount; i++) {
3179 regval = snd_soc_read(codec, regbase+i) & regwmask;
3180 val |= regval << (regwshift*(regcount-i-1));
3181 }
3182 val &= mask;
3183 if (min < 0 && val > max)
3184 val |= ~mask;
3185 if (invert)
3186 val = max - val;
3187 ucontrol->value.integer.value[0] = val;
3188
3189 return 0;
3190}
3191EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3192
3193/**
3194 * snd_soc_put_xr_sx - signed multi register get callback
3195 * @kcontrol: mreg control
3196 * @ucontrol: control element information
3197 *
3198 * Callback to set the value of a control that can span
3199 * multiple codec registers which together forms a single
3200 * signed value in a MSB/LSB manner. The control supports
3201 * specifying total no of bits used to allow for bitfields
3202 * across the multiple codec registers.
3203 *
3204 * Returns 0 for success.
3205 */
3206int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3207 struct snd_ctl_elem_value *ucontrol)
3208{
3209 struct soc_mreg_control *mc =
3210 (struct soc_mreg_control *)kcontrol->private_value;
3211 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3212 unsigned int regbase = mc->regbase;
3213 unsigned int regcount = mc->regcount;
Lars-Peter Clausen6137a5c2014-03-18 09:02:06 +01003214 unsigned int regwshift = codec->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003215 unsigned int regwmask = (1<<regwshift)-1;
3216 unsigned int invert = mc->invert;
3217 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003218 long max = mc->max;
3219 long val = ucontrol->value.integer.value[0];
3220 unsigned int i, regval, regmask;
3221 int err;
3222
3223 if (invert)
3224 val = max - val;
3225 val &= mask;
3226 for (i = 0; i < regcount; i++) {
3227 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3228 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3229 err = snd_soc_update_bits_locked(codec, regbase+i,
3230 regmask, regval);
3231 if (err < 0)
3232 return err;
3233 }
3234
3235 return 0;
3236}
3237EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3238
3239/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003240 * snd_soc_get_strobe - strobe get callback
3241 * @kcontrol: mixer control
3242 * @ucontrol: control element information
3243 *
3244 * Callback get the value of a strobe mixer control.
3245 *
3246 * Returns 0 for success.
3247 */
3248int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3249 struct snd_ctl_elem_value *ucontrol)
3250{
3251 struct soc_mixer_control *mc =
3252 (struct soc_mixer_control *)kcontrol->private_value;
3253 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3254 unsigned int reg = mc->reg;
3255 unsigned int shift = mc->shift;
3256 unsigned int mask = 1 << shift;
3257 unsigned int invert = mc->invert != 0;
3258 unsigned int val = snd_soc_read(codec, reg) & mask;
3259
3260 if (shift != 0 && val != 0)
3261 val = val >> shift;
3262 ucontrol->value.enumerated.item[0] = val ^ invert;
3263
3264 return 0;
3265}
3266EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3267
3268/**
3269 * snd_soc_put_strobe - strobe put callback
3270 * @kcontrol: mixer control
3271 * @ucontrol: control element information
3272 *
3273 * Callback strobe a register bit to high then low (or the inverse)
3274 * in one pass of a single mixer enum control.
3275 *
3276 * Returns 1 for success.
3277 */
3278int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3279 struct snd_ctl_elem_value *ucontrol)
3280{
3281 struct soc_mixer_control *mc =
3282 (struct soc_mixer_control *)kcontrol->private_value;
3283 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3284 unsigned int reg = mc->reg;
3285 unsigned int shift = mc->shift;
3286 unsigned int mask = 1 << shift;
3287 unsigned int invert = mc->invert != 0;
3288 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3289 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3290 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3291 int err;
3292
3293 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3294 if (err < 0)
3295 return err;
3296
3297 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3298 return err;
3299}
3300EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3301
3302/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003303 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3304 * @dai: DAI
3305 * @clk_id: DAI specific clock ID
3306 * @freq: new clock frequency in Hz
3307 * @dir: new clock direction - input/output.
3308 *
3309 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3310 */
3311int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3312 unsigned int freq, int dir)
3313{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003314 if (dai->driver && dai->driver->ops->set_sysclk)
3315 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003316 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003317 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003318 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003319 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003320 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003321}
3322EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3323
3324/**
Mark Brownec4ee522011-03-07 20:58:11 +00003325 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3326 * @codec: CODEC
3327 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003328 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003329 * @freq: new clock frequency in Hz
3330 * @dir: new clock direction - input/output.
3331 *
3332 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3333 */
3334int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003335 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003336{
3337 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003338 return codec->driver->set_sysclk(codec, clk_id, source,
3339 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003340 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003341 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003342}
3343EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3344
3345/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003346 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3347 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003348 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003349 * @div: new clock divisor.
3350 *
3351 * Configures the clock dividers. This is used to derive the best DAI bit and
3352 * frame clocks from the system or master clock. It's best to set the DAI bit
3353 * and frame clocks as low as possible to save system power.
3354 */
3355int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3356 int div_id, int div)
3357{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003358 if (dai->driver && dai->driver->ops->set_clkdiv)
3359 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003360 else
3361 return -EINVAL;
3362}
3363EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3364
3365/**
3366 * snd_soc_dai_set_pll - configure DAI PLL.
3367 * @dai: DAI
3368 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003369 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003370 * @freq_in: PLL input clock frequency in Hz
3371 * @freq_out: requested PLL output clock frequency in Hz
3372 *
3373 * Configures and enables PLL to generate output clock based on input clock.
3374 */
Mark Brown85488032009-09-05 18:52:16 +01003375int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3376 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003377{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003378 if (dai->driver && dai->driver->ops->set_pll)
3379 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003380 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003381 else if (dai->codec && dai->codec->driver->set_pll)
3382 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3383 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003384 else
3385 return -EINVAL;
3386}
3387EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3388
Mark Brownec4ee522011-03-07 20:58:11 +00003389/*
3390 * snd_soc_codec_set_pll - configure codec PLL.
3391 * @codec: CODEC
3392 * @pll_id: DAI specific PLL ID
3393 * @source: DAI specific source for the PLL
3394 * @freq_in: PLL input clock frequency in Hz
3395 * @freq_out: requested PLL output clock frequency in Hz
3396 *
3397 * Configures and enables PLL to generate output clock based on input clock.
3398 */
3399int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3400 unsigned int freq_in, unsigned int freq_out)
3401{
3402 if (codec->driver->set_pll)
3403 return codec->driver->set_pll(codec, pll_id, source,
3404 freq_in, freq_out);
3405 else
3406 return -EINVAL;
3407}
3408EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3409
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003410/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003411 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3412 * @dai: DAI
3413 * @ratio Ratio of BCLK to Sample rate.
3414 *
3415 * Configures the DAI for a preset BCLK to sample rate ratio.
3416 */
3417int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3418{
3419 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3420 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3421 else
3422 return -EINVAL;
3423}
3424EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3425
3426/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003427 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3428 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003429 * @fmt: SND_SOC_DAIFMT_ format value.
3430 *
3431 * Configures the DAI hardware format and clocking.
3432 */
3433int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3434{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003435 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003436 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003437 if (dai->driver->ops->set_fmt == NULL)
3438 return -ENOTSUPP;
3439 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003440}
3441EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3442
3443/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003444 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003445 * @slots: Number of slots in use.
3446 * @tx_mask: bitmask representing active TX slots.
3447 * @rx_mask: bitmask representing active RX slots.
3448 *
3449 * Generates the TDM tx and rx slot default masks for DAI.
3450 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003451static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003452 unsigned int *tx_mask,
3453 unsigned int *rx_mask)
3454{
3455 if (*tx_mask || *rx_mask)
3456 return 0;
3457
3458 if (!slots)
3459 return -EINVAL;
3460
3461 *tx_mask = (1 << slots) - 1;
3462 *rx_mask = (1 << slots) - 1;
3463
3464 return 0;
3465}
3466
3467/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003468 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3469 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003470 * @tx_mask: bitmask representing active TX slots.
3471 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003472 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003473 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003474 *
3475 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3476 * specific.
3477 */
3478int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003479 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003480{
Xiubo Lie5c21512014-03-21 14:17:12 +08003481 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3482 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003483 &tx_mask, &rx_mask);
3484 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003485 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003486
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003487 if (dai->driver && dai->driver->ops->set_tdm_slot)
3488 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003489 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003490 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003491 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003492}
3493EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3494
3495/**
Barry Song472df3c2009-09-12 01:16:29 +08003496 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3497 * @dai: DAI
3498 * @tx_num: how many TX channels
3499 * @tx_slot: pointer to an array which imply the TX slot number channel
3500 * 0~num-1 uses
3501 * @rx_num: how many RX channels
3502 * @rx_slot: pointer to an array which imply the RX slot number channel
3503 * 0~num-1 uses
3504 *
3505 * configure the relationship between channel number and TDM slot number.
3506 */
3507int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3508 unsigned int tx_num, unsigned int *tx_slot,
3509 unsigned int rx_num, unsigned int *rx_slot)
3510{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003511 if (dai->driver && dai->driver->ops->set_channel_map)
3512 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003513 rx_num, rx_slot);
3514 else
3515 return -EINVAL;
3516}
3517EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3518
3519/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003520 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3521 * @dai: DAI
3522 * @tristate: tristate enable
3523 *
3524 * Tristates the DAI so that others can use it.
3525 */
3526int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3527{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003528 if (dai->driver && dai->driver->ops->set_tristate)
3529 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003530 else
3531 return -EINVAL;
3532}
3533EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3534
3535/**
3536 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3537 * @dai: DAI
3538 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003539 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003540 *
3541 * Mutes the DAI DAC.
3542 */
Mark Brownda183962013-02-06 15:44:07 +00003543int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3544 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003545{
Mark Brownda183962013-02-06 15:44:07 +00003546 if (!dai->driver)
3547 return -ENOTSUPP;
3548
3549 if (dai->driver->ops->mute_stream)
3550 return dai->driver->ops->mute_stream(dai, mute, direction);
3551 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3552 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003553 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003554 else
Mark Brown04570c62012-04-13 19:16:03 +01003555 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003556}
3557EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3558
Mark Brownc5af3a22008-11-28 13:29:45 +00003559/**
3560 * snd_soc_register_card - Register a card with the ASoC core
3561 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003562 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003563 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003564 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303565int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003566{
Mark Brownb19e6e72012-03-14 21:18:39 +00003567 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003568
Mark Brownc5af3a22008-11-28 13:29:45 +00003569 if (!card->name || !card->dev)
3570 return -EINVAL;
3571
Stephen Warren5a504962011-12-21 10:40:59 -07003572 for (i = 0; i < card->num_links; i++) {
3573 struct snd_soc_dai_link *link = &card->dai_link[i];
3574
3575 /*
3576 * Codec must be specified by 1 of name or OF node,
3577 * not both or neither.
3578 */
3579 if (!!link->codec_name == !!link->codec_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003580 dev_err(card->dev,
3581 "ASoC: Neither/both codec name/of_node are set for %s\n",
3582 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003583 return -EINVAL;
3584 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003585 /* Codec DAI name must be specified */
3586 if (!link->codec_dai_name) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003587 dev_err(card->dev,
3588 "ASoC: codec_dai_name not set for %s\n",
3589 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003590 return -EINVAL;
3591 }
Stephen Warren5a504962011-12-21 10:40:59 -07003592
3593 /*
3594 * Platform may be specified by either name or OF node, but
3595 * can be left unspecified, and a dummy platform will be used.
3596 */
3597 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003598 dev_err(card->dev,
3599 "ASoC: Both platform name/of_node are set for %s\n",
3600 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003601 return -EINVAL;
3602 }
3603
3604 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003605 * CPU device may be specified by either name or OF node, but
3606 * can be left unspecified, and will be matched based on DAI
3607 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003608 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003609 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003610 dev_err(card->dev,
3611 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3612 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003613 return -EINVAL;
3614 }
3615 /*
3616 * At least one of CPU DAI name or CPU device name/node must be
3617 * specified
3618 */
3619 if (!link->cpu_dai_name &&
3620 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003621 dev_err(card->dev,
3622 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3623 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003624 return -EINVAL;
3625 }
3626 }
3627
Mark Browned77cc12011-05-03 18:25:34 +01003628 dev_set_drvdata(card->dev, card);
3629
Stephen Warren111c6412011-01-28 14:26:35 -07003630 snd_soc_initialize_card_lists(card);
3631
Vinod Koul150dd2f2011-01-13 22:48:32 +05303632 soc_init_card_debugfs(card);
3633
Mark Brown181a6892012-03-14 20:18:49 +00003634 card->rtd = devm_kzalloc(card->dev,
3635 sizeof(struct snd_soc_pcm_runtime) *
3636 (card->num_links + card->num_aux_devs),
3637 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003638 if (card->rtd == NULL)
3639 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003640 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003641 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003642
3643 for (i = 0; i < card->num_links; i++)
3644 card->rtd[i].dai_link = &card->dai_link[i];
3645
Mark Brownc5af3a22008-11-28 13:29:45 +00003646 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003647 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003648 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003649 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003650 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003651
Mark Brownb19e6e72012-03-14 21:18:39 +00003652 ret = snd_soc_instantiate_card(card);
3653 if (ret != 0)
3654 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003655
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003656 /* deactivate pins to sleep state */
3657 for (i = 0; i < card->num_rtd; i++) {
3658 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3659 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3660 if (!codec_dai->active)
3661 pinctrl_pm_select_sleep_state(codec_dai->dev);
3662 if (!cpu_dai->active)
3663 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3664 }
3665
Mark Brownb19e6e72012-03-14 21:18:39 +00003666 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003667}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303668EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003669
3670/**
3671 * snd_soc_unregister_card - Unregister a card with the ASoC core
3672 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003673 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003674 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003675 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303676int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003677{
Vinod Koulb0e26482011-01-13 22:48:02 +05303678 if (card->instantiated)
3679 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003680 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003681
3682 return 0;
3683}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303684EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003685
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003686/*
3687 * Simplify DAI link configuration by removing ".-1" from device names
3688 * and sanitizing names.
3689 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003690static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003691{
3692 char *found, name[NAME_SIZE];
3693 int id1, id2;
3694
3695 if (dev_name(dev) == NULL)
3696 return NULL;
3697
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003698 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003699
3700 /* are we a "%s.%d" name (platform and SPI components) */
3701 found = strstr(name, dev->driver->name);
3702 if (found) {
3703 /* get ID */
3704 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3705
3706 /* discard ID from name if ID == -1 */
3707 if (*id == -1)
3708 found[strlen(dev->driver->name)] = '\0';
3709 }
3710
3711 } else {
3712 /* I2C component devices are named "bus-addr" */
3713 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3714 char tmp[NAME_SIZE];
3715
3716 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003717 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003718
3719 /* sanitize component name for DAI link creation */
3720 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003721 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003722 } else
3723 *id = 0;
3724 }
3725
3726 return kstrdup(name, GFP_KERNEL);
3727}
3728
3729/*
3730 * Simplify DAI link naming for single devices with multiple DAIs by removing
3731 * any ".-1" and using the DAI name (instead of device name).
3732 */
3733static inline char *fmt_multiple_name(struct device *dev,
3734 struct snd_soc_dai_driver *dai_drv)
3735{
3736 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003737 dev_err(dev,
3738 "ASoC: error - multiple DAI %s registered with no name\n",
3739 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003740 return NULL;
3741 }
3742
3743 return kstrdup(dai_drv->name, GFP_KERNEL);
3744}
3745
Mark Brown91151712008-11-30 23:31:24 +00003746/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003747 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003748 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003749 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003750 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003751static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003752{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003753 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003754
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003755 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003756 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3757 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003758 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003759 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003760 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003761 }
Mark Brown91151712008-11-30 23:31:24 +00003762}
Mark Brown91151712008-11-30 23:31:24 +00003763
3764/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003765 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003766 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003767 * @component: The component the DAIs are registered for
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003768 * @codec: The CODEC that the DAIs are registered for, NULL if the component is
3769 * not a CODEC.
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003770 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003771 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003772 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3773 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003774 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003775static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003776 struct snd_soc_codec *codec, struct snd_soc_dai_driver *dai_drv,
3777 size_t count, bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003778{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003779 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003780 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003781 unsigned int i;
3782 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003783
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003784 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003785
3786 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003787
3788 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003789 if (dai == NULL) {
3790 ret = -ENOMEM;
3791 goto err;
3792 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003793
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003794 /*
3795 * Back in the old days when we still had component-less DAIs,
3796 * instead of having a static name, component-less DAIs would
3797 * inherit the name of the parent device so it is possible to
3798 * register multiple instances of the DAI. We still need to keep
3799 * the same naming style even though those DAIs are not
3800 * component-less anymore.
3801 */
3802 if (count == 1 && legacy_dai_naming) {
3803 dai->name = fmt_single_name(dev, &dai->id);
3804 } else {
3805 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3806 if (dai_drv[i].id)
3807 dai->id = dai_drv[i].id;
3808 else
3809 dai->id = i;
3810 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003811 if (dai->name == NULL) {
3812 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003813 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003814 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003815 }
3816
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003817 dai->component = component;
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003818 dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003819 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003820 dai->driver = &dai_drv[i];
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003821 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003822 if (!dai->driver->ops)
3823 dai->driver->ops = &null_dai_ops;
3824
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003825 if (!dai->codec)
3826 dai->dapm.idle_bias_off = 1;
3827
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003828 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003829
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003830 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003831 }
3832
3833 return 0;
3834
3835err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003836 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003837
3838 return ret;
3839}
Mark Brown91151712008-11-30 23:31:24 +00003840
3841/**
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003842 * snd_soc_register_component - Register a component with the ASoC core
3843 *
3844 */
3845static int
3846__snd_soc_register_component(struct device *dev,
3847 struct snd_soc_component *cmpnt,
3848 const struct snd_soc_component_driver *cmpnt_drv,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003849 struct snd_soc_codec *codec,
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003850 struct snd_soc_dai_driver *dai_drv,
3851 int num_dai, bool allow_single_dai)
3852{
3853 int ret;
3854
3855 dev_dbg(dev, "component register %s\n", dev_name(dev));
3856
3857 if (!cmpnt) {
3858 dev_err(dev, "ASoC: Failed to connecting component\n");
3859 return -ENOMEM;
3860 }
3861
3862 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
3863 if (!cmpnt->name) {
3864 dev_err(dev, "ASoC: Failed to simplifying name\n");
3865 return -ENOMEM;
3866 }
3867
3868 cmpnt->dev = dev;
3869 cmpnt->driver = cmpnt_drv;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003870 cmpnt->dai_drv = dai_drv;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003871 cmpnt->num_dai = num_dai;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003872 INIT_LIST_HEAD(&cmpnt->dai_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003873
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003874 ret = snd_soc_register_dais(cmpnt, codec, dai_drv, num_dai,
3875 allow_single_dai);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003876 if (ret < 0) {
3877 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3878 goto error_component_name;
3879 }
3880
3881 mutex_lock(&client_mutex);
3882 list_add(&cmpnt->list, &component_list);
3883 mutex_unlock(&client_mutex);
3884
3885 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
3886
3887 return ret;
3888
3889error_component_name:
3890 kfree(cmpnt->name);
3891
3892 return ret;
3893}
3894
3895int snd_soc_register_component(struct device *dev,
3896 const struct snd_soc_component_driver *cmpnt_drv,
3897 struct snd_soc_dai_driver *dai_drv,
3898 int num_dai)
3899{
3900 struct snd_soc_component *cmpnt;
3901
3902 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
3903 if (!cmpnt) {
3904 dev_err(dev, "ASoC: Failed to allocate memory\n");
3905 return -ENOMEM;
3906 }
3907
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003908 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01003909 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003910
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003911 return __snd_soc_register_component(dev, cmpnt, cmpnt_drv, NULL,
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003912 dai_drv, num_dai, true);
3913}
3914EXPORT_SYMBOL_GPL(snd_soc_register_component);
3915
3916/**
3917 * snd_soc_unregister_component - Unregister a component from the ASoC core
3918 *
3919 */
3920void snd_soc_unregister_component(struct device *dev)
3921{
3922 struct snd_soc_component *cmpnt;
3923
3924 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01003925 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003926 goto found;
3927 }
3928 return;
3929
3930found:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003931 snd_soc_unregister_dais(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003932
3933 mutex_lock(&client_mutex);
3934 list_del(&cmpnt->list);
3935 mutex_unlock(&client_mutex);
3936
3937 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
3938 kfree(cmpnt->name);
3939}
3940EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3941
3942/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003943 * snd_soc_add_platform - Add a platform to the ASoC core
3944 * @dev: The parent device for the platform
3945 * @platform: The platform to add
3946 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00003947 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003948int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01003949 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003950{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01003951 int ret;
3952
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003953 /* create platform component name */
3954 platform->name = fmt_single_name(dev, &platform->id);
Dan Carpenterb5c745f2013-07-22 09:56:54 +03003955 if (platform->name == NULL)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003956 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003957
3958 platform->dev = dev;
3959 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003960 platform->dapm.dev = dev;
3961 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003962 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003963 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003964
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01003965 /* register component */
3966 ret = __snd_soc_register_component(dev, &platform->component,
3967 &platform_drv->component_driver,
3968 NULL, NULL, 0, false);
3969 if (ret < 0) {
3970 dev_err(platform->component.dev,
3971 "ASoC: Failed to register component: %d\n", ret);
3972 return ret;
3973 }
3974
Mark Brown12a48a8c2008-12-03 19:40:30 +00003975 mutex_lock(&client_mutex);
3976 list_add(&platform->list, &platform_list);
3977 mutex_unlock(&client_mutex);
3978
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003979 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003980
3981 return 0;
3982}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003983EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3984
3985/**
3986 * snd_soc_register_platform - Register a platform with the ASoC core
3987 *
3988 * @platform: platform to register
3989 */
3990int snd_soc_register_platform(struct device *dev,
3991 const struct snd_soc_platform_driver *platform_drv)
3992{
3993 struct snd_soc_platform *platform;
3994 int ret;
3995
3996 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3997
3998 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3999 if (platform == NULL)
4000 return -ENOMEM;
4001
4002 ret = snd_soc_add_platform(dev, platform, platform_drv);
4003 if (ret)
4004 kfree(platform);
4005
4006 return ret;
4007}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004008EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4009
4010/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004011 * snd_soc_remove_platform - Remove a platform from the ASoC core
4012 * @platform: the platform to remove
4013 */
4014void snd_soc_remove_platform(struct snd_soc_platform *platform)
4015{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004016 snd_soc_unregister_component(platform->dev);
4017
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004018 mutex_lock(&client_mutex);
4019 list_del(&platform->list);
4020 mutex_unlock(&client_mutex);
4021
4022 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4023 platform->name);
4024 kfree(platform->name);
4025}
4026EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4027
4028struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4029{
4030 struct snd_soc_platform *platform;
4031
4032 list_for_each_entry(platform, &platform_list, list) {
4033 if (dev == platform->dev)
4034 return platform;
4035 }
4036
4037 return NULL;
4038}
4039EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4040
4041/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004042 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4043 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004044 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004045 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004046void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004047{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004048 struct snd_soc_platform *platform;
4049
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004050 platform = snd_soc_lookup_platform(dev);
4051 if (!platform)
4052 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004053
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004054 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004055 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004056}
4057EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4058
Mark Brown151ab222009-05-09 16:22:58 +01004059static u64 codec_format_map[] = {
4060 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4061 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4062 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4063 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4064 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4065 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4066 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4067 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4068 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4069 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4070 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4071 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4072 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4073 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4074 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4075 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4076};
4077
4078/* Fix up the DAI formats for endianness: codecs don't actually see
4079 * the endianness of the data but we're using the CPU format
4080 * definitions which do need to include endianness so we ensure that
4081 * codec DAIs always have both big and little endian variants set.
4082 */
4083static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4084{
4085 int i;
4086
4087 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4088 if (stream->formats & codec_format_map[i])
4089 stream->formats |= codec_format_map[i];
4090}
4091
Mark Brown0d0cf002008-12-10 14:32:45 +00004092/**
4093 * snd_soc_register_codec - Register a codec with the ASoC core
4094 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004095 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004096 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004097int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004098 const struct snd_soc_codec_driver *codec_drv,
4099 struct snd_soc_dai_driver *dai_drv,
4100 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004101{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004102 struct snd_soc_codec *codec;
4103 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004104
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004105 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004106
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004107 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4108 if (codec == NULL)
4109 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004110
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004111 /* create CODEC component name */
4112 codec->name = fmt_single_name(dev, &codec->id);
4113 if (codec->name == NULL) {
Kuninori Morimotof790b942013-02-25 00:40:09 -08004114 ret = -ENOMEM;
4115 goto fail_codec;
Mark Brown151ab222009-05-09 16:22:58 +01004116 }
4117
Mark Brownc3acec22010-12-02 16:15:29 +00004118 codec->write = codec_drv->write;
4119 codec->read = codec_drv->read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004120 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004121 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4122 codec->dapm.dev = dev;
4123 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004124 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004125 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004126 codec->dev = dev;
4127 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004128 codec->num_dai = num_dai;
Lars-Peter Clausen6137a5c2014-03-18 09:02:06 +01004129 codec->val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004130 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004131
4132 for (i = 0; i < num_dai; i++) {
4133 fixup_codec_formats(&dai_drv[i].playback);
4134 fixup_codec_formats(&dai_drv[i].capture);
4135 }
4136
Mark Brown054880f2012-04-09 17:29:19 +01004137 mutex_lock(&client_mutex);
4138 list_add(&codec->list, &codec_list);
4139 mutex_unlock(&client_mutex);
4140
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004141 /* register component */
4142 ret = __snd_soc_register_component(dev, &codec->component,
4143 &codec_drv->component_driver,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004144 codec, dai_drv, num_dai, false);
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004145 if (ret < 0) {
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004146 dev_err(codec->dev, "ASoC: Failed to regster component: %d\n", ret);
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004147 goto fail_codec_name;
Mark Brown26b01cc2010-08-18 20:20:55 +01004148 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004149
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004150 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004151 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004152
Kuninori Morimotof790b942013-02-25 00:40:09 -08004153fail_codec_name:
Kuninori Morimotoe1328a82013-03-07 17:42:33 -08004154 mutex_lock(&client_mutex);
4155 list_del(&codec->list);
4156 mutex_unlock(&client_mutex);
4157
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004158 kfree(codec->name);
Kuninori Morimotof790b942013-02-25 00:40:09 -08004159fail_codec:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004160 kfree(codec);
4161 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004162}
4163EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4164
4165/**
4166 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4167 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004168 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004169 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004170void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004171{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004172 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004173
4174 list_for_each_entry(codec, &codec_list, list) {
4175 if (dev == codec->dev)
4176 goto found;
4177 }
4178 return;
4179
4180found:
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004181 snd_soc_unregister_component(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004182
Mark Brown0d0cf002008-12-10 14:32:45 +00004183 mutex_lock(&client_mutex);
4184 list_del(&codec->list);
4185 mutex_unlock(&client_mutex);
4186
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004187 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004188
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004189 snd_soc_cache_exit(codec);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004190 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004191 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004192}
4193EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4194
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004195/* Retrieve a card's name from device tree */
4196int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4197 const char *propname)
4198{
4199 struct device_node *np = card->dev->of_node;
4200 int ret;
4201
4202 ret = of_property_read_string_index(np, propname, 0, &card->name);
4203 /*
4204 * EINVAL means the property does not exist. This is fine providing
4205 * card->name was previously set, which is checked later in
4206 * snd_soc_register_card.
4207 */
4208 if (ret < 0 && ret != -EINVAL) {
4209 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004210 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004211 propname, ret);
4212 return ret;
4213 }
4214
4215 return 0;
4216}
4217EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4218
Xiubo Li9a6d4862014-02-08 15:59:52 +08004219static const struct snd_soc_dapm_widget simple_widgets[] = {
4220 SND_SOC_DAPM_MIC("Microphone", NULL),
4221 SND_SOC_DAPM_LINE("Line", NULL),
4222 SND_SOC_DAPM_HP("Headphone", NULL),
4223 SND_SOC_DAPM_SPK("Speaker", NULL),
4224};
4225
4226int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4227 const char *propname)
4228{
4229 struct device_node *np = card->dev->of_node;
4230 struct snd_soc_dapm_widget *widgets;
4231 const char *template, *wname;
4232 int i, j, num_widgets, ret;
4233
4234 num_widgets = of_property_count_strings(np, propname);
4235 if (num_widgets < 0) {
4236 dev_err(card->dev,
4237 "ASoC: Property '%s' does not exist\n", propname);
4238 return -EINVAL;
4239 }
4240 if (num_widgets & 1) {
4241 dev_err(card->dev,
4242 "ASoC: Property '%s' length is not even\n", propname);
4243 return -EINVAL;
4244 }
4245
4246 num_widgets /= 2;
4247 if (!num_widgets) {
4248 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4249 propname);
4250 return -EINVAL;
4251 }
4252
4253 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4254 GFP_KERNEL);
4255 if (!widgets) {
4256 dev_err(card->dev,
4257 "ASoC: Could not allocate memory for widgets\n");
4258 return -ENOMEM;
4259 }
4260
4261 for (i = 0; i < num_widgets; i++) {
4262 ret = of_property_read_string_index(np, propname,
4263 2 * i, &template);
4264 if (ret) {
4265 dev_err(card->dev,
4266 "ASoC: Property '%s' index %d read error:%d\n",
4267 propname, 2 * i, ret);
4268 return -EINVAL;
4269 }
4270
4271 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4272 if (!strncmp(template, simple_widgets[j].name,
4273 strlen(simple_widgets[j].name))) {
4274 widgets[i] = simple_widgets[j];
4275 break;
4276 }
4277 }
4278
4279 if (j >= ARRAY_SIZE(simple_widgets)) {
4280 dev_err(card->dev,
4281 "ASoC: DAPM widget '%s' is not supported\n",
4282 template);
4283 return -EINVAL;
4284 }
4285
4286 ret = of_property_read_string_index(np, propname,
4287 (2 * i) + 1,
4288 &wname);
4289 if (ret) {
4290 dev_err(card->dev,
4291 "ASoC: Property '%s' index %d read error:%d\n",
4292 propname, (2 * i) + 1, ret);
4293 return -EINVAL;
4294 }
4295
4296 widgets[i].name = wname;
4297 }
4298
4299 card->dapm_widgets = widgets;
4300 card->num_dapm_widgets = num_widgets;
4301
4302 return 0;
4303}
4304EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4305
Xiubo Li89c67852014-02-14 09:34:35 +08004306int snd_soc_of_parse_tdm_slot(struct device_node *np,
4307 unsigned int *slots,
4308 unsigned int *slot_width)
4309{
4310 u32 val;
4311 int ret;
4312
4313 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4314 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4315 if (ret)
4316 return ret;
4317
4318 if (slots)
4319 *slots = val;
4320 }
4321
4322 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4323 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4324 if (ret)
4325 return ret;
4326
4327 if (slot_width)
4328 *slot_width = val;
4329 }
4330
4331 return 0;
4332}
4333EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4334
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004335int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4336 const char *propname)
4337{
4338 struct device_node *np = card->dev->of_node;
4339 int num_routes;
4340 struct snd_soc_dapm_route *routes;
4341 int i, ret;
4342
4343 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004344 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004345 dev_err(card->dev,
4346 "ASoC: Property '%s' does not exist or its length is not even\n",
4347 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004348 return -EINVAL;
4349 }
4350 num_routes /= 2;
4351 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004352 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004353 propname);
4354 return -EINVAL;
4355 }
4356
4357 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4358 GFP_KERNEL);
4359 if (!routes) {
4360 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004361 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004362 return -EINVAL;
4363 }
4364
4365 for (i = 0; i < num_routes; i++) {
4366 ret = of_property_read_string_index(np, propname,
4367 2 * i, &routes[i].sink);
4368 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004369 dev_err(card->dev,
4370 "ASoC: Property '%s' index %d could not be read: %d\n",
4371 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004372 return -EINVAL;
4373 }
4374 ret = of_property_read_string_index(np, propname,
4375 (2 * i) + 1, &routes[i].source);
4376 if (ret) {
4377 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004378 "ASoC: Property '%s' index %d could not be read: %d\n",
4379 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004380 return -EINVAL;
4381 }
4382 }
4383
4384 card->num_dapm_routes = num_routes;
4385 card->dapm_routes = routes;
4386
4387 return 0;
4388}
4389EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4390
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004391unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4392 const char *prefix)
4393{
4394 int ret, i;
4395 char prop[128];
4396 unsigned int format = 0;
4397 int bit, frame;
4398 const char *str;
4399 struct {
4400 char *name;
4401 unsigned int val;
4402 } of_fmt_table[] = {
4403 { "i2s", SND_SOC_DAIFMT_I2S },
4404 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4405 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4406 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4407 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4408 { "ac97", SND_SOC_DAIFMT_AC97 },
4409 { "pdm", SND_SOC_DAIFMT_PDM},
4410 { "msb", SND_SOC_DAIFMT_MSB },
4411 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004412 };
4413
4414 if (!prefix)
4415 prefix = "";
4416
4417 /*
4418 * check "[prefix]format = xxx"
4419 * SND_SOC_DAIFMT_FORMAT_MASK area
4420 */
4421 snprintf(prop, sizeof(prop), "%sformat", prefix);
4422 ret = of_property_read_string(np, prop, &str);
4423 if (ret == 0) {
4424 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4425 if (strcmp(str, of_fmt_table[i].name) == 0) {
4426 format |= of_fmt_table[i].val;
4427 break;
4428 }
4429 }
4430 }
4431
4432 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004433 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004434 * SND_SOC_DAIFMT_CLOCK_MASK area
4435 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004436 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4437 if (of_get_property(np, prop, NULL))
4438 format |= SND_SOC_DAIFMT_CONT;
4439 else
4440 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004441
4442 /*
4443 * check "[prefix]bitclock-inversion"
4444 * check "[prefix]frame-inversion"
4445 * SND_SOC_DAIFMT_INV_MASK area
4446 */
4447 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4448 bit = !!of_get_property(np, prop, NULL);
4449
4450 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4451 frame = !!of_get_property(np, prop, NULL);
4452
4453 switch ((bit << 4) + frame) {
4454 case 0x11:
4455 format |= SND_SOC_DAIFMT_IB_IF;
4456 break;
4457 case 0x10:
4458 format |= SND_SOC_DAIFMT_IB_NF;
4459 break;
4460 case 0x01:
4461 format |= SND_SOC_DAIFMT_NB_IF;
4462 break;
4463 default:
4464 /* SND_SOC_DAIFMT_NB_NF is default */
4465 break;
4466 }
4467
4468 /*
4469 * check "[prefix]bitclock-master"
4470 * check "[prefix]frame-master"
4471 * SND_SOC_DAIFMT_MASTER_MASK area
4472 */
4473 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4474 bit = !!of_get_property(np, prop, NULL);
4475
4476 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4477 frame = !!of_get_property(np, prop, NULL);
4478
4479 switch ((bit << 4) + frame) {
4480 case 0x11:
4481 format |= SND_SOC_DAIFMT_CBM_CFM;
4482 break;
4483 case 0x10:
4484 format |= SND_SOC_DAIFMT_CBM_CFS;
4485 break;
4486 case 0x01:
4487 format |= SND_SOC_DAIFMT_CBS_CFM;
4488 break;
4489 default:
4490 format |= SND_SOC_DAIFMT_CBS_CFS;
4491 break;
4492 }
4493
4494 return format;
4495}
4496EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4497
Kuninori Morimotocb470082013-09-10 17:39:56 -07004498int snd_soc_of_get_dai_name(struct device_node *of_node,
4499 const char **dai_name)
4500{
4501 struct snd_soc_component *pos;
4502 struct of_phandle_args args;
4503 int ret;
4504
4505 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4506 "#sound-dai-cells", 0, &args);
4507 if (ret)
4508 return ret;
4509
4510 ret = -EPROBE_DEFER;
4511
4512 mutex_lock(&client_mutex);
4513 list_for_each_entry(pos, &component_list, list) {
4514 if (pos->dev->of_node != args.np)
4515 continue;
4516
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004517 if (pos->driver->of_xlate_dai_name) {
4518 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4519 } else {
4520 int id = -1;
4521
4522 switch (args.args_count) {
4523 case 0:
4524 id = 0; /* same as dai_drv[0] */
4525 break;
4526 case 1:
4527 id = args.args[0];
4528 break;
4529 default:
4530 /* not supported */
4531 break;
4532 }
4533
4534 if (id < 0 || id >= pos->num_dai) {
4535 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004536 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004537 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004538
4539 ret = 0;
4540
4541 *dai_name = pos->dai_drv[id].name;
4542 if (!*dai_name)
4543 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004544 }
4545
Kuninori Morimotocb470082013-09-10 17:39:56 -07004546 break;
4547 }
4548 mutex_unlock(&client_mutex);
4549
4550 of_node_put(args.np);
4551
4552 return ret;
4553}
4554EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4555
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004556static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004557{
Mark Brown384c89e2008-12-03 17:34:03 +00004558#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004559 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4560 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004561 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004562 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004563 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004564
Mark Brown8a9dab12011-01-10 22:25:21 +00004565 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004566 &codec_list_fops))
4567 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4568
Mark Brown8a9dab12011-01-10 22:25:21 +00004569 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004570 &dai_list_fops))
4571 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004572
Mark Brown8a9dab12011-01-10 22:25:21 +00004573 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004574 &platform_list_fops))
4575 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004576#endif
4577
Mark Brownfb257892011-04-28 10:57:54 +01004578 snd_soc_util_init();
4579
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004580 return platform_driver_register(&soc_driver);
4581}
Mark Brown4abe8e12010-10-12 17:41:03 +01004582module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004583
Mark Brown7d8c16a2008-11-30 22:11:24 +00004584static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004585{
Mark Brownfb257892011-04-28 10:57:54 +01004586 snd_soc_util_exit();
4587
Mark Brown384c89e2008-12-03 17:34:03 +00004588#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004589 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004590#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004591 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004592}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004593module_exit(snd_soc_exit);
4594
4595/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004596MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004597MODULE_DESCRIPTION("ALSA SoC Core");
4598MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004599MODULE_ALIAS("platform:soc-audio");