ac97.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/kernel.h>
  16. #include <linux/device.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/ac97_codec.h>
  20. #include <sound/initval.h>
  21. #include <sound/soc.h>
  22. #include "ac97.h"
  23. #define AC97_VERSION "0.6"
  24. static int ac97_prepare(struct snd_pcm_substream *substream,
  25. struct snd_soc_dai *dai)
  26. {
  27. struct snd_pcm_runtime *runtime = substream->runtime;
  28. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  29. struct snd_soc_device *socdev = rtd->socdev;
  30. struct snd_soc_codec *codec = socdev->card->codec;
  31. int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  32. AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
  33. return snd_ac97_set_rate(codec->ac97, reg, runtime->rate);
  34. }
  35. #define STD_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  36. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 |\
  37. SNDRV_PCM_RATE_48000)
  38. static struct snd_soc_dai_ops ac97_dai_ops = {
  39. .prepare = ac97_prepare,
  40. };
  41. struct snd_soc_dai ac97_dai = {
  42. .name = "AC97 HiFi",
  43. .ac97_control = 1,
  44. .playback = {
  45. .stream_name = "AC97 Playback",
  46. .channels_min = 1,
  47. .channels_max = 2,
  48. .rates = STD_AC97_RATES,
  49. .formats = SND_SOC_STD_AC97_FMTS,},
  50. .capture = {
  51. .stream_name = "AC97 Capture",
  52. .channels_min = 1,
  53. .channels_max = 2,
  54. .rates = STD_AC97_RATES,
  55. .formats = SND_SOC_STD_AC97_FMTS,},
  56. .ops = &ac97_dai_ops,
  57. };
  58. EXPORT_SYMBOL_GPL(ac97_dai);
  59. static unsigned int ac97_read(struct snd_soc_codec *codec,
  60. unsigned int reg)
  61. {
  62. return soc_ac97_ops.read(codec->ac97, reg);
  63. }
  64. static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
  65. unsigned int val)
  66. {
  67. soc_ac97_ops.write(codec->ac97, reg, val);
  68. return 0;
  69. }
  70. static int ac97_soc_probe(struct platform_device *pdev)
  71. {
  72. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  73. struct snd_soc_codec *codec;
  74. struct snd_ac97_bus *ac97_bus;
  75. struct snd_ac97_template ac97_template;
  76. int ret = 0;
  77. printk(KERN_INFO "AC97 SoC Audio Codec %s\n", AC97_VERSION);
  78. socdev->card->codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  79. if (!socdev->card->codec)
  80. return -ENOMEM;
  81. codec = socdev->card->codec;
  82. mutex_init(&codec->mutex);
  83. codec->name = "AC97";
  84. codec->owner = THIS_MODULE;
  85. codec->dai = &ac97_dai;
  86. codec->num_dai = 1;
  87. codec->write = ac97_write;
  88. codec->read = ac97_read;
  89. INIT_LIST_HEAD(&codec->dapm_widgets);
  90. INIT_LIST_HEAD(&codec->dapm_paths);
  91. ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
  92. if (ret < 0) {
  93. printk(KERN_ERR "ASoC: failed to init gen ac97 glue\n");
  94. goto err;
  95. }
  96. /* register pcms */
  97. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  98. if (ret < 0)
  99. goto err;
  100. /* add codec as bus device for standard ac97 */
  101. ret = snd_ac97_bus(codec->card, 0, &soc_ac97_ops, NULL, &ac97_bus);
  102. if (ret < 0)
  103. goto bus_err;
  104. memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
  105. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &codec->ac97);
  106. if (ret < 0)
  107. goto bus_err;
  108. return 0;
  109. bus_err:
  110. snd_soc_free_pcms(socdev);
  111. err:
  112. kfree(socdev->card->codec);
  113. socdev->card->codec = NULL;
  114. return ret;
  115. }
  116. static int ac97_soc_remove(struct platform_device *pdev)
  117. {
  118. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  119. struct snd_soc_codec *codec = socdev->card->codec;
  120. if (!codec)
  121. return 0;
  122. snd_soc_free_pcms(socdev);
  123. kfree(socdev->card->codec);
  124. return 0;
  125. }
  126. #ifdef CONFIG_PM
  127. static int ac97_soc_suspend(struct platform_device *pdev, pm_message_t msg)
  128. {
  129. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  130. snd_ac97_suspend(socdev->card->codec->ac97);
  131. return 0;
  132. }
  133. static int ac97_soc_resume(struct platform_device *pdev)
  134. {
  135. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  136. snd_ac97_resume(socdev->card->codec->ac97);
  137. return 0;
  138. }
  139. #else
  140. #define ac97_soc_suspend NULL
  141. #define ac97_soc_resume NULL
  142. #endif
  143. struct snd_soc_codec_device soc_codec_dev_ac97 = {
  144. .probe = ac97_soc_probe,
  145. .remove = ac97_soc_remove,
  146. .suspend = ac97_soc_suspend,
  147. .resume = ac97_soc_resume,
  148. };
  149. EXPORT_SYMBOL_GPL(soc_codec_dev_ac97);
  150. MODULE_DESCRIPTION("Soc Generic AC97 driver");
  151. MODULE_AUTHOR("Liam Girdwood");
  152. MODULE_LICENSE("GPL");