ac97.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. static int ac97_prepare(struct snd_pcm_substream *substream,
  24. struct snd_soc_dai *dai)
  25. {
  26. struct snd_pcm_runtime *runtime = substream->runtime;
  27. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  28. struct snd_soc_codec *codec = rtd->codec;
  29. int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  30. AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
  31. return snd_ac97_set_rate(codec->ac97, reg, runtime->rate);
  32. }
  33. #define STD_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  34. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 |\
  35. SNDRV_PCM_RATE_48000)
  36. static struct snd_soc_dai_ops ac97_dai_ops = {
  37. .prepare = ac97_prepare,
  38. };
  39. static struct snd_soc_dai_driver ac97_dai = {
  40. .name = "ac97-hifi",
  41. .ac97_control = 1,
  42. .playback = {
  43. .stream_name = "AC97 Playback",
  44. .channels_min = 1,
  45. .channels_max = 2,
  46. .rates = STD_AC97_RATES,
  47. .formats = SND_SOC_STD_AC97_FMTS,},
  48. .capture = {
  49. .stream_name = "AC97 Capture",
  50. .channels_min = 1,
  51. .channels_max = 2,
  52. .rates = STD_AC97_RATES,
  53. .formats = SND_SOC_STD_AC97_FMTS,},
  54. .ops = &ac97_dai_ops,
  55. };
  56. static unsigned int ac97_read(struct snd_soc_codec *codec,
  57. unsigned int reg)
  58. {
  59. return soc_ac97_ops.read(codec->ac97, reg);
  60. }
  61. static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
  62. unsigned int val)
  63. {
  64. soc_ac97_ops.write(codec->ac97, reg, val);
  65. return 0;
  66. }
  67. static int ac97_soc_probe(struct snd_soc_codec *codec)
  68. {
  69. struct snd_ac97_bus *ac97_bus;
  70. struct snd_ac97_template ac97_template;
  71. int ret;
  72. ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
  73. if (ret < 0) {
  74. printk(KERN_ERR "ASoC: failed to init generic ac97 glue\n");
  75. return ret;
  76. }
  77. /* add codec as bus device for standard ac97 */
  78. ret = snd_ac97_bus(codec->card->snd_card, 0, &soc_ac97_ops, NULL, &ac97_bus);
  79. if (ret < 0)
  80. return ret;
  81. memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
  82. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &codec->ac97);
  83. if (ret < 0)
  84. return ret;
  85. return 0;
  86. }
  87. static int ac97_soc_remove(struct snd_soc_codec *codec)
  88. {
  89. return 0;
  90. }
  91. #ifdef CONFIG_PM
  92. static int ac97_soc_suspend(struct snd_soc_codec *codec, pm_message_t msg)
  93. {
  94. snd_ac97_suspend(codec->ac97);
  95. return 0;
  96. }
  97. static int ac97_soc_resume(struct snd_soc_codec *codec)
  98. {
  99. snd_ac97_resume(codec->ac97);
  100. return 0;
  101. }
  102. #else
  103. #define ac97_soc_suspend NULL
  104. #define ac97_soc_resume NULL
  105. #endif
  106. static struct snd_soc_codec_driver soc_codec_dev_ac97 = {
  107. .write = ac97_write,
  108. .read = ac97_read,
  109. .probe = ac97_soc_probe,
  110. .remove = ac97_soc_remove,
  111. .suspend = ac97_soc_suspend,
  112. .resume = ac97_soc_resume,
  113. };
  114. static __devinit int ac97_probe(struct platform_device *pdev)
  115. {
  116. return snd_soc_register_codec(&pdev->dev,
  117. &soc_codec_dev_ac97, &ac97_dai, 1);
  118. }
  119. static int __devexit ac97_remove(struct platform_device *pdev)
  120. {
  121. snd_soc_unregister_codec(&pdev->dev);
  122. return 0;
  123. }
  124. static struct platform_driver ac97_codec_driver = {
  125. .driver = {
  126. .name = "ac97-codec",
  127. .owner = THIS_MODULE,
  128. },
  129. .probe = ac97_probe,
  130. .remove = __devexit_p(ac97_remove),
  131. };
  132. static int __init ac97_init(void)
  133. {
  134. return platform_driver_register(&ac97_codec_driver);
  135. }
  136. module_init(ac97_init);
  137. static void __exit ac97_exit(void)
  138. {
  139. platform_driver_unregister(&ac97_codec_driver);
  140. }
  141. module_exit(ac97_exit);
  142. MODULE_DESCRIPTION("Soc Generic AC97 driver");
  143. MODULE_AUTHOR("Liam Girdwood");
  144. MODULE_LICENSE("GPL");