rme32.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  1. /*
  2. * ALSA driver for RME Digi32, Digi32/8 and Digi32 PRO audio interfaces
  3. *
  4. * Copyright (c) 2002-2004 Martin Langer <martin-langer@gmx.de>,
  5. * Pilo Chambert <pilo.c@wanadoo.fr>
  6. *
  7. * Thanks to : Anders Torger <torger@ludd.luth.se>,
  8. * Henk Hesselink <henk@anda.nl>
  9. * for writing the digi96-driver
  10. * and RME for all informations.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * ****************************************************************************
  28. *
  29. * Note #1 "Sek'd models" ................................... martin 2002-12-07
  30. *
  31. * Identical soundcards by Sek'd were labeled:
  32. * RME Digi 32 = Sek'd Prodif 32
  33. * RME Digi 32 Pro = Sek'd Prodif 96
  34. * RME Digi 32/8 = Sek'd Prodif Gold
  35. *
  36. * ****************************************************************************
  37. *
  38. * Note #2 "full duplex mode" ............................... martin 2002-12-07
  39. *
  40. * Full duplex doesn't work. All cards (32, 32/8, 32Pro) are working identical
  41. * in this mode. Rec data and play data are using the same buffer therefore. At
  42. * first you have got the playing bits in the buffer and then (after playing
  43. * them) they were overwitten by the captured sound of the CS8412/14. Both
  44. * modes (play/record) are running harmonically hand in hand in the same buffer
  45. * and you have only one start bit plus one interrupt bit to control this
  46. * paired action.
  47. * This is opposite to the latter rme96 where playing and capturing is totally
  48. * separated and so their full duplex mode is supported by alsa (using two
  49. * start bits and two interrupts for two different buffers).
  50. * But due to the wrong sequence of playing and capturing ALSA shows no solved
  51. * full duplex support for the rme32 at the moment. That's bad, but I'm not
  52. * able to solve it. Are you motivated enough to solve this problem now? Your
  53. * patch would be welcome!
  54. *
  55. * ****************************************************************************
  56. *
  57. * "The story after the long seeking" -- tiwai
  58. *
  59. * Ok, the situation regarding the full duplex is now improved a bit.
  60. * In the fullduplex mode (given by the module parameter), the hardware buffer
  61. * is split to halves for read and write directions at the DMA pointer.
  62. * That is, the half above the current DMA pointer is used for write, and
  63. * the half below is used for read. To mangle this strange behavior, an
  64. * software intermediate buffer is introduced. This is, of course, not good
  65. * from the viewpoint of the data transfer efficiency. However, this allows
  66. * you to use arbitrary buffer sizes, instead of the fixed I/O buffer size.
  67. *
  68. * ****************************************************************************
  69. */
  70. #include <linux/delay.h>
  71. #include <linux/gfp.h>
  72. #include <linux/init.h>
  73. #include <linux/interrupt.h>
  74. #include <linux/pci.h>
  75. #include <linux/module.h>
  76. #include <linux/io.h>
  77. #include <sound/core.h>
  78. #include <sound/info.h>
  79. #include <sound/control.h>
  80. #include <sound/pcm.h>
  81. #include <sound/pcm_params.h>
  82. #include <sound/pcm-indirect.h>
  83. #include <sound/asoundef.h>
  84. #include <sound/initval.h>
  85. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  86. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  87. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  88. static bool fullduplex[SNDRV_CARDS]; // = {[0 ... (SNDRV_CARDS - 1)] = 1};
  89. module_param_array(index, int, NULL, 0444);
  90. MODULE_PARM_DESC(index, "Index value for RME Digi32 soundcard.");
  91. module_param_array(id, charp, NULL, 0444);
  92. MODULE_PARM_DESC(id, "ID string for RME Digi32 soundcard.");
  93. module_param_array(enable, bool, NULL, 0444);
  94. MODULE_PARM_DESC(enable, "Enable RME Digi32 soundcard.");
  95. module_param_array(fullduplex, bool, NULL, 0444);
  96. MODULE_PARM_DESC(fullduplex, "Support full-duplex mode.");
  97. MODULE_AUTHOR("Martin Langer <martin-langer@gmx.de>, Pilo Chambert <pilo.c@wanadoo.fr>");
  98. MODULE_DESCRIPTION("RME Digi32, Digi32/8, Digi32 PRO");
  99. MODULE_LICENSE("GPL");
  100. MODULE_SUPPORTED_DEVICE("{{RME,Digi32}," "{RME,Digi32/8}," "{RME,Digi32 PRO}}");
  101. /* Defines for RME Digi32 series */
  102. #define RME32_SPDIF_NCHANNELS 2
  103. /* Playback and capture buffer size */
  104. #define RME32_BUFFER_SIZE 0x20000
  105. /* IO area size */
  106. #define RME32_IO_SIZE 0x30000
  107. /* IO area offsets */
  108. #define RME32_IO_DATA_BUFFER 0x0
  109. #define RME32_IO_CONTROL_REGISTER 0x20000
  110. #define RME32_IO_GET_POS 0x20000
  111. #define RME32_IO_CONFIRM_ACTION_IRQ 0x20004
  112. #define RME32_IO_RESET_POS 0x20100
  113. /* Write control register bits */
  114. #define RME32_WCR_START (1 << 0) /* startbit */
  115. #define RME32_WCR_MONO (1 << 1) /* 0=stereo, 1=mono
  116. Setting the whole card to mono
  117. doesn't seem to be very useful.
  118. A software-solution can handle
  119. full-duplex with one direction in
  120. stereo and the other way in mono.
  121. So, the hardware should work all
  122. the time in stereo! */
  123. #define RME32_WCR_MODE24 (1 << 2) /* 0=16bit, 1=32bit */
  124. #define RME32_WCR_SEL (1 << 3) /* 0=input on output, 1=normal playback/capture */
  125. #define RME32_WCR_FREQ_0 (1 << 4) /* frequency (play) */
  126. #define RME32_WCR_FREQ_1 (1 << 5)
  127. #define RME32_WCR_INP_0 (1 << 6) /* input switch */
  128. #define RME32_WCR_INP_1 (1 << 7)
  129. #define RME32_WCR_RESET (1 << 8) /* Reset address */
  130. #define RME32_WCR_MUTE (1 << 9) /* digital mute for output */
  131. #define RME32_WCR_PRO (1 << 10) /* 1=professional, 0=consumer */
  132. #define RME32_WCR_DS_BM (1 << 11) /* 1=DoubleSpeed (only PRO-Version); 1=BlockMode (only Adat-Version) */
  133. #define RME32_WCR_ADAT (1 << 12) /* Adat Mode (only Adat-Version) */
  134. #define RME32_WCR_AUTOSYNC (1 << 13) /* AutoSync */
  135. #define RME32_WCR_PD (1 << 14) /* DAC Reset (only PRO-Version) */
  136. #define RME32_WCR_EMP (1 << 15) /* 1=Emphasis on (only PRO-Version) */
  137. #define RME32_WCR_BITPOS_FREQ_0 4
  138. #define RME32_WCR_BITPOS_FREQ_1 5
  139. #define RME32_WCR_BITPOS_INP_0 6
  140. #define RME32_WCR_BITPOS_INP_1 7
  141. /* Read control register bits */
  142. #define RME32_RCR_AUDIO_ADDR_MASK 0x1ffff
  143. #define RME32_RCR_LOCK (1 << 23) /* 1=locked, 0=not locked */
  144. #define RME32_RCR_ERF (1 << 26) /* 1=Error, 0=no Error */
  145. #define RME32_RCR_FREQ_0 (1 << 27) /* CS841x frequency (record) */
  146. #define RME32_RCR_FREQ_1 (1 << 28)
  147. #define RME32_RCR_FREQ_2 (1 << 29)
  148. #define RME32_RCR_KMODE (1 << 30) /* card mode: 1=PLL, 0=quartz */
  149. #define RME32_RCR_IRQ (1 << 31) /* interrupt */
  150. #define RME32_RCR_BITPOS_F0 27
  151. #define RME32_RCR_BITPOS_F1 28
  152. #define RME32_RCR_BITPOS_F2 29
  153. /* Input types */
  154. #define RME32_INPUT_OPTICAL 0
  155. #define RME32_INPUT_COAXIAL 1
  156. #define RME32_INPUT_INTERNAL 2
  157. #define RME32_INPUT_XLR 3
  158. /* Clock modes */
  159. #define RME32_CLOCKMODE_SLAVE 0
  160. #define RME32_CLOCKMODE_MASTER_32 1
  161. #define RME32_CLOCKMODE_MASTER_44 2
  162. #define RME32_CLOCKMODE_MASTER_48 3
  163. /* Block sizes in bytes */
  164. #define RME32_BLOCK_SIZE 8192
  165. /* Software intermediate buffer (max) size */
  166. #define RME32_MID_BUFFER_SIZE (1024*1024)
  167. /* Hardware revisions */
  168. #define RME32_32_REVISION 192
  169. #define RME32_328_REVISION_OLD 100
  170. #define RME32_328_REVISION_NEW 101
  171. #define RME32_PRO_REVISION_WITH_8412 192
  172. #define RME32_PRO_REVISION_WITH_8414 150
  173. struct rme32 {
  174. spinlock_t lock;
  175. int irq;
  176. unsigned long port;
  177. void __iomem *iobase;
  178. u32 wcreg; /* cached write control register value */
  179. u32 wcreg_spdif; /* S/PDIF setup */
  180. u32 wcreg_spdif_stream; /* S/PDIF setup (temporary) */
  181. u32 rcreg; /* cached read control register value */
  182. u8 rev; /* card revision number */
  183. struct snd_pcm_substream *playback_substream;
  184. struct snd_pcm_substream *capture_substream;
  185. int playback_frlog; /* log2 of framesize */
  186. int capture_frlog;
  187. size_t playback_periodsize; /* in bytes, zero if not used */
  188. size_t capture_periodsize; /* in bytes, zero if not used */
  189. unsigned int fullduplex_mode;
  190. int running;
  191. struct snd_pcm_indirect playback_pcm;
  192. struct snd_pcm_indirect capture_pcm;
  193. struct snd_card *card;
  194. struct snd_pcm *spdif_pcm;
  195. struct snd_pcm *adat_pcm;
  196. struct pci_dev *pci;
  197. struct snd_kcontrol *spdif_ctl;
  198. };
  199. static const struct pci_device_id snd_rme32_ids[] = {
  200. {PCI_VDEVICE(XILINX_RME, PCI_DEVICE_ID_RME_DIGI32), 0,},
  201. {PCI_VDEVICE(XILINX_RME, PCI_DEVICE_ID_RME_DIGI32_8), 0,},
  202. {PCI_VDEVICE(XILINX_RME, PCI_DEVICE_ID_RME_DIGI32_PRO), 0,},
  203. {0,}
  204. };
  205. MODULE_DEVICE_TABLE(pci, snd_rme32_ids);
  206. #define RME32_ISWORKING(rme32) ((rme32)->wcreg & RME32_WCR_START)
  207. #define RME32_PRO_WITH_8414(rme32) ((rme32)->pci->device == PCI_DEVICE_ID_RME_DIGI32_PRO && (rme32)->rev == RME32_PRO_REVISION_WITH_8414)
  208. static int snd_rme32_playback_prepare(struct snd_pcm_substream *substream);
  209. static int snd_rme32_capture_prepare(struct snd_pcm_substream *substream);
  210. static int snd_rme32_pcm_trigger(struct snd_pcm_substream *substream, int cmd);
  211. static void snd_rme32_proc_init(struct rme32 * rme32);
  212. static int snd_rme32_create_switches(struct snd_card *card, struct rme32 * rme32);
  213. static inline unsigned int snd_rme32_pcm_byteptr(struct rme32 * rme32)
  214. {
  215. return (readl(rme32->iobase + RME32_IO_GET_POS)
  216. & RME32_RCR_AUDIO_ADDR_MASK);
  217. }
  218. /* silence callback for halfduplex mode */
  219. static int snd_rme32_playback_silence(struct snd_pcm_substream *substream, int channel, /* not used (interleaved data) */
  220. snd_pcm_uframes_t pos,
  221. snd_pcm_uframes_t count)
  222. {
  223. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  224. count <<= rme32->playback_frlog;
  225. pos <<= rme32->playback_frlog;
  226. memset_io(rme32->iobase + RME32_IO_DATA_BUFFER + pos, 0, count);
  227. return 0;
  228. }
  229. /* copy callback for halfduplex mode */
  230. static int snd_rme32_playback_copy(struct snd_pcm_substream *substream, int channel, /* not used (interleaved data) */
  231. snd_pcm_uframes_t pos,
  232. void __user *src, snd_pcm_uframes_t count)
  233. {
  234. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  235. count <<= rme32->playback_frlog;
  236. pos <<= rme32->playback_frlog;
  237. if (copy_from_user_toio(rme32->iobase + RME32_IO_DATA_BUFFER + pos,
  238. src, count))
  239. return -EFAULT;
  240. return 0;
  241. }
  242. /* copy callback for halfduplex mode */
  243. static int snd_rme32_capture_copy(struct snd_pcm_substream *substream, int channel, /* not used (interleaved data) */
  244. snd_pcm_uframes_t pos,
  245. void __user *dst, snd_pcm_uframes_t count)
  246. {
  247. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  248. count <<= rme32->capture_frlog;
  249. pos <<= rme32->capture_frlog;
  250. if (copy_to_user_fromio(dst,
  251. rme32->iobase + RME32_IO_DATA_BUFFER + pos,
  252. count))
  253. return -EFAULT;
  254. return 0;
  255. }
  256. /*
  257. * SPDIF I/O capabilities (half-duplex mode)
  258. */
  259. static struct snd_pcm_hardware snd_rme32_spdif_info = {
  260. .info = (SNDRV_PCM_INFO_MMAP_IOMEM |
  261. SNDRV_PCM_INFO_MMAP_VALID |
  262. SNDRV_PCM_INFO_INTERLEAVED |
  263. SNDRV_PCM_INFO_PAUSE |
  264. SNDRV_PCM_INFO_SYNC_START),
  265. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  266. SNDRV_PCM_FMTBIT_S32_LE),
  267. .rates = (SNDRV_PCM_RATE_32000 |
  268. SNDRV_PCM_RATE_44100 |
  269. SNDRV_PCM_RATE_48000),
  270. .rate_min = 32000,
  271. .rate_max = 48000,
  272. .channels_min = 2,
  273. .channels_max = 2,
  274. .buffer_bytes_max = RME32_BUFFER_SIZE,
  275. .period_bytes_min = RME32_BLOCK_SIZE,
  276. .period_bytes_max = RME32_BLOCK_SIZE,
  277. .periods_min = RME32_BUFFER_SIZE / RME32_BLOCK_SIZE,
  278. .periods_max = RME32_BUFFER_SIZE / RME32_BLOCK_SIZE,
  279. .fifo_size = 0,
  280. };
  281. /*
  282. * ADAT I/O capabilities (half-duplex mode)
  283. */
  284. static struct snd_pcm_hardware snd_rme32_adat_info =
  285. {
  286. .info = (SNDRV_PCM_INFO_MMAP_IOMEM |
  287. SNDRV_PCM_INFO_MMAP_VALID |
  288. SNDRV_PCM_INFO_INTERLEAVED |
  289. SNDRV_PCM_INFO_PAUSE |
  290. SNDRV_PCM_INFO_SYNC_START),
  291. .formats= SNDRV_PCM_FMTBIT_S16_LE,
  292. .rates = (SNDRV_PCM_RATE_44100 |
  293. SNDRV_PCM_RATE_48000),
  294. .rate_min = 44100,
  295. .rate_max = 48000,
  296. .channels_min = 8,
  297. .channels_max = 8,
  298. .buffer_bytes_max = RME32_BUFFER_SIZE,
  299. .period_bytes_min = RME32_BLOCK_SIZE,
  300. .period_bytes_max = RME32_BLOCK_SIZE,
  301. .periods_min = RME32_BUFFER_SIZE / RME32_BLOCK_SIZE,
  302. .periods_max = RME32_BUFFER_SIZE / RME32_BLOCK_SIZE,
  303. .fifo_size = 0,
  304. };
  305. /*
  306. * SPDIF I/O capabilities (full-duplex mode)
  307. */
  308. static struct snd_pcm_hardware snd_rme32_spdif_fd_info = {
  309. .info = (SNDRV_PCM_INFO_MMAP |
  310. SNDRV_PCM_INFO_MMAP_VALID |
  311. SNDRV_PCM_INFO_INTERLEAVED |
  312. SNDRV_PCM_INFO_PAUSE |
  313. SNDRV_PCM_INFO_SYNC_START),
  314. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  315. SNDRV_PCM_FMTBIT_S32_LE),
  316. .rates = (SNDRV_PCM_RATE_32000 |
  317. SNDRV_PCM_RATE_44100 |
  318. SNDRV_PCM_RATE_48000),
  319. .rate_min = 32000,
  320. .rate_max = 48000,
  321. .channels_min = 2,
  322. .channels_max = 2,
  323. .buffer_bytes_max = RME32_MID_BUFFER_SIZE,
  324. .period_bytes_min = RME32_BLOCK_SIZE,
  325. .period_bytes_max = RME32_BLOCK_SIZE,
  326. .periods_min = 2,
  327. .periods_max = RME32_MID_BUFFER_SIZE / RME32_BLOCK_SIZE,
  328. .fifo_size = 0,
  329. };
  330. /*
  331. * ADAT I/O capabilities (full-duplex mode)
  332. */
  333. static struct snd_pcm_hardware snd_rme32_adat_fd_info =
  334. {
  335. .info = (SNDRV_PCM_INFO_MMAP |
  336. SNDRV_PCM_INFO_MMAP_VALID |
  337. SNDRV_PCM_INFO_INTERLEAVED |
  338. SNDRV_PCM_INFO_PAUSE |
  339. SNDRV_PCM_INFO_SYNC_START),
  340. .formats= SNDRV_PCM_FMTBIT_S16_LE,
  341. .rates = (SNDRV_PCM_RATE_44100 |
  342. SNDRV_PCM_RATE_48000),
  343. .rate_min = 44100,
  344. .rate_max = 48000,
  345. .channels_min = 8,
  346. .channels_max = 8,
  347. .buffer_bytes_max = RME32_MID_BUFFER_SIZE,
  348. .period_bytes_min = RME32_BLOCK_SIZE,
  349. .period_bytes_max = RME32_BLOCK_SIZE,
  350. .periods_min = 2,
  351. .periods_max = RME32_MID_BUFFER_SIZE / RME32_BLOCK_SIZE,
  352. .fifo_size = 0,
  353. };
  354. static void snd_rme32_reset_dac(struct rme32 *rme32)
  355. {
  356. writel(rme32->wcreg | RME32_WCR_PD,
  357. rme32->iobase + RME32_IO_CONTROL_REGISTER);
  358. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  359. }
  360. static int snd_rme32_playback_getrate(struct rme32 * rme32)
  361. {
  362. int rate;
  363. rate = ((rme32->wcreg >> RME32_WCR_BITPOS_FREQ_0) & 1) +
  364. (((rme32->wcreg >> RME32_WCR_BITPOS_FREQ_1) & 1) << 1);
  365. switch (rate) {
  366. case 1:
  367. rate = 32000;
  368. break;
  369. case 2:
  370. rate = 44100;
  371. break;
  372. case 3:
  373. rate = 48000;
  374. break;
  375. default:
  376. return -1;
  377. }
  378. return (rme32->wcreg & RME32_WCR_DS_BM) ? rate << 1 : rate;
  379. }
  380. static int snd_rme32_capture_getrate(struct rme32 * rme32, int *is_adat)
  381. {
  382. int n;
  383. *is_adat = 0;
  384. if (rme32->rcreg & RME32_RCR_LOCK) {
  385. /* ADAT rate */
  386. *is_adat = 1;
  387. }
  388. if (rme32->rcreg & RME32_RCR_ERF) {
  389. return -1;
  390. }
  391. /* S/PDIF rate */
  392. n = ((rme32->rcreg >> RME32_RCR_BITPOS_F0) & 1) +
  393. (((rme32->rcreg >> RME32_RCR_BITPOS_F1) & 1) << 1) +
  394. (((rme32->rcreg >> RME32_RCR_BITPOS_F2) & 1) << 2);
  395. if (RME32_PRO_WITH_8414(rme32))
  396. switch (n) { /* supporting the CS8414 */
  397. case 0:
  398. case 1:
  399. case 2:
  400. return -1;
  401. case 3:
  402. return 96000;
  403. case 4:
  404. return 88200;
  405. case 5:
  406. return 48000;
  407. case 6:
  408. return 44100;
  409. case 7:
  410. return 32000;
  411. default:
  412. return -1;
  413. break;
  414. }
  415. else
  416. switch (n) { /* supporting the CS8412 */
  417. case 0:
  418. return -1;
  419. case 1:
  420. return 48000;
  421. case 2:
  422. return 44100;
  423. case 3:
  424. return 32000;
  425. case 4:
  426. return 48000;
  427. case 5:
  428. return 44100;
  429. case 6:
  430. return 44056;
  431. case 7:
  432. return 32000;
  433. default:
  434. break;
  435. }
  436. return -1;
  437. }
  438. static int snd_rme32_playback_setrate(struct rme32 * rme32, int rate)
  439. {
  440. int ds;
  441. ds = rme32->wcreg & RME32_WCR_DS_BM;
  442. switch (rate) {
  443. case 32000:
  444. rme32->wcreg &= ~RME32_WCR_DS_BM;
  445. rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) &
  446. ~RME32_WCR_FREQ_1;
  447. break;
  448. case 44100:
  449. rme32->wcreg &= ~RME32_WCR_DS_BM;
  450. rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_1) &
  451. ~RME32_WCR_FREQ_0;
  452. break;
  453. case 48000:
  454. rme32->wcreg &= ~RME32_WCR_DS_BM;
  455. rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) |
  456. RME32_WCR_FREQ_1;
  457. break;
  458. case 64000:
  459. if (rme32->pci->device != PCI_DEVICE_ID_RME_DIGI32_PRO)
  460. return -EINVAL;
  461. rme32->wcreg |= RME32_WCR_DS_BM;
  462. rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) &
  463. ~RME32_WCR_FREQ_1;
  464. break;
  465. case 88200:
  466. if (rme32->pci->device != PCI_DEVICE_ID_RME_DIGI32_PRO)
  467. return -EINVAL;
  468. rme32->wcreg |= RME32_WCR_DS_BM;
  469. rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_1) &
  470. ~RME32_WCR_FREQ_0;
  471. break;
  472. case 96000:
  473. if (rme32->pci->device != PCI_DEVICE_ID_RME_DIGI32_PRO)
  474. return -EINVAL;
  475. rme32->wcreg |= RME32_WCR_DS_BM;
  476. rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) |
  477. RME32_WCR_FREQ_1;
  478. break;
  479. default:
  480. return -EINVAL;
  481. }
  482. if ((!ds && rme32->wcreg & RME32_WCR_DS_BM) ||
  483. (ds && !(rme32->wcreg & RME32_WCR_DS_BM)))
  484. {
  485. /* change to/from double-speed: reset the DAC (if available) */
  486. snd_rme32_reset_dac(rme32);
  487. } else {
  488. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  489. }
  490. return 0;
  491. }
  492. static int snd_rme32_setclockmode(struct rme32 * rme32, int mode)
  493. {
  494. switch (mode) {
  495. case RME32_CLOCKMODE_SLAVE:
  496. /* AutoSync */
  497. rme32->wcreg = (rme32->wcreg & ~RME32_WCR_FREQ_0) &
  498. ~RME32_WCR_FREQ_1;
  499. break;
  500. case RME32_CLOCKMODE_MASTER_32:
  501. /* Internal 32.0kHz */
  502. rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) &
  503. ~RME32_WCR_FREQ_1;
  504. break;
  505. case RME32_CLOCKMODE_MASTER_44:
  506. /* Internal 44.1kHz */
  507. rme32->wcreg = (rme32->wcreg & ~RME32_WCR_FREQ_0) |
  508. RME32_WCR_FREQ_1;
  509. break;
  510. case RME32_CLOCKMODE_MASTER_48:
  511. /* Internal 48.0kHz */
  512. rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) |
  513. RME32_WCR_FREQ_1;
  514. break;
  515. default:
  516. return -EINVAL;
  517. }
  518. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  519. return 0;
  520. }
  521. static int snd_rme32_getclockmode(struct rme32 * rme32)
  522. {
  523. return ((rme32->wcreg >> RME32_WCR_BITPOS_FREQ_0) & 1) +
  524. (((rme32->wcreg >> RME32_WCR_BITPOS_FREQ_1) & 1) << 1);
  525. }
  526. static int snd_rme32_setinputtype(struct rme32 * rme32, int type)
  527. {
  528. switch (type) {
  529. case RME32_INPUT_OPTICAL:
  530. rme32->wcreg = (rme32->wcreg & ~RME32_WCR_INP_0) &
  531. ~RME32_WCR_INP_1;
  532. break;
  533. case RME32_INPUT_COAXIAL:
  534. rme32->wcreg = (rme32->wcreg | RME32_WCR_INP_0) &
  535. ~RME32_WCR_INP_1;
  536. break;
  537. case RME32_INPUT_INTERNAL:
  538. rme32->wcreg = (rme32->wcreg & ~RME32_WCR_INP_0) |
  539. RME32_WCR_INP_1;
  540. break;
  541. case RME32_INPUT_XLR:
  542. rme32->wcreg = (rme32->wcreg | RME32_WCR_INP_0) |
  543. RME32_WCR_INP_1;
  544. break;
  545. default:
  546. return -EINVAL;
  547. }
  548. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  549. return 0;
  550. }
  551. static int snd_rme32_getinputtype(struct rme32 * rme32)
  552. {
  553. return ((rme32->wcreg >> RME32_WCR_BITPOS_INP_0) & 1) +
  554. (((rme32->wcreg >> RME32_WCR_BITPOS_INP_1) & 1) << 1);
  555. }
  556. static void
  557. snd_rme32_setframelog(struct rme32 * rme32, int n_channels, int is_playback)
  558. {
  559. int frlog;
  560. if (n_channels == 2) {
  561. frlog = 1;
  562. } else {
  563. /* assume 8 channels */
  564. frlog = 3;
  565. }
  566. if (is_playback) {
  567. frlog += (rme32->wcreg & RME32_WCR_MODE24) ? 2 : 1;
  568. rme32->playback_frlog = frlog;
  569. } else {
  570. frlog += (rme32->wcreg & RME32_WCR_MODE24) ? 2 : 1;
  571. rme32->capture_frlog = frlog;
  572. }
  573. }
  574. static int snd_rme32_setformat(struct rme32 *rme32, snd_pcm_format_t format)
  575. {
  576. switch (format) {
  577. case SNDRV_PCM_FORMAT_S16_LE:
  578. rme32->wcreg &= ~RME32_WCR_MODE24;
  579. break;
  580. case SNDRV_PCM_FORMAT_S32_LE:
  581. rme32->wcreg |= RME32_WCR_MODE24;
  582. break;
  583. default:
  584. return -EINVAL;
  585. }
  586. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  587. return 0;
  588. }
  589. static int
  590. snd_rme32_playback_hw_params(struct snd_pcm_substream *substream,
  591. struct snd_pcm_hw_params *params)
  592. {
  593. int err, rate, dummy;
  594. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  595. struct snd_pcm_runtime *runtime = substream->runtime;
  596. if (rme32->fullduplex_mode) {
  597. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  598. if (err < 0)
  599. return err;
  600. } else {
  601. runtime->dma_area = (void __force *)(rme32->iobase +
  602. RME32_IO_DATA_BUFFER);
  603. runtime->dma_addr = rme32->port + RME32_IO_DATA_BUFFER;
  604. runtime->dma_bytes = RME32_BUFFER_SIZE;
  605. }
  606. spin_lock_irq(&rme32->lock);
  607. if ((rme32->rcreg & RME32_RCR_KMODE) &&
  608. (rate = snd_rme32_capture_getrate(rme32, &dummy)) > 0) {
  609. /* AutoSync */
  610. if ((int)params_rate(params) != rate) {
  611. spin_unlock_irq(&rme32->lock);
  612. return -EIO;
  613. }
  614. } else if ((err = snd_rme32_playback_setrate(rme32, params_rate(params))) < 0) {
  615. spin_unlock_irq(&rme32->lock);
  616. return err;
  617. }
  618. if ((err = snd_rme32_setformat(rme32, params_format(params))) < 0) {
  619. spin_unlock_irq(&rme32->lock);
  620. return err;
  621. }
  622. snd_rme32_setframelog(rme32, params_channels(params), 1);
  623. if (rme32->capture_periodsize != 0) {
  624. if (params_period_size(params) << rme32->playback_frlog != rme32->capture_periodsize) {
  625. spin_unlock_irq(&rme32->lock);
  626. return -EBUSY;
  627. }
  628. }
  629. rme32->playback_periodsize = params_period_size(params) << rme32->playback_frlog;
  630. /* S/PDIF setup */
  631. if ((rme32->wcreg & RME32_WCR_ADAT) == 0) {
  632. rme32->wcreg &= ~(RME32_WCR_PRO | RME32_WCR_EMP);
  633. rme32->wcreg |= rme32->wcreg_spdif_stream;
  634. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  635. }
  636. spin_unlock_irq(&rme32->lock);
  637. return 0;
  638. }
  639. static int
  640. snd_rme32_capture_hw_params(struct snd_pcm_substream *substream,
  641. struct snd_pcm_hw_params *params)
  642. {
  643. int err, isadat, rate;
  644. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  645. struct snd_pcm_runtime *runtime = substream->runtime;
  646. if (rme32->fullduplex_mode) {
  647. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  648. if (err < 0)
  649. return err;
  650. } else {
  651. runtime->dma_area = (void __force *)rme32->iobase +
  652. RME32_IO_DATA_BUFFER;
  653. runtime->dma_addr = rme32->port + RME32_IO_DATA_BUFFER;
  654. runtime->dma_bytes = RME32_BUFFER_SIZE;
  655. }
  656. spin_lock_irq(&rme32->lock);
  657. /* enable AutoSync for record-preparing */
  658. rme32->wcreg |= RME32_WCR_AUTOSYNC;
  659. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  660. if ((err = snd_rme32_setformat(rme32, params_format(params))) < 0) {
  661. spin_unlock_irq(&rme32->lock);
  662. return err;
  663. }
  664. if ((err = snd_rme32_playback_setrate(rme32, params_rate(params))) < 0) {
  665. spin_unlock_irq(&rme32->lock);
  666. return err;
  667. }
  668. if ((rate = snd_rme32_capture_getrate(rme32, &isadat)) > 0) {
  669. if ((int)params_rate(params) != rate) {
  670. spin_unlock_irq(&rme32->lock);
  671. return -EIO;
  672. }
  673. if ((isadat && runtime->hw.channels_min == 2) ||
  674. (!isadat && runtime->hw.channels_min == 8)) {
  675. spin_unlock_irq(&rme32->lock);
  676. return -EIO;
  677. }
  678. }
  679. /* AutoSync off for recording */
  680. rme32->wcreg &= ~RME32_WCR_AUTOSYNC;
  681. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  682. snd_rme32_setframelog(rme32, params_channels(params), 0);
  683. if (rme32->playback_periodsize != 0) {
  684. if (params_period_size(params) << rme32->capture_frlog !=
  685. rme32->playback_periodsize) {
  686. spin_unlock_irq(&rme32->lock);
  687. return -EBUSY;
  688. }
  689. }
  690. rme32->capture_periodsize =
  691. params_period_size(params) << rme32->capture_frlog;
  692. spin_unlock_irq(&rme32->lock);
  693. return 0;
  694. }
  695. static int snd_rme32_pcm_hw_free(struct snd_pcm_substream *substream)
  696. {
  697. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  698. if (! rme32->fullduplex_mode)
  699. return 0;
  700. return snd_pcm_lib_free_pages(substream);
  701. }
  702. static void snd_rme32_pcm_start(struct rme32 * rme32, int from_pause)
  703. {
  704. if (!from_pause) {
  705. writel(0, rme32->iobase + RME32_IO_RESET_POS);
  706. }
  707. rme32->wcreg |= RME32_WCR_START;
  708. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  709. }
  710. static void snd_rme32_pcm_stop(struct rme32 * rme32, int to_pause)
  711. {
  712. /*
  713. * Check if there is an unconfirmed IRQ, if so confirm it, or else
  714. * the hardware will not stop generating interrupts
  715. */
  716. rme32->rcreg = readl(rme32->iobase + RME32_IO_CONTROL_REGISTER);
  717. if (rme32->rcreg & RME32_RCR_IRQ) {
  718. writel(0, rme32->iobase + RME32_IO_CONFIRM_ACTION_IRQ);
  719. }
  720. rme32->wcreg &= ~RME32_WCR_START;
  721. if (rme32->wcreg & RME32_WCR_SEL)
  722. rme32->wcreg |= RME32_WCR_MUTE;
  723. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  724. if (! to_pause)
  725. writel(0, rme32->iobase + RME32_IO_RESET_POS);
  726. }
  727. static irqreturn_t snd_rme32_interrupt(int irq, void *dev_id)
  728. {
  729. struct rme32 *rme32 = (struct rme32 *) dev_id;
  730. rme32->rcreg = readl(rme32->iobase + RME32_IO_CONTROL_REGISTER);
  731. if (!(rme32->rcreg & RME32_RCR_IRQ)) {
  732. return IRQ_NONE;
  733. } else {
  734. if (rme32->capture_substream) {
  735. snd_pcm_period_elapsed(rme32->capture_substream);
  736. }
  737. if (rme32->playback_substream) {
  738. snd_pcm_period_elapsed(rme32->playback_substream);
  739. }
  740. writel(0, rme32->iobase + RME32_IO_CONFIRM_ACTION_IRQ);
  741. }
  742. return IRQ_HANDLED;
  743. }
  744. static unsigned int period_bytes[] = { RME32_BLOCK_SIZE };
  745. static struct snd_pcm_hw_constraint_list hw_constraints_period_bytes = {
  746. .count = ARRAY_SIZE(period_bytes),
  747. .list = period_bytes,
  748. .mask = 0
  749. };
  750. static void snd_rme32_set_buffer_constraint(struct rme32 *rme32, struct snd_pcm_runtime *runtime)
  751. {
  752. if (! rme32->fullduplex_mode) {
  753. snd_pcm_hw_constraint_single(runtime,
  754. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  755. RME32_BUFFER_SIZE);
  756. snd_pcm_hw_constraint_list(runtime, 0,
  757. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  758. &hw_constraints_period_bytes);
  759. }
  760. }
  761. static int snd_rme32_playback_spdif_open(struct snd_pcm_substream *substream)
  762. {
  763. int rate, dummy;
  764. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  765. struct snd_pcm_runtime *runtime = substream->runtime;
  766. snd_pcm_set_sync(substream);
  767. spin_lock_irq(&rme32->lock);
  768. if (rme32->playback_substream != NULL) {
  769. spin_unlock_irq(&rme32->lock);
  770. return -EBUSY;
  771. }
  772. rme32->wcreg &= ~RME32_WCR_ADAT;
  773. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  774. rme32->playback_substream = substream;
  775. spin_unlock_irq(&rme32->lock);
  776. if (rme32->fullduplex_mode)
  777. runtime->hw = snd_rme32_spdif_fd_info;
  778. else
  779. runtime->hw = snd_rme32_spdif_info;
  780. if (rme32->pci->device == PCI_DEVICE_ID_RME_DIGI32_PRO) {
  781. runtime->hw.rates |= SNDRV_PCM_RATE_64000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000;
  782. runtime->hw.rate_max = 96000;
  783. }
  784. if ((rme32->rcreg & RME32_RCR_KMODE) &&
  785. (rate = snd_rme32_capture_getrate(rme32, &dummy)) > 0) {
  786. /* AutoSync */
  787. runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
  788. runtime->hw.rate_min = rate;
  789. runtime->hw.rate_max = rate;
  790. }
  791. snd_rme32_set_buffer_constraint(rme32, runtime);
  792. rme32->wcreg_spdif_stream = rme32->wcreg_spdif;
  793. rme32->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  794. snd_ctl_notify(rme32->card, SNDRV_CTL_EVENT_MASK_VALUE |
  795. SNDRV_CTL_EVENT_MASK_INFO, &rme32->spdif_ctl->id);
  796. return 0;
  797. }
  798. static int snd_rme32_capture_spdif_open(struct snd_pcm_substream *substream)
  799. {
  800. int isadat, rate;
  801. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  802. struct snd_pcm_runtime *runtime = substream->runtime;
  803. snd_pcm_set_sync(substream);
  804. spin_lock_irq(&rme32->lock);
  805. if (rme32->capture_substream != NULL) {
  806. spin_unlock_irq(&rme32->lock);
  807. return -EBUSY;
  808. }
  809. rme32->capture_substream = substream;
  810. spin_unlock_irq(&rme32->lock);
  811. if (rme32->fullduplex_mode)
  812. runtime->hw = snd_rme32_spdif_fd_info;
  813. else
  814. runtime->hw = snd_rme32_spdif_info;
  815. if (RME32_PRO_WITH_8414(rme32)) {
  816. runtime->hw.rates |= SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000;
  817. runtime->hw.rate_max = 96000;
  818. }
  819. if ((rate = snd_rme32_capture_getrate(rme32, &isadat)) > 0) {
  820. if (isadat) {
  821. return -EIO;
  822. }
  823. runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
  824. runtime->hw.rate_min = rate;
  825. runtime->hw.rate_max = rate;
  826. }
  827. snd_rme32_set_buffer_constraint(rme32, runtime);
  828. return 0;
  829. }
  830. static int
  831. snd_rme32_playback_adat_open(struct snd_pcm_substream *substream)
  832. {
  833. int rate, dummy;
  834. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  835. struct snd_pcm_runtime *runtime = substream->runtime;
  836. snd_pcm_set_sync(substream);
  837. spin_lock_irq(&rme32->lock);
  838. if (rme32->playback_substream != NULL) {
  839. spin_unlock_irq(&rme32->lock);
  840. return -EBUSY;
  841. }
  842. rme32->wcreg |= RME32_WCR_ADAT;
  843. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  844. rme32->playback_substream = substream;
  845. spin_unlock_irq(&rme32->lock);
  846. if (rme32->fullduplex_mode)
  847. runtime->hw = snd_rme32_adat_fd_info;
  848. else
  849. runtime->hw = snd_rme32_adat_info;
  850. if ((rme32->rcreg & RME32_RCR_KMODE) &&
  851. (rate = snd_rme32_capture_getrate(rme32, &dummy)) > 0) {
  852. /* AutoSync */
  853. runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
  854. runtime->hw.rate_min = rate;
  855. runtime->hw.rate_max = rate;
  856. }
  857. snd_rme32_set_buffer_constraint(rme32, runtime);
  858. return 0;
  859. }
  860. static int
  861. snd_rme32_capture_adat_open(struct snd_pcm_substream *substream)
  862. {
  863. int isadat, rate;
  864. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  865. struct snd_pcm_runtime *runtime = substream->runtime;
  866. if (rme32->fullduplex_mode)
  867. runtime->hw = snd_rme32_adat_fd_info;
  868. else
  869. runtime->hw = snd_rme32_adat_info;
  870. if ((rate = snd_rme32_capture_getrate(rme32, &isadat)) > 0) {
  871. if (!isadat) {
  872. return -EIO;
  873. }
  874. runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
  875. runtime->hw.rate_min = rate;
  876. runtime->hw.rate_max = rate;
  877. }
  878. snd_pcm_set_sync(substream);
  879. spin_lock_irq(&rme32->lock);
  880. if (rme32->capture_substream != NULL) {
  881. spin_unlock_irq(&rme32->lock);
  882. return -EBUSY;
  883. }
  884. rme32->capture_substream = substream;
  885. spin_unlock_irq(&rme32->lock);
  886. snd_rme32_set_buffer_constraint(rme32, runtime);
  887. return 0;
  888. }
  889. static int snd_rme32_playback_close(struct snd_pcm_substream *substream)
  890. {
  891. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  892. int spdif = 0;
  893. spin_lock_irq(&rme32->lock);
  894. rme32->playback_substream = NULL;
  895. rme32->playback_periodsize = 0;
  896. spdif = (rme32->wcreg & RME32_WCR_ADAT) == 0;
  897. spin_unlock_irq(&rme32->lock);
  898. if (spdif) {
  899. rme32->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  900. snd_ctl_notify(rme32->card, SNDRV_CTL_EVENT_MASK_VALUE |
  901. SNDRV_CTL_EVENT_MASK_INFO,
  902. &rme32->spdif_ctl->id);
  903. }
  904. return 0;
  905. }
  906. static int snd_rme32_capture_close(struct snd_pcm_substream *substream)
  907. {
  908. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  909. spin_lock_irq(&rme32->lock);
  910. rme32->capture_substream = NULL;
  911. rme32->capture_periodsize = 0;
  912. spin_unlock_irq(&rme32->lock);
  913. return 0;
  914. }
  915. static int snd_rme32_playback_prepare(struct snd_pcm_substream *substream)
  916. {
  917. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  918. spin_lock_irq(&rme32->lock);
  919. if (rme32->fullduplex_mode) {
  920. memset(&rme32->playback_pcm, 0, sizeof(rme32->playback_pcm));
  921. rme32->playback_pcm.hw_buffer_size = RME32_BUFFER_SIZE;
  922. rme32->playback_pcm.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
  923. } else {
  924. writel(0, rme32->iobase + RME32_IO_RESET_POS);
  925. }
  926. if (rme32->wcreg & RME32_WCR_SEL)
  927. rme32->wcreg &= ~RME32_WCR_MUTE;
  928. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  929. spin_unlock_irq(&rme32->lock);
  930. return 0;
  931. }
  932. static int snd_rme32_capture_prepare(struct snd_pcm_substream *substream)
  933. {
  934. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  935. spin_lock_irq(&rme32->lock);
  936. if (rme32->fullduplex_mode) {
  937. memset(&rme32->capture_pcm, 0, sizeof(rme32->capture_pcm));
  938. rme32->capture_pcm.hw_buffer_size = RME32_BUFFER_SIZE;
  939. rme32->capture_pcm.hw_queue_size = RME32_BUFFER_SIZE / 2;
  940. rme32->capture_pcm.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
  941. } else {
  942. writel(0, rme32->iobase + RME32_IO_RESET_POS);
  943. }
  944. spin_unlock_irq(&rme32->lock);
  945. return 0;
  946. }
  947. static int
  948. snd_rme32_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  949. {
  950. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  951. struct snd_pcm_substream *s;
  952. spin_lock(&rme32->lock);
  953. snd_pcm_group_for_each_entry(s, substream) {
  954. if (s != rme32->playback_substream &&
  955. s != rme32->capture_substream)
  956. continue;
  957. switch (cmd) {
  958. case SNDRV_PCM_TRIGGER_START:
  959. rme32->running |= (1 << s->stream);
  960. if (rme32->fullduplex_mode) {
  961. /* remember the current DMA position */
  962. if (s == rme32->playback_substream) {
  963. rme32->playback_pcm.hw_io =
  964. rme32->playback_pcm.hw_data = snd_rme32_pcm_byteptr(rme32);
  965. } else {
  966. rme32->capture_pcm.hw_io =
  967. rme32->capture_pcm.hw_data = snd_rme32_pcm_byteptr(rme32);
  968. }
  969. }
  970. break;
  971. case SNDRV_PCM_TRIGGER_STOP:
  972. rme32->running &= ~(1 << s->stream);
  973. break;
  974. }
  975. snd_pcm_trigger_done(s, substream);
  976. }
  977. /* prefill playback buffer */
  978. if (cmd == SNDRV_PCM_TRIGGER_START && rme32->fullduplex_mode) {
  979. snd_pcm_group_for_each_entry(s, substream) {
  980. if (s == rme32->playback_substream) {
  981. s->ops->ack(s);
  982. break;
  983. }
  984. }
  985. }
  986. switch (cmd) {
  987. case SNDRV_PCM_TRIGGER_START:
  988. if (rme32->running && ! RME32_ISWORKING(rme32))
  989. snd_rme32_pcm_start(rme32, 0);
  990. break;
  991. case SNDRV_PCM_TRIGGER_STOP:
  992. if (! rme32->running && RME32_ISWORKING(rme32))
  993. snd_rme32_pcm_stop(rme32, 0);
  994. break;
  995. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  996. if (rme32->running && RME32_ISWORKING(rme32))
  997. snd_rme32_pcm_stop(rme32, 1);
  998. break;
  999. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1000. if (rme32->running && ! RME32_ISWORKING(rme32))
  1001. snd_rme32_pcm_start(rme32, 1);
  1002. break;
  1003. }
  1004. spin_unlock(&rme32->lock);
  1005. return 0;
  1006. }
  1007. /* pointer callback for halfduplex mode */
  1008. static snd_pcm_uframes_t
  1009. snd_rme32_playback_pointer(struct snd_pcm_substream *substream)
  1010. {
  1011. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  1012. return snd_rme32_pcm_byteptr(rme32) >> rme32->playback_frlog;
  1013. }
  1014. static snd_pcm_uframes_t
  1015. snd_rme32_capture_pointer(struct snd_pcm_substream *substream)
  1016. {
  1017. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  1018. return snd_rme32_pcm_byteptr(rme32) >> rme32->capture_frlog;
  1019. }
  1020. /* ack and pointer callbacks for fullduplex mode */
  1021. static void snd_rme32_pb_trans_copy(struct snd_pcm_substream *substream,
  1022. struct snd_pcm_indirect *rec, size_t bytes)
  1023. {
  1024. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  1025. memcpy_toio(rme32->iobase + RME32_IO_DATA_BUFFER + rec->hw_data,
  1026. substream->runtime->dma_area + rec->sw_data, bytes);
  1027. }
  1028. static int snd_rme32_playback_fd_ack(struct snd_pcm_substream *substream)
  1029. {
  1030. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  1031. struct snd_pcm_indirect *rec, *cprec;
  1032. rec = &rme32->playback_pcm;
  1033. cprec = &rme32->capture_pcm;
  1034. spin_lock(&rme32->lock);
  1035. rec->hw_queue_size = RME32_BUFFER_SIZE;
  1036. if (rme32->running & (1 << SNDRV_PCM_STREAM_CAPTURE))
  1037. rec->hw_queue_size -= cprec->hw_ready;
  1038. spin_unlock(&rme32->lock);
  1039. snd_pcm_indirect_playback_transfer(substream, rec,
  1040. snd_rme32_pb_trans_copy);
  1041. return 0;
  1042. }
  1043. static void snd_rme32_cp_trans_copy(struct snd_pcm_substream *substream,
  1044. struct snd_pcm_indirect *rec, size_t bytes)
  1045. {
  1046. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  1047. memcpy_fromio(substream->runtime->dma_area + rec->sw_data,
  1048. rme32->iobase + RME32_IO_DATA_BUFFER + rec->hw_data,
  1049. bytes);
  1050. }
  1051. static int snd_rme32_capture_fd_ack(struct snd_pcm_substream *substream)
  1052. {
  1053. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  1054. snd_pcm_indirect_capture_transfer(substream, &rme32->capture_pcm,
  1055. snd_rme32_cp_trans_copy);
  1056. return 0;
  1057. }
  1058. static snd_pcm_uframes_t
  1059. snd_rme32_playback_fd_pointer(struct snd_pcm_substream *substream)
  1060. {
  1061. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  1062. return snd_pcm_indirect_playback_pointer(substream, &rme32->playback_pcm,
  1063. snd_rme32_pcm_byteptr(rme32));
  1064. }
  1065. static snd_pcm_uframes_t
  1066. snd_rme32_capture_fd_pointer(struct snd_pcm_substream *substream)
  1067. {
  1068. struct rme32 *rme32 = snd_pcm_substream_chip(substream);
  1069. return snd_pcm_indirect_capture_pointer(substream, &rme32->capture_pcm,
  1070. snd_rme32_pcm_byteptr(rme32));
  1071. }
  1072. /* for halfduplex mode */
  1073. static struct snd_pcm_ops snd_rme32_playback_spdif_ops = {
  1074. .open = snd_rme32_playback_spdif_open,
  1075. .close = snd_rme32_playback_close,
  1076. .ioctl = snd_pcm_lib_ioctl,
  1077. .hw_params = snd_rme32_playback_hw_params,
  1078. .hw_free = snd_rme32_pcm_hw_free,
  1079. .prepare = snd_rme32_playback_prepare,
  1080. .trigger = snd_rme32_pcm_trigger,
  1081. .pointer = snd_rme32_playback_pointer,
  1082. .copy = snd_rme32_playback_copy,
  1083. .silence = snd_rme32_playback_silence,
  1084. .mmap = snd_pcm_lib_mmap_iomem,
  1085. };
  1086. static struct snd_pcm_ops snd_rme32_capture_spdif_ops = {
  1087. .open = snd_rme32_capture_spdif_open,
  1088. .close = snd_rme32_capture_close,
  1089. .ioctl = snd_pcm_lib_ioctl,
  1090. .hw_params = snd_rme32_capture_hw_params,
  1091. .hw_free = snd_rme32_pcm_hw_free,
  1092. .prepare = snd_rme32_capture_prepare,
  1093. .trigger = snd_rme32_pcm_trigger,
  1094. .pointer = snd_rme32_capture_pointer,
  1095. .copy = snd_rme32_capture_copy,
  1096. .mmap = snd_pcm_lib_mmap_iomem,
  1097. };
  1098. static struct snd_pcm_ops snd_rme32_playback_adat_ops = {
  1099. .open = snd_rme32_playback_adat_open,
  1100. .close = snd_rme32_playback_close,
  1101. .ioctl = snd_pcm_lib_ioctl,
  1102. .hw_params = snd_rme32_playback_hw_params,
  1103. .prepare = snd_rme32_playback_prepare,
  1104. .trigger = snd_rme32_pcm_trigger,
  1105. .pointer = snd_rme32_playback_pointer,
  1106. .copy = snd_rme32_playback_copy,
  1107. .silence = snd_rme32_playback_silence,
  1108. .mmap = snd_pcm_lib_mmap_iomem,
  1109. };
  1110. static struct snd_pcm_ops snd_rme32_capture_adat_ops = {
  1111. .open = snd_rme32_capture_adat_open,
  1112. .close = snd_rme32_capture_close,
  1113. .ioctl = snd_pcm_lib_ioctl,
  1114. .hw_params = snd_rme32_capture_hw_params,
  1115. .prepare = snd_rme32_capture_prepare,
  1116. .trigger = snd_rme32_pcm_trigger,
  1117. .pointer = snd_rme32_capture_pointer,
  1118. .copy = snd_rme32_capture_copy,
  1119. .mmap = snd_pcm_lib_mmap_iomem,
  1120. };
  1121. /* for fullduplex mode */
  1122. static struct snd_pcm_ops snd_rme32_playback_spdif_fd_ops = {
  1123. .open = snd_rme32_playback_spdif_open,
  1124. .close = snd_rme32_playback_close,
  1125. .ioctl = snd_pcm_lib_ioctl,
  1126. .hw_params = snd_rme32_playback_hw_params,
  1127. .hw_free = snd_rme32_pcm_hw_free,
  1128. .prepare = snd_rme32_playback_prepare,
  1129. .trigger = snd_rme32_pcm_trigger,
  1130. .pointer = snd_rme32_playback_fd_pointer,
  1131. .ack = snd_rme32_playback_fd_ack,
  1132. };
  1133. static struct snd_pcm_ops snd_rme32_capture_spdif_fd_ops = {
  1134. .open = snd_rme32_capture_spdif_open,
  1135. .close = snd_rme32_capture_close,
  1136. .ioctl = snd_pcm_lib_ioctl,
  1137. .hw_params = snd_rme32_capture_hw_params,
  1138. .hw_free = snd_rme32_pcm_hw_free,
  1139. .prepare = snd_rme32_capture_prepare,
  1140. .trigger = snd_rme32_pcm_trigger,
  1141. .pointer = snd_rme32_capture_fd_pointer,
  1142. .ack = snd_rme32_capture_fd_ack,
  1143. };
  1144. static struct snd_pcm_ops snd_rme32_playback_adat_fd_ops = {
  1145. .open = snd_rme32_playback_adat_open,
  1146. .close = snd_rme32_playback_close,
  1147. .ioctl = snd_pcm_lib_ioctl,
  1148. .hw_params = snd_rme32_playback_hw_params,
  1149. .prepare = snd_rme32_playback_prepare,
  1150. .trigger = snd_rme32_pcm_trigger,
  1151. .pointer = snd_rme32_playback_fd_pointer,
  1152. .ack = snd_rme32_playback_fd_ack,
  1153. };
  1154. static struct snd_pcm_ops snd_rme32_capture_adat_fd_ops = {
  1155. .open = snd_rme32_capture_adat_open,
  1156. .close = snd_rme32_capture_close,
  1157. .ioctl = snd_pcm_lib_ioctl,
  1158. .hw_params = snd_rme32_capture_hw_params,
  1159. .prepare = snd_rme32_capture_prepare,
  1160. .trigger = snd_rme32_pcm_trigger,
  1161. .pointer = snd_rme32_capture_fd_pointer,
  1162. .ack = snd_rme32_capture_fd_ack,
  1163. };
  1164. static void snd_rme32_free(void *private_data)
  1165. {
  1166. struct rme32 *rme32 = (struct rme32 *) private_data;
  1167. if (rme32 == NULL) {
  1168. return;
  1169. }
  1170. if (rme32->irq >= 0) {
  1171. snd_rme32_pcm_stop(rme32, 0);
  1172. free_irq(rme32->irq, (void *) rme32);
  1173. rme32->irq = -1;
  1174. }
  1175. if (rme32->iobase) {
  1176. iounmap(rme32->iobase);
  1177. rme32->iobase = NULL;
  1178. }
  1179. if (rme32->port) {
  1180. pci_release_regions(rme32->pci);
  1181. rme32->port = 0;
  1182. }
  1183. pci_disable_device(rme32->pci);
  1184. }
  1185. static void snd_rme32_free_spdif_pcm(struct snd_pcm *pcm)
  1186. {
  1187. struct rme32 *rme32 = (struct rme32 *) pcm->private_data;
  1188. rme32->spdif_pcm = NULL;
  1189. }
  1190. static void
  1191. snd_rme32_free_adat_pcm(struct snd_pcm *pcm)
  1192. {
  1193. struct rme32 *rme32 = (struct rme32 *) pcm->private_data;
  1194. rme32->adat_pcm = NULL;
  1195. }
  1196. static int snd_rme32_create(struct rme32 *rme32)
  1197. {
  1198. struct pci_dev *pci = rme32->pci;
  1199. int err;
  1200. rme32->irq = -1;
  1201. spin_lock_init(&rme32->lock);
  1202. if ((err = pci_enable_device(pci)) < 0)
  1203. return err;
  1204. if ((err = pci_request_regions(pci, "RME32")) < 0)
  1205. return err;
  1206. rme32->port = pci_resource_start(rme32->pci, 0);
  1207. rme32->iobase = ioremap_nocache(rme32->port, RME32_IO_SIZE);
  1208. if (!rme32->iobase) {
  1209. dev_err(rme32->card->dev,
  1210. "unable to remap memory region 0x%lx-0x%lx\n",
  1211. rme32->port, rme32->port + RME32_IO_SIZE - 1);
  1212. return -ENOMEM;
  1213. }
  1214. if (request_irq(pci->irq, snd_rme32_interrupt, IRQF_SHARED,
  1215. KBUILD_MODNAME, rme32)) {
  1216. dev_err(rme32->card->dev, "unable to grab IRQ %d\n", pci->irq);
  1217. return -EBUSY;
  1218. }
  1219. rme32->irq = pci->irq;
  1220. /* read the card's revision number */
  1221. pci_read_config_byte(pci, 8, &rme32->rev);
  1222. /* set up ALSA pcm device for S/PDIF */
  1223. if ((err = snd_pcm_new(rme32->card, "Digi32 IEC958", 0, 1, 1, &rme32->spdif_pcm)) < 0) {
  1224. return err;
  1225. }
  1226. rme32->spdif_pcm->private_data = rme32;
  1227. rme32->spdif_pcm->private_free = snd_rme32_free_spdif_pcm;
  1228. strcpy(rme32->spdif_pcm->name, "Digi32 IEC958");
  1229. if (rme32->fullduplex_mode) {
  1230. snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1231. &snd_rme32_playback_spdif_fd_ops);
  1232. snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_CAPTURE,
  1233. &snd_rme32_capture_spdif_fd_ops);
  1234. snd_pcm_lib_preallocate_pages_for_all(rme32->spdif_pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  1235. snd_dma_continuous_data(GFP_KERNEL),
  1236. 0, RME32_MID_BUFFER_SIZE);
  1237. rme32->spdif_pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  1238. } else {
  1239. snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1240. &snd_rme32_playback_spdif_ops);
  1241. snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_CAPTURE,
  1242. &snd_rme32_capture_spdif_ops);
  1243. rme32->spdif_pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
  1244. }
  1245. /* set up ALSA pcm device for ADAT */
  1246. if ((pci->device == PCI_DEVICE_ID_RME_DIGI32) ||
  1247. (pci->device == PCI_DEVICE_ID_RME_DIGI32_PRO)) {
  1248. /* ADAT is not available on DIGI32 and DIGI32 Pro */
  1249. rme32->adat_pcm = NULL;
  1250. }
  1251. else {
  1252. if ((err = snd_pcm_new(rme32->card, "Digi32 ADAT", 1,
  1253. 1, 1, &rme32->adat_pcm)) < 0)
  1254. {
  1255. return err;
  1256. }
  1257. rme32->adat_pcm->private_data = rme32;
  1258. rme32->adat_pcm->private_free = snd_rme32_free_adat_pcm;
  1259. strcpy(rme32->adat_pcm->name, "Digi32 ADAT");
  1260. if (rme32->fullduplex_mode) {
  1261. snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1262. &snd_rme32_playback_adat_fd_ops);
  1263. snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_CAPTURE,
  1264. &snd_rme32_capture_adat_fd_ops);
  1265. snd_pcm_lib_preallocate_pages_for_all(rme32->adat_pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  1266. snd_dma_continuous_data(GFP_KERNEL),
  1267. 0, RME32_MID_BUFFER_SIZE);
  1268. rme32->adat_pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  1269. } else {
  1270. snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1271. &snd_rme32_playback_adat_ops);
  1272. snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_CAPTURE,
  1273. &snd_rme32_capture_adat_ops);
  1274. rme32->adat_pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
  1275. }
  1276. }
  1277. rme32->playback_periodsize = 0;
  1278. rme32->capture_periodsize = 0;
  1279. /* make sure playback/capture is stopped, if by some reason active */
  1280. snd_rme32_pcm_stop(rme32, 0);
  1281. /* reset DAC */
  1282. snd_rme32_reset_dac(rme32);
  1283. /* reset buffer pointer */
  1284. writel(0, rme32->iobase + RME32_IO_RESET_POS);
  1285. /* set default values in registers */
  1286. rme32->wcreg = RME32_WCR_SEL | /* normal playback */
  1287. RME32_WCR_INP_0 | /* input select */
  1288. RME32_WCR_MUTE; /* muting on */
  1289. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  1290. /* init switch interface */
  1291. if ((err = snd_rme32_create_switches(rme32->card, rme32)) < 0) {
  1292. return err;
  1293. }
  1294. /* init proc interface */
  1295. snd_rme32_proc_init(rme32);
  1296. rme32->capture_substream = NULL;
  1297. rme32->playback_substream = NULL;
  1298. return 0;
  1299. }
  1300. /*
  1301. * proc interface
  1302. */
  1303. static void
  1304. snd_rme32_proc_read(struct snd_info_entry * entry, struct snd_info_buffer *buffer)
  1305. {
  1306. int n;
  1307. struct rme32 *rme32 = (struct rme32 *) entry->private_data;
  1308. rme32->rcreg = readl(rme32->iobase + RME32_IO_CONTROL_REGISTER);
  1309. snd_iprintf(buffer, rme32->card->longname);
  1310. snd_iprintf(buffer, " (index #%d)\n", rme32->card->number + 1);
  1311. snd_iprintf(buffer, "\nGeneral settings\n");
  1312. if (rme32->fullduplex_mode)
  1313. snd_iprintf(buffer, " Full-duplex mode\n");
  1314. else
  1315. snd_iprintf(buffer, " Half-duplex mode\n");
  1316. if (RME32_PRO_WITH_8414(rme32)) {
  1317. snd_iprintf(buffer, " receiver: CS8414\n");
  1318. } else {
  1319. snd_iprintf(buffer, " receiver: CS8412\n");
  1320. }
  1321. if (rme32->wcreg & RME32_WCR_MODE24) {
  1322. snd_iprintf(buffer, " format: 24 bit");
  1323. } else {
  1324. snd_iprintf(buffer, " format: 16 bit");
  1325. }
  1326. if (rme32->wcreg & RME32_WCR_MONO) {
  1327. snd_iprintf(buffer, ", Mono\n");
  1328. } else {
  1329. snd_iprintf(buffer, ", Stereo\n");
  1330. }
  1331. snd_iprintf(buffer, "\nInput settings\n");
  1332. switch (snd_rme32_getinputtype(rme32)) {
  1333. case RME32_INPUT_OPTICAL:
  1334. snd_iprintf(buffer, " input: optical");
  1335. break;
  1336. case RME32_INPUT_COAXIAL:
  1337. snd_iprintf(buffer, " input: coaxial");
  1338. break;
  1339. case RME32_INPUT_INTERNAL:
  1340. snd_iprintf(buffer, " input: internal");
  1341. break;
  1342. case RME32_INPUT_XLR:
  1343. snd_iprintf(buffer, " input: XLR");
  1344. break;
  1345. }
  1346. if (snd_rme32_capture_getrate(rme32, &n) < 0) {
  1347. snd_iprintf(buffer, "\n sample rate: no valid signal\n");
  1348. } else {
  1349. if (n) {
  1350. snd_iprintf(buffer, " (8 channels)\n");
  1351. } else {
  1352. snd_iprintf(buffer, " (2 channels)\n");
  1353. }
  1354. snd_iprintf(buffer, " sample rate: %d Hz\n",
  1355. snd_rme32_capture_getrate(rme32, &n));
  1356. }
  1357. snd_iprintf(buffer, "\nOutput settings\n");
  1358. if (rme32->wcreg & RME32_WCR_SEL) {
  1359. snd_iprintf(buffer, " output signal: normal playback");
  1360. } else {
  1361. snd_iprintf(buffer, " output signal: same as input");
  1362. }
  1363. if (rme32->wcreg & RME32_WCR_MUTE) {
  1364. snd_iprintf(buffer, " (muted)\n");
  1365. } else {
  1366. snd_iprintf(buffer, "\n");
  1367. }
  1368. /* master output frequency */
  1369. if (!
  1370. ((!(rme32->wcreg & RME32_WCR_FREQ_0))
  1371. && (!(rme32->wcreg & RME32_WCR_FREQ_1)))) {
  1372. snd_iprintf(buffer, " sample rate: %d Hz\n",
  1373. snd_rme32_playback_getrate(rme32));
  1374. }
  1375. if (rme32->rcreg & RME32_RCR_KMODE) {
  1376. snd_iprintf(buffer, " sample clock source: AutoSync\n");
  1377. } else {
  1378. snd_iprintf(buffer, " sample clock source: Internal\n");
  1379. }
  1380. if (rme32->wcreg & RME32_WCR_PRO) {
  1381. snd_iprintf(buffer, " format: AES/EBU (professional)\n");
  1382. } else {
  1383. snd_iprintf(buffer, " format: IEC958 (consumer)\n");
  1384. }
  1385. if (rme32->wcreg & RME32_WCR_EMP) {
  1386. snd_iprintf(buffer, " emphasis: on\n");
  1387. } else {
  1388. snd_iprintf(buffer, " emphasis: off\n");
  1389. }
  1390. }
  1391. static void snd_rme32_proc_init(struct rme32 *rme32)
  1392. {
  1393. struct snd_info_entry *entry;
  1394. if (! snd_card_proc_new(rme32->card, "rme32", &entry))
  1395. snd_info_set_text_ops(entry, rme32, snd_rme32_proc_read);
  1396. }
  1397. /*
  1398. * control interface
  1399. */
  1400. #define snd_rme32_info_loopback_control snd_ctl_boolean_mono_info
  1401. static int
  1402. snd_rme32_get_loopback_control(struct snd_kcontrol *kcontrol,
  1403. struct snd_ctl_elem_value *ucontrol)
  1404. {
  1405. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1406. spin_lock_irq(&rme32->lock);
  1407. ucontrol->value.integer.value[0] =
  1408. rme32->wcreg & RME32_WCR_SEL ? 0 : 1;
  1409. spin_unlock_irq(&rme32->lock);
  1410. return 0;
  1411. }
  1412. static int
  1413. snd_rme32_put_loopback_control(struct snd_kcontrol *kcontrol,
  1414. struct snd_ctl_elem_value *ucontrol)
  1415. {
  1416. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1417. unsigned int val;
  1418. int change;
  1419. val = ucontrol->value.integer.value[0] ? 0 : RME32_WCR_SEL;
  1420. spin_lock_irq(&rme32->lock);
  1421. val = (rme32->wcreg & ~RME32_WCR_SEL) | val;
  1422. change = val != rme32->wcreg;
  1423. if (ucontrol->value.integer.value[0])
  1424. val &= ~RME32_WCR_MUTE;
  1425. else
  1426. val |= RME32_WCR_MUTE;
  1427. rme32->wcreg = val;
  1428. writel(val, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  1429. spin_unlock_irq(&rme32->lock);
  1430. return change;
  1431. }
  1432. static int
  1433. snd_rme32_info_inputtype_control(struct snd_kcontrol *kcontrol,
  1434. struct snd_ctl_elem_info *uinfo)
  1435. {
  1436. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1437. static const char * const texts[4] = {
  1438. "Optical", "Coaxial", "Internal", "XLR"
  1439. };
  1440. int num_items;
  1441. switch (rme32->pci->device) {
  1442. case PCI_DEVICE_ID_RME_DIGI32:
  1443. case PCI_DEVICE_ID_RME_DIGI32_8:
  1444. num_items = 3;
  1445. break;
  1446. case PCI_DEVICE_ID_RME_DIGI32_PRO:
  1447. num_items = 4;
  1448. break;
  1449. default:
  1450. snd_BUG();
  1451. return -EINVAL;
  1452. }
  1453. return snd_ctl_enum_info(uinfo, 1, num_items, texts);
  1454. }
  1455. static int
  1456. snd_rme32_get_inputtype_control(struct snd_kcontrol *kcontrol,
  1457. struct snd_ctl_elem_value *ucontrol)
  1458. {
  1459. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1460. unsigned int items = 3;
  1461. spin_lock_irq(&rme32->lock);
  1462. ucontrol->value.enumerated.item[0] = snd_rme32_getinputtype(rme32);
  1463. switch (rme32->pci->device) {
  1464. case PCI_DEVICE_ID_RME_DIGI32:
  1465. case PCI_DEVICE_ID_RME_DIGI32_8:
  1466. items = 3;
  1467. break;
  1468. case PCI_DEVICE_ID_RME_DIGI32_PRO:
  1469. items = 4;
  1470. break;
  1471. default:
  1472. snd_BUG();
  1473. break;
  1474. }
  1475. if (ucontrol->value.enumerated.item[0] >= items) {
  1476. ucontrol->value.enumerated.item[0] = items - 1;
  1477. }
  1478. spin_unlock_irq(&rme32->lock);
  1479. return 0;
  1480. }
  1481. static int
  1482. snd_rme32_put_inputtype_control(struct snd_kcontrol *kcontrol,
  1483. struct snd_ctl_elem_value *ucontrol)
  1484. {
  1485. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1486. unsigned int val;
  1487. int change, items = 3;
  1488. switch (rme32->pci->device) {
  1489. case PCI_DEVICE_ID_RME_DIGI32:
  1490. case PCI_DEVICE_ID_RME_DIGI32_8:
  1491. items = 3;
  1492. break;
  1493. case PCI_DEVICE_ID_RME_DIGI32_PRO:
  1494. items = 4;
  1495. break;
  1496. default:
  1497. snd_BUG();
  1498. break;
  1499. }
  1500. val = ucontrol->value.enumerated.item[0] % items;
  1501. spin_lock_irq(&rme32->lock);
  1502. change = val != (unsigned int)snd_rme32_getinputtype(rme32);
  1503. snd_rme32_setinputtype(rme32, val);
  1504. spin_unlock_irq(&rme32->lock);
  1505. return change;
  1506. }
  1507. static int
  1508. snd_rme32_info_clockmode_control(struct snd_kcontrol *kcontrol,
  1509. struct snd_ctl_elem_info *uinfo)
  1510. {
  1511. static const char * const texts[4] = { "AutoSync",
  1512. "Internal 32.0kHz",
  1513. "Internal 44.1kHz",
  1514. "Internal 48.0kHz" };
  1515. return snd_ctl_enum_info(uinfo, 1, 4, texts);
  1516. }
  1517. static int
  1518. snd_rme32_get_clockmode_control(struct snd_kcontrol *kcontrol,
  1519. struct snd_ctl_elem_value *ucontrol)
  1520. {
  1521. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1522. spin_lock_irq(&rme32->lock);
  1523. ucontrol->value.enumerated.item[0] = snd_rme32_getclockmode(rme32);
  1524. spin_unlock_irq(&rme32->lock);
  1525. return 0;
  1526. }
  1527. static int
  1528. snd_rme32_put_clockmode_control(struct snd_kcontrol *kcontrol,
  1529. struct snd_ctl_elem_value *ucontrol)
  1530. {
  1531. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1532. unsigned int val;
  1533. int change;
  1534. val = ucontrol->value.enumerated.item[0] % 3;
  1535. spin_lock_irq(&rme32->lock);
  1536. change = val != (unsigned int)snd_rme32_getclockmode(rme32);
  1537. snd_rme32_setclockmode(rme32, val);
  1538. spin_unlock_irq(&rme32->lock);
  1539. return change;
  1540. }
  1541. static u32 snd_rme32_convert_from_aes(struct snd_aes_iec958 * aes)
  1542. {
  1543. u32 val = 0;
  1544. val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? RME32_WCR_PRO : 0;
  1545. if (val & RME32_WCR_PRO)
  1546. val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? RME32_WCR_EMP : 0;
  1547. else
  1548. val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? RME32_WCR_EMP : 0;
  1549. return val;
  1550. }
  1551. static void snd_rme32_convert_to_aes(struct snd_aes_iec958 * aes, u32 val)
  1552. {
  1553. aes->status[0] = ((val & RME32_WCR_PRO) ? IEC958_AES0_PROFESSIONAL : 0);
  1554. if (val & RME32_WCR_PRO)
  1555. aes->status[0] |= (val & RME32_WCR_EMP) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
  1556. else
  1557. aes->status[0] |= (val & RME32_WCR_EMP) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
  1558. }
  1559. static int snd_rme32_control_spdif_info(struct snd_kcontrol *kcontrol,
  1560. struct snd_ctl_elem_info *uinfo)
  1561. {
  1562. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1563. uinfo->count = 1;
  1564. return 0;
  1565. }
  1566. static int snd_rme32_control_spdif_get(struct snd_kcontrol *kcontrol,
  1567. struct snd_ctl_elem_value *ucontrol)
  1568. {
  1569. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1570. snd_rme32_convert_to_aes(&ucontrol->value.iec958,
  1571. rme32->wcreg_spdif);
  1572. return 0;
  1573. }
  1574. static int snd_rme32_control_spdif_put(struct snd_kcontrol *kcontrol,
  1575. struct snd_ctl_elem_value *ucontrol)
  1576. {
  1577. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1578. int change;
  1579. u32 val;
  1580. val = snd_rme32_convert_from_aes(&ucontrol->value.iec958);
  1581. spin_lock_irq(&rme32->lock);
  1582. change = val != rme32->wcreg_spdif;
  1583. rme32->wcreg_spdif = val;
  1584. spin_unlock_irq(&rme32->lock);
  1585. return change;
  1586. }
  1587. static int snd_rme32_control_spdif_stream_info(struct snd_kcontrol *kcontrol,
  1588. struct snd_ctl_elem_info *uinfo)
  1589. {
  1590. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1591. uinfo->count = 1;
  1592. return 0;
  1593. }
  1594. static int snd_rme32_control_spdif_stream_get(struct snd_kcontrol *kcontrol,
  1595. struct snd_ctl_elem_value *
  1596. ucontrol)
  1597. {
  1598. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1599. snd_rme32_convert_to_aes(&ucontrol->value.iec958,
  1600. rme32->wcreg_spdif_stream);
  1601. return 0;
  1602. }
  1603. static int snd_rme32_control_spdif_stream_put(struct snd_kcontrol *kcontrol,
  1604. struct snd_ctl_elem_value *
  1605. ucontrol)
  1606. {
  1607. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1608. int change;
  1609. u32 val;
  1610. val = snd_rme32_convert_from_aes(&ucontrol->value.iec958);
  1611. spin_lock_irq(&rme32->lock);
  1612. change = val != rme32->wcreg_spdif_stream;
  1613. rme32->wcreg_spdif_stream = val;
  1614. rme32->wcreg &= ~(RME32_WCR_PRO | RME32_WCR_EMP);
  1615. rme32->wcreg |= val;
  1616. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  1617. spin_unlock_irq(&rme32->lock);
  1618. return change;
  1619. }
  1620. static int snd_rme32_control_spdif_mask_info(struct snd_kcontrol *kcontrol,
  1621. struct snd_ctl_elem_info *uinfo)
  1622. {
  1623. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1624. uinfo->count = 1;
  1625. return 0;
  1626. }
  1627. static int snd_rme32_control_spdif_mask_get(struct snd_kcontrol *kcontrol,
  1628. struct snd_ctl_elem_value *
  1629. ucontrol)
  1630. {
  1631. ucontrol->value.iec958.status[0] = kcontrol->private_value;
  1632. return 0;
  1633. }
  1634. static struct snd_kcontrol_new snd_rme32_controls[] = {
  1635. {
  1636. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1637. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  1638. .info = snd_rme32_control_spdif_info,
  1639. .get = snd_rme32_control_spdif_get,
  1640. .put = snd_rme32_control_spdif_put
  1641. },
  1642. {
  1643. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  1644. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1645. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
  1646. .info = snd_rme32_control_spdif_stream_info,
  1647. .get = snd_rme32_control_spdif_stream_get,
  1648. .put = snd_rme32_control_spdif_stream_put
  1649. },
  1650. {
  1651. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1652. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1653. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
  1654. .info = snd_rme32_control_spdif_mask_info,
  1655. .get = snd_rme32_control_spdif_mask_get,
  1656. .private_value = IEC958_AES0_PROFESSIONAL | IEC958_AES0_CON_EMPHASIS
  1657. },
  1658. {
  1659. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1660. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1661. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
  1662. .info = snd_rme32_control_spdif_mask_info,
  1663. .get = snd_rme32_control_spdif_mask_get,
  1664. .private_value = IEC958_AES0_PROFESSIONAL | IEC958_AES0_PRO_EMPHASIS
  1665. },
  1666. {
  1667. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1668. .name = "Input Connector",
  1669. .info = snd_rme32_info_inputtype_control,
  1670. .get = snd_rme32_get_inputtype_control,
  1671. .put = snd_rme32_put_inputtype_control
  1672. },
  1673. {
  1674. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1675. .name = "Loopback Input",
  1676. .info = snd_rme32_info_loopback_control,
  1677. .get = snd_rme32_get_loopback_control,
  1678. .put = snd_rme32_put_loopback_control
  1679. },
  1680. {
  1681. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1682. .name = "Sample Clock Source",
  1683. .info = snd_rme32_info_clockmode_control,
  1684. .get = snd_rme32_get_clockmode_control,
  1685. .put = snd_rme32_put_clockmode_control
  1686. }
  1687. };
  1688. static int snd_rme32_create_switches(struct snd_card *card, struct rme32 * rme32)
  1689. {
  1690. int idx, err;
  1691. struct snd_kcontrol *kctl;
  1692. for (idx = 0; idx < (int)ARRAY_SIZE(snd_rme32_controls); idx++) {
  1693. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme32_controls[idx], rme32))) < 0)
  1694. return err;
  1695. if (idx == 1) /* IEC958 (S/PDIF) Stream */
  1696. rme32->spdif_ctl = kctl;
  1697. }
  1698. return 0;
  1699. }
  1700. /*
  1701. * Card initialisation
  1702. */
  1703. static void snd_rme32_card_free(struct snd_card *card)
  1704. {
  1705. snd_rme32_free(card->private_data);
  1706. }
  1707. static int
  1708. snd_rme32_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
  1709. {
  1710. static int dev;
  1711. struct rme32 *rme32;
  1712. struct snd_card *card;
  1713. int err;
  1714. if (dev >= SNDRV_CARDS) {
  1715. return -ENODEV;
  1716. }
  1717. if (!enable[dev]) {
  1718. dev++;
  1719. return -ENOENT;
  1720. }
  1721. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  1722. sizeof(struct rme32), &card);
  1723. if (err < 0)
  1724. return err;
  1725. card->private_free = snd_rme32_card_free;
  1726. rme32 = (struct rme32 *) card->private_data;
  1727. rme32->card = card;
  1728. rme32->pci = pci;
  1729. if (fullduplex[dev])
  1730. rme32->fullduplex_mode = 1;
  1731. if ((err = snd_rme32_create(rme32)) < 0) {
  1732. snd_card_free(card);
  1733. return err;
  1734. }
  1735. strcpy(card->driver, "Digi32");
  1736. switch (rme32->pci->device) {
  1737. case PCI_DEVICE_ID_RME_DIGI32:
  1738. strcpy(card->shortname, "RME Digi32");
  1739. break;
  1740. case PCI_DEVICE_ID_RME_DIGI32_8:
  1741. strcpy(card->shortname, "RME Digi32/8");
  1742. break;
  1743. case PCI_DEVICE_ID_RME_DIGI32_PRO:
  1744. strcpy(card->shortname, "RME Digi32 PRO");
  1745. break;
  1746. }
  1747. sprintf(card->longname, "%s (Rev. %d) at 0x%lx, irq %d",
  1748. card->shortname, rme32->rev, rme32->port, rme32->irq);
  1749. if ((err = snd_card_register(card)) < 0) {
  1750. snd_card_free(card);
  1751. return err;
  1752. }
  1753. pci_set_drvdata(pci, card);
  1754. dev++;
  1755. return 0;
  1756. }
  1757. static void snd_rme32_remove(struct pci_dev *pci)
  1758. {
  1759. snd_card_free(pci_get_drvdata(pci));
  1760. }
  1761. static struct pci_driver rme32_driver = {
  1762. .name = KBUILD_MODNAME,
  1763. .id_table = snd_rme32_ids,
  1764. .probe = snd_rme32_probe,
  1765. .remove = snd_rme32_remove,
  1766. };
  1767. module_pci_driver(rme32_driver);