cs46xx_lib.c 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Abramo Bagnara <abramo@alsa-project.org>
  4. * Cirrus Logic, Inc.
  5. * Routines for control of Cirrus Logic CS461x chips
  6. *
  7. * KNOWN BUGS:
  8. * - Sometimes the SPDIF input DSP tasks get's unsynchronized
  9. * and the SPDIF get somewhat "distorcionated", or/and left right channel
  10. * are swapped. To get around this problem when it happens, mute and unmute
  11. * the SPDIF input mixer control.
  12. * - On the Hercules Game Theater XP the amplifier are sometimes turned
  13. * off on inadecuate moments which causes distorcions on sound.
  14. *
  15. * TODO:
  16. * - Secondary CODEC on some soundcards
  17. * - SPDIF input support for other sample rates then 48khz
  18. * - Posibility to mix the SPDIF output with analog sources.
  19. * - PCM channels for Center and LFE on secondary codec
  20. *
  21. * NOTE: with CONFIG_SND_CS46XX_NEW_DSP unset uses old DSP image (which
  22. * is default configuration), no SPDIF, no secondary codec, no
  23. * multi channel PCM. But known to work.
  24. *
  25. * FINALLY: A credit to the developers Tom and Jordan
  26. * at Cirrus for have helping me out with the DSP, however we
  27. * still don't have sufficient documentation and technical
  28. * references to be able to implement all fancy feutures
  29. * supported by the cs46xx DSP's.
  30. * Benny <benny@hostmobility.com>
  31. *
  32. * This program is free software; you can redistribute it and/or modify
  33. * it under the terms of the GNU General Public License as published by
  34. * the Free Software Foundation; either version 2 of the License, or
  35. * (at your option) any later version.
  36. *
  37. * This program is distributed in the hope that it will be useful,
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. * GNU General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU General Public License
  43. * along with this program; if not, write to the Free Software
  44. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  45. *
  46. */
  47. #include <linux/delay.h>
  48. #include <linux/pci.h>
  49. #include <linux/pm.h>
  50. #include <linux/init.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/slab.h>
  53. #include <linux/gameport.h>
  54. #include <linux/mutex.h>
  55. #include <linux/export.h>
  56. #include <linux/module.h>
  57. #include <linux/firmware.h>
  58. #include <linux/vmalloc.h>
  59. #include <sound/core.h>
  60. #include <sound/control.h>
  61. #include <sound/info.h>
  62. #include <sound/pcm.h>
  63. #include <sound/pcm_params.h>
  64. #include "cs46xx.h"
  65. #include <asm/io.h>
  66. #include "cs46xx_lib.h"
  67. #include "dsp_spos.h"
  68. static void amp_voyetra(struct snd_cs46xx *chip, int change);
  69. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  70. static struct snd_pcm_ops snd_cs46xx_playback_rear_ops;
  71. static struct snd_pcm_ops snd_cs46xx_playback_indirect_rear_ops;
  72. static struct snd_pcm_ops snd_cs46xx_playback_clfe_ops;
  73. static struct snd_pcm_ops snd_cs46xx_playback_indirect_clfe_ops;
  74. static struct snd_pcm_ops snd_cs46xx_playback_iec958_ops;
  75. static struct snd_pcm_ops snd_cs46xx_playback_indirect_iec958_ops;
  76. #endif
  77. static struct snd_pcm_ops snd_cs46xx_playback_ops;
  78. static struct snd_pcm_ops snd_cs46xx_playback_indirect_ops;
  79. static struct snd_pcm_ops snd_cs46xx_capture_ops;
  80. static struct snd_pcm_ops snd_cs46xx_capture_indirect_ops;
  81. static unsigned short snd_cs46xx_codec_read(struct snd_cs46xx *chip,
  82. unsigned short reg,
  83. int codec_index)
  84. {
  85. int count;
  86. unsigned short result,tmp;
  87. u32 offset = 0;
  88. if (snd_BUG_ON(codec_index != CS46XX_PRIMARY_CODEC_INDEX &&
  89. codec_index != CS46XX_SECONDARY_CODEC_INDEX))
  90. return 0xffff;
  91. chip->active_ctrl(chip, 1);
  92. if (codec_index == CS46XX_SECONDARY_CODEC_INDEX)
  93. offset = CS46XX_SECONDARY_CODEC_OFFSET;
  94. /*
  95. * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
  96. * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
  97. * 3. Write ACCTL = Control Register = 460h for initiating the write7---55
  98. * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
  99. * 5. if DCV not cleared, break and return error
  100. * 6. Read ACSTS = Status Register = 464h, check VSTS bit
  101. */
  102. snd_cs46xx_peekBA0(chip, BA0_ACSDA + offset);
  103. tmp = snd_cs46xx_peekBA0(chip, BA0_ACCTL);
  104. if ((tmp & ACCTL_VFRM) == 0) {
  105. snd_printk(KERN_WARNING "cs46xx: ACCTL_VFRM not set 0x%x\n",tmp);
  106. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, (tmp & (~ACCTL_ESYN)) | ACCTL_VFRM );
  107. msleep(50);
  108. tmp = snd_cs46xx_peekBA0(chip, BA0_ACCTL + offset);
  109. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, tmp | ACCTL_ESYN | ACCTL_VFRM );
  110. }
  111. /*
  112. * Setup the AC97 control registers on the CS461x to send the
  113. * appropriate command to the AC97 to perform the read.
  114. * ACCAD = Command Address Register = 46Ch
  115. * ACCDA = Command Data Register = 470h
  116. * ACCTL = Control Register = 460h
  117. * set DCV - will clear when process completed
  118. * set CRW - Read command
  119. * set VFRM - valid frame enabled
  120. * set ESYN - ASYNC generation enabled
  121. * set RSTN - ARST# inactive, AC97 codec not reset
  122. */
  123. snd_cs46xx_pokeBA0(chip, BA0_ACCAD, reg);
  124. snd_cs46xx_pokeBA0(chip, BA0_ACCDA, 0);
  125. if (codec_index == CS46XX_PRIMARY_CODEC_INDEX) {
  126. snd_cs46xx_pokeBA0(chip, BA0_ACCTL,/* clear ACCTL_DCV */ ACCTL_CRW |
  127. ACCTL_VFRM | ACCTL_ESYN |
  128. ACCTL_RSTN);
  129. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_CRW |
  130. ACCTL_VFRM | ACCTL_ESYN |
  131. ACCTL_RSTN);
  132. } else {
  133. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_TC |
  134. ACCTL_CRW | ACCTL_VFRM | ACCTL_ESYN |
  135. ACCTL_RSTN);
  136. }
  137. /*
  138. * Wait for the read to occur.
  139. */
  140. for (count = 0; count < 1000; count++) {
  141. /*
  142. * First, we want to wait for a short time.
  143. */
  144. udelay(10);
  145. /*
  146. * Now, check to see if the read has completed.
  147. * ACCTL = 460h, DCV should be reset by now and 460h = 17h
  148. */
  149. if (!(snd_cs46xx_peekBA0(chip, BA0_ACCTL) & ACCTL_DCV))
  150. goto ok1;
  151. }
  152. snd_printk(KERN_ERR "AC'97 read problem (ACCTL_DCV), reg = 0x%x\n", reg);
  153. result = 0xffff;
  154. goto end;
  155. ok1:
  156. /*
  157. * Wait for the valid status bit to go active.
  158. */
  159. for (count = 0; count < 100; count++) {
  160. /*
  161. * Read the AC97 status register.
  162. * ACSTS = Status Register = 464h
  163. * VSTS - Valid Status
  164. */
  165. if (snd_cs46xx_peekBA0(chip, BA0_ACSTS + offset) & ACSTS_VSTS)
  166. goto ok2;
  167. udelay(10);
  168. }
  169. snd_printk(KERN_ERR "AC'97 read problem (ACSTS_VSTS), codec_index %d, reg = 0x%x\n", codec_index, reg);
  170. result = 0xffff;
  171. goto end;
  172. ok2:
  173. /*
  174. * Read the data returned from the AC97 register.
  175. * ACSDA = Status Data Register = 474h
  176. */
  177. #if 0
  178. printk(KERN_DEBUG "e) reg = 0x%x, val = 0x%x, BA0_ACCAD = 0x%x\n", reg,
  179. snd_cs46xx_peekBA0(chip, BA0_ACSDA),
  180. snd_cs46xx_peekBA0(chip, BA0_ACCAD));
  181. #endif
  182. //snd_cs46xx_peekBA0(chip, BA0_ACCAD);
  183. result = snd_cs46xx_peekBA0(chip, BA0_ACSDA + offset);
  184. end:
  185. chip->active_ctrl(chip, -1);
  186. return result;
  187. }
  188. static unsigned short snd_cs46xx_ac97_read(struct snd_ac97 * ac97,
  189. unsigned short reg)
  190. {
  191. struct snd_cs46xx *chip = ac97->private_data;
  192. unsigned short val;
  193. int codec_index = ac97->num;
  194. if (snd_BUG_ON(codec_index != CS46XX_PRIMARY_CODEC_INDEX &&
  195. codec_index != CS46XX_SECONDARY_CODEC_INDEX))
  196. return 0xffff;
  197. val = snd_cs46xx_codec_read(chip, reg, codec_index);
  198. return val;
  199. }
  200. static void snd_cs46xx_codec_write(struct snd_cs46xx *chip,
  201. unsigned short reg,
  202. unsigned short val,
  203. int codec_index)
  204. {
  205. int count;
  206. if (snd_BUG_ON(codec_index != CS46XX_PRIMARY_CODEC_INDEX &&
  207. codec_index != CS46XX_SECONDARY_CODEC_INDEX))
  208. return;
  209. chip->active_ctrl(chip, 1);
  210. /*
  211. * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
  212. * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
  213. * 3. Write ACCTL = Control Register = 460h for initiating the write
  214. * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
  215. * 5. if DCV not cleared, break and return error
  216. */
  217. /*
  218. * Setup the AC97 control registers on the CS461x to send the
  219. * appropriate command to the AC97 to perform the read.
  220. * ACCAD = Command Address Register = 46Ch
  221. * ACCDA = Command Data Register = 470h
  222. * ACCTL = Control Register = 460h
  223. * set DCV - will clear when process completed
  224. * reset CRW - Write command
  225. * set VFRM - valid frame enabled
  226. * set ESYN - ASYNC generation enabled
  227. * set RSTN - ARST# inactive, AC97 codec not reset
  228. */
  229. snd_cs46xx_pokeBA0(chip, BA0_ACCAD , reg);
  230. snd_cs46xx_pokeBA0(chip, BA0_ACCDA , val);
  231. snd_cs46xx_peekBA0(chip, BA0_ACCTL);
  232. if (codec_index == CS46XX_PRIMARY_CODEC_INDEX) {
  233. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, /* clear ACCTL_DCV */ ACCTL_VFRM |
  234. ACCTL_ESYN | ACCTL_RSTN);
  235. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_VFRM |
  236. ACCTL_ESYN | ACCTL_RSTN);
  237. } else {
  238. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_TC |
  239. ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
  240. }
  241. for (count = 0; count < 4000; count++) {
  242. /*
  243. * First, we want to wait for a short time.
  244. */
  245. udelay(10);
  246. /*
  247. * Now, check to see if the write has completed.
  248. * ACCTL = 460h, DCV should be reset by now and 460h = 07h
  249. */
  250. if (!(snd_cs46xx_peekBA0(chip, BA0_ACCTL) & ACCTL_DCV)) {
  251. goto end;
  252. }
  253. }
  254. snd_printk(KERN_ERR "AC'97 write problem, codec_index = %d, reg = 0x%x, val = 0x%x\n", codec_index, reg, val);
  255. end:
  256. chip->active_ctrl(chip, -1);
  257. }
  258. static void snd_cs46xx_ac97_write(struct snd_ac97 *ac97,
  259. unsigned short reg,
  260. unsigned short val)
  261. {
  262. struct snd_cs46xx *chip = ac97->private_data;
  263. int codec_index = ac97->num;
  264. if (snd_BUG_ON(codec_index != CS46XX_PRIMARY_CODEC_INDEX &&
  265. codec_index != CS46XX_SECONDARY_CODEC_INDEX))
  266. return;
  267. snd_cs46xx_codec_write(chip, reg, val, codec_index);
  268. }
  269. /*
  270. * Chip initialization
  271. */
  272. int snd_cs46xx_download(struct snd_cs46xx *chip,
  273. u32 *src,
  274. unsigned long offset,
  275. unsigned long len)
  276. {
  277. void __iomem *dst;
  278. unsigned int bank = offset >> 16;
  279. offset = offset & 0xffff;
  280. if (snd_BUG_ON((offset & 3) || (len & 3)))
  281. return -EINVAL;
  282. dst = chip->region.idx[bank+1].remap_addr + offset;
  283. len /= sizeof(u32);
  284. /* writel already converts 32-bit value to right endianess */
  285. while (len-- > 0) {
  286. writel(*src++, dst);
  287. dst += sizeof(u32);
  288. }
  289. return 0;
  290. }
  291. static inline void memcpy_le32(void *dst, const void *src, unsigned int len)
  292. {
  293. #ifdef __LITTLE_ENDIAN
  294. memcpy(dst, src, len);
  295. #else
  296. u32 *_dst = dst;
  297. const __le32 *_src = src;
  298. len /= 4;
  299. while (len-- > 0)
  300. *_dst++ = le32_to_cpu(*_src++);
  301. #endif
  302. }
  303. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  304. static const char *module_names[CS46XX_DSP_MODULES] = {
  305. "cwc4630", "cwcasync", "cwcsnoop", "cwcbinhack", "cwcdma"
  306. };
  307. MODULE_FIRMWARE("cs46xx/cwc4630");
  308. MODULE_FIRMWARE("cs46xx/cwcasync");
  309. MODULE_FIRMWARE("cs46xx/cwcsnoop");
  310. MODULE_FIRMWARE("cs46xx/cwcbinhack");
  311. MODULE_FIRMWARE("cs46xx/cwcdma");
  312. static void free_module_desc(struct dsp_module_desc *module)
  313. {
  314. if (!module)
  315. return;
  316. kfree(module->module_name);
  317. kfree(module->symbol_table.symbols);
  318. if (module->segments) {
  319. int i;
  320. for (i = 0; i < module->nsegments; i++)
  321. kfree(module->segments[i].data);
  322. kfree(module->segments);
  323. }
  324. kfree(module);
  325. }
  326. /* firmware binary format:
  327. * le32 nsymbols;
  328. * struct {
  329. * le32 address;
  330. * char symbol_name[DSP_MAX_SYMBOL_NAME];
  331. * le32 symbol_type;
  332. * } symbols[nsymbols];
  333. * le32 nsegments;
  334. * struct {
  335. * le32 segment_type;
  336. * le32 offset;
  337. * le32 size;
  338. * le32 data[size];
  339. * } segments[nsegments];
  340. */
  341. static int load_firmware(struct snd_cs46xx *chip,
  342. struct dsp_module_desc **module_ret,
  343. const char *fw_name)
  344. {
  345. int i, err;
  346. unsigned int nums, fwlen, fwsize;
  347. const __le32 *fwdat;
  348. struct dsp_module_desc *module = NULL;
  349. const struct firmware *fw;
  350. char fw_path[32];
  351. sprintf(fw_path, "cs46xx/%s", fw_name);
  352. err = request_firmware(&fw, fw_path, &chip->pci->dev);
  353. if (err < 0)
  354. return err;
  355. fwsize = fw->size / 4;
  356. if (fwsize < 2) {
  357. err = -EINVAL;
  358. goto error;
  359. }
  360. err = -ENOMEM;
  361. module = kzalloc(sizeof(*module), GFP_KERNEL);
  362. if (!module)
  363. goto error;
  364. module->module_name = kstrdup(fw_name, GFP_KERNEL);
  365. if (!module->module_name)
  366. goto error;
  367. fwlen = 0;
  368. fwdat = (const __le32 *)fw->data;
  369. nums = module->symbol_table.nsymbols = le32_to_cpu(fwdat[fwlen++]);
  370. if (nums >= 40)
  371. goto error_inval;
  372. module->symbol_table.symbols =
  373. kcalloc(nums, sizeof(struct dsp_symbol_entry), GFP_KERNEL);
  374. if (!module->symbol_table.symbols)
  375. goto error;
  376. for (i = 0; i < nums; i++) {
  377. struct dsp_symbol_entry *entry =
  378. &module->symbol_table.symbols[i];
  379. if (fwlen + 2 + DSP_MAX_SYMBOL_NAME / 4 > fwsize)
  380. goto error_inval;
  381. entry->address = le32_to_cpu(fwdat[fwlen++]);
  382. memcpy(entry->symbol_name, &fwdat[fwlen], DSP_MAX_SYMBOL_NAME - 1);
  383. fwlen += DSP_MAX_SYMBOL_NAME / 4;
  384. entry->symbol_type = le32_to_cpu(fwdat[fwlen++]);
  385. }
  386. if (fwlen >= fwsize)
  387. goto error_inval;
  388. nums = module->nsegments = le32_to_cpu(fwdat[fwlen++]);
  389. if (nums > 10)
  390. goto error_inval;
  391. module->segments =
  392. kcalloc(nums, sizeof(struct dsp_segment_desc), GFP_KERNEL);
  393. if (!module->segments)
  394. goto error;
  395. for (i = 0; i < nums; i++) {
  396. struct dsp_segment_desc *entry = &module->segments[i];
  397. if (fwlen + 3 > fwsize)
  398. goto error_inval;
  399. entry->segment_type = le32_to_cpu(fwdat[fwlen++]);
  400. entry->offset = le32_to_cpu(fwdat[fwlen++]);
  401. entry->size = le32_to_cpu(fwdat[fwlen++]);
  402. if (fwlen + entry->size > fwsize)
  403. goto error_inval;
  404. entry->data = kmalloc(entry->size * 4, GFP_KERNEL);
  405. if (!entry->data)
  406. goto error;
  407. memcpy_le32(entry->data, &fwdat[fwlen], entry->size * 4);
  408. fwlen += entry->size;
  409. }
  410. *module_ret = module;
  411. release_firmware(fw);
  412. return 0;
  413. error_inval:
  414. err = -EINVAL;
  415. error:
  416. free_module_desc(module);
  417. release_firmware(fw);
  418. return err;
  419. }
  420. int snd_cs46xx_clear_BA1(struct snd_cs46xx *chip,
  421. unsigned long offset,
  422. unsigned long len)
  423. {
  424. void __iomem *dst;
  425. unsigned int bank = offset >> 16;
  426. offset = offset & 0xffff;
  427. if (snd_BUG_ON((offset & 3) || (len & 3)))
  428. return -EINVAL;
  429. dst = chip->region.idx[bank+1].remap_addr + offset;
  430. len /= sizeof(u32);
  431. /* writel already converts 32-bit value to right endianess */
  432. while (len-- > 0) {
  433. writel(0, dst);
  434. dst += sizeof(u32);
  435. }
  436. return 0;
  437. }
  438. #else /* old DSP image */
  439. struct ba1_struct {
  440. struct {
  441. u32 offset;
  442. u32 size;
  443. } memory[BA1_MEMORY_COUNT];
  444. u32 map[BA1_DWORD_SIZE];
  445. };
  446. MODULE_FIRMWARE("cs46xx/ba1");
  447. static int load_firmware(struct snd_cs46xx *chip)
  448. {
  449. const struct firmware *fw;
  450. int i, size, err;
  451. err = request_firmware(&fw, "cs46xx/ba1", &chip->pci->dev);
  452. if (err < 0)
  453. return err;
  454. if (fw->size != sizeof(*chip->ba1)) {
  455. err = -EINVAL;
  456. goto error;
  457. }
  458. chip->ba1 = vmalloc(sizeof(*chip->ba1));
  459. if (!chip->ba1) {
  460. err = -ENOMEM;
  461. goto error;
  462. }
  463. memcpy_le32(chip->ba1, fw->data, sizeof(*chip->ba1));
  464. /* sanity check */
  465. size = 0;
  466. for (i = 0; i < BA1_MEMORY_COUNT; i++)
  467. size += chip->ba1->memory[i].size;
  468. if (size > BA1_DWORD_SIZE * 4)
  469. err = -EINVAL;
  470. error:
  471. release_firmware(fw);
  472. return err;
  473. }
  474. int snd_cs46xx_download_image(struct snd_cs46xx *chip)
  475. {
  476. int idx, err;
  477. unsigned int offset = 0;
  478. struct ba1_struct *ba1 = chip->ba1;
  479. for (idx = 0; idx < BA1_MEMORY_COUNT; idx++) {
  480. err = snd_cs46xx_download(chip,
  481. &ba1->map[offset],
  482. ba1->memory[idx].offset,
  483. ba1->memory[idx].size);
  484. if (err < 0)
  485. return err;
  486. offset += ba1->memory[idx].size >> 2;
  487. }
  488. return 0;
  489. }
  490. #endif /* CONFIG_SND_CS46XX_NEW_DSP */
  491. /*
  492. * Chip reset
  493. */
  494. static void snd_cs46xx_reset(struct snd_cs46xx *chip)
  495. {
  496. int idx;
  497. /*
  498. * Write the reset bit of the SP control register.
  499. */
  500. snd_cs46xx_poke(chip, BA1_SPCR, SPCR_RSTSP);
  501. /*
  502. * Write the control register.
  503. */
  504. snd_cs46xx_poke(chip, BA1_SPCR, SPCR_DRQEN);
  505. /*
  506. * Clear the trap registers.
  507. */
  508. for (idx = 0; idx < 8; idx++) {
  509. snd_cs46xx_poke(chip, BA1_DREG, DREG_REGID_TRAP_SELECT + idx);
  510. snd_cs46xx_poke(chip, BA1_TWPR, 0xFFFF);
  511. }
  512. snd_cs46xx_poke(chip, BA1_DREG, 0);
  513. /*
  514. * Set the frame timer to reflect the number of cycles per frame.
  515. */
  516. snd_cs46xx_poke(chip, BA1_FRMT, 0xadf);
  517. }
  518. static int cs46xx_wait_for_fifo(struct snd_cs46xx * chip,int retry_timeout)
  519. {
  520. u32 i, status = 0;
  521. /*
  522. * Make sure the previous FIFO write operation has completed.
  523. */
  524. for(i = 0; i < 50; i++){
  525. status = snd_cs46xx_peekBA0(chip, BA0_SERBST);
  526. if( !(status & SERBST_WBSY) )
  527. break;
  528. mdelay(retry_timeout);
  529. }
  530. if(status & SERBST_WBSY) {
  531. snd_printk(KERN_ERR "cs46xx: failure waiting for "
  532. "FIFO command to complete\n");
  533. return -EINVAL;
  534. }
  535. return 0;
  536. }
  537. static void snd_cs46xx_clear_serial_FIFOs(struct snd_cs46xx *chip)
  538. {
  539. int idx, powerdown = 0;
  540. unsigned int tmp;
  541. /*
  542. * See if the devices are powered down. If so, we must power them up first
  543. * or they will not respond.
  544. */
  545. tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1);
  546. if (!(tmp & CLKCR1_SWCE)) {
  547. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp | CLKCR1_SWCE);
  548. powerdown = 1;
  549. }
  550. /*
  551. * We want to clear out the serial port FIFOs so we don't end up playing
  552. * whatever random garbage happens to be in them. We fill the sample FIFOS
  553. * with zero (silence).
  554. */
  555. snd_cs46xx_pokeBA0(chip, BA0_SERBWP, 0);
  556. /*
  557. * Fill all 256 sample FIFO locations.
  558. */
  559. for (idx = 0; idx < 0xFF; idx++) {
  560. /*
  561. * Make sure the previous FIFO write operation has completed.
  562. */
  563. if (cs46xx_wait_for_fifo(chip,1)) {
  564. snd_printdd ("failed waiting for FIFO at addr (%02X)\n",idx);
  565. if (powerdown)
  566. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
  567. break;
  568. }
  569. /*
  570. * Write the serial port FIFO index.
  571. */
  572. snd_cs46xx_pokeBA0(chip, BA0_SERBAD, idx);
  573. /*
  574. * Tell the serial port to load the new value into the FIFO location.
  575. */
  576. snd_cs46xx_pokeBA0(chip, BA0_SERBCM, SERBCM_WRC);
  577. }
  578. /*
  579. * Now, if we powered up the devices, then power them back down again.
  580. * This is kinda ugly, but should never happen.
  581. */
  582. if (powerdown)
  583. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
  584. }
  585. static void snd_cs46xx_proc_start(struct snd_cs46xx *chip)
  586. {
  587. int cnt;
  588. /*
  589. * Set the frame timer to reflect the number of cycles per frame.
  590. */
  591. snd_cs46xx_poke(chip, BA1_FRMT, 0xadf);
  592. /*
  593. * Turn on the run, run at frame, and DMA enable bits in the local copy of
  594. * the SP control register.
  595. */
  596. snd_cs46xx_poke(chip, BA1_SPCR, SPCR_RUN | SPCR_RUNFR | SPCR_DRQEN);
  597. /*
  598. * Wait until the run at frame bit resets itself in the SP control
  599. * register.
  600. */
  601. for (cnt = 0; cnt < 25; cnt++) {
  602. udelay(50);
  603. if (!(snd_cs46xx_peek(chip, BA1_SPCR) & SPCR_RUNFR))
  604. break;
  605. }
  606. if (snd_cs46xx_peek(chip, BA1_SPCR) & SPCR_RUNFR)
  607. snd_printk(KERN_ERR "SPCR_RUNFR never reset\n");
  608. }
  609. static void snd_cs46xx_proc_stop(struct snd_cs46xx *chip)
  610. {
  611. /*
  612. * Turn off the run, run at frame, and DMA enable bits in the local copy of
  613. * the SP control register.
  614. */
  615. snd_cs46xx_poke(chip, BA1_SPCR, 0);
  616. }
  617. /*
  618. * Sample rate routines
  619. */
  620. #define GOF_PER_SEC 200
  621. static void snd_cs46xx_set_play_sample_rate(struct snd_cs46xx *chip, unsigned int rate)
  622. {
  623. unsigned long flags;
  624. unsigned int tmp1, tmp2;
  625. unsigned int phiIncr;
  626. unsigned int correctionPerGOF, correctionPerSec;
  627. /*
  628. * Compute the values used to drive the actual sample rate conversion.
  629. * The following formulas are being computed, using inline assembly
  630. * since we need to use 64 bit arithmetic to compute the values:
  631. *
  632. * phiIncr = floor((Fs,in * 2^26) / Fs,out)
  633. * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
  634. * GOF_PER_SEC)
  635. * ulCorrectionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -M
  636. * GOF_PER_SEC * correctionPerGOF
  637. *
  638. * i.e.
  639. *
  640. * phiIncr:other = dividend:remainder((Fs,in * 2^26) / Fs,out)
  641. * correctionPerGOF:correctionPerSec =
  642. * dividend:remainder(ulOther / GOF_PER_SEC)
  643. */
  644. tmp1 = rate << 16;
  645. phiIncr = tmp1 / 48000;
  646. tmp1 -= phiIncr * 48000;
  647. tmp1 <<= 10;
  648. phiIncr <<= 10;
  649. tmp2 = tmp1 / 48000;
  650. phiIncr += tmp2;
  651. tmp1 -= tmp2 * 48000;
  652. correctionPerGOF = tmp1 / GOF_PER_SEC;
  653. tmp1 -= correctionPerGOF * GOF_PER_SEC;
  654. correctionPerSec = tmp1;
  655. /*
  656. * Fill in the SampleRateConverter control block.
  657. */
  658. spin_lock_irqsave(&chip->reg_lock, flags);
  659. snd_cs46xx_poke(chip, BA1_PSRC,
  660. ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
  661. snd_cs46xx_poke(chip, BA1_PPI, phiIncr);
  662. spin_unlock_irqrestore(&chip->reg_lock, flags);
  663. }
  664. static void snd_cs46xx_set_capture_sample_rate(struct snd_cs46xx *chip, unsigned int rate)
  665. {
  666. unsigned long flags;
  667. unsigned int phiIncr, coeffIncr, tmp1, tmp2;
  668. unsigned int correctionPerGOF, correctionPerSec, initialDelay;
  669. unsigned int frameGroupLength, cnt;
  670. /*
  671. * We can only decimate by up to a factor of 1/9th the hardware rate.
  672. * Correct the value if an attempt is made to stray outside that limit.
  673. */
  674. if ((rate * 9) < 48000)
  675. rate = 48000 / 9;
  676. /*
  677. * We can not capture at at rate greater than the Input Rate (48000).
  678. * Return an error if an attempt is made to stray outside that limit.
  679. */
  680. if (rate > 48000)
  681. rate = 48000;
  682. /*
  683. * Compute the values used to drive the actual sample rate conversion.
  684. * The following formulas are being computed, using inline assembly
  685. * since we need to use 64 bit arithmetic to compute the values:
  686. *
  687. * coeffIncr = -floor((Fs,out * 2^23) / Fs,in)
  688. * phiIncr = floor((Fs,in * 2^26) / Fs,out)
  689. * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
  690. * GOF_PER_SEC)
  691. * correctionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -
  692. * GOF_PER_SEC * correctionPerGOF
  693. * initialDelay = ceil((24 * Fs,in) / Fs,out)
  694. *
  695. * i.e.
  696. *
  697. * coeffIncr = neg(dividend((Fs,out * 2^23) / Fs,in))
  698. * phiIncr:ulOther = dividend:remainder((Fs,in * 2^26) / Fs,out)
  699. * correctionPerGOF:correctionPerSec =
  700. * dividend:remainder(ulOther / GOF_PER_SEC)
  701. * initialDelay = dividend(((24 * Fs,in) + Fs,out - 1) / Fs,out)
  702. */
  703. tmp1 = rate << 16;
  704. coeffIncr = tmp1 / 48000;
  705. tmp1 -= coeffIncr * 48000;
  706. tmp1 <<= 7;
  707. coeffIncr <<= 7;
  708. coeffIncr += tmp1 / 48000;
  709. coeffIncr ^= 0xFFFFFFFF;
  710. coeffIncr++;
  711. tmp1 = 48000 << 16;
  712. phiIncr = tmp1 / rate;
  713. tmp1 -= phiIncr * rate;
  714. tmp1 <<= 10;
  715. phiIncr <<= 10;
  716. tmp2 = tmp1 / rate;
  717. phiIncr += tmp2;
  718. tmp1 -= tmp2 * rate;
  719. correctionPerGOF = tmp1 / GOF_PER_SEC;
  720. tmp1 -= correctionPerGOF * GOF_PER_SEC;
  721. correctionPerSec = tmp1;
  722. initialDelay = ((48000 * 24) + rate - 1) / rate;
  723. /*
  724. * Fill in the VariDecimate control block.
  725. */
  726. spin_lock_irqsave(&chip->reg_lock, flags);
  727. snd_cs46xx_poke(chip, BA1_CSRC,
  728. ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
  729. snd_cs46xx_poke(chip, BA1_CCI, coeffIncr);
  730. snd_cs46xx_poke(chip, BA1_CD,
  731. (((BA1_VARIDEC_BUF_1 + (initialDelay << 2)) << 16) & 0xFFFF0000) | 0x80);
  732. snd_cs46xx_poke(chip, BA1_CPI, phiIncr);
  733. spin_unlock_irqrestore(&chip->reg_lock, flags);
  734. /*
  735. * Figure out the frame group length for the write back task. Basically,
  736. * this is just the factors of 24000 (2^6*3*5^3) that are not present in
  737. * the output sample rate.
  738. */
  739. frameGroupLength = 1;
  740. for (cnt = 2; cnt <= 64; cnt *= 2) {
  741. if (((rate / cnt) * cnt) != rate)
  742. frameGroupLength *= 2;
  743. }
  744. if (((rate / 3) * 3) != rate) {
  745. frameGroupLength *= 3;
  746. }
  747. for (cnt = 5; cnt <= 125; cnt *= 5) {
  748. if (((rate / cnt) * cnt) != rate)
  749. frameGroupLength *= 5;
  750. }
  751. /*
  752. * Fill in the WriteBack control block.
  753. */
  754. spin_lock_irqsave(&chip->reg_lock, flags);
  755. snd_cs46xx_poke(chip, BA1_CFG1, frameGroupLength);
  756. snd_cs46xx_poke(chip, BA1_CFG2, (0x00800000 | frameGroupLength));
  757. snd_cs46xx_poke(chip, BA1_CCST, 0x0000FFFF);
  758. snd_cs46xx_poke(chip, BA1_CSPB, ((65536 * rate) / 24000));
  759. snd_cs46xx_poke(chip, (BA1_CSPB + 4), 0x0000FFFF);
  760. spin_unlock_irqrestore(&chip->reg_lock, flags);
  761. }
  762. /*
  763. * PCM part
  764. */
  765. static void snd_cs46xx_pb_trans_copy(struct snd_pcm_substream *substream,
  766. struct snd_pcm_indirect *rec, size_t bytes)
  767. {
  768. struct snd_pcm_runtime *runtime = substream->runtime;
  769. struct snd_cs46xx_pcm * cpcm = runtime->private_data;
  770. memcpy(cpcm->hw_buf.area + rec->hw_data, runtime->dma_area + rec->sw_data, bytes);
  771. }
  772. static int snd_cs46xx_playback_transfer(struct snd_pcm_substream *substream)
  773. {
  774. struct snd_pcm_runtime *runtime = substream->runtime;
  775. struct snd_cs46xx_pcm * cpcm = runtime->private_data;
  776. snd_pcm_indirect_playback_transfer(substream, &cpcm->pcm_rec, snd_cs46xx_pb_trans_copy);
  777. return 0;
  778. }
  779. static void snd_cs46xx_cp_trans_copy(struct snd_pcm_substream *substream,
  780. struct snd_pcm_indirect *rec, size_t bytes)
  781. {
  782. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  783. struct snd_pcm_runtime *runtime = substream->runtime;
  784. memcpy(runtime->dma_area + rec->sw_data,
  785. chip->capt.hw_buf.area + rec->hw_data, bytes);
  786. }
  787. static int snd_cs46xx_capture_transfer(struct snd_pcm_substream *substream)
  788. {
  789. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  790. snd_pcm_indirect_capture_transfer(substream, &chip->capt.pcm_rec, snd_cs46xx_cp_trans_copy);
  791. return 0;
  792. }
  793. static snd_pcm_uframes_t snd_cs46xx_playback_direct_pointer(struct snd_pcm_substream *substream)
  794. {
  795. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  796. size_t ptr;
  797. struct snd_cs46xx_pcm *cpcm = substream->runtime->private_data;
  798. if (snd_BUG_ON(!cpcm->pcm_channel))
  799. return -ENXIO;
  800. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  801. ptr = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 2) << 2);
  802. #else
  803. ptr = snd_cs46xx_peek(chip, BA1_PBA);
  804. #endif
  805. ptr -= cpcm->hw_buf.addr;
  806. return ptr >> cpcm->shift;
  807. }
  808. static snd_pcm_uframes_t snd_cs46xx_playback_indirect_pointer(struct snd_pcm_substream *substream)
  809. {
  810. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  811. size_t ptr;
  812. struct snd_cs46xx_pcm *cpcm = substream->runtime->private_data;
  813. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  814. if (snd_BUG_ON(!cpcm->pcm_channel))
  815. return -ENXIO;
  816. ptr = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 2) << 2);
  817. #else
  818. ptr = snd_cs46xx_peek(chip, BA1_PBA);
  819. #endif
  820. ptr -= cpcm->hw_buf.addr;
  821. return snd_pcm_indirect_playback_pointer(substream, &cpcm->pcm_rec, ptr);
  822. }
  823. static snd_pcm_uframes_t snd_cs46xx_capture_direct_pointer(struct snd_pcm_substream *substream)
  824. {
  825. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  826. size_t ptr = snd_cs46xx_peek(chip, BA1_CBA) - chip->capt.hw_buf.addr;
  827. return ptr >> chip->capt.shift;
  828. }
  829. static snd_pcm_uframes_t snd_cs46xx_capture_indirect_pointer(struct snd_pcm_substream *substream)
  830. {
  831. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  832. size_t ptr = snd_cs46xx_peek(chip, BA1_CBA) - chip->capt.hw_buf.addr;
  833. return snd_pcm_indirect_capture_pointer(substream, &chip->capt.pcm_rec, ptr);
  834. }
  835. static int snd_cs46xx_playback_trigger(struct snd_pcm_substream *substream,
  836. int cmd)
  837. {
  838. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  839. /*struct snd_pcm_runtime *runtime = substream->runtime;*/
  840. int result = 0;
  841. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  842. struct snd_cs46xx_pcm *cpcm = substream->runtime->private_data;
  843. if (! cpcm->pcm_channel) {
  844. return -ENXIO;
  845. }
  846. #endif
  847. switch (cmd) {
  848. case SNDRV_PCM_TRIGGER_START:
  849. case SNDRV_PCM_TRIGGER_RESUME:
  850. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  851. /* magic value to unmute PCM stream playback volume */
  852. snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address +
  853. SCBVolumeCtrl) << 2, 0x80008000);
  854. if (cpcm->pcm_channel->unlinked)
  855. cs46xx_dsp_pcm_link(chip,cpcm->pcm_channel);
  856. if (substream->runtime->periods != CS46XX_FRAGS)
  857. snd_cs46xx_playback_transfer(substream);
  858. #else
  859. spin_lock(&chip->reg_lock);
  860. if (substream->runtime->periods != CS46XX_FRAGS)
  861. snd_cs46xx_playback_transfer(substream);
  862. { unsigned int tmp;
  863. tmp = snd_cs46xx_peek(chip, BA1_PCTL);
  864. tmp &= 0x0000ffff;
  865. snd_cs46xx_poke(chip, BA1_PCTL, chip->play_ctl | tmp);
  866. }
  867. spin_unlock(&chip->reg_lock);
  868. #endif
  869. break;
  870. case SNDRV_PCM_TRIGGER_STOP:
  871. case SNDRV_PCM_TRIGGER_SUSPEND:
  872. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  873. /* magic mute channel */
  874. snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address +
  875. SCBVolumeCtrl) << 2, 0xffffffff);
  876. if (!cpcm->pcm_channel->unlinked)
  877. cs46xx_dsp_pcm_unlink(chip,cpcm->pcm_channel);
  878. #else
  879. spin_lock(&chip->reg_lock);
  880. { unsigned int tmp;
  881. tmp = snd_cs46xx_peek(chip, BA1_PCTL);
  882. tmp &= 0x0000ffff;
  883. snd_cs46xx_poke(chip, BA1_PCTL, tmp);
  884. }
  885. spin_unlock(&chip->reg_lock);
  886. #endif
  887. break;
  888. default:
  889. result = -EINVAL;
  890. break;
  891. }
  892. return result;
  893. }
  894. static int snd_cs46xx_capture_trigger(struct snd_pcm_substream *substream,
  895. int cmd)
  896. {
  897. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  898. unsigned int tmp;
  899. int result = 0;
  900. spin_lock(&chip->reg_lock);
  901. switch (cmd) {
  902. case SNDRV_PCM_TRIGGER_START:
  903. case SNDRV_PCM_TRIGGER_RESUME:
  904. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  905. tmp &= 0xffff0000;
  906. snd_cs46xx_poke(chip, BA1_CCTL, chip->capt.ctl | tmp);
  907. break;
  908. case SNDRV_PCM_TRIGGER_STOP:
  909. case SNDRV_PCM_TRIGGER_SUSPEND:
  910. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  911. tmp &= 0xffff0000;
  912. snd_cs46xx_poke(chip, BA1_CCTL, tmp);
  913. break;
  914. default:
  915. result = -EINVAL;
  916. break;
  917. }
  918. spin_unlock(&chip->reg_lock);
  919. return result;
  920. }
  921. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  922. static int _cs46xx_adjust_sample_rate (struct snd_cs46xx *chip, struct snd_cs46xx_pcm *cpcm,
  923. int sample_rate)
  924. {
  925. /* If PCMReaderSCB and SrcTaskSCB not created yet ... */
  926. if ( cpcm->pcm_channel == NULL) {
  927. cpcm->pcm_channel = cs46xx_dsp_create_pcm_channel (chip, sample_rate,
  928. cpcm, cpcm->hw_buf.addr,cpcm->pcm_channel_id);
  929. if (cpcm->pcm_channel == NULL) {
  930. snd_printk(KERN_ERR "cs46xx: failed to create virtual PCM channel\n");
  931. return -ENOMEM;
  932. }
  933. cpcm->pcm_channel->sample_rate = sample_rate;
  934. } else
  935. /* if sample rate is changed */
  936. if ((int)cpcm->pcm_channel->sample_rate != sample_rate) {
  937. int unlinked = cpcm->pcm_channel->unlinked;
  938. cs46xx_dsp_destroy_pcm_channel (chip,cpcm->pcm_channel);
  939. if ( (cpcm->pcm_channel = cs46xx_dsp_create_pcm_channel (chip, sample_rate, cpcm,
  940. cpcm->hw_buf.addr,
  941. cpcm->pcm_channel_id)) == NULL) {
  942. snd_printk(KERN_ERR "cs46xx: failed to re-create virtual PCM channel\n");
  943. return -ENOMEM;
  944. }
  945. if (!unlinked) cs46xx_dsp_pcm_link (chip,cpcm->pcm_channel);
  946. cpcm->pcm_channel->sample_rate = sample_rate;
  947. }
  948. return 0;
  949. }
  950. #endif
  951. static int snd_cs46xx_playback_hw_params(struct snd_pcm_substream *substream,
  952. struct snd_pcm_hw_params *hw_params)
  953. {
  954. struct snd_pcm_runtime *runtime = substream->runtime;
  955. struct snd_cs46xx_pcm *cpcm;
  956. int err;
  957. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  958. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  959. int sample_rate = params_rate(hw_params);
  960. int period_size = params_period_bytes(hw_params);
  961. #endif
  962. cpcm = runtime->private_data;
  963. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  964. if (snd_BUG_ON(!sample_rate))
  965. return -ENXIO;
  966. mutex_lock(&chip->spos_mutex);
  967. if (_cs46xx_adjust_sample_rate (chip,cpcm,sample_rate)) {
  968. mutex_unlock(&chip->spos_mutex);
  969. return -ENXIO;
  970. }
  971. snd_BUG_ON(!cpcm->pcm_channel);
  972. if (!cpcm->pcm_channel) {
  973. mutex_unlock(&chip->spos_mutex);
  974. return -ENXIO;
  975. }
  976. if (cs46xx_dsp_pcm_channel_set_period (chip,cpcm->pcm_channel,period_size)) {
  977. mutex_unlock(&chip->spos_mutex);
  978. return -EINVAL;
  979. }
  980. snd_printdd ("period_size (%d), periods (%d) buffer_size(%d)\n",
  981. period_size, params_periods(hw_params),
  982. params_buffer_bytes(hw_params));
  983. #endif
  984. if (params_periods(hw_params) == CS46XX_FRAGS) {
  985. if (runtime->dma_area != cpcm->hw_buf.area)
  986. snd_pcm_lib_free_pages(substream);
  987. runtime->dma_area = cpcm->hw_buf.area;
  988. runtime->dma_addr = cpcm->hw_buf.addr;
  989. runtime->dma_bytes = cpcm->hw_buf.bytes;
  990. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  991. if (cpcm->pcm_channel_id == DSP_PCM_MAIN_CHANNEL) {
  992. substream->ops = &snd_cs46xx_playback_ops;
  993. } else if (cpcm->pcm_channel_id == DSP_PCM_REAR_CHANNEL) {
  994. substream->ops = &snd_cs46xx_playback_rear_ops;
  995. } else if (cpcm->pcm_channel_id == DSP_PCM_CENTER_LFE_CHANNEL) {
  996. substream->ops = &snd_cs46xx_playback_clfe_ops;
  997. } else if (cpcm->pcm_channel_id == DSP_IEC958_CHANNEL) {
  998. substream->ops = &snd_cs46xx_playback_iec958_ops;
  999. } else {
  1000. snd_BUG();
  1001. }
  1002. #else
  1003. substream->ops = &snd_cs46xx_playback_ops;
  1004. #endif
  1005. } else {
  1006. if (runtime->dma_area == cpcm->hw_buf.area) {
  1007. runtime->dma_area = NULL;
  1008. runtime->dma_addr = 0;
  1009. runtime->dma_bytes = 0;
  1010. }
  1011. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0) {
  1012. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1013. mutex_unlock(&chip->spos_mutex);
  1014. #endif
  1015. return err;
  1016. }
  1017. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1018. if (cpcm->pcm_channel_id == DSP_PCM_MAIN_CHANNEL) {
  1019. substream->ops = &snd_cs46xx_playback_indirect_ops;
  1020. } else if (cpcm->pcm_channel_id == DSP_PCM_REAR_CHANNEL) {
  1021. substream->ops = &snd_cs46xx_playback_indirect_rear_ops;
  1022. } else if (cpcm->pcm_channel_id == DSP_PCM_CENTER_LFE_CHANNEL) {
  1023. substream->ops = &snd_cs46xx_playback_indirect_clfe_ops;
  1024. } else if (cpcm->pcm_channel_id == DSP_IEC958_CHANNEL) {
  1025. substream->ops = &snd_cs46xx_playback_indirect_iec958_ops;
  1026. } else {
  1027. snd_BUG();
  1028. }
  1029. #else
  1030. substream->ops = &snd_cs46xx_playback_indirect_ops;
  1031. #endif
  1032. }
  1033. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1034. mutex_unlock(&chip->spos_mutex);
  1035. #endif
  1036. return 0;
  1037. }
  1038. static int snd_cs46xx_playback_hw_free(struct snd_pcm_substream *substream)
  1039. {
  1040. /*struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);*/
  1041. struct snd_pcm_runtime *runtime = substream->runtime;
  1042. struct snd_cs46xx_pcm *cpcm;
  1043. cpcm = runtime->private_data;
  1044. /* if play_back open fails, then this function
  1045. is called and cpcm can actually be NULL here */
  1046. if (!cpcm) return -ENXIO;
  1047. if (runtime->dma_area != cpcm->hw_buf.area)
  1048. snd_pcm_lib_free_pages(substream);
  1049. runtime->dma_area = NULL;
  1050. runtime->dma_addr = 0;
  1051. runtime->dma_bytes = 0;
  1052. return 0;
  1053. }
  1054. static int snd_cs46xx_playback_prepare(struct snd_pcm_substream *substream)
  1055. {
  1056. unsigned int tmp;
  1057. unsigned int pfie;
  1058. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1059. struct snd_pcm_runtime *runtime = substream->runtime;
  1060. struct snd_cs46xx_pcm *cpcm;
  1061. cpcm = runtime->private_data;
  1062. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1063. if (snd_BUG_ON(!cpcm->pcm_channel))
  1064. return -ENXIO;
  1065. pfie = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 1) << 2 );
  1066. pfie &= ~0x0000f03f;
  1067. #else
  1068. /* old dsp */
  1069. pfie = snd_cs46xx_peek(chip, BA1_PFIE);
  1070. pfie &= ~0x0000f03f;
  1071. #endif
  1072. cpcm->shift = 2;
  1073. /* if to convert from stereo to mono */
  1074. if (runtime->channels == 1) {
  1075. cpcm->shift--;
  1076. pfie |= 0x00002000;
  1077. }
  1078. /* if to convert from 8 bit to 16 bit */
  1079. if (snd_pcm_format_width(runtime->format) == 8) {
  1080. cpcm->shift--;
  1081. pfie |= 0x00001000;
  1082. }
  1083. /* if to convert to unsigned */
  1084. if (snd_pcm_format_unsigned(runtime->format))
  1085. pfie |= 0x00008000;
  1086. /* Never convert byte order when sample stream is 8 bit */
  1087. if (snd_pcm_format_width(runtime->format) != 8) {
  1088. /* convert from big endian to little endian */
  1089. if (snd_pcm_format_big_endian(runtime->format))
  1090. pfie |= 0x00004000;
  1091. }
  1092. memset(&cpcm->pcm_rec, 0, sizeof(cpcm->pcm_rec));
  1093. cpcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
  1094. cpcm->pcm_rec.hw_buffer_size = runtime->period_size * CS46XX_FRAGS << cpcm->shift;
  1095. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1096. tmp = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address) << 2);
  1097. tmp &= ~0x000003ff;
  1098. tmp |= (4 << cpcm->shift) - 1;
  1099. /* playback transaction count register */
  1100. snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address) << 2, tmp);
  1101. /* playback format && interrupt enable */
  1102. snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 1) << 2, pfie | cpcm->pcm_channel->pcm_slot);
  1103. #else
  1104. snd_cs46xx_poke(chip, BA1_PBA, cpcm->hw_buf.addr);
  1105. tmp = snd_cs46xx_peek(chip, BA1_PDTC);
  1106. tmp &= ~0x000003ff;
  1107. tmp |= (4 << cpcm->shift) - 1;
  1108. snd_cs46xx_poke(chip, BA1_PDTC, tmp);
  1109. snd_cs46xx_poke(chip, BA1_PFIE, pfie);
  1110. snd_cs46xx_set_play_sample_rate(chip, runtime->rate);
  1111. #endif
  1112. return 0;
  1113. }
  1114. static int snd_cs46xx_capture_hw_params(struct snd_pcm_substream *substream,
  1115. struct snd_pcm_hw_params *hw_params)
  1116. {
  1117. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1118. struct snd_pcm_runtime *runtime = substream->runtime;
  1119. int err;
  1120. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1121. cs46xx_dsp_pcm_ostream_set_period (chip, params_period_bytes(hw_params));
  1122. #endif
  1123. if (runtime->periods == CS46XX_FRAGS) {
  1124. if (runtime->dma_area != chip->capt.hw_buf.area)
  1125. snd_pcm_lib_free_pages(substream);
  1126. runtime->dma_area = chip->capt.hw_buf.area;
  1127. runtime->dma_addr = chip->capt.hw_buf.addr;
  1128. runtime->dma_bytes = chip->capt.hw_buf.bytes;
  1129. substream->ops = &snd_cs46xx_capture_ops;
  1130. } else {
  1131. if (runtime->dma_area == chip->capt.hw_buf.area) {
  1132. runtime->dma_area = NULL;
  1133. runtime->dma_addr = 0;
  1134. runtime->dma_bytes = 0;
  1135. }
  1136. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  1137. return err;
  1138. substream->ops = &snd_cs46xx_capture_indirect_ops;
  1139. }
  1140. return 0;
  1141. }
  1142. static int snd_cs46xx_capture_hw_free(struct snd_pcm_substream *substream)
  1143. {
  1144. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1145. struct snd_pcm_runtime *runtime = substream->runtime;
  1146. if (runtime->dma_area != chip->capt.hw_buf.area)
  1147. snd_pcm_lib_free_pages(substream);
  1148. runtime->dma_area = NULL;
  1149. runtime->dma_addr = 0;
  1150. runtime->dma_bytes = 0;
  1151. return 0;
  1152. }
  1153. static int snd_cs46xx_capture_prepare(struct snd_pcm_substream *substream)
  1154. {
  1155. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1156. struct snd_pcm_runtime *runtime = substream->runtime;
  1157. snd_cs46xx_poke(chip, BA1_CBA, chip->capt.hw_buf.addr);
  1158. chip->capt.shift = 2;
  1159. memset(&chip->capt.pcm_rec, 0, sizeof(chip->capt.pcm_rec));
  1160. chip->capt.pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
  1161. chip->capt.pcm_rec.hw_buffer_size = runtime->period_size * CS46XX_FRAGS << 2;
  1162. snd_cs46xx_set_capture_sample_rate(chip, runtime->rate);
  1163. return 0;
  1164. }
  1165. static irqreturn_t snd_cs46xx_interrupt(int irq, void *dev_id)
  1166. {
  1167. struct snd_cs46xx *chip = dev_id;
  1168. u32 status1;
  1169. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1170. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1171. u32 status2;
  1172. int i;
  1173. struct snd_cs46xx_pcm *cpcm = NULL;
  1174. #endif
  1175. /*
  1176. * Read the Interrupt Status Register to clear the interrupt
  1177. */
  1178. status1 = snd_cs46xx_peekBA0(chip, BA0_HISR);
  1179. if ((status1 & 0x7fffffff) == 0) {
  1180. snd_cs46xx_pokeBA0(chip, BA0_HICR, HICR_CHGM | HICR_IEV);
  1181. return IRQ_NONE;
  1182. }
  1183. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1184. status2 = snd_cs46xx_peekBA0(chip, BA0_HSR0);
  1185. for (i = 0; i < DSP_MAX_PCM_CHANNELS; ++i) {
  1186. if (i <= 15) {
  1187. if ( status1 & (1 << i) ) {
  1188. if (i == CS46XX_DSP_CAPTURE_CHANNEL) {
  1189. if (chip->capt.substream)
  1190. snd_pcm_period_elapsed(chip->capt.substream);
  1191. } else {
  1192. if (ins->pcm_channels[i].active &&
  1193. ins->pcm_channels[i].private_data &&
  1194. !ins->pcm_channels[i].unlinked) {
  1195. cpcm = ins->pcm_channels[i].private_data;
  1196. snd_pcm_period_elapsed(cpcm->substream);
  1197. }
  1198. }
  1199. }
  1200. } else {
  1201. if ( status2 & (1 << (i - 16))) {
  1202. if (ins->pcm_channels[i].active &&
  1203. ins->pcm_channels[i].private_data &&
  1204. !ins->pcm_channels[i].unlinked) {
  1205. cpcm = ins->pcm_channels[i].private_data;
  1206. snd_pcm_period_elapsed(cpcm->substream);
  1207. }
  1208. }
  1209. }
  1210. }
  1211. #else
  1212. /* old dsp */
  1213. if ((status1 & HISR_VC0) && chip->playback_pcm) {
  1214. if (chip->playback_pcm->substream)
  1215. snd_pcm_period_elapsed(chip->playback_pcm->substream);
  1216. }
  1217. if ((status1 & HISR_VC1) && chip->pcm) {
  1218. if (chip->capt.substream)
  1219. snd_pcm_period_elapsed(chip->capt.substream);
  1220. }
  1221. #endif
  1222. if ((status1 & HISR_MIDI) && chip->rmidi) {
  1223. unsigned char c;
  1224. spin_lock(&chip->reg_lock);
  1225. while ((snd_cs46xx_peekBA0(chip, BA0_MIDSR) & MIDSR_RBE) == 0) {
  1226. c = snd_cs46xx_peekBA0(chip, BA0_MIDRP);
  1227. if ((chip->midcr & MIDCR_RIE) == 0)
  1228. continue;
  1229. snd_rawmidi_receive(chip->midi_input, &c, 1);
  1230. }
  1231. while ((snd_cs46xx_peekBA0(chip, BA0_MIDSR) & MIDSR_TBF) == 0) {
  1232. if ((chip->midcr & MIDCR_TIE) == 0)
  1233. break;
  1234. if (snd_rawmidi_transmit(chip->midi_output, &c, 1) != 1) {
  1235. chip->midcr &= ~MIDCR_TIE;
  1236. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  1237. break;
  1238. }
  1239. snd_cs46xx_pokeBA0(chip, BA0_MIDWP, c);
  1240. }
  1241. spin_unlock(&chip->reg_lock);
  1242. }
  1243. /*
  1244. * EOI to the PCI part....reenables interrupts
  1245. */
  1246. snd_cs46xx_pokeBA0(chip, BA0_HICR, HICR_CHGM | HICR_IEV);
  1247. return IRQ_HANDLED;
  1248. }
  1249. static struct snd_pcm_hardware snd_cs46xx_playback =
  1250. {
  1251. .info = (SNDRV_PCM_INFO_MMAP |
  1252. SNDRV_PCM_INFO_INTERLEAVED |
  1253. SNDRV_PCM_INFO_BLOCK_TRANSFER /*|*/
  1254. /*SNDRV_PCM_INFO_RESUME*/),
  1255. .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  1256. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  1257. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE),
  1258. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1259. .rate_min = 5500,
  1260. .rate_max = 48000,
  1261. .channels_min = 1,
  1262. .channels_max = 2,
  1263. .buffer_bytes_max = (256 * 1024),
  1264. .period_bytes_min = CS46XX_MIN_PERIOD_SIZE,
  1265. .period_bytes_max = CS46XX_MAX_PERIOD_SIZE,
  1266. .periods_min = CS46XX_FRAGS,
  1267. .periods_max = 1024,
  1268. .fifo_size = 0,
  1269. };
  1270. static struct snd_pcm_hardware snd_cs46xx_capture =
  1271. {
  1272. .info = (SNDRV_PCM_INFO_MMAP |
  1273. SNDRV_PCM_INFO_INTERLEAVED |
  1274. SNDRV_PCM_INFO_BLOCK_TRANSFER /*|*/
  1275. /*SNDRV_PCM_INFO_RESUME*/),
  1276. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1277. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1278. .rate_min = 5500,
  1279. .rate_max = 48000,
  1280. .channels_min = 2,
  1281. .channels_max = 2,
  1282. .buffer_bytes_max = (256 * 1024),
  1283. .period_bytes_min = CS46XX_MIN_PERIOD_SIZE,
  1284. .period_bytes_max = CS46XX_MAX_PERIOD_SIZE,
  1285. .periods_min = CS46XX_FRAGS,
  1286. .periods_max = 1024,
  1287. .fifo_size = 0,
  1288. };
  1289. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1290. static unsigned int period_sizes[] = { 32, 64, 128, 256, 512, 1024, 2048 };
  1291. static struct snd_pcm_hw_constraint_list hw_constraints_period_sizes = {
  1292. .count = ARRAY_SIZE(period_sizes),
  1293. .list = period_sizes,
  1294. .mask = 0
  1295. };
  1296. #endif
  1297. static void snd_cs46xx_pcm_free_substream(struct snd_pcm_runtime *runtime)
  1298. {
  1299. kfree(runtime->private_data);
  1300. }
  1301. static int _cs46xx_playback_open_channel (struct snd_pcm_substream *substream,int pcm_channel_id)
  1302. {
  1303. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1304. struct snd_cs46xx_pcm * cpcm;
  1305. struct snd_pcm_runtime *runtime = substream->runtime;
  1306. cpcm = kzalloc(sizeof(*cpcm), GFP_KERNEL);
  1307. if (cpcm == NULL)
  1308. return -ENOMEM;
  1309. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  1310. PAGE_SIZE, &cpcm->hw_buf) < 0) {
  1311. kfree(cpcm);
  1312. return -ENOMEM;
  1313. }
  1314. runtime->hw = snd_cs46xx_playback;
  1315. runtime->private_data = cpcm;
  1316. runtime->private_free = snd_cs46xx_pcm_free_substream;
  1317. cpcm->substream = substream;
  1318. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1319. mutex_lock(&chip->spos_mutex);
  1320. cpcm->pcm_channel = NULL;
  1321. cpcm->pcm_channel_id = pcm_channel_id;
  1322. snd_pcm_hw_constraint_list(runtime, 0,
  1323. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1324. &hw_constraints_period_sizes);
  1325. mutex_unlock(&chip->spos_mutex);
  1326. #else
  1327. chip->playback_pcm = cpcm; /* HACK */
  1328. #endif
  1329. if (chip->accept_valid)
  1330. substream->runtime->hw.info |= SNDRV_PCM_INFO_MMAP_VALID;
  1331. chip->active_ctrl(chip, 1);
  1332. return 0;
  1333. }
  1334. static int snd_cs46xx_playback_open(struct snd_pcm_substream *substream)
  1335. {
  1336. snd_printdd("open front channel\n");
  1337. return _cs46xx_playback_open_channel(substream,DSP_PCM_MAIN_CHANNEL);
  1338. }
  1339. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1340. static int snd_cs46xx_playback_open_rear(struct snd_pcm_substream *substream)
  1341. {
  1342. snd_printdd("open rear channel\n");
  1343. return _cs46xx_playback_open_channel(substream,DSP_PCM_REAR_CHANNEL);
  1344. }
  1345. static int snd_cs46xx_playback_open_clfe(struct snd_pcm_substream *substream)
  1346. {
  1347. snd_printdd("open center - LFE channel\n");
  1348. return _cs46xx_playback_open_channel(substream,DSP_PCM_CENTER_LFE_CHANNEL);
  1349. }
  1350. static int snd_cs46xx_playback_open_iec958(struct snd_pcm_substream *substream)
  1351. {
  1352. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1353. snd_printdd("open raw iec958 channel\n");
  1354. mutex_lock(&chip->spos_mutex);
  1355. cs46xx_iec958_pre_open (chip);
  1356. mutex_unlock(&chip->spos_mutex);
  1357. return _cs46xx_playback_open_channel(substream,DSP_IEC958_CHANNEL);
  1358. }
  1359. static int snd_cs46xx_playback_close(struct snd_pcm_substream *substream);
  1360. static int snd_cs46xx_playback_close_iec958(struct snd_pcm_substream *substream)
  1361. {
  1362. int err;
  1363. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1364. snd_printdd("close raw iec958 channel\n");
  1365. err = snd_cs46xx_playback_close(substream);
  1366. mutex_lock(&chip->spos_mutex);
  1367. cs46xx_iec958_post_close (chip);
  1368. mutex_unlock(&chip->spos_mutex);
  1369. return err;
  1370. }
  1371. #endif
  1372. static int snd_cs46xx_capture_open(struct snd_pcm_substream *substream)
  1373. {
  1374. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1375. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  1376. PAGE_SIZE, &chip->capt.hw_buf) < 0)
  1377. return -ENOMEM;
  1378. chip->capt.substream = substream;
  1379. substream->runtime->hw = snd_cs46xx_capture;
  1380. if (chip->accept_valid)
  1381. substream->runtime->hw.info |= SNDRV_PCM_INFO_MMAP_VALID;
  1382. chip->active_ctrl(chip, 1);
  1383. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1384. snd_pcm_hw_constraint_list(substream->runtime, 0,
  1385. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1386. &hw_constraints_period_sizes);
  1387. #endif
  1388. return 0;
  1389. }
  1390. static int snd_cs46xx_playback_close(struct snd_pcm_substream *substream)
  1391. {
  1392. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1393. struct snd_pcm_runtime *runtime = substream->runtime;
  1394. struct snd_cs46xx_pcm * cpcm;
  1395. cpcm = runtime->private_data;
  1396. /* when playback_open fails, then cpcm can be NULL */
  1397. if (!cpcm) return -ENXIO;
  1398. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1399. mutex_lock(&chip->spos_mutex);
  1400. if (cpcm->pcm_channel) {
  1401. cs46xx_dsp_destroy_pcm_channel(chip,cpcm->pcm_channel);
  1402. cpcm->pcm_channel = NULL;
  1403. }
  1404. mutex_unlock(&chip->spos_mutex);
  1405. #else
  1406. chip->playback_pcm = NULL;
  1407. #endif
  1408. cpcm->substream = NULL;
  1409. snd_dma_free_pages(&cpcm->hw_buf);
  1410. chip->active_ctrl(chip, -1);
  1411. return 0;
  1412. }
  1413. static int snd_cs46xx_capture_close(struct snd_pcm_substream *substream)
  1414. {
  1415. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1416. chip->capt.substream = NULL;
  1417. snd_dma_free_pages(&chip->capt.hw_buf);
  1418. chip->active_ctrl(chip, -1);
  1419. return 0;
  1420. }
  1421. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1422. static struct snd_pcm_ops snd_cs46xx_playback_rear_ops = {
  1423. .open = snd_cs46xx_playback_open_rear,
  1424. .close = snd_cs46xx_playback_close,
  1425. .ioctl = snd_pcm_lib_ioctl,
  1426. .hw_params = snd_cs46xx_playback_hw_params,
  1427. .hw_free = snd_cs46xx_playback_hw_free,
  1428. .prepare = snd_cs46xx_playback_prepare,
  1429. .trigger = snd_cs46xx_playback_trigger,
  1430. .pointer = snd_cs46xx_playback_direct_pointer,
  1431. };
  1432. static struct snd_pcm_ops snd_cs46xx_playback_indirect_rear_ops = {
  1433. .open = snd_cs46xx_playback_open_rear,
  1434. .close = snd_cs46xx_playback_close,
  1435. .ioctl = snd_pcm_lib_ioctl,
  1436. .hw_params = snd_cs46xx_playback_hw_params,
  1437. .hw_free = snd_cs46xx_playback_hw_free,
  1438. .prepare = snd_cs46xx_playback_prepare,
  1439. .trigger = snd_cs46xx_playback_trigger,
  1440. .pointer = snd_cs46xx_playback_indirect_pointer,
  1441. .ack = snd_cs46xx_playback_transfer,
  1442. };
  1443. static struct snd_pcm_ops snd_cs46xx_playback_clfe_ops = {
  1444. .open = snd_cs46xx_playback_open_clfe,
  1445. .close = snd_cs46xx_playback_close,
  1446. .ioctl = snd_pcm_lib_ioctl,
  1447. .hw_params = snd_cs46xx_playback_hw_params,
  1448. .hw_free = snd_cs46xx_playback_hw_free,
  1449. .prepare = snd_cs46xx_playback_prepare,
  1450. .trigger = snd_cs46xx_playback_trigger,
  1451. .pointer = snd_cs46xx_playback_direct_pointer,
  1452. };
  1453. static struct snd_pcm_ops snd_cs46xx_playback_indirect_clfe_ops = {
  1454. .open = snd_cs46xx_playback_open_clfe,
  1455. .close = snd_cs46xx_playback_close,
  1456. .ioctl = snd_pcm_lib_ioctl,
  1457. .hw_params = snd_cs46xx_playback_hw_params,
  1458. .hw_free = snd_cs46xx_playback_hw_free,
  1459. .prepare = snd_cs46xx_playback_prepare,
  1460. .trigger = snd_cs46xx_playback_trigger,
  1461. .pointer = snd_cs46xx_playback_indirect_pointer,
  1462. .ack = snd_cs46xx_playback_transfer,
  1463. };
  1464. static struct snd_pcm_ops snd_cs46xx_playback_iec958_ops = {
  1465. .open = snd_cs46xx_playback_open_iec958,
  1466. .close = snd_cs46xx_playback_close_iec958,
  1467. .ioctl = snd_pcm_lib_ioctl,
  1468. .hw_params = snd_cs46xx_playback_hw_params,
  1469. .hw_free = snd_cs46xx_playback_hw_free,
  1470. .prepare = snd_cs46xx_playback_prepare,
  1471. .trigger = snd_cs46xx_playback_trigger,
  1472. .pointer = snd_cs46xx_playback_direct_pointer,
  1473. };
  1474. static struct snd_pcm_ops snd_cs46xx_playback_indirect_iec958_ops = {
  1475. .open = snd_cs46xx_playback_open_iec958,
  1476. .close = snd_cs46xx_playback_close_iec958,
  1477. .ioctl = snd_pcm_lib_ioctl,
  1478. .hw_params = snd_cs46xx_playback_hw_params,
  1479. .hw_free = snd_cs46xx_playback_hw_free,
  1480. .prepare = snd_cs46xx_playback_prepare,
  1481. .trigger = snd_cs46xx_playback_trigger,
  1482. .pointer = snd_cs46xx_playback_indirect_pointer,
  1483. .ack = snd_cs46xx_playback_transfer,
  1484. };
  1485. #endif
  1486. static struct snd_pcm_ops snd_cs46xx_playback_ops = {
  1487. .open = snd_cs46xx_playback_open,
  1488. .close = snd_cs46xx_playback_close,
  1489. .ioctl = snd_pcm_lib_ioctl,
  1490. .hw_params = snd_cs46xx_playback_hw_params,
  1491. .hw_free = snd_cs46xx_playback_hw_free,
  1492. .prepare = snd_cs46xx_playback_prepare,
  1493. .trigger = snd_cs46xx_playback_trigger,
  1494. .pointer = snd_cs46xx_playback_direct_pointer,
  1495. };
  1496. static struct snd_pcm_ops snd_cs46xx_playback_indirect_ops = {
  1497. .open = snd_cs46xx_playback_open,
  1498. .close = snd_cs46xx_playback_close,
  1499. .ioctl = snd_pcm_lib_ioctl,
  1500. .hw_params = snd_cs46xx_playback_hw_params,
  1501. .hw_free = snd_cs46xx_playback_hw_free,
  1502. .prepare = snd_cs46xx_playback_prepare,
  1503. .trigger = snd_cs46xx_playback_trigger,
  1504. .pointer = snd_cs46xx_playback_indirect_pointer,
  1505. .ack = snd_cs46xx_playback_transfer,
  1506. };
  1507. static struct snd_pcm_ops snd_cs46xx_capture_ops = {
  1508. .open = snd_cs46xx_capture_open,
  1509. .close = snd_cs46xx_capture_close,
  1510. .ioctl = snd_pcm_lib_ioctl,
  1511. .hw_params = snd_cs46xx_capture_hw_params,
  1512. .hw_free = snd_cs46xx_capture_hw_free,
  1513. .prepare = snd_cs46xx_capture_prepare,
  1514. .trigger = snd_cs46xx_capture_trigger,
  1515. .pointer = snd_cs46xx_capture_direct_pointer,
  1516. };
  1517. static struct snd_pcm_ops snd_cs46xx_capture_indirect_ops = {
  1518. .open = snd_cs46xx_capture_open,
  1519. .close = snd_cs46xx_capture_close,
  1520. .ioctl = snd_pcm_lib_ioctl,
  1521. .hw_params = snd_cs46xx_capture_hw_params,
  1522. .hw_free = snd_cs46xx_capture_hw_free,
  1523. .prepare = snd_cs46xx_capture_prepare,
  1524. .trigger = snd_cs46xx_capture_trigger,
  1525. .pointer = snd_cs46xx_capture_indirect_pointer,
  1526. .ack = snd_cs46xx_capture_transfer,
  1527. };
  1528. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1529. #define MAX_PLAYBACK_CHANNELS (DSP_MAX_PCM_CHANNELS - 1)
  1530. #else
  1531. #define MAX_PLAYBACK_CHANNELS 1
  1532. #endif
  1533. int snd_cs46xx_pcm(struct snd_cs46xx *chip, int device, struct snd_pcm **rpcm)
  1534. {
  1535. struct snd_pcm *pcm;
  1536. int err;
  1537. if (rpcm)
  1538. *rpcm = NULL;
  1539. if ((err = snd_pcm_new(chip->card, "CS46xx", device, MAX_PLAYBACK_CHANNELS, 1, &pcm)) < 0)
  1540. return err;
  1541. pcm->private_data = chip;
  1542. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_ops);
  1543. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cs46xx_capture_ops);
  1544. /* global setup */
  1545. pcm->info_flags = 0;
  1546. strcpy(pcm->name, "CS46xx");
  1547. chip->pcm = pcm;
  1548. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1549. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1550. if (rpcm)
  1551. *rpcm = pcm;
  1552. return 0;
  1553. }
  1554. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1555. int snd_cs46xx_pcm_rear(struct snd_cs46xx *chip, int device,
  1556. struct snd_pcm **rpcm)
  1557. {
  1558. struct snd_pcm *pcm;
  1559. int err;
  1560. if (rpcm)
  1561. *rpcm = NULL;
  1562. if ((err = snd_pcm_new(chip->card, "CS46xx - Rear", device, MAX_PLAYBACK_CHANNELS, 0, &pcm)) < 0)
  1563. return err;
  1564. pcm->private_data = chip;
  1565. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_rear_ops);
  1566. /* global setup */
  1567. pcm->info_flags = 0;
  1568. strcpy(pcm->name, "CS46xx - Rear");
  1569. chip->pcm_rear = pcm;
  1570. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1571. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1572. if (rpcm)
  1573. *rpcm = pcm;
  1574. return 0;
  1575. }
  1576. int snd_cs46xx_pcm_center_lfe(struct snd_cs46xx *chip, int device,
  1577. struct snd_pcm **rpcm)
  1578. {
  1579. struct snd_pcm *pcm;
  1580. int err;
  1581. if (rpcm)
  1582. *rpcm = NULL;
  1583. if ((err = snd_pcm_new(chip->card, "CS46xx - Center LFE", device, MAX_PLAYBACK_CHANNELS, 0, &pcm)) < 0)
  1584. return err;
  1585. pcm->private_data = chip;
  1586. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_clfe_ops);
  1587. /* global setup */
  1588. pcm->info_flags = 0;
  1589. strcpy(pcm->name, "CS46xx - Center LFE");
  1590. chip->pcm_center_lfe = pcm;
  1591. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1592. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1593. if (rpcm)
  1594. *rpcm = pcm;
  1595. return 0;
  1596. }
  1597. int snd_cs46xx_pcm_iec958(struct snd_cs46xx *chip, int device,
  1598. struct snd_pcm **rpcm)
  1599. {
  1600. struct snd_pcm *pcm;
  1601. int err;
  1602. if (rpcm)
  1603. *rpcm = NULL;
  1604. if ((err = snd_pcm_new(chip->card, "CS46xx - IEC958", device, 1, 0, &pcm)) < 0)
  1605. return err;
  1606. pcm->private_data = chip;
  1607. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_iec958_ops);
  1608. /* global setup */
  1609. pcm->info_flags = 0;
  1610. strcpy(pcm->name, "CS46xx - IEC958");
  1611. chip->pcm_rear = pcm;
  1612. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1613. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1614. if (rpcm)
  1615. *rpcm = pcm;
  1616. return 0;
  1617. }
  1618. #endif
  1619. /*
  1620. * Mixer routines
  1621. */
  1622. static void snd_cs46xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
  1623. {
  1624. struct snd_cs46xx *chip = bus->private_data;
  1625. chip->ac97_bus = NULL;
  1626. }
  1627. static void snd_cs46xx_mixer_free_ac97(struct snd_ac97 *ac97)
  1628. {
  1629. struct snd_cs46xx *chip = ac97->private_data;
  1630. if (snd_BUG_ON(ac97 != chip->ac97[CS46XX_PRIMARY_CODEC_INDEX] &&
  1631. ac97 != chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]))
  1632. return;
  1633. if (ac97 == chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]) {
  1634. chip->ac97[CS46XX_PRIMARY_CODEC_INDEX] = NULL;
  1635. chip->eapd_switch = NULL;
  1636. }
  1637. else
  1638. chip->ac97[CS46XX_SECONDARY_CODEC_INDEX] = NULL;
  1639. }
  1640. static int snd_cs46xx_vol_info(struct snd_kcontrol *kcontrol,
  1641. struct snd_ctl_elem_info *uinfo)
  1642. {
  1643. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1644. uinfo->count = 2;
  1645. uinfo->value.integer.min = 0;
  1646. uinfo->value.integer.max = 0x7fff;
  1647. return 0;
  1648. }
  1649. static int snd_cs46xx_vol_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1650. {
  1651. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1652. int reg = kcontrol->private_value;
  1653. unsigned int val = snd_cs46xx_peek(chip, reg);
  1654. ucontrol->value.integer.value[0] = 0xffff - (val >> 16);
  1655. ucontrol->value.integer.value[1] = 0xffff - (val & 0xffff);
  1656. return 0;
  1657. }
  1658. static int snd_cs46xx_vol_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1659. {
  1660. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1661. int reg = kcontrol->private_value;
  1662. unsigned int val = ((0xffff - ucontrol->value.integer.value[0]) << 16 |
  1663. (0xffff - ucontrol->value.integer.value[1]));
  1664. unsigned int old = snd_cs46xx_peek(chip, reg);
  1665. int change = (old != val);
  1666. if (change) {
  1667. snd_cs46xx_poke(chip, reg, val);
  1668. }
  1669. return change;
  1670. }
  1671. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1672. static int snd_cs46xx_vol_dac_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1673. {
  1674. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1675. ucontrol->value.integer.value[0] = chip->dsp_spos_instance->dac_volume_left;
  1676. ucontrol->value.integer.value[1] = chip->dsp_spos_instance->dac_volume_right;
  1677. return 0;
  1678. }
  1679. static int snd_cs46xx_vol_dac_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1680. {
  1681. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1682. int change = 0;
  1683. if (chip->dsp_spos_instance->dac_volume_right != ucontrol->value.integer.value[0] ||
  1684. chip->dsp_spos_instance->dac_volume_left != ucontrol->value.integer.value[1]) {
  1685. cs46xx_dsp_set_dac_volume(chip,
  1686. ucontrol->value.integer.value[0],
  1687. ucontrol->value.integer.value[1]);
  1688. change = 1;
  1689. }
  1690. return change;
  1691. }
  1692. #if 0
  1693. static int snd_cs46xx_vol_iec958_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1694. {
  1695. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1696. ucontrol->value.integer.value[0] = chip->dsp_spos_instance->spdif_input_volume_left;
  1697. ucontrol->value.integer.value[1] = chip->dsp_spos_instance->spdif_input_volume_right;
  1698. return 0;
  1699. }
  1700. static int snd_cs46xx_vol_iec958_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1701. {
  1702. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1703. int change = 0;
  1704. if (chip->dsp_spos_instance->spdif_input_volume_left != ucontrol->value.integer.value[0] ||
  1705. chip->dsp_spos_instance->spdif_input_volume_right!= ucontrol->value.integer.value[1]) {
  1706. cs46xx_dsp_set_iec958_volume (chip,
  1707. ucontrol->value.integer.value[0],
  1708. ucontrol->value.integer.value[1]);
  1709. change = 1;
  1710. }
  1711. return change;
  1712. }
  1713. #endif
  1714. #define snd_mixer_boolean_info snd_ctl_boolean_mono_info
  1715. static int snd_cs46xx_iec958_get(struct snd_kcontrol *kcontrol,
  1716. struct snd_ctl_elem_value *ucontrol)
  1717. {
  1718. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1719. int reg = kcontrol->private_value;
  1720. if (reg == CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT)
  1721. ucontrol->value.integer.value[0] = (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED);
  1722. else
  1723. ucontrol->value.integer.value[0] = chip->dsp_spos_instance->spdif_status_in;
  1724. return 0;
  1725. }
  1726. static int snd_cs46xx_iec958_put(struct snd_kcontrol *kcontrol,
  1727. struct snd_ctl_elem_value *ucontrol)
  1728. {
  1729. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1730. int change, res;
  1731. switch (kcontrol->private_value) {
  1732. case CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT:
  1733. mutex_lock(&chip->spos_mutex);
  1734. change = (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED);
  1735. if (ucontrol->value.integer.value[0] && !change)
  1736. cs46xx_dsp_enable_spdif_out(chip);
  1737. else if (change && !ucontrol->value.integer.value[0])
  1738. cs46xx_dsp_disable_spdif_out(chip);
  1739. res = (change != (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED));
  1740. mutex_unlock(&chip->spos_mutex);
  1741. break;
  1742. case CS46XX_MIXER_SPDIF_INPUT_ELEMENT:
  1743. change = chip->dsp_spos_instance->spdif_status_in;
  1744. if (ucontrol->value.integer.value[0] && !change) {
  1745. cs46xx_dsp_enable_spdif_in(chip);
  1746. /* restore volume */
  1747. }
  1748. else if (change && !ucontrol->value.integer.value[0])
  1749. cs46xx_dsp_disable_spdif_in(chip);
  1750. res = (change != chip->dsp_spos_instance->spdif_status_in);
  1751. break;
  1752. default:
  1753. res = -EINVAL;
  1754. snd_BUG(); /* should never happen ... */
  1755. }
  1756. return res;
  1757. }
  1758. static int snd_cs46xx_adc_capture_get(struct snd_kcontrol *kcontrol,
  1759. struct snd_ctl_elem_value *ucontrol)
  1760. {
  1761. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1762. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1763. if (ins->adc_input != NULL)
  1764. ucontrol->value.integer.value[0] = 1;
  1765. else
  1766. ucontrol->value.integer.value[0] = 0;
  1767. return 0;
  1768. }
  1769. static int snd_cs46xx_adc_capture_put(struct snd_kcontrol *kcontrol,
  1770. struct snd_ctl_elem_value *ucontrol)
  1771. {
  1772. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1773. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1774. int change = 0;
  1775. if (ucontrol->value.integer.value[0] && !ins->adc_input) {
  1776. cs46xx_dsp_enable_adc_capture(chip);
  1777. change = 1;
  1778. } else if (!ucontrol->value.integer.value[0] && ins->adc_input) {
  1779. cs46xx_dsp_disable_adc_capture(chip);
  1780. change = 1;
  1781. }
  1782. return change;
  1783. }
  1784. static int snd_cs46xx_pcm_capture_get(struct snd_kcontrol *kcontrol,
  1785. struct snd_ctl_elem_value *ucontrol)
  1786. {
  1787. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1788. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1789. if (ins->pcm_input != NULL)
  1790. ucontrol->value.integer.value[0] = 1;
  1791. else
  1792. ucontrol->value.integer.value[0] = 0;
  1793. return 0;
  1794. }
  1795. static int snd_cs46xx_pcm_capture_put(struct snd_kcontrol *kcontrol,
  1796. struct snd_ctl_elem_value *ucontrol)
  1797. {
  1798. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1799. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1800. int change = 0;
  1801. if (ucontrol->value.integer.value[0] && !ins->pcm_input) {
  1802. cs46xx_dsp_enable_pcm_capture(chip);
  1803. change = 1;
  1804. } else if (!ucontrol->value.integer.value[0] && ins->pcm_input) {
  1805. cs46xx_dsp_disable_pcm_capture(chip);
  1806. change = 1;
  1807. }
  1808. return change;
  1809. }
  1810. static int snd_herc_spdif_select_get(struct snd_kcontrol *kcontrol,
  1811. struct snd_ctl_elem_value *ucontrol)
  1812. {
  1813. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1814. int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
  1815. if (val1 & EGPIODR_GPOE0)
  1816. ucontrol->value.integer.value[0] = 1;
  1817. else
  1818. ucontrol->value.integer.value[0] = 0;
  1819. return 0;
  1820. }
  1821. /*
  1822. * Game Theatre XP card - EGPIO[0] is used to select SPDIF input optical or coaxial.
  1823. */
  1824. static int snd_herc_spdif_select_put(struct snd_kcontrol *kcontrol,
  1825. struct snd_ctl_elem_value *ucontrol)
  1826. {
  1827. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1828. int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
  1829. int val2 = snd_cs46xx_peekBA0(chip, BA0_EGPIOPTR);
  1830. if (ucontrol->value.integer.value[0]) {
  1831. /* optical is default */
  1832. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR,
  1833. EGPIODR_GPOE0 | val1); /* enable EGPIO0 output */
  1834. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR,
  1835. EGPIOPTR_GPPT0 | val2); /* open-drain on output */
  1836. } else {
  1837. /* coaxial */
  1838. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, val1 & ~EGPIODR_GPOE0); /* disable */
  1839. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, val2 & ~EGPIOPTR_GPPT0); /* disable */
  1840. }
  1841. /* checking diff from the EGPIO direction register
  1842. should be enough */
  1843. return (val1 != (int)snd_cs46xx_peekBA0(chip, BA0_EGPIODR));
  1844. }
  1845. static int snd_cs46xx_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1846. {
  1847. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1848. uinfo->count = 1;
  1849. return 0;
  1850. }
  1851. static int snd_cs46xx_spdif_default_get(struct snd_kcontrol *kcontrol,
  1852. struct snd_ctl_elem_value *ucontrol)
  1853. {
  1854. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1855. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1856. mutex_lock(&chip->spos_mutex);
  1857. ucontrol->value.iec958.status[0] = _wrap_all_bits((ins->spdif_csuv_default >> 24) & 0xff);
  1858. ucontrol->value.iec958.status[1] = _wrap_all_bits((ins->spdif_csuv_default >> 16) & 0xff);
  1859. ucontrol->value.iec958.status[2] = 0;
  1860. ucontrol->value.iec958.status[3] = _wrap_all_bits((ins->spdif_csuv_default) & 0xff);
  1861. mutex_unlock(&chip->spos_mutex);
  1862. return 0;
  1863. }
  1864. static int snd_cs46xx_spdif_default_put(struct snd_kcontrol *kcontrol,
  1865. struct snd_ctl_elem_value *ucontrol)
  1866. {
  1867. struct snd_cs46xx * chip = snd_kcontrol_chip(kcontrol);
  1868. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1869. unsigned int val;
  1870. int change;
  1871. mutex_lock(&chip->spos_mutex);
  1872. val = ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[0]) << 24) |
  1873. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[2]) << 16) |
  1874. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[3])) |
  1875. /* left and right validity bit */
  1876. (1 << 13) | (1 << 12);
  1877. change = (unsigned int)ins->spdif_csuv_default != val;
  1878. ins->spdif_csuv_default = val;
  1879. if ( !(ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN) )
  1880. cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV,val);
  1881. mutex_unlock(&chip->spos_mutex);
  1882. return change;
  1883. }
  1884. static int snd_cs46xx_spdif_mask_get(struct snd_kcontrol *kcontrol,
  1885. struct snd_ctl_elem_value *ucontrol)
  1886. {
  1887. ucontrol->value.iec958.status[0] = 0xff;
  1888. ucontrol->value.iec958.status[1] = 0xff;
  1889. ucontrol->value.iec958.status[2] = 0x00;
  1890. ucontrol->value.iec958.status[3] = 0xff;
  1891. return 0;
  1892. }
  1893. static int snd_cs46xx_spdif_stream_get(struct snd_kcontrol *kcontrol,
  1894. struct snd_ctl_elem_value *ucontrol)
  1895. {
  1896. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1897. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1898. mutex_lock(&chip->spos_mutex);
  1899. ucontrol->value.iec958.status[0] = _wrap_all_bits((ins->spdif_csuv_stream >> 24) & 0xff);
  1900. ucontrol->value.iec958.status[1] = _wrap_all_bits((ins->spdif_csuv_stream >> 16) & 0xff);
  1901. ucontrol->value.iec958.status[2] = 0;
  1902. ucontrol->value.iec958.status[3] = _wrap_all_bits((ins->spdif_csuv_stream) & 0xff);
  1903. mutex_unlock(&chip->spos_mutex);
  1904. return 0;
  1905. }
  1906. static int snd_cs46xx_spdif_stream_put(struct snd_kcontrol *kcontrol,
  1907. struct snd_ctl_elem_value *ucontrol)
  1908. {
  1909. struct snd_cs46xx * chip = snd_kcontrol_chip(kcontrol);
  1910. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1911. unsigned int val;
  1912. int change;
  1913. mutex_lock(&chip->spos_mutex);
  1914. val = ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[0]) << 24) |
  1915. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[1]) << 16) |
  1916. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[3])) |
  1917. /* left and right validity bit */
  1918. (1 << 13) | (1 << 12);
  1919. change = ins->spdif_csuv_stream != val;
  1920. ins->spdif_csuv_stream = val;
  1921. if ( ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN )
  1922. cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV,val);
  1923. mutex_unlock(&chip->spos_mutex);
  1924. return change;
  1925. }
  1926. #endif /* CONFIG_SND_CS46XX_NEW_DSP */
  1927. static struct snd_kcontrol_new snd_cs46xx_controls[] = {
  1928. {
  1929. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1930. .name = "DAC Volume",
  1931. .info = snd_cs46xx_vol_info,
  1932. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  1933. .get = snd_cs46xx_vol_get,
  1934. .put = snd_cs46xx_vol_put,
  1935. .private_value = BA1_PVOL,
  1936. #else
  1937. .get = snd_cs46xx_vol_dac_get,
  1938. .put = snd_cs46xx_vol_dac_put,
  1939. #endif
  1940. },
  1941. {
  1942. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1943. .name = "ADC Volume",
  1944. .info = snd_cs46xx_vol_info,
  1945. .get = snd_cs46xx_vol_get,
  1946. .put = snd_cs46xx_vol_put,
  1947. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  1948. .private_value = BA1_CVOL,
  1949. #else
  1950. .private_value = (VARIDECIMATE_SCB_ADDR + 0xE) << 2,
  1951. #endif
  1952. },
  1953. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1954. {
  1955. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1956. .name = "ADC Capture Switch",
  1957. .info = snd_mixer_boolean_info,
  1958. .get = snd_cs46xx_adc_capture_get,
  1959. .put = snd_cs46xx_adc_capture_put
  1960. },
  1961. {
  1962. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1963. .name = "DAC Capture Switch",
  1964. .info = snd_mixer_boolean_info,
  1965. .get = snd_cs46xx_pcm_capture_get,
  1966. .put = snd_cs46xx_pcm_capture_put
  1967. },
  1968. {
  1969. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1970. .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
  1971. .info = snd_mixer_boolean_info,
  1972. .get = snd_cs46xx_iec958_get,
  1973. .put = snd_cs46xx_iec958_put,
  1974. .private_value = CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT,
  1975. },
  1976. {
  1977. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1978. .name = SNDRV_CTL_NAME_IEC958("Input ",NONE,SWITCH),
  1979. .info = snd_mixer_boolean_info,
  1980. .get = snd_cs46xx_iec958_get,
  1981. .put = snd_cs46xx_iec958_put,
  1982. .private_value = CS46XX_MIXER_SPDIF_INPUT_ELEMENT,
  1983. },
  1984. #if 0
  1985. /* Input IEC958 volume does not work for the moment. (Benny) */
  1986. {
  1987. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1988. .name = SNDRV_CTL_NAME_IEC958("Input ",NONE,VOLUME),
  1989. .info = snd_cs46xx_vol_info,
  1990. .get = snd_cs46xx_vol_iec958_get,
  1991. .put = snd_cs46xx_vol_iec958_put,
  1992. .private_value = (ASYNCRX_SCB_ADDR + 0xE) << 2,
  1993. },
  1994. #endif
  1995. {
  1996. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1997. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1998. .info = snd_cs46xx_spdif_info,
  1999. .get = snd_cs46xx_spdif_default_get,
  2000. .put = snd_cs46xx_spdif_default_put,
  2001. },
  2002. {
  2003. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2004. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  2005. .info = snd_cs46xx_spdif_info,
  2006. .get = snd_cs46xx_spdif_mask_get,
  2007. .access = SNDRV_CTL_ELEM_ACCESS_READ
  2008. },
  2009. {
  2010. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2011. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  2012. .info = snd_cs46xx_spdif_info,
  2013. .get = snd_cs46xx_spdif_stream_get,
  2014. .put = snd_cs46xx_spdif_stream_put
  2015. },
  2016. #endif
  2017. };
  2018. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2019. /* set primary cs4294 codec into Extended Audio Mode */
  2020. static int snd_cs46xx_front_dup_get(struct snd_kcontrol *kcontrol,
  2021. struct snd_ctl_elem_value *ucontrol)
  2022. {
  2023. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  2024. unsigned short val;
  2025. val = snd_ac97_read(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX], AC97_CSR_ACMODE);
  2026. ucontrol->value.integer.value[0] = (val & 0x200) ? 0 : 1;
  2027. return 0;
  2028. }
  2029. static int snd_cs46xx_front_dup_put(struct snd_kcontrol *kcontrol,
  2030. struct snd_ctl_elem_value *ucontrol)
  2031. {
  2032. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  2033. return snd_ac97_update_bits(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX],
  2034. AC97_CSR_ACMODE, 0x200,
  2035. ucontrol->value.integer.value[0] ? 0 : 0x200);
  2036. }
  2037. static struct snd_kcontrol_new snd_cs46xx_front_dup_ctl = {
  2038. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2039. .name = "Duplicate Front",
  2040. .info = snd_mixer_boolean_info,
  2041. .get = snd_cs46xx_front_dup_get,
  2042. .put = snd_cs46xx_front_dup_put,
  2043. };
  2044. #endif
  2045. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2046. /* Only available on the Hercules Game Theater XP soundcard */
  2047. static struct snd_kcontrol_new snd_hercules_controls[] = {
  2048. {
  2049. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2050. .name = "Optical/Coaxial SPDIF Input Switch",
  2051. .info = snd_mixer_boolean_info,
  2052. .get = snd_herc_spdif_select_get,
  2053. .put = snd_herc_spdif_select_put,
  2054. },
  2055. };
  2056. static void snd_cs46xx_codec_reset (struct snd_ac97 * ac97)
  2057. {
  2058. unsigned long end_time;
  2059. int err;
  2060. /* reset to defaults */
  2061. snd_ac97_write(ac97, AC97_RESET, 0);
  2062. /* set the desired CODEC mode */
  2063. if (ac97->num == CS46XX_PRIMARY_CODEC_INDEX) {
  2064. snd_printdd("cs46xx: CODEC1 mode %04x\n", 0x0);
  2065. snd_cs46xx_ac97_write(ac97, AC97_CSR_ACMODE, 0x0);
  2066. } else if (ac97->num == CS46XX_SECONDARY_CODEC_INDEX) {
  2067. snd_printdd("cs46xx: CODEC2 mode %04x\n", 0x3);
  2068. snd_cs46xx_ac97_write(ac97, AC97_CSR_ACMODE, 0x3);
  2069. } else {
  2070. snd_BUG(); /* should never happen ... */
  2071. }
  2072. udelay(50);
  2073. /* it's necessary to wait awhile until registers are accessible after RESET */
  2074. /* because the PCM or MASTER volume registers can be modified, */
  2075. /* the REC_GAIN register is used for tests */
  2076. end_time = jiffies + HZ;
  2077. do {
  2078. unsigned short ext_mid;
  2079. /* use preliminary reads to settle the communication */
  2080. snd_ac97_read(ac97, AC97_RESET);
  2081. snd_ac97_read(ac97, AC97_VENDOR_ID1);
  2082. snd_ac97_read(ac97, AC97_VENDOR_ID2);
  2083. /* modem? */
  2084. ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID);
  2085. if (ext_mid != 0xffff && (ext_mid & 1) != 0)
  2086. return;
  2087. /* test if we can write to the record gain volume register */
  2088. snd_ac97_write(ac97, AC97_REC_GAIN, 0x8a05);
  2089. if ((err = snd_ac97_read(ac97, AC97_REC_GAIN)) == 0x8a05)
  2090. return;
  2091. msleep(10);
  2092. } while (time_after_eq(end_time, jiffies));
  2093. snd_printk(KERN_ERR "CS46xx secondary codec doesn't respond!\n");
  2094. }
  2095. #endif
  2096. static int cs46xx_detect_codec(struct snd_cs46xx *chip, int codec)
  2097. {
  2098. int idx, err;
  2099. struct snd_ac97_template ac97;
  2100. memset(&ac97, 0, sizeof(ac97));
  2101. ac97.private_data = chip;
  2102. ac97.private_free = snd_cs46xx_mixer_free_ac97;
  2103. ac97.num = codec;
  2104. if (chip->amplifier_ctrl == amp_voyetra)
  2105. ac97.scaps = AC97_SCAP_INV_EAPD;
  2106. if (codec == CS46XX_SECONDARY_CODEC_INDEX) {
  2107. snd_cs46xx_codec_write(chip, AC97_RESET, 0, codec);
  2108. udelay(10);
  2109. if (snd_cs46xx_codec_read(chip, AC97_RESET, codec) & 0x8000) {
  2110. snd_printdd("snd_cs46xx: seconadry codec not present\n");
  2111. return -ENXIO;
  2112. }
  2113. }
  2114. snd_cs46xx_codec_write(chip, AC97_MASTER, 0x8000, codec);
  2115. for (idx = 0; idx < 100; ++idx) {
  2116. if (snd_cs46xx_codec_read(chip, AC97_MASTER, codec) == 0x8000) {
  2117. err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97[codec]);
  2118. return err;
  2119. }
  2120. msleep(10);
  2121. }
  2122. snd_printdd("snd_cs46xx: codec %d detection timeout\n", codec);
  2123. return -ENXIO;
  2124. }
  2125. int snd_cs46xx_mixer(struct snd_cs46xx *chip, int spdif_device)
  2126. {
  2127. struct snd_card *card = chip->card;
  2128. struct snd_ctl_elem_id id;
  2129. int err;
  2130. unsigned int idx;
  2131. static struct snd_ac97_bus_ops ops = {
  2132. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2133. .reset = snd_cs46xx_codec_reset,
  2134. #endif
  2135. .write = snd_cs46xx_ac97_write,
  2136. .read = snd_cs46xx_ac97_read,
  2137. };
  2138. /* detect primary codec */
  2139. chip->nr_ac97_codecs = 0;
  2140. snd_printdd("snd_cs46xx: detecting primary codec\n");
  2141. if ((err = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus)) < 0)
  2142. return err;
  2143. chip->ac97_bus->private_free = snd_cs46xx_mixer_free_ac97_bus;
  2144. if (cs46xx_detect_codec(chip, CS46XX_PRIMARY_CODEC_INDEX) < 0)
  2145. return -ENXIO;
  2146. chip->nr_ac97_codecs = 1;
  2147. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2148. snd_printdd("snd_cs46xx: detecting seconadry codec\n");
  2149. /* try detect a secondary codec */
  2150. if (! cs46xx_detect_codec(chip, CS46XX_SECONDARY_CODEC_INDEX))
  2151. chip->nr_ac97_codecs = 2;
  2152. #endif /* CONFIG_SND_CS46XX_NEW_DSP */
  2153. /* add cs4630 mixer controls */
  2154. for (idx = 0; idx < ARRAY_SIZE(snd_cs46xx_controls); idx++) {
  2155. struct snd_kcontrol *kctl;
  2156. kctl = snd_ctl_new1(&snd_cs46xx_controls[idx], chip);
  2157. if (kctl && kctl->id.iface == SNDRV_CTL_ELEM_IFACE_PCM)
  2158. kctl->id.device = spdif_device;
  2159. if ((err = snd_ctl_add(card, kctl)) < 0)
  2160. return err;
  2161. }
  2162. /* get EAPD mixer switch (for voyetra hack) */
  2163. memset(&id, 0, sizeof(id));
  2164. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  2165. strcpy(id.name, "External Amplifier");
  2166. chip->eapd_switch = snd_ctl_find_id(chip->card, &id);
  2167. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2168. if (chip->nr_ac97_codecs == 1) {
  2169. unsigned int id2 = chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]->id & 0xffff;
  2170. if (id2 == 0x592b || id2 == 0x592d) {
  2171. err = snd_ctl_add(card, snd_ctl_new1(&snd_cs46xx_front_dup_ctl, chip));
  2172. if (err < 0)
  2173. return err;
  2174. snd_ac97_write_cache(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX],
  2175. AC97_CSR_ACMODE, 0x200);
  2176. }
  2177. }
  2178. /* do soundcard specific mixer setup */
  2179. if (chip->mixer_init) {
  2180. snd_printdd ("calling chip->mixer_init(chip);\n");
  2181. chip->mixer_init(chip);
  2182. }
  2183. #endif
  2184. /* turn on amplifier */
  2185. chip->amplifier_ctrl(chip, 1);
  2186. return 0;
  2187. }
  2188. /*
  2189. * RawMIDI interface
  2190. */
  2191. static void snd_cs46xx_midi_reset(struct snd_cs46xx *chip)
  2192. {
  2193. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, MIDCR_MRST);
  2194. udelay(100);
  2195. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2196. }
  2197. static int snd_cs46xx_midi_input_open(struct snd_rawmidi_substream *substream)
  2198. {
  2199. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2200. chip->active_ctrl(chip, 1);
  2201. spin_lock_irq(&chip->reg_lock);
  2202. chip->uartm |= CS46XX_MODE_INPUT;
  2203. chip->midcr |= MIDCR_RXE;
  2204. chip->midi_input = substream;
  2205. if (!(chip->uartm & CS46XX_MODE_OUTPUT)) {
  2206. snd_cs46xx_midi_reset(chip);
  2207. } else {
  2208. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2209. }
  2210. spin_unlock_irq(&chip->reg_lock);
  2211. return 0;
  2212. }
  2213. static int snd_cs46xx_midi_input_close(struct snd_rawmidi_substream *substream)
  2214. {
  2215. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2216. spin_lock_irq(&chip->reg_lock);
  2217. chip->midcr &= ~(MIDCR_RXE | MIDCR_RIE);
  2218. chip->midi_input = NULL;
  2219. if (!(chip->uartm & CS46XX_MODE_OUTPUT)) {
  2220. snd_cs46xx_midi_reset(chip);
  2221. } else {
  2222. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2223. }
  2224. chip->uartm &= ~CS46XX_MODE_INPUT;
  2225. spin_unlock_irq(&chip->reg_lock);
  2226. chip->active_ctrl(chip, -1);
  2227. return 0;
  2228. }
  2229. static int snd_cs46xx_midi_output_open(struct snd_rawmidi_substream *substream)
  2230. {
  2231. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2232. chip->active_ctrl(chip, 1);
  2233. spin_lock_irq(&chip->reg_lock);
  2234. chip->uartm |= CS46XX_MODE_OUTPUT;
  2235. chip->midcr |= MIDCR_TXE;
  2236. chip->midi_output = substream;
  2237. if (!(chip->uartm & CS46XX_MODE_INPUT)) {
  2238. snd_cs46xx_midi_reset(chip);
  2239. } else {
  2240. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2241. }
  2242. spin_unlock_irq(&chip->reg_lock);
  2243. return 0;
  2244. }
  2245. static int snd_cs46xx_midi_output_close(struct snd_rawmidi_substream *substream)
  2246. {
  2247. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2248. spin_lock_irq(&chip->reg_lock);
  2249. chip->midcr &= ~(MIDCR_TXE | MIDCR_TIE);
  2250. chip->midi_output = NULL;
  2251. if (!(chip->uartm & CS46XX_MODE_INPUT)) {
  2252. snd_cs46xx_midi_reset(chip);
  2253. } else {
  2254. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2255. }
  2256. chip->uartm &= ~CS46XX_MODE_OUTPUT;
  2257. spin_unlock_irq(&chip->reg_lock);
  2258. chip->active_ctrl(chip, -1);
  2259. return 0;
  2260. }
  2261. static void snd_cs46xx_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  2262. {
  2263. unsigned long flags;
  2264. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2265. spin_lock_irqsave(&chip->reg_lock, flags);
  2266. if (up) {
  2267. if ((chip->midcr & MIDCR_RIE) == 0) {
  2268. chip->midcr |= MIDCR_RIE;
  2269. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2270. }
  2271. } else {
  2272. if (chip->midcr & MIDCR_RIE) {
  2273. chip->midcr &= ~MIDCR_RIE;
  2274. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2275. }
  2276. }
  2277. spin_unlock_irqrestore(&chip->reg_lock, flags);
  2278. }
  2279. static void snd_cs46xx_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  2280. {
  2281. unsigned long flags;
  2282. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2283. unsigned char byte;
  2284. spin_lock_irqsave(&chip->reg_lock, flags);
  2285. if (up) {
  2286. if ((chip->midcr & MIDCR_TIE) == 0) {
  2287. chip->midcr |= MIDCR_TIE;
  2288. /* fill UART FIFO buffer at first, and turn Tx interrupts only if necessary */
  2289. while ((chip->midcr & MIDCR_TIE) &&
  2290. (snd_cs46xx_peekBA0(chip, BA0_MIDSR) & MIDSR_TBF) == 0) {
  2291. if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  2292. chip->midcr &= ~MIDCR_TIE;
  2293. } else {
  2294. snd_cs46xx_pokeBA0(chip, BA0_MIDWP, byte);
  2295. }
  2296. }
  2297. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2298. }
  2299. } else {
  2300. if (chip->midcr & MIDCR_TIE) {
  2301. chip->midcr &= ~MIDCR_TIE;
  2302. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2303. }
  2304. }
  2305. spin_unlock_irqrestore(&chip->reg_lock, flags);
  2306. }
  2307. static struct snd_rawmidi_ops snd_cs46xx_midi_output =
  2308. {
  2309. .open = snd_cs46xx_midi_output_open,
  2310. .close = snd_cs46xx_midi_output_close,
  2311. .trigger = snd_cs46xx_midi_output_trigger,
  2312. };
  2313. static struct snd_rawmidi_ops snd_cs46xx_midi_input =
  2314. {
  2315. .open = snd_cs46xx_midi_input_open,
  2316. .close = snd_cs46xx_midi_input_close,
  2317. .trigger = snd_cs46xx_midi_input_trigger,
  2318. };
  2319. int snd_cs46xx_midi(struct snd_cs46xx *chip, int device, struct snd_rawmidi **rrawmidi)
  2320. {
  2321. struct snd_rawmidi *rmidi;
  2322. int err;
  2323. if (rrawmidi)
  2324. *rrawmidi = NULL;
  2325. if ((err = snd_rawmidi_new(chip->card, "CS46XX", device, 1, 1, &rmidi)) < 0)
  2326. return err;
  2327. strcpy(rmidi->name, "CS46XX");
  2328. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_cs46xx_midi_output);
  2329. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_cs46xx_midi_input);
  2330. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
  2331. rmidi->private_data = chip;
  2332. chip->rmidi = rmidi;
  2333. if (rrawmidi)
  2334. *rrawmidi = NULL;
  2335. return 0;
  2336. }
  2337. /*
  2338. * gameport interface
  2339. */
  2340. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  2341. static void snd_cs46xx_gameport_trigger(struct gameport *gameport)
  2342. {
  2343. struct snd_cs46xx *chip = gameport_get_port_data(gameport);
  2344. if (snd_BUG_ON(!chip))
  2345. return;
  2346. snd_cs46xx_pokeBA0(chip, BA0_JSPT, 0xFF); //outb(gameport->io, 0xFF);
  2347. }
  2348. static unsigned char snd_cs46xx_gameport_read(struct gameport *gameport)
  2349. {
  2350. struct snd_cs46xx *chip = gameport_get_port_data(gameport);
  2351. if (snd_BUG_ON(!chip))
  2352. return 0;
  2353. return snd_cs46xx_peekBA0(chip, BA0_JSPT); //inb(gameport->io);
  2354. }
  2355. static int snd_cs46xx_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  2356. {
  2357. struct snd_cs46xx *chip = gameport_get_port_data(gameport);
  2358. unsigned js1, js2, jst;
  2359. if (snd_BUG_ON(!chip))
  2360. return 0;
  2361. js1 = snd_cs46xx_peekBA0(chip, BA0_JSC1);
  2362. js2 = snd_cs46xx_peekBA0(chip, BA0_JSC2);
  2363. jst = snd_cs46xx_peekBA0(chip, BA0_JSPT);
  2364. *buttons = (~jst >> 4) & 0x0F;
  2365. axes[0] = ((js1 & JSC1_Y1V_MASK) >> JSC1_Y1V_SHIFT) & 0xFFFF;
  2366. axes[1] = ((js1 & JSC1_X1V_MASK) >> JSC1_X1V_SHIFT) & 0xFFFF;
  2367. axes[2] = ((js2 & JSC2_Y2V_MASK) >> JSC2_Y2V_SHIFT) & 0xFFFF;
  2368. axes[3] = ((js2 & JSC2_X2V_MASK) >> JSC2_X2V_SHIFT) & 0xFFFF;
  2369. for(jst=0;jst<4;++jst)
  2370. if(axes[jst]==0xFFFF) axes[jst] = -1;
  2371. return 0;
  2372. }
  2373. static int snd_cs46xx_gameport_open(struct gameport *gameport, int mode)
  2374. {
  2375. switch (mode) {
  2376. case GAMEPORT_MODE_COOKED:
  2377. return 0;
  2378. case GAMEPORT_MODE_RAW:
  2379. return 0;
  2380. default:
  2381. return -1;
  2382. }
  2383. return 0;
  2384. }
  2385. int snd_cs46xx_gameport(struct snd_cs46xx *chip)
  2386. {
  2387. struct gameport *gp;
  2388. chip->gameport = gp = gameport_allocate_port();
  2389. if (!gp) {
  2390. printk(KERN_ERR "cs46xx: cannot allocate memory for gameport\n");
  2391. return -ENOMEM;
  2392. }
  2393. gameport_set_name(gp, "CS46xx Gameport");
  2394. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  2395. gameport_set_dev_parent(gp, &chip->pci->dev);
  2396. gameport_set_port_data(gp, chip);
  2397. gp->open = snd_cs46xx_gameport_open;
  2398. gp->read = snd_cs46xx_gameport_read;
  2399. gp->trigger = snd_cs46xx_gameport_trigger;
  2400. gp->cooked_read = snd_cs46xx_gameport_cooked_read;
  2401. snd_cs46xx_pokeBA0(chip, BA0_JSIO, 0xFF); // ?
  2402. snd_cs46xx_pokeBA0(chip, BA0_JSCTL, JSCTL_SP_MEDIUM_SLOW);
  2403. gameport_register_port(gp);
  2404. return 0;
  2405. }
  2406. static inline void snd_cs46xx_remove_gameport(struct snd_cs46xx *chip)
  2407. {
  2408. if (chip->gameport) {
  2409. gameport_unregister_port(chip->gameport);
  2410. chip->gameport = NULL;
  2411. }
  2412. }
  2413. #else
  2414. int snd_cs46xx_gameport(struct snd_cs46xx *chip) { return -ENOSYS; }
  2415. static inline void snd_cs46xx_remove_gameport(struct snd_cs46xx *chip) { }
  2416. #endif /* CONFIG_GAMEPORT */
  2417. #ifdef CONFIG_PROC_FS
  2418. /*
  2419. * proc interface
  2420. */
  2421. static ssize_t snd_cs46xx_io_read(struct snd_info_entry *entry,
  2422. void *file_private_data,
  2423. struct file *file, char __user *buf,
  2424. size_t count, loff_t pos)
  2425. {
  2426. struct snd_cs46xx_region *region = entry->private_data;
  2427. if (copy_to_user_fromio(buf, region->remap_addr + pos, count))
  2428. return -EFAULT;
  2429. return count;
  2430. }
  2431. static struct snd_info_entry_ops snd_cs46xx_proc_io_ops = {
  2432. .read = snd_cs46xx_io_read,
  2433. };
  2434. static int snd_cs46xx_proc_init(struct snd_card *card, struct snd_cs46xx *chip)
  2435. {
  2436. struct snd_info_entry *entry;
  2437. int idx;
  2438. for (idx = 0; idx < 5; idx++) {
  2439. struct snd_cs46xx_region *region = &chip->region.idx[idx];
  2440. if (! snd_card_proc_new(card, region->name, &entry)) {
  2441. entry->content = SNDRV_INFO_CONTENT_DATA;
  2442. entry->private_data = chip;
  2443. entry->c.ops = &snd_cs46xx_proc_io_ops;
  2444. entry->size = region->size;
  2445. entry->mode = S_IFREG | S_IRUSR;
  2446. }
  2447. }
  2448. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2449. cs46xx_dsp_proc_init(card, chip);
  2450. #endif
  2451. return 0;
  2452. }
  2453. static int snd_cs46xx_proc_done(struct snd_cs46xx *chip)
  2454. {
  2455. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2456. cs46xx_dsp_proc_done(chip);
  2457. #endif
  2458. return 0;
  2459. }
  2460. #else /* !CONFIG_PROC_FS */
  2461. #define snd_cs46xx_proc_init(card, chip)
  2462. #define snd_cs46xx_proc_done(chip)
  2463. #endif
  2464. /*
  2465. * stop the h/w
  2466. */
  2467. static void snd_cs46xx_hw_stop(struct snd_cs46xx *chip)
  2468. {
  2469. unsigned int tmp;
  2470. tmp = snd_cs46xx_peek(chip, BA1_PFIE);
  2471. tmp &= ~0x0000f03f;
  2472. tmp |= 0x00000010;
  2473. snd_cs46xx_poke(chip, BA1_PFIE, tmp); /* playback interrupt disable */
  2474. tmp = snd_cs46xx_peek(chip, BA1_CIE);
  2475. tmp &= ~0x0000003f;
  2476. tmp |= 0x00000011;
  2477. snd_cs46xx_poke(chip, BA1_CIE, tmp); /* capture interrupt disable */
  2478. /*
  2479. * Stop playback DMA.
  2480. */
  2481. tmp = snd_cs46xx_peek(chip, BA1_PCTL);
  2482. snd_cs46xx_poke(chip, BA1_PCTL, tmp & 0x0000ffff);
  2483. /*
  2484. * Stop capture DMA.
  2485. */
  2486. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  2487. snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
  2488. /*
  2489. * Reset the processor.
  2490. */
  2491. snd_cs46xx_reset(chip);
  2492. snd_cs46xx_proc_stop(chip);
  2493. /*
  2494. * Power down the PLL.
  2495. */
  2496. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, 0);
  2497. /*
  2498. * Turn off the Processor by turning off the software clock enable flag in
  2499. * the clock control register.
  2500. */
  2501. tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1) & ~CLKCR1_SWCE;
  2502. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
  2503. }
  2504. static int snd_cs46xx_free(struct snd_cs46xx *chip)
  2505. {
  2506. int idx;
  2507. if (snd_BUG_ON(!chip))
  2508. return -EINVAL;
  2509. if (chip->active_ctrl)
  2510. chip->active_ctrl(chip, 1);
  2511. snd_cs46xx_remove_gameport(chip);
  2512. if (chip->amplifier_ctrl)
  2513. chip->amplifier_ctrl(chip, -chip->amplifier); /* force to off */
  2514. snd_cs46xx_proc_done(chip);
  2515. if (chip->region.idx[0].resource)
  2516. snd_cs46xx_hw_stop(chip);
  2517. if (chip->irq >= 0)
  2518. free_irq(chip->irq, chip);
  2519. if (chip->active_ctrl)
  2520. chip->active_ctrl(chip, -chip->amplifier);
  2521. for (idx = 0; idx < 5; idx++) {
  2522. struct snd_cs46xx_region *region = &chip->region.idx[idx];
  2523. if (region->remap_addr)
  2524. iounmap(region->remap_addr);
  2525. release_and_free_resource(region->resource);
  2526. }
  2527. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2528. if (chip->dsp_spos_instance) {
  2529. cs46xx_dsp_spos_destroy(chip);
  2530. chip->dsp_spos_instance = NULL;
  2531. }
  2532. for (idx = 0; idx < CS46XX_DSP_MODULES; idx++)
  2533. free_module_desc(chip->modules[idx]);
  2534. #else
  2535. vfree(chip->ba1);
  2536. #endif
  2537. #ifdef CONFIG_PM_SLEEP
  2538. kfree(chip->saved_regs);
  2539. #endif
  2540. pci_disable_device(chip->pci);
  2541. kfree(chip);
  2542. return 0;
  2543. }
  2544. static int snd_cs46xx_dev_free(struct snd_device *device)
  2545. {
  2546. struct snd_cs46xx *chip = device->device_data;
  2547. return snd_cs46xx_free(chip);
  2548. }
  2549. /*
  2550. * initialize chip
  2551. */
  2552. static int snd_cs46xx_chip_init(struct snd_cs46xx *chip)
  2553. {
  2554. int timeout;
  2555. /*
  2556. * First, blast the clock control register to zero so that the PLL starts
  2557. * out in a known state, and blast the master serial port control register
  2558. * to zero so that the serial ports also start out in a known state.
  2559. */
  2560. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, 0);
  2561. snd_cs46xx_pokeBA0(chip, BA0_SERMC1, 0);
  2562. /*
  2563. * If we are in AC97 mode, then we must set the part to a host controlled
  2564. * AC-link. Otherwise, we won't be able to bring up the link.
  2565. */
  2566. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2567. snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_2_0 |
  2568. SERACC_TWO_CODECS); /* 2.00 dual codecs */
  2569. /* snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_2_0); */ /* 2.00 codec */
  2570. #else
  2571. snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_1_03); /* 1.03 codec */
  2572. #endif
  2573. /*
  2574. * Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
  2575. * spec) and then drive it high. This is done for non AC97 modes since
  2576. * there might be logic external to the CS461x that uses the ARST# line
  2577. * for a reset.
  2578. */
  2579. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, 0);
  2580. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2581. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, 0);
  2582. #endif
  2583. udelay(50);
  2584. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_RSTN);
  2585. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2586. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_RSTN);
  2587. #endif
  2588. /*
  2589. * The first thing we do here is to enable sync generation. As soon
  2590. * as we start receiving bit clock, we'll start producing the SYNC
  2591. * signal.
  2592. */
  2593. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
  2594. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2595. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_ESYN | ACCTL_RSTN);
  2596. #endif
  2597. /*
  2598. * Now wait for a short while to allow the AC97 part to start
  2599. * generating bit clock (so we don't try to start the PLL without an
  2600. * input clock).
  2601. */
  2602. mdelay(10);
  2603. /*
  2604. * Set the serial port timing configuration, so that
  2605. * the clock control circuit gets its clock from the correct place.
  2606. */
  2607. snd_cs46xx_pokeBA0(chip, BA0_SERMC1, SERMC1_PTC_AC97);
  2608. /*
  2609. * Write the selected clock control setup to the hardware. Do not turn on
  2610. * SWCE yet (if requested), so that the devices clocked by the output of
  2611. * PLL are not clocked until the PLL is stable.
  2612. */
  2613. snd_cs46xx_pokeBA0(chip, BA0_PLLCC, PLLCC_LPF_1050_2780_KHZ | PLLCC_CDR_73_104_MHZ);
  2614. snd_cs46xx_pokeBA0(chip, BA0_PLLM, 0x3a);
  2615. snd_cs46xx_pokeBA0(chip, BA0_CLKCR2, CLKCR2_PDIVS_8);
  2616. /*
  2617. * Power up the PLL.
  2618. */
  2619. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, CLKCR1_PLLP);
  2620. /*
  2621. * Wait until the PLL has stabilized.
  2622. */
  2623. msleep(100);
  2624. /*
  2625. * Turn on clocking of the core so that we can setup the serial ports.
  2626. */
  2627. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, CLKCR1_PLLP | CLKCR1_SWCE);
  2628. /*
  2629. * Enable FIFO Host Bypass
  2630. */
  2631. snd_cs46xx_pokeBA0(chip, BA0_SERBCF, SERBCF_HBP);
  2632. /*
  2633. * Fill the serial port FIFOs with silence.
  2634. */
  2635. snd_cs46xx_clear_serial_FIFOs(chip);
  2636. /*
  2637. * Set the serial port FIFO pointer to the first sample in the FIFO.
  2638. */
  2639. /* snd_cs46xx_pokeBA0(chip, BA0_SERBSP, 0); */
  2640. /*
  2641. * Write the serial port configuration to the part. The master
  2642. * enable bit is not set until all other values have been written.
  2643. */
  2644. snd_cs46xx_pokeBA0(chip, BA0_SERC1, SERC1_SO1F_AC97 | SERC1_SO1EN);
  2645. snd_cs46xx_pokeBA0(chip, BA0_SERC2, SERC2_SI1F_AC97 | SERC1_SO1EN);
  2646. snd_cs46xx_pokeBA0(chip, BA0_SERMC1, SERMC1_PTC_AC97 | SERMC1_MSPE);
  2647. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2648. snd_cs46xx_pokeBA0(chip, BA0_SERC7, SERC7_ASDI2EN);
  2649. snd_cs46xx_pokeBA0(chip, BA0_SERC3, 0);
  2650. snd_cs46xx_pokeBA0(chip, BA0_SERC4, 0);
  2651. snd_cs46xx_pokeBA0(chip, BA0_SERC5, 0);
  2652. snd_cs46xx_pokeBA0(chip, BA0_SERC6, 1);
  2653. #endif
  2654. mdelay(5);
  2655. /*
  2656. * Wait for the codec ready signal from the AC97 codec.
  2657. */
  2658. timeout = 150;
  2659. while (timeout-- > 0) {
  2660. /*
  2661. * Read the AC97 status register to see if we've seen a CODEC READY
  2662. * signal from the AC97 codec.
  2663. */
  2664. if (snd_cs46xx_peekBA0(chip, BA0_ACSTS) & ACSTS_CRDY)
  2665. goto ok1;
  2666. msleep(10);
  2667. }
  2668. snd_printk(KERN_ERR "create - never read codec ready from AC'97\n");
  2669. snd_printk(KERN_ERR "it is not probably bug, try to use CS4236 driver\n");
  2670. return -EIO;
  2671. ok1:
  2672. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2673. {
  2674. int count;
  2675. for (count = 0; count < 150; count++) {
  2676. /* First, we want to wait for a short time. */
  2677. udelay(25);
  2678. if (snd_cs46xx_peekBA0(chip, BA0_ACSTS2) & ACSTS_CRDY)
  2679. break;
  2680. }
  2681. /*
  2682. * Make sure CODEC is READY.
  2683. */
  2684. if (!(snd_cs46xx_peekBA0(chip, BA0_ACSTS2) & ACSTS_CRDY))
  2685. snd_printdd("cs46xx: never read card ready from secondary AC'97\n");
  2686. }
  2687. #endif
  2688. /*
  2689. * Assert the vaid frame signal so that we can start sending commands
  2690. * to the AC97 codec.
  2691. */
  2692. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
  2693. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2694. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
  2695. #endif
  2696. /*
  2697. * Wait until we've sampled input slots 3 and 4 as valid, meaning that
  2698. * the codec is pumping ADC data across the AC-link.
  2699. */
  2700. timeout = 150;
  2701. while (timeout-- > 0) {
  2702. /*
  2703. * Read the input slot valid register and see if input slots 3 and
  2704. * 4 are valid yet.
  2705. */
  2706. if ((snd_cs46xx_peekBA0(chip, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
  2707. goto ok2;
  2708. msleep(10);
  2709. }
  2710. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  2711. snd_printk(KERN_ERR "create - never read ISV3 & ISV4 from AC'97\n");
  2712. return -EIO;
  2713. #else
  2714. /* This may happen on a cold boot with a Terratec SiXPack 5.1.
  2715. Reloading the driver may help, if there's other soundcards
  2716. with the same problem I would like to know. (Benny) */
  2717. snd_printk(KERN_ERR "ERROR: snd-cs46xx: never read ISV3 & ISV4 from AC'97\n");
  2718. snd_printk(KERN_ERR " Try reloading the ALSA driver, if you find something\n");
  2719. snd_printk(KERN_ERR " broken or not working on your soundcard upon\n");
  2720. snd_printk(KERN_ERR " this message please report to alsa-devel@alsa-project.org\n");
  2721. return -EIO;
  2722. #endif
  2723. ok2:
  2724. /*
  2725. * Now, assert valid frame and the slot 3 and 4 valid bits. This will
  2726. * commense the transfer of digital audio data to the AC97 codec.
  2727. */
  2728. snd_cs46xx_pokeBA0(chip, BA0_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
  2729. /*
  2730. * Power down the DAC and ADC. We will power them up (if) when we need
  2731. * them.
  2732. */
  2733. /* snd_cs46xx_pokeBA0(chip, BA0_AC97_POWERDOWN, 0x300); */
  2734. /*
  2735. * Turn off the Processor by turning off the software clock enable flag in
  2736. * the clock control register.
  2737. */
  2738. /* tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1) & ~CLKCR1_SWCE; */
  2739. /* snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp); */
  2740. return 0;
  2741. }
  2742. /*
  2743. * start and load DSP
  2744. */
  2745. static void cs46xx_enable_stream_irqs(struct snd_cs46xx *chip)
  2746. {
  2747. unsigned int tmp;
  2748. snd_cs46xx_pokeBA0(chip, BA0_HICR, HICR_IEV | HICR_CHGM);
  2749. tmp = snd_cs46xx_peek(chip, BA1_PFIE);
  2750. tmp &= ~0x0000f03f;
  2751. snd_cs46xx_poke(chip, BA1_PFIE, tmp); /* playback interrupt enable */
  2752. tmp = snd_cs46xx_peek(chip, BA1_CIE);
  2753. tmp &= ~0x0000003f;
  2754. tmp |= 0x00000001;
  2755. snd_cs46xx_poke(chip, BA1_CIE, tmp); /* capture interrupt enable */
  2756. }
  2757. int snd_cs46xx_start_dsp(struct snd_cs46xx *chip)
  2758. {
  2759. unsigned int tmp;
  2760. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2761. int i;
  2762. #endif
  2763. int err;
  2764. /*
  2765. * Reset the processor.
  2766. */
  2767. snd_cs46xx_reset(chip);
  2768. /*
  2769. * Download the image to the processor.
  2770. */
  2771. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2772. for (i = 0; i < CS46XX_DSP_MODULES; i++) {
  2773. err = load_firmware(chip, &chip->modules[i], module_names[i]);
  2774. if (err < 0) {
  2775. snd_printk(KERN_ERR "firmware load error [%s]\n",
  2776. module_names[i]);
  2777. return err;
  2778. }
  2779. err = cs46xx_dsp_load_module(chip, chip->modules[i]);
  2780. if (err < 0) {
  2781. snd_printk(KERN_ERR "image download error [%s]\n",
  2782. module_names[i]);
  2783. return err;
  2784. }
  2785. }
  2786. if (cs46xx_dsp_scb_and_task_init(chip) < 0)
  2787. return -EIO;
  2788. #else
  2789. err = load_firmware(chip);
  2790. if (err < 0)
  2791. return err;
  2792. /* old image */
  2793. err = snd_cs46xx_download_image(chip);
  2794. if (err < 0) {
  2795. snd_printk(KERN_ERR "image download error\n");
  2796. return err;
  2797. }
  2798. /*
  2799. * Stop playback DMA.
  2800. */
  2801. tmp = snd_cs46xx_peek(chip, BA1_PCTL);
  2802. chip->play_ctl = tmp & 0xffff0000;
  2803. snd_cs46xx_poke(chip, BA1_PCTL, tmp & 0x0000ffff);
  2804. #endif
  2805. /*
  2806. * Stop capture DMA.
  2807. */
  2808. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  2809. chip->capt.ctl = tmp & 0x0000ffff;
  2810. snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
  2811. mdelay(5);
  2812. snd_cs46xx_set_play_sample_rate(chip, 8000);
  2813. snd_cs46xx_set_capture_sample_rate(chip, 8000);
  2814. snd_cs46xx_proc_start(chip);
  2815. cs46xx_enable_stream_irqs(chip);
  2816. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  2817. /* set the attenuation to 0dB */
  2818. snd_cs46xx_poke(chip, BA1_PVOL, 0x80008000);
  2819. snd_cs46xx_poke(chip, BA1_CVOL, 0x80008000);
  2820. #endif
  2821. return 0;
  2822. }
  2823. /*
  2824. * AMP control - null AMP
  2825. */
  2826. static void amp_none(struct snd_cs46xx *chip, int change)
  2827. {
  2828. }
  2829. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2830. static int voyetra_setup_eapd_slot(struct snd_cs46xx *chip)
  2831. {
  2832. u32 idx, valid_slots,tmp,powerdown = 0;
  2833. u16 modem_power,pin_config,logic_type;
  2834. snd_printdd ("cs46xx: cs46xx_setup_eapd_slot()+\n");
  2835. /*
  2836. * See if the devices are powered down. If so, we must power them up first
  2837. * or they will not respond.
  2838. */
  2839. tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1);
  2840. if (!(tmp & CLKCR1_SWCE)) {
  2841. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp | CLKCR1_SWCE);
  2842. powerdown = 1;
  2843. }
  2844. /*
  2845. * Clear PRA. The Bonzo chip will be used for GPIO not for modem
  2846. * stuff.
  2847. */
  2848. if(chip->nr_ac97_codecs != 2) {
  2849. snd_printk (KERN_ERR "cs46xx: cs46xx_setup_eapd_slot() - no secondary codec configured\n");
  2850. return -EINVAL;
  2851. }
  2852. modem_power = snd_cs46xx_codec_read (chip,
  2853. AC97_EXTENDED_MSTATUS,
  2854. CS46XX_SECONDARY_CODEC_INDEX);
  2855. modem_power &=0xFEFF;
  2856. snd_cs46xx_codec_write(chip,
  2857. AC97_EXTENDED_MSTATUS, modem_power,
  2858. CS46XX_SECONDARY_CODEC_INDEX);
  2859. /*
  2860. * Set GPIO pin's 7 and 8 so that they are configured for output.
  2861. */
  2862. pin_config = snd_cs46xx_codec_read (chip,
  2863. AC97_GPIO_CFG,
  2864. CS46XX_SECONDARY_CODEC_INDEX);
  2865. pin_config &=0x27F;
  2866. snd_cs46xx_codec_write(chip,
  2867. AC97_GPIO_CFG, pin_config,
  2868. CS46XX_SECONDARY_CODEC_INDEX);
  2869. /*
  2870. * Set GPIO pin's 7 and 8 so that they are compatible with CMOS logic.
  2871. */
  2872. logic_type = snd_cs46xx_codec_read(chip, AC97_GPIO_POLARITY,
  2873. CS46XX_SECONDARY_CODEC_INDEX);
  2874. logic_type &=0x27F;
  2875. snd_cs46xx_codec_write (chip, AC97_GPIO_POLARITY, logic_type,
  2876. CS46XX_SECONDARY_CODEC_INDEX);
  2877. valid_slots = snd_cs46xx_peekBA0(chip, BA0_ACOSV);
  2878. valid_slots |= 0x200;
  2879. snd_cs46xx_pokeBA0(chip, BA0_ACOSV, valid_slots);
  2880. if ( cs46xx_wait_for_fifo(chip,1) ) {
  2881. snd_printdd("FIFO is busy\n");
  2882. return -EINVAL;
  2883. }
  2884. /*
  2885. * Fill slots 12 with the correct value for the GPIO pins.
  2886. */
  2887. for(idx = 0x90; idx <= 0x9F; idx++) {
  2888. /*
  2889. * Initialize the fifo so that bits 7 and 8 are on.
  2890. *
  2891. * Remember that the GPIO pins in bonzo are shifted by 4 bits to
  2892. * the left. 0x1800 corresponds to bits 7 and 8.
  2893. */
  2894. snd_cs46xx_pokeBA0(chip, BA0_SERBWP, 0x1800);
  2895. /*
  2896. * Wait for command to complete
  2897. */
  2898. if ( cs46xx_wait_for_fifo(chip,200) ) {
  2899. snd_printdd("failed waiting for FIFO at addr (%02X)\n",idx);
  2900. return -EINVAL;
  2901. }
  2902. /*
  2903. * Write the serial port FIFO index.
  2904. */
  2905. snd_cs46xx_pokeBA0(chip, BA0_SERBAD, idx);
  2906. /*
  2907. * Tell the serial port to load the new value into the FIFO location.
  2908. */
  2909. snd_cs46xx_pokeBA0(chip, BA0_SERBCM, SERBCM_WRC);
  2910. }
  2911. /* wait for last command to complete */
  2912. cs46xx_wait_for_fifo(chip,200);
  2913. /*
  2914. * Now, if we powered up the devices, then power them back down again.
  2915. * This is kinda ugly, but should never happen.
  2916. */
  2917. if (powerdown)
  2918. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
  2919. return 0;
  2920. }
  2921. #endif
  2922. /*
  2923. * Crystal EAPD mode
  2924. */
  2925. static void amp_voyetra(struct snd_cs46xx *chip, int change)
  2926. {
  2927. /* Manage the EAPD bit on the Crystal 4297
  2928. and the Analog AD1885 */
  2929. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2930. int old = chip->amplifier;
  2931. #endif
  2932. int oval, val;
  2933. chip->amplifier += change;
  2934. oval = snd_cs46xx_codec_read(chip, AC97_POWERDOWN,
  2935. CS46XX_PRIMARY_CODEC_INDEX);
  2936. val = oval;
  2937. if (chip->amplifier) {
  2938. /* Turn the EAPD amp on */
  2939. val |= 0x8000;
  2940. } else {
  2941. /* Turn the EAPD amp off */
  2942. val &= ~0x8000;
  2943. }
  2944. if (val != oval) {
  2945. snd_cs46xx_codec_write(chip, AC97_POWERDOWN, val,
  2946. CS46XX_PRIMARY_CODEC_INDEX);
  2947. if (chip->eapd_switch)
  2948. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  2949. &chip->eapd_switch->id);
  2950. }
  2951. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2952. if (chip->amplifier && !old) {
  2953. voyetra_setup_eapd_slot(chip);
  2954. }
  2955. #endif
  2956. }
  2957. static void hercules_init(struct snd_cs46xx *chip)
  2958. {
  2959. /* default: AMP off, and SPDIF input optical */
  2960. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, EGPIODR_GPOE0);
  2961. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, EGPIODR_GPOE0);
  2962. }
  2963. /*
  2964. * Game Theatre XP card - EGPIO[2] is used to enable the external amp.
  2965. */
  2966. static void amp_hercules(struct snd_cs46xx *chip, int change)
  2967. {
  2968. int old = chip->amplifier;
  2969. int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
  2970. int val2 = snd_cs46xx_peekBA0(chip, BA0_EGPIOPTR);
  2971. chip->amplifier += change;
  2972. if (chip->amplifier && !old) {
  2973. snd_printdd ("Hercules amplifier ON\n");
  2974. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR,
  2975. EGPIODR_GPOE2 | val1); /* enable EGPIO2 output */
  2976. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR,
  2977. EGPIOPTR_GPPT2 | val2); /* open-drain on output */
  2978. } else if (old && !chip->amplifier) {
  2979. snd_printdd ("Hercules amplifier OFF\n");
  2980. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, val1 & ~EGPIODR_GPOE2); /* disable */
  2981. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, val2 & ~EGPIOPTR_GPPT2); /* disable */
  2982. }
  2983. }
  2984. static void voyetra_mixer_init (struct snd_cs46xx *chip)
  2985. {
  2986. snd_printdd ("initializing Voyetra mixer\n");
  2987. /* Enable SPDIF out */
  2988. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, EGPIODR_GPOE0);
  2989. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, EGPIODR_GPOE0);
  2990. }
  2991. static void hercules_mixer_init (struct snd_cs46xx *chip)
  2992. {
  2993. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2994. unsigned int idx;
  2995. int err;
  2996. struct snd_card *card = chip->card;
  2997. #endif
  2998. /* set EGPIO to default */
  2999. hercules_init(chip);
  3000. snd_printdd ("initializing Hercules mixer\n");
  3001. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3002. if (chip->in_suspend)
  3003. return;
  3004. for (idx = 0 ; idx < ARRAY_SIZE(snd_hercules_controls); idx++) {
  3005. struct snd_kcontrol *kctl;
  3006. kctl = snd_ctl_new1(&snd_hercules_controls[idx], chip);
  3007. if ((err = snd_ctl_add(card, kctl)) < 0) {
  3008. printk (KERN_ERR "cs46xx: failed to initialize Hercules mixer (%d)\n",err);
  3009. break;
  3010. }
  3011. }
  3012. #endif
  3013. }
  3014. #if 0
  3015. /*
  3016. * Untested
  3017. */
  3018. static void amp_voyetra_4294(struct snd_cs46xx *chip, int change)
  3019. {
  3020. chip->amplifier += change;
  3021. if (chip->amplifier) {
  3022. /* Switch the GPIO pins 7 and 8 to open drain */
  3023. snd_cs46xx_codec_write(chip, 0x4C,
  3024. snd_cs46xx_codec_read(chip, 0x4C) & 0xFE7F);
  3025. snd_cs46xx_codec_write(chip, 0x4E,
  3026. snd_cs46xx_codec_read(chip, 0x4E) | 0x0180);
  3027. /* Now wake the AMP (this might be backwards) */
  3028. snd_cs46xx_codec_write(chip, 0x54,
  3029. snd_cs46xx_codec_read(chip, 0x54) & ~0x0180);
  3030. } else {
  3031. snd_cs46xx_codec_write(chip, 0x54,
  3032. snd_cs46xx_codec_read(chip, 0x54) | 0x0180);
  3033. }
  3034. }
  3035. #endif
  3036. /*
  3037. * Handle the CLKRUN on a thinkpad. We must disable CLKRUN support
  3038. * whenever we need to beat on the chip.
  3039. *
  3040. * The original idea and code for this hack comes from David Kaiser at
  3041. * Linuxcare. Perhaps one day Crystal will document their chips well
  3042. * enough to make them useful.
  3043. */
  3044. static void clkrun_hack(struct snd_cs46xx *chip, int change)
  3045. {
  3046. u16 control, nval;
  3047. if (!chip->acpi_port)
  3048. return;
  3049. chip->amplifier += change;
  3050. /* Read ACPI port */
  3051. nval = control = inw(chip->acpi_port + 0x10);
  3052. /* Flip CLKRUN off while running */
  3053. if (! chip->amplifier)
  3054. nval |= 0x2000;
  3055. else
  3056. nval &= ~0x2000;
  3057. if (nval != control)
  3058. outw(nval, chip->acpi_port + 0x10);
  3059. }
  3060. /*
  3061. * detect intel piix4
  3062. */
  3063. static void clkrun_init(struct snd_cs46xx *chip)
  3064. {
  3065. struct pci_dev *pdev;
  3066. u8 pp;
  3067. chip->acpi_port = 0;
  3068. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  3069. PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
  3070. if (pdev == NULL)
  3071. return; /* Not a thinkpad thats for sure */
  3072. /* Find the control port */
  3073. pci_read_config_byte(pdev, 0x41, &pp);
  3074. chip->acpi_port = pp << 8;
  3075. pci_dev_put(pdev);
  3076. }
  3077. /*
  3078. * Card subid table
  3079. */
  3080. struct cs_card_type
  3081. {
  3082. u16 vendor;
  3083. u16 id;
  3084. char *name;
  3085. void (*init)(struct snd_cs46xx *);
  3086. void (*amp)(struct snd_cs46xx *, int);
  3087. void (*active)(struct snd_cs46xx *, int);
  3088. void (*mixer_init)(struct snd_cs46xx *);
  3089. };
  3090. static struct cs_card_type cards[] = {
  3091. {
  3092. .vendor = 0x1489,
  3093. .id = 0x7001,
  3094. .name = "Genius Soundmaker 128 value",
  3095. /* nothing special */
  3096. },
  3097. {
  3098. .vendor = 0x5053,
  3099. .id = 0x3357,
  3100. .name = "Voyetra",
  3101. .amp = amp_voyetra,
  3102. .mixer_init = voyetra_mixer_init,
  3103. },
  3104. {
  3105. .vendor = 0x1071,
  3106. .id = 0x6003,
  3107. .name = "Mitac MI6020/21",
  3108. .amp = amp_voyetra,
  3109. },
  3110. /* Hercules Game Theatre XP */
  3111. {
  3112. .vendor = 0x14af, /* Guillemot Corporation */
  3113. .id = 0x0050,
  3114. .name = "Hercules Game Theatre XP",
  3115. .amp = amp_hercules,
  3116. .mixer_init = hercules_mixer_init,
  3117. },
  3118. {
  3119. .vendor = 0x1681,
  3120. .id = 0x0050,
  3121. .name = "Hercules Game Theatre XP",
  3122. .amp = amp_hercules,
  3123. .mixer_init = hercules_mixer_init,
  3124. },
  3125. {
  3126. .vendor = 0x1681,
  3127. .id = 0x0051,
  3128. .name = "Hercules Game Theatre XP",
  3129. .amp = amp_hercules,
  3130. .mixer_init = hercules_mixer_init,
  3131. },
  3132. {
  3133. .vendor = 0x1681,
  3134. .id = 0x0052,
  3135. .name = "Hercules Game Theatre XP",
  3136. .amp = amp_hercules,
  3137. .mixer_init = hercules_mixer_init,
  3138. },
  3139. {
  3140. .vendor = 0x1681,
  3141. .id = 0x0053,
  3142. .name = "Hercules Game Theatre XP",
  3143. .amp = amp_hercules,
  3144. .mixer_init = hercules_mixer_init,
  3145. },
  3146. {
  3147. .vendor = 0x1681,
  3148. .id = 0x0054,
  3149. .name = "Hercules Game Theatre XP",
  3150. .amp = amp_hercules,
  3151. .mixer_init = hercules_mixer_init,
  3152. },
  3153. /* Herculess Fortissimo */
  3154. {
  3155. .vendor = 0x1681,
  3156. .id = 0xa010,
  3157. .name = "Hercules Gamesurround Fortissimo II",
  3158. },
  3159. {
  3160. .vendor = 0x1681,
  3161. .id = 0xa011,
  3162. .name = "Hercules Gamesurround Fortissimo III 7.1",
  3163. },
  3164. /* Teratec */
  3165. {
  3166. .vendor = 0x153b,
  3167. .id = 0x112e,
  3168. .name = "Terratec DMX XFire 1024",
  3169. },
  3170. {
  3171. .vendor = 0x153b,
  3172. .id = 0x1136,
  3173. .name = "Terratec SiXPack 5.1",
  3174. },
  3175. /* Not sure if the 570 needs the clkrun hack */
  3176. {
  3177. .vendor = PCI_VENDOR_ID_IBM,
  3178. .id = 0x0132,
  3179. .name = "Thinkpad 570",
  3180. .init = clkrun_init,
  3181. .active = clkrun_hack,
  3182. },
  3183. {
  3184. .vendor = PCI_VENDOR_ID_IBM,
  3185. .id = 0x0153,
  3186. .name = "Thinkpad 600X/A20/T20",
  3187. .init = clkrun_init,
  3188. .active = clkrun_hack,
  3189. },
  3190. {
  3191. .vendor = PCI_VENDOR_ID_IBM,
  3192. .id = 0x1010,
  3193. .name = "Thinkpad 600E (unsupported)",
  3194. },
  3195. {} /* terminator */
  3196. };
  3197. /*
  3198. * APM support
  3199. */
  3200. #ifdef CONFIG_PM_SLEEP
  3201. static unsigned int saved_regs[] = {
  3202. BA0_ACOSV,
  3203. /*BA0_ASER_FADDR,*/
  3204. BA0_ASER_MASTER,
  3205. BA1_PVOL,
  3206. BA1_CVOL,
  3207. };
  3208. static int snd_cs46xx_suspend(struct device *dev)
  3209. {
  3210. struct pci_dev *pci = to_pci_dev(dev);
  3211. struct snd_card *card = dev_get_drvdata(dev);
  3212. struct snd_cs46xx *chip = card->private_data;
  3213. int i, amp_saved;
  3214. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  3215. chip->in_suspend = 1;
  3216. snd_pcm_suspend_all(chip->pcm);
  3217. // chip->ac97_powerdown = snd_cs46xx_codec_read(chip, AC97_POWER_CONTROL);
  3218. // chip->ac97_general_purpose = snd_cs46xx_codec_read(chip, BA0_AC97_GENERAL_PURPOSE);
  3219. snd_ac97_suspend(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]);
  3220. snd_ac97_suspend(chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]);
  3221. /* save some registers */
  3222. for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  3223. chip->saved_regs[i] = snd_cs46xx_peekBA0(chip, saved_regs[i]);
  3224. amp_saved = chip->amplifier;
  3225. /* turn off amp */
  3226. chip->amplifier_ctrl(chip, -chip->amplifier);
  3227. snd_cs46xx_hw_stop(chip);
  3228. /* disable CLKRUN */
  3229. chip->active_ctrl(chip, -chip->amplifier);
  3230. chip->amplifier = amp_saved; /* restore the status */
  3231. pci_disable_device(pci);
  3232. pci_save_state(pci);
  3233. pci_set_power_state(pci, PCI_D3hot);
  3234. return 0;
  3235. }
  3236. static int snd_cs46xx_resume(struct device *dev)
  3237. {
  3238. struct pci_dev *pci = to_pci_dev(dev);
  3239. struct snd_card *card = dev_get_drvdata(dev);
  3240. struct snd_cs46xx *chip = card->private_data;
  3241. int amp_saved;
  3242. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3243. int i;
  3244. #endif
  3245. unsigned int tmp;
  3246. pci_set_power_state(pci, PCI_D0);
  3247. pci_restore_state(pci);
  3248. if (pci_enable_device(pci) < 0) {
  3249. printk(KERN_ERR "cs46xx: pci_enable_device failed, "
  3250. "disabling device\n");
  3251. snd_card_disconnect(card);
  3252. return -EIO;
  3253. }
  3254. pci_set_master(pci);
  3255. amp_saved = chip->amplifier;
  3256. chip->amplifier = 0;
  3257. chip->active_ctrl(chip, 1); /* force to on */
  3258. snd_cs46xx_chip_init(chip);
  3259. snd_cs46xx_reset(chip);
  3260. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3261. cs46xx_dsp_resume(chip);
  3262. /* restore some registers */
  3263. for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  3264. snd_cs46xx_pokeBA0(chip, saved_regs[i], chip->saved_regs[i]);
  3265. #else
  3266. snd_cs46xx_download_image(chip);
  3267. #endif
  3268. #if 0
  3269. snd_cs46xx_codec_write(chip, BA0_AC97_GENERAL_PURPOSE,
  3270. chip->ac97_general_purpose);
  3271. snd_cs46xx_codec_write(chip, AC97_POWER_CONTROL,
  3272. chip->ac97_powerdown);
  3273. mdelay(10);
  3274. snd_cs46xx_codec_write(chip, BA0_AC97_POWERDOWN,
  3275. chip->ac97_powerdown);
  3276. mdelay(5);
  3277. #endif
  3278. snd_ac97_resume(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]);
  3279. snd_ac97_resume(chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]);
  3280. /*
  3281. * Stop capture DMA.
  3282. */
  3283. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  3284. chip->capt.ctl = tmp & 0x0000ffff;
  3285. snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
  3286. mdelay(5);
  3287. /* reset playback/capture */
  3288. snd_cs46xx_set_play_sample_rate(chip, 8000);
  3289. snd_cs46xx_set_capture_sample_rate(chip, 8000);
  3290. snd_cs46xx_proc_start(chip);
  3291. cs46xx_enable_stream_irqs(chip);
  3292. if (amp_saved)
  3293. chip->amplifier_ctrl(chip, 1); /* turn amp on */
  3294. else
  3295. chip->active_ctrl(chip, -1); /* disable CLKRUN */
  3296. chip->amplifier = amp_saved;
  3297. chip->in_suspend = 0;
  3298. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  3299. return 0;
  3300. }
  3301. SIMPLE_DEV_PM_OPS(snd_cs46xx_pm, snd_cs46xx_suspend, snd_cs46xx_resume);
  3302. #endif /* CONFIG_PM_SLEEP */
  3303. /*
  3304. */
  3305. int snd_cs46xx_create(struct snd_card *card,
  3306. struct pci_dev *pci,
  3307. int external_amp, int thinkpad,
  3308. struct snd_cs46xx **rchip)
  3309. {
  3310. struct snd_cs46xx *chip;
  3311. int err, idx;
  3312. struct snd_cs46xx_region *region;
  3313. struct cs_card_type *cp;
  3314. u16 ss_card, ss_vendor;
  3315. static struct snd_device_ops ops = {
  3316. .dev_free = snd_cs46xx_dev_free,
  3317. };
  3318. *rchip = NULL;
  3319. /* enable PCI device */
  3320. if ((err = pci_enable_device(pci)) < 0)
  3321. return err;
  3322. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  3323. if (chip == NULL) {
  3324. pci_disable_device(pci);
  3325. return -ENOMEM;
  3326. }
  3327. spin_lock_init(&chip->reg_lock);
  3328. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3329. mutex_init(&chip->spos_mutex);
  3330. #endif
  3331. chip->card = card;
  3332. chip->pci = pci;
  3333. chip->irq = -1;
  3334. chip->ba0_addr = pci_resource_start(pci, 0);
  3335. chip->ba1_addr = pci_resource_start(pci, 1);
  3336. if (chip->ba0_addr == 0 || chip->ba0_addr == (unsigned long)~0 ||
  3337. chip->ba1_addr == 0 || chip->ba1_addr == (unsigned long)~0) {
  3338. snd_printk(KERN_ERR "wrong address(es) - ba0 = 0x%lx, ba1 = 0x%lx\n",
  3339. chip->ba0_addr, chip->ba1_addr);
  3340. snd_cs46xx_free(chip);
  3341. return -ENOMEM;
  3342. }
  3343. region = &chip->region.name.ba0;
  3344. strcpy(region->name, "CS46xx_BA0");
  3345. region->base = chip->ba0_addr;
  3346. region->size = CS46XX_BA0_SIZE;
  3347. region = &chip->region.name.data0;
  3348. strcpy(region->name, "CS46xx_BA1_data0");
  3349. region->base = chip->ba1_addr + BA1_SP_DMEM0;
  3350. region->size = CS46XX_BA1_DATA0_SIZE;
  3351. region = &chip->region.name.data1;
  3352. strcpy(region->name, "CS46xx_BA1_data1");
  3353. region->base = chip->ba1_addr + BA1_SP_DMEM1;
  3354. region->size = CS46XX_BA1_DATA1_SIZE;
  3355. region = &chip->region.name.pmem;
  3356. strcpy(region->name, "CS46xx_BA1_pmem");
  3357. region->base = chip->ba1_addr + BA1_SP_PMEM;
  3358. region->size = CS46XX_BA1_PRG_SIZE;
  3359. region = &chip->region.name.reg;
  3360. strcpy(region->name, "CS46xx_BA1_reg");
  3361. region->base = chip->ba1_addr + BA1_SP_REG;
  3362. region->size = CS46XX_BA1_REG_SIZE;
  3363. /* set up amp and clkrun hack */
  3364. pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &ss_vendor);
  3365. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &ss_card);
  3366. for (cp = &cards[0]; cp->name; cp++) {
  3367. if (cp->vendor == ss_vendor && cp->id == ss_card) {
  3368. snd_printdd ("hack for %s enabled\n", cp->name);
  3369. chip->amplifier_ctrl = cp->amp;
  3370. chip->active_ctrl = cp->active;
  3371. chip->mixer_init = cp->mixer_init;
  3372. if (cp->init)
  3373. cp->init(chip);
  3374. break;
  3375. }
  3376. }
  3377. if (external_amp) {
  3378. snd_printk(KERN_INFO "Crystal EAPD support forced on.\n");
  3379. chip->amplifier_ctrl = amp_voyetra;
  3380. }
  3381. if (thinkpad) {
  3382. snd_printk(KERN_INFO "Activating CLKRUN hack for Thinkpad.\n");
  3383. chip->active_ctrl = clkrun_hack;
  3384. clkrun_init(chip);
  3385. }
  3386. if (chip->amplifier_ctrl == NULL)
  3387. chip->amplifier_ctrl = amp_none;
  3388. if (chip->active_ctrl == NULL)
  3389. chip->active_ctrl = amp_none;
  3390. chip->active_ctrl(chip, 1); /* enable CLKRUN */
  3391. pci_set_master(pci);
  3392. for (idx = 0; idx < 5; idx++) {
  3393. region = &chip->region.idx[idx];
  3394. if ((region->resource = request_mem_region(region->base, region->size,
  3395. region->name)) == NULL) {
  3396. snd_printk(KERN_ERR "unable to request memory region 0x%lx-0x%lx\n",
  3397. region->base, region->base + region->size - 1);
  3398. snd_cs46xx_free(chip);
  3399. return -EBUSY;
  3400. }
  3401. region->remap_addr = ioremap_nocache(region->base, region->size);
  3402. if (region->remap_addr == NULL) {
  3403. snd_printk(KERN_ERR "%s ioremap problem\n", region->name);
  3404. snd_cs46xx_free(chip);
  3405. return -ENOMEM;
  3406. }
  3407. }
  3408. if (request_irq(pci->irq, snd_cs46xx_interrupt, IRQF_SHARED,
  3409. KBUILD_MODNAME, chip)) {
  3410. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  3411. snd_cs46xx_free(chip);
  3412. return -EBUSY;
  3413. }
  3414. chip->irq = pci->irq;
  3415. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3416. chip->dsp_spos_instance = cs46xx_dsp_spos_create(chip);
  3417. if (chip->dsp_spos_instance == NULL) {
  3418. snd_cs46xx_free(chip);
  3419. return -ENOMEM;
  3420. }
  3421. #endif
  3422. err = snd_cs46xx_chip_init(chip);
  3423. if (err < 0) {
  3424. snd_cs46xx_free(chip);
  3425. return err;
  3426. }
  3427. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  3428. snd_cs46xx_free(chip);
  3429. return err;
  3430. }
  3431. snd_cs46xx_proc_init(card, chip);
  3432. #ifdef CONFIG_PM_SLEEP
  3433. chip->saved_regs = kmalloc(sizeof(*chip->saved_regs) *
  3434. ARRAY_SIZE(saved_regs), GFP_KERNEL);
  3435. if (!chip->saved_regs) {
  3436. snd_cs46xx_free(chip);
  3437. return -ENOMEM;
  3438. }
  3439. #endif
  3440. chip->active_ctrl(chip, -1); /* disable CLKRUN */
  3441. *rchip = chip;
  3442. return 0;
  3443. }