cmipci.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181
  1. /*
  2. * Driver for C-Media CMI8338 and 8738 PCI soundcards.
  3. * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /* Does not work. Warning may block system in capture mode */
  20. /* #define USE_VAR48KRATE */
  21. #include <sound/driver.h>
  22. #include <asm/io.h>
  23. #include <linux/delay.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/init.h>
  26. #include <linux/pci.h>
  27. #include <linux/slab.h>
  28. #include <linux/gameport.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/mutex.h>
  31. #include <sound/core.h>
  32. #include <sound/info.h>
  33. #include <sound/control.h>
  34. #include <sound/pcm.h>
  35. #include <sound/rawmidi.h>
  36. #include <sound/mpu401.h>
  37. #include <sound/opl3.h>
  38. #include <sound/sb.h>
  39. #include <sound/asoundef.h>
  40. #include <sound/initval.h>
  41. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  42. MODULE_DESCRIPTION("C-Media CMI8x38 PCI");
  43. MODULE_LICENSE("GPL");
  44. MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8738},"
  45. "{C-Media,CMI8738B},"
  46. "{C-Media,CMI8338A},"
  47. "{C-Media,CMI8338B}}");
  48. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  49. #define SUPPORT_JOYSTICK 1
  50. #endif
  51. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  52. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  53. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable switches */
  54. static long mpu_port[SNDRV_CARDS];
  55. static long fm_port[SNDRV_CARDS];
  56. static int soft_ac3[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
  57. #ifdef SUPPORT_JOYSTICK
  58. static int joystick_port[SNDRV_CARDS];
  59. #endif
  60. module_param_array(index, int, NULL, 0444);
  61. MODULE_PARM_DESC(index, "Index value for C-Media PCI soundcard.");
  62. module_param_array(id, charp, NULL, 0444);
  63. MODULE_PARM_DESC(id, "ID string for C-Media PCI soundcard.");
  64. module_param_array(enable, bool, NULL, 0444);
  65. MODULE_PARM_DESC(enable, "Enable C-Media PCI soundcard.");
  66. module_param_array(mpu_port, long, NULL, 0444);
  67. MODULE_PARM_DESC(mpu_port, "MPU-401 port.");
  68. module_param_array(fm_port, long, NULL, 0444);
  69. MODULE_PARM_DESC(fm_port, "FM port.");
  70. module_param_array(soft_ac3, bool, NULL, 0444);
  71. MODULE_PARM_DESC(soft_ac3, "Sofware-conversion of raw SPDIF packets (model 033 only).");
  72. #ifdef SUPPORT_JOYSTICK
  73. module_param_array(joystick_port, int, NULL, 0444);
  74. MODULE_PARM_DESC(joystick_port, "Joystick port address.");
  75. #endif
  76. /*
  77. * CM8x38 registers definition
  78. */
  79. #define CM_REG_FUNCTRL0 0x00
  80. #define CM_RST_CH1 0x00080000
  81. #define CM_RST_CH0 0x00040000
  82. #define CM_CHEN1 0x00020000 /* ch1: enable */
  83. #define CM_CHEN0 0x00010000 /* ch0: enable */
  84. #define CM_PAUSE1 0x00000008 /* ch1: pause */
  85. #define CM_PAUSE0 0x00000004 /* ch0: pause */
  86. #define CM_CHADC1 0x00000002 /* ch1, 0:playback, 1:record */
  87. #define CM_CHADC0 0x00000001 /* ch0, 0:playback, 1:record */
  88. #define CM_REG_FUNCTRL1 0x04
  89. #define CM_ASFC_MASK 0x0000E000 /* ADC sampling frequency */
  90. #define CM_ASFC_SHIFT 13
  91. #define CM_DSFC_MASK 0x00001C00 /* DAC sampling frequency */
  92. #define CM_DSFC_SHIFT 10
  93. #define CM_SPDF_1 0x00000200 /* SPDIF IN/OUT at channel B */
  94. #define CM_SPDF_0 0x00000100 /* SPDIF OUT only channel A */
  95. #define CM_SPDFLOOP 0x00000080 /* ext. SPDIIF/OUT -> IN loopback */
  96. #define CM_SPDO2DAC 0x00000040 /* SPDIF/OUT can be heard from internal DAC */
  97. #define CM_INTRM 0x00000020 /* master control block (MCB) interrupt enabled */
  98. #define CM_BREQ 0x00000010 /* bus master enabled */
  99. #define CM_VOICE_EN 0x00000008 /* legacy voice (SB16,FM) */
  100. #define CM_UART_EN 0x00000004 /* UART */
  101. #define CM_JYSTK_EN 0x00000002 /* joy stick */
  102. #define CM_REG_CHFORMAT 0x08
  103. #define CM_CHB3D5C 0x80000000 /* 5,6 channels */
  104. #define CM_CHB3D 0x20000000 /* 4 channels */
  105. #define CM_CHIP_MASK1 0x1f000000
  106. #define CM_CHIP_037 0x01000000
  107. #define CM_SPDIF_SELECT1 0x00080000 /* for model <= 037 ? */
  108. #define CM_AC3EN1 0x00100000 /* enable AC3: model 037 */
  109. #define CM_SPD24SEL 0x00020000 /* 24bit spdif: model 037 */
  110. /* #define CM_SPDIF_INVERSE 0x00010000 */ /* ??? */
  111. #define CM_ADCBITLEN_MASK 0x0000C000
  112. #define CM_ADCBITLEN_16 0x00000000
  113. #define CM_ADCBITLEN_15 0x00004000
  114. #define CM_ADCBITLEN_14 0x00008000
  115. #define CM_ADCBITLEN_13 0x0000C000
  116. #define CM_ADCDACLEN_MASK 0x00003000
  117. #define CM_ADCDACLEN_060 0x00000000
  118. #define CM_ADCDACLEN_066 0x00001000
  119. #define CM_ADCDACLEN_130 0x00002000
  120. #define CM_ADCDACLEN_280 0x00003000
  121. #define CM_CH1_SRATE_176K 0x00000800
  122. #define CM_CH1_SRATE_88K 0x00000400
  123. #define CM_CH0_SRATE_176K 0x00000200
  124. #define CM_CH0_SRATE_88K 0x00000100
  125. #define CM_SPDIF_INVERSE2 0x00000080 /* model 055? */
  126. #define CM_CH1FMT_MASK 0x0000000C
  127. #define CM_CH1FMT_SHIFT 2
  128. #define CM_CH0FMT_MASK 0x00000003
  129. #define CM_CH0FMT_SHIFT 0
  130. #define CM_REG_INT_HLDCLR 0x0C
  131. #define CM_CHIP_MASK2 0xff000000
  132. #define CM_CHIP_039 0x04000000
  133. #define CM_CHIP_039_6CH 0x01000000
  134. #define CM_CHIP_055 0x08000000
  135. #define CM_CHIP_8768 0x20000000
  136. #define CM_TDMA_INT_EN 0x00040000
  137. #define CM_CH1_INT_EN 0x00020000
  138. #define CM_CH0_INT_EN 0x00010000
  139. #define CM_INT_HOLD 0x00000002
  140. #define CM_INT_CLEAR 0x00000001
  141. #define CM_REG_INT_STATUS 0x10
  142. #define CM_INTR 0x80000000
  143. #define CM_VCO 0x08000000 /* Voice Control? CMI8738 */
  144. #define CM_MCBINT 0x04000000 /* Master Control Block abort cond.? */
  145. #define CM_UARTINT 0x00010000
  146. #define CM_LTDMAINT 0x00008000
  147. #define CM_HTDMAINT 0x00004000
  148. #define CM_XDO46 0x00000080 /* Modell 033? Direct programming EEPROM (read data register) */
  149. #define CM_LHBTOG 0x00000040 /* High/Low status from DMA ctrl register */
  150. #define CM_LEG_HDMA 0x00000020 /* Legacy is in High DMA channel */
  151. #define CM_LEG_STEREO 0x00000010 /* Legacy is in Stereo mode */
  152. #define CM_CH1BUSY 0x00000008
  153. #define CM_CH0BUSY 0x00000004
  154. #define CM_CHINT1 0x00000002
  155. #define CM_CHINT0 0x00000001
  156. #define CM_REG_LEGACY_CTRL 0x14
  157. #define CM_NXCHG 0x80000000 /* h/w multi channels? */
  158. #define CM_VMPU_MASK 0x60000000 /* MPU401 i/o port address */
  159. #define CM_VMPU_330 0x00000000
  160. #define CM_VMPU_320 0x20000000
  161. #define CM_VMPU_310 0x40000000
  162. #define CM_VMPU_300 0x60000000
  163. #define CM_VSBSEL_MASK 0x0C000000 /* SB16 base address */
  164. #define CM_VSBSEL_220 0x00000000
  165. #define CM_VSBSEL_240 0x04000000
  166. #define CM_VSBSEL_260 0x08000000
  167. #define CM_VSBSEL_280 0x0C000000
  168. #define CM_FMSEL_MASK 0x03000000 /* FM OPL3 base address */
  169. #define CM_FMSEL_388 0x00000000
  170. #define CM_FMSEL_3C8 0x01000000
  171. #define CM_FMSEL_3E0 0x02000000
  172. #define CM_FMSEL_3E8 0x03000000
  173. #define CM_ENSPDOUT 0x00800000 /* enable XPDIF/OUT to I/O interface */
  174. #define CM_SPDCOPYRHT 0x00400000 /* set copyright spdif in/out */
  175. #define CM_DAC2SPDO 0x00200000 /* enable wave+fm_midi -> SPDIF/OUT */
  176. #define CM_SETRETRY 0x00010000 /* 0: legacy i/o wait (default), 1: legacy i/o bus retry */
  177. #define CM_CHB3D6C 0x00008000 /* 5.1 channels support */
  178. #define CM_LINE_AS_BASS 0x00006000 /* use line-in as bass */
  179. #define CM_REG_MISC_CTRL 0x18
  180. #define CM_PWD 0x80000000
  181. #define CM_RESET 0x40000000
  182. #define CM_SFIL_MASK 0x30000000
  183. #define CM_TXVX 0x08000000
  184. #define CM_N4SPK3D 0x04000000 /* 4ch output */
  185. #define CM_SPDO5V 0x02000000 /* 5V spdif output (1 = 0.5v (coax)) */
  186. #define CM_SPDIF48K 0x01000000 /* write */
  187. #define CM_SPATUS48K 0x01000000 /* read */
  188. #define CM_ENDBDAC 0x00800000 /* enable dual dac */
  189. #define CM_XCHGDAC 0x00400000 /* 0: front=ch0, 1: front=ch1 */
  190. #define CM_SPD32SEL 0x00200000 /* 0: 16bit SPDIF, 1: 32bit */
  191. #define CM_SPDFLOOPI 0x00100000 /* int. SPDIF-IN -> int. OUT */
  192. #define CM_FM_EN 0x00080000 /* enalbe FM */
  193. #define CM_AC3EN2 0x00040000 /* enable AC3: model 039 */
  194. #define CM_VIDWPDSB 0x00010000
  195. #define CM_SPDF_AC97 0x00008000 /* 0: SPDIF/OUT 44.1K, 1: 48K */
  196. #define CM_MASK_EN 0x00004000
  197. #define CM_VIDWPPRT 0x00002000
  198. #define CM_SFILENB 0x00001000
  199. #define CM_MMODE_MASK 0x00000E00
  200. #define CM_SPDIF_SELECT2 0x00000100 /* for model > 039 ? */
  201. #define CM_ENCENTER 0x00000080
  202. #define CM_FLINKON 0x00000040
  203. #define CM_FLINKOFF 0x00000020
  204. #define CM_MIDSMP 0x00000010
  205. #define CM_UPDDMA_MASK 0x0000000C
  206. #define CM_TWAIT_MASK 0x00000003
  207. /* byte */
  208. #define CM_REG_MIXER0 0x20
  209. #define CM_REG_SB16_DATA 0x22
  210. #define CM_REG_SB16_ADDR 0x23
  211. #define CM_REFFREQ_XIN (315*1000*1000)/22 /* 14.31818 Mhz reference clock frequency pin XIN */
  212. #define CM_ADCMULT_XIN 512 /* Guessed (487 best for 44.1kHz, not for 88/176kHz) */
  213. #define CM_TOLERANCE_RATE 0.001 /* Tolerance sample rate pitch (1000ppm) */
  214. #define CM_MAXIMUM_RATE 80000000 /* Note more than 80MHz */
  215. #define CM_REG_MIXER1 0x24
  216. #define CM_FMMUTE 0x80 /* mute FM */
  217. #define CM_FMMUTE_SHIFT 7
  218. #define CM_WSMUTE 0x40 /* mute PCM */
  219. #define CM_WSMUTE_SHIFT 6
  220. #define CM_SPK4 0x20 /* lin-in -> rear line out */
  221. #define CM_SPK4_SHIFT 5
  222. #define CM_REAR2FRONT 0x10 /* exchange rear/front */
  223. #define CM_REAR2FRONT_SHIFT 4
  224. #define CM_WAVEINL 0x08 /* digital wave rec. left chan */
  225. #define CM_WAVEINL_SHIFT 3
  226. #define CM_WAVEINR 0x04 /* digical wave rec. right */
  227. #define CM_WAVEINR_SHIFT 2
  228. #define CM_X3DEN 0x02 /* 3D surround enable */
  229. #define CM_X3DEN_SHIFT 1
  230. #define CM_CDPLAY 0x01 /* enable SPDIF/IN PCM -> DAC */
  231. #define CM_CDPLAY_SHIFT 0
  232. #define CM_REG_MIXER2 0x25
  233. #define CM_RAUXREN 0x80 /* AUX right capture */
  234. #define CM_RAUXREN_SHIFT 7
  235. #define CM_RAUXLEN 0x40 /* AUX left capture */
  236. #define CM_RAUXLEN_SHIFT 6
  237. #define CM_VAUXRM 0x20 /* AUX right mute */
  238. #define CM_VAUXRM_SHIFT 5
  239. #define CM_VAUXLM 0x10 /* AUX left mute */
  240. #define CM_VAUXLM_SHIFT 4
  241. #define CM_VADMIC_MASK 0x0e /* mic gain level (0-3) << 1 */
  242. #define CM_VADMIC_SHIFT 1
  243. #define CM_MICGAINZ 0x01 /* mic boost */
  244. #define CM_MICGAINZ_SHIFT 0
  245. #define CM_REG_MIXER3 0x24
  246. #define CM_REG_AUX_VOL 0x26
  247. #define CM_VAUXL_MASK 0xf0
  248. #define CM_VAUXR_MASK 0x0f
  249. #define CM_REG_MISC 0x27
  250. #define CM_XGPO1 0x20
  251. // #define CM_XGPBIO 0x04
  252. #define CM_MIC_CENTER_LFE 0x04 /* mic as center/lfe out? (model 039 or later?) */
  253. #define CM_SPDIF_INVERSE 0x04 /* spdif input phase inverse (model 037) */
  254. #define CM_SPDVALID 0x02 /* spdif input valid check */
  255. #define CM_DMAUTO 0x01
  256. #define CM_REG_AC97 0x28 /* hmmm.. do we have ac97 link? */
  257. /*
  258. * For CMI-8338 (0x28 - 0x2b) .. is this valid for CMI-8738
  259. * or identical with AC97 codec?
  260. */
  261. #define CM_REG_EXTERN_CODEC CM_REG_AC97
  262. /*
  263. * MPU401 pci port index address 0x40 - 0x4f (CMI-8738 spec ver. 0.6)
  264. */
  265. #define CM_REG_MPU_PCI 0x40
  266. /*
  267. * FM pci port index address 0x50 - 0x5f (CMI-8738 spec ver. 0.6)
  268. */
  269. #define CM_REG_FM_PCI 0x50
  270. /*
  271. * access from SB-mixer port
  272. */
  273. #define CM_REG_EXTENT_IND 0xf0
  274. #define CM_VPHONE_MASK 0xe0 /* Phone volume control (0-3) << 5 */
  275. #define CM_VPHONE_SHIFT 5
  276. #define CM_VPHOM 0x10 /* Phone mute control */
  277. #define CM_VSPKM 0x08 /* Speaker mute control, default high */
  278. #define CM_RLOOPREN 0x04 /* Rec. R-channel enable */
  279. #define CM_RLOOPLEN 0x02 /* Rec. L-channel enable */
  280. #define CM_VADMIC3 0x01 /* Mic record boost */
  281. /*
  282. * CMI-8338 spec ver 0.5 (this is not valid for CMI-8738):
  283. * the 8 registers 0xf8 - 0xff are used for programming m/n counter by the PLL
  284. * unit (readonly?).
  285. */
  286. #define CM_REG_PLL 0xf8
  287. /*
  288. * extended registers
  289. */
  290. #define CM_REG_CH0_FRAME1 0x80 /* base address */
  291. #define CM_REG_CH0_FRAME2 0x84
  292. #define CM_REG_CH1_FRAME1 0x88 /* 0-15: count of samples at bus master; buffer size */
  293. #define CM_REG_CH1_FRAME2 0x8C /* 16-31: count of samples at codec; fragment size */
  294. #define CM_REG_EXT_MISC 0x90
  295. #define CM_REG_MISC_CTRL_8768 0x92 /* reg. name the same as 0x18 */
  296. #define CM_CHB3D8C 0x20 /* 7.1 channels support */
  297. #define CM_SPD32FMT 0x10 /* SPDIF/IN 32k */
  298. #define CM_ADC2SPDIF 0x08 /* ADC output to SPDIF/OUT */
  299. #define CM_SHAREADC 0x04 /* DAC in ADC as Center/LFE */
  300. #define CM_REALTCMP 0x02 /* monitor the CMPL/CMPR of ADC */
  301. #define CM_INVLRCK 0x01 /* invert ZVPORT's LRCK */
  302. /*
  303. * size of i/o region
  304. */
  305. #define CM_EXTENT_CODEC 0x100
  306. #define CM_EXTENT_MIDI 0x2
  307. #define CM_EXTENT_SYNTH 0x4
  308. /*
  309. * channels for playback / capture
  310. */
  311. #define CM_CH_PLAY 0
  312. #define CM_CH_CAPT 1
  313. /*
  314. * flags to check device open/close
  315. */
  316. #define CM_OPEN_NONE 0
  317. #define CM_OPEN_CH_MASK 0x01
  318. #define CM_OPEN_DAC 0x10
  319. #define CM_OPEN_ADC 0x20
  320. #define CM_OPEN_SPDIF 0x40
  321. #define CM_OPEN_MCHAN 0x80
  322. #define CM_OPEN_PLAYBACK (CM_CH_PLAY | CM_OPEN_DAC)
  323. #define CM_OPEN_PLAYBACK2 (CM_CH_CAPT | CM_OPEN_DAC)
  324. #define CM_OPEN_PLAYBACK_MULTI (CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_MCHAN)
  325. #define CM_OPEN_CAPTURE (CM_CH_CAPT | CM_OPEN_ADC)
  326. #define CM_OPEN_SPDIF_PLAYBACK (CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_SPDIF)
  327. #define CM_OPEN_SPDIF_CAPTURE (CM_CH_CAPT | CM_OPEN_ADC | CM_OPEN_SPDIF)
  328. #if CM_CH_PLAY == 1
  329. #define CM_PLAYBACK_SRATE_176K CM_CH1_SRATE_176K
  330. #define CM_PLAYBACK_SPDF CM_SPDF_1
  331. #define CM_CAPTURE_SPDF CM_SPDF_0
  332. #else
  333. #define CM_PLAYBACK_SRATE_176K CM_CH0_SRATE_176K
  334. #define CM_PLAYBACK_SPDF CM_SPDF_0
  335. #define CM_CAPTURE_SPDF CM_SPDF_1
  336. #endif
  337. /*
  338. * driver data
  339. */
  340. struct cmipci_pcm {
  341. struct snd_pcm_substream *substream;
  342. int running; /* dac/adc running? */
  343. unsigned int dma_size; /* in frames */
  344. unsigned int period_size; /* in frames */
  345. unsigned int offset; /* physical address of the buffer */
  346. unsigned int fmt; /* format bits */
  347. int ch; /* channel (0/1) */
  348. unsigned int is_dac; /* is dac? */
  349. int bytes_per_frame;
  350. int shift;
  351. };
  352. /* mixer elements toggled/resumed during ac3 playback */
  353. struct cmipci_mixer_auto_switches {
  354. const char *name; /* switch to toggle */
  355. int toggle_on; /* value to change when ac3 mode */
  356. };
  357. static const struct cmipci_mixer_auto_switches cm_saved_mixer[] = {
  358. {"PCM Playback Switch", 0},
  359. {"IEC958 Output Switch", 1},
  360. {"IEC958 Mix Analog", 0},
  361. // {"IEC958 Out To DAC", 1}, // no longer used
  362. {"IEC958 Loop", 0},
  363. };
  364. #define CM_SAVED_MIXERS ARRAY_SIZE(cm_saved_mixer)
  365. struct cmipci {
  366. struct snd_card *card;
  367. struct pci_dev *pci;
  368. unsigned int device; /* device ID */
  369. int irq;
  370. unsigned long iobase;
  371. unsigned int ctrl; /* FUNCTRL0 current value */
  372. struct snd_pcm *pcm; /* DAC/ADC PCM */
  373. struct snd_pcm *pcm2; /* 2nd DAC */
  374. struct snd_pcm *pcm_spdif; /* SPDIF */
  375. int chip_version;
  376. int max_channels;
  377. unsigned int has_dual_dac: 1;
  378. unsigned int can_ac3_sw: 1;
  379. unsigned int can_ac3_hw: 1;
  380. unsigned int can_multi_ch: 1;
  381. unsigned int do_soft_ac3: 1;
  382. unsigned int spdif_playback_avail: 1; /* spdif ready? */
  383. unsigned int spdif_playback_enabled: 1; /* spdif switch enabled? */
  384. int spdif_counter; /* for software AC3 */
  385. unsigned int dig_status;
  386. unsigned int dig_pcm_status;
  387. struct snd_pcm_hardware *hw_info[3]; /* for playbacks */
  388. int opened[2]; /* open mode */
  389. struct mutex open_mutex;
  390. unsigned int mixer_insensitive: 1;
  391. struct snd_kcontrol *mixer_res_ctl[CM_SAVED_MIXERS];
  392. int mixer_res_status[CM_SAVED_MIXERS];
  393. struct cmipci_pcm channel[2]; /* ch0 - DAC, ch1 - ADC or 2nd DAC */
  394. /* external MIDI */
  395. struct snd_rawmidi *rmidi;
  396. #ifdef SUPPORT_JOYSTICK
  397. struct gameport *gameport;
  398. #endif
  399. spinlock_t reg_lock;
  400. #ifdef CONFIG_PM
  401. unsigned int saved_regs[0x20];
  402. unsigned char saved_mixers[0x20];
  403. #endif
  404. };
  405. /* read/write operations for dword register */
  406. static inline void snd_cmipci_write(struct cmipci *cm, unsigned int cmd, unsigned int data)
  407. {
  408. outl(data, cm->iobase + cmd);
  409. }
  410. static inline unsigned int snd_cmipci_read(struct cmipci *cm, unsigned int cmd)
  411. {
  412. return inl(cm->iobase + cmd);
  413. }
  414. /* read/write operations for word register */
  415. static inline void snd_cmipci_write_w(struct cmipci *cm, unsigned int cmd, unsigned short data)
  416. {
  417. outw(data, cm->iobase + cmd);
  418. }
  419. static inline unsigned short snd_cmipci_read_w(struct cmipci *cm, unsigned int cmd)
  420. {
  421. return inw(cm->iobase + cmd);
  422. }
  423. /* read/write operations for byte register */
  424. static inline void snd_cmipci_write_b(struct cmipci *cm, unsigned int cmd, unsigned char data)
  425. {
  426. outb(data, cm->iobase + cmd);
  427. }
  428. static inline unsigned char snd_cmipci_read_b(struct cmipci *cm, unsigned int cmd)
  429. {
  430. return inb(cm->iobase + cmd);
  431. }
  432. /* bit operations for dword register */
  433. static int snd_cmipci_set_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
  434. {
  435. unsigned int val, oval;
  436. val = oval = inl(cm->iobase + cmd);
  437. val |= flag;
  438. if (val == oval)
  439. return 0;
  440. outl(val, cm->iobase + cmd);
  441. return 1;
  442. }
  443. static int snd_cmipci_clear_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
  444. {
  445. unsigned int val, oval;
  446. val = oval = inl(cm->iobase + cmd);
  447. val &= ~flag;
  448. if (val == oval)
  449. return 0;
  450. outl(val, cm->iobase + cmd);
  451. return 1;
  452. }
  453. /* bit operations for byte register */
  454. static int snd_cmipci_set_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
  455. {
  456. unsigned char val, oval;
  457. val = oval = inb(cm->iobase + cmd);
  458. val |= flag;
  459. if (val == oval)
  460. return 0;
  461. outb(val, cm->iobase + cmd);
  462. return 1;
  463. }
  464. static int snd_cmipci_clear_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
  465. {
  466. unsigned char val, oval;
  467. val = oval = inb(cm->iobase + cmd);
  468. val &= ~flag;
  469. if (val == oval)
  470. return 0;
  471. outb(val, cm->iobase + cmd);
  472. return 1;
  473. }
  474. /*
  475. * PCM interface
  476. */
  477. /*
  478. * calculate frequency
  479. */
  480. static unsigned int rates[] = { 5512, 11025, 22050, 44100, 8000, 16000, 32000, 48000 };
  481. static unsigned int snd_cmipci_rate_freq(unsigned int rate)
  482. {
  483. unsigned int i;
  484. for (i = 0; i < ARRAY_SIZE(rates); i++) {
  485. if (rates[i] == rate)
  486. return i;
  487. }
  488. snd_BUG();
  489. return 0;
  490. }
  491. #ifdef USE_VAR48KRATE
  492. /*
  493. * Determine PLL values for frequency setup, maybe the CMI8338 (CMI8738???)
  494. * does it this way .. maybe not. Never get any information from C-Media about
  495. * that <werner@suse.de>.
  496. */
  497. static int snd_cmipci_pll_rmn(unsigned int rate, unsigned int adcmult, int *r, int *m, int *n)
  498. {
  499. unsigned int delta, tolerance;
  500. int xm, xn, xr;
  501. for (*r = 0; rate < CM_MAXIMUM_RATE/adcmult; *r += (1<<5))
  502. rate <<= 1;
  503. *n = -1;
  504. if (*r > 0xff)
  505. goto out;
  506. tolerance = rate*CM_TOLERANCE_RATE;
  507. for (xn = (1+2); xn < (0x1f+2); xn++) {
  508. for (xm = (1+2); xm < (0xff+2); xm++) {
  509. xr = ((CM_REFFREQ_XIN/adcmult) * xm) / xn;
  510. if (xr < rate)
  511. delta = rate - xr;
  512. else
  513. delta = xr - rate;
  514. /*
  515. * If we found one, remember this,
  516. * and try to find a closer one
  517. */
  518. if (delta < tolerance) {
  519. tolerance = delta;
  520. *m = xm - 2;
  521. *n = xn - 2;
  522. }
  523. }
  524. }
  525. out:
  526. return (*n > -1);
  527. }
  528. /*
  529. * Program pll register bits, I assume that the 8 registers 0xf8 upto 0xff
  530. * are mapped onto the 8 ADC/DAC sampling frequency which can be choosen
  531. * at the register CM_REG_FUNCTRL1 (0x04).
  532. * Problem: other ways are also possible (any information about that?)
  533. */
  534. static void snd_cmipci_set_pll(struct cmipci *cm, unsigned int rate, unsigned int slot)
  535. {
  536. unsigned int reg = CM_REG_PLL + slot;
  537. /*
  538. * Guess that this programs at reg. 0x04 the pos 15:13/12:10
  539. * for DSFC/ASFC (000 upto 111).
  540. */
  541. /* FIXME: Init (Do we've to set an other register first before programming?) */
  542. /* FIXME: Is this correct? Or shouldn't the m/n/r values be used for that? */
  543. snd_cmipci_write_b(cm, reg, rate>>8);
  544. snd_cmipci_write_b(cm, reg, rate&0xff);
  545. /* FIXME: Setup (Do we've to set an other register first to enable this?) */
  546. }
  547. #endif /* USE_VAR48KRATE */
  548. static int snd_cmipci_hw_params(struct snd_pcm_substream *substream,
  549. struct snd_pcm_hw_params *hw_params)
  550. {
  551. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  552. }
  553. static int snd_cmipci_playback2_hw_params(struct snd_pcm_substream *substream,
  554. struct snd_pcm_hw_params *hw_params)
  555. {
  556. struct cmipci *cm = snd_pcm_substream_chip(substream);
  557. if (params_channels(hw_params) > 2) {
  558. mutex_lock(&cm->open_mutex);
  559. if (cm->opened[CM_CH_PLAY]) {
  560. mutex_unlock(&cm->open_mutex);
  561. return -EBUSY;
  562. }
  563. /* reserve the channel A */
  564. cm->opened[CM_CH_PLAY] = CM_OPEN_PLAYBACK_MULTI;
  565. mutex_unlock(&cm->open_mutex);
  566. }
  567. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  568. }
  569. static void snd_cmipci_ch_reset(struct cmipci *cm, int ch)
  570. {
  571. int reset = CM_RST_CH0 << (cm->channel[ch].ch);
  572. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
  573. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
  574. udelay(10);
  575. }
  576. static int snd_cmipci_hw_free(struct snd_pcm_substream *substream)
  577. {
  578. return snd_pcm_lib_free_pages(substream);
  579. }
  580. /*
  581. */
  582. static unsigned int hw_channels[] = {1, 2, 4, 5, 6, 8};
  583. static struct snd_pcm_hw_constraint_list hw_constraints_channels_4 = {
  584. .count = 3,
  585. .list = hw_channels,
  586. .mask = 0,
  587. };
  588. static struct snd_pcm_hw_constraint_list hw_constraints_channels_6 = {
  589. .count = 5,
  590. .list = hw_channels,
  591. .mask = 0,
  592. };
  593. static struct snd_pcm_hw_constraint_list hw_constraints_channels_8 = {
  594. .count = 6,
  595. .list = hw_channels,
  596. .mask = 0,
  597. };
  598. static int set_dac_channels(struct cmipci *cm, struct cmipci_pcm *rec, int channels)
  599. {
  600. if (channels > 2) {
  601. if (! cm->can_multi_ch)
  602. return -EINVAL;
  603. if (rec->fmt != 0x03) /* stereo 16bit only */
  604. return -EINVAL;
  605. spin_lock_irq(&cm->reg_lock);
  606. snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
  607. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  608. if (channels > 4) {
  609. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
  610. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
  611. } else {
  612. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
  613. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
  614. }
  615. if (channels >= 6) {
  616. snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
  617. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENCENTER);
  618. } else {
  619. snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
  620. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENCENTER);
  621. }
  622. if (cm->chip_version == 68) {
  623. if (channels == 8) {
  624. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL_8768, CM_CHB3D8C);
  625. } else {
  626. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL_8768, CM_CHB3D8C);
  627. }
  628. }
  629. spin_unlock_irq(&cm->reg_lock);
  630. } else {
  631. if (cm->can_multi_ch) {
  632. spin_lock_irq(&cm->reg_lock);
  633. snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
  634. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
  635. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
  636. snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
  637. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENCENTER);
  638. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  639. spin_unlock_irq(&cm->reg_lock);
  640. }
  641. }
  642. return 0;
  643. }
  644. /*
  645. * prepare playback/capture channel
  646. * channel to be used must have been set in rec->ch.
  647. */
  648. static int snd_cmipci_pcm_prepare(struct cmipci *cm, struct cmipci_pcm *rec,
  649. struct snd_pcm_substream *substream)
  650. {
  651. unsigned int reg, freq, val;
  652. struct snd_pcm_runtime *runtime = substream->runtime;
  653. rec->fmt = 0;
  654. rec->shift = 0;
  655. if (snd_pcm_format_width(runtime->format) >= 16) {
  656. rec->fmt |= 0x02;
  657. if (snd_pcm_format_width(runtime->format) > 16)
  658. rec->shift++; /* 24/32bit */
  659. }
  660. if (runtime->channels > 1)
  661. rec->fmt |= 0x01;
  662. if (rec->is_dac && set_dac_channels(cm, rec, runtime->channels) < 0) {
  663. snd_printd("cannot set dac channels\n");
  664. return -EINVAL;
  665. }
  666. rec->offset = runtime->dma_addr;
  667. /* buffer and period sizes in frame */
  668. rec->dma_size = runtime->buffer_size << rec->shift;
  669. rec->period_size = runtime->period_size << rec->shift;
  670. if (runtime->channels > 2) {
  671. /* multi-channels */
  672. rec->dma_size = (rec->dma_size * runtime->channels) / 2;
  673. rec->period_size = (rec->period_size * runtime->channels) / 2;
  674. }
  675. spin_lock_irq(&cm->reg_lock);
  676. /* set buffer address */
  677. reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
  678. snd_cmipci_write(cm, reg, rec->offset);
  679. /* program sample counts */
  680. reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
  681. snd_cmipci_write_w(cm, reg, rec->dma_size - 1);
  682. snd_cmipci_write_w(cm, reg + 2, rec->period_size - 1);
  683. /* set adc/dac flag */
  684. val = rec->ch ? CM_CHADC1 : CM_CHADC0;
  685. if (rec->is_dac)
  686. cm->ctrl &= ~val;
  687. else
  688. cm->ctrl |= val;
  689. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  690. //snd_printd("cmipci: functrl0 = %08x\n", cm->ctrl);
  691. /* set sample rate */
  692. freq = snd_cmipci_rate_freq(runtime->rate);
  693. val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
  694. if (rec->ch) {
  695. val &= ~CM_ASFC_MASK;
  696. val |= (freq << CM_ASFC_SHIFT) & CM_ASFC_MASK;
  697. } else {
  698. val &= ~CM_DSFC_MASK;
  699. val |= (freq << CM_DSFC_SHIFT) & CM_DSFC_MASK;
  700. }
  701. snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
  702. //snd_printd("cmipci: functrl1 = %08x\n", val);
  703. /* set format */
  704. val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
  705. if (rec->ch) {
  706. val &= ~CM_CH1FMT_MASK;
  707. val |= rec->fmt << CM_CH1FMT_SHIFT;
  708. } else {
  709. val &= ~CM_CH0FMT_MASK;
  710. val |= rec->fmt << CM_CH0FMT_SHIFT;
  711. }
  712. snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
  713. //snd_printd("cmipci: chformat = %08x\n", val);
  714. rec->running = 0;
  715. spin_unlock_irq(&cm->reg_lock);
  716. return 0;
  717. }
  718. /*
  719. * PCM trigger/stop
  720. */
  721. static int snd_cmipci_pcm_trigger(struct cmipci *cm, struct cmipci_pcm *rec,
  722. struct snd_pcm_substream *substream, int cmd)
  723. {
  724. unsigned int inthld, chen, reset, pause;
  725. int result = 0;
  726. inthld = CM_CH0_INT_EN << rec->ch;
  727. chen = CM_CHEN0 << rec->ch;
  728. reset = CM_RST_CH0 << rec->ch;
  729. pause = CM_PAUSE0 << rec->ch;
  730. spin_lock(&cm->reg_lock);
  731. switch (cmd) {
  732. case SNDRV_PCM_TRIGGER_START:
  733. rec->running = 1;
  734. /* set interrupt */
  735. snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, inthld);
  736. cm->ctrl |= chen;
  737. /* enable channel */
  738. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  739. //snd_printd("cmipci: functrl0 = %08x\n", cm->ctrl);
  740. break;
  741. case SNDRV_PCM_TRIGGER_STOP:
  742. rec->running = 0;
  743. /* disable interrupt */
  744. snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, inthld);
  745. /* reset */
  746. cm->ctrl &= ~chen;
  747. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
  748. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
  749. break;
  750. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  751. case SNDRV_PCM_TRIGGER_SUSPEND:
  752. cm->ctrl |= pause;
  753. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  754. break;
  755. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  756. case SNDRV_PCM_TRIGGER_RESUME:
  757. cm->ctrl &= ~pause;
  758. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  759. break;
  760. default:
  761. result = -EINVAL;
  762. break;
  763. }
  764. spin_unlock(&cm->reg_lock);
  765. return result;
  766. }
  767. /*
  768. * return the current pointer
  769. */
  770. static snd_pcm_uframes_t snd_cmipci_pcm_pointer(struct cmipci *cm, struct cmipci_pcm *rec,
  771. struct snd_pcm_substream *substream)
  772. {
  773. size_t ptr;
  774. unsigned int reg;
  775. if (!rec->running)
  776. return 0;
  777. #if 1 // this seems better..
  778. reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
  779. ptr = rec->dma_size - (snd_cmipci_read_w(cm, reg) + 1);
  780. ptr >>= rec->shift;
  781. #else
  782. reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
  783. ptr = snd_cmipci_read(cm, reg) - rec->offset;
  784. ptr = bytes_to_frames(substream->runtime, ptr);
  785. #endif
  786. if (substream->runtime->channels > 2)
  787. ptr = (ptr * 2) / substream->runtime->channels;
  788. return ptr;
  789. }
  790. /*
  791. * playback
  792. */
  793. static int snd_cmipci_playback_trigger(struct snd_pcm_substream *substream,
  794. int cmd)
  795. {
  796. struct cmipci *cm = snd_pcm_substream_chip(substream);
  797. return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_PLAY], substream, cmd);
  798. }
  799. static snd_pcm_uframes_t snd_cmipci_playback_pointer(struct snd_pcm_substream *substream)
  800. {
  801. struct cmipci *cm = snd_pcm_substream_chip(substream);
  802. return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_PLAY], substream);
  803. }
  804. /*
  805. * capture
  806. */
  807. static int snd_cmipci_capture_trigger(struct snd_pcm_substream *substream,
  808. int cmd)
  809. {
  810. struct cmipci *cm = snd_pcm_substream_chip(substream);
  811. return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_CAPT], substream, cmd);
  812. }
  813. static snd_pcm_uframes_t snd_cmipci_capture_pointer(struct snd_pcm_substream *substream)
  814. {
  815. struct cmipci *cm = snd_pcm_substream_chip(substream);
  816. return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_CAPT], substream);
  817. }
  818. /*
  819. * hw preparation for spdif
  820. */
  821. static int snd_cmipci_spdif_default_info(struct snd_kcontrol *kcontrol,
  822. struct snd_ctl_elem_info *uinfo)
  823. {
  824. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  825. uinfo->count = 1;
  826. return 0;
  827. }
  828. static int snd_cmipci_spdif_default_get(struct snd_kcontrol *kcontrol,
  829. struct snd_ctl_elem_value *ucontrol)
  830. {
  831. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  832. int i;
  833. spin_lock_irq(&chip->reg_lock);
  834. for (i = 0; i < 4; i++)
  835. ucontrol->value.iec958.status[i] = (chip->dig_status >> (i * 8)) & 0xff;
  836. spin_unlock_irq(&chip->reg_lock);
  837. return 0;
  838. }
  839. static int snd_cmipci_spdif_default_put(struct snd_kcontrol *kcontrol,
  840. struct snd_ctl_elem_value *ucontrol)
  841. {
  842. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  843. int i, change;
  844. unsigned int val;
  845. val = 0;
  846. spin_lock_irq(&chip->reg_lock);
  847. for (i = 0; i < 4; i++)
  848. val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  849. change = val != chip->dig_status;
  850. chip->dig_status = val;
  851. spin_unlock_irq(&chip->reg_lock);
  852. return change;
  853. }
  854. static struct snd_kcontrol_new snd_cmipci_spdif_default __devinitdata =
  855. {
  856. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  857. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  858. .info = snd_cmipci_spdif_default_info,
  859. .get = snd_cmipci_spdif_default_get,
  860. .put = snd_cmipci_spdif_default_put
  861. };
  862. static int snd_cmipci_spdif_mask_info(struct snd_kcontrol *kcontrol,
  863. struct snd_ctl_elem_info *uinfo)
  864. {
  865. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  866. uinfo->count = 1;
  867. return 0;
  868. }
  869. static int snd_cmipci_spdif_mask_get(struct snd_kcontrol *kcontrol,
  870. struct snd_ctl_elem_value *ucontrol)
  871. {
  872. ucontrol->value.iec958.status[0] = 0xff;
  873. ucontrol->value.iec958.status[1] = 0xff;
  874. ucontrol->value.iec958.status[2] = 0xff;
  875. ucontrol->value.iec958.status[3] = 0xff;
  876. return 0;
  877. }
  878. static struct snd_kcontrol_new snd_cmipci_spdif_mask __devinitdata =
  879. {
  880. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  881. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  882. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  883. .info = snd_cmipci_spdif_mask_info,
  884. .get = snd_cmipci_spdif_mask_get,
  885. };
  886. static int snd_cmipci_spdif_stream_info(struct snd_kcontrol *kcontrol,
  887. struct snd_ctl_elem_info *uinfo)
  888. {
  889. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  890. uinfo->count = 1;
  891. return 0;
  892. }
  893. static int snd_cmipci_spdif_stream_get(struct snd_kcontrol *kcontrol,
  894. struct snd_ctl_elem_value *ucontrol)
  895. {
  896. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  897. int i;
  898. spin_lock_irq(&chip->reg_lock);
  899. for (i = 0; i < 4; i++)
  900. ucontrol->value.iec958.status[i] = (chip->dig_pcm_status >> (i * 8)) & 0xff;
  901. spin_unlock_irq(&chip->reg_lock);
  902. return 0;
  903. }
  904. static int snd_cmipci_spdif_stream_put(struct snd_kcontrol *kcontrol,
  905. struct snd_ctl_elem_value *ucontrol)
  906. {
  907. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  908. int i, change;
  909. unsigned int val;
  910. val = 0;
  911. spin_lock_irq(&chip->reg_lock);
  912. for (i = 0; i < 4; i++)
  913. val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  914. change = val != chip->dig_pcm_status;
  915. chip->dig_pcm_status = val;
  916. spin_unlock_irq(&chip->reg_lock);
  917. return change;
  918. }
  919. static struct snd_kcontrol_new snd_cmipci_spdif_stream __devinitdata =
  920. {
  921. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  922. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  923. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  924. .info = snd_cmipci_spdif_stream_info,
  925. .get = snd_cmipci_spdif_stream_get,
  926. .put = snd_cmipci_spdif_stream_put
  927. };
  928. /*
  929. */
  930. /* save mixer setting and mute for AC3 playback */
  931. static int save_mixer_state(struct cmipci *cm)
  932. {
  933. if (! cm->mixer_insensitive) {
  934. struct snd_ctl_elem_value *val;
  935. unsigned int i;
  936. val = kmalloc(sizeof(*val), GFP_ATOMIC);
  937. if (!val)
  938. return -ENOMEM;
  939. for (i = 0; i < CM_SAVED_MIXERS; i++) {
  940. struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
  941. if (ctl) {
  942. int event;
  943. memset(val, 0, sizeof(*val));
  944. ctl->get(ctl, val);
  945. cm->mixer_res_status[i] = val->value.integer.value[0];
  946. val->value.integer.value[0] = cm_saved_mixer[i].toggle_on;
  947. event = SNDRV_CTL_EVENT_MASK_INFO;
  948. if (cm->mixer_res_status[i] != val->value.integer.value[0]) {
  949. ctl->put(ctl, val); /* toggle */
  950. event |= SNDRV_CTL_EVENT_MASK_VALUE;
  951. }
  952. ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  953. snd_ctl_notify(cm->card, event, &ctl->id);
  954. }
  955. }
  956. kfree(val);
  957. cm->mixer_insensitive = 1;
  958. }
  959. return 0;
  960. }
  961. /* restore the previously saved mixer status */
  962. static void restore_mixer_state(struct cmipci *cm)
  963. {
  964. if (cm->mixer_insensitive) {
  965. struct snd_ctl_elem_value *val;
  966. unsigned int i;
  967. val = kmalloc(sizeof(*val), GFP_KERNEL);
  968. if (!val)
  969. return;
  970. cm->mixer_insensitive = 0; /* at first clear this;
  971. otherwise the changes will be ignored */
  972. for (i = 0; i < CM_SAVED_MIXERS; i++) {
  973. struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
  974. if (ctl) {
  975. int event;
  976. memset(val, 0, sizeof(*val));
  977. ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  978. ctl->get(ctl, val);
  979. event = SNDRV_CTL_EVENT_MASK_INFO;
  980. if (val->value.integer.value[0] != cm->mixer_res_status[i]) {
  981. val->value.integer.value[0] = cm->mixer_res_status[i];
  982. ctl->put(ctl, val);
  983. event |= SNDRV_CTL_EVENT_MASK_VALUE;
  984. }
  985. snd_ctl_notify(cm->card, event, &ctl->id);
  986. }
  987. }
  988. kfree(val);
  989. }
  990. }
  991. /* spinlock held! */
  992. static void setup_ac3(struct cmipci *cm, struct snd_pcm_substream *subs, int do_ac3, int rate)
  993. {
  994. if (do_ac3) {
  995. /* AC3EN for 037 */
  996. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
  997. /* AC3EN for 039 */
  998. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
  999. if (cm->can_ac3_hw) {
  1000. /* SPD24SEL for 037, 0x02 */
  1001. /* SPD24SEL for 039, 0x20, but cannot be set */
  1002. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1003. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1004. } else { /* can_ac3_sw */
  1005. /* SPD32SEL for 037 & 039, 0x20 */
  1006. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1007. /* set 176K sample rate to fix 033 HW bug */
  1008. if (cm->chip_version == 33) {
  1009. if (rate >= 48000) {
  1010. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  1011. } else {
  1012. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  1013. }
  1014. }
  1015. }
  1016. } else {
  1017. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
  1018. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
  1019. if (cm->can_ac3_hw) {
  1020. /* chip model >= 37 */
  1021. if (snd_pcm_format_width(subs->runtime->format) > 16) {
  1022. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1023. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1024. } else {
  1025. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1026. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1027. }
  1028. } else {
  1029. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1030. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1031. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  1032. }
  1033. }
  1034. }
  1035. static int setup_spdif_playback(struct cmipci *cm, struct snd_pcm_substream *subs, int up, int do_ac3)
  1036. {
  1037. int rate, err;
  1038. rate = subs->runtime->rate;
  1039. if (up && do_ac3)
  1040. if ((err = save_mixer_state(cm)) < 0)
  1041. return err;
  1042. spin_lock_irq(&cm->reg_lock);
  1043. cm->spdif_playback_avail = up;
  1044. if (up) {
  1045. /* they are controlled via "IEC958 Output Switch" */
  1046. /* snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
  1047. /* snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
  1048. if (cm->spdif_playback_enabled)
  1049. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  1050. setup_ac3(cm, subs, do_ac3, rate);
  1051. if (rate == 48000)
  1052. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
  1053. else
  1054. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
  1055. } else {
  1056. /* they are controlled via "IEC958 Output Switch" */
  1057. /* snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
  1058. /* snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
  1059. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  1060. setup_ac3(cm, subs, 0, 0);
  1061. }
  1062. spin_unlock_irq(&cm->reg_lock);
  1063. return 0;
  1064. }
  1065. /*
  1066. * preparation
  1067. */
  1068. /* playback - enable spdif only on the certain condition */
  1069. static int snd_cmipci_playback_prepare(struct snd_pcm_substream *substream)
  1070. {
  1071. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1072. int rate = substream->runtime->rate;
  1073. int err, do_spdif, do_ac3 = 0;
  1074. do_spdif = ((rate == 44100 || rate == 48000) &&
  1075. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE &&
  1076. substream->runtime->channels == 2);
  1077. if (do_spdif && cm->can_ac3_hw)
  1078. do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
  1079. if ((err = setup_spdif_playback(cm, substream, do_spdif, do_ac3)) < 0)
  1080. return err;
  1081. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
  1082. }
  1083. /* playback (via device #2) - enable spdif always */
  1084. static int snd_cmipci_playback_spdif_prepare(struct snd_pcm_substream *substream)
  1085. {
  1086. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1087. int err, do_ac3;
  1088. if (cm->can_ac3_hw)
  1089. do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
  1090. else
  1091. do_ac3 = 1; /* doesn't matter */
  1092. if ((err = setup_spdif_playback(cm, substream, 1, do_ac3)) < 0)
  1093. return err;
  1094. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
  1095. }
  1096. static int snd_cmipci_playback_hw_free(struct snd_pcm_substream *substream)
  1097. {
  1098. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1099. setup_spdif_playback(cm, substream, 0, 0);
  1100. restore_mixer_state(cm);
  1101. return snd_cmipci_hw_free(substream);
  1102. }
  1103. /* capture */
  1104. static int snd_cmipci_capture_prepare(struct snd_pcm_substream *substream)
  1105. {
  1106. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1107. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
  1108. }
  1109. /* capture with spdif (via device #2) */
  1110. static int snd_cmipci_capture_spdif_prepare(struct snd_pcm_substream *substream)
  1111. {
  1112. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1113. spin_lock_irq(&cm->reg_lock);
  1114. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
  1115. spin_unlock_irq(&cm->reg_lock);
  1116. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
  1117. }
  1118. static int snd_cmipci_capture_spdif_hw_free(struct snd_pcm_substream *subs)
  1119. {
  1120. struct cmipci *cm = snd_pcm_substream_chip(subs);
  1121. spin_lock_irq(&cm->reg_lock);
  1122. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
  1123. spin_unlock_irq(&cm->reg_lock);
  1124. return snd_cmipci_hw_free(subs);
  1125. }
  1126. /*
  1127. * interrupt handler
  1128. */
  1129. static irqreturn_t snd_cmipci_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1130. {
  1131. struct cmipci *cm = dev_id;
  1132. unsigned int status, mask = 0;
  1133. /* fastpath out, to ease interrupt sharing */
  1134. status = snd_cmipci_read(cm, CM_REG_INT_STATUS);
  1135. if (!(status & CM_INTR))
  1136. return IRQ_NONE;
  1137. /* acknowledge interrupt */
  1138. spin_lock(&cm->reg_lock);
  1139. if (status & CM_CHINT0)
  1140. mask |= CM_CH0_INT_EN;
  1141. if (status & CM_CHINT1)
  1142. mask |= CM_CH1_INT_EN;
  1143. snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, mask);
  1144. snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, mask);
  1145. spin_unlock(&cm->reg_lock);
  1146. if (cm->rmidi && (status & CM_UARTINT))
  1147. snd_mpu401_uart_interrupt(irq, cm->rmidi->private_data, regs);
  1148. if (cm->pcm) {
  1149. if ((status & CM_CHINT0) && cm->channel[0].running)
  1150. snd_pcm_period_elapsed(cm->channel[0].substream);
  1151. if ((status & CM_CHINT1) && cm->channel[1].running)
  1152. snd_pcm_period_elapsed(cm->channel[1].substream);
  1153. }
  1154. return IRQ_HANDLED;
  1155. }
  1156. /*
  1157. * h/w infos
  1158. */
  1159. /* playback on channel A */
  1160. static struct snd_pcm_hardware snd_cmipci_playback =
  1161. {
  1162. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1163. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1164. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1165. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  1166. .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  1167. .rate_min = 5512,
  1168. .rate_max = 48000,
  1169. .channels_min = 1,
  1170. .channels_max = 2,
  1171. .buffer_bytes_max = (128*1024),
  1172. .period_bytes_min = 64,
  1173. .period_bytes_max = (128*1024),
  1174. .periods_min = 2,
  1175. .periods_max = 1024,
  1176. .fifo_size = 0,
  1177. };
  1178. /* capture on channel B */
  1179. static struct snd_pcm_hardware snd_cmipci_capture =
  1180. {
  1181. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1182. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1183. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1184. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  1185. .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  1186. .rate_min = 5512,
  1187. .rate_max = 48000,
  1188. .channels_min = 1,
  1189. .channels_max = 2,
  1190. .buffer_bytes_max = (128*1024),
  1191. .period_bytes_min = 64,
  1192. .period_bytes_max = (128*1024),
  1193. .periods_min = 2,
  1194. .periods_max = 1024,
  1195. .fifo_size = 0,
  1196. };
  1197. /* playback on channel B - stereo 16bit only? */
  1198. static struct snd_pcm_hardware snd_cmipci_playback2 =
  1199. {
  1200. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1201. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1202. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1203. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1204. .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  1205. .rate_min = 5512,
  1206. .rate_max = 48000,
  1207. .channels_min = 2,
  1208. .channels_max = 2,
  1209. .buffer_bytes_max = (128*1024),
  1210. .period_bytes_min = 64,
  1211. .period_bytes_max = (128*1024),
  1212. .periods_min = 2,
  1213. .periods_max = 1024,
  1214. .fifo_size = 0,
  1215. };
  1216. /* spdif playback on channel A */
  1217. static struct snd_pcm_hardware snd_cmipci_playback_spdif =
  1218. {
  1219. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1220. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1221. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1222. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1223. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  1224. .rate_min = 44100,
  1225. .rate_max = 48000,
  1226. .channels_min = 2,
  1227. .channels_max = 2,
  1228. .buffer_bytes_max = (128*1024),
  1229. .period_bytes_min = 64,
  1230. .period_bytes_max = (128*1024),
  1231. .periods_min = 2,
  1232. .periods_max = 1024,
  1233. .fifo_size = 0,
  1234. };
  1235. /* spdif playback on channel A (32bit, IEC958 subframes) */
  1236. static struct snd_pcm_hardware snd_cmipci_playback_iec958_subframe =
  1237. {
  1238. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1239. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1240. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1241. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  1242. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  1243. .rate_min = 44100,
  1244. .rate_max = 48000,
  1245. .channels_min = 2,
  1246. .channels_max = 2,
  1247. .buffer_bytes_max = (128*1024),
  1248. .period_bytes_min = 64,
  1249. .period_bytes_max = (128*1024),
  1250. .periods_min = 2,
  1251. .periods_max = 1024,
  1252. .fifo_size = 0,
  1253. };
  1254. /* spdif capture on channel B */
  1255. static struct snd_pcm_hardware snd_cmipci_capture_spdif =
  1256. {
  1257. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1258. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1259. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1260. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1261. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  1262. .rate_min = 44100,
  1263. .rate_max = 48000,
  1264. .channels_min = 2,
  1265. .channels_max = 2,
  1266. .buffer_bytes_max = (128*1024),
  1267. .period_bytes_min = 64,
  1268. .period_bytes_max = (128*1024),
  1269. .periods_min = 2,
  1270. .periods_max = 1024,
  1271. .fifo_size = 0,
  1272. };
  1273. /*
  1274. * check device open/close
  1275. */
  1276. static int open_device_check(struct cmipci *cm, int mode, struct snd_pcm_substream *subs)
  1277. {
  1278. int ch = mode & CM_OPEN_CH_MASK;
  1279. /* FIXME: a file should wait until the device becomes free
  1280. * when it's opened on blocking mode. however, since the current
  1281. * pcm framework doesn't pass file pointer before actually opened,
  1282. * we can't know whether blocking mode or not in open callback..
  1283. */
  1284. mutex_lock(&cm->open_mutex);
  1285. if (cm->opened[ch]) {
  1286. mutex_unlock(&cm->open_mutex);
  1287. return -EBUSY;
  1288. }
  1289. cm->opened[ch] = mode;
  1290. cm->channel[ch].substream = subs;
  1291. if (! (mode & CM_OPEN_DAC)) {
  1292. /* disable dual DAC mode */
  1293. cm->channel[ch].is_dac = 0;
  1294. spin_lock_irq(&cm->reg_lock);
  1295. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
  1296. spin_unlock_irq(&cm->reg_lock);
  1297. }
  1298. mutex_unlock(&cm->open_mutex);
  1299. return 0;
  1300. }
  1301. static void close_device_check(struct cmipci *cm, int mode)
  1302. {
  1303. int ch = mode & CM_OPEN_CH_MASK;
  1304. mutex_lock(&cm->open_mutex);
  1305. if (cm->opened[ch] == mode) {
  1306. if (cm->channel[ch].substream) {
  1307. snd_cmipci_ch_reset(cm, ch);
  1308. cm->channel[ch].running = 0;
  1309. cm->channel[ch].substream = NULL;
  1310. }
  1311. cm->opened[ch] = 0;
  1312. if (! cm->channel[ch].is_dac) {
  1313. /* enable dual DAC mode again */
  1314. cm->channel[ch].is_dac = 1;
  1315. spin_lock_irq(&cm->reg_lock);
  1316. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
  1317. spin_unlock_irq(&cm->reg_lock);
  1318. }
  1319. }
  1320. mutex_unlock(&cm->open_mutex);
  1321. }
  1322. /*
  1323. */
  1324. static int snd_cmipci_playback_open(struct snd_pcm_substream *substream)
  1325. {
  1326. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1327. struct snd_pcm_runtime *runtime = substream->runtime;
  1328. int err;
  1329. if ((err = open_device_check(cm, CM_OPEN_PLAYBACK, substream)) < 0)
  1330. return err;
  1331. runtime->hw = snd_cmipci_playback;
  1332. runtime->hw.channels_max = cm->max_channels;
  1333. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
  1334. cm->dig_pcm_status = cm->dig_status;
  1335. return 0;
  1336. }
  1337. static int snd_cmipci_capture_open(struct snd_pcm_substream *substream)
  1338. {
  1339. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1340. struct snd_pcm_runtime *runtime = substream->runtime;
  1341. int err;
  1342. if ((err = open_device_check(cm, CM_OPEN_CAPTURE, substream)) < 0)
  1343. return err;
  1344. runtime->hw = snd_cmipci_capture;
  1345. if (cm->chip_version == 68) { // 8768 only supports 44k/48k recording
  1346. runtime->hw.rate_min = 41000;
  1347. runtime->hw.rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000;
  1348. }
  1349. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
  1350. return 0;
  1351. }
  1352. static int snd_cmipci_playback2_open(struct snd_pcm_substream *substream)
  1353. {
  1354. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1355. struct snd_pcm_runtime *runtime = substream->runtime;
  1356. int err;
  1357. if ((err = open_device_check(cm, CM_OPEN_PLAYBACK2, substream)) < 0) /* use channel B */
  1358. return err;
  1359. runtime->hw = snd_cmipci_playback2;
  1360. mutex_lock(&cm->open_mutex);
  1361. if (! cm->opened[CM_CH_PLAY]) {
  1362. if (cm->can_multi_ch) {
  1363. runtime->hw.channels_max = cm->max_channels;
  1364. if (cm->max_channels == 4)
  1365. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_4);
  1366. else if (cm->max_channels == 6)
  1367. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_6);
  1368. else if (cm->max_channels == 8)
  1369. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_8);
  1370. }
  1371. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
  1372. }
  1373. mutex_unlock(&cm->open_mutex);
  1374. return 0;
  1375. }
  1376. static int snd_cmipci_playback_spdif_open(struct snd_pcm_substream *substream)
  1377. {
  1378. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1379. struct snd_pcm_runtime *runtime = substream->runtime;
  1380. int err;
  1381. if ((err = open_device_check(cm, CM_OPEN_SPDIF_PLAYBACK, substream)) < 0) /* use channel A */
  1382. return err;
  1383. if (cm->can_ac3_hw) {
  1384. runtime->hw = snd_cmipci_playback_spdif;
  1385. if (cm->chip_version >= 37)
  1386. runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1387. } else {
  1388. runtime->hw = snd_cmipci_playback_iec958_subframe;
  1389. }
  1390. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
  1391. cm->dig_pcm_status = cm->dig_status;
  1392. return 0;
  1393. }
  1394. static int snd_cmipci_capture_spdif_open(struct snd_pcm_substream *substream)
  1395. {
  1396. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1397. struct snd_pcm_runtime *runtime = substream->runtime;
  1398. int err;
  1399. if ((err = open_device_check(cm, CM_OPEN_SPDIF_CAPTURE, substream)) < 0) /* use channel B */
  1400. return err;
  1401. runtime->hw = snd_cmipci_capture_spdif;
  1402. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
  1403. return 0;
  1404. }
  1405. /*
  1406. */
  1407. static int snd_cmipci_playback_close(struct snd_pcm_substream *substream)
  1408. {
  1409. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1410. close_device_check(cm, CM_OPEN_PLAYBACK);
  1411. return 0;
  1412. }
  1413. static int snd_cmipci_capture_close(struct snd_pcm_substream *substream)
  1414. {
  1415. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1416. close_device_check(cm, CM_OPEN_CAPTURE);
  1417. return 0;
  1418. }
  1419. static int snd_cmipci_playback2_close(struct snd_pcm_substream *substream)
  1420. {
  1421. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1422. close_device_check(cm, CM_OPEN_PLAYBACK2);
  1423. close_device_check(cm, CM_OPEN_PLAYBACK_MULTI);
  1424. return 0;
  1425. }
  1426. static int snd_cmipci_playback_spdif_close(struct snd_pcm_substream *substream)
  1427. {
  1428. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1429. close_device_check(cm, CM_OPEN_SPDIF_PLAYBACK);
  1430. return 0;
  1431. }
  1432. static int snd_cmipci_capture_spdif_close(struct snd_pcm_substream *substream)
  1433. {
  1434. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1435. close_device_check(cm, CM_OPEN_SPDIF_CAPTURE);
  1436. return 0;
  1437. }
  1438. /*
  1439. */
  1440. static struct snd_pcm_ops snd_cmipci_playback_ops = {
  1441. .open = snd_cmipci_playback_open,
  1442. .close = snd_cmipci_playback_close,
  1443. .ioctl = snd_pcm_lib_ioctl,
  1444. .hw_params = snd_cmipci_hw_params,
  1445. .hw_free = snd_cmipci_playback_hw_free,
  1446. .prepare = snd_cmipci_playback_prepare,
  1447. .trigger = snd_cmipci_playback_trigger,
  1448. .pointer = snd_cmipci_playback_pointer,
  1449. };
  1450. static struct snd_pcm_ops snd_cmipci_capture_ops = {
  1451. .open = snd_cmipci_capture_open,
  1452. .close = snd_cmipci_capture_close,
  1453. .ioctl = snd_pcm_lib_ioctl,
  1454. .hw_params = snd_cmipci_hw_params,
  1455. .hw_free = snd_cmipci_hw_free,
  1456. .prepare = snd_cmipci_capture_prepare,
  1457. .trigger = snd_cmipci_capture_trigger,
  1458. .pointer = snd_cmipci_capture_pointer,
  1459. };
  1460. static struct snd_pcm_ops snd_cmipci_playback2_ops = {
  1461. .open = snd_cmipci_playback2_open,
  1462. .close = snd_cmipci_playback2_close,
  1463. .ioctl = snd_pcm_lib_ioctl,
  1464. .hw_params = snd_cmipci_playback2_hw_params,
  1465. .hw_free = snd_cmipci_hw_free,
  1466. .prepare = snd_cmipci_capture_prepare, /* channel B */
  1467. .trigger = snd_cmipci_capture_trigger, /* channel B */
  1468. .pointer = snd_cmipci_capture_pointer, /* channel B */
  1469. };
  1470. static struct snd_pcm_ops snd_cmipci_playback_spdif_ops = {
  1471. .open = snd_cmipci_playback_spdif_open,
  1472. .close = snd_cmipci_playback_spdif_close,
  1473. .ioctl = snd_pcm_lib_ioctl,
  1474. .hw_params = snd_cmipci_hw_params,
  1475. .hw_free = snd_cmipci_playback_hw_free,
  1476. .prepare = snd_cmipci_playback_spdif_prepare, /* set up rate */
  1477. .trigger = snd_cmipci_playback_trigger,
  1478. .pointer = snd_cmipci_playback_pointer,
  1479. };
  1480. static struct snd_pcm_ops snd_cmipci_capture_spdif_ops = {
  1481. .open = snd_cmipci_capture_spdif_open,
  1482. .close = snd_cmipci_capture_spdif_close,
  1483. .ioctl = snd_pcm_lib_ioctl,
  1484. .hw_params = snd_cmipci_hw_params,
  1485. .hw_free = snd_cmipci_capture_spdif_hw_free,
  1486. .prepare = snd_cmipci_capture_spdif_prepare,
  1487. .trigger = snd_cmipci_capture_trigger,
  1488. .pointer = snd_cmipci_capture_pointer,
  1489. };
  1490. /*
  1491. */
  1492. static int __devinit snd_cmipci_pcm_new(struct cmipci *cm, int device)
  1493. {
  1494. struct snd_pcm *pcm;
  1495. int err;
  1496. err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
  1497. if (err < 0)
  1498. return err;
  1499. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_ops);
  1500. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_ops);
  1501. pcm->private_data = cm;
  1502. pcm->info_flags = 0;
  1503. strcpy(pcm->name, "C-Media PCI DAC/ADC");
  1504. cm->pcm = pcm;
  1505. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1506. snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  1507. return 0;
  1508. }
  1509. static int __devinit snd_cmipci_pcm2_new(struct cmipci *cm, int device)
  1510. {
  1511. struct snd_pcm *pcm;
  1512. int err;
  1513. err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 0, &pcm);
  1514. if (err < 0)
  1515. return err;
  1516. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback2_ops);
  1517. pcm->private_data = cm;
  1518. pcm->info_flags = 0;
  1519. strcpy(pcm->name, "C-Media PCI 2nd DAC");
  1520. cm->pcm2 = pcm;
  1521. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1522. snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  1523. return 0;
  1524. }
  1525. static int __devinit snd_cmipci_pcm_spdif_new(struct cmipci *cm, int device)
  1526. {
  1527. struct snd_pcm *pcm;
  1528. int err;
  1529. err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
  1530. if (err < 0)
  1531. return err;
  1532. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_spdif_ops);
  1533. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_spdif_ops);
  1534. pcm->private_data = cm;
  1535. pcm->info_flags = 0;
  1536. strcpy(pcm->name, "C-Media PCI IEC958");
  1537. cm->pcm_spdif = pcm;
  1538. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1539. snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  1540. return 0;
  1541. }
  1542. /*
  1543. * mixer interface:
  1544. * - CM8338/8738 has a compatible mixer interface with SB16, but
  1545. * lack of some elements like tone control, i/o gain and AGC.
  1546. * - Access to native registers:
  1547. * - A 3D switch
  1548. * - Output mute switches
  1549. */
  1550. static void snd_cmipci_mixer_write(struct cmipci *s, unsigned char idx, unsigned char data)
  1551. {
  1552. outb(idx, s->iobase + CM_REG_SB16_ADDR);
  1553. outb(data, s->iobase + CM_REG_SB16_DATA);
  1554. }
  1555. static unsigned char snd_cmipci_mixer_read(struct cmipci *s, unsigned char idx)
  1556. {
  1557. unsigned char v;
  1558. outb(idx, s->iobase + CM_REG_SB16_ADDR);
  1559. v = inb(s->iobase + CM_REG_SB16_DATA);
  1560. return v;
  1561. }
  1562. /*
  1563. * general mixer element
  1564. */
  1565. struct cmipci_sb_reg {
  1566. unsigned int left_reg, right_reg;
  1567. unsigned int left_shift, right_shift;
  1568. unsigned int mask;
  1569. unsigned int invert: 1;
  1570. unsigned int stereo: 1;
  1571. };
  1572. #define COMPOSE_SB_REG(lreg,rreg,lshift,rshift,mask,invert,stereo) \
  1573. ((lreg) | ((rreg) << 8) | (lshift << 16) | (rshift << 19) | (mask << 24) | (invert << 22) | (stereo << 23))
  1574. #define CMIPCI_DOUBLE(xname, left_reg, right_reg, left_shift, right_shift, mask, invert, stereo) \
  1575. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1576. .info = snd_cmipci_info_volume, \
  1577. .get = snd_cmipci_get_volume, .put = snd_cmipci_put_volume, \
  1578. .private_value = COMPOSE_SB_REG(left_reg, right_reg, left_shift, right_shift, mask, invert, stereo), \
  1579. }
  1580. #define CMIPCI_SB_VOL_STEREO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg+1, shift, shift, mask, 0, 1)
  1581. #define CMIPCI_SB_VOL_MONO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg, shift, shift, mask, 0, 0)
  1582. #define CMIPCI_SB_SW_STEREO(xname,lshift,rshift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, lshift, rshift, 1, 0, 1)
  1583. #define CMIPCI_SB_SW_MONO(xname,shift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, shift, shift, 1, 0, 0)
  1584. static void cmipci_sb_reg_decode(struct cmipci_sb_reg *r, unsigned long val)
  1585. {
  1586. r->left_reg = val & 0xff;
  1587. r->right_reg = (val >> 8) & 0xff;
  1588. r->left_shift = (val >> 16) & 0x07;
  1589. r->right_shift = (val >> 19) & 0x07;
  1590. r->invert = (val >> 22) & 1;
  1591. r->stereo = (val >> 23) & 1;
  1592. r->mask = (val >> 24) & 0xff;
  1593. }
  1594. static int snd_cmipci_info_volume(struct snd_kcontrol *kcontrol,
  1595. struct snd_ctl_elem_info *uinfo)
  1596. {
  1597. struct cmipci_sb_reg reg;
  1598. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1599. uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1600. uinfo->count = reg.stereo + 1;
  1601. uinfo->value.integer.min = 0;
  1602. uinfo->value.integer.max = reg.mask;
  1603. return 0;
  1604. }
  1605. static int snd_cmipci_get_volume(struct snd_kcontrol *kcontrol,
  1606. struct snd_ctl_elem_value *ucontrol)
  1607. {
  1608. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1609. struct cmipci_sb_reg reg;
  1610. int val;
  1611. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1612. spin_lock_irq(&cm->reg_lock);
  1613. val = (snd_cmipci_mixer_read(cm, reg.left_reg) >> reg.left_shift) & reg.mask;
  1614. if (reg.invert)
  1615. val = reg.mask - val;
  1616. ucontrol->value.integer.value[0] = val;
  1617. if (reg.stereo) {
  1618. val = (snd_cmipci_mixer_read(cm, reg.right_reg) >> reg.right_shift) & reg.mask;
  1619. if (reg.invert)
  1620. val = reg.mask - val;
  1621. ucontrol->value.integer.value[1] = val;
  1622. }
  1623. spin_unlock_irq(&cm->reg_lock);
  1624. return 0;
  1625. }
  1626. static int snd_cmipci_put_volume(struct snd_kcontrol *kcontrol,
  1627. struct snd_ctl_elem_value *ucontrol)
  1628. {
  1629. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1630. struct cmipci_sb_reg reg;
  1631. int change;
  1632. int left, right, oleft, oright;
  1633. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1634. left = ucontrol->value.integer.value[0] & reg.mask;
  1635. if (reg.invert)
  1636. left = reg.mask - left;
  1637. left <<= reg.left_shift;
  1638. if (reg.stereo) {
  1639. right = ucontrol->value.integer.value[1] & reg.mask;
  1640. if (reg.invert)
  1641. right = reg.mask - right;
  1642. right <<= reg.right_shift;
  1643. } else
  1644. right = 0;
  1645. spin_lock_irq(&cm->reg_lock);
  1646. oleft = snd_cmipci_mixer_read(cm, reg.left_reg);
  1647. left |= oleft & ~(reg.mask << reg.left_shift);
  1648. change = left != oleft;
  1649. if (reg.stereo) {
  1650. if (reg.left_reg != reg.right_reg) {
  1651. snd_cmipci_mixer_write(cm, reg.left_reg, left);
  1652. oright = snd_cmipci_mixer_read(cm, reg.right_reg);
  1653. } else
  1654. oright = left;
  1655. right |= oright & ~(reg.mask << reg.right_shift);
  1656. change |= right != oright;
  1657. snd_cmipci_mixer_write(cm, reg.right_reg, right);
  1658. } else
  1659. snd_cmipci_mixer_write(cm, reg.left_reg, left);
  1660. spin_unlock_irq(&cm->reg_lock);
  1661. return change;
  1662. }
  1663. /*
  1664. * input route (left,right) -> (left,right)
  1665. */
  1666. #define CMIPCI_SB_INPUT_SW(xname, left_shift, right_shift) \
  1667. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1668. .info = snd_cmipci_info_input_sw, \
  1669. .get = snd_cmipci_get_input_sw, .put = snd_cmipci_put_input_sw, \
  1670. .private_value = COMPOSE_SB_REG(SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, left_shift, right_shift, 1, 0, 1), \
  1671. }
  1672. static int snd_cmipci_info_input_sw(struct snd_kcontrol *kcontrol,
  1673. struct snd_ctl_elem_info *uinfo)
  1674. {
  1675. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1676. uinfo->count = 4;
  1677. uinfo->value.integer.min = 0;
  1678. uinfo->value.integer.max = 1;
  1679. return 0;
  1680. }
  1681. static int snd_cmipci_get_input_sw(struct snd_kcontrol *kcontrol,
  1682. struct snd_ctl_elem_value *ucontrol)
  1683. {
  1684. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1685. struct cmipci_sb_reg reg;
  1686. int val1, val2;
  1687. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1688. spin_lock_irq(&cm->reg_lock);
  1689. val1 = snd_cmipci_mixer_read(cm, reg.left_reg);
  1690. val2 = snd_cmipci_mixer_read(cm, reg.right_reg);
  1691. spin_unlock_irq(&cm->reg_lock);
  1692. ucontrol->value.integer.value[0] = (val1 >> reg.left_shift) & 1;
  1693. ucontrol->value.integer.value[1] = (val2 >> reg.left_shift) & 1;
  1694. ucontrol->value.integer.value[2] = (val1 >> reg.right_shift) & 1;
  1695. ucontrol->value.integer.value[3] = (val2 >> reg.right_shift) & 1;
  1696. return 0;
  1697. }
  1698. static int snd_cmipci_put_input_sw(struct snd_kcontrol *kcontrol,
  1699. struct snd_ctl_elem_value *ucontrol)
  1700. {
  1701. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1702. struct cmipci_sb_reg reg;
  1703. int change;
  1704. int val1, val2, oval1, oval2;
  1705. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1706. spin_lock_irq(&cm->reg_lock);
  1707. oval1 = snd_cmipci_mixer_read(cm, reg.left_reg);
  1708. oval2 = snd_cmipci_mixer_read(cm, reg.right_reg);
  1709. val1 = oval1 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
  1710. val2 = oval2 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
  1711. val1 |= (ucontrol->value.integer.value[0] & 1) << reg.left_shift;
  1712. val2 |= (ucontrol->value.integer.value[1] & 1) << reg.left_shift;
  1713. val1 |= (ucontrol->value.integer.value[2] & 1) << reg.right_shift;
  1714. val2 |= (ucontrol->value.integer.value[3] & 1) << reg.right_shift;
  1715. change = val1 != oval1 || val2 != oval2;
  1716. snd_cmipci_mixer_write(cm, reg.left_reg, val1);
  1717. snd_cmipci_mixer_write(cm, reg.right_reg, val2);
  1718. spin_unlock_irq(&cm->reg_lock);
  1719. return change;
  1720. }
  1721. /*
  1722. * native mixer switches/volumes
  1723. */
  1724. #define CMIPCI_MIXER_SW_STEREO(xname, reg, lshift, rshift, invert) \
  1725. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1726. .info = snd_cmipci_info_native_mixer, \
  1727. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1728. .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, 1, invert, 1), \
  1729. }
  1730. #define CMIPCI_MIXER_SW_MONO(xname, reg, shift, invert) \
  1731. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1732. .info = snd_cmipci_info_native_mixer, \
  1733. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1734. .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, 1, invert, 0), \
  1735. }
  1736. #define CMIPCI_MIXER_VOL_STEREO(xname, reg, lshift, rshift, mask) \
  1737. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1738. .info = snd_cmipci_info_native_mixer, \
  1739. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1740. .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, mask, 0, 1), \
  1741. }
  1742. #define CMIPCI_MIXER_VOL_MONO(xname, reg, shift, mask) \
  1743. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1744. .info = snd_cmipci_info_native_mixer, \
  1745. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1746. .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, mask, 0, 0), \
  1747. }
  1748. static int snd_cmipci_info_native_mixer(struct snd_kcontrol *kcontrol,
  1749. struct snd_ctl_elem_info *uinfo)
  1750. {
  1751. struct cmipci_sb_reg reg;
  1752. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1753. uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1754. uinfo->count = reg.stereo + 1;
  1755. uinfo->value.integer.min = 0;
  1756. uinfo->value.integer.max = reg.mask;
  1757. return 0;
  1758. }
  1759. static int snd_cmipci_get_native_mixer(struct snd_kcontrol *kcontrol,
  1760. struct snd_ctl_elem_value *ucontrol)
  1761. {
  1762. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1763. struct cmipci_sb_reg reg;
  1764. unsigned char oreg, val;
  1765. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1766. spin_lock_irq(&cm->reg_lock);
  1767. oreg = inb(cm->iobase + reg.left_reg);
  1768. val = (oreg >> reg.left_shift) & reg.mask;
  1769. if (reg.invert)
  1770. val = reg.mask - val;
  1771. ucontrol->value.integer.value[0] = val;
  1772. if (reg.stereo) {
  1773. val = (oreg >> reg.right_shift) & reg.mask;
  1774. if (reg.invert)
  1775. val = reg.mask - val;
  1776. ucontrol->value.integer.value[1] = val;
  1777. }
  1778. spin_unlock_irq(&cm->reg_lock);
  1779. return 0;
  1780. }
  1781. static int snd_cmipci_put_native_mixer(struct snd_kcontrol *kcontrol,
  1782. struct snd_ctl_elem_value *ucontrol)
  1783. {
  1784. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1785. struct cmipci_sb_reg reg;
  1786. unsigned char oreg, nreg, val;
  1787. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1788. spin_lock_irq(&cm->reg_lock);
  1789. oreg = inb(cm->iobase + reg.left_reg);
  1790. val = ucontrol->value.integer.value[0] & reg.mask;
  1791. if (reg.invert)
  1792. val = reg.mask - val;
  1793. nreg = oreg & ~(reg.mask << reg.left_shift);
  1794. nreg |= (val << reg.left_shift);
  1795. if (reg.stereo) {
  1796. val = ucontrol->value.integer.value[1] & reg.mask;
  1797. if (reg.invert)
  1798. val = reg.mask - val;
  1799. nreg &= ~(reg.mask << reg.right_shift);
  1800. nreg |= (val << reg.right_shift);
  1801. }
  1802. outb(nreg, cm->iobase + reg.left_reg);
  1803. spin_unlock_irq(&cm->reg_lock);
  1804. return (nreg != oreg);
  1805. }
  1806. /*
  1807. * special case - check mixer sensitivity
  1808. */
  1809. static int snd_cmipci_get_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
  1810. struct snd_ctl_elem_value *ucontrol)
  1811. {
  1812. //struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1813. return snd_cmipci_get_native_mixer(kcontrol, ucontrol);
  1814. }
  1815. static int snd_cmipci_put_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
  1816. struct snd_ctl_elem_value *ucontrol)
  1817. {
  1818. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1819. if (cm->mixer_insensitive) {
  1820. /* ignored */
  1821. return 0;
  1822. }
  1823. return snd_cmipci_put_native_mixer(kcontrol, ucontrol);
  1824. }
  1825. static struct snd_kcontrol_new snd_cmipci_mixers[] __devinitdata = {
  1826. CMIPCI_SB_VOL_STEREO("Master Playback Volume", SB_DSP4_MASTER_DEV, 3, 31),
  1827. CMIPCI_MIXER_SW_MONO("3D Control - Switch", CM_REG_MIXER1, CM_X3DEN_SHIFT, 0),
  1828. CMIPCI_SB_VOL_STEREO("PCM Playback Volume", SB_DSP4_PCM_DEV, 3, 31),
  1829. //CMIPCI_MIXER_SW_MONO("PCM Playback Switch", CM_REG_MIXER1, CM_WSMUTE_SHIFT, 1),
  1830. { /* switch with sensitivity */
  1831. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1832. .name = "PCM Playback Switch",
  1833. .info = snd_cmipci_info_native_mixer,
  1834. .get = snd_cmipci_get_native_mixer_sensitive,
  1835. .put = snd_cmipci_put_native_mixer_sensitive,
  1836. .private_value = COMPOSE_SB_REG(CM_REG_MIXER1, CM_REG_MIXER1, CM_WSMUTE_SHIFT, CM_WSMUTE_SHIFT, 1, 1, 0),
  1837. },
  1838. CMIPCI_MIXER_SW_STEREO("PCM Capture Switch", CM_REG_MIXER1, CM_WAVEINL_SHIFT, CM_WAVEINR_SHIFT, 0),
  1839. CMIPCI_SB_VOL_STEREO("Synth Playback Volume", SB_DSP4_SYNTH_DEV, 3, 31),
  1840. CMIPCI_MIXER_SW_MONO("Synth Playback Switch", CM_REG_MIXER1, CM_FMMUTE_SHIFT, 1),
  1841. CMIPCI_SB_INPUT_SW("Synth Capture Route", 6, 5),
  1842. CMIPCI_SB_VOL_STEREO("CD Playback Volume", SB_DSP4_CD_DEV, 3, 31),
  1843. CMIPCI_SB_SW_STEREO("CD Playback Switch", 2, 1),
  1844. CMIPCI_SB_INPUT_SW("CD Capture Route", 2, 1),
  1845. CMIPCI_SB_VOL_STEREO("Line Playback Volume", SB_DSP4_LINE_DEV, 3, 31),
  1846. CMIPCI_SB_SW_STEREO("Line Playback Switch", 4, 3),
  1847. CMIPCI_SB_INPUT_SW("Line Capture Route", 4, 3),
  1848. CMIPCI_SB_VOL_MONO("Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
  1849. CMIPCI_SB_SW_MONO("Mic Playback Switch", 0),
  1850. CMIPCI_DOUBLE("Mic Capture Switch", SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, 0, 0, 1, 0, 0),
  1851. CMIPCI_SB_VOL_MONO("PC Speaker Playback Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
  1852. CMIPCI_MIXER_VOL_STEREO("Aux Playback Volume", CM_REG_AUX_VOL, 4, 0, 15),
  1853. CMIPCI_MIXER_SW_STEREO("Aux Playback Switch", CM_REG_MIXER2, CM_VAUXLM_SHIFT, CM_VAUXRM_SHIFT, 0),
  1854. CMIPCI_MIXER_SW_STEREO("Aux Capture Switch", CM_REG_MIXER2, CM_RAUXLEN_SHIFT, CM_RAUXREN_SHIFT, 0),
  1855. CMIPCI_MIXER_SW_MONO("Mic Boost Playback Switch", CM_REG_MIXER2, CM_MICGAINZ_SHIFT, 1),
  1856. CMIPCI_MIXER_VOL_MONO("Mic Capture Volume", CM_REG_MIXER2, CM_VADMIC_SHIFT, 7),
  1857. CMIPCI_SB_VOL_MONO("Phone Playback Volume", CM_REG_EXTENT_IND, 5, 7),
  1858. CMIPCI_DOUBLE("Phone Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 4, 4, 1, 0, 0),
  1859. CMIPCI_DOUBLE("PC Speaker Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 3, 3, 1, 0, 0),
  1860. CMIPCI_DOUBLE("Mic Boost Capture Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 0, 0, 1, 0, 0),
  1861. };
  1862. /*
  1863. * other switches
  1864. */
  1865. struct cmipci_switch_args {
  1866. int reg; /* register index */
  1867. unsigned int mask; /* mask bits */
  1868. unsigned int mask_on; /* mask bits to turn on */
  1869. unsigned int is_byte: 1; /* byte access? */
  1870. unsigned int ac3_sensitive: 1; /* access forbidden during
  1871. * non-audio operation?
  1872. */
  1873. };
  1874. static int snd_cmipci_uswitch_info(struct snd_kcontrol *kcontrol,
  1875. struct snd_ctl_elem_info *uinfo)
  1876. {
  1877. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1878. uinfo->count = 1;
  1879. uinfo->value.integer.min = 0;
  1880. uinfo->value.integer.max = 1;
  1881. return 0;
  1882. }
  1883. static int _snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
  1884. struct snd_ctl_elem_value *ucontrol,
  1885. struct cmipci_switch_args *args)
  1886. {
  1887. unsigned int val;
  1888. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1889. spin_lock_irq(&cm->reg_lock);
  1890. if (args->ac3_sensitive && cm->mixer_insensitive) {
  1891. ucontrol->value.integer.value[0] = 0;
  1892. spin_unlock_irq(&cm->reg_lock);
  1893. return 0;
  1894. }
  1895. if (args->is_byte)
  1896. val = inb(cm->iobase + args->reg);
  1897. else
  1898. val = snd_cmipci_read(cm, args->reg);
  1899. ucontrol->value.integer.value[0] = ((val & args->mask) == args->mask_on) ? 1 : 0;
  1900. spin_unlock_irq(&cm->reg_lock);
  1901. return 0;
  1902. }
  1903. static int snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
  1904. struct snd_ctl_elem_value *ucontrol)
  1905. {
  1906. struct cmipci_switch_args *args;
  1907. args = (struct cmipci_switch_args *)kcontrol->private_value;
  1908. snd_assert(args != NULL, return -EINVAL);
  1909. return _snd_cmipci_uswitch_get(kcontrol, ucontrol, args);
  1910. }
  1911. static int _snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
  1912. struct snd_ctl_elem_value *ucontrol,
  1913. struct cmipci_switch_args *args)
  1914. {
  1915. unsigned int val;
  1916. int change;
  1917. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1918. spin_lock_irq(&cm->reg_lock);
  1919. if (args->ac3_sensitive && cm->mixer_insensitive) {
  1920. /* ignored */
  1921. spin_unlock_irq(&cm->reg_lock);
  1922. return 0;
  1923. }
  1924. if (args->is_byte)
  1925. val = inb(cm->iobase + args->reg);
  1926. else
  1927. val = snd_cmipci_read(cm, args->reg);
  1928. change = (val & args->mask) != (ucontrol->value.integer.value[0] ? args->mask : 0);
  1929. if (change) {
  1930. val &= ~args->mask;
  1931. if (ucontrol->value.integer.value[0])
  1932. val |= args->mask_on;
  1933. else
  1934. val |= (args->mask & ~args->mask_on);
  1935. if (args->is_byte)
  1936. outb((unsigned char)val, cm->iobase + args->reg);
  1937. else
  1938. snd_cmipci_write(cm, args->reg, val);
  1939. }
  1940. spin_unlock_irq(&cm->reg_lock);
  1941. return change;
  1942. }
  1943. static int snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
  1944. struct snd_ctl_elem_value *ucontrol)
  1945. {
  1946. struct cmipci_switch_args *args;
  1947. args = (struct cmipci_switch_args *)kcontrol->private_value;
  1948. snd_assert(args != NULL, return -EINVAL);
  1949. return _snd_cmipci_uswitch_put(kcontrol, ucontrol, args);
  1950. }
  1951. #define DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask_on, xis_byte, xac3) \
  1952. static struct cmipci_switch_args cmipci_switch_arg_##sname = { \
  1953. .reg = xreg, \
  1954. .mask = xmask, \
  1955. .mask_on = xmask_on, \
  1956. .is_byte = xis_byte, \
  1957. .ac3_sensitive = xac3, \
  1958. }
  1959. #define DEFINE_BIT_SWITCH_ARG(sname, xreg, xmask, xis_byte, xac3) \
  1960. DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask, xis_byte, xac3)
  1961. #if 0 /* these will be controlled in pcm device */
  1962. DEFINE_BIT_SWITCH_ARG(spdif_in, CM_REG_FUNCTRL1, CM_SPDF_1, 0, 0);
  1963. DEFINE_BIT_SWITCH_ARG(spdif_out, CM_REG_FUNCTRL1, CM_SPDF_0, 0, 0);
  1964. #endif
  1965. DEFINE_BIT_SWITCH_ARG(spdif_in_sel1, CM_REG_CHFORMAT, CM_SPDIF_SELECT1, 0, 0);
  1966. DEFINE_BIT_SWITCH_ARG(spdif_in_sel2, CM_REG_MISC_CTRL, CM_SPDIF_SELECT2, 0, 0);
  1967. DEFINE_BIT_SWITCH_ARG(spdif_enable, CM_REG_LEGACY_CTRL, CM_ENSPDOUT, 0, 0);
  1968. DEFINE_BIT_SWITCH_ARG(spdo2dac, CM_REG_FUNCTRL1, CM_SPDO2DAC, 0, 1);
  1969. DEFINE_BIT_SWITCH_ARG(spdi_valid, CM_REG_MISC, CM_SPDVALID, 1, 0);
  1970. DEFINE_BIT_SWITCH_ARG(spdif_copyright, CM_REG_LEGACY_CTRL, CM_SPDCOPYRHT, 0, 0);
  1971. DEFINE_BIT_SWITCH_ARG(spdif_dac_out, CM_REG_LEGACY_CTRL, CM_DAC2SPDO, 0, 1);
  1972. DEFINE_SWITCH_ARG(spdo_5v, CM_REG_MISC_CTRL, CM_SPDO5V, 0, 0, 0); /* inverse: 0 = 5V */
  1973. // DEFINE_BIT_SWITCH_ARG(spdo_48k, CM_REG_MISC_CTRL, CM_SPDF_AC97|CM_SPDIF48K, 0, 1);
  1974. DEFINE_BIT_SWITCH_ARG(spdif_loop, CM_REG_FUNCTRL1, CM_SPDFLOOP, 0, 1);
  1975. DEFINE_BIT_SWITCH_ARG(spdi_monitor, CM_REG_MIXER1, CM_CDPLAY, 1, 0);
  1976. /* DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_CHFORMAT, CM_SPDIF_INVERSE, 0, 0); */
  1977. DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_MISC, CM_SPDIF_INVERSE, 1, 0);
  1978. DEFINE_BIT_SWITCH_ARG(spdi_phase2, CM_REG_CHFORMAT, CM_SPDIF_INVERSE2, 0, 0);
  1979. #if CM_CH_PLAY == 1
  1980. DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, 0, 0, 0); /* reversed */
  1981. #else
  1982. DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, CM_XCHGDAC, 0, 0);
  1983. #endif
  1984. DEFINE_BIT_SWITCH_ARG(fourch, CM_REG_MISC_CTRL, CM_N4SPK3D, 0, 0);
  1985. // DEFINE_BIT_SWITCH_ARG(line_rear, CM_REG_MIXER1, CM_SPK4, 1, 0);
  1986. // DEFINE_BIT_SWITCH_ARG(line_bass, CM_REG_LEGACY_CTRL, CM_LINE_AS_BASS, 0, 0);
  1987. // DEFINE_BIT_SWITCH_ARG(joystick, CM_REG_FUNCTRL1, CM_JYSTK_EN, 0, 0); /* now module option */
  1988. DEFINE_SWITCH_ARG(modem, CM_REG_MISC_CTRL, CM_FLINKON|CM_FLINKOFF, CM_FLINKON, 0, 0);
  1989. #define DEFINE_SWITCH(sname, stype, sarg) \
  1990. { .name = sname, \
  1991. .iface = stype, \
  1992. .info = snd_cmipci_uswitch_info, \
  1993. .get = snd_cmipci_uswitch_get, \
  1994. .put = snd_cmipci_uswitch_put, \
  1995. .private_value = (unsigned long)&cmipci_switch_arg_##sarg,\
  1996. }
  1997. #define DEFINE_CARD_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_CARD, sarg)
  1998. #define DEFINE_MIXER_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_MIXER, sarg)
  1999. /*
  2000. * callbacks for spdif output switch
  2001. * needs toggle two registers..
  2002. */
  2003. static int snd_cmipci_spdout_enable_get(struct snd_kcontrol *kcontrol,
  2004. struct snd_ctl_elem_value *ucontrol)
  2005. {
  2006. int changed;
  2007. changed = _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
  2008. changed |= _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
  2009. return changed;
  2010. }
  2011. static int snd_cmipci_spdout_enable_put(struct snd_kcontrol *kcontrol,
  2012. struct snd_ctl_elem_value *ucontrol)
  2013. {
  2014. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  2015. int changed;
  2016. changed = _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
  2017. changed |= _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
  2018. if (changed) {
  2019. if (ucontrol->value.integer.value[0]) {
  2020. if (chip->spdif_playback_avail)
  2021. snd_cmipci_set_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  2022. } else {
  2023. if (chip->spdif_playback_avail)
  2024. snd_cmipci_clear_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  2025. }
  2026. }
  2027. chip->spdif_playback_enabled = ucontrol->value.integer.value[0];
  2028. return changed;
  2029. }
  2030. static int snd_cmipci_line_in_mode_info(struct snd_kcontrol *kcontrol,
  2031. struct snd_ctl_elem_info *uinfo)
  2032. {
  2033. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2034. static char *texts[3] = { "Line-In", "Rear Output", "Bass Output" };
  2035. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2036. uinfo->count = 1;
  2037. uinfo->value.enumerated.items = cm->chip_version >= 39 ? 3 : 2;
  2038. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  2039. uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
  2040. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  2041. return 0;
  2042. }
  2043. static inline unsigned int get_line_in_mode(struct cmipci *cm)
  2044. {
  2045. unsigned int val;
  2046. if (cm->chip_version >= 39) {
  2047. val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL);
  2048. if (val & CM_LINE_AS_BASS)
  2049. return 2;
  2050. }
  2051. val = snd_cmipci_read_b(cm, CM_REG_MIXER1);
  2052. if (val & CM_SPK4)
  2053. return 1;
  2054. return 0;
  2055. }
  2056. static int snd_cmipci_line_in_mode_get(struct snd_kcontrol *kcontrol,
  2057. struct snd_ctl_elem_value *ucontrol)
  2058. {
  2059. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2060. spin_lock_irq(&cm->reg_lock);
  2061. ucontrol->value.enumerated.item[0] = get_line_in_mode(cm);
  2062. spin_unlock_irq(&cm->reg_lock);
  2063. return 0;
  2064. }
  2065. static int snd_cmipci_line_in_mode_put(struct snd_kcontrol *kcontrol,
  2066. struct snd_ctl_elem_value *ucontrol)
  2067. {
  2068. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2069. int change;
  2070. spin_lock_irq(&cm->reg_lock);
  2071. if (ucontrol->value.enumerated.item[0] == 2)
  2072. change = snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_LINE_AS_BASS);
  2073. else
  2074. change = snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_LINE_AS_BASS);
  2075. if (ucontrol->value.enumerated.item[0] == 1)
  2076. change |= snd_cmipci_set_bit_b(cm, CM_REG_MIXER1, CM_SPK4);
  2077. else
  2078. change |= snd_cmipci_clear_bit_b(cm, CM_REG_MIXER1, CM_SPK4);
  2079. spin_unlock_irq(&cm->reg_lock);
  2080. return change;
  2081. }
  2082. static int snd_cmipci_mic_in_mode_info(struct snd_kcontrol *kcontrol,
  2083. struct snd_ctl_elem_info *uinfo)
  2084. {
  2085. static char *texts[2] = { "Mic-In", "Center/LFE Output" };
  2086. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2087. uinfo->count = 1;
  2088. uinfo->value.enumerated.items = 2;
  2089. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  2090. uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
  2091. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  2092. return 0;
  2093. }
  2094. static int snd_cmipci_mic_in_mode_get(struct snd_kcontrol *kcontrol,
  2095. struct snd_ctl_elem_value *ucontrol)
  2096. {
  2097. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2098. /* same bit as spdi_phase */
  2099. spin_lock_irq(&cm->reg_lock);
  2100. ucontrol->value.enumerated.item[0] =
  2101. (snd_cmipci_read_b(cm, CM_REG_MISC) & CM_SPDIF_INVERSE) ? 1 : 0;
  2102. spin_unlock_irq(&cm->reg_lock);
  2103. return 0;
  2104. }
  2105. static int snd_cmipci_mic_in_mode_put(struct snd_kcontrol *kcontrol,
  2106. struct snd_ctl_elem_value *ucontrol)
  2107. {
  2108. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2109. int change;
  2110. spin_lock_irq(&cm->reg_lock);
  2111. if (ucontrol->value.enumerated.item[0])
  2112. change = snd_cmipci_set_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
  2113. else
  2114. change = snd_cmipci_clear_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
  2115. spin_unlock_irq(&cm->reg_lock);
  2116. return change;
  2117. }
  2118. /* both for CM8338/8738 */
  2119. static struct snd_kcontrol_new snd_cmipci_mixer_switches[] __devinitdata = {
  2120. DEFINE_MIXER_SWITCH("Four Channel Mode", fourch),
  2121. {
  2122. .name = "Line-In Mode",
  2123. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2124. .info = snd_cmipci_line_in_mode_info,
  2125. .get = snd_cmipci_line_in_mode_get,
  2126. .put = snd_cmipci_line_in_mode_put,
  2127. },
  2128. };
  2129. /* for non-multichannel chips */
  2130. static struct snd_kcontrol_new snd_cmipci_nomulti_switch __devinitdata =
  2131. DEFINE_MIXER_SWITCH("Exchange DAC", exchange_dac);
  2132. /* only for CM8738 */
  2133. static struct snd_kcontrol_new snd_cmipci_8738_mixer_switches[] __devinitdata = {
  2134. #if 0 /* controlled in pcm device */
  2135. DEFINE_MIXER_SWITCH("IEC958 In Record", spdif_in),
  2136. DEFINE_MIXER_SWITCH("IEC958 Out", spdif_out),
  2137. DEFINE_MIXER_SWITCH("IEC958 Out To DAC", spdo2dac),
  2138. #endif
  2139. // DEFINE_MIXER_SWITCH("IEC958 Output Switch", spdif_enable),
  2140. { .name = "IEC958 Output Switch",
  2141. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2142. .info = snd_cmipci_uswitch_info,
  2143. .get = snd_cmipci_spdout_enable_get,
  2144. .put = snd_cmipci_spdout_enable_put,
  2145. },
  2146. DEFINE_MIXER_SWITCH("IEC958 In Valid", spdi_valid),
  2147. DEFINE_MIXER_SWITCH("IEC958 Copyright", spdif_copyright),
  2148. DEFINE_MIXER_SWITCH("IEC958 5V", spdo_5v),
  2149. // DEFINE_MIXER_SWITCH("IEC958 In/Out 48KHz", spdo_48k),
  2150. DEFINE_MIXER_SWITCH("IEC958 Loop", spdif_loop),
  2151. DEFINE_MIXER_SWITCH("IEC958 In Monitor", spdi_monitor),
  2152. };
  2153. /* only for model 033/037 */
  2154. static struct snd_kcontrol_new snd_cmipci_old_mixer_switches[] __devinitdata = {
  2155. DEFINE_MIXER_SWITCH("IEC958 Mix Analog", spdif_dac_out),
  2156. DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase),
  2157. DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel1),
  2158. };
  2159. /* only for model 039 or later */
  2160. static struct snd_kcontrol_new snd_cmipci_extra_mixer_switches[] __devinitdata = {
  2161. DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel2),
  2162. DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase2),
  2163. {
  2164. .name = "Mic-In Mode",
  2165. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2166. .info = snd_cmipci_mic_in_mode_info,
  2167. .get = snd_cmipci_mic_in_mode_get,
  2168. .put = snd_cmipci_mic_in_mode_put,
  2169. }
  2170. };
  2171. /* card control switches */
  2172. static struct snd_kcontrol_new snd_cmipci_control_switches[] __devinitdata = {
  2173. // DEFINE_CARD_SWITCH("Joystick", joystick), /* now module option */
  2174. DEFINE_CARD_SWITCH("Modem", modem),
  2175. };
  2176. static int __devinit snd_cmipci_mixer_new(struct cmipci *cm, int pcm_spdif_device)
  2177. {
  2178. struct snd_card *card;
  2179. struct snd_kcontrol_new *sw;
  2180. struct snd_kcontrol *kctl;
  2181. unsigned int idx;
  2182. int err;
  2183. snd_assert(cm != NULL && cm->card != NULL, return -EINVAL);
  2184. card = cm->card;
  2185. strcpy(card->mixername, "CMedia PCI");
  2186. spin_lock_irq(&cm->reg_lock);
  2187. snd_cmipci_mixer_write(cm, 0x00, 0x00); /* mixer reset */
  2188. spin_unlock_irq(&cm->reg_lock);
  2189. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixers); idx++) {
  2190. if (cm->chip_version == 68) { // 8768 has no PCM volume
  2191. if (!strcmp(snd_cmipci_mixers[idx].name,
  2192. "PCM Playback Volume"))
  2193. continue;
  2194. }
  2195. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cmipci_mixers[idx], cm))) < 0)
  2196. return err;
  2197. }
  2198. /* mixer switches */
  2199. sw = snd_cmipci_mixer_switches;
  2200. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixer_switches); idx++, sw++) {
  2201. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2202. if (err < 0)
  2203. return err;
  2204. }
  2205. if (! cm->can_multi_ch) {
  2206. err = snd_ctl_add(cm->card, snd_ctl_new1(&snd_cmipci_nomulti_switch, cm));
  2207. if (err < 0)
  2208. return err;
  2209. }
  2210. if (cm->device == PCI_DEVICE_ID_CMEDIA_CM8738 ||
  2211. cm->device == PCI_DEVICE_ID_CMEDIA_CM8738B) {
  2212. sw = snd_cmipci_8738_mixer_switches;
  2213. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_8738_mixer_switches); idx++, sw++) {
  2214. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2215. if (err < 0)
  2216. return err;
  2217. }
  2218. if (cm->can_ac3_hw) {
  2219. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_default, cm))) < 0)
  2220. return err;
  2221. kctl->id.device = pcm_spdif_device;
  2222. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_mask, cm))) < 0)
  2223. return err;
  2224. kctl->id.device = pcm_spdif_device;
  2225. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_stream, cm))) < 0)
  2226. return err;
  2227. kctl->id.device = pcm_spdif_device;
  2228. }
  2229. if (cm->chip_version <= 37) {
  2230. sw = snd_cmipci_old_mixer_switches;
  2231. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_old_mixer_switches); idx++, sw++) {
  2232. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2233. if (err < 0)
  2234. return err;
  2235. }
  2236. }
  2237. }
  2238. if (cm->chip_version >= 39) {
  2239. sw = snd_cmipci_extra_mixer_switches;
  2240. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_extra_mixer_switches); idx++, sw++) {
  2241. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2242. if (err < 0)
  2243. return err;
  2244. }
  2245. }
  2246. /* card switches */
  2247. sw = snd_cmipci_control_switches;
  2248. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_control_switches); idx++, sw++) {
  2249. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2250. if (err < 0)
  2251. return err;
  2252. }
  2253. for (idx = 0; idx < CM_SAVED_MIXERS; idx++) {
  2254. struct snd_ctl_elem_id id;
  2255. struct snd_kcontrol *ctl;
  2256. memset(&id, 0, sizeof(id));
  2257. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  2258. strcpy(id.name, cm_saved_mixer[idx].name);
  2259. if ((ctl = snd_ctl_find_id(cm->card, &id)) != NULL)
  2260. cm->mixer_res_ctl[idx] = ctl;
  2261. }
  2262. return 0;
  2263. }
  2264. /*
  2265. * proc interface
  2266. */
  2267. #ifdef CONFIG_PROC_FS
  2268. static void snd_cmipci_proc_read(struct snd_info_entry *entry,
  2269. struct snd_info_buffer *buffer)
  2270. {
  2271. struct cmipci *cm = entry->private_data;
  2272. int i;
  2273. snd_iprintf(buffer, "%s\n\n", cm->card->longname);
  2274. for (i = 0; i < 0x40; i++) {
  2275. int v = inb(cm->iobase + i);
  2276. if (i % 4 == 0)
  2277. snd_iprintf(buffer, "%02x: ", i);
  2278. snd_iprintf(buffer, "%02x", v);
  2279. if (i % 4 == 3)
  2280. snd_iprintf(buffer, "\n");
  2281. else
  2282. snd_iprintf(buffer, " ");
  2283. }
  2284. }
  2285. static void __devinit snd_cmipci_proc_init(struct cmipci *cm)
  2286. {
  2287. struct snd_info_entry *entry;
  2288. if (! snd_card_proc_new(cm->card, "cmipci", &entry))
  2289. snd_info_set_text_ops(entry, cm, snd_cmipci_proc_read);
  2290. }
  2291. #else /* !CONFIG_PROC_FS */
  2292. static inline void snd_cmipci_proc_init(struct cmipci *cm) {}
  2293. #endif
  2294. static struct pci_device_id snd_cmipci_ids[] = {
  2295. {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338A, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2296. {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2297. {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2298. {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2299. {PCI_VENDOR_ID_AL, PCI_DEVICE_ID_CMEDIA_CM8738, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2300. {0,},
  2301. };
  2302. /*
  2303. * check chip version and capabilities
  2304. * driver name is modified according to the chip model
  2305. */
  2306. static void __devinit query_chip(struct cmipci *cm)
  2307. {
  2308. unsigned int detect;
  2309. /* check reg 0Ch, bit 24-31 */
  2310. detect = snd_cmipci_read(cm, CM_REG_INT_HLDCLR) & CM_CHIP_MASK2;
  2311. if (! detect) {
  2312. /* check reg 08h, bit 24-28 */
  2313. detect = snd_cmipci_read(cm, CM_REG_CHFORMAT) & CM_CHIP_MASK1;
  2314. if (! detect) {
  2315. cm->chip_version = 33;
  2316. cm->max_channels = 2;
  2317. if (cm->do_soft_ac3)
  2318. cm->can_ac3_sw = 1;
  2319. else
  2320. cm->can_ac3_hw = 1;
  2321. cm->has_dual_dac = 1;
  2322. } else {
  2323. cm->chip_version = 37;
  2324. cm->max_channels = 2;
  2325. cm->can_ac3_hw = 1;
  2326. cm->has_dual_dac = 1;
  2327. }
  2328. } else {
  2329. /* check reg 0Ch, bit 26 */
  2330. if (detect & CM_CHIP_8768) {
  2331. cm->chip_version = 68;
  2332. cm->max_channels = 8;
  2333. cm->can_ac3_hw = 1;
  2334. cm->has_dual_dac = 1;
  2335. cm->can_multi_ch = 1;
  2336. } else if (detect & CM_CHIP_055) {
  2337. cm->chip_version = 55;
  2338. cm->max_channels = 6;
  2339. cm->can_ac3_hw = 1;
  2340. cm->has_dual_dac = 1;
  2341. cm->can_multi_ch = 1;
  2342. } else if (detect & CM_CHIP_039) {
  2343. cm->chip_version = 39;
  2344. if (detect & CM_CHIP_039_6CH) /* 4 or 6 channels */
  2345. cm->max_channels = 6;
  2346. else
  2347. cm->max_channels = 4;
  2348. cm->can_ac3_hw = 1;
  2349. cm->has_dual_dac = 1;
  2350. cm->can_multi_ch = 1;
  2351. } else {
  2352. printk(KERN_ERR "chip %x version not supported\n", detect);
  2353. }
  2354. }
  2355. }
  2356. #ifdef SUPPORT_JOYSTICK
  2357. static int __devinit snd_cmipci_create_gameport(struct cmipci *cm, int dev)
  2358. {
  2359. static int ports[] = { 0x201, 0x200, 0 }; /* FIXME: majority is 0x201? */
  2360. struct gameport *gp;
  2361. struct resource *r = NULL;
  2362. int i, io_port = 0;
  2363. if (joystick_port[dev] == 0)
  2364. return -ENODEV;
  2365. if (joystick_port[dev] == 1) { /* auto-detect */
  2366. for (i = 0; ports[i]; i++) {
  2367. io_port = ports[i];
  2368. r = request_region(io_port, 1, "CMIPCI gameport");
  2369. if (r)
  2370. break;
  2371. }
  2372. } else {
  2373. io_port = joystick_port[dev];
  2374. r = request_region(io_port, 1, "CMIPCI gameport");
  2375. }
  2376. if (!r) {
  2377. printk(KERN_WARNING "cmipci: cannot reserve joystick ports\n");
  2378. return -EBUSY;
  2379. }
  2380. cm->gameport = gp = gameport_allocate_port();
  2381. if (!gp) {
  2382. printk(KERN_ERR "cmipci: cannot allocate memory for gameport\n");
  2383. release_and_free_resource(r);
  2384. return -ENOMEM;
  2385. }
  2386. gameport_set_name(gp, "C-Media Gameport");
  2387. gameport_set_phys(gp, "pci%s/gameport0", pci_name(cm->pci));
  2388. gameport_set_dev_parent(gp, &cm->pci->dev);
  2389. gp->io = io_port;
  2390. gameport_set_port_data(gp, r);
  2391. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
  2392. gameport_register_port(cm->gameport);
  2393. return 0;
  2394. }
  2395. static void snd_cmipci_free_gameport(struct cmipci *cm)
  2396. {
  2397. if (cm->gameport) {
  2398. struct resource *r = gameport_get_port_data(cm->gameport);
  2399. gameport_unregister_port(cm->gameport);
  2400. cm->gameport = NULL;
  2401. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
  2402. release_and_free_resource(r);
  2403. }
  2404. }
  2405. #else
  2406. static inline int snd_cmipci_create_gameport(struct cmipci *cm, int dev) { return -ENOSYS; }
  2407. static inline void snd_cmipci_free_gameport(struct cmipci *cm) { }
  2408. #endif
  2409. static int snd_cmipci_free(struct cmipci *cm)
  2410. {
  2411. if (cm->irq >= 0) {
  2412. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  2413. snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT);
  2414. snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0); /* disable ints */
  2415. snd_cmipci_ch_reset(cm, CM_CH_PLAY);
  2416. snd_cmipci_ch_reset(cm, CM_CH_CAPT);
  2417. snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
  2418. snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
  2419. /* reset mixer */
  2420. snd_cmipci_mixer_write(cm, 0, 0);
  2421. synchronize_irq(cm->irq);
  2422. free_irq(cm->irq, cm);
  2423. }
  2424. snd_cmipci_free_gameport(cm);
  2425. pci_release_regions(cm->pci);
  2426. pci_disable_device(cm->pci);
  2427. kfree(cm);
  2428. return 0;
  2429. }
  2430. static int snd_cmipci_dev_free(struct snd_device *device)
  2431. {
  2432. struct cmipci *cm = device->device_data;
  2433. return snd_cmipci_free(cm);
  2434. }
  2435. static int __devinit snd_cmipci_create_fm(struct cmipci *cm, long fm_port)
  2436. {
  2437. long iosynth;
  2438. unsigned int val;
  2439. struct snd_opl3 *opl3;
  2440. int err;
  2441. /* first try FM regs in PCI port range */
  2442. iosynth = cm->iobase + CM_REG_FM_PCI;
  2443. err = snd_opl3_create(cm->card, iosynth, iosynth + 2,
  2444. OPL3_HW_OPL3, 1, &opl3);
  2445. if (err < 0) {
  2446. /* then try legacy ports */
  2447. val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL) & ~CM_FMSEL_MASK;
  2448. iosynth = fm_port;
  2449. switch (iosynth) {
  2450. case 0x3E8: val |= CM_FMSEL_3E8; break;
  2451. case 0x3E0: val |= CM_FMSEL_3E0; break;
  2452. case 0x3C8: val |= CM_FMSEL_3C8; break;
  2453. case 0x388: val |= CM_FMSEL_388; break;
  2454. default:
  2455. return 0;
  2456. }
  2457. snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
  2458. /* enable FM */
  2459. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  2460. if (snd_opl3_create(cm->card, iosynth, iosynth + 2,
  2461. OPL3_HW_OPL3, 0, &opl3) < 0) {
  2462. printk(KERN_ERR "cmipci: no OPL device at %#lx, "
  2463. "skipping...\n", iosynth);
  2464. /* disable FM */
  2465. snd_cmipci_write(cm, CM_REG_LEGACY_CTRL,
  2466. val & ~CM_FMSEL_MASK);
  2467. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  2468. return 0;
  2469. }
  2470. }
  2471. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  2472. printk(KERN_ERR "cmipci: cannot create OPL3 hwdep\n");
  2473. return err;
  2474. }
  2475. return 0;
  2476. }
  2477. static int __devinit snd_cmipci_create(struct snd_card *card, struct pci_dev *pci,
  2478. int dev, struct cmipci **rcmipci)
  2479. {
  2480. struct cmipci *cm;
  2481. int err;
  2482. static struct snd_device_ops ops = {
  2483. .dev_free = snd_cmipci_dev_free,
  2484. };
  2485. unsigned int val = 0;
  2486. long iomidi;
  2487. int integrated_midi;
  2488. int pcm_index, pcm_spdif_index;
  2489. static struct pci_device_id intel_82437vx[] = {
  2490. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437VX) },
  2491. { },
  2492. };
  2493. *rcmipci = NULL;
  2494. if ((err = pci_enable_device(pci)) < 0)
  2495. return err;
  2496. cm = kzalloc(sizeof(*cm), GFP_KERNEL);
  2497. if (cm == NULL) {
  2498. pci_disable_device(pci);
  2499. return -ENOMEM;
  2500. }
  2501. spin_lock_init(&cm->reg_lock);
  2502. mutex_init(&cm->open_mutex);
  2503. cm->device = pci->device;
  2504. cm->card = card;
  2505. cm->pci = pci;
  2506. cm->irq = -1;
  2507. cm->channel[0].ch = 0;
  2508. cm->channel[1].ch = 1;
  2509. cm->channel[0].is_dac = cm->channel[1].is_dac = 1; /* dual DAC mode */
  2510. if ((err = pci_request_regions(pci, card->driver)) < 0) {
  2511. kfree(cm);
  2512. pci_disable_device(pci);
  2513. return err;
  2514. }
  2515. cm->iobase = pci_resource_start(pci, 0);
  2516. if (request_irq(pci->irq, snd_cmipci_interrupt,
  2517. IRQF_DISABLED|IRQF_SHARED, card->driver, cm)) {
  2518. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  2519. snd_cmipci_free(cm);
  2520. return -EBUSY;
  2521. }
  2522. cm->irq = pci->irq;
  2523. pci_set_master(cm->pci);
  2524. /*
  2525. * check chip version, max channels and capabilities
  2526. */
  2527. cm->chip_version = 0;
  2528. cm->max_channels = 2;
  2529. cm->do_soft_ac3 = soft_ac3[dev];
  2530. if (pci->device != PCI_DEVICE_ID_CMEDIA_CM8338A &&
  2531. pci->device != PCI_DEVICE_ID_CMEDIA_CM8338B)
  2532. query_chip(cm);
  2533. /* added -MCx suffix for chip supporting multi-channels */
  2534. if (cm->can_multi_ch)
  2535. sprintf(cm->card->driver + strlen(cm->card->driver),
  2536. "-MC%d", cm->max_channels);
  2537. else if (cm->can_ac3_sw)
  2538. strcpy(cm->card->driver + strlen(cm->card->driver), "-SWIEC");
  2539. cm->dig_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
  2540. cm->dig_pcm_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
  2541. #if CM_CH_PLAY == 1
  2542. cm->ctrl = CM_CHADC0; /* default FUNCNTRL0 */
  2543. #else
  2544. cm->ctrl = CM_CHADC1; /* default FUNCNTRL0 */
  2545. #endif
  2546. /* initialize codec registers */
  2547. snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0); /* disable ints */
  2548. snd_cmipci_ch_reset(cm, CM_CH_PLAY);
  2549. snd_cmipci_ch_reset(cm, CM_CH_CAPT);
  2550. snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
  2551. snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
  2552. snd_cmipci_write(cm, CM_REG_CHFORMAT, 0);
  2553. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC|CM_N4SPK3D);
  2554. #if CM_CH_PLAY == 1
  2555. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  2556. #else
  2557. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  2558. #endif
  2559. /* Set Bus Master Request */
  2560. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_BREQ);
  2561. /* Assume TX and compatible chip set (Autodetection required for VX chip sets) */
  2562. switch (pci->device) {
  2563. case PCI_DEVICE_ID_CMEDIA_CM8738:
  2564. case PCI_DEVICE_ID_CMEDIA_CM8738B:
  2565. if (!pci_dev_present(intel_82437vx))
  2566. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_TXVX);
  2567. break;
  2568. default:
  2569. break;
  2570. }
  2571. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, cm, &ops)) < 0) {
  2572. snd_cmipci_free(cm);
  2573. return err;
  2574. }
  2575. integrated_midi = snd_cmipci_read_b(cm, CM_REG_MPU_PCI) != 0xff;
  2576. if (integrated_midi && mpu_port[dev] == 1)
  2577. iomidi = cm->iobase + CM_REG_MPU_PCI;
  2578. else {
  2579. iomidi = mpu_port[dev];
  2580. switch (iomidi) {
  2581. case 0x320: val = CM_VMPU_320; break;
  2582. case 0x310: val = CM_VMPU_310; break;
  2583. case 0x300: val = CM_VMPU_300; break;
  2584. case 0x330: val = CM_VMPU_330; break;
  2585. default:
  2586. iomidi = 0; break;
  2587. }
  2588. if (iomidi > 0) {
  2589. snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
  2590. /* enable UART */
  2591. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_UART_EN);
  2592. }
  2593. }
  2594. if ((err = snd_cmipci_create_fm(cm, fm_port[dev])) < 0)
  2595. return err;
  2596. /* reset mixer */
  2597. snd_cmipci_mixer_write(cm, 0, 0);
  2598. snd_cmipci_proc_init(cm);
  2599. /* create pcm devices */
  2600. pcm_index = pcm_spdif_index = 0;
  2601. if ((err = snd_cmipci_pcm_new(cm, pcm_index)) < 0)
  2602. return err;
  2603. pcm_index++;
  2604. if (cm->has_dual_dac) {
  2605. if ((err = snd_cmipci_pcm2_new(cm, pcm_index)) < 0)
  2606. return err;
  2607. pcm_index++;
  2608. }
  2609. if (cm->can_ac3_hw || cm->can_ac3_sw) {
  2610. pcm_spdif_index = pcm_index;
  2611. if ((err = snd_cmipci_pcm_spdif_new(cm, pcm_index)) < 0)
  2612. return err;
  2613. }
  2614. /* create mixer interface & switches */
  2615. if ((err = snd_cmipci_mixer_new(cm, pcm_spdif_index)) < 0)
  2616. return err;
  2617. if (iomidi > 0) {
  2618. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
  2619. iomidi,
  2620. (integrated_midi ?
  2621. MPU401_INFO_INTEGRATED : 0),
  2622. cm->irq, 0, &cm->rmidi)) < 0) {
  2623. printk(KERN_ERR "cmipci: no UART401 device at 0x%lx\n", iomidi);
  2624. }
  2625. }
  2626. #ifdef USE_VAR48KRATE
  2627. for (val = 0; val < ARRAY_SIZE(rates); val++)
  2628. snd_cmipci_set_pll(cm, rates[val], val);
  2629. /*
  2630. * (Re-)Enable external switch spdo_48k
  2631. */
  2632. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K|CM_SPDF_AC97);
  2633. #endif /* USE_VAR48KRATE */
  2634. if (snd_cmipci_create_gameport(cm, dev) < 0)
  2635. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
  2636. snd_card_set_dev(card, &pci->dev);
  2637. *rcmipci = cm;
  2638. return 0;
  2639. }
  2640. /*
  2641. */
  2642. MODULE_DEVICE_TABLE(pci, snd_cmipci_ids);
  2643. static int __devinit snd_cmipci_probe(struct pci_dev *pci,
  2644. const struct pci_device_id *pci_id)
  2645. {
  2646. static int dev;
  2647. struct snd_card *card;
  2648. struct cmipci *cm;
  2649. int err;
  2650. if (dev >= SNDRV_CARDS)
  2651. return -ENODEV;
  2652. if (! enable[dev]) {
  2653. dev++;
  2654. return -ENOENT;
  2655. }
  2656. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  2657. if (card == NULL)
  2658. return -ENOMEM;
  2659. switch (pci->device) {
  2660. case PCI_DEVICE_ID_CMEDIA_CM8738:
  2661. case PCI_DEVICE_ID_CMEDIA_CM8738B:
  2662. strcpy(card->driver, "CMI8738");
  2663. break;
  2664. case PCI_DEVICE_ID_CMEDIA_CM8338A:
  2665. case PCI_DEVICE_ID_CMEDIA_CM8338B:
  2666. strcpy(card->driver, "CMI8338");
  2667. break;
  2668. default:
  2669. strcpy(card->driver, "CMIPCI");
  2670. break;
  2671. }
  2672. if ((err = snd_cmipci_create(card, pci, dev, &cm)) < 0) {
  2673. snd_card_free(card);
  2674. return err;
  2675. }
  2676. card->private_data = cm;
  2677. sprintf(card->shortname, "C-Media PCI %s", card->driver);
  2678. sprintf(card->longname, "%s (model %d) at 0x%lx, irq %i",
  2679. card->shortname,
  2680. cm->chip_version,
  2681. cm->iobase,
  2682. cm->irq);
  2683. //snd_printd("%s is detected\n", card->longname);
  2684. if ((err = snd_card_register(card)) < 0) {
  2685. snd_card_free(card);
  2686. return err;
  2687. }
  2688. pci_set_drvdata(pci, card);
  2689. dev++;
  2690. return 0;
  2691. }
  2692. static void __devexit snd_cmipci_remove(struct pci_dev *pci)
  2693. {
  2694. snd_card_free(pci_get_drvdata(pci));
  2695. pci_set_drvdata(pci, NULL);
  2696. }
  2697. #ifdef CONFIG_PM
  2698. /*
  2699. * power management
  2700. */
  2701. static unsigned char saved_regs[] = {
  2702. CM_REG_FUNCTRL1, CM_REG_CHFORMAT, CM_REG_LEGACY_CTRL, CM_REG_MISC_CTRL,
  2703. CM_REG_MIXER0, CM_REG_MIXER1, CM_REG_MIXER2, CM_REG_MIXER3, CM_REG_PLL,
  2704. CM_REG_CH0_FRAME1, CM_REG_CH0_FRAME2,
  2705. CM_REG_CH1_FRAME1, CM_REG_CH1_FRAME2, CM_REG_EXT_MISC,
  2706. CM_REG_INT_STATUS, CM_REG_INT_HLDCLR, CM_REG_FUNCTRL0,
  2707. };
  2708. static unsigned char saved_mixers[] = {
  2709. SB_DSP4_MASTER_DEV, SB_DSP4_MASTER_DEV + 1,
  2710. SB_DSP4_PCM_DEV, SB_DSP4_PCM_DEV + 1,
  2711. SB_DSP4_SYNTH_DEV, SB_DSP4_SYNTH_DEV + 1,
  2712. SB_DSP4_CD_DEV, SB_DSP4_CD_DEV + 1,
  2713. SB_DSP4_LINE_DEV, SB_DSP4_LINE_DEV + 1,
  2714. SB_DSP4_MIC_DEV, SB_DSP4_SPEAKER_DEV,
  2715. CM_REG_EXTENT_IND, SB_DSP4_OUTPUT_SW,
  2716. SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT,
  2717. };
  2718. static int snd_cmipci_suspend(struct pci_dev *pci, pm_message_t state)
  2719. {
  2720. struct snd_card *card = pci_get_drvdata(pci);
  2721. struct cmipci *cm = card->private_data;
  2722. int i;
  2723. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  2724. snd_pcm_suspend_all(cm->pcm);
  2725. snd_pcm_suspend_all(cm->pcm2);
  2726. snd_pcm_suspend_all(cm->pcm_spdif);
  2727. /* save registers */
  2728. for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  2729. cm->saved_regs[i] = snd_cmipci_read(cm, saved_regs[i]);
  2730. for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
  2731. cm->saved_mixers[i] = snd_cmipci_mixer_read(cm, saved_mixers[i]);
  2732. /* disable ints */
  2733. snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
  2734. pci_set_power_state(pci, PCI_D3hot);
  2735. pci_disable_device(pci);
  2736. pci_save_state(pci);
  2737. return 0;
  2738. }
  2739. static int snd_cmipci_resume(struct pci_dev *pci)
  2740. {
  2741. struct snd_card *card = pci_get_drvdata(pci);
  2742. struct cmipci *cm = card->private_data;
  2743. int i;
  2744. pci_restore_state(pci);
  2745. pci_enable_device(pci);
  2746. pci_set_power_state(pci, PCI_D0);
  2747. pci_set_master(pci);
  2748. /* reset / initialize to a sane state */
  2749. snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
  2750. snd_cmipci_ch_reset(cm, CM_CH_PLAY);
  2751. snd_cmipci_ch_reset(cm, CM_CH_CAPT);
  2752. snd_cmipci_mixer_write(cm, 0, 0);
  2753. /* restore registers */
  2754. for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  2755. snd_cmipci_write(cm, saved_regs[i], cm->saved_regs[i]);
  2756. for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
  2757. snd_cmipci_mixer_write(cm, saved_mixers[i], cm->saved_mixers[i]);
  2758. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  2759. return 0;
  2760. }
  2761. #endif /* CONFIG_PM */
  2762. static struct pci_driver driver = {
  2763. .name = "C-Media PCI",
  2764. .id_table = snd_cmipci_ids,
  2765. .probe = snd_cmipci_probe,
  2766. .remove = __devexit_p(snd_cmipci_remove),
  2767. #ifdef CONFIG_PM
  2768. .suspend = snd_cmipci_suspend,
  2769. .resume = snd_cmipci_resume,
  2770. #endif
  2771. };
  2772. static int __init alsa_card_cmipci_init(void)
  2773. {
  2774. return pci_register_driver(&driver);
  2775. }
  2776. static void __exit alsa_card_cmipci_exit(void)
  2777. {
  2778. pci_unregister_driver(&driver);
  2779. }
  2780. module_init(alsa_card_cmipci_init)
  2781. module_exit(alsa_card_cmipci_exit)