ac97.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. static struct snd_soc_codec_dai ac97_dai = {
  41. .name = "AC97 HiFi",
  42. .playback = {
  43. .stream_name = "AC97 Playback",
  44. .channels_min = 1,
  45. .channels_max = 2,
  46. .rates = STD_AC97_RATES,
  47. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  48. .capture = {
  49. .stream_name = "AC97 Capture",
  50. .channels_min = 1,
  51. .channels_max = 2,
  52. .rates = STD_AC97_RATES,
  53. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  54. .ops = {
  55. .prepare = ac97_prepare,},
  56. };
  57. EXPORT_SYMBOL_GPL(ac97_dai);
  58. static unsigned int ac97_read(struct snd_soc_codec *codec,
  59. unsigned int reg)
  60. {
  61. return soc_ac97_ops.read(codec->ac97, reg);
  62. }
  63. static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
  64. unsigned int val)
  65. {
  66. soc_ac97_ops.write(codec->ac97, reg, val);
  67. return 0;
  68. }
  69. static int ac97_soc_probe(struct platform_device *pdev)
  70. {
  71. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  72. struct snd_soc_codec *codec;
  73. struct snd_ac97_bus *ac97_bus;
  74. struct snd_ac97_template ac97_template;
  75. int ret = 0;
  76. printk(KERN_INFO "AC97 SoC Audio Codec %s\n", AC97_VERSION);
  77. socdev->codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  78. if (socdev->codec == NULL)
  79. return -ENOMEM;
  80. codec = socdev->codec;
  81. mutex_init(&codec->mutex);
  82. codec->name = "AC97";
  83. codec->owner = THIS_MODULE;
  84. codec->dai = &ac97_dai;
  85. codec->num_dai = 1;
  86. codec->write = ac97_write;
  87. codec->read = ac97_read;
  88. INIT_LIST_HEAD(&codec->dapm_widgets);
  89. INIT_LIST_HEAD(&codec->dapm_paths);
  90. /* register pcms */
  91. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  92. if(ret < 0)
  93. goto err;
  94. /* add codec as bus device for standard ac97 */
  95. ret = snd_ac97_bus(codec->card, 0, &soc_ac97_ops, NULL, &ac97_bus);
  96. if(ret < 0)
  97. goto bus_err;
  98. memset(&ac97_template, 0, sizeof(struct snd_ac97_template));
  99. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &codec->ac97);
  100. if(ret < 0)
  101. goto bus_err;
  102. ret = snd_soc_register_card(socdev);
  103. if (ret < 0)
  104. goto bus_err;
  105. return 0;
  106. bus_err:
  107. snd_soc_free_pcms(socdev);
  108. err:
  109. kfree(socdev->codec->reg_cache);
  110. kfree(socdev->codec);
  111. socdev->codec = NULL;
  112. return ret;
  113. }
  114. static int ac97_soc_remove(struct platform_device *pdev)
  115. {
  116. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  117. struct snd_soc_codec *codec = socdev->codec;
  118. if(codec == NULL)
  119. return 0;
  120. snd_soc_free_pcms(socdev);
  121. kfree(socdev->codec->reg_cache);
  122. kfree(socdev->codec);
  123. return 0;
  124. }
  125. struct snd_soc_codec_device soc_codec_dev_ac97= {
  126. .probe = ac97_soc_probe,
  127. .remove = ac97_soc_remove,
  128. };
  129. EXPORT_SYMBOL_GPL(soc_codec_dev_ac97);
  130. MODULE_DESCRIPTION("Soc Generic AC97 driver");
  131. MODULE_AUTHOR("Liam Girdwood");
  132. MODULE_LICENSE("GPL");