rme96.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535
  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. return 1; /* need to restore volume */
  674. } else {
  675. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  676. return 0;
  677. }
  678. }
  679. static int
  680. snd_rme96_capture_analog_setrate(struct rme96 *rme96,
  681. int rate)
  682. {
  683. switch (rate) {
  684. case 32000:
  685. rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) &
  686. ~RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
  687. break;
  688. case 44100:
  689. rme96->areg = ((rme96->areg & ~RME96_AR_FREQPAD_0) |
  690. RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
  691. break;
  692. case 48000:
  693. rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) |
  694. RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
  695. break;
  696. case 64000:
  697. if (rme96->rev < 4) {
  698. return -EINVAL;
  699. }
  700. rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) &
  701. ~RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
  702. break;
  703. case 88200:
  704. if (rme96->rev < 4) {
  705. return -EINVAL;
  706. }
  707. rme96->areg = ((rme96->areg & ~RME96_AR_FREQPAD_0) |
  708. RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
  709. break;
  710. case 96000:
  711. rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) |
  712. RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
  713. break;
  714. default:
  715. return -EINVAL;
  716. }
  717. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  718. return 0;
  719. }
  720. static int
  721. snd_rme96_setclockmode(struct rme96 *rme96,
  722. int mode)
  723. {
  724. switch (mode) {
  725. case RME96_CLOCKMODE_SLAVE:
  726. /* AutoSync */
  727. rme96->wcreg &= ~RME96_WCR_MASTER;
  728. rme96->areg &= ~RME96_AR_WSEL;
  729. break;
  730. case RME96_CLOCKMODE_MASTER:
  731. /* Internal */
  732. rme96->wcreg |= RME96_WCR_MASTER;
  733. rme96->areg &= ~RME96_AR_WSEL;
  734. break;
  735. case RME96_CLOCKMODE_WORDCLOCK:
  736. /* Word clock is a master mode */
  737. rme96->wcreg |= RME96_WCR_MASTER;
  738. rme96->areg |= RME96_AR_WSEL;
  739. break;
  740. default:
  741. return -EINVAL;
  742. }
  743. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  744. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  745. return 0;
  746. }
  747. static int
  748. snd_rme96_getclockmode(struct rme96 *rme96)
  749. {
  750. if (rme96->areg & RME96_AR_WSEL) {
  751. return RME96_CLOCKMODE_WORDCLOCK;
  752. }
  753. return (rme96->wcreg & RME96_WCR_MASTER) ? RME96_CLOCKMODE_MASTER :
  754. RME96_CLOCKMODE_SLAVE;
  755. }
  756. static int
  757. snd_rme96_setinputtype(struct rme96 *rme96,
  758. int type)
  759. {
  760. int n;
  761. switch (type) {
  762. case RME96_INPUT_OPTICAL:
  763. rme96->wcreg = (rme96->wcreg & ~RME96_WCR_INP_0) &
  764. ~RME96_WCR_INP_1;
  765. break;
  766. case RME96_INPUT_COAXIAL:
  767. rme96->wcreg = (rme96->wcreg | RME96_WCR_INP_0) &
  768. ~RME96_WCR_INP_1;
  769. break;
  770. case RME96_INPUT_INTERNAL:
  771. rme96->wcreg = (rme96->wcreg & ~RME96_WCR_INP_0) |
  772. RME96_WCR_INP_1;
  773. break;
  774. case RME96_INPUT_XLR:
  775. if ((rme96->pci->device != PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST &&
  776. rme96->pci->device != PCI_DEVICE_ID_RME_DIGI96_8_PRO) ||
  777. (rme96->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST &&
  778. rme96->rev > 4))
  779. {
  780. /* Only Digi96/8 PRO and Digi96/8 PAD supports XLR */
  781. return -EINVAL;
  782. }
  783. rme96->wcreg = (rme96->wcreg | RME96_WCR_INP_0) |
  784. RME96_WCR_INP_1;
  785. break;
  786. case RME96_INPUT_ANALOG:
  787. if (!RME96_HAS_ANALOG_IN(rme96)) {
  788. return -EINVAL;
  789. }
  790. rme96->areg |= RME96_AR_ANALOG;
  791. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  792. if (rme96->rev < 4) {
  793. /*
  794. * Revision less than 004 does not support 64 and
  795. * 88.2 kHz
  796. */
  797. if (snd_rme96_capture_getrate(rme96, &n) == 88200) {
  798. snd_rme96_capture_analog_setrate(rme96, 44100);
  799. }
  800. if (snd_rme96_capture_getrate(rme96, &n) == 64000) {
  801. snd_rme96_capture_analog_setrate(rme96, 32000);
  802. }
  803. }
  804. return 0;
  805. default:
  806. return -EINVAL;
  807. }
  808. if (type != RME96_INPUT_ANALOG && RME96_HAS_ANALOG_IN(rme96)) {
  809. rme96->areg &= ~RME96_AR_ANALOG;
  810. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  811. }
  812. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  813. return 0;
  814. }
  815. static int
  816. snd_rme96_getinputtype(struct rme96 *rme96)
  817. {
  818. if (rme96->areg & RME96_AR_ANALOG) {
  819. return RME96_INPUT_ANALOG;
  820. }
  821. return ((rme96->wcreg >> RME96_WCR_BITPOS_INP_0) & 1) +
  822. (((rme96->wcreg >> RME96_WCR_BITPOS_INP_1) & 1) << 1);
  823. }
  824. static void
  825. snd_rme96_setframelog(struct rme96 *rme96,
  826. int n_channels,
  827. int is_playback)
  828. {
  829. int frlog;
  830. if (n_channels == 2) {
  831. frlog = 1;
  832. } else {
  833. /* assume 8 channels */
  834. frlog = 3;
  835. }
  836. if (is_playback) {
  837. frlog += (rme96->wcreg & RME96_WCR_MODE24) ? 2 : 1;
  838. rme96->playback_frlog = frlog;
  839. } else {
  840. frlog += (rme96->wcreg & RME96_WCR_MODE24_2) ? 2 : 1;
  841. rme96->capture_frlog = frlog;
  842. }
  843. }
  844. static int
  845. snd_rme96_playback_setformat(struct rme96 *rme96, snd_pcm_format_t format)
  846. {
  847. switch (format) {
  848. case SNDRV_PCM_FORMAT_S16_LE:
  849. rme96->wcreg &= ~RME96_WCR_MODE24;
  850. break;
  851. case SNDRV_PCM_FORMAT_S32_LE:
  852. rme96->wcreg |= RME96_WCR_MODE24;
  853. break;
  854. default:
  855. return -EINVAL;
  856. }
  857. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  858. return 0;
  859. }
  860. static int
  861. snd_rme96_capture_setformat(struct rme96 *rme96, snd_pcm_format_t format)
  862. {
  863. switch (format) {
  864. case SNDRV_PCM_FORMAT_S16_LE:
  865. rme96->wcreg &= ~RME96_WCR_MODE24_2;
  866. break;
  867. case SNDRV_PCM_FORMAT_S32_LE:
  868. rme96->wcreg |= RME96_WCR_MODE24_2;
  869. break;
  870. default:
  871. return -EINVAL;
  872. }
  873. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  874. return 0;
  875. }
  876. static void
  877. snd_rme96_set_period_properties(struct rme96 *rme96,
  878. size_t period_bytes)
  879. {
  880. switch (period_bytes) {
  881. case RME96_LARGE_BLOCK_SIZE:
  882. rme96->wcreg &= ~RME96_WCR_ISEL;
  883. break;
  884. case RME96_SMALL_BLOCK_SIZE:
  885. rme96->wcreg |= RME96_WCR_ISEL;
  886. break;
  887. default:
  888. snd_BUG();
  889. break;
  890. }
  891. rme96->wcreg &= ~RME96_WCR_IDIS;
  892. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  893. }
  894. static int
  895. snd_rme96_playback_hw_params(struct snd_pcm_substream *substream,
  896. struct snd_pcm_hw_params *params)
  897. {
  898. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  899. struct snd_pcm_runtime *runtime = substream->runtime;
  900. int err, rate, dummy;
  901. bool apply_dac_volume = false;
  902. runtime->dma_area = (void __force *)(rme96->iobase +
  903. RME96_IO_PLAY_BUFFER);
  904. runtime->dma_addr = rme96->port + RME96_IO_PLAY_BUFFER;
  905. runtime->dma_bytes = RME96_BUFFER_SIZE;
  906. spin_lock_irq(&rme96->lock);
  907. if (!(rme96->wcreg & RME96_WCR_MASTER) &&
  908. snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
  909. (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
  910. {
  911. /* slave clock */
  912. if ((int)params_rate(params) != rate) {
  913. err = -EIO;
  914. goto error;
  915. }
  916. } else {
  917. err = snd_rme96_playback_setrate(rme96, params_rate(params));
  918. if (err < 0)
  919. goto error;
  920. apply_dac_volume = err > 0; /* need to restore volume later? */
  921. }
  922. err = snd_rme96_playback_setformat(rme96, params_format(params));
  923. if (err < 0)
  924. goto error;
  925. snd_rme96_setframelog(rme96, params_channels(params), 1);
  926. if (rme96->capture_periodsize != 0) {
  927. if (params_period_size(params) << rme96->playback_frlog !=
  928. rme96->capture_periodsize)
  929. {
  930. err = -EBUSY;
  931. goto error;
  932. }
  933. }
  934. rme96->playback_periodsize =
  935. params_period_size(params) << rme96->playback_frlog;
  936. snd_rme96_set_period_properties(rme96, rme96->playback_periodsize);
  937. /* S/PDIF setup */
  938. if ((rme96->wcreg & RME96_WCR_ADAT) == 0) {
  939. rme96->wcreg &= ~(RME96_WCR_PRO | RME96_WCR_DOLBY | RME96_WCR_EMP);
  940. writel(rme96->wcreg |= rme96->wcreg_spdif_stream, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  941. }
  942. err = 0;
  943. error:
  944. spin_unlock_irq(&rme96->lock);
  945. if (apply_dac_volume) {
  946. usleep_range(3000, 10000);
  947. snd_rme96_apply_dac_volume(rme96);
  948. }
  949. return err;
  950. }
  951. static int
  952. snd_rme96_capture_hw_params(struct snd_pcm_substream *substream,
  953. struct snd_pcm_hw_params *params)
  954. {
  955. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  956. struct snd_pcm_runtime *runtime = substream->runtime;
  957. int err, isadat, rate;
  958. runtime->dma_area = (void __force *)(rme96->iobase +
  959. RME96_IO_REC_BUFFER);
  960. runtime->dma_addr = rme96->port + RME96_IO_REC_BUFFER;
  961. runtime->dma_bytes = RME96_BUFFER_SIZE;
  962. spin_lock_irq(&rme96->lock);
  963. if ((err = snd_rme96_capture_setformat(rme96, params_format(params))) < 0) {
  964. spin_unlock_irq(&rme96->lock);
  965. return err;
  966. }
  967. if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
  968. if ((err = snd_rme96_capture_analog_setrate(rme96,
  969. params_rate(params))) < 0)
  970. {
  971. spin_unlock_irq(&rme96->lock);
  972. return err;
  973. }
  974. } else if ((rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0) {
  975. if ((int)params_rate(params) != rate) {
  976. spin_unlock_irq(&rme96->lock);
  977. return -EIO;
  978. }
  979. if ((isadat && runtime->hw.channels_min == 2) ||
  980. (!isadat && runtime->hw.channels_min == 8))
  981. {
  982. spin_unlock_irq(&rme96->lock);
  983. return -EIO;
  984. }
  985. }
  986. snd_rme96_setframelog(rme96, params_channels(params), 0);
  987. if (rme96->playback_periodsize != 0) {
  988. if (params_period_size(params) << rme96->capture_frlog !=
  989. rme96->playback_periodsize)
  990. {
  991. spin_unlock_irq(&rme96->lock);
  992. return -EBUSY;
  993. }
  994. }
  995. rme96->capture_periodsize =
  996. params_period_size(params) << rme96->capture_frlog;
  997. snd_rme96_set_period_properties(rme96, rme96->capture_periodsize);
  998. spin_unlock_irq(&rme96->lock);
  999. return 0;
  1000. }
  1001. static void
  1002. snd_rme96_trigger(struct rme96 *rme96,
  1003. int op)
  1004. {
  1005. if (op & RME96_TB_RESET_PLAYPOS)
  1006. writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
  1007. if (op & RME96_TB_RESET_CAPTUREPOS)
  1008. writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
  1009. if (op & RME96_TB_CLEAR_PLAYBACK_IRQ) {
  1010. rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1011. if (rme96->rcreg & RME96_RCR_IRQ)
  1012. writel(0, rme96->iobase + RME96_IO_CONFIRM_PLAY_IRQ);
  1013. }
  1014. if (op & RME96_TB_CLEAR_CAPTURE_IRQ) {
  1015. rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1016. if (rme96->rcreg & RME96_RCR_IRQ_2)
  1017. writel(0, rme96->iobase + RME96_IO_CONFIRM_REC_IRQ);
  1018. }
  1019. if (op & RME96_TB_START_PLAYBACK)
  1020. rme96->wcreg |= RME96_WCR_START;
  1021. if (op & RME96_TB_STOP_PLAYBACK)
  1022. rme96->wcreg &= ~RME96_WCR_START;
  1023. if (op & RME96_TB_START_CAPTURE)
  1024. rme96->wcreg |= RME96_WCR_START_2;
  1025. if (op & RME96_TB_STOP_CAPTURE)
  1026. rme96->wcreg &= ~RME96_WCR_START_2;
  1027. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1028. }
  1029. static irqreturn_t
  1030. snd_rme96_interrupt(int irq,
  1031. void *dev_id)
  1032. {
  1033. struct rme96 *rme96 = (struct rme96 *)dev_id;
  1034. rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1035. /* fastpath out, to ease interrupt sharing */
  1036. if (!((rme96->rcreg & RME96_RCR_IRQ) ||
  1037. (rme96->rcreg & RME96_RCR_IRQ_2)))
  1038. {
  1039. return IRQ_NONE;
  1040. }
  1041. if (rme96->rcreg & RME96_RCR_IRQ) {
  1042. /* playback */
  1043. snd_pcm_period_elapsed(rme96->playback_substream);
  1044. writel(0, rme96->iobase + RME96_IO_CONFIRM_PLAY_IRQ);
  1045. }
  1046. if (rme96->rcreg & RME96_RCR_IRQ_2) {
  1047. /* capture */
  1048. snd_pcm_period_elapsed(rme96->capture_substream);
  1049. writel(0, rme96->iobase + RME96_IO_CONFIRM_REC_IRQ);
  1050. }
  1051. return IRQ_HANDLED;
  1052. }
  1053. static unsigned int period_bytes[] = { RME96_SMALL_BLOCK_SIZE, RME96_LARGE_BLOCK_SIZE };
  1054. static struct snd_pcm_hw_constraint_list hw_constraints_period_bytes = {
  1055. .count = ARRAY_SIZE(period_bytes),
  1056. .list = period_bytes,
  1057. .mask = 0
  1058. };
  1059. static void
  1060. rme96_set_buffer_size_constraint(struct rme96 *rme96,
  1061. struct snd_pcm_runtime *runtime)
  1062. {
  1063. unsigned int size;
  1064. snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1065. RME96_BUFFER_SIZE);
  1066. if ((size = rme96->playback_periodsize) != 0 ||
  1067. (size = rme96->capture_periodsize) != 0)
  1068. snd_pcm_hw_constraint_single(runtime,
  1069. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1070. size);
  1071. else
  1072. snd_pcm_hw_constraint_list(runtime, 0,
  1073. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1074. &hw_constraints_period_bytes);
  1075. }
  1076. static int
  1077. snd_rme96_playback_spdif_open(struct snd_pcm_substream *substream)
  1078. {
  1079. int rate, dummy;
  1080. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1081. struct snd_pcm_runtime *runtime = substream->runtime;
  1082. snd_pcm_set_sync(substream);
  1083. spin_lock_irq(&rme96->lock);
  1084. if (rme96->playback_substream != NULL) {
  1085. spin_unlock_irq(&rme96->lock);
  1086. return -EBUSY;
  1087. }
  1088. rme96->wcreg &= ~RME96_WCR_ADAT;
  1089. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1090. rme96->playback_substream = substream;
  1091. spin_unlock_irq(&rme96->lock);
  1092. runtime->hw = snd_rme96_playback_spdif_info;
  1093. if (!(rme96->wcreg & RME96_WCR_MASTER) &&
  1094. snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
  1095. (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
  1096. {
  1097. /* slave clock */
  1098. runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
  1099. runtime->hw.rate_min = rate;
  1100. runtime->hw.rate_max = rate;
  1101. }
  1102. rme96_set_buffer_size_constraint(rme96, runtime);
  1103. rme96->wcreg_spdif_stream = rme96->wcreg_spdif;
  1104. rme96->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1105. snd_ctl_notify(rme96->card, SNDRV_CTL_EVENT_MASK_VALUE |
  1106. SNDRV_CTL_EVENT_MASK_INFO, &rme96->spdif_ctl->id);
  1107. return 0;
  1108. }
  1109. static int
  1110. snd_rme96_capture_spdif_open(struct snd_pcm_substream *substream)
  1111. {
  1112. int isadat, rate;
  1113. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1114. struct snd_pcm_runtime *runtime = substream->runtime;
  1115. snd_pcm_set_sync(substream);
  1116. runtime->hw = snd_rme96_capture_spdif_info;
  1117. if (snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
  1118. (rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0)
  1119. {
  1120. if (isadat) {
  1121. return -EIO;
  1122. }
  1123. runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
  1124. runtime->hw.rate_min = rate;
  1125. runtime->hw.rate_max = rate;
  1126. }
  1127. spin_lock_irq(&rme96->lock);
  1128. if (rme96->capture_substream != NULL) {
  1129. spin_unlock_irq(&rme96->lock);
  1130. return -EBUSY;
  1131. }
  1132. rme96->capture_substream = substream;
  1133. spin_unlock_irq(&rme96->lock);
  1134. rme96_set_buffer_size_constraint(rme96, runtime);
  1135. return 0;
  1136. }
  1137. static int
  1138. snd_rme96_playback_adat_open(struct snd_pcm_substream *substream)
  1139. {
  1140. int rate, dummy;
  1141. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1142. struct snd_pcm_runtime *runtime = substream->runtime;
  1143. snd_pcm_set_sync(substream);
  1144. spin_lock_irq(&rme96->lock);
  1145. if (rme96->playback_substream != NULL) {
  1146. spin_unlock_irq(&rme96->lock);
  1147. return -EBUSY;
  1148. }
  1149. rme96->wcreg |= RME96_WCR_ADAT;
  1150. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1151. rme96->playback_substream = substream;
  1152. spin_unlock_irq(&rme96->lock);
  1153. runtime->hw = snd_rme96_playback_adat_info;
  1154. if (!(rme96->wcreg & RME96_WCR_MASTER) &&
  1155. snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
  1156. (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
  1157. {
  1158. /* slave clock */
  1159. runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
  1160. runtime->hw.rate_min = rate;
  1161. runtime->hw.rate_max = rate;
  1162. }
  1163. rme96_set_buffer_size_constraint(rme96, runtime);
  1164. return 0;
  1165. }
  1166. static int
  1167. snd_rme96_capture_adat_open(struct snd_pcm_substream *substream)
  1168. {
  1169. int isadat, rate;
  1170. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1171. struct snd_pcm_runtime *runtime = substream->runtime;
  1172. snd_pcm_set_sync(substream);
  1173. runtime->hw = snd_rme96_capture_adat_info;
  1174. if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
  1175. /* makes no sense to use analog input. Note that analog
  1176. expension cards AEB4/8-I are RME96_INPUT_INTERNAL */
  1177. return -EIO;
  1178. }
  1179. if ((rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0) {
  1180. if (!isadat) {
  1181. return -EIO;
  1182. }
  1183. runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
  1184. runtime->hw.rate_min = rate;
  1185. runtime->hw.rate_max = rate;
  1186. }
  1187. spin_lock_irq(&rme96->lock);
  1188. if (rme96->capture_substream != NULL) {
  1189. spin_unlock_irq(&rme96->lock);
  1190. return -EBUSY;
  1191. }
  1192. rme96->capture_substream = substream;
  1193. spin_unlock_irq(&rme96->lock);
  1194. rme96_set_buffer_size_constraint(rme96, runtime);
  1195. return 0;
  1196. }
  1197. static int
  1198. snd_rme96_playback_close(struct snd_pcm_substream *substream)
  1199. {
  1200. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1201. int spdif = 0;
  1202. spin_lock_irq(&rme96->lock);
  1203. if (RME96_ISPLAYING(rme96)) {
  1204. snd_rme96_trigger(rme96, RME96_STOP_PLAYBACK);
  1205. }
  1206. rme96->playback_substream = NULL;
  1207. rme96->playback_periodsize = 0;
  1208. spdif = (rme96->wcreg & RME96_WCR_ADAT) == 0;
  1209. spin_unlock_irq(&rme96->lock);
  1210. if (spdif) {
  1211. rme96->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1212. snd_ctl_notify(rme96->card, SNDRV_CTL_EVENT_MASK_VALUE |
  1213. SNDRV_CTL_EVENT_MASK_INFO, &rme96->spdif_ctl->id);
  1214. }
  1215. return 0;
  1216. }
  1217. static int
  1218. snd_rme96_capture_close(struct snd_pcm_substream *substream)
  1219. {
  1220. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1221. spin_lock_irq(&rme96->lock);
  1222. if (RME96_ISRECORDING(rme96)) {
  1223. snd_rme96_trigger(rme96, RME96_STOP_CAPTURE);
  1224. }
  1225. rme96->capture_substream = NULL;
  1226. rme96->capture_periodsize = 0;
  1227. spin_unlock_irq(&rme96->lock);
  1228. return 0;
  1229. }
  1230. static int
  1231. snd_rme96_playback_prepare(struct snd_pcm_substream *substream)
  1232. {
  1233. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1234. spin_lock_irq(&rme96->lock);
  1235. if (RME96_ISPLAYING(rme96)) {
  1236. snd_rme96_trigger(rme96, RME96_STOP_PLAYBACK);
  1237. }
  1238. writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
  1239. spin_unlock_irq(&rme96->lock);
  1240. return 0;
  1241. }
  1242. static int
  1243. snd_rme96_capture_prepare(struct snd_pcm_substream *substream)
  1244. {
  1245. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1246. spin_lock_irq(&rme96->lock);
  1247. if (RME96_ISRECORDING(rme96)) {
  1248. snd_rme96_trigger(rme96, RME96_STOP_CAPTURE);
  1249. }
  1250. writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
  1251. spin_unlock_irq(&rme96->lock);
  1252. return 0;
  1253. }
  1254. static int
  1255. snd_rme96_playback_trigger(struct snd_pcm_substream *substream,
  1256. int cmd)
  1257. {
  1258. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1259. struct snd_pcm_substream *s;
  1260. bool sync;
  1261. snd_pcm_group_for_each_entry(s, substream) {
  1262. if (snd_pcm_substream_chip(s) == rme96)
  1263. snd_pcm_trigger_done(s, substream);
  1264. }
  1265. sync = (rme96->playback_substream && rme96->capture_substream) &&
  1266. (rme96->playback_substream->group ==
  1267. rme96->capture_substream->group);
  1268. switch (cmd) {
  1269. case SNDRV_PCM_TRIGGER_START:
  1270. if (!RME96_ISPLAYING(rme96)) {
  1271. if (substream != rme96->playback_substream)
  1272. return -EBUSY;
  1273. snd_rme96_trigger(rme96, sync ? RME96_START_BOTH
  1274. : RME96_START_PLAYBACK);
  1275. }
  1276. break;
  1277. case SNDRV_PCM_TRIGGER_SUSPEND:
  1278. case SNDRV_PCM_TRIGGER_STOP:
  1279. if (RME96_ISPLAYING(rme96)) {
  1280. if (substream != rme96->playback_substream)
  1281. return -EBUSY;
  1282. snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
  1283. : RME96_STOP_PLAYBACK);
  1284. }
  1285. break;
  1286. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1287. if (RME96_ISPLAYING(rme96))
  1288. snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
  1289. : RME96_STOP_PLAYBACK);
  1290. break;
  1291. case SNDRV_PCM_TRIGGER_RESUME:
  1292. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1293. if (!RME96_ISPLAYING(rme96))
  1294. snd_rme96_trigger(rme96, sync ? RME96_RESUME_BOTH
  1295. : RME96_RESUME_PLAYBACK);
  1296. break;
  1297. default:
  1298. return -EINVAL;
  1299. }
  1300. return 0;
  1301. }
  1302. static int
  1303. snd_rme96_capture_trigger(struct snd_pcm_substream *substream,
  1304. int cmd)
  1305. {
  1306. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1307. struct snd_pcm_substream *s;
  1308. bool sync;
  1309. snd_pcm_group_for_each_entry(s, substream) {
  1310. if (snd_pcm_substream_chip(s) == rme96)
  1311. snd_pcm_trigger_done(s, substream);
  1312. }
  1313. sync = (rme96->playback_substream && rme96->capture_substream) &&
  1314. (rme96->playback_substream->group ==
  1315. rme96->capture_substream->group);
  1316. switch (cmd) {
  1317. case SNDRV_PCM_TRIGGER_START:
  1318. if (!RME96_ISRECORDING(rme96)) {
  1319. if (substream != rme96->capture_substream)
  1320. return -EBUSY;
  1321. snd_rme96_trigger(rme96, sync ? RME96_START_BOTH
  1322. : RME96_START_CAPTURE);
  1323. }
  1324. break;
  1325. case SNDRV_PCM_TRIGGER_SUSPEND:
  1326. case SNDRV_PCM_TRIGGER_STOP:
  1327. if (RME96_ISRECORDING(rme96)) {
  1328. if (substream != rme96->capture_substream)
  1329. return -EBUSY;
  1330. snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
  1331. : RME96_STOP_CAPTURE);
  1332. }
  1333. break;
  1334. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1335. if (RME96_ISRECORDING(rme96))
  1336. snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
  1337. : RME96_STOP_CAPTURE);
  1338. break;
  1339. case SNDRV_PCM_TRIGGER_RESUME:
  1340. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1341. if (!RME96_ISRECORDING(rme96))
  1342. snd_rme96_trigger(rme96, sync ? RME96_RESUME_BOTH
  1343. : RME96_RESUME_CAPTURE);
  1344. break;
  1345. default:
  1346. return -EINVAL;
  1347. }
  1348. return 0;
  1349. }
  1350. static snd_pcm_uframes_t
  1351. snd_rme96_playback_pointer(struct snd_pcm_substream *substream)
  1352. {
  1353. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1354. return snd_rme96_playback_ptr(rme96);
  1355. }
  1356. static snd_pcm_uframes_t
  1357. snd_rme96_capture_pointer(struct snd_pcm_substream *substream)
  1358. {
  1359. struct rme96 *rme96 = snd_pcm_substream_chip(substream);
  1360. return snd_rme96_capture_ptr(rme96);
  1361. }
  1362. static struct snd_pcm_ops snd_rme96_playback_spdif_ops = {
  1363. .open = snd_rme96_playback_spdif_open,
  1364. .close = snd_rme96_playback_close,
  1365. .ioctl = snd_pcm_lib_ioctl,
  1366. .hw_params = snd_rme96_playback_hw_params,
  1367. .prepare = snd_rme96_playback_prepare,
  1368. .trigger = snd_rme96_playback_trigger,
  1369. .pointer = snd_rme96_playback_pointer,
  1370. .copy = snd_rme96_playback_copy,
  1371. .silence = snd_rme96_playback_silence,
  1372. .mmap = snd_pcm_lib_mmap_iomem,
  1373. };
  1374. static struct snd_pcm_ops snd_rme96_capture_spdif_ops = {
  1375. .open = snd_rme96_capture_spdif_open,
  1376. .close = snd_rme96_capture_close,
  1377. .ioctl = snd_pcm_lib_ioctl,
  1378. .hw_params = snd_rme96_capture_hw_params,
  1379. .prepare = snd_rme96_capture_prepare,
  1380. .trigger = snd_rme96_capture_trigger,
  1381. .pointer = snd_rme96_capture_pointer,
  1382. .copy = snd_rme96_capture_copy,
  1383. .mmap = snd_pcm_lib_mmap_iomem,
  1384. };
  1385. static struct snd_pcm_ops snd_rme96_playback_adat_ops = {
  1386. .open = snd_rme96_playback_adat_open,
  1387. .close = snd_rme96_playback_close,
  1388. .ioctl = snd_pcm_lib_ioctl,
  1389. .hw_params = snd_rme96_playback_hw_params,
  1390. .prepare = snd_rme96_playback_prepare,
  1391. .trigger = snd_rme96_playback_trigger,
  1392. .pointer = snd_rme96_playback_pointer,
  1393. .copy = snd_rme96_playback_copy,
  1394. .silence = snd_rme96_playback_silence,
  1395. .mmap = snd_pcm_lib_mmap_iomem,
  1396. };
  1397. static struct snd_pcm_ops snd_rme96_capture_adat_ops = {
  1398. .open = snd_rme96_capture_adat_open,
  1399. .close = snd_rme96_capture_close,
  1400. .ioctl = snd_pcm_lib_ioctl,
  1401. .hw_params = snd_rme96_capture_hw_params,
  1402. .prepare = snd_rme96_capture_prepare,
  1403. .trigger = snd_rme96_capture_trigger,
  1404. .pointer = snd_rme96_capture_pointer,
  1405. .copy = snd_rme96_capture_copy,
  1406. .mmap = snd_pcm_lib_mmap_iomem,
  1407. };
  1408. static void
  1409. snd_rme96_free(void *private_data)
  1410. {
  1411. struct rme96 *rme96 = (struct rme96 *)private_data;
  1412. if (rme96 == NULL) {
  1413. return;
  1414. }
  1415. if (rme96->irq >= 0) {
  1416. snd_rme96_trigger(rme96, RME96_STOP_BOTH);
  1417. rme96->areg &= ~RME96_AR_DAC_EN;
  1418. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  1419. free_irq(rme96->irq, (void *)rme96);
  1420. rme96->irq = -1;
  1421. }
  1422. if (rme96->iobase) {
  1423. iounmap(rme96->iobase);
  1424. rme96->iobase = NULL;
  1425. }
  1426. if (rme96->port) {
  1427. pci_release_regions(rme96->pci);
  1428. rme96->port = 0;
  1429. }
  1430. #ifdef CONFIG_PM_SLEEP
  1431. vfree(rme96->playback_suspend_buffer);
  1432. vfree(rme96->capture_suspend_buffer);
  1433. #endif
  1434. pci_disable_device(rme96->pci);
  1435. }
  1436. static void
  1437. snd_rme96_free_spdif_pcm(struct snd_pcm *pcm)
  1438. {
  1439. struct rme96 *rme96 = pcm->private_data;
  1440. rme96->spdif_pcm = NULL;
  1441. }
  1442. static void
  1443. snd_rme96_free_adat_pcm(struct snd_pcm *pcm)
  1444. {
  1445. struct rme96 *rme96 = pcm->private_data;
  1446. rme96->adat_pcm = NULL;
  1447. }
  1448. static int
  1449. snd_rme96_create(struct rme96 *rme96)
  1450. {
  1451. struct pci_dev *pci = rme96->pci;
  1452. int err;
  1453. rme96->irq = -1;
  1454. spin_lock_init(&rme96->lock);
  1455. if ((err = pci_enable_device(pci)) < 0)
  1456. return err;
  1457. if ((err = pci_request_regions(pci, "RME96")) < 0)
  1458. return err;
  1459. rme96->port = pci_resource_start(rme96->pci, 0);
  1460. rme96->iobase = ioremap_nocache(rme96->port, RME96_IO_SIZE);
  1461. if (!rme96->iobase) {
  1462. dev_err(rme96->card->dev,
  1463. "unable to remap memory region 0x%lx-0x%lx\n",
  1464. rme96->port, rme96->port + RME96_IO_SIZE - 1);
  1465. return -ENOMEM;
  1466. }
  1467. if (request_irq(pci->irq, snd_rme96_interrupt, IRQF_SHARED,
  1468. KBUILD_MODNAME, rme96)) {
  1469. dev_err(rme96->card->dev, "unable to grab IRQ %d\n", pci->irq);
  1470. return -EBUSY;
  1471. }
  1472. rme96->irq = pci->irq;
  1473. /* read the card's revision number */
  1474. pci_read_config_byte(pci, 8, &rme96->rev);
  1475. /* set up ALSA pcm device for S/PDIF */
  1476. if ((err = snd_pcm_new(rme96->card, "Digi96 IEC958", 0,
  1477. 1, 1, &rme96->spdif_pcm)) < 0)
  1478. {
  1479. return err;
  1480. }
  1481. rme96->spdif_pcm->private_data = rme96;
  1482. rme96->spdif_pcm->private_free = snd_rme96_free_spdif_pcm;
  1483. strcpy(rme96->spdif_pcm->name, "Digi96 IEC958");
  1484. snd_pcm_set_ops(rme96->spdif_pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme96_playback_spdif_ops);
  1485. snd_pcm_set_ops(rme96->spdif_pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme96_capture_spdif_ops);
  1486. rme96->spdif_pcm->info_flags = 0;
  1487. /* set up ALSA pcm device for ADAT */
  1488. if (pci->device == PCI_DEVICE_ID_RME_DIGI96) {
  1489. /* ADAT is not available on the base model */
  1490. rme96->adat_pcm = NULL;
  1491. } else {
  1492. if ((err = snd_pcm_new(rme96->card, "Digi96 ADAT", 1,
  1493. 1, 1, &rme96->adat_pcm)) < 0)
  1494. {
  1495. return err;
  1496. }
  1497. rme96->adat_pcm->private_data = rme96;
  1498. rme96->adat_pcm->private_free = snd_rme96_free_adat_pcm;
  1499. strcpy(rme96->adat_pcm->name, "Digi96 ADAT");
  1500. snd_pcm_set_ops(rme96->adat_pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme96_playback_adat_ops);
  1501. snd_pcm_set_ops(rme96->adat_pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme96_capture_adat_ops);
  1502. rme96->adat_pcm->info_flags = 0;
  1503. }
  1504. rme96->playback_periodsize = 0;
  1505. rme96->capture_periodsize = 0;
  1506. /* make sure playback/capture is stopped, if by some reason active */
  1507. snd_rme96_trigger(rme96, RME96_STOP_BOTH);
  1508. /* set default values in registers */
  1509. rme96->wcreg =
  1510. RME96_WCR_FREQ_1 | /* set 44.1 kHz playback */
  1511. RME96_WCR_SEL | /* normal playback */
  1512. RME96_WCR_MASTER | /* set to master clock mode */
  1513. RME96_WCR_INP_0; /* set coaxial input */
  1514. rme96->areg = RME96_AR_FREQPAD_1; /* set 44.1 kHz analog capture */
  1515. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1516. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  1517. /* reset the ADC */
  1518. writel(rme96->areg | RME96_AR_PD2,
  1519. rme96->iobase + RME96_IO_ADDITIONAL_REG);
  1520. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  1521. /* reset and enable the DAC (order is important). */
  1522. snd_rme96_reset_dac(rme96);
  1523. rme96->areg |= RME96_AR_DAC_EN;
  1524. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  1525. /* reset playback and record buffer pointers */
  1526. writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
  1527. writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
  1528. /* reset volume */
  1529. rme96->vol[0] = rme96->vol[1] = 0;
  1530. if (RME96_HAS_ANALOG_OUT(rme96)) {
  1531. snd_rme96_apply_dac_volume(rme96);
  1532. }
  1533. /* init switch interface */
  1534. if ((err = snd_rme96_create_switches(rme96->card, rme96)) < 0) {
  1535. return err;
  1536. }
  1537. /* init proc interface */
  1538. snd_rme96_proc_init(rme96);
  1539. return 0;
  1540. }
  1541. /*
  1542. * proc interface
  1543. */
  1544. static void
  1545. snd_rme96_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  1546. {
  1547. int n;
  1548. struct rme96 *rme96 = entry->private_data;
  1549. rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1550. snd_iprintf(buffer, rme96->card->longname);
  1551. snd_iprintf(buffer, " (index #%d)\n", rme96->card->number + 1);
  1552. snd_iprintf(buffer, "\nGeneral settings\n");
  1553. if (rme96->wcreg & RME96_WCR_IDIS) {
  1554. snd_iprintf(buffer, " period size: N/A (interrupts "
  1555. "disabled)\n");
  1556. } else if (rme96->wcreg & RME96_WCR_ISEL) {
  1557. snd_iprintf(buffer, " period size: 2048 bytes\n");
  1558. } else {
  1559. snd_iprintf(buffer, " period size: 8192 bytes\n");
  1560. }
  1561. snd_iprintf(buffer, "\nInput settings\n");
  1562. switch (snd_rme96_getinputtype(rme96)) {
  1563. case RME96_INPUT_OPTICAL:
  1564. snd_iprintf(buffer, " input: optical");
  1565. break;
  1566. case RME96_INPUT_COAXIAL:
  1567. snd_iprintf(buffer, " input: coaxial");
  1568. break;
  1569. case RME96_INPUT_INTERNAL:
  1570. snd_iprintf(buffer, " input: internal");
  1571. break;
  1572. case RME96_INPUT_XLR:
  1573. snd_iprintf(buffer, " input: XLR");
  1574. break;
  1575. case RME96_INPUT_ANALOG:
  1576. snd_iprintf(buffer, " input: analog");
  1577. break;
  1578. }
  1579. if (snd_rme96_capture_getrate(rme96, &n) < 0) {
  1580. snd_iprintf(buffer, "\n sample rate: no valid signal\n");
  1581. } else {
  1582. if (n) {
  1583. snd_iprintf(buffer, " (8 channels)\n");
  1584. } else {
  1585. snd_iprintf(buffer, " (2 channels)\n");
  1586. }
  1587. snd_iprintf(buffer, " sample rate: %d Hz\n",
  1588. snd_rme96_capture_getrate(rme96, &n));
  1589. }
  1590. if (rme96->wcreg & RME96_WCR_MODE24_2) {
  1591. snd_iprintf(buffer, " sample format: 24 bit\n");
  1592. } else {
  1593. snd_iprintf(buffer, " sample format: 16 bit\n");
  1594. }
  1595. snd_iprintf(buffer, "\nOutput settings\n");
  1596. if (rme96->wcreg & RME96_WCR_SEL) {
  1597. snd_iprintf(buffer, " output signal: normal playback\n");
  1598. } else {
  1599. snd_iprintf(buffer, " output signal: same as input\n");
  1600. }
  1601. snd_iprintf(buffer, " sample rate: %d Hz\n",
  1602. snd_rme96_playback_getrate(rme96));
  1603. if (rme96->wcreg & RME96_WCR_MODE24) {
  1604. snd_iprintf(buffer, " sample format: 24 bit\n");
  1605. } else {
  1606. snd_iprintf(buffer, " sample format: 16 bit\n");
  1607. }
  1608. if (rme96->areg & RME96_AR_WSEL) {
  1609. snd_iprintf(buffer, " sample clock source: word clock\n");
  1610. } else if (rme96->wcreg & RME96_WCR_MASTER) {
  1611. snd_iprintf(buffer, " sample clock source: internal\n");
  1612. } else if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
  1613. snd_iprintf(buffer, " sample clock source: autosync (internal anyway due to analog input setting)\n");
  1614. } else if (snd_rme96_capture_getrate(rme96, &n) < 0) {
  1615. snd_iprintf(buffer, " sample clock source: autosync (internal anyway due to no valid signal)\n");
  1616. } else {
  1617. snd_iprintf(buffer, " sample clock source: autosync\n");
  1618. }
  1619. if (rme96->wcreg & RME96_WCR_PRO) {
  1620. snd_iprintf(buffer, " format: AES/EBU (professional)\n");
  1621. } else {
  1622. snd_iprintf(buffer, " format: IEC958 (consumer)\n");
  1623. }
  1624. if (rme96->wcreg & RME96_WCR_EMP) {
  1625. snd_iprintf(buffer, " emphasis: on\n");
  1626. } else {
  1627. snd_iprintf(buffer, " emphasis: off\n");
  1628. }
  1629. if (rme96->wcreg & RME96_WCR_DOLBY) {
  1630. snd_iprintf(buffer, " non-audio (dolby): on\n");
  1631. } else {
  1632. snd_iprintf(buffer, " non-audio (dolby): off\n");
  1633. }
  1634. if (RME96_HAS_ANALOG_IN(rme96)) {
  1635. snd_iprintf(buffer, "\nAnalog output settings\n");
  1636. switch (snd_rme96_getmontracks(rme96)) {
  1637. case RME96_MONITOR_TRACKS_1_2:
  1638. snd_iprintf(buffer, " monitored ADAT tracks: 1+2\n");
  1639. break;
  1640. case RME96_MONITOR_TRACKS_3_4:
  1641. snd_iprintf(buffer, " monitored ADAT tracks: 3+4\n");
  1642. break;
  1643. case RME96_MONITOR_TRACKS_5_6:
  1644. snd_iprintf(buffer, " monitored ADAT tracks: 5+6\n");
  1645. break;
  1646. case RME96_MONITOR_TRACKS_7_8:
  1647. snd_iprintf(buffer, " monitored ADAT tracks: 7+8\n");
  1648. break;
  1649. }
  1650. switch (snd_rme96_getattenuation(rme96)) {
  1651. case RME96_ATTENUATION_0:
  1652. snd_iprintf(buffer, " attenuation: 0 dB\n");
  1653. break;
  1654. case RME96_ATTENUATION_6:
  1655. snd_iprintf(buffer, " attenuation: -6 dB\n");
  1656. break;
  1657. case RME96_ATTENUATION_12:
  1658. snd_iprintf(buffer, " attenuation: -12 dB\n");
  1659. break;
  1660. case RME96_ATTENUATION_18:
  1661. snd_iprintf(buffer, " attenuation: -18 dB\n");
  1662. break;
  1663. }
  1664. snd_iprintf(buffer, " volume left: %u\n", rme96->vol[0]);
  1665. snd_iprintf(buffer, " volume right: %u\n", rme96->vol[1]);
  1666. }
  1667. }
  1668. static void snd_rme96_proc_init(struct rme96 *rme96)
  1669. {
  1670. struct snd_info_entry *entry;
  1671. if (! snd_card_proc_new(rme96->card, "rme96", &entry))
  1672. snd_info_set_text_ops(entry, rme96, snd_rme96_proc_read);
  1673. }
  1674. /*
  1675. * control interface
  1676. */
  1677. #define snd_rme96_info_loopback_control snd_ctl_boolean_mono_info
  1678. static int
  1679. snd_rme96_get_loopback_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1680. {
  1681. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1682. spin_lock_irq(&rme96->lock);
  1683. ucontrol->value.integer.value[0] = rme96->wcreg & RME96_WCR_SEL ? 0 : 1;
  1684. spin_unlock_irq(&rme96->lock);
  1685. return 0;
  1686. }
  1687. static int
  1688. snd_rme96_put_loopback_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1689. {
  1690. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1691. unsigned int val;
  1692. int change;
  1693. val = ucontrol->value.integer.value[0] ? 0 : RME96_WCR_SEL;
  1694. spin_lock_irq(&rme96->lock);
  1695. val = (rme96->wcreg & ~RME96_WCR_SEL) | val;
  1696. change = val != rme96->wcreg;
  1697. rme96->wcreg = val;
  1698. writel(val, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1699. spin_unlock_irq(&rme96->lock);
  1700. return change;
  1701. }
  1702. static int
  1703. snd_rme96_info_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1704. {
  1705. static const char * const _texts[5] = {
  1706. "Optical", "Coaxial", "Internal", "XLR", "Analog"
  1707. };
  1708. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1709. const char *texts[5] = {
  1710. _texts[0], _texts[1], _texts[2], _texts[3], _texts[4]
  1711. };
  1712. int num_items;
  1713. switch (rme96->pci->device) {
  1714. case PCI_DEVICE_ID_RME_DIGI96:
  1715. case PCI_DEVICE_ID_RME_DIGI96_8:
  1716. num_items = 3;
  1717. break;
  1718. case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
  1719. num_items = 4;
  1720. break;
  1721. case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
  1722. if (rme96->rev > 4) {
  1723. /* PST */
  1724. num_items = 4;
  1725. texts[3] = _texts[4]; /* Analog instead of XLR */
  1726. } else {
  1727. /* PAD */
  1728. num_items = 5;
  1729. }
  1730. break;
  1731. default:
  1732. snd_BUG();
  1733. return -EINVAL;
  1734. }
  1735. return snd_ctl_enum_info(uinfo, 1, num_items, texts);
  1736. }
  1737. static int
  1738. snd_rme96_get_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1739. {
  1740. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1741. unsigned int items = 3;
  1742. spin_lock_irq(&rme96->lock);
  1743. ucontrol->value.enumerated.item[0] = snd_rme96_getinputtype(rme96);
  1744. switch (rme96->pci->device) {
  1745. case PCI_DEVICE_ID_RME_DIGI96:
  1746. case PCI_DEVICE_ID_RME_DIGI96_8:
  1747. items = 3;
  1748. break;
  1749. case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
  1750. items = 4;
  1751. break;
  1752. case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
  1753. if (rme96->rev > 4) {
  1754. /* for handling PST case, (INPUT_ANALOG is moved to INPUT_XLR */
  1755. if (ucontrol->value.enumerated.item[0] == RME96_INPUT_ANALOG) {
  1756. ucontrol->value.enumerated.item[0] = RME96_INPUT_XLR;
  1757. }
  1758. items = 4;
  1759. } else {
  1760. items = 5;
  1761. }
  1762. break;
  1763. default:
  1764. snd_BUG();
  1765. break;
  1766. }
  1767. if (ucontrol->value.enumerated.item[0] >= items) {
  1768. ucontrol->value.enumerated.item[0] = items - 1;
  1769. }
  1770. spin_unlock_irq(&rme96->lock);
  1771. return 0;
  1772. }
  1773. static int
  1774. snd_rme96_put_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1775. {
  1776. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1777. unsigned int val;
  1778. int change, items = 3;
  1779. switch (rme96->pci->device) {
  1780. case PCI_DEVICE_ID_RME_DIGI96:
  1781. case PCI_DEVICE_ID_RME_DIGI96_8:
  1782. items = 3;
  1783. break;
  1784. case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
  1785. items = 4;
  1786. break;
  1787. case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
  1788. if (rme96->rev > 4) {
  1789. items = 4;
  1790. } else {
  1791. items = 5;
  1792. }
  1793. break;
  1794. default:
  1795. snd_BUG();
  1796. break;
  1797. }
  1798. val = ucontrol->value.enumerated.item[0] % items;
  1799. /* special case for PST */
  1800. if (rme96->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST && rme96->rev > 4) {
  1801. if (val == RME96_INPUT_XLR) {
  1802. val = RME96_INPUT_ANALOG;
  1803. }
  1804. }
  1805. spin_lock_irq(&rme96->lock);
  1806. change = (int)val != snd_rme96_getinputtype(rme96);
  1807. snd_rme96_setinputtype(rme96, val);
  1808. spin_unlock_irq(&rme96->lock);
  1809. return change;
  1810. }
  1811. static int
  1812. snd_rme96_info_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1813. {
  1814. static const char * const texts[3] = { "AutoSync", "Internal", "Word" };
  1815. return snd_ctl_enum_info(uinfo, 1, 3, texts);
  1816. }
  1817. static int
  1818. snd_rme96_get_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1819. {
  1820. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1821. spin_lock_irq(&rme96->lock);
  1822. ucontrol->value.enumerated.item[0] = snd_rme96_getclockmode(rme96);
  1823. spin_unlock_irq(&rme96->lock);
  1824. return 0;
  1825. }
  1826. static int
  1827. snd_rme96_put_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1828. {
  1829. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1830. unsigned int val;
  1831. int change;
  1832. val = ucontrol->value.enumerated.item[0] % 3;
  1833. spin_lock_irq(&rme96->lock);
  1834. change = (int)val != snd_rme96_getclockmode(rme96);
  1835. snd_rme96_setclockmode(rme96, val);
  1836. spin_unlock_irq(&rme96->lock);
  1837. return change;
  1838. }
  1839. static int
  1840. snd_rme96_info_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1841. {
  1842. static const char * const texts[4] = {
  1843. "0 dB", "-6 dB", "-12 dB", "-18 dB"
  1844. };
  1845. return snd_ctl_enum_info(uinfo, 1, 4, texts);
  1846. }
  1847. static int
  1848. snd_rme96_get_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1849. {
  1850. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1851. spin_lock_irq(&rme96->lock);
  1852. ucontrol->value.enumerated.item[0] = snd_rme96_getattenuation(rme96);
  1853. spin_unlock_irq(&rme96->lock);
  1854. return 0;
  1855. }
  1856. static int
  1857. snd_rme96_put_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1858. {
  1859. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1860. unsigned int val;
  1861. int change;
  1862. val = ucontrol->value.enumerated.item[0] % 4;
  1863. spin_lock_irq(&rme96->lock);
  1864. change = (int)val != snd_rme96_getattenuation(rme96);
  1865. snd_rme96_setattenuation(rme96, val);
  1866. spin_unlock_irq(&rme96->lock);
  1867. return change;
  1868. }
  1869. static int
  1870. snd_rme96_info_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1871. {
  1872. static const char * const texts[4] = { "1+2", "3+4", "5+6", "7+8" };
  1873. return snd_ctl_enum_info(uinfo, 1, 4, texts);
  1874. }
  1875. static int
  1876. snd_rme96_get_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1877. {
  1878. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1879. spin_lock_irq(&rme96->lock);
  1880. ucontrol->value.enumerated.item[0] = snd_rme96_getmontracks(rme96);
  1881. spin_unlock_irq(&rme96->lock);
  1882. return 0;
  1883. }
  1884. static int
  1885. snd_rme96_put_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1886. {
  1887. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1888. unsigned int val;
  1889. int change;
  1890. val = ucontrol->value.enumerated.item[0] % 4;
  1891. spin_lock_irq(&rme96->lock);
  1892. change = (int)val != snd_rme96_getmontracks(rme96);
  1893. snd_rme96_setmontracks(rme96, val);
  1894. spin_unlock_irq(&rme96->lock);
  1895. return change;
  1896. }
  1897. static u32 snd_rme96_convert_from_aes(struct snd_aes_iec958 *aes)
  1898. {
  1899. u32 val = 0;
  1900. val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? RME96_WCR_PRO : 0;
  1901. val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? RME96_WCR_DOLBY : 0;
  1902. if (val & RME96_WCR_PRO)
  1903. val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? RME96_WCR_EMP : 0;
  1904. else
  1905. val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? RME96_WCR_EMP : 0;
  1906. return val;
  1907. }
  1908. static void snd_rme96_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
  1909. {
  1910. aes->status[0] = ((val & RME96_WCR_PRO) ? IEC958_AES0_PROFESSIONAL : 0) |
  1911. ((val & RME96_WCR_DOLBY) ? IEC958_AES0_NONAUDIO : 0);
  1912. if (val & RME96_WCR_PRO)
  1913. aes->status[0] |= (val & RME96_WCR_EMP) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
  1914. else
  1915. aes->status[0] |= (val & RME96_WCR_EMP) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
  1916. }
  1917. static int snd_rme96_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1918. {
  1919. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1920. uinfo->count = 1;
  1921. return 0;
  1922. }
  1923. static int snd_rme96_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1924. {
  1925. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1926. snd_rme96_convert_to_aes(&ucontrol->value.iec958, rme96->wcreg_spdif);
  1927. return 0;
  1928. }
  1929. static int snd_rme96_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1930. {
  1931. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1932. int change;
  1933. u32 val;
  1934. val = snd_rme96_convert_from_aes(&ucontrol->value.iec958);
  1935. spin_lock_irq(&rme96->lock);
  1936. change = val != rme96->wcreg_spdif;
  1937. rme96->wcreg_spdif = val;
  1938. spin_unlock_irq(&rme96->lock);
  1939. return change;
  1940. }
  1941. static int snd_rme96_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1942. {
  1943. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1944. uinfo->count = 1;
  1945. return 0;
  1946. }
  1947. static int snd_rme96_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1948. {
  1949. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1950. snd_rme96_convert_to_aes(&ucontrol->value.iec958, rme96->wcreg_spdif_stream);
  1951. return 0;
  1952. }
  1953. static int snd_rme96_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1954. {
  1955. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1956. int change;
  1957. u32 val;
  1958. val = snd_rme96_convert_from_aes(&ucontrol->value.iec958);
  1959. spin_lock_irq(&rme96->lock);
  1960. change = val != rme96->wcreg_spdif_stream;
  1961. rme96->wcreg_spdif_stream = val;
  1962. rme96->wcreg &= ~(RME96_WCR_PRO | RME96_WCR_DOLBY | RME96_WCR_EMP);
  1963. rme96->wcreg |= val;
  1964. writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
  1965. spin_unlock_irq(&rme96->lock);
  1966. return change;
  1967. }
  1968. static int snd_rme96_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1969. {
  1970. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1971. uinfo->count = 1;
  1972. return 0;
  1973. }
  1974. static int snd_rme96_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1975. {
  1976. ucontrol->value.iec958.status[0] = kcontrol->private_value;
  1977. return 0;
  1978. }
  1979. static int
  1980. snd_rme96_dac_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1981. {
  1982. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1983. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1984. uinfo->count = 2;
  1985. uinfo->value.integer.min = 0;
  1986. uinfo->value.integer.max = RME96_185X_MAX_OUT(rme96);
  1987. return 0;
  1988. }
  1989. static int
  1990. snd_rme96_dac_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *u)
  1991. {
  1992. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  1993. spin_lock_irq(&rme96->lock);
  1994. u->value.integer.value[0] = rme96->vol[0];
  1995. u->value.integer.value[1] = rme96->vol[1];
  1996. spin_unlock_irq(&rme96->lock);
  1997. return 0;
  1998. }
  1999. static int
  2000. snd_rme96_dac_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *u)
  2001. {
  2002. struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
  2003. int change = 0;
  2004. unsigned int vol, maxvol;
  2005. if (!RME96_HAS_ANALOG_OUT(rme96))
  2006. return -EINVAL;
  2007. maxvol = RME96_185X_MAX_OUT(rme96);
  2008. spin_lock_irq(&rme96->lock);
  2009. vol = u->value.integer.value[0];
  2010. if (vol != rme96->vol[0] && vol <= maxvol) {
  2011. rme96->vol[0] = vol;
  2012. change = 1;
  2013. }
  2014. vol = u->value.integer.value[1];
  2015. if (vol != rme96->vol[1] && vol <= maxvol) {
  2016. rme96->vol[1] = vol;
  2017. change = 1;
  2018. }
  2019. if (change)
  2020. snd_rme96_apply_dac_volume(rme96);
  2021. spin_unlock_irq(&rme96->lock);
  2022. return change;
  2023. }
  2024. static struct snd_kcontrol_new snd_rme96_controls[] = {
  2025. {
  2026. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2027. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  2028. .info = snd_rme96_control_spdif_info,
  2029. .get = snd_rme96_control_spdif_get,
  2030. .put = snd_rme96_control_spdif_put
  2031. },
  2032. {
  2033. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2034. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2035. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  2036. .info = snd_rme96_control_spdif_stream_info,
  2037. .get = snd_rme96_control_spdif_stream_get,
  2038. .put = snd_rme96_control_spdif_stream_put
  2039. },
  2040. {
  2041. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2042. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2043. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  2044. .info = snd_rme96_control_spdif_mask_info,
  2045. .get = snd_rme96_control_spdif_mask_get,
  2046. .private_value = IEC958_AES0_NONAUDIO |
  2047. IEC958_AES0_PROFESSIONAL |
  2048. IEC958_AES0_CON_EMPHASIS
  2049. },
  2050. {
  2051. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2052. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2053. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  2054. .info = snd_rme96_control_spdif_mask_info,
  2055. .get = snd_rme96_control_spdif_mask_get,
  2056. .private_value = IEC958_AES0_NONAUDIO |
  2057. IEC958_AES0_PROFESSIONAL |
  2058. IEC958_AES0_PRO_EMPHASIS
  2059. },
  2060. {
  2061. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2062. .name = "Input Connector",
  2063. .info = snd_rme96_info_inputtype_control,
  2064. .get = snd_rme96_get_inputtype_control,
  2065. .put = snd_rme96_put_inputtype_control
  2066. },
  2067. {
  2068. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2069. .name = "Loopback Input",
  2070. .info = snd_rme96_info_loopback_control,
  2071. .get = snd_rme96_get_loopback_control,
  2072. .put = snd_rme96_put_loopback_control
  2073. },
  2074. {
  2075. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2076. .name = "Sample Clock Source",
  2077. .info = snd_rme96_info_clockmode_control,
  2078. .get = snd_rme96_get_clockmode_control,
  2079. .put = snd_rme96_put_clockmode_control
  2080. },
  2081. {
  2082. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2083. .name = "Monitor Tracks",
  2084. .info = snd_rme96_info_montracks_control,
  2085. .get = snd_rme96_get_montracks_control,
  2086. .put = snd_rme96_put_montracks_control
  2087. },
  2088. {
  2089. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2090. .name = "Attenuation",
  2091. .info = snd_rme96_info_attenuation_control,
  2092. .get = snd_rme96_get_attenuation_control,
  2093. .put = snd_rme96_put_attenuation_control
  2094. },
  2095. {
  2096. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2097. .name = "DAC Playback Volume",
  2098. .info = snd_rme96_dac_volume_info,
  2099. .get = snd_rme96_dac_volume_get,
  2100. .put = snd_rme96_dac_volume_put
  2101. }
  2102. };
  2103. static int
  2104. snd_rme96_create_switches(struct snd_card *card,
  2105. struct rme96 *rme96)
  2106. {
  2107. int idx, err;
  2108. struct snd_kcontrol *kctl;
  2109. for (idx = 0; idx < 7; idx++) {
  2110. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme96_controls[idx], rme96))) < 0)
  2111. return err;
  2112. if (idx == 1) /* IEC958 (S/PDIF) Stream */
  2113. rme96->spdif_ctl = kctl;
  2114. }
  2115. if (RME96_HAS_ANALOG_OUT(rme96)) {
  2116. for (idx = 7; idx < 10; idx++)
  2117. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_rme96_controls[idx], rme96))) < 0)
  2118. return err;
  2119. }
  2120. return 0;
  2121. }
  2122. /*
  2123. * Card initialisation
  2124. */
  2125. #ifdef CONFIG_PM_SLEEP
  2126. static int rme96_suspend(struct device *dev)
  2127. {
  2128. struct snd_card *card = dev_get_drvdata(dev);
  2129. struct rme96 *rme96 = card->private_data;
  2130. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  2131. snd_pcm_suspend(rme96->playback_substream);
  2132. snd_pcm_suspend(rme96->capture_substream);
  2133. /* save capture & playback pointers */
  2134. rme96->playback_pointer = readl(rme96->iobase + RME96_IO_GET_PLAY_POS)
  2135. & RME96_RCR_AUDIO_ADDR_MASK;
  2136. rme96->capture_pointer = readl(rme96->iobase + RME96_IO_GET_REC_POS)
  2137. & RME96_RCR_AUDIO_ADDR_MASK;
  2138. /* save playback and capture buffers */
  2139. memcpy_fromio(rme96->playback_suspend_buffer,
  2140. rme96->iobase + RME96_IO_PLAY_BUFFER, RME96_BUFFER_SIZE);
  2141. memcpy_fromio(rme96->capture_suspend_buffer,
  2142. rme96->iobase + RME96_IO_REC_BUFFER, RME96_BUFFER_SIZE);
  2143. /* disable the DAC */
  2144. rme96->areg &= ~RME96_AR_DAC_EN;
  2145. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  2146. return 0;
  2147. }
  2148. static int rme96_resume(struct device *dev)
  2149. {
  2150. struct snd_card *card = dev_get_drvdata(dev);
  2151. struct rme96 *rme96 = card->private_data;
  2152. /* reset playback and record buffer pointers */
  2153. writel(0, rme96->iobase + RME96_IO_SET_PLAY_POS
  2154. + rme96->playback_pointer);
  2155. writel(0, rme96->iobase + RME96_IO_SET_REC_POS
  2156. + rme96->capture_pointer);
  2157. /* restore playback and capture buffers */
  2158. memcpy_toio(rme96->iobase + RME96_IO_PLAY_BUFFER,
  2159. rme96->playback_suspend_buffer, RME96_BUFFER_SIZE);
  2160. memcpy_toio(rme96->iobase + RME96_IO_REC_BUFFER,
  2161. rme96->capture_suspend_buffer, RME96_BUFFER_SIZE);
  2162. /* reset the ADC */
  2163. writel(rme96->areg | RME96_AR_PD2,
  2164. rme96->iobase + RME96_IO_ADDITIONAL_REG);
  2165. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  2166. /* reset and enable DAC, restore analog volume */
  2167. snd_rme96_reset_dac(rme96);
  2168. rme96->areg |= RME96_AR_DAC_EN;
  2169. writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
  2170. if (RME96_HAS_ANALOG_OUT(rme96)) {
  2171. usleep_range(3000, 10000);
  2172. snd_rme96_apply_dac_volume(rme96);
  2173. }
  2174. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  2175. return 0;
  2176. }
  2177. static SIMPLE_DEV_PM_OPS(rme96_pm, rme96_suspend, rme96_resume);
  2178. #define RME96_PM_OPS &rme96_pm
  2179. #else
  2180. #define RME96_PM_OPS NULL
  2181. #endif /* CONFIG_PM_SLEEP */
  2182. static void snd_rme96_card_free(struct snd_card *card)
  2183. {
  2184. snd_rme96_free(card->private_data);
  2185. }
  2186. static int
  2187. snd_rme96_probe(struct pci_dev *pci,
  2188. const struct pci_device_id *pci_id)
  2189. {
  2190. static int dev;
  2191. struct rme96 *rme96;
  2192. struct snd_card *card;
  2193. int err;
  2194. u8 val;
  2195. if (dev >= SNDRV_CARDS) {
  2196. return -ENODEV;
  2197. }
  2198. if (!enable[dev]) {
  2199. dev++;
  2200. return -ENOENT;
  2201. }
  2202. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  2203. sizeof(struct rme96), &card);
  2204. if (err < 0)
  2205. return err;
  2206. card->private_free = snd_rme96_card_free;
  2207. rme96 = card->private_data;
  2208. rme96->card = card;
  2209. rme96->pci = pci;
  2210. if ((err = snd_rme96_create(rme96)) < 0) {
  2211. snd_card_free(card);
  2212. return err;
  2213. }
  2214. #ifdef CONFIG_PM_SLEEP
  2215. rme96->playback_suspend_buffer = vmalloc(RME96_BUFFER_SIZE);
  2216. if (!rme96->playback_suspend_buffer) {
  2217. dev_err(card->dev,
  2218. "Failed to allocate playback suspend buffer!\n");
  2219. snd_card_free(card);
  2220. return -ENOMEM;
  2221. }
  2222. rme96->capture_suspend_buffer = vmalloc(RME96_BUFFER_SIZE);
  2223. if (!rme96->capture_suspend_buffer) {
  2224. dev_err(card->dev,
  2225. "Failed to allocate capture suspend buffer!\n");
  2226. snd_card_free(card);
  2227. return -ENOMEM;
  2228. }
  2229. #endif
  2230. strcpy(card->driver, "Digi96");
  2231. switch (rme96->pci->device) {
  2232. case PCI_DEVICE_ID_RME_DIGI96:
  2233. strcpy(card->shortname, "RME Digi96");
  2234. break;
  2235. case PCI_DEVICE_ID_RME_DIGI96_8:
  2236. strcpy(card->shortname, "RME Digi96/8");
  2237. break;
  2238. case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
  2239. strcpy(card->shortname, "RME Digi96/8 PRO");
  2240. break;
  2241. case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
  2242. pci_read_config_byte(rme96->pci, 8, &val);
  2243. if (val < 5) {
  2244. strcpy(card->shortname, "RME Digi96/8 PAD");
  2245. } else {
  2246. strcpy(card->shortname, "RME Digi96/8 PST");
  2247. }
  2248. break;
  2249. }
  2250. sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
  2251. rme96->port, rme96->irq);
  2252. if ((err = snd_card_register(card)) < 0) {
  2253. snd_card_free(card);
  2254. return err;
  2255. }
  2256. pci_set_drvdata(pci, card);
  2257. dev++;
  2258. return 0;
  2259. }
  2260. static void snd_rme96_remove(struct pci_dev *pci)
  2261. {
  2262. snd_card_free(pci_get_drvdata(pci));
  2263. }
  2264. static struct pci_driver rme96_driver = {
  2265. .name = KBUILD_MODNAME,
  2266. .id_table = snd_rme96_ids,
  2267. .probe = snd_rme96_probe,
  2268. .remove = snd_rme96_remove,
  2269. .driver = {
  2270. .pm = RME96_PM_OPS,
  2271. },
  2272. };
  2273. module_pci_driver(rme96_driver);