]>
Commit | Line | Data |
---|---|---|
db2a4165 FM |
1 | /* |
2 | * soc-core.c -- ALSA SoC Audio Layer | |
3 | * | |
4 | * Copyright 2005 Wolfson Microelectronics PLC. | |
0664d888 LG |
5 | * Copyright 2005 Openedhand Ltd. |
6 | * | |
d331124d | 7 | * Author: Liam Girdwood <lrg@slimlogic.co.uk> |
0664d888 LG |
8 | * with code, comments and ideas from :- |
9 | * Richard Purdie <richard@openedhand.com> | |
db2a4165 FM |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify it | |
12 | * under the terms of the GNU General Public License as published by the | |
13 | * Free Software Foundation; either version 2 of the License, or (at your | |
14 | * option) any later version. | |
15 | * | |
db2a4165 FM |
16 | * TODO: |
17 | * o Add hw rules to enforce rates, etc. | |
18 | * o More testing with other codecs/machines. | |
19 | * o Add more codecs and platforms to ensure good API coverage. | |
20 | * o Support TDM on PCM and I2S | |
21 | */ | |
22 | ||
23 | #include <linux/module.h> | |
24 | #include <linux/moduleparam.h> | |
25 | #include <linux/init.h> | |
26 | #include <linux/delay.h> | |
27 | #include <linux/pm.h> | |
28 | #include <linux/bitops.h> | |
12ef193d | 29 | #include <linux/debugfs.h> |
db2a4165 | 30 | #include <linux/platform_device.h> |
474828a4 | 31 | #include <sound/ac97_codec.h> |
db2a4165 FM |
32 | #include <sound/core.h> |
33 | #include <sound/pcm.h> | |
34 | #include <sound/pcm_params.h> | |
35 | #include <sound/soc.h> | |
36 | #include <sound/soc-dapm.h> | |
37 | #include <sound/initval.h> | |
38 | ||
db2a4165 | 39 | static DEFINE_MUTEX(pcm_mutex); |
db2a4165 FM |
40 | static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq); |
41 | ||
384c89e2 MB |
42 | #ifdef CONFIG_DEBUG_FS |
43 | static struct dentry *debugfs_root; | |
44 | #endif | |
45 | ||
c5af3a2e MB |
46 | static DEFINE_MUTEX(client_mutex); |
47 | static LIST_HEAD(card_list); | |
9115171a | 48 | static LIST_HEAD(dai_list); |
12a48a8c | 49 | static LIST_HEAD(platform_list); |
0d0cf00a | 50 | static LIST_HEAD(codec_list); |
c5af3a2e MB |
51 | |
52 | static int snd_soc_register_card(struct snd_soc_card *card); | |
53 | static int snd_soc_unregister_card(struct snd_soc_card *card); | |
54 | ||
db2a4165 FM |
55 | /* |
56 | * This is a timeout to do a DAPM powerdown after a stream is closed(). | |
57 | * It can be used to eliminate pops between different playback streams, e.g. | |
58 | * between two audio tracks. | |
59 | */ | |
60 | static int pmdown_time = 5000; | |
61 | module_param(pmdown_time, int, 0); | |
62 | MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); | |
63 | ||
965ac42c LG |
64 | /* |
65 | * This function forces any delayed work to be queued and run. | |
66 | */ | |
67 | static int run_delayed_work(struct delayed_work *dwork) | |
68 | { | |
69 | int ret; | |
70 | ||
71 | /* cancel any work waiting to be queued. */ | |
72 | ret = cancel_delayed_work(dwork); | |
73 | ||
74 | /* if there was any work waiting then we run it now and | |
75 | * wait for it's completion */ | |
76 | if (ret) { | |
77 | schedule_delayed_work(dwork, 0); | |
78 | flush_scheduled_work(); | |
79 | } | |
80 | return ret; | |
81 | } | |
82 | ||
2624d5fa MB |
83 | /* codec register dump */ |
84 | static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf) | |
85 | { | |
86 | int i, step = 1, count = 0; | |
87 | ||
88 | if (!codec->reg_cache_size) | |
89 | return 0; | |
90 | ||
91 | if (codec->reg_cache_step) | |
92 | step = codec->reg_cache_step; | |
93 | ||
94 | count += sprintf(buf, "%s registers\n", codec->name); | |
95 | for (i = 0; i < codec->reg_cache_size; i += step) { | |
96 | if (codec->readable_register && !codec->readable_register(i)) | |
97 | continue; | |
98 | ||
99 | count += sprintf(buf + count, "%2x: ", i); | |
100 | if (count >= PAGE_SIZE - 1) | |
101 | break; | |
102 | ||
103 | if (codec->display_register) | |
104 | count += codec->display_register(codec, buf + count, | |
105 | PAGE_SIZE - count, i); | |
106 | else | |
107 | count += snprintf(buf + count, PAGE_SIZE - count, | |
108 | "%4x", codec->read(codec, i)); | |
109 | ||
110 | if (count >= PAGE_SIZE - 1) | |
111 | break; | |
112 | ||
113 | count += snprintf(buf + count, PAGE_SIZE - count, "\n"); | |
114 | if (count >= PAGE_SIZE - 1) | |
115 | break; | |
116 | } | |
117 | ||
118 | /* Truncate count; min() would cause a warning */ | |
119 | if (count >= PAGE_SIZE) | |
120 | count = PAGE_SIZE - 1; | |
121 | ||
122 | return count; | |
123 | } | |
124 | static ssize_t codec_reg_show(struct device *dev, | |
125 | struct device_attribute *attr, char *buf) | |
126 | { | |
127 | struct snd_soc_device *devdata = dev_get_drvdata(dev); | |
128 | return soc_codec_reg_show(devdata->card->codec, buf); | |
129 | } | |
130 | ||
131 | static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); | |
132 | ||
dbe21408 MB |
133 | static ssize_t pmdown_time_show(struct device *dev, |
134 | struct device_attribute *attr, char *buf) | |
135 | { | |
136 | struct snd_soc_device *socdev = dev_get_drvdata(dev); | |
137 | struct snd_soc_card *card = socdev->card; | |
138 | ||
6c5f1fed | 139 | return sprintf(buf, "%ld\n", card->pmdown_time); |
dbe21408 MB |
140 | } |
141 | ||
142 | static ssize_t pmdown_time_set(struct device *dev, | |
143 | struct device_attribute *attr, | |
144 | const char *buf, size_t count) | |
145 | { | |
146 | struct snd_soc_device *socdev = dev_get_drvdata(dev); | |
147 | struct snd_soc_card *card = socdev->card; | |
148 | ||
149 | strict_strtol(buf, 10, &card->pmdown_time); | |
150 | ||
151 | return count; | |
152 | } | |
153 | ||
154 | static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); | |
155 | ||
2624d5fa MB |
156 | #ifdef CONFIG_DEBUG_FS |
157 | static int codec_reg_open_file(struct inode *inode, struct file *file) | |
158 | { | |
159 | file->private_data = inode->i_private; | |
160 | return 0; | |
161 | } | |
162 | ||
163 | static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, | |
164 | size_t count, loff_t *ppos) | |
165 | { | |
166 | ssize_t ret; | |
167 | struct snd_soc_codec *codec = file->private_data; | |
168 | char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
169 | if (!buf) | |
170 | return -ENOMEM; | |
171 | ret = soc_codec_reg_show(codec, buf); | |
172 | if (ret >= 0) | |
173 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); | |
174 | kfree(buf); | |
175 | return ret; | |
176 | } | |
177 | ||
178 | static ssize_t codec_reg_write_file(struct file *file, | |
179 | const char __user *user_buf, size_t count, loff_t *ppos) | |
180 | { | |
181 | char buf[32]; | |
182 | int buf_size; | |
183 | char *start = buf; | |
184 | unsigned long reg, value; | |
185 | int step = 1; | |
186 | struct snd_soc_codec *codec = file->private_data; | |
187 | ||
188 | buf_size = min(count, (sizeof(buf)-1)); | |
189 | if (copy_from_user(buf, user_buf, buf_size)) | |
190 | return -EFAULT; | |
191 | buf[buf_size] = 0; | |
192 | ||
193 | if (codec->reg_cache_step) | |
194 | step = codec->reg_cache_step; | |
195 | ||
196 | while (*start == ' ') | |
197 | start++; | |
198 | reg = simple_strtoul(start, &start, 16); | |
199 | if ((reg >= codec->reg_cache_size) || (reg % step)) | |
200 | return -EINVAL; | |
201 | while (*start == ' ') | |
202 | start++; | |
203 | if (strict_strtoul(start, 16, &value)) | |
204 | return -EINVAL; | |
205 | codec->write(codec, reg, value); | |
206 | return buf_size; | |
207 | } | |
208 | ||
209 | static const struct file_operations codec_reg_fops = { | |
210 | .open = codec_reg_open_file, | |
211 | .read = codec_reg_read_file, | |
212 | .write = codec_reg_write_file, | |
213 | }; | |
214 | ||
215 | static void soc_init_codec_debugfs(struct snd_soc_codec *codec) | |
216 | { | |
217 | char codec_root[128]; | |
218 | ||
219 | if (codec->dev) | |
220 | snprintf(codec_root, sizeof(codec_root), | |
221 | "%s.%s", codec->name, dev_name(codec->dev)); | |
222 | else | |
223 | snprintf(codec_root, sizeof(codec_root), | |
224 | "%s", codec->name); | |
225 | ||
226 | codec->debugfs_codec_root = debugfs_create_dir(codec_root, | |
227 | debugfs_root); | |
228 | if (!codec->debugfs_codec_root) { | |
229 | printk(KERN_WARNING | |
230 | "ASoC: Failed to create codec debugfs directory\n"); | |
231 | return; | |
232 | } | |
233 | ||
234 | codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, | |
235 | codec->debugfs_codec_root, | |
236 | codec, &codec_reg_fops); | |
237 | if (!codec->debugfs_reg) | |
238 | printk(KERN_WARNING | |
239 | "ASoC: Failed to create codec register debugfs file\n"); | |
240 | ||
241 | codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744, | |
242 | codec->debugfs_codec_root, | |
243 | &codec->pop_time); | |
244 | if (!codec->debugfs_pop_time) | |
245 | printk(KERN_WARNING | |
246 | "Failed to create pop time debugfs file\n"); | |
247 | ||
248 | codec->debugfs_dapm = debugfs_create_dir("dapm", | |
249 | codec->debugfs_codec_root); | |
250 | if (!codec->debugfs_dapm) | |
251 | printk(KERN_WARNING | |
252 | "Failed to create DAPM debugfs directory\n"); | |
253 | ||
254 | snd_soc_dapm_debugfs_init(codec); | |
255 | } | |
256 | ||
257 | static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) | |
258 | { | |
259 | debugfs_remove_recursive(codec->debugfs_codec_root); | |
260 | } | |
261 | ||
262 | #else | |
263 | ||
264 | static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec) | |
265 | { | |
266 | } | |
267 | ||
268 | static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) | |
269 | { | |
270 | } | |
271 | #endif | |
272 | ||
db2a4165 FM |
273 | #ifdef CONFIG_SND_SOC_AC97_BUS |
274 | /* unregister ac97 codec */ | |
275 | static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) | |
276 | { | |
277 | if (codec->ac97->dev.bus) | |
278 | device_unregister(&codec->ac97->dev); | |
279 | return 0; | |
280 | } | |
281 | ||
282 | /* stop no dev release warning */ | |
283 | static void soc_ac97_device_release(struct device *dev){} | |
284 | ||
285 | /* register ac97 codec to bus */ | |
286 | static int soc_ac97_dev_register(struct snd_soc_codec *codec) | |
287 | { | |
288 | int err; | |
289 | ||
290 | codec->ac97->dev.bus = &ac97_bus_type; | |
4ac5c61f | 291 | codec->ac97->dev.parent = codec->card->dev; |
db2a4165 FM |
292 | codec->ac97->dev.release = soc_ac97_device_release; |
293 | ||
bb072bf0 KS |
294 | dev_set_name(&codec->ac97->dev, "%d-%d:%s", |
295 | codec->card->number, 0, codec->name); | |
db2a4165 FM |
296 | err = device_register(&codec->ac97->dev); |
297 | if (err < 0) { | |
298 | snd_printk(KERN_ERR "Can't register ac97 bus\n"); | |
299 | codec->ac97->dev.bus = NULL; | |
300 | return err; | |
301 | } | |
302 | return 0; | |
303 | } | |
304 | #endif | |
305 | ||
06f409d7 MB |
306 | static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream) |
307 | { | |
308 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
309 | struct snd_soc_device *socdev = rtd->socdev; | |
310 | struct snd_soc_card *card = socdev->card; | |
311 | struct snd_soc_dai_link *machine = rtd->dai; | |
312 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; | |
313 | struct snd_soc_dai *codec_dai = machine->codec_dai; | |
314 | int ret; | |
315 | ||
316 | if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates || | |
317 | machine->symmetric_rates) { | |
318 | dev_dbg(card->dev, "Symmetry forces %dHz rate\n", | |
319 | machine->rate); | |
320 | ||
321 | ret = snd_pcm_hw_constraint_minmax(substream->runtime, | |
322 | SNDRV_PCM_HW_PARAM_RATE, | |
323 | machine->rate, | |
324 | machine->rate); | |
325 | if (ret < 0) { | |
326 | dev_err(card->dev, | |
327 | "Unable to apply rate symmetry constraint: %d\n", ret); | |
328 | return ret; | |
329 | } | |
330 | } | |
331 | ||
332 | return 0; | |
333 | } | |
334 | ||
db2a4165 FM |
335 | /* |
336 | * Called by ALSA when a PCM substream is opened, the runtime->hw record is | |
337 | * then initialized and any private data can be allocated. This also calls | |
338 | * startup for the cpu DAI, platform, machine and codec DAI. | |
339 | */ | |
340 | static int soc_pcm_open(struct snd_pcm_substream *substream) | |
341 | { | |
342 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
343 | struct snd_soc_device *socdev = rtd->socdev; | |
87689d56 | 344 | struct snd_soc_card *card = socdev->card; |
db2a4165 | 345 | struct snd_pcm_runtime *runtime = substream->runtime; |
cb666e5b | 346 | struct snd_soc_dai_link *machine = rtd->dai; |
87689d56 | 347 | struct snd_soc_platform *platform = card->platform; |
3c4b266f LG |
348 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
349 | struct snd_soc_dai *codec_dai = machine->codec_dai; | |
db2a4165 FM |
350 | int ret = 0; |
351 | ||
352 | mutex_lock(&pcm_mutex); | |
353 | ||
354 | /* startup the audio subsystem */ | |
6335d055 EM |
355 | if (cpu_dai->ops->startup) { |
356 | ret = cpu_dai->ops->startup(substream, cpu_dai); | |
db2a4165 FM |
357 | if (ret < 0) { |
358 | printk(KERN_ERR "asoc: can't open interface %s\n", | |
cb666e5b | 359 | cpu_dai->name); |
db2a4165 FM |
360 | goto out; |
361 | } | |
362 | } | |
363 | ||
364 | if (platform->pcm_ops->open) { | |
365 | ret = platform->pcm_ops->open(substream); | |
366 | if (ret < 0) { | |
367 | printk(KERN_ERR "asoc: can't open platform %s\n", platform->name); | |
368 | goto platform_err; | |
369 | } | |
370 | } | |
371 | ||
6335d055 EM |
372 | if (codec_dai->ops->startup) { |
373 | ret = codec_dai->ops->startup(substream, codec_dai); | |
db2a4165 | 374 | if (ret < 0) { |
cb666e5b LG |
375 | printk(KERN_ERR "asoc: can't open codec %s\n", |
376 | codec_dai->name); | |
377 | goto codec_dai_err; | |
db2a4165 FM |
378 | } |
379 | } | |
380 | ||
cb666e5b LG |
381 | if (machine->ops && machine->ops->startup) { |
382 | ret = machine->ops->startup(substream); | |
db2a4165 | 383 | if (ret < 0) { |
cb666e5b LG |
384 | printk(KERN_ERR "asoc: %s startup failed\n", machine->name); |
385 | goto machine_err; | |
db2a4165 FM |
386 | } |
387 | } | |
388 | ||
db2a4165 FM |
389 | /* Check that the codec and cpu DAI's are compatible */ |
390 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
391 | runtime->hw.rate_min = | |
3ff3f64b MB |
392 | max(codec_dai->playback.rate_min, |
393 | cpu_dai->playback.rate_min); | |
db2a4165 | 394 | runtime->hw.rate_max = |
3ff3f64b MB |
395 | min(codec_dai->playback.rate_max, |
396 | cpu_dai->playback.rate_max); | |
db2a4165 | 397 | runtime->hw.channels_min = |
cb666e5b LG |
398 | max(codec_dai->playback.channels_min, |
399 | cpu_dai->playback.channels_min); | |
db2a4165 | 400 | runtime->hw.channels_max = |
cb666e5b LG |
401 | min(codec_dai->playback.channels_max, |
402 | cpu_dai->playback.channels_max); | |
403 | runtime->hw.formats = | |
404 | codec_dai->playback.formats & cpu_dai->playback.formats; | |
405 | runtime->hw.rates = | |
406 | codec_dai->playback.rates & cpu_dai->playback.rates; | |
db2a4165 FM |
407 | } else { |
408 | runtime->hw.rate_min = | |
3ff3f64b MB |
409 | max(codec_dai->capture.rate_min, |
410 | cpu_dai->capture.rate_min); | |
db2a4165 | 411 | runtime->hw.rate_max = |
3ff3f64b MB |
412 | min(codec_dai->capture.rate_max, |
413 | cpu_dai->capture.rate_max); | |
db2a4165 | 414 | runtime->hw.channels_min = |
cb666e5b LG |
415 | max(codec_dai->capture.channels_min, |
416 | cpu_dai->capture.channels_min); | |
db2a4165 | 417 | runtime->hw.channels_max = |
cb666e5b LG |
418 | min(codec_dai->capture.channels_max, |
419 | cpu_dai->capture.channels_max); | |
420 | runtime->hw.formats = | |
421 | codec_dai->capture.formats & cpu_dai->capture.formats; | |
422 | runtime->hw.rates = | |
423 | codec_dai->capture.rates & cpu_dai->capture.rates; | |
db2a4165 FM |
424 | } |
425 | ||
426 | snd_pcm_limit_hw_rates(runtime); | |
427 | if (!runtime->hw.rates) { | |
428 | printk(KERN_ERR "asoc: %s <-> %s No matching rates\n", | |
cb666e5b LG |
429 | codec_dai->name, cpu_dai->name); |
430 | goto machine_err; | |
db2a4165 FM |
431 | } |
432 | if (!runtime->hw.formats) { | |
433 | printk(KERN_ERR "asoc: %s <-> %s No matching formats\n", | |
cb666e5b LG |
434 | codec_dai->name, cpu_dai->name); |
435 | goto machine_err; | |
db2a4165 FM |
436 | } |
437 | if (!runtime->hw.channels_min || !runtime->hw.channels_max) { | |
438 | printk(KERN_ERR "asoc: %s <-> %s No matching channels\n", | |
cb666e5b LG |
439 | codec_dai->name, cpu_dai->name); |
440 | goto machine_err; | |
db2a4165 FM |
441 | } |
442 | ||
06f409d7 MB |
443 | /* Symmetry only applies if we've already got an active stream. */ |
444 | if (cpu_dai->active || codec_dai->active) { | |
445 | ret = soc_pcm_apply_symmetry(substream); | |
446 | if (ret != 0) | |
447 | goto machine_err; | |
448 | } | |
449 | ||
f24368c2 MB |
450 | pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name); |
451 | pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates); | |
452 | pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min, | |
453 | runtime->hw.channels_max); | |
454 | pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min, | |
455 | runtime->hw.rate_max); | |
db2a4165 | 456 | |
db2a4165 | 457 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
cb666e5b | 458 | cpu_dai->playback.active = codec_dai->playback.active = 1; |
db2a4165 | 459 | else |
cb666e5b LG |
460 | cpu_dai->capture.active = codec_dai->capture.active = 1; |
461 | cpu_dai->active = codec_dai->active = 1; | |
462 | cpu_dai->runtime = runtime; | |
6627a653 | 463 | card->codec->active++; |
db2a4165 FM |
464 | mutex_unlock(&pcm_mutex); |
465 | return 0; | |
466 | ||
cb666e5b | 467 | machine_err: |
db2a4165 FM |
468 | if (machine->ops && machine->ops->shutdown) |
469 | machine->ops->shutdown(substream); | |
470 | ||
cb666e5b | 471 | codec_dai_err: |
db2a4165 FM |
472 | if (platform->pcm_ops->close) |
473 | platform->pcm_ops->close(substream); | |
474 | ||
475 | platform_err: | |
6335d055 EM |
476 | if (cpu_dai->ops->shutdown) |
477 | cpu_dai->ops->shutdown(substream, cpu_dai); | |
db2a4165 FM |
478 | out: |
479 | mutex_unlock(&pcm_mutex); | |
480 | return ret; | |
481 | } | |
482 | ||
483 | /* | |
3a4fa0a2 | 484 | * Power down the audio subsystem pmdown_time msecs after close is called. |
db2a4165 FM |
485 | * This is to ensure there are no pops or clicks in between any music tracks |
486 | * due to DAPM power cycling. | |
487 | */ | |
4484bb2e | 488 | static void close_delayed_work(struct work_struct *work) |
db2a4165 | 489 | { |
6308419a MB |
490 | struct snd_soc_card *card = container_of(work, struct snd_soc_card, |
491 | delayed_work.work); | |
6627a653 | 492 | struct snd_soc_codec *codec = card->codec; |
3c4b266f | 493 | struct snd_soc_dai *codec_dai; |
db2a4165 FM |
494 | int i; |
495 | ||
496 | mutex_lock(&pcm_mutex); | |
3ff3f64b | 497 | for (i = 0; i < codec->num_dai; i++) { |
db2a4165 FM |
498 | codec_dai = &codec->dai[i]; |
499 | ||
f24368c2 MB |
500 | pr_debug("pop wq checking: %s status: %s waiting: %s\n", |
501 | codec_dai->playback.stream_name, | |
502 | codec_dai->playback.active ? "active" : "inactive", | |
503 | codec_dai->pop_wait ? "yes" : "no"); | |
db2a4165 FM |
504 | |
505 | /* are we waiting on this codec DAI stream */ | |
506 | if (codec_dai->pop_wait == 1) { | |
db2a4165 | 507 | codec_dai->pop_wait = 0; |
0b4d221b LG |
508 | snd_soc_dapm_stream_event(codec, |
509 | codec_dai->playback.stream_name, | |
db2a4165 | 510 | SND_SOC_DAPM_STREAM_STOP); |
db2a4165 FM |
511 | } |
512 | } | |
513 | mutex_unlock(&pcm_mutex); | |
514 | } | |
515 | ||
516 | /* | |
517 | * Called by ALSA when a PCM substream is closed. Private data can be | |
518 | * freed here. The cpu DAI, codec DAI, machine and platform are also | |
519 | * shutdown. | |
520 | */ | |
521 | static int soc_codec_close(struct snd_pcm_substream *substream) | |
522 | { | |
523 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
524 | struct snd_soc_device *socdev = rtd->socdev; | |
6308419a | 525 | struct snd_soc_card *card = socdev->card; |
cb666e5b | 526 | struct snd_soc_dai_link *machine = rtd->dai; |
87689d56 | 527 | struct snd_soc_platform *platform = card->platform; |
3c4b266f LG |
528 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
529 | struct snd_soc_dai *codec_dai = machine->codec_dai; | |
6627a653 | 530 | struct snd_soc_codec *codec = card->codec; |
db2a4165 FM |
531 | |
532 | mutex_lock(&pcm_mutex); | |
533 | ||
534 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
cb666e5b | 535 | cpu_dai->playback.active = codec_dai->playback.active = 0; |
db2a4165 | 536 | else |
cb666e5b | 537 | cpu_dai->capture.active = codec_dai->capture.active = 0; |
db2a4165 | 538 | |
cb666e5b LG |
539 | if (codec_dai->playback.active == 0 && |
540 | codec_dai->capture.active == 0) { | |
541 | cpu_dai->active = codec_dai->active = 0; | |
db2a4165 FM |
542 | } |
543 | codec->active--; | |
544 | ||
6010b2da MB |
545 | /* Muting the DAC suppresses artifacts caused during digital |
546 | * shutdown, for example from stopping clocks. | |
547 | */ | |
548 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
549 | snd_soc_dai_digital_mute(codec_dai, 1); | |
550 | ||
6335d055 EM |
551 | if (cpu_dai->ops->shutdown) |
552 | cpu_dai->ops->shutdown(substream, cpu_dai); | |
db2a4165 | 553 | |
6335d055 EM |
554 | if (codec_dai->ops->shutdown) |
555 | codec_dai->ops->shutdown(substream, codec_dai); | |
db2a4165 FM |
556 | |
557 | if (machine->ops && machine->ops->shutdown) | |
558 | machine->ops->shutdown(substream); | |
559 | ||
560 | if (platform->pcm_ops->close) | |
561 | platform->pcm_ops->close(substream); | |
cb666e5b | 562 | cpu_dai->runtime = NULL; |
db2a4165 FM |
563 | |
564 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
565 | /* start delayed pop wq here for playback streams */ | |
cb666e5b | 566 | codec_dai->pop_wait = 1; |
6308419a | 567 | schedule_delayed_work(&card->delayed_work, |
96dd3622 | 568 | msecs_to_jiffies(card->pmdown_time)); |
db2a4165 FM |
569 | } else { |
570 | /* capture streams can be powered down now */ | |
cb666e5b | 571 | snd_soc_dapm_stream_event(codec, |
0b4d221b LG |
572 | codec_dai->capture.stream_name, |
573 | SND_SOC_DAPM_STREAM_STOP); | |
db2a4165 FM |
574 | } |
575 | ||
576 | mutex_unlock(&pcm_mutex); | |
577 | return 0; | |
578 | } | |
579 | ||
580 | /* | |
581 | * Called by ALSA when the PCM substream is prepared, can set format, sample | |
582 | * rate, etc. This function is non atomic and can be called multiple times, | |
583 | * it can refer to the runtime info. | |
584 | */ | |
585 | static int soc_pcm_prepare(struct snd_pcm_substream *substream) | |
586 | { | |
587 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
588 | struct snd_soc_device *socdev = rtd->socdev; | |
6308419a | 589 | struct snd_soc_card *card = socdev->card; |
cb666e5b | 590 | struct snd_soc_dai_link *machine = rtd->dai; |
87689d56 | 591 | struct snd_soc_platform *platform = card->platform; |
3c4b266f LG |
592 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
593 | struct snd_soc_dai *codec_dai = machine->codec_dai; | |
6627a653 | 594 | struct snd_soc_codec *codec = card->codec; |
db2a4165 FM |
595 | int ret = 0; |
596 | ||
597 | mutex_lock(&pcm_mutex); | |
cb666e5b LG |
598 | |
599 | if (machine->ops && machine->ops->prepare) { | |
600 | ret = machine->ops->prepare(substream); | |
601 | if (ret < 0) { | |
602 | printk(KERN_ERR "asoc: machine prepare error\n"); | |
603 | goto out; | |
604 | } | |
605 | } | |
606 | ||
db2a4165 FM |
607 | if (platform->pcm_ops->prepare) { |
608 | ret = platform->pcm_ops->prepare(substream); | |
a71a468a LG |
609 | if (ret < 0) { |
610 | printk(KERN_ERR "asoc: platform prepare error\n"); | |
db2a4165 | 611 | goto out; |
a71a468a | 612 | } |
db2a4165 FM |
613 | } |
614 | ||
6335d055 EM |
615 | if (codec_dai->ops->prepare) { |
616 | ret = codec_dai->ops->prepare(substream, codec_dai); | |
a71a468a LG |
617 | if (ret < 0) { |
618 | printk(KERN_ERR "asoc: codec DAI prepare error\n"); | |
db2a4165 | 619 | goto out; |
a71a468a | 620 | } |
db2a4165 FM |
621 | } |
622 | ||
6335d055 EM |
623 | if (cpu_dai->ops->prepare) { |
624 | ret = cpu_dai->ops->prepare(substream, cpu_dai); | |
cb666e5b LG |
625 | if (ret < 0) { |
626 | printk(KERN_ERR "asoc: cpu DAI prepare error\n"); | |
627 | goto out; | |
628 | } | |
629 | } | |
db2a4165 | 630 | |
d45f6219 MB |
631 | /* cancel any delayed stream shutdown that is pending */ |
632 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && | |
633 | codec_dai->pop_wait) { | |
634 | codec_dai->pop_wait = 0; | |
6308419a | 635 | cancel_delayed_work(&card->delayed_work); |
d45f6219 | 636 | } |
db2a4165 | 637 | |
452c5eaa MB |
638 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
639 | snd_soc_dapm_stream_event(codec, | |
640 | codec_dai->playback.stream_name, | |
641 | SND_SOC_DAPM_STREAM_START); | |
642 | else | |
643 | snd_soc_dapm_stream_event(codec, | |
644 | codec_dai->capture.stream_name, | |
645 | SND_SOC_DAPM_STREAM_START); | |
8c6529db | 646 | |
452c5eaa | 647 | snd_soc_dai_digital_mute(codec_dai, 0); |
db2a4165 FM |
648 | |
649 | out: | |
650 | mutex_unlock(&pcm_mutex); | |
651 | return ret; | |
652 | } | |
653 | ||
654 | /* | |
655 | * Called by ALSA when the hardware params are set by application. This | |
656 | * function can also be called multiple times and can allocate buffers | |
657 | * (using snd_pcm_lib_* ). It's non-atomic. | |
658 | */ | |
659 | static int soc_pcm_hw_params(struct snd_pcm_substream *substream, | |
660 | struct snd_pcm_hw_params *params) | |
661 | { | |
662 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
663 | struct snd_soc_device *socdev = rtd->socdev; | |
cb666e5b | 664 | struct snd_soc_dai_link *machine = rtd->dai; |
87689d56 MB |
665 | struct snd_soc_card *card = socdev->card; |
666 | struct snd_soc_platform *platform = card->platform; | |
3c4b266f LG |
667 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
668 | struct snd_soc_dai *codec_dai = machine->codec_dai; | |
db2a4165 FM |
669 | int ret = 0; |
670 | ||
671 | mutex_lock(&pcm_mutex); | |
672 | ||
cb666e5b LG |
673 | if (machine->ops && machine->ops->hw_params) { |
674 | ret = machine->ops->hw_params(substream, params); | |
675 | if (ret < 0) { | |
676 | printk(KERN_ERR "asoc: machine hw_params failed\n"); | |
db2a4165 | 677 | goto out; |
cb666e5b | 678 | } |
db2a4165 FM |
679 | } |
680 | ||
6335d055 EM |
681 | if (codec_dai->ops->hw_params) { |
682 | ret = codec_dai->ops->hw_params(substream, params, codec_dai); | |
db2a4165 FM |
683 | if (ret < 0) { |
684 | printk(KERN_ERR "asoc: can't set codec %s hw params\n", | |
cb666e5b LG |
685 | codec_dai->name); |
686 | goto codec_err; | |
db2a4165 FM |
687 | } |
688 | } | |
689 | ||
6335d055 EM |
690 | if (cpu_dai->ops->hw_params) { |
691 | ret = cpu_dai->ops->hw_params(substream, params, cpu_dai); | |
db2a4165 | 692 | if (ret < 0) { |
3ff3f64b | 693 | printk(KERN_ERR "asoc: interface %s hw params failed\n", |
cb666e5b | 694 | cpu_dai->name); |
db2a4165 FM |
695 | goto interface_err; |
696 | } | |
697 | } | |
698 | ||
699 | if (platform->pcm_ops->hw_params) { | |
700 | ret = platform->pcm_ops->hw_params(substream, params); | |
701 | if (ret < 0) { | |
3ff3f64b | 702 | printk(KERN_ERR "asoc: platform %s hw params failed\n", |
db2a4165 FM |
703 | platform->name); |
704 | goto platform_err; | |
705 | } | |
706 | } | |
707 | ||
06f409d7 MB |
708 | machine->rate = params_rate(params); |
709 | ||
db2a4165 FM |
710 | out: |
711 | mutex_unlock(&pcm_mutex); | |
712 | return ret; | |
713 | ||
db2a4165 | 714 | platform_err: |
6335d055 EM |
715 | if (cpu_dai->ops->hw_free) |
716 | cpu_dai->ops->hw_free(substream, cpu_dai); | |
db2a4165 FM |
717 | |
718 | interface_err: | |
6335d055 EM |
719 | if (codec_dai->ops->hw_free) |
720 | codec_dai->ops->hw_free(substream, codec_dai); | |
cb666e5b LG |
721 | |
722 | codec_err: | |
3ff3f64b | 723 | if (machine->ops && machine->ops->hw_free) |
cb666e5b | 724 | machine->ops->hw_free(substream); |
db2a4165 FM |
725 | |
726 | mutex_unlock(&pcm_mutex); | |
727 | return ret; | |
728 | } | |
729 | ||
730 | /* | |
731 | * Free's resources allocated by hw_params, can be called multiple times | |
732 | */ | |
733 | static int soc_pcm_hw_free(struct snd_pcm_substream *substream) | |
734 | { | |
735 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
736 | struct snd_soc_device *socdev = rtd->socdev; | |
cb666e5b | 737 | struct snd_soc_dai_link *machine = rtd->dai; |
87689d56 MB |
738 | struct snd_soc_card *card = socdev->card; |
739 | struct snd_soc_platform *platform = card->platform; | |
3c4b266f LG |
740 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
741 | struct snd_soc_dai *codec_dai = machine->codec_dai; | |
6627a653 | 742 | struct snd_soc_codec *codec = card->codec; |
db2a4165 FM |
743 | |
744 | mutex_lock(&pcm_mutex); | |
745 | ||
746 | /* apply codec digital mute */ | |
8c6529db LG |
747 | if (!codec->active) |
748 | snd_soc_dai_digital_mute(codec_dai, 1); | |
db2a4165 FM |
749 | |
750 | /* free any machine hw params */ | |
751 | if (machine->ops && machine->ops->hw_free) | |
752 | machine->ops->hw_free(substream); | |
753 | ||
754 | /* free any DMA resources */ | |
755 | if (platform->pcm_ops->hw_free) | |
756 | platform->pcm_ops->hw_free(substream); | |
757 | ||
758 | /* now free hw params for the DAI's */ | |
6335d055 EM |
759 | if (codec_dai->ops->hw_free) |
760 | codec_dai->ops->hw_free(substream, codec_dai); | |
db2a4165 | 761 | |
6335d055 EM |
762 | if (cpu_dai->ops->hw_free) |
763 | cpu_dai->ops->hw_free(substream, cpu_dai); | |
db2a4165 FM |
764 | |
765 | mutex_unlock(&pcm_mutex); | |
766 | return 0; | |
767 | } | |
768 | ||
769 | static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) | |
770 | { | |
771 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
772 | struct snd_soc_device *socdev = rtd->socdev; | |
87689d56 | 773 | struct snd_soc_card *card= socdev->card; |
cb666e5b | 774 | struct snd_soc_dai_link *machine = rtd->dai; |
87689d56 | 775 | struct snd_soc_platform *platform = card->platform; |
3c4b266f LG |
776 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
777 | struct snd_soc_dai *codec_dai = machine->codec_dai; | |
db2a4165 FM |
778 | int ret; |
779 | ||
6335d055 EM |
780 | if (codec_dai->ops->trigger) { |
781 | ret = codec_dai->ops->trigger(substream, cmd, codec_dai); | |
db2a4165 FM |
782 | if (ret < 0) |
783 | return ret; | |
784 | } | |
785 | ||
786 | if (platform->pcm_ops->trigger) { | |
787 | ret = platform->pcm_ops->trigger(substream, cmd); | |
788 | if (ret < 0) | |
789 | return ret; | |
790 | } | |
791 | ||
6335d055 EM |
792 | if (cpu_dai->ops->trigger) { |
793 | ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai); | |
db2a4165 FM |
794 | if (ret < 0) |
795 | return ret; | |
796 | } | |
797 | return 0; | |
798 | } | |
799 | ||
800 | /* ASoC PCM operations */ | |
801 | static struct snd_pcm_ops soc_pcm_ops = { | |
802 | .open = soc_pcm_open, | |
803 | .close = soc_codec_close, | |
804 | .hw_params = soc_pcm_hw_params, | |
805 | .hw_free = soc_pcm_hw_free, | |
806 | .prepare = soc_pcm_prepare, | |
807 | .trigger = soc_pcm_trigger, | |
808 | }; | |
809 | ||
810 | #ifdef CONFIG_PM | |
811 | /* powers down audio subsystem for suspend */ | |
416356fc | 812 | static int soc_suspend(struct device *dev) |
db2a4165 | 813 | { |
416356fc | 814 | struct platform_device *pdev = to_platform_device(dev); |
3ff3f64b | 815 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
87506549 | 816 | struct snd_soc_card *card = socdev->card; |
87689d56 | 817 | struct snd_soc_platform *platform = card->platform; |
3ff3f64b | 818 | struct snd_soc_codec_device *codec_dev = socdev->codec_dev; |
6627a653 | 819 | struct snd_soc_codec *codec = card->codec; |
db2a4165 FM |
820 | int i; |
821 | ||
e3509ff0 DM |
822 | /* If the initialization of this soc device failed, there is no codec |
823 | * associated with it. Just bail out in this case. | |
824 | */ | |
825 | if (!codec) | |
826 | return 0; | |
827 | ||
6ed25978 AG |
828 | /* Due to the resume being scheduled into a workqueue we could |
829 | * suspend before that's finished - wait for it to complete. | |
830 | */ | |
831 | snd_power_lock(codec->card); | |
832 | snd_power_wait(codec->card, SNDRV_CTL_POWER_D0); | |
833 | snd_power_unlock(codec->card); | |
834 | ||
835 | /* we're going to block userspace touching us until resume completes */ | |
836 | snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot); | |
837 | ||
db2a4165 | 838 | /* mute any active DAC's */ |
dee89c4d MB |
839 | for (i = 0; i < card->num_links; i++) { |
840 | struct snd_soc_dai *dai = card->dai_link[i].codec_dai; | |
6335d055 EM |
841 | if (dai->ops->digital_mute && dai->playback.active) |
842 | dai->ops->digital_mute(dai, 1); | |
db2a4165 FM |
843 | } |
844 | ||
4ccab3e7 | 845 | /* suspend all pcms */ |
87506549 MB |
846 | for (i = 0; i < card->num_links; i++) |
847 | snd_pcm_suspend_all(card->dai_link[i].pcm); | |
4ccab3e7 | 848 | |
87506549 | 849 | if (card->suspend_pre) |
416356fc | 850 | card->suspend_pre(pdev, PMSG_SUSPEND); |
db2a4165 | 851 | |
87506549 MB |
852 | for (i = 0; i < card->num_links; i++) { |
853 | struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; | |
3ba9e10a | 854 | if (cpu_dai->suspend && !cpu_dai->ac97_control) |
dc7d7b83 | 855 | cpu_dai->suspend(cpu_dai); |
db2a4165 | 856 | if (platform->suspend) |
07c84d04 | 857 | platform->suspend(cpu_dai); |
db2a4165 FM |
858 | } |
859 | ||
860 | /* close any waiting streams and save state */ | |
6308419a | 861 | run_delayed_work(&card->delayed_work); |
0be9898a | 862 | codec->suspend_bias_level = codec->bias_level; |
db2a4165 | 863 | |
3ff3f64b | 864 | for (i = 0; i < codec->num_dai; i++) { |
db2a4165 FM |
865 | char *stream = codec->dai[i].playback.stream_name; |
866 | if (stream != NULL) | |
867 | snd_soc_dapm_stream_event(codec, stream, | |
868 | SND_SOC_DAPM_STREAM_SUSPEND); | |
869 | stream = codec->dai[i].capture.stream_name; | |
870 | if (stream != NULL) | |
871 | snd_soc_dapm_stream_event(codec, stream, | |
872 | SND_SOC_DAPM_STREAM_SUSPEND); | |
873 | } | |
874 | ||
875 | if (codec_dev->suspend) | |
416356fc | 876 | codec_dev->suspend(pdev, PMSG_SUSPEND); |
db2a4165 | 877 | |
87506549 MB |
878 | for (i = 0; i < card->num_links; i++) { |
879 | struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; | |
3ba9e10a | 880 | if (cpu_dai->suspend && cpu_dai->ac97_control) |
dc7d7b83 | 881 | cpu_dai->suspend(cpu_dai); |
db2a4165 FM |
882 | } |
883 | ||
87506549 | 884 | if (card->suspend_post) |
416356fc | 885 | card->suspend_post(pdev, PMSG_SUSPEND); |
db2a4165 FM |
886 | |
887 | return 0; | |
888 | } | |
889 | ||
6ed25978 AG |
890 | /* deferred resume work, so resume can complete before we finished |
891 | * setting our codec back up, which can be very slow on I2C | |
892 | */ | |
893 | static void soc_resume_deferred(struct work_struct *work) | |
db2a4165 | 894 | { |
6308419a MB |
895 | struct snd_soc_card *card = container_of(work, |
896 | struct snd_soc_card, | |
897 | deferred_resume_work); | |
898 | struct snd_soc_device *socdev = card->socdev; | |
87689d56 | 899 | struct snd_soc_platform *platform = card->platform; |
3ff3f64b | 900 | struct snd_soc_codec_device *codec_dev = socdev->codec_dev; |
6627a653 | 901 | struct snd_soc_codec *codec = card->codec; |
6ed25978 | 902 | struct platform_device *pdev = to_platform_device(socdev->dev); |
db2a4165 FM |
903 | int i; |
904 | ||
6ed25978 AG |
905 | /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, |
906 | * so userspace apps are blocked from touching us | |
907 | */ | |
908 | ||
fde22f27 | 909 | dev_dbg(socdev->dev, "starting resume work\n"); |
6ed25978 | 910 | |
87506549 MB |
911 | if (card->resume_pre) |
912 | card->resume_pre(pdev); | |
db2a4165 | 913 | |
87506549 MB |
914 | for (i = 0; i < card->num_links; i++) { |
915 | struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; | |
3ba9e10a | 916 | if (cpu_dai->resume && cpu_dai->ac97_control) |
dc7d7b83 | 917 | cpu_dai->resume(cpu_dai); |
db2a4165 FM |
918 | } |
919 | ||
920 | if (codec_dev->resume) | |
921 | codec_dev->resume(pdev); | |
922 | ||
3ff3f64b MB |
923 | for (i = 0; i < codec->num_dai; i++) { |
924 | char *stream = codec->dai[i].playback.stream_name; | |
db2a4165 FM |
925 | if (stream != NULL) |
926 | snd_soc_dapm_stream_event(codec, stream, | |
927 | SND_SOC_DAPM_STREAM_RESUME); | |
928 | stream = codec->dai[i].capture.stream_name; | |
929 | if (stream != NULL) | |
930 | snd_soc_dapm_stream_event(codec, stream, | |
931 | SND_SOC_DAPM_STREAM_RESUME); | |
932 | } | |
933 | ||
3ff3f64b | 934 | /* unmute any active DACs */ |
dee89c4d MB |
935 | for (i = 0; i < card->num_links; i++) { |
936 | struct snd_soc_dai *dai = card->dai_link[i].codec_dai; | |
6335d055 EM |
937 | if (dai->ops->digital_mute && dai->playback.active) |
938 | dai->ops->digital_mute(dai, 0); | |
db2a4165 FM |
939 | } |
940 | ||
87506549 MB |
941 | for (i = 0; i < card->num_links; i++) { |
942 | struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; | |
3ba9e10a | 943 | if (cpu_dai->resume && !cpu_dai->ac97_control) |
dc7d7b83 | 944 | cpu_dai->resume(cpu_dai); |
db2a4165 | 945 | if (platform->resume) |
07c84d04 | 946 | platform->resume(cpu_dai); |
db2a4165 FM |
947 | } |
948 | ||
87506549 MB |
949 | if (card->resume_post) |
950 | card->resume_post(pdev); | |
db2a4165 | 951 | |
fde22f27 | 952 | dev_dbg(socdev->dev, "resume work completed\n"); |
6ed25978 AG |
953 | |
954 | /* userspace can access us now we are back as we were before */ | |
955 | snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0); | |
956 | } | |
957 | ||
958 | /* powers up audio subsystem after a suspend */ | |
416356fc | 959 | static int soc_resume(struct device *dev) |
6ed25978 | 960 | { |
416356fc | 961 | struct platform_device *pdev = to_platform_device(dev); |
6ed25978 | 962 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
6308419a | 963 | struct snd_soc_card *card = socdev->card; |
64ab9baa | 964 | struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai; |
6ed25978 | 965 | |
64ab9baa MB |
966 | /* AC97 devices might have other drivers hanging off them so |
967 | * need to resume immediately. Other drivers don't have that | |
968 | * problem and may take a substantial amount of time to resume | |
969 | * due to I/O costs and anti-pop so handle them out of line. | |
970 | */ | |
971 | if (cpu_dai->ac97_control) { | |
972 | dev_dbg(socdev->dev, "Resuming AC97 immediately\n"); | |
973 | soc_resume_deferred(&card->deferred_resume_work); | |
974 | } else { | |
975 | dev_dbg(socdev->dev, "Scheduling resume work\n"); | |
976 | if (!schedule_work(&card->deferred_resume_work)) | |
977 | dev_err(socdev->dev, "resume work item may be lost\n"); | |
978 | } | |
6ed25978 | 979 | |
db2a4165 FM |
980 | return 0; |
981 | } | |
db2a4165 FM |
982 | #else |
983 | #define soc_suspend NULL | |
984 | #define soc_resume NULL | |
985 | #endif | |
986 | ||
02a06d30 BS |
987 | static struct snd_soc_dai_ops null_dai_ops = { |
988 | }; | |
989 | ||
435c5e25 | 990 | static void snd_soc_instantiate_card(struct snd_soc_card *card) |
db2a4165 | 991 | { |
435c5e25 MB |
992 | struct platform_device *pdev = container_of(card->dev, |
993 | struct platform_device, | |
994 | dev); | |
995 | struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev; | |
fe3e78e0 | 996 | struct snd_soc_codec *codec; |
435c5e25 MB |
997 | struct snd_soc_platform *platform; |
998 | struct snd_soc_dai *dai; | |
6b05eda6 | 999 | int i, found, ret, ac97; |
435c5e25 MB |
1000 | |
1001 | if (card->instantiated) | |
1002 | return; | |
1003 | ||
1004 | found = 0; | |
1005 | list_for_each_entry(platform, &platform_list, list) | |
1006 | if (card->platform == platform) { | |
1007 | found = 1; | |
1008 | break; | |
1009 | } | |
1010 | if (!found) { | |
1011 | dev_dbg(card->dev, "Platform %s not registered\n", | |
1012 | card->platform->name); | |
1013 | return; | |
1014 | } | |
6308419a | 1015 | |
6b05eda6 | 1016 | ac97 = 0; |
435c5e25 MB |
1017 | for (i = 0; i < card->num_links; i++) { |
1018 | found = 0; | |
1019 | list_for_each_entry(dai, &dai_list, list) | |
1020 | if (card->dai_link[i].cpu_dai == dai) { | |
1021 | found = 1; | |
1022 | break; | |
1023 | } | |
1024 | if (!found) { | |
1025 | dev_dbg(card->dev, "DAI %s not registered\n", | |
1026 | card->dai_link[i].cpu_dai->name); | |
1027 | return; | |
1028 | } | |
6b05eda6 MB |
1029 | |
1030 | if (card->dai_link[i].cpu_dai->ac97_control) | |
1031 | ac97 = 1; | |
c5af3a2e MB |
1032 | } |
1033 | ||
02a06d30 BS |
1034 | for (i = 0; i < card->num_links; i++) { |
1035 | if (!card->dai_link[i].codec_dai->ops) | |
1036 | card->dai_link[i].codec_dai->ops = &null_dai_ops; | |
1037 | } | |
1038 | ||
6b05eda6 MB |
1039 | /* If we have AC97 in the system then don't wait for the |
1040 | * codec. This will need revisiting if we have to handle | |
1041 | * systems with mixed AC97 and non-AC97 parts. Only check for | |
1042 | * DAIs currently; we can't do this per link since some AC97 | |
1043 | * codecs have non-AC97 DAIs. | |
1044 | */ | |
1045 | if (!ac97) | |
1046 | for (i = 0; i < card->num_links; i++) { | |
1047 | found = 0; | |
1048 | list_for_each_entry(dai, &dai_list, list) | |
1049 | if (card->dai_link[i].codec_dai == dai) { | |
1050 | found = 1; | |
1051 | break; | |
1052 | } | |
1053 | if (!found) { | |
1054 | dev_dbg(card->dev, "DAI %s not registered\n", | |
1055 | card->dai_link[i].codec_dai->name); | |
1056 | return; | |
1057 | } | |
1058 | } | |
1059 | ||
435c5e25 MB |
1060 | /* Note that we do not current check for codec components */ |
1061 | ||
1062 | dev_dbg(card->dev, "All components present, instantiating\n"); | |
1063 | ||
1064 | /* Found everything, bring it up */ | |
96dd3622 MB |
1065 | card->pmdown_time = pmdown_time; |
1066 | ||
87506549 MB |
1067 | if (card->probe) { |
1068 | ret = card->probe(pdev); | |
3ff3f64b | 1069 | if (ret < 0) |
435c5e25 | 1070 | return; |
db2a4165 FM |
1071 | } |
1072 | ||
87506549 MB |
1073 | for (i = 0; i < card->num_links; i++) { |
1074 | struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; | |
db2a4165 | 1075 | if (cpu_dai->probe) { |
bdb92876 | 1076 | ret = cpu_dai->probe(pdev, cpu_dai); |
3ff3f64b | 1077 | if (ret < 0) |
db2a4165 FM |
1078 | goto cpu_dai_err; |
1079 | } | |
1080 | } | |
1081 | ||
1082 | if (codec_dev->probe) { | |
1083 | ret = codec_dev->probe(pdev); | |
3ff3f64b | 1084 | if (ret < 0) |
db2a4165 FM |
1085 | goto cpu_dai_err; |
1086 | } | |
fe3e78e0 | 1087 | codec = card->codec; |
db2a4165 FM |
1088 | |
1089 | if (platform->probe) { | |
1090 | ret = platform->probe(pdev); | |
3ff3f64b | 1091 | if (ret < 0) |
db2a4165 FM |
1092 | goto platform_err; |
1093 | } | |
1094 | ||
1095 | /* DAPM stream work */ | |
6308419a | 1096 | INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work); |
1301a964 | 1097 | #ifdef CONFIG_PM |
6ed25978 | 1098 | /* deferred resume work */ |
6308419a | 1099 | INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); |
1301a964 | 1100 | #endif |
6ed25978 | 1101 | |
fe3e78e0 MB |
1102 | for (i = 0; i < card->num_links; i++) { |
1103 | if (card->dai_link[i].init) { | |
1104 | ret = card->dai_link[i].init(codec); | |
1105 | if (ret < 0) { | |
1106 | printk(KERN_ERR "asoc: failed to init %s\n", | |
1107 | card->dai_link[i].stream_name); | |
1108 | continue; | |
1109 | } | |
1110 | } | |
f7732053 | 1111 | if (card->dai_link[i].codec_dai->ac97_control) |
fe3e78e0 | 1112 | ac97 = 1; |
fe3e78e0 MB |
1113 | } |
1114 | ||
1115 | snprintf(codec->card->shortname, sizeof(codec->card->shortname), | |
1116 | "%s", card->name); | |
1117 | snprintf(codec->card->longname, sizeof(codec->card->longname), | |
1118 | "%s (%s)", card->name, codec->name); | |
1119 | ||
1120 | /* Make sure all DAPM widgets are instantiated */ | |
1121 | snd_soc_dapm_new_widgets(codec); | |
1122 | ||
1123 | ret = snd_card_register(codec->card); | |
1124 | if (ret < 0) { | |
1125 | printk(KERN_ERR "asoc: failed to register soundcard for %s\n", | |
1126 | codec->name); | |
1127 | goto card_err; | |
1128 | } | |
1129 | ||
1130 | mutex_lock(&codec->mutex); | |
1131 | #ifdef CONFIG_SND_SOC_AC97_BUS | |
1132 | /* Only instantiate AC97 if not already done by the adaptor | |
1133 | * for the generic AC97 subsystem. | |
1134 | */ | |
1135 | if (ac97 && strcmp(codec->name, "AC97") != 0) { | |
1136 | ret = soc_ac97_dev_register(codec); | |
1137 | if (ret < 0) { | |
1138 | printk(KERN_ERR "asoc: AC97 device register failed\n"); | |
1139 | snd_card_free(codec->card); | |
1140 | mutex_unlock(&codec->mutex); | |
1141 | goto card_err; | |
1142 | } | |
1143 | } | |
1144 | #endif | |
1145 | ||
1146 | ret = snd_soc_dapm_sys_add(card->socdev->dev); | |
1147 | if (ret < 0) | |
1148 | printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n"); | |
1149 | ||
dbe21408 MB |
1150 | ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time); |
1151 | if (ret < 0) | |
1152 | printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n"); | |
1153 | ||
fe3e78e0 MB |
1154 | ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg); |
1155 | if (ret < 0) | |
1156 | printk(KERN_WARNING "asoc: failed to add codec sysfs files\n"); | |
1157 | ||
1158 | soc_init_codec_debugfs(codec); | |
1159 | mutex_unlock(&codec->mutex); | |
1160 | ||
435c5e25 MB |
1161 | card->instantiated = 1; |
1162 | ||
1163 | return; | |
db2a4165 | 1164 | |
fe3e78e0 MB |
1165 | card_err: |
1166 | if (platform->remove) | |
1167 | platform->remove(pdev); | |
1168 | ||
db2a4165 FM |
1169 | platform_err: |
1170 | if (codec_dev->remove) | |
1171 | codec_dev->remove(pdev); | |
1172 | ||
1173 | cpu_dai_err: | |
18b9b3d9 | 1174 | for (i--; i >= 0; i--) { |
87506549 | 1175 | struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; |
db2a4165 | 1176 | if (cpu_dai->remove) |
bdb92876 | 1177 | cpu_dai->remove(pdev, cpu_dai); |
db2a4165 FM |
1178 | } |
1179 | ||
87506549 MB |
1180 | if (card->remove) |
1181 | card->remove(pdev); | |
435c5e25 | 1182 | } |
db2a4165 | 1183 | |
435c5e25 MB |
1184 | /* |
1185 | * Attempt to initialise any uninitalised cards. Must be called with | |
1186 | * client_mutex. | |
1187 | */ | |
1188 | static void snd_soc_instantiate_cards(void) | |
1189 | { | |
1190 | struct snd_soc_card *card; | |
1191 | list_for_each_entry(card, &card_list, list) | |
1192 | snd_soc_instantiate_card(card); | |
1193 | } | |
1194 | ||
1195 | /* probes a new socdev */ | |
1196 | static int soc_probe(struct platform_device *pdev) | |
1197 | { | |
1198 | int ret = 0; | |
1199 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
1200 | struct snd_soc_card *card = socdev->card; | |
1201 | ||
1202 | /* Bodge while we push things out of socdev */ | |
1203 | card->socdev = socdev; | |
1204 | ||
1205 | /* Bodge while we unpick instantiation */ | |
1206 | card->dev = &pdev->dev; | |
1207 | ret = snd_soc_register_card(card); | |
1208 | if (ret != 0) { | |
1209 | dev_err(&pdev->dev, "Failed to register card\n"); | |
1210 | return ret; | |
1211 | } | |
1212 | ||
1213 | return 0; | |
db2a4165 FM |
1214 | } |
1215 | ||
1216 | /* removes a socdev */ | |
1217 | static int soc_remove(struct platform_device *pdev) | |
1218 | { | |
1219 | int i; | |
1220 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
87506549 | 1221 | struct snd_soc_card *card = socdev->card; |
87689d56 | 1222 | struct snd_soc_platform *platform = card->platform; |
db2a4165 FM |
1223 | struct snd_soc_codec_device *codec_dev = socdev->codec_dev; |
1224 | ||
914dc182 MR |
1225 | if (!card->instantiated) |
1226 | return 0; | |
1227 | ||
6308419a | 1228 | run_delayed_work(&card->delayed_work); |
965ac42c | 1229 | |
db2a4165 FM |
1230 | if (platform->remove) |
1231 | platform->remove(pdev); | |
1232 | ||
1233 | if (codec_dev->remove) | |
1234 | codec_dev->remove(pdev); | |
1235 | ||
87506549 MB |
1236 | for (i = 0; i < card->num_links; i++) { |
1237 | struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; | |
db2a4165 | 1238 | if (cpu_dai->remove) |
bdb92876 | 1239 | cpu_dai->remove(pdev, cpu_dai); |
db2a4165 FM |
1240 | } |
1241 | ||
87506549 MB |
1242 | if (card->remove) |
1243 | card->remove(pdev); | |
db2a4165 | 1244 | |
c5af3a2e MB |
1245 | snd_soc_unregister_card(card); |
1246 | ||
db2a4165 FM |
1247 | return 0; |
1248 | } | |
1249 | ||
416356fc | 1250 | static int soc_poweroff(struct device *dev) |
51737470 | 1251 | { |
416356fc | 1252 | struct platform_device *pdev = to_platform_device(dev); |
51737470 MB |
1253 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
1254 | struct snd_soc_card *card = socdev->card; | |
1255 | ||
1256 | if (!card->instantiated) | |
416356fc | 1257 | return 0; |
51737470 MB |
1258 | |
1259 | /* Flush out pmdown_time work - we actually do want to run it | |
1260 | * now, we're shutting down so no imminent restart. */ | |
1261 | run_delayed_work(&card->delayed_work); | |
1262 | ||
1263 | snd_soc_dapm_shutdown(socdev); | |
416356fc MB |
1264 | |
1265 | return 0; | |
51737470 MB |
1266 | } |
1267 | ||
47145210 | 1268 | static const struct dev_pm_ops soc_pm_ops = { |
416356fc MB |
1269 | .suspend = soc_suspend, |
1270 | .resume = soc_resume, | |
1271 | .poweroff = soc_poweroff, | |
1272 | }; | |
1273 | ||
db2a4165 FM |
1274 | /* ASoC platform driver */ |
1275 | static struct platform_driver soc_driver = { | |
1276 | .driver = { | |
1277 | .name = "soc-audio", | |
8b45a209 | 1278 | .owner = THIS_MODULE, |
416356fc | 1279 | .pm = &soc_pm_ops, |
db2a4165 FM |
1280 | }, |
1281 | .probe = soc_probe, | |
1282 | .remove = soc_remove, | |
db2a4165 FM |
1283 | }; |
1284 | ||
1285 | /* create a new pcm */ | |
1286 | static int soc_new_pcm(struct snd_soc_device *socdev, | |
1287 | struct snd_soc_dai_link *dai_link, int num) | |
1288 | { | |
87689d56 | 1289 | struct snd_soc_card *card = socdev->card; |
6627a653 | 1290 | struct snd_soc_codec *codec = card->codec; |
87689d56 | 1291 | struct snd_soc_platform *platform = card->platform; |
3c4b266f LG |
1292 | struct snd_soc_dai *codec_dai = dai_link->codec_dai; |
1293 | struct snd_soc_dai *cpu_dai = dai_link->cpu_dai; | |
db2a4165 FM |
1294 | struct snd_soc_pcm_runtime *rtd; |
1295 | struct snd_pcm *pcm; | |
1296 | char new_name[64]; | |
1297 | int ret = 0, playback = 0, capture = 0; | |
1298 | ||
1299 | rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL); | |
1300 | if (rtd == NULL) | |
1301 | return -ENOMEM; | |
cb666e5b LG |
1302 | |
1303 | rtd->dai = dai_link; | |
db2a4165 | 1304 | rtd->socdev = socdev; |
6627a653 | 1305 | codec_dai->codec = card->codec; |
db2a4165 FM |
1306 | |
1307 | /* check client and interface hw capabilities */ | |
40ca1142 MB |
1308 | snprintf(new_name, sizeof(new_name), "%s %s-%d", |
1309 | dai_link->stream_name, codec_dai->name, num); | |
db2a4165 FM |
1310 | |
1311 | if (codec_dai->playback.channels_min) | |
1312 | playback = 1; | |
1313 | if (codec_dai->capture.channels_min) | |
1314 | capture = 1; | |
1315 | ||
1316 | ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback, | |
1317 | capture, &pcm); | |
1318 | if (ret < 0) { | |
3ff3f64b MB |
1319 | printk(KERN_ERR "asoc: can't create pcm for codec %s\n", |
1320 | codec->name); | |
db2a4165 FM |
1321 | kfree(rtd); |
1322 | return ret; | |
1323 | } | |
1324 | ||
4ccab3e7 | 1325 | dai_link->pcm = pcm; |
db2a4165 | 1326 | pcm->private_data = rtd; |
87689d56 MB |
1327 | soc_pcm_ops.mmap = platform->pcm_ops->mmap; |
1328 | soc_pcm_ops.pointer = platform->pcm_ops->pointer; | |
1329 | soc_pcm_ops.ioctl = platform->pcm_ops->ioctl; | |
1330 | soc_pcm_ops.copy = platform->pcm_ops->copy; | |
1331 | soc_pcm_ops.silence = platform->pcm_ops->silence; | |
1332 | soc_pcm_ops.ack = platform->pcm_ops->ack; | |
1333 | soc_pcm_ops.page = platform->pcm_ops->page; | |
db2a4165 FM |
1334 | |
1335 | if (playback) | |
1336 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops); | |
1337 | ||
1338 | if (capture) | |
1339 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops); | |
1340 | ||
87689d56 | 1341 | ret = platform->pcm_new(codec->card, codec_dai, pcm); |
db2a4165 FM |
1342 | if (ret < 0) { |
1343 | printk(KERN_ERR "asoc: platform pcm constructor failed\n"); | |
1344 | kfree(rtd); | |
1345 | return ret; | |
1346 | } | |
1347 | ||
87689d56 | 1348 | pcm->private_free = platform->pcm_free; |
db2a4165 FM |
1349 | printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name, |
1350 | cpu_dai->name); | |
1351 | return ret; | |
1352 | } | |
1353 | ||
096e49d5 MB |
1354 | /** |
1355 | * snd_soc_codec_volatile_register: Report if a register is volatile. | |
1356 | * | |
1357 | * @codec: CODEC to query. | |
1358 | * @reg: Register to query. | |
1359 | * | |
1360 | * Boolean function indiciating if a CODEC register is volatile. | |
1361 | */ | |
1362 | int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg) | |
1363 | { | |
1364 | if (codec->volatile_register) | |
1365 | return codec->volatile_register(reg); | |
1366 | else | |
1367 | return 0; | |
1368 | } | |
1369 | EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register); | |
1370 | ||
db2a4165 FM |
1371 | /** |
1372 | * snd_soc_new_ac97_codec - initailise AC97 device | |
1373 | * @codec: audio codec | |
1374 | * @ops: AC97 bus operations | |
1375 | * @num: AC97 codec number | |
1376 | * | |
1377 | * Initialises AC97 codec resources for use by ad-hoc devices only. | |
1378 | */ | |
1379 | int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, | |
1380 | struct snd_ac97_bus_ops *ops, int num) | |
1381 | { | |
1382 | mutex_lock(&codec->mutex); | |
1383 | ||
1384 | codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); | |
1385 | if (codec->ac97 == NULL) { | |
1386 | mutex_unlock(&codec->mutex); | |
1387 | return -ENOMEM; | |
1388 | } | |
1389 | ||
1390 | codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); | |
1391 | if (codec->ac97->bus == NULL) { | |
1392 | kfree(codec->ac97); | |
1393 | codec->ac97 = NULL; | |
1394 | mutex_unlock(&codec->mutex); | |
1395 | return -ENOMEM; | |
1396 | } | |
1397 | ||
1398 | codec->ac97->bus->ops = ops; | |
1399 | codec->ac97->num = num; | |
2718625f | 1400 | codec->dev = &codec->ac97->dev; |
db2a4165 FM |
1401 | mutex_unlock(&codec->mutex); |
1402 | return 0; | |
1403 | } | |
1404 | EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); | |
1405 | ||
1406 | /** | |
1407 | * snd_soc_free_ac97_codec - free AC97 codec device | |
1408 | * @codec: audio codec | |
1409 | * | |
1410 | * Frees AC97 codec device resources. | |
1411 | */ | |
1412 | void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) | |
1413 | { | |
1414 | mutex_lock(&codec->mutex); | |
1415 | kfree(codec->ac97->bus); | |
1416 | kfree(codec->ac97); | |
1417 | codec->ac97 = NULL; | |
1418 | mutex_unlock(&codec->mutex); | |
1419 | } | |
1420 | EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); | |
1421 | ||
1422 | /** | |
1423 | * snd_soc_update_bits - update codec register bits | |
1424 | * @codec: audio codec | |
1425 | * @reg: codec register | |
1426 | * @mask: register mask | |
1427 | * @value: new value | |
1428 | * | |
1429 | * Writes new register value. | |
1430 | * | |
1431 | * Returns 1 for change else 0. | |
1432 | */ | |
1433 | int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg, | |
46f5822f | 1434 | unsigned int mask, unsigned int value) |
db2a4165 FM |
1435 | { |
1436 | int change; | |
46f5822f | 1437 | unsigned int old, new; |
db2a4165 | 1438 | |
db2a4165 FM |
1439 | old = snd_soc_read(codec, reg); |
1440 | new = (old & ~mask) | value; | |
1441 | change = old != new; | |
1442 | if (change) | |
1443 | snd_soc_write(codec, reg, new); | |
1444 | ||
db2a4165 FM |
1445 | return change; |
1446 | } | |
1447 | EXPORT_SYMBOL_GPL(snd_soc_update_bits); | |
1448 | ||
6c508c62 EN |
1449 | /** |
1450 | * snd_soc_update_bits_locked - update codec register bits | |
1451 | * @codec: audio codec | |
1452 | * @reg: codec register | |
1453 | * @mask: register mask | |
1454 | * @value: new value | |
1455 | * | |
1456 | * Writes new register value, and takes the codec mutex. | |
1457 | * | |
1458 | * Returns 1 for change else 0. | |
1459 | */ | |
dd1b3d53 MB |
1460 | int snd_soc_update_bits_locked(struct snd_soc_codec *codec, |
1461 | unsigned short reg, unsigned int mask, | |
1462 | unsigned int value) | |
6c508c62 EN |
1463 | { |
1464 | int change; | |
1465 | ||
1466 | mutex_lock(&codec->mutex); | |
1467 | change = snd_soc_update_bits(codec, reg, mask, value); | |
1468 | mutex_unlock(&codec->mutex); | |
1469 | ||
1470 | return change; | |
1471 | } | |
dd1b3d53 | 1472 | EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked); |
6c508c62 | 1473 | |
db2a4165 FM |
1474 | /** |
1475 | * snd_soc_test_bits - test register for change | |
1476 | * @codec: audio codec | |
1477 | * @reg: codec register | |
1478 | * @mask: register mask | |
1479 | * @value: new value | |
1480 | * | |
1481 | * Tests a register with a new value and checks if the new value is | |
1482 | * different from the old value. | |
1483 | * | |
1484 | * Returns 1 for change else 0. | |
1485 | */ | |
1486 | int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg, | |
46f5822f | 1487 | unsigned int mask, unsigned int value) |
db2a4165 FM |
1488 | { |
1489 | int change; | |
46f5822f | 1490 | unsigned int old, new; |
db2a4165 | 1491 | |
db2a4165 FM |
1492 | old = snd_soc_read(codec, reg); |
1493 | new = (old & ~mask) | value; | |
1494 | change = old != new; | |
db2a4165 FM |
1495 | |
1496 | return change; | |
1497 | } | |
1498 | EXPORT_SYMBOL_GPL(snd_soc_test_bits); | |
1499 | ||
db2a4165 FM |
1500 | /** |
1501 | * snd_soc_new_pcms - create new sound card and pcms | |
1502 | * @socdev: the SoC audio device | |
ac11a2b3 MB |
1503 | * @idx: ALSA card index |
1504 | * @xid: card identification | |
db2a4165 FM |
1505 | * |
1506 | * Create a new sound card based upon the codec and interface pcms. | |
1507 | * | |
1508 | * Returns 0 for success, else error. | |
1509 | */ | |
bc7320c5 | 1510 | int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid) |
db2a4165 | 1511 | { |
87506549 | 1512 | struct snd_soc_card *card = socdev->card; |
6627a653 | 1513 | struct snd_soc_codec *codec = card->codec; |
bd7dd77c | 1514 | int ret, i; |
db2a4165 FM |
1515 | |
1516 | mutex_lock(&codec->mutex); | |
1517 | ||
1518 | /* register a sound card */ | |
bd7dd77c TI |
1519 | ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card); |
1520 | if (ret < 0) { | |
db2a4165 FM |
1521 | printk(KERN_ERR "asoc: can't create sound card for codec %s\n", |
1522 | codec->name); | |
1523 | mutex_unlock(&codec->mutex); | |
bd7dd77c | 1524 | return ret; |
db2a4165 FM |
1525 | } |
1526 | ||
452c5eaa | 1527 | codec->socdev = socdev; |
db2a4165 FM |
1528 | codec->card->dev = socdev->dev; |
1529 | codec->card->private_data = codec; | |
1530 | strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver)); | |
1531 | ||
1532 | /* create the pcms */ | |
87506549 MB |
1533 | for (i = 0; i < card->num_links; i++) { |
1534 | ret = soc_new_pcm(socdev, &card->dai_link[i], i); | |
db2a4165 FM |
1535 | if (ret < 0) { |
1536 | printk(KERN_ERR "asoc: can't create pcm %s\n", | |
87506549 | 1537 | card->dai_link[i].stream_name); |
db2a4165 FM |
1538 | mutex_unlock(&codec->mutex); |
1539 | return ret; | |
1540 | } | |
f7732053 BS |
1541 | if (card->dai_link[i].codec_dai->ac97_control) { |
1542 | snd_ac97_dev_add_pdata(codec->ac97, | |
1543 | card->dai_link[i].cpu_dai->ac97_pdata); | |
1544 | } | |
db2a4165 FM |
1545 | } |
1546 | ||
1547 | mutex_unlock(&codec->mutex); | |
1548 | return ret; | |
1549 | } | |
1550 | EXPORT_SYMBOL_GPL(snd_soc_new_pcms); | |
1551 | ||
db2a4165 FM |
1552 | /** |
1553 | * snd_soc_free_pcms - free sound card and pcms | |
1554 | * @socdev: the SoC audio device | |
1555 | * | |
1556 | * Frees sound card and pcms associated with the socdev. | |
1557 | * Also unregister the codec if it is an AC97 device. | |
1558 | */ | |
1559 | void snd_soc_free_pcms(struct snd_soc_device *socdev) | |
1560 | { | |
6627a653 | 1561 | struct snd_soc_codec *codec = socdev->card->codec; |
a68660e0 | 1562 | #ifdef CONFIG_SND_SOC_AC97_BUS |
3c4b266f | 1563 | struct snd_soc_dai *codec_dai; |
a68660e0 LG |
1564 | int i; |
1565 | #endif | |
db2a4165 FM |
1566 | |
1567 | mutex_lock(&codec->mutex); | |
6627a653 | 1568 | soc_cleanup_codec_debugfs(codec); |
db2a4165 | 1569 | #ifdef CONFIG_SND_SOC_AC97_BUS |
3ff3f64b | 1570 | for (i = 0; i < codec->num_dai; i++) { |
a68660e0 | 1571 | codec_dai = &codec->dai[i]; |
d2314e0e AN |
1572 | if (codec_dai->ac97_control && codec->ac97 && |
1573 | strcmp(codec->name, "AC97") != 0) { | |
a68660e0 LG |
1574 | soc_ac97_dev_unregister(codec); |
1575 | goto free_card; | |
1576 | } | |
1577 | } | |
1578 | free_card: | |
db2a4165 FM |
1579 | #endif |
1580 | ||
1581 | if (codec->card) | |
1582 | snd_card_free(codec->card); | |
1583 | device_remove_file(socdev->dev, &dev_attr_codec_reg); | |
1584 | mutex_unlock(&codec->mutex); | |
1585 | } | |
1586 | EXPORT_SYMBOL_GPL(snd_soc_free_pcms); | |
1587 | ||
1588 | /** | |
1589 | * snd_soc_set_runtime_hwparams - set the runtime hardware parameters | |
1590 | * @substream: the pcm substream | |
1591 | * @hw: the hardware parameters | |
1592 | * | |
1593 | * Sets the substream runtime hardware parameters. | |
1594 | */ | |
1595 | int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, | |
1596 | const struct snd_pcm_hardware *hw) | |
1597 | { | |
1598 | struct snd_pcm_runtime *runtime = substream->runtime; | |
1599 | runtime->hw.info = hw->info; | |
1600 | runtime->hw.formats = hw->formats; | |
1601 | runtime->hw.period_bytes_min = hw->period_bytes_min; | |
1602 | runtime->hw.period_bytes_max = hw->period_bytes_max; | |
1603 | runtime->hw.periods_min = hw->periods_min; | |
1604 | runtime->hw.periods_max = hw->periods_max; | |
1605 | runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; | |
1606 | runtime->hw.fifo_size = hw->fifo_size; | |
1607 | return 0; | |
1608 | } | |
1609 | EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); | |
1610 | ||
1611 | /** | |
1612 | * snd_soc_cnew - create new control | |
1613 | * @_template: control template | |
1614 | * @data: control private data | |
ac11a2b3 | 1615 | * @long_name: control long name |
db2a4165 FM |
1616 | * |
1617 | * Create a new mixer control from a template control. | |
1618 | * | |
1619 | * Returns 0 for success, else error. | |
1620 | */ | |
1621 | struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, | |
1622 | void *data, char *long_name) | |
1623 | { | |
1624 | struct snd_kcontrol_new template; | |
1625 | ||
1626 | memcpy(&template, _template, sizeof(template)); | |
1627 | if (long_name) | |
1628 | template.name = long_name; | |
db2a4165 FM |
1629 | template.index = 0; |
1630 | ||
1631 | return snd_ctl_new1(&template, data); | |
1632 | } | |
1633 | EXPORT_SYMBOL_GPL(snd_soc_cnew); | |
1634 | ||
3e8e1952 IM |
1635 | /** |
1636 | * snd_soc_add_controls - add an array of controls to a codec. | |
1637 | * Convienience function to add a list of controls. Many codecs were | |
1638 | * duplicating this code. | |
1639 | * | |
1640 | * @codec: codec to add controls to | |
1641 | * @controls: array of controls to add | |
1642 | * @num_controls: number of elements in the array | |
1643 | * | |
1644 | * Return 0 for success, else error. | |
1645 | */ | |
1646 | int snd_soc_add_controls(struct snd_soc_codec *codec, | |
1647 | const struct snd_kcontrol_new *controls, int num_controls) | |
1648 | { | |
1649 | struct snd_card *card = codec->card; | |
1650 | int err, i; | |
1651 | ||
1652 | for (i = 0; i < num_controls; i++) { | |
1653 | const struct snd_kcontrol_new *control = &controls[i]; | |
1654 | err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL)); | |
1655 | if (err < 0) { | |
1656 | dev_err(codec->dev, "%s: Failed to add %s\n", | |
1657 | codec->name, control->name); | |
1658 | return err; | |
1659 | } | |
1660 | } | |
1661 | ||
1662 | return 0; | |
1663 | } | |
1664 | EXPORT_SYMBOL_GPL(snd_soc_add_controls); | |
1665 | ||
db2a4165 FM |
1666 | /** |
1667 | * snd_soc_info_enum_double - enumerated double mixer info callback | |
1668 | * @kcontrol: mixer control | |
1669 | * @uinfo: control element information | |
1670 | * | |
1671 | * Callback to provide information about a double enumerated | |
1672 | * mixer control. | |
1673 | * | |
1674 | * Returns 0 for success. | |
1675 | */ | |
1676 | int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, | |
1677 | struct snd_ctl_elem_info *uinfo) | |
1678 | { | |
1679 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; | |
1680 | ||
1681 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
1682 | uinfo->count = e->shift_l == e->shift_r ? 1 : 2; | |
f8ba0b7b | 1683 | uinfo->value.enumerated.items = e->max; |
db2a4165 | 1684 | |
f8ba0b7b JS |
1685 | if (uinfo->value.enumerated.item > e->max - 1) |
1686 | uinfo->value.enumerated.item = e->max - 1; | |
db2a4165 FM |
1687 | strcpy(uinfo->value.enumerated.name, |
1688 | e->texts[uinfo->value.enumerated.item]); | |
1689 | return 0; | |
1690 | } | |
1691 | EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); | |
1692 | ||
1693 | /** | |
1694 | * snd_soc_get_enum_double - enumerated double mixer get callback | |
1695 | * @kcontrol: mixer control | |
ac11a2b3 | 1696 | * @ucontrol: control element information |
db2a4165 FM |
1697 | * |
1698 | * Callback to get the value of a double enumerated mixer. | |
1699 | * | |
1700 | * Returns 0 for success. | |
1701 | */ | |
1702 | int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, | |
1703 | struct snd_ctl_elem_value *ucontrol) | |
1704 | { | |
1705 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
1706 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; | |
46f5822f | 1707 | unsigned int val, bitmask; |
db2a4165 | 1708 | |
f8ba0b7b | 1709 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
db2a4165 FM |
1710 | ; |
1711 | val = snd_soc_read(codec, e->reg); | |
3ff3f64b MB |
1712 | ucontrol->value.enumerated.item[0] |
1713 | = (val >> e->shift_l) & (bitmask - 1); | |
db2a4165 FM |
1714 | if (e->shift_l != e->shift_r) |
1715 | ucontrol->value.enumerated.item[1] = | |
1716 | (val >> e->shift_r) & (bitmask - 1); | |
1717 | ||
1718 | return 0; | |
1719 | } | |
1720 | EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); | |
1721 | ||
1722 | /** | |
1723 | * snd_soc_put_enum_double - enumerated double mixer put callback | |
1724 | * @kcontrol: mixer control | |
ac11a2b3 | 1725 | * @ucontrol: control element information |
db2a4165 FM |
1726 | * |
1727 | * Callback to set the value of a double enumerated mixer. | |
1728 | * | |
1729 | * Returns 0 for success. | |
1730 | */ | |
1731 | int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, | |
1732 | struct snd_ctl_elem_value *ucontrol) | |
1733 | { | |
1734 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
1735 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; | |
46f5822f DR |
1736 | unsigned int val; |
1737 | unsigned int mask, bitmask; | |
db2a4165 | 1738 | |
f8ba0b7b | 1739 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
db2a4165 | 1740 | ; |
f8ba0b7b | 1741 | if (ucontrol->value.enumerated.item[0] > e->max - 1) |
db2a4165 FM |
1742 | return -EINVAL; |
1743 | val = ucontrol->value.enumerated.item[0] << e->shift_l; | |
1744 | mask = (bitmask - 1) << e->shift_l; | |
1745 | if (e->shift_l != e->shift_r) { | |
f8ba0b7b | 1746 | if (ucontrol->value.enumerated.item[1] > e->max - 1) |
db2a4165 FM |
1747 | return -EINVAL; |
1748 | val |= ucontrol->value.enumerated.item[1] << e->shift_r; | |
1749 | mask |= (bitmask - 1) << e->shift_r; | |
1750 | } | |
1751 | ||
6c508c62 | 1752 | return snd_soc_update_bits_locked(codec, e->reg, mask, val); |
db2a4165 FM |
1753 | } |
1754 | EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); | |
1755 | ||
2e72f8e3 PU |
1756 | /** |
1757 | * snd_soc_get_value_enum_double - semi enumerated double mixer get callback | |
1758 | * @kcontrol: mixer control | |
1759 | * @ucontrol: control element information | |
1760 | * | |
1761 | * Callback to get the value of a double semi enumerated mixer. | |
1762 | * | |
1763 | * Semi enumerated mixer: the enumerated items are referred as values. Can be | |
1764 | * used for handling bitfield coded enumeration for example. | |
1765 | * | |
1766 | * Returns 0 for success. | |
1767 | */ | |
1768 | int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol, | |
1769 | struct snd_ctl_elem_value *ucontrol) | |
1770 | { | |
1771 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
74155556 | 1772 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
46f5822f | 1773 | unsigned int reg_val, val, mux; |
2e72f8e3 PU |
1774 | |
1775 | reg_val = snd_soc_read(codec, e->reg); | |
1776 | val = (reg_val >> e->shift_l) & e->mask; | |
1777 | for (mux = 0; mux < e->max; mux++) { | |
1778 | if (val == e->values[mux]) | |
1779 | break; | |
1780 | } | |
1781 | ucontrol->value.enumerated.item[0] = mux; | |
1782 | if (e->shift_l != e->shift_r) { | |
1783 | val = (reg_val >> e->shift_r) & e->mask; | |
1784 | for (mux = 0; mux < e->max; mux++) { | |
1785 | if (val == e->values[mux]) | |
1786 | break; | |
1787 | } | |
1788 | ucontrol->value.enumerated.item[1] = mux; | |
1789 | } | |
1790 | ||
1791 | return 0; | |
1792 | } | |
1793 | EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double); | |
1794 | ||
1795 | /** | |
1796 | * snd_soc_put_value_enum_double - semi enumerated double mixer put callback | |
1797 | * @kcontrol: mixer control | |
1798 | * @ucontrol: control element information | |
1799 | * | |
1800 | * Callback to set the value of a double semi enumerated mixer. | |
1801 | * | |
1802 | * Semi enumerated mixer: the enumerated items are referred as values. Can be | |
1803 | * used for handling bitfield coded enumeration for example. | |
1804 | * | |
1805 | * Returns 0 for success. | |
1806 | */ | |
1807 | int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol, | |
1808 | struct snd_ctl_elem_value *ucontrol) | |
1809 | { | |
1810 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
74155556 | 1811 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
46f5822f DR |
1812 | unsigned int val; |
1813 | unsigned int mask; | |
2e72f8e3 PU |
1814 | |
1815 | if (ucontrol->value.enumerated.item[0] > e->max - 1) | |
1816 | return -EINVAL; | |
1817 | val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l; | |
1818 | mask = e->mask << e->shift_l; | |
1819 | if (e->shift_l != e->shift_r) { | |
1820 | if (ucontrol->value.enumerated.item[1] > e->max - 1) | |
1821 | return -EINVAL; | |
1822 | val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r; | |
1823 | mask |= e->mask << e->shift_r; | |
1824 | } | |
1825 | ||
6c508c62 | 1826 | return snd_soc_update_bits_locked(codec, e->reg, mask, val); |
2e72f8e3 PU |
1827 | } |
1828 | EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double); | |
1829 | ||
db2a4165 FM |
1830 | /** |
1831 | * snd_soc_info_enum_ext - external enumerated single mixer info callback | |
1832 | * @kcontrol: mixer control | |
1833 | * @uinfo: control element information | |
1834 | * | |
1835 | * Callback to provide information about an external enumerated | |
1836 | * single mixer. | |
1837 | * | |
1838 | * Returns 0 for success. | |
1839 | */ | |
1840 | int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol, | |
1841 | struct snd_ctl_elem_info *uinfo) | |
1842 | { | |
1843 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; | |
1844 | ||
1845 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
1846 | uinfo->count = 1; | |
f8ba0b7b | 1847 | uinfo->value.enumerated.items = e->max; |
db2a4165 | 1848 | |
f8ba0b7b JS |
1849 | if (uinfo->value.enumerated.item > e->max - 1) |
1850 | uinfo->value.enumerated.item = e->max - 1; | |
db2a4165 FM |
1851 | strcpy(uinfo->value.enumerated.name, |
1852 | e->texts[uinfo->value.enumerated.item]); | |
1853 | return 0; | |
1854 | } | |
1855 | EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext); | |
1856 | ||
1857 | /** | |
1858 | * snd_soc_info_volsw_ext - external single mixer info callback | |
1859 | * @kcontrol: mixer control | |
1860 | * @uinfo: control element information | |
1861 | * | |
1862 | * Callback to provide information about a single external mixer control. | |
1863 | * | |
1864 | * Returns 0 for success. | |
1865 | */ | |
1866 | int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol, | |
1867 | struct snd_ctl_elem_info *uinfo) | |
1868 | { | |
a7a4ac86 PZ |
1869 | int max = kcontrol->private_value; |
1870 | ||
fd5dfad9 | 1871 | if (max == 1 && !strstr(kcontrol->id.name, " Volume")) |
a7a4ac86 PZ |
1872 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
1873 | else | |
1874 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
db2a4165 | 1875 | |
db2a4165 FM |
1876 | uinfo->count = 1; |
1877 | uinfo->value.integer.min = 0; | |
a7a4ac86 | 1878 | uinfo->value.integer.max = max; |
db2a4165 FM |
1879 | return 0; |
1880 | } | |
1881 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext); | |
1882 | ||
db2a4165 FM |
1883 | /** |
1884 | * snd_soc_info_volsw - single mixer info callback | |
1885 | * @kcontrol: mixer control | |
1886 | * @uinfo: control element information | |
1887 | * | |
1888 | * Callback to provide information about a single mixer control. | |
1889 | * | |
1890 | * Returns 0 for success. | |
1891 | */ | |
1892 | int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, | |
1893 | struct snd_ctl_elem_info *uinfo) | |
1894 | { | |
4eaa9819 JS |
1895 | struct soc_mixer_control *mc = |
1896 | (struct soc_mixer_control *)kcontrol->private_value; | |
1897 | int max = mc->max; | |
762b8df7 | 1898 | unsigned int shift = mc->shift; |
815ecf8d | 1899 | unsigned int rshift = mc->rshift; |
db2a4165 | 1900 | |
fd5dfad9 | 1901 | if (max == 1 && !strstr(kcontrol->id.name, " Volume")) |
a7a4ac86 PZ |
1902 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
1903 | else | |
1904 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
1905 | ||
db2a4165 FM |
1906 | uinfo->count = shift == rshift ? 1 : 2; |
1907 | uinfo->value.integer.min = 0; | |
a7a4ac86 | 1908 | uinfo->value.integer.max = max; |
db2a4165 FM |
1909 | return 0; |
1910 | } | |
1911 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw); | |
1912 | ||
1913 | /** | |
1914 | * snd_soc_get_volsw - single mixer get callback | |
1915 | * @kcontrol: mixer control | |
ac11a2b3 | 1916 | * @ucontrol: control element information |
db2a4165 FM |
1917 | * |
1918 | * Callback to get the value of a single mixer control. | |
1919 | * | |
1920 | * Returns 0 for success. | |
1921 | */ | |
1922 | int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, | |
1923 | struct snd_ctl_elem_value *ucontrol) | |
1924 | { | |
4eaa9819 JS |
1925 | struct soc_mixer_control *mc = |
1926 | (struct soc_mixer_control *)kcontrol->private_value; | |
db2a4165 | 1927 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d JS |
1928 | unsigned int reg = mc->reg; |
1929 | unsigned int shift = mc->shift; | |
1930 | unsigned int rshift = mc->rshift; | |
4eaa9819 | 1931 | int max = mc->max; |
815ecf8d JS |
1932 | unsigned int mask = (1 << fls(max)) - 1; |
1933 | unsigned int invert = mc->invert; | |
db2a4165 FM |
1934 | |
1935 | ucontrol->value.integer.value[0] = | |
1936 | (snd_soc_read(codec, reg) >> shift) & mask; | |
1937 | if (shift != rshift) | |
1938 | ucontrol->value.integer.value[1] = | |
1939 | (snd_soc_read(codec, reg) >> rshift) & mask; | |
1940 | if (invert) { | |
1941 | ucontrol->value.integer.value[0] = | |
a7a4ac86 | 1942 | max - ucontrol->value.integer.value[0]; |
db2a4165 FM |
1943 | if (shift != rshift) |
1944 | ucontrol->value.integer.value[1] = | |
a7a4ac86 | 1945 | max - ucontrol->value.integer.value[1]; |
db2a4165 FM |
1946 | } |
1947 | ||
1948 | return 0; | |
1949 | } | |
1950 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw); | |
1951 | ||
1952 | /** | |
1953 | * snd_soc_put_volsw - single mixer put callback | |
1954 | * @kcontrol: mixer control | |
ac11a2b3 | 1955 | * @ucontrol: control element information |
db2a4165 FM |
1956 | * |
1957 | * Callback to set the value of a single mixer control. | |
1958 | * | |
1959 | * Returns 0 for success. | |
1960 | */ | |
1961 | int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, | |
1962 | struct snd_ctl_elem_value *ucontrol) | |
1963 | { | |
4eaa9819 JS |
1964 | struct soc_mixer_control *mc = |
1965 | (struct soc_mixer_control *)kcontrol->private_value; | |
db2a4165 | 1966 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d JS |
1967 | unsigned int reg = mc->reg; |
1968 | unsigned int shift = mc->shift; | |
1969 | unsigned int rshift = mc->rshift; | |
4eaa9819 | 1970 | int max = mc->max; |
815ecf8d JS |
1971 | unsigned int mask = (1 << fls(max)) - 1; |
1972 | unsigned int invert = mc->invert; | |
46f5822f | 1973 | unsigned int val, val2, val_mask; |
db2a4165 FM |
1974 | |
1975 | val = (ucontrol->value.integer.value[0] & mask); | |
1976 | if (invert) | |
a7a4ac86 | 1977 | val = max - val; |
db2a4165 FM |
1978 | val_mask = mask << shift; |
1979 | val = val << shift; | |
1980 | if (shift != rshift) { | |
1981 | val2 = (ucontrol->value.integer.value[1] & mask); | |
1982 | if (invert) | |
a7a4ac86 | 1983 | val2 = max - val2; |
db2a4165 FM |
1984 | val_mask |= mask << rshift; |
1985 | val |= val2 << rshift; | |
1986 | } | |
6c508c62 | 1987 | return snd_soc_update_bits_locked(codec, reg, val_mask, val); |
db2a4165 FM |
1988 | } |
1989 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw); | |
1990 | ||
1991 | /** | |
1992 | * snd_soc_info_volsw_2r - double mixer info callback | |
1993 | * @kcontrol: mixer control | |
1994 | * @uinfo: control element information | |
1995 | * | |
1996 | * Callback to provide information about a double mixer control that | |
1997 | * spans 2 codec registers. | |
1998 | * | |
1999 | * Returns 0 for success. | |
2000 | */ | |
2001 | int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol, | |
2002 | struct snd_ctl_elem_info *uinfo) | |
2003 | { | |
4eaa9819 JS |
2004 | struct soc_mixer_control *mc = |
2005 | (struct soc_mixer_control *)kcontrol->private_value; | |
2006 | int max = mc->max; | |
a7a4ac86 | 2007 | |
fd5dfad9 | 2008 | if (max == 1 && !strstr(kcontrol->id.name, " Volume")) |
a7a4ac86 PZ |
2009 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
2010 | else | |
2011 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
db2a4165 | 2012 | |
db2a4165 FM |
2013 | uinfo->count = 2; |
2014 | uinfo->value.integer.min = 0; | |
a7a4ac86 | 2015 | uinfo->value.integer.max = max; |
db2a4165 FM |
2016 | return 0; |
2017 | } | |
2018 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r); | |
2019 | ||
2020 | /** | |
2021 | * snd_soc_get_volsw_2r - double mixer get callback | |
2022 | * @kcontrol: mixer control | |
ac11a2b3 | 2023 | * @ucontrol: control element information |
db2a4165 FM |
2024 | * |
2025 | * Callback to get the value of a double mixer control that spans 2 registers. | |
2026 | * | |
2027 | * Returns 0 for success. | |
2028 | */ | |
2029 | int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol, | |
2030 | struct snd_ctl_elem_value *ucontrol) | |
2031 | { | |
4eaa9819 JS |
2032 | struct soc_mixer_control *mc = |
2033 | (struct soc_mixer_control *)kcontrol->private_value; | |
db2a4165 | 2034 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d JS |
2035 | unsigned int reg = mc->reg; |
2036 | unsigned int reg2 = mc->rreg; | |
2037 | unsigned int shift = mc->shift; | |
4eaa9819 | 2038 | int max = mc->max; |
46f5822f | 2039 | unsigned int mask = (1 << fls(max)) - 1; |
815ecf8d | 2040 | unsigned int invert = mc->invert; |
db2a4165 FM |
2041 | |
2042 | ucontrol->value.integer.value[0] = | |
2043 | (snd_soc_read(codec, reg) >> shift) & mask; | |
2044 | ucontrol->value.integer.value[1] = | |
2045 | (snd_soc_read(codec, reg2) >> shift) & mask; | |
2046 | if (invert) { | |
2047 | ucontrol->value.integer.value[0] = | |
a7a4ac86 | 2048 | max - ucontrol->value.integer.value[0]; |
db2a4165 | 2049 | ucontrol->value.integer.value[1] = |
a7a4ac86 | 2050 | max - ucontrol->value.integer.value[1]; |
db2a4165 FM |
2051 | } |
2052 | ||
2053 | return 0; | |
2054 | } | |
2055 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r); | |
2056 | ||
2057 | /** | |
2058 | * snd_soc_put_volsw_2r - double mixer set callback | |
2059 | * @kcontrol: mixer control | |
ac11a2b3 | 2060 | * @ucontrol: control element information |
db2a4165 FM |
2061 | * |
2062 | * Callback to set the value of a double mixer control that spans 2 registers. | |
2063 | * | |
2064 | * Returns 0 for success. | |
2065 | */ | |
2066 | int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol, | |
2067 | struct snd_ctl_elem_value *ucontrol) | |
2068 | { | |
4eaa9819 JS |
2069 | struct soc_mixer_control *mc = |
2070 | (struct soc_mixer_control *)kcontrol->private_value; | |
db2a4165 | 2071 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d JS |
2072 | unsigned int reg = mc->reg; |
2073 | unsigned int reg2 = mc->rreg; | |
2074 | unsigned int shift = mc->shift; | |
4eaa9819 | 2075 | int max = mc->max; |
815ecf8d JS |
2076 | unsigned int mask = (1 << fls(max)) - 1; |
2077 | unsigned int invert = mc->invert; | |
db2a4165 | 2078 | int err; |
46f5822f | 2079 | unsigned int val, val2, val_mask; |
db2a4165 FM |
2080 | |
2081 | val_mask = mask << shift; | |
2082 | val = (ucontrol->value.integer.value[0] & mask); | |
2083 | val2 = (ucontrol->value.integer.value[1] & mask); | |
2084 | ||
2085 | if (invert) { | |
a7a4ac86 PZ |
2086 | val = max - val; |
2087 | val2 = max - val2; | |
db2a4165 FM |
2088 | } |
2089 | ||
2090 | val = val << shift; | |
2091 | val2 = val2 << shift; | |
2092 | ||
6c508c62 | 2093 | err = snd_soc_update_bits_locked(codec, reg, val_mask, val); |
3ff3f64b | 2094 | if (err < 0) |
db2a4165 FM |
2095 | return err; |
2096 | ||
6c508c62 | 2097 | err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2); |
db2a4165 FM |
2098 | return err; |
2099 | } | |
2100 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r); | |
2101 | ||
e13ac2e9 MB |
2102 | /** |
2103 | * snd_soc_info_volsw_s8 - signed mixer info callback | |
2104 | * @kcontrol: mixer control | |
2105 | * @uinfo: control element information | |
2106 | * | |
2107 | * Callback to provide information about a signed mixer control. | |
2108 | * | |
2109 | * Returns 0 for success. | |
2110 | */ | |
2111 | int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol, | |
2112 | struct snd_ctl_elem_info *uinfo) | |
2113 | { | |
4eaa9819 JS |
2114 | struct soc_mixer_control *mc = |
2115 | (struct soc_mixer_control *)kcontrol->private_value; | |
2116 | int max = mc->max; | |
2117 | int min = mc->min; | |
e13ac2e9 MB |
2118 | |
2119 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
2120 | uinfo->count = 2; | |
2121 | uinfo->value.integer.min = 0; | |
2122 | uinfo->value.integer.max = max-min; | |
2123 | return 0; | |
2124 | } | |
2125 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8); | |
2126 | ||
2127 | /** | |
2128 | * snd_soc_get_volsw_s8 - signed mixer get callback | |
2129 | * @kcontrol: mixer control | |
ac11a2b3 | 2130 | * @ucontrol: control element information |
e13ac2e9 MB |
2131 | * |
2132 | * Callback to get the value of a signed mixer control. | |
2133 | * | |
2134 | * Returns 0 for success. | |
2135 | */ | |
2136 | int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol, | |
2137 | struct snd_ctl_elem_value *ucontrol) | |
2138 | { | |
4eaa9819 JS |
2139 | struct soc_mixer_control *mc = |
2140 | (struct soc_mixer_control *)kcontrol->private_value; | |
e13ac2e9 | 2141 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d | 2142 | unsigned int reg = mc->reg; |
4eaa9819 | 2143 | int min = mc->min; |
e13ac2e9 MB |
2144 | int val = snd_soc_read(codec, reg); |
2145 | ||
2146 | ucontrol->value.integer.value[0] = | |
2147 | ((signed char)(val & 0xff))-min; | |
2148 | ucontrol->value.integer.value[1] = | |
2149 | ((signed char)((val >> 8) & 0xff))-min; | |
2150 | return 0; | |
2151 | } | |
2152 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8); | |
2153 | ||
2154 | /** | |
2155 | * snd_soc_put_volsw_sgn - signed mixer put callback | |
2156 | * @kcontrol: mixer control | |
ac11a2b3 | 2157 | * @ucontrol: control element information |
e13ac2e9 MB |
2158 | * |
2159 | * Callback to set the value of a signed mixer control. | |
2160 | * | |
2161 | * Returns 0 for success. | |
2162 | */ | |
2163 | int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol, | |
2164 | struct snd_ctl_elem_value *ucontrol) | |
2165 | { | |
4eaa9819 JS |
2166 | struct soc_mixer_control *mc = |
2167 | (struct soc_mixer_control *)kcontrol->private_value; | |
e13ac2e9 | 2168 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d | 2169 | unsigned int reg = mc->reg; |
4eaa9819 | 2170 | int min = mc->min; |
46f5822f | 2171 | unsigned int val; |
e13ac2e9 MB |
2172 | |
2173 | val = (ucontrol->value.integer.value[0]+min) & 0xff; | |
2174 | val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8; | |
2175 | ||
6c508c62 | 2176 | return snd_soc_update_bits_locked(codec, reg, 0xffff, val); |
e13ac2e9 MB |
2177 | } |
2178 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8); | |
2179 | ||
8c6529db LG |
2180 | /** |
2181 | * snd_soc_dai_set_sysclk - configure DAI system or master clock. | |
2182 | * @dai: DAI | |
2183 | * @clk_id: DAI specific clock ID | |
2184 | * @freq: new clock frequency in Hz | |
2185 | * @dir: new clock direction - input/output. | |
2186 | * | |
2187 | * Configures the DAI master (MCLK) or system (SYSCLK) clocking. | |
2188 | */ | |
2189 | int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, | |
2190 | unsigned int freq, int dir) | |
2191 | { | |
3f1a4d82 | 2192 | if (dai->ops && dai->ops->set_sysclk) |
6335d055 | 2193 | return dai->ops->set_sysclk(dai, clk_id, freq, dir); |
8c6529db LG |
2194 | else |
2195 | return -EINVAL; | |
2196 | } | |
2197 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); | |
2198 | ||
2199 | /** | |
2200 | * snd_soc_dai_set_clkdiv - configure DAI clock dividers. | |
2201 | * @dai: DAI | |
ac11a2b3 | 2202 | * @div_id: DAI specific clock divider ID |
8c6529db LG |
2203 | * @div: new clock divisor. |
2204 | * | |
2205 | * Configures the clock dividers. This is used to derive the best DAI bit and | |
2206 | * frame clocks from the system or master clock. It's best to set the DAI bit | |
2207 | * and frame clocks as low as possible to save system power. | |
2208 | */ | |
2209 | int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, | |
2210 | int div_id, int div) | |
2211 | { | |
3f1a4d82 | 2212 | if (dai->ops && dai->ops->set_clkdiv) |
6335d055 | 2213 | return dai->ops->set_clkdiv(dai, div_id, div); |
8c6529db LG |
2214 | else |
2215 | return -EINVAL; | |
2216 | } | |
2217 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); | |
2218 | ||
2219 | /** | |
2220 | * snd_soc_dai_set_pll - configure DAI PLL. | |
2221 | * @dai: DAI | |
2222 | * @pll_id: DAI specific PLL ID | |
85488037 | 2223 | * @source: DAI specific source for the PLL |
8c6529db LG |
2224 | * @freq_in: PLL input clock frequency in Hz |
2225 | * @freq_out: requested PLL output clock frequency in Hz | |
2226 | * | |
2227 | * Configures and enables PLL to generate output clock based on input clock. | |
2228 | */ | |
85488037 MB |
2229 | int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, |
2230 | unsigned int freq_in, unsigned int freq_out) | |
8c6529db | 2231 | { |
3f1a4d82 | 2232 | if (dai->ops && dai->ops->set_pll) |
85488037 MB |
2233 | return dai->ops->set_pll(dai, pll_id, source, |
2234 | freq_in, freq_out); | |
8c6529db LG |
2235 | else |
2236 | return -EINVAL; | |
2237 | } | |
2238 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); | |
2239 | ||
2240 | /** | |
2241 | * snd_soc_dai_set_fmt - configure DAI hardware audio format. | |
2242 | * @dai: DAI | |
8c6529db LG |
2243 | * @fmt: SND_SOC_DAIFMT_ format value. |
2244 | * | |
2245 | * Configures the DAI hardware format and clocking. | |
2246 | */ | |
2247 | int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) | |
2248 | { | |
3f1a4d82 | 2249 | if (dai->ops && dai->ops->set_fmt) |
6335d055 | 2250 | return dai->ops->set_fmt(dai, fmt); |
8c6529db LG |
2251 | else |
2252 | return -EINVAL; | |
2253 | } | |
2254 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); | |
2255 | ||
2256 | /** | |
2257 | * snd_soc_dai_set_tdm_slot - configure DAI TDM. | |
2258 | * @dai: DAI | |
a5479e38 DR |
2259 | * @tx_mask: bitmask representing active TX slots. |
2260 | * @rx_mask: bitmask representing active RX slots. | |
8c6529db | 2261 | * @slots: Number of slots in use. |
a5479e38 | 2262 | * @slot_width: Width in bits for each slot. |
8c6529db LG |
2263 | * |
2264 | * Configures a DAI for TDM operation. Both mask and slots are codec and DAI | |
2265 | * specific. | |
2266 | */ | |
2267 | int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, | |
a5479e38 | 2268 | unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) |
8c6529db | 2269 | { |
3f1a4d82 | 2270 | if (dai->ops && dai->ops->set_tdm_slot) |
a5479e38 DR |
2271 | return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask, |
2272 | slots, slot_width); | |
8c6529db LG |
2273 | else |
2274 | return -EINVAL; | |
2275 | } | |
2276 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); | |
2277 | ||
472df3cb BS |
2278 | /** |
2279 | * snd_soc_dai_set_channel_map - configure DAI audio channel map | |
2280 | * @dai: DAI | |
2281 | * @tx_num: how many TX channels | |
2282 | * @tx_slot: pointer to an array which imply the TX slot number channel | |
2283 | * 0~num-1 uses | |
2284 | * @rx_num: how many RX channels | |
2285 | * @rx_slot: pointer to an array which imply the RX slot number channel | |
2286 | * 0~num-1 uses | |
2287 | * | |
2288 | * configure the relationship between channel number and TDM slot number. | |
2289 | */ | |
2290 | int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, | |
2291 | unsigned int tx_num, unsigned int *tx_slot, | |
2292 | unsigned int rx_num, unsigned int *rx_slot) | |
2293 | { | |
2294 | if (dai->ops && dai->ops->set_channel_map) | |
2295 | return dai->ops->set_channel_map(dai, tx_num, tx_slot, | |
2296 | rx_num, rx_slot); | |
2297 | else | |
2298 | return -EINVAL; | |
2299 | } | |
2300 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); | |
2301 | ||
8c6529db LG |
2302 | /** |
2303 | * snd_soc_dai_set_tristate - configure DAI system or master clock. | |
2304 | * @dai: DAI | |
2305 | * @tristate: tristate enable | |
2306 | * | |
2307 | * Tristates the DAI so that others can use it. | |
2308 | */ | |
2309 | int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) | |
2310 | { | |
3f1a4d82 | 2311 | if (dai->ops && dai->ops->set_tristate) |
6335d055 | 2312 | return dai->ops->set_tristate(dai, tristate); |
8c6529db LG |
2313 | else |
2314 | return -EINVAL; | |
2315 | } | |
2316 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); | |
2317 | ||
2318 | /** | |
2319 | * snd_soc_dai_digital_mute - configure DAI system or master clock. | |
2320 | * @dai: DAI | |
2321 | * @mute: mute enable | |
2322 | * | |
2323 | * Mutes the DAI DAC. | |
2324 | */ | |
2325 | int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute) | |
2326 | { | |
3f1a4d82 | 2327 | if (dai->ops && dai->ops->digital_mute) |
6335d055 | 2328 | return dai->ops->digital_mute(dai, mute); |
8c6529db LG |
2329 | else |
2330 | return -EINVAL; | |
2331 | } | |
2332 | EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); | |
2333 | ||
c5af3a2e MB |
2334 | /** |
2335 | * snd_soc_register_card - Register a card with the ASoC core | |
2336 | * | |
ac11a2b3 | 2337 | * @card: Card to register |
c5af3a2e MB |
2338 | * |
2339 | * Note that currently this is an internal only function: it will be | |
2340 | * exposed to machine drivers after further backporting of ASoC v2 | |
2341 | * registration APIs. | |
2342 | */ | |
2343 | static int snd_soc_register_card(struct snd_soc_card *card) | |
2344 | { | |
2345 | if (!card->name || !card->dev) | |
2346 | return -EINVAL; | |
2347 | ||
2348 | INIT_LIST_HEAD(&card->list); | |
2349 | card->instantiated = 0; | |
2350 | ||
2351 | mutex_lock(&client_mutex); | |
2352 | list_add(&card->list, &card_list); | |
435c5e25 | 2353 | snd_soc_instantiate_cards(); |
c5af3a2e MB |
2354 | mutex_unlock(&client_mutex); |
2355 | ||
2356 | dev_dbg(card->dev, "Registered card '%s'\n", card->name); | |
2357 | ||
2358 | return 0; | |
2359 | } | |
2360 | ||
2361 | /** | |
2362 | * snd_soc_unregister_card - Unregister a card with the ASoC core | |
2363 | * | |
ac11a2b3 | 2364 | * @card: Card to unregister |
c5af3a2e MB |
2365 | * |
2366 | * Note that currently this is an internal only function: it will be | |
2367 | * exposed to machine drivers after further backporting of ASoC v2 | |
2368 | * registration APIs. | |
2369 | */ | |
2370 | static int snd_soc_unregister_card(struct snd_soc_card *card) | |
2371 | { | |
2372 | mutex_lock(&client_mutex); | |
2373 | list_del(&card->list); | |
2374 | mutex_unlock(&client_mutex); | |
2375 | ||
2376 | dev_dbg(card->dev, "Unregistered card '%s'\n", card->name); | |
2377 | ||
2378 | return 0; | |
2379 | } | |
2380 | ||
9115171a MB |
2381 | /** |
2382 | * snd_soc_register_dai - Register a DAI with the ASoC core | |
2383 | * | |
ac11a2b3 | 2384 | * @dai: DAI to register |
9115171a MB |
2385 | */ |
2386 | int snd_soc_register_dai(struct snd_soc_dai *dai) | |
2387 | { | |
2388 | if (!dai->name) | |
2389 | return -EINVAL; | |
2390 | ||
2391 | /* The device should become mandatory over time */ | |
2392 | if (!dai->dev) | |
2393 | printk(KERN_WARNING "No device for DAI %s\n", dai->name); | |
2394 | ||
6335d055 EM |
2395 | if (!dai->ops) |
2396 | dai->ops = &null_dai_ops; | |
2397 | ||
9115171a MB |
2398 | INIT_LIST_HEAD(&dai->list); |
2399 | ||
2400 | mutex_lock(&client_mutex); | |
2401 | list_add(&dai->list, &dai_list); | |
435c5e25 | 2402 | snd_soc_instantiate_cards(); |
9115171a MB |
2403 | mutex_unlock(&client_mutex); |
2404 | ||
2405 | pr_debug("Registered DAI '%s'\n", dai->name); | |
2406 | ||
2407 | return 0; | |
2408 | } | |
2409 | EXPORT_SYMBOL_GPL(snd_soc_register_dai); | |
2410 | ||
2411 | /** | |
2412 | * snd_soc_unregister_dai - Unregister a DAI from the ASoC core | |
2413 | * | |
ac11a2b3 | 2414 | * @dai: DAI to unregister |
9115171a MB |
2415 | */ |
2416 | void snd_soc_unregister_dai(struct snd_soc_dai *dai) | |
2417 | { | |
2418 | mutex_lock(&client_mutex); | |
2419 | list_del(&dai->list); | |
2420 | mutex_unlock(&client_mutex); | |
2421 | ||
2422 | pr_debug("Unregistered DAI '%s'\n", dai->name); | |
2423 | } | |
2424 | EXPORT_SYMBOL_GPL(snd_soc_unregister_dai); | |
2425 | ||
2426 | /** | |
2427 | * snd_soc_register_dais - Register multiple DAIs with the ASoC core | |
2428 | * | |
ac11a2b3 MB |
2429 | * @dai: Array of DAIs to register |
2430 | * @count: Number of DAIs | |
9115171a MB |
2431 | */ |
2432 | int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count) | |
2433 | { | |
2434 | int i, ret; | |
2435 | ||
2436 | for (i = 0; i < count; i++) { | |
2437 | ret = snd_soc_register_dai(&dai[i]); | |
2438 | if (ret != 0) | |
2439 | goto err; | |
2440 | } | |
2441 | ||
2442 | return 0; | |
2443 | ||
2444 | err: | |
2445 | for (i--; i >= 0; i--) | |
2446 | snd_soc_unregister_dai(&dai[i]); | |
2447 | ||
2448 | return ret; | |
2449 | } | |
2450 | EXPORT_SYMBOL_GPL(snd_soc_register_dais); | |
2451 | ||
2452 | /** | |
2453 | * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core | |
2454 | * | |
ac11a2b3 MB |
2455 | * @dai: Array of DAIs to unregister |
2456 | * @count: Number of DAIs | |
9115171a MB |
2457 | */ |
2458 | void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count) | |
2459 | { | |
2460 | int i; | |
2461 | ||
2462 | for (i = 0; i < count; i++) | |
2463 | snd_soc_unregister_dai(&dai[i]); | |
2464 | } | |
2465 | EXPORT_SYMBOL_GPL(snd_soc_unregister_dais); | |
2466 | ||
12a48a8c MB |
2467 | /** |
2468 | * snd_soc_register_platform - Register a platform with the ASoC core | |
2469 | * | |
ac11a2b3 | 2470 | * @platform: platform to register |
12a48a8c MB |
2471 | */ |
2472 | int snd_soc_register_platform(struct snd_soc_platform *platform) | |
2473 | { | |
2474 | if (!platform->name) | |
2475 | return -EINVAL; | |
2476 | ||
2477 | INIT_LIST_HEAD(&platform->list); | |
2478 | ||
2479 | mutex_lock(&client_mutex); | |
2480 | list_add(&platform->list, &platform_list); | |
435c5e25 | 2481 | snd_soc_instantiate_cards(); |
12a48a8c MB |
2482 | mutex_unlock(&client_mutex); |
2483 | ||
2484 | pr_debug("Registered platform '%s'\n", platform->name); | |
2485 | ||
2486 | return 0; | |
2487 | } | |
2488 | EXPORT_SYMBOL_GPL(snd_soc_register_platform); | |
2489 | ||
2490 | /** | |
2491 | * snd_soc_unregister_platform - Unregister a platform from the ASoC core | |
2492 | * | |
ac11a2b3 | 2493 | * @platform: platform to unregister |
12a48a8c MB |
2494 | */ |
2495 | void snd_soc_unregister_platform(struct snd_soc_platform *platform) | |
2496 | { | |
2497 | mutex_lock(&client_mutex); | |
2498 | list_del(&platform->list); | |
2499 | mutex_unlock(&client_mutex); | |
2500 | ||
2501 | pr_debug("Unregistered platform '%s'\n", platform->name); | |
2502 | } | |
2503 | EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); | |
2504 | ||
151ab22c MB |
2505 | static u64 codec_format_map[] = { |
2506 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, | |
2507 | SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, | |
2508 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, | |
2509 | SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, | |
2510 | SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, | |
2511 | SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, | |
2512 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, | |
2513 | SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, | |
2514 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, | |
2515 | SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, | |
2516 | SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, | |
2517 | SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, | |
2518 | SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, | |
2519 | SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, | |
2520 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE | |
2521 | | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, | |
2522 | }; | |
2523 | ||
2524 | /* Fix up the DAI formats for endianness: codecs don't actually see | |
2525 | * the endianness of the data but we're using the CPU format | |
2526 | * definitions which do need to include endianness so we ensure that | |
2527 | * codec DAIs always have both big and little endian variants set. | |
2528 | */ | |
2529 | static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) | |
2530 | { | |
2531 | int i; | |
2532 | ||
2533 | for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) | |
2534 | if (stream->formats & codec_format_map[i]) | |
2535 | stream->formats |= codec_format_map[i]; | |
2536 | } | |
2537 | ||
0d0cf00a MB |
2538 | /** |
2539 | * snd_soc_register_codec - Register a codec with the ASoC core | |
2540 | * | |
ac11a2b3 | 2541 | * @codec: codec to register |
0d0cf00a MB |
2542 | */ |
2543 | int snd_soc_register_codec(struct snd_soc_codec *codec) | |
2544 | { | |
151ab22c MB |
2545 | int i; |
2546 | ||
0d0cf00a MB |
2547 | if (!codec->name) |
2548 | return -EINVAL; | |
2549 | ||
2550 | /* The device should become mandatory over time */ | |
2551 | if (!codec->dev) | |
2552 | printk(KERN_WARNING "No device for codec %s\n", codec->name); | |
2553 | ||
2554 | INIT_LIST_HEAD(&codec->list); | |
2555 | ||
151ab22c MB |
2556 | for (i = 0; i < codec->num_dai; i++) { |
2557 | fixup_codec_formats(&codec->dai[i].playback); | |
2558 | fixup_codec_formats(&codec->dai[i].capture); | |
2559 | } | |
2560 | ||
0d0cf00a MB |
2561 | mutex_lock(&client_mutex); |
2562 | list_add(&codec->list, &codec_list); | |
2563 | snd_soc_instantiate_cards(); | |
2564 | mutex_unlock(&client_mutex); | |
2565 | ||
2566 | pr_debug("Registered codec '%s'\n", codec->name); | |
2567 | ||
2568 | return 0; | |
2569 | } | |
2570 | EXPORT_SYMBOL_GPL(snd_soc_register_codec); | |
2571 | ||
2572 | /** | |
2573 | * snd_soc_unregister_codec - Unregister a codec from the ASoC core | |
2574 | * | |
ac11a2b3 | 2575 | * @codec: codec to unregister |
0d0cf00a MB |
2576 | */ |
2577 | void snd_soc_unregister_codec(struct snd_soc_codec *codec) | |
2578 | { | |
2579 | mutex_lock(&client_mutex); | |
2580 | list_del(&codec->list); | |
2581 | mutex_unlock(&client_mutex); | |
2582 | ||
2583 | pr_debug("Unregistered codec '%s'\n", codec->name); | |
2584 | } | |
2585 | EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); | |
2586 | ||
c9b3a40f | 2587 | static int __init snd_soc_init(void) |
db2a4165 | 2588 | { |
384c89e2 MB |
2589 | #ifdef CONFIG_DEBUG_FS |
2590 | debugfs_root = debugfs_create_dir("asoc", NULL); | |
2591 | if (IS_ERR(debugfs_root) || !debugfs_root) { | |
2592 | printk(KERN_WARNING | |
2593 | "ASoC: Failed to create debugfs directory\n"); | |
2594 | debugfs_root = NULL; | |
2595 | } | |
2596 | #endif | |
2597 | ||
db2a4165 FM |
2598 | return platform_driver_register(&soc_driver); |
2599 | } | |
2600 | ||
7d8c16a6 | 2601 | static void __exit snd_soc_exit(void) |
db2a4165 | 2602 | { |
384c89e2 MB |
2603 | #ifdef CONFIG_DEBUG_FS |
2604 | debugfs_remove_recursive(debugfs_root); | |
2605 | #endif | |
3ff3f64b | 2606 | platform_driver_unregister(&soc_driver); |
db2a4165 FM |
2607 | } |
2608 | ||
2609 | module_init(snd_soc_init); | |
2610 | module_exit(snd_soc_exit); | |
2611 | ||
2612 | /* Module information */ | |
d331124d | 2613 | MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); |
db2a4165 FM |
2614 | MODULE_DESCRIPTION("ALSA SoC Core"); |
2615 | MODULE_LICENSE("GPL"); | |
8b45a209 | 2616 | MODULE_ALIAS("platform:soc-audio"); |