gus_pcm.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. * Routines for control of GF1 chip (PCM things)
  5. *
  6. * InterWave chips supports interleaved DMA, but this feature isn't used in
  7. * this code.
  8. *
  9. * This code emulates autoinit DMA transfer for playback, recording by GF1
  10. * chip doesn't support autoinit DMA.
  11. */
  12. #include <asm/dma.h>
  13. #include <linux/slab.h>
  14. #include <linux/sched/signal.h>
  15. #include <sound/core.h>
  16. #include <sound/control.h>
  17. #include <sound/gus.h>
  18. #include <sound/pcm_params.h>
  19. #include "gus_tables.h"
  20. /* maximum rate */
  21. #define SNDRV_GF1_PCM_RATE 48000
  22. #define SNDRV_GF1_PCM_PFLG_NONE 0
  23. #define SNDRV_GF1_PCM_PFLG_ACTIVE (1<<0)
  24. #define SNDRV_GF1_PCM_PFLG_NEUTRAL (2<<0)
  25. struct gus_pcm_private {
  26. struct snd_gus_card * gus;
  27. struct snd_pcm_substream *substream;
  28. spinlock_t lock;
  29. unsigned int voices;
  30. struct snd_gus_voice *pvoices[2];
  31. unsigned int memory;
  32. unsigned short flags;
  33. unsigned char voice_ctrl, ramp_ctrl;
  34. unsigned int bpos;
  35. unsigned int blocks;
  36. unsigned int block_size;
  37. unsigned int dma_size;
  38. wait_queue_head_t sleep;
  39. atomic_t dma_count;
  40. int final_volume;
  41. };
  42. static void snd_gf1_pcm_block_change_ack(struct snd_gus_card * gus, void *private_data)
  43. {
  44. struct gus_pcm_private *pcmp = private_data;
  45. if (pcmp) {
  46. atomic_dec(&pcmp->dma_count);
  47. wake_up(&pcmp->sleep);
  48. }
  49. }
  50. static int snd_gf1_pcm_block_change(struct snd_pcm_substream *substream,
  51. unsigned int offset,
  52. unsigned int addr,
  53. unsigned int count)
  54. {
  55. struct snd_gf1_dma_block block;
  56. struct snd_pcm_runtime *runtime = substream->runtime;
  57. struct gus_pcm_private *pcmp = runtime->private_data;
  58. count += offset & 31;
  59. offset &= ~31;
  60. memset(&block, 0, sizeof(block));
  61. block.cmd = SNDRV_GF1_DMA_IRQ;
  62. if (snd_pcm_format_unsigned(runtime->format))
  63. block.cmd |= SNDRV_GF1_DMA_UNSIGNED;
  64. if (snd_pcm_format_width(runtime->format) == 16)
  65. block.cmd |= SNDRV_GF1_DMA_16BIT;
  66. block.addr = addr & ~31;
  67. block.buffer = runtime->dma_area + offset;
  68. block.buf_addr = runtime->dma_addr + offset;
  69. block.count = count;
  70. block.private_data = pcmp;
  71. block.ack = snd_gf1_pcm_block_change_ack;
  72. if (!snd_gf1_dma_transfer_block(pcmp->gus, &block, 0, 0))
  73. atomic_inc(&pcmp->dma_count);
  74. return 0;
  75. }
  76. static void snd_gf1_pcm_trigger_up(struct snd_pcm_substream *substream)
  77. {
  78. struct snd_pcm_runtime *runtime = substream->runtime;
  79. struct gus_pcm_private *pcmp = runtime->private_data;
  80. struct snd_gus_card * gus = pcmp->gus;
  81. unsigned char voice_ctrl, ramp_ctrl;
  82. unsigned short rate;
  83. unsigned int curr, begin, end;
  84. unsigned short vol;
  85. unsigned char pan;
  86. unsigned int voice;
  87. scoped_guard(spinlock_irqsave, &pcmp->lock) {
  88. if (pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE)
  89. return;
  90. pcmp->flags |= SNDRV_GF1_PCM_PFLG_ACTIVE;
  91. pcmp->final_volume = 0;
  92. }
  93. rate = snd_gf1_translate_freq(gus, runtime->rate << 4);
  94. /* enable WAVE IRQ */
  95. voice_ctrl = snd_pcm_format_width(runtime->format) == 16 ? 0x24 : 0x20;
  96. /* enable RAMP IRQ + rollover */
  97. ramp_ctrl = 0x24;
  98. if (pcmp->blocks == 1) {
  99. voice_ctrl |= 0x08; /* loop enable */
  100. ramp_ctrl &= ~0x04; /* disable rollover */
  101. }
  102. for (voice = 0; voice < pcmp->voices; voice++) {
  103. begin = pcmp->memory + voice * (pcmp->dma_size / runtime->channels);
  104. curr = begin + (pcmp->bpos * pcmp->block_size) / runtime->channels;
  105. end = curr + (pcmp->block_size / runtime->channels);
  106. end -= snd_pcm_format_width(runtime->format) == 16 ? 2 : 1;
  107. pan = runtime->channels == 2 ? (!voice ? 1 : 14) : 8;
  108. vol = !voice ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
  109. guard(spinlock_irqsave)(&gus->reg_lock);
  110. snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number);
  111. snd_gf1_write8(gus, SNDRV_GF1_VB_PAN, pan);
  112. snd_gf1_write16(gus, SNDRV_GF1_VW_FREQUENCY, rate);
  113. snd_gf1_write_addr(gus, SNDRV_GF1_VA_START, begin << 4, voice_ctrl & 4);
  114. snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, end << 4, voice_ctrl & 4);
  115. snd_gf1_write_addr(gus, SNDRV_GF1_VA_CURRENT, curr << 4, voice_ctrl & 4);
  116. snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, SNDRV_GF1_MIN_VOLUME << 4);
  117. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_RATE, 0x2f);
  118. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_START, SNDRV_GF1_MIN_OFFSET);
  119. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_END, vol >> 8);
  120. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
  121. if (!gus->gf1.enh_mode) {
  122. snd_gf1_delay(gus);
  123. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
  124. }
  125. }
  126. guard(spinlock_irqsave)(&gus->reg_lock);
  127. for (voice = 0; voice < pcmp->voices; voice++) {
  128. snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number);
  129. if (gus->gf1.enh_mode)
  130. snd_gf1_write8(gus, SNDRV_GF1_VB_MODE, 0x00); /* deactivate voice */
  131. snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
  132. voice_ctrl &= ~0x20;
  133. }
  134. voice_ctrl |= 0x20;
  135. if (!gus->gf1.enh_mode) {
  136. snd_gf1_delay(gus);
  137. for (voice = 0; voice < pcmp->voices; voice++) {
  138. snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number);
  139. snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
  140. voice_ctrl &= ~0x20; /* disable IRQ for next voice */
  141. }
  142. }
  143. }
  144. static void snd_gf1_pcm_interrupt_wave(struct snd_gus_card * gus,
  145. struct snd_gus_voice *pvoice)
  146. {
  147. struct gus_pcm_private * pcmp;
  148. struct snd_pcm_runtime *runtime;
  149. unsigned char voice_ctrl, ramp_ctrl;
  150. unsigned int idx;
  151. unsigned int end, step;
  152. if (!pvoice->private_data) {
  153. dev_dbg(gus->card->dev, "%s: unknown wave irq?\n", __func__);
  154. snd_gf1_smart_stop_voice(gus, pvoice->number);
  155. return;
  156. }
  157. pcmp = pvoice->private_data;
  158. if (pcmp == NULL) {
  159. dev_dbg(gus->card->dev, "%s: unknown wave irq?\n", __func__);
  160. snd_gf1_smart_stop_voice(gus, pvoice->number);
  161. return;
  162. }
  163. gus = pcmp->gus;
  164. runtime = pcmp->substream->runtime;
  165. scoped_guard(spinlock, &gus->reg_lock) {
  166. snd_gf1_select_voice(gus, pvoice->number);
  167. voice_ctrl = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL) & ~0x8b;
  168. ramp_ctrl = (snd_gf1_read8(gus, SNDRV_GF1_VB_VOLUME_CONTROL) & ~0xa4) | 0x03;
  169. #if 0
  170. snd_gf1_select_voice(gus, pvoice->number);
  171. dev_dbg(gus->card->dev, "position = 0x%x\n",
  172. (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4));
  173. snd_gf1_select_voice(gus, pcmp->pvoices[1]->number);
  174. dev_dbg(gus->card->dev, "position = 0x%x\n",
  175. (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4));
  176. snd_gf1_select_voice(gus, pvoice->number);
  177. #endif
  178. pcmp->bpos++;
  179. pcmp->bpos %= pcmp->blocks;
  180. if (pcmp->bpos + 1 >= pcmp->blocks) { /* last block? */
  181. voice_ctrl |= 0x08; /* enable loop */
  182. } else {
  183. ramp_ctrl |= 0x04; /* enable rollover */
  184. }
  185. end = pcmp->memory + (((pcmp->bpos + 1) * pcmp->block_size) / runtime->channels);
  186. end -= voice_ctrl & 4 ? 2 : 1;
  187. step = pcmp->dma_size / runtime->channels;
  188. voice_ctrl |= 0x20;
  189. if (!pcmp->final_volume) {
  190. ramp_ctrl |= 0x20;
  191. ramp_ctrl &= ~0x03;
  192. }
  193. for (idx = 0; idx < pcmp->voices; idx++, end += step) {
  194. snd_gf1_select_voice(gus, pcmp->pvoices[idx]->number);
  195. snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, end << 4, voice_ctrl & 4);
  196. snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
  197. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
  198. voice_ctrl &= ~0x20;
  199. }
  200. if (!gus->gf1.enh_mode) {
  201. snd_gf1_delay(gus);
  202. voice_ctrl |= 0x20;
  203. for (idx = 0; idx < pcmp->voices; idx++) {
  204. snd_gf1_select_voice(gus, pcmp->pvoices[idx]->number);
  205. snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl);
  206. snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl);
  207. voice_ctrl &= ~0x20;
  208. }
  209. }
  210. }
  211. snd_pcm_period_elapsed(pcmp->substream);
  212. #if 0
  213. if ((runtime->flags & SNDRV_PCM_FLG_MMAP) &&
  214. *runtime->state == SNDRV_PCM_STATE_RUNNING) {
  215. end = pcmp->bpos * pcmp->block_size;
  216. if (runtime->channels > 1) {
  217. snd_gf1_pcm_block_change(pcmp->substream, end, pcmp->memory + (end / 2), pcmp->block_size / 2);
  218. snd_gf1_pcm_block_change(pcmp->substream, end + (pcmp->block_size / 2), pcmp->memory + (pcmp->dma_size / 2) + (end / 2), pcmp->block_size / 2);
  219. } else {
  220. snd_gf1_pcm_block_change(pcmp->substream, end, pcmp->memory + end, pcmp->block_size);
  221. }
  222. }
  223. #endif
  224. }
  225. static void snd_gf1_pcm_interrupt_volume(struct snd_gus_card * gus,
  226. struct snd_gus_voice * pvoice)
  227. {
  228. unsigned short vol;
  229. int cvoice;
  230. struct gus_pcm_private *pcmp = pvoice->private_data;
  231. /* stop ramp, but leave rollover bit untouched */
  232. scoped_guard(spinlock, &gus->reg_lock) {
  233. snd_gf1_select_voice(gus, pvoice->number);
  234. snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
  235. }
  236. if (pcmp == NULL)
  237. return;
  238. /* are we active? */
  239. if (!(pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE))
  240. return;
  241. /* load real volume - better precision */
  242. cvoice = pcmp->pvoices[0] == pvoice ? 0 : 1;
  243. if (pcmp->substream == NULL)
  244. return;
  245. vol = !cvoice ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
  246. guard(spinlock)(&gus->reg_lock);
  247. snd_gf1_select_voice(gus, pvoice->number);
  248. snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, vol);
  249. pcmp->final_volume = 1;
  250. }
  251. static void snd_gf1_pcm_volume_change(struct snd_gus_card * gus)
  252. {
  253. }
  254. static int snd_gf1_pcm_poke_block(struct snd_gus_card *gus, unsigned char *buf,
  255. unsigned int pos, unsigned int count,
  256. int w16, int invert)
  257. {
  258. unsigned int len;
  259. while (count > 0) {
  260. len = count;
  261. if (len > 512) /* limit, to allow IRQ */
  262. len = 512;
  263. count -= len;
  264. if (gus->interwave) {
  265. guard(spinlock_irqsave)(&gus->reg_lock);
  266. snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01 | (invert ? 0x08 : 0x00));
  267. snd_gf1_dram_addr(gus, pos);
  268. if (w16) {
  269. outb(SNDRV_GF1_GW_DRAM_IO16, GUSP(gus, GF1REGSEL));
  270. outsw(GUSP(gus, GF1DATALOW), buf, len >> 1);
  271. } else {
  272. outsb(GUSP(gus, DRAM), buf, len);
  273. }
  274. buf += 512;
  275. pos += 512;
  276. } else {
  277. invert = invert ? 0x80 : 0x00;
  278. if (w16) {
  279. len >>= 1;
  280. while (len--) {
  281. snd_gf1_poke(gus, pos++, *buf++);
  282. snd_gf1_poke(gus, pos++, *buf++ ^ invert);
  283. }
  284. } else {
  285. while (len--)
  286. snd_gf1_poke(gus, pos++, *buf++ ^ invert);
  287. }
  288. }
  289. if (count > 0 && !in_interrupt()) {
  290. schedule_timeout_interruptible(1);
  291. if (signal_pending(current))
  292. return -EAGAIN;
  293. }
  294. }
  295. return 0;
  296. }
  297. static int get_bpos(struct gus_pcm_private *pcmp, int voice, unsigned int pos,
  298. unsigned int len)
  299. {
  300. unsigned int bpos = pos + (voice * (pcmp->dma_size / 2));
  301. if (snd_BUG_ON(bpos > pcmp->dma_size))
  302. return -EIO;
  303. if (snd_BUG_ON(bpos + len > pcmp->dma_size))
  304. return -EIO;
  305. return bpos;
  306. }
  307. static int playback_copy_ack(struct snd_pcm_substream *substream,
  308. unsigned int bpos, unsigned int len)
  309. {
  310. struct snd_pcm_runtime *runtime = substream->runtime;
  311. struct gus_pcm_private *pcmp = runtime->private_data;
  312. struct snd_gus_card *gus = pcmp->gus;
  313. int w16, invert;
  314. if (len > 32)
  315. return snd_gf1_pcm_block_change(substream, bpos,
  316. pcmp->memory + bpos, len);
  317. w16 = (snd_pcm_format_width(runtime->format) == 16);
  318. invert = snd_pcm_format_unsigned(runtime->format);
  319. return snd_gf1_pcm_poke_block(gus, runtime->dma_area + bpos,
  320. pcmp->memory + bpos, len, w16, invert);
  321. }
  322. static int snd_gf1_pcm_playback_copy(struct snd_pcm_substream *substream,
  323. int voice, unsigned long pos,
  324. struct iov_iter *src, unsigned long count)
  325. {
  326. struct snd_pcm_runtime *runtime = substream->runtime;
  327. struct gus_pcm_private *pcmp = runtime->private_data;
  328. unsigned int len = count;
  329. int bpos;
  330. bpos = get_bpos(pcmp, voice, pos, len);
  331. if (bpos < 0)
  332. return bpos;
  333. if (copy_from_iter(runtime->dma_area + bpos, len, src) != len)
  334. return -EFAULT;
  335. return playback_copy_ack(substream, bpos, len);
  336. }
  337. static int snd_gf1_pcm_playback_silence(struct snd_pcm_substream *substream,
  338. int voice, unsigned long pos,
  339. unsigned long count)
  340. {
  341. struct snd_pcm_runtime *runtime = substream->runtime;
  342. struct gus_pcm_private *pcmp = runtime->private_data;
  343. unsigned int len = count;
  344. int bpos;
  345. bpos = get_bpos(pcmp, voice, pos, len);
  346. if (bpos < 0)
  347. return bpos;
  348. snd_pcm_format_set_silence(runtime->format, runtime->dma_area + bpos,
  349. bytes_to_samples(runtime, count));
  350. return playback_copy_ack(substream, bpos, len);
  351. }
  352. static int snd_gf1_pcm_playback_hw_params(struct snd_pcm_substream *substream,
  353. struct snd_pcm_hw_params *hw_params)
  354. {
  355. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  356. struct snd_pcm_runtime *runtime = substream->runtime;
  357. struct gus_pcm_private *pcmp = runtime->private_data;
  358. if (runtime->buffer_changed) {
  359. struct snd_gf1_mem_block *block;
  360. if (pcmp->memory > 0) {
  361. snd_gf1_mem_free(&gus->gf1.mem_alloc, pcmp->memory);
  362. pcmp->memory = 0;
  363. }
  364. block = snd_gf1_mem_alloc(&gus->gf1.mem_alloc,
  365. SNDRV_GF1_MEM_OWNER_DRIVER,
  366. "GF1 PCM",
  367. runtime->dma_bytes, 1, 32,
  368. NULL);
  369. if (!block)
  370. return -ENOMEM;
  371. pcmp->memory = block->ptr;
  372. }
  373. pcmp->voices = params_channels(hw_params);
  374. if (pcmp->pvoices[0] == NULL) {
  375. pcmp->pvoices[0] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0);
  376. if (!pcmp->pvoices[0])
  377. return -ENOMEM;
  378. pcmp->pvoices[0]->handler_wave = snd_gf1_pcm_interrupt_wave;
  379. pcmp->pvoices[0]->handler_volume = snd_gf1_pcm_interrupt_volume;
  380. pcmp->pvoices[0]->volume_change = snd_gf1_pcm_volume_change;
  381. pcmp->pvoices[0]->private_data = pcmp;
  382. }
  383. if (pcmp->voices > 1 && pcmp->pvoices[1] == NULL) {
  384. pcmp->pvoices[1] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0);
  385. if (!pcmp->pvoices[1])
  386. return -ENOMEM;
  387. pcmp->pvoices[1]->handler_wave = snd_gf1_pcm_interrupt_wave;
  388. pcmp->pvoices[1]->handler_volume = snd_gf1_pcm_interrupt_volume;
  389. pcmp->pvoices[1]->volume_change = snd_gf1_pcm_volume_change;
  390. pcmp->pvoices[1]->private_data = pcmp;
  391. } else if (pcmp->voices == 1) {
  392. if (pcmp->pvoices[1]) {
  393. snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]);
  394. pcmp->pvoices[1] = NULL;
  395. }
  396. }
  397. return 0;
  398. }
  399. static int snd_gf1_pcm_playback_hw_free(struct snd_pcm_substream *substream)
  400. {
  401. struct snd_pcm_runtime *runtime = substream->runtime;
  402. struct gus_pcm_private *pcmp = runtime->private_data;
  403. if (pcmp->pvoices[0]) {
  404. snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[0]);
  405. pcmp->pvoices[0] = NULL;
  406. }
  407. if (pcmp->pvoices[1]) {
  408. snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]);
  409. pcmp->pvoices[1] = NULL;
  410. }
  411. if (pcmp->memory > 0) {
  412. snd_gf1_mem_free(&pcmp->gus->gf1.mem_alloc, pcmp->memory);
  413. pcmp->memory = 0;
  414. }
  415. return 0;
  416. }
  417. static int snd_gf1_pcm_playback_prepare(struct snd_pcm_substream *substream)
  418. {
  419. struct snd_pcm_runtime *runtime = substream->runtime;
  420. struct gus_pcm_private *pcmp = runtime->private_data;
  421. pcmp->bpos = 0;
  422. pcmp->dma_size = snd_pcm_lib_buffer_bytes(substream);
  423. pcmp->block_size = snd_pcm_lib_period_bytes(substream);
  424. pcmp->blocks = pcmp->dma_size / pcmp->block_size;
  425. return 0;
  426. }
  427. static int snd_gf1_pcm_playback_trigger(struct snd_pcm_substream *substream,
  428. int cmd)
  429. {
  430. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  431. struct snd_pcm_runtime *runtime = substream->runtime;
  432. struct gus_pcm_private *pcmp = runtime->private_data;
  433. int voice;
  434. if (cmd == SNDRV_PCM_TRIGGER_START) {
  435. snd_gf1_pcm_trigger_up(substream);
  436. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  437. scoped_guard(spinlock, &pcmp->lock) {
  438. pcmp->flags &= ~SNDRV_GF1_PCM_PFLG_ACTIVE;
  439. }
  440. voice = pcmp->pvoices[0]->number;
  441. snd_gf1_stop_voices(gus, voice, voice);
  442. if (pcmp->pvoices[1]) {
  443. voice = pcmp->pvoices[1]->number;
  444. snd_gf1_stop_voices(gus, voice, voice);
  445. }
  446. } else {
  447. return -EINVAL;
  448. }
  449. return 0;
  450. }
  451. static snd_pcm_uframes_t snd_gf1_pcm_playback_pointer(struct snd_pcm_substream *substream)
  452. {
  453. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  454. struct snd_pcm_runtime *runtime = substream->runtime;
  455. struct gus_pcm_private *pcmp = runtime->private_data;
  456. unsigned int pos;
  457. unsigned char voice_ctrl;
  458. pos = 0;
  459. guard(spinlock)(&gus->reg_lock);
  460. if (pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE) {
  461. snd_gf1_select_voice(gus, pcmp->pvoices[0]->number);
  462. voice_ctrl = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);
  463. pos = (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4) - pcmp->memory;
  464. if (substream->runtime->channels > 1)
  465. pos <<= 1;
  466. pos = bytes_to_frames(runtime, pos);
  467. }
  468. return pos;
  469. }
  470. static const struct snd_ratnum clock = {
  471. .num = 9878400/16,
  472. .den_min = 2,
  473. .den_max = 257,
  474. .den_step = 1,
  475. };
  476. static const struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
  477. .nrats = 1,
  478. .rats = &clock,
  479. };
  480. static int snd_gf1_pcm_capture_hw_params(struct snd_pcm_substream *substream,
  481. struct snd_pcm_hw_params *hw_params)
  482. {
  483. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  484. gus->c_dma_size = params_buffer_bytes(hw_params);
  485. gus->c_period_size = params_period_bytes(hw_params);
  486. gus->c_pos = 0;
  487. gus->gf1.pcm_rcntrl_reg = 0x21; /* IRQ at end, enable & start */
  488. if (params_channels(hw_params) > 1)
  489. gus->gf1.pcm_rcntrl_reg |= 2;
  490. if (gus->gf1.dma2 > 3)
  491. gus->gf1.pcm_rcntrl_reg |= 4;
  492. if (snd_pcm_format_unsigned(params_format(hw_params)))
  493. gus->gf1.pcm_rcntrl_reg |= 0x80;
  494. return 0;
  495. }
  496. static int snd_gf1_pcm_capture_prepare(struct snd_pcm_substream *substream)
  497. {
  498. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  499. struct snd_pcm_runtime *runtime = substream->runtime;
  500. snd_gf1_i_write8(gus, SNDRV_GF1_GB_RECORD_RATE, runtime->rate_den - 2);
  501. snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0); /* disable sampling */
  502. snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL); /* Sampling Control Register */
  503. snd_dma_program(gus->gf1.dma2, runtime->dma_addr, gus->c_period_size, DMA_MODE_READ);
  504. return 0;
  505. }
  506. static int snd_gf1_pcm_capture_trigger(struct snd_pcm_substream *substream,
  507. int cmd)
  508. {
  509. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  510. int val;
  511. if (cmd == SNDRV_PCM_TRIGGER_START) {
  512. val = gus->gf1.pcm_rcntrl_reg;
  513. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  514. val = 0;
  515. } else {
  516. return -EINVAL;
  517. }
  518. guard(spinlock)(&gus->reg_lock);
  519. snd_gf1_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, val);
  520. snd_gf1_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL);
  521. return 0;
  522. }
  523. static snd_pcm_uframes_t snd_gf1_pcm_capture_pointer(struct snd_pcm_substream *substream)
  524. {
  525. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  526. int pos = snd_dma_pointer(gus->gf1.dma2, gus->c_period_size);
  527. pos = bytes_to_frames(substream->runtime, (gus->c_pos + pos) % gus->c_dma_size);
  528. return pos;
  529. }
  530. static void snd_gf1_pcm_interrupt_dma_read(struct snd_gus_card * gus)
  531. {
  532. snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0); /* disable sampling */
  533. snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL); /* Sampling Control Register */
  534. if (gus->pcm_cap_substream != NULL) {
  535. snd_gf1_pcm_capture_prepare(gus->pcm_cap_substream);
  536. snd_gf1_pcm_capture_trigger(gus->pcm_cap_substream, SNDRV_PCM_TRIGGER_START);
  537. gus->c_pos += gus->c_period_size;
  538. snd_pcm_period_elapsed(gus->pcm_cap_substream);
  539. }
  540. }
  541. static const struct snd_pcm_hardware snd_gf1_pcm_playback =
  542. {
  543. .info = SNDRV_PCM_INFO_NONINTERLEAVED,
  544. .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  545. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
  546. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  547. .rate_min = 5510,
  548. .rate_max = 48000,
  549. .channels_min = 1,
  550. .channels_max = 2,
  551. .buffer_bytes_max = (128*1024),
  552. .period_bytes_min = 64,
  553. .period_bytes_max = (128*1024),
  554. .periods_min = 1,
  555. .periods_max = 1024,
  556. .fifo_size = 0,
  557. };
  558. static const struct snd_pcm_hardware snd_gf1_pcm_capture =
  559. {
  560. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  561. SNDRV_PCM_INFO_MMAP_VALID),
  562. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8,
  563. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100,
  564. .rate_min = 5510,
  565. .rate_max = 44100,
  566. .channels_min = 1,
  567. .channels_max = 2,
  568. .buffer_bytes_max = (128*1024),
  569. .period_bytes_min = 64,
  570. .period_bytes_max = (128*1024),
  571. .periods_min = 1,
  572. .periods_max = 1024,
  573. .fifo_size = 0,
  574. };
  575. static void snd_gf1_pcm_playback_free(struct snd_pcm_runtime *runtime)
  576. {
  577. kfree(runtime->private_data);
  578. }
  579. static int snd_gf1_pcm_playback_open(struct snd_pcm_substream *substream)
  580. {
  581. struct gus_pcm_private *pcmp;
  582. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  583. struct snd_pcm_runtime *runtime = substream->runtime;
  584. int err;
  585. pcmp = kzalloc_obj(*pcmp);
  586. if (pcmp == NULL)
  587. return -ENOMEM;
  588. pcmp->gus = gus;
  589. spin_lock_init(&pcmp->lock);
  590. init_waitqueue_head(&pcmp->sleep);
  591. atomic_set(&pcmp->dma_count, 0);
  592. runtime->private_data = pcmp;
  593. runtime->private_free = snd_gf1_pcm_playback_free;
  594. #if 0
  595. dev_dbg(gus->card->dev,
  596. "playback.buffer = 0x%lx, gf1.pcm_buffer = 0x%lx\n",
  597. (long) pcm->playback.buffer, (long) gus->gf1.pcm_buffer);
  598. #endif
  599. err = snd_gf1_dma_init(gus);
  600. if (err < 0)
  601. return err;
  602. pcmp->flags = SNDRV_GF1_PCM_PFLG_NONE;
  603. pcmp->substream = substream;
  604. runtime->hw = snd_gf1_pcm_playback;
  605. snd_pcm_limit_isa_dma_size(gus->gf1.dma1, &runtime->hw.buffer_bytes_max);
  606. snd_pcm_limit_isa_dma_size(gus->gf1.dma1, &runtime->hw.period_bytes_max);
  607. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
  608. return 0;
  609. }
  610. static int snd_gf1_pcm_playback_close(struct snd_pcm_substream *substream)
  611. {
  612. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  613. struct snd_pcm_runtime *runtime = substream->runtime;
  614. struct gus_pcm_private *pcmp = runtime->private_data;
  615. if (!wait_event_timeout(pcmp->sleep, (atomic_read(&pcmp->dma_count) <= 0), 2*HZ))
  616. dev_err(gus->card->dev, "gf1 pcm - serious DMA problem\n");
  617. snd_gf1_dma_done(gus);
  618. return 0;
  619. }
  620. static int snd_gf1_pcm_capture_open(struct snd_pcm_substream *substream)
  621. {
  622. struct snd_pcm_runtime *runtime = substream->runtime;
  623. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  624. gus->gf1.interrupt_handler_dma_read = snd_gf1_pcm_interrupt_dma_read;
  625. gus->pcm_cap_substream = substream;
  626. substream->runtime->hw = snd_gf1_pcm_capture;
  627. snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.buffer_bytes_max);
  628. snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.period_bytes_max);
  629. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  630. &hw_constraints_clocks);
  631. return 0;
  632. }
  633. static int snd_gf1_pcm_capture_close(struct snd_pcm_substream *substream)
  634. {
  635. struct snd_gus_card *gus = snd_pcm_substream_chip(substream);
  636. gus->pcm_cap_substream = NULL;
  637. snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_DMA_READ);
  638. return 0;
  639. }
  640. static int snd_gf1_pcm_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  641. {
  642. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  643. uinfo->count = 2;
  644. uinfo->value.integer.min = 0;
  645. uinfo->value.integer.max = 127;
  646. return 0;
  647. }
  648. static int snd_gf1_pcm_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  649. {
  650. struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
  651. guard(spinlock_irqsave)(&gus->pcm_volume_level_lock);
  652. ucontrol->value.integer.value[0] = gus->gf1.pcm_volume_level_left1;
  653. ucontrol->value.integer.value[1] = gus->gf1.pcm_volume_level_right1;
  654. return 0;
  655. }
  656. static int snd_gf1_pcm_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  657. {
  658. struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
  659. int change;
  660. unsigned int idx;
  661. unsigned short val1, val2, vol;
  662. struct gus_pcm_private *pcmp;
  663. struct snd_gus_voice *pvoice;
  664. val1 = ucontrol->value.integer.value[0] & 127;
  665. val2 = ucontrol->value.integer.value[1] & 127;
  666. scoped_guard(spinlock_irqsave, &gus->pcm_volume_level_lock) {
  667. change = val1 != gus->gf1.pcm_volume_level_left1 ||
  668. val2 != gus->gf1.pcm_volume_level_right1;
  669. gus->gf1.pcm_volume_level_left1 = val1;
  670. gus->gf1.pcm_volume_level_right1 = val2;
  671. gus->gf1.pcm_volume_level_left = snd_gf1_lvol_to_gvol_raw(val1 << 9) << 4;
  672. gus->gf1.pcm_volume_level_right = snd_gf1_lvol_to_gvol_raw(val2 << 9) << 4;
  673. }
  674. /* are we active? */
  675. scoped_guard(spinlock_irqsave, &gus->voice_alloc) {
  676. for (idx = 0; idx < 32; idx++) {
  677. pvoice = &gus->gf1.voices[idx];
  678. if (!pvoice->pcm)
  679. continue;
  680. pcmp = pvoice->private_data;
  681. if (!(pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE))
  682. continue;
  683. /* load real volume - better precision */
  684. guard(spinlock)(&gus->reg_lock);
  685. snd_gf1_select_voice(gus, pvoice->number);
  686. snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
  687. vol = pvoice == pcmp->pvoices[0] ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right;
  688. snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, vol);
  689. pcmp->final_volume = 1;
  690. }
  691. }
  692. return change;
  693. }
  694. static const struct snd_kcontrol_new snd_gf1_pcm_volume_control =
  695. {
  696. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  697. .name = "PCM Playback Volume",
  698. .info = snd_gf1_pcm_volume_info,
  699. .get = snd_gf1_pcm_volume_get,
  700. .put = snd_gf1_pcm_volume_put
  701. };
  702. static const struct snd_kcontrol_new snd_gf1_pcm_volume_control1 =
  703. {
  704. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  705. .name = "GPCM Playback Volume",
  706. .info = snd_gf1_pcm_volume_info,
  707. .get = snd_gf1_pcm_volume_get,
  708. .put = snd_gf1_pcm_volume_put
  709. };
  710. static const struct snd_pcm_ops snd_gf1_pcm_playback_ops = {
  711. .open = snd_gf1_pcm_playback_open,
  712. .close = snd_gf1_pcm_playback_close,
  713. .hw_params = snd_gf1_pcm_playback_hw_params,
  714. .hw_free = snd_gf1_pcm_playback_hw_free,
  715. .prepare = snd_gf1_pcm_playback_prepare,
  716. .trigger = snd_gf1_pcm_playback_trigger,
  717. .pointer = snd_gf1_pcm_playback_pointer,
  718. .copy = snd_gf1_pcm_playback_copy,
  719. .fill_silence = snd_gf1_pcm_playback_silence,
  720. };
  721. static const struct snd_pcm_ops snd_gf1_pcm_capture_ops = {
  722. .open = snd_gf1_pcm_capture_open,
  723. .close = snd_gf1_pcm_capture_close,
  724. .hw_params = snd_gf1_pcm_capture_hw_params,
  725. .prepare = snd_gf1_pcm_capture_prepare,
  726. .trigger = snd_gf1_pcm_capture_trigger,
  727. .pointer = snd_gf1_pcm_capture_pointer,
  728. };
  729. int snd_gf1_pcm_new(struct snd_gus_card *gus, int pcm_dev, int control_index)
  730. {
  731. struct snd_card *card;
  732. struct snd_kcontrol *kctl;
  733. struct snd_pcm *pcm;
  734. struct snd_pcm_substream *substream;
  735. int capture, err;
  736. card = gus->card;
  737. capture = !gus->interwave && !gus->ess_flag && !gus->ace_flag ? 1 : 0;
  738. err = snd_pcm_new(card,
  739. gus->interwave ? "AMD InterWave" : "GF1",
  740. pcm_dev,
  741. gus->gf1.pcm_channels / 2,
  742. capture,
  743. &pcm);
  744. if (err < 0)
  745. return err;
  746. pcm->private_data = gus;
  747. /* playback setup */
  748. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_gf1_pcm_playback_ops);
  749. for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
  750. snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV,
  751. card->dev,
  752. 64*1024, gus->gf1.dma1 > 3 ? 128*1024 : 64*1024);
  753. pcm->info_flags = 0;
  754. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  755. if (capture) {
  756. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_gf1_pcm_capture_ops);
  757. if (gus->gf1.dma2 == gus->gf1.dma1)
  758. pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
  759. snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  760. SNDRV_DMA_TYPE_DEV, card->dev,
  761. 64*1024, gus->gf1.dma2 > 3 ? 128*1024 : 64*1024);
  762. }
  763. strscpy(pcm->name, pcm->id);
  764. if (gus->interwave) {
  765. sprintf(pcm->name + strlen(pcm->name), " rev %c", gus->revision + 'A');
  766. }
  767. strcat(pcm->name, " (synth)");
  768. gus->pcm = pcm;
  769. if (gus->codec_flag)
  770. kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control1, gus);
  771. else
  772. kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control, gus);
  773. kctl->id.index = control_index;
  774. err = snd_ctl_add(card, kctl);
  775. if (err < 0)
  776. return err;
  777. return 0;
  778. }