cs46xx_lib.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889
  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 "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 0xffff;
  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 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 snd_cs46xx_pcm_rear(struct snd_cs46xx *chip, int device,
  1394. struct snd_pcm **rpcm)
  1395. {
  1396. struct snd_pcm *pcm;
  1397. int err;
  1398. if (rpcm)
  1399. *rpcm = NULL;
  1400. if ((err = snd_pcm_new(chip->card, "CS46xx - Rear", device, MAX_PLAYBACK_CHANNELS, 0, &pcm)) < 0)
  1401. return err;
  1402. pcm->private_data = chip;
  1403. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_rear_ops);
  1404. /* global setup */
  1405. pcm->info_flags = 0;
  1406. strcpy(pcm->name, "CS46xx - Rear");
  1407. chip->pcm_rear = pcm;
  1408. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1409. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1410. if (rpcm)
  1411. *rpcm = pcm;
  1412. return 0;
  1413. }
  1414. int snd_cs46xx_pcm_center_lfe(struct snd_cs46xx *chip, int device,
  1415. struct snd_pcm **rpcm)
  1416. {
  1417. struct snd_pcm *pcm;
  1418. int err;
  1419. if (rpcm)
  1420. *rpcm = NULL;
  1421. if ((err = snd_pcm_new(chip->card, "CS46xx - Center LFE", device, MAX_PLAYBACK_CHANNELS, 0, &pcm)) < 0)
  1422. return err;
  1423. pcm->private_data = chip;
  1424. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_clfe_ops);
  1425. /* global setup */
  1426. pcm->info_flags = 0;
  1427. strcpy(pcm->name, "CS46xx - Center LFE");
  1428. chip->pcm_center_lfe = pcm;
  1429. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1430. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1431. if (rpcm)
  1432. *rpcm = pcm;
  1433. return 0;
  1434. }
  1435. int snd_cs46xx_pcm_iec958(struct snd_cs46xx *chip, int device,
  1436. struct snd_pcm **rpcm)
  1437. {
  1438. struct snd_pcm *pcm;
  1439. int err;
  1440. if (rpcm)
  1441. *rpcm = NULL;
  1442. if ((err = snd_pcm_new(chip->card, "CS46xx - IEC958", device, 1, 0, &pcm)) < 0)
  1443. return err;
  1444. pcm->private_data = chip;
  1445. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_iec958_ops);
  1446. /* global setup */
  1447. pcm->info_flags = 0;
  1448. strcpy(pcm->name, "CS46xx - IEC958");
  1449. chip->pcm_rear = pcm;
  1450. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1451. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1452. if (rpcm)
  1453. *rpcm = pcm;
  1454. return 0;
  1455. }
  1456. #endif
  1457. /*
  1458. * Mixer routines
  1459. */
  1460. static void snd_cs46xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
  1461. {
  1462. struct snd_cs46xx *chip = bus->private_data;
  1463. chip->ac97_bus = NULL;
  1464. }
  1465. static void snd_cs46xx_mixer_free_ac97(struct snd_ac97 *ac97)
  1466. {
  1467. struct snd_cs46xx *chip = ac97->private_data;
  1468. if (snd_BUG_ON(ac97 != chip->ac97[CS46XX_PRIMARY_CODEC_INDEX] &&
  1469. ac97 != chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]))
  1470. return;
  1471. if (ac97 == chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]) {
  1472. chip->ac97[CS46XX_PRIMARY_CODEC_INDEX] = NULL;
  1473. chip->eapd_switch = NULL;
  1474. }
  1475. else
  1476. chip->ac97[CS46XX_SECONDARY_CODEC_INDEX] = NULL;
  1477. }
  1478. static int snd_cs46xx_vol_info(struct snd_kcontrol *kcontrol,
  1479. struct snd_ctl_elem_info *uinfo)
  1480. {
  1481. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1482. uinfo->count = 2;
  1483. uinfo->value.integer.min = 0;
  1484. uinfo->value.integer.max = 0x7fff;
  1485. return 0;
  1486. }
  1487. static int snd_cs46xx_vol_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1488. {
  1489. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1490. int reg = kcontrol->private_value;
  1491. unsigned int val = snd_cs46xx_peek(chip, reg);
  1492. ucontrol->value.integer.value[0] = 0xffff - (val >> 16);
  1493. ucontrol->value.integer.value[1] = 0xffff - (val & 0xffff);
  1494. return 0;
  1495. }
  1496. static int snd_cs46xx_vol_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1497. {
  1498. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1499. int reg = kcontrol->private_value;
  1500. unsigned int val = ((0xffff - ucontrol->value.integer.value[0]) << 16 |
  1501. (0xffff - ucontrol->value.integer.value[1]));
  1502. unsigned int old = snd_cs46xx_peek(chip, reg);
  1503. int change = (old != val);
  1504. if (change) {
  1505. snd_cs46xx_poke(chip, reg, val);
  1506. }
  1507. return change;
  1508. }
  1509. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1510. static int snd_cs46xx_vol_dac_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1511. {
  1512. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1513. ucontrol->value.integer.value[0] = chip->dsp_spos_instance->dac_volume_left;
  1514. ucontrol->value.integer.value[1] = chip->dsp_spos_instance->dac_volume_right;
  1515. return 0;
  1516. }
  1517. static int snd_cs46xx_vol_dac_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1518. {
  1519. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1520. int change = 0;
  1521. if (chip->dsp_spos_instance->dac_volume_right != ucontrol->value.integer.value[0] ||
  1522. chip->dsp_spos_instance->dac_volume_left != ucontrol->value.integer.value[1]) {
  1523. cs46xx_dsp_set_dac_volume(chip,
  1524. ucontrol->value.integer.value[0],
  1525. ucontrol->value.integer.value[1]);
  1526. change = 1;
  1527. }
  1528. return change;
  1529. }
  1530. #if 0
  1531. static int snd_cs46xx_vol_iec958_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1532. {
  1533. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1534. ucontrol->value.integer.value[0] = chip->dsp_spos_instance->spdif_input_volume_left;
  1535. ucontrol->value.integer.value[1] = chip->dsp_spos_instance->spdif_input_volume_right;
  1536. return 0;
  1537. }
  1538. static int snd_cs46xx_vol_iec958_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1539. {
  1540. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1541. int change = 0;
  1542. if (chip->dsp_spos_instance->spdif_input_volume_left != ucontrol->value.integer.value[0] ||
  1543. chip->dsp_spos_instance->spdif_input_volume_right!= ucontrol->value.integer.value[1]) {
  1544. cs46xx_dsp_set_iec958_volume (chip,
  1545. ucontrol->value.integer.value[0],
  1546. ucontrol->value.integer.value[1]);
  1547. change = 1;
  1548. }
  1549. return change;
  1550. }
  1551. #endif
  1552. #define snd_mixer_boolean_info snd_ctl_boolean_mono_info
  1553. static int snd_cs46xx_iec958_get(struct snd_kcontrol *kcontrol,
  1554. struct snd_ctl_elem_value *ucontrol)
  1555. {
  1556. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1557. int reg = kcontrol->private_value;
  1558. if (reg == CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT)
  1559. ucontrol->value.integer.value[0] = (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED);
  1560. else
  1561. ucontrol->value.integer.value[0] = chip->dsp_spos_instance->spdif_status_in;
  1562. return 0;
  1563. }
  1564. static int snd_cs46xx_iec958_put(struct snd_kcontrol *kcontrol,
  1565. struct snd_ctl_elem_value *ucontrol)
  1566. {
  1567. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1568. int change, res;
  1569. switch (kcontrol->private_value) {
  1570. case CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT:
  1571. mutex_lock(&chip->spos_mutex);
  1572. change = (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED);
  1573. if (ucontrol->value.integer.value[0] && !change)
  1574. cs46xx_dsp_enable_spdif_out(chip);
  1575. else if (change && !ucontrol->value.integer.value[0])
  1576. cs46xx_dsp_disable_spdif_out(chip);
  1577. res = (change != (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED));
  1578. mutex_unlock(&chip->spos_mutex);
  1579. break;
  1580. case CS46XX_MIXER_SPDIF_INPUT_ELEMENT:
  1581. change = chip->dsp_spos_instance->spdif_status_in;
  1582. if (ucontrol->value.integer.value[0] && !change) {
  1583. cs46xx_dsp_enable_spdif_in(chip);
  1584. /* restore volume */
  1585. }
  1586. else if (change && !ucontrol->value.integer.value[0])
  1587. cs46xx_dsp_disable_spdif_in(chip);
  1588. res = (change != chip->dsp_spos_instance->spdif_status_in);
  1589. break;
  1590. default:
  1591. res = -EINVAL;
  1592. snd_BUG(); /* should never happen ... */
  1593. }
  1594. return res;
  1595. }
  1596. static int snd_cs46xx_adc_capture_get(struct snd_kcontrol *kcontrol,
  1597. struct snd_ctl_elem_value *ucontrol)
  1598. {
  1599. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1600. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1601. if (ins->adc_input != NULL)
  1602. ucontrol->value.integer.value[0] = 1;
  1603. else
  1604. ucontrol->value.integer.value[0] = 0;
  1605. return 0;
  1606. }
  1607. static int snd_cs46xx_adc_capture_put(struct snd_kcontrol *kcontrol,
  1608. struct snd_ctl_elem_value *ucontrol)
  1609. {
  1610. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1611. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1612. int change = 0;
  1613. if (ucontrol->value.integer.value[0] && !ins->adc_input) {
  1614. cs46xx_dsp_enable_adc_capture(chip);
  1615. change = 1;
  1616. } else if (!ucontrol->value.integer.value[0] && ins->adc_input) {
  1617. cs46xx_dsp_disable_adc_capture(chip);
  1618. change = 1;
  1619. }
  1620. return change;
  1621. }
  1622. static int snd_cs46xx_pcm_capture_get(struct snd_kcontrol *kcontrol,
  1623. struct snd_ctl_elem_value *ucontrol)
  1624. {
  1625. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1626. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1627. if (ins->pcm_input != NULL)
  1628. ucontrol->value.integer.value[0] = 1;
  1629. else
  1630. ucontrol->value.integer.value[0] = 0;
  1631. return 0;
  1632. }
  1633. static int snd_cs46xx_pcm_capture_put(struct snd_kcontrol *kcontrol,
  1634. struct snd_ctl_elem_value *ucontrol)
  1635. {
  1636. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1637. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1638. int change = 0;
  1639. if (ucontrol->value.integer.value[0] && !ins->pcm_input) {
  1640. cs46xx_dsp_enable_pcm_capture(chip);
  1641. change = 1;
  1642. } else if (!ucontrol->value.integer.value[0] && ins->pcm_input) {
  1643. cs46xx_dsp_disable_pcm_capture(chip);
  1644. change = 1;
  1645. }
  1646. return change;
  1647. }
  1648. static int snd_herc_spdif_select_get(struct snd_kcontrol *kcontrol,
  1649. struct snd_ctl_elem_value *ucontrol)
  1650. {
  1651. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1652. int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
  1653. if (val1 & EGPIODR_GPOE0)
  1654. ucontrol->value.integer.value[0] = 1;
  1655. else
  1656. ucontrol->value.integer.value[0] = 0;
  1657. return 0;
  1658. }
  1659. /*
  1660. * Game Theatre XP card - EGPIO[0] is used to select SPDIF input optical or coaxial.
  1661. */
  1662. static int snd_herc_spdif_select_put(struct snd_kcontrol *kcontrol,
  1663. struct snd_ctl_elem_value *ucontrol)
  1664. {
  1665. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1666. int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
  1667. int val2 = snd_cs46xx_peekBA0(chip, BA0_EGPIOPTR);
  1668. if (ucontrol->value.integer.value[0]) {
  1669. /* optical is default */
  1670. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR,
  1671. EGPIODR_GPOE0 | val1); /* enable EGPIO0 output */
  1672. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR,
  1673. EGPIOPTR_GPPT0 | val2); /* open-drain on output */
  1674. } else {
  1675. /* coaxial */
  1676. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, val1 & ~EGPIODR_GPOE0); /* disable */
  1677. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, val2 & ~EGPIOPTR_GPPT0); /* disable */
  1678. }
  1679. /* checking diff from the EGPIO direction register
  1680. should be enough */
  1681. return (val1 != (int)snd_cs46xx_peekBA0(chip, BA0_EGPIODR));
  1682. }
  1683. static int snd_cs46xx_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1684. {
  1685. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1686. uinfo->count = 1;
  1687. return 0;
  1688. }
  1689. static int snd_cs46xx_spdif_default_get(struct snd_kcontrol *kcontrol,
  1690. struct snd_ctl_elem_value *ucontrol)
  1691. {
  1692. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1693. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1694. mutex_lock(&chip->spos_mutex);
  1695. ucontrol->value.iec958.status[0] = _wrap_all_bits((ins->spdif_csuv_default >> 24) & 0xff);
  1696. ucontrol->value.iec958.status[1] = _wrap_all_bits((ins->spdif_csuv_default >> 16) & 0xff);
  1697. ucontrol->value.iec958.status[2] = 0;
  1698. ucontrol->value.iec958.status[3] = _wrap_all_bits((ins->spdif_csuv_default) & 0xff);
  1699. mutex_unlock(&chip->spos_mutex);
  1700. return 0;
  1701. }
  1702. static int snd_cs46xx_spdif_default_put(struct snd_kcontrol *kcontrol,
  1703. struct snd_ctl_elem_value *ucontrol)
  1704. {
  1705. struct snd_cs46xx * chip = snd_kcontrol_chip(kcontrol);
  1706. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1707. unsigned int val;
  1708. int change;
  1709. mutex_lock(&chip->spos_mutex);
  1710. val = ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[0]) << 24) |
  1711. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[2]) << 16) |
  1712. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[3])) |
  1713. /* left and right validity bit */
  1714. (1 << 13) | (1 << 12);
  1715. change = (unsigned int)ins->spdif_csuv_default != val;
  1716. ins->spdif_csuv_default = val;
  1717. if ( !(ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN) )
  1718. cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV,val);
  1719. mutex_unlock(&chip->spos_mutex);
  1720. return change;
  1721. }
  1722. static int snd_cs46xx_spdif_mask_get(struct snd_kcontrol *kcontrol,
  1723. struct snd_ctl_elem_value *ucontrol)
  1724. {
  1725. ucontrol->value.iec958.status[0] = 0xff;
  1726. ucontrol->value.iec958.status[1] = 0xff;
  1727. ucontrol->value.iec958.status[2] = 0x00;
  1728. ucontrol->value.iec958.status[3] = 0xff;
  1729. return 0;
  1730. }
  1731. static int snd_cs46xx_spdif_stream_get(struct snd_kcontrol *kcontrol,
  1732. struct snd_ctl_elem_value *ucontrol)
  1733. {
  1734. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1735. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1736. mutex_lock(&chip->spos_mutex);
  1737. ucontrol->value.iec958.status[0] = _wrap_all_bits((ins->spdif_csuv_stream >> 24) & 0xff);
  1738. ucontrol->value.iec958.status[1] = _wrap_all_bits((ins->spdif_csuv_stream >> 16) & 0xff);
  1739. ucontrol->value.iec958.status[2] = 0;
  1740. ucontrol->value.iec958.status[3] = _wrap_all_bits((ins->spdif_csuv_stream) & 0xff);
  1741. mutex_unlock(&chip->spos_mutex);
  1742. return 0;
  1743. }
  1744. static int snd_cs46xx_spdif_stream_put(struct snd_kcontrol *kcontrol,
  1745. struct snd_ctl_elem_value *ucontrol)
  1746. {
  1747. struct snd_cs46xx * chip = snd_kcontrol_chip(kcontrol);
  1748. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1749. unsigned int val;
  1750. int change;
  1751. mutex_lock(&chip->spos_mutex);
  1752. val = ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[0]) << 24) |
  1753. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[1]) << 16) |
  1754. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[3])) |
  1755. /* left and right validity bit */
  1756. (1 << 13) | (1 << 12);
  1757. change = ins->spdif_csuv_stream != val;
  1758. ins->spdif_csuv_stream = val;
  1759. if ( ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN )
  1760. cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV,val);
  1761. mutex_unlock(&chip->spos_mutex);
  1762. return change;
  1763. }
  1764. #endif /* CONFIG_SND_CS46XX_NEW_DSP */
  1765. static struct snd_kcontrol_new snd_cs46xx_controls[] = {
  1766. {
  1767. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1768. .name = "DAC Volume",
  1769. .info = snd_cs46xx_vol_info,
  1770. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  1771. .get = snd_cs46xx_vol_get,
  1772. .put = snd_cs46xx_vol_put,
  1773. .private_value = BA1_PVOL,
  1774. #else
  1775. .get = snd_cs46xx_vol_dac_get,
  1776. .put = snd_cs46xx_vol_dac_put,
  1777. #endif
  1778. },
  1779. {
  1780. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1781. .name = "ADC Volume",
  1782. .info = snd_cs46xx_vol_info,
  1783. .get = snd_cs46xx_vol_get,
  1784. .put = snd_cs46xx_vol_put,
  1785. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  1786. .private_value = BA1_CVOL,
  1787. #else
  1788. .private_value = (VARIDECIMATE_SCB_ADDR + 0xE) << 2,
  1789. #endif
  1790. },
  1791. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1792. {
  1793. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1794. .name = "ADC Capture Switch",
  1795. .info = snd_mixer_boolean_info,
  1796. .get = snd_cs46xx_adc_capture_get,
  1797. .put = snd_cs46xx_adc_capture_put
  1798. },
  1799. {
  1800. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1801. .name = "DAC Capture Switch",
  1802. .info = snd_mixer_boolean_info,
  1803. .get = snd_cs46xx_pcm_capture_get,
  1804. .put = snd_cs46xx_pcm_capture_put
  1805. },
  1806. {
  1807. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1808. .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
  1809. .info = snd_mixer_boolean_info,
  1810. .get = snd_cs46xx_iec958_get,
  1811. .put = snd_cs46xx_iec958_put,
  1812. .private_value = CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT,
  1813. },
  1814. {
  1815. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1816. .name = SNDRV_CTL_NAME_IEC958("Input ",NONE,SWITCH),
  1817. .info = snd_mixer_boolean_info,
  1818. .get = snd_cs46xx_iec958_get,
  1819. .put = snd_cs46xx_iec958_put,
  1820. .private_value = CS46XX_MIXER_SPDIF_INPUT_ELEMENT,
  1821. },
  1822. #if 0
  1823. /* Input IEC958 volume does not work for the moment. (Benny) */
  1824. {
  1825. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1826. .name = SNDRV_CTL_NAME_IEC958("Input ",NONE,VOLUME),
  1827. .info = snd_cs46xx_vol_info,
  1828. .get = snd_cs46xx_vol_iec958_get,
  1829. .put = snd_cs46xx_vol_iec958_put,
  1830. .private_value = (ASYNCRX_SCB_ADDR + 0xE) << 2,
  1831. },
  1832. #endif
  1833. {
  1834. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1835. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1836. .info = snd_cs46xx_spdif_info,
  1837. .get = snd_cs46xx_spdif_default_get,
  1838. .put = snd_cs46xx_spdif_default_put,
  1839. },
  1840. {
  1841. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1842. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  1843. .info = snd_cs46xx_spdif_info,
  1844. .get = snd_cs46xx_spdif_mask_get,
  1845. .access = SNDRV_CTL_ELEM_ACCESS_READ
  1846. },
  1847. {
  1848. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1849. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  1850. .info = snd_cs46xx_spdif_info,
  1851. .get = snd_cs46xx_spdif_stream_get,
  1852. .put = snd_cs46xx_spdif_stream_put
  1853. },
  1854. #endif
  1855. };
  1856. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1857. /* set primary cs4294 codec into Extended Audio Mode */
  1858. static int snd_cs46xx_front_dup_get(struct snd_kcontrol *kcontrol,
  1859. struct snd_ctl_elem_value *ucontrol)
  1860. {
  1861. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1862. unsigned short val;
  1863. val = snd_ac97_read(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX], AC97_CSR_ACMODE);
  1864. ucontrol->value.integer.value[0] = (val & 0x200) ? 0 : 1;
  1865. return 0;
  1866. }
  1867. static int snd_cs46xx_front_dup_put(struct snd_kcontrol *kcontrol,
  1868. struct snd_ctl_elem_value *ucontrol)
  1869. {
  1870. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1871. return snd_ac97_update_bits(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX],
  1872. AC97_CSR_ACMODE, 0x200,
  1873. ucontrol->value.integer.value[0] ? 0 : 0x200);
  1874. }
  1875. static struct snd_kcontrol_new snd_cs46xx_front_dup_ctl = {
  1876. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1877. .name = "Duplicate Front",
  1878. .info = snd_mixer_boolean_info,
  1879. .get = snd_cs46xx_front_dup_get,
  1880. .put = snd_cs46xx_front_dup_put,
  1881. };
  1882. #endif
  1883. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1884. /* Only available on the Hercules Game Theater XP soundcard */
  1885. static struct snd_kcontrol_new snd_hercules_controls[] = {
  1886. {
  1887. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1888. .name = "Optical/Coaxial SPDIF Input Switch",
  1889. .info = snd_mixer_boolean_info,
  1890. .get = snd_herc_spdif_select_get,
  1891. .put = snd_herc_spdif_select_put,
  1892. },
  1893. };
  1894. static void snd_cs46xx_codec_reset (struct snd_ac97 * ac97)
  1895. {
  1896. unsigned long end_time;
  1897. int err;
  1898. /* reset to defaults */
  1899. snd_ac97_write(ac97, AC97_RESET, 0);
  1900. /* set the desired CODEC mode */
  1901. if (ac97->num == CS46XX_PRIMARY_CODEC_INDEX) {
  1902. snd_printdd("cs46xx: CODEC1 mode %04x\n", 0x0);
  1903. snd_cs46xx_ac97_write(ac97, AC97_CSR_ACMODE, 0x0);
  1904. } else if (ac97->num == CS46XX_SECONDARY_CODEC_INDEX) {
  1905. snd_printdd("cs46xx: CODEC2 mode %04x\n", 0x3);
  1906. snd_cs46xx_ac97_write(ac97, AC97_CSR_ACMODE, 0x3);
  1907. } else {
  1908. snd_BUG(); /* should never happen ... */
  1909. }
  1910. udelay(50);
  1911. /* it's necessary to wait awhile until registers are accessible after RESET */
  1912. /* because the PCM or MASTER volume registers can be modified, */
  1913. /* the REC_GAIN register is used for tests */
  1914. end_time = jiffies + HZ;
  1915. do {
  1916. unsigned short ext_mid;
  1917. /* use preliminary reads to settle the communication */
  1918. snd_ac97_read(ac97, AC97_RESET);
  1919. snd_ac97_read(ac97, AC97_VENDOR_ID1);
  1920. snd_ac97_read(ac97, AC97_VENDOR_ID2);
  1921. /* modem? */
  1922. ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID);
  1923. if (ext_mid != 0xffff && (ext_mid & 1) != 0)
  1924. return;
  1925. /* test if we can write to the record gain volume register */
  1926. snd_ac97_write(ac97, AC97_REC_GAIN, 0x8a05);
  1927. if ((err = snd_ac97_read(ac97, AC97_REC_GAIN)) == 0x8a05)
  1928. return;
  1929. msleep(10);
  1930. } while (time_after_eq(end_time, jiffies));
  1931. snd_printk(KERN_ERR "CS46xx secondary codec doesn't respond!\n");
  1932. }
  1933. #endif
  1934. static int cs46xx_detect_codec(struct snd_cs46xx *chip, int codec)
  1935. {
  1936. int idx, err;
  1937. struct snd_ac97_template ac97;
  1938. memset(&ac97, 0, sizeof(ac97));
  1939. ac97.private_data = chip;
  1940. ac97.private_free = snd_cs46xx_mixer_free_ac97;
  1941. ac97.num = codec;
  1942. if (chip->amplifier_ctrl == amp_voyetra)
  1943. ac97.scaps = AC97_SCAP_INV_EAPD;
  1944. if (codec == CS46XX_SECONDARY_CODEC_INDEX) {
  1945. snd_cs46xx_codec_write(chip, AC97_RESET, 0, codec);
  1946. udelay(10);
  1947. if (snd_cs46xx_codec_read(chip, AC97_RESET, codec) & 0x8000) {
  1948. snd_printdd("snd_cs46xx: seconadry codec not present\n");
  1949. return -ENXIO;
  1950. }
  1951. }
  1952. snd_cs46xx_codec_write(chip, AC97_MASTER, 0x8000, codec);
  1953. for (idx = 0; idx < 100; ++idx) {
  1954. if (snd_cs46xx_codec_read(chip, AC97_MASTER, codec) == 0x8000) {
  1955. err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97[codec]);
  1956. return err;
  1957. }
  1958. msleep(10);
  1959. }
  1960. snd_printdd("snd_cs46xx: codec %d detection timeout\n", codec);
  1961. return -ENXIO;
  1962. }
  1963. int snd_cs46xx_mixer(struct snd_cs46xx *chip, int spdif_device)
  1964. {
  1965. struct snd_card *card = chip->card;
  1966. struct snd_ctl_elem_id id;
  1967. int err;
  1968. unsigned int idx;
  1969. static struct snd_ac97_bus_ops ops = {
  1970. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1971. .reset = snd_cs46xx_codec_reset,
  1972. #endif
  1973. .write = snd_cs46xx_ac97_write,
  1974. .read = snd_cs46xx_ac97_read,
  1975. };
  1976. /* detect primary codec */
  1977. chip->nr_ac97_codecs = 0;
  1978. snd_printdd("snd_cs46xx: detecting primary codec\n");
  1979. if ((err = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus)) < 0)
  1980. return err;
  1981. chip->ac97_bus->private_free = snd_cs46xx_mixer_free_ac97_bus;
  1982. if (cs46xx_detect_codec(chip, CS46XX_PRIMARY_CODEC_INDEX) < 0)
  1983. return -ENXIO;
  1984. chip->nr_ac97_codecs = 1;
  1985. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1986. snd_printdd("snd_cs46xx: detecting seconadry codec\n");
  1987. /* try detect a secondary codec */
  1988. if (! cs46xx_detect_codec(chip, CS46XX_SECONDARY_CODEC_INDEX))
  1989. chip->nr_ac97_codecs = 2;
  1990. #endif /* CONFIG_SND_CS46XX_NEW_DSP */
  1991. /* add cs4630 mixer controls */
  1992. for (idx = 0; idx < ARRAY_SIZE(snd_cs46xx_controls); idx++) {
  1993. struct snd_kcontrol *kctl;
  1994. kctl = snd_ctl_new1(&snd_cs46xx_controls[idx], chip);
  1995. if (kctl && kctl->id.iface == SNDRV_CTL_ELEM_IFACE_PCM)
  1996. kctl->id.device = spdif_device;
  1997. if ((err = snd_ctl_add(card, kctl)) < 0)
  1998. return err;
  1999. }
  2000. /* get EAPD mixer switch (for voyetra hack) */
  2001. memset(&id, 0, sizeof(id));
  2002. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  2003. strcpy(id.name, "External Amplifier");
  2004. chip->eapd_switch = snd_ctl_find_id(chip->card, &id);
  2005. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2006. if (chip->nr_ac97_codecs == 1) {
  2007. unsigned int id2 = chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]->id & 0xffff;
  2008. if (id2 == 0x592b || id2 == 0x592d) {
  2009. err = snd_ctl_add(card, snd_ctl_new1(&snd_cs46xx_front_dup_ctl, chip));
  2010. if (err < 0)
  2011. return err;
  2012. snd_ac97_write_cache(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX],
  2013. AC97_CSR_ACMODE, 0x200);
  2014. }
  2015. }
  2016. /* do soundcard specific mixer setup */
  2017. if (chip->mixer_init) {
  2018. snd_printdd ("calling chip->mixer_init(chip);\n");
  2019. chip->mixer_init(chip);
  2020. }
  2021. #endif
  2022. /* turn on amplifier */
  2023. chip->amplifier_ctrl(chip, 1);
  2024. return 0;
  2025. }
  2026. /*
  2027. * RawMIDI interface
  2028. */
  2029. static void snd_cs46xx_midi_reset(struct snd_cs46xx *chip)
  2030. {
  2031. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, MIDCR_MRST);
  2032. udelay(100);
  2033. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2034. }
  2035. static int snd_cs46xx_midi_input_open(struct snd_rawmidi_substream *substream)
  2036. {
  2037. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2038. chip->active_ctrl(chip, 1);
  2039. spin_lock_irq(&chip->reg_lock);
  2040. chip->uartm |= CS46XX_MODE_INPUT;
  2041. chip->midcr |= MIDCR_RXE;
  2042. chip->midi_input = substream;
  2043. if (!(chip->uartm & CS46XX_MODE_OUTPUT)) {
  2044. snd_cs46xx_midi_reset(chip);
  2045. } else {
  2046. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2047. }
  2048. spin_unlock_irq(&chip->reg_lock);
  2049. return 0;
  2050. }
  2051. static int snd_cs46xx_midi_input_close(struct snd_rawmidi_substream *substream)
  2052. {
  2053. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2054. spin_lock_irq(&chip->reg_lock);
  2055. chip->midcr &= ~(MIDCR_RXE | MIDCR_RIE);
  2056. chip->midi_input = NULL;
  2057. if (!(chip->uartm & CS46XX_MODE_OUTPUT)) {
  2058. snd_cs46xx_midi_reset(chip);
  2059. } else {
  2060. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2061. }
  2062. chip->uartm &= ~CS46XX_MODE_INPUT;
  2063. spin_unlock_irq(&chip->reg_lock);
  2064. chip->active_ctrl(chip, -1);
  2065. return 0;
  2066. }
  2067. static int snd_cs46xx_midi_output_open(struct snd_rawmidi_substream *substream)
  2068. {
  2069. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2070. chip->active_ctrl(chip, 1);
  2071. spin_lock_irq(&chip->reg_lock);
  2072. chip->uartm |= CS46XX_MODE_OUTPUT;
  2073. chip->midcr |= MIDCR_TXE;
  2074. chip->midi_output = substream;
  2075. if (!(chip->uartm & CS46XX_MODE_INPUT)) {
  2076. snd_cs46xx_midi_reset(chip);
  2077. } else {
  2078. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2079. }
  2080. spin_unlock_irq(&chip->reg_lock);
  2081. return 0;
  2082. }
  2083. static int snd_cs46xx_midi_output_close(struct snd_rawmidi_substream *substream)
  2084. {
  2085. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2086. spin_lock_irq(&chip->reg_lock);
  2087. chip->midcr &= ~(MIDCR_TXE | MIDCR_TIE);
  2088. chip->midi_output = NULL;
  2089. if (!(chip->uartm & CS46XX_MODE_INPUT)) {
  2090. snd_cs46xx_midi_reset(chip);
  2091. } else {
  2092. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2093. }
  2094. chip->uartm &= ~CS46XX_MODE_OUTPUT;
  2095. spin_unlock_irq(&chip->reg_lock);
  2096. chip->active_ctrl(chip, -1);
  2097. return 0;
  2098. }
  2099. static void snd_cs46xx_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  2100. {
  2101. unsigned long flags;
  2102. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2103. spin_lock_irqsave(&chip->reg_lock, flags);
  2104. if (up) {
  2105. if ((chip->midcr & MIDCR_RIE) == 0) {
  2106. chip->midcr |= MIDCR_RIE;
  2107. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2108. }
  2109. } else {
  2110. if (chip->midcr & MIDCR_RIE) {
  2111. chip->midcr &= ~MIDCR_RIE;
  2112. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2113. }
  2114. }
  2115. spin_unlock_irqrestore(&chip->reg_lock, flags);
  2116. }
  2117. static void snd_cs46xx_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  2118. {
  2119. unsigned long flags;
  2120. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2121. unsigned char byte;
  2122. spin_lock_irqsave(&chip->reg_lock, flags);
  2123. if (up) {
  2124. if ((chip->midcr & MIDCR_TIE) == 0) {
  2125. chip->midcr |= MIDCR_TIE;
  2126. /* fill UART FIFO buffer at first, and turn Tx interrupts only if necessary */
  2127. while ((chip->midcr & MIDCR_TIE) &&
  2128. (snd_cs46xx_peekBA0(chip, BA0_MIDSR) & MIDSR_TBF) == 0) {
  2129. if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  2130. chip->midcr &= ~MIDCR_TIE;
  2131. } else {
  2132. snd_cs46xx_pokeBA0(chip, BA0_MIDWP, byte);
  2133. }
  2134. }
  2135. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2136. }
  2137. } else {
  2138. if (chip->midcr & MIDCR_TIE) {
  2139. chip->midcr &= ~MIDCR_TIE;
  2140. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2141. }
  2142. }
  2143. spin_unlock_irqrestore(&chip->reg_lock, flags);
  2144. }
  2145. static struct snd_rawmidi_ops snd_cs46xx_midi_output =
  2146. {
  2147. .open = snd_cs46xx_midi_output_open,
  2148. .close = snd_cs46xx_midi_output_close,
  2149. .trigger = snd_cs46xx_midi_output_trigger,
  2150. };
  2151. static struct snd_rawmidi_ops snd_cs46xx_midi_input =
  2152. {
  2153. .open = snd_cs46xx_midi_input_open,
  2154. .close = snd_cs46xx_midi_input_close,
  2155. .trigger = snd_cs46xx_midi_input_trigger,
  2156. };
  2157. int snd_cs46xx_midi(struct snd_cs46xx *chip, int device, struct snd_rawmidi **rrawmidi)
  2158. {
  2159. struct snd_rawmidi *rmidi;
  2160. int err;
  2161. if (rrawmidi)
  2162. *rrawmidi = NULL;
  2163. if ((err = snd_rawmidi_new(chip->card, "CS46XX", device, 1, 1, &rmidi)) < 0)
  2164. return err;
  2165. strcpy(rmidi->name, "CS46XX");
  2166. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_cs46xx_midi_output);
  2167. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_cs46xx_midi_input);
  2168. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
  2169. rmidi->private_data = chip;
  2170. chip->rmidi = rmidi;
  2171. if (rrawmidi)
  2172. *rrawmidi = NULL;
  2173. return 0;
  2174. }
  2175. /*
  2176. * gameport interface
  2177. */
  2178. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  2179. static void snd_cs46xx_gameport_trigger(struct gameport *gameport)
  2180. {
  2181. struct snd_cs46xx *chip = gameport_get_port_data(gameport);
  2182. if (snd_BUG_ON(!chip))
  2183. return;
  2184. snd_cs46xx_pokeBA0(chip, BA0_JSPT, 0xFF); //outb(gameport->io, 0xFF);
  2185. }
  2186. static unsigned char snd_cs46xx_gameport_read(struct gameport *gameport)
  2187. {
  2188. struct snd_cs46xx *chip = gameport_get_port_data(gameport);
  2189. if (snd_BUG_ON(!chip))
  2190. return 0;
  2191. return snd_cs46xx_peekBA0(chip, BA0_JSPT); //inb(gameport->io);
  2192. }
  2193. static int snd_cs46xx_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  2194. {
  2195. struct snd_cs46xx *chip = gameport_get_port_data(gameport);
  2196. unsigned js1, js2, jst;
  2197. if (snd_BUG_ON(!chip))
  2198. return 0;
  2199. js1 = snd_cs46xx_peekBA0(chip, BA0_JSC1);
  2200. js2 = snd_cs46xx_peekBA0(chip, BA0_JSC2);
  2201. jst = snd_cs46xx_peekBA0(chip, BA0_JSPT);
  2202. *buttons = (~jst >> 4) & 0x0F;
  2203. axes[0] = ((js1 & JSC1_Y1V_MASK) >> JSC1_Y1V_SHIFT) & 0xFFFF;
  2204. axes[1] = ((js1 & JSC1_X1V_MASK) >> JSC1_X1V_SHIFT) & 0xFFFF;
  2205. axes[2] = ((js2 & JSC2_Y2V_MASK) >> JSC2_Y2V_SHIFT) & 0xFFFF;
  2206. axes[3] = ((js2 & JSC2_X2V_MASK) >> JSC2_X2V_SHIFT) & 0xFFFF;
  2207. for(jst=0;jst<4;++jst)
  2208. if(axes[jst]==0xFFFF) axes[jst] = -1;
  2209. return 0;
  2210. }
  2211. static int snd_cs46xx_gameport_open(struct gameport *gameport, int mode)
  2212. {
  2213. switch (mode) {
  2214. case GAMEPORT_MODE_COOKED:
  2215. return 0;
  2216. case GAMEPORT_MODE_RAW:
  2217. return 0;
  2218. default:
  2219. return -1;
  2220. }
  2221. return 0;
  2222. }
  2223. int snd_cs46xx_gameport(struct snd_cs46xx *chip)
  2224. {
  2225. struct gameport *gp;
  2226. chip->gameport = gp = gameport_allocate_port();
  2227. if (!gp) {
  2228. printk(KERN_ERR "cs46xx: cannot allocate memory for gameport\n");
  2229. return -ENOMEM;
  2230. }
  2231. gameport_set_name(gp, "CS46xx Gameport");
  2232. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  2233. gameport_set_dev_parent(gp, &chip->pci->dev);
  2234. gameport_set_port_data(gp, chip);
  2235. gp->open = snd_cs46xx_gameport_open;
  2236. gp->read = snd_cs46xx_gameport_read;
  2237. gp->trigger = snd_cs46xx_gameport_trigger;
  2238. gp->cooked_read = snd_cs46xx_gameport_cooked_read;
  2239. snd_cs46xx_pokeBA0(chip, BA0_JSIO, 0xFF); // ?
  2240. snd_cs46xx_pokeBA0(chip, BA0_JSCTL, JSCTL_SP_MEDIUM_SLOW);
  2241. gameport_register_port(gp);
  2242. return 0;
  2243. }
  2244. static inline void snd_cs46xx_remove_gameport(struct snd_cs46xx *chip)
  2245. {
  2246. if (chip->gameport) {
  2247. gameport_unregister_port(chip->gameport);
  2248. chip->gameport = NULL;
  2249. }
  2250. }
  2251. #else
  2252. int snd_cs46xx_gameport(struct snd_cs46xx *chip) { return -ENOSYS; }
  2253. static inline void snd_cs46xx_remove_gameport(struct snd_cs46xx *chip) { }
  2254. #endif /* CONFIG_GAMEPORT */
  2255. #ifdef CONFIG_PROC_FS
  2256. /*
  2257. * proc interface
  2258. */
  2259. static ssize_t snd_cs46xx_io_read(struct snd_info_entry *entry,
  2260. void *file_private_data,
  2261. struct file *file, char __user *buf,
  2262. size_t count, loff_t pos)
  2263. {
  2264. struct snd_cs46xx_region *region = entry->private_data;
  2265. if (copy_to_user_fromio(buf, region->remap_addr + pos, count))
  2266. return -EFAULT;
  2267. return count;
  2268. }
  2269. static struct snd_info_entry_ops snd_cs46xx_proc_io_ops = {
  2270. .read = snd_cs46xx_io_read,
  2271. };
  2272. static int snd_cs46xx_proc_init(struct snd_card *card, struct snd_cs46xx *chip)
  2273. {
  2274. struct snd_info_entry *entry;
  2275. int idx;
  2276. for (idx = 0; idx < 5; idx++) {
  2277. struct snd_cs46xx_region *region = &chip->region.idx[idx];
  2278. if (! snd_card_proc_new(card, region->name, &entry)) {
  2279. entry->content = SNDRV_INFO_CONTENT_DATA;
  2280. entry->private_data = chip;
  2281. entry->c.ops = &snd_cs46xx_proc_io_ops;
  2282. entry->size = region->size;
  2283. entry->mode = S_IFREG | S_IRUSR;
  2284. }
  2285. }
  2286. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2287. cs46xx_dsp_proc_init(card, chip);
  2288. #endif
  2289. return 0;
  2290. }
  2291. static int snd_cs46xx_proc_done(struct snd_cs46xx *chip)
  2292. {
  2293. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2294. cs46xx_dsp_proc_done(chip);
  2295. #endif
  2296. return 0;
  2297. }
  2298. #else /* !CONFIG_PROC_FS */
  2299. #define snd_cs46xx_proc_init(card, chip)
  2300. #define snd_cs46xx_proc_done(chip)
  2301. #endif
  2302. /*
  2303. * stop the h/w
  2304. */
  2305. static void snd_cs46xx_hw_stop(struct snd_cs46xx *chip)
  2306. {
  2307. unsigned int tmp;
  2308. tmp = snd_cs46xx_peek(chip, BA1_PFIE);
  2309. tmp &= ~0x0000f03f;
  2310. tmp |= 0x00000010;
  2311. snd_cs46xx_poke(chip, BA1_PFIE, tmp); /* playback interrupt disable */
  2312. tmp = snd_cs46xx_peek(chip, BA1_CIE);
  2313. tmp &= ~0x0000003f;
  2314. tmp |= 0x00000011;
  2315. snd_cs46xx_poke(chip, BA1_CIE, tmp); /* capture interrupt disable */
  2316. /*
  2317. * Stop playback DMA.
  2318. */
  2319. tmp = snd_cs46xx_peek(chip, BA1_PCTL);
  2320. snd_cs46xx_poke(chip, BA1_PCTL, tmp & 0x0000ffff);
  2321. /*
  2322. * Stop capture DMA.
  2323. */
  2324. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  2325. snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
  2326. /*
  2327. * Reset the processor.
  2328. */
  2329. snd_cs46xx_reset(chip);
  2330. snd_cs46xx_proc_stop(chip);
  2331. /*
  2332. * Power down the PLL.
  2333. */
  2334. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, 0);
  2335. /*
  2336. * Turn off the Processor by turning off the software clock enable flag in
  2337. * the clock control register.
  2338. */
  2339. tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1) & ~CLKCR1_SWCE;
  2340. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
  2341. }
  2342. static int snd_cs46xx_free(struct snd_cs46xx *chip)
  2343. {
  2344. int idx;
  2345. if (snd_BUG_ON(!chip))
  2346. return -EINVAL;
  2347. if (chip->active_ctrl)
  2348. chip->active_ctrl(chip, 1);
  2349. snd_cs46xx_remove_gameport(chip);
  2350. if (chip->amplifier_ctrl)
  2351. chip->amplifier_ctrl(chip, -chip->amplifier); /* force to off */
  2352. snd_cs46xx_proc_done(chip);
  2353. if (chip->region.idx[0].resource)
  2354. snd_cs46xx_hw_stop(chip);
  2355. if (chip->irq >= 0)
  2356. free_irq(chip->irq, chip);
  2357. if (chip->active_ctrl)
  2358. chip->active_ctrl(chip, -chip->amplifier);
  2359. for (idx = 0; idx < 5; idx++) {
  2360. struct snd_cs46xx_region *region = &chip->region.idx[idx];
  2361. if (region->remap_addr)
  2362. iounmap(region->remap_addr);
  2363. release_and_free_resource(region->resource);
  2364. }
  2365. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2366. if (chip->dsp_spos_instance) {
  2367. cs46xx_dsp_spos_destroy(chip);
  2368. chip->dsp_spos_instance = NULL;
  2369. }
  2370. #endif
  2371. #ifdef CONFIG_PM_SLEEP
  2372. kfree(chip->saved_regs);
  2373. #endif
  2374. pci_disable_device(chip->pci);
  2375. kfree(chip);
  2376. return 0;
  2377. }
  2378. static int snd_cs46xx_dev_free(struct snd_device *device)
  2379. {
  2380. struct snd_cs46xx *chip = device->device_data;
  2381. return snd_cs46xx_free(chip);
  2382. }
  2383. /*
  2384. * initialize chip
  2385. */
  2386. static int snd_cs46xx_chip_init(struct snd_cs46xx *chip)
  2387. {
  2388. int timeout;
  2389. /*
  2390. * First, blast the clock control register to zero so that the PLL starts
  2391. * out in a known state, and blast the master serial port control register
  2392. * to zero so that the serial ports also start out in a known state.
  2393. */
  2394. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, 0);
  2395. snd_cs46xx_pokeBA0(chip, BA0_SERMC1, 0);
  2396. /*
  2397. * If we are in AC97 mode, then we must set the part to a host controlled
  2398. * AC-link. Otherwise, we won't be able to bring up the link.
  2399. */
  2400. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2401. snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_2_0 |
  2402. SERACC_TWO_CODECS); /* 2.00 dual codecs */
  2403. /* snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_2_0); */ /* 2.00 codec */
  2404. #else
  2405. snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_1_03); /* 1.03 codec */
  2406. #endif
  2407. /*
  2408. * Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
  2409. * spec) and then drive it high. This is done for non AC97 modes since
  2410. * there might be logic external to the CS461x that uses the ARST# line
  2411. * for a reset.
  2412. */
  2413. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, 0);
  2414. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2415. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, 0);
  2416. #endif
  2417. udelay(50);
  2418. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_RSTN);
  2419. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2420. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_RSTN);
  2421. #endif
  2422. /*
  2423. * The first thing we do here is to enable sync generation. As soon
  2424. * as we start receiving bit clock, we'll start producing the SYNC
  2425. * signal.
  2426. */
  2427. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
  2428. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2429. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_ESYN | ACCTL_RSTN);
  2430. #endif
  2431. /*
  2432. * Now wait for a short while to allow the AC97 part to start
  2433. * generating bit clock (so we don't try to start the PLL without an
  2434. * input clock).
  2435. */
  2436. mdelay(10);
  2437. /*
  2438. * Set the serial port timing configuration, so that
  2439. * the clock control circuit gets its clock from the correct place.
  2440. */
  2441. snd_cs46xx_pokeBA0(chip, BA0_SERMC1, SERMC1_PTC_AC97);
  2442. /*
  2443. * Write the selected clock control setup to the hardware. Do not turn on
  2444. * SWCE yet (if requested), so that the devices clocked by the output of
  2445. * PLL are not clocked until the PLL is stable.
  2446. */
  2447. snd_cs46xx_pokeBA0(chip, BA0_PLLCC, PLLCC_LPF_1050_2780_KHZ | PLLCC_CDR_73_104_MHZ);
  2448. snd_cs46xx_pokeBA0(chip, BA0_PLLM, 0x3a);
  2449. snd_cs46xx_pokeBA0(chip, BA0_CLKCR2, CLKCR2_PDIVS_8);
  2450. /*
  2451. * Power up the PLL.
  2452. */
  2453. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, CLKCR1_PLLP);
  2454. /*
  2455. * Wait until the PLL has stabilized.
  2456. */
  2457. msleep(100);
  2458. /*
  2459. * Turn on clocking of the core so that we can setup the serial ports.
  2460. */
  2461. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, CLKCR1_PLLP | CLKCR1_SWCE);
  2462. /*
  2463. * Enable FIFO Host Bypass
  2464. */
  2465. snd_cs46xx_pokeBA0(chip, BA0_SERBCF, SERBCF_HBP);
  2466. /*
  2467. * Fill the serial port FIFOs with silence.
  2468. */
  2469. snd_cs46xx_clear_serial_FIFOs(chip);
  2470. /*
  2471. * Set the serial port FIFO pointer to the first sample in the FIFO.
  2472. */
  2473. /* snd_cs46xx_pokeBA0(chip, BA0_SERBSP, 0); */
  2474. /*
  2475. * Write the serial port configuration to the part. The master
  2476. * enable bit is not set until all other values have been written.
  2477. */
  2478. snd_cs46xx_pokeBA0(chip, BA0_SERC1, SERC1_SO1F_AC97 | SERC1_SO1EN);
  2479. snd_cs46xx_pokeBA0(chip, BA0_SERC2, SERC2_SI1F_AC97 | SERC1_SO1EN);
  2480. snd_cs46xx_pokeBA0(chip, BA0_SERMC1, SERMC1_PTC_AC97 | SERMC1_MSPE);
  2481. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2482. snd_cs46xx_pokeBA0(chip, BA0_SERC7, SERC7_ASDI2EN);
  2483. snd_cs46xx_pokeBA0(chip, BA0_SERC3, 0);
  2484. snd_cs46xx_pokeBA0(chip, BA0_SERC4, 0);
  2485. snd_cs46xx_pokeBA0(chip, BA0_SERC5, 0);
  2486. snd_cs46xx_pokeBA0(chip, BA0_SERC6, 1);
  2487. #endif
  2488. mdelay(5);
  2489. /*
  2490. * Wait for the codec ready signal from the AC97 codec.
  2491. */
  2492. timeout = 150;
  2493. while (timeout-- > 0) {
  2494. /*
  2495. * Read the AC97 status register to see if we've seen a CODEC READY
  2496. * signal from the AC97 codec.
  2497. */
  2498. if (snd_cs46xx_peekBA0(chip, BA0_ACSTS) & ACSTS_CRDY)
  2499. goto ok1;
  2500. msleep(10);
  2501. }
  2502. snd_printk(KERN_ERR "create - never read codec ready from AC'97\n");
  2503. snd_printk(KERN_ERR "it is not probably bug, try to use CS4236 driver\n");
  2504. return -EIO;
  2505. ok1:
  2506. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2507. {
  2508. int count;
  2509. for (count = 0; count < 150; count++) {
  2510. /* First, we want to wait for a short time. */
  2511. udelay(25);
  2512. if (snd_cs46xx_peekBA0(chip, BA0_ACSTS2) & ACSTS_CRDY)
  2513. break;
  2514. }
  2515. /*
  2516. * Make sure CODEC is READY.
  2517. */
  2518. if (!(snd_cs46xx_peekBA0(chip, BA0_ACSTS2) & ACSTS_CRDY))
  2519. snd_printdd("cs46xx: never read card ready from secondary AC'97\n");
  2520. }
  2521. #endif
  2522. /*
  2523. * Assert the vaid frame signal so that we can start sending commands
  2524. * to the AC97 codec.
  2525. */
  2526. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
  2527. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2528. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
  2529. #endif
  2530. /*
  2531. * Wait until we've sampled input slots 3 and 4 as valid, meaning that
  2532. * the codec is pumping ADC data across the AC-link.
  2533. */
  2534. timeout = 150;
  2535. while (timeout-- > 0) {
  2536. /*
  2537. * Read the input slot valid register and see if input slots 3 and
  2538. * 4 are valid yet.
  2539. */
  2540. if ((snd_cs46xx_peekBA0(chip, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
  2541. goto ok2;
  2542. msleep(10);
  2543. }
  2544. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  2545. snd_printk(KERN_ERR "create - never read ISV3 & ISV4 from AC'97\n");
  2546. return -EIO;
  2547. #else
  2548. /* This may happen on a cold boot with a Terratec SiXPack 5.1.
  2549. Reloading the driver may help, if there's other soundcards
  2550. with the same problem I would like to know. (Benny) */
  2551. snd_printk(KERN_ERR "ERROR: snd-cs46xx: never read ISV3 & ISV4 from AC'97\n");
  2552. snd_printk(KERN_ERR " Try reloading the ALSA driver, if you find something\n");
  2553. snd_printk(KERN_ERR " broken or not working on your soundcard upon\n");
  2554. snd_printk(KERN_ERR " this message please report to alsa-devel@alsa-project.org\n");
  2555. return -EIO;
  2556. #endif
  2557. ok2:
  2558. /*
  2559. * Now, assert valid frame and the slot 3 and 4 valid bits. This will
  2560. * commense the transfer of digital audio data to the AC97 codec.
  2561. */
  2562. snd_cs46xx_pokeBA0(chip, BA0_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
  2563. /*
  2564. * Power down the DAC and ADC. We will power them up (if) when we need
  2565. * them.
  2566. */
  2567. /* snd_cs46xx_pokeBA0(chip, BA0_AC97_POWERDOWN, 0x300); */
  2568. /*
  2569. * Turn off the Processor by turning off the software clock enable flag in
  2570. * the clock control register.
  2571. */
  2572. /* tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1) & ~CLKCR1_SWCE; */
  2573. /* snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp); */
  2574. return 0;
  2575. }
  2576. /*
  2577. * start and load DSP
  2578. */
  2579. static void cs46xx_enable_stream_irqs(struct snd_cs46xx *chip)
  2580. {
  2581. unsigned int tmp;
  2582. snd_cs46xx_pokeBA0(chip, BA0_HICR, HICR_IEV | HICR_CHGM);
  2583. tmp = snd_cs46xx_peek(chip, BA1_PFIE);
  2584. tmp &= ~0x0000f03f;
  2585. snd_cs46xx_poke(chip, BA1_PFIE, tmp); /* playback interrupt enable */
  2586. tmp = snd_cs46xx_peek(chip, BA1_CIE);
  2587. tmp &= ~0x0000003f;
  2588. tmp |= 0x00000001;
  2589. snd_cs46xx_poke(chip, BA1_CIE, tmp); /* capture interrupt enable */
  2590. }
  2591. int snd_cs46xx_start_dsp(struct snd_cs46xx *chip)
  2592. {
  2593. unsigned int tmp;
  2594. /*
  2595. * Reset the processor.
  2596. */
  2597. snd_cs46xx_reset(chip);
  2598. /*
  2599. * Download the image to the processor.
  2600. */
  2601. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2602. #if 0
  2603. if (cs46xx_dsp_load_module(chip, &cwcemb80_module) < 0) {
  2604. snd_printk(KERN_ERR "image download error\n");
  2605. return -EIO;
  2606. }
  2607. #endif
  2608. if (cs46xx_dsp_load_module(chip, &cwc4630_module) < 0) {
  2609. snd_printk(KERN_ERR "image download error [cwc4630]\n");
  2610. return -EIO;
  2611. }
  2612. if (cs46xx_dsp_load_module(chip, &cwcasync_module) < 0) {
  2613. snd_printk(KERN_ERR "image download error [cwcasync]\n");
  2614. return -EIO;
  2615. }
  2616. if (cs46xx_dsp_load_module(chip, &cwcsnoop_module) < 0) {
  2617. snd_printk(KERN_ERR "image download error [cwcsnoop]\n");
  2618. return -EIO;
  2619. }
  2620. if (cs46xx_dsp_load_module(chip, &cwcbinhack_module) < 0) {
  2621. snd_printk(KERN_ERR "image download error [cwcbinhack]\n");
  2622. return -EIO;
  2623. }
  2624. if (cs46xx_dsp_load_module(chip, &cwcdma_module) < 0) {
  2625. snd_printk(KERN_ERR "image download error [cwcdma]\n");
  2626. return -EIO;
  2627. }
  2628. if (cs46xx_dsp_scb_and_task_init(chip) < 0)
  2629. return -EIO;
  2630. #else
  2631. /* old image */
  2632. if (snd_cs46xx_download_image(chip) < 0) {
  2633. snd_printk(KERN_ERR "image download error\n");
  2634. return -EIO;
  2635. }
  2636. /*
  2637. * Stop playback DMA.
  2638. */
  2639. tmp = snd_cs46xx_peek(chip, BA1_PCTL);
  2640. chip->play_ctl = tmp & 0xffff0000;
  2641. snd_cs46xx_poke(chip, BA1_PCTL, tmp & 0x0000ffff);
  2642. #endif
  2643. /*
  2644. * Stop capture DMA.
  2645. */
  2646. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  2647. chip->capt.ctl = tmp & 0x0000ffff;
  2648. snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
  2649. mdelay(5);
  2650. snd_cs46xx_set_play_sample_rate(chip, 8000);
  2651. snd_cs46xx_set_capture_sample_rate(chip, 8000);
  2652. snd_cs46xx_proc_start(chip);
  2653. cs46xx_enable_stream_irqs(chip);
  2654. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  2655. /* set the attenuation to 0dB */
  2656. snd_cs46xx_poke(chip, BA1_PVOL, 0x80008000);
  2657. snd_cs46xx_poke(chip, BA1_CVOL, 0x80008000);
  2658. #endif
  2659. return 0;
  2660. }
  2661. /*
  2662. * AMP control - null AMP
  2663. */
  2664. static void amp_none(struct snd_cs46xx *chip, int change)
  2665. {
  2666. }
  2667. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2668. static int voyetra_setup_eapd_slot(struct snd_cs46xx *chip)
  2669. {
  2670. u32 idx, valid_slots,tmp,powerdown = 0;
  2671. u16 modem_power,pin_config,logic_type;
  2672. snd_printdd ("cs46xx: cs46xx_setup_eapd_slot()+\n");
  2673. /*
  2674. * See if the devices are powered down. If so, we must power them up first
  2675. * or they will not respond.
  2676. */
  2677. tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1);
  2678. if (!(tmp & CLKCR1_SWCE)) {
  2679. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp | CLKCR1_SWCE);
  2680. powerdown = 1;
  2681. }
  2682. /*
  2683. * Clear PRA. The Bonzo chip will be used for GPIO not for modem
  2684. * stuff.
  2685. */
  2686. if(chip->nr_ac97_codecs != 2) {
  2687. snd_printk (KERN_ERR "cs46xx: cs46xx_setup_eapd_slot() - no secondary codec configured\n");
  2688. return -EINVAL;
  2689. }
  2690. modem_power = snd_cs46xx_codec_read (chip,
  2691. AC97_EXTENDED_MSTATUS,
  2692. CS46XX_SECONDARY_CODEC_INDEX);
  2693. modem_power &=0xFEFF;
  2694. snd_cs46xx_codec_write(chip,
  2695. AC97_EXTENDED_MSTATUS, modem_power,
  2696. CS46XX_SECONDARY_CODEC_INDEX);
  2697. /*
  2698. * Set GPIO pin's 7 and 8 so that they are configured for output.
  2699. */
  2700. pin_config = snd_cs46xx_codec_read (chip,
  2701. AC97_GPIO_CFG,
  2702. CS46XX_SECONDARY_CODEC_INDEX);
  2703. pin_config &=0x27F;
  2704. snd_cs46xx_codec_write(chip,
  2705. AC97_GPIO_CFG, pin_config,
  2706. CS46XX_SECONDARY_CODEC_INDEX);
  2707. /*
  2708. * Set GPIO pin's 7 and 8 so that they are compatible with CMOS logic.
  2709. */
  2710. logic_type = snd_cs46xx_codec_read(chip, AC97_GPIO_POLARITY,
  2711. CS46XX_SECONDARY_CODEC_INDEX);
  2712. logic_type &=0x27F;
  2713. snd_cs46xx_codec_write (chip, AC97_GPIO_POLARITY, logic_type,
  2714. CS46XX_SECONDARY_CODEC_INDEX);
  2715. valid_slots = snd_cs46xx_peekBA0(chip, BA0_ACOSV);
  2716. valid_slots |= 0x200;
  2717. snd_cs46xx_pokeBA0(chip, BA0_ACOSV, valid_slots);
  2718. if ( cs46xx_wait_for_fifo(chip,1) ) {
  2719. snd_printdd("FIFO is busy\n");
  2720. return -EINVAL;
  2721. }
  2722. /*
  2723. * Fill slots 12 with the correct value for the GPIO pins.
  2724. */
  2725. for(idx = 0x90; idx <= 0x9F; idx++) {
  2726. /*
  2727. * Initialize the fifo so that bits 7 and 8 are on.
  2728. *
  2729. * Remember that the GPIO pins in bonzo are shifted by 4 bits to
  2730. * the left. 0x1800 corresponds to bits 7 and 8.
  2731. */
  2732. snd_cs46xx_pokeBA0(chip, BA0_SERBWP, 0x1800);
  2733. /*
  2734. * Wait for command to complete
  2735. */
  2736. if ( cs46xx_wait_for_fifo(chip,200) ) {
  2737. snd_printdd("failed waiting for FIFO at addr (%02X)\n",idx);
  2738. return -EINVAL;
  2739. }
  2740. /*
  2741. * Write the serial port FIFO index.
  2742. */
  2743. snd_cs46xx_pokeBA0(chip, BA0_SERBAD, idx);
  2744. /*
  2745. * Tell the serial port to load the new value into the FIFO location.
  2746. */
  2747. snd_cs46xx_pokeBA0(chip, BA0_SERBCM, SERBCM_WRC);
  2748. }
  2749. /* wait for last command to complete */
  2750. cs46xx_wait_for_fifo(chip,200);
  2751. /*
  2752. * Now, if we powered up the devices, then power them back down again.
  2753. * This is kinda ugly, but should never happen.
  2754. */
  2755. if (powerdown)
  2756. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
  2757. return 0;
  2758. }
  2759. #endif
  2760. /*
  2761. * Crystal EAPD mode
  2762. */
  2763. static void amp_voyetra(struct snd_cs46xx *chip, int change)
  2764. {
  2765. /* Manage the EAPD bit on the Crystal 4297
  2766. and the Analog AD1885 */
  2767. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2768. int old = chip->amplifier;
  2769. #endif
  2770. int oval, val;
  2771. chip->amplifier += change;
  2772. oval = snd_cs46xx_codec_read(chip, AC97_POWERDOWN,
  2773. CS46XX_PRIMARY_CODEC_INDEX);
  2774. val = oval;
  2775. if (chip->amplifier) {
  2776. /* Turn the EAPD amp on */
  2777. val |= 0x8000;
  2778. } else {
  2779. /* Turn the EAPD amp off */
  2780. val &= ~0x8000;
  2781. }
  2782. if (val != oval) {
  2783. snd_cs46xx_codec_write(chip, AC97_POWERDOWN, val,
  2784. CS46XX_PRIMARY_CODEC_INDEX);
  2785. if (chip->eapd_switch)
  2786. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  2787. &chip->eapd_switch->id);
  2788. }
  2789. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2790. if (chip->amplifier && !old) {
  2791. voyetra_setup_eapd_slot(chip);
  2792. }
  2793. #endif
  2794. }
  2795. static void hercules_init(struct snd_cs46xx *chip)
  2796. {
  2797. /* default: AMP off, and SPDIF input optical */
  2798. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, EGPIODR_GPOE0);
  2799. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, EGPIODR_GPOE0);
  2800. }
  2801. /*
  2802. * Game Theatre XP card - EGPIO[2] is used to enable the external amp.
  2803. */
  2804. static void amp_hercules(struct snd_cs46xx *chip, int change)
  2805. {
  2806. int old = chip->amplifier;
  2807. int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
  2808. int val2 = snd_cs46xx_peekBA0(chip, BA0_EGPIOPTR);
  2809. chip->amplifier += change;
  2810. if (chip->amplifier && !old) {
  2811. snd_printdd ("Hercules amplifier ON\n");
  2812. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR,
  2813. EGPIODR_GPOE2 | val1); /* enable EGPIO2 output */
  2814. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR,
  2815. EGPIOPTR_GPPT2 | val2); /* open-drain on output */
  2816. } else if (old && !chip->amplifier) {
  2817. snd_printdd ("Hercules amplifier OFF\n");
  2818. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, val1 & ~EGPIODR_GPOE2); /* disable */
  2819. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, val2 & ~EGPIOPTR_GPPT2); /* disable */
  2820. }
  2821. }
  2822. static void voyetra_mixer_init (struct snd_cs46xx *chip)
  2823. {
  2824. snd_printdd ("initializing Voyetra mixer\n");
  2825. /* Enable SPDIF out */
  2826. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, EGPIODR_GPOE0);
  2827. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, EGPIODR_GPOE0);
  2828. }
  2829. static void hercules_mixer_init (struct snd_cs46xx *chip)
  2830. {
  2831. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2832. unsigned int idx;
  2833. int err;
  2834. struct snd_card *card = chip->card;
  2835. #endif
  2836. /* set EGPIO to default */
  2837. hercules_init(chip);
  2838. snd_printdd ("initializing Hercules mixer\n");
  2839. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2840. if (chip->in_suspend)
  2841. return;
  2842. for (idx = 0 ; idx < ARRAY_SIZE(snd_hercules_controls); idx++) {
  2843. struct snd_kcontrol *kctl;
  2844. kctl = snd_ctl_new1(&snd_hercules_controls[idx], chip);
  2845. if ((err = snd_ctl_add(card, kctl)) < 0) {
  2846. printk (KERN_ERR "cs46xx: failed to initialize Hercules mixer (%d)\n",err);
  2847. break;
  2848. }
  2849. }
  2850. #endif
  2851. }
  2852. #if 0
  2853. /*
  2854. * Untested
  2855. */
  2856. static void amp_voyetra_4294(struct snd_cs46xx *chip, int change)
  2857. {
  2858. chip->amplifier += change;
  2859. if (chip->amplifier) {
  2860. /* Switch the GPIO pins 7 and 8 to open drain */
  2861. snd_cs46xx_codec_write(chip, 0x4C,
  2862. snd_cs46xx_codec_read(chip, 0x4C) & 0xFE7F);
  2863. snd_cs46xx_codec_write(chip, 0x4E,
  2864. snd_cs46xx_codec_read(chip, 0x4E) | 0x0180);
  2865. /* Now wake the AMP (this might be backwards) */
  2866. snd_cs46xx_codec_write(chip, 0x54,
  2867. snd_cs46xx_codec_read(chip, 0x54) & ~0x0180);
  2868. } else {
  2869. snd_cs46xx_codec_write(chip, 0x54,
  2870. snd_cs46xx_codec_read(chip, 0x54) | 0x0180);
  2871. }
  2872. }
  2873. #endif
  2874. /*
  2875. * Handle the CLKRUN on a thinkpad. We must disable CLKRUN support
  2876. * whenever we need to beat on the chip.
  2877. *
  2878. * The original idea and code for this hack comes from David Kaiser at
  2879. * Linuxcare. Perhaps one day Crystal will document their chips well
  2880. * enough to make them useful.
  2881. */
  2882. static void clkrun_hack(struct snd_cs46xx *chip, int change)
  2883. {
  2884. u16 control, nval;
  2885. if (!chip->acpi_port)
  2886. return;
  2887. chip->amplifier += change;
  2888. /* Read ACPI port */
  2889. nval = control = inw(chip->acpi_port + 0x10);
  2890. /* Flip CLKRUN off while running */
  2891. if (! chip->amplifier)
  2892. nval |= 0x2000;
  2893. else
  2894. nval &= ~0x2000;
  2895. if (nval != control)
  2896. outw(nval, chip->acpi_port + 0x10);
  2897. }
  2898. /*
  2899. * detect intel piix4
  2900. */
  2901. static void clkrun_init(struct snd_cs46xx *chip)
  2902. {
  2903. struct pci_dev *pdev;
  2904. u8 pp;
  2905. chip->acpi_port = 0;
  2906. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  2907. PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
  2908. if (pdev == NULL)
  2909. return; /* Not a thinkpad thats for sure */
  2910. /* Find the control port */
  2911. pci_read_config_byte(pdev, 0x41, &pp);
  2912. chip->acpi_port = pp << 8;
  2913. pci_dev_put(pdev);
  2914. }
  2915. /*
  2916. * Card subid table
  2917. */
  2918. struct cs_card_type
  2919. {
  2920. u16 vendor;
  2921. u16 id;
  2922. char *name;
  2923. void (*init)(struct snd_cs46xx *);
  2924. void (*amp)(struct snd_cs46xx *, int);
  2925. void (*active)(struct snd_cs46xx *, int);
  2926. void (*mixer_init)(struct snd_cs46xx *);
  2927. };
  2928. static struct cs_card_type cards[] = {
  2929. {
  2930. .vendor = 0x1489,
  2931. .id = 0x7001,
  2932. .name = "Genius Soundmaker 128 value",
  2933. /* nothing special */
  2934. },
  2935. {
  2936. .vendor = 0x5053,
  2937. .id = 0x3357,
  2938. .name = "Voyetra",
  2939. .amp = amp_voyetra,
  2940. .mixer_init = voyetra_mixer_init,
  2941. },
  2942. {
  2943. .vendor = 0x1071,
  2944. .id = 0x6003,
  2945. .name = "Mitac MI6020/21",
  2946. .amp = amp_voyetra,
  2947. },
  2948. /* Hercules Game Theatre XP */
  2949. {
  2950. .vendor = 0x14af, /* Guillemot Corporation */
  2951. .id = 0x0050,
  2952. .name = "Hercules Game Theatre XP",
  2953. .amp = amp_hercules,
  2954. .mixer_init = hercules_mixer_init,
  2955. },
  2956. {
  2957. .vendor = 0x1681,
  2958. .id = 0x0050,
  2959. .name = "Hercules Game Theatre XP",
  2960. .amp = amp_hercules,
  2961. .mixer_init = hercules_mixer_init,
  2962. },
  2963. {
  2964. .vendor = 0x1681,
  2965. .id = 0x0051,
  2966. .name = "Hercules Game Theatre XP",
  2967. .amp = amp_hercules,
  2968. .mixer_init = hercules_mixer_init,
  2969. },
  2970. {
  2971. .vendor = 0x1681,
  2972. .id = 0x0052,
  2973. .name = "Hercules Game Theatre XP",
  2974. .amp = amp_hercules,
  2975. .mixer_init = hercules_mixer_init,
  2976. },
  2977. {
  2978. .vendor = 0x1681,
  2979. .id = 0x0053,
  2980. .name = "Hercules Game Theatre XP",
  2981. .amp = amp_hercules,
  2982. .mixer_init = hercules_mixer_init,
  2983. },
  2984. {
  2985. .vendor = 0x1681,
  2986. .id = 0x0054,
  2987. .name = "Hercules Game Theatre XP",
  2988. .amp = amp_hercules,
  2989. .mixer_init = hercules_mixer_init,
  2990. },
  2991. /* Herculess Fortissimo */
  2992. {
  2993. .vendor = 0x1681,
  2994. .id = 0xa010,
  2995. .name = "Hercules Gamesurround Fortissimo II",
  2996. },
  2997. {
  2998. .vendor = 0x1681,
  2999. .id = 0xa011,
  3000. .name = "Hercules Gamesurround Fortissimo III 7.1",
  3001. },
  3002. /* Teratec */
  3003. {
  3004. .vendor = 0x153b,
  3005. .id = 0x112e,
  3006. .name = "Terratec DMX XFire 1024",
  3007. },
  3008. {
  3009. .vendor = 0x153b,
  3010. .id = 0x1136,
  3011. .name = "Terratec SiXPack 5.1",
  3012. },
  3013. /* Not sure if the 570 needs the clkrun hack */
  3014. {
  3015. .vendor = PCI_VENDOR_ID_IBM,
  3016. .id = 0x0132,
  3017. .name = "Thinkpad 570",
  3018. .init = clkrun_init,
  3019. .active = clkrun_hack,
  3020. },
  3021. {
  3022. .vendor = PCI_VENDOR_ID_IBM,
  3023. .id = 0x0153,
  3024. .name = "Thinkpad 600X/A20/T20",
  3025. .init = clkrun_init,
  3026. .active = clkrun_hack,
  3027. },
  3028. {
  3029. .vendor = PCI_VENDOR_ID_IBM,
  3030. .id = 0x1010,
  3031. .name = "Thinkpad 600E (unsupported)",
  3032. },
  3033. {} /* terminator */
  3034. };
  3035. /*
  3036. * APM support
  3037. */
  3038. #ifdef CONFIG_PM_SLEEP
  3039. static unsigned int saved_regs[] = {
  3040. BA0_ACOSV,
  3041. /*BA0_ASER_FADDR,*/
  3042. BA0_ASER_MASTER,
  3043. BA1_PVOL,
  3044. BA1_CVOL,
  3045. };
  3046. static int snd_cs46xx_suspend(struct device *dev)
  3047. {
  3048. struct pci_dev *pci = to_pci_dev(dev);
  3049. struct snd_card *card = dev_get_drvdata(dev);
  3050. struct snd_cs46xx *chip = card->private_data;
  3051. int i, amp_saved;
  3052. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  3053. chip->in_suspend = 1;
  3054. snd_pcm_suspend_all(chip->pcm);
  3055. // chip->ac97_powerdown = snd_cs46xx_codec_read(chip, AC97_POWER_CONTROL);
  3056. // chip->ac97_general_purpose = snd_cs46xx_codec_read(chip, BA0_AC97_GENERAL_PURPOSE);
  3057. snd_ac97_suspend(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]);
  3058. snd_ac97_suspend(chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]);
  3059. /* save some registers */
  3060. for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  3061. chip->saved_regs[i] = snd_cs46xx_peekBA0(chip, saved_regs[i]);
  3062. amp_saved = chip->amplifier;
  3063. /* turn off amp */
  3064. chip->amplifier_ctrl(chip, -chip->amplifier);
  3065. snd_cs46xx_hw_stop(chip);
  3066. /* disable CLKRUN */
  3067. chip->active_ctrl(chip, -chip->amplifier);
  3068. chip->amplifier = amp_saved; /* restore the status */
  3069. pci_disable_device(pci);
  3070. pci_save_state(pci);
  3071. pci_set_power_state(pci, PCI_D3hot);
  3072. return 0;
  3073. }
  3074. static int snd_cs46xx_resume(struct device *dev)
  3075. {
  3076. struct pci_dev *pci = to_pci_dev(dev);
  3077. struct snd_card *card = dev_get_drvdata(dev);
  3078. struct snd_cs46xx *chip = card->private_data;
  3079. int amp_saved;
  3080. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3081. int i;
  3082. #endif
  3083. unsigned int tmp;
  3084. pci_set_power_state(pci, PCI_D0);
  3085. pci_restore_state(pci);
  3086. if (pci_enable_device(pci) < 0) {
  3087. printk(KERN_ERR "cs46xx: pci_enable_device failed, "
  3088. "disabling device\n");
  3089. snd_card_disconnect(card);
  3090. return -EIO;
  3091. }
  3092. pci_set_master(pci);
  3093. amp_saved = chip->amplifier;
  3094. chip->amplifier = 0;
  3095. chip->active_ctrl(chip, 1); /* force to on */
  3096. snd_cs46xx_chip_init(chip);
  3097. snd_cs46xx_reset(chip);
  3098. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3099. cs46xx_dsp_resume(chip);
  3100. /* restore some registers */
  3101. for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  3102. snd_cs46xx_pokeBA0(chip, saved_regs[i], chip->saved_regs[i]);
  3103. #else
  3104. snd_cs46xx_download_image(chip);
  3105. #endif
  3106. #if 0
  3107. snd_cs46xx_codec_write(chip, BA0_AC97_GENERAL_PURPOSE,
  3108. chip->ac97_general_purpose);
  3109. snd_cs46xx_codec_write(chip, AC97_POWER_CONTROL,
  3110. chip->ac97_powerdown);
  3111. mdelay(10);
  3112. snd_cs46xx_codec_write(chip, BA0_AC97_POWERDOWN,
  3113. chip->ac97_powerdown);
  3114. mdelay(5);
  3115. #endif
  3116. snd_ac97_resume(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]);
  3117. snd_ac97_resume(chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]);
  3118. /*
  3119. * Stop capture DMA.
  3120. */
  3121. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  3122. chip->capt.ctl = tmp & 0x0000ffff;
  3123. snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
  3124. mdelay(5);
  3125. /* reset playback/capture */
  3126. snd_cs46xx_set_play_sample_rate(chip, 8000);
  3127. snd_cs46xx_set_capture_sample_rate(chip, 8000);
  3128. snd_cs46xx_proc_start(chip);
  3129. cs46xx_enable_stream_irqs(chip);
  3130. if (amp_saved)
  3131. chip->amplifier_ctrl(chip, 1); /* turn amp on */
  3132. else
  3133. chip->active_ctrl(chip, -1); /* disable CLKRUN */
  3134. chip->amplifier = amp_saved;
  3135. chip->in_suspend = 0;
  3136. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  3137. return 0;
  3138. }
  3139. SIMPLE_DEV_PM_OPS(snd_cs46xx_pm, snd_cs46xx_suspend, snd_cs46xx_resume);
  3140. #endif /* CONFIG_PM_SLEEP */
  3141. /*
  3142. */
  3143. int snd_cs46xx_create(struct snd_card *card,
  3144. struct pci_dev *pci,
  3145. int external_amp, int thinkpad,
  3146. struct snd_cs46xx **rchip)
  3147. {
  3148. struct snd_cs46xx *chip;
  3149. int err, idx;
  3150. struct snd_cs46xx_region *region;
  3151. struct cs_card_type *cp;
  3152. u16 ss_card, ss_vendor;
  3153. static struct snd_device_ops ops = {
  3154. .dev_free = snd_cs46xx_dev_free,
  3155. };
  3156. *rchip = NULL;
  3157. /* enable PCI device */
  3158. if ((err = pci_enable_device(pci)) < 0)
  3159. return err;
  3160. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  3161. if (chip == NULL) {
  3162. pci_disable_device(pci);
  3163. return -ENOMEM;
  3164. }
  3165. spin_lock_init(&chip->reg_lock);
  3166. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3167. mutex_init(&chip->spos_mutex);
  3168. #endif
  3169. chip->card = card;
  3170. chip->pci = pci;
  3171. chip->irq = -1;
  3172. chip->ba0_addr = pci_resource_start(pci, 0);
  3173. chip->ba1_addr = pci_resource_start(pci, 1);
  3174. if (chip->ba0_addr == 0 || chip->ba0_addr == (unsigned long)~0 ||
  3175. chip->ba1_addr == 0 || chip->ba1_addr == (unsigned long)~0) {
  3176. snd_printk(KERN_ERR "wrong address(es) - ba0 = 0x%lx, ba1 = 0x%lx\n",
  3177. chip->ba0_addr, chip->ba1_addr);
  3178. snd_cs46xx_free(chip);
  3179. return -ENOMEM;
  3180. }
  3181. region = &chip->region.name.ba0;
  3182. strcpy(region->name, "CS46xx_BA0");
  3183. region->base = chip->ba0_addr;
  3184. region->size = CS46XX_BA0_SIZE;
  3185. region = &chip->region.name.data0;
  3186. strcpy(region->name, "CS46xx_BA1_data0");
  3187. region->base = chip->ba1_addr + BA1_SP_DMEM0;
  3188. region->size = CS46XX_BA1_DATA0_SIZE;
  3189. region = &chip->region.name.data1;
  3190. strcpy(region->name, "CS46xx_BA1_data1");
  3191. region->base = chip->ba1_addr + BA1_SP_DMEM1;
  3192. region->size = CS46XX_BA1_DATA1_SIZE;
  3193. region = &chip->region.name.pmem;
  3194. strcpy(region->name, "CS46xx_BA1_pmem");
  3195. region->base = chip->ba1_addr + BA1_SP_PMEM;
  3196. region->size = CS46XX_BA1_PRG_SIZE;
  3197. region = &chip->region.name.reg;
  3198. strcpy(region->name, "CS46xx_BA1_reg");
  3199. region->base = chip->ba1_addr + BA1_SP_REG;
  3200. region->size = CS46XX_BA1_REG_SIZE;
  3201. /* set up amp and clkrun hack */
  3202. pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &ss_vendor);
  3203. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &ss_card);
  3204. for (cp = &cards[0]; cp->name; cp++) {
  3205. if (cp->vendor == ss_vendor && cp->id == ss_card) {
  3206. snd_printdd ("hack for %s enabled\n", cp->name);
  3207. chip->amplifier_ctrl = cp->amp;
  3208. chip->active_ctrl = cp->active;
  3209. chip->mixer_init = cp->mixer_init;
  3210. if (cp->init)
  3211. cp->init(chip);
  3212. break;
  3213. }
  3214. }
  3215. if (external_amp) {
  3216. snd_printk(KERN_INFO "Crystal EAPD support forced on.\n");
  3217. chip->amplifier_ctrl = amp_voyetra;
  3218. }
  3219. if (thinkpad) {
  3220. snd_printk(KERN_INFO "Activating CLKRUN hack for Thinkpad.\n");
  3221. chip->active_ctrl = clkrun_hack;
  3222. clkrun_init(chip);
  3223. }
  3224. if (chip->amplifier_ctrl == NULL)
  3225. chip->amplifier_ctrl = amp_none;
  3226. if (chip->active_ctrl == NULL)
  3227. chip->active_ctrl = amp_none;
  3228. chip->active_ctrl(chip, 1); /* enable CLKRUN */
  3229. pci_set_master(pci);
  3230. for (idx = 0; idx < 5; idx++) {
  3231. region = &chip->region.idx[idx];
  3232. if ((region->resource = request_mem_region(region->base, region->size,
  3233. region->name)) == NULL) {
  3234. snd_printk(KERN_ERR "unable to request memory region 0x%lx-0x%lx\n",
  3235. region->base, region->base + region->size - 1);
  3236. snd_cs46xx_free(chip);
  3237. return -EBUSY;
  3238. }
  3239. region->remap_addr = ioremap_nocache(region->base, region->size);
  3240. if (region->remap_addr == NULL) {
  3241. snd_printk(KERN_ERR "%s ioremap problem\n", region->name);
  3242. snd_cs46xx_free(chip);
  3243. return -ENOMEM;
  3244. }
  3245. }
  3246. if (request_irq(pci->irq, snd_cs46xx_interrupt, IRQF_SHARED,
  3247. KBUILD_MODNAME, chip)) {
  3248. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  3249. snd_cs46xx_free(chip);
  3250. return -EBUSY;
  3251. }
  3252. chip->irq = pci->irq;
  3253. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3254. chip->dsp_spos_instance = cs46xx_dsp_spos_create(chip);
  3255. if (chip->dsp_spos_instance == NULL) {
  3256. snd_cs46xx_free(chip);
  3257. return -ENOMEM;
  3258. }
  3259. #endif
  3260. err = snd_cs46xx_chip_init(chip);
  3261. if (err < 0) {
  3262. snd_cs46xx_free(chip);
  3263. return err;
  3264. }
  3265. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  3266. snd_cs46xx_free(chip);
  3267. return err;
  3268. }
  3269. snd_cs46xx_proc_init(card, chip);
  3270. #ifdef CONFIG_PM_SLEEP
  3271. chip->saved_regs = kmalloc(sizeof(*chip->saved_regs) *
  3272. ARRAY_SIZE(saved_regs), GFP_KERNEL);
  3273. if (!chip->saved_regs) {
  3274. snd_cs46xx_free(chip);
  3275. return -ENOMEM;
  3276. }
  3277. #endif
  3278. chip->active_ctrl(chip, -1); /* disable CLKRUN */
  3279. snd_card_set_dev(card, &pci->dev);
  3280. *rchip = chip;
  3281. return 0;
  3282. }