rme32.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  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/moduleparam.h>
  76. #include <sound/core.h>
  77. #include <sound/info.h>
  78. #include <sound/control.h>
  79. #include <sound/pcm.h>
  80. #include <sound/pcm_params.h>
  81. #include <sound/pcm-indirect.h>
  82. #include <sound/asoundef.h>
  83. #include <sound/initval.h>
  84. #include <asm/io.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 int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  88. static int 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 DEFINE_PCI_DEVICE_TABLE(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, int 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_minmax(runtime,
  754. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  755. RME32_BUFFER_SIZE, 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(&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 __devinit 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. snd_printk(KERN_ERR "unable to remap memory region 0x%lx-0x%lx\n",
  1210. rme32->port, rme32->port + RME32_IO_SIZE - 1);
  1211. return -ENOMEM;
  1212. }
  1213. if (request_irq(pci->irq, snd_rme32_interrupt, IRQF_SHARED,
  1214. "RME32", rme32)) {
  1215. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1216. return -EBUSY;
  1217. }
  1218. rme32->irq = pci->irq;
  1219. /* read the card's revision number */
  1220. pci_read_config_byte(pci, 8, &rme32->rev);
  1221. /* set up ALSA pcm device for S/PDIF */
  1222. if ((err = snd_pcm_new(rme32->card, "Digi32 IEC958", 0, 1, 1, &rme32->spdif_pcm)) < 0) {
  1223. return err;
  1224. }
  1225. rme32->spdif_pcm->private_data = rme32;
  1226. rme32->spdif_pcm->private_free = snd_rme32_free_spdif_pcm;
  1227. strcpy(rme32->spdif_pcm->name, "Digi32 IEC958");
  1228. if (rme32->fullduplex_mode) {
  1229. snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1230. &snd_rme32_playback_spdif_fd_ops);
  1231. snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_CAPTURE,
  1232. &snd_rme32_capture_spdif_fd_ops);
  1233. snd_pcm_lib_preallocate_pages_for_all(rme32->spdif_pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  1234. snd_dma_continuous_data(GFP_KERNEL),
  1235. 0, RME32_MID_BUFFER_SIZE);
  1236. rme32->spdif_pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  1237. } else {
  1238. snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1239. &snd_rme32_playback_spdif_ops);
  1240. snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_CAPTURE,
  1241. &snd_rme32_capture_spdif_ops);
  1242. rme32->spdif_pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
  1243. }
  1244. /* set up ALSA pcm device for ADAT */
  1245. if ((pci->device == PCI_DEVICE_ID_RME_DIGI32) ||
  1246. (pci->device == PCI_DEVICE_ID_RME_DIGI32_PRO)) {
  1247. /* ADAT is not available on DIGI32 and DIGI32 Pro */
  1248. rme32->adat_pcm = NULL;
  1249. }
  1250. else {
  1251. if ((err = snd_pcm_new(rme32->card, "Digi32 ADAT", 1,
  1252. 1, 1, &rme32->adat_pcm)) < 0)
  1253. {
  1254. return err;
  1255. }
  1256. rme32->adat_pcm->private_data = rme32;
  1257. rme32->adat_pcm->private_free = snd_rme32_free_adat_pcm;
  1258. strcpy(rme32->adat_pcm->name, "Digi32 ADAT");
  1259. if (rme32->fullduplex_mode) {
  1260. snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1261. &snd_rme32_playback_adat_fd_ops);
  1262. snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_CAPTURE,
  1263. &snd_rme32_capture_adat_fd_ops);
  1264. snd_pcm_lib_preallocate_pages_for_all(rme32->adat_pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  1265. snd_dma_continuous_data(GFP_KERNEL),
  1266. 0, RME32_MID_BUFFER_SIZE);
  1267. rme32->adat_pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  1268. } else {
  1269. snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1270. &snd_rme32_playback_adat_ops);
  1271. snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_CAPTURE,
  1272. &snd_rme32_capture_adat_ops);
  1273. rme32->adat_pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
  1274. }
  1275. }
  1276. rme32->playback_periodsize = 0;
  1277. rme32->capture_periodsize = 0;
  1278. /* make sure playback/capture is stopped, if by some reason active */
  1279. snd_rme32_pcm_stop(rme32, 0);
  1280. /* reset DAC */
  1281. snd_rme32_reset_dac(rme32);
  1282. /* reset buffer pointer */
  1283. writel(0, rme32->iobase + RME32_IO_RESET_POS);
  1284. /* set default values in registers */
  1285. rme32->wcreg = RME32_WCR_SEL | /* normal playback */
  1286. RME32_WCR_INP_0 | /* input select */
  1287. RME32_WCR_MUTE; /* muting on */
  1288. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  1289. /* init switch interface */
  1290. if ((err = snd_rme32_create_switches(rme32->card, rme32)) < 0) {
  1291. return err;
  1292. }
  1293. /* init proc interface */
  1294. snd_rme32_proc_init(rme32);
  1295. rme32->capture_substream = NULL;
  1296. rme32->playback_substream = NULL;
  1297. return 0;
  1298. }
  1299. /*
  1300. * proc interface
  1301. */
  1302. static void
  1303. snd_rme32_proc_read(struct snd_info_entry * entry, struct snd_info_buffer *buffer)
  1304. {
  1305. int n;
  1306. struct rme32 *rme32 = (struct rme32 *) entry->private_data;
  1307. rme32->rcreg = readl(rme32->iobase + RME32_IO_CONTROL_REGISTER);
  1308. snd_iprintf(buffer, rme32->card->longname);
  1309. snd_iprintf(buffer, " (index #%d)\n", rme32->card->number + 1);
  1310. snd_iprintf(buffer, "\nGeneral settings\n");
  1311. if (rme32->fullduplex_mode)
  1312. snd_iprintf(buffer, " Full-duplex mode\n");
  1313. else
  1314. snd_iprintf(buffer, " Half-duplex mode\n");
  1315. if (RME32_PRO_WITH_8414(rme32)) {
  1316. snd_iprintf(buffer, " receiver: CS8414\n");
  1317. } else {
  1318. snd_iprintf(buffer, " receiver: CS8412\n");
  1319. }
  1320. if (rme32->wcreg & RME32_WCR_MODE24) {
  1321. snd_iprintf(buffer, " format: 24 bit");
  1322. } else {
  1323. snd_iprintf(buffer, " format: 16 bit");
  1324. }
  1325. if (rme32->wcreg & RME32_WCR_MONO) {
  1326. snd_iprintf(buffer, ", Mono\n");
  1327. } else {
  1328. snd_iprintf(buffer, ", Stereo\n");
  1329. }
  1330. snd_iprintf(buffer, "\nInput settings\n");
  1331. switch (snd_rme32_getinputtype(rme32)) {
  1332. case RME32_INPUT_OPTICAL:
  1333. snd_iprintf(buffer, " input: optical");
  1334. break;
  1335. case RME32_INPUT_COAXIAL:
  1336. snd_iprintf(buffer, " input: coaxial");
  1337. break;
  1338. case RME32_INPUT_INTERNAL:
  1339. snd_iprintf(buffer, " input: internal");
  1340. break;
  1341. case RME32_INPUT_XLR:
  1342. snd_iprintf(buffer, " input: XLR");
  1343. break;
  1344. }
  1345. if (snd_rme32_capture_getrate(rme32, &n) < 0) {
  1346. snd_iprintf(buffer, "\n sample rate: no valid signal\n");
  1347. } else {
  1348. if (n) {
  1349. snd_iprintf(buffer, " (8 channels)\n");
  1350. } else {
  1351. snd_iprintf(buffer, " (2 channels)\n");
  1352. }
  1353. snd_iprintf(buffer, " sample rate: %d Hz\n",
  1354. snd_rme32_capture_getrate(rme32, &n));
  1355. }
  1356. snd_iprintf(buffer, "\nOutput settings\n");
  1357. if (rme32->wcreg & RME32_WCR_SEL) {
  1358. snd_iprintf(buffer, " output signal: normal playback");
  1359. } else {
  1360. snd_iprintf(buffer, " output signal: same as input");
  1361. }
  1362. if (rme32->wcreg & RME32_WCR_MUTE) {
  1363. snd_iprintf(buffer, " (muted)\n");
  1364. } else {
  1365. snd_iprintf(buffer, "\n");
  1366. }
  1367. /* master output frequency */
  1368. if (!
  1369. ((!(rme32->wcreg & RME32_WCR_FREQ_0))
  1370. && (!(rme32->wcreg & RME32_WCR_FREQ_1)))) {
  1371. snd_iprintf(buffer, " sample rate: %d Hz\n",
  1372. snd_rme32_playback_getrate(rme32));
  1373. }
  1374. if (rme32->rcreg & RME32_RCR_KMODE) {
  1375. snd_iprintf(buffer, " sample clock source: AutoSync\n");
  1376. } else {
  1377. snd_iprintf(buffer, " sample clock source: Internal\n");
  1378. }
  1379. if (rme32->wcreg & RME32_WCR_PRO) {
  1380. snd_iprintf(buffer, " format: AES/EBU (professional)\n");
  1381. } else {
  1382. snd_iprintf(buffer, " format: IEC958 (consumer)\n");
  1383. }
  1384. if (rme32->wcreg & RME32_WCR_EMP) {
  1385. snd_iprintf(buffer, " emphasis: on\n");
  1386. } else {
  1387. snd_iprintf(buffer, " emphasis: off\n");
  1388. }
  1389. }
  1390. static void __devinit snd_rme32_proc_init(struct rme32 * rme32)
  1391. {
  1392. struct snd_info_entry *entry;
  1393. if (! snd_card_proc_new(rme32->card, "rme32", &entry))
  1394. snd_info_set_text_ops(entry, rme32, snd_rme32_proc_read);
  1395. }
  1396. /*
  1397. * control interface
  1398. */
  1399. #define snd_rme32_info_loopback_control snd_ctl_boolean_mono_info
  1400. static int
  1401. snd_rme32_get_loopback_control(struct snd_kcontrol *kcontrol,
  1402. struct snd_ctl_elem_value *ucontrol)
  1403. {
  1404. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1405. spin_lock_irq(&rme32->lock);
  1406. ucontrol->value.integer.value[0] =
  1407. rme32->wcreg & RME32_WCR_SEL ? 0 : 1;
  1408. spin_unlock_irq(&rme32->lock);
  1409. return 0;
  1410. }
  1411. static int
  1412. snd_rme32_put_loopback_control(struct snd_kcontrol *kcontrol,
  1413. struct snd_ctl_elem_value *ucontrol)
  1414. {
  1415. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1416. unsigned int val;
  1417. int change;
  1418. val = ucontrol->value.integer.value[0] ? 0 : RME32_WCR_SEL;
  1419. spin_lock_irq(&rme32->lock);
  1420. val = (rme32->wcreg & ~RME32_WCR_SEL) | val;
  1421. change = val != rme32->wcreg;
  1422. if (ucontrol->value.integer.value[0])
  1423. val &= ~RME32_WCR_MUTE;
  1424. else
  1425. val |= RME32_WCR_MUTE;
  1426. rme32->wcreg = val;
  1427. writel(val, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  1428. spin_unlock_irq(&rme32->lock);
  1429. return change;
  1430. }
  1431. static int
  1432. snd_rme32_info_inputtype_control(struct snd_kcontrol *kcontrol,
  1433. struct snd_ctl_elem_info *uinfo)
  1434. {
  1435. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1436. static char *texts[4] = { "Optical", "Coaxial", "Internal", "XLR" };
  1437. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1438. uinfo->count = 1;
  1439. switch (rme32->pci->device) {
  1440. case PCI_DEVICE_ID_RME_DIGI32:
  1441. case PCI_DEVICE_ID_RME_DIGI32_8:
  1442. uinfo->value.enumerated.items = 3;
  1443. break;
  1444. case PCI_DEVICE_ID_RME_DIGI32_PRO:
  1445. uinfo->value.enumerated.items = 4;
  1446. break;
  1447. default:
  1448. snd_BUG();
  1449. break;
  1450. }
  1451. if (uinfo->value.enumerated.item >
  1452. uinfo->value.enumerated.items - 1) {
  1453. uinfo->value.enumerated.item =
  1454. uinfo->value.enumerated.items - 1;
  1455. }
  1456. strcpy(uinfo->value.enumerated.name,
  1457. texts[uinfo->value.enumerated.item]);
  1458. return 0;
  1459. }
  1460. static int
  1461. snd_rme32_get_inputtype_control(struct snd_kcontrol *kcontrol,
  1462. struct snd_ctl_elem_value *ucontrol)
  1463. {
  1464. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1465. unsigned int items = 3;
  1466. spin_lock_irq(&rme32->lock);
  1467. ucontrol->value.enumerated.item[0] = snd_rme32_getinputtype(rme32);
  1468. switch (rme32->pci->device) {
  1469. case PCI_DEVICE_ID_RME_DIGI32:
  1470. case PCI_DEVICE_ID_RME_DIGI32_8:
  1471. items = 3;
  1472. break;
  1473. case PCI_DEVICE_ID_RME_DIGI32_PRO:
  1474. items = 4;
  1475. break;
  1476. default:
  1477. snd_BUG();
  1478. break;
  1479. }
  1480. if (ucontrol->value.enumerated.item[0] >= items) {
  1481. ucontrol->value.enumerated.item[0] = items - 1;
  1482. }
  1483. spin_unlock_irq(&rme32->lock);
  1484. return 0;
  1485. }
  1486. static int
  1487. snd_rme32_put_inputtype_control(struct snd_kcontrol *kcontrol,
  1488. struct snd_ctl_elem_value *ucontrol)
  1489. {
  1490. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1491. unsigned int val;
  1492. int change, items = 3;
  1493. switch (rme32->pci->device) {
  1494. case PCI_DEVICE_ID_RME_DIGI32:
  1495. case PCI_DEVICE_ID_RME_DIGI32_8:
  1496. items = 3;
  1497. break;
  1498. case PCI_DEVICE_ID_RME_DIGI32_PRO:
  1499. items = 4;
  1500. break;
  1501. default:
  1502. snd_BUG();
  1503. break;
  1504. }
  1505. val = ucontrol->value.enumerated.item[0] % items;
  1506. spin_lock_irq(&rme32->lock);
  1507. change = val != (unsigned int)snd_rme32_getinputtype(rme32);
  1508. snd_rme32_setinputtype(rme32, val);
  1509. spin_unlock_irq(&rme32->lock);
  1510. return change;
  1511. }
  1512. static int
  1513. snd_rme32_info_clockmode_control(struct snd_kcontrol *kcontrol,
  1514. struct snd_ctl_elem_info *uinfo)
  1515. {
  1516. static char *texts[4] = { "AutoSync",
  1517. "Internal 32.0kHz",
  1518. "Internal 44.1kHz",
  1519. "Internal 48.0kHz" };
  1520. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1521. uinfo->count = 1;
  1522. uinfo->value.enumerated.items = 4;
  1523. if (uinfo->value.enumerated.item > 3) {
  1524. uinfo->value.enumerated.item = 3;
  1525. }
  1526. strcpy(uinfo->value.enumerated.name,
  1527. texts[uinfo->value.enumerated.item]);
  1528. return 0;
  1529. }
  1530. static int
  1531. snd_rme32_get_clockmode_control(struct snd_kcontrol *kcontrol,
  1532. struct snd_ctl_elem_value *ucontrol)
  1533. {
  1534. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1535. spin_lock_irq(&rme32->lock);
  1536. ucontrol->value.enumerated.item[0] = snd_rme32_getclockmode(rme32);
  1537. spin_unlock_irq(&rme32->lock);
  1538. return 0;
  1539. }
  1540. static int
  1541. snd_rme32_put_clockmode_control(struct snd_kcontrol *kcontrol,
  1542. struct snd_ctl_elem_value *ucontrol)
  1543. {
  1544. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1545. unsigned int val;
  1546. int change;
  1547. val = ucontrol->value.enumerated.item[0] % 3;
  1548. spin_lock_irq(&rme32->lock);
  1549. change = val != (unsigned int)snd_rme32_getclockmode(rme32);
  1550. snd_rme32_setclockmode(rme32, val);
  1551. spin_unlock_irq(&rme32->lock);
  1552. return change;
  1553. }
  1554. static u32 snd_rme32_convert_from_aes(struct snd_aes_iec958 * aes)
  1555. {
  1556. u32 val = 0;
  1557. val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? RME32_WCR_PRO : 0;
  1558. if (val & RME32_WCR_PRO)
  1559. val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? RME32_WCR_EMP : 0;
  1560. else
  1561. val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? RME32_WCR_EMP : 0;
  1562. return val;
  1563. }
  1564. static void snd_rme32_convert_to_aes(struct snd_aes_iec958 * aes, u32 val)
  1565. {
  1566. aes->status[0] = ((val & RME32_WCR_PRO) ? IEC958_AES0_PROFESSIONAL : 0);
  1567. if (val & RME32_WCR_PRO)
  1568. aes->status[0] |= (val & RME32_WCR_EMP) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
  1569. else
  1570. aes->status[0] |= (val & RME32_WCR_EMP) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
  1571. }
  1572. static int snd_rme32_control_spdif_info(struct snd_kcontrol *kcontrol,
  1573. struct snd_ctl_elem_info *uinfo)
  1574. {
  1575. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1576. uinfo->count = 1;
  1577. return 0;
  1578. }
  1579. static int snd_rme32_control_spdif_get(struct snd_kcontrol *kcontrol,
  1580. struct snd_ctl_elem_value *ucontrol)
  1581. {
  1582. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1583. snd_rme32_convert_to_aes(&ucontrol->value.iec958,
  1584. rme32->wcreg_spdif);
  1585. return 0;
  1586. }
  1587. static int snd_rme32_control_spdif_put(struct snd_kcontrol *kcontrol,
  1588. struct snd_ctl_elem_value *ucontrol)
  1589. {
  1590. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1591. int change;
  1592. u32 val;
  1593. val = snd_rme32_convert_from_aes(&ucontrol->value.iec958);
  1594. spin_lock_irq(&rme32->lock);
  1595. change = val != rme32->wcreg_spdif;
  1596. rme32->wcreg_spdif = val;
  1597. spin_unlock_irq(&rme32->lock);
  1598. return change;
  1599. }
  1600. static int snd_rme32_control_spdif_stream_info(struct snd_kcontrol *kcontrol,
  1601. struct snd_ctl_elem_info *uinfo)
  1602. {
  1603. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1604. uinfo->count = 1;
  1605. return 0;
  1606. }
  1607. static int snd_rme32_control_spdif_stream_get(struct snd_kcontrol *kcontrol,
  1608. struct snd_ctl_elem_value *
  1609. ucontrol)
  1610. {
  1611. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1612. snd_rme32_convert_to_aes(&ucontrol->value.iec958,
  1613. rme32->wcreg_spdif_stream);
  1614. return 0;
  1615. }
  1616. static int snd_rme32_control_spdif_stream_put(struct snd_kcontrol *kcontrol,
  1617. struct snd_ctl_elem_value *
  1618. ucontrol)
  1619. {
  1620. struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
  1621. int change;
  1622. u32 val;
  1623. val = snd_rme32_convert_from_aes(&ucontrol->value.iec958);
  1624. spin_lock_irq(&rme32->lock);
  1625. change = val != rme32->wcreg_spdif_stream;
  1626. rme32->wcreg_spdif_stream = val;
  1627. rme32->wcreg &= ~(RME32_WCR_PRO | RME32_WCR_EMP);
  1628. rme32->wcreg |= val;
  1629. writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
  1630. spin_unlock_irq(&rme32->lock);
  1631. return change;
  1632. }
  1633. static int snd_rme32_control_spdif_mask_info(struct snd_kcontrol *kcontrol,
  1634. struct snd_ctl_elem_info *uinfo)
  1635. {
  1636. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1637. uinfo->count = 1;
  1638. return 0;
  1639. }
  1640. static int snd_rme32_control_spdif_mask_get(struct snd_kcontrol *kcontrol,
  1641. struct snd_ctl_elem_value *
  1642. ucontrol)
  1643. {
  1644. ucontrol->value.iec958.status[0] = kcontrol->private_value;
  1645. return 0;
  1646. }
  1647. static struct snd_kcontrol_new snd_rme32_controls[] = {
  1648. {
  1649. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1650. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  1651. .info = snd_rme32_control_spdif_info,
  1652. .get = snd_rme32_control_spdif_get,
  1653. .put = snd_rme32_control_spdif_put
  1654. },
  1655. {
  1656. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  1657. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1658. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
  1659. .info = snd_rme32_control_spdif_stream_info,
  1660. .get = snd_rme32_control_spdif_stream_get,
  1661. .put = snd_rme32_control_spdif_stream_put
  1662. },
  1663. {
  1664. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1665. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1666. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
  1667. .info = snd_rme32_control_spdif_mask_info,
  1668. .get = snd_rme32_control_spdif_mask_get,
  1669. .private_value = IEC958_AES0_PROFESSIONAL | IEC958_AES0_CON_EMPHASIS
  1670. },
  1671. {
  1672. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1673. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1674. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
  1675. .info = snd_rme32_control_spdif_mask_info,
  1676. .get = snd_rme32_control_spdif_mask_get,
  1677. .private_value = IEC958_AES0_PROFESSIONAL | IEC958_AES0_PRO_EMPHASIS
  1678. },
  1679. {
  1680. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1681. .name = "Input Connector",
  1682. .info = snd_rme32_info_inputtype_control,
  1683. .get = snd_rme32_get_inputtype_control,
  1684. .put = snd_rme32_put_inputtype_control
  1685. },
  1686. {
  1687. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1688. .name = "Loopback Input",
  1689. .info = snd_rme32_info_loopback_control,
  1690. .get = snd_rme32_get_loopback_control,
  1691. .put = snd_rme32_put_loopback_control
  1692. },
  1693. {
  1694. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1695. .name = "Sample Clock Source",
  1696. .info = snd_rme32_info_clockmode_control,
  1697. .get = snd_rme32_get_clockmode_control,
  1698. .put = snd_rme32_put_clockmode_control
  1699. }
  1700. };
  1701. static int snd_rme32_create_switches(struct snd_card *card, struct rme32 * rme32)
  1702. {
  1703. int idx, err;
  1704. struct snd_kcontrol *kctl;
  1705. for (idx = 0; idx < (int)ARRAY_SIZE(snd_rme32_controls); idx++) {
  1706. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme32_controls[idx], rme32))) < 0)
  1707. return err;
  1708. if (idx == 1) /* IEC958 (S/PDIF) Stream */
  1709. rme32->spdif_ctl = kctl;
  1710. }
  1711. return 0;
  1712. }
  1713. /*
  1714. * Card initialisation
  1715. */
  1716. static void snd_rme32_card_free(struct snd_card *card)
  1717. {
  1718. snd_rme32_free(card->private_data);
  1719. }
  1720. static int __devinit
  1721. snd_rme32_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
  1722. {
  1723. static int dev;
  1724. struct rme32 *rme32;
  1725. struct snd_card *card;
  1726. int err;
  1727. if (dev >= SNDRV_CARDS) {
  1728. return -ENODEV;
  1729. }
  1730. if (!enable[dev]) {
  1731. dev++;
  1732. return -ENOENT;
  1733. }
  1734. err = snd_card_create(index[dev], id[dev], THIS_MODULE,
  1735. sizeof(struct rme32), &card);
  1736. if (err < 0)
  1737. return err;
  1738. card->private_free = snd_rme32_card_free;
  1739. rme32 = (struct rme32 *) card->private_data;
  1740. rme32->card = card;
  1741. rme32->pci = pci;
  1742. snd_card_set_dev(card, &pci->dev);
  1743. if (fullduplex[dev])
  1744. rme32->fullduplex_mode = 1;
  1745. if ((err = snd_rme32_create(rme32)) < 0) {
  1746. snd_card_free(card);
  1747. return err;
  1748. }
  1749. strcpy(card->driver, "Digi32");
  1750. switch (rme32->pci->device) {
  1751. case PCI_DEVICE_ID_RME_DIGI32:
  1752. strcpy(card->shortname, "RME Digi32");
  1753. break;
  1754. case PCI_DEVICE_ID_RME_DIGI32_8:
  1755. strcpy(card->shortname, "RME Digi32/8");
  1756. break;
  1757. case PCI_DEVICE_ID_RME_DIGI32_PRO:
  1758. strcpy(card->shortname, "RME Digi32 PRO");
  1759. break;
  1760. }
  1761. sprintf(card->longname, "%s (Rev. %d) at 0x%lx, irq %d",
  1762. card->shortname, rme32->rev, rme32->port, rme32->irq);
  1763. if ((err = snd_card_register(card)) < 0) {
  1764. snd_card_free(card);
  1765. return err;
  1766. }
  1767. pci_set_drvdata(pci, card);
  1768. dev++;
  1769. return 0;
  1770. }
  1771. static void __devexit snd_rme32_remove(struct pci_dev *pci)
  1772. {
  1773. snd_card_free(pci_get_drvdata(pci));
  1774. pci_set_drvdata(pci, NULL);
  1775. }
  1776. static struct pci_driver driver = {
  1777. .name = "RME Digi32",
  1778. .id_table = snd_rme32_ids,
  1779. .probe = snd_rme32_probe,
  1780. .remove = __devexit_p(snd_rme32_remove),
  1781. };
  1782. static int __init alsa_card_rme32_init(void)
  1783. {
  1784. return pci_register_driver(&driver);
  1785. }
  1786. static void __exit alsa_card_rme32_exit(void)
  1787. {
  1788. pci_unregister_driver(&driver);
  1789. }
  1790. module_init(alsa_card_rme32_init)
  1791. module_exit(alsa_card_rme32_exit)