ac97.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * ac97.c -- ALSA Soc AC97 codec support
  3. *
  4. * Copyright 2005 Wolfson Microelectronics PLC.
  5. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * Generic AC97 support.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/kernel.h>
  17. #include <linux/device.h>
  18. #include <sound/core.h>
  19. #include <sound/pcm.h>
  20. #include <sound/ac97_codec.h>
  21. #include <sound/initval.h>
  22. #include <sound/soc.h>
  23. #include "ac97.h"
  24. #define AC97_VERSION "0.6"
  25. static int ac97_prepare(struct snd_pcm_substream *substream,
  26. struct snd_soc_dai *dai)
  27. {
  28. struct snd_pcm_runtime *runtime = substream->runtime;
  29. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  30. struct snd_soc_device *socdev = rtd->socdev;
  31. struct snd_soc_codec *codec = socdev->card->codec;
  32. int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  33. AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
  34. return snd_ac97_set_rate(codec->ac97, reg, runtime->rate);
  35. }
  36. #define STD_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  37. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 |\
  38. SNDRV_PCM_RATE_48000)
  39. static struct snd_soc_dai_ops ac97_dai_ops = {
  40. .prepare = ac97_prepare,
  41. };
  42. struct snd_soc_dai ac97_dai = {
  43. .name = "AC97 HiFi",
  44. .ac97_control = 1,
  45. .playback = {
  46. .stream_name = "AC97 Playback",
  47. .channels_min = 1,
  48. .channels_max = 2,
  49. .rates = STD_AC97_RATES,
  50. .formats = SND_SOC_STD_AC97_FMTS,},
  51. .capture = {
  52. .stream_name = "AC97 Capture",
  53. .channels_min = 1,
  54. .channels_max = 2,
  55. .rates = STD_AC97_RATES,
  56. .formats = SND_SOC_STD_AC97_FMTS,},
  57. .ops = &ac97_dai_ops,
  58. };
  59. EXPORT_SYMBOL_GPL(ac97_dai);
  60. static unsigned int ac97_read(struct snd_soc_codec *codec,
  61. unsigned int reg)
  62. {
  63. return soc_ac97_ops.read(codec->ac97, reg);
  64. }
  65. static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
  66. unsigned int val)
  67. {
  68. soc_ac97_ops.write(codec->ac97, reg, val);
  69. return 0;
  70. }
  71. static int ac97_soc_probe(struct platform_device *pdev)
  72. {
  73. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  74. struct snd_soc_card *card = socdev->card;
  75. struct snd_soc_codec *codec;
  76. struct snd_ac97_bus *ac97_bus;
  77. struct snd_ac97_template ac97_template;
  78. int i;
  79. int ret = 0;
  80. printk(KERN_INFO "AC97 SoC Audio Codec %s\n", AC97_VERSION);
  81. socdev->card->codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  82. if (!socdev->card->codec)
  83. return -ENOMEM;
  84. codec = socdev->card->codec;
  85. mutex_init(&codec->mutex);
  86. codec->name = "AC97";
  87. codec->owner = THIS_MODULE;
  88. codec->dai = &ac97_dai;
  89. codec->num_dai = 1;
  90. codec->write = ac97_write;
  91. codec->read = ac97_read;
  92. INIT_LIST_HEAD(&codec->dapm_widgets);
  93. INIT_LIST_HEAD(&codec->dapm_paths);
  94. /* register pcms */
  95. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  96. if (ret < 0)
  97. goto err;
  98. /* add codec as bus device for standard ac97 */
  99. ret = snd_ac97_bus(codec->card, 0, &soc_ac97_ops, NULL, &ac97_bus);
  100. if (ret < 0)
  101. goto bus_err;
  102. memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
  103. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &codec->ac97);
  104. if (ret < 0)
  105. goto bus_err;
  106. for (i = 0; i < card->num_links; i++) {
  107. if (card->dai_link[i].codec_dai->ac97_control) {
  108. snd_ac97_dev_add_pdata(codec->ac97,
  109. card->dai_link[i].cpu_dai->ac97_pdata);
  110. }
  111. }
  112. return 0;
  113. bus_err:
  114. snd_soc_free_pcms(socdev);
  115. err:
  116. kfree(socdev->card->codec);
  117. socdev->card->codec = NULL;
  118. return ret;
  119. }
  120. static int ac97_soc_remove(struct platform_device *pdev)
  121. {
  122. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  123. struct snd_soc_codec *codec = socdev->card->codec;
  124. if (!codec)
  125. return 0;
  126. snd_soc_free_pcms(socdev);
  127. kfree(socdev->card->codec);
  128. return 0;
  129. }
  130. #ifdef CONFIG_PM
  131. static int ac97_soc_suspend(struct platform_device *pdev, pm_message_t msg)
  132. {
  133. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  134. snd_ac97_suspend(socdev->card->codec->ac97);
  135. return 0;
  136. }
  137. static int ac97_soc_resume(struct platform_device *pdev)
  138. {
  139. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  140. snd_ac97_resume(socdev->card->codec->ac97);
  141. return 0;
  142. }
  143. #else
  144. #define ac97_soc_suspend NULL
  145. #define ac97_soc_resume NULL
  146. #endif
  147. struct snd_soc_codec_device soc_codec_dev_ac97 = {
  148. .probe = ac97_soc_probe,
  149. .remove = ac97_soc_remove,
  150. .suspend = ac97_soc_suspend,
  151. .resume = ac97_soc_resume,
  152. };
  153. EXPORT_SYMBOL_GPL(soc_codec_dev_ac97);
  154. MODULE_DESCRIPTION("Soc Generic AC97 driver");
  155. MODULE_AUTHOR("Liam Girdwood");
  156. MODULE_LICENSE("GPL");