snd-n64.c 8.8 KB

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