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