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