pxa2xx-ac97.c 6.4 KB

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