pxa2xx-ac97.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. static int pxa2xx_ac97_do_suspend(struct snd_card *card)
  92. {
  93. pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
  94. snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
  95. snd_ac97_suspend(pxa2xx_ac97_ac97);
  96. if (platform_ops && platform_ops->suspend)
  97. platform_ops->suspend(platform_ops->priv);
  98. return pxa2xx_ac97_hw_suspend();
  99. }
  100. static int pxa2xx_ac97_do_resume(struct snd_card *card)
  101. {
  102. pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
  103. int rc;
  104. rc = pxa2xx_ac97_hw_resume();
  105. if (rc)
  106. return rc;
  107. if (platform_ops && platform_ops->resume)
  108. platform_ops->resume(platform_ops->priv);
  109. snd_ac97_resume(pxa2xx_ac97_ac97);
  110. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  111. return 0;
  112. }
  113. static int pxa2xx_ac97_suspend(struct device *dev)
  114. {
  115. struct snd_card *card = dev_get_drvdata(dev);
  116. int ret = 0;
  117. if (card)
  118. ret = pxa2xx_ac97_do_suspend(card);
  119. return ret;
  120. }
  121. static int pxa2xx_ac97_resume(struct device *dev)
  122. {
  123. struct snd_card *card = dev_get_drvdata(dev);
  124. int ret = 0;
  125. if (card)
  126. ret = pxa2xx_ac97_do_resume(card);
  127. return ret;
  128. }
  129. static DEFINE_SIMPLE_DEV_PM_OPS(pxa2xx_ac97_pm_ops, pxa2xx_ac97_suspend, pxa2xx_ac97_resume);
  130. static const struct snd_pcm_ops pxa2xx_ac97_pcm_ops = {
  131. .open = pxa2xx_ac97_pcm_open,
  132. .close = pxa2xx_ac97_pcm_close,
  133. .hw_params = pxa2xx_pcm_hw_params,
  134. .prepare = pxa2xx_ac97_pcm_prepare,
  135. .trigger = pxa2xx_pcm_trigger,
  136. .pointer = pxa2xx_pcm_pointer,
  137. };
  138. static int pxa2xx_ac97_pcm_new(struct snd_card *card)
  139. {
  140. struct snd_pcm *pcm;
  141. int ret;
  142. ret = snd_pcm_new(card, "PXA2xx-PCM", 0, 1, 1, &pcm);
  143. if (ret)
  144. goto out;
  145. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  146. if (ret)
  147. goto out;
  148. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pxa2xx_ac97_pcm_ops);
  149. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pxa2xx_ac97_pcm_ops);
  150. ret = pxa2xx_pcm_preallocate_dma_buffer(pcm);
  151. if (ret)
  152. goto out;
  153. pxa2xx_ac97_pcm = pcm;
  154. ret = 0;
  155. out:
  156. return ret;
  157. }
  158. static int pxa2xx_ac97_probe(struct platform_device *dev)
  159. {
  160. struct snd_card *card;
  161. struct snd_ac97_bus *ac97_bus;
  162. struct snd_ac97_template ac97_template;
  163. int ret;
  164. pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;
  165. if (dev->id >= 0) {
  166. dev_err(&dev->dev, "PXA2xx has only one AC97 port.\n");
  167. ret = -ENXIO;
  168. goto err_dev;
  169. }
  170. ret = snd_card_new(&dev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  171. THIS_MODULE, 0, &card);
  172. if (ret < 0)
  173. goto err;
  174. strscpy(card->driver, dev->dev.driver->name, sizeof(card->driver));
  175. ret = pxa2xx_ac97_pcm_new(card);
  176. if (ret)
  177. goto err;
  178. ret = pxa2xx_ac97_hw_probe(dev);
  179. if (ret)
  180. goto err;
  181. ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);
  182. if (ret)
  183. goto err_remove;
  184. memset(&ac97_template, 0, sizeof(ac97_template));
  185. ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);
  186. if (ret)
  187. goto err_remove;
  188. snprintf(card->shortname, sizeof(card->shortname),
  189. "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));
  190. snprintf(card->longname, sizeof(card->longname),
  191. "%s (%s)", dev->dev.driver->name, card->mixername);
  192. if (pdata && pdata->codec_pdata[0])
  193. snd_ac97_dev_add_pdata(ac97_bus->codec[0], pdata->codec_pdata[0]);
  194. ret = snd_card_register(card);
  195. if (ret == 0) {
  196. platform_set_drvdata(dev, card);
  197. return 0;
  198. }
  199. err_remove:
  200. pxa2xx_ac97_hw_remove(dev);
  201. err:
  202. if (card)
  203. snd_card_free(card);
  204. err_dev:
  205. return ret;
  206. }
  207. static void pxa2xx_ac97_remove(struct platform_device *dev)
  208. {
  209. struct snd_card *card = platform_get_drvdata(dev);
  210. if (card) {
  211. snd_card_free(card);
  212. pxa2xx_ac97_hw_remove(dev);
  213. }
  214. }
  215. static struct platform_driver pxa2xx_ac97_driver = {
  216. .probe = pxa2xx_ac97_probe,
  217. .remove_new = pxa2xx_ac97_remove,
  218. .driver = {
  219. .name = "pxa2xx-ac97",
  220. .pm = &pxa2xx_ac97_pm_ops,
  221. },
  222. };
  223. module_platform_driver(pxa2xx_ac97_driver);
  224. MODULE_AUTHOR("Nicolas Pitre");
  225. MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
  226. MODULE_LICENSE("GPL");
  227. MODULE_ALIAS("platform:pxa2xx-ac97");