rme96.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525
  1. /*
  2. * ALSA driver for RME Digi96, Digi96/8 and Digi96/8 PRO/PAD/PST audio
  3. * interfaces
  4. *
  5. * Copyright (c) 2000, 2001 Anders Torger <torger@ludd.luth.se>
  6. *
  7. * Thanks to Henk Hesselink <henk@anda.nl> for the analog volume control
  8. * code.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/delay.h>
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/pci.h>
  29. #include <linux/module.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/io.h>
  32. #include <sound/core.h>
  33. #include <sound/info.h>
  34. #include <sound/control.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include <sound/asoundef.h>
  38. #include <sound/initval.h>
  39. /* note, two last pcis should be equal, it is not a bug */
  40. MODULE_AUTHOR("Anders Torger <torger@ludd.luth.se>");
  41. MODULE_DESCRIPTION("RME Digi96, Digi96/8, Digi96/8 PRO, Digi96/8 PST, "
  42. "Digi96/8 PAD");
  43. MODULE_LICENSE("GPL");
  44. MODULE_SUPPORTED_DEVICE("{{RME,Digi96},"
  45. "{RME,Digi96/8},"
  46. "{RME,Digi96/8 PRO},"
  47. "{RME,Digi96/8 PST},"
  48. "{RME,Digi96/8 PAD}}");
  49. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  50. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  51. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  52. module_param_array(index, int, NULL, 0444);
  53. MODULE_PARM_DESC(index, "Index value for RME Digi96 soundcard.");
  54. module_param_array(id, charp, NULL, 0444);
  55. MODULE_PARM_DESC(id, "ID string for RME Digi96 soundcard.");
  56. module_param_array(enable, bool, NULL, 0444);
  57. MODULE_PARM_DESC(enable, "Enable RME Digi96 soundcard.");
  58. /*
  59. * Defines for RME Digi96 series, from internal RME reference documents
  60. * dated 12.01.00
  61. */
  62. #define RME96_SPDIF_NCHANNELS 2
  63. /* Playback and capture buffer size */
  64. #define RME96_BUFFER_SIZE 0x10000
  65. /* IO area size */
  66. #define RME96_IO_SIZE 0x60000
  67. /* IO area offsets */
  68. #define RME96_IO_PLAY_BUFFER 0x0
  69. #define RME96_IO_REC_BUFFER 0x10000
  70. #define RME96_IO_CONTROL_REGISTER 0x20000
  71. #define RME96_IO_ADDITIONAL_REG 0x20004
  72. #define RME96_IO_CONFIRM_PLAY_IRQ 0x20008
  73. #define RME96_IO_CONFIRM_REC_IRQ 0x2000C
  74. #define RME96_IO_SET_PLAY_POS 0x40000
  75. #define RME96_IO_RESET_PLAY_POS 0x4FFFC
  76. #define RME96_IO_SET_REC_POS 0x50000
  77. #define RME96_IO_RESET_REC_POS 0x5FFFC
  78. #define RME96_IO_GET_PLAY_POS 0x20000
  79. #define RME96_IO_GET_REC_POS 0x30000
  80. /* Write control register bits */
  81. #define RME96_WCR_START (1 << 0)
  82. #define RME96_WCR_START_2 (1 << 1)
  83. #define RME96_WCR_GAIN_0 (1 << 2)
  84. #define RME96_WCR_GAIN_1 (1 << 3)
  85. #define RME96_WCR_MODE24 (1 << 4)
  86. #define RME96_WCR_MODE24_2 (1 << 5)
  87. #define RME96_WCR_BM (1 << 6)
  88. #define RME96_WCR_BM_2 (1 << 7)
  89. #define RME96_WCR_ADAT (1 << 8)
  90. #define RME96_WCR_FREQ_0 (1 << 9)
  91. #define RME96_WCR_FREQ_1 (1 << 10)
  92. #define RME96_WCR_DS (1 << 11)
  93. #define RME96_WCR_PRO (1 << 12)
  94. #define RME96_WCR_EMP (1 << 13)
  95. #define RME96_WCR_SEL (1 << 14)
  96. #define RME96_WCR_MASTER (1 << 15)
  97. #define RME96_WCR_PD (1 << 16)
  98. #define RME96_WCR_INP_0 (1 << 17)
  99. #define RME96_WCR_INP_1 (1 << 18)
  100. #define RME96_WCR_THRU_0 (1 << 19)
  101. #define RME96_WCR_THRU_1 (1 << 20)
  102. #define RME96_WCR_THRU_2 (1 << 21)
  103. #define RME96_WCR_THRU_3 (1 << 22)
  104. #define RME96_WCR_THRU_4 (1 << 23)
  105. #define RME96_WCR_THRU_5 (1 << 24)
  106. #define RME96_WCR_THRU_6 (1 << 25)
  107. #define RME96_WCR_THRU_7 (1 << 26)
  108. #define RME96_WCR_DOLBY (1 << 27)
  109. #define RME96_WCR_MONITOR_0 (1 << 28)
  110. #define RME96_WCR_MONITOR_1 (1 << 29)
  111. #define RME96_WCR_ISEL (1 << 30)
  112. #define RME96_WCR_IDIS (1 << 31)
  113. #define RME96_WCR_BITPOS_GAIN_0 2
  114. #define RME96_WCR_BITPOS_GAIN_1 3
  115. #define RME96_WCR_BITPOS_FREQ_0 9
  116. #define RME96_WCR_BITPOS_FREQ_1 10
  117. #define RME96_WCR_BITPOS_INP_0 17
  118. #define RME96_WCR_BITPOS_INP_1 18
  119. #define RME96_WCR_BITPOS_MONITOR_0 28
  120. #define RME96_WCR_BITPOS_MONITOR_1 29
  121. /* Read control register bits */
  122. #define RME96_RCR_AUDIO_ADDR_MASK 0xFFFF
  123. #define RME96_RCR_IRQ_2 (1 << 16)
  124. #define RME96_RCR_T_OUT (1 << 17)
  125. #define RME96_RCR_DEV_ID_0 (1 << 21)
  126. #define RME96_RCR_DEV_ID_1 (1 << 22)
  127. #define RME96_RCR_LOCK (1 << 23)
  128. #define RME96_RCR_VERF (1 << 26)
  129. #define RME96_RCR_F0 (1 << 27)
  130. #define RME96_RCR_F1 (1 << 28)
  131. #define RME96_RCR_F2 (1 << 29)
  132. #define RME96_RCR_AUTOSYNC (1 << 30)
  133. #define RME96_RCR_IRQ (1 << 31)
  134. #define RME96_RCR_BITPOS_F0 27
  135. #define RME96_RCR_BITPOS_F1 28
  136. #define RME96_RCR_BITPOS_F2 29
  137. /* Additional register bits */
  138. #define RME96_AR_WSEL (1 << 0)
  139. #define RME96_AR_ANALOG (1 << 1)
  140. #define RME96_AR_FREQPAD_0 (1 << 2)
  141. #define RME96_AR_FREQPAD_1 (1 << 3)
  142. #define RME96_AR_FREQPAD_2 (1 << 4)
  143. #define RME96_AR_PD2 (1 << 5)
  144. #define RME96_AR_DAC_EN (1 << 6)
  145. #define RME96_AR_CLATCH (1 << 7)
  146. #define RME96_AR_CCLK (1 << 8)
  147. #define RME96_AR_CDATA (1 << 9)
  148. #define RME96_AR_BITPOS_F0 2
  149. #define RME96_AR_BITPOS_F1 3
  150. #define RME96_AR_BITPOS_F2 4
  151. /* Monitor tracks */
  152. #define RME96_MONITOR_TRACKS_1_2 0
  153. #define RME96_MONITOR_TRACKS_3_4 1
  154. #define RME96_MONITOR_TRACKS_5_6 2
  155. #define RME96_MONITOR_TRACKS_7_8 3
  156. /* Attenuation */
  157. #define RME96_ATTENUATION_0 0
  158. #define RME96_ATTENUATION_6 1
  159. #define RME96_ATTENUATION_12 2
  160. #define RME96_ATTENUATION_18 3
  161. /* Input types */
  162. #define RME96_INPUT_OPTICAL 0
  163. #define RME96_INPUT_COAXIAL 1
  164. #define RME96_INPUT_INTERNAL 2
  165. #define RME96_INPUT_XLR 3
  166. #define RME96_INPUT_ANALOG 4
  167. /* Clock modes */
  168. #define RME96_CLOCKMODE_SLAVE 0
  169. #define RME96_CLOCKMODE_MASTER 1
  170. #define RME96_CLOCKMODE_WORDCLOCK 2
  171. /* Block sizes in bytes */
  172. #define RME96_SMALL_BLOCK_SIZE 2048
  173. #define RME96_LARGE_BLOCK_SIZE 8192
  174. /* Volume control */
  175. #define RME96_AD1852_VOL_BITS 14
  176. #define RME96_AD1855_VOL_BITS 10
  177. /* Defines for snd_rme96_trigger */
  178. #define RME96_TB_START_PLAYBACK 1
  179. #define RME96_TB_START_CAPTURE 2
  180. #define RME96_TB_STOP_PLAYBACK 4
  181. #define RME96_TB_STOP_CAPTURE 8
  182. #define RME96_TB_RESET_PLAYPOS 16
  183. #define RME96_TB_RESET_CAPTUREPOS 32
  184. #define RME96_TB_CLEAR_PLAYBACK_IRQ 64
  185. #define RME96_TB_CLEAR_CAPTURE_IRQ 128
  186. #define RME96_RESUME_PLAYBACK (RME96_TB_START_PLAYBACK)
  187. #define RME96_RESUME_CAPTURE (RME96_TB_START_CAPTURE)
  188. #define RME96_RESUME_BOTH (RME96_RESUME_PLAYBACK \
  189. | RME96_RESUME_CAPTURE)
  190. #define RME96_START_PLAYBACK (RME96_TB_START_PLAYBACK \
  191. | RME96_TB_RESET_PLAYPOS)
  192. #define RME96_START_CAPTURE (RME96_TB_START_CAPTURE \
  193. | RME96_TB_RESET_CAPTUREPOS)
  194. #define RME96_START_BOTH (RME96_START_PLAYBACK \
  195. | RME96_START_CAPTURE)
  196. #define RME96_STOP_PLAYBACK (RME96_TB_STOP_PLAYBACK \
  197. | RME96_TB_CLEAR_PLAYBACK_IRQ)
  198. #define RME96_STOP_CAPTURE (RME96_TB_STOP_CAPTURE \
  199. | RME96_TB_CLEAR_CAPTURE_IRQ)
  200. #define RME96_STOP_BOTH (RME96_STOP_PLAYBACK \
  201. | RME96_STOP_CAPTURE)
  202. struct rme96 {
  203. spinlock_t lock;
  204. int irq;
  205. unsigned long port;
  206. void __iomem *iobase;
  207. u32 wcreg; /* cached write control register value */
  208. u32 wcreg_spdif; /* S/PDIF setup */
  209. u32 wcreg_spdif_stream; /* S/PDIF setup (temporary) */
  210. u32 rcreg; /* cached read control register value */
  211. u32 areg; /* cached additional register value */
  212. u16 vol[2]; /* cached volume of analog output */
  213. u8 rev; /* card revision number */
  214. #ifdef CONFIG_PM_SLEEP
  215. u32 playback_pointer;
  216. u32 capture_pointer;
  217. void *playback_suspend_buffer;
  218. void *capture_suspend_buffer;
  219. #endif
  220. struct snd_pcm_substream *playback_substream;
  221. struct snd_pcm_substream *capture_substream;
  222. int playback_frlog; /* log2 of framesize */
  223. int capture_frlog;
  224. size_t playback_periodsize; /* in bytes, zero if not used */
  225. size_t capture_periodsize; /* in bytes, zero if not used */
  226. struct snd_card *card;
  227. struct snd_pcm *spdif_pcm;
  228. struct snd_pcm *adat_pcm;
  229. struct pci_dev *pci;
  230. struct snd_kcontrol *spdif_ctl;
  231. };
  232. static const struct pci_device_id snd_rme96_ids[] = {
  233. { PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96), 0, },
  234. { PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96_8), 0, },
  235. { PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96_8_PRO), 0, },
  236. { PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST), 0, },
  237. { 0, }
  238. };
  239. MODULE_DEVICE_TABLE(pci, snd_rme96_ids);
  240. #define RME96_ISPLAYING(rme96) ((rme96)->wcreg & RME96_WCR_START)
  241. #define RME96_ISRECORDING(rme96) ((rme96)->wcreg & RME96_WCR_START_2)
  242. #define RME96_HAS_ANALOG_IN(rme96) ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST)
  243. #define RME96_HAS_ANALOG_OUT(rme96) ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PRO || \
  244. (rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST)
  245. #define RME96_DAC_IS_1852(rme96) (RME96_HAS_ANALOG_OUT(rme96) && (rme96)->rev >= 4)
  246. #define RME96_DAC_IS_1855(rme96) (((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST && (rme96)->rev < 4) || \
  247. ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PRO && (rme96)->rev == 2))
  248. #define RME96_185X_MAX_OUT(rme96) ((1 << (RME96_DAC_IS_1852(rme96) ? RME96_AD1852_VOL_BITS : RME96_AD1855_VOL_BITS)) - 1)
  249. static int
  250. snd_rme96_playback_prepare(struct snd_pcm_substream *substream);
  251. static int
  252. snd_rme96_capture_prepare(struct snd_pcm_substream *substream);
  253. static int
  254. snd_rme96_playback_trigger(struct snd_pcm_substream *substream,
  255. int cmd);
  256. static int
  257. snd_rme96_capture_trigger(struct snd_pcm_substream *substream,
  258. int cmd);
  259. static snd_pcm_uframes_t
  260. snd_rme96_playback_pointer(struct snd_pcm_substream *substream);
  261. static snd_pcm_uframes_t
  262. snd_rme96_capture_pointer(struct snd_pcm_substream *substream);
  263. static void snd_rme96_proc_init(struct rme96 *rme96);
  264. static int
  265. snd_rme96_create_switches(struct snd_card *card,
  266. struct rme96 *rme96);
  267. static int
  268. snd_rme96_getinputtype(struct rme96 *rme96);
  269. static inline unsigned int
  270. snd_rme96_playback_ptr(struct rme96 *rme96)
  271. {
  272. return (readl(rme96->iobase + RME96_IO_GET_PLAY_POS)
  273. & RME96_RCR_AUDIO_ADDR_MASK) >> rme96->playback_frlog;
  274. }
  275. static inline unsigned int
  276. snd_rme96_capture_ptr(struct rme96 *rme96)
  277. {
  278. return (readl(rme96->iobase + RME96_IO_GET_REC_POS)
  279. & RME96_RCR_AUDIO_ADDR_MASK) >> rme96->capture_frlog;
  280. }
  281. static int
  282. snd_rme96_playback_silence(struct snd_pcm_substream *substream,
  283. int channel, /* not used (interleaved data) */
  284. snd_pcm_uframes_t pos,
  285. snd_pcm_uframes_t count)
  286. {
  287. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  288. count <<= rme96->playback_frlog;
  289. pos <<= rme96->playback_frlog;
  290. memset_io(rme96->iobase + RME96_IO_PLAY_BUFFER + pos,
  291. 0, count);
  292. return 0;
  293. }
  294. static int
  295. snd_rme96_playback_copy(struct snd_pcm_substream *substream,
  296. int channel, /* not used (interleaved data) */
  297. snd_pcm_uframes_t pos,
  298. void __user *src,
  299. snd_pcm_uframes_t count)
  300. {
  301. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  302. count <<= rme96->playback_frlog;
  303. pos <<= rme96->playback_frlog;
  304. return copy_from_user_toio(rme96->iobase + RME96_IO_PLAY_BUFFER + pos, src,
  305. count);
  306. }
  307. static int
  308. snd_rme96_capture_copy(struct snd_pcm_substream *substream,
  309. int channel, /* not used (interleaved data) */
  310. snd_pcm_uframes_t pos,
  311. void __user *dst,
  312. snd_pcm_uframes_t count)
  313. {
  314. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  315. count <<= rme96->capture_frlog;
  316. pos <<= rme96->capture_frlog;
  317. return copy_to_user_fromio(dst, rme96->iobase + RME96_IO_REC_BUFFER + pos,
  318. count);
  319. }
  320. /*
  321. * Digital output capabilities (S/PDIF)
  322. */
  323. static struct snd_pcm_hardware snd_rme96_playback_spdif_info =
  324. {
  325. .info = (SNDRV_PCM_INFO_MMAP_IOMEM |
  326. SNDRV_PCM_INFO_MMAP_VALID |
  327. SNDRV_PCM_INFO_SYNC_START |
  328. SNDRV_PCM_INFO_RESUME |
  329. SNDRV_PCM_INFO_INTERLEAVED |
  330. SNDRV_PCM_INFO_PAUSE),
  331. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  332. SNDRV_PCM_FMTBIT_S32_LE),
  333. .rates = (SNDRV_PCM_RATE_32000 |
  334. SNDRV_PCM_RATE_44100 |
  335. SNDRV_PCM_RATE_48000 |
  336. SNDRV_PCM_RATE_64000 |
  337. SNDRV_PCM_RATE_88200 |
  338. SNDRV_PCM_RATE_96000),
  339. .rate_min = 32000,
  340. .rate_max = 96000,
  341. .channels_min = 2,
  342. .channels_max = 2,
  343. .buffer_bytes_max = RME96_BUFFER_SIZE,
  344. .period_bytes_min = RME96_SMALL_BLOCK_SIZE,
  345. .period_bytes_max = RME96_LARGE_BLOCK_SIZE,
  346. .periods_min = RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
  347. .periods_max = RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
  348. .fifo_size = 0,
  349. };
  350. /*
  351. * Digital input capabilities (S/PDIF)
  352. */
  353. static struct snd_pcm_hardware snd_rme96_capture_spdif_info =
  354. {
  355. .info = (SNDRV_PCM_INFO_MMAP_IOMEM |
  356. SNDRV_PCM_INFO_MMAP_VALID |
  357. SNDRV_PCM_INFO_SYNC_START |
  358. SNDRV_PCM_INFO_RESUME |
  359. SNDRV_PCM_INFO_INTERLEAVED |
  360. SNDRV_PCM_INFO_PAUSE),
  361. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  362. SNDRV_PCM_FMTBIT_S32_LE),
  363. .rates = (SNDRV_PCM_RATE_32000 |
  364. SNDRV_PCM_RATE_44100 |
  365. SNDRV_PCM_RATE_48000 |
  366. SNDRV_PCM_RATE_64000 |
  367. SNDRV_PCM_RATE_88200 |
  368. SNDRV_PCM_RATE_96000),
  369. .rate_min = 32000,
  370. .rate_max = 96000,
  371. .channels_min = 2,
  372. .channels_max = 2,
  373. .buffer_bytes_max = RME96_BUFFER_SIZE,
  374. .period_bytes_min = RME96_SMALL_BLOCK_SIZE,
  375. .period_bytes_max = RME96_LARGE_BLOCK_SIZE,
  376. .periods_min = RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
  377. .periods_max = RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
  378. .fifo_size = 0,
  379. };
  380. /*
  381. * Digital output capabilities (ADAT)
  382. */
  383. static struct snd_pcm_hardware snd_rme96_playback_adat_info =
  384. {
  385. .info = (SNDRV_PCM_INFO_MMAP_IOMEM |
  386. SNDRV_PCM_INFO_MMAP_VALID |
  387. SNDRV_PCM_INFO_SYNC_START |
  388. SNDRV_PCM_INFO_RESUME |
  389. SNDRV_PCM_INFO_INTERLEAVED |
  390. SNDRV_PCM_INFO_PAUSE),
  391. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  392. SNDRV_PCM_FMTBIT_S32_LE),
  393. .rates = (SNDRV_PCM_RATE_44100 |
  394. SNDRV_PCM_RATE_48000),
  395. .rate_min = 44100,
  396. .rate_max = 48000,
  397. .channels_min = 8,
  398. .channels_max = 8,
  399. .buffer_bytes_max = RME96_BUFFER_SIZE,
  400. .period_bytes_min = RME96_SMALL_BLOCK_SIZE,
  401. .period_bytes_max = RME96_LARGE_BLOCK_SIZE,
  402. .periods_min = RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
  403. .periods_max = RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
  404. .fifo_size = 0,
  405. };
  406. /*
  407. * Digital input capabilities (ADAT)
  408. */
  409. static struct snd_pcm_hardware snd_rme96_capture_adat_info =
  410. {
  411. .info = (SNDRV_PCM_INFO_MMAP_IOMEM |
  412. SNDRV_PCM_INFO_MMAP_VALID |
  413. SNDRV_PCM_INFO_SYNC_START |
  414. SNDRV_PCM_INFO_RESUME |
  415. SNDRV_PCM_INFO_INTERLEAVED |
  416. SNDRV_PCM_INFO_PAUSE),
  417. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  418. SNDRV_PCM_FMTBIT_S32_LE),
  419. .rates = (SNDRV_PCM_RATE_44100 |
  420. SNDRV_PCM_RATE_48000),
  421. .rate_min = 44100,
  422. .rate_max = 48000,
  423. .channels_min = 8,
  424. .channels_max = 8,
  425. .buffer_bytes_max = RME96_BUFFER_SIZE,
  426. .period_bytes_min = RME96_SMALL_BLOCK_SIZE,
  427. .period_bytes_max = RME96_LARGE_BLOCK_SIZE,
  428. .periods_min = RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
  429. .periods_max = RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
  430. .fifo_size = 0,
  431. };
  432. /*
  433. * The CDATA, CCLK and CLATCH bits can be used to write to the SPI interface
  434. * of the AD1852 or AD1852 D/A converter on the board. CDATA must be set up
  435. * on the falling edge of CCLK and be stable on the rising edge. The rising
  436. * edge of CLATCH after the last data bit clocks in the whole data word.
  437. * A fast processor could probably drive the SPI interface faster than the
  438. * DAC can handle (3MHz for the 1855, unknown for the 1852). The udelay(1)
  439. * limits the data rate to 500KHz and only causes a delay of 33 microsecs.
  440. *
  441. * NOTE: increased delay from 1 to 10, since there where problems setting
  442. * the volume.
  443. */
  444. static void
  445. snd_rme96_write_SPI(struct rme96 *rme96, u16 val)
  446. {
  447. int i;
  448. for (i = 0; i < 16; i++) {
  449. if (val & 0x8000) {
  450. rme96->areg |= RME96_AR_CDATA;
  451. } else {
  452. rme96->areg &= ~RME96_AR_CDATA;
  453. }
  454. rme96->areg &= ~(RME96_AR_CCLK | RME96_AR_CLATCH);
  455. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  456. udelay(10);
  457. rme96->areg |= RME96_AR_CCLK;
  458. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  459. udelay(10);
  460. val <<= 1;
  461. }
  462. rme96->areg &= ~(RME96_AR_CCLK | RME96_AR_CDATA);
  463. rme96->areg |= RME96_AR_CLATCH;
  464. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  465. udelay(10);
  466. rme96->areg &= ~RME96_AR_CLATCH;
  467. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  468. }
  469. static void
  470. snd_rme96_apply_dac_volume(struct rme96 *rme96)
  471. {
  472. if (RME96_DAC_IS_1852(rme96)) {
  473. snd_rme96_write_SPI(rme96, (rme96->vol[0] << 2) | 0x0);
  474. snd_rme96_write_SPI(rme96, (rme96->vol[1] << 2) | 0x2);
  475. } else if (RME96_DAC_IS_1855(rme96)) {
  476. snd_rme96_write_SPI(rme96, (rme96->vol[0] & 0x3FF) | 0x000);
  477. snd_rme96_write_SPI(rme96, (rme96->vol[1] & 0x3FF) | 0x400);
  478. }
  479. }
  480. static void
  481. snd_rme96_reset_dac(struct rme96 *rme96)
  482. {
  483. writel(rme96->wcreg | RME96_WCR_PD,
  484. rme96->iobase + RME96_IO_CONTROL_REGISTER);
  485. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  486. }
  487. static int
  488. snd_rme96_getmontracks(struct rme96 *rme96)
  489. {
  490. return ((rme96->wcreg >> RME96_WCR_BITPOS_MONITOR_0) & 1) +
  491. (((rme96->wcreg >> RME96_WCR_BITPOS_MONITOR_1) & 1) << 1);
  492. }
  493. static int
  494. snd_rme96_setmontracks(struct rme96 *rme96,
  495. int montracks)
  496. {
  497. if (montracks & 1) {
  498. rme96->wcreg |= RME96_WCR_MONITOR_0;
  499. } else {
  500. rme96->wcreg &= ~RME96_WCR_MONITOR_0;
  501. }
  502. if (montracks & 2) {
  503. rme96->wcreg |= RME96_WCR_MONITOR_1;
  504. } else {
  505. rme96->wcreg &= ~RME96_WCR_MONITOR_1;
  506. }
  507. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  508. return 0;
  509. }
  510. static int
  511. snd_rme96_getattenuation(struct rme96 *rme96)
  512. {
  513. return ((rme96->wcreg >> RME96_WCR_BITPOS_GAIN_0) & 1) +
  514. (((rme96->wcreg >> RME96_WCR_BITPOS_GAIN_1) & 1) << 1);
  515. }
  516. static int
  517. snd_rme96_setattenuation(struct rme96 *rme96,
  518. int attenuation)
  519. {
  520. switch (attenuation) {
  521. case 0:
  522. rme96->wcreg = (rme96->wcreg & ~RME96_WCR_GAIN_0) &
  523. ~RME96_WCR_GAIN_1;
  524. break;
  525. case 1:
  526. rme96->wcreg = (rme96->wcreg | RME96_WCR_GAIN_0) &
  527. ~RME96_WCR_GAIN_1;
  528. break;
  529. case 2:
  530. rme96->wcreg = (rme96->wcreg & ~RME96_WCR_GAIN_0) |
  531. RME96_WCR_GAIN_1;
  532. break;
  533. case 3:
  534. rme96->wcreg = (rme96->wcreg | RME96_WCR_GAIN_0) |
  535. RME96_WCR_GAIN_1;
  536. break;
  537. default:
  538. return -EINVAL;
  539. }
  540. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  541. return 0;
  542. }
  543. static int
  544. snd_rme96_capture_getrate(struct rme96 *rme96,
  545. int *is_adat)
  546. {
  547. int n, rate;
  548. *is_adat = 0;
  549. if (rme96->areg & RME96_AR_ANALOG) {
  550. /* Analog input, overrides S/PDIF setting */
  551. n = ((rme96->areg >> RME96_AR_BITPOS_F0) & 1) +
  552. (((rme96->areg >> RME96_AR_BITPOS_F1) & 1) << 1);
  553. switch (n) {
  554. case 1:
  555. rate = 32000;
  556. break;
  557. case 2:
  558. rate = 44100;
  559. break;
  560. case 3:
  561. rate = 48000;
  562. break;
  563. default:
  564. return -1;
  565. }
  566. return (rme96->areg & RME96_AR_BITPOS_F2) ? rate << 1 : rate;
  567. }
  568. rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
  569. if (rme96->rcreg & RME96_RCR_LOCK) {
  570. /* ADAT rate */
  571. *is_adat = 1;
  572. if (rme96->rcreg & RME96_RCR_T_OUT) {
  573. return 48000;
  574. }
  575. return 44100;
  576. }
  577. if (rme96->rcreg & RME96_RCR_VERF) {
  578. return -1;
  579. }
  580. /* S/PDIF rate */
  581. n = ((rme96->rcreg >> RME96_RCR_BITPOS_F0) & 1) +
  582. (((rme96->rcreg >> RME96_RCR_BITPOS_F1) & 1) << 1) +
  583. (((rme96->rcreg >> RME96_RCR_BITPOS_F2) & 1) << 2);
  584. switch (n) {
  585. case 0:
  586. if (rme96->rcreg & RME96_RCR_T_OUT) {
  587. return 64000;
  588. }
  589. return -1;
  590. case 3: return 96000;
  591. case 4: return 88200;
  592. case 5: return 48000;
  593. case 6: return 44100;
  594. case 7: return 32000;
  595. default:
  596. break;
  597. }
  598. return -1;
  599. }
  600. static int
  601. snd_rme96_playback_getrate(struct rme96 *rme96)
  602. {
  603. int rate, dummy;
  604. if (!(rme96->wcreg & RME96_WCR_MASTER) &&
  605. snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
  606. (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
  607. {
  608. /* slave clock */
  609. return rate;
  610. }
  611. rate = ((rme96->wcreg >> RME96_WCR_BITPOS_FREQ_0) & 1) +
  612. (((rme96->wcreg >> RME96_WCR_BITPOS_FREQ_1) & 1) << 1);
  613. switch (rate) {
  614. case 1:
  615. rate = 32000;
  616. break;
  617. case 2:
  618. rate = 44100;
  619. break;
  620. case 3:
  621. rate = 48000;
  622. break;
  623. default:
  624. return -1;
  625. }
  626. return (rme96->wcreg & RME96_WCR_DS) ? rate << 1 : rate;
  627. }
  628. static int
  629. snd_rme96_playback_setrate(struct rme96 *rme96,
  630. int rate)
  631. {
  632. int ds;
  633. ds = rme96->wcreg & RME96_WCR_DS;
  634. switch (rate) {
  635. case 32000:
  636. rme96->wcreg &= ~RME96_WCR_DS;
  637. rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) &
  638. ~RME96_WCR_FREQ_1;
  639. break;
  640. case 44100:
  641. rme96->wcreg &= ~RME96_WCR_DS;
  642. rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_1) &
  643. ~RME96_WCR_FREQ_0;
  644. break;
  645. case 48000:
  646. rme96->wcreg &= ~RME96_WCR_DS;
  647. rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) |
  648. RME96_WCR_FREQ_1;
  649. break;
  650. case 64000:
  651. rme96->wcreg |= RME96_WCR_DS;
  652. rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) &
  653. ~RME96_WCR_FREQ_1;
  654. break;
  655. case 88200:
  656. rme96->wcreg |= RME96_WCR_DS;
  657. rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_1) &
  658. ~RME96_WCR_FREQ_0;
  659. break;
  660. case 96000:
  661. rme96->wcreg |= RME96_WCR_DS;
  662. rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) |
  663. RME96_WCR_FREQ_1;
  664. break;
  665. default:
  666. return -EINVAL;
  667. }
  668. if ((!ds && rme96->wcreg & RME96_WCR_DS) ||
  669. (ds && !(rme96->wcreg & RME96_WCR_DS)))
  670. {
  671. /* change to/from double-speed: reset the DAC (if available) */
  672. snd_rme96_reset_dac(rme96);
  673. } else {
  674. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  675. }
  676. return 0;
  677. }
  678. static int
  679. snd_rme96_capture_analog_setrate(struct rme96 *rme96,
  680. int rate)
  681. {
  682. switch (rate) {
  683. case 32000:
  684. rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) &
  685. ~RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
  686. break;
  687. case 44100:
  688. rme96->areg = ((rme96->areg & ~RME96_AR_FREQPAD_0) |
  689. RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
  690. break;
  691. case 48000:
  692. rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) |
  693. RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
  694. break;
  695. case 64000:
  696. if (rme96->rev < 4) {
  697. return -EINVAL;
  698. }
  699. rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) &
  700. ~RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
  701. break;
  702. case 88200:
  703. if (rme96->rev < 4) {
  704. return -EINVAL;
  705. }
  706. rme96->areg = ((rme96->areg & ~RME96_AR_FREQPAD_0) |
  707. RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
  708. break;
  709. case 96000:
  710. rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) |
  711. RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
  712. break;
  713. default:
  714. return -EINVAL;
  715. }
  716. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  717. return 0;
  718. }
  719. static int
  720. snd_rme96_setclockmode(struct rme96 *rme96,
  721. int mode)
  722. {
  723. switch (mode) {
  724. case RME96_CLOCKMODE_SLAVE:
  725. /* AutoSync */
  726. rme96->wcreg &= ~RME96_WCR_MASTER;
  727. rme96->areg &= ~RME96_AR_WSEL;
  728. break;
  729. case RME96_CLOCKMODE_MASTER:
  730. /* Internal */
  731. rme96->wcreg |= RME96_WCR_MASTER;
  732. rme96->areg &= ~RME96_AR_WSEL;
  733. break;
  734. case RME96_CLOCKMODE_WORDCLOCK:
  735. /* Word clock is a master mode */
  736. rme96->wcreg |= RME96_WCR_MASTER;
  737. rme96->areg |= RME96_AR_WSEL;
  738. break;
  739. default:
  740. return -EINVAL;
  741. }
  742. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  743. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  744. return 0;
  745. }
  746. static int
  747. snd_rme96_getclockmode(struct rme96 *rme96)
  748. {
  749. if (rme96->areg & RME96_AR_WSEL) {
  750. return RME96_CLOCKMODE_WORDCLOCK;
  751. }
  752. return (rme96->wcreg & RME96_WCR_MASTER) ? RME96_CLOCKMODE_MASTER :
  753. RME96_CLOCKMODE_SLAVE;
  754. }
  755. static int
  756. snd_rme96_setinputtype(struct rme96 *rme96,
  757. int type)
  758. {
  759. int n;
  760. switch (type) {
  761. case RME96_INPUT_OPTICAL:
  762. rme96->wcreg = (rme96->wcreg & ~RME96_WCR_INP_0) &
  763. ~RME96_WCR_INP_1;
  764. break;
  765. case RME96_INPUT_COAXIAL:
  766. rme96->wcreg = (rme96->wcreg | RME96_WCR_INP_0) &
  767. ~RME96_WCR_INP_1;
  768. break;
  769. case RME96_INPUT_INTERNAL:
  770. rme96->wcreg = (rme96->wcreg & ~RME96_WCR_INP_0) |
  771. RME96_WCR_INP_1;
  772. break;
  773. case RME96_INPUT_XLR:
  774. if ((rme96->pci->device != PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST &&
  775. rme96->pci->device != PCI_DEVICE_ID_RME_DIGI96_8_PRO) ||
  776. (rme96->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST &&
  777. rme96->rev > 4))
  778. {
  779. /* Only Digi96/8 PRO and Digi96/8 PAD supports XLR */
  780. return -EINVAL;
  781. }
  782. rme96->wcreg = (rme96->wcreg | RME96_WCR_INP_0) |
  783. RME96_WCR_INP_1;
  784. break;
  785. case RME96_INPUT_ANALOG:
  786. if (!RME96_HAS_ANALOG_IN(rme96)) {
  787. return -EINVAL;
  788. }
  789. rme96->areg |= RME96_AR_ANALOG;
  790. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  791. if (rme96->rev < 4) {
  792. /*
  793. * Revision less than 004 does not support 64 and
  794. * 88.2 kHz
  795. */
  796. if (snd_rme96_capture_getrate(rme96, &n) == 88200) {
  797. snd_rme96_capture_analog_setrate(rme96, 44100);
  798. }
  799. if (snd_rme96_capture_getrate(rme96, &n) == 64000) {
  800. snd_rme96_capture_analog_setrate(rme96, 32000);
  801. }
  802. }
  803. return 0;
  804. default:
  805. return -EINVAL;
  806. }
  807. if (type != RME96_INPUT_ANALOG && RME96_HAS_ANALOG_IN(rme96)) {
  808. rme96->areg &= ~RME96_AR_ANALOG;
  809. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  810. }
  811. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  812. return 0;
  813. }
  814. static int
  815. snd_rme96_getinputtype(struct rme96 *rme96)
  816. {
  817. if (rme96->areg & RME96_AR_ANALOG) {
  818. return RME96_INPUT_ANALOG;
  819. }
  820. return ((rme96->wcreg >> RME96_WCR_BITPOS_INP_0) & 1) +
  821. (((rme96->wcreg >> RME96_WCR_BITPOS_INP_1) & 1) << 1);
  822. }
  823. static void
  824. snd_rme96_setframelog(struct rme96 *rme96,
  825. int n_channels,
  826. int is_playback)
  827. {
  828. int frlog;
  829. if (n_channels == 2) {
  830. frlog = 1;
  831. } else {
  832. /* assume 8 channels */
  833. frlog = 3;
  834. }
  835. if (is_playback) {
  836. frlog += (rme96->wcreg & RME96_WCR_MODE24) ? 2 : 1;
  837. rme96->playback_frlog = frlog;
  838. } else {
  839. frlog += (rme96->wcreg & RME96_WCR_MODE24_2) ? 2 : 1;
  840. rme96->capture_frlog = frlog;
  841. }
  842. }
  843. static int
  844. snd_rme96_playback_setformat(struct rme96 *rme96, snd_pcm_format_t format)
  845. {
  846. switch (format) {
  847. case SNDRV_PCM_FORMAT_S16_LE:
  848. rme96->wcreg &= ~RME96_WCR_MODE24;
  849. break;
  850. case SNDRV_PCM_FORMAT_S32_LE:
  851. rme96->wcreg |= RME96_WCR_MODE24;
  852. break;
  853. default:
  854. return -EINVAL;
  855. }
  856. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  857. return 0;
  858. }
  859. static int
  860. snd_rme96_capture_setformat(struct rme96 *rme96, snd_pcm_format_t format)
  861. {
  862. switch (format) {
  863. case SNDRV_PCM_FORMAT_S16_LE:
  864. rme96->wcreg &= ~RME96_WCR_MODE24_2;
  865. break;
  866. case SNDRV_PCM_FORMAT_S32_LE:
  867. rme96->wcreg |= RME96_WCR_MODE24_2;
  868. break;
  869. default:
  870. return -EINVAL;
  871. }
  872. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  873. return 0;
  874. }
  875. static void
  876. snd_rme96_set_period_properties(struct rme96 *rme96,
  877. size_t period_bytes)
  878. {
  879. switch (period_bytes) {
  880. case RME96_LARGE_BLOCK_SIZE:
  881. rme96->wcreg &= ~RME96_WCR_ISEL;
  882. break;
  883. case RME96_SMALL_BLOCK_SIZE:
  884. rme96->wcreg |= RME96_WCR_ISEL;
  885. break;
  886. default:
  887. snd_BUG();
  888. break;
  889. }
  890. rme96->wcreg &= ~RME96_WCR_IDIS;
  891. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  892. }
  893. static int
  894. snd_rme96_playback_hw_params(struct snd_pcm_substream *substream,
  895. struct snd_pcm_hw_params *params)
  896. {
  897. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  898. struct snd_pcm_runtime *runtime = substream->runtime;
  899. int err, rate, dummy;
  900. runtime->dma_area = (void __force *)(rme96->iobase +
  901. RME96_IO_PLAY_BUFFER);
  902. runtime->dma_addr = rme96->port + RME96_IO_PLAY_BUFFER;
  903. runtime->dma_bytes = RME96_BUFFER_SIZE;
  904. spin_lock_irq(&rme96->lock);
  905. if (!(rme96->wcreg & RME96_WCR_MASTER) &&
  906. snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
  907. (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
  908. {
  909. /* slave clock */
  910. if ((int)params_rate(params) != rate) {
  911. spin_unlock_irq(&rme96->lock);
  912. return -EIO;
  913. }
  914. } else if ((err = snd_rme96_playback_setrate(rme96, params_rate(params))) < 0) {
  915. spin_unlock_irq(&rme96->lock);
  916. return err;
  917. }
  918. if ((err = snd_rme96_playback_setformat(rme96, params_format(params))) < 0) {
  919. spin_unlock_irq(&rme96->lock);
  920. return err;
  921. }
  922. snd_rme96_setframelog(rme96, params_channels(params), 1);
  923. if (rme96->capture_periodsize != 0) {
  924. if (params_period_size(params) << rme96->playback_frlog !=
  925. rme96->capture_periodsize)
  926. {
  927. spin_unlock_irq(&rme96->lock);
  928. return -EBUSY;
  929. }
  930. }
  931. rme96->playback_periodsize =
  932. params_period_size(params) << rme96->playback_frlog;
  933. snd_rme96_set_period_properties(rme96, rme96->playback_periodsize);
  934. /* S/PDIF setup */
  935. if ((rme96->wcreg & RME96_WCR_ADAT) == 0) {
  936. rme96->wcreg &= ~(RME96_WCR_PRO | RME96_WCR_DOLBY | RME96_WCR_EMP);
  937. writel(rme96->wcreg |= rme96->wcreg_spdif_stream, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  938. }
  939. spin_unlock_irq(&rme96->lock);
  940. return 0;
  941. }
  942. static int
  943. snd_rme96_capture_hw_params(struct snd_pcm_substream *substream,
  944. struct snd_pcm_hw_params *params)
  945. {
  946. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  947. struct snd_pcm_runtime *runtime = substream->runtime;
  948. int err, isadat, rate;
  949. runtime->dma_area = (void __force *)(rme96->iobase +
  950. RME96_IO_REC_BUFFER);
  951. runtime->dma_addr = rme96->port + RME96_IO_REC_BUFFER;
  952. runtime->dma_bytes = RME96_BUFFER_SIZE;
  953. spin_lock_irq(&rme96->lock);
  954. if ((err = snd_rme96_capture_setformat(rme96, params_format(params))) < 0) {
  955. spin_unlock_irq(&rme96->lock);
  956. return err;
  957. }
  958. if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
  959. if ((err = snd_rme96_capture_analog_setrate(rme96,
  960. params_rate(params))) < 0)
  961. {
  962. spin_unlock_irq(&rme96->lock);
  963. return err;
  964. }
  965. } else if ((rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0) {
  966. if ((int)params_rate(params) != rate) {
  967. spin_unlock_irq(&rme96->lock);
  968. return -EIO;
  969. }
  970. if ((isadat && runtime->hw.channels_min == 2) ||
  971. (!isadat && runtime->hw.channels_min == 8))
  972. {
  973. spin_unlock_irq(&rme96->lock);
  974. return -EIO;
  975. }
  976. }
  977. snd_rme96_setframelog(rme96, params_channels(params), 0);
  978. if (rme96->playback_periodsize != 0) {
  979. if (params_period_size(params) << rme96->capture_frlog !=
  980. rme96->playback_periodsize)
  981. {
  982. spin_unlock_irq(&rme96->lock);
  983. return -EBUSY;
  984. }
  985. }
  986. rme96->capture_periodsize =
  987. params_period_size(params) << rme96->capture_frlog;
  988. snd_rme96_set_period_properties(rme96, rme96->capture_periodsize);
  989. spin_unlock_irq(&rme96->lock);
  990. return 0;
  991. }
  992. static void
  993. snd_rme96_trigger(struct rme96 *rme96,
  994. int op)
  995. {
  996. if (op & RME96_TB_RESET_PLAYPOS)
  997. writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
  998. if (op & RME96_TB_RESET_CAPTUREPOS)
  999. writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
  1000. if (op & RME96_TB_CLEAR_PLAYBACK_IRQ) {
  1001. rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1002. if (rme96->rcreg & RME96_RCR_IRQ)
  1003. writel(0, rme96->iobase + RME96_IO_CONFIRM_PLAY_IRQ);
  1004. }
  1005. if (op & RME96_TB_CLEAR_CAPTURE_IRQ) {
  1006. rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1007. if (rme96->rcreg & RME96_RCR_IRQ_2)
  1008. writel(0, rme96->iobase + RME96_IO_CONFIRM_REC_IRQ);
  1009. }
  1010. if (op & RME96_TB_START_PLAYBACK)
  1011. rme96->wcreg |= RME96_WCR_START;
  1012. if (op & RME96_TB_STOP_PLAYBACK)
  1013. rme96->wcreg &= ~RME96_WCR_START;
  1014. if (op & RME96_TB_START_CAPTURE)
  1015. rme96->wcreg |= RME96_WCR_START_2;
  1016. if (op & RME96_TB_STOP_CAPTURE)
  1017. rme96->wcreg &= ~RME96_WCR_START_2;
  1018. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1019. }
  1020. static irqreturn_t
  1021. snd_rme96_interrupt(int irq,
  1022. void *dev_id)
  1023. {
  1024. struct rme96 *rme96 = (struct rme96 *)dev_id;
  1025. rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1026. /* fastpath out, to ease interrupt sharing */
  1027. if (!((rme96->rcreg & RME96_RCR_IRQ) ||
  1028. (rme96->rcreg & RME96_RCR_IRQ_2)))
  1029. {
  1030. return IRQ_NONE;
  1031. }
  1032. if (rme96->rcreg & RME96_RCR_IRQ) {
  1033. /* playback */
  1034. snd_pcm_period_elapsed(rme96->playback_substream);
  1035. writel(0, rme96->iobase + RME96_IO_CONFIRM_PLAY_IRQ);
  1036. }
  1037. if (rme96->rcreg & RME96_RCR_IRQ_2) {
  1038. /* capture */
  1039. snd_pcm_period_elapsed(rme96->capture_substream);
  1040. writel(0, rme96->iobase + RME96_IO_CONFIRM_REC_IRQ);
  1041. }
  1042. return IRQ_HANDLED;
  1043. }
  1044. static unsigned int period_bytes[] = { RME96_SMALL_BLOCK_SIZE, RME96_LARGE_BLOCK_SIZE };
  1045. static struct snd_pcm_hw_constraint_list hw_constraints_period_bytes = {
  1046. .count = ARRAY_SIZE(period_bytes),
  1047. .list = period_bytes,
  1048. .mask = 0
  1049. };
  1050. static void
  1051. rme96_set_buffer_size_constraint(struct rme96 *rme96,
  1052. struct snd_pcm_runtime *runtime)
  1053. {
  1054. unsigned int size;
  1055. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1056. RME96_BUFFER_SIZE, RME96_BUFFER_SIZE);
  1057. if ((size = rme96->playback_periodsize) != 0 ||
  1058. (size = rme96->capture_periodsize) != 0)
  1059. snd_pcm_hw_constraint_minmax(runtime,
  1060. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1061. size, size);
  1062. else
  1063. snd_pcm_hw_constraint_list(runtime, 0,
  1064. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1065. &hw_constraints_period_bytes);
  1066. }
  1067. static int
  1068. snd_rme96_playback_spdif_open(struct snd_pcm_substream *substream)
  1069. {
  1070. int rate, dummy;
  1071. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1072. struct snd_pcm_runtime *runtime = substream->runtime;
  1073. snd_pcm_set_sync(substream);
  1074. spin_lock_irq(&rme96->lock);
  1075. if (rme96->playback_substream != NULL) {
  1076. spin_unlock_irq(&rme96->lock);
  1077. return -EBUSY;
  1078. }
  1079. rme96->wcreg &= ~RME96_WCR_ADAT;
  1080. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1081. rme96->playback_substream = substream;
  1082. spin_unlock_irq(&rme96->lock);
  1083. runtime->hw = snd_rme96_playback_spdif_info;
  1084. if (!(rme96->wcreg & RME96_WCR_MASTER) &&
  1085. snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
  1086. (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
  1087. {
  1088. /* slave clock */
  1089. runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
  1090. runtime->hw.rate_min = rate;
  1091. runtime->hw.rate_max = rate;
  1092. }
  1093. rme96_set_buffer_size_constraint(rme96, runtime);
  1094. rme96->wcreg_spdif_stream = rme96->wcreg_spdif;
  1095. rme96->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1096. snd_ctl_notify(rme96->card, SNDRV_CTL_EVENT_MASK_VALUE |
  1097. SNDRV_CTL_EVENT_MASK_INFO, &rme96->spdif_ctl->id);
  1098. return 0;
  1099. }
  1100. static int
  1101. snd_rme96_capture_spdif_open(struct snd_pcm_substream *substream)
  1102. {
  1103. int isadat, rate;
  1104. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1105. struct snd_pcm_runtime *runtime = substream->runtime;
  1106. snd_pcm_set_sync(substream);
  1107. runtime->hw = snd_rme96_capture_spdif_info;
  1108. if (snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
  1109. (rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0)
  1110. {
  1111. if (isadat) {
  1112. return -EIO;
  1113. }
  1114. runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
  1115. runtime->hw.rate_min = rate;
  1116. runtime->hw.rate_max = rate;
  1117. }
  1118. spin_lock_irq(&rme96->lock);
  1119. if (rme96->capture_substream != NULL) {
  1120. spin_unlock_irq(&rme96->lock);
  1121. return -EBUSY;
  1122. }
  1123. rme96->capture_substream = substream;
  1124. spin_unlock_irq(&rme96->lock);
  1125. rme96_set_buffer_size_constraint(rme96, runtime);
  1126. return 0;
  1127. }
  1128. static int
  1129. snd_rme96_playback_adat_open(struct snd_pcm_substream *substream)
  1130. {
  1131. int rate, dummy;
  1132. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1133. struct snd_pcm_runtime *runtime = substream->runtime;
  1134. snd_pcm_set_sync(substream);
  1135. spin_lock_irq(&rme96->lock);
  1136. if (rme96->playback_substream != NULL) {
  1137. spin_unlock_irq(&rme96->lock);
  1138. return -EBUSY;
  1139. }
  1140. rme96->wcreg |= RME96_WCR_ADAT;
  1141. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1142. rme96->playback_substream = substream;
  1143. spin_unlock_irq(&rme96->lock);
  1144. runtime->hw = snd_rme96_playback_adat_info;
  1145. if (!(rme96->wcreg & RME96_WCR_MASTER) &&
  1146. snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
  1147. (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
  1148. {
  1149. /* slave clock */
  1150. runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
  1151. runtime->hw.rate_min = rate;
  1152. runtime->hw.rate_max = rate;
  1153. }
  1154. rme96_set_buffer_size_constraint(rme96, runtime);
  1155. return 0;
  1156. }
  1157. static int
  1158. snd_rme96_capture_adat_open(struct snd_pcm_substream *substream)
  1159. {
  1160. int isadat, rate;
  1161. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1162. struct snd_pcm_runtime *runtime = substream->runtime;
  1163. snd_pcm_set_sync(substream);
  1164. runtime->hw = snd_rme96_capture_adat_info;
  1165. if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
  1166. /* makes no sense to use analog input. Note that analog
  1167. expension cards AEB4/8-I are RME96_INPUT_INTERNAL */
  1168. return -EIO;
  1169. }
  1170. if ((rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0) {
  1171. if (!isadat) {
  1172. return -EIO;
  1173. }
  1174. runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
  1175. runtime->hw.rate_min = rate;
  1176. runtime->hw.rate_max = rate;
  1177. }
  1178. spin_lock_irq(&rme96->lock);
  1179. if (rme96->capture_substream != NULL) {
  1180. spin_unlock_irq(&rme96->lock);
  1181. return -EBUSY;
  1182. }
  1183. rme96->capture_substream = substream;
  1184. spin_unlock_irq(&rme96->lock);
  1185. rme96_set_buffer_size_constraint(rme96, runtime);
  1186. return 0;
  1187. }
  1188. static int
  1189. snd_rme96_playback_close(struct snd_pcm_substream *substream)
  1190. {
  1191. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1192. int spdif = 0;
  1193. spin_lock_irq(&rme96->lock);
  1194. if (RME96_ISPLAYING(rme96)) {
  1195. snd_rme96_trigger(rme96, RME96_STOP_PLAYBACK);
  1196. }
  1197. rme96->playback_substream = NULL;
  1198. rme96->playback_periodsize = 0;
  1199. spdif = (rme96->wcreg & RME96_WCR_ADAT) == 0;
  1200. spin_unlock_irq(&rme96->lock);
  1201. if (spdif) {
  1202. rme96->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1203. snd_ctl_notify(rme96->card, SNDRV_CTL_EVENT_MASK_VALUE |
  1204. SNDRV_CTL_EVENT_MASK_INFO, &rme96->spdif_ctl->id);
  1205. }
  1206. return 0;
  1207. }
  1208. static int
  1209. snd_rme96_capture_close(struct snd_pcm_substream *substream)
  1210. {
  1211. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1212. spin_lock_irq(&rme96->lock);
  1213. if (RME96_ISRECORDING(rme96)) {
  1214. snd_rme96_trigger(rme96, RME96_STOP_CAPTURE);
  1215. }
  1216. rme96->capture_substream = NULL;
  1217. rme96->capture_periodsize = 0;
  1218. spin_unlock_irq(&rme96->lock);
  1219. return 0;
  1220. }
  1221. static int
  1222. snd_rme96_playback_prepare(struct snd_pcm_substream *substream)
  1223. {
  1224. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1225. spin_lock_irq(&rme96->lock);
  1226. if (RME96_ISPLAYING(rme96)) {
  1227. snd_rme96_trigger(rme96, RME96_STOP_PLAYBACK);
  1228. }
  1229. writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
  1230. spin_unlock_irq(&rme96->lock);
  1231. return 0;
  1232. }
  1233. static int
  1234. snd_rme96_capture_prepare(struct snd_pcm_substream *substream)
  1235. {
  1236. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1237. spin_lock_irq(&rme96->lock);
  1238. if (RME96_ISRECORDING(rme96)) {
  1239. snd_rme96_trigger(rme96, RME96_STOP_CAPTURE);
  1240. }
  1241. writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
  1242. spin_unlock_irq(&rme96->lock);
  1243. return 0;
  1244. }
  1245. static int
  1246. snd_rme96_playback_trigger(struct snd_pcm_substream *substream,
  1247. int cmd)
  1248. {
  1249. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1250. struct snd_pcm_substream *s;
  1251. bool sync;
  1252. snd_pcm_group_for_each_entry(s, substream) {
  1253. if (snd_pcm_substream_chip(s) == rme96)
  1254. snd_pcm_trigger_done(s, substream);
  1255. }
  1256. sync = (rme96->playback_substream && rme96->capture_substream) &&
  1257. (rme96->playback_substream->group ==
  1258. rme96->capture_substream->group);
  1259. switch (cmd) {
  1260. case SNDRV_PCM_TRIGGER_START:
  1261. if (!RME96_ISPLAYING(rme96)) {
  1262. if (substream != rme96->playback_substream)
  1263. return -EBUSY;
  1264. snd_rme96_trigger(rme96, sync ? RME96_START_BOTH
  1265. : RME96_START_PLAYBACK);
  1266. }
  1267. break;
  1268. case SNDRV_PCM_TRIGGER_SUSPEND:
  1269. case SNDRV_PCM_TRIGGER_STOP:
  1270. if (RME96_ISPLAYING(rme96)) {
  1271. if (substream != rme96->playback_substream)
  1272. return -EBUSY;
  1273. snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
  1274. : RME96_STOP_PLAYBACK);
  1275. }
  1276. break;
  1277. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1278. if (RME96_ISPLAYING(rme96))
  1279. snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
  1280. : RME96_STOP_PLAYBACK);
  1281. break;
  1282. case SNDRV_PCM_TRIGGER_RESUME:
  1283. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1284. if (!RME96_ISPLAYING(rme96))
  1285. snd_rme96_trigger(rme96, sync ? RME96_RESUME_BOTH
  1286. : RME96_RESUME_PLAYBACK);
  1287. break;
  1288. default:
  1289. return -EINVAL;
  1290. }
  1291. return 0;
  1292. }
  1293. static int
  1294. snd_rme96_capture_trigger(struct snd_pcm_substream *substream,
  1295. int cmd)
  1296. {
  1297. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1298. struct snd_pcm_substream *s;
  1299. bool sync;
  1300. snd_pcm_group_for_each_entry(s, substream) {
  1301. if (snd_pcm_substream_chip(s) == rme96)
  1302. snd_pcm_trigger_done(s, substream);
  1303. }
  1304. sync = (rme96->playback_substream && rme96->capture_substream) &&
  1305. (rme96->playback_substream->group ==
  1306. rme96->capture_substream->group);
  1307. switch (cmd) {
  1308. case SNDRV_PCM_TRIGGER_START:
  1309. if (!RME96_ISRECORDING(rme96)) {
  1310. if (substream != rme96->capture_substream)
  1311. return -EBUSY;
  1312. snd_rme96_trigger(rme96, sync ? RME96_START_BOTH
  1313. : RME96_START_CAPTURE);
  1314. }
  1315. break;
  1316. case SNDRV_PCM_TRIGGER_SUSPEND:
  1317. case SNDRV_PCM_TRIGGER_STOP:
  1318. if (RME96_ISRECORDING(rme96)) {
  1319. if (substream != rme96->capture_substream)
  1320. return -EBUSY;
  1321. snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
  1322. : RME96_STOP_CAPTURE);
  1323. }
  1324. break;
  1325. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1326. if (RME96_ISRECORDING(rme96))
  1327. snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
  1328. : RME96_STOP_CAPTURE);
  1329. break;
  1330. case SNDRV_PCM_TRIGGER_RESUME:
  1331. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1332. if (!RME96_ISRECORDING(rme96))
  1333. snd_rme96_trigger(rme96, sync ? RME96_RESUME_BOTH
  1334. : RME96_RESUME_CAPTURE);
  1335. break;
  1336. default:
  1337. return -EINVAL;
  1338. }
  1339. return 0;
  1340. }
  1341. static snd_pcm_uframes_t
  1342. snd_rme96_playback_pointer(struct snd_pcm_substream *substream)
  1343. {
  1344. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1345. return snd_rme96_playback_ptr(rme96);
  1346. }
  1347. static snd_pcm_uframes_t
  1348. snd_rme96_capture_pointer(struct snd_pcm_substream *substream)
  1349. {
  1350. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1351. return snd_rme96_capture_ptr(rme96);
  1352. }
  1353. static struct snd_pcm_ops snd_rme96_playback_spdif_ops = {
  1354. .open = snd_rme96_playback_spdif_open,
  1355. .close = snd_rme96_playback_close,
  1356. .ioctl = snd_pcm_lib_ioctl,
  1357. .hw_params = snd_rme96_playback_hw_params,
  1358. .prepare = snd_rme96_playback_prepare,
  1359. .trigger = snd_rme96_playback_trigger,
  1360. .pointer = snd_rme96_playback_pointer,
  1361. .copy = snd_rme96_playback_copy,
  1362. .silence = snd_rme96_playback_silence,
  1363. .mmap = snd_pcm_lib_mmap_iomem,
  1364. };
  1365. static struct snd_pcm_ops snd_rme96_capture_spdif_ops = {
  1366. .open = snd_rme96_capture_spdif_open,
  1367. .close = snd_rme96_capture_close,
  1368. .ioctl = snd_pcm_lib_ioctl,
  1369. .hw_params = snd_rme96_capture_hw_params,
  1370. .prepare = snd_rme96_capture_prepare,
  1371. .trigger = snd_rme96_capture_trigger,
  1372. .pointer = snd_rme96_capture_pointer,
  1373. .copy = snd_rme96_capture_copy,
  1374. .mmap = snd_pcm_lib_mmap_iomem,
  1375. };
  1376. static struct snd_pcm_ops snd_rme96_playback_adat_ops = {
  1377. .open = snd_rme96_playback_adat_open,
  1378. .close = snd_rme96_playback_close,
  1379. .ioctl = snd_pcm_lib_ioctl,
  1380. .hw_params = snd_rme96_playback_hw_params,
  1381. .prepare = snd_rme96_playback_prepare,
  1382. .trigger = snd_rme96_playback_trigger,
  1383. .pointer = snd_rme96_playback_pointer,
  1384. .copy = snd_rme96_playback_copy,
  1385. .silence = snd_rme96_playback_silence,
  1386. .mmap = snd_pcm_lib_mmap_iomem,
  1387. };
  1388. static struct snd_pcm_ops snd_rme96_capture_adat_ops = {
  1389. .open = snd_rme96_capture_adat_open,
  1390. .close = snd_rme96_capture_close,
  1391. .ioctl = snd_pcm_lib_ioctl,
  1392. .hw_params = snd_rme96_capture_hw_params,
  1393. .prepare = snd_rme96_capture_prepare,
  1394. .trigger = snd_rme96_capture_trigger,
  1395. .pointer = snd_rme96_capture_pointer,
  1396. .copy = snd_rme96_capture_copy,
  1397. .mmap = snd_pcm_lib_mmap_iomem,
  1398. };
  1399. static void
  1400. snd_rme96_free(void *private_data)
  1401. {
  1402. struct rme96 *rme96 = (struct rme96 *)private_data;
  1403. if (rme96 == NULL) {
  1404. return;
  1405. }
  1406. if (rme96->irq >= 0) {
  1407. snd_rme96_trigger(rme96, RME96_STOP_BOTH);
  1408. rme96->areg &= ~RME96_AR_DAC_EN;
  1409. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  1410. free_irq(rme96->irq, (void *)rme96);
  1411. rme96->irq = -1;
  1412. }
  1413. if (rme96->iobase) {
  1414. iounmap(rme96->iobase);
  1415. rme96->iobase = NULL;
  1416. }
  1417. if (rme96->port) {
  1418. pci_release_regions(rme96->pci);
  1419. rme96->port = 0;
  1420. }
  1421. #ifdef CONFIG_PM_SLEEP
  1422. vfree(rme96->playback_suspend_buffer);
  1423. vfree(rme96->capture_suspend_buffer);
  1424. #endif
  1425. pci_disable_device(rme96->pci);
  1426. }
  1427. static void
  1428. snd_rme96_free_spdif_pcm(struct snd_pcm *pcm)
  1429. {
  1430. struct rme96 *rme96 = pcm->private_data;
  1431. rme96->spdif_pcm = NULL;
  1432. }
  1433. static void
  1434. snd_rme96_free_adat_pcm(struct snd_pcm *pcm)
  1435. {
  1436. struct rme96 *rme96 = pcm->private_data;
  1437. rme96->adat_pcm = NULL;
  1438. }
  1439. static int
  1440. snd_rme96_create(struct rme96 *rme96)
  1441. {
  1442. struct pci_dev *pci = rme96->pci;
  1443. int err;
  1444. rme96->irq = -1;
  1445. spin_lock_init(&rme96->lock);
  1446. if ((err = pci_enable_device(pci)) < 0)
  1447. return err;
  1448. if ((err = pci_request_regions(pci, "RME96")) < 0)
  1449. return err;
  1450. rme96->port = pci_resource_start(rme96->pci, 0);
  1451. rme96->iobase = ioremap_nocache(rme96->port, RME96_IO_SIZE);
  1452. if (!rme96->iobase) {
  1453. dev_err(rme96->card->dev,
  1454. "unable to remap memory region 0x%lx-0x%lx\n",
  1455. rme96->port, rme96->port + RME96_IO_SIZE - 1);
  1456. return -ENOMEM;
  1457. }
  1458. if (request_irq(pci->irq, snd_rme96_interrupt, IRQF_SHARED,
  1459. KBUILD_MODNAME, rme96)) {
  1460. dev_err(rme96->card->dev, "unable to grab IRQ %d\n", pci->irq);
  1461. return -EBUSY;
  1462. }
  1463. rme96->irq = pci->irq;
  1464. /* read the card's revision number */
  1465. pci_read_config_byte(pci, 8, &rme96->rev);
  1466. /* set up ALSA pcm device for S/PDIF */
  1467. if ((err = snd_pcm_new(rme96->card, "Digi96 IEC958", 0,
  1468. 1, 1, &rme96->spdif_pcm)) < 0)
  1469. {
  1470. return err;
  1471. }
  1472. rme96->spdif_pcm->private_data = rme96;
  1473. rme96->spdif_pcm->private_free = snd_rme96_free_spdif_pcm;
  1474. strcpy(rme96->spdif_pcm->name, "Digi96 IEC958");
  1475. snd_pcm_set_ops(rme96->spdif_pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme96_playback_spdif_ops);
  1476. snd_pcm_set_ops(rme96->spdif_pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme96_capture_spdif_ops);
  1477. rme96->spdif_pcm->info_flags = 0;
  1478. /* set up ALSA pcm device for ADAT */
  1479. if (pci->device == PCI_DEVICE_ID_RME_DIGI96) {
  1480. /* ADAT is not available on the base model */
  1481. rme96->adat_pcm = NULL;
  1482. } else {
  1483. if ((err = snd_pcm_new(rme96->card, "Digi96 ADAT", 1,
  1484. 1, 1, &rme96->adat_pcm)) < 0)
  1485. {
  1486. return err;
  1487. }
  1488. rme96->adat_pcm->private_data = rme96;
  1489. rme96->adat_pcm->private_free = snd_rme96_free_adat_pcm;
  1490. strcpy(rme96->adat_pcm->name, "Digi96 ADAT");
  1491. snd_pcm_set_ops(rme96->adat_pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme96_playback_adat_ops);
  1492. snd_pcm_set_ops(rme96->adat_pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme96_capture_adat_ops);
  1493. rme96->adat_pcm->info_flags = 0;
  1494. }
  1495. rme96->playback_periodsize = 0;
  1496. rme96->capture_periodsize = 0;
  1497. /* make sure playback/capture is stopped, if by some reason active */
  1498. snd_rme96_trigger(rme96, RME96_STOP_BOTH);
  1499. /* set default values in registers */
  1500. rme96->wcreg =
  1501. RME96_WCR_FREQ_1 | /* set 44.1 kHz playback */
  1502. RME96_WCR_SEL | /* normal playback */
  1503. RME96_WCR_MASTER | /* set to master clock mode */
  1504. RME96_WCR_INP_0; /* set coaxial input */
  1505. rme96->areg = RME96_AR_FREQPAD_1; /* set 44.1 kHz analog capture */
  1506. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1507. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  1508. /* reset the ADC */
  1509. writel(rme96->areg | RME96_AR_PD2,
  1510. rme96->iobase + RME96_IO_ADDITIONAL_REG);
  1511. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  1512. /* reset and enable the DAC (order is important). */
  1513. snd_rme96_reset_dac(rme96);
  1514. rme96->areg |= RME96_AR_DAC_EN;
  1515. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  1516. /* reset playback and record buffer pointers */
  1517. writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
  1518. writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
  1519. /* reset volume */
  1520. rme96->vol[0] = rme96->vol[1] = 0;
  1521. if (RME96_HAS_ANALOG_OUT(rme96)) {
  1522. snd_rme96_apply_dac_volume(rme96);
  1523. }
  1524. /* init switch interface */
  1525. if ((err = snd_rme96_create_switches(rme96->card, rme96)) < 0) {
  1526. return err;
  1527. }
  1528. /* init proc interface */
  1529. snd_rme96_proc_init(rme96);
  1530. return 0;
  1531. }
  1532. /*
  1533. * proc interface
  1534. */
  1535. static void
  1536. snd_rme96_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  1537. {
  1538. int n;
  1539. struct rme96 *rme96 = entry->private_data;
  1540. rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1541. snd_iprintf(buffer, rme96->card->longname);
  1542. snd_iprintf(buffer, " (index #%d)\n", rme96->card->number + 1);
  1543. snd_iprintf(buffer, "\nGeneral settings\n");
  1544. if (rme96->wcreg & RME96_WCR_IDIS) {
  1545. snd_iprintf(buffer, " period size: N/A (interrupts "
  1546. "disabled)\n");
  1547. } else if (rme96->wcreg & RME96_WCR_ISEL) {
  1548. snd_iprintf(buffer, " period size: 2048 bytes\n");
  1549. } else {
  1550. snd_iprintf(buffer, " period size: 8192 bytes\n");
  1551. }
  1552. snd_iprintf(buffer, "\nInput settings\n");
  1553. switch (snd_rme96_getinputtype(rme96)) {
  1554. case RME96_INPUT_OPTICAL:
  1555. snd_iprintf(buffer, " input: optical");
  1556. break;
  1557. case RME96_INPUT_COAXIAL:
  1558. snd_iprintf(buffer, " input: coaxial");
  1559. break;
  1560. case RME96_INPUT_INTERNAL:
  1561. snd_iprintf(buffer, " input: internal");
  1562. break;
  1563. case RME96_INPUT_XLR:
  1564. snd_iprintf(buffer, " input: XLR");
  1565. break;
  1566. case RME96_INPUT_ANALOG:
  1567. snd_iprintf(buffer, " input: analog");
  1568. break;
  1569. }
  1570. if (snd_rme96_capture_getrate(rme96, &n) < 0) {
  1571. snd_iprintf(buffer, "\n sample rate: no valid signal\n");
  1572. } else {
  1573. if (n) {
  1574. snd_iprintf(buffer, " (8 channels)\n");
  1575. } else {
  1576. snd_iprintf(buffer, " (2 channels)\n");
  1577. }
  1578. snd_iprintf(buffer, " sample rate: %d Hz\n",
  1579. snd_rme96_capture_getrate(rme96, &n));
  1580. }
  1581. if (rme96->wcreg & RME96_WCR_MODE24_2) {
  1582. snd_iprintf(buffer, " sample format: 24 bit\n");
  1583. } else {
  1584. snd_iprintf(buffer, " sample format: 16 bit\n");
  1585. }
  1586. snd_iprintf(buffer, "\nOutput settings\n");
  1587. if (rme96->wcreg & RME96_WCR_SEL) {
  1588. snd_iprintf(buffer, " output signal: normal playback\n");
  1589. } else {
  1590. snd_iprintf(buffer, " output signal: same as input\n");
  1591. }
  1592. snd_iprintf(buffer, " sample rate: %d Hz\n",
  1593. snd_rme96_playback_getrate(rme96));
  1594. if (rme96->wcreg & RME96_WCR_MODE24) {
  1595. snd_iprintf(buffer, " sample format: 24 bit\n");
  1596. } else {
  1597. snd_iprintf(buffer, " sample format: 16 bit\n");
  1598. }
  1599. if (rme96->areg & RME96_AR_WSEL) {
  1600. snd_iprintf(buffer, " sample clock source: word clock\n");
  1601. } else if (rme96->wcreg & RME96_WCR_MASTER) {
  1602. snd_iprintf(buffer, " sample clock source: internal\n");
  1603. } else if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
  1604. snd_iprintf(buffer, " sample clock source: autosync (internal anyway due to analog input setting)\n");
  1605. } else if (snd_rme96_capture_getrate(rme96, &n) < 0) {
  1606. snd_iprintf(buffer, " sample clock source: autosync (internal anyway due to no valid signal)\n");
  1607. } else {
  1608. snd_iprintf(buffer, " sample clock source: autosync\n");
  1609. }
  1610. if (rme96->wcreg & RME96_WCR_PRO) {
  1611. snd_iprintf(buffer, " format: AES/EBU (professional)\n");
  1612. } else {
  1613. snd_iprintf(buffer, " format: IEC958 (consumer)\n");
  1614. }
  1615. if (rme96->wcreg & RME96_WCR_EMP) {
  1616. snd_iprintf(buffer, " emphasis: on\n");
  1617. } else {
  1618. snd_iprintf(buffer, " emphasis: off\n");
  1619. }
  1620. if (rme96->wcreg & RME96_WCR_DOLBY) {
  1621. snd_iprintf(buffer, " non-audio (dolby): on\n");
  1622. } else {
  1623. snd_iprintf(buffer, " non-audio (dolby): off\n");
  1624. }
  1625. if (RME96_HAS_ANALOG_IN(rme96)) {
  1626. snd_iprintf(buffer, "\nAnalog output settings\n");
  1627. switch (snd_rme96_getmontracks(rme96)) {
  1628. case RME96_MONITOR_TRACKS_1_2:
  1629. snd_iprintf(buffer, " monitored ADAT tracks: 1+2\n");
  1630. break;
  1631. case RME96_MONITOR_TRACKS_3_4:
  1632. snd_iprintf(buffer, " monitored ADAT tracks: 3+4\n");
  1633. break;
  1634. case RME96_MONITOR_TRACKS_5_6:
  1635. snd_iprintf(buffer, " monitored ADAT tracks: 5+6\n");
  1636. break;
  1637. case RME96_MONITOR_TRACKS_7_8:
  1638. snd_iprintf(buffer, " monitored ADAT tracks: 7+8\n");
  1639. break;
  1640. }
  1641. switch (snd_rme96_getattenuation(rme96)) {
  1642. case RME96_ATTENUATION_0:
  1643. snd_iprintf(buffer, " attenuation: 0 dB\n");
  1644. break;
  1645. case RME96_ATTENUATION_6:
  1646. snd_iprintf(buffer, " attenuation: -6 dB\n");
  1647. break;
  1648. case RME96_ATTENUATION_12:
  1649. snd_iprintf(buffer, " attenuation: -12 dB\n");
  1650. break;
  1651. case RME96_ATTENUATION_18:
  1652. snd_iprintf(buffer, " attenuation: -18 dB\n");
  1653. break;
  1654. }
  1655. snd_iprintf(buffer, " volume left: %u\n", rme96->vol[0]);
  1656. snd_iprintf(buffer, " volume right: %u\n", rme96->vol[1]);
  1657. }
  1658. }
  1659. static void snd_rme96_proc_init(struct rme96 *rme96)
  1660. {
  1661. struct snd_info_entry *entry;
  1662. if (! snd_card_proc_new(rme96->card, "rme96", &entry))
  1663. snd_info_set_text_ops(entry, rme96, snd_rme96_proc_read);
  1664. }
  1665. /*
  1666. * control interface
  1667. */
  1668. #define snd_rme96_info_loopback_control snd_ctl_boolean_mono_info
  1669. static int
  1670. snd_rme96_get_loopback_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1671. {
  1672. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1673. spin_lock_irq(&rme96->lock);
  1674. ucontrol->value.integer.value[0] = rme96->wcreg & RME96_WCR_SEL ? 0 : 1;
  1675. spin_unlock_irq(&rme96->lock);
  1676. return 0;
  1677. }
  1678. static int
  1679. snd_rme96_put_loopback_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1680. {
  1681. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1682. unsigned int val;
  1683. int change;
  1684. val = ucontrol->value.integer.value[0] ? 0 : RME96_WCR_SEL;
  1685. spin_lock_irq(&rme96->lock);
  1686. val = (rme96->wcreg & ~RME96_WCR_SEL) | val;
  1687. change = val != rme96->wcreg;
  1688. rme96->wcreg = val;
  1689. writel(val, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1690. spin_unlock_irq(&rme96->lock);
  1691. return change;
  1692. }
  1693. static int
  1694. snd_rme96_info_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1695. {
  1696. static const char * const _texts[5] = {
  1697. "Optical", "Coaxial", "Internal", "XLR", "Analog"
  1698. };
  1699. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1700. const char *texts[5] = {
  1701. _texts[0], _texts[1], _texts[2], _texts[3], _texts[4]
  1702. };
  1703. int num_items;
  1704. switch (rme96->pci->device) {
  1705. case PCI_DEVICE_ID_RME_DIGI96:
  1706. case PCI_DEVICE_ID_RME_DIGI96_8:
  1707. num_items = 3;
  1708. break;
  1709. case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
  1710. num_items = 4;
  1711. break;
  1712. case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
  1713. if (rme96->rev > 4) {
  1714. /* PST */
  1715. num_items = 4;
  1716. texts[3] = _texts[4]; /* Analog instead of XLR */
  1717. } else {
  1718. /* PAD */
  1719. num_items = 5;
  1720. }
  1721. break;
  1722. default:
  1723. snd_BUG();
  1724. return -EINVAL;
  1725. }
  1726. return snd_ctl_enum_info(uinfo, 1, num_items, texts);
  1727. }
  1728. static int
  1729. snd_rme96_get_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1730. {
  1731. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1732. unsigned int items = 3;
  1733. spin_lock_irq(&rme96->lock);
  1734. ucontrol->value.enumerated.item[0] = snd_rme96_getinputtype(rme96);
  1735. switch (rme96->pci->device) {
  1736. case PCI_DEVICE_ID_RME_DIGI96:
  1737. case PCI_DEVICE_ID_RME_DIGI96_8:
  1738. items = 3;
  1739. break;
  1740. case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
  1741. items = 4;
  1742. break;
  1743. case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
  1744. if (rme96->rev > 4) {
  1745. /* for handling PST case, (INPUT_ANALOG is moved to INPUT_XLR */
  1746. if (ucontrol->value.enumerated.item[0] == RME96_INPUT_ANALOG) {
  1747. ucontrol->value.enumerated.item[0] = RME96_INPUT_XLR;
  1748. }
  1749. items = 4;
  1750. } else {
  1751. items = 5;
  1752. }
  1753. break;
  1754. default:
  1755. snd_BUG();
  1756. break;
  1757. }
  1758. if (ucontrol->value.enumerated.item[0] >= items) {
  1759. ucontrol->value.enumerated.item[0] = items - 1;
  1760. }
  1761. spin_unlock_irq(&rme96->lock);
  1762. return 0;
  1763. }
  1764. static int
  1765. snd_rme96_put_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1766. {
  1767. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1768. unsigned int val;
  1769. int change, items = 3;
  1770. switch (rme96->pci->device) {
  1771. case PCI_DEVICE_ID_RME_DIGI96:
  1772. case PCI_DEVICE_ID_RME_DIGI96_8:
  1773. items = 3;
  1774. break;
  1775. case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
  1776. items = 4;
  1777. break;
  1778. case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
  1779. if (rme96->rev > 4) {
  1780. items = 4;
  1781. } else {
  1782. items = 5;
  1783. }
  1784. break;
  1785. default:
  1786. snd_BUG();
  1787. break;
  1788. }
  1789. val = ucontrol->value.enumerated.item[0] % items;
  1790. /* special case for PST */
  1791. if (rme96->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST && rme96->rev > 4) {
  1792. if (val == RME96_INPUT_XLR) {
  1793. val = RME96_INPUT_ANALOG;
  1794. }
  1795. }
  1796. spin_lock_irq(&rme96->lock);
  1797. change = (int)val != snd_rme96_getinputtype(rme96);
  1798. snd_rme96_setinputtype(rme96, val);
  1799. spin_unlock_irq(&rme96->lock);
  1800. return change;
  1801. }
  1802. static int
  1803. snd_rme96_info_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1804. {
  1805. static const char * const texts[3] = { "AutoSync", "Internal", "Word" };
  1806. return snd_ctl_enum_info(uinfo, 1, 3, texts);
  1807. }
  1808. static int
  1809. snd_rme96_get_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1810. {
  1811. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1812. spin_lock_irq(&rme96->lock);
  1813. ucontrol->value.enumerated.item[0] = snd_rme96_getclockmode(rme96);
  1814. spin_unlock_irq(&rme96->lock);
  1815. return 0;
  1816. }
  1817. static int
  1818. snd_rme96_put_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1819. {
  1820. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1821. unsigned int val;
  1822. int change;
  1823. val = ucontrol->value.enumerated.item[0] % 3;
  1824. spin_lock_irq(&rme96->lock);
  1825. change = (int)val != snd_rme96_getclockmode(rme96);
  1826. snd_rme96_setclockmode(rme96, val);
  1827. spin_unlock_irq(&rme96->lock);
  1828. return change;
  1829. }
  1830. static int
  1831. snd_rme96_info_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1832. {
  1833. static const char * const texts[4] = {
  1834. "0 dB", "-6 dB", "-12 dB", "-18 dB"
  1835. };
  1836. return snd_ctl_enum_info(uinfo, 1, 4, texts);
  1837. }
  1838. static int
  1839. snd_rme96_get_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1840. {
  1841. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1842. spin_lock_irq(&rme96->lock);
  1843. ucontrol->value.enumerated.item[0] = snd_rme96_getattenuation(rme96);
  1844. spin_unlock_irq(&rme96->lock);
  1845. return 0;
  1846. }
  1847. static int
  1848. snd_rme96_put_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1849. {
  1850. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1851. unsigned int val;
  1852. int change;
  1853. val = ucontrol->value.enumerated.item[0] % 4;
  1854. spin_lock_irq(&rme96->lock);
  1855. change = (int)val != snd_rme96_getattenuation(rme96);
  1856. snd_rme96_setattenuation(rme96, val);
  1857. spin_unlock_irq(&rme96->lock);
  1858. return change;
  1859. }
  1860. static int
  1861. snd_rme96_info_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1862. {
  1863. static const char * const texts[4] = { "1+2", "3+4", "5+6", "7+8" };
  1864. return snd_ctl_enum_info(uinfo, 1, 4, texts);
  1865. }
  1866. static int
  1867. snd_rme96_get_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1868. {
  1869. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1870. spin_lock_irq(&rme96->lock);
  1871. ucontrol->value.enumerated.item[0] = snd_rme96_getmontracks(rme96);
  1872. spin_unlock_irq(&rme96->lock);
  1873. return 0;
  1874. }
  1875. static int
  1876. snd_rme96_put_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1877. {
  1878. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1879. unsigned int val;
  1880. int change;
  1881. val = ucontrol->value.enumerated.item[0] % 4;
  1882. spin_lock_irq(&rme96->lock);
  1883. change = (int)val != snd_rme96_getmontracks(rme96);
  1884. snd_rme96_setmontracks(rme96, val);
  1885. spin_unlock_irq(&rme96->lock);
  1886. return change;
  1887. }
  1888. static u32 snd_rme96_convert_from_aes(struct snd_aes_iec958 *aes)
  1889. {
  1890. u32 val = 0;
  1891. val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? RME96_WCR_PRO : 0;
  1892. val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? RME96_WCR_DOLBY : 0;
  1893. if (val & RME96_WCR_PRO)
  1894. val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? RME96_WCR_EMP : 0;
  1895. else
  1896. val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? RME96_WCR_EMP : 0;
  1897. return val;
  1898. }
  1899. static void snd_rme96_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
  1900. {
  1901. aes->status[0] = ((val & RME96_WCR_PRO) ? IEC958_AES0_PROFESSIONAL : 0) |
  1902. ((val & RME96_WCR_DOLBY) ? IEC958_AES0_NONAUDIO : 0);
  1903. if (val & RME96_WCR_PRO)
  1904. aes->status[0] |= (val & RME96_WCR_EMP) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
  1905. else
  1906. aes->status[0] |= (val & RME96_WCR_EMP) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
  1907. }
  1908. static int snd_rme96_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1909. {
  1910. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1911. uinfo->count = 1;
  1912. return 0;
  1913. }
  1914. static int snd_rme96_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1915. {
  1916. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1917. snd_rme96_convert_to_aes(&ucontrol->value.iec958, rme96->wcreg_spdif);
  1918. return 0;
  1919. }
  1920. static int snd_rme96_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1921. {
  1922. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1923. int change;
  1924. u32 val;
  1925. val = snd_rme96_convert_from_aes(&ucontrol->value.iec958);
  1926. spin_lock_irq(&rme96->lock);
  1927. change = val != rme96->wcreg_spdif;
  1928. rme96->wcreg_spdif = val;
  1929. spin_unlock_irq(&rme96->lock);
  1930. return change;
  1931. }
  1932. static int snd_rme96_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1933. {
  1934. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1935. uinfo->count = 1;
  1936. return 0;
  1937. }
  1938. static int snd_rme96_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1939. {
  1940. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1941. snd_rme96_convert_to_aes(&ucontrol->value.iec958, rme96->wcreg_spdif_stream);
  1942. return 0;
  1943. }
  1944. static int snd_rme96_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1945. {
  1946. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1947. int change;
  1948. u32 val;
  1949. val = snd_rme96_convert_from_aes(&ucontrol->value.iec958);
  1950. spin_lock_irq(&rme96->lock);
  1951. change = val != rme96->wcreg_spdif_stream;
  1952. rme96->wcreg_spdif_stream = val;
  1953. rme96->wcreg &= ~(RME96_WCR_PRO | RME96_WCR_DOLBY | RME96_WCR_EMP);
  1954. rme96->wcreg |= val;
  1955. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1956. spin_unlock_irq(&rme96->lock);
  1957. return change;
  1958. }
  1959. static int snd_rme96_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1960. {
  1961. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1962. uinfo->count = 1;
  1963. return 0;
  1964. }
  1965. static int snd_rme96_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1966. {
  1967. ucontrol->value.iec958.status[0] = kcontrol->private_value;
  1968. return 0;
  1969. }
  1970. static int
  1971. snd_rme96_dac_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1972. {
  1973. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1974. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1975. uinfo->count = 2;
  1976. uinfo->value.integer.min = 0;
  1977. uinfo->value.integer.max = RME96_185X_MAX_OUT(rme96);
  1978. return 0;
  1979. }
  1980. static int
  1981. snd_rme96_dac_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *u)
  1982. {
  1983. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1984. spin_lock_irq(&rme96->lock);
  1985. u->value.integer.value[0] = rme96->vol[0];
  1986. u->value.integer.value[1] = rme96->vol[1];
  1987. spin_unlock_irq(&rme96->lock);
  1988. return 0;
  1989. }
  1990. static int
  1991. snd_rme96_dac_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *u)
  1992. {
  1993. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1994. int change = 0;
  1995. unsigned int vol, maxvol;
  1996. if (!RME96_HAS_ANALOG_OUT(rme96))
  1997. return -EINVAL;
  1998. maxvol = RME96_185X_MAX_OUT(rme96);
  1999. spin_lock_irq(&rme96->lock);
  2000. vol = u->value.integer.value[0];
  2001. if (vol != rme96->vol[0] && vol <= maxvol) {
  2002. rme96->vol[0] = vol;
  2003. change = 1;
  2004. }
  2005. vol = u->value.integer.value[1];
  2006. if (vol != rme96->vol[1] && vol <= maxvol) {
  2007. rme96->vol[1] = vol;
  2008. change = 1;
  2009. }
  2010. if (change)
  2011. snd_rme96_apply_dac_volume(rme96);
  2012. spin_unlock_irq(&rme96->lock);
  2013. return change;
  2014. }
  2015. static struct snd_kcontrol_new snd_rme96_controls[] = {
  2016. {
  2017. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2018. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  2019. .info = snd_rme96_control_spdif_info,
  2020. .get = snd_rme96_control_spdif_get,
  2021. .put = snd_rme96_control_spdif_put
  2022. },
  2023. {
  2024. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2025. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2026. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  2027. .info = snd_rme96_control_spdif_stream_info,
  2028. .get = snd_rme96_control_spdif_stream_get,
  2029. .put = snd_rme96_control_spdif_stream_put
  2030. },
  2031. {
  2032. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2033. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2034. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  2035. .info = snd_rme96_control_spdif_mask_info,
  2036. .get = snd_rme96_control_spdif_mask_get,
  2037. .private_value = IEC958_AES0_NONAUDIO |
  2038. IEC958_AES0_PROFESSIONAL |
  2039. IEC958_AES0_CON_EMPHASIS
  2040. },
  2041. {
  2042. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2043. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2044. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  2045. .info = snd_rme96_control_spdif_mask_info,
  2046. .get = snd_rme96_control_spdif_mask_get,
  2047. .private_value = IEC958_AES0_NONAUDIO |
  2048. IEC958_AES0_PROFESSIONAL |
  2049. IEC958_AES0_PRO_EMPHASIS
  2050. },
  2051. {
  2052. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2053. .name = "Input Connector",
  2054. .info = snd_rme96_info_inputtype_control,
  2055. .get = snd_rme96_get_inputtype_control,
  2056. .put = snd_rme96_put_inputtype_control
  2057. },
  2058. {
  2059. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2060. .name = "Loopback Input",
  2061. .info = snd_rme96_info_loopback_control,
  2062. .get = snd_rme96_get_loopback_control,
  2063. .put = snd_rme96_put_loopback_control
  2064. },
  2065. {
  2066. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2067. .name = "Sample Clock Source",
  2068. .info = snd_rme96_info_clockmode_control,
  2069. .get = snd_rme96_get_clockmode_control,
  2070. .put = snd_rme96_put_clockmode_control
  2071. },
  2072. {
  2073. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2074. .name = "Monitor Tracks",
  2075. .info = snd_rme96_info_montracks_control,
  2076. .get = snd_rme96_get_montracks_control,
  2077. .put = snd_rme96_put_montracks_control
  2078. },
  2079. {
  2080. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2081. .name = "Attenuation",
  2082. .info = snd_rme96_info_attenuation_control,
  2083. .get = snd_rme96_get_attenuation_control,
  2084. .put = snd_rme96_put_attenuation_control
  2085. },
  2086. {
  2087. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2088. .name = "DAC Playback Volume",
  2089. .info = snd_rme96_dac_volume_info,
  2090. .get = snd_rme96_dac_volume_get,
  2091. .put = snd_rme96_dac_volume_put
  2092. }
  2093. };
  2094. static int
  2095. snd_rme96_create_switches(struct snd_card *card,
  2096. struct rme96 *rme96)
  2097. {
  2098. int idx, err;
  2099. struct snd_kcontrol *kctl;
  2100. for (idx = 0; idx < 7; idx++) {
  2101. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme96_controls[idx], rme96))) < 0)
  2102. return err;
  2103. if (idx == 1) /* IEC958 (S/PDIF) Stream */
  2104. rme96->spdif_ctl = kctl;
  2105. }
  2106. if (RME96_HAS_ANALOG_OUT(rme96)) {
  2107. for (idx = 7; idx < 10; idx++)
  2108. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_rme96_controls[idx], rme96))) < 0)
  2109. return err;
  2110. }
  2111. return 0;
  2112. }
  2113. /*
  2114. * Card initialisation
  2115. */
  2116. #ifdef CONFIG_PM_SLEEP
  2117. static int rme96_suspend(struct device *dev)
  2118. {
  2119. struct snd_card *card = dev_get_drvdata(dev);
  2120. struct rme96 *rme96 = card->private_data;
  2121. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  2122. snd_pcm_suspend(rme96->playback_substream);
  2123. snd_pcm_suspend(rme96->capture_substream);
  2124. /* save capture & playback pointers */
  2125. rme96->playback_pointer = readl(rme96->iobase + RME96_IO_GET_PLAY_POS)
  2126. & RME96_RCR_AUDIO_ADDR_MASK;
  2127. rme96->capture_pointer = readl(rme96->iobase + RME96_IO_GET_REC_POS)
  2128. & RME96_RCR_AUDIO_ADDR_MASK;
  2129. /* save playback and capture buffers */
  2130. memcpy_fromio(rme96->playback_suspend_buffer,
  2131. rme96->iobase + RME96_IO_PLAY_BUFFER, RME96_BUFFER_SIZE);
  2132. memcpy_fromio(rme96->capture_suspend_buffer,
  2133. rme96->iobase + RME96_IO_REC_BUFFER, RME96_BUFFER_SIZE);
  2134. /* disable the DAC */
  2135. rme96->areg &= ~RME96_AR_DAC_EN;
  2136. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  2137. return 0;
  2138. }
  2139. static int rme96_resume(struct device *dev)
  2140. {
  2141. struct snd_card *card = dev_get_drvdata(dev);
  2142. struct rme96 *rme96 = card->private_data;
  2143. /* reset playback and record buffer pointers */
  2144. writel(0, rme96->iobase + RME96_IO_SET_PLAY_POS
  2145. + rme96->playback_pointer);
  2146. writel(0, rme96->iobase + RME96_IO_SET_REC_POS
  2147. + rme96->capture_pointer);
  2148. /* restore playback and capture buffers */
  2149. memcpy_toio(rme96->iobase + RME96_IO_PLAY_BUFFER,
  2150. rme96->playback_suspend_buffer, RME96_BUFFER_SIZE);
  2151. memcpy_toio(rme96->iobase + RME96_IO_REC_BUFFER,
  2152. rme96->capture_suspend_buffer, RME96_BUFFER_SIZE);
  2153. /* reset the ADC */
  2154. writel(rme96->areg | RME96_AR_PD2,
  2155. rme96->iobase + RME96_IO_ADDITIONAL_REG);
  2156. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  2157. /* reset and enable DAC, restore analog volume */
  2158. snd_rme96_reset_dac(rme96);
  2159. rme96->areg |= RME96_AR_DAC_EN;
  2160. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  2161. if (RME96_HAS_ANALOG_OUT(rme96)) {
  2162. usleep_range(3000, 10000);
  2163. snd_rme96_apply_dac_volume(rme96);
  2164. }
  2165. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  2166. return 0;
  2167. }
  2168. static SIMPLE_DEV_PM_OPS(rme96_pm, rme96_suspend, rme96_resume);
  2169. #define RME96_PM_OPS &rme96_pm
  2170. #else
  2171. #define RME96_PM_OPS NULL
  2172. #endif /* CONFIG_PM_SLEEP */
  2173. static void snd_rme96_card_free(struct snd_card *card)
  2174. {
  2175. snd_rme96_free(card->private_data);
  2176. }
  2177. static int
  2178. snd_rme96_probe(struct pci_dev *pci,
  2179. const struct pci_device_id *pci_id)
  2180. {
  2181. static int dev;
  2182. struct rme96 *rme96;
  2183. struct snd_card *card;
  2184. int err;
  2185. u8 val;
  2186. if (dev >= SNDRV_CARDS) {
  2187. return -ENODEV;
  2188. }
  2189. if (!enable[dev]) {
  2190. dev++;
  2191. return -ENOENT;
  2192. }
  2193. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  2194. sizeof(struct rme96), &card);
  2195. if (err < 0)
  2196. return err;
  2197. card->private_free = snd_rme96_card_free;
  2198. rme96 = card->private_data;
  2199. rme96->card = card;
  2200. rme96->pci = pci;
  2201. if ((err = snd_rme96_create(rme96)) < 0) {
  2202. snd_card_free(card);
  2203. return err;
  2204. }
  2205. #ifdef CONFIG_PM_SLEEP
  2206. rme96->playback_suspend_buffer = vmalloc(RME96_BUFFER_SIZE);
  2207. if (!rme96->playback_suspend_buffer) {
  2208. dev_err(card->dev,
  2209. "Failed to allocate playback suspend buffer!\n");
  2210. snd_card_free(card);
  2211. return -ENOMEM;
  2212. }
  2213. rme96->capture_suspend_buffer = vmalloc(RME96_BUFFER_SIZE);
  2214. if (!rme96->capture_suspend_buffer) {
  2215. dev_err(card->dev,
  2216. "Failed to allocate capture suspend buffer!\n");
  2217. snd_card_free(card);
  2218. return -ENOMEM;
  2219. }
  2220. #endif
  2221. strcpy(card->driver, "Digi96");
  2222. switch (rme96->pci->device) {
  2223. case PCI_DEVICE_ID_RME_DIGI96:
  2224. strcpy(card->shortname, "RME Digi96");
  2225. break;
  2226. case PCI_DEVICE_ID_RME_DIGI96_8:
  2227. strcpy(card->shortname, "RME Digi96/8");
  2228. break;
  2229. case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
  2230. strcpy(card->shortname, "RME Digi96/8 PRO");
  2231. break;
  2232. case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
  2233. pci_read_config_byte(rme96->pci, 8, &val);
  2234. if (val < 5) {
  2235. strcpy(card->shortname, "RME Digi96/8 PAD");
  2236. } else {
  2237. strcpy(card->shortname, "RME Digi96/8 PST");
  2238. }
  2239. break;
  2240. }
  2241. sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
  2242. rme96->port, rme96->irq);
  2243. if ((err = snd_card_register(card)) < 0) {
  2244. snd_card_free(card);
  2245. return err;
  2246. }
  2247. pci_set_drvdata(pci, card);
  2248. dev++;
  2249. return 0;
  2250. }
  2251. static void snd_rme96_remove(struct pci_dev *pci)
  2252. {
  2253. snd_card_free(pci_get_drvdata(pci));
  2254. }
  2255. static struct pci_driver rme96_driver = {
  2256. .name = KBUILD_MODNAME,
  2257. .id_table = snd_rme96_ids,
  2258. .probe = snd_rme96_probe,
  2259. .remove = snd_rme96_remove,
  2260. .driver = {
  2261. .pm = RME96_PM_OPS,
  2262. },
  2263. };
  2264. module_pci_driver(rme96_driver);