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/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
  91. static int pxa2xx_ac97_do_suspend(struct snd_card *card, pm_message_t state)
  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, PMSG_SUSPEND);
  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 const struct dev_pm_ops pxa2xx_ac97_pm_ops = {
  131. .suspend = pxa2xx_ac97_suspend,
  132. .resume = pxa2xx_ac97_resume,
  133. };
  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. pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;
  142. if (dev->id >= 0) {
  143. dev_err(&dev->dev, "PXA2xx has only one AC97 port.\n");
  144. ret = -ENXIO;
  145. goto err_dev;
  146. }
  147. ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  148. THIS_MODULE, 0, &card);
  149. if (ret < 0)
  150. goto err;
  151. card->dev = &dev->dev;
  152. strncpy(card->driver, dev->dev.driver->name, sizeof(card->driver));
  153. ret = pxa2xx_pcm_new(card, &pxa2xx_ac97_pcm_client, &pxa2xx_ac97_pcm);
  154. if (ret)
  155. goto err;
  156. ret = pxa2xx_ac97_hw_probe(dev);
  157. if (ret)
  158. goto err;
  159. ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);
  160. if (ret)
  161. goto err_remove;
  162. memset(&ac97_template, 0, sizeof(ac97_template));
  163. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);
  164. if (ret)
  165. goto err_remove;
  166. snprintf(card->shortname, sizeof(card->shortname),
  167. "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));
  168. snprintf(card->longname, sizeof(card->longname),
  169. "%s (%s)", dev->dev.driver->name, card->mixername);
  170. if (pdata && pdata->codec_pdata[0])
  171. snd_ac97_dev_add_pdata(ac97_bus->codec[0], pdata->codec_pdata[0]);
  172. snd_card_set_dev(card, &dev->dev);
  173. ret = snd_card_register(card);
  174. if (ret == 0) {
  175. platform_set_drvdata(dev, card);
  176. return 0;
  177. }
  178. err_remove:
  179. pxa2xx_ac97_hw_remove(dev);
  180. err:
  181. if (card)
  182. snd_card_free(card);
  183. err_dev:
  184. return ret;
  185. }
  186. static int __devexit pxa2xx_ac97_remove(struct platform_device *dev)
  187. {
  188. struct snd_card *card = platform_get_drvdata(dev);
  189. if (card) {
  190. snd_card_free(card);
  191. platform_set_drvdata(dev, NULL);
  192. pxa2xx_ac97_hw_remove(dev);
  193. }
  194. return 0;
  195. }
  196. static struct platform_driver pxa2xx_ac97_driver = {
  197. .probe = pxa2xx_ac97_probe,
  198. .remove = __devexit_p(pxa2xx_ac97_remove),
  199. .driver = {
  200. .name = "pxa2xx-ac97",
  201. .owner = THIS_MODULE,
  202. #ifdef CONFIG_PM
  203. .pm = &pxa2xx_ac97_pm_ops,
  204. #endif
  205. },
  206. };
  207. module_platform_driver(pxa2xx_ac97_driver);
  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");