cs46xx_lib.c 104 KB

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