pxa2xx-ac97.c 6.3 KB

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