blob: a9ce63f634b2c2d5aa1d91aa81d65a8b2a6d31ad [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;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200659 if (codec->component.regmap)
660 regcache_mark_dirty(codec->component.regmap);
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
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100850static struct snd_soc_codec *soc_find_codec(const struct device_node *codec_of_node,
851 const char *codec_name)
852{
853 struct snd_soc_codec *codec;
854
855 list_for_each_entry(codec, &codec_list, list) {
856 if (codec_of_node) {
857 if (codec->dev->of_node != codec_of_node)
858 continue;
859 } else {
860 if (strcmp(codec->name, codec_name))
861 continue;
862 }
863
864 return codec;
865 }
866
867 return NULL;
868}
869
870static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
871 const char *codec_dai_name)
872{
873 struct snd_soc_dai *codec_dai;
874
875 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
876 if (!strcmp(codec_dai->name, codec_dai_name)) {
877 return codec_dai;
878 }
879 }
880
881 return NULL;
882}
883
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000884static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200885{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000886 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
887 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100888 struct snd_soc_component *component;
Mark Brown435c5e22008-12-04 15:32:53 +0000889 struct snd_soc_platform *platform;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100890 struct snd_soc_dai *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100891 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200892
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000893 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000894
Mark Brownb19e6e72012-03-14 21:18:39 +0000895 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100896 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600897 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100898 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600899 continue;
900 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100901 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600902 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100903 list_for_each_entry(cpu_dai, &component->dai_list, list) {
904 if (dai_link->cpu_dai_name &&
905 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
906 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700907
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100908 rtd->cpu_dai = cpu_dai;
909 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000910 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000911
912 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000913 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000914 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000915 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000916 }
917
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100918 /* Find CODEC from registered list */
919 rtd->codec = soc_find_codec(dai_link->codec_of_node,
920 dai_link->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000921 if (!rtd->codec) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000922 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
Mark Brownb19e6e72012-03-14 21:18:39 +0000923 dai_link->codec_name);
924 return -EPROBE_DEFER;
925 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100926
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100927 /* Find CODEC DAI from registered list */
928 rtd->codec_dai = soc_find_codec_dai(rtd->codec,
929 dai_link->codec_dai_name);
930 if (!rtd->codec_dai) {
931 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
932 dai_link->codec_dai_name);
933 return -EPROBE_DEFER;
934 }
935
Mark Brown848dd8b2011-04-27 18:16:32 +0100936 /* if there's no platform we match on the empty platform */
937 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700938 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100939 platform_name = "snd-soc-dummy";
940
Mark Brownb19e6e72012-03-14 21:18:39 +0000941 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000942 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700943 if (dai_link->platform_of_node) {
944 if (platform->dev->of_node !=
945 dai_link->platform_of_node)
946 continue;
947 } else {
948 if (strcmp(platform->name, platform_name))
949 continue;
950 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700951
952 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000953 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000954 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000955 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000956 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000957 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000958 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000959
960 card->num_rtd++;
961
962 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000963}
964
Stephen Warrend12cd192012-06-08 12:34:22 -0600965static int soc_remove_platform(struct snd_soc_platform *platform)
966{
967 int ret;
968
969 if (platform->driver->remove) {
970 ret = platform->driver->remove(platform);
971 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000972 dev_err(platform->dev, "ASoC: failed to remove %d\n",
973 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -0600974 }
975
976 /* Make sure all DAPM widgets are freed */
977 snd_soc_dapm_free(&platform->dapm);
978
979 soc_cleanup_platform_debugfs(platform);
980 platform->probed = 0;
981 list_del(&platform->card_list);
982 module_put(platform->dev->driver->owner);
983
984 return 0;
985}
986
Jarkko Nikula589c3562010-12-06 16:27:07 +0200987static void soc_remove_codec(struct snd_soc_codec *codec)
988{
989 int err;
990
991 if (codec->driver->remove) {
992 err = codec->driver->remove(codec);
993 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000994 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200995 }
996
997 /* Make sure all DAPM widgets are freed */
998 snd_soc_dapm_free(&codec->dapm);
999
1000 soc_cleanup_codec_debugfs(codec);
1001 codec->probed = 0;
1002 list_del(&codec->card_list);
1003 module_put(codec->dev->driver->owner);
1004}
1005
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001006static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1007{
1008 int err;
1009
1010 if (codec_dai && codec_dai->probed &&
1011 codec_dai->driver->remove_order == order) {
1012 if (codec_dai->driver->remove) {
1013 err = codec_dai->driver->remove(codec_dai);
1014 if (err < 0)
1015 dev_err(codec_dai->dev,
1016 "ASoC: failed to remove %s: %d\n",
1017 codec_dai->name, err);
1018 }
1019 codec_dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001020 }
1021}
1022
Stephen Warren62ae68f2012-06-08 12:34:23 -06001023static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001024{
1025 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001026 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1027 int err;
1028
1029 /* unregister the rtd device */
1030 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001031 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1032 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1033 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001034 rtd->dev_registered = 0;
1035 }
1036
1037 /* remove the CODEC DAI */
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001038 soc_remove_codec_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001039
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001040 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001041 if (cpu_dai && cpu_dai->probed &&
1042 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001043 if (cpu_dai->driver->remove) {
1044 err = cpu_dai->driver->remove(cpu_dai);
1045 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001046 dev_err(cpu_dai->dev,
1047 "ASoC: failed to remove %s: %d\n",
1048 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001049 }
1050 cpu_dai->probed = 0;
Stephen Warrena9db7db2012-06-08 12:34:20 -06001051
Stephen Warren18d75642012-06-08 12:34:21 -06001052 if (!cpu_dai->codec) {
1053 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001054 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -06001055 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001056 }
1057}
1058
Stephen Warren62ae68f2012-06-08 12:34:23 -06001059static void soc_remove_link_components(struct snd_soc_card *card, int num,
1060 int order)
1061{
1062 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1063 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1064 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1065 struct snd_soc_platform *platform = rtd->platform;
1066 struct snd_soc_codec *codec;
1067
1068 /* remove the platform */
1069 if (platform && platform->probed &&
1070 platform->driver->remove_order == order) {
1071 soc_remove_platform(platform);
1072 }
1073
1074 /* remove the CODEC-side CODEC */
1075 if (codec_dai) {
1076 codec = codec_dai->codec;
1077 if (codec && codec->probed &&
1078 codec->driver->remove_order == order)
1079 soc_remove_codec(codec);
1080 }
1081
1082 /* remove any CPU-side CODEC */
1083 if (cpu_dai) {
1084 codec = cpu_dai->codec;
1085 if (codec && codec->probed &&
1086 codec->driver->remove_order == order)
1087 soc_remove_codec(codec);
1088 }
1089}
1090
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001091static void soc_remove_dai_links(struct snd_soc_card *card)
1092{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001093 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001094
Liam Girdwood0168bf02011-06-07 16:08:05 +01001095 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1096 order++) {
1097 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001098 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001099 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001100
1101 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1102 order++) {
1103 for (dai = 0; dai < card->num_rtd; dai++)
1104 soc_remove_link_components(card, dai, order);
1105 }
1106
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001107 card->num_rtd = 0;
1108}
1109
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001110static void soc_set_name_prefix(struct snd_soc_card *card,
1111 struct snd_soc_codec *codec)
1112{
1113 int i;
1114
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001115 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001116 return;
1117
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001118 for (i = 0; i < card->num_configs; i++) {
1119 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001120 if (map->of_node && codec->dev->of_node != map->of_node)
1121 continue;
1122 if (map->dev_name && strcmp(codec->name, map->dev_name))
1123 continue;
1124 codec->name_prefix = map->name_prefix;
1125 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001126 }
1127}
1128
Jarkko Nikula589c3562010-12-06 16:27:07 +02001129static int soc_probe_codec(struct snd_soc_card *card,
1130 struct snd_soc_codec *codec)
1131{
1132 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001133 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001134 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001135
1136 codec->card = card;
1137 codec->dapm.card = card;
1138 soc_set_name_prefix(card, codec);
1139
Jarkko Nikula70d29332011-01-27 16:24:22 +02001140 if (!try_module_get(codec->dev->driver->owner))
1141 return -ENODEV;
1142
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001143 soc_init_codec_debugfs(codec);
1144
Nariman Poushinb318ad52014-04-01 13:59:33 +01001145 if (driver->dapm_widgets) {
1146 ret = snd_soc_dapm_new_controls(&codec->dapm,
1147 driver->dapm_widgets,
1148 driver->num_dapm_widgets);
1149
1150 if (ret != 0) {
1151 dev_err(codec->dev,
1152 "Failed to create new controls %d\n", ret);
1153 goto err_probe;
1154 }
1155 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001156
Mark Brown888df392012-02-16 19:37:51 -08001157 /* Create DAPM widgets for each DAI stream */
Nariman Poushin261edc72014-03-31 15:47:12 +01001158 list_for_each_entry(dai, &codec->component.dai_list, list) {
1159 ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1160
1161 if (ret != 0) {
1162 dev_err(codec->dev,
1163 "Failed to create DAI widgets %d\n", ret);
1164 goto err_probe;
1165 }
1166 }
Mark Brown888df392012-02-16 19:37:51 -08001167
Mark Brown33c5f962011-08-22 18:40:30 +01001168 codec->dapm.idle_bias_off = driver->idle_bias_off;
1169
Mark Brown89b95ac02011-03-07 16:38:44 +00001170 if (driver->probe) {
1171 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001172 if (ret < 0) {
1173 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001174 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001175 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001176 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001177 WARN(codec->dapm.idle_bias_off &&
1178 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001179 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1180 codec->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001181 }
1182
Mark Brownb7af1da2011-04-07 19:18:44 +09001183 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001184 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001185 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001186 if (driver->dapm_routes)
1187 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1188 driver->num_dapm_routes);
1189
Jarkko Nikula589c3562010-12-06 16:27:07 +02001190 /* mark codec as probed and add to card codec list */
1191 codec->probed = 1;
1192 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001193 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001194
Jarkko Nikula70d29332011-01-27 16:24:22 +02001195 return 0;
1196
1197err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001198 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001199 module_put(codec->dev->driver->owner);
1200
Jarkko Nikula589c3562010-12-06 16:27:07 +02001201 return ret;
1202}
1203
Liam Girdwood956245e2011-07-01 16:54:08 +01001204static int soc_probe_platform(struct snd_soc_card *card,
1205 struct snd_soc_platform *platform)
1206{
1207 int ret = 0;
1208 const struct snd_soc_platform_driver *driver = platform->driver;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001209 struct snd_soc_component *component;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001210 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001211
1212 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001213 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001214
1215 if (!try_module_get(platform->dev->driver->owner))
1216 return -ENODEV;
1217
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001218 soc_init_platform_debugfs(platform);
1219
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001220 if (driver->dapm_widgets)
1221 snd_soc_dapm_new_controls(&platform->dapm,
1222 driver->dapm_widgets, driver->num_dapm_widgets);
1223
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001224 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001225 list_for_each_entry(component, &component_list, list) {
1226 if (component->dev != platform->dev)
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001227 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001228 list_for_each_entry(dai, &component->dai_list, list)
1229 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001230 }
1231
Stephen Warren3fec6b62012-04-05 12:28:01 -06001232 platform->dapm.idle_bias_off = 1;
1233
Liam Girdwood956245e2011-07-01 16:54:08 +01001234 if (driver->probe) {
1235 ret = driver->probe(platform);
1236 if (ret < 0) {
1237 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001238 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001239 goto err_probe;
1240 }
1241 }
1242
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001243 if (driver->controls)
1244 snd_soc_add_platform_controls(platform, driver->controls,
1245 driver->num_controls);
1246 if (driver->dapm_routes)
1247 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1248 driver->num_dapm_routes);
1249
Liam Girdwood956245e2011-07-01 16:54:08 +01001250 /* mark platform as probed and add to card platform list */
1251 platform->probed = 1;
1252 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001253 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001254
1255 return 0;
1256
1257err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001258 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001259 module_put(platform->dev->driver->owner);
1260
1261 return ret;
1262}
1263
Mark Brown36ae1a92012-01-06 17:12:45 -08001264static void rtd_release(struct device *dev)
1265{
1266 kfree(dev);
1267}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001268
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001269static int soc_aux_dev_init(struct snd_soc_card *card,
1270 struct snd_soc_codec *codec,
1271 int num)
1272{
1273 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1274 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001275 int ret;
1276
1277 rtd->card = card;
1278
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001279 /* do machine specific initialization */
1280 if (aux_dev->init) {
1281 ret = aux_dev->init(&codec->dapm);
1282 if (ret < 0)
1283 return ret;
1284 }
1285
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001286 rtd->codec = codec;
1287
1288 return 0;
1289}
1290
1291static int soc_dai_link_init(struct snd_soc_card *card,
1292 struct snd_soc_codec *codec,
1293 int num)
1294{
1295 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1296 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001297 int ret;
1298
1299 rtd->card = card;
1300
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001301 /* do machine specific initialization */
1302 if (dai_link->init) {
1303 ret = dai_link->init(rtd);
1304 if (ret < 0)
1305 return ret;
1306 }
1307
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001308 rtd->codec = codec;
1309
1310 return 0;
1311}
1312
Jarkko Nikula589c3562010-12-06 16:27:07 +02001313static int soc_post_component_init(struct snd_soc_card *card,
1314 struct snd_soc_codec *codec,
1315 int num, int dailess)
1316{
1317 struct snd_soc_dai_link *dai_link = NULL;
1318 struct snd_soc_aux_dev *aux_dev = NULL;
1319 struct snd_soc_pcm_runtime *rtd;
Lars-Peter Clausen6479f15ad2014-03-12 15:27:40 +01001320 const char *name;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001321 int ret = 0;
1322
1323 if (!dailess) {
1324 dai_link = &card->dai_link[num];
1325 rtd = &card->rtd[num];
1326 name = dai_link->name;
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001327 ret = soc_dai_link_init(card, codec, num);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001328 } else {
1329 aux_dev = &card->aux_dev[num];
1330 rtd = &card->rtd_aux[num];
1331 name = aux_dev->name;
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001332 ret = soc_aux_dev_init(card, codec, num);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001333 }
1334
Jarkko Nikula589c3562010-12-06 16:27:07 +02001335 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001336 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001337 return ret;
1338 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001339
Jarkko Nikula589c3562010-12-06 16:27:07 +02001340 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001341 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1342 if (!rtd->dev)
1343 return -ENOMEM;
1344 device_initialize(rtd->dev);
1345 rtd->dev->parent = card->dev;
1346 rtd->dev->release = rtd_release;
1347 rtd->dev->init_name = name;
1348 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001349 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001350 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1351 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1352 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1353 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001354 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001355 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001356 /* calling put_device() here to free the rtd->dev */
1357 put_device(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001358 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001359 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001360 return ret;
1361 }
1362 rtd->dev_registered = 1;
1363
1364 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001365 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001366 if (ret < 0)
1367 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001368 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001369
1370 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001371 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001372 if (ret < 0)
1373 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001374 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001375
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001376#ifdef CONFIG_DEBUG_FS
1377 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001378 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001379 goto out;
1380
1381 ret = soc_dpcm_debugfs_add(rtd);
1382 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001383 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001384
1385out:
1386#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001387 return 0;
1388}
1389
Stephen Warren62ae68f2012-06-08 12:34:23 -06001390static int soc_probe_link_components(struct snd_soc_card *card, int num,
1391 int order)
1392{
1393 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1394 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1395 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1396 struct snd_soc_platform *platform = rtd->platform;
1397 int ret;
1398
1399 /* probe the CPU-side component, if it is a CODEC */
1400 if (cpu_dai->codec &&
1401 !cpu_dai->codec->probed &&
1402 cpu_dai->codec->driver->probe_order == order) {
1403 ret = soc_probe_codec(card, cpu_dai->codec);
1404 if (ret < 0)
1405 return ret;
1406 }
1407
1408 /* probe the CODEC-side component */
1409 if (!codec_dai->codec->probed &&
1410 codec_dai->codec->driver->probe_order == order) {
1411 ret = soc_probe_codec(card, codec_dai->codec);
1412 if (ret < 0)
1413 return ret;
1414 }
1415
1416 /* probe the platform */
1417 if (!platform->probed &&
1418 platform->driver->probe_order == order) {
1419 ret = soc_probe_platform(card, platform);
1420 if (ret < 0)
1421 return ret;
1422 }
1423
1424 return 0;
1425}
1426
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001427static int soc_probe_codec_dai(struct snd_soc_card *card,
1428 struct snd_soc_dai *codec_dai,
1429 int order)
1430{
1431 int ret;
1432
1433 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1434 if (codec_dai->driver->probe) {
1435 ret = codec_dai->driver->probe(codec_dai);
1436 if (ret < 0) {
1437 dev_err(codec_dai->dev,
1438 "ASoC: failed to probe CODEC DAI %s: %d\n",
1439 codec_dai->name, ret);
1440 return ret;
1441 }
1442 }
1443
1444 /* mark codec_dai as probed and add to card dai list */
1445 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001446 }
1447
1448 return 0;
1449}
1450
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001451static int soc_link_dai_widgets(struct snd_soc_card *card,
1452 struct snd_soc_dai_link *dai_link,
1453 struct snd_soc_dai *cpu_dai,
1454 struct snd_soc_dai *codec_dai)
1455{
1456 struct snd_soc_dapm_widget *play_w, *capture_w;
1457 int ret;
1458
1459 /* link the DAI widgets */
1460 play_w = codec_dai->playback_widget;
1461 capture_w = cpu_dai->capture_widget;
1462 if (play_w && capture_w) {
1463 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1464 capture_w, play_w);
1465 if (ret != 0) {
1466 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1467 play_w->name, capture_w->name, ret);
1468 return ret;
1469 }
1470 }
1471
1472 play_w = cpu_dai->playback_widget;
1473 capture_w = codec_dai->capture_widget;
1474 if (play_w && capture_w) {
1475 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1476 capture_w, play_w);
1477 if (ret != 0) {
1478 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1479 play_w->name, capture_w->name, ret);
1480 return ret;
1481 }
1482 }
1483
1484 return 0;
1485}
1486
Stephen Warren62ae68f2012-06-08 12:34:23 -06001487static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001488{
1489 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1490 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1491 struct snd_soc_codec *codec = rtd->codec;
1492 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001493 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1494 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001495 int ret;
1496
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001497 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001498 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001499
1500 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001501 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001502 codec_dai->card = card;
1503 cpu_dai->card = card;
1504
1505 /* set default power off timeout */
1506 rtd->pmdown_time = pmdown_time;
1507
1508 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001509 if (!cpu_dai->probed &&
1510 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001511 if (!cpu_dai->codec) {
1512 cpu_dai->dapm.card = card;
1513 if (!try_module_get(cpu_dai->dev->driver->owner))
1514 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001515
Stephen Warren18d75642012-06-08 12:34:21 -06001516 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001517 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001518
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001519 if (cpu_dai->driver->probe) {
1520 ret = cpu_dai->driver->probe(cpu_dai);
1521 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001522 dev_err(cpu_dai->dev,
1523 "ASoC: failed to probe CPU DAI %s: %d\n",
1524 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001525 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001526 return ret;
1527 }
1528 }
1529 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001530 }
1531
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001532 /* probe the CODEC DAI */
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001533 ret = soc_probe_codec_dai(card, codec_dai, order);
1534 if (ret)
1535 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001536
Liam Girdwood0168bf02011-06-07 16:08:05 +01001537 /* complete DAI probe during last probe */
1538 if (order != SND_SOC_COMP_ORDER_LAST)
1539 return 0;
1540
Jarkko Nikula589c3562010-12-06 16:27:07 +02001541 ret = soc_post_component_init(card, codec, num, 0);
1542 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001543 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001544
Mark Brown36ae1a92012-01-06 17:12:45 -08001545 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001546 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001547 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1548 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001549
Namarta Kohli1245b702012-08-16 17:10:41 +05301550 if (cpu_dai->driver->compress_dai) {
1551 /*create compress_device"*/
1552 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001553 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001554 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301555 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001556 return ret;
1557 }
1558 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301559
1560 if (!dai_link->params) {
1561 /* create the pcm */
1562 ret = soc_new_pcm(rtd, num);
1563 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001564 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301565 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001566 return ret;
1567 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301568 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001569 INIT_DELAYED_WORK(&rtd->delayed_work,
1570 codec2codec_close_delayed_work);
1571
Namarta Kohli1245b702012-08-16 17:10:41 +05301572 /* link the DAI widgets */
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001573 ret = soc_link_dai_widgets(card, dai_link,
1574 cpu_dai, codec_dai);
1575 if (ret)
1576 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001577 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001578 }
1579
1580 /* add platform data for AC97 devices */
1581 if (rtd->codec_dai->driver->ac97_control)
1582 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1583
1584 return 0;
1585}
1586
1587#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001588static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1589 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001590{
1591 int ret;
1592
1593 /* Only instantiate AC97 if not already done by the adaptor
1594 * for the generic AC97 subsystem.
1595 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001596 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001597 /*
1598 * It is possible that the AC97 device is already registered to
1599 * the device subsystem. This happens when the device is created
1600 * via snd_ac97_mixer(). Currently only SoC codec that does so
1601 * is the generic AC97 glue but others migh emerge.
1602 *
1603 * In those cases we don't try to register the device again.
1604 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001605 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001606 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001607
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001608 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001609 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001610 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001611 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001612 return ret;
1613 }
1614
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001615 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001616 }
1617 return 0;
1618}
1619
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001620static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1621{
1622 return soc_register_ac97_codec(rtd->codec, rtd->codec_dai);
1623}
1624
1625static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001626{
1627 if (codec->ac97_registered) {
1628 soc_ac97_dev_unregister(codec);
1629 codec->ac97_registered = 0;
1630 }
1631}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001632
1633static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1634{
1635 soc_unregister_ac97_codec(rtd->codec);
1636}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001637#endif
1638
Lars-Peter Clausen6b0a0b32014-05-04 19:18:30 +02001639static struct snd_soc_codec *soc_find_matching_codec(struct snd_soc_card *card,
1640 int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001641{
1642 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1643 struct snd_soc_codec *codec;
1644
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001645 /* find CODEC from registered CODECs */
Mark Brownb19e6e72012-03-14 21:18:39 +00001646 list_for_each_entry(codec, &codec_list, list) {
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001647 if (aux_dev->codec_of_node &&
1648 (codec->dev->of_node != aux_dev->codec_of_node))
1649 continue;
1650 if (aux_dev->codec_name && strcmp(codec->name, aux_dev->codec_name))
1651 continue;
1652 return codec;
Mark Brownb19e6e72012-03-14 21:18:39 +00001653 }
1654
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001655 return NULL;
1656}
Mark Brownfb099cb2012-08-09 18:44:37 +01001657
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001658static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1659{
1660 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1661 const char *codecname = aux_dev->codec_name;
1662 struct snd_soc_codec *codec = soc_find_matching_codec(card, num);
1663
1664 if (codec)
1665 return 0;
1666 if (aux_dev->codec_of_node)
1667 codecname = of_node_full_name(aux_dev->codec_of_node);
1668
1669 dev_err(card->dev, "ASoC: %s not registered\n", codecname);
Mark Brownb19e6e72012-03-14 21:18:39 +00001670 return -EPROBE_DEFER;
1671}
1672
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001673static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1674{
1675 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001676 const char *codecname = aux_dev->codec_name;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001677 int ret = -ENODEV;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001678 struct snd_soc_codec *codec = soc_find_matching_codec(card, num);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001679
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001680 if (!codec) {
1681 if (aux_dev->codec_of_node)
1682 codecname = of_node_full_name(aux_dev->codec_of_node);
1683
1684 /* codec not found */
1685 dev_err(card->dev, "ASoC: codec %s not found", codecname);
1686 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001687 }
1688
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001689 if (codec->probed) {
1690 dev_err(codec->dev, "ASoC: codec already probed");
1691 return -EBUSY;
1692 }
1693
Jarkko Nikula589c3562010-12-06 16:27:07 +02001694 ret = soc_probe_codec(card, codec);
1695 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001696 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001697
Jarkko Nikula589c3562010-12-06 16:27:07 +02001698 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001699
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001700 return ret;
1701}
1702
1703static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1704{
1705 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1706 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001707
1708 /* unregister the rtd device */
1709 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001710 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001711 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001712 rtd->dev_registered = 0;
1713 }
1714
Jarkko Nikula589c3562010-12-06 16:27:07 +02001715 if (codec && codec->probed)
1716 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001717}
1718
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001719static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001720{
1721 int ret;
1722
1723 if (codec->cache_init)
1724 return 0;
1725
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001726 ret = snd_soc_cache_init(codec);
1727 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001728 dev_err(codec->dev,
1729 "ASoC: Failed to set cache compression type: %d\n",
1730 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001731 return ret;
1732 }
1733 codec->cache_init = 1;
1734 return 0;
1735}
1736
Mark Brownb19e6e72012-03-14 21:18:39 +00001737static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001738{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001739 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001740 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001741 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001742
Liam Girdwood01b9d992012-03-07 10:38:25 +00001743 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001744
Mark Brownb19e6e72012-03-14 21:18:39 +00001745 /* bind DAIs */
1746 for (i = 0; i < card->num_links; i++) {
1747 ret = soc_bind_dai_link(card, i);
1748 if (ret != 0)
1749 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001750 }
1751
Mark Brownb19e6e72012-03-14 21:18:39 +00001752 /* check aux_devs too */
1753 for (i = 0; i < card->num_aux_devs; i++) {
1754 ret = soc_check_aux_dev(card, i);
1755 if (ret != 0)
1756 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001757 }
1758
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001759 /* initialize the register cache for each available codec */
1760 list_for_each_entry(codec, &codec_list, list) {
1761 if (codec->cache_init)
1762 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001763 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001764 if (ret < 0)
1765 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001766 }
1767
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001768 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001769 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001770 card->owner, 0, &card->snd_card);
1771 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001772 dev_err(card->dev,
1773 "ASoC: can't create sound card for card %s: %d\n",
1774 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001775 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001776 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001777
Mark Browne37a4972011-03-02 18:21:57 +00001778 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1779 card->dapm.dev = card->dev;
1780 card->dapm.card = card;
1781 list_add(&card->dapm.list, &card->dapm_list);
1782
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001783#ifdef CONFIG_DEBUG_FS
1784 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1785#endif
1786
Mark Brown88ee1c62011-02-02 10:43:26 +00001787#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001788 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001789 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001790#endif
Andy Green6ed25972008-06-13 16:24:05 +01001791
Mark Brown9a841eb2011-04-12 17:51:37 -07001792 if (card->dapm_widgets)
1793 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1794 card->num_dapm_widgets);
1795
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001796 /* initialise the sound card only once */
1797 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001798 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001799 if (ret < 0)
1800 goto card_probe_error;
1801 }
1802
Stephen Warren62ae68f2012-06-08 12:34:23 -06001803 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001804 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1805 order++) {
1806 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001807 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001808 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001809 dev_err(card->dev,
1810 "ASoC: failed to instantiate card %d\n",
1811 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001812 goto probe_dai_err;
1813 }
1814 }
1815 }
1816
1817 /* probe all DAI links on this card */
1818 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1819 order++) {
1820 for (i = 0; i < card->num_links; i++) {
1821 ret = soc_probe_link_dais(card, i, order);
1822 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001823 dev_err(card->dev,
1824 "ASoC: failed to instantiate card %d\n",
1825 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001826 goto probe_dai_err;
1827 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001828 }
1829 }
1830
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001831 for (i = 0; i < card->num_aux_devs; i++) {
1832 ret = soc_probe_aux_dev(card, i);
1833 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001834 dev_err(card->dev,
1835 "ASoC: failed to add auxiliary devices %d\n",
1836 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001837 goto probe_aux_dev_err;
1838 }
1839 }
1840
Mark Brown888df392012-02-16 19:37:51 -08001841 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001842 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001843
Mark Brownb7af1da2011-04-07 19:18:44 +09001844 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001845 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001846
Mark Brownb8ad29d2011-03-02 18:35:51 +00001847 if (card->dapm_routes)
1848 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1849 card->num_dapm_routes);
1850
Mark Brown75d9ac42011-09-27 16:41:01 +01001851 for (i = 0; i < card->num_links; i++) {
1852 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001853 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001854
Mark Brownf04209a2012-04-09 16:29:21 +01001855 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001856 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001857 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001858 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001859 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001860 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001861 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001862 }
1863
1864 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001865 if (dai_fmt &&
1866 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001867 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1868 dai_fmt);
1869 if (ret != 0 && ret != -ENOTSUPP)
1870 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001871 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001872 ret);
1873 } else if (dai_fmt) {
1874 /* Flip the polarity for the "CPU" end */
1875 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1876 switch (dai_link->dai_fmt &
1877 SND_SOC_DAIFMT_MASTER_MASK) {
1878 case SND_SOC_DAIFMT_CBM_CFM:
1879 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1880 break;
1881 case SND_SOC_DAIFMT_CBM_CFS:
1882 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1883 break;
1884 case SND_SOC_DAIFMT_CBS_CFM:
1885 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1886 break;
1887 case SND_SOC_DAIFMT_CBS_CFS:
1888 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1889 break;
1890 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001891
1892 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001893 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001894 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001895 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001896 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001897 ret);
1898 }
1899 }
1900
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001901 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001902 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001903 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1904 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001905 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1906 "%s", card->driver_name ? card->driver_name : card->name);
1907 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1908 switch (card->snd_card->driver[i]) {
1909 case '_':
1910 case '-':
1911 case '\0':
1912 break;
1913 default:
1914 if (!isalnum(card->snd_card->driver[i]))
1915 card->snd_card->driver[i] = '_';
1916 break;
1917 }
1918 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001919
Mark Brown28e9ad92011-03-02 18:36:34 +00001920 if (card->late_probe) {
1921 ret = card->late_probe(card);
1922 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001923 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001924 card->name, ret);
1925 goto probe_aux_dev_err;
1926 }
1927 }
1928
Stephen Warren16332812011-11-23 12:42:04 -07001929 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001930 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001931 snd_soc_dapm_auto_nc_codec_pins(codec);
1932
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001933 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001934
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001935 ret = snd_card_register(card->snd_card);
1936 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001937 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1938 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001939 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001940 }
1941
1942#ifdef CONFIG_SND_SOC_AC97_BUS
1943 /* register any AC97 codecs */
1944 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001945 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1946 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001947 dev_err(card->dev,
1948 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001949 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001950 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001951 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001952 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001953 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001954#endif
1955
Mark Brown435c5e22008-12-04 15:32:53 +00001956 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001957 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001958 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001959
1960 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001961
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001962probe_aux_dev_err:
1963 for (i = 0; i < card->num_aux_devs; i++)
1964 soc_remove_aux_dev(card, i);
1965
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001966probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001967 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001968
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001969card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001970 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001971 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001972
1973 snd_card_free(card->snd_card);
1974
Mark Brownb19e6e72012-03-14 21:18:39 +00001975base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001976 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001977
Mark Brownb19e6e72012-03-14 21:18:39 +00001978 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001979}
1980
1981/* probes a new socdev */
1982static int soc_probe(struct platform_device *pdev)
1983{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001984 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001985
Vinod Koul70a7ca32011-01-14 19:22:48 +05301986 /*
1987 * no card, so machine driver should be registering card
1988 * we should not be here in that case so ret error
1989 */
1990 if (!card)
1991 return -EINVAL;
1992
Mark Brownfe4085e2012-03-02 13:07:41 +00001993 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001994 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001995 card->name);
1996
Mark Brown435c5e22008-12-04 15:32:53 +00001997 /* Bodge while we unpick instantiation */
1998 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001999
Mark Brown28d528c2012-08-09 18:45:23 +01002000 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002001}
2002
Vinod Koulb0e26482011-01-13 22:48:02 +05302003static int soc_cleanup_card_resources(struct snd_soc_card *card)
2004{
Vinod Koulb0e26482011-01-13 22:48:02 +05302005 int i;
2006
2007 /* make sure any delayed work runs */
2008 for (i = 0; i < card->num_rtd; i++) {
2009 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002010 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302011 }
2012
2013 /* remove auxiliary devices */
2014 for (i = 0; i < card->num_aux_devs; i++)
2015 soc_remove_aux_dev(card, i);
2016
2017 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002018 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302019
2020 soc_cleanup_card_debugfs(card);
2021
2022 /* remove the card */
2023 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002024 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302025
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02002026 snd_soc_dapm_free(&card->dapm);
2027
Vinod Koulb0e26482011-01-13 22:48:02 +05302028 snd_card_free(card->snd_card);
2029 return 0;
2030
2031}
2032
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002033/* removes a socdev */
2034static int soc_remove(struct platform_device *pdev)
2035{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002036 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002037
Mark Brownc5af3a22008-11-28 13:29:45 +00002038 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002039 return 0;
2040}
2041
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002042int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002043{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002044 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002045 int i;
Mark Brown51737472009-06-22 13:16:51 +01002046
2047 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002048 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002049
2050 /* Flush out pmdown_time work - we actually do want to run it
2051 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002052 for (i = 0; i < card->num_rtd; i++) {
2053 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002054 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002055 }
Mark Brown51737472009-06-22 13:16:51 +01002056
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002057 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002058
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002059 /* deactivate pins to sleep state */
2060 for (i = 0; i < card->num_rtd; i++) {
2061 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
2062 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
2063 pinctrl_pm_select_sleep_state(codec_dai->dev);
2064 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2065 }
2066
Mark Brown416356f2009-06-30 19:05:15 +01002067 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002068}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002069EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002070
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002071const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302072 .suspend = snd_soc_suspend,
2073 .resume = snd_soc_resume,
2074 .freeze = snd_soc_suspend,
2075 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002076 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302077 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002078};
Stephen Warrendeb26072011-04-05 19:35:30 -06002079EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002080
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002081/* ASoC platform driver */
2082static struct platform_driver soc_driver = {
2083 .driver = {
2084 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002085 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002086 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002087 },
2088 .probe = soc_probe,
2089 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002090};
2091
Mark Brown096e49d2009-07-05 15:12:22 +01002092/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002093 * snd_soc_new_ac97_codec - initailise AC97 device
2094 * @codec: audio codec
2095 * @ops: AC97 bus operations
2096 * @num: AC97 codec number
2097 *
2098 * Initialises AC97 codec resources for use by ad-hoc devices only.
2099 */
2100int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2101 struct snd_ac97_bus_ops *ops, int num)
2102{
2103 mutex_lock(&codec->mutex);
2104
2105 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2106 if (codec->ac97 == NULL) {
2107 mutex_unlock(&codec->mutex);
2108 return -ENOMEM;
2109 }
2110
2111 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2112 if (codec->ac97->bus == NULL) {
2113 kfree(codec->ac97);
2114 codec->ac97 = NULL;
2115 mutex_unlock(&codec->mutex);
2116 return -ENOMEM;
2117 }
2118
2119 codec->ac97->bus->ops = ops;
2120 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002121
2122 /*
2123 * Mark the AC97 device to be created by us. This way we ensure that the
2124 * device will be registered with the device subsystem later on.
2125 */
2126 codec->ac97_created = 1;
2127
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002128 mutex_unlock(&codec->mutex);
2129 return 0;
2130}
2131EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2132
Markus Pargmann741a5092013-08-19 17:05:55 +02002133static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2134
2135static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2136{
2137 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2138
2139 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2140
2141 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2142
2143 udelay(10);
2144
2145 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2146
2147 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2148 msleep(2);
2149}
2150
2151static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2152{
2153 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2154
2155 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2156
2157 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2158 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2159 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2160
2161 udelay(10);
2162
2163 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2164
2165 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2166 msleep(2);
2167}
2168
2169static int snd_soc_ac97_parse_pinctl(struct device *dev,
2170 struct snd_ac97_reset_cfg *cfg)
2171{
2172 struct pinctrl *p;
2173 struct pinctrl_state *state;
2174 int gpio;
2175 int ret;
2176
2177 p = devm_pinctrl_get(dev);
2178 if (IS_ERR(p)) {
2179 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002180 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002181 }
2182 cfg->pctl = p;
2183
2184 state = pinctrl_lookup_state(p, "ac97-reset");
2185 if (IS_ERR(state)) {
2186 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002187 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002188 }
2189 cfg->pstate_reset = state;
2190
2191 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2192 if (IS_ERR(state)) {
2193 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002194 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002195 }
2196 cfg->pstate_warm_reset = state;
2197
2198 state = pinctrl_lookup_state(p, "ac97-running");
2199 if (IS_ERR(state)) {
2200 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002201 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002202 }
2203 cfg->pstate_run = state;
2204
2205 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2206 if (gpio < 0) {
2207 dev_err(dev, "Can't find ac97-sync gpio\n");
2208 return gpio;
2209 }
2210 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2211 if (ret) {
2212 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2213 return ret;
2214 }
2215 cfg->gpio_sync = gpio;
2216
2217 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2218 if (gpio < 0) {
2219 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2220 return gpio;
2221 }
2222 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2223 if (ret) {
2224 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2225 return ret;
2226 }
2227 cfg->gpio_sdata = gpio;
2228
2229 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2230 if (gpio < 0) {
2231 dev_err(dev, "Can't find ac97-reset gpio\n");
2232 return gpio;
2233 }
2234 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2235 if (ret) {
2236 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2237 return ret;
2238 }
2239 cfg->gpio_reset = gpio;
2240
2241 return 0;
2242}
2243
Mark Brownb047e1c2013-06-26 12:45:59 +01002244struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002245EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002246
2247int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2248{
2249 if (ops == soc_ac97_ops)
2250 return 0;
2251
2252 if (soc_ac97_ops && ops)
2253 return -EBUSY;
2254
2255 soc_ac97_ops = ops;
2256
2257 return 0;
2258}
2259EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2260
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002261/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002262 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2263 *
2264 * This function sets the reset and warm_reset properties of ops and parses
2265 * the device node of pdev to get pinctrl states and gpio numbers to use.
2266 */
2267int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2268 struct platform_device *pdev)
2269{
2270 struct device *dev = &pdev->dev;
2271 struct snd_ac97_reset_cfg cfg;
2272 int ret;
2273
2274 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2275 if (ret)
2276 return ret;
2277
2278 ret = snd_soc_set_ac97_ops(ops);
2279 if (ret)
2280 return ret;
2281
2282 ops->warm_reset = snd_soc_ac97_warm_reset;
2283 ops->reset = snd_soc_ac97_reset;
2284
2285 snd_ac97_rst_cfg = cfg;
2286 return 0;
2287}
2288EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2289
2290/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002291 * snd_soc_free_ac97_codec - free AC97 codec device
2292 * @codec: audio codec
2293 *
2294 * Frees AC97 codec device resources.
2295 */
2296void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2297{
2298 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002299#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002300 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002301#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002302 kfree(codec->ac97->bus);
2303 kfree(codec->ac97);
2304 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002305 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002306 mutex_unlock(&codec->mutex);
2307}
2308EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2309
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002310/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002311 * snd_soc_cnew - create new control
2312 * @_template: control template
2313 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002314 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002315 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002316 *
2317 * Create a new mixer control from a template control.
2318 *
2319 * Returns 0 for success, else error.
2320 */
2321struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002322 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002323 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002324{
2325 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002326 struct snd_kcontrol *kcontrol;
2327 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002328
2329 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002330 template.index = 0;
2331
Mark Brownefb7ac32011-03-08 17:23:24 +00002332 if (!long_name)
2333 long_name = template.name;
2334
2335 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002336 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002337 if (!name)
2338 return NULL;
2339
Mark Brownefb7ac32011-03-08 17:23:24 +00002340 template.name = name;
2341 } else {
2342 template.name = long_name;
2343 }
2344
2345 kcontrol = snd_ctl_new1(&template, data);
2346
2347 kfree(name);
2348
2349 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002350}
2351EXPORT_SYMBOL_GPL(snd_soc_cnew);
2352
Liam Girdwood022658b2012-02-03 17:43:09 +00002353static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2354 const struct snd_kcontrol_new *controls, int num_controls,
2355 const char *prefix, void *data)
2356{
2357 int err, i;
2358
2359 for (i = 0; i < num_controls; i++) {
2360 const struct snd_kcontrol_new *control = &controls[i];
2361 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2362 control->name, prefix));
2363 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002364 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2365 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002366 return err;
2367 }
2368 }
2369
2370 return 0;
2371}
2372
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002373struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2374 const char *name)
2375{
2376 struct snd_card *card = soc_card->snd_card;
2377 struct snd_kcontrol *kctl;
2378
2379 if (unlikely(!name))
2380 return NULL;
2381
2382 list_for_each_entry(kctl, &card->controls, list)
2383 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2384 return kctl;
2385 return NULL;
2386}
2387EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2388
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002389/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002390 * snd_soc_add_codec_controls - add an array of controls to a codec.
2391 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002392 * duplicating this code.
2393 *
2394 * @codec: codec to add controls to
2395 * @controls: array of controls to add
2396 * @num_controls: number of elements in the array
2397 *
2398 * Return 0 for success, else error.
2399 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002400int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002401 const struct snd_kcontrol_new *controls, int num_controls)
2402{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002403 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002404
Liam Girdwood022658b2012-02-03 17:43:09 +00002405 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002406 codec->name_prefix, &codec->component);
Ian Molton3e8e1952009-01-09 00:23:21 +00002407}
Liam Girdwood022658b2012-02-03 17:43:09 +00002408EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002409
2410/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002411 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002412 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002413 *
2414 * @platform: platform to add controls to
2415 * @controls: array of controls to add
2416 * @num_controls: number of elements in the array
2417 *
2418 * Return 0 for success, else error.
2419 */
2420int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2421 const struct snd_kcontrol_new *controls, int num_controls)
2422{
2423 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002424
Liam Girdwood022658b2012-02-03 17:43:09 +00002425 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002426 NULL, &platform->component);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002427}
2428EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2429
2430/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002431 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2432 * Convenience function to add a list of controls.
2433 *
2434 * @soc_card: SoC card to add controls to
2435 * @controls: array of controls to add
2436 * @num_controls: number of elements in the array
2437 *
2438 * Return 0 for success, else error.
2439 */
2440int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2441 const struct snd_kcontrol_new *controls, int num_controls)
2442{
2443 struct snd_card *card = soc_card->snd_card;
2444
2445 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2446 NULL, soc_card);
2447}
2448EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2449
2450/**
2451 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2452 * Convienience function to add a list of controls.
2453 *
2454 * @dai: DAI to add controls to
2455 * @controls: array of controls to add
2456 * @num_controls: number of elements in the array
2457 *
2458 * Return 0 for success, else error.
2459 */
2460int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2461 const struct snd_kcontrol_new *controls, int num_controls)
2462{
2463 struct snd_card *card = dai->card->snd_card;
2464
2465 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2466 NULL, dai);
2467}
2468EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2469
2470/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002471 * snd_soc_info_enum_double - enumerated double mixer info callback
2472 * @kcontrol: mixer control
2473 * @uinfo: control element information
2474 *
2475 * Callback to provide information about a double enumerated
2476 * mixer control.
2477 *
2478 * Returns 0 for success.
2479 */
2480int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2481 struct snd_ctl_elem_info *uinfo)
2482{
2483 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2484
2485 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2486 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002487 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002488
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002489 if (uinfo->value.enumerated.item >= e->items)
2490 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002491 strlcpy(uinfo->value.enumerated.name,
2492 e->texts[uinfo->value.enumerated.item],
2493 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002494 return 0;
2495}
2496EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2497
2498/**
2499 * snd_soc_get_enum_double - enumerated double mixer get callback
2500 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002501 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002502 *
2503 * Callback to get the value of a double enumerated mixer.
2504 *
2505 * Returns 0 for success.
2506 */
2507int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2508 struct snd_ctl_elem_value *ucontrol)
2509{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002510 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002511 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002512 unsigned int val, item;
2513 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002514 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002515
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002516 ret = snd_soc_component_read(component, e->reg, &reg_val);
2517 if (ret)
2518 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002519 val = (reg_val >> e->shift_l) & e->mask;
2520 item = snd_soc_enum_val_to_item(e, val);
2521 ucontrol->value.enumerated.item[0] = item;
2522 if (e->shift_l != e->shift_r) {
2523 val = (reg_val >> e->shift_l) & e->mask;
2524 item = snd_soc_enum_val_to_item(e, val);
2525 ucontrol->value.enumerated.item[1] = item;
2526 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002527
2528 return 0;
2529}
2530EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2531
2532/**
2533 * snd_soc_put_enum_double - enumerated double mixer put callback
2534 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002535 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002536 *
2537 * Callback to set the value of a double enumerated mixer.
2538 *
2539 * Returns 0 for success.
2540 */
2541int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2542 struct snd_ctl_elem_value *ucontrol)
2543{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002544 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002545 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002546 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002547 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002548 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002549
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002550 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002551 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002552 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002553 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002554 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002555 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002556 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002557 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002558 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002559 }
2560
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002561 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002562}
2563EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2564
2565/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002566 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002567 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002568 * @reg: Register to read
2569 * @mask: Mask to use after shifting the register value
2570 * @shift: Right shift of register value
2571 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002572 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002573 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002574 * This functions reads a codec register. The register value is shifted right
2575 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2576 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002577 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002578 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002579 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002580static int snd_soc_read_signed(struct snd_soc_component *component,
2581 unsigned int reg, unsigned int mask, unsigned int shift,
2582 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002583{
Markus Pargmannf227b882014-01-16 16:02:10 +01002584 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002585 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002586
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002587 ret = snd_soc_component_read(component, reg, &val);
2588 if (ret < 0)
2589 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002590
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002591 val = (val >> shift) & mask;
2592
2593 if (!sign_bit) {
2594 *signed_val = val;
2595 return 0;
2596 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002597
2598 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002599 if (!(val & BIT(sign_bit))) {
2600 *signed_val = val;
2601 return 0;
2602 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002603
2604 ret = val;
2605
2606 /*
2607 * The register most probably does not contain a full-sized int.
2608 * Instead we have an arbitrary number of bits in a signed
2609 * representation which has to be translated into a full-sized int.
2610 * This is done by filling up all bits above the sign-bit.
2611 */
2612 ret |= ~((int)(BIT(sign_bit) - 1));
2613
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002614 *signed_val = ret;
2615
2616 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002617}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002618
2619/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002620 * snd_soc_info_volsw - single mixer info callback
2621 * @kcontrol: mixer control
2622 * @uinfo: control element information
2623 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002624 * Callback to provide information about a single mixer control, or a double
2625 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002626 *
2627 * Returns 0 for success.
2628 */
2629int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2630 struct snd_ctl_elem_info *uinfo)
2631{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002632 struct soc_mixer_control *mc =
2633 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002634 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002635
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002636 if (!mc->platform_max)
2637 mc->platform_max = mc->max;
2638 platform_max = mc->platform_max;
2639
2640 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002641 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2642 else
2643 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2644
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002645 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002646 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002647 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002648 return 0;
2649}
2650EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2651
2652/**
2653 * snd_soc_get_volsw - single mixer get callback
2654 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002655 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002656 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002657 * Callback to get the value of a single mixer control, or a double mixer
2658 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002659 *
2660 * Returns 0 for success.
2661 */
2662int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2663 struct snd_ctl_elem_value *ucontrol)
2664{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002665 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002666 struct soc_mixer_control *mc =
2667 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002668 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002669 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002670 unsigned int shift = mc->shift;
2671 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002672 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002673 int min = mc->min;
2674 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002675 unsigned int mask = (1 << fls(max)) - 1;
2676 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002677 int val;
2678 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002679
Markus Pargmannf227b882014-01-16 16:02:10 +01002680 if (sign_bit)
2681 mask = BIT(sign_bit + 1) - 1;
2682
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002683 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2684 if (ret)
2685 return ret;
2686
2687 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002688 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002689 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002690 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002691
2692 if (snd_soc_volsw_is_stereo(mc)) {
2693 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002694 ret = snd_soc_read_signed(component, reg, mask, rshift,
2695 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002696 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002697 ret = snd_soc_read_signed(component, reg2, mask, shift,
2698 sign_bit, &val);
2699 if (ret)
2700 return ret;
2701
2702 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002703 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002704 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002705 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002706 }
2707
2708 return 0;
2709}
2710EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2711
2712/**
2713 * snd_soc_put_volsw - single mixer put callback
2714 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002715 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002716 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002717 * Callback to set the value of a single mixer control, or a double mixer
2718 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002719 *
2720 * Returns 0 for success.
2721 */
2722int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2723 struct snd_ctl_elem_value *ucontrol)
2724{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002725 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002726 struct soc_mixer_control *mc =
2727 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002728 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002729 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002730 unsigned int shift = mc->shift;
2731 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002732 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002733 int min = mc->min;
2734 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002735 unsigned int mask = (1 << fls(max)) - 1;
2736 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002737 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002738 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002739 unsigned int val2 = 0;
2740 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002741
Markus Pargmannf227b882014-01-16 16:02:10 +01002742 if (sign_bit)
2743 mask = BIT(sign_bit + 1) - 1;
2744
2745 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002746 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002747 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002748 val_mask = mask << shift;
2749 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002750 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002751 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002752 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002753 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002754 if (reg == reg2) {
2755 val_mask |= mask << rshift;
2756 val |= val2 << rshift;
2757 } else {
2758 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002759 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002760 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002761 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002762 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002763 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002764 return err;
2765
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002766 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002767 err = snd_soc_component_update_bits(component, reg2, val_mask,
2768 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002769
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002770 return err;
2771}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002772EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002773
Mark Browne13ac2e2008-05-28 17:58:05 +01002774/**
Brian Austin1d99f242012-03-30 10:43:55 -05002775 * snd_soc_get_volsw_sx - single mixer get callback
2776 * @kcontrol: mixer control
2777 * @ucontrol: control element information
2778 *
2779 * Callback to get the value of a single mixer control, or a double mixer
2780 * control that spans 2 registers.
2781 *
2782 * Returns 0 for success.
2783 */
2784int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2785 struct snd_ctl_elem_value *ucontrol)
2786{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002787 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002788 struct soc_mixer_control *mc =
2789 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002790 unsigned int reg = mc->reg;
2791 unsigned int reg2 = mc->rreg;
2792 unsigned int shift = mc->shift;
2793 unsigned int rshift = mc->rshift;
2794 int max = mc->max;
2795 int min = mc->min;
2796 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002797 unsigned int val;
2798 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002799
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002800 ret = snd_soc_component_read(component, reg, &val);
2801 if (ret < 0)
2802 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002803
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002804 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2805
2806 if (snd_soc_volsw_is_stereo(mc)) {
2807 ret = snd_soc_component_read(component, reg2, &val);
2808 if (ret < 0)
2809 return ret;
2810
2811 val = ((val >> rshift) - min) & mask;
2812 ucontrol->value.integer.value[1] = val;
2813 }
Brian Austin1d99f242012-03-30 10:43:55 -05002814
2815 return 0;
2816}
2817EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2818
2819/**
2820 * snd_soc_put_volsw_sx - double mixer set callback
2821 * @kcontrol: mixer control
2822 * @uinfo: control element information
2823 *
2824 * Callback to set the value of a double mixer control that spans 2 registers.
2825 *
2826 * Returns 0 for success.
2827 */
2828int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2829 struct snd_ctl_elem_value *ucontrol)
2830{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002831 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002832 struct soc_mixer_control *mc =
2833 (struct soc_mixer_control *)kcontrol->private_value;
2834
2835 unsigned int reg = mc->reg;
2836 unsigned int reg2 = mc->rreg;
2837 unsigned int shift = mc->shift;
2838 unsigned int rshift = mc->rshift;
2839 int max = mc->max;
2840 int min = mc->min;
2841 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002842 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002843 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002844
2845 val_mask = mask << shift;
2846 val = (ucontrol->value.integer.value[0] + min) & mask;
2847 val = val << shift;
2848
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002849 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302850 if (err < 0)
2851 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002852
2853 if (snd_soc_volsw_is_stereo(mc)) {
2854 val_mask = mask << rshift;
2855 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2856 val2 = val2 << rshift;
2857
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002858 err = snd_soc_component_update_bits(component, reg2, val_mask,
2859 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002860 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002861 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002862}
2863EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2864
2865/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002866 * snd_soc_info_volsw_s8 - signed mixer info callback
2867 * @kcontrol: mixer control
2868 * @uinfo: control element information
2869 *
2870 * Callback to provide information about a signed mixer control.
2871 *
2872 * Returns 0 for success.
2873 */
2874int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2875 struct snd_ctl_elem_info *uinfo)
2876{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002877 struct soc_mixer_control *mc =
2878 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002879 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002880 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002881
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002882 if (!mc->platform_max)
2883 mc->platform_max = mc->max;
2884 platform_max = mc->platform_max;
2885
Mark Browne13ac2e2008-05-28 17:58:05 +01002886 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2887 uinfo->count = 2;
2888 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002889 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002890 return 0;
2891}
2892EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2893
2894/**
2895 * snd_soc_get_volsw_s8 - signed mixer get callback
2896 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002897 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002898 *
2899 * Callback to get the value of a signed mixer control.
2900 *
2901 * Returns 0 for success.
2902 */
2903int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2904 struct snd_ctl_elem_value *ucontrol)
2905{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002906 struct soc_mixer_control *mc =
2907 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002908 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002909 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002910 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002911 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002912 int ret;
2913
2914 ret = snd_soc_component_read(component, reg, &val);
2915 if (ret)
2916 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002917
2918 ucontrol->value.integer.value[0] =
2919 ((signed char)(val & 0xff))-min;
2920 ucontrol->value.integer.value[1] =
2921 ((signed char)((val >> 8) & 0xff))-min;
2922 return 0;
2923}
2924EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2925
2926/**
2927 * snd_soc_put_volsw_sgn - signed mixer put callback
2928 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002929 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002930 *
2931 * Callback to set the value of a signed mixer control.
2932 *
2933 * Returns 0 for success.
2934 */
2935int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2936 struct snd_ctl_elem_value *ucontrol)
2937{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002938 struct soc_mixer_control *mc =
2939 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002940 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002941 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002942 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002943 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002944
2945 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2946 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2947
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002948 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002949}
2950EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2951
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002952/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002953 * snd_soc_info_volsw_range - single mixer info callback with range.
2954 * @kcontrol: mixer control
2955 * @uinfo: control element information
2956 *
2957 * Callback to provide information, within a range, about a single
2958 * mixer control.
2959 *
2960 * returns 0 for success.
2961 */
2962int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2963 struct snd_ctl_elem_info *uinfo)
2964{
2965 struct soc_mixer_control *mc =
2966 (struct soc_mixer_control *)kcontrol->private_value;
2967 int platform_max;
2968 int min = mc->min;
2969
2970 if (!mc->platform_max)
2971 mc->platform_max = mc->max;
2972 platform_max = mc->platform_max;
2973
2974 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002975 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002976 uinfo->value.integer.min = 0;
2977 uinfo->value.integer.max = platform_max - min;
2978
2979 return 0;
2980}
2981EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2982
2983/**
2984 * snd_soc_put_volsw_range - single mixer put value callback with range.
2985 * @kcontrol: mixer control
2986 * @ucontrol: control element information
2987 *
2988 * Callback to set the value, within a range, for a single mixer control.
2989 *
2990 * Returns 0 for success.
2991 */
2992int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2993 struct snd_ctl_elem_value *ucontrol)
2994{
2995 struct soc_mixer_control *mc =
2996 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002997 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002998 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002999 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003000 unsigned int shift = mc->shift;
3001 int min = mc->min;
3002 int max = mc->max;
3003 unsigned int mask = (1 << fls(max)) - 1;
3004 unsigned int invert = mc->invert;
3005 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003006 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003007
3008 val = ((ucontrol->value.integer.value[0] + min) & mask);
3009 if (invert)
3010 val = max - val;
3011 val_mask = mask << shift;
3012 val = val << shift;
3013
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003014 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09003015 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00003016 return ret;
3017
3018 if (snd_soc_volsw_is_stereo(mc)) {
3019 val = ((ucontrol->value.integer.value[1] + min) & mask);
3020 if (invert)
3021 val = max - val;
3022 val_mask = mask << shift;
3023 val = val << shift;
3024
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003025 ret = snd_soc_component_update_bits(component, rreg, val_mask,
3026 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00003027 }
3028
3029 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003030}
3031EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3032
3033/**
3034 * snd_soc_get_volsw_range - single mixer get callback with range
3035 * @kcontrol: mixer control
3036 * @ucontrol: control element information
3037 *
3038 * Callback to get the value, within a range, of a single mixer control.
3039 *
3040 * Returns 0 for success.
3041 */
3042int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3043 struct snd_ctl_elem_value *ucontrol)
3044{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003045 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003046 struct soc_mixer_control *mc =
3047 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003048 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003049 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003050 unsigned int shift = mc->shift;
3051 int min = mc->min;
3052 int max = mc->max;
3053 unsigned int mask = (1 << fls(max)) - 1;
3054 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003055 unsigned int val;
3056 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003057
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003058 ret = snd_soc_component_read(component, reg, &val);
3059 if (ret)
3060 return ret;
3061
3062 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003063 if (invert)
3064 ucontrol->value.integer.value[0] =
3065 max - ucontrol->value.integer.value[0];
3066 ucontrol->value.integer.value[0] =
3067 ucontrol->value.integer.value[0] - min;
3068
Mark Brown9bde4f02012-12-19 16:05:00 +00003069 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003070 ret = snd_soc_component_read(component, rreg, &val);
3071 if (ret)
3072 return ret;
3073
3074 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003075 if (invert)
3076 ucontrol->value.integer.value[1] =
3077 max - ucontrol->value.integer.value[1];
3078 ucontrol->value.integer.value[1] =
3079 ucontrol->value.integer.value[1] - min;
3080 }
3081
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003082 return 0;
3083}
3084EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3085
3086/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003087 * snd_soc_limit_volume - Set new limit to an existing volume control.
3088 *
3089 * @codec: where to look for the control
3090 * @name: Name of the control
3091 * @max: new maximum limit
3092 *
3093 * Return 0 for success, else error.
3094 */
3095int snd_soc_limit_volume(struct snd_soc_codec *codec,
3096 const char *name, int max)
3097{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003098 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003099 struct snd_kcontrol *kctl;
3100 struct soc_mixer_control *mc;
3101 int found = 0;
3102 int ret = -EINVAL;
3103
3104 /* Sanity check for name and max */
3105 if (unlikely(!name || max <= 0))
3106 return -EINVAL;
3107
3108 list_for_each_entry(kctl, &card->controls, list) {
3109 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3110 found = 1;
3111 break;
3112 }
3113 }
3114 if (found) {
3115 mc = (struct soc_mixer_control *)kctl->private_value;
3116 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003117 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003118 ret = 0;
3119 }
3120 }
3121 return ret;
3122}
3123EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3124
Mark Brown71d08512011-10-10 18:31:26 +01003125int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3126 struct snd_ctl_elem_info *uinfo)
3127{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003128 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003129 struct soc_bytes *params = (void *)kcontrol->private_value;
3130
3131 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003132 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003133
3134 return 0;
3135}
3136EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3137
3138int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3139 struct snd_ctl_elem_value *ucontrol)
3140{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003141 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003142 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003143 int ret;
3144
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003145 if (component->regmap)
3146 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003147 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003148 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003149 else
3150 ret = -EINVAL;
3151
Mark Brownf831b052012-02-17 16:20:33 -08003152 /* Hide any masked bytes to ensure consistent data reporting */
3153 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003154 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003155 case 1:
3156 ucontrol->value.bytes.data[0] &= ~params->mask;
3157 break;
3158 case 2:
3159 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003160 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003161 break;
3162 case 4:
3163 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003164 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003165 break;
3166 default:
3167 return -EINVAL;
3168 }
3169 }
3170
Mark Brown71d08512011-10-10 18:31:26 +01003171 return ret;
3172}
3173EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3174
3175int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3176 struct snd_ctl_elem_value *ucontrol)
3177{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003178 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003179 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003180 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003181 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003182 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003183
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003184 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003185 return -EINVAL;
3186
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003187 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003188
Mark Brownb5a8fe42013-01-20 21:42:22 +09003189 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3190 if (!data)
3191 return -ENOMEM;
3192
Mark Brownf831b052012-02-17 16:20:33 -08003193 /*
3194 * If we've got a mask then we need to preserve the register
3195 * bits. We shouldn't modify the incoming data so take a
3196 * copy.
3197 */
3198 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003199 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003200 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003201 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003202
3203 val &= params->mask;
3204
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003205 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003206 case 1:
3207 ((u8 *)data)[0] &= ~params->mask;
3208 ((u8 *)data)[0] |= val;
3209 break;
3210 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003211 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003212 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003213 &mask, &mask);
3214 if (ret != 0)
3215 goto out;
3216
3217 ((u16 *)data)[0] &= mask;
3218
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003219 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003220 &val, &val);
3221 if (ret != 0)
3222 goto out;
3223
3224 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003225 break;
3226 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003227 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003228 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003229 &mask, &mask);
3230 if (ret != 0)
3231 goto out;
3232
3233 ((u32 *)data)[0] &= mask;
3234
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003235 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003236 &val, &val);
3237 if (ret != 0)
3238 goto out;
3239
3240 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003241 break;
3242 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003243 ret = -EINVAL;
3244 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003245 }
3246 }
3247
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003248 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003249 data, len);
3250
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003251out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003252 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003253
3254 return ret;
3255}
3256EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3257
Vinod Kould9881202014-05-02 10:58:11 +05303258int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3259 struct snd_ctl_elem_info *ucontrol)
3260{
3261 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3262
3263 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3264 ucontrol->count = params->max;
3265
3266 return 0;
3267}
3268EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3269
Omair Mohammed Abdullah7523a272014-07-15 21:34:48 +05303270int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3271 unsigned int size, unsigned int __user *tlv)
3272{
3273 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3274 unsigned int count = size < params->max ? size : params->max;
3275 int ret = -ENXIO;
3276
3277 switch (op_flag) {
3278 case SNDRV_CTL_TLV_OP_READ:
3279 if (params->get)
3280 ret = params->get(tlv, count);
3281 break;
3282 case SNDRV_CTL_TLV_OP_WRITE:
3283 if (params->put)
3284 ret = params->put(tlv, count);
3285 break;
3286 }
3287 return ret;
3288}
3289EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3290
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003291/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003292 * snd_soc_info_xr_sx - signed multi register info callback
3293 * @kcontrol: mreg control
3294 * @uinfo: control element information
3295 *
3296 * Callback to provide information of a control that can
3297 * span multiple codec registers which together
3298 * forms a single signed value in a MSB/LSB manner.
3299 *
3300 * Returns 0 for success.
3301 */
3302int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3303 struct snd_ctl_elem_info *uinfo)
3304{
3305 struct soc_mreg_control *mc =
3306 (struct soc_mreg_control *)kcontrol->private_value;
3307 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3308 uinfo->count = 1;
3309 uinfo->value.integer.min = mc->min;
3310 uinfo->value.integer.max = mc->max;
3311
3312 return 0;
3313}
3314EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3315
3316/**
3317 * snd_soc_get_xr_sx - signed multi register get callback
3318 * @kcontrol: mreg control
3319 * @ucontrol: control element information
3320 *
3321 * Callback to get the value of a control that can span
3322 * multiple codec registers which together forms a single
3323 * signed value in a MSB/LSB manner. The control supports
3324 * specifying total no of bits used to allow for bitfields
3325 * across the multiple codec registers.
3326 *
3327 * Returns 0 for success.
3328 */
3329int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3330 struct snd_ctl_elem_value *ucontrol)
3331{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003332 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003333 struct soc_mreg_control *mc =
3334 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003335 unsigned int regbase = mc->regbase;
3336 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003337 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003338 unsigned int regwmask = (1<<regwshift)-1;
3339 unsigned int invert = mc->invert;
3340 unsigned long mask = (1UL<<mc->nbits)-1;
3341 long min = mc->min;
3342 long max = mc->max;
3343 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003344 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003345 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003346 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003347
3348 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003349 ret = snd_soc_component_read(component, regbase+i, &regval);
3350 if (ret)
3351 return ret;
3352 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003353 }
3354 val &= mask;
3355 if (min < 0 && val > max)
3356 val |= ~mask;
3357 if (invert)
3358 val = max - val;
3359 ucontrol->value.integer.value[0] = val;
3360
3361 return 0;
3362}
3363EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3364
3365/**
3366 * snd_soc_put_xr_sx - signed multi register get callback
3367 * @kcontrol: mreg control
3368 * @ucontrol: control element information
3369 *
3370 * Callback to set the value of a control that can span
3371 * multiple codec registers which together forms a single
3372 * signed value in a MSB/LSB manner. The control supports
3373 * specifying total no of bits used to allow for bitfields
3374 * across the multiple codec registers.
3375 *
3376 * Returns 0 for success.
3377 */
3378int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3379 struct snd_ctl_elem_value *ucontrol)
3380{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003381 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003382 struct soc_mreg_control *mc =
3383 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003384 unsigned int regbase = mc->regbase;
3385 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003386 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003387 unsigned int regwmask = (1<<regwshift)-1;
3388 unsigned int invert = mc->invert;
3389 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003390 long max = mc->max;
3391 long val = ucontrol->value.integer.value[0];
3392 unsigned int i, regval, regmask;
3393 int err;
3394
3395 if (invert)
3396 val = max - val;
3397 val &= mask;
3398 for (i = 0; i < regcount; i++) {
3399 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3400 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003401 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003402 regmask, regval);
3403 if (err < 0)
3404 return err;
3405 }
3406
3407 return 0;
3408}
3409EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3410
3411/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003412 * snd_soc_get_strobe - strobe get callback
3413 * @kcontrol: mixer control
3414 * @ucontrol: control element information
3415 *
3416 * Callback get the value of a strobe mixer control.
3417 *
3418 * Returns 0 for success.
3419 */
3420int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3421 struct snd_ctl_elem_value *ucontrol)
3422{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003423 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003424 struct soc_mixer_control *mc =
3425 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003426 unsigned int reg = mc->reg;
3427 unsigned int shift = mc->shift;
3428 unsigned int mask = 1 << shift;
3429 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003430 unsigned int val;
3431 int ret;
3432
3433 ret = snd_soc_component_read(component, reg, &val);
3434 if (ret)
3435 return ret;
3436
3437 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003438
3439 if (shift != 0 && val != 0)
3440 val = val >> shift;
3441 ucontrol->value.enumerated.item[0] = val ^ invert;
3442
3443 return 0;
3444}
3445EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3446
3447/**
3448 * snd_soc_put_strobe - strobe put callback
3449 * @kcontrol: mixer control
3450 * @ucontrol: control element information
3451 *
3452 * Callback strobe a register bit to high then low (or the inverse)
3453 * in one pass of a single mixer enum control.
3454 *
3455 * Returns 1 for success.
3456 */
3457int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3458 struct snd_ctl_elem_value *ucontrol)
3459{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003460 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003461 struct soc_mixer_control *mc =
3462 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003463 unsigned int reg = mc->reg;
3464 unsigned int shift = mc->shift;
3465 unsigned int mask = 1 << shift;
3466 unsigned int invert = mc->invert != 0;
3467 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3468 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3469 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3470 int err;
3471
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003472 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003473 if (err < 0)
3474 return err;
3475
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003476 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003477}
3478EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3479
3480/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003481 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3482 * @dai: DAI
3483 * @clk_id: DAI specific clock ID
3484 * @freq: new clock frequency in Hz
3485 * @dir: new clock direction - input/output.
3486 *
3487 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3488 */
3489int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3490 unsigned int freq, int dir)
3491{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003492 if (dai->driver && dai->driver->ops->set_sysclk)
3493 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003494 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003495 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003496 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003497 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003498 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003499}
3500EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3501
3502/**
Mark Brownec4ee522011-03-07 20:58:11 +00003503 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3504 * @codec: CODEC
3505 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003506 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003507 * @freq: new clock frequency in Hz
3508 * @dir: new clock direction - input/output.
3509 *
3510 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3511 */
3512int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003513 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003514{
3515 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003516 return codec->driver->set_sysclk(codec, clk_id, source,
3517 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003518 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003519 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003520}
3521EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3522
3523/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003524 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3525 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003526 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003527 * @div: new clock divisor.
3528 *
3529 * Configures the clock dividers. This is used to derive the best DAI bit and
3530 * frame clocks from the system or master clock. It's best to set the DAI bit
3531 * and frame clocks as low as possible to save system power.
3532 */
3533int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3534 int div_id, int div)
3535{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003536 if (dai->driver && dai->driver->ops->set_clkdiv)
3537 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003538 else
3539 return -EINVAL;
3540}
3541EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3542
3543/**
3544 * snd_soc_dai_set_pll - configure DAI PLL.
3545 * @dai: DAI
3546 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003547 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003548 * @freq_in: PLL input clock frequency in Hz
3549 * @freq_out: requested PLL output clock frequency in Hz
3550 *
3551 * Configures and enables PLL to generate output clock based on input clock.
3552 */
Mark Brown85488032009-09-05 18:52:16 +01003553int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3554 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003555{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003556 if (dai->driver && dai->driver->ops->set_pll)
3557 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003558 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003559 else if (dai->codec && dai->codec->driver->set_pll)
3560 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3561 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003562 else
3563 return -EINVAL;
3564}
3565EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3566
Mark Brownec4ee522011-03-07 20:58:11 +00003567/*
3568 * snd_soc_codec_set_pll - configure codec PLL.
3569 * @codec: CODEC
3570 * @pll_id: DAI specific PLL ID
3571 * @source: DAI specific source for the PLL
3572 * @freq_in: PLL input clock frequency in Hz
3573 * @freq_out: requested PLL output clock frequency in Hz
3574 *
3575 * Configures and enables PLL to generate output clock based on input clock.
3576 */
3577int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3578 unsigned int freq_in, unsigned int freq_out)
3579{
3580 if (codec->driver->set_pll)
3581 return codec->driver->set_pll(codec, pll_id, source,
3582 freq_in, freq_out);
3583 else
3584 return -EINVAL;
3585}
3586EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3587
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003588/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003589 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3590 * @dai: DAI
3591 * @ratio Ratio of BCLK to Sample rate.
3592 *
3593 * Configures the DAI for a preset BCLK to sample rate ratio.
3594 */
3595int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3596{
3597 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3598 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3599 else
3600 return -EINVAL;
3601}
3602EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3603
3604/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003605 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3606 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003607 * @fmt: SND_SOC_DAIFMT_ format value.
3608 *
3609 * Configures the DAI hardware format and clocking.
3610 */
3611int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3612{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003613 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003614 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003615 if (dai->driver->ops->set_fmt == NULL)
3616 return -ENOTSUPP;
3617 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003618}
3619EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3620
3621/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003622 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003623 * @slots: Number of slots in use.
3624 * @tx_mask: bitmask representing active TX slots.
3625 * @rx_mask: bitmask representing active RX slots.
3626 *
3627 * Generates the TDM tx and rx slot default masks for DAI.
3628 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003629static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003630 unsigned int *tx_mask,
3631 unsigned int *rx_mask)
3632{
3633 if (*tx_mask || *rx_mask)
3634 return 0;
3635
3636 if (!slots)
3637 return -EINVAL;
3638
3639 *tx_mask = (1 << slots) - 1;
3640 *rx_mask = (1 << slots) - 1;
3641
3642 return 0;
3643}
3644
3645/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003646 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3647 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003648 * @tx_mask: bitmask representing active TX slots.
3649 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003650 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003651 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003652 *
3653 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3654 * specific.
3655 */
3656int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003657 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003658{
Xiubo Lie5c21512014-03-21 14:17:12 +08003659 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3660 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003661 &tx_mask, &rx_mask);
3662 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003663 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003664
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003665 if (dai->driver && dai->driver->ops->set_tdm_slot)
3666 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003667 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003668 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003669 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003670}
3671EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3672
3673/**
Barry Song472df3c2009-09-12 01:16:29 +08003674 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3675 * @dai: DAI
3676 * @tx_num: how many TX channels
3677 * @tx_slot: pointer to an array which imply the TX slot number channel
3678 * 0~num-1 uses
3679 * @rx_num: how many RX channels
3680 * @rx_slot: pointer to an array which imply the RX slot number channel
3681 * 0~num-1 uses
3682 *
3683 * configure the relationship between channel number and TDM slot number.
3684 */
3685int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3686 unsigned int tx_num, unsigned int *tx_slot,
3687 unsigned int rx_num, unsigned int *rx_slot)
3688{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003689 if (dai->driver && dai->driver->ops->set_channel_map)
3690 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003691 rx_num, rx_slot);
3692 else
3693 return -EINVAL;
3694}
3695EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3696
3697/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003698 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3699 * @dai: DAI
3700 * @tristate: tristate enable
3701 *
3702 * Tristates the DAI so that others can use it.
3703 */
3704int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3705{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003706 if (dai->driver && dai->driver->ops->set_tristate)
3707 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003708 else
3709 return -EINVAL;
3710}
3711EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3712
3713/**
3714 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3715 * @dai: DAI
3716 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003717 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003718 *
3719 * Mutes the DAI DAC.
3720 */
Mark Brownda183962013-02-06 15:44:07 +00003721int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3722 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003723{
Mark Brownda183962013-02-06 15:44:07 +00003724 if (!dai->driver)
3725 return -ENOTSUPP;
3726
3727 if (dai->driver->ops->mute_stream)
3728 return dai->driver->ops->mute_stream(dai, mute, direction);
3729 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3730 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003731 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003732 else
Mark Brown04570c62012-04-13 19:16:03 +01003733 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003734}
3735EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3736
Mark Brownc5af3a22008-11-28 13:29:45 +00003737/**
3738 * snd_soc_register_card - Register a card with the ASoC core
3739 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003740 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003741 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003742 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303743int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003744{
Mark Brownb19e6e72012-03-14 21:18:39 +00003745 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003746
Mark Brownc5af3a22008-11-28 13:29:45 +00003747 if (!card->name || !card->dev)
3748 return -EINVAL;
3749
Stephen Warren5a504962011-12-21 10:40:59 -07003750 for (i = 0; i < card->num_links; i++) {
3751 struct snd_soc_dai_link *link = &card->dai_link[i];
3752
3753 /*
3754 * Codec must be specified by 1 of name or OF node,
3755 * not both or neither.
3756 */
3757 if (!!link->codec_name == !!link->codec_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003758 dev_err(card->dev,
3759 "ASoC: Neither/both codec name/of_node are set for %s\n",
3760 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003761 return -EINVAL;
3762 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003763 /* Codec DAI name must be specified */
3764 if (!link->codec_dai_name) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003765 dev_err(card->dev,
3766 "ASoC: codec_dai_name not set for %s\n",
3767 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003768 return -EINVAL;
3769 }
Stephen Warren5a504962011-12-21 10:40:59 -07003770
3771 /*
3772 * Platform may be specified by either name or OF node, but
3773 * can be left unspecified, and a dummy platform will be used.
3774 */
3775 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003776 dev_err(card->dev,
3777 "ASoC: Both platform name/of_node are set for %s\n",
3778 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003779 return -EINVAL;
3780 }
3781
3782 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003783 * CPU device may be specified by either name or OF node, but
3784 * can be left unspecified, and will be matched based on DAI
3785 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003786 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003787 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003788 dev_err(card->dev,
3789 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3790 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003791 return -EINVAL;
3792 }
3793 /*
3794 * At least one of CPU DAI name or CPU device name/node must be
3795 * specified
3796 */
3797 if (!link->cpu_dai_name &&
3798 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003799 dev_err(card->dev,
3800 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3801 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003802 return -EINVAL;
3803 }
3804 }
3805
Mark Browned77cc12011-05-03 18:25:34 +01003806 dev_set_drvdata(card->dev, card);
3807
Stephen Warren111c6412011-01-28 14:26:35 -07003808 snd_soc_initialize_card_lists(card);
3809
Vinod Koul150dd2f2011-01-13 22:48:32 +05303810 soc_init_card_debugfs(card);
3811
Mark Brown181a6892012-03-14 20:18:49 +00003812 card->rtd = devm_kzalloc(card->dev,
3813 sizeof(struct snd_soc_pcm_runtime) *
3814 (card->num_links + card->num_aux_devs),
3815 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003816 if (card->rtd == NULL)
3817 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003818 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003819 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003820
3821 for (i = 0; i < card->num_links; i++)
3822 card->rtd[i].dai_link = &card->dai_link[i];
3823
Mark Browndb432b42011-10-03 21:06:40 +01003824 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003825 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003826 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003827 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003828
Mark Brownb19e6e72012-03-14 21:18:39 +00003829 ret = snd_soc_instantiate_card(card);
3830 if (ret != 0)
3831 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003832
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003833 /* deactivate pins to sleep state */
3834 for (i = 0; i < card->num_rtd; i++) {
3835 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3836 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3837 if (!codec_dai->active)
3838 pinctrl_pm_select_sleep_state(codec_dai->dev);
3839 if (!cpu_dai->active)
3840 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3841 }
3842
Mark Brownb19e6e72012-03-14 21:18:39 +00003843 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003844}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303845EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003846
3847/**
3848 * snd_soc_unregister_card - Unregister a card with the ASoC core
3849 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003850 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003851 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003852 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303853int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003854{
Vinod Koulb0e26482011-01-13 22:48:02 +05303855 if (card->instantiated)
3856 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003857 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003858
3859 return 0;
3860}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303861EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003862
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003863/*
3864 * Simplify DAI link configuration by removing ".-1" from device names
3865 * and sanitizing names.
3866 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003867static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003868{
3869 char *found, name[NAME_SIZE];
3870 int id1, id2;
3871
3872 if (dev_name(dev) == NULL)
3873 return NULL;
3874
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003875 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003876
3877 /* are we a "%s.%d" name (platform and SPI components) */
3878 found = strstr(name, dev->driver->name);
3879 if (found) {
3880 /* get ID */
3881 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3882
3883 /* discard ID from name if ID == -1 */
3884 if (*id == -1)
3885 found[strlen(dev->driver->name)] = '\0';
3886 }
3887
3888 } else {
3889 /* I2C component devices are named "bus-addr" */
3890 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3891 char tmp[NAME_SIZE];
3892
3893 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003894 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003895
3896 /* sanitize component name for DAI link creation */
3897 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003898 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003899 } else
3900 *id = 0;
3901 }
3902
3903 return kstrdup(name, GFP_KERNEL);
3904}
3905
3906/*
3907 * Simplify DAI link naming for single devices with multiple DAIs by removing
3908 * any ".-1" and using the DAI name (instead of device name).
3909 */
3910static inline char *fmt_multiple_name(struct device *dev,
3911 struct snd_soc_dai_driver *dai_drv)
3912{
3913 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003914 dev_err(dev,
3915 "ASoC: error - multiple DAI %s registered with no name\n",
3916 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003917 return NULL;
3918 }
3919
3920 return kstrdup(dai_drv->name, GFP_KERNEL);
3921}
3922
Mark Brown91151712008-11-30 23:31:24 +00003923/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003924 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003925 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003926 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003927 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003928static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003929{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003930 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003931
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003932 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003933 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3934 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003935 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003936 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003937 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003938 }
Mark Brown91151712008-11-30 23:31:24 +00003939}
Mark Brown91151712008-11-30 23:31:24 +00003940
3941/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003942 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003943 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003944 * @component: The component the DAIs are registered for
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003945 * @codec: The CODEC that the DAIs are registered for, NULL if the component is
3946 * not a CODEC.
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003947 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003948 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003949 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3950 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003951 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003952static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003953 struct snd_soc_codec *codec, struct snd_soc_dai_driver *dai_drv,
3954 size_t count, bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003955{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003956 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003957 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003958 unsigned int i;
3959 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003960
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003961 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003962
3963 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003964
3965 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003966 if (dai == NULL) {
3967 ret = -ENOMEM;
3968 goto err;
3969 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003970
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003971 /*
3972 * Back in the old days when we still had component-less DAIs,
3973 * instead of having a static name, component-less DAIs would
3974 * inherit the name of the parent device so it is possible to
3975 * register multiple instances of the DAI. We still need to keep
3976 * the same naming style even though those DAIs are not
3977 * component-less anymore.
3978 */
3979 if (count == 1 && legacy_dai_naming) {
3980 dai->name = fmt_single_name(dev, &dai->id);
3981 } else {
3982 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3983 if (dai_drv[i].id)
3984 dai->id = dai_drv[i].id;
3985 else
3986 dai->id = i;
3987 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003988 if (dai->name == NULL) {
3989 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003990 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003991 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003992 }
3993
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003994 dai->component = component;
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003995 dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003996 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003997 dai->driver = &dai_drv[i];
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003998 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003999 if (!dai->driver->ops)
4000 dai->driver->ops = &null_dai_ops;
4001
Peter Ujfalusi5f800082012-08-07 10:24:13 +03004002 if (!dai->codec)
4003 dai->dapm.idle_bias_off = 1;
4004
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01004005 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01004006
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004007 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00004008 }
4009
4010 return 0;
4011
4012err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004013 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00004014
4015 return ret;
4016}
Mark Brown91151712008-11-30 23:31:24 +00004017
4018/**
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004019 * snd_soc_register_component - Register a component with the ASoC core
4020 *
4021 */
4022static int
4023__snd_soc_register_component(struct device *dev,
4024 struct snd_soc_component *cmpnt,
4025 const struct snd_soc_component_driver *cmpnt_drv,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004026 struct snd_soc_codec *codec,
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004027 struct snd_soc_dai_driver *dai_drv,
4028 int num_dai, bool allow_single_dai)
4029{
4030 int ret;
4031
4032 dev_dbg(dev, "component register %s\n", dev_name(dev));
4033
4034 if (!cmpnt) {
4035 dev_err(dev, "ASoC: Failed to connecting component\n");
4036 return -ENOMEM;
4037 }
4038
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004039 mutex_init(&cmpnt->io_mutex);
4040
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004041 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4042 if (!cmpnt->name) {
4043 dev_err(dev, "ASoC: Failed to simplifying name\n");
4044 return -ENOMEM;
4045 }
4046
4047 cmpnt->dev = dev;
4048 cmpnt->driver = cmpnt_drv;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004049 cmpnt->dai_drv = dai_drv;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004050 cmpnt->num_dai = num_dai;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01004051 INIT_LIST_HEAD(&cmpnt->dai_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004052
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004053 ret = snd_soc_register_dais(cmpnt, codec, dai_drv, num_dai,
4054 allow_single_dai);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004055 if (ret < 0) {
4056 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4057 goto error_component_name;
4058 }
4059
4060 mutex_lock(&client_mutex);
4061 list_add(&cmpnt->list, &component_list);
4062 mutex_unlock(&client_mutex);
4063
4064 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4065
4066 return ret;
4067
4068error_component_name:
4069 kfree(cmpnt->name);
4070
4071 return ret;
4072}
4073
4074int snd_soc_register_component(struct device *dev,
4075 const struct snd_soc_component_driver *cmpnt_drv,
4076 struct snd_soc_dai_driver *dai_drv,
4077 int num_dai)
4078{
4079 struct snd_soc_component *cmpnt;
4080
4081 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4082 if (!cmpnt) {
4083 dev_err(dev, "ASoC: Failed to allocate memory\n");
4084 return -ENOMEM;
4085 }
4086
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004087 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004088 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004089
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004090 return __snd_soc_register_component(dev, cmpnt, cmpnt_drv, NULL,
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004091 dai_drv, num_dai, true);
4092}
4093EXPORT_SYMBOL_GPL(snd_soc_register_component);
4094
Jarkko Nikula4da53392014-04-28 15:30:51 +03004095static void __snd_soc_unregister_component(struct snd_soc_component *cmpnt)
4096{
4097 snd_soc_unregister_dais(cmpnt);
4098
4099 mutex_lock(&client_mutex);
4100 list_del(&cmpnt->list);
4101 mutex_unlock(&client_mutex);
4102
4103 dev_dbg(cmpnt->dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4104 kfree(cmpnt->name);
4105}
4106
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004107/**
4108 * snd_soc_unregister_component - Unregister a component from the ASoC core
4109 *
4110 */
4111void snd_soc_unregister_component(struct device *dev)
4112{
4113 struct snd_soc_component *cmpnt;
4114
4115 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004116 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004117 goto found;
4118 }
4119 return;
4120
4121found:
Jarkko Nikula4da53392014-04-28 15:30:51 +03004122 __snd_soc_unregister_component(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004123}
4124EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4125
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004126static int snd_soc_platform_drv_write(struct snd_soc_component *component,
4127 unsigned int reg, unsigned int val)
4128{
4129 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4130
4131 return platform->driver->write(platform, reg, val);
4132}
4133
4134static int snd_soc_platform_drv_read(struct snd_soc_component *component,
4135 unsigned int reg, unsigned int *val)
4136{
4137 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4138
4139 *val = platform->driver->read(platform, reg);
4140
4141 return 0;
4142}
4143
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004144/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004145 * snd_soc_add_platform - Add a platform to the ASoC core
4146 * @dev: The parent device for the platform
4147 * @platform: The platform to add
4148 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004149 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004150int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004151 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004152{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004153 int ret;
4154
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004155 /* create platform component name */
4156 platform->name = fmt_single_name(dev, &platform->id);
Dan Carpenterb5c745f2013-07-22 09:56:54 +03004157 if (platform->name == NULL)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004158 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004159
4160 platform->dev = dev;
4161 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01004162 platform->dapm.dev = dev;
4163 platform->dapm.platform = platform;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004164 platform->dapm.component = &platform->component;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004165 platform->dapm.stream_event = platform_drv->stream_event;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004166 if (platform_drv->write)
4167 platform->component.write = snd_soc_platform_drv_write;
4168 if (platform_drv->read)
4169 platform->component.read = snd_soc_platform_drv_read;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004170
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004171 /* register component */
4172 ret = __snd_soc_register_component(dev, &platform->component,
4173 &platform_drv->component_driver,
4174 NULL, NULL, 0, false);
4175 if (ret < 0) {
4176 dev_err(platform->component.dev,
4177 "ASoC: Failed to register component: %d\n", ret);
4178 return ret;
4179 }
4180
Mark Brown12a48a8c2008-12-03 19:40:30 +00004181 mutex_lock(&client_mutex);
4182 list_add(&platform->list, &platform_list);
4183 mutex_unlock(&client_mutex);
4184
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004185 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004186
4187 return 0;
4188}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004189EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4190
4191/**
4192 * snd_soc_register_platform - Register a platform with the ASoC core
4193 *
4194 * @platform: platform to register
4195 */
4196int snd_soc_register_platform(struct device *dev,
4197 const struct snd_soc_platform_driver *platform_drv)
4198{
4199 struct snd_soc_platform *platform;
4200 int ret;
4201
4202 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4203
4204 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4205 if (platform == NULL)
4206 return -ENOMEM;
4207
4208 ret = snd_soc_add_platform(dev, platform, platform_drv);
4209 if (ret)
4210 kfree(platform);
4211
4212 return ret;
4213}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004214EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4215
4216/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004217 * snd_soc_remove_platform - Remove a platform from the ASoC core
4218 * @platform: the platform to remove
4219 */
4220void snd_soc_remove_platform(struct snd_soc_platform *platform)
4221{
Jarkko Nikula4da53392014-04-28 15:30:51 +03004222 __snd_soc_unregister_component(&platform->component);
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004223
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004224 mutex_lock(&client_mutex);
4225 list_del(&platform->list);
4226 mutex_unlock(&client_mutex);
4227
4228 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4229 platform->name);
4230 kfree(platform->name);
4231}
4232EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4233
4234struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4235{
4236 struct snd_soc_platform *platform;
4237
4238 list_for_each_entry(platform, &platform_list, list) {
4239 if (dev == platform->dev)
4240 return platform;
4241 }
4242
4243 return NULL;
4244}
4245EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4246
4247/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004248 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4249 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004250 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004251 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004252void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004253{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004254 struct snd_soc_platform *platform;
4255
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004256 platform = snd_soc_lookup_platform(dev);
4257 if (!platform)
4258 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004259
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004260 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004261 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004262}
4263EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4264
Mark Brown151ab222009-05-09 16:22:58 +01004265static u64 codec_format_map[] = {
4266 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4267 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4268 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4269 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4270 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4271 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4272 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4273 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4274 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4275 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4276 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4277 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4278 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4279 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4280 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4281 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4282};
4283
4284/* Fix up the DAI formats for endianness: codecs don't actually see
4285 * the endianness of the data but we're using the CPU format
4286 * definitions which do need to include endianness so we ensure that
4287 * codec DAIs always have both big and little endian variants set.
4288 */
4289static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4290{
4291 int i;
4292
4293 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4294 if (stream->formats & codec_format_map[i])
4295 stream->formats |= codec_format_map[i];
4296}
4297
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004298static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4299 unsigned int reg, unsigned int val)
4300{
4301 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4302
4303 return codec->driver->write(codec, reg, val);
4304}
4305
4306static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4307 unsigned int reg, unsigned int *val)
4308{
4309 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4310
4311 *val = codec->driver->read(codec, reg);
4312
4313 return 0;
4314}
4315
Mark Brown0d0cf002008-12-10 14:32:45 +00004316/**
4317 * snd_soc_register_codec - Register a codec with the ASoC core
4318 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004319 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004320 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004321int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004322 const struct snd_soc_codec_driver *codec_drv,
4323 struct snd_soc_dai_driver *dai_drv,
4324 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004325{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004326 struct snd_soc_codec *codec;
Xiubo Lia39f75f2014-03-26 13:40:23 +08004327 struct regmap *regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004328 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004329
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004330 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004331
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004332 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4333 if (codec == NULL)
4334 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004335
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004336 /* create CODEC component name */
4337 codec->name = fmt_single_name(dev, &codec->id);
4338 if (codec->name == NULL) {
Kuninori Morimotof790b942013-02-25 00:40:09 -08004339 ret = -ENOMEM;
4340 goto fail_codec;
Mark Brown151ab222009-05-09 16:22:58 +01004341 }
4342
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004343 if (codec_drv->write)
4344 codec->component.write = snd_soc_codec_drv_write;
4345 if (codec_drv->read)
4346 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004347 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004348 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4349 codec->dapm.dev = dev;
4350 codec->dapm.codec = codec;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004351 codec->dapm.component = &codec->component;
Mark Brown474b62d2011-01-18 16:14:44 +00004352 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004353 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004354 codec->dev = dev;
4355 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004356 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004357 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004358
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004359 if (!codec->component.write) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004360 if (codec_drv->get_regmap)
4361 regmap = codec_drv->get_regmap(dev);
4362 else
4363 regmap = dev_get_regmap(dev, NULL);
4364
4365 if (regmap) {
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004366 ret = snd_soc_component_init_io(&codec->component,
4367 regmap);
4368 if (ret) {
Xiubo Lia39f75f2014-03-26 13:40:23 +08004369 dev_err(codec->dev,
4370 "Failed to set cache I/O:%d\n",
4371 ret);
4372 return ret;
4373 }
4374 }
4375 }
4376
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004377 for (i = 0; i < num_dai; i++) {
4378 fixup_codec_formats(&dai_drv[i].playback);
4379 fixup_codec_formats(&dai_drv[i].capture);
4380 }
4381
Mark Brown054880f2012-04-09 17:29:19 +01004382 mutex_lock(&client_mutex);
4383 list_add(&codec->list, &codec_list);
4384 mutex_unlock(&client_mutex);
4385
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004386 /* register component */
4387 ret = __snd_soc_register_component(dev, &codec->component,
4388 &codec_drv->component_driver,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004389 codec, dai_drv, num_dai, false);
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004390 if (ret < 0) {
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004391 dev_err(codec->dev, "ASoC: Failed to regster component: %d\n", ret);
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004392 goto fail_codec_name;
Mark Brown26b01cc2010-08-18 20:20:55 +01004393 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004394
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004395 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004396 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004397
Kuninori Morimotof790b942013-02-25 00:40:09 -08004398fail_codec_name:
Kuninori Morimotoe1328a82013-03-07 17:42:33 -08004399 mutex_lock(&client_mutex);
4400 list_del(&codec->list);
4401 mutex_unlock(&client_mutex);
4402
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004403 kfree(codec->name);
Kuninori Morimotof790b942013-02-25 00:40:09 -08004404fail_codec:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004405 kfree(codec);
4406 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004407}
4408EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4409
4410/**
4411 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4412 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004413 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004414 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004415void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004416{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004417 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004418
4419 list_for_each_entry(codec, &codec_list, list) {
4420 if (dev == codec->dev)
4421 goto found;
4422 }
4423 return;
4424
4425found:
Jarkko Nikula4da53392014-04-28 15:30:51 +03004426 __snd_soc_unregister_component(&codec->component);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004427
Mark Brown0d0cf002008-12-10 14:32:45 +00004428 mutex_lock(&client_mutex);
4429 list_del(&codec->list);
4430 mutex_unlock(&client_mutex);
4431
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004432 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004433
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004434 snd_soc_cache_exit(codec);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004435 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004436 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004437}
4438EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4439
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004440/* Retrieve a card's name from device tree */
4441int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4442 const char *propname)
4443{
4444 struct device_node *np = card->dev->of_node;
4445 int ret;
4446
4447 ret = of_property_read_string_index(np, propname, 0, &card->name);
4448 /*
4449 * EINVAL means the property does not exist. This is fine providing
4450 * card->name was previously set, which is checked later in
4451 * snd_soc_register_card.
4452 */
4453 if (ret < 0 && ret != -EINVAL) {
4454 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004455 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004456 propname, ret);
4457 return ret;
4458 }
4459
4460 return 0;
4461}
4462EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4463
Xiubo Li9a6d4862014-02-08 15:59:52 +08004464static const struct snd_soc_dapm_widget simple_widgets[] = {
4465 SND_SOC_DAPM_MIC("Microphone", NULL),
4466 SND_SOC_DAPM_LINE("Line", NULL),
4467 SND_SOC_DAPM_HP("Headphone", NULL),
4468 SND_SOC_DAPM_SPK("Speaker", NULL),
4469};
4470
4471int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4472 const char *propname)
4473{
4474 struct device_node *np = card->dev->of_node;
4475 struct snd_soc_dapm_widget *widgets;
4476 const char *template, *wname;
4477 int i, j, num_widgets, ret;
4478
4479 num_widgets = of_property_count_strings(np, propname);
4480 if (num_widgets < 0) {
4481 dev_err(card->dev,
4482 "ASoC: Property '%s' does not exist\n", propname);
4483 return -EINVAL;
4484 }
4485 if (num_widgets & 1) {
4486 dev_err(card->dev,
4487 "ASoC: Property '%s' length is not even\n", propname);
4488 return -EINVAL;
4489 }
4490
4491 num_widgets /= 2;
4492 if (!num_widgets) {
4493 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4494 propname);
4495 return -EINVAL;
4496 }
4497
4498 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4499 GFP_KERNEL);
4500 if (!widgets) {
4501 dev_err(card->dev,
4502 "ASoC: Could not allocate memory for widgets\n");
4503 return -ENOMEM;
4504 }
4505
4506 for (i = 0; i < num_widgets; i++) {
4507 ret = of_property_read_string_index(np, propname,
4508 2 * i, &template);
4509 if (ret) {
4510 dev_err(card->dev,
4511 "ASoC: Property '%s' index %d read error:%d\n",
4512 propname, 2 * i, ret);
4513 return -EINVAL;
4514 }
4515
4516 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4517 if (!strncmp(template, simple_widgets[j].name,
4518 strlen(simple_widgets[j].name))) {
4519 widgets[i] = simple_widgets[j];
4520 break;
4521 }
4522 }
4523
4524 if (j >= ARRAY_SIZE(simple_widgets)) {
4525 dev_err(card->dev,
4526 "ASoC: DAPM widget '%s' is not supported\n",
4527 template);
4528 return -EINVAL;
4529 }
4530
4531 ret = of_property_read_string_index(np, propname,
4532 (2 * i) + 1,
4533 &wname);
4534 if (ret) {
4535 dev_err(card->dev,
4536 "ASoC: Property '%s' index %d read error:%d\n",
4537 propname, (2 * i) + 1, ret);
4538 return -EINVAL;
4539 }
4540
4541 widgets[i].name = wname;
4542 }
4543
4544 card->dapm_widgets = widgets;
4545 card->num_dapm_widgets = num_widgets;
4546
4547 return 0;
4548}
4549EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4550
Xiubo Li89c67852014-02-14 09:34:35 +08004551int snd_soc_of_parse_tdm_slot(struct device_node *np,
4552 unsigned int *slots,
4553 unsigned int *slot_width)
4554{
4555 u32 val;
4556 int ret;
4557
4558 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4559 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4560 if (ret)
4561 return ret;
4562
4563 if (slots)
4564 *slots = val;
4565 }
4566
4567 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4568 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4569 if (ret)
4570 return ret;
4571
4572 if (slot_width)
4573 *slot_width = val;
4574 }
4575
4576 return 0;
4577}
4578EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4579
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004580int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4581 const char *propname)
4582{
4583 struct device_node *np = card->dev->of_node;
4584 int num_routes;
4585 struct snd_soc_dapm_route *routes;
4586 int i, ret;
4587
4588 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004589 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004590 dev_err(card->dev,
4591 "ASoC: Property '%s' does not exist or its length is not even\n",
4592 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004593 return -EINVAL;
4594 }
4595 num_routes /= 2;
4596 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004597 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004598 propname);
4599 return -EINVAL;
4600 }
4601
4602 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4603 GFP_KERNEL);
4604 if (!routes) {
4605 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004606 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004607 return -EINVAL;
4608 }
4609
4610 for (i = 0; i < num_routes; i++) {
4611 ret = of_property_read_string_index(np, propname,
4612 2 * i, &routes[i].sink);
4613 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004614 dev_err(card->dev,
4615 "ASoC: Property '%s' index %d could not be read: %d\n",
4616 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004617 return -EINVAL;
4618 }
4619 ret = of_property_read_string_index(np, propname,
4620 (2 * i) + 1, &routes[i].source);
4621 if (ret) {
4622 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004623 "ASoC: Property '%s' index %d could not be read: %d\n",
4624 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004625 return -EINVAL;
4626 }
4627 }
4628
4629 card->num_dapm_routes = num_routes;
4630 card->dapm_routes = routes;
4631
4632 return 0;
4633}
4634EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4635
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004636unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004637 const char *prefix,
4638 struct device_node **bitclkmaster,
4639 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004640{
4641 int ret, i;
4642 char prop[128];
4643 unsigned int format = 0;
4644 int bit, frame;
4645 const char *str;
4646 struct {
4647 char *name;
4648 unsigned int val;
4649 } of_fmt_table[] = {
4650 { "i2s", SND_SOC_DAIFMT_I2S },
4651 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4652 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4653 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4654 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4655 { "ac97", SND_SOC_DAIFMT_AC97 },
4656 { "pdm", SND_SOC_DAIFMT_PDM},
4657 { "msb", SND_SOC_DAIFMT_MSB },
4658 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004659 };
4660
4661 if (!prefix)
4662 prefix = "";
4663
4664 /*
4665 * check "[prefix]format = xxx"
4666 * SND_SOC_DAIFMT_FORMAT_MASK area
4667 */
4668 snprintf(prop, sizeof(prop), "%sformat", prefix);
4669 ret = of_property_read_string(np, prop, &str);
4670 if (ret == 0) {
4671 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4672 if (strcmp(str, of_fmt_table[i].name) == 0) {
4673 format |= of_fmt_table[i].val;
4674 break;
4675 }
4676 }
4677 }
4678
4679 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004680 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004681 * SND_SOC_DAIFMT_CLOCK_MASK area
4682 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004683 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4684 if (of_get_property(np, prop, NULL))
4685 format |= SND_SOC_DAIFMT_CONT;
4686 else
4687 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004688
4689 /*
4690 * check "[prefix]bitclock-inversion"
4691 * check "[prefix]frame-inversion"
4692 * SND_SOC_DAIFMT_INV_MASK area
4693 */
4694 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4695 bit = !!of_get_property(np, prop, NULL);
4696
4697 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4698 frame = !!of_get_property(np, prop, NULL);
4699
4700 switch ((bit << 4) + frame) {
4701 case 0x11:
4702 format |= SND_SOC_DAIFMT_IB_IF;
4703 break;
4704 case 0x10:
4705 format |= SND_SOC_DAIFMT_IB_NF;
4706 break;
4707 case 0x01:
4708 format |= SND_SOC_DAIFMT_NB_IF;
4709 break;
4710 default:
4711 /* SND_SOC_DAIFMT_NB_NF is default */
4712 break;
4713 }
4714
4715 /*
4716 * check "[prefix]bitclock-master"
4717 * check "[prefix]frame-master"
4718 * SND_SOC_DAIFMT_MASTER_MASK area
4719 */
4720 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4721 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004722 if (bit && bitclkmaster)
4723 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004724
4725 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4726 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004727 if (frame && framemaster)
4728 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004729
4730 switch ((bit << 4) + frame) {
4731 case 0x11:
4732 format |= SND_SOC_DAIFMT_CBM_CFM;
4733 break;
4734 case 0x10:
4735 format |= SND_SOC_DAIFMT_CBM_CFS;
4736 break;
4737 case 0x01:
4738 format |= SND_SOC_DAIFMT_CBS_CFM;
4739 break;
4740 default:
4741 format |= SND_SOC_DAIFMT_CBS_CFS;
4742 break;
4743 }
4744
4745 return format;
4746}
4747EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4748
Kuninori Morimotocb470082013-09-10 17:39:56 -07004749int snd_soc_of_get_dai_name(struct device_node *of_node,
4750 const char **dai_name)
4751{
4752 struct snd_soc_component *pos;
4753 struct of_phandle_args args;
4754 int ret;
4755
4756 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4757 "#sound-dai-cells", 0, &args);
4758 if (ret)
4759 return ret;
4760
4761 ret = -EPROBE_DEFER;
4762
4763 mutex_lock(&client_mutex);
4764 list_for_each_entry(pos, &component_list, list) {
4765 if (pos->dev->of_node != args.np)
4766 continue;
4767
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004768 if (pos->driver->of_xlate_dai_name) {
4769 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4770 } else {
4771 int id = -1;
4772
4773 switch (args.args_count) {
4774 case 0:
4775 id = 0; /* same as dai_drv[0] */
4776 break;
4777 case 1:
4778 id = args.args[0];
4779 break;
4780 default:
4781 /* not supported */
4782 break;
4783 }
4784
4785 if (id < 0 || id >= pos->num_dai) {
4786 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004787 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004788 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004789
4790 ret = 0;
4791
4792 *dai_name = pos->dai_drv[id].name;
4793 if (!*dai_name)
4794 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004795 }
4796
Kuninori Morimotocb470082013-09-10 17:39:56 -07004797 break;
4798 }
4799 mutex_unlock(&client_mutex);
4800
4801 of_node_put(args.np);
4802
4803 return ret;
4804}
4805EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4806
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004807static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004808{
Mark Brown384c89e2008-12-03 17:34:03 +00004809#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004810 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4811 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004812 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004813 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004814 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004815
Mark Brown8a9dab12011-01-10 22:25:21 +00004816 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004817 &codec_list_fops))
4818 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4819
Mark Brown8a9dab12011-01-10 22:25:21 +00004820 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004821 &dai_list_fops))
4822 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004823
Mark Brown8a9dab12011-01-10 22:25:21 +00004824 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004825 &platform_list_fops))
4826 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004827#endif
4828
Mark Brownfb257892011-04-28 10:57:54 +01004829 snd_soc_util_init();
4830
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004831 return platform_driver_register(&soc_driver);
4832}
Mark Brown4abe8e12010-10-12 17:41:03 +01004833module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004834
Mark Brown7d8c16a2008-11-30 22:11:24 +00004835static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004836{
Mark Brownfb257892011-04-28 10:57:54 +01004837 snd_soc_util_exit();
4838
Mark Brown384c89e2008-12-03 17:34:03 +00004839#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004840 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004841#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004842 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004843}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004844module_exit(snd_soc_exit);
4845
4846/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004847MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004848MODULE_DESCRIPTION("ALSA SoC Core");
4849MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004850MODULE_ALIAS("platform:soc-audio");