pxa2xx-ac97.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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-mapping.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. static void pxa2xx_ac97_legacy_reset(struct snd_ac97 *ac97)
  27. {
  28. if (!pxa2xx_ac97_try_cold_reset())
  29. pxa2xx_ac97_try_warm_reset();
  30. pxa2xx_ac97_finish_reset();
  31. }
  32. static unsigned short pxa2xx_ac97_legacy_read(struct snd_ac97 *ac97,
  33. unsigned short reg)
  34. {
  35. int ret;
  36. ret = pxa2xx_ac97_read(ac97->num, reg);
  37. if (ret < 0)
  38. return 0;
  39. else
  40. return (unsigned short)(ret & 0xffff);
  41. }
  42. static void pxa2xx_ac97_legacy_write(struct snd_ac97 *ac97,
  43. unsigned short reg, unsigned short val)
  44. {
  45. int __always_unused ret;
  46. ret = pxa2xx_ac97_write(ac97->num, reg, val);
  47. }
  48. static struct snd_ac97_bus_ops pxa2xx_ac97_ops = {
  49. .read = pxa2xx_ac97_legacy_read,
  50. .write = pxa2xx_ac97_legacy_write,
  51. .reset = pxa2xx_ac97_legacy_reset,
  52. };
  53. static struct snd_pcm *pxa2xx_ac97_pcm;
  54. static struct snd_ac97 *pxa2xx_ac97_ac97;
  55. static int pxa2xx_ac97_pcm_open(struct snd_pcm_substream *substream)
  56. {
  57. struct snd_pcm_runtime *runtime = substream->runtime;
  58. pxa2xx_audio_ops_t *platform_ops;
  59. int ret, i;
  60. ret = pxa2xx_pcm_open(substream);
  61. if (ret)
  62. return ret;
  63. runtime->hw.channels_min = 2;
  64. runtime->hw.channels_max = 2;
  65. i = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  66. AC97_RATES_FRONT_DAC : AC97_RATES_ADC;
  67. runtime->hw.rates = pxa2xx_ac97_ac97->rates[i];
  68. snd_pcm_limit_hw_rates(runtime);
  69. platform_ops = substream->pcm->card->dev->platform_data;
  70. if (platform_ops && platform_ops->startup) {
  71. ret = platform_ops->startup(substream, platform_ops->priv);
  72. if (ret < 0)
  73. pxa2xx_pcm_close(substream);
  74. }
  75. return ret;
  76. }
  77. static int pxa2xx_ac97_pcm_close(struct snd_pcm_substream *substream)
  78. {
  79. pxa2xx_audio_ops_t *platform_ops;
  80. platform_ops = substream->pcm->card->dev->platform_data;
  81. if (platform_ops && platform_ops->shutdown)
  82. platform_ops->shutdown(substream, platform_ops->priv);
  83. return 0;
  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. int ret;
  91. ret = pxa2xx_pcm_prepare(substream);
  92. if (ret < 0)
  93. return ret;
  94. return snd_ac97_set_rate(pxa2xx_ac97_ac97, reg, runtime->rate);
  95. }
  96. #ifdef CONFIG_PM_SLEEP
  97. static int pxa2xx_ac97_do_suspend(struct snd_card *card)
  98. {
  99. pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
  100. snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
  101. snd_pcm_suspend_all(pxa2xx_ac97_pcm);
  102. snd_ac97_suspend(pxa2xx_ac97_ac97);
  103. if (platform_ops && platform_ops->suspend)
  104. platform_ops->suspend(platform_ops->priv);
  105. return pxa2xx_ac97_hw_suspend();
  106. }
  107. static int pxa2xx_ac97_do_resume(struct snd_card *card)
  108. {
  109. pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
  110. int rc;
  111. rc = pxa2xx_ac97_hw_resume();
  112. if (rc)
  113. return rc;
  114. if (platform_ops && platform_ops->resume)
  115. platform_ops->resume(platform_ops->priv);
  116. snd_ac97_resume(pxa2xx_ac97_ac97);
  117. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  118. return 0;
  119. }
  120. static int pxa2xx_ac97_suspend(struct device *dev)
  121. {
  122. struct snd_card *card = dev_get_drvdata(dev);
  123. int ret = 0;
  124. if (card)
  125. ret = pxa2xx_ac97_do_suspend(card);
  126. return ret;
  127. }
  128. static int pxa2xx_ac97_resume(struct device *dev)
  129. {
  130. struct snd_card *card = dev_get_drvdata(dev);
  131. int ret = 0;
  132. if (card)
  133. ret = pxa2xx_ac97_do_resume(card);
  134. return ret;
  135. }
  136. static SIMPLE_DEV_PM_OPS(pxa2xx_ac97_pm_ops, pxa2xx_ac97_suspend, pxa2xx_ac97_resume);
  137. #endif
  138. static const struct snd_pcm_ops pxa2xx_ac97_pcm_ops = {
  139. .open = pxa2xx_ac97_pcm_open,
  140. .close = pxa2xx_ac97_pcm_close,
  141. .ioctl = snd_pcm_lib_ioctl,
  142. .hw_params = pxa2xx_pcm_hw_params,
  143. .hw_free = pxa2xx_pcm_hw_free,
  144. .prepare = pxa2xx_ac97_pcm_prepare,
  145. .trigger = pxa2xx_pcm_trigger,
  146. .pointer = pxa2xx_pcm_pointer,
  147. .mmap = pxa2xx_pcm_mmap,
  148. };
  149. static int pxa2xx_ac97_pcm_new(struct snd_card *card)
  150. {
  151. struct snd_pcm *pcm;
  152. int stream, ret;
  153. ret = snd_pcm_new(card, "PXA2xx-PCM", 0, 1, 1, &pcm);
  154. if (ret)
  155. goto out;
  156. pcm->private_free = pxa2xx_pcm_free_dma_buffers;
  157. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  158. if (ret)
  159. goto out;
  160. stream = SNDRV_PCM_STREAM_PLAYBACK;
  161. snd_pcm_set_ops(pcm, stream, &pxa2xx_ac97_pcm_ops);
  162. ret = pxa2xx_pcm_preallocate_dma_buffer(pcm, stream);
  163. if (ret)
  164. goto out;
  165. stream = SNDRV_PCM_STREAM_CAPTURE;
  166. snd_pcm_set_ops(pcm, stream, &pxa2xx_ac97_pcm_ops);
  167. ret = pxa2xx_pcm_preallocate_dma_buffer(pcm, stream);
  168. if (ret)
  169. goto out;
  170. pxa2xx_ac97_pcm = pcm;
  171. ret = 0;
  172. out:
  173. return ret;
  174. }
  175. static int pxa2xx_ac97_probe(struct platform_device *dev)
  176. {
  177. struct snd_card *card;
  178. struct snd_ac97_bus *ac97_bus;
  179. struct snd_ac97_template ac97_template;
  180. int ret;
  181. pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;
  182. if (dev->id >= 0) {
  183. dev_err(&dev->dev, "PXA2xx has only one AC97 port.\n");
  184. ret = -ENXIO;
  185. goto err_dev;
  186. }
  187. ret = snd_card_new(&dev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  188. THIS_MODULE, 0, &card);
  189. if (ret < 0)
  190. goto err;
  191. strlcpy(card->driver, dev->dev.driver->name, sizeof(card->driver));
  192. ret = pxa2xx_ac97_pcm_new(card);
  193. if (ret)
  194. goto err;
  195. ret = pxa2xx_ac97_hw_probe(dev);
  196. if (ret)
  197. goto err;
  198. ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);
  199. if (ret)
  200. goto err_remove;
  201. memset(&ac97_template, 0, sizeof(ac97_template));
  202. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);
  203. if (ret)
  204. goto err_remove;
  205. snprintf(card->shortname, sizeof(card->shortname),
  206. "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));
  207. snprintf(card->longname, sizeof(card->longname),
  208. "%s (%s)", dev->dev.driver->name, card->mixername);
  209. if (pdata && pdata->codec_pdata[0])
  210. snd_ac97_dev_add_pdata(ac97_bus->codec[0], pdata->codec_pdata[0]);
  211. ret = snd_card_register(card);
  212. if (ret == 0) {
  213. platform_set_drvdata(dev, card);
  214. return 0;
  215. }
  216. err_remove:
  217. pxa2xx_ac97_hw_remove(dev);
  218. err:
  219. if (card)
  220. snd_card_free(card);
  221. err_dev:
  222. return ret;
  223. }
  224. static int pxa2xx_ac97_remove(struct platform_device *dev)
  225. {
  226. struct snd_card *card = platform_get_drvdata(dev);
  227. if (card) {
  228. snd_card_free(card);
  229. pxa2xx_ac97_hw_remove(dev);
  230. }
  231. return 0;
  232. }
  233. static struct platform_driver pxa2xx_ac97_driver = {
  234. .probe = pxa2xx_ac97_probe,
  235. .remove = pxa2xx_ac97_remove,
  236. .driver = {
  237. .name = "pxa2xx-ac97",
  238. #ifdef CONFIG_PM_SLEEP
  239. .pm = &pxa2xx_ac97_pm_ops,
  240. #endif
  241. },
  242. };
  243. module_platform_driver(pxa2xx_ac97_driver);
  244. MODULE_AUTHOR("Nicolas Pitre");
  245. MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
  246. MODULE_LICENSE("GPL");
  247. MODULE_ALIAS("platform:pxa2xx-ac97");