snd-n64.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Sound driver for Nintendo 64.
  4. *
  5. * Copyright 2021 Lauri Kasanen
  6. */
  7. #include <linux/dma-mapping.h>
  8. #include <linux/init.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/log2.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/string.h>
  16. #include <sound/control.h>
  17. #include <sound/core.h>
  18. #include <sound/initval.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. MODULE_AUTHOR("Lauri Kasanen <cand@gmx.com>");
  22. MODULE_DESCRIPTION("N64 Audio");
  23. MODULE_LICENSE("GPL");
  24. #define AI_NTSC_DACRATE 48681812
  25. #define AI_STATUS_BUSY (1 << 30)
  26. #define AI_STATUS_FULL (1 << 31)
  27. #define AI_ADDR_REG 0
  28. #define AI_LEN_REG 1
  29. #define AI_CONTROL_REG 2
  30. #define AI_STATUS_REG 3
  31. #define AI_RATE_REG 4
  32. #define AI_BITCLOCK_REG 5
  33. #define MI_INTR_REG 2
  34. #define MI_MASK_REG 3
  35. #define MI_INTR_AI 0x04
  36. #define MI_MASK_CLR_AI 0x0010
  37. #define MI_MASK_SET_AI 0x0020
  38. struct n64audio {
  39. u32 __iomem *ai_reg_base;
  40. u32 __iomem *mi_reg_base;
  41. void *ring_base;
  42. dma_addr_t ring_base_dma;
  43. struct snd_card *card;
  44. struct {
  45. struct snd_pcm_substream *substream;
  46. int pos, nextpos;
  47. u32 writesize;
  48. u32 bufsize;
  49. spinlock_t lock;
  50. } chan;
  51. };
  52. static void n64audio_write_reg(struct n64audio *priv, const u8 reg, const u32 value)
  53. {
  54. writel(value, priv->ai_reg_base + reg);
  55. }
  56. static void n64mi_write_reg(struct n64audio *priv, const u8 reg, const u32 value)
  57. {
  58. writel(value, priv->mi_reg_base + reg);
  59. }
  60. static u32 n64mi_read_reg(struct n64audio *priv, const u8 reg)
  61. {
  62. return readl(priv->mi_reg_base + reg);
  63. }
  64. static void n64audio_push(struct n64audio *priv)
  65. {
  66. struct snd_pcm_runtime *runtime = priv->chan.substream->runtime;
  67. u32 count;
  68. guard(spinlock_irqsave)(&priv->chan.lock);
  69. count = priv->chan.writesize;
  70. memcpy(priv->ring_base + priv->chan.nextpos,
  71. runtime->dma_area + priv->chan.nextpos, count);
  72. /*
  73. * The hw registers are double-buffered, and the IRQ fires essentially
  74. * one period behind. The core only allows one period's distance, so we
  75. * keep a private DMA buffer to afford two.
  76. */
  77. n64audio_write_reg(priv, AI_ADDR_REG, priv->ring_base_dma + priv->chan.nextpos);
  78. barrier();
  79. n64audio_write_reg(priv, AI_LEN_REG, count);
  80. priv->chan.nextpos += count;
  81. priv->chan.nextpos %= priv->chan.bufsize;
  82. runtime->delay = runtime->period_size;
  83. }
  84. static irqreturn_t n64audio_isr(int irq, void *dev_id)
  85. {
  86. struct n64audio *priv = dev_id;
  87. const u32 intrs = n64mi_read_reg(priv, MI_INTR_REG);
  88. // Check it's ours
  89. if (!(intrs & MI_INTR_AI))
  90. return IRQ_NONE;
  91. n64audio_write_reg(priv, AI_STATUS_REG, 1);
  92. if (priv->chan.substream && snd_pcm_running(priv->chan.substream)) {
  93. scoped_guard(spinlock_irqsave, &priv->chan.lock) {
  94. priv->chan.pos = priv->chan.nextpos;
  95. }
  96. snd_pcm_period_elapsed(priv->chan.substream);
  97. if (priv->chan.substream && snd_pcm_running(priv->chan.substream))
  98. n64audio_push(priv);
  99. }
  100. return IRQ_HANDLED;
  101. }
  102. static const struct snd_pcm_hardware n64audio_pcm_hw = {
  103. .info = (SNDRV_PCM_INFO_MMAP |
  104. SNDRV_PCM_INFO_MMAP_VALID |
  105. SNDRV_PCM_INFO_INTERLEAVED |
  106. SNDRV_PCM_INFO_BLOCK_TRANSFER),
  107. .formats = SNDRV_PCM_FMTBIT_S16_BE,
  108. .rates = SNDRV_PCM_RATE_8000_48000,
  109. .rate_min = 8000,
  110. .rate_max = 48000,
  111. .channels_min = 2,
  112. .channels_max = 2,
  113. .buffer_bytes_max = 32768,
  114. .period_bytes_min = 1024,
  115. .period_bytes_max = 32768,
  116. .periods_min = 3,
  117. // 3 periods lets the double-buffering hw read one buffer behind safely
  118. .periods_max = 128,
  119. };
  120. static int hw_rule_period_size(struct snd_pcm_hw_params *params,
  121. struct snd_pcm_hw_rule *rule)
  122. {
  123. struct snd_interval *c = hw_param_interval(params,
  124. SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
  125. int changed = 0;
  126. /*
  127. * The DMA unit has errata on (start + len) & 0x3fff == 0x2000.
  128. * This constraint makes sure that the period size is not a power of two,
  129. * which combined with dma_alloc_coherent aligning the buffer to the largest
  130. * PoT <= size guarantees it won't be hit.
  131. */
  132. if (is_power_of_2(c->min)) {
  133. c->min += 2;
  134. changed = 1;
  135. }
  136. if (is_power_of_2(c->max)) {
  137. c->max -= 2;
  138. changed = 1;
  139. }
  140. if (snd_interval_checkempty(c)) {
  141. c->empty = 1;
  142. return -EINVAL;
  143. }
  144. return changed;
  145. }
  146. static int n64audio_pcm_open(struct snd_pcm_substream *substream)
  147. {
  148. struct snd_pcm_runtime *runtime = substream->runtime;
  149. int err;
  150. runtime->hw = n64audio_pcm_hw;
  151. err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  152. if (err < 0)
  153. return err;
  154. err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
  155. if (err < 0)
  156. return err;
  157. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  158. hw_rule_period_size, NULL, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
  159. if (err < 0)
  160. return err;
  161. return 0;
  162. }
  163. static int n64audio_pcm_prepare(struct snd_pcm_substream *substream)
  164. {
  165. struct snd_pcm_runtime *runtime = substream->runtime;
  166. struct n64audio *priv = substream->pcm->private_data;
  167. u32 rate;
  168. rate = ((2 * AI_NTSC_DACRATE / runtime->rate) + 1) / 2 - 1;
  169. n64audio_write_reg(priv, AI_RATE_REG, rate);
  170. rate /= 66;
  171. if (rate > 16)
  172. rate = 16;
  173. n64audio_write_reg(priv, AI_BITCLOCK_REG, rate - 1);
  174. guard(spinlock_irq)(&priv->chan.lock);
  175. /* Setup the pseudo-dma transfer pointers. */
  176. priv->chan.pos = 0;
  177. priv->chan.nextpos = 0;
  178. priv->chan.substream = substream;
  179. priv->chan.writesize = snd_pcm_lib_period_bytes(substream);
  180. priv->chan.bufsize = snd_pcm_lib_buffer_bytes(substream);
  181. return 0;
  182. }
  183. static int n64audio_pcm_trigger(struct snd_pcm_substream *substream,
  184. int cmd)
  185. {
  186. struct n64audio *priv = substream->pcm->private_data;
  187. switch (cmd) {
  188. case SNDRV_PCM_TRIGGER_START:
  189. n64audio_push(substream->pcm->private_data);
  190. n64audio_write_reg(priv, AI_CONTROL_REG, 1);
  191. n64mi_write_reg(priv, MI_MASK_REG, MI_MASK_SET_AI);
  192. break;
  193. case SNDRV_PCM_TRIGGER_STOP:
  194. n64audio_write_reg(priv, AI_CONTROL_REG, 0);
  195. n64mi_write_reg(priv, MI_MASK_REG, MI_MASK_CLR_AI);
  196. break;
  197. default:
  198. return -EINVAL;
  199. }
  200. return 0;
  201. }
  202. static snd_pcm_uframes_t n64audio_pcm_pointer(struct snd_pcm_substream *substream)
  203. {
  204. struct n64audio *priv = substream->pcm->private_data;
  205. return bytes_to_frames(substream->runtime,
  206. priv->chan.pos);
  207. }
  208. static int n64audio_pcm_close(struct snd_pcm_substream *substream)
  209. {
  210. struct n64audio *priv = substream->pcm->private_data;
  211. priv->chan.substream = NULL;
  212. return 0;
  213. }
  214. static const struct snd_pcm_ops n64audio_pcm_ops = {
  215. .open = n64audio_pcm_open,
  216. .prepare = n64audio_pcm_prepare,
  217. .trigger = n64audio_pcm_trigger,
  218. .pointer = n64audio_pcm_pointer,
  219. .close = n64audio_pcm_close,
  220. };
  221. /*
  222. * The target device is embedded and RAM-constrained. We save RAM
  223. * by initializing in __init code that gets dropped late in boot.
  224. * For the same reason there is no module or unloading support.
  225. */
  226. static int __init n64audio_probe(struct platform_device *pdev)
  227. {
  228. struct snd_card *card;
  229. struct snd_pcm *pcm;
  230. struct n64audio *priv;
  231. int err, irq;
  232. err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1,
  233. SNDRV_DEFAULT_STR1,
  234. THIS_MODULE, sizeof(*priv), &card);
  235. if (err < 0)
  236. return err;
  237. priv = card->private_data;
  238. spin_lock_init(&priv->chan.lock);
  239. priv->card = card;
  240. priv->ring_base = dma_alloc_coherent(card->dev, 32 * 1024, &priv->ring_base_dma,
  241. GFP_DMA|GFP_KERNEL);
  242. if (!priv->ring_base) {
  243. err = -ENOMEM;
  244. goto fail_card;
  245. }
  246. priv->mi_reg_base = devm_platform_ioremap_resource(pdev, 0);
  247. if (IS_ERR(priv->mi_reg_base)) {
  248. err = PTR_ERR(priv->mi_reg_base);
  249. goto fail_dma_alloc;
  250. }
  251. priv->ai_reg_base = devm_platform_ioremap_resource(pdev, 1);
  252. if (IS_ERR(priv->ai_reg_base)) {
  253. err = PTR_ERR(priv->ai_reg_base);
  254. goto fail_dma_alloc;
  255. }
  256. err = snd_pcm_new(card, "N64 Audio", 0, 1, 0, &pcm);
  257. if (err < 0)
  258. goto fail_dma_alloc;
  259. pcm->private_data = priv;
  260. strscpy(pcm->name, "N64 Audio");
  261. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &n64audio_pcm_ops);
  262. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, card->dev, 0, 0);
  263. strscpy(card->driver, "N64 Audio");
  264. strscpy(card->shortname, "N64 Audio");
  265. strscpy(card->longname, "N64 Audio");
  266. irq = platform_get_irq(pdev, 0);
  267. if (irq < 0) {
  268. err = -EINVAL;
  269. goto fail_dma_alloc;
  270. }
  271. if (devm_request_irq(&pdev->dev, irq, n64audio_isr,
  272. IRQF_SHARED, "N64 Audio", priv)) {
  273. err = -EBUSY;
  274. goto fail_dma_alloc;
  275. }
  276. err = snd_card_register(card);
  277. if (err < 0)
  278. goto fail_dma_alloc;
  279. return 0;
  280. fail_dma_alloc:
  281. dma_free_coherent(card->dev, 32 * 1024, priv->ring_base, priv->ring_base_dma);
  282. fail_card:
  283. snd_card_free(card);
  284. return err;
  285. }
  286. static struct platform_driver n64audio_driver = {
  287. .driver = {
  288. .name = "n64audio",
  289. },
  290. };
  291. static int __init n64audio_init(void)
  292. {
  293. return platform_driver_probe(&n64audio_driver, n64audio_probe);
  294. }
  295. module_init(n64audio_init);