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