cmipci.c 94 KB

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