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