pdaudiocf_pcm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Sound Core PDAudioCF soundcards
  4. *
  5. * PCM part
  6. *
  7. * Copyright (c) 2003 by Jaroslav Kysela <[email protected]>
  8. */
  9. #include <linux/delay.h>
  10. #include <sound/core.h>
  11. #include <sound/asoundef.h>
  12. #include "pdaudiocf.h"
  13. /*
  14. * clear the SRAM contents
  15. */
  16. static int pdacf_pcm_clear_sram(struct snd_pdacf *chip)
  17. {
  18. int max_loop = 64 * 1024;
  19. while (inw(chip->port + PDAUDIOCF_REG_RDP) != inw(chip->port + PDAUDIOCF_REG_WDP)) {
  20. if (max_loop-- < 0)
  21. return -EIO;
  22. inw(chip->port + PDAUDIOCF_REG_MD);
  23. }
  24. return 0;
  25. }
  26. /*
  27. * pdacf_pcm_trigger - trigger callback for capture
  28. */
  29. static int pdacf_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
  30. {
  31. struct snd_pdacf *chip = snd_pcm_substream_chip(subs);
  32. struct snd_pcm_runtime *runtime = subs->runtime;
  33. int inc, ret = 0, rate;
  34. unsigned short mask, val, tmp;
  35. if (chip->chip_status & PDAUDIOCF_STAT_IS_STALE)
  36. return -EBUSY;
  37. switch (cmd) {
  38. case SNDRV_PCM_TRIGGER_START:
  39. chip->pcm_hwptr = 0;
  40. chip->pcm_tdone = 0;
  41. fallthrough;
  42. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  43. case SNDRV_PCM_TRIGGER_RESUME:
  44. mask = 0;
  45. val = PDAUDIOCF_RECORD;
  46. inc = 1;
  47. rate = snd_ak4117_check_rate_and_errors(chip->ak4117, AK4117_CHECK_NO_STAT|AK4117_CHECK_NO_RATE);
  48. break;
  49. case SNDRV_PCM_TRIGGER_STOP:
  50. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  51. case SNDRV_PCM_TRIGGER_SUSPEND:
  52. mask = PDAUDIOCF_RECORD;
  53. val = 0;
  54. inc = -1;
  55. rate = 0;
  56. break;
  57. default:
  58. return -EINVAL;
  59. }
  60. mutex_lock(&chip->reg_lock);
  61. chip->pcm_running += inc;
  62. tmp = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);
  63. if (chip->pcm_running) {
  64. if ((chip->ak4117->rcs0 & AK4117_UNLCK) || runtime->rate != rate) {
  65. chip->pcm_running -= inc;
  66. ret = -EIO;
  67. goto __end;
  68. }
  69. }
  70. tmp &= ~mask;
  71. tmp |= val;
  72. pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, tmp);
  73. __end:
  74. mutex_unlock(&chip->reg_lock);
  75. snd_ak4117_check_rate_and_errors(chip->ak4117, AK4117_CHECK_NO_RATE);
  76. return ret;
  77. }
  78. /*
  79. * pdacf_pcm_prepare - prepare callback for playback and capture
  80. */
  81. static int pdacf_pcm_prepare(struct snd_pcm_substream *subs)
  82. {
  83. struct snd_pdacf *chip = snd_pcm_substream_chip(subs);
  84. struct snd_pcm_runtime *runtime = subs->runtime;
  85. u16 val, nval, aval;
  86. if (chip->chip_status & PDAUDIOCF_STAT_IS_STALE)
  87. return -EBUSY;
  88. chip->pcm_channels = runtime->channels;
  89. chip->pcm_little = snd_pcm_format_little_endian(runtime->format) > 0;
  90. #ifdef SNDRV_LITTLE_ENDIAN
  91. chip->pcm_swab = snd_pcm_format_big_endian(runtime->format) > 0;
  92. #else
  93. chip->pcm_swab = chip->pcm_little;
  94. #endif
  95. if (snd_pcm_format_unsigned(runtime->format))
  96. chip->pcm_xor = 0x80008000;
  97. if (pdacf_pcm_clear_sram(chip) < 0)
  98. return -EIO;
  99. val = nval = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);
  100. nval &= ~(PDAUDIOCF_DATAFMT0|PDAUDIOCF_DATAFMT1);
  101. switch (runtime->format) {
  102. case SNDRV_PCM_FORMAT_S16_LE:
  103. case SNDRV_PCM_FORMAT_S16_BE:
  104. break;
  105. default: /* 24-bit */
  106. nval |= PDAUDIOCF_DATAFMT0 | PDAUDIOCF_DATAFMT1;
  107. break;
  108. }
  109. aval = 0;
  110. chip->pcm_sample = 4;
  111. switch (runtime->format) {
  112. case SNDRV_PCM_FORMAT_S16_LE:
  113. case SNDRV_PCM_FORMAT_S16_BE:
  114. aval = AK4117_DIF_16R;
  115. chip->pcm_frame = 2;
  116. chip->pcm_sample = 2;
  117. break;
  118. case SNDRV_PCM_FORMAT_S24_3LE:
  119. case SNDRV_PCM_FORMAT_S24_3BE:
  120. chip->pcm_sample = 3;
  121. fallthrough;
  122. default: /* 24-bit */
  123. aval = AK4117_DIF_24R;
  124. chip->pcm_frame = 3;
  125. chip->pcm_xor &= 0xffff0000;
  126. break;
  127. }
  128. if (val != nval) {
  129. snd_ak4117_reg_write(chip->ak4117, AK4117_REG_IO, AK4117_DIF2|AK4117_DIF1|AK4117_DIF0, aval);
  130. pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, nval);
  131. }
  132. val = pdacf_reg_read(chip, PDAUDIOCF_REG_IER);
  133. val &= ~(PDAUDIOCF_IRQLVLEN1);
  134. val |= PDAUDIOCF_IRQLVLEN0;
  135. pdacf_reg_write(chip, PDAUDIOCF_REG_IER, val);
  136. chip->pcm_size = runtime->buffer_size;
  137. chip->pcm_period = runtime->period_size;
  138. chip->pcm_area = runtime->dma_area;
  139. return 0;
  140. }
  141. /*
  142. * capture hw information
  143. */
  144. static const struct snd_pcm_hardware pdacf_pcm_capture_hw = {
  145. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  146. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
  147. SNDRV_PCM_INFO_MMAP_VALID |
  148. SNDRV_PCM_INFO_BATCH),
  149. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  150. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
  151. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
  152. .rates = SNDRV_PCM_RATE_32000 |
  153. SNDRV_PCM_RATE_44100 |
  154. SNDRV_PCM_RATE_48000 |
  155. SNDRV_PCM_RATE_88200 |
  156. SNDRV_PCM_RATE_96000 |
  157. SNDRV_PCM_RATE_176400 |
  158. SNDRV_PCM_RATE_192000,
  159. .rate_min = 32000,
  160. .rate_max = 192000,
  161. .channels_min = 1,
  162. .channels_max = 2,
  163. .buffer_bytes_max = (512*1024),
  164. .period_bytes_min = 8*1024,
  165. .period_bytes_max = (64*1024),
  166. .periods_min = 2,
  167. .periods_max = 128,
  168. .fifo_size = 0,
  169. };
  170. /*
  171. * pdacf_pcm_capture_open - open callback for capture
  172. */
  173. static int pdacf_pcm_capture_open(struct snd_pcm_substream *subs)
  174. {
  175. struct snd_pcm_runtime *runtime = subs->runtime;
  176. struct snd_pdacf *chip = snd_pcm_substream_chip(subs);
  177. if (chip->chip_status & PDAUDIOCF_STAT_IS_STALE)
  178. return -EBUSY;
  179. runtime->hw = pdacf_pcm_capture_hw;
  180. runtime->private_data = chip;
  181. chip->pcm_substream = subs;
  182. return 0;
  183. }
  184. /*
  185. * pdacf_pcm_capture_close - close callback for capture
  186. */
  187. static int pdacf_pcm_capture_close(struct snd_pcm_substream *subs)
  188. {
  189. struct snd_pdacf *chip = snd_pcm_substream_chip(subs);
  190. if (!chip)
  191. return -EINVAL;
  192. pdacf_reinit(chip, 0);
  193. chip->pcm_substream = NULL;
  194. return 0;
  195. }
  196. /*
  197. * pdacf_pcm_capture_pointer - pointer callback for capture
  198. */
  199. static snd_pcm_uframes_t pdacf_pcm_capture_pointer(struct snd_pcm_substream *subs)
  200. {
  201. struct snd_pdacf *chip = snd_pcm_substream_chip(subs);
  202. return chip->pcm_hwptr;
  203. }
  204. /*
  205. * operators for PCM capture
  206. */
  207. static const struct snd_pcm_ops pdacf_pcm_capture_ops = {
  208. .open = pdacf_pcm_capture_open,
  209. .close = pdacf_pcm_capture_close,
  210. .prepare = pdacf_pcm_prepare,
  211. .trigger = pdacf_pcm_trigger,
  212. .pointer = pdacf_pcm_capture_pointer,
  213. };
  214. /*
  215. * snd_pdacf_pcm_new - create and initialize a pcm
  216. */
  217. int snd_pdacf_pcm_new(struct snd_pdacf *chip)
  218. {
  219. struct snd_pcm *pcm;
  220. int err;
  221. err = snd_pcm_new(chip->card, "PDAudioCF", 0, 0, 1, &pcm);
  222. if (err < 0)
  223. return err;
  224. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pdacf_pcm_capture_ops);
  225. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL,
  226. 0, 0);
  227. pcm->private_data = chip;
  228. pcm->info_flags = 0;
  229. pcm->nonatomic = true;
  230. strcpy(pcm->name, chip->card->shortname);
  231. chip->pcm = pcm;
  232. err = snd_ak4117_build(chip->ak4117, pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
  233. if (err < 0)
  234. return err;
  235. return 0;
  236. }