cs46xx_lib.c 104 KB

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