blob: 4c0f7dccbd835c21103d8d6cc8c9347ed80c48a8 [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 Clausenb92d1502011-08-27 18:24:14 +0200157 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000158 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000159 if (codec->driver->display_register) {
160 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000161 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100162 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000163 /* only support larger than PAGE_SIZE bytes debugfs
164 * entries for the default case */
165 if (p >= pos) {
166 if (total + len >= count - 1)
167 break;
168 format_register_str(codec, i, buf + total, len);
169 total += len;
170 }
171 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100172 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000173 }
174
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000175 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000176
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000177 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000178}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000179
Mark Brown2624d5f2009-11-03 21:56:13 +0000180static ssize_t codec_reg_show(struct device *dev,
181 struct device_attribute *attr, char *buf)
182{
Mark Brown36ae1a92012-01-06 17:12:45 -0800183 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000185 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000186}
187
188static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
189
Mark Browndbe21402010-02-12 11:37:24 +0000190static ssize_t pmdown_time_show(struct device *dev,
191 struct device_attribute *attr, char *buf)
192{
Mark Brown36ae1a92012-01-06 17:12:45 -0800193 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000194
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000195 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000196}
197
198static ssize_t pmdown_time_set(struct device *dev,
199 struct device_attribute *attr,
200 const char *buf, size_t count)
201{
Mark Brown36ae1a92012-01-06 17:12:45 -0800202 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700203 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000204
Jingoo Hanb785a492013-07-19 16:24:59 +0900205 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700206 if (ret)
207 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000208
209 return count;
210}
211
212static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
213
Mark Brown2624d5f2009-11-03 21:56:13 +0000214#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000215static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000216 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000217{
218 ssize_t ret;
219 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000220 char *buf;
221
222 if (*ppos < 0 || !count)
223 return -EINVAL;
224
225 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000226 if (!buf)
227 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000228
229 ret = soc_codec_reg_show(codec, buf, count, *ppos);
230 if (ret >= 0) {
231 if (copy_to_user(user_buf, buf, ret)) {
232 kfree(buf);
233 return -EFAULT;
234 }
235 *ppos += ret;
236 }
237
Mark Brown2624d5f2009-11-03 21:56:13 +0000238 kfree(buf);
239 return ret;
240}
241
242static ssize_t codec_reg_write_file(struct file *file,
243 const char __user *user_buf, size_t count, loff_t *ppos)
244{
245 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700246 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000247 char *start = buf;
248 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000249 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900250 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000251
252 buf_size = min(count, (sizeof(buf)-1));
253 if (copy_from_user(buf, user_buf, buf_size))
254 return -EFAULT;
255 buf[buf_size] = 0;
256
Mark Brown2624d5f2009-11-03 21:56:13 +0000257 while (*start == ' ')
258 start++;
259 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000260 while (*start == ' ')
261 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900262 ret = kstrtoul(start, 16, &value);
263 if (ret)
264 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000265
266 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030267 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000268
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000269 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000270 return buf_size;
271}
272
273static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700274 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000275 .read = codec_reg_read_file,
276 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200277 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000278};
279
280static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
281{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200282 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
283
284 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
285 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000286 if (!codec->debugfs_codec_root) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200287 dev_warn(codec->dev,
288 "ASoC: Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000289 return;
290 }
291
Mark Brownaaee8ef2011-01-26 20:53:50 +0000292 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
293 &codec->cache_sync);
294 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
295 &codec->cache_only);
296
Mark Brown2624d5f2009-11-03 21:56:13 +0000297 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
298 codec->debugfs_codec_root,
299 codec, &codec_reg_fops);
300 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200301 dev_warn(codec->dev,
302 "ASoC: Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000303
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200304 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000305}
306
307static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
308{
309 debugfs_remove_recursive(codec->debugfs_codec_root);
310}
311
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000312static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
313{
314 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
315
316 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
317 debugfs_card_root);
318 if (!platform->debugfs_platform_root) {
319 dev_warn(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000320 "ASoC: Failed to create platform debugfs directory\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000321 return;
322 }
323
324 snd_soc_dapm_debugfs_init(&platform->dapm,
325 platform->debugfs_platform_root);
326}
327
328static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
329{
330 debugfs_remove_recursive(platform->debugfs_platform_root);
331}
332
Mark Brownc3c5a192010-09-15 18:15:14 +0100333static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
334 size_t count, loff_t *ppos)
335{
336 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100337 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100338 struct snd_soc_codec *codec;
339
340 if (!buf)
341 return -ENOMEM;
342
Mark Brown2b194f9d2010-10-13 10:52:16 +0100343 list_for_each_entry(codec, &codec_list, list) {
344 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
345 codec->name);
346 if (len >= 0)
347 ret += len;
348 if (ret > PAGE_SIZE) {
349 ret = PAGE_SIZE;
350 break;
351 }
352 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100353
354 if (ret >= 0)
355 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
356
357 kfree(buf);
358
359 return ret;
360}
361
362static const struct file_operations codec_list_fops = {
363 .read = codec_list_read_file,
364 .llseek = default_llseek,/* read accesses f_pos */
365};
366
Mark Brownf3208782010-09-15 18:19:07 +0100367static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
368 size_t count, loff_t *ppos)
369{
370 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100371 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100372 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100373 struct snd_soc_dai *dai;
374
375 if (!buf)
376 return -ENOMEM;
377
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100378 list_for_each_entry(component, &component_list, list) {
379 list_for_each_entry(dai, &component->dai_list, list) {
380 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
381 dai->name);
382 if (len >= 0)
383 ret += len;
384 if (ret > PAGE_SIZE) {
385 ret = PAGE_SIZE;
386 break;
387 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100388 }
389 }
Mark Brownf3208782010-09-15 18:19:07 +0100390
Mark Brown2b194f9d2010-10-13 10:52:16 +0100391 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100392
393 kfree(buf);
394
395 return ret;
396}
397
398static const struct file_operations dai_list_fops = {
399 .read = dai_list_read_file,
400 .llseek = default_llseek,/* read accesses f_pos */
401};
402
Mark Brown19c7ac22010-09-15 18:22:40 +0100403static ssize_t platform_list_read_file(struct file *file,
404 char __user *user_buf,
405 size_t count, loff_t *ppos)
406{
407 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100408 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100409 struct snd_soc_platform *platform;
410
411 if (!buf)
412 return -ENOMEM;
413
Mark Brown2b194f9d2010-10-13 10:52:16 +0100414 list_for_each_entry(platform, &platform_list, list) {
415 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
416 platform->name);
417 if (len >= 0)
418 ret += len;
419 if (ret > PAGE_SIZE) {
420 ret = PAGE_SIZE;
421 break;
422 }
423 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100424
Mark Brown2b194f9d2010-10-13 10:52:16 +0100425 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100426
427 kfree(buf);
428
429 return ret;
430}
431
432static const struct file_operations platform_list_fops = {
433 .read = platform_list_read_file,
434 .llseek = default_llseek,/* read accesses f_pos */
435};
436
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200437static void soc_init_card_debugfs(struct snd_soc_card *card)
438{
439 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000440 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200441 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200442 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100443 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200444 return;
445 }
446
447 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
448 card->debugfs_card_root,
449 &card->pop_time);
450 if (!card->debugfs_pop_time)
451 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000452 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200453}
454
455static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
456{
457 debugfs_remove_recursive(card->debugfs_card_root);
458}
459
Mark Brown2624d5f2009-11-03 21:56:13 +0000460#else
461
462static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
463{
464}
465
466static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
467{
468}
Axel Linb95fccb2010-11-09 17:06:44 +0800469
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000470static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
471{
472}
473
474static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
475{
476}
477
Axel Linb95fccb2010-11-09 17:06:44 +0800478static inline void soc_init_card_debugfs(struct snd_soc_card *card)
479{
480}
481
482static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
483{
484}
Mark Brown2624d5f2009-11-03 21:56:13 +0000485#endif
486
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100487struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
488 const char *dai_link, int stream)
489{
490 int i;
491
492 for (i = 0; i < card->num_links; i++) {
493 if (card->rtd[i].dai_link->no_pcm &&
494 !strcmp(card->rtd[i].dai_link->name, dai_link))
495 return card->rtd[i].pcm->streams[stream].substream;
496 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000497 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100498 return NULL;
499}
500EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
501
502struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
503 const char *dai_link)
504{
505 int i;
506
507 for (i = 0; i < card->num_links; i++) {
508 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
509 return &card->rtd[i];
510 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000511 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100512 return NULL;
513}
514EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
515
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200516#ifdef CONFIG_SND_SOC_AC97_BUS
517/* unregister ac97 codec */
518static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
519{
520 if (codec->ac97->dev.bus)
521 device_unregister(&codec->ac97->dev);
522 return 0;
523}
524
525/* stop no dev release warning */
526static void soc_ac97_device_release(struct device *dev){}
527
528/* register ac97 codec to bus */
529static int soc_ac97_dev_register(struct snd_soc_codec *codec)
530{
531 int err;
532
533 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100534 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200535 codec->ac97->dev.release = soc_ac97_device_release;
536
Kay Sieversbb072bf2008-11-02 03:50:35 +0100537 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000538 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200539 err = device_register(&codec->ac97->dev);
540 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000541 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200542 codec->ac97->dev.bus = NULL;
543 return err;
544 }
545 return 0;
546}
547#endif
548
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100549static void codec2codec_close_delayed_work(struct work_struct *work)
550{
551 /* Currently nothing to do for c2c links
552 * Since c2c links are internal nodes in the DAPM graph and
553 * don't interface with the outside world or application layer
554 * we don't have to do any special handling on close.
555 */
556}
557
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000558#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200559/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000560int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200561{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000562 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200563 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564 int i;
565
Daniel Macke3509ff2009-06-03 17:44:49 +0200566 /* If the initialization of this soc device failed, there is no codec
567 * associated with it. Just bail out in this case.
568 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200570 return 0;
571
Andy Green6ed25972008-06-13 16:24:05 +0100572 /* Due to the resume being scheduled into a workqueue we could
573 * suspend before that's finished - wait for it to complete.
574 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000575 snd_power_lock(card->snd_card);
576 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
577 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100578
579 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100581
Mark Browna00f90f2010-12-02 16:24:24 +0000582 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000583 for (i = 0; i < card->num_rtd; i++) {
584 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
585 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100586
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100588 continue;
589
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000590 if (drv->ops->digital_mute && dai->playback_active)
591 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592 }
593
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100594 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000595 for (i = 0; i < card->num_rtd; i++) {
596 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100597 continue;
598
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100600 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100601
Mark Brown87506542008-11-18 20:50:34 +0000602 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000603 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200604
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605 for (i = 0; i < card->num_rtd; i++) {
606 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
607 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100608
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100610 continue;
611
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
613 cpu_dai->driver->suspend(cpu_dai);
614 if (platform->driver->suspend && !platform->suspended) {
615 platform->driver->suspend(cpu_dai);
616 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100617 }
618 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200619
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 /* close any waiting streams and save state */
621 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo43829732012-08-20 14:51:24 -0700622 flush_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200623 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100625
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627
628 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100629 continue;
630
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800631 snd_soc_dapm_stream_event(&card->rtd[i],
632 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800633 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800635 snd_soc_dapm_stream_event(&card->rtd[i],
636 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800637 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000638 }
639
Mark Browne2d32ff2012-08-31 17:38:32 -0700640 /* Recheck all analogue paths too */
641 dapm_mark_io_dirty(&card->dapm);
642 snd_soc_dapm_sync(&card->dapm);
643
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200645 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 /* If there are paths active then the CODEC will be held with
647 * bias _ON and should not be suspended. */
648 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200649 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000650 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000651 /*
652 * If the CODEC is capable of idle
653 * bias off then being in STANDBY
654 * means it's doing something,
655 * otherwise fall through.
656 */
657 if (codec->dapm.idle_bias_off) {
658 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200659 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000660 break;
661 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000662 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100663 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000664 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900665 codec->cache_sync = 1;
Mark Brownda8b8e02012-09-12 12:21:52 +0800666 if (codec->using_regmap)
667 regcache_mark_dirty(codec->control_data);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800668 /* deactivate pins to sleep state */
669 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000670 break;
671 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200672 dev_dbg(codec->dev,
673 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000674 break;
675 }
676 }
677 }
678
679 for (i = 0; i < card->num_rtd; i++) {
680 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
681
682 if (card->rtd[i].dai_link->ignore_suspend)
683 continue;
684
685 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
686 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800687
688 /* deactivate pins to sleep state */
689 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690 }
691
Mark Brown87506542008-11-18 20:50:34 +0000692 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000693 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200694
695 return 0;
696}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000697EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200698
Andy Green6ed25972008-06-13 16:24:05 +0100699/* deferred resume work, so resume can complete before we finished
700 * setting our codec back up, which can be very slow on I2C
701 */
702static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200703{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000704 struct snd_soc_card *card =
705 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200706 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200707 int i;
708
Andy Green6ed25972008-06-13 16:24:05 +0100709 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
710 * so userspace apps are blocked from touching us
711 */
712
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000713 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100714
Mark Brown9949788b2010-05-07 20:24:05 +0100715 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000716 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100717
Mark Brown87506542008-11-18 20:50:34 +0000718 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000719 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200720
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000721 /* resume AC97 DAIs */
722 for (i = 0; i < card->num_rtd; i++) {
723 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100724
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000725 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100726 continue;
727
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000728 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
729 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200730 }
731
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200732 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000733 /* If the CODEC was idle over suspend then it will have been
734 * left with bias OFF or STANDBY and suspended so we must now
735 * resume. Otherwise the suspend was suppressed.
736 */
737 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200738 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000739 case SND_SOC_BIAS_STANDBY:
740 case SND_SOC_BIAS_OFF:
741 codec->driver->resume(codec);
742 codec->suspended = 0;
743 break;
744 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200745 dev_dbg(codec->dev,
746 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000747 break;
748 }
Mark Brown1547aba2010-05-07 21:11:40 +0100749 }
750 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200751
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000752 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100753
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100755 continue;
756
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800757 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000758 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800759 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000760
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800761 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000762 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800763 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200764 }
765
Mark Brown3ff3f642008-05-19 12:32:25 +0200766 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000767 for (i = 0; i < card->num_rtd; i++) {
768 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
769 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100770
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000771 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100772 continue;
773
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000774 if (drv->ops->digital_mute && dai->playback_active)
775 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200776 }
777
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778 for (i = 0; i < card->num_rtd; i++) {
779 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
780 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100781
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100783 continue;
784
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000785 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
786 cpu_dai->driver->resume(cpu_dai);
787 if (platform->driver->resume && platform->suspended) {
788 platform->driver->resume(cpu_dai);
789 platform->suspended = 0;
790 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791 }
792
Mark Brown87506542008-11-18 20:50:34 +0000793 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000794 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200795
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000796 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100797
798 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000799 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700800
801 /* Recheck all analogue paths too */
802 dapm_mark_io_dirty(&card->dapm);
803 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100804}
805
806/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000807int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100808{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000809 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600810 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200811
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800812 /* If the initialization of this soc device failed, there is no codec
813 * associated with it. Just bail out in this case.
814 */
815 if (list_empty(&card->codec_dev_list))
816 return 0;
817
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800818 /* activate pins from sleep state */
819 for (i = 0; i < card->num_rtd; i++) {
820 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
821 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
822 if (cpu_dai->active)
823 pinctrl_pm_select_default_state(cpu_dai->dev);
824 if (codec_dai->active)
825 pinctrl_pm_select_default_state(codec_dai->dev);
826 }
827
Mark Brown64ab9ba2009-03-31 11:27:03 +0100828 /* AC97 devices might have other drivers hanging off them so
829 * need to resume immediately. Other drivers don't have that
830 * problem and may take a substantial amount of time to resume
831 * due to I/O costs and anti-pop so handle them out of line.
832 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000833 for (i = 0; i < card->num_rtd; i++) {
834 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600835 ac97_control |= cpu_dai->driver->ac97_control;
836 }
837 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000838 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600839 soc_resume_deferred(&card->deferred_resume_work);
840 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000841 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600842 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000843 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100844 }
Andy Green6ed25972008-06-13 16:24:05 +0100845
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200846 return 0;
847}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000848EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000850#define snd_soc_suspend NULL
851#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200852#endif
853
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100854static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800855};
856
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100857static struct snd_soc_codec *soc_find_codec(const struct device_node *codec_of_node,
858 const char *codec_name)
859{
860 struct snd_soc_codec *codec;
861
862 list_for_each_entry(codec, &codec_list, list) {
863 if (codec_of_node) {
864 if (codec->dev->of_node != codec_of_node)
865 continue;
866 } else {
867 if (strcmp(codec->name, codec_name))
868 continue;
869 }
870
871 return codec;
872 }
873
874 return NULL;
875}
876
877static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
878 const char *codec_dai_name)
879{
880 struct snd_soc_dai *codec_dai;
881
882 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
883 if (!strcmp(codec_dai->name, codec_dai_name)) {
884 return codec_dai;
885 }
886 }
887
888 return NULL;
889}
890
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000891static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200892{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000893 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
894 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100895 struct snd_soc_component *component;
Mark Brown435c5e22008-12-04 15:32:53 +0000896 struct snd_soc_platform *platform;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100897 struct snd_soc_dai *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100898 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200899
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000900 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000901
Mark Brownb19e6e72012-03-14 21:18:39 +0000902 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100903 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600904 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100905 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600906 continue;
907 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100908 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600909 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100910 list_for_each_entry(cpu_dai, &component->dai_list, list) {
911 if (dai_link->cpu_dai_name &&
912 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
913 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700914
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100915 rtd->cpu_dai = cpu_dai;
916 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000917 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000918
919 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000920 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000921 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000922 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000923 }
924
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100925 /* Find CODEC from registered list */
926 rtd->codec = soc_find_codec(dai_link->codec_of_node,
927 dai_link->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000928 if (!rtd->codec) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000929 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
Mark Brownb19e6e72012-03-14 21:18:39 +0000930 dai_link->codec_name);
931 return -EPROBE_DEFER;
932 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100933
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100934 /* Find CODEC DAI from registered list */
935 rtd->codec_dai = soc_find_codec_dai(rtd->codec,
936 dai_link->codec_dai_name);
937 if (!rtd->codec_dai) {
938 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
939 dai_link->codec_dai_name);
940 return -EPROBE_DEFER;
941 }
942
Mark Brown848dd8b2011-04-27 18:16:32 +0100943 /* if there's no platform we match on the empty platform */
944 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700945 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100946 platform_name = "snd-soc-dummy";
947
Mark Brownb19e6e72012-03-14 21:18:39 +0000948 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000949 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700950 if (dai_link->platform_of_node) {
951 if (platform->dev->of_node !=
952 dai_link->platform_of_node)
953 continue;
954 } else {
955 if (strcmp(platform->name, platform_name))
956 continue;
957 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700958
959 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000960 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000961 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000962 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000963 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000964 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000965 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000966
967 card->num_rtd++;
968
969 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000970}
971
Stephen Warrend12cd192012-06-08 12:34:22 -0600972static int soc_remove_platform(struct snd_soc_platform *platform)
973{
974 int ret;
975
976 if (platform->driver->remove) {
977 ret = platform->driver->remove(platform);
978 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000979 dev_err(platform->dev, "ASoC: failed to remove %d\n",
980 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -0600981 }
982
983 /* Make sure all DAPM widgets are freed */
984 snd_soc_dapm_free(&platform->dapm);
985
986 soc_cleanup_platform_debugfs(platform);
987 platform->probed = 0;
988 list_del(&platform->card_list);
989 module_put(platform->dev->driver->owner);
990
991 return 0;
992}
993
Jarkko Nikula589c3562010-12-06 16:27:07 +0200994static void soc_remove_codec(struct snd_soc_codec *codec)
995{
996 int err;
997
998 if (codec->driver->remove) {
999 err = codec->driver->remove(codec);
1000 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001001 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001002 }
1003
1004 /* Make sure all DAPM widgets are freed */
1005 snd_soc_dapm_free(&codec->dapm);
1006
1007 soc_cleanup_codec_debugfs(codec);
1008 codec->probed = 0;
1009 list_del(&codec->card_list);
1010 module_put(codec->dev->driver->owner);
1011}
1012
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001013static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1014{
1015 int err;
1016
1017 if (codec_dai && codec_dai->probed &&
1018 codec_dai->driver->remove_order == order) {
1019 if (codec_dai->driver->remove) {
1020 err = codec_dai->driver->remove(codec_dai);
1021 if (err < 0)
1022 dev_err(codec_dai->dev,
1023 "ASoC: failed to remove %s: %d\n",
1024 codec_dai->name, err);
1025 }
1026 codec_dai->probed = 0;
1027 list_del(&codec_dai->card_list);
1028 }
1029}
1030
Stephen Warren62ae68f2012-06-08 12:34:23 -06001031static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001032{
1033 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001034 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1035 int err;
1036
1037 /* unregister the rtd device */
1038 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001039 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1040 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1041 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001042 rtd->dev_registered = 0;
1043 }
1044
1045 /* remove the CODEC DAI */
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001046 soc_remove_codec_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001047
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001048 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001049 if (cpu_dai && cpu_dai->probed &&
1050 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001051 if (cpu_dai->driver->remove) {
1052 err = cpu_dai->driver->remove(cpu_dai);
1053 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001054 dev_err(cpu_dai->dev,
1055 "ASoC: failed to remove %s: %d\n",
1056 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001057 }
1058 cpu_dai->probed = 0;
1059 list_del(&cpu_dai->card_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001060
Stephen Warren18d75642012-06-08 12:34:21 -06001061 if (!cpu_dai->codec) {
1062 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001063 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -06001064 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001065 }
1066}
1067
Stephen Warren62ae68f2012-06-08 12:34:23 -06001068static void soc_remove_link_components(struct snd_soc_card *card, int num,
1069 int order)
1070{
1071 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1072 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1073 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1074 struct snd_soc_platform *platform = rtd->platform;
1075 struct snd_soc_codec *codec;
1076
1077 /* remove the platform */
1078 if (platform && platform->probed &&
1079 platform->driver->remove_order == order) {
1080 soc_remove_platform(platform);
1081 }
1082
1083 /* remove the CODEC-side CODEC */
1084 if (codec_dai) {
1085 codec = codec_dai->codec;
1086 if (codec && codec->probed &&
1087 codec->driver->remove_order == order)
1088 soc_remove_codec(codec);
1089 }
1090
1091 /* remove any CPU-side CODEC */
1092 if (cpu_dai) {
1093 codec = cpu_dai->codec;
1094 if (codec && codec->probed &&
1095 codec->driver->remove_order == order)
1096 soc_remove_codec(codec);
1097 }
1098}
1099
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001100static void soc_remove_dai_links(struct snd_soc_card *card)
1101{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001102 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001103
Liam Girdwood0168bf02011-06-07 16:08:05 +01001104 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1105 order++) {
1106 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001107 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001108 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001109
1110 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1111 order++) {
1112 for (dai = 0; dai < card->num_rtd; dai++)
1113 soc_remove_link_components(card, dai, order);
1114 }
1115
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001116 card->num_rtd = 0;
1117}
1118
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001119static void soc_set_name_prefix(struct snd_soc_card *card,
1120 struct snd_soc_codec *codec)
1121{
1122 int i;
1123
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001124 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001125 return;
1126
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001127 for (i = 0; i < card->num_configs; i++) {
1128 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001129 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1130 codec->name_prefix = map->name_prefix;
1131 break;
1132 }
1133 }
1134}
1135
Jarkko Nikula589c3562010-12-06 16:27:07 +02001136static int soc_probe_codec(struct snd_soc_card *card,
1137 struct snd_soc_codec *codec)
1138{
1139 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001140 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001141 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001142
1143 codec->card = card;
1144 codec->dapm.card = card;
1145 soc_set_name_prefix(card, codec);
1146
Jarkko Nikula70d293312011-01-27 16:24:22 +02001147 if (!try_module_get(codec->dev->driver->owner))
1148 return -ENODEV;
1149
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001150 soc_init_codec_debugfs(codec);
1151
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001152 if (driver->dapm_widgets)
1153 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1154 driver->num_dapm_widgets);
1155
Mark Brown888df392012-02-16 19:37:51 -08001156 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001157 list_for_each_entry(dai, &codec->component.dai_list, list)
Mark Brown888df392012-02-16 19:37:51 -08001158 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
Mark Brown888df392012-02-16 19:37:51 -08001159
Mark Brown33c5f962011-08-22 18:40:30 +01001160 codec->dapm.idle_bias_off = driver->idle_bias_off;
1161
Xiubo Lia32c17b2014-03-11 12:43:22 +08001162 if (!codec->write && dev_get_regmap(codec->dev, NULL)) {
1163 /* Set the default I/O up try regmap */
1164 ret = snd_soc_codec_set_cache_io(codec, NULL);
1165 if (ret < 0) {
1166 dev_err(codec->dev,
1167 "Failed to set cache I/O: %d\n", ret);
1168 goto err_probe;
1169 }
1170 }
Xiubo Life2265e2014-02-27 17:49:52 +08001171
Mark Brown89b95ac02011-03-07 16:38:44 +00001172 if (driver->probe) {
1173 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001174 if (ret < 0) {
1175 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001176 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001177 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001178 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001179 WARN(codec->dapm.idle_bias_off &&
1180 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001181 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1182 codec->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001183 }
1184
Mark Brownb7af1da2011-04-07 19:18:44 +09001185 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001186 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001187 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001188 if (driver->dapm_routes)
1189 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1190 driver->num_dapm_routes);
1191
Jarkko Nikula589c3562010-12-06 16:27:07 +02001192 /* mark codec as probed and add to card codec list */
1193 codec->probed = 1;
1194 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001195 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001196
Jarkko Nikula70d293312011-01-27 16:24:22 +02001197 return 0;
1198
1199err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001200 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001201 module_put(codec->dev->driver->owner);
1202
Jarkko Nikula589c3562010-12-06 16:27:07 +02001203 return ret;
1204}
1205
Liam Girdwood956245e2011-07-01 16:54:08 +01001206static int soc_probe_platform(struct snd_soc_card *card,
1207 struct snd_soc_platform *platform)
1208{
1209 int ret = 0;
1210 const struct snd_soc_platform_driver *driver = platform->driver;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001211 struct snd_soc_component *component;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001212 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001213
1214 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001215 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001216
1217 if (!try_module_get(platform->dev->driver->owner))
1218 return -ENODEV;
1219
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001220 soc_init_platform_debugfs(platform);
1221
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001222 if (driver->dapm_widgets)
1223 snd_soc_dapm_new_controls(&platform->dapm,
1224 driver->dapm_widgets, driver->num_dapm_widgets);
1225
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001226 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001227 list_for_each_entry(component, &component_list, list) {
1228 if (component->dev != platform->dev)
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001229 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001230 list_for_each_entry(dai, &component->dai_list, list)
1231 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001232 }
1233
Stephen Warren3fec6b62012-04-05 12:28:01 -06001234 platform->dapm.idle_bias_off = 1;
1235
Liam Girdwood956245e2011-07-01 16:54:08 +01001236 if (driver->probe) {
1237 ret = driver->probe(platform);
1238 if (ret < 0) {
1239 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001240 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001241 goto err_probe;
1242 }
1243 }
1244
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001245 if (driver->controls)
1246 snd_soc_add_platform_controls(platform, driver->controls,
1247 driver->num_controls);
1248 if (driver->dapm_routes)
1249 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1250 driver->num_dapm_routes);
1251
Liam Girdwood956245e2011-07-01 16:54:08 +01001252 /* mark platform as probed and add to card platform list */
1253 platform->probed = 1;
1254 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001255 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001256
1257 return 0;
1258
1259err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001260 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001261 module_put(platform->dev->driver->owner);
1262
1263 return ret;
1264}
1265
Mark Brown36ae1a92012-01-06 17:12:45 -08001266static void rtd_release(struct device *dev)
1267{
1268 kfree(dev);
1269}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001270
Jarkko Nikula589c3562010-12-06 16:27:07 +02001271static int soc_post_component_init(struct snd_soc_card *card,
1272 struct snd_soc_codec *codec,
1273 int num, int dailess)
1274{
1275 struct snd_soc_dai_link *dai_link = NULL;
1276 struct snd_soc_aux_dev *aux_dev = NULL;
1277 struct snd_soc_pcm_runtime *rtd;
Lars-Peter Clausen6479f15ad2014-03-12 15:27:40 +01001278 const char *name;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001279 int ret = 0;
1280
1281 if (!dailess) {
1282 dai_link = &card->dai_link[num];
1283 rtd = &card->rtd[num];
1284 name = dai_link->name;
1285 } else {
1286 aux_dev = &card->aux_dev[num];
1287 rtd = &card->rtd_aux[num];
1288 name = aux_dev->name;
1289 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001290 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001291
Jarkko Nikula589c3562010-12-06 16:27:07 +02001292 /* do machine specific initialization */
1293 if (!dailess && dai_link->init)
1294 ret = dai_link->init(rtd);
1295 else if (dailess && aux_dev->init)
1296 ret = aux_dev->init(&codec->dapm);
1297 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001298 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001299 return ret;
1300 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001301
Jarkko Nikula589c3562010-12-06 16:27:07 +02001302 /* register the rtd device */
1303 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001304
1305 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1306 if (!rtd->dev)
1307 return -ENOMEM;
1308 device_initialize(rtd->dev);
1309 rtd->dev->parent = card->dev;
1310 rtd->dev->release = rtd_release;
1311 rtd->dev->init_name = name;
1312 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001313 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001314 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1315 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1316 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1317 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001318 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001319 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001320 /* calling put_device() here to free the rtd->dev */
1321 put_device(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001322 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001323 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001324 return ret;
1325 }
1326 rtd->dev_registered = 1;
1327
1328 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001329 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001330 if (ret < 0)
1331 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001332 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001333
1334 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001335 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001336 if (ret < 0)
1337 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001338 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001339
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001340#ifdef CONFIG_DEBUG_FS
1341 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001342 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001343 goto out;
1344
1345 ret = soc_dpcm_debugfs_add(rtd);
1346 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001347 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001348
1349out:
1350#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001351 return 0;
1352}
1353
Stephen Warren62ae68f2012-06-08 12:34:23 -06001354static int soc_probe_link_components(struct snd_soc_card *card, int num,
1355 int order)
1356{
1357 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1358 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1359 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1360 struct snd_soc_platform *platform = rtd->platform;
1361 int ret;
1362
1363 /* probe the CPU-side component, if it is a CODEC */
1364 if (cpu_dai->codec &&
1365 !cpu_dai->codec->probed &&
1366 cpu_dai->codec->driver->probe_order == order) {
1367 ret = soc_probe_codec(card, cpu_dai->codec);
1368 if (ret < 0)
1369 return ret;
1370 }
1371
1372 /* probe the CODEC-side component */
1373 if (!codec_dai->codec->probed &&
1374 codec_dai->codec->driver->probe_order == order) {
1375 ret = soc_probe_codec(card, codec_dai->codec);
1376 if (ret < 0)
1377 return ret;
1378 }
1379
1380 /* probe the platform */
1381 if (!platform->probed &&
1382 platform->driver->probe_order == order) {
1383 ret = soc_probe_platform(card, platform);
1384 if (ret < 0)
1385 return ret;
1386 }
1387
1388 return 0;
1389}
1390
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001391static int soc_probe_codec_dai(struct snd_soc_card *card,
1392 struct snd_soc_dai *codec_dai,
1393 int order)
1394{
1395 int ret;
1396
1397 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1398 if (codec_dai->driver->probe) {
1399 ret = codec_dai->driver->probe(codec_dai);
1400 if (ret < 0) {
1401 dev_err(codec_dai->dev,
1402 "ASoC: failed to probe CODEC DAI %s: %d\n",
1403 codec_dai->name, ret);
1404 return ret;
1405 }
1406 }
1407
1408 /* mark codec_dai as probed and add to card dai list */
1409 codec_dai->probed = 1;
1410 list_add(&codec_dai->card_list, &card->dai_dev_list);
1411 }
1412
1413 return 0;
1414}
1415
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001416static int soc_link_dai_widgets(struct snd_soc_card *card,
1417 struct snd_soc_dai_link *dai_link,
1418 struct snd_soc_dai *cpu_dai,
1419 struct snd_soc_dai *codec_dai)
1420{
1421 struct snd_soc_dapm_widget *play_w, *capture_w;
1422 int ret;
1423
1424 /* link the DAI widgets */
1425 play_w = codec_dai->playback_widget;
1426 capture_w = cpu_dai->capture_widget;
1427 if (play_w && capture_w) {
1428 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1429 capture_w, play_w);
1430 if (ret != 0) {
1431 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1432 play_w->name, capture_w->name, ret);
1433 return ret;
1434 }
1435 }
1436
1437 play_w = cpu_dai->playback_widget;
1438 capture_w = codec_dai->capture_widget;
1439 if (play_w && capture_w) {
1440 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1441 capture_w, play_w);
1442 if (ret != 0) {
1443 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1444 play_w->name, capture_w->name, ret);
1445 return ret;
1446 }
1447 }
1448
1449 return 0;
1450}
1451
Stephen Warren62ae68f2012-06-08 12:34:23 -06001452static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001453{
1454 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1455 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1456 struct snd_soc_codec *codec = rtd->codec;
1457 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001458 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1459 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001460 int ret;
1461
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001462 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001463 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001464
1465 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001466 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001467 codec_dai->card = card;
1468 cpu_dai->card = card;
1469
1470 /* set default power off timeout */
1471 rtd->pmdown_time = pmdown_time;
1472
1473 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001474 if (!cpu_dai->probed &&
1475 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001476 if (!cpu_dai->codec) {
1477 cpu_dai->dapm.card = card;
1478 if (!try_module_get(cpu_dai->dev->driver->owner))
1479 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001480
Stephen Warren18d75642012-06-08 12:34:21 -06001481 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001482 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001483
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001484 if (cpu_dai->driver->probe) {
1485 ret = cpu_dai->driver->probe(cpu_dai);
1486 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001487 dev_err(cpu_dai->dev,
1488 "ASoC: failed to probe CPU DAI %s: %d\n",
1489 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001490 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001491 return ret;
1492 }
1493 }
1494 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001495 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001496 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1497 }
1498
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001499 /* probe the CODEC DAI */
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001500 ret = soc_probe_codec_dai(card, codec_dai, order);
1501 if (ret)
1502 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001503
Liam Girdwood0168bf02011-06-07 16:08:05 +01001504 /* complete DAI probe during last probe */
1505 if (order != SND_SOC_COMP_ORDER_LAST)
1506 return 0;
1507
Jarkko Nikula589c3562010-12-06 16:27:07 +02001508 ret = soc_post_component_init(card, codec, num, 0);
1509 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001510 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001511
Mark Brown36ae1a92012-01-06 17:12:45 -08001512 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001513 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001514 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1515 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001516
Namarta Kohli1245b702012-08-16 17:10:41 +05301517 if (cpu_dai->driver->compress_dai) {
1518 /*create compress_device"*/
1519 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001520 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001521 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301522 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001523 return ret;
1524 }
1525 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301526
1527 if (!dai_link->params) {
1528 /* create the pcm */
1529 ret = soc_new_pcm(rtd, num);
1530 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001531 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301532 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001533 return ret;
1534 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301535 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001536 INIT_DELAYED_WORK(&rtd->delayed_work,
1537 codec2codec_close_delayed_work);
1538
Namarta Kohli1245b702012-08-16 17:10:41 +05301539 /* link the DAI widgets */
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001540 ret = soc_link_dai_widgets(card, dai_link,
1541 cpu_dai, codec_dai);
1542 if (ret)
1543 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001544 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001545 }
1546
1547 /* add platform data for AC97 devices */
1548 if (rtd->codec_dai->driver->ac97_control)
1549 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1550
1551 return 0;
1552}
1553
1554#ifdef CONFIG_SND_SOC_AC97_BUS
1555static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1556{
1557 int ret;
1558
1559 /* Only instantiate AC97 if not already done by the adaptor
1560 * for the generic AC97 subsystem.
1561 */
1562 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001563 /*
1564 * It is possible that the AC97 device is already registered to
1565 * the device subsystem. This happens when the device is created
1566 * via snd_ac97_mixer(). Currently only SoC codec that does so
1567 * is the generic AC97 glue but others migh emerge.
1568 *
1569 * In those cases we don't try to register the device again.
1570 */
1571 if (!rtd->codec->ac97_created)
1572 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001573
1574 ret = soc_ac97_dev_register(rtd->codec);
1575 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001576 dev_err(rtd->codec->dev,
1577 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001578 return ret;
1579 }
1580
1581 rtd->codec->ac97_registered = 1;
1582 }
1583 return 0;
1584}
1585
1586static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1587{
1588 if (codec->ac97_registered) {
1589 soc_ac97_dev_unregister(codec);
1590 codec->ac97_registered = 0;
1591 }
1592}
1593#endif
1594
Mark Brownb19e6e72012-03-14 21:18:39 +00001595static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1596{
1597 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1598 struct snd_soc_codec *codec;
1599
1600 /* find CODEC from registered CODECs*/
1601 list_for_each_entry(codec, &codec_list, list) {
1602 if (!strcmp(codec->name, aux_dev->codec_name))
1603 return 0;
1604 }
1605
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001606 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
Mark Brownfb099cb2012-08-09 18:44:37 +01001607
Mark Brownb19e6e72012-03-14 21:18:39 +00001608 return -EPROBE_DEFER;
1609}
1610
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001611static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1612{
1613 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001614 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001615 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001616
1617 /* find CODEC from registered CODECs*/
1618 list_for_each_entry(codec, &codec_list, list) {
1619 if (!strcmp(codec->name, aux_dev->codec_name)) {
1620 if (codec->probed) {
1621 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001622 "ASoC: codec already probed");
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001623 ret = -EBUSY;
1624 goto out;
1625 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001626 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001627 }
1628 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001629 /* codec not found */
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001630 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001631 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001632
Jarkko Nikula676ad982010-12-03 09:18:22 +02001633found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001634 ret = soc_probe_codec(card, codec);
1635 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001636 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001637
Jarkko Nikula589c3562010-12-06 16:27:07 +02001638 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001639
1640out:
1641 return ret;
1642}
1643
1644static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1645{
1646 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1647 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001648
1649 /* unregister the rtd device */
1650 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001651 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001652 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001653 rtd->dev_registered = 0;
1654 }
1655
Jarkko Nikula589c3562010-12-06 16:27:07 +02001656 if (codec && codec->probed)
1657 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001658}
1659
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001660static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001661{
1662 int ret;
1663
1664 if (codec->cache_init)
1665 return 0;
1666
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001667 ret = snd_soc_cache_init(codec);
1668 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001669 dev_err(codec->dev,
1670 "ASoC: Failed to set cache compression type: %d\n",
1671 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001672 return ret;
1673 }
1674 codec->cache_init = 1;
1675 return 0;
1676}
1677
Mark Brownb19e6e72012-03-14 21:18:39 +00001678static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001679{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001680 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001681 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001682 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001683
Liam Girdwood01b9d992012-03-07 10:38:25 +00001684 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001685
Mark Brownb19e6e72012-03-14 21:18:39 +00001686 /* bind DAIs */
1687 for (i = 0; i < card->num_links; i++) {
1688 ret = soc_bind_dai_link(card, i);
1689 if (ret != 0)
1690 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001691 }
1692
Mark Brownb19e6e72012-03-14 21:18:39 +00001693 /* check aux_devs too */
1694 for (i = 0; i < card->num_aux_devs; i++) {
1695 ret = soc_check_aux_dev(card, i);
1696 if (ret != 0)
1697 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001698 }
1699
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001700 /* initialize the register cache for each available codec */
1701 list_for_each_entry(codec, &codec_list, list) {
1702 if (codec->cache_init)
1703 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001704 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001705 if (ret < 0)
1706 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001707 }
1708
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001709 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001710 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001711 card->owner, 0, &card->snd_card);
1712 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001713 dev_err(card->dev,
1714 "ASoC: can't create sound card for card %s: %d\n",
1715 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001716 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001717 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001718
Mark Browne37a4972011-03-02 18:21:57 +00001719 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1720 card->dapm.dev = card->dev;
1721 card->dapm.card = card;
1722 list_add(&card->dapm.list, &card->dapm_list);
1723
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001724#ifdef CONFIG_DEBUG_FS
1725 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1726#endif
1727
Mark Brown88ee1c62011-02-02 10:43:26 +00001728#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001729 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001730 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001731#endif
Andy Green6ed25972008-06-13 16:24:05 +01001732
Mark Brown9a841eb2011-04-12 17:51:37 -07001733 if (card->dapm_widgets)
1734 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1735 card->num_dapm_widgets);
1736
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001737 /* initialise the sound card only once */
1738 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001739 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001740 if (ret < 0)
1741 goto card_probe_error;
1742 }
1743
Stephen Warren62ae68f2012-06-08 12:34:23 -06001744 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001745 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1746 order++) {
1747 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001748 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001749 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001750 dev_err(card->dev,
1751 "ASoC: failed to instantiate card %d\n",
1752 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001753 goto probe_dai_err;
1754 }
1755 }
1756 }
1757
1758 /* probe all DAI links on this card */
1759 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1760 order++) {
1761 for (i = 0; i < card->num_links; i++) {
1762 ret = soc_probe_link_dais(card, i, order);
1763 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001764 dev_err(card->dev,
1765 "ASoC: failed to instantiate card %d\n",
1766 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001767 goto probe_dai_err;
1768 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001769 }
1770 }
1771
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001772 for (i = 0; i < card->num_aux_devs; i++) {
1773 ret = soc_probe_aux_dev(card, i);
1774 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001775 dev_err(card->dev,
1776 "ASoC: failed to add auxiliary devices %d\n",
1777 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001778 goto probe_aux_dev_err;
1779 }
1780 }
1781
Mark Brown888df392012-02-16 19:37:51 -08001782 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001783 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001784
Mark Brownb7af1da2011-04-07 19:18:44 +09001785 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001786 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001787
Mark Brownb8ad29d2011-03-02 18:35:51 +00001788 if (card->dapm_routes)
1789 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1790 card->num_dapm_routes);
1791
Mark Brown75d9ac42011-09-27 16:41:01 +01001792 for (i = 0; i < card->num_links; i++) {
1793 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001794 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001795
Mark Brownf04209a2012-04-09 16:29:21 +01001796 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001797 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001798 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001799 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001800 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001801 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001802 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001803 }
1804
1805 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001806 if (dai_fmt &&
1807 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001808 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1809 dai_fmt);
1810 if (ret != 0 && ret != -ENOTSUPP)
1811 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001812 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001813 ret);
1814 } else if (dai_fmt) {
1815 /* Flip the polarity for the "CPU" end */
1816 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1817 switch (dai_link->dai_fmt &
1818 SND_SOC_DAIFMT_MASTER_MASK) {
1819 case SND_SOC_DAIFMT_CBM_CFM:
1820 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1821 break;
1822 case SND_SOC_DAIFMT_CBM_CFS:
1823 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1824 break;
1825 case SND_SOC_DAIFMT_CBS_CFM:
1826 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1827 break;
1828 case SND_SOC_DAIFMT_CBS_CFS:
1829 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1830 break;
1831 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001832
1833 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001834 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001835 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001836 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001837 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001838 ret);
1839 }
1840 }
1841
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001842 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001843 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001844 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1845 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001846 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1847 "%s", card->driver_name ? card->driver_name : card->name);
1848 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1849 switch (card->snd_card->driver[i]) {
1850 case '_':
1851 case '-':
1852 case '\0':
1853 break;
1854 default:
1855 if (!isalnum(card->snd_card->driver[i]))
1856 card->snd_card->driver[i] = '_';
1857 break;
1858 }
1859 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001860
Mark Brown28e9ad92011-03-02 18:36:34 +00001861 if (card->late_probe) {
1862 ret = card->late_probe(card);
1863 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001864 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001865 card->name, ret);
1866 goto probe_aux_dev_err;
1867 }
1868 }
1869
Stephen Warren16332812011-11-23 12:42:04 -07001870 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001871 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001872 snd_soc_dapm_auto_nc_codec_pins(codec);
1873
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001874 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001875
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001876 ret = snd_card_register(card->snd_card);
1877 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001878 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1879 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001880 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001881 }
1882
1883#ifdef CONFIG_SND_SOC_AC97_BUS
1884 /* register any AC97 codecs */
1885 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001886 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1887 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001888 dev_err(card->dev,
1889 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001890 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001891 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001892 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001893 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001894 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001895#endif
1896
Mark Brown435c5e22008-12-04 15:32:53 +00001897 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001898 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001899 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001900
1901 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001902
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001903probe_aux_dev_err:
1904 for (i = 0; i < card->num_aux_devs; i++)
1905 soc_remove_aux_dev(card, i);
1906
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001907probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001908 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001909
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001910card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001911 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001912 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001913
1914 snd_card_free(card->snd_card);
1915
Mark Brownb19e6e72012-03-14 21:18:39 +00001916base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001917 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001918
Mark Brownb19e6e72012-03-14 21:18:39 +00001919 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001920}
1921
1922/* probes a new socdev */
1923static int soc_probe(struct platform_device *pdev)
1924{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001925 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001926
Vinod Koul70a7ca32011-01-14 19:22:48 +05301927 /*
1928 * no card, so machine driver should be registering card
1929 * we should not be here in that case so ret error
1930 */
1931 if (!card)
1932 return -EINVAL;
1933
Mark Brownfe4085e2012-03-02 13:07:41 +00001934 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001935 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001936 card->name);
1937
Mark Brown435c5e22008-12-04 15:32:53 +00001938 /* Bodge while we unpick instantiation */
1939 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001940
Mark Brown28d528c2012-08-09 18:45:23 +01001941 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001942}
1943
Vinod Koulb0e26482011-01-13 22:48:02 +05301944static int soc_cleanup_card_resources(struct snd_soc_card *card)
1945{
Vinod Koulb0e26482011-01-13 22:48:02 +05301946 int i;
1947
1948 /* make sure any delayed work runs */
1949 for (i = 0; i < card->num_rtd; i++) {
1950 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001951 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301952 }
1953
1954 /* remove auxiliary devices */
1955 for (i = 0; i < card->num_aux_devs; i++)
1956 soc_remove_aux_dev(card, i);
1957
1958 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001959 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301960
1961 soc_cleanup_card_debugfs(card);
1962
1963 /* remove the card */
1964 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001965 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301966
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001967 snd_soc_dapm_free(&card->dapm);
1968
Vinod Koulb0e26482011-01-13 22:48:02 +05301969 snd_card_free(card->snd_card);
1970 return 0;
1971
1972}
1973
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001974/* removes a socdev */
1975static int soc_remove(struct platform_device *pdev)
1976{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001977 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001978
Mark Brownc5af3a22008-11-28 13:29:45 +00001979 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001980 return 0;
1981}
1982
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001983int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001984{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001985 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001986 int i;
Mark Brown51737472009-06-22 13:16:51 +01001987
1988 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001989 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001990
1991 /* Flush out pmdown_time work - we actually do want to run it
1992 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001993 for (i = 0; i < card->num_rtd; i++) {
1994 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001995 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001996 }
Mark Brown51737472009-06-22 13:16:51 +01001997
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001998 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001999
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002000 /* deactivate pins to sleep state */
2001 for (i = 0; i < card->num_rtd; i++) {
2002 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
2003 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
2004 pinctrl_pm_select_sleep_state(codec_dai->dev);
2005 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2006 }
2007
Mark Brown416356f2009-06-30 19:05:15 +01002008 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002009}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002010EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002011
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002012const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302013 .suspend = snd_soc_suspend,
2014 .resume = snd_soc_resume,
2015 .freeze = snd_soc_suspend,
2016 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002017 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302018 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002019};
Stephen Warrendeb26072011-04-05 19:35:30 -06002020EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002021
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002022/* ASoC platform driver */
2023static struct platform_driver soc_driver = {
2024 .driver = {
2025 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002026 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002027 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002028 },
2029 .probe = soc_probe,
2030 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002031};
2032
Mark Brown096e49d2009-07-05 15:12:22 +01002033/**
2034 * snd_soc_codec_volatile_register: Report if a register is volatile.
2035 *
2036 * @codec: CODEC to query.
2037 * @reg: Register to query.
2038 *
2039 * Boolean function indiciating if a CODEC register is volatile.
2040 */
Mark Brown181e0552011-01-24 14:05:25 +00002041int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
2042 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01002043{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00002044 if (codec->volatile_register)
2045 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01002046 else
2047 return 0;
2048}
2049EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2050
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002051/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002052 * snd_soc_codec_readable_register: Report if a register is readable.
2053 *
2054 * @codec: CODEC to query.
2055 * @reg: Register to query.
2056 *
2057 * Boolean function indicating if a CODEC register is readable.
2058 */
2059int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
2060 unsigned int reg)
2061{
2062 if (codec->readable_register)
2063 return codec->readable_register(codec, reg);
2064 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02002065 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002066}
2067EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
2068
2069/**
2070 * snd_soc_codec_writable_register: Report if a register is writable.
2071 *
2072 * @codec: CODEC to query.
2073 * @reg: Register to query.
2074 *
2075 * Boolean function indicating if a CODEC register is writable.
2076 */
2077int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2078 unsigned int reg)
2079{
2080 if (codec->writable_register)
2081 return codec->writable_register(codec, reg);
2082 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02002083 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002084}
2085EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2086
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002087int snd_soc_platform_read(struct snd_soc_platform *platform,
2088 unsigned int reg)
2089{
2090 unsigned int ret;
2091
2092 if (!platform->driver->read) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002093 dev_err(platform->dev, "ASoC: platform has no read back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002094 return -1;
2095 }
2096
2097 ret = platform->driver->read(platform, reg);
2098 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002099 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002100
2101 return ret;
2102}
2103EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2104
2105int snd_soc_platform_write(struct snd_soc_platform *platform,
2106 unsigned int reg, unsigned int val)
2107{
2108 if (!platform->driver->write) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002109 dev_err(platform->dev, "ASoC: platform has no write back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002110 return -1;
2111 }
2112
2113 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002114 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002115 return platform->driver->write(platform, reg, val);
2116}
2117EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2118
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002119/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002120 * snd_soc_new_ac97_codec - initailise AC97 device
2121 * @codec: audio codec
2122 * @ops: AC97 bus operations
2123 * @num: AC97 codec number
2124 *
2125 * Initialises AC97 codec resources for use by ad-hoc devices only.
2126 */
2127int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2128 struct snd_ac97_bus_ops *ops, int num)
2129{
2130 mutex_lock(&codec->mutex);
2131
2132 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2133 if (codec->ac97 == NULL) {
2134 mutex_unlock(&codec->mutex);
2135 return -ENOMEM;
2136 }
2137
2138 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2139 if (codec->ac97->bus == NULL) {
2140 kfree(codec->ac97);
2141 codec->ac97 = NULL;
2142 mutex_unlock(&codec->mutex);
2143 return -ENOMEM;
2144 }
2145
2146 codec->ac97->bus->ops = ops;
2147 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002148
2149 /*
2150 * Mark the AC97 device to be created by us. This way we ensure that the
2151 * device will be registered with the device subsystem later on.
2152 */
2153 codec->ac97_created = 1;
2154
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002155 mutex_unlock(&codec->mutex);
2156 return 0;
2157}
2158EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2159
Markus Pargmann741a5092013-08-19 17:05:55 +02002160static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2161
2162static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2163{
2164 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2165
2166 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2167
2168 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2169
2170 udelay(10);
2171
2172 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2173
2174 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2175 msleep(2);
2176}
2177
2178static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2179{
2180 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2181
2182 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2183
2184 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2185 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2186 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2187
2188 udelay(10);
2189
2190 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2191
2192 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2193 msleep(2);
2194}
2195
2196static int snd_soc_ac97_parse_pinctl(struct device *dev,
2197 struct snd_ac97_reset_cfg *cfg)
2198{
2199 struct pinctrl *p;
2200 struct pinctrl_state *state;
2201 int gpio;
2202 int ret;
2203
2204 p = devm_pinctrl_get(dev);
2205 if (IS_ERR(p)) {
2206 dev_err(dev, "Failed to get pinctrl\n");
2207 return PTR_RET(p);
2208 }
2209 cfg->pctl = p;
2210
2211 state = pinctrl_lookup_state(p, "ac97-reset");
2212 if (IS_ERR(state)) {
2213 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2214 return PTR_RET(state);
2215 }
2216 cfg->pstate_reset = state;
2217
2218 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2219 if (IS_ERR(state)) {
2220 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2221 return PTR_RET(state);
2222 }
2223 cfg->pstate_warm_reset = state;
2224
2225 state = pinctrl_lookup_state(p, "ac97-running");
2226 if (IS_ERR(state)) {
2227 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2228 return PTR_RET(state);
2229 }
2230 cfg->pstate_run = state;
2231
2232 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2233 if (gpio < 0) {
2234 dev_err(dev, "Can't find ac97-sync gpio\n");
2235 return gpio;
2236 }
2237 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2238 if (ret) {
2239 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2240 return ret;
2241 }
2242 cfg->gpio_sync = gpio;
2243
2244 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2245 if (gpio < 0) {
2246 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2247 return gpio;
2248 }
2249 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2250 if (ret) {
2251 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2252 return ret;
2253 }
2254 cfg->gpio_sdata = gpio;
2255
2256 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2257 if (gpio < 0) {
2258 dev_err(dev, "Can't find ac97-reset gpio\n");
2259 return gpio;
2260 }
2261 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2262 if (ret) {
2263 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2264 return ret;
2265 }
2266 cfg->gpio_reset = gpio;
2267
2268 return 0;
2269}
2270
Mark Brownb047e1c2013-06-26 12:45:59 +01002271struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002272EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002273
2274int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2275{
2276 if (ops == soc_ac97_ops)
2277 return 0;
2278
2279 if (soc_ac97_ops && ops)
2280 return -EBUSY;
2281
2282 soc_ac97_ops = ops;
2283
2284 return 0;
2285}
2286EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2287
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002288/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002289 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2290 *
2291 * This function sets the reset and warm_reset properties of ops and parses
2292 * the device node of pdev to get pinctrl states and gpio numbers to use.
2293 */
2294int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2295 struct platform_device *pdev)
2296{
2297 struct device *dev = &pdev->dev;
2298 struct snd_ac97_reset_cfg cfg;
2299 int ret;
2300
2301 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2302 if (ret)
2303 return ret;
2304
2305 ret = snd_soc_set_ac97_ops(ops);
2306 if (ret)
2307 return ret;
2308
2309 ops->warm_reset = snd_soc_ac97_warm_reset;
2310 ops->reset = snd_soc_ac97_reset;
2311
2312 snd_ac97_rst_cfg = cfg;
2313 return 0;
2314}
2315EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2316
2317/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002318 * snd_soc_free_ac97_codec - free AC97 codec device
2319 * @codec: audio codec
2320 *
2321 * Frees AC97 codec device resources.
2322 */
2323void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2324{
2325 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002326#ifdef CONFIG_SND_SOC_AC97_BUS
2327 soc_unregister_ac97_dai_link(codec);
2328#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002329 kfree(codec->ac97->bus);
2330 kfree(codec->ac97);
2331 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002332 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002333 mutex_unlock(&codec->mutex);
2334}
2335EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2336
Mark Brownc3753702010-11-01 15:41:57 -04002337unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2338{
2339 unsigned int ret;
2340
Mark Brownc3acec22010-12-02 16:15:29 +00002341 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002342 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002343 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002344
2345 return ret;
2346}
2347EXPORT_SYMBOL_GPL(snd_soc_read);
2348
2349unsigned int snd_soc_write(struct snd_soc_codec *codec,
2350 unsigned int reg, unsigned int val)
2351{
2352 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002353 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002354 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002355}
2356EXPORT_SYMBOL_GPL(snd_soc_write);
2357
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002358/**
2359 * snd_soc_update_bits - update codec register bits
2360 * @codec: audio codec
2361 * @reg: codec register
2362 * @mask: register mask
2363 * @value: new value
2364 *
2365 * Writes new register value.
2366 *
Timur Tabi180c3292011-01-10 15:58:13 -06002367 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002368 */
2369int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002370 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002371{
Mark Brown8a713da2011-12-03 12:33:55 +00002372 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002373 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002374 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002375
Mark Brown8a713da2011-12-03 12:33:55 +00002376 if (codec->using_regmap) {
2377 ret = regmap_update_bits_check(codec->control_data, reg,
2378 mask, value, &change);
2379 } else {
2380 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002381 if (ret < 0)
2382 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002383
2384 old = ret;
2385 new = (old & ~mask) | (value & mask);
2386 change = old != new;
2387 if (change)
2388 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002389 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002390
Mark Brown8a713da2011-12-03 12:33:55 +00002391 if (ret < 0)
2392 return ret;
2393
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002394 return change;
2395}
2396EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2397
2398/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002399 * snd_soc_update_bits_locked - update codec register bits
2400 * @codec: audio codec
2401 * @reg: codec register
2402 * @mask: register mask
2403 * @value: new value
2404 *
2405 * Writes new register value, and takes the codec mutex.
2406 *
2407 * Returns 1 for change else 0.
2408 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002409int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2410 unsigned short reg, unsigned int mask,
2411 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002412{
2413 int change;
2414
2415 mutex_lock(&codec->mutex);
2416 change = snd_soc_update_bits(codec, reg, mask, value);
2417 mutex_unlock(&codec->mutex);
2418
2419 return change;
2420}
Mark Browndd1b3d52009-12-04 14:22:03 +00002421EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002422
2423/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002424 * snd_soc_test_bits - test register for change
2425 * @codec: audio codec
2426 * @reg: codec register
2427 * @mask: register mask
2428 * @value: new value
2429 *
2430 * Tests a register with a new value and checks if the new value is
2431 * different from the old value.
2432 *
2433 * Returns 1 for change else 0.
2434 */
2435int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002436 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002437{
2438 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002439 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002440
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002441 old = snd_soc_read(codec, reg);
2442 new = (old & ~mask) | value;
2443 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002444
2445 return change;
2446}
2447EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2448
2449/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002450 * snd_soc_cnew - create new control
2451 * @_template: control template
2452 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002453 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002454 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002455 *
2456 * Create a new mixer control from a template control.
2457 *
2458 * Returns 0 for success, else error.
2459 */
2460struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002461 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002462 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002463{
2464 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002465 struct snd_kcontrol *kcontrol;
2466 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002467
2468 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002469 template.index = 0;
2470
Mark Brownefb7ac32011-03-08 17:23:24 +00002471 if (!long_name)
2472 long_name = template.name;
2473
2474 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002475 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002476 if (!name)
2477 return NULL;
2478
Mark Brownefb7ac32011-03-08 17:23:24 +00002479 template.name = name;
2480 } else {
2481 template.name = long_name;
2482 }
2483
2484 kcontrol = snd_ctl_new1(&template, data);
2485
2486 kfree(name);
2487
2488 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002489}
2490EXPORT_SYMBOL_GPL(snd_soc_cnew);
2491
Liam Girdwood022658b2012-02-03 17:43:09 +00002492static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2493 const struct snd_kcontrol_new *controls, int num_controls,
2494 const char *prefix, void *data)
2495{
2496 int err, i;
2497
2498 for (i = 0; i < num_controls; i++) {
2499 const struct snd_kcontrol_new *control = &controls[i];
2500 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2501 control->name, prefix));
2502 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002503 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2504 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002505 return err;
2506 }
2507 }
2508
2509 return 0;
2510}
2511
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002512struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2513 const char *name)
2514{
2515 struct snd_card *card = soc_card->snd_card;
2516 struct snd_kcontrol *kctl;
2517
2518 if (unlikely(!name))
2519 return NULL;
2520
2521 list_for_each_entry(kctl, &card->controls, list)
2522 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2523 return kctl;
2524 return NULL;
2525}
2526EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2527
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002528/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002529 * snd_soc_add_codec_controls - add an array of controls to a codec.
2530 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002531 * duplicating this code.
2532 *
2533 * @codec: codec to add controls to
2534 * @controls: array of controls to add
2535 * @num_controls: number of elements in the array
2536 *
2537 * Return 0 for success, else error.
2538 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002539int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002540 const struct snd_kcontrol_new *controls, int num_controls)
2541{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002542 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002543
Liam Girdwood022658b2012-02-03 17:43:09 +00002544 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2545 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002546}
Liam Girdwood022658b2012-02-03 17:43:09 +00002547EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002548
2549/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002550 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002551 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002552 *
2553 * @platform: platform to add controls to
2554 * @controls: array of controls to add
2555 * @num_controls: number of elements in the array
2556 *
2557 * Return 0 for success, else error.
2558 */
2559int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2560 const struct snd_kcontrol_new *controls, int num_controls)
2561{
2562 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002563
Liam Girdwood022658b2012-02-03 17:43:09 +00002564 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2565 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002566}
2567EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2568
2569/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002570 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2571 * Convenience function to add a list of controls.
2572 *
2573 * @soc_card: SoC card to add controls to
2574 * @controls: array of controls to add
2575 * @num_controls: number of elements in the array
2576 *
2577 * Return 0 for success, else error.
2578 */
2579int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2580 const struct snd_kcontrol_new *controls, int num_controls)
2581{
2582 struct snd_card *card = soc_card->snd_card;
2583
2584 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2585 NULL, soc_card);
2586}
2587EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2588
2589/**
2590 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2591 * Convienience function to add a list of controls.
2592 *
2593 * @dai: DAI to add controls to
2594 * @controls: array of controls to add
2595 * @num_controls: number of elements in the array
2596 *
2597 * Return 0 for success, else error.
2598 */
2599int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2600 const struct snd_kcontrol_new *controls, int num_controls)
2601{
2602 struct snd_card *card = dai->card->snd_card;
2603
2604 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2605 NULL, dai);
2606}
2607EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2608
2609/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002610 * snd_soc_info_enum_double - enumerated double mixer info callback
2611 * @kcontrol: mixer control
2612 * @uinfo: control element information
2613 *
2614 * Callback to provide information about a double enumerated
2615 * mixer control.
2616 *
2617 * Returns 0 for success.
2618 */
2619int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2620 struct snd_ctl_elem_info *uinfo)
2621{
2622 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2623
2624 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2625 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002626 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002627
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002628 if (uinfo->value.enumerated.item >= e->items)
2629 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002630 strlcpy(uinfo->value.enumerated.name,
2631 e->texts[uinfo->value.enumerated.item],
2632 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002633 return 0;
2634}
2635EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2636
2637/**
2638 * snd_soc_get_enum_double - enumerated double mixer get callback
2639 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002640 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002641 *
2642 * Callback to get the value of a double enumerated mixer.
2643 *
2644 * Returns 0 for success.
2645 */
2646int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2647 struct snd_ctl_elem_value *ucontrol)
2648{
2649 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2650 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002651 unsigned int val, item;
2652 unsigned int reg_val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002653
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002654 reg_val = snd_soc_read(codec, e->reg);
2655 val = (reg_val >> e->shift_l) & e->mask;
2656 item = snd_soc_enum_val_to_item(e, val);
2657 ucontrol->value.enumerated.item[0] = item;
2658 if (e->shift_l != e->shift_r) {
2659 val = (reg_val >> e->shift_l) & e->mask;
2660 item = snd_soc_enum_val_to_item(e, val);
2661 ucontrol->value.enumerated.item[1] = item;
2662 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002663
2664 return 0;
2665}
2666EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2667
2668/**
2669 * snd_soc_put_enum_double - enumerated double mixer put callback
2670 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002671 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002672 *
2673 * Callback to set the value of a double enumerated mixer.
2674 *
2675 * Returns 0 for success.
2676 */
2677int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2678 struct snd_ctl_elem_value *ucontrol)
2679{
2680 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2681 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002682 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002683 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002684 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002685
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002686 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002687 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002688 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002689 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002690 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002691 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002692 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002693 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002694 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002695 }
2696
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002697 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002698}
2699EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2700
2701/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002702 * snd_soc_read_signed - Read a codec register and interprete as signed value
2703 * @codec: codec
2704 * @reg: Register to read
2705 * @mask: Mask to use after shifting the register value
2706 * @shift: Right shift of register value
2707 * @sign_bit: Bit that describes if a number is negative or not.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002708 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002709 * This functions reads a codec register. The register value is shifted right
2710 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2711 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002712 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002713 * Returns the register value as signed int.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002714 */
Markus Pargmannf227b882014-01-16 16:02:10 +01002715static int snd_soc_read_signed(struct snd_soc_codec *codec, unsigned int reg,
2716 unsigned int mask, unsigned int shift, unsigned int sign_bit)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002717{
Markus Pargmannf227b882014-01-16 16:02:10 +01002718 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002719 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002720
Markus Pargmannf227b882014-01-16 16:02:10 +01002721 val = (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002722
Markus Pargmannf227b882014-01-16 16:02:10 +01002723 if (!sign_bit)
2724 return val;
2725
2726 /* non-negative number */
2727 if (!(val & BIT(sign_bit)))
2728 return val;
2729
2730 ret = val;
2731
2732 /*
2733 * The register most probably does not contain a full-sized int.
2734 * Instead we have an arbitrary number of bits in a signed
2735 * representation which has to be translated into a full-sized int.
2736 * This is done by filling up all bits above the sign-bit.
2737 */
2738 ret |= ~((int)(BIT(sign_bit) - 1));
2739
2740 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002741}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002742
2743/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002744 * snd_soc_info_volsw - single mixer info callback
2745 * @kcontrol: mixer control
2746 * @uinfo: control element information
2747 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002748 * Callback to provide information about a single mixer control, or a double
2749 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002750 *
2751 * Returns 0 for success.
2752 */
2753int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2754 struct snd_ctl_elem_info *uinfo)
2755{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002756 struct soc_mixer_control *mc =
2757 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002758 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002759
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002760 if (!mc->platform_max)
2761 mc->platform_max = mc->max;
2762 platform_max = mc->platform_max;
2763
2764 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002765 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2766 else
2767 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2768
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002769 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002770 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002771 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002772 return 0;
2773}
2774EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2775
2776/**
2777 * snd_soc_get_volsw - single mixer get callback
2778 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002779 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002780 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002781 * Callback to get the value of a single mixer control, or a double mixer
2782 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002783 *
2784 * Returns 0 for success.
2785 */
2786int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2787 struct snd_ctl_elem_value *ucontrol)
2788{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002789 struct soc_mixer_control *mc =
2790 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002791 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002792 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002793 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002794 unsigned int shift = mc->shift;
2795 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002796 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002797 int min = mc->min;
2798 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002799 unsigned int mask = (1 << fls(max)) - 1;
2800 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002801
Markus Pargmannf227b882014-01-16 16:02:10 +01002802 if (sign_bit)
2803 mask = BIT(sign_bit + 1) - 1;
2804
2805 ucontrol->value.integer.value[0] = snd_soc_read_signed(codec, reg, mask,
2806 shift, sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002807 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002808 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002809 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002810
2811 if (snd_soc_volsw_is_stereo(mc)) {
2812 if (reg == reg2)
2813 ucontrol->value.integer.value[1] =
Markus Pargmannf227b882014-01-16 16:02:10 +01002814 snd_soc_read_signed(codec, reg, mask, rshift,
2815 sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002816 else
2817 ucontrol->value.integer.value[1] =
Markus Pargmannf227b882014-01-16 16:02:10 +01002818 snd_soc_read_signed(codec, reg2, mask, shift,
2819 sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002820 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002821 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002822 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002823 }
2824
2825 return 0;
2826}
2827EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2828
2829/**
2830 * snd_soc_put_volsw - single mixer put callback
2831 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002832 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002833 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002834 * Callback to set the value of a single mixer control, or a double mixer
2835 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002836 *
2837 * Returns 0 for success.
2838 */
2839int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2840 struct snd_ctl_elem_value *ucontrol)
2841{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002842 struct soc_mixer_control *mc =
2843 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002844 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002845 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002846 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002847 unsigned int shift = mc->shift;
2848 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002849 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002850 int min = mc->min;
2851 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002852 unsigned int mask = (1 << fls(max)) - 1;
2853 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002854 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002855 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002856 unsigned int val2 = 0;
2857 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002858
Markus Pargmannf227b882014-01-16 16:02:10 +01002859 if (sign_bit)
2860 mask = BIT(sign_bit + 1) - 1;
2861
2862 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002863 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002864 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002865 val_mask = mask << shift;
2866 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002867 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002868 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002869 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002870 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002871 if (reg == reg2) {
2872 val_mask |= mask << rshift;
2873 val |= val2 << rshift;
2874 } else {
2875 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002876 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002877 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002878 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002879 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002880 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002881 return err;
2882
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002883 if (type_2r)
2884 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2885
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002886 return err;
2887}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002888EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002889
Mark Browne13ac2e2008-05-28 17:58:05 +01002890/**
Brian Austin1d99f242012-03-30 10:43:55 -05002891 * snd_soc_get_volsw_sx - single mixer get callback
2892 * @kcontrol: mixer control
2893 * @ucontrol: control element information
2894 *
2895 * Callback to get the value of a single mixer control, or a double mixer
2896 * control that spans 2 registers.
2897 *
2898 * Returns 0 for success.
2899 */
2900int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2901 struct snd_ctl_elem_value *ucontrol)
2902{
2903 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2904 struct soc_mixer_control *mc =
2905 (struct soc_mixer_control *)kcontrol->private_value;
2906
2907 unsigned int reg = mc->reg;
2908 unsigned int reg2 = mc->rreg;
2909 unsigned int shift = mc->shift;
2910 unsigned int rshift = mc->rshift;
2911 int max = mc->max;
2912 int min = mc->min;
2913 int mask = (1 << (fls(min + max) - 1)) - 1;
2914
2915 ucontrol->value.integer.value[0] =
2916 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2917
2918 if (snd_soc_volsw_is_stereo(mc))
2919 ucontrol->value.integer.value[1] =
2920 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2921
2922 return 0;
2923}
2924EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2925
2926/**
2927 * snd_soc_put_volsw_sx - double mixer set callback
2928 * @kcontrol: mixer control
2929 * @uinfo: control element information
2930 *
2931 * Callback to set the value of a double mixer control that spans 2 registers.
2932 *
2933 * Returns 0 for success.
2934 */
2935int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2936 struct snd_ctl_elem_value *ucontrol)
2937{
2938 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2939 struct soc_mixer_control *mc =
2940 (struct soc_mixer_control *)kcontrol->private_value;
2941
2942 unsigned int reg = mc->reg;
2943 unsigned int reg2 = mc->rreg;
2944 unsigned int shift = mc->shift;
2945 unsigned int rshift = mc->rshift;
2946 int max = mc->max;
2947 int min = mc->min;
2948 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002949 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002950 unsigned short val, val_mask, val2 = 0;
2951
2952 val_mask = mask << shift;
2953 val = (ucontrol->value.integer.value[0] + min) & mask;
2954 val = val << shift;
2955
Mukund Navadad0558522012-11-09 11:53:40 +05302956 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2957 if (err < 0)
2958 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002959
2960 if (snd_soc_volsw_is_stereo(mc)) {
2961 val_mask = mask << rshift;
2962 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2963 val2 = val2 << rshift;
2964
2965 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2966 return err;
2967 }
2968 return 0;
2969}
2970EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2971
2972/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002973 * snd_soc_info_volsw_s8 - signed mixer info callback
2974 * @kcontrol: mixer control
2975 * @uinfo: control element information
2976 *
2977 * Callback to provide information about a signed mixer control.
2978 *
2979 * Returns 0 for success.
2980 */
2981int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2982 struct snd_ctl_elem_info *uinfo)
2983{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002984 struct soc_mixer_control *mc =
2985 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002986 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002987 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002988
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002989 if (!mc->platform_max)
2990 mc->platform_max = mc->max;
2991 platform_max = mc->platform_max;
2992
Mark Browne13ac2e2008-05-28 17:58:05 +01002993 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2994 uinfo->count = 2;
2995 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002996 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002997 return 0;
2998}
2999EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
3000
3001/**
3002 * snd_soc_get_volsw_s8 - signed mixer get callback
3003 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003004 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01003005 *
3006 * Callback to get the value of a signed mixer control.
3007 *
3008 * Returns 0 for success.
3009 */
3010int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
3011 struct snd_ctl_elem_value *ucontrol)
3012{
Jon Smirl4eaa9812008-07-29 11:42:26 +01003013 struct soc_mixer_control *mc =
3014 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01003015 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003016 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003017 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01003018 int val = snd_soc_read(codec, reg);
3019
3020 ucontrol->value.integer.value[0] =
3021 ((signed char)(val & 0xff))-min;
3022 ucontrol->value.integer.value[1] =
3023 ((signed char)((val >> 8) & 0xff))-min;
3024 return 0;
3025}
3026EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
3027
3028/**
3029 * snd_soc_put_volsw_sgn - signed mixer put callback
3030 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003031 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01003032 *
3033 * Callback to set the value of a signed mixer control.
3034 *
3035 * Returns 0 for success.
3036 */
3037int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
3038 struct snd_ctl_elem_value *ucontrol)
3039{
Jon Smirl4eaa9812008-07-29 11:42:26 +01003040 struct soc_mixer_control *mc =
3041 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01003042 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003043 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003044 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003045 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01003046
3047 val = (ucontrol->value.integer.value[0]+min) & 0xff;
3048 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
3049
Eero Nurkkala6c508c62009-10-30 13:34:03 +02003050 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01003051}
3052EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
3053
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003054/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003055 * snd_soc_info_volsw_range - single mixer info callback with range.
3056 * @kcontrol: mixer control
3057 * @uinfo: control element information
3058 *
3059 * Callback to provide information, within a range, about a single
3060 * mixer control.
3061 *
3062 * returns 0 for success.
3063 */
3064int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
3065 struct snd_ctl_elem_info *uinfo)
3066{
3067 struct soc_mixer_control *mc =
3068 (struct soc_mixer_control *)kcontrol->private_value;
3069 int platform_max;
3070 int min = mc->min;
3071
3072 if (!mc->platform_max)
3073 mc->platform_max = mc->max;
3074 platform_max = mc->platform_max;
3075
3076 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00003077 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003078 uinfo->value.integer.min = 0;
3079 uinfo->value.integer.max = platform_max - min;
3080
3081 return 0;
3082}
3083EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
3084
3085/**
3086 * snd_soc_put_volsw_range - single mixer put value callback with range.
3087 * @kcontrol: mixer control
3088 * @ucontrol: control element information
3089 *
3090 * Callback to set the value, within a range, for a single mixer control.
3091 *
3092 * Returns 0 for success.
3093 */
3094int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
3095 struct snd_ctl_elem_value *ucontrol)
3096{
3097 struct soc_mixer_control *mc =
3098 (struct soc_mixer_control *)kcontrol->private_value;
3099 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3100 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003101 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003102 unsigned int shift = mc->shift;
3103 int min = mc->min;
3104 int max = mc->max;
3105 unsigned int mask = (1 << fls(max)) - 1;
3106 unsigned int invert = mc->invert;
3107 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003108 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003109
3110 val = ((ucontrol->value.integer.value[0] + min) & mask);
3111 if (invert)
3112 val = max - val;
3113 val_mask = mask << shift;
3114 val = val << shift;
3115
Mark Brown9bde4f02012-12-19 16:05:00 +00003116 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09003117 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00003118 return ret;
3119
3120 if (snd_soc_volsw_is_stereo(mc)) {
3121 val = ((ucontrol->value.integer.value[1] + min) & mask);
3122 if (invert)
3123 val = max - val;
3124 val_mask = mask << shift;
3125 val = val << shift;
3126
3127 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
3128 }
3129
3130 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003131}
3132EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3133
3134/**
3135 * snd_soc_get_volsw_range - single mixer get callback with range
3136 * @kcontrol: mixer control
3137 * @ucontrol: control element information
3138 *
3139 * Callback to get the value, within a range, of a single mixer control.
3140 *
3141 * Returns 0 for success.
3142 */
3143int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3144 struct snd_ctl_elem_value *ucontrol)
3145{
3146 struct soc_mixer_control *mc =
3147 (struct soc_mixer_control *)kcontrol->private_value;
3148 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3149 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003150 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003151 unsigned int shift = mc->shift;
3152 int min = mc->min;
3153 int max = mc->max;
3154 unsigned int mask = (1 << fls(max)) - 1;
3155 unsigned int invert = mc->invert;
3156
3157 ucontrol->value.integer.value[0] =
3158 (snd_soc_read(codec, reg) >> shift) & mask;
3159 if (invert)
3160 ucontrol->value.integer.value[0] =
3161 max - ucontrol->value.integer.value[0];
3162 ucontrol->value.integer.value[0] =
3163 ucontrol->value.integer.value[0] - min;
3164
Mark Brown9bde4f02012-12-19 16:05:00 +00003165 if (snd_soc_volsw_is_stereo(mc)) {
3166 ucontrol->value.integer.value[1] =
3167 (snd_soc_read(codec, rreg) >> shift) & mask;
3168 if (invert)
3169 ucontrol->value.integer.value[1] =
3170 max - ucontrol->value.integer.value[1];
3171 ucontrol->value.integer.value[1] =
3172 ucontrol->value.integer.value[1] - min;
3173 }
3174
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003175 return 0;
3176}
3177EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3178
3179/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003180 * snd_soc_limit_volume - Set new limit to an existing volume control.
3181 *
3182 * @codec: where to look for the control
3183 * @name: Name of the control
3184 * @max: new maximum limit
3185 *
3186 * Return 0 for success, else error.
3187 */
3188int snd_soc_limit_volume(struct snd_soc_codec *codec,
3189 const char *name, int max)
3190{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003191 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003192 struct snd_kcontrol *kctl;
3193 struct soc_mixer_control *mc;
3194 int found = 0;
3195 int ret = -EINVAL;
3196
3197 /* Sanity check for name and max */
3198 if (unlikely(!name || max <= 0))
3199 return -EINVAL;
3200
3201 list_for_each_entry(kctl, &card->controls, list) {
3202 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3203 found = 1;
3204 break;
3205 }
3206 }
3207 if (found) {
3208 mc = (struct soc_mixer_control *)kctl->private_value;
3209 if (max <= mc->max) {
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03003210 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003211 ret = 0;
3212 }
3213 }
3214 return ret;
3215}
3216EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3217
Mark Brown71d08512011-10-10 18:31:26 +01003218int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3219 struct snd_ctl_elem_info *uinfo)
3220{
3221 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3222 struct soc_bytes *params = (void *)kcontrol->private_value;
3223
3224 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3225 uinfo->count = params->num_regs * codec->val_bytes;
3226
3227 return 0;
3228}
3229EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3230
3231int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3232 struct snd_ctl_elem_value *ucontrol)
3233{
3234 struct soc_bytes *params = (void *)kcontrol->private_value;
3235 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3236 int ret;
3237
3238 if (codec->using_regmap)
3239 ret = regmap_raw_read(codec->control_data, params->base,
3240 ucontrol->value.bytes.data,
3241 params->num_regs * codec->val_bytes);
3242 else
3243 ret = -EINVAL;
3244
Mark Brownf831b052012-02-17 16:20:33 -08003245 /* Hide any masked bytes to ensure consistent data reporting */
3246 if (ret == 0 && params->mask) {
3247 switch (codec->val_bytes) {
3248 case 1:
3249 ucontrol->value.bytes.data[0] &= ~params->mask;
3250 break;
3251 case 2:
3252 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003253 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003254 break;
3255 case 4:
3256 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003257 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003258 break;
3259 default:
3260 return -EINVAL;
3261 }
3262 }
3263
Mark Brown71d08512011-10-10 18:31:26 +01003264 return ret;
3265}
3266EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3267
3268int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3269 struct snd_ctl_elem_value *ucontrol)
3270{
3271 struct soc_bytes *params = (void *)kcontrol->private_value;
3272 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003273 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003274 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003275 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003276
Mark Brownf831b052012-02-17 16:20:33 -08003277 if (!codec->using_regmap)
3278 return -EINVAL;
3279
Mark Brownf831b052012-02-17 16:20:33 -08003280 len = params->num_regs * codec->val_bytes;
3281
Mark Brownb5a8fe42013-01-20 21:42:22 +09003282 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3283 if (!data)
3284 return -ENOMEM;
3285
Mark Brownf831b052012-02-17 16:20:33 -08003286 /*
3287 * If we've got a mask then we need to preserve the register
3288 * bits. We shouldn't modify the incoming data so take a
3289 * copy.
3290 */
3291 if (params->mask) {
3292 ret = regmap_read(codec->control_data, params->base, &val);
3293 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003294 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003295
3296 val &= params->mask;
3297
Mark Brownf831b052012-02-17 16:20:33 -08003298 switch (codec->val_bytes) {
3299 case 1:
3300 ((u8 *)data)[0] &= ~params->mask;
3301 ((u8 *)data)[0] |= val;
3302 break;
3303 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003304 mask = ~params->mask;
3305 ret = regmap_parse_val(codec->control_data,
3306 &mask, &mask);
3307 if (ret != 0)
3308 goto out;
3309
3310 ((u16 *)data)[0] &= mask;
3311
3312 ret = regmap_parse_val(codec->control_data,
3313 &val, &val);
3314 if (ret != 0)
3315 goto out;
3316
3317 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003318 break;
3319 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003320 mask = ~params->mask;
3321 ret = regmap_parse_val(codec->control_data,
3322 &mask, &mask);
3323 if (ret != 0)
3324 goto out;
3325
3326 ((u32 *)data)[0] &= mask;
3327
3328 ret = regmap_parse_val(codec->control_data,
3329 &val, &val);
3330 if (ret != 0)
3331 goto out;
3332
3333 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003334 break;
3335 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003336 ret = -EINVAL;
3337 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003338 }
3339 }
3340
3341 ret = regmap_raw_write(codec->control_data, params->base,
3342 data, len);
3343
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003344out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003345 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003346
3347 return ret;
3348}
3349EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3350
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003351/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003352 * snd_soc_info_xr_sx - signed multi register info callback
3353 * @kcontrol: mreg control
3354 * @uinfo: control element information
3355 *
3356 * Callback to provide information of a control that can
3357 * span multiple codec registers which together
3358 * forms a single signed value in a MSB/LSB manner.
3359 *
3360 * Returns 0 for success.
3361 */
3362int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3363 struct snd_ctl_elem_info *uinfo)
3364{
3365 struct soc_mreg_control *mc =
3366 (struct soc_mreg_control *)kcontrol->private_value;
3367 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3368 uinfo->count = 1;
3369 uinfo->value.integer.min = mc->min;
3370 uinfo->value.integer.max = mc->max;
3371
3372 return 0;
3373}
3374EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3375
3376/**
3377 * snd_soc_get_xr_sx - signed multi register get callback
3378 * @kcontrol: mreg control
3379 * @ucontrol: control element information
3380 *
3381 * Callback to get the value of a control that can span
3382 * multiple codec registers which together forms a single
3383 * signed value in a MSB/LSB manner. The control supports
3384 * specifying total no of bits used to allow for bitfields
3385 * across the multiple codec registers.
3386 *
3387 * Returns 0 for success.
3388 */
3389int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3390 struct snd_ctl_elem_value *ucontrol)
3391{
3392 struct soc_mreg_control *mc =
3393 (struct soc_mreg_control *)kcontrol->private_value;
3394 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3395 unsigned int regbase = mc->regbase;
3396 unsigned int regcount = mc->regcount;
3397 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3398 unsigned int regwmask = (1<<regwshift)-1;
3399 unsigned int invert = mc->invert;
3400 unsigned long mask = (1UL<<mc->nbits)-1;
3401 long min = mc->min;
3402 long max = mc->max;
3403 long val = 0;
3404 unsigned long regval;
3405 unsigned int i;
3406
3407 for (i = 0; i < regcount; i++) {
3408 regval = snd_soc_read(codec, regbase+i) & regwmask;
3409 val |= regval << (regwshift*(regcount-i-1));
3410 }
3411 val &= mask;
3412 if (min < 0 && val > max)
3413 val |= ~mask;
3414 if (invert)
3415 val = max - val;
3416 ucontrol->value.integer.value[0] = val;
3417
3418 return 0;
3419}
3420EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3421
3422/**
3423 * snd_soc_put_xr_sx - signed multi register get callback
3424 * @kcontrol: mreg control
3425 * @ucontrol: control element information
3426 *
3427 * Callback to set the value of a control that can span
3428 * multiple codec registers which together forms a single
3429 * signed value in a MSB/LSB manner. The control supports
3430 * specifying total no of bits used to allow for bitfields
3431 * across the multiple codec registers.
3432 *
3433 * Returns 0 for success.
3434 */
3435int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3436 struct snd_ctl_elem_value *ucontrol)
3437{
3438 struct soc_mreg_control *mc =
3439 (struct soc_mreg_control *)kcontrol->private_value;
3440 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3441 unsigned int regbase = mc->regbase;
3442 unsigned int regcount = mc->regcount;
3443 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3444 unsigned int regwmask = (1<<regwshift)-1;
3445 unsigned int invert = mc->invert;
3446 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003447 long max = mc->max;
3448 long val = ucontrol->value.integer.value[0];
3449 unsigned int i, regval, regmask;
3450 int err;
3451
3452 if (invert)
3453 val = max - val;
3454 val &= mask;
3455 for (i = 0; i < regcount; i++) {
3456 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3457 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3458 err = snd_soc_update_bits_locked(codec, regbase+i,
3459 regmask, regval);
3460 if (err < 0)
3461 return err;
3462 }
3463
3464 return 0;
3465}
3466EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3467
3468/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003469 * snd_soc_get_strobe - strobe get callback
3470 * @kcontrol: mixer control
3471 * @ucontrol: control element information
3472 *
3473 * Callback get the value of a strobe mixer control.
3474 *
3475 * Returns 0 for success.
3476 */
3477int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3478 struct snd_ctl_elem_value *ucontrol)
3479{
3480 struct soc_mixer_control *mc =
3481 (struct soc_mixer_control *)kcontrol->private_value;
3482 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3483 unsigned int reg = mc->reg;
3484 unsigned int shift = mc->shift;
3485 unsigned int mask = 1 << shift;
3486 unsigned int invert = mc->invert != 0;
3487 unsigned int val = snd_soc_read(codec, reg) & mask;
3488
3489 if (shift != 0 && val != 0)
3490 val = val >> shift;
3491 ucontrol->value.enumerated.item[0] = val ^ invert;
3492
3493 return 0;
3494}
3495EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3496
3497/**
3498 * snd_soc_put_strobe - strobe put callback
3499 * @kcontrol: mixer control
3500 * @ucontrol: control element information
3501 *
3502 * Callback strobe a register bit to high then low (or the inverse)
3503 * in one pass of a single mixer enum control.
3504 *
3505 * Returns 1 for success.
3506 */
3507int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3508 struct snd_ctl_elem_value *ucontrol)
3509{
3510 struct soc_mixer_control *mc =
3511 (struct soc_mixer_control *)kcontrol->private_value;
3512 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3513 unsigned int reg = mc->reg;
3514 unsigned int shift = mc->shift;
3515 unsigned int mask = 1 << shift;
3516 unsigned int invert = mc->invert != 0;
3517 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3518 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3519 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3520 int err;
3521
3522 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3523 if (err < 0)
3524 return err;
3525
3526 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3527 return err;
3528}
3529EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3530
3531/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003532 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3533 * @dai: DAI
3534 * @clk_id: DAI specific clock ID
3535 * @freq: new clock frequency in Hz
3536 * @dir: new clock direction - input/output.
3537 *
3538 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3539 */
3540int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3541 unsigned int freq, int dir)
3542{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003543 if (dai->driver && dai->driver->ops->set_sysclk)
3544 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003545 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003546 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003547 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003548 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003549 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003550}
3551EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3552
3553/**
Mark Brownec4ee522011-03-07 20:58:11 +00003554 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3555 * @codec: CODEC
3556 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003557 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003558 * @freq: new clock frequency in Hz
3559 * @dir: new clock direction - input/output.
3560 *
3561 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3562 */
3563int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003564 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003565{
3566 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003567 return codec->driver->set_sysclk(codec, clk_id, source,
3568 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003569 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003570 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003571}
3572EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3573
3574/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003575 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3576 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003577 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003578 * @div: new clock divisor.
3579 *
3580 * Configures the clock dividers. This is used to derive the best DAI bit and
3581 * frame clocks from the system or master clock. It's best to set the DAI bit
3582 * and frame clocks as low as possible to save system power.
3583 */
3584int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3585 int div_id, int div)
3586{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003587 if (dai->driver && dai->driver->ops->set_clkdiv)
3588 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003589 else
3590 return -EINVAL;
3591}
3592EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3593
3594/**
3595 * snd_soc_dai_set_pll - configure DAI PLL.
3596 * @dai: DAI
3597 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003598 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003599 * @freq_in: PLL input clock frequency in Hz
3600 * @freq_out: requested PLL output clock frequency in Hz
3601 *
3602 * Configures and enables PLL to generate output clock based on input clock.
3603 */
Mark Brown85488032009-09-05 18:52:16 +01003604int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3605 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003606{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003607 if (dai->driver && dai->driver->ops->set_pll)
3608 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003609 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003610 else if (dai->codec && dai->codec->driver->set_pll)
3611 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3612 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003613 else
3614 return -EINVAL;
3615}
3616EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3617
Mark Brownec4ee522011-03-07 20:58:11 +00003618/*
3619 * snd_soc_codec_set_pll - configure codec PLL.
3620 * @codec: CODEC
3621 * @pll_id: DAI specific PLL ID
3622 * @source: DAI specific source for the PLL
3623 * @freq_in: PLL input clock frequency in Hz
3624 * @freq_out: requested PLL output clock frequency in Hz
3625 *
3626 * Configures and enables PLL to generate output clock based on input clock.
3627 */
3628int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3629 unsigned int freq_in, unsigned int freq_out)
3630{
3631 if (codec->driver->set_pll)
3632 return codec->driver->set_pll(codec, pll_id, source,
3633 freq_in, freq_out);
3634 else
3635 return -EINVAL;
3636}
3637EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3638
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003639/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003640 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3641 * @dai: DAI
3642 * @ratio Ratio of BCLK to Sample rate.
3643 *
3644 * Configures the DAI for a preset BCLK to sample rate ratio.
3645 */
3646int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3647{
3648 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3649 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3650 else
3651 return -EINVAL;
3652}
3653EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3654
3655/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003656 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3657 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003658 * @fmt: SND_SOC_DAIFMT_ format value.
3659 *
3660 * Configures the DAI hardware format and clocking.
3661 */
3662int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3663{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003664 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003665 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003666 if (dai->driver->ops->set_fmt == NULL)
3667 return -ENOTSUPP;
3668 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003669}
3670EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3671
3672/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003673 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003674 * @slots: Number of slots in use.
3675 * @tx_mask: bitmask representing active TX slots.
3676 * @rx_mask: bitmask representing active RX slots.
3677 *
3678 * Generates the TDM tx and rx slot default masks for DAI.
3679 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003680static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003681 unsigned int *tx_mask,
3682 unsigned int *rx_mask)
3683{
3684 if (*tx_mask || *rx_mask)
3685 return 0;
3686
3687 if (!slots)
3688 return -EINVAL;
3689
3690 *tx_mask = (1 << slots) - 1;
3691 *rx_mask = (1 << slots) - 1;
3692
3693 return 0;
3694}
3695
3696/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003697 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3698 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003699 * @tx_mask: bitmask representing active TX slots.
3700 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003701 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003702 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003703 *
3704 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3705 * specific.
3706 */
3707int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003708 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003709{
Xiubo Lie5c21512014-03-21 14:17:12 +08003710 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3711 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003712 &tx_mask, &rx_mask);
3713 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003714 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003715
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003716 if (dai->driver && dai->driver->ops->set_tdm_slot)
3717 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003718 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003719 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003720 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003721}
3722EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3723
3724/**
Barry Song472df3c2009-09-12 01:16:29 +08003725 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3726 * @dai: DAI
3727 * @tx_num: how many TX channels
3728 * @tx_slot: pointer to an array which imply the TX slot number channel
3729 * 0~num-1 uses
3730 * @rx_num: how many RX channels
3731 * @rx_slot: pointer to an array which imply the RX slot number channel
3732 * 0~num-1 uses
3733 *
3734 * configure the relationship between channel number and TDM slot number.
3735 */
3736int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3737 unsigned int tx_num, unsigned int *tx_slot,
3738 unsigned int rx_num, unsigned int *rx_slot)
3739{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003740 if (dai->driver && dai->driver->ops->set_channel_map)
3741 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003742 rx_num, rx_slot);
3743 else
3744 return -EINVAL;
3745}
3746EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3747
3748/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003749 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3750 * @dai: DAI
3751 * @tristate: tristate enable
3752 *
3753 * Tristates the DAI so that others can use it.
3754 */
3755int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3756{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003757 if (dai->driver && dai->driver->ops->set_tristate)
3758 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003759 else
3760 return -EINVAL;
3761}
3762EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3763
3764/**
3765 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3766 * @dai: DAI
3767 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003768 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003769 *
3770 * Mutes the DAI DAC.
3771 */
Mark Brownda183962013-02-06 15:44:07 +00003772int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3773 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003774{
Mark Brownda183962013-02-06 15:44:07 +00003775 if (!dai->driver)
3776 return -ENOTSUPP;
3777
3778 if (dai->driver->ops->mute_stream)
3779 return dai->driver->ops->mute_stream(dai, mute, direction);
3780 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3781 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003782 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003783 else
Mark Brown04570c62012-04-13 19:16:03 +01003784 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003785}
3786EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3787
Mark Brownc5af3a22008-11-28 13:29:45 +00003788/**
3789 * snd_soc_register_card - Register a card with the ASoC core
3790 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003791 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003792 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003793 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303794int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003795{
Mark Brownb19e6e72012-03-14 21:18:39 +00003796 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003797
Mark Brownc5af3a22008-11-28 13:29:45 +00003798 if (!card->name || !card->dev)
3799 return -EINVAL;
3800
Stephen Warren5a504962011-12-21 10:40:59 -07003801 for (i = 0; i < card->num_links; i++) {
3802 struct snd_soc_dai_link *link = &card->dai_link[i];
3803
3804 /*
3805 * Codec must be specified by 1 of name or OF node,
3806 * not both or neither.
3807 */
3808 if (!!link->codec_name == !!link->codec_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003809 dev_err(card->dev,
3810 "ASoC: Neither/both codec name/of_node are set for %s\n",
3811 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003812 return -EINVAL;
3813 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003814 /* Codec DAI name must be specified */
3815 if (!link->codec_dai_name) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003816 dev_err(card->dev,
3817 "ASoC: codec_dai_name not set for %s\n",
3818 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003819 return -EINVAL;
3820 }
Stephen Warren5a504962011-12-21 10:40:59 -07003821
3822 /*
3823 * Platform may be specified by either name or OF node, but
3824 * can be left unspecified, and a dummy platform will be used.
3825 */
3826 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003827 dev_err(card->dev,
3828 "ASoC: Both platform name/of_node are set for %s\n",
3829 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003830 return -EINVAL;
3831 }
3832
3833 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003834 * CPU device may be specified by either name or OF node, but
3835 * can be left unspecified, and will be matched based on DAI
3836 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003837 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003838 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003839 dev_err(card->dev,
3840 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3841 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003842 return -EINVAL;
3843 }
3844 /*
3845 * At least one of CPU DAI name or CPU device name/node must be
3846 * specified
3847 */
3848 if (!link->cpu_dai_name &&
3849 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003850 dev_err(card->dev,
3851 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3852 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003853 return -EINVAL;
3854 }
3855 }
3856
Mark Browned77cc12011-05-03 18:25:34 +01003857 dev_set_drvdata(card->dev, card);
3858
Stephen Warren111c6412011-01-28 14:26:35 -07003859 snd_soc_initialize_card_lists(card);
3860
Vinod Koul150dd2f2011-01-13 22:48:32 +05303861 soc_init_card_debugfs(card);
3862
Mark Brown181a6892012-03-14 20:18:49 +00003863 card->rtd = devm_kzalloc(card->dev,
3864 sizeof(struct snd_soc_pcm_runtime) *
3865 (card->num_links + card->num_aux_devs),
3866 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003867 if (card->rtd == NULL)
3868 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003869 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003870 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003871
3872 for (i = 0; i < card->num_links; i++)
3873 card->rtd[i].dai_link = &card->dai_link[i];
3874
Mark Brownc5af3a22008-11-28 13:29:45 +00003875 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003876 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003877 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003878 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003879 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003880
Mark Brownb19e6e72012-03-14 21:18:39 +00003881 ret = snd_soc_instantiate_card(card);
3882 if (ret != 0)
3883 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003884
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003885 /* deactivate pins to sleep state */
3886 for (i = 0; i < card->num_rtd; i++) {
3887 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3888 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3889 if (!codec_dai->active)
3890 pinctrl_pm_select_sleep_state(codec_dai->dev);
3891 if (!cpu_dai->active)
3892 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3893 }
3894
Mark Brownb19e6e72012-03-14 21:18:39 +00003895 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003896}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303897EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003898
3899/**
3900 * snd_soc_unregister_card - Unregister a card with the ASoC core
3901 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003902 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003903 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003904 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303905int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003906{
Vinod Koulb0e26482011-01-13 22:48:02 +05303907 if (card->instantiated)
3908 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003909 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003910
3911 return 0;
3912}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303913EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003914
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003915/*
3916 * Simplify DAI link configuration by removing ".-1" from device names
3917 * and sanitizing names.
3918 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003919static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003920{
3921 char *found, name[NAME_SIZE];
3922 int id1, id2;
3923
3924 if (dev_name(dev) == NULL)
3925 return NULL;
3926
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003927 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003928
3929 /* are we a "%s.%d" name (platform and SPI components) */
3930 found = strstr(name, dev->driver->name);
3931 if (found) {
3932 /* get ID */
3933 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3934
3935 /* discard ID from name if ID == -1 */
3936 if (*id == -1)
3937 found[strlen(dev->driver->name)] = '\0';
3938 }
3939
3940 } else {
3941 /* I2C component devices are named "bus-addr" */
3942 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3943 char tmp[NAME_SIZE];
3944
3945 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003946 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003947
3948 /* sanitize component name for DAI link creation */
3949 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003950 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003951 } else
3952 *id = 0;
3953 }
3954
3955 return kstrdup(name, GFP_KERNEL);
3956}
3957
3958/*
3959 * Simplify DAI link naming for single devices with multiple DAIs by removing
3960 * any ".-1" and using the DAI name (instead of device name).
3961 */
3962static inline char *fmt_multiple_name(struct device *dev,
3963 struct snd_soc_dai_driver *dai_drv)
3964{
3965 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003966 dev_err(dev,
3967 "ASoC: error - multiple DAI %s registered with no name\n",
3968 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003969 return NULL;
3970 }
3971
3972 return kstrdup(dai_drv->name, GFP_KERNEL);
3973}
3974
Mark Brown91151712008-11-30 23:31:24 +00003975/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003976 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003977 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003978 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003979 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003980static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003981{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003982 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003983
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003984 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003985 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3986 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003987 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003988 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003989 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003990 }
Mark Brown91151712008-11-30 23:31:24 +00003991}
Mark Brown91151712008-11-30 23:31:24 +00003992
3993/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003994 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003995 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003996 * @component: The component the DAIs are registered for
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003997 * @codec: The CODEC that the DAIs are registered for, NULL if the component is
3998 * not a CODEC.
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003999 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00004000 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004001 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
4002 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00004003 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004004static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004005 struct snd_soc_codec *codec, struct snd_soc_dai_driver *dai_drv,
4006 size_t count, bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00004007{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004008 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004009 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004010 unsigned int i;
4011 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004012
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004013 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00004014
4015 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004016
4017 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08004018 if (dai == NULL) {
4019 ret = -ENOMEM;
4020 goto err;
4021 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004022
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004023 /*
4024 * Back in the old days when we still had component-less DAIs,
4025 * instead of having a static name, component-less DAIs would
4026 * inherit the name of the parent device so it is possible to
4027 * register multiple instances of the DAI. We still need to keep
4028 * the same naming style even though those DAIs are not
4029 * component-less anymore.
4030 */
4031 if (count == 1 && legacy_dai_naming) {
4032 dai->name = fmt_single_name(dev, &dai->id);
4033 } else {
4034 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
4035 if (dai_drv[i].id)
4036 dai->id = dai_drv[i].id;
4037 else
4038 dai->id = i;
4039 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004040 if (dai->name == NULL) {
4041 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004042 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00004043 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004044 }
4045
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004046 dai->component = component;
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004047 dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004048 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004049 dai->driver = &dai_drv[i];
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00004050 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004051 if (!dai->driver->ops)
4052 dai->driver->ops = &null_dai_ops;
4053
Peter Ujfalusi5f800082012-08-07 10:24:13 +03004054 if (!dai->codec)
4055 dai->dapm.idle_bias_off = 1;
4056
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01004057 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01004058
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004059 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00004060 }
4061
4062 return 0;
4063
4064err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004065 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00004066
4067 return ret;
4068}
Mark Brown91151712008-11-30 23:31:24 +00004069
4070/**
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004071 * snd_soc_register_component - Register a component with the ASoC core
4072 *
4073 */
4074static int
4075__snd_soc_register_component(struct device *dev,
4076 struct snd_soc_component *cmpnt,
4077 const struct snd_soc_component_driver *cmpnt_drv,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004078 struct snd_soc_codec *codec,
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004079 struct snd_soc_dai_driver *dai_drv,
4080 int num_dai, bool allow_single_dai)
4081{
4082 int ret;
4083
4084 dev_dbg(dev, "component register %s\n", dev_name(dev));
4085
4086 if (!cmpnt) {
4087 dev_err(dev, "ASoC: Failed to connecting component\n");
4088 return -ENOMEM;
4089 }
4090
4091 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4092 if (!cmpnt->name) {
4093 dev_err(dev, "ASoC: Failed to simplifying name\n");
4094 return -ENOMEM;
4095 }
4096
4097 cmpnt->dev = dev;
4098 cmpnt->driver = cmpnt_drv;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004099 cmpnt->dai_drv = dai_drv;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004100 cmpnt->num_dai = num_dai;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01004101 INIT_LIST_HEAD(&cmpnt->dai_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004102
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004103 ret = snd_soc_register_dais(cmpnt, codec, dai_drv, num_dai,
4104 allow_single_dai);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004105 if (ret < 0) {
4106 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4107 goto error_component_name;
4108 }
4109
4110 mutex_lock(&client_mutex);
4111 list_add(&cmpnt->list, &component_list);
4112 mutex_unlock(&client_mutex);
4113
4114 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4115
4116 return ret;
4117
4118error_component_name:
4119 kfree(cmpnt->name);
4120
4121 return ret;
4122}
4123
4124int snd_soc_register_component(struct device *dev,
4125 const struct snd_soc_component_driver *cmpnt_drv,
4126 struct snd_soc_dai_driver *dai_drv,
4127 int num_dai)
4128{
4129 struct snd_soc_component *cmpnt;
4130
4131 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4132 if (!cmpnt) {
4133 dev_err(dev, "ASoC: Failed to allocate memory\n");
4134 return -ENOMEM;
4135 }
4136
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004137 cmpnt->ignore_pmdown_time = true;
4138
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004139 return __snd_soc_register_component(dev, cmpnt, cmpnt_drv, NULL,
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004140 dai_drv, num_dai, true);
4141}
4142EXPORT_SYMBOL_GPL(snd_soc_register_component);
4143
4144/**
4145 * snd_soc_unregister_component - Unregister a component from the ASoC core
4146 *
4147 */
4148void snd_soc_unregister_component(struct device *dev)
4149{
4150 struct snd_soc_component *cmpnt;
4151
4152 list_for_each_entry(cmpnt, &component_list, list) {
4153 if (dev == cmpnt->dev)
4154 goto found;
4155 }
4156 return;
4157
4158found:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004159 snd_soc_unregister_dais(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004160
4161 mutex_lock(&client_mutex);
4162 list_del(&cmpnt->list);
4163 mutex_unlock(&client_mutex);
4164
4165 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4166 kfree(cmpnt->name);
4167}
4168EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4169
4170/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004171 * snd_soc_add_platform - Add a platform to the ASoC core
4172 * @dev: The parent device for the platform
4173 * @platform: The platform to add
4174 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004175 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004176int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004177 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004178{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004179 /* create platform component name */
4180 platform->name = fmt_single_name(dev, &platform->id);
Dan Carpenterb5c745f2013-07-22 09:56:54 +03004181 if (platform->name == NULL)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004182 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004183
4184 platform->dev = dev;
4185 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01004186 platform->dapm.dev = dev;
4187 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004188 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00004189 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004190
4191 mutex_lock(&client_mutex);
4192 list_add(&platform->list, &platform_list);
4193 mutex_unlock(&client_mutex);
4194
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004195 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004196
4197 return 0;
4198}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004199EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4200
4201/**
4202 * snd_soc_register_platform - Register a platform with the ASoC core
4203 *
4204 * @platform: platform to register
4205 */
4206int snd_soc_register_platform(struct device *dev,
4207 const struct snd_soc_platform_driver *platform_drv)
4208{
4209 struct snd_soc_platform *platform;
4210 int ret;
4211
4212 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4213
4214 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4215 if (platform == NULL)
4216 return -ENOMEM;
4217
4218 ret = snd_soc_add_platform(dev, platform, platform_drv);
4219 if (ret)
4220 kfree(platform);
4221
4222 return ret;
4223}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004224EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4225
4226/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004227 * snd_soc_remove_platform - Remove a platform from the ASoC core
4228 * @platform: the platform to remove
4229 */
4230void snd_soc_remove_platform(struct snd_soc_platform *platform)
4231{
4232 mutex_lock(&client_mutex);
4233 list_del(&platform->list);
4234 mutex_unlock(&client_mutex);
4235
4236 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4237 platform->name);
4238 kfree(platform->name);
4239}
4240EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4241
4242struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4243{
4244 struct snd_soc_platform *platform;
4245
4246 list_for_each_entry(platform, &platform_list, list) {
4247 if (dev == platform->dev)
4248 return platform;
4249 }
4250
4251 return NULL;
4252}
4253EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4254
4255/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004256 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4257 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004258 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004259 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004260void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004261{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004262 struct snd_soc_platform *platform;
4263
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004264 platform = snd_soc_lookup_platform(dev);
4265 if (!platform)
4266 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004267
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004268 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004269 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004270}
4271EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4272
Mark Brown151ab222009-05-09 16:22:58 +01004273static u64 codec_format_map[] = {
4274 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4275 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4276 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4277 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4278 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4279 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4280 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4281 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4282 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4283 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4284 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4285 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4286 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4287 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4288 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4289 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4290};
4291
4292/* Fix up the DAI formats for endianness: codecs don't actually see
4293 * the endianness of the data but we're using the CPU format
4294 * definitions which do need to include endianness so we ensure that
4295 * codec DAIs always have both big and little endian variants set.
4296 */
4297static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4298{
4299 int i;
4300
4301 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4302 if (stream->formats & codec_format_map[i])
4303 stream->formats |= codec_format_map[i];
4304}
4305
Mark Brown0d0cf002008-12-10 14:32:45 +00004306/**
4307 * snd_soc_register_codec - Register a codec with the ASoC core
4308 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004309 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004310 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004311int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004312 const struct snd_soc_codec_driver *codec_drv,
4313 struct snd_soc_dai_driver *dai_drv,
4314 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004315{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004316 struct snd_soc_codec *codec;
4317 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004318
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004319 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004320
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004321 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4322 if (codec == NULL)
4323 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004324
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004325 /* create CODEC component name */
4326 codec->name = fmt_single_name(dev, &codec->id);
4327 if (codec->name == NULL) {
Kuninori Morimotof790b942013-02-25 00:40:09 -08004328 ret = -ENOMEM;
4329 goto fail_codec;
Mark Brown151ab222009-05-09 16:22:58 +01004330 }
4331
Mark Brownc3acec22010-12-02 16:15:29 +00004332 codec->write = codec_drv->write;
4333 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004334 codec->volatile_register = codec_drv->volatile_register;
4335 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004336 codec->writable_register = codec_drv->writable_register;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004337 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004338 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4339 codec->dapm.dev = dev;
4340 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004341 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004342 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004343 codec->dev = dev;
4344 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004345 codec->num_dai = num_dai;
4346 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004347
4348 for (i = 0; i < num_dai; i++) {
4349 fixup_codec_formats(&dai_drv[i].playback);
4350 fixup_codec_formats(&dai_drv[i].capture);
4351 }
4352
Mark Brown054880f2012-04-09 17:29:19 +01004353 mutex_lock(&client_mutex);
4354 list_add(&codec->list, &codec_list);
4355 mutex_unlock(&client_mutex);
4356
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004357 /* register component */
4358 ret = __snd_soc_register_component(dev, &codec->component,
4359 &codec_drv->component_driver,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004360 codec, dai_drv, num_dai, false);
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004361 if (ret < 0) {
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004362 dev_err(codec->dev, "ASoC: Failed to regster component: %d\n", ret);
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004363 goto fail_codec_name;
Mark Brown26b01cc2010-08-18 20:20:55 +01004364 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004365
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004366 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004367 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004368
Kuninori Morimotof790b942013-02-25 00:40:09 -08004369fail_codec_name:
Kuninori Morimotoe1328a82013-03-07 17:42:33 -08004370 mutex_lock(&client_mutex);
4371 list_del(&codec->list);
4372 mutex_unlock(&client_mutex);
4373
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004374 kfree(codec->name);
Kuninori Morimotof790b942013-02-25 00:40:09 -08004375fail_codec:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004376 kfree(codec);
4377 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004378}
4379EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4380
4381/**
4382 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4383 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004384 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004385 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004386void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004387{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004388 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004389
4390 list_for_each_entry(codec, &codec_list, list) {
4391 if (dev == codec->dev)
4392 goto found;
4393 }
4394 return;
4395
4396found:
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004397 snd_soc_unregister_component(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004398
Mark Brown0d0cf002008-12-10 14:32:45 +00004399 mutex_lock(&client_mutex);
4400 list_del(&codec->list);
4401 mutex_unlock(&client_mutex);
4402
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004403 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004404
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004405 snd_soc_cache_exit(codec);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004406 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004407 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004408}
4409EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4410
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004411/* Retrieve a card's name from device tree */
4412int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4413 const char *propname)
4414{
4415 struct device_node *np = card->dev->of_node;
4416 int ret;
4417
4418 ret = of_property_read_string_index(np, propname, 0, &card->name);
4419 /*
4420 * EINVAL means the property does not exist. This is fine providing
4421 * card->name was previously set, which is checked later in
4422 * snd_soc_register_card.
4423 */
4424 if (ret < 0 && ret != -EINVAL) {
4425 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004426 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004427 propname, ret);
4428 return ret;
4429 }
4430
4431 return 0;
4432}
4433EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4434
Xiubo Li9a6d4862014-02-08 15:59:52 +08004435static const struct snd_soc_dapm_widget simple_widgets[] = {
4436 SND_SOC_DAPM_MIC("Microphone", NULL),
4437 SND_SOC_DAPM_LINE("Line", NULL),
4438 SND_SOC_DAPM_HP("Headphone", NULL),
4439 SND_SOC_DAPM_SPK("Speaker", NULL),
4440};
4441
4442int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4443 const char *propname)
4444{
4445 struct device_node *np = card->dev->of_node;
4446 struct snd_soc_dapm_widget *widgets;
4447 const char *template, *wname;
4448 int i, j, num_widgets, ret;
4449
4450 num_widgets = of_property_count_strings(np, propname);
4451 if (num_widgets < 0) {
4452 dev_err(card->dev,
4453 "ASoC: Property '%s' does not exist\n", propname);
4454 return -EINVAL;
4455 }
4456 if (num_widgets & 1) {
4457 dev_err(card->dev,
4458 "ASoC: Property '%s' length is not even\n", propname);
4459 return -EINVAL;
4460 }
4461
4462 num_widgets /= 2;
4463 if (!num_widgets) {
4464 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4465 propname);
4466 return -EINVAL;
4467 }
4468
4469 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4470 GFP_KERNEL);
4471 if (!widgets) {
4472 dev_err(card->dev,
4473 "ASoC: Could not allocate memory for widgets\n");
4474 return -ENOMEM;
4475 }
4476
4477 for (i = 0; i < num_widgets; i++) {
4478 ret = of_property_read_string_index(np, propname,
4479 2 * i, &template);
4480 if (ret) {
4481 dev_err(card->dev,
4482 "ASoC: Property '%s' index %d read error:%d\n",
4483 propname, 2 * i, ret);
4484 return -EINVAL;
4485 }
4486
4487 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4488 if (!strncmp(template, simple_widgets[j].name,
4489 strlen(simple_widgets[j].name))) {
4490 widgets[i] = simple_widgets[j];
4491 break;
4492 }
4493 }
4494
4495 if (j >= ARRAY_SIZE(simple_widgets)) {
4496 dev_err(card->dev,
4497 "ASoC: DAPM widget '%s' is not supported\n",
4498 template);
4499 return -EINVAL;
4500 }
4501
4502 ret = of_property_read_string_index(np, propname,
4503 (2 * i) + 1,
4504 &wname);
4505 if (ret) {
4506 dev_err(card->dev,
4507 "ASoC: Property '%s' index %d read error:%d\n",
4508 propname, (2 * i) + 1, ret);
4509 return -EINVAL;
4510 }
4511
4512 widgets[i].name = wname;
4513 }
4514
4515 card->dapm_widgets = widgets;
4516 card->num_dapm_widgets = num_widgets;
4517
4518 return 0;
4519}
4520EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4521
Xiubo Li89c67852014-02-14 09:34:35 +08004522int snd_soc_of_parse_tdm_slot(struct device_node *np,
4523 unsigned int *slots,
4524 unsigned int *slot_width)
4525{
4526 u32 val;
4527 int ret;
4528
4529 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4530 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4531 if (ret)
4532 return ret;
4533
4534 if (slots)
4535 *slots = val;
4536 }
4537
4538 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4539 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4540 if (ret)
4541 return ret;
4542
4543 if (slot_width)
4544 *slot_width = val;
4545 }
4546
4547 return 0;
4548}
4549EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4550
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004551int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4552 const char *propname)
4553{
4554 struct device_node *np = card->dev->of_node;
4555 int num_routes;
4556 struct snd_soc_dapm_route *routes;
4557 int i, ret;
4558
4559 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004560 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004561 dev_err(card->dev,
4562 "ASoC: Property '%s' does not exist or its length is not even\n",
4563 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004564 return -EINVAL;
4565 }
4566 num_routes /= 2;
4567 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004568 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004569 propname);
4570 return -EINVAL;
4571 }
4572
4573 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4574 GFP_KERNEL);
4575 if (!routes) {
4576 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004577 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004578 return -EINVAL;
4579 }
4580
4581 for (i = 0; i < num_routes; i++) {
4582 ret = of_property_read_string_index(np, propname,
4583 2 * i, &routes[i].sink);
4584 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004585 dev_err(card->dev,
4586 "ASoC: Property '%s' index %d could not be read: %d\n",
4587 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004588 return -EINVAL;
4589 }
4590 ret = of_property_read_string_index(np, propname,
4591 (2 * i) + 1, &routes[i].source);
4592 if (ret) {
4593 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004594 "ASoC: Property '%s' index %d could not be read: %d\n",
4595 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004596 return -EINVAL;
4597 }
4598 }
4599
4600 card->num_dapm_routes = num_routes;
4601 card->dapm_routes = routes;
4602
4603 return 0;
4604}
4605EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4606
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004607unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4608 const char *prefix)
4609{
4610 int ret, i;
4611 char prop[128];
4612 unsigned int format = 0;
4613 int bit, frame;
4614 const char *str;
4615 struct {
4616 char *name;
4617 unsigned int val;
4618 } of_fmt_table[] = {
4619 { "i2s", SND_SOC_DAIFMT_I2S },
4620 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4621 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4622 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4623 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4624 { "ac97", SND_SOC_DAIFMT_AC97 },
4625 { "pdm", SND_SOC_DAIFMT_PDM},
4626 { "msb", SND_SOC_DAIFMT_MSB },
4627 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004628 };
4629
4630 if (!prefix)
4631 prefix = "";
4632
4633 /*
4634 * check "[prefix]format = xxx"
4635 * SND_SOC_DAIFMT_FORMAT_MASK area
4636 */
4637 snprintf(prop, sizeof(prop), "%sformat", prefix);
4638 ret = of_property_read_string(np, prop, &str);
4639 if (ret == 0) {
4640 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4641 if (strcmp(str, of_fmt_table[i].name) == 0) {
4642 format |= of_fmt_table[i].val;
4643 break;
4644 }
4645 }
4646 }
4647
4648 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004649 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004650 * SND_SOC_DAIFMT_CLOCK_MASK area
4651 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004652 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4653 if (of_get_property(np, prop, NULL))
4654 format |= SND_SOC_DAIFMT_CONT;
4655 else
4656 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004657
4658 /*
4659 * check "[prefix]bitclock-inversion"
4660 * check "[prefix]frame-inversion"
4661 * SND_SOC_DAIFMT_INV_MASK area
4662 */
4663 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4664 bit = !!of_get_property(np, prop, NULL);
4665
4666 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4667 frame = !!of_get_property(np, prop, NULL);
4668
4669 switch ((bit << 4) + frame) {
4670 case 0x11:
4671 format |= SND_SOC_DAIFMT_IB_IF;
4672 break;
4673 case 0x10:
4674 format |= SND_SOC_DAIFMT_IB_NF;
4675 break;
4676 case 0x01:
4677 format |= SND_SOC_DAIFMT_NB_IF;
4678 break;
4679 default:
4680 /* SND_SOC_DAIFMT_NB_NF is default */
4681 break;
4682 }
4683
4684 /*
4685 * check "[prefix]bitclock-master"
4686 * check "[prefix]frame-master"
4687 * SND_SOC_DAIFMT_MASTER_MASK area
4688 */
4689 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4690 bit = !!of_get_property(np, prop, NULL);
4691
4692 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4693 frame = !!of_get_property(np, prop, NULL);
4694
4695 switch ((bit << 4) + frame) {
4696 case 0x11:
4697 format |= SND_SOC_DAIFMT_CBM_CFM;
4698 break;
4699 case 0x10:
4700 format |= SND_SOC_DAIFMT_CBM_CFS;
4701 break;
4702 case 0x01:
4703 format |= SND_SOC_DAIFMT_CBS_CFM;
4704 break;
4705 default:
4706 format |= SND_SOC_DAIFMT_CBS_CFS;
4707 break;
4708 }
4709
4710 return format;
4711}
4712EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4713
Kuninori Morimotocb470082013-09-10 17:39:56 -07004714int snd_soc_of_get_dai_name(struct device_node *of_node,
4715 const char **dai_name)
4716{
4717 struct snd_soc_component *pos;
4718 struct of_phandle_args args;
4719 int ret;
4720
4721 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4722 "#sound-dai-cells", 0, &args);
4723 if (ret)
4724 return ret;
4725
4726 ret = -EPROBE_DEFER;
4727
4728 mutex_lock(&client_mutex);
4729 list_for_each_entry(pos, &component_list, list) {
4730 if (pos->dev->of_node != args.np)
4731 continue;
4732
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004733 if (pos->driver->of_xlate_dai_name) {
4734 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4735 } else {
4736 int id = -1;
4737
4738 switch (args.args_count) {
4739 case 0:
4740 id = 0; /* same as dai_drv[0] */
4741 break;
4742 case 1:
4743 id = args.args[0];
4744 break;
4745 default:
4746 /* not supported */
4747 break;
4748 }
4749
4750 if (id < 0 || id >= pos->num_dai) {
4751 ret = -EINVAL;
Xiubo Lie41975e2013-12-20 14:39:51 +08004752 break;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004753 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004754
4755 ret = 0;
4756
4757 *dai_name = pos->dai_drv[id].name;
4758 if (!*dai_name)
4759 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004760 }
4761
Kuninori Morimotocb470082013-09-10 17:39:56 -07004762 break;
4763 }
4764 mutex_unlock(&client_mutex);
4765
4766 of_node_put(args.np);
4767
4768 return ret;
4769}
4770EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4771
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004772static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004773{
Mark Brown384c89e2008-12-03 17:34:03 +00004774#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004775 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4776 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004777 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004778 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004779 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004780
Mark Brown8a9dab12011-01-10 22:25:21 +00004781 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004782 &codec_list_fops))
4783 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4784
Mark Brown8a9dab12011-01-10 22:25:21 +00004785 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004786 &dai_list_fops))
4787 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004788
Mark Brown8a9dab12011-01-10 22:25:21 +00004789 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004790 &platform_list_fops))
4791 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004792#endif
4793
Mark Brownfb257892011-04-28 10:57:54 +01004794 snd_soc_util_init();
4795
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004796 return platform_driver_register(&soc_driver);
4797}
Mark Brown4abe8e12010-10-12 17:41:03 +01004798module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004799
Mark Brown7d8c16a2008-11-30 22:11:24 +00004800static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004801{
Mark Brownfb257892011-04-28 10:57:54 +01004802 snd_soc_util_exit();
4803
Mark Brown384c89e2008-12-03 17:34:03 +00004804#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004805 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004806#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004807 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004808}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004809module_exit(snd_soc_exit);
4810
4811/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004812MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004813MODULE_DESCRIPTION("ALSA SoC Core");
4814MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004815MODULE_ALIAS("platform:soc-audio");