pxa2xx-ac97.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/sound/pxa2xx-ac97.c -- AC97 support for the Intel PXA2xx chip.
  4. *
  5. * Author: Nicolas Pitre
  6. * Created: Dec 02, 2004
  7. * Copyright: MontaVista Software Inc.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/dma-mapping.h>
  15. #include <sound/core.h>
  16. #include <sound/pcm.h>
  17. #include <sound/ac97_codec.h>
  18. #include <sound/initval.h>
  19. #include <sound/pxa2xx-lib.h>
  20. #include <sound/dmaengine_pcm.h>
  21. #include <linux/platform_data/asoc-pxa.h>
  22. static void pxa2xx_ac97_legacy_reset(struct snd_ac97 *ac97)
  23. {
  24. if (!pxa2xx_ac97_try_cold_reset())
  25. pxa2xx_ac97_try_warm_reset();
  26. pxa2xx_ac97_finish_reset();
  27. }
  28. static unsigned short pxa2xx_ac97_legacy_read(struct snd_ac97 *ac97,
  29. unsigned short reg)
  30. {
  31. int ret;
  32. ret = pxa2xx_ac97_read(ac97->num, reg);
  33. if (ret < 0)
  34. return 0;
  35. else
  36. return (unsigned short)(ret & 0xffff);
  37. }
  38. static void pxa2xx_ac97_legacy_write(struct snd_ac97 *ac97,
  39. unsigned short reg, unsigned short val)
  40. {
  41. pxa2xx_ac97_write(ac97->num, reg, val);
  42. }
  43. static const struct snd_ac97_bus_ops pxa2xx_ac97_ops = {
  44. .read = pxa2xx_ac97_legacy_read,
  45. .write = pxa2xx_ac97_legacy_write,
  46. .reset = pxa2xx_ac97_legacy_reset,
  47. };
  48. static struct snd_pcm *pxa2xx_ac97_pcm;
  49. static struct snd_ac97 *pxa2xx_ac97_ac97;
  50. static int pxa2xx_ac97_pcm_open(struct snd_pcm_substream *substream)
  51. {
  52. struct snd_pcm_runtime *runtime = substream->runtime;
  53. pxa2xx_audio_ops_t *platform_ops;
  54. int ret, i;
  55. ret = pxa2xx_pcm_open(substream);
  56. if (ret)
  57. return ret;
  58. runtime->hw.channels_min = 2;
  59. runtime->hw.channels_max = 2;
  60. i = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  61. AC97_RATES_FRONT_DAC : AC97_RATES_ADC;
  62. runtime->hw.rates = pxa2xx_ac97_ac97->rates[i];
  63. snd_pcm_limit_hw_rates(runtime);
  64. platform_ops = substream->pcm->card->dev->platform_data;
  65. if (platform_ops && platform_ops->startup) {
  66. ret = platform_ops->startup(substream, platform_ops->priv);
  67. if (ret < 0)
  68. pxa2xx_pcm_close(substream);
  69. }
  70. return ret;
  71. }
  72. static int pxa2xx_ac97_pcm_close(struct snd_pcm_substream *substream)
  73. {
  74. pxa2xx_audio_ops_t *platform_ops;
  75. platform_ops = substream->pcm->card->dev->platform_data;
  76. if (platform_ops && platform_ops->shutdown)
  77. platform_ops->shutdown(substream, platform_ops->priv);
  78. return 0;
  79. }
  80. static int pxa2xx_ac97_pcm_prepare(struct snd_pcm_substream *substream)
  81. {
  82. struct snd_pcm_runtime *runtime = substream->runtime;
  83. int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  84. AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
  85. int ret;
  86. ret = pxa2xx_pcm_prepare(substream);
  87. if (ret < 0)
  88. return ret;
  89. return snd_ac97_set_rate(pxa2xx_ac97_ac97, reg, runtime->rate);
  90. }
  91. #ifdef CONFIG_PM_SLEEP
  92. static int pxa2xx_ac97_do_suspend(struct snd_card *card)
  93. {
  94. pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
  95. snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
  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 const struct snd_pcm_ops pxa2xx_ac97_pcm_ops = {
  133. .open = pxa2xx_ac97_pcm_open,
  134. .close = pxa2xx_ac97_pcm_close,
  135. .hw_params = pxa2xx_pcm_hw_params,
  136. .prepare = pxa2xx_ac97_pcm_prepare,
  137. .trigger = pxa2xx_pcm_trigger,
  138. .pointer = pxa2xx_pcm_pointer,
  139. };
  140. static int pxa2xx_ac97_pcm_new(struct snd_card *card)
  141. {
  142. struct snd_pcm *pcm;
  143. int ret;
  144. ret = snd_pcm_new(card, "PXA2xx-PCM", 0, 1, 1, &pcm);
  145. if (ret)
  146. goto out;
  147. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  148. if (ret)
  149. goto out;
  150. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pxa2xx_ac97_pcm_ops);
  151. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pxa2xx_ac97_pcm_ops);
  152. ret = pxa2xx_pcm_preallocate_dma_buffer(pcm);
  153. if (ret)
  154. goto out;
  155. pxa2xx_ac97_pcm = pcm;
  156. ret = 0;
  157. out:
  158. return ret;
  159. }
  160. static int pxa2xx_ac97_probe(struct platform_device *dev)
  161. {
  162. struct snd_card *card;
  163. struct snd_ac97_bus *ac97_bus;
  164. struct snd_ac97_template ac97_template;
  165. int ret;
  166. pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;
  167. if (dev->id >= 0) {
  168. dev_err(&dev->dev, "PXA2xx has only one AC97 port.\n");
  169. ret = -ENXIO;
  170. goto err_dev;
  171. }
  172. ret = snd_card_new(&dev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  173. THIS_MODULE, 0, &card);
  174. if (ret < 0)
  175. goto err;
  176. strscpy(card->driver, dev->dev.driver->name, sizeof(card->driver));
  177. ret = pxa2xx_ac97_pcm_new(card);
  178. if (ret)
  179. goto err;
  180. ret = pxa2xx_ac97_hw_probe(dev);
  181. if (ret)
  182. goto err;
  183. ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);
  184. if (ret)
  185. goto err_remove;
  186. memset(&ac97_template, 0, sizeof(ac97_template));
  187. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);
  188. if (ret)
  189. goto err_remove;
  190. snprintf(card->shortname, sizeof(card->shortname),
  191. "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));
  192. snprintf(card->longname, sizeof(card->longname),
  193. "%s (%s)", dev->dev.driver->name, card->mixername);
  194. if (pdata && pdata->codec_pdata[0])
  195. snd_ac97_dev_add_pdata(ac97_bus->codec[0], pdata->codec_pdata[0]);
  196. ret = snd_card_register(card);
  197. if (ret == 0) {
  198. platform_set_drvdata(dev, card);
  199. return 0;
  200. }
  201. err_remove:
  202. pxa2xx_ac97_hw_remove(dev);
  203. err:
  204. if (card)
  205. snd_card_free(card);
  206. err_dev:
  207. return ret;
  208. }
  209. static int pxa2xx_ac97_remove(struct platform_device *dev)
  210. {
  211. struct snd_card *card = platform_get_drvdata(dev);
  212. if (card) {
  213. snd_card_free(card);
  214. pxa2xx_ac97_hw_remove(dev);
  215. }
  216. return 0;
  217. }
  218. static struct platform_driver pxa2xx_ac97_driver = {
  219. .probe = pxa2xx_ac97_probe,
  220. .remove = pxa2xx_ac97_remove,
  221. .driver = {
  222. .name = "pxa2xx-ac97",
  223. #ifdef CONFIG_PM_SLEEP
  224. .pm = &pxa2xx_ac97_pm_ops,
  225. #endif
  226. },
  227. };
  228. module_platform_driver(pxa2xx_ac97_driver);
  229. MODULE_AUTHOR("Nicolas Pitre");
  230. MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
  231. MODULE_LICENSE("GPL");
  232. MODULE_ALIAS("platform:pxa2xx-ac97");