pxa2xx-ac97.c 6.3 KB

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