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