ac97.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * ac97.c -- ALSA Soc AC97 codec support
  3. *
  4. * Copyright 2005 Wolfson Microelectronics PLC.
  5. * Author: Liam Girdwood
  6. * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * Revision history
  14. * 17th Oct 2005 Initial version.
  15. *
  16. * Generic AC97 support.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/device.h>
  21. #include <sound/driver.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/ac97_codec.h>
  25. #include <sound/initval.h>
  26. #include <sound/soc.h>
  27. #define AC97_VERSION "0.6"
  28. static int ac97_prepare(struct snd_pcm_substream *substream)
  29. {
  30. struct snd_pcm_runtime *runtime = substream->runtime;
  31. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  32. struct snd_soc_device *socdev = rtd->socdev;
  33. struct snd_soc_codec *codec = socdev->codec;
  34. int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  35. AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
  36. return snd_ac97_set_rate(codec->ac97, reg, runtime->rate);
  37. }
  38. #define STD_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  39. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  40. struct snd_soc_codec_dai ac97_dai = {
  41. .name = "AC97 HiFi",
  42. .type = SND_SOC_DAI_AC97,
  43. .playback = {
  44. .stream_name = "AC97 Playback",
  45. .channels_min = 1,
  46. .channels_max = 2,
  47. .rates = STD_AC97_RATES,
  48. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  49. .capture = {
  50. .stream_name = "AC97 Capture",
  51. .channels_min = 1,
  52. .channels_max = 2,
  53. .rates = STD_AC97_RATES,
  54. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  55. .ops = {
  56. .prepare = ac97_prepare,},
  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->codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  79. if (socdev->codec == NULL)
  80. return -ENOMEM;
  81. codec = socdev->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. /* register pcms */
  92. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  93. if(ret < 0)
  94. goto err;
  95. /* add codec as bus device for standard ac97 */
  96. ret = snd_ac97_bus(codec->card, 0, &soc_ac97_ops, NULL, &ac97_bus);
  97. if(ret < 0)
  98. goto bus_err;
  99. memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
  100. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &codec->ac97);
  101. if(ret < 0)
  102. goto bus_err;
  103. ret = snd_soc_register_card(socdev);
  104. if (ret < 0)
  105. goto bus_err;
  106. return 0;
  107. bus_err:
  108. snd_soc_free_pcms(socdev);
  109. err:
  110. kfree(socdev->codec->reg_cache);
  111. kfree(socdev->codec);
  112. socdev->codec = NULL;
  113. return ret;
  114. }
  115. static int ac97_soc_remove(struct platform_device *pdev)
  116. {
  117. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  118. struct snd_soc_codec *codec = socdev->codec;
  119. if(codec == NULL)
  120. return 0;
  121. snd_soc_free_pcms(socdev);
  122. kfree(socdev->codec->reg_cache);
  123. kfree(socdev->codec);
  124. return 0;
  125. }
  126. struct snd_soc_codec_device soc_codec_dev_ac97= {
  127. .probe = ac97_soc_probe,
  128. .remove = ac97_soc_remove,
  129. };
  130. EXPORT_SYMBOL_GPL(soc_codec_dev_ac97);
  131. MODULE_DESCRIPTION("Soc Generic AC97 driver");
  132. MODULE_AUTHOR("Liam Girdwood");
  133. MODULE_LICENSE("GPL");