cs46xx_lib.c 105 KB

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