cs46xx_lib.c 105 KB

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