pxa2xx-ac97.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * linux/sound/pxa2xx-ac97.c -- AC97 support for the Intel PXA2xx chip.
  3. *
  4. * Author: Nicolas Pitre
  5. * Created: Dec 02, 2004
  6. * Copyright: MontaVista Software Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <sound/core.h>
  16. #include <sound/pcm.h>
  17. #include <sound/ac97_codec.h>
  18. #include <sound/initval.h>
  19. #include <sound/pxa2xx-lib.h>
  20. #include <mach/regs-ac97.h>
  21. #include <mach/audio.h>
  22. #include "pxa2xx-pcm.h"
  23. static void pxa2xx_ac97_reset(struct snd_ac97 *ac97)
  24. {
  25. if (!pxa2xx_ac97_try_cold_reset(ac97)) {
  26. pxa2xx_ac97_try_warm_reset(ac97);
  27. }
  28. pxa2xx_ac97_finish_reset(ac97);
  29. }
  30. static struct snd_ac97_bus_ops pxa2xx_ac97_ops = {
  31. .read = pxa2xx_ac97_read,
  32. .write = pxa2xx_ac97_write,
  33. .reset = pxa2xx_ac97_reset,
  34. };
  35. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_out = {
  36. .name = "AC97 PCM out",
  37. .dev_addr = __PREG(PCDR),
  38. .drcmr = &DRCMR(12),
  39. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  40. DCMD_BURST32 | DCMD_WIDTH4,
  41. };
  42. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_in = {
  43. .name = "AC97 PCM in",
  44. .dev_addr = __PREG(PCDR),
  45. .drcmr = &DRCMR(11),
  46. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  47. DCMD_BURST32 | DCMD_WIDTH4,
  48. };
  49. static struct snd_pcm *pxa2xx_ac97_pcm;
  50. static struct snd_ac97 *pxa2xx_ac97_ac97;
  51. static int pxa2xx_ac97_pcm_startup(struct snd_pcm_substream *substream)
  52. {
  53. struct snd_pcm_runtime *runtime = substream->runtime;
  54. pxa2xx_audio_ops_t *platform_ops;
  55. int r;
  56. runtime->hw.channels_min = 2;
  57. runtime->hw.channels_max = 2;
  58. r = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  59. AC97_RATES_FRONT_DAC : AC97_RATES_ADC;
  60. runtime->hw.rates = pxa2xx_ac97_ac97->rates[r];
  61. snd_pcm_limit_hw_rates(runtime);
  62. platform_ops = substream->pcm->card->dev->platform_data;
  63. if (platform_ops && platform_ops->startup)
  64. return platform_ops->startup(substream, platform_ops->priv);
  65. else
  66. return 0;
  67. }
  68. static void pxa2xx_ac97_pcm_shutdown(struct snd_pcm_substream *substream)
  69. {
  70. pxa2xx_audio_ops_t *platform_ops;
  71. platform_ops = substream->pcm->card->dev->platform_data;
  72. if (platform_ops && platform_ops->shutdown)
  73. platform_ops->shutdown(substream, platform_ops->priv);
  74. }
  75. static int pxa2xx_ac97_pcm_prepare(struct snd_pcm_substream *substream)
  76. {
  77. struct snd_pcm_runtime *runtime = substream->runtime;
  78. int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  79. AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
  80. return snd_ac97_set_rate(pxa2xx_ac97_ac97, reg, runtime->rate);
  81. }
  82. static struct pxa2xx_pcm_client pxa2xx_ac97_pcm_client = {
  83. .playback_params = &pxa2xx_ac97_pcm_out,
  84. .capture_params = &pxa2xx_ac97_pcm_in,
  85. .startup = pxa2xx_ac97_pcm_startup,
  86. .shutdown = pxa2xx_ac97_pcm_shutdown,
  87. .prepare = pxa2xx_ac97_pcm_prepare,
  88. };
  89. #ifdef CONFIG_PM
  90. static int pxa2xx_ac97_do_suspend(struct snd_card *card, pm_message_t state)
  91. {
  92. pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
  93. snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
  94. snd_pcm_suspend_all(pxa2xx_ac97_pcm);
  95. snd_ac97_suspend(pxa2xx_ac97_ac97);
  96. if (platform_ops && platform_ops->suspend)
  97. platform_ops->suspend(platform_ops->priv);
  98. return pxa2xx_ac97_hw_suspend();
  99. }
  100. static int pxa2xx_ac97_do_resume(struct snd_card *card)
  101. {
  102. pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
  103. int rc;
  104. rc = pxa2xx_ac97_hw_resume();
  105. if (rc)
  106. return rc;
  107. if (platform_ops && platform_ops->resume)
  108. platform_ops->resume(platform_ops->priv);
  109. snd_ac97_resume(pxa2xx_ac97_ac97);
  110. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  111. return 0;
  112. }
  113. static int pxa2xx_ac97_suspend(struct platform_device *dev, pm_message_t state)
  114. {
  115. struct snd_card *card = platform_get_drvdata(dev);
  116. int ret = 0;
  117. if (card)
  118. ret = pxa2xx_ac97_do_suspend(card, PMSG_SUSPEND);
  119. return ret;
  120. }
  121. static int pxa2xx_ac97_resume(struct platform_device *dev)
  122. {
  123. struct snd_card *card = platform_get_drvdata(dev);
  124. int ret = 0;
  125. if (card)
  126. ret = pxa2xx_ac97_do_resume(card);
  127. return ret;
  128. }
  129. #else
  130. #define pxa2xx_ac97_suspend NULL
  131. #define pxa2xx_ac97_resume NULL
  132. #endif
  133. static int __devinit pxa2xx_ac97_probe(struct platform_device *dev)
  134. {
  135. struct snd_card *card;
  136. struct snd_ac97_bus *ac97_bus;
  137. struct snd_ac97_template ac97_template;
  138. int ret;
  139. ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  140. THIS_MODULE, 0, &card);
  141. if (ret < 0)
  142. goto err;
  143. card->dev = &dev->dev;
  144. strncpy(card->driver, dev->dev.driver->name, sizeof(card->driver));
  145. ret = pxa2xx_pcm_new(card, &pxa2xx_ac97_pcm_client, &pxa2xx_ac97_pcm);
  146. if (ret)
  147. goto err;
  148. ret = pxa2xx_ac97_hw_probe(dev);
  149. if (ret)
  150. goto err;
  151. ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);
  152. if (ret)
  153. goto err_remove;
  154. memset(&ac97_template, 0, sizeof(ac97_template));
  155. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);
  156. if (ret)
  157. goto err_remove;
  158. snprintf(card->shortname, sizeof(card->shortname),
  159. "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));
  160. snprintf(card->longname, sizeof(card->longname),
  161. "%s (%s)", dev->dev.driver->name, card->mixername);
  162. snd_card_set_dev(card, &dev->dev);
  163. ret = snd_card_register(card);
  164. if (ret == 0) {
  165. platform_set_drvdata(dev, card);
  166. return 0;
  167. }
  168. err_remove:
  169. pxa2xx_ac97_hw_remove(dev);
  170. err:
  171. if (card)
  172. snd_card_free(card);
  173. return ret;
  174. }
  175. static int __devexit pxa2xx_ac97_remove(struct platform_device *dev)
  176. {
  177. struct snd_card *card = platform_get_drvdata(dev);
  178. if (card) {
  179. snd_card_free(card);
  180. platform_set_drvdata(dev, NULL);
  181. pxa2xx_ac97_hw_remove(dev);
  182. }
  183. return 0;
  184. }
  185. static struct platform_driver pxa2xx_ac97_driver = {
  186. .probe = pxa2xx_ac97_probe,
  187. .remove = __devexit_p(pxa2xx_ac97_remove),
  188. .suspend = pxa2xx_ac97_suspend,
  189. .resume = pxa2xx_ac97_resume,
  190. .driver = {
  191. .name = "pxa2xx-ac97",
  192. .owner = THIS_MODULE,
  193. },
  194. };
  195. static int __init pxa2xx_ac97_init(void)
  196. {
  197. return platform_driver_register(&pxa2xx_ac97_driver);
  198. }
  199. static void __exit pxa2xx_ac97_exit(void)
  200. {
  201. platform_driver_unregister(&pxa2xx_ac97_driver);
  202. }
  203. module_init(pxa2xx_ac97_init);
  204. module_exit(pxa2xx_ac97_exit);
  205. MODULE_AUTHOR("Nicolas Pitre");
  206. MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
  207. MODULE_LICENSE("GPL");
  208. MODULE_ALIAS("platform:pxa2xx-ac97");