cs46xx_lib.c 106 KB

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