blob: 42ce14485b92ee291fb2b5870d538e8439c1e1d8 [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>
Mark Brownf0e8ed82011-09-20 11:41:54 +010033#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070035#include <linux/of.h>
Marek Vasut474828a2009-07-22 13:01:03 +020036#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020037#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000038#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020039#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/soc.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/initval.h>
43
Mark Browna8b1d342010-11-03 18:05:58 -040044#define CREATE_TRACE_POINTS
45#include <trace/events/asoc.h>
46
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000047#define NAME_SIZE 32
48
Frank Mandarinodb2a4162006-10-06 18:31:09 +020049static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
50
Mark Brown384c89e2008-12-03 17:34:03 +000051#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000052struct dentry *snd_soc_debugfs_root;
53EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000054#endif
55
Mark Brownc5af3a22008-11-28 13:29:45 +000056static DEFINE_MUTEX(client_mutex);
57static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000058static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000061
Frank Mandarinodb2a4162006-10-06 18:31:09 +020062/*
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
66 */
67static int pmdown_time = 5000;
68module_param(pmdown_time, int, 0);
69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000071/* returns the minimum number of bytes needed to represent
72 * a particular given value */
73static int min_bytes_needed(unsigned long val)
74{
75 int c = 0;
76 int i;
77
78 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
79 if (val & (1UL << i))
80 break;
81 c = (sizeof val * 8) - c;
82 if (!c || (c % 8))
83 c = (c + 8) / 8;
84 else
85 c /= 8;
86 return c;
87}
88
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000089/* fill buf which is 'len' bytes with a formatted
90 * string of the form 'reg: value\n' */
91static int format_register_str(struct snd_soc_codec *codec,
92 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000093{
Stephen Warren00b317a2011-04-01 14:50:44 -060094 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
95 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000096 int ret;
97 char tmpbuf[len + 1];
98 char regbuf[regsize + 1];
99
100 /* since tmpbuf is allocated on the stack, warn the callers if they
101 * try to abuse this function */
102 WARN_ON(len > 63);
103
104 /* +2 for ': ' and + 1 for '\n' */
105 if (wordsize + regsize + 2 + 1 != len)
106 return -EINVAL;
107
Mark Brown25032c12011-08-01 13:52:48 +0900108 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000109 if (ret < 0) {
110 memset(regbuf, 'X', regsize);
111 regbuf[regsize] = '\0';
112 } else {
113 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
114 }
115
116 /* prepare the buffer */
117 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
118 /* copy it back to the caller without the '\0' */
119 memcpy(buf, tmpbuf, len);
120
121 return 0;
122}
123
124/* codec register dump */
125static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
126 size_t count, loff_t pos)
127{
128 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000129 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000130 int len;
131 size_t total = 0;
132 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000133
Stephen Warren00b317a2011-04-01 14:50:44 -0600134 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
135 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000136
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000137 len = wordsize + regsize + 2 + 1;
138
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000139 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000140 return 0;
141
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000142 if (codec->driver->reg_cache_step)
143 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000144
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000145 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200146 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000147 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000148 if (codec->driver->display_register) {
149 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000150 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100151 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000152 /* only support larger than PAGE_SIZE bytes debugfs
153 * entries for the default case */
154 if (p >= pos) {
155 if (total + len >= count - 1)
156 break;
157 format_register_str(codec, i, buf + total, len);
158 total += len;
159 }
160 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100161 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000162 }
163
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000164 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000165
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000166 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000167}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168
Mark Brown2624d5f2009-11-03 21:56:13 +0000169static ssize_t codec_reg_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
171{
Mark Brown36ae1a92012-01-06 17:12:45 -0800172 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000173
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000174 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000175}
176
177static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
178
Mark Browndbe21402010-02-12 11:37:24 +0000179static ssize_t pmdown_time_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
Mark Brown36ae1a92012-01-06 17:12:45 -0800182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000183
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000185}
186
187static ssize_t pmdown_time_set(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
190{
Mark Brown36ae1a92012-01-06 17:12:45 -0800191 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700192 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000193
Mark Brownc593b522010-10-27 20:11:17 -0700194 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
195 if (ret)
196 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
198 return count;
199}
200
201static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202
Mark Brown2624d5f2009-11-03 21:56:13 +0000203#ifdef CONFIG_DEBUG_FS
204static int codec_reg_open_file(struct inode *inode, struct file *file)
205{
206 file->private_data = inode->i_private;
207 return 0;
208}
209
210static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000211 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000212{
213 ssize_t ret;
214 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000215 char *buf;
216
217 if (*ppos < 0 || !count)
218 return -EINVAL;
219
220 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000221 if (!buf)
222 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000223
224 ret = soc_codec_reg_show(codec, buf, count, *ppos);
225 if (ret >= 0) {
226 if (copy_to_user(user_buf, buf, ret)) {
227 kfree(buf);
228 return -EFAULT;
229 }
230 *ppos += ret;
231 }
232
Mark Brown2624d5f2009-11-03 21:56:13 +0000233 kfree(buf);
234 return ret;
235}
236
237static ssize_t codec_reg_write_file(struct file *file,
238 const char __user *user_buf, size_t count, loff_t *ppos)
239{
240 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700241 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000242 char *start = buf;
243 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000244 struct snd_soc_codec *codec = file->private_data;
245
246 buf_size = min(count, (sizeof(buf)-1));
247 if (copy_from_user(buf, user_buf, buf_size))
248 return -EFAULT;
249 buf[buf_size] = 0;
250
Mark Brown2624d5f2009-11-03 21:56:13 +0000251 while (*start == ' ')
252 start++;
253 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000254 while (*start == ' ')
255 start++;
256 if (strict_strtoul(start, 16, &value))
257 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000258
259 /* Userspace has been fiddling around behind the kernel's back */
260 add_taint(TAINT_USER);
261
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000262 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000263 return buf_size;
264}
265
266static const struct file_operations codec_reg_fops = {
267 .open = codec_reg_open_file,
268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200270 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000271};
272
273static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
274{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200275 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
276
277 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
278 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000279 if (!codec->debugfs_codec_root) {
Liam Girdwood64e60f92012-02-15 15:15:37 +0000280 dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000281 return;
282 }
283
Mark Brownaaee8ef2011-01-26 20:53:50 +0000284 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
285 &codec->cache_sync);
286 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
287 &codec->cache_only);
288
Mark Brown2624d5f2009-11-03 21:56:13 +0000289 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
290 codec->debugfs_codec_root,
291 codec, &codec_reg_fops);
292 if (!codec->debugfs_reg)
Liam Girdwood64e60f92012-02-15 15:15:37 +0000293 dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000294
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200295 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000296}
297
298static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
299{
300 debugfs_remove_recursive(codec->debugfs_codec_root);
301}
302
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000303static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
304{
305 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
306
307 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
308 debugfs_card_root);
309 if (!platform->debugfs_platform_root) {
310 dev_warn(platform->dev,
311 "Failed to create platform debugfs directory\n");
312 return;
313 }
314
315 snd_soc_dapm_debugfs_init(&platform->dapm,
316 platform->debugfs_platform_root);
317}
318
319static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
320{
321 debugfs_remove_recursive(platform->debugfs_platform_root);
322}
323
Mark Brownc3c5a192010-09-15 18:15:14 +0100324static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
325 size_t count, loff_t *ppos)
326{
327 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100328 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100329 struct snd_soc_codec *codec;
330
331 if (!buf)
332 return -ENOMEM;
333
Mark Brown2b194f9d2010-10-13 10:52:16 +0100334 list_for_each_entry(codec, &codec_list, list) {
335 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
336 codec->name);
337 if (len >= 0)
338 ret += len;
339 if (ret > PAGE_SIZE) {
340 ret = PAGE_SIZE;
341 break;
342 }
343 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100344
345 if (ret >= 0)
346 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
347
348 kfree(buf);
349
350 return ret;
351}
352
353static const struct file_operations codec_list_fops = {
354 .read = codec_list_read_file,
355 .llseek = default_llseek,/* read accesses f_pos */
356};
357
Mark Brownf3208782010-09-15 18:19:07 +0100358static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
359 size_t count, loff_t *ppos)
360{
361 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100362 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100363 struct snd_soc_dai *dai;
364
365 if (!buf)
366 return -ENOMEM;
367
Mark Brown2b194f9d2010-10-13 10:52:16 +0100368 list_for_each_entry(dai, &dai_list, list) {
369 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
370 if (len >= 0)
371 ret += len;
372 if (ret > PAGE_SIZE) {
373 ret = PAGE_SIZE;
374 break;
375 }
376 }
Mark Brownf3208782010-09-15 18:19:07 +0100377
Mark Brown2b194f9d2010-10-13 10:52:16 +0100378 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100379
380 kfree(buf);
381
382 return ret;
383}
384
385static const struct file_operations dai_list_fops = {
386 .read = dai_list_read_file,
387 .llseek = default_llseek,/* read accesses f_pos */
388};
389
Mark Brown19c7ac22010-09-15 18:22:40 +0100390static ssize_t platform_list_read_file(struct file *file,
391 char __user *user_buf,
392 size_t count, loff_t *ppos)
393{
394 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100395 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100396 struct snd_soc_platform *platform;
397
398 if (!buf)
399 return -ENOMEM;
400
Mark Brown2b194f9d2010-10-13 10:52:16 +0100401 list_for_each_entry(platform, &platform_list, list) {
402 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
403 platform->name);
404 if (len >= 0)
405 ret += len;
406 if (ret > PAGE_SIZE) {
407 ret = PAGE_SIZE;
408 break;
409 }
410 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100411
Mark Brown2b194f9d2010-10-13 10:52:16 +0100412 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100413
414 kfree(buf);
415
416 return ret;
417}
418
419static const struct file_operations platform_list_fops = {
420 .read = platform_list_read_file,
421 .llseek = default_llseek,/* read accesses f_pos */
422};
423
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200424static void soc_init_card_debugfs(struct snd_soc_card *card)
425{
426 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000427 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200428 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200429 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100430 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200431 return;
432 }
433
434 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
435 card->debugfs_card_root,
436 &card->pop_time);
437 if (!card->debugfs_pop_time)
438 dev_warn(card->dev,
439 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200440}
441
442static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
443{
444 debugfs_remove_recursive(card->debugfs_card_root);
445}
446
Mark Brown2624d5f2009-11-03 21:56:13 +0000447#else
448
449static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
450{
451}
452
453static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
454{
455}
Axel Linb95fccb2010-11-09 17:06:44 +0800456
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000457static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
458{
459}
460
461static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
462{
463}
464
Axel Linb95fccb2010-11-09 17:06:44 +0800465static inline void soc_init_card_debugfs(struct snd_soc_card *card)
466{
467}
468
469static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
470{
471}
Mark Brown2624d5f2009-11-03 21:56:13 +0000472#endif
473
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200474#ifdef CONFIG_SND_SOC_AC97_BUS
475/* unregister ac97 codec */
476static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
477{
478 if (codec->ac97->dev.bus)
479 device_unregister(&codec->ac97->dev);
480 return 0;
481}
482
483/* stop no dev release warning */
484static void soc_ac97_device_release(struct device *dev){}
485
486/* register ac97 codec to bus */
487static int soc_ac97_dev_register(struct snd_soc_codec *codec)
488{
489 int err;
490
491 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100492 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200493 codec->ac97->dev.release = soc_ac97_device_release;
494
Kay Sieversbb072bf2008-11-02 03:50:35 +0100495 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000496 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200497 err = device_register(&codec->ac97->dev);
498 if (err < 0) {
499 snd_printk(KERN_ERR "Can't register ac97 bus\n");
500 codec->ac97->dev.bus = NULL;
501 return err;
502 }
503 return 0;
504}
505#endif
506
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000507#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200508/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000509int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200510{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000511 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200512 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200513 int i;
514
Daniel Macke3509ff2009-06-03 17:44:49 +0200515 /* If the initialization of this soc device failed, there is no codec
516 * associated with it. Just bail out in this case.
517 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200519 return 0;
520
Andy Green6ed25972008-06-13 16:24:05 +0100521 /* Due to the resume being scheduled into a workqueue we could
522 * suspend before that's finished - wait for it to complete.
523 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000524 snd_power_lock(card->snd_card);
525 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
526 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100527
528 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000529 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100530
Mark Browna00f90f2010-12-02 16:24:24 +0000531 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532 for (i = 0; i < card->num_rtd; i++) {
533 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
534 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100535
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000536 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100537 continue;
538
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000539 if (drv->ops->digital_mute && dai->playback_active)
540 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200541 }
542
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100543 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000544 for (i = 0; i < card->num_rtd; i++) {
545 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100546 continue;
547
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100549 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100550
Mark Brown87506542008-11-18 20:50:34 +0000551 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000552 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200553
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 for (i = 0; i < card->num_rtd; i++) {
555 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
556 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100557
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100559 continue;
560
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
562 cpu_dai->driver->suspend(cpu_dai);
563 if (platform->driver->suspend && !platform->suspended) {
564 platform->driver->suspend(cpu_dai);
565 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100566 }
567 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200568
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 /* close any waiting streams and save state */
570 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100571 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200572 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000573 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100574
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000575 for (i = 0; i < card->num_rtd; i++) {
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800576 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577
578 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100579 continue;
580
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800581 snd_soc_dapm_stream_event(&card->rtd[i],
582 SNDRV_PCM_STREAM_PLAYBACK,
583 codec_dai,
584 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000585
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800586 snd_soc_dapm_stream_event(&card->rtd[i],
587 SNDRV_PCM_STREAM_CAPTURE,
588 codec_dai,
589 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000590 }
591
592 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200593 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000594 /* If there are paths active then the CODEC will be held with
595 * bias _ON and should not be suspended. */
596 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200597 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000599 /*
600 * If the CODEC is capable of idle
601 * bias off then being in STANDBY
602 * means it's doing something,
603 * otherwise fall through.
604 */
605 if (codec->dapm.idle_bias_off) {
606 dev_dbg(codec->dev,
607 "idle_bias_off CODEC on over suspend\n");
608 break;
609 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100611 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900613 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000614 break;
615 default:
616 dev_dbg(codec->dev, "CODEC is on over suspend\n");
617 break;
618 }
619 }
620 }
621
622 for (i = 0; i < card->num_rtd; i++) {
623 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
624
625 if (card->rtd[i].dai_link->ignore_suspend)
626 continue;
627
628 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
629 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200630 }
631
Mark Brown87506542008-11-18 20:50:34 +0000632 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000633 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634
635 return 0;
636}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000637EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200638
Andy Green6ed25972008-06-13 16:24:05 +0100639/* deferred resume work, so resume can complete before we finished
640 * setting our codec back up, which can be very slow on I2C
641 */
642static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200643{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644 struct snd_soc_card *card =
645 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200646 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200647 int i;
648
Andy Green6ed25972008-06-13 16:24:05 +0100649 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
650 * so userspace apps are blocked from touching us
651 */
652
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000653 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100654
Mark Brown99497882010-05-07 20:24:05 +0100655 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100657
Mark Brown87506542008-11-18 20:50:34 +0000658 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000659 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200660
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 /* resume AC97 DAIs */
662 for (i = 0; i < card->num_rtd; i++) {
663 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100664
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100666 continue;
667
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
669 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200670 }
671
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200672 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 /* If the CODEC was idle over suspend then it will have been
674 * left with bias OFF or STANDBY and suspended so we must now
675 * resume. Otherwise the suspend was suppressed.
676 */
677 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200678 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000679 case SND_SOC_BIAS_STANDBY:
680 case SND_SOC_BIAS_OFF:
681 codec->driver->resume(codec);
682 codec->suspended = 0;
683 break;
684 default:
685 dev_dbg(codec->dev, "CODEC was on over suspend\n");
686 break;
687 }
Mark Brown1547aba2010-05-07 21:11:40 +0100688 }
689 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000691 for (i = 0; i < card->num_rtd; i++) {
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800692 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100693
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000694 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100695 continue;
696
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800697 snd_soc_dapm_stream_event(&card->rtd[i],
698 SNDRV_PCM_STREAM_PLAYBACK, codec_dai,
699 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800701 snd_soc_dapm_stream_event(&card->rtd[i],
702 SNDRV_PCM_STREAM_CAPTURE, codec_dai,
703 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200704 }
705
Mark Brown3ff3f642008-05-19 12:32:25 +0200706 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000707 for (i = 0; i < card->num_rtd; i++) {
708 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
709 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100710
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000711 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100712 continue;
713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714 if (drv->ops->digital_mute && dai->playback_active)
715 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716 }
717
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000718 for (i = 0; i < card->num_rtd; i++) {
719 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
720 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100721
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000722 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100723 continue;
724
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000725 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
726 cpu_dai->driver->resume(cpu_dai);
727 if (platform->driver->resume && platform->suspended) {
728 platform->driver->resume(cpu_dai);
729 platform->suspended = 0;
730 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200731 }
732
Mark Brown87506542008-11-18 20:50:34 +0000733 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000734 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200735
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000736 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100737
738 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000739 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100740}
741
742/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000743int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100744{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000745 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600746 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200747
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800748 /* If the initialization of this soc device failed, there is no codec
749 * associated with it. Just bail out in this case.
750 */
751 if (list_empty(&card->codec_dev_list))
752 return 0;
753
Mark Brown64ab9ba2009-03-31 11:27:03 +0100754 /* AC97 devices might have other drivers hanging off them so
755 * need to resume immediately. Other drivers don't have that
756 * problem and may take a substantial amount of time to resume
757 * due to I/O costs and anti-pop so handle them out of line.
758 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000759 for (i = 0; i < card->num_rtd; i++) {
760 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600761 ac97_control |= cpu_dai->driver->ac97_control;
762 }
763 if (ac97_control) {
764 dev_dbg(dev, "Resuming AC97 immediately\n");
765 soc_resume_deferred(&card->deferred_resume_work);
766 } else {
767 dev_dbg(dev, "Scheduling resume work\n");
768 if (!schedule_work(&card->deferred_resume_work))
769 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100770 }
Andy Green6ed25972008-06-13 16:24:05 +0100771
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200772 return 0;
773}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000774EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200775#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000776#define snd_soc_suspend NULL
777#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200778#endif
779
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100780static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800781};
782
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000783static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200784{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000785 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
786 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000787 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000788 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000789 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100790 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000792 if (rtd->complete)
793 return 1;
794 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000795
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000796 /* do we already have the CPU DAI for this link ? */
797 if (rtd->cpu_dai) {
798 goto find_codec;
799 }
800 /* no, then find CPU DAI from registered DAIs*/
801 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700802 if (dai_link->cpu_dai_of_node) {
803 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
804 continue;
805 } else {
806 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
807 continue;
808 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700809
810 rtd->cpu_dai = cpu_dai;
811 goto find_codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000812 }
813 dev_dbg(card->dev, "CPU DAI %s not registered\n",
814 dai_link->cpu_dai_name);
815
816find_codec:
817 /* do we already have the CODEC for this link ? */
818 if (rtd->codec) {
819 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +0000820 }
821
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000822 /* no, then find CODEC from registered CODECs*/
823 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700824 if (dai_link->codec_of_node) {
825 if (codec->dev->of_node != dai_link->codec_of_node)
826 continue;
827 } else {
828 if (strcmp(codec->name, dai_link->codec_name))
829 continue;
830 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000831
Stephen Warren2610ab72011-12-07 13:58:27 -0700832 rtd->codec = codec;
833
834 /*
835 * CODEC found, so find CODEC DAI from registered DAIs from
836 * this CODEC
837 */
838 list_for_each_entry(codec_dai, &dai_list, list) {
839 if (codec->dev == codec_dai->dev &&
840 !strcmp(codec_dai->name,
841 dai_link->codec_dai_name)) {
842
843 rtd->codec_dai = codec_dai;
844 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000845 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000846 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700847 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
848 dai_link->codec_dai_name);
849
850 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000851 }
852 dev_dbg(card->dev, "CODEC %s not registered\n",
853 dai_link->codec_name);
854
855find_platform:
Mark Brown848dd8b2011-04-27 18:16:32 +0100856 /* do we need a platform? */
857 if (rtd->platform)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000858 goto out;
Mark Brown848dd8b2011-04-27 18:16:32 +0100859
860 /* if there's no platform we match on the empty platform */
861 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700862 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100863 platform_name = "snd-soc-dummy";
864
865 /* no, then find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000866 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700867 if (dai_link->platform_of_node) {
868 if (platform->dev->of_node !=
869 dai_link->platform_of_node)
870 continue;
871 } else {
872 if (strcmp(platform->name, platform_name))
873 continue;
874 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700875
876 rtd->platform = platform;
877 goto out;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000878 }
879
880 dev_dbg(card->dev, "platform %s not registered\n",
881 dai_link->platform_name);
882 return 0;
883
884out:
885 /* mark rtd as complete if we found all 4 of our client devices */
886 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
887 rtd->complete = 1;
888 card->num_rtd++;
889 }
890 return 1;
891}
892
Jarkko Nikula589c3562010-12-06 16:27:07 +0200893static void soc_remove_codec(struct snd_soc_codec *codec)
894{
895 int err;
896
897 if (codec->driver->remove) {
898 err = codec->driver->remove(codec);
899 if (err < 0)
900 dev_err(codec->dev,
901 "asoc: failed to remove %s: %d\n",
902 codec->name, err);
903 }
904
905 /* Make sure all DAPM widgets are freed */
906 snd_soc_dapm_free(&codec->dapm);
907
908 soc_cleanup_codec_debugfs(codec);
909 codec->probed = 0;
910 list_del(&codec->card_list);
911 module_put(codec->dev->driver->owner);
912}
913
Liam Girdwood0168bf02011-06-07 16:08:05 +0100914static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000915{
916 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
917 struct snd_soc_codec *codec = rtd->codec;
918 struct snd_soc_platform *platform = rtd->platform;
919 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
920 int err;
921
922 /* unregister the rtd device */
923 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800924 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
925 device_remove_file(rtd->dev, &dev_attr_codec_reg);
926 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000927 rtd->dev_registered = 0;
928 }
929
930 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100931 if (codec_dai && codec_dai->probed &&
932 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000933 if (codec_dai->driver->remove) {
934 err = codec_dai->driver->remove(codec_dai);
935 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200936 pr_err("asoc: failed to remove %s: %d\n",
937 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000938 }
939 codec_dai->probed = 0;
940 list_del(&codec_dai->card_list);
941 }
942
943 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100944 if (platform && platform->probed &&
945 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000946 if (platform->driver->remove) {
947 err = platform->driver->remove(platform);
948 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200949 pr_err("asoc: failed to remove %s: %d\n",
950 platform->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000951 }
Liam Girdwood675c4962012-01-16 15:25:37 +0000952
953 /* Make sure all DAPM widgets are freed */
954 snd_soc_dapm_free(&platform->dapm);
955
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000956 soc_cleanup_platform_debugfs(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000957 platform->probed = 0;
958 list_del(&platform->card_list);
959 module_put(platform->dev->driver->owner);
960 }
961
962 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100963 if (codec && codec->probed &&
964 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200965 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000966
967 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100968 if (cpu_dai && cpu_dai->probed &&
969 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000970 if (cpu_dai->driver->remove) {
971 err = cpu_dai->driver->remove(cpu_dai);
972 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200973 pr_err("asoc: failed to remove %s: %d\n",
974 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000975 }
976 cpu_dai->probed = 0;
977 list_del(&cpu_dai->card_list);
978 module_put(cpu_dai->dev->driver->owner);
979 }
980}
981
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900982static void soc_remove_dai_links(struct snd_soc_card *card)
983{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100984 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900985
Liam Girdwood0168bf02011-06-07 16:08:05 +0100986 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
987 order++) {
988 for (dai = 0; dai < card->num_rtd; dai++)
989 soc_remove_dai_link(card, dai, order);
990 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900991 card->num_rtd = 0;
992}
993
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200994static void soc_set_name_prefix(struct snd_soc_card *card,
995 struct snd_soc_codec *codec)
996{
997 int i;
998
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000999 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001000 return;
1001
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001002 for (i = 0; i < card->num_configs; i++) {
1003 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001004 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1005 codec->name_prefix = map->name_prefix;
1006 break;
1007 }
1008 }
1009}
1010
Jarkko Nikula589c3562010-12-06 16:27:07 +02001011static int soc_probe_codec(struct snd_soc_card *card,
1012 struct snd_soc_codec *codec)
1013{
1014 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001015 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001016 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001017
1018 codec->card = card;
1019 codec->dapm.card = card;
1020 soc_set_name_prefix(card, codec);
1021
Jarkko Nikula70d29332011-01-27 16:24:22 +02001022 if (!try_module_get(codec->dev->driver->owner))
1023 return -ENODEV;
1024
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001025 soc_init_codec_debugfs(codec);
1026
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001027 if (driver->dapm_widgets)
1028 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1029 driver->num_dapm_widgets);
1030
Mark Brown888df392012-02-16 19:37:51 -08001031 /* Create DAPM widgets for each DAI stream */
1032 list_for_each_entry(dai, &dai_list, list) {
1033 if (dai->dev != codec->dev)
1034 continue;
1035
1036 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1037 }
1038
Mark Brown33c5f962011-08-22 18:40:30 +01001039 codec->dapm.idle_bias_off = driver->idle_bias_off;
1040
Mark Brown89b95ac02011-03-07 16:38:44 +00001041 if (driver->probe) {
1042 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001043 if (ret < 0) {
1044 dev_err(codec->dev,
1045 "asoc: failed to probe CODEC %s: %d\n",
1046 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001047 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001048 }
1049 }
1050
Mark Brownb7af1da2011-04-07 19:18:44 +09001051 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001052 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001053 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001054 if (driver->dapm_routes)
1055 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1056 driver->num_dapm_routes);
1057
Jarkko Nikula589c3562010-12-06 16:27:07 +02001058 /* mark codec as probed and add to card codec list */
1059 codec->probed = 1;
1060 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001061 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001062
Jarkko Nikula70d29332011-01-27 16:24:22 +02001063 return 0;
1064
1065err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001066 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001067 module_put(codec->dev->driver->owner);
1068
Jarkko Nikula589c3562010-12-06 16:27:07 +02001069 return ret;
1070}
1071
Liam Girdwood956245e2011-07-01 16:54:08 +01001072static int soc_probe_platform(struct snd_soc_card *card,
1073 struct snd_soc_platform *platform)
1074{
1075 int ret = 0;
1076 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001077 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001078
1079 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001080 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001081
1082 if (!try_module_get(platform->dev->driver->owner))
1083 return -ENODEV;
1084
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001085 soc_init_platform_debugfs(platform);
1086
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001087 if (driver->dapm_widgets)
1088 snd_soc_dapm_new_controls(&platform->dapm,
1089 driver->dapm_widgets, driver->num_dapm_widgets);
1090
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001091 /* Create DAPM widgets for each DAI stream */
1092 list_for_each_entry(dai, &dai_list, list) {
1093 if (dai->dev != platform->dev)
1094 continue;
1095
1096 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1097 }
1098
Liam Girdwood956245e2011-07-01 16:54:08 +01001099 if (driver->probe) {
1100 ret = driver->probe(platform);
1101 if (ret < 0) {
1102 dev_err(platform->dev,
1103 "asoc: failed to probe platform %s: %d\n",
1104 platform->name, ret);
1105 goto err_probe;
1106 }
1107 }
1108
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001109 if (driver->controls)
1110 snd_soc_add_platform_controls(platform, driver->controls,
1111 driver->num_controls);
1112 if (driver->dapm_routes)
1113 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1114 driver->num_dapm_routes);
1115
Liam Girdwood956245e2011-07-01 16:54:08 +01001116 /* mark platform as probed and add to card platform list */
1117 platform->probed = 1;
1118 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001119 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001120
1121 return 0;
1122
1123err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001124 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001125 module_put(platform->dev->driver->owner);
1126
1127 return ret;
1128}
1129
Mark Brown36ae1a92012-01-06 17:12:45 -08001130static void rtd_release(struct device *dev)
1131{
1132 kfree(dev);
1133}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001134
Jarkko Nikula589c3562010-12-06 16:27:07 +02001135static int soc_post_component_init(struct snd_soc_card *card,
1136 struct snd_soc_codec *codec,
1137 int num, int dailess)
1138{
1139 struct snd_soc_dai_link *dai_link = NULL;
1140 struct snd_soc_aux_dev *aux_dev = NULL;
1141 struct snd_soc_pcm_runtime *rtd;
1142 const char *temp, *name;
1143 int ret = 0;
1144
1145 if (!dailess) {
1146 dai_link = &card->dai_link[num];
1147 rtd = &card->rtd[num];
1148 name = dai_link->name;
1149 } else {
1150 aux_dev = &card->aux_dev[num];
1151 rtd = &card->rtd_aux[num];
1152 name = aux_dev->name;
1153 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001154 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001155
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001156 /* Make sure all DAPM widgets are instantiated */
1157 snd_soc_dapm_new_widgets(&codec->dapm);
1158
Jarkko Nikula589c3562010-12-06 16:27:07 +02001159 /* machine controls, routes and widgets are not prefixed */
1160 temp = codec->name_prefix;
1161 codec->name_prefix = NULL;
1162
1163 /* do machine specific initialization */
1164 if (!dailess && dai_link->init)
1165 ret = dai_link->init(rtd);
1166 else if (dailess && aux_dev->init)
1167 ret = aux_dev->init(&codec->dapm);
1168 if (ret < 0) {
1169 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1170 return ret;
1171 }
1172 codec->name_prefix = temp;
1173
Jarkko Nikula589c3562010-12-06 16:27:07 +02001174 /* register the rtd device */
1175 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001176
1177 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1178 if (!rtd->dev)
1179 return -ENOMEM;
1180 device_initialize(rtd->dev);
1181 rtd->dev->parent = card->dev;
1182 rtd->dev->release = rtd_release;
1183 rtd->dev->init_name = name;
1184 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001185 mutex_init(&rtd->pcm_mutex);
Mark Brown36ae1a92012-01-06 17:12:45 -08001186 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001187 if (ret < 0) {
1188 dev_err(card->dev,
1189 "asoc: failed to register runtime device: %d\n", ret);
1190 return ret;
1191 }
1192 rtd->dev_registered = 1;
1193
1194 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001195 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001196 if (ret < 0)
1197 dev_err(codec->dev,
1198 "asoc: failed to add codec dapm sysfs entries: %d\n",
1199 ret);
1200
1201 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001202 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001203 if (ret < 0)
1204 dev_err(codec->dev,
1205 "asoc: failed to add codec sysfs files: %d\n", ret);
1206
1207 return 0;
1208}
1209
Liam Girdwood0168bf02011-06-07 16:08:05 +01001210static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001211{
1212 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1213 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1214 struct snd_soc_codec *codec = rtd->codec;
1215 struct snd_soc_platform *platform = rtd->platform;
1216 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1217 int ret;
1218
Liam Girdwood0168bf02011-06-07 16:08:05 +01001219 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1220 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001221
1222 /* config components */
1223 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001224 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001225 codec_dai->card = card;
1226 cpu_dai->card = card;
1227
1228 /* set default power off timeout */
1229 rtd->pmdown_time = pmdown_time;
1230
1231 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001232 if (!cpu_dai->probed &&
1233 cpu_dai->driver->probe_order == order) {
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001234 cpu_dai->dapm.card = card;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001235 if (!try_module_get(cpu_dai->dev->driver->owner))
1236 return -ENODEV;
1237
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001238 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1239
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001240 if (cpu_dai->driver->probe) {
1241 ret = cpu_dai->driver->probe(cpu_dai);
1242 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001243 pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1244 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001245 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001246 return ret;
1247 }
1248 }
1249 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001250 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001251 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1252 }
1253
1254 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001255 if (!codec->probed &&
1256 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001257 ret = soc_probe_codec(card, codec);
1258 if (ret < 0)
1259 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001260 }
1261
1262 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001263 if (!platform->probed &&
1264 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001265 ret = soc_probe_platform(card, platform);
1266 if (ret < 0)
1267 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001268 }
1269
1270 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001271 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001272 if (codec_dai->driver->probe) {
1273 ret = codec_dai->driver->probe(codec_dai);
1274 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001275 pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1276 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001277 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001278 }
1279 }
1280
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001281 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001282 codec_dai->probed = 1;
1283 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001284 }
1285
Liam Girdwood0168bf02011-06-07 16:08:05 +01001286 /* complete DAI probe during last probe */
1287 if (order != SND_SOC_COMP_ORDER_LAST)
1288 return 0;
1289
Jarkko Nikula589c3562010-12-06 16:27:07 +02001290 ret = soc_post_component_init(card, codec, num, 0);
1291 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001292 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001293
Mark Brown36ae1a92012-01-06 17:12:45 -08001294 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001295 if (ret < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -02001296 pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001297
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001298 /* create the pcm */
1299 ret = soc_new_pcm(rtd, num);
1300 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001301 pr_err("asoc: can't create pcm %s :%d\n",
1302 dai_link->stream_name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001303 return ret;
1304 }
1305
1306 /* add platform data for AC97 devices */
1307 if (rtd->codec_dai->driver->ac97_control)
1308 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1309
1310 return 0;
1311}
1312
1313#ifdef CONFIG_SND_SOC_AC97_BUS
1314static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1315{
1316 int ret;
1317
1318 /* Only instantiate AC97 if not already done by the adaptor
1319 * for the generic AC97 subsystem.
1320 */
1321 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001322 /*
1323 * It is possible that the AC97 device is already registered to
1324 * the device subsystem. This happens when the device is created
1325 * via snd_ac97_mixer(). Currently only SoC codec that does so
1326 * is the generic AC97 glue but others migh emerge.
1327 *
1328 * In those cases we don't try to register the device again.
1329 */
1330 if (!rtd->codec->ac97_created)
1331 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001332
1333 ret = soc_ac97_dev_register(rtd->codec);
1334 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001335 pr_err("asoc: AC97 device register failed:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001336 return ret;
1337 }
1338
1339 rtd->codec->ac97_registered = 1;
1340 }
1341 return 0;
1342}
1343
1344static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1345{
1346 if (codec->ac97_registered) {
1347 soc_ac97_dev_unregister(codec);
1348 codec->ac97_registered = 0;
1349 }
1350}
1351#endif
1352
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001353static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1354{
1355 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001356 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001357 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001358
1359 /* find CODEC from registered CODECs*/
1360 list_for_each_entry(codec, &codec_list, list) {
1361 if (!strcmp(codec->name, aux_dev->codec_name)) {
1362 if (codec->probed) {
1363 dev_err(codec->dev,
1364 "asoc: codec already probed");
1365 ret = -EBUSY;
1366 goto out;
1367 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001368 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001369 }
1370 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001371 /* codec not found */
1372 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1373 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001374
Jarkko Nikula676ad982010-12-03 09:18:22 +02001375found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001376 ret = soc_probe_codec(card, codec);
1377 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001378 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001379
Jarkko Nikula589c3562010-12-06 16:27:07 +02001380 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001381
1382out:
1383 return ret;
1384}
1385
1386static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1387{
1388 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1389 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001390
1391 /* unregister the rtd device */
1392 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001393 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1394 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001395 rtd->dev_registered = 0;
1396 }
1397
Jarkko Nikula589c3562010-12-06 16:27:07 +02001398 if (codec && codec->probed)
1399 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001400}
1401
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001402static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1403 enum snd_soc_compress_type compress_type)
1404{
1405 int ret;
1406
1407 if (codec->cache_init)
1408 return 0;
1409
1410 /* override the compress_type if necessary */
1411 if (compress_type && codec->compress_type != compress_type)
1412 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001413 ret = snd_soc_cache_init(codec);
1414 if (ret < 0) {
1415 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1416 ret);
1417 return ret;
1418 }
1419 codec->cache_init = 1;
1420 return 0;
1421}
1422
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001423static void snd_soc_instantiate_card(struct snd_soc_card *card)
1424{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001425 struct snd_soc_codec *codec;
1426 struct snd_soc_codec_conf *codec_conf;
1427 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001428 struct snd_soc_dai_link *dai_link;
Liam Girdwood0168bf02011-06-07 16:08:05 +01001429 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001430
Liam Girdwood01b9d992012-03-07 10:38:25 +00001431 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001432
1433 if (card->instantiated) {
1434 mutex_unlock(&card->mutex);
1435 return;
1436 }
1437
1438 /* bind DAIs */
1439 for (i = 0; i < card->num_links; i++)
1440 soc_bind_dai_link(card, i);
1441
1442 /* bind completed ? */
1443 if (card->num_rtd != card->num_links) {
1444 mutex_unlock(&card->mutex);
1445 return;
1446 }
1447
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001448 /* initialize the register cache for each available codec */
1449 list_for_each_entry(codec, &codec_list, list) {
1450 if (codec->cache_init)
1451 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001452 /* by default we don't override the compress_type */
1453 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001454 /* check to see if we need to override the compress_type */
1455 for (i = 0; i < card->num_configs; ++i) {
1456 codec_conf = &card->codec_conf[i];
1457 if (!strcmp(codec->name, codec_conf->dev_name)) {
1458 compress_type = codec_conf->compress_type;
1459 if (compress_type && compress_type
1460 != codec->compress_type)
1461 break;
1462 }
1463 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001464 ret = snd_soc_init_codec_cache(codec, compress_type);
1465 if (ret < 0) {
1466 mutex_unlock(&card->mutex);
1467 return;
1468 }
1469 }
1470
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001471 /* card bind complete so register a sound card */
1472 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1473 card->owner, 0, &card->snd_card);
1474 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001475 pr_err("asoc: can't create sound card for card %s: %d\n",
1476 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001477 mutex_unlock(&card->mutex);
1478 return;
1479 }
1480 card->snd_card->dev = card->dev;
1481
Mark Browne37a4972011-03-02 18:21:57 +00001482 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1483 card->dapm.dev = card->dev;
1484 card->dapm.card = card;
1485 list_add(&card->dapm.list, &card->dapm_list);
1486
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001487#ifdef CONFIG_DEBUG_FS
1488 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1489#endif
1490
Mark Brown88ee1c62011-02-02 10:43:26 +00001491#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001492 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001493 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001494#endif
Andy Green6ed25972008-06-13 16:24:05 +01001495
Mark Brown9a841eb2011-04-12 17:51:37 -07001496 if (card->dapm_widgets)
1497 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1498 card->num_dapm_widgets);
1499
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001500 /* initialise the sound card only once */
1501 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001502 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001503 if (ret < 0)
1504 goto card_probe_error;
1505 }
1506
Liam Girdwood0168bf02011-06-07 16:08:05 +01001507 /* early DAI link probe */
1508 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1509 order++) {
1510 for (i = 0; i < card->num_links; i++) {
1511 ret = soc_probe_dai_link(card, i, order);
1512 if (ret < 0) {
1513 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001514 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001515 goto probe_dai_err;
1516 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001517 }
1518 }
1519
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001520 for (i = 0; i < card->num_aux_devs; i++) {
1521 ret = soc_probe_aux_dev(card, i);
1522 if (ret < 0) {
1523 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1524 card->name, ret);
1525 goto probe_aux_dev_err;
1526 }
1527 }
1528
Mark Brown888df392012-02-16 19:37:51 -08001529 snd_soc_dapm_link_dai_widgets(card);
1530
Mark Brownb7af1da2011-04-07 19:18:44 +09001531 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001532 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001533
Mark Brownb8ad29d2011-03-02 18:35:51 +00001534 if (card->dapm_routes)
1535 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1536 card->num_dapm_routes);
1537
Mark Brownb90d2f92011-10-10 13:38:06 +01001538 snd_soc_dapm_new_widgets(&card->dapm);
1539
Mark Brown75d9ac42011-09-27 16:41:01 +01001540 for (i = 0; i < card->num_links; i++) {
1541 dai_link = &card->dai_link[i];
1542
1543 if (dai_link->dai_fmt) {
1544 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1545 dai_link->dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001546 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001547 dev_warn(card->rtd[i].codec_dai->dev,
1548 "Failed to set DAI format: %d\n",
1549 ret);
1550
1551 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1552 dai_link->dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001553 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001554 dev_warn(card->rtd[i].cpu_dai->dev,
1555 "Failed to set DAI format: %d\n",
1556 ret);
1557 }
1558 }
1559
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001560 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001561 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001562 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1563 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001564 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1565 "%s", card->driver_name ? card->driver_name : card->name);
1566 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1567 switch (card->snd_card->driver[i]) {
1568 case '_':
1569 case '-':
1570 case '\0':
1571 break;
1572 default:
1573 if (!isalnum(card->snd_card->driver[i]))
1574 card->snd_card->driver[i] = '_';
1575 break;
1576 }
1577 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001578
Mark Brown28e9ad92011-03-02 18:36:34 +00001579 if (card->late_probe) {
1580 ret = card->late_probe(card);
1581 if (ret < 0) {
1582 dev_err(card->dev, "%s late_probe() failed: %d\n",
1583 card->name, ret);
1584 goto probe_aux_dev_err;
1585 }
1586 }
1587
Mark Brown2dc00212011-10-08 13:59:44 +01001588 snd_soc_dapm_new_widgets(&card->dapm);
1589
Stephen Warren16332812011-11-23 12:42:04 -07001590 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001591 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001592 snd_soc_dapm_auto_nc_codec_pins(codec);
1593
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001594 ret = snd_card_register(card->snd_card);
1595 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001596 pr_err("asoc: failed to register soundcard for %s: %d\n",
1597 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001598 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001599 }
1600
1601#ifdef CONFIG_SND_SOC_AC97_BUS
1602 /* register any AC97 codecs */
1603 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001604 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1605 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001606 pr_err("asoc: failed to register AC97 %s: %d\n",
1607 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001608 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001609 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001610 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001611 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001612 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001613#endif
1614
Mark Brown435c5e22008-12-04 15:32:53 +00001615 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001616 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001617 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001618 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001619
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001620probe_aux_dev_err:
1621 for (i = 0; i < card->num_aux_devs; i++)
1622 soc_remove_aux_dev(card, i);
1623
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001624probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001625 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001626
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001627card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001628 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001629 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001630
1631 snd_card_free(card->snd_card);
1632
1633 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001634}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001635
Mark Brown435c5e22008-12-04 15:32:53 +00001636/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001637 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001638 * client_mutex.
1639 */
1640static void snd_soc_instantiate_cards(void)
1641{
1642 struct snd_soc_card *card;
1643 list_for_each_entry(card, &card_list, list)
1644 snd_soc_instantiate_card(card);
1645}
1646
1647/* probes a new socdev */
1648static int soc_probe(struct platform_device *pdev)
1649{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001650 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001651 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001652
Vinod Koul70a7ca32011-01-14 19:22:48 +05301653 /*
1654 * no card, so machine driver should be registering card
1655 * we should not be here in that case so ret error
1656 */
1657 if (!card)
1658 return -EINVAL;
1659
Mark Brownfe4085e2012-03-02 13:07:41 +00001660 dev_warn(&pdev->dev,
1661 "ASoC machine %s should use snd_soc_register_card()\n",
1662 card->name);
1663
Mark Brown435c5e22008-12-04 15:32:53 +00001664 /* Bodge while we unpick instantiation */
1665 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001666
Mark Brown435c5e22008-12-04 15:32:53 +00001667 ret = snd_soc_register_card(card);
1668 if (ret != 0) {
1669 dev_err(&pdev->dev, "Failed to register card\n");
1670 return ret;
1671 }
1672
1673 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001674}
1675
Vinod Koulb0e26482011-01-13 22:48:02 +05301676static int soc_cleanup_card_resources(struct snd_soc_card *card)
1677{
Vinod Koulb0e26482011-01-13 22:48:02 +05301678 int i;
1679
1680 /* make sure any delayed work runs */
1681 for (i = 0; i < card->num_rtd; i++) {
1682 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1683 flush_delayed_work_sync(&rtd->delayed_work);
1684 }
1685
1686 /* remove auxiliary devices */
1687 for (i = 0; i < card->num_aux_devs; i++)
1688 soc_remove_aux_dev(card, i);
1689
1690 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001691 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301692
1693 soc_cleanup_card_debugfs(card);
1694
1695 /* remove the card */
1696 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001697 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301698
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001699 snd_soc_dapm_free(&card->dapm);
1700
Vinod Koulb0e26482011-01-13 22:48:02 +05301701 snd_card_free(card->snd_card);
1702 return 0;
1703
1704}
1705
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001706/* removes a socdev */
1707static int soc_remove(struct platform_device *pdev)
1708{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001709 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001710
Mark Brownc5af3a22008-11-28 13:29:45 +00001711 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001712 return 0;
1713}
1714
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001715int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001716{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001717 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001718 int i;
Mark Brown51737472009-06-22 13:16:51 +01001719
1720 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001721 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001722
1723 /* Flush out pmdown_time work - we actually do want to run it
1724 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001725 for (i = 0; i < card->num_rtd; i++) {
1726 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001727 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001728 }
Mark Brown51737472009-06-22 13:16:51 +01001729
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001730 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001731
1732 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001733}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001734EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001735
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001736const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301737 .suspend = snd_soc_suspend,
1738 .resume = snd_soc_resume,
1739 .freeze = snd_soc_suspend,
1740 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001741 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301742 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001743};
Stephen Warrendeb26072011-04-05 19:35:30 -06001744EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001745
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001746/* ASoC platform driver */
1747static struct platform_driver soc_driver = {
1748 .driver = {
1749 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001750 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001751 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001752 },
1753 .probe = soc_probe,
1754 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001755};
1756
Mark Brown096e49d2009-07-05 15:12:22 +01001757/**
1758 * snd_soc_codec_volatile_register: Report if a register is volatile.
1759 *
1760 * @codec: CODEC to query.
1761 * @reg: Register to query.
1762 *
1763 * Boolean function indiciating if a CODEC register is volatile.
1764 */
Mark Brown181e0552011-01-24 14:05:25 +00001765int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1766 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001767{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001768 if (codec->volatile_register)
1769 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001770 else
1771 return 0;
1772}
1773EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1774
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001775/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001776 * snd_soc_codec_readable_register: Report if a register is readable.
1777 *
1778 * @codec: CODEC to query.
1779 * @reg: Register to query.
1780 *
1781 * Boolean function indicating if a CODEC register is readable.
1782 */
1783int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1784 unsigned int reg)
1785{
1786 if (codec->readable_register)
1787 return codec->readable_register(codec, reg);
1788 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001789 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001790}
1791EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1792
1793/**
1794 * snd_soc_codec_writable_register: Report if a register is writable.
1795 *
1796 * @codec: CODEC to query.
1797 * @reg: Register to query.
1798 *
1799 * Boolean function indicating if a CODEC register is writable.
1800 */
1801int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1802 unsigned int reg)
1803{
1804 if (codec->writable_register)
1805 return codec->writable_register(codec, reg);
1806 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001807 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001808}
1809EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1810
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001811int snd_soc_platform_read(struct snd_soc_platform *platform,
1812 unsigned int reg)
1813{
1814 unsigned int ret;
1815
1816 if (!platform->driver->read) {
1817 dev_err(platform->dev, "platform has no read back\n");
1818 return -1;
1819 }
1820
1821 ret = platform->driver->read(platform, reg);
1822 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001823 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001824
1825 return ret;
1826}
1827EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1828
1829int snd_soc_platform_write(struct snd_soc_platform *platform,
1830 unsigned int reg, unsigned int val)
1831{
1832 if (!platform->driver->write) {
1833 dev_err(platform->dev, "platform has no write back\n");
1834 return -1;
1835 }
1836
1837 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001838 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001839 return platform->driver->write(platform, reg, val);
1840}
1841EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1842
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001843/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001844 * snd_soc_new_ac97_codec - initailise AC97 device
1845 * @codec: audio codec
1846 * @ops: AC97 bus operations
1847 * @num: AC97 codec number
1848 *
1849 * Initialises AC97 codec resources for use by ad-hoc devices only.
1850 */
1851int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1852 struct snd_ac97_bus_ops *ops, int num)
1853{
1854 mutex_lock(&codec->mutex);
1855
1856 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1857 if (codec->ac97 == NULL) {
1858 mutex_unlock(&codec->mutex);
1859 return -ENOMEM;
1860 }
1861
1862 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1863 if (codec->ac97->bus == NULL) {
1864 kfree(codec->ac97);
1865 codec->ac97 = NULL;
1866 mutex_unlock(&codec->mutex);
1867 return -ENOMEM;
1868 }
1869
1870 codec->ac97->bus->ops = ops;
1871 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001872
1873 /*
1874 * Mark the AC97 device to be created by us. This way we ensure that the
1875 * device will be registered with the device subsystem later on.
1876 */
1877 codec->ac97_created = 1;
1878
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001879 mutex_unlock(&codec->mutex);
1880 return 0;
1881}
1882EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1883
1884/**
1885 * snd_soc_free_ac97_codec - free AC97 codec device
1886 * @codec: audio codec
1887 *
1888 * Frees AC97 codec device resources.
1889 */
1890void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1891{
1892 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001893#ifdef CONFIG_SND_SOC_AC97_BUS
1894 soc_unregister_ac97_dai_link(codec);
1895#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001896 kfree(codec->ac97->bus);
1897 kfree(codec->ac97);
1898 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001899 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001900 mutex_unlock(&codec->mutex);
1901}
1902EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1903
Mark Brownc3753702010-11-01 15:41:57 -04001904unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1905{
1906 unsigned int ret;
1907
Mark Brownc3acec22010-12-02 16:15:29 +00001908 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04001909 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04001910 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04001911
1912 return ret;
1913}
1914EXPORT_SYMBOL_GPL(snd_soc_read);
1915
1916unsigned int snd_soc_write(struct snd_soc_codec *codec,
1917 unsigned int reg, unsigned int val)
1918{
1919 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04001920 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00001921 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04001922}
1923EXPORT_SYMBOL_GPL(snd_soc_write);
1924
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00001925unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1926 unsigned int reg, const void *data, size_t len)
1927{
1928 return codec->bulk_write_raw(codec, reg, data, len);
1929}
1930EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1931
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001932/**
1933 * snd_soc_update_bits - update codec register bits
1934 * @codec: audio codec
1935 * @reg: codec register
1936 * @mask: register mask
1937 * @value: new value
1938 *
1939 * Writes new register value.
1940 *
Timur Tabi180c3292011-01-10 15:58:13 -06001941 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001942 */
1943int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001944 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001945{
Mark Brown8a713da2011-12-03 12:33:55 +00001946 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001947 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06001948 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001949
Mark Brown8a713da2011-12-03 12:33:55 +00001950 if (codec->using_regmap) {
1951 ret = regmap_update_bits_check(codec->control_data, reg,
1952 mask, value, &change);
1953 } else {
1954 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06001955 if (ret < 0)
1956 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00001957
1958 old = ret;
1959 new = (old & ~mask) | (value & mask);
1960 change = old != new;
1961 if (change)
1962 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06001963 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001964
Mark Brown8a713da2011-12-03 12:33:55 +00001965 if (ret < 0)
1966 return ret;
1967
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001968 return change;
1969}
1970EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1971
1972/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001973 * snd_soc_update_bits_locked - update codec register bits
1974 * @codec: audio codec
1975 * @reg: codec register
1976 * @mask: register mask
1977 * @value: new value
1978 *
1979 * Writes new register value, and takes the codec mutex.
1980 *
1981 * Returns 1 for change else 0.
1982 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001983int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1984 unsigned short reg, unsigned int mask,
1985 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001986{
1987 int change;
1988
1989 mutex_lock(&codec->mutex);
1990 change = snd_soc_update_bits(codec, reg, mask, value);
1991 mutex_unlock(&codec->mutex);
1992
1993 return change;
1994}
Mark Browndd1b3d52009-12-04 14:22:03 +00001995EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001996
1997/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001998 * snd_soc_test_bits - test register for change
1999 * @codec: audio codec
2000 * @reg: codec register
2001 * @mask: register mask
2002 * @value: new value
2003 *
2004 * Tests a register with a new value and checks if the new value is
2005 * different from the old value.
2006 *
2007 * Returns 1 for change else 0.
2008 */
2009int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002010 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002011{
2012 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002013 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002014
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002015 old = snd_soc_read(codec, reg);
2016 new = (old & ~mask) | value;
2017 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002018
2019 return change;
2020}
2021EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2022
2023/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002024 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2025 * @substream: the pcm substream
2026 * @hw: the hardware parameters
2027 *
2028 * Sets the substream runtime hardware parameters.
2029 */
2030int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2031 const struct snd_pcm_hardware *hw)
2032{
2033 struct snd_pcm_runtime *runtime = substream->runtime;
2034 runtime->hw.info = hw->info;
2035 runtime->hw.formats = hw->formats;
2036 runtime->hw.period_bytes_min = hw->period_bytes_min;
2037 runtime->hw.period_bytes_max = hw->period_bytes_max;
2038 runtime->hw.periods_min = hw->periods_min;
2039 runtime->hw.periods_max = hw->periods_max;
2040 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2041 runtime->hw.fifo_size = hw->fifo_size;
2042 return 0;
2043}
2044EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2045
2046/**
2047 * snd_soc_cnew - create new control
2048 * @_template: control template
2049 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002050 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002051 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002052 *
2053 * Create a new mixer control from a template control.
2054 *
2055 * Returns 0 for success, else error.
2056 */
2057struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002058 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002059 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002060{
2061 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002062 struct snd_kcontrol *kcontrol;
2063 char *name = NULL;
2064 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002065
2066 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002067 template.index = 0;
2068
Mark Brownefb7ac32011-03-08 17:23:24 +00002069 if (!long_name)
2070 long_name = template.name;
2071
2072 if (prefix) {
2073 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002074 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002075 if (!name)
2076 return NULL;
2077
2078 snprintf(name, name_len, "%s %s", prefix, long_name);
2079
2080 template.name = name;
2081 } else {
2082 template.name = long_name;
2083 }
2084
2085 kcontrol = snd_ctl_new1(&template, data);
2086
2087 kfree(name);
2088
2089 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002090}
2091EXPORT_SYMBOL_GPL(snd_soc_cnew);
2092
Liam Girdwood022658b2012-02-03 17:43:09 +00002093static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2094 const struct snd_kcontrol_new *controls, int num_controls,
2095 const char *prefix, void *data)
2096{
2097 int err, i;
2098
2099 for (i = 0; i < num_controls; i++) {
2100 const struct snd_kcontrol_new *control = &controls[i];
2101 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2102 control->name, prefix));
2103 if (err < 0) {
2104 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2105 return err;
2106 }
2107 }
2108
2109 return 0;
2110}
2111
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002112/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002113 * snd_soc_add_codec_controls - add an array of controls to a codec.
2114 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002115 * duplicating this code.
2116 *
2117 * @codec: codec to add controls to
2118 * @controls: array of controls to add
2119 * @num_controls: number of elements in the array
2120 *
2121 * Return 0 for success, else error.
2122 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002123int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002124 const struct snd_kcontrol_new *controls, int num_controls)
2125{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002126 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002127
Liam Girdwood022658b2012-02-03 17:43:09 +00002128 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2129 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002130}
Liam Girdwood022658b2012-02-03 17:43:09 +00002131EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002132
2133/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002134 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002135 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002136 *
2137 * @platform: platform to add controls to
2138 * @controls: array of controls to add
2139 * @num_controls: number of elements in the array
2140 *
2141 * Return 0 for success, else error.
2142 */
2143int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2144 const struct snd_kcontrol_new *controls, int num_controls)
2145{
2146 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002147
Liam Girdwood022658b2012-02-03 17:43:09 +00002148 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2149 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002150}
2151EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2152
2153/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002154 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2155 * Convenience function to add a list of controls.
2156 *
2157 * @soc_card: SoC card to add controls to
2158 * @controls: array of controls to add
2159 * @num_controls: number of elements in the array
2160 *
2161 * Return 0 for success, else error.
2162 */
2163int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2164 const struct snd_kcontrol_new *controls, int num_controls)
2165{
2166 struct snd_card *card = soc_card->snd_card;
2167
2168 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2169 NULL, soc_card);
2170}
2171EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2172
2173/**
2174 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2175 * Convienience function to add a list of controls.
2176 *
2177 * @dai: DAI to add controls to
2178 * @controls: array of controls to add
2179 * @num_controls: number of elements in the array
2180 *
2181 * Return 0 for success, else error.
2182 */
2183int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2184 const struct snd_kcontrol_new *controls, int num_controls)
2185{
2186 struct snd_card *card = dai->card->snd_card;
2187
2188 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2189 NULL, dai);
2190}
2191EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2192
2193/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002194 * snd_soc_info_enum_double - enumerated double mixer info callback
2195 * @kcontrol: mixer control
2196 * @uinfo: control element information
2197 *
2198 * Callback to provide information about a double enumerated
2199 * mixer control.
2200 *
2201 * Returns 0 for success.
2202 */
2203int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2204 struct snd_ctl_elem_info *uinfo)
2205{
2206 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2207
2208 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2209 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002210 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002211
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002212 if (uinfo->value.enumerated.item > e->max - 1)
2213 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002214 strcpy(uinfo->value.enumerated.name,
2215 e->texts[uinfo->value.enumerated.item]);
2216 return 0;
2217}
2218EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2219
2220/**
2221 * snd_soc_get_enum_double - enumerated double mixer get callback
2222 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002223 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002224 *
2225 * Callback to get the value of a double enumerated mixer.
2226 *
2227 * Returns 0 for success.
2228 */
2229int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2230 struct snd_ctl_elem_value *ucontrol)
2231{
2232 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2233 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002234 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002235
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002236 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002237 ;
2238 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002239 ucontrol->value.enumerated.item[0]
2240 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002241 if (e->shift_l != e->shift_r)
2242 ucontrol->value.enumerated.item[1] =
2243 (val >> e->shift_r) & (bitmask - 1);
2244
2245 return 0;
2246}
2247EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2248
2249/**
2250 * snd_soc_put_enum_double - enumerated double mixer put callback
2251 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002252 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002253 *
2254 * Callback to set the value of a double enumerated mixer.
2255 *
2256 * Returns 0 for success.
2257 */
2258int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2259 struct snd_ctl_elem_value *ucontrol)
2260{
2261 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2262 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002263 unsigned int val;
2264 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002265
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002266 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002267 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002268 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002269 return -EINVAL;
2270 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2271 mask = (bitmask - 1) << e->shift_l;
2272 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002273 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002274 return -EINVAL;
2275 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2276 mask |= (bitmask - 1) << e->shift_r;
2277 }
2278
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002279 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002280}
2281EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2282
2283/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002284 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2285 * @kcontrol: mixer control
2286 * @ucontrol: control element information
2287 *
2288 * Callback to get the value of a double semi enumerated mixer.
2289 *
2290 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2291 * used for handling bitfield coded enumeration for example.
2292 *
2293 * Returns 0 for success.
2294 */
2295int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2296 struct snd_ctl_elem_value *ucontrol)
2297{
2298 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002299 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002300 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002301
2302 reg_val = snd_soc_read(codec, e->reg);
2303 val = (reg_val >> e->shift_l) & e->mask;
2304 for (mux = 0; mux < e->max; mux++) {
2305 if (val == e->values[mux])
2306 break;
2307 }
2308 ucontrol->value.enumerated.item[0] = mux;
2309 if (e->shift_l != e->shift_r) {
2310 val = (reg_val >> e->shift_r) & e->mask;
2311 for (mux = 0; mux < e->max; mux++) {
2312 if (val == e->values[mux])
2313 break;
2314 }
2315 ucontrol->value.enumerated.item[1] = mux;
2316 }
2317
2318 return 0;
2319}
2320EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2321
2322/**
2323 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2324 * @kcontrol: mixer control
2325 * @ucontrol: control element information
2326 *
2327 * Callback to set the value of a double semi enumerated mixer.
2328 *
2329 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2330 * used for handling bitfield coded enumeration for example.
2331 *
2332 * Returns 0 for success.
2333 */
2334int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2335 struct snd_ctl_elem_value *ucontrol)
2336{
2337 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002338 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002339 unsigned int val;
2340 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002341
2342 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2343 return -EINVAL;
2344 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2345 mask = e->mask << e->shift_l;
2346 if (e->shift_l != e->shift_r) {
2347 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2348 return -EINVAL;
2349 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2350 mask |= e->mask << e->shift_r;
2351 }
2352
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002353 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002354}
2355EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2356
2357/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002358 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2359 * @kcontrol: mixer control
2360 * @uinfo: control element information
2361 *
2362 * Callback to provide information about an external enumerated
2363 * single mixer.
2364 *
2365 * Returns 0 for success.
2366 */
2367int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2368 struct snd_ctl_elem_info *uinfo)
2369{
2370 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2371
2372 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2373 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002374 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002375
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002376 if (uinfo->value.enumerated.item > e->max - 1)
2377 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002378 strcpy(uinfo->value.enumerated.name,
2379 e->texts[uinfo->value.enumerated.item]);
2380 return 0;
2381}
2382EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2383
2384/**
2385 * snd_soc_info_volsw_ext - external single mixer info callback
2386 * @kcontrol: mixer control
2387 * @uinfo: control element information
2388 *
2389 * Callback to provide information about a single external mixer control.
2390 *
2391 * Returns 0 for success.
2392 */
2393int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2394 struct snd_ctl_elem_info *uinfo)
2395{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002396 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002397
Mark Brownfd5dfad2009-04-15 21:37:46 +01002398 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002399 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2400 else
2401 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2402
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002403 uinfo->count = 1;
2404 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002405 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002406 return 0;
2407}
2408EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2409
2410/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002411 * snd_soc_info_volsw - single mixer info callback
2412 * @kcontrol: mixer control
2413 * @uinfo: control element information
2414 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002415 * Callback to provide information about a single mixer control, or a double
2416 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002417 *
2418 * Returns 0 for success.
2419 */
2420int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2421 struct snd_ctl_elem_info *uinfo)
2422{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002423 struct soc_mixer_control *mc =
2424 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002425 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002426
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002427 if (!mc->platform_max)
2428 mc->platform_max = mc->max;
2429 platform_max = mc->platform_max;
2430
2431 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002432 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2433 else
2434 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2435
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002436 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002437 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002438 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002439 return 0;
2440}
2441EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2442
2443/**
2444 * snd_soc_get_volsw - single mixer get callback
2445 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002446 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002447 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002448 * Callback to get the value of a single mixer control, or a double mixer
2449 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002450 *
2451 * Returns 0 for success.
2452 */
2453int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2454 struct snd_ctl_elem_value *ucontrol)
2455{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002456 struct soc_mixer_control *mc =
2457 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002458 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002459 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002460 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002461 unsigned int shift = mc->shift;
2462 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002463 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002464 unsigned int mask = (1 << fls(max)) - 1;
2465 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002466
2467 ucontrol->value.integer.value[0] =
2468 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002469 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002470 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002471 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002472
2473 if (snd_soc_volsw_is_stereo(mc)) {
2474 if (reg == reg2)
2475 ucontrol->value.integer.value[1] =
2476 (snd_soc_read(codec, reg) >> rshift) & mask;
2477 else
2478 ucontrol->value.integer.value[1] =
2479 (snd_soc_read(codec, reg2) >> shift) & mask;
2480 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002481 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002482 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002483 }
2484
2485 return 0;
2486}
2487EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2488
2489/**
2490 * snd_soc_put_volsw - single mixer put callback
2491 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002492 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002493 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002494 * Callback to set the value of a single mixer control, or a double mixer
2495 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002496 *
2497 * Returns 0 for success.
2498 */
2499int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2500 struct snd_ctl_elem_value *ucontrol)
2501{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002502 struct soc_mixer_control *mc =
2503 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002504 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002505 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002506 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002507 unsigned int shift = mc->shift;
2508 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002509 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002510 unsigned int mask = (1 << fls(max)) - 1;
2511 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002512 int err;
2513 bool type_2r = 0;
2514 unsigned int val2 = 0;
2515 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002516
2517 val = (ucontrol->value.integer.value[0] & mask);
2518 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002519 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002520 val_mask = mask << shift;
2521 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002522 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002523 val2 = (ucontrol->value.integer.value[1] & mask);
2524 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002525 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002526 if (reg == reg2) {
2527 val_mask |= mask << rshift;
2528 val |= val2 << rshift;
2529 } else {
2530 val2 = val2 << shift;
2531 type_2r = 1;
2532 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002533 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002534 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002535 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002536 return err;
2537
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002538 if (type_2r)
2539 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2540
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002541 return err;
2542}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002543EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002544
Mark Browne13ac2e2008-05-28 17:58:05 +01002545/**
2546 * snd_soc_info_volsw_s8 - signed mixer info callback
2547 * @kcontrol: mixer control
2548 * @uinfo: control element information
2549 *
2550 * Callback to provide information about a signed mixer control.
2551 *
2552 * Returns 0 for success.
2553 */
2554int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2555 struct snd_ctl_elem_info *uinfo)
2556{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002557 struct soc_mixer_control *mc =
2558 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002559 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002560 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002561
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002562 if (!mc->platform_max)
2563 mc->platform_max = mc->max;
2564 platform_max = mc->platform_max;
2565
Mark Browne13ac2e2008-05-28 17:58:05 +01002566 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2567 uinfo->count = 2;
2568 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002569 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002570 return 0;
2571}
2572EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2573
2574/**
2575 * snd_soc_get_volsw_s8 - signed mixer get callback
2576 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002577 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002578 *
2579 * Callback to get the value of a signed mixer control.
2580 *
2581 * Returns 0 for success.
2582 */
2583int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2584 struct snd_ctl_elem_value *ucontrol)
2585{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002586 struct soc_mixer_control *mc =
2587 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002588 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002589 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002590 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002591 int val = snd_soc_read(codec, reg);
2592
2593 ucontrol->value.integer.value[0] =
2594 ((signed char)(val & 0xff))-min;
2595 ucontrol->value.integer.value[1] =
2596 ((signed char)((val >> 8) & 0xff))-min;
2597 return 0;
2598}
2599EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2600
2601/**
2602 * snd_soc_put_volsw_sgn - signed mixer put callback
2603 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002604 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002605 *
2606 * Callback to set the value of a signed mixer control.
2607 *
2608 * Returns 0 for success.
2609 */
2610int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2611 struct snd_ctl_elem_value *ucontrol)
2612{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002613 struct soc_mixer_control *mc =
2614 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002615 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002616 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002617 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002618 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002619
2620 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2621 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2622
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002623 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002624}
2625EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2626
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002627/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002628 * snd_soc_limit_volume - Set new limit to an existing volume control.
2629 *
2630 * @codec: where to look for the control
2631 * @name: Name of the control
2632 * @max: new maximum limit
2633 *
2634 * Return 0 for success, else error.
2635 */
2636int snd_soc_limit_volume(struct snd_soc_codec *codec,
2637 const char *name, int max)
2638{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002639 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002640 struct snd_kcontrol *kctl;
2641 struct soc_mixer_control *mc;
2642 int found = 0;
2643 int ret = -EINVAL;
2644
2645 /* Sanity check for name and max */
2646 if (unlikely(!name || max <= 0))
2647 return -EINVAL;
2648
2649 list_for_each_entry(kctl, &card->controls, list) {
2650 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2651 found = 1;
2652 break;
2653 }
2654 }
2655 if (found) {
2656 mc = (struct soc_mixer_control *)kctl->private_value;
2657 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002658 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002659 ret = 0;
2660 }
2661 }
2662 return ret;
2663}
2664EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2665
2666/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002667 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2668 * mixer info callback
2669 * @kcontrol: mixer control
2670 * @uinfo: control element information
2671 *
2672 * Returns 0 for success.
2673 */
2674int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2675 struct snd_ctl_elem_info *uinfo)
2676{
2677 struct soc_mixer_control *mc =
2678 (struct soc_mixer_control *)kcontrol->private_value;
2679 int max = mc->max;
2680 int min = mc->min;
2681
2682 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2683 uinfo->count = 2;
2684 uinfo->value.integer.min = 0;
2685 uinfo->value.integer.max = max-min;
2686
2687 return 0;
2688}
2689EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2690
2691/**
2692 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2693 * mixer get callback
2694 * @kcontrol: mixer control
2695 * @uinfo: control element information
2696 *
2697 * Returns 0 for success.
2698 */
2699int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2700 struct snd_ctl_elem_value *ucontrol)
2701{
2702 struct soc_mixer_control *mc =
2703 (struct soc_mixer_control *)kcontrol->private_value;
2704 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2705 unsigned int mask = (1<<mc->shift)-1;
2706 int min = mc->min;
2707 int val = snd_soc_read(codec, mc->reg) & mask;
2708 int valr = snd_soc_read(codec, mc->rreg) & mask;
2709
Stuart Longland20630c72010-06-18 12:56:10 +10002710 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2711 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002712 return 0;
2713}
2714EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2715
2716/**
2717 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2718 * mixer put callback
2719 * @kcontrol: mixer control
2720 * @uinfo: control element information
2721 *
2722 * Returns 0 for success.
2723 */
2724int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2725 struct snd_ctl_elem_value *ucontrol)
2726{
2727 struct soc_mixer_control *mc =
2728 (struct soc_mixer_control *)kcontrol->private_value;
2729 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2730 unsigned int mask = (1<<mc->shift)-1;
2731 int min = mc->min;
2732 int ret;
2733 unsigned int val, valr, oval, ovalr;
2734
2735 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2736 val &= mask;
2737 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2738 valr &= mask;
2739
2740 oval = snd_soc_read(codec, mc->reg) & mask;
2741 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2742
2743 ret = 0;
2744 if (oval != val) {
2745 ret = snd_soc_write(codec, mc->reg, val);
2746 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002747 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002748 }
2749 if (ovalr != valr) {
2750 ret = snd_soc_write(codec, mc->rreg, valr);
2751 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002752 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002753 }
2754
2755 return 0;
2756}
2757EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2758
Mark Brown71d08512011-10-10 18:31:26 +01002759int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2760 struct snd_ctl_elem_info *uinfo)
2761{
2762 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2763 struct soc_bytes *params = (void *)kcontrol->private_value;
2764
2765 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2766 uinfo->count = params->num_regs * codec->val_bytes;
2767
2768 return 0;
2769}
2770EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2771
2772int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2773 struct snd_ctl_elem_value *ucontrol)
2774{
2775 struct soc_bytes *params = (void *)kcontrol->private_value;
2776 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2777 int ret;
2778
2779 if (codec->using_regmap)
2780 ret = regmap_raw_read(codec->control_data, params->base,
2781 ucontrol->value.bytes.data,
2782 params->num_regs * codec->val_bytes);
2783 else
2784 ret = -EINVAL;
2785
Mark Brownf831b052012-02-17 16:20:33 -08002786 /* Hide any masked bytes to ensure consistent data reporting */
2787 if (ret == 0 && params->mask) {
2788 switch (codec->val_bytes) {
2789 case 1:
2790 ucontrol->value.bytes.data[0] &= ~params->mask;
2791 break;
2792 case 2:
2793 ((u16 *)(&ucontrol->value.bytes.data))[0]
2794 &= ~params->mask;
2795 break;
2796 case 4:
2797 ((u32 *)(&ucontrol->value.bytes.data))[0]
2798 &= ~params->mask;
2799 break;
2800 default:
2801 return -EINVAL;
2802 }
2803 }
2804
Mark Brown71d08512011-10-10 18:31:26 +01002805 return ret;
2806}
2807EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2808
2809int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2810 struct snd_ctl_elem_value *ucontrol)
2811{
2812 struct soc_bytes *params = (void *)kcontrol->private_value;
2813 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08002814 int ret, len;
2815 unsigned int val;
2816 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01002817
Mark Brownf831b052012-02-17 16:20:33 -08002818 if (!codec->using_regmap)
2819 return -EINVAL;
2820
2821 data = ucontrol->value.bytes.data;
2822 len = params->num_regs * codec->val_bytes;
2823
2824 /*
2825 * If we've got a mask then we need to preserve the register
2826 * bits. We shouldn't modify the incoming data so take a
2827 * copy.
2828 */
2829 if (params->mask) {
2830 ret = regmap_read(codec->control_data, params->base, &val);
2831 if (ret != 0)
2832 return ret;
2833
2834 val &= params->mask;
2835
2836 data = kmemdup(data, len, GFP_KERNEL);
2837 if (!data)
2838 return -ENOMEM;
2839
2840 switch (codec->val_bytes) {
2841 case 1:
2842 ((u8 *)data)[0] &= ~params->mask;
2843 ((u8 *)data)[0] |= val;
2844 break;
2845 case 2:
2846 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
2847 ((u16 *)data)[0] |= cpu_to_be16(val);
2848 break;
2849 case 4:
2850 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
2851 ((u32 *)data)[0] |= cpu_to_be32(val);
2852 break;
2853 default:
2854 return -EINVAL;
2855 }
2856 }
2857
2858 ret = regmap_raw_write(codec->control_data, params->base,
2859 data, len);
2860
2861 if (params->mask)
2862 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01002863
2864 return ret;
2865}
2866EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
2867
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002868/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002869 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2870 * @dai: DAI
2871 * @clk_id: DAI specific clock ID
2872 * @freq: new clock frequency in Hz
2873 * @dir: new clock direction - input/output.
2874 *
2875 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2876 */
2877int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2878 unsigned int freq, int dir)
2879{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002880 if (dai->driver && dai->driver->ops->set_sysclk)
2881 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002882 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002883 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002884 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002885 else
2886 return -EINVAL;
2887}
2888EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2889
2890/**
Mark Brownec4ee522011-03-07 20:58:11 +00002891 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2892 * @codec: CODEC
2893 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002894 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002895 * @freq: new clock frequency in Hz
2896 * @dir: new clock direction - input/output.
2897 *
2898 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2899 */
2900int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002901 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002902{
2903 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002904 return codec->driver->set_sysclk(codec, clk_id, source,
2905 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002906 else
2907 return -EINVAL;
2908}
2909EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2910
2911/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002912 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2913 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002914 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002915 * @div: new clock divisor.
2916 *
2917 * Configures the clock dividers. This is used to derive the best DAI bit and
2918 * frame clocks from the system or master clock. It's best to set the DAI bit
2919 * and frame clocks as low as possible to save system power.
2920 */
2921int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2922 int div_id, int div)
2923{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002924 if (dai->driver && dai->driver->ops->set_clkdiv)
2925 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002926 else
2927 return -EINVAL;
2928}
2929EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2930
2931/**
2932 * snd_soc_dai_set_pll - configure DAI PLL.
2933 * @dai: DAI
2934 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002935 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002936 * @freq_in: PLL input clock frequency in Hz
2937 * @freq_out: requested PLL output clock frequency in Hz
2938 *
2939 * Configures and enables PLL to generate output clock based on input clock.
2940 */
Mark Brown85488032009-09-05 18:52:16 +01002941int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2942 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002943{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002944 if (dai->driver && dai->driver->ops->set_pll)
2945 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002946 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002947 else if (dai->codec && dai->codec->driver->set_pll)
2948 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2949 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002950 else
2951 return -EINVAL;
2952}
2953EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2954
Mark Brownec4ee522011-03-07 20:58:11 +00002955/*
2956 * snd_soc_codec_set_pll - configure codec PLL.
2957 * @codec: CODEC
2958 * @pll_id: DAI specific PLL ID
2959 * @source: DAI specific source for the PLL
2960 * @freq_in: PLL input clock frequency in Hz
2961 * @freq_out: requested PLL output clock frequency in Hz
2962 *
2963 * Configures and enables PLL to generate output clock based on input clock.
2964 */
2965int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2966 unsigned int freq_in, unsigned int freq_out)
2967{
2968 if (codec->driver->set_pll)
2969 return codec->driver->set_pll(codec, pll_id, source,
2970 freq_in, freq_out);
2971 else
2972 return -EINVAL;
2973}
2974EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2975
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002976/**
2977 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2978 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002979 * @fmt: SND_SOC_DAIFMT_ format value.
2980 *
2981 * Configures the DAI hardware format and clocking.
2982 */
2983int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2984{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002985 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002986 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002987 if (dai->driver->ops->set_fmt == NULL)
2988 return -ENOTSUPP;
2989 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002990}
2991EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2992
2993/**
2994 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2995 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002996 * @tx_mask: bitmask representing active TX slots.
2997 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002998 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002999 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003000 *
3001 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3002 * specific.
3003 */
3004int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003005 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003006{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003007 if (dai->driver && dai->driver->ops->set_tdm_slot)
3008 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003009 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003010 else
3011 return -EINVAL;
3012}
3013EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3014
3015/**
Barry Song472df3c2009-09-12 01:16:29 +08003016 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3017 * @dai: DAI
3018 * @tx_num: how many TX channels
3019 * @tx_slot: pointer to an array which imply the TX slot number channel
3020 * 0~num-1 uses
3021 * @rx_num: how many RX channels
3022 * @rx_slot: pointer to an array which imply the RX slot number channel
3023 * 0~num-1 uses
3024 *
3025 * configure the relationship between channel number and TDM slot number.
3026 */
3027int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3028 unsigned int tx_num, unsigned int *tx_slot,
3029 unsigned int rx_num, unsigned int *rx_slot)
3030{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003031 if (dai->driver && dai->driver->ops->set_channel_map)
3032 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003033 rx_num, rx_slot);
3034 else
3035 return -EINVAL;
3036}
3037EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3038
3039/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003040 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3041 * @dai: DAI
3042 * @tristate: tristate enable
3043 *
3044 * Tristates the DAI so that others can use it.
3045 */
3046int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3047{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003048 if (dai->driver && dai->driver->ops->set_tristate)
3049 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003050 else
3051 return -EINVAL;
3052}
3053EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3054
3055/**
3056 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3057 * @dai: DAI
3058 * @mute: mute enable
3059 *
3060 * Mutes the DAI DAC.
3061 */
3062int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3063{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003064 if (dai->driver && dai->driver->ops->digital_mute)
3065 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003066 else
3067 return -EINVAL;
3068}
3069EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3070
Mark Brownc5af3a22008-11-28 13:29:45 +00003071/**
3072 * snd_soc_register_card - Register a card with the ASoC core
3073 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003074 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003075 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003076 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303077int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003078{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003079 int i;
3080
Mark Brownc5af3a22008-11-28 13:29:45 +00003081 if (!card->name || !card->dev)
3082 return -EINVAL;
3083
Stephen Warren5a504962011-12-21 10:40:59 -07003084 for (i = 0; i < card->num_links; i++) {
3085 struct snd_soc_dai_link *link = &card->dai_link[i];
3086
3087 /*
3088 * Codec must be specified by 1 of name or OF node,
3089 * not both or neither.
3090 */
3091 if (!!link->codec_name == !!link->codec_of_node) {
3092 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003093 "Neither/both codec name/of_node are set for %s\n",
3094 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003095 return -EINVAL;
3096 }
3097
3098 /*
3099 * Platform may be specified by either name or OF node, but
3100 * can be left unspecified, and a dummy platform will be used.
3101 */
3102 if (link->platform_name && link->platform_of_node) {
3103 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003104 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003105 return -EINVAL;
3106 }
3107
3108 /*
3109 * CPU DAI must be specified by 1 of name or OF node,
3110 * not both or neither.
3111 */
3112 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
3113 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003114 "Neither/both cpu_dai name/of_node are set for %s\n",
3115 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003116 return -EINVAL;
3117 }
3118 }
3119
Mark Browned77cc12011-05-03 18:25:34 +01003120 dev_set_drvdata(card->dev, card);
3121
Stephen Warren111c6412011-01-28 14:26:35 -07003122 snd_soc_initialize_card_lists(card);
3123
Vinod Koul150dd2f2011-01-13 22:48:32 +05303124 soc_init_card_debugfs(card);
3125
Mark Brown181a6892012-03-14 20:18:49 +00003126 card->rtd = devm_kzalloc(card->dev,
3127 sizeof(struct snd_soc_pcm_runtime) *
3128 (card->num_links + card->num_aux_devs),
3129 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003130 if (card->rtd == NULL)
3131 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003132 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003133
3134 for (i = 0; i < card->num_links; i++)
3135 card->rtd[i].dai_link = &card->dai_link[i];
3136
Mark Brownc5af3a22008-11-28 13:29:45 +00003137 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003138 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003139 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003140 mutex_init(&card->mutex);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003141 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003142
3143 mutex_lock(&client_mutex);
3144 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003145 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00003146 mutex_unlock(&client_mutex);
3147
3148 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3149
3150 return 0;
3151}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303152EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003153
3154/**
3155 * snd_soc_unregister_card - Unregister a card with the ASoC core
3156 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003157 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003158 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003159 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303160int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003161{
Vinod Koulb0e26482011-01-13 22:48:02 +05303162 if (card->instantiated)
3163 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003164 mutex_lock(&client_mutex);
3165 list_del(&card->list);
3166 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003167 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3168
3169 return 0;
3170}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303171EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003172
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003173/*
3174 * Simplify DAI link configuration by removing ".-1" from device names
3175 * and sanitizing names.
3176 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003177static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003178{
3179 char *found, name[NAME_SIZE];
3180 int id1, id2;
3181
3182 if (dev_name(dev) == NULL)
3183 return NULL;
3184
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003185 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003186
3187 /* are we a "%s.%d" name (platform and SPI components) */
3188 found = strstr(name, dev->driver->name);
3189 if (found) {
3190 /* get ID */
3191 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3192
3193 /* discard ID from name if ID == -1 */
3194 if (*id == -1)
3195 found[strlen(dev->driver->name)] = '\0';
3196 }
3197
3198 } else {
3199 /* I2C component devices are named "bus-addr" */
3200 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3201 char tmp[NAME_SIZE];
3202
3203 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003204 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003205
3206 /* sanitize component name for DAI link creation */
3207 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003208 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003209 } else
3210 *id = 0;
3211 }
3212
3213 return kstrdup(name, GFP_KERNEL);
3214}
3215
3216/*
3217 * Simplify DAI link naming for single devices with multiple DAIs by removing
3218 * any ".-1" and using the DAI name (instead of device name).
3219 */
3220static inline char *fmt_multiple_name(struct device *dev,
3221 struct snd_soc_dai_driver *dai_drv)
3222{
3223 if (dai_drv->name == NULL) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003224 pr_err("asoc: error - multiple DAI %s registered with no name\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003225 dev_name(dev));
3226 return NULL;
3227 }
3228
3229 return kstrdup(dai_drv->name, GFP_KERNEL);
3230}
3231
Mark Brown91151712008-11-30 23:31:24 +00003232/**
3233 * snd_soc_register_dai - Register a DAI with the ASoC core
3234 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003235 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003236 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003237int snd_soc_register_dai(struct device *dev,
3238 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003239{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003240 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003241
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003242 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003243
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003244 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3245 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003246 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003247
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003248 /* create DAI component name */
3249 dai->name = fmt_single_name(dev, &dai->id);
3250 if (dai->name == NULL) {
3251 kfree(dai);
3252 return -ENOMEM;
3253 }
3254
3255 dai->dev = dev;
3256 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003257 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003258 if (!dai->driver->ops)
3259 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003260
3261 mutex_lock(&client_mutex);
3262 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003263 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003264 mutex_unlock(&client_mutex);
3265
3266 pr_debug("Registered DAI '%s'\n", dai->name);
3267
3268 return 0;
3269}
3270EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3271
3272/**
3273 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3274 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003275 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003276 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003277void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003278{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003279 struct snd_soc_dai *dai;
3280
3281 list_for_each_entry(dai, &dai_list, list) {
3282 if (dev == dai->dev)
3283 goto found;
3284 }
3285 return;
3286
3287found:
Mark Brown91151712008-11-30 23:31:24 +00003288 mutex_lock(&client_mutex);
3289 list_del(&dai->list);
3290 mutex_unlock(&client_mutex);
3291
3292 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003293 kfree(dai->name);
3294 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003295}
3296EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3297
3298/**
3299 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3300 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003301 * @dai: Array of DAIs to register
3302 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003303 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003304int snd_soc_register_dais(struct device *dev,
3305 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003306{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003307 struct snd_soc_dai *dai;
3308 int i, ret = 0;
3309
Liam Girdwood720ffa42010-08-18 00:30:30 +01003310 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003311
3312 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003313
3314 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003315 if (dai == NULL) {
3316 ret = -ENOMEM;
3317 goto err;
3318 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003319
3320 /* create DAI component name */
3321 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3322 if (dai->name == NULL) {
3323 kfree(dai);
3324 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003325 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003326 }
3327
3328 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003329 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003330 if (dai->driver->id)
3331 dai->id = dai->driver->id;
3332 else
3333 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003334 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003335 if (!dai->driver->ops)
3336 dai->driver->ops = &null_dai_ops;
3337
3338 mutex_lock(&client_mutex);
3339 list_add(&dai->list, &dai_list);
3340 mutex_unlock(&client_mutex);
3341
3342 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003343 }
3344
Axel Lin1dcb4f32010-12-06 16:48:03 +08003345 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003346 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08003347 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00003348 return 0;
3349
3350err:
3351 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003352 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003353
3354 return ret;
3355}
3356EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3357
3358/**
3359 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3360 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003361 * @dai: Array of DAIs to unregister
3362 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003363 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003364void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003365{
3366 int i;
3367
3368 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003369 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003370}
3371EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3372
Mark Brown12a48a8c2008-12-03 19:40:30 +00003373/**
3374 * snd_soc_register_platform - Register a platform with the ASoC core
3375 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003376 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003377 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003378int snd_soc_register_platform(struct device *dev,
3379 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003380{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003381 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003382
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003383 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3384
3385 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3386 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003387 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003388
3389 /* create platform component name */
3390 platform->name = fmt_single_name(dev, &platform->id);
3391 if (platform->name == NULL) {
3392 kfree(platform);
3393 return -ENOMEM;
3394 }
3395
3396 platform->dev = dev;
3397 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003398 platform->dapm.dev = dev;
3399 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003400 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003401 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003402
3403 mutex_lock(&client_mutex);
3404 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003405 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003406 mutex_unlock(&client_mutex);
3407
3408 pr_debug("Registered platform '%s'\n", platform->name);
3409
3410 return 0;
3411}
3412EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3413
3414/**
3415 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3416 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003417 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003418 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003419void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003420{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003421 struct snd_soc_platform *platform;
3422
3423 list_for_each_entry(platform, &platform_list, list) {
3424 if (dev == platform->dev)
3425 goto found;
3426 }
3427 return;
3428
3429found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003430 mutex_lock(&client_mutex);
3431 list_del(&platform->list);
3432 mutex_unlock(&client_mutex);
3433
3434 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003435 kfree(platform->name);
3436 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003437}
3438EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3439
Mark Brown151ab222009-05-09 16:22:58 +01003440static u64 codec_format_map[] = {
3441 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3442 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3443 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3444 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3445 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3446 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3447 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3448 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3449 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3450 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3451 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3452 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3453 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3454 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3455 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3456 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3457};
3458
3459/* Fix up the DAI formats for endianness: codecs don't actually see
3460 * the endianness of the data but we're using the CPU format
3461 * definitions which do need to include endianness so we ensure that
3462 * codec DAIs always have both big and little endian variants set.
3463 */
3464static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3465{
3466 int i;
3467
3468 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3469 if (stream->formats & codec_format_map[i])
3470 stream->formats |= codec_format_map[i];
3471}
3472
Mark Brown0d0cf002008-12-10 14:32:45 +00003473/**
3474 * snd_soc_register_codec - Register a codec with the ASoC core
3475 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003476 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003477 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003478int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003479 const struct snd_soc_codec_driver *codec_drv,
3480 struct snd_soc_dai_driver *dai_drv,
3481 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003482{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003483 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003484 struct snd_soc_codec *codec;
3485 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003486
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003487 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003488
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003489 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3490 if (codec == NULL)
3491 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003492
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003493 /* create CODEC component name */
3494 codec->name = fmt_single_name(dev, &codec->id);
3495 if (codec->name == NULL) {
3496 kfree(codec);
3497 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003498 }
3499
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003500 if (codec_drv->compress_type)
3501 codec->compress_type = codec_drv->compress_type;
3502 else
3503 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3504
Mark Brownc3acec22010-12-02 16:15:29 +00003505 codec->write = codec_drv->write;
3506 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003507 codec->volatile_register = codec_drv->volatile_register;
3508 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003509 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00003510 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003511 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3512 codec->dapm.dev = dev;
3513 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003514 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003515 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003516 codec->dev = dev;
3517 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003518 codec->num_dai = num_dai;
3519 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003520
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003521 /* allocate CODEC register cache */
3522 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003523 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003524 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003525 /* it is necessary to make a copy of the default register cache
3526 * because in the case of using a compression type that requires
3527 * the default register cache to be marked as __devinitconst the
3528 * kernel might have freed the array by the time we initialize
3529 * the cache.
3530 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003531 if (codec_drv->reg_cache_default) {
3532 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3533 reg_size, GFP_KERNEL);
3534 if (!codec->reg_def_copy) {
3535 ret = -ENOMEM;
3536 goto fail;
3537 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003538 }
3539 }
3540
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003541 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3542 if (!codec->volatile_register)
3543 codec->volatile_register = snd_soc_default_volatile_register;
3544 if (!codec->readable_register)
3545 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003546 if (!codec->writable_register)
3547 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003548 }
3549
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003550 for (i = 0; i < num_dai; i++) {
3551 fixup_codec_formats(&dai_drv[i].playback);
3552 fixup_codec_formats(&dai_drv[i].capture);
3553 }
3554
Mark Brown26b01cc2010-08-18 20:20:55 +01003555 /* register any DAIs */
3556 if (num_dai) {
3557 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3558 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003559 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003560 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003561
Mark Brown0d0cf002008-12-10 14:32:45 +00003562 mutex_lock(&client_mutex);
3563 list_add(&codec->list, &codec_list);
3564 snd_soc_instantiate_cards();
3565 mutex_unlock(&client_mutex);
3566
3567 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003568 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003569
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003570fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003571 kfree(codec->reg_def_copy);
3572 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003573 kfree(codec->name);
3574 kfree(codec);
3575 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003576}
3577EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3578
3579/**
3580 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3581 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003582 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003583 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003584void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003585{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003586 struct snd_soc_codec *codec;
3587 int i;
3588
3589 list_for_each_entry(codec, &codec_list, list) {
3590 if (dev == codec->dev)
3591 goto found;
3592 }
3593 return;
3594
3595found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003596 if (codec->num_dai)
3597 for (i = 0; i < codec->num_dai; i++)
3598 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003599
Mark Brown0d0cf002008-12-10 14:32:45 +00003600 mutex_lock(&client_mutex);
3601 list_del(&codec->list);
3602 mutex_unlock(&client_mutex);
3603
3604 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003605
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003606 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003607 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003608 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003609 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003610}
3611EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3612
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003613/* Retrieve a card's name from device tree */
3614int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3615 const char *propname)
3616{
3617 struct device_node *np = card->dev->of_node;
3618 int ret;
3619
3620 ret = of_property_read_string_index(np, propname, 0, &card->name);
3621 /*
3622 * EINVAL means the property does not exist. This is fine providing
3623 * card->name was previously set, which is checked later in
3624 * snd_soc_register_card.
3625 */
3626 if (ret < 0 && ret != -EINVAL) {
3627 dev_err(card->dev,
3628 "Property '%s' could not be read: %d\n",
3629 propname, ret);
3630 return ret;
3631 }
3632
3633 return 0;
3634}
3635EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3636
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003637int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3638 const char *propname)
3639{
3640 struct device_node *np = card->dev->of_node;
3641 int num_routes;
3642 struct snd_soc_dapm_route *routes;
3643 int i, ret;
3644
3645 num_routes = of_property_count_strings(np, propname);
3646 if (num_routes & 1) {
3647 dev_err(card->dev,
3648 "Property '%s's length is not even\n",
3649 propname);
3650 return -EINVAL;
3651 }
3652 num_routes /= 2;
3653 if (!num_routes) {
3654 dev_err(card->dev,
3655 "Property '%s's length is zero\n",
3656 propname);
3657 return -EINVAL;
3658 }
3659
3660 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3661 GFP_KERNEL);
3662 if (!routes) {
3663 dev_err(card->dev,
3664 "Could not allocate DAPM route table\n");
3665 return -EINVAL;
3666 }
3667
3668 for (i = 0; i < num_routes; i++) {
3669 ret = of_property_read_string_index(np, propname,
3670 2 * i, &routes[i].sink);
3671 if (ret) {
3672 dev_err(card->dev,
3673 "Property '%s' index %d could not be read: %d\n",
3674 propname, 2 * i, ret);
3675 return -EINVAL;
3676 }
3677 ret = of_property_read_string_index(np, propname,
3678 (2 * i) + 1, &routes[i].source);
3679 if (ret) {
3680 dev_err(card->dev,
3681 "Property '%s' index %d could not be read: %d\n",
3682 propname, (2 * i) + 1, ret);
3683 return -EINVAL;
3684 }
3685 }
3686
3687 card->num_dapm_routes = num_routes;
3688 card->dapm_routes = routes;
3689
3690 return 0;
3691}
3692EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3693
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003694static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003695{
Mark Brown384c89e2008-12-03 17:34:03 +00003696#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003697 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3698 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003699 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003700 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003701 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003702
Mark Brown8a9dab12011-01-10 22:25:21 +00003703 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003704 &codec_list_fops))
3705 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3706
Mark Brown8a9dab12011-01-10 22:25:21 +00003707 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003708 &dai_list_fops))
3709 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003710
Mark Brown8a9dab12011-01-10 22:25:21 +00003711 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003712 &platform_list_fops))
3713 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003714#endif
3715
Mark Brownfb257892011-04-28 10:57:54 +01003716 snd_soc_util_init();
3717
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003718 return platform_driver_register(&soc_driver);
3719}
Mark Brown4abe8e12010-10-12 17:41:03 +01003720module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003721
Mark Brown7d8c16a2008-11-30 22:11:24 +00003722static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003723{
Mark Brownfb257892011-04-28 10:57:54 +01003724 snd_soc_util_exit();
3725
Mark Brown384c89e2008-12-03 17:34:03 +00003726#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003727 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003728#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003729 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003730}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003731module_exit(snd_soc_exit);
3732
3733/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003734MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003735MODULE_DESCRIPTION("ALSA SoC Core");
3736MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003737MODULE_ALIAS("platform:soc-audio");