cs46xx_lib.c 108 KB

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