cs46xx_lib.c 107 KB

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