patch_ca0132.c 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702
  1. /*
  2. * HD audio interface patch for Creative CA0132 chip
  3. *
  4. * Copyright (c) 2011, Creative Technology Ltd.
  5. *
  6. * Based on patch_ca0110.c
  7. * Copyright (c) 2008 Takashi Iwai <tiwai@suse.de>
  8. *
  9. * This driver is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This driver is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <linux/pci.h>
  27. #include <linux/mutex.h>
  28. #include <linux/module.h>
  29. #include <linux/firmware.h>
  30. #include <sound/core.h>
  31. #include "hda_codec.h"
  32. #include "hda_local.h"
  33. #include "hda_auto_parser.h"
  34. #include "hda_jack.h"
  35. #include "ca0132_regs.h"
  36. /* Enable this to see controls for tuning purpose. */
  37. /*#define ENABLE_TUNING_CONTROLS*/
  38. #define FLOAT_ZERO 0x00000000
  39. #define FLOAT_ONE 0x3f800000
  40. #define FLOAT_TWO 0x40000000
  41. #define FLOAT_MINUS_5 0xc0a00000
  42. #define UNSOL_TAG_HP 0x10
  43. #define UNSOL_TAG_AMIC1 0x12
  44. #define UNSOL_TAG_DSP 0x16
  45. #define DSP_DMA_WRITE_BUFLEN_INIT (1UL<<18)
  46. #define DSP_DMA_WRITE_BUFLEN_OVLY (1UL<<15)
  47. #define DMA_TRANSFER_FRAME_SIZE_NWORDS 8
  48. #define DMA_TRANSFER_MAX_FRAME_SIZE_NWORDS 32
  49. #define DMA_OVERLAY_FRAME_SIZE_NWORDS 2
  50. #define MASTERCONTROL 0x80
  51. #define MASTERCONTROL_ALLOC_DMA_CHAN 10
  52. #define MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS 60
  53. #define WIDGET_CHIP_CTRL 0x15
  54. #define WIDGET_DSP_CTRL 0x16
  55. #define MEM_CONNID_MICIN1 3
  56. #define MEM_CONNID_MICIN2 5
  57. #define MEM_CONNID_MICOUT1 12
  58. #define MEM_CONNID_MICOUT2 14
  59. #define MEM_CONNID_WUH 10
  60. #define MEM_CONNID_DSP 16
  61. #define MEM_CONNID_DMIC 100
  62. #define SCP_SET 0
  63. #define SCP_GET 1
  64. #define EFX_FILE "ctefx.bin"
  65. #ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
  66. MODULE_FIRMWARE(EFX_FILE);
  67. #endif
  68. static char *dirstr[2] = { "Playback", "Capture" };
  69. enum {
  70. SPEAKER_OUT,
  71. HEADPHONE_OUT
  72. };
  73. enum {
  74. DIGITAL_MIC,
  75. LINE_MIC_IN
  76. };
  77. enum {
  78. #define VNODE_START_NID 0x80
  79. VNID_SPK = VNODE_START_NID, /* Speaker vnid */
  80. VNID_MIC,
  81. VNID_HP_SEL,
  82. VNID_AMIC1_SEL,
  83. VNID_HP_ASEL,
  84. VNID_AMIC1_ASEL,
  85. VNODE_END_NID,
  86. #define VNODES_COUNT (VNODE_END_NID - VNODE_START_NID)
  87. #define EFFECT_START_NID 0x90
  88. #define OUT_EFFECT_START_NID EFFECT_START_NID
  89. SURROUND = OUT_EFFECT_START_NID,
  90. CRYSTALIZER,
  91. DIALOG_PLUS,
  92. SMART_VOLUME,
  93. X_BASS,
  94. EQUALIZER,
  95. OUT_EFFECT_END_NID,
  96. #define OUT_EFFECTS_COUNT (OUT_EFFECT_END_NID - OUT_EFFECT_START_NID)
  97. #define IN_EFFECT_START_NID OUT_EFFECT_END_NID
  98. ECHO_CANCELLATION = IN_EFFECT_START_NID,
  99. VOICE_FOCUS,
  100. MIC_SVM,
  101. NOISE_REDUCTION,
  102. IN_EFFECT_END_NID,
  103. #define IN_EFFECTS_COUNT (IN_EFFECT_END_NID - IN_EFFECT_START_NID)
  104. VOICEFX = IN_EFFECT_END_NID,
  105. PLAY_ENHANCEMENT,
  106. CRYSTAL_VOICE,
  107. EFFECT_END_NID
  108. #define EFFECTS_COUNT (EFFECT_END_NID - EFFECT_START_NID)
  109. };
  110. /* Effects values size*/
  111. #define EFFECT_VALS_MAX_COUNT 12
  112. struct ct_effect {
  113. char name[44];
  114. hda_nid_t nid;
  115. int mid; /*effect module ID*/
  116. int reqs[EFFECT_VALS_MAX_COUNT]; /*effect module request*/
  117. int direct; /* 0:output; 1:input*/
  118. int params; /* number of default non-on/off params */
  119. /*effect default values, 1st is on/off. */
  120. unsigned int def_vals[EFFECT_VALS_MAX_COUNT];
  121. };
  122. #define EFX_DIR_OUT 0
  123. #define EFX_DIR_IN 1
  124. static struct ct_effect ca0132_effects[EFFECTS_COUNT] = {
  125. { .name = "Surround",
  126. .nid = SURROUND,
  127. .mid = 0x96,
  128. .reqs = {0, 1},
  129. .direct = EFX_DIR_OUT,
  130. .params = 1,
  131. .def_vals = {0x3F800000, 0x3F2B851F}
  132. },
  133. { .name = "Crystalizer",
  134. .nid = CRYSTALIZER,
  135. .mid = 0x96,
  136. .reqs = {7, 8},
  137. .direct = EFX_DIR_OUT,
  138. .params = 1,
  139. .def_vals = {0x3F800000, 0x3F266666}
  140. },
  141. { .name = "Dialog Plus",
  142. .nid = DIALOG_PLUS,
  143. .mid = 0x96,
  144. .reqs = {2, 3},
  145. .direct = EFX_DIR_OUT,
  146. .params = 1,
  147. .def_vals = {0x00000000, 0x3F000000}
  148. },
  149. { .name = "Smart Volume",
  150. .nid = SMART_VOLUME,
  151. .mid = 0x96,
  152. .reqs = {4, 5, 6},
  153. .direct = EFX_DIR_OUT,
  154. .params = 2,
  155. .def_vals = {0x3F800000, 0x3F3D70A4, 0x00000000}
  156. },
  157. { .name = "X-Bass",
  158. .nid = X_BASS,
  159. .mid = 0x96,
  160. .reqs = {24, 23, 25},
  161. .direct = EFX_DIR_OUT,
  162. .params = 2,
  163. .def_vals = {0x3F800000, 0x42A00000, 0x3F000000}
  164. },
  165. { .name = "Equalizer",
  166. .nid = EQUALIZER,
  167. .mid = 0x96,
  168. .reqs = {9, 10, 11, 12, 13, 14,
  169. 15, 16, 17, 18, 19, 20},
  170. .direct = EFX_DIR_OUT,
  171. .params = 11,
  172. .def_vals = {0x00000000, 0x00000000, 0x00000000, 0x00000000,
  173. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  174. 0x00000000, 0x00000000, 0x00000000, 0x00000000}
  175. },
  176. { .name = "Echo Cancellation",
  177. .nid = ECHO_CANCELLATION,
  178. .mid = 0x95,
  179. .reqs = {0, 1, 2, 3},
  180. .direct = EFX_DIR_IN,
  181. .params = 3,
  182. .def_vals = {0x00000000, 0x3F3A9692, 0x00000000, 0x00000000}
  183. },
  184. { .name = "Voice Focus",
  185. .nid = VOICE_FOCUS,
  186. .mid = 0x95,
  187. .reqs = {6, 7, 8, 9},
  188. .direct = EFX_DIR_IN,
  189. .params = 3,
  190. .def_vals = {0x3F800000, 0x3D7DF3B6, 0x41F00000, 0x41F00000}
  191. },
  192. { .name = "Mic SVM",
  193. .nid = MIC_SVM,
  194. .mid = 0x95,
  195. .reqs = {44, 45},
  196. .direct = EFX_DIR_IN,
  197. .params = 1,
  198. .def_vals = {0x00000000, 0x3F3D70A4}
  199. },
  200. { .name = "Noise Reduction",
  201. .nid = NOISE_REDUCTION,
  202. .mid = 0x95,
  203. .reqs = {4, 5},
  204. .direct = EFX_DIR_IN,
  205. .params = 1,
  206. .def_vals = {0x3F800000, 0x3F000000}
  207. },
  208. { .name = "VoiceFX",
  209. .nid = VOICEFX,
  210. .mid = 0x95,
  211. .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18},
  212. .direct = EFX_DIR_IN,
  213. .params = 8,
  214. .def_vals = {0x00000000, 0x43C80000, 0x44AF0000, 0x44FA0000,
  215. 0x3F800000, 0x3F800000, 0x3F800000, 0x00000000,
  216. 0x00000000}
  217. }
  218. };
  219. /* Tuning controls */
  220. #ifdef ENABLE_TUNING_CONTROLS
  221. enum {
  222. #define TUNING_CTL_START_NID 0xC0
  223. WEDGE_ANGLE = TUNING_CTL_START_NID,
  224. SVM_LEVEL,
  225. EQUALIZER_BAND_0,
  226. EQUALIZER_BAND_1,
  227. EQUALIZER_BAND_2,
  228. EQUALIZER_BAND_3,
  229. EQUALIZER_BAND_4,
  230. EQUALIZER_BAND_5,
  231. EQUALIZER_BAND_6,
  232. EQUALIZER_BAND_7,
  233. EQUALIZER_BAND_8,
  234. EQUALIZER_BAND_9,
  235. TUNING_CTL_END_NID
  236. #define TUNING_CTLS_COUNT (TUNING_CTL_END_NID - TUNING_CTL_START_NID)
  237. };
  238. struct ct_tuning_ctl {
  239. char name[44];
  240. hda_nid_t parent_nid;
  241. hda_nid_t nid;
  242. int mid; /*effect module ID*/
  243. int req; /*effect module request*/
  244. int direct; /* 0:output; 1:input*/
  245. unsigned int def_val;/*effect default values*/
  246. };
  247. static struct ct_tuning_ctl ca0132_tuning_ctls[] = {
  248. { .name = "Wedge Angle",
  249. .parent_nid = VOICE_FOCUS,
  250. .nid = WEDGE_ANGLE,
  251. .mid = 0x95,
  252. .req = 8,
  253. .direct = EFX_DIR_IN,
  254. .def_val = 0x41F00000
  255. },
  256. { .name = "SVM Level",
  257. .parent_nid = MIC_SVM,
  258. .nid = SVM_LEVEL,
  259. .mid = 0x95,
  260. .req = 45,
  261. .direct = EFX_DIR_IN,
  262. .def_val = 0x3F3D70A4
  263. },
  264. { .name = "EQ Band0",
  265. .parent_nid = EQUALIZER,
  266. .nid = EQUALIZER_BAND_0,
  267. .mid = 0x96,
  268. .req = 11,
  269. .direct = EFX_DIR_OUT,
  270. .def_val = 0x00000000
  271. },
  272. { .name = "EQ Band1",
  273. .parent_nid = EQUALIZER,
  274. .nid = EQUALIZER_BAND_1,
  275. .mid = 0x96,
  276. .req = 12,
  277. .direct = EFX_DIR_OUT,
  278. .def_val = 0x00000000
  279. },
  280. { .name = "EQ Band2",
  281. .parent_nid = EQUALIZER,
  282. .nid = EQUALIZER_BAND_2,
  283. .mid = 0x96,
  284. .req = 13,
  285. .direct = EFX_DIR_OUT,
  286. .def_val = 0x00000000
  287. },
  288. { .name = "EQ Band3",
  289. .parent_nid = EQUALIZER,
  290. .nid = EQUALIZER_BAND_3,
  291. .mid = 0x96,
  292. .req = 14,
  293. .direct = EFX_DIR_OUT,
  294. .def_val = 0x00000000
  295. },
  296. { .name = "EQ Band4",
  297. .parent_nid = EQUALIZER,
  298. .nid = EQUALIZER_BAND_4,
  299. .mid = 0x96,
  300. .req = 15,
  301. .direct = EFX_DIR_OUT,
  302. .def_val = 0x00000000
  303. },
  304. { .name = "EQ Band5",
  305. .parent_nid = EQUALIZER,
  306. .nid = EQUALIZER_BAND_5,
  307. .mid = 0x96,
  308. .req = 16,
  309. .direct = EFX_DIR_OUT,
  310. .def_val = 0x00000000
  311. },
  312. { .name = "EQ Band6",
  313. .parent_nid = EQUALIZER,
  314. .nid = EQUALIZER_BAND_6,
  315. .mid = 0x96,
  316. .req = 17,
  317. .direct = EFX_DIR_OUT,
  318. .def_val = 0x00000000
  319. },
  320. { .name = "EQ Band7",
  321. .parent_nid = EQUALIZER,
  322. .nid = EQUALIZER_BAND_7,
  323. .mid = 0x96,
  324. .req = 18,
  325. .direct = EFX_DIR_OUT,
  326. .def_val = 0x00000000
  327. },
  328. { .name = "EQ Band8",
  329. .parent_nid = EQUALIZER,
  330. .nid = EQUALIZER_BAND_8,
  331. .mid = 0x96,
  332. .req = 19,
  333. .direct = EFX_DIR_OUT,
  334. .def_val = 0x00000000
  335. },
  336. { .name = "EQ Band9",
  337. .parent_nid = EQUALIZER,
  338. .nid = EQUALIZER_BAND_9,
  339. .mid = 0x96,
  340. .req = 20,
  341. .direct = EFX_DIR_OUT,
  342. .def_val = 0x00000000
  343. }
  344. };
  345. #endif
  346. /* Voice FX Presets */
  347. #define VOICEFX_MAX_PARAM_COUNT 9
  348. struct ct_voicefx {
  349. char *name;
  350. hda_nid_t nid;
  351. int mid;
  352. int reqs[VOICEFX_MAX_PARAM_COUNT]; /*effect module request*/
  353. };
  354. struct ct_voicefx_preset {
  355. char *name; /*preset name*/
  356. unsigned int vals[VOICEFX_MAX_PARAM_COUNT];
  357. };
  358. static struct ct_voicefx ca0132_voicefx = {
  359. .name = "VoiceFX Capture Switch",
  360. .nid = VOICEFX,
  361. .mid = 0x95,
  362. .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18}
  363. };
  364. static struct ct_voicefx_preset ca0132_voicefx_presets[] = {
  365. { .name = "Neutral",
  366. .vals = { 0x00000000, 0x43C80000, 0x44AF0000,
  367. 0x44FA0000, 0x3F800000, 0x3F800000,
  368. 0x3F800000, 0x00000000, 0x00000000 }
  369. },
  370. { .name = "Female2Male",
  371. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  372. 0x44FA0000, 0x3F19999A, 0x3F866666,
  373. 0x3F800000, 0x00000000, 0x00000000 }
  374. },
  375. { .name = "Male2Female",
  376. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  377. 0x450AC000, 0x4017AE14, 0x3F6B851F,
  378. 0x3F800000, 0x00000000, 0x00000000 }
  379. },
  380. { .name = "ScrappyKid",
  381. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  382. 0x44FA0000, 0x40400000, 0x3F28F5C3,
  383. 0x3F800000, 0x00000000, 0x00000000 }
  384. },
  385. { .name = "Elderly",
  386. .vals = { 0x3F800000, 0x44324000, 0x44BB8000,
  387. 0x44E10000, 0x3FB33333, 0x3FB9999A,
  388. 0x3F800000, 0x3E3A2E43, 0x00000000 }
  389. },
  390. { .name = "Orc",
  391. .vals = { 0x3F800000, 0x43EA0000, 0x44A52000,
  392. 0x45098000, 0x3F266666, 0x3FC00000,
  393. 0x3F800000, 0x00000000, 0x00000000 }
  394. },
  395. { .name = "Elf",
  396. .vals = { 0x3F800000, 0x43C70000, 0x44AE6000,
  397. 0x45193000, 0x3F8E147B, 0x3F75C28F,
  398. 0x3F800000, 0x00000000, 0x00000000 }
  399. },
  400. { .name = "Dwarf",
  401. .vals = { 0x3F800000, 0x43930000, 0x44BEE000,
  402. 0x45007000, 0x3F451EB8, 0x3F7851EC,
  403. 0x3F800000, 0x00000000, 0x00000000 }
  404. },
  405. { .name = "AlienBrute",
  406. .vals = { 0x3F800000, 0x43BFC5AC, 0x44B28FDF,
  407. 0x451F6000, 0x3F266666, 0x3FA7D945,
  408. 0x3F800000, 0x3CF5C28F, 0x00000000 }
  409. },
  410. { .name = "Robot",
  411. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  412. 0x44FA0000, 0x3FB2718B, 0x3F800000,
  413. 0xBC07010E, 0x00000000, 0x00000000 }
  414. },
  415. { .name = "Marine",
  416. .vals = { 0x3F800000, 0x43C20000, 0x44906000,
  417. 0x44E70000, 0x3F4CCCCD, 0x3F8A3D71,
  418. 0x3F0A3D71, 0x00000000, 0x00000000 }
  419. },
  420. { .name = "Emo",
  421. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  422. 0x44FA0000, 0x3F800000, 0x3F800000,
  423. 0x3E4CCCCD, 0x00000000, 0x00000000 }
  424. },
  425. { .name = "DeepVoice",
  426. .vals = { 0x3F800000, 0x43A9C5AC, 0x44AA4FDF,
  427. 0x44FFC000, 0x3EDBB56F, 0x3F99C4CA,
  428. 0x3F800000, 0x00000000, 0x00000000 }
  429. },
  430. { .name = "Munchkin",
  431. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  432. 0x44FA0000, 0x3F800000, 0x3F1A043C,
  433. 0x3F800000, 0x00000000, 0x00000000 }
  434. }
  435. };
  436. enum hda_cmd_vendor_io {
  437. /* for DspIO node */
  438. VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
  439. VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
  440. VENDOR_DSPIO_STATUS = 0xF01,
  441. VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
  442. VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
  443. VENDOR_DSPIO_DSP_INIT = 0x703,
  444. VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
  445. VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
  446. /* for ChipIO node */
  447. VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
  448. VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
  449. VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
  450. VENDOR_CHIPIO_DATA_LOW = 0x300,
  451. VENDOR_CHIPIO_DATA_HIGH = 0x400,
  452. VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
  453. VENDOR_CHIPIO_STATUS = 0xF01,
  454. VENDOR_CHIPIO_HIC_POST_READ = 0x702,
  455. VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
  456. VENDOR_CHIPIO_8051_DATA_WRITE = 0x707,
  457. VENDOR_CHIPIO_8051_DATA_READ = 0xF07,
  458. VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
  459. VENDOR_CHIPIO_CT_EXTENSIONS_GET = 0xF0A,
  460. VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
  461. VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
  462. VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
  463. VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
  464. VENDOR_CHIPIO_FLAG_SET = 0x70F,
  465. VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
  466. VENDOR_CHIPIO_PARAM_SET = 0x710,
  467. VENDOR_CHIPIO_PARAM_GET = 0xF10,
  468. VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
  469. VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
  470. VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
  471. VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
  472. VENDOR_CHIPIO_PARAM_EX_ID_GET = 0xF17,
  473. VENDOR_CHIPIO_PARAM_EX_ID_SET = 0x717,
  474. VENDOR_CHIPIO_PARAM_EX_VALUE_GET = 0xF18,
  475. VENDOR_CHIPIO_PARAM_EX_VALUE_SET = 0x718,
  476. VENDOR_CHIPIO_DMIC_CTL_SET = 0x788,
  477. VENDOR_CHIPIO_DMIC_CTL_GET = 0xF88,
  478. VENDOR_CHIPIO_DMIC_PIN_SET = 0x789,
  479. VENDOR_CHIPIO_DMIC_PIN_GET = 0xF89,
  480. VENDOR_CHIPIO_DMIC_MCLK_SET = 0x78A,
  481. VENDOR_CHIPIO_DMIC_MCLK_GET = 0xF8A,
  482. VENDOR_CHIPIO_EAPD_SEL_SET = 0x78D
  483. };
  484. /*
  485. * Control flag IDs
  486. */
  487. enum control_flag_id {
  488. /* Connection manager stream setup is bypassed/enabled */
  489. CONTROL_FLAG_C_MGR = 0,
  490. /* DSP DMA is bypassed/enabled */
  491. CONTROL_FLAG_DMA = 1,
  492. /* 8051 'idle' mode is disabled/enabled */
  493. CONTROL_FLAG_IDLE_ENABLE = 2,
  494. /* Tracker for the SPDIF-in path is bypassed/enabled */
  495. CONTROL_FLAG_TRACKER = 3,
  496. /* DigitalOut to Spdif2Out connection is disabled/enabled */
  497. CONTROL_FLAG_SPDIF2OUT = 4,
  498. /* Digital Microphone is disabled/enabled */
  499. CONTROL_FLAG_DMIC = 5,
  500. /* ADC_B rate is 48 kHz/96 kHz */
  501. CONTROL_FLAG_ADC_B_96KHZ = 6,
  502. /* ADC_C rate is 48 kHz/96 kHz */
  503. CONTROL_FLAG_ADC_C_96KHZ = 7,
  504. /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
  505. CONTROL_FLAG_DAC_96KHZ = 8,
  506. /* DSP rate is 48 kHz/96 kHz */
  507. CONTROL_FLAG_DSP_96KHZ = 9,
  508. /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
  509. CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
  510. /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
  511. CONTROL_FLAG_SRC_RATE_96KHZ = 11,
  512. /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
  513. CONTROL_FLAG_DECODE_LOOP = 12,
  514. /* De-emphasis filter on DAC-1 disabled/enabled */
  515. CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
  516. /* De-emphasis filter on DAC-2 disabled/enabled */
  517. CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
  518. /* De-emphasis filter on DAC-3 disabled/enabled */
  519. CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
  520. /* High-pass filter on ADC_B disabled/enabled */
  521. CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
  522. /* High-pass filter on ADC_C disabled/enabled */
  523. CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
  524. /* Common mode on Port_A disabled/enabled */
  525. CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
  526. /* Common mode on Port_D disabled/enabled */
  527. CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
  528. /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
  529. CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
  530. /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
  531. CONTROL_FLAG_PORT_D_10KOHM_LOAD = 21,
  532. /* ASI rate is 48kHz/96kHz */
  533. CONTROL_FLAG_ASI_96KHZ = 22,
  534. /* DAC power settings able to control attached ports no/yes */
  535. CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
  536. /* Clock Stop OK reporting is disabled/enabled */
  537. CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
  538. /* Number of control flags */
  539. CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
  540. };
  541. /*
  542. * Control parameter IDs
  543. */
  544. enum control_param_id {
  545. /* 0: None, 1: Mic1In*/
  546. CONTROL_PARAM_VIP_SOURCE = 1,
  547. /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
  548. CONTROL_PARAM_SPDIF1_SOURCE = 2,
  549. /* Port A output stage gain setting to use when 16 Ohm output
  550. * impedance is selected*/
  551. CONTROL_PARAM_PORTA_160OHM_GAIN = 8,
  552. /* Port D output stage gain setting to use when 16 Ohm output
  553. * impedance is selected*/
  554. CONTROL_PARAM_PORTD_160OHM_GAIN = 10,
  555. /* Stream Control */
  556. /* Select stream with the given ID */
  557. CONTROL_PARAM_STREAM_ID = 24,
  558. /* Source connection point for the selected stream */
  559. CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
  560. /* Destination connection point for the selected stream */
  561. CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
  562. /* Number of audio channels in the selected stream */
  563. CONTROL_PARAM_STREAMS_CHANNELS = 27,
  564. /*Enable control for the selected stream */
  565. CONTROL_PARAM_STREAM_CONTROL = 28,
  566. /* Connection Point Control */
  567. /* Select connection point with the given ID */
  568. CONTROL_PARAM_CONN_POINT_ID = 29,
  569. /* Connection point sample rate */
  570. CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
  571. /* Node Control */
  572. /* Select HDA node with the given ID */
  573. CONTROL_PARAM_NODE_ID = 31
  574. };
  575. /*
  576. * Dsp Io Status codes
  577. */
  578. enum hda_vendor_status_dspio {
  579. /* Success */
  580. VENDOR_STATUS_DSPIO_OK = 0x00,
  581. /* Busy, unable to accept new command, the host must retry */
  582. VENDOR_STATUS_DSPIO_BUSY = 0x01,
  583. /* SCP command queue is full */
  584. VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
  585. /* SCP response queue is empty */
  586. VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
  587. };
  588. /*
  589. * Chip Io Status codes
  590. */
  591. enum hda_vendor_status_chipio {
  592. /* Success */
  593. VENDOR_STATUS_CHIPIO_OK = 0x00,
  594. /* Busy, unable to accept new command, the host must retry */
  595. VENDOR_STATUS_CHIPIO_BUSY = 0x01
  596. };
  597. /*
  598. * CA0132 sample rate
  599. */
  600. enum ca0132_sample_rate {
  601. SR_6_000 = 0x00,
  602. SR_8_000 = 0x01,
  603. SR_9_600 = 0x02,
  604. SR_11_025 = 0x03,
  605. SR_16_000 = 0x04,
  606. SR_22_050 = 0x05,
  607. SR_24_000 = 0x06,
  608. SR_32_000 = 0x07,
  609. SR_44_100 = 0x08,
  610. SR_48_000 = 0x09,
  611. SR_88_200 = 0x0A,
  612. SR_96_000 = 0x0B,
  613. SR_144_000 = 0x0C,
  614. SR_176_400 = 0x0D,
  615. SR_192_000 = 0x0E,
  616. SR_384_000 = 0x0F,
  617. SR_COUNT = 0x10,
  618. SR_RATE_UNKNOWN = 0x1F
  619. };
  620. enum dsp_download_state {
  621. DSP_DOWNLOAD_FAILED = -1,
  622. DSP_DOWNLOAD_INIT = 0,
  623. DSP_DOWNLOADING = 1,
  624. DSP_DOWNLOADED = 2
  625. };
  626. /* retrieve parameters from hda format */
  627. #define get_hdafmt_chs(fmt) (fmt & 0xf)
  628. #define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
  629. #define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
  630. #define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
  631. /*
  632. * CA0132 specific
  633. */
  634. struct ca0132_spec {
  635. struct snd_kcontrol_new *mixers[5];
  636. unsigned int num_mixers;
  637. const struct hda_verb *base_init_verbs;
  638. const struct hda_verb *base_exit_verbs;
  639. const struct hda_verb *init_verbs[5];
  640. unsigned int num_init_verbs; /* exclude base init verbs */
  641. struct auto_pin_cfg autocfg;
  642. /* Nodes configurations */
  643. struct hda_multi_out multiout;
  644. hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
  645. hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
  646. unsigned int num_outputs;
  647. hda_nid_t input_pins[AUTO_PIN_LAST];
  648. hda_nid_t adcs[AUTO_PIN_LAST];
  649. hda_nid_t dig_out;
  650. hda_nid_t dig_in;
  651. unsigned int num_inputs;
  652. hda_nid_t shared_mic_nid;
  653. hda_nid_t shared_out_nid;
  654. struct hda_pcm pcm_rec[5]; /* PCM information */
  655. /* chip access */
  656. struct mutex chipio_mutex; /* chip access mutex */
  657. u32 curr_chip_addx;
  658. /* DSP download related */
  659. enum dsp_download_state dsp_state;
  660. unsigned int dsp_stream_id;
  661. unsigned int wait_scp;
  662. unsigned int wait_scp_header;
  663. unsigned int wait_num_data;
  664. unsigned int scp_resp_header;
  665. unsigned int scp_resp_data[4];
  666. unsigned int scp_resp_count;
  667. /* mixer and effects related */
  668. unsigned char dmic_ctl;
  669. int cur_out_type;
  670. int cur_mic_type;
  671. long vnode_lvol[VNODES_COUNT];
  672. long vnode_rvol[VNODES_COUNT];
  673. long vnode_lswitch[VNODES_COUNT];
  674. long vnode_rswitch[VNODES_COUNT];
  675. long effects_switch[EFFECTS_COUNT];
  676. long voicefx_val;
  677. long cur_mic_boost;
  678. #ifdef ENABLE_TUNING_CONTROLS
  679. long cur_ctl_vals[TUNING_CTLS_COUNT];
  680. #endif
  681. };
  682. /*
  683. * CA0132 codec access
  684. */
  685. unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
  686. unsigned int verb, unsigned int parm, unsigned int *res)
  687. {
  688. unsigned int response;
  689. response = snd_hda_codec_read(codec, nid, 0, verb, parm);
  690. *res = response;
  691. return ((response == -1) ? -1 : 0);
  692. }
  693. static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
  694. unsigned short converter_format, unsigned int *res)
  695. {
  696. return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
  697. converter_format & 0xffff, res);
  698. }
  699. static int codec_set_converter_stream_channel(struct hda_codec *codec,
  700. hda_nid_t nid, unsigned char stream,
  701. unsigned char channel, unsigned int *res)
  702. {
  703. unsigned char converter_stream_channel = 0;
  704. converter_stream_channel = (stream << 4) | (channel & 0x0f);
  705. return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
  706. converter_stream_channel, res);
  707. }
  708. /* Chip access helper function */
  709. static int chipio_send(struct hda_codec *codec,
  710. unsigned int reg,
  711. unsigned int data)
  712. {
  713. unsigned int res;
  714. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  715. /* send bits of data specified by reg */
  716. do {
  717. res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  718. reg, data);
  719. if (res == VENDOR_STATUS_CHIPIO_OK)
  720. return 0;
  721. msleep(20);
  722. } while (time_before(jiffies, timeout));
  723. return -EIO;
  724. }
  725. /*
  726. * Write chip address through the vendor widget -- NOT protected by the Mutex!
  727. */
  728. static int chipio_write_address(struct hda_codec *codec,
  729. unsigned int chip_addx)
  730. {
  731. struct ca0132_spec *spec = codec->spec;
  732. int res;
  733. if (spec->curr_chip_addx == chip_addx)
  734. return 0;
  735. /* send low 16 bits of the address */
  736. res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
  737. chip_addx & 0xffff);
  738. if (res != -EIO) {
  739. /* send high 16 bits of the address */
  740. res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
  741. chip_addx >> 16);
  742. }
  743. spec->curr_chip_addx = (res < 0) ? ~0UL : chip_addx;
  744. return res;
  745. }
  746. /*
  747. * Write data through the vendor widget -- NOT protected by the Mutex!
  748. */
  749. static int chipio_write_data(struct hda_codec *codec, unsigned int data)
  750. {
  751. struct ca0132_spec *spec = codec->spec;
  752. int res;
  753. /* send low 16 bits of the data */
  754. res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
  755. if (res != -EIO) {
  756. /* send high 16 bits of the data */
  757. res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
  758. data >> 16);
  759. }
  760. /*If no error encountered, automatically increment the address
  761. as per chip behaviour*/
  762. spec->curr_chip_addx = (res != -EIO) ?
  763. (spec->curr_chip_addx + 4) : ~0UL;
  764. return res;
  765. }
  766. /*
  767. * Write multiple data through the vendor widget -- NOT protected by the Mutex!
  768. */
  769. static int chipio_write_data_multiple(struct hda_codec *codec,
  770. const u32 *data,
  771. unsigned int count)
  772. {
  773. int status = 0;
  774. if (data == NULL) {
  775. snd_printdd(KERN_ERR "chipio_write_data null ptr\n");
  776. return -EINVAL;
  777. }
  778. while ((count-- != 0) && (status == 0))
  779. status = chipio_write_data(codec, *data++);
  780. return status;
  781. }
  782. /*
  783. * Read data through the vendor widget -- NOT protected by the Mutex!
  784. */
  785. static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
  786. {
  787. struct ca0132_spec *spec = codec->spec;
  788. int res;
  789. /* post read */
  790. res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
  791. if (res != -EIO) {
  792. /* read status */
  793. res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  794. }
  795. if (res != -EIO) {
  796. /* read data */
  797. *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  798. VENDOR_CHIPIO_HIC_READ_DATA,
  799. 0);
  800. }
  801. /*If no error encountered, automatically increment the address
  802. as per chip behaviour*/
  803. spec->curr_chip_addx = (res != -EIO) ?
  804. (spec->curr_chip_addx + 4) : ~0UL;
  805. return res;
  806. }
  807. /*
  808. * Write given value to the given address through the chip I/O widget.
  809. * protected by the Mutex
  810. */
  811. static int chipio_write(struct hda_codec *codec,
  812. unsigned int chip_addx, const unsigned int data)
  813. {
  814. struct ca0132_spec *spec = codec->spec;
  815. int err;
  816. mutex_lock(&spec->chipio_mutex);
  817. /* write the address, and if successful proceed to write data */
  818. err = chipio_write_address(codec, chip_addx);
  819. if (err < 0)
  820. goto exit;
  821. err = chipio_write_data(codec, data);
  822. if (err < 0)
  823. goto exit;
  824. exit:
  825. mutex_unlock(&spec->chipio_mutex);
  826. return err;
  827. }
  828. /*
  829. * Write multiple values to the given address through the chip I/O widget.
  830. * protected by the Mutex
  831. */
  832. static int chipio_write_multiple(struct hda_codec *codec,
  833. u32 chip_addx,
  834. const u32 *data,
  835. unsigned int count)
  836. {
  837. struct ca0132_spec *spec = codec->spec;
  838. int status;
  839. mutex_lock(&spec->chipio_mutex);
  840. status = chipio_write_address(codec, chip_addx);
  841. if (status < 0)
  842. goto error;
  843. status = chipio_write_data_multiple(codec, data, count);
  844. error:
  845. mutex_unlock(&spec->chipio_mutex);
  846. return status;
  847. }
  848. /*
  849. * Read the given address through the chip I/O widget
  850. * protected by the Mutex
  851. */
  852. static int chipio_read(struct hda_codec *codec,
  853. unsigned int chip_addx, unsigned int *data)
  854. {
  855. struct ca0132_spec *spec = codec->spec;
  856. int err;
  857. mutex_lock(&spec->chipio_mutex);
  858. /* write the address, and if successful proceed to write data */
  859. err = chipio_write_address(codec, chip_addx);
  860. if (err < 0)
  861. goto exit;
  862. err = chipio_read_data(codec, data);
  863. if (err < 0)
  864. goto exit;
  865. exit:
  866. mutex_unlock(&spec->chipio_mutex);
  867. return err;
  868. }
  869. /*
  870. * Set chip control flags through the chip I/O widget.
  871. */
  872. static void chipio_set_control_flag(struct hda_codec *codec,
  873. enum control_flag_id flag_id,
  874. bool flag_state)
  875. {
  876. unsigned int val;
  877. unsigned int flag_bit;
  878. flag_bit = (flag_state ? 1 : 0);
  879. val = (flag_bit << 7) | (flag_id);
  880. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  881. VENDOR_CHIPIO_FLAG_SET, val);
  882. }
  883. /*
  884. * Set chip parameters through the chip I/O widget.
  885. */
  886. static void chipio_set_control_param(struct hda_codec *codec,
  887. enum control_param_id param_id, int param_val)
  888. {
  889. struct ca0132_spec *spec = codec->spec;
  890. int val;
  891. if ((param_id < 32) && (param_val < 8)) {
  892. val = (param_val << 5) | (param_id);
  893. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  894. VENDOR_CHIPIO_PARAM_SET, val);
  895. } else {
  896. mutex_lock(&spec->chipio_mutex);
  897. if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
  898. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  899. VENDOR_CHIPIO_PARAM_EX_ID_SET,
  900. param_id);
  901. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  902. VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
  903. param_val);
  904. }
  905. mutex_unlock(&spec->chipio_mutex);
  906. }
  907. }
  908. /*
  909. * Set sampling rate of the connection point.
  910. */
  911. static void chipio_set_conn_rate(struct hda_codec *codec,
  912. int connid, enum ca0132_sample_rate rate)
  913. {
  914. chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
  915. chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
  916. rate);
  917. }
  918. /*
  919. * Enable clocks.
  920. */
  921. static void chipio_enable_clocks(struct hda_codec *codec)
  922. {
  923. struct ca0132_spec *spec = codec->spec;
  924. mutex_lock(&spec->chipio_mutex);
  925. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  926. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
  927. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  928. VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
  929. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  930. VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
  931. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  932. VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
  933. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  934. VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
  935. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  936. VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
  937. mutex_unlock(&spec->chipio_mutex);
  938. }
  939. /*
  940. * CA0132 DSP IO stuffs
  941. */
  942. static int dspio_send(struct hda_codec *codec, unsigned int reg,
  943. unsigned int data)
  944. {
  945. int res;
  946. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  947. /* send bits of data specified by reg to dsp */
  948. do {
  949. res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
  950. if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
  951. return res;
  952. msleep(20);
  953. } while (time_before(jiffies, timeout));
  954. return -EIO;
  955. }
  956. /*
  957. * Wait for DSP to be ready for commands
  958. */
  959. static void dspio_write_wait(struct hda_codec *codec)
  960. {
  961. int status;
  962. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  963. do {
  964. status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  965. VENDOR_DSPIO_STATUS, 0);
  966. if ((status == VENDOR_STATUS_DSPIO_OK) ||
  967. (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
  968. break;
  969. msleep(1);
  970. } while (time_before(jiffies, timeout));
  971. }
  972. /*
  973. * Write SCP data to DSP
  974. */
  975. static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
  976. {
  977. struct ca0132_spec *spec = codec->spec;
  978. int status;
  979. dspio_write_wait(codec);
  980. mutex_lock(&spec->chipio_mutex);
  981. status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
  982. scp_data & 0xffff);
  983. if (status < 0)
  984. goto error;
  985. status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
  986. scp_data >> 16);
  987. if (status < 0)
  988. goto error;
  989. /* OK, now check if the write itself has executed*/
  990. status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  991. VENDOR_DSPIO_STATUS, 0);
  992. error:
  993. mutex_unlock(&spec->chipio_mutex);
  994. return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
  995. -EIO : 0;
  996. }
  997. /*
  998. * Write multiple SCP data to DSP
  999. */
  1000. static int dspio_write_multiple(struct hda_codec *codec,
  1001. unsigned int *buffer, unsigned int size)
  1002. {
  1003. int status = 0;
  1004. unsigned int count;
  1005. if ((buffer == NULL))
  1006. return -EINVAL;
  1007. count = 0;
  1008. while (count < size) {
  1009. status = dspio_write(codec, *buffer++);
  1010. if (status != 0)
  1011. break;
  1012. count++;
  1013. }
  1014. return status;
  1015. }
  1016. static int dspio_read(struct hda_codec *codec, unsigned int *data)
  1017. {
  1018. int status;
  1019. status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
  1020. if (status == -EIO)
  1021. return status;
  1022. status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
  1023. if (status == -EIO ||
  1024. status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY)
  1025. return -EIO;
  1026. *data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  1027. VENDOR_DSPIO_SCP_READ_DATA, 0);
  1028. return 0;
  1029. }
  1030. static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
  1031. unsigned int *buf_size, unsigned int size_count)
  1032. {
  1033. int status = 0;
  1034. unsigned int size = *buf_size;
  1035. unsigned int count;
  1036. unsigned int skip_count;
  1037. unsigned int dummy;
  1038. if ((buffer == NULL))
  1039. return -1;
  1040. count = 0;
  1041. while (count < size && count < size_count) {
  1042. status = dspio_read(codec, buffer++);
  1043. if (status != 0)
  1044. break;
  1045. count++;
  1046. }
  1047. skip_count = count;
  1048. if (status == 0) {
  1049. while (skip_count < size) {
  1050. status = dspio_read(codec, &dummy);
  1051. if (status != 0)
  1052. break;
  1053. skip_count++;
  1054. }
  1055. }
  1056. *buf_size = count;
  1057. return status;
  1058. }
  1059. /*
  1060. * Construct the SCP header using corresponding fields
  1061. */
  1062. static inline unsigned int
  1063. make_scp_header(unsigned int target_id, unsigned int source_id,
  1064. unsigned int get_flag, unsigned int req,
  1065. unsigned int device_flag, unsigned int resp_flag,
  1066. unsigned int error_flag, unsigned int data_size)
  1067. {
  1068. unsigned int header = 0;
  1069. header = (data_size & 0x1f) << 27;
  1070. header |= (error_flag & 0x01) << 26;
  1071. header |= (resp_flag & 0x01) << 25;
  1072. header |= (device_flag & 0x01) << 24;
  1073. header |= (req & 0x7f) << 17;
  1074. header |= (get_flag & 0x01) << 16;
  1075. header |= (source_id & 0xff) << 8;
  1076. header |= target_id & 0xff;
  1077. return header;
  1078. }
  1079. /*
  1080. * Extract corresponding fields from SCP header
  1081. */
  1082. static inline void
  1083. extract_scp_header(unsigned int header,
  1084. unsigned int *target_id, unsigned int *source_id,
  1085. unsigned int *get_flag, unsigned int *req,
  1086. unsigned int *device_flag, unsigned int *resp_flag,
  1087. unsigned int *error_flag, unsigned int *data_size)
  1088. {
  1089. if (data_size)
  1090. *data_size = (header >> 27) & 0x1f;
  1091. if (error_flag)
  1092. *error_flag = (header >> 26) & 0x01;
  1093. if (resp_flag)
  1094. *resp_flag = (header >> 25) & 0x01;
  1095. if (device_flag)
  1096. *device_flag = (header >> 24) & 0x01;
  1097. if (req)
  1098. *req = (header >> 17) & 0x7f;
  1099. if (get_flag)
  1100. *get_flag = (header >> 16) & 0x01;
  1101. if (source_id)
  1102. *source_id = (header >> 8) & 0xff;
  1103. if (target_id)
  1104. *target_id = header & 0xff;
  1105. }
  1106. #define SCP_MAX_DATA_WORDS (16)
  1107. /* Structure to contain any SCP message */
  1108. struct scp_msg {
  1109. unsigned int hdr;
  1110. unsigned int data[SCP_MAX_DATA_WORDS];
  1111. };
  1112. static void dspio_clear_response_queue(struct hda_codec *codec)
  1113. {
  1114. unsigned int dummy = 0;
  1115. int status = -1;
  1116. /* clear all from the response queue */
  1117. do {
  1118. status = dspio_read(codec, &dummy);
  1119. } while (status == 0);
  1120. }
  1121. static int dspio_get_response_data(struct hda_codec *codec)
  1122. {
  1123. struct ca0132_spec *spec = codec->spec;
  1124. unsigned int data = 0;
  1125. unsigned int count;
  1126. if (dspio_read(codec, &data) < 0)
  1127. return -EIO;
  1128. if ((data & 0x00ffffff) == spec->wait_scp_header) {
  1129. spec->scp_resp_header = data;
  1130. spec->scp_resp_count = data >> 27;
  1131. count = spec->wait_num_data;
  1132. dspio_read_multiple(codec, spec->scp_resp_data,
  1133. &spec->scp_resp_count, count);
  1134. return 0;
  1135. }
  1136. return -EIO;
  1137. }
  1138. /*
  1139. * Send SCP message to DSP
  1140. */
  1141. static int dspio_send_scp_message(struct hda_codec *codec,
  1142. unsigned char *send_buf,
  1143. unsigned int send_buf_size,
  1144. unsigned char *return_buf,
  1145. unsigned int return_buf_size,
  1146. unsigned int *bytes_returned)
  1147. {
  1148. struct ca0132_spec *spec = codec->spec;
  1149. int status = -1;
  1150. unsigned int scp_send_size = 0;
  1151. unsigned int total_size;
  1152. bool waiting_for_resp = false;
  1153. unsigned int header;
  1154. struct scp_msg *ret_msg;
  1155. unsigned int resp_src_id, resp_target_id;
  1156. unsigned int data_size, src_id, target_id, get_flag, device_flag;
  1157. if (bytes_returned)
  1158. *bytes_returned = 0;
  1159. /* get scp header from buffer */
  1160. header = *((unsigned int *)send_buf);
  1161. extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
  1162. &device_flag, NULL, NULL, &data_size);
  1163. scp_send_size = data_size + 1;
  1164. total_size = (scp_send_size * 4);
  1165. if (send_buf_size < total_size)
  1166. return -EINVAL;
  1167. if (get_flag || device_flag) {
  1168. if (!return_buf || return_buf_size < 4 || !bytes_returned)
  1169. return -EINVAL;
  1170. spec->wait_scp_header = *((unsigned int *)send_buf);
  1171. /* swap source id with target id */
  1172. resp_target_id = src_id;
  1173. resp_src_id = target_id;
  1174. spec->wait_scp_header &= 0xffff0000;
  1175. spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
  1176. spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
  1177. spec->wait_scp = 1;
  1178. waiting_for_resp = true;
  1179. }
  1180. status = dspio_write_multiple(codec, (unsigned int *)send_buf,
  1181. scp_send_size);
  1182. if (status < 0) {
  1183. spec->wait_scp = 0;
  1184. return status;
  1185. }
  1186. if (waiting_for_resp) {
  1187. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  1188. memset(return_buf, 0, return_buf_size);
  1189. do {
  1190. msleep(20);
  1191. } while (spec->wait_scp && time_before(jiffies, timeout));
  1192. waiting_for_resp = false;
  1193. if (!spec->wait_scp) {
  1194. ret_msg = (struct scp_msg *)return_buf;
  1195. memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
  1196. memcpy(&ret_msg->data, spec->scp_resp_data,
  1197. spec->wait_num_data);
  1198. *bytes_returned = (spec->scp_resp_count + 1) * 4;
  1199. status = 0;
  1200. } else {
  1201. status = -EIO;
  1202. }
  1203. spec->wait_scp = 0;
  1204. }
  1205. return status;
  1206. }
  1207. /**
  1208. * Prepare and send the SCP message to DSP
  1209. * @codec: the HDA codec
  1210. * @mod_id: ID of the DSP module to send the command
  1211. * @req: ID of request to send to the DSP module
  1212. * @dir: SET or GET
  1213. * @data: pointer to the data to send with the request, request specific
  1214. * @len: length of the data, in bytes
  1215. * @reply: point to the buffer to hold data returned for a reply
  1216. * @reply_len: length of the reply buffer returned from GET
  1217. *
  1218. * Returns zero or a negative error code.
  1219. */
  1220. static int dspio_scp(struct hda_codec *codec,
  1221. int mod_id, int req, int dir, void *data, unsigned int len,
  1222. void *reply, unsigned int *reply_len)
  1223. {
  1224. int status = 0;
  1225. struct scp_msg scp_send, scp_reply;
  1226. unsigned int ret_bytes, send_size, ret_size;
  1227. unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
  1228. unsigned int reply_data_size;
  1229. memset(&scp_send, 0, sizeof(scp_send));
  1230. memset(&scp_reply, 0, sizeof(scp_reply));
  1231. if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
  1232. return -EINVAL;
  1233. if (dir == SCP_GET && reply == NULL) {
  1234. snd_printdd(KERN_ERR "dspio_scp get but has no buffer\n");
  1235. return -EINVAL;
  1236. }
  1237. if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
  1238. snd_printdd(KERN_ERR "dspio_scp bad resp buf len parms\n");
  1239. return -EINVAL;
  1240. }
  1241. scp_send.hdr = make_scp_header(mod_id, 0x20, (dir == SCP_GET), req,
  1242. 0, 0, 0, len/sizeof(unsigned int));
  1243. if (data != NULL && len > 0) {
  1244. len = min((unsigned int)(sizeof(scp_send.data)), len);
  1245. memcpy(scp_send.data, data, len);
  1246. }
  1247. ret_bytes = 0;
  1248. send_size = sizeof(unsigned int) + len;
  1249. status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
  1250. send_size, (unsigned char *)&scp_reply,
  1251. sizeof(scp_reply), &ret_bytes);
  1252. if (status < 0) {
  1253. snd_printdd(KERN_ERR "dspio_scp: send scp msg failed\n");
  1254. return status;
  1255. }
  1256. /* extract send and reply headers members */
  1257. extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
  1258. NULL, NULL, NULL, NULL, NULL);
  1259. extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
  1260. &reply_resp_flag, &reply_error_flag,
  1261. &reply_data_size);
  1262. if (!send_get_flag)
  1263. return 0;
  1264. if (reply_resp_flag && !reply_error_flag) {
  1265. ret_size = (ret_bytes - sizeof(scp_reply.hdr))
  1266. / sizeof(unsigned int);
  1267. if (*reply_len < ret_size*sizeof(unsigned int)) {
  1268. snd_printdd(KERN_ERR "reply too long for buf\n");
  1269. return -EINVAL;
  1270. } else if (ret_size != reply_data_size) {
  1271. snd_printdd(KERN_ERR "RetLen and HdrLen .NE.\n");
  1272. return -EINVAL;
  1273. } else {
  1274. *reply_len = ret_size*sizeof(unsigned int);
  1275. memcpy(reply, scp_reply.data, *reply_len);
  1276. }
  1277. } else {
  1278. snd_printdd(KERN_ERR "reply ill-formed or errflag set\n");
  1279. return -EIO;
  1280. }
  1281. return status;
  1282. }
  1283. /*
  1284. * Set DSP parameters
  1285. */
  1286. static int dspio_set_param(struct hda_codec *codec, int mod_id,
  1287. int req, void *data, unsigned int len)
  1288. {
  1289. return dspio_scp(codec, mod_id, req, SCP_SET, data, len, NULL, NULL);
  1290. }
  1291. static int dspio_set_uint_param(struct hda_codec *codec, int mod_id,
  1292. int req, unsigned int data)
  1293. {
  1294. return dspio_set_param(codec, mod_id, req, &data, sizeof(unsigned int));
  1295. }
  1296. /*
  1297. * Allocate a DSP DMA channel via an SCP message
  1298. */
  1299. static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
  1300. {
  1301. int status = 0;
  1302. unsigned int size = sizeof(dma_chan);
  1303. snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- begin\n");
  1304. status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
  1305. SCP_GET, NULL, 0, dma_chan, &size);
  1306. if (status < 0) {
  1307. snd_printdd(KERN_INFO "dspio_alloc_dma_chan: SCP Failed\n");
  1308. return status;
  1309. }
  1310. if ((*dma_chan + 1) == 0) {
  1311. snd_printdd(KERN_INFO "no free dma channels to allocate\n");
  1312. return -EBUSY;
  1313. }
  1314. snd_printdd("dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
  1315. snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- complete\n");
  1316. return status;
  1317. }
  1318. /*
  1319. * Free a DSP DMA via an SCP message
  1320. */
  1321. static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
  1322. {
  1323. int status = 0;
  1324. unsigned int dummy = 0;
  1325. snd_printdd(KERN_INFO " dspio_free_dma_chan() -- begin\n");
  1326. snd_printdd("dspio_free_dma_chan: chan=%d\n", dma_chan);
  1327. status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
  1328. SCP_SET, &dma_chan, sizeof(dma_chan), NULL, &dummy);
  1329. if (status < 0) {
  1330. snd_printdd(KERN_INFO "dspio_free_dma_chan: SCP Failed\n");
  1331. return status;
  1332. }
  1333. snd_printdd(KERN_INFO " dspio_free_dma_chan() -- complete\n");
  1334. return status;
  1335. }
  1336. /*
  1337. * (Re)start the DSP
  1338. */
  1339. static int dsp_set_run_state(struct hda_codec *codec)
  1340. {
  1341. unsigned int dbg_ctrl_reg;
  1342. unsigned int halt_state;
  1343. int err;
  1344. err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
  1345. if (err < 0)
  1346. return err;
  1347. halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
  1348. DSP_DBGCNTL_STATE_LOBIT;
  1349. if (halt_state != 0) {
  1350. dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
  1351. DSP_DBGCNTL_SS_MASK);
  1352. err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
  1353. dbg_ctrl_reg);
  1354. if (err < 0)
  1355. return err;
  1356. dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
  1357. DSP_DBGCNTL_EXEC_MASK;
  1358. err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
  1359. dbg_ctrl_reg);
  1360. if (err < 0)
  1361. return err;
  1362. }
  1363. return 0;
  1364. }
  1365. /*
  1366. * Reset the DSP
  1367. */
  1368. static int dsp_reset(struct hda_codec *codec)
  1369. {
  1370. unsigned int res;
  1371. int retry = 20;
  1372. snd_printdd("dsp_reset\n");
  1373. do {
  1374. res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
  1375. retry--;
  1376. } while (res == -EIO && retry);
  1377. if (!retry) {
  1378. snd_printdd("dsp_reset timeout\n");
  1379. return -EIO;
  1380. }
  1381. return 0;
  1382. }
  1383. /*
  1384. * Convert chip address to DSP address
  1385. */
  1386. static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
  1387. bool *code, bool *yram)
  1388. {
  1389. *code = *yram = false;
  1390. if (UC_RANGE(chip_addx, 1)) {
  1391. *code = true;
  1392. return UC_OFF(chip_addx);
  1393. } else if (X_RANGE_ALL(chip_addx, 1)) {
  1394. return X_OFF(chip_addx);
  1395. } else if (Y_RANGE_ALL(chip_addx, 1)) {
  1396. *yram = true;
  1397. return Y_OFF(chip_addx);
  1398. }
  1399. return INVALID_CHIP_ADDRESS;
  1400. }
  1401. /*
  1402. * Check if the DSP DMA is active
  1403. */
  1404. static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
  1405. {
  1406. unsigned int dma_chnlstart_reg;
  1407. chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
  1408. return ((dma_chnlstart_reg & (1 <<
  1409. (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
  1410. }
  1411. static int dsp_dma_setup_common(struct hda_codec *codec,
  1412. unsigned int chip_addx,
  1413. unsigned int dma_chan,
  1414. unsigned int port_map_mask,
  1415. bool ovly)
  1416. {
  1417. int status = 0;
  1418. unsigned int chnl_prop;
  1419. unsigned int dsp_addx;
  1420. unsigned int active;
  1421. bool code, yram;
  1422. snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Begin ---------\n");
  1423. if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
  1424. snd_printdd(KERN_ERR "dma chan num invalid\n");
  1425. return -EINVAL;
  1426. }
  1427. if (dsp_is_dma_active(codec, dma_chan)) {
  1428. snd_printdd(KERN_ERR "dma already active\n");
  1429. return -EBUSY;
  1430. }
  1431. dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
  1432. if (dsp_addx == INVALID_CHIP_ADDRESS) {
  1433. snd_printdd(KERN_ERR "invalid chip addr\n");
  1434. return -ENXIO;
  1435. }
  1436. chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
  1437. active = 0;
  1438. snd_printdd(KERN_INFO " dsp_dma_setup_common() start reg pgm\n");
  1439. if (ovly) {
  1440. status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
  1441. &chnl_prop);
  1442. if (status < 0) {
  1443. snd_printdd(KERN_ERR "read CHNLPROP Reg fail\n");
  1444. return status;
  1445. }
  1446. snd_printdd(KERN_INFO "dsp_dma_setup_common() Read CHNLPROP\n");
  1447. }
  1448. if (!code)
  1449. chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
  1450. else
  1451. chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
  1452. chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
  1453. status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
  1454. if (status < 0) {
  1455. snd_printdd(KERN_ERR "write CHNLPROP Reg fail\n");
  1456. return status;
  1457. }
  1458. snd_printdd(KERN_INFO " dsp_dma_setup_common() Write CHNLPROP\n");
  1459. if (ovly) {
  1460. status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
  1461. &active);
  1462. if (status < 0) {
  1463. snd_printdd(KERN_ERR "read ACTIVE Reg fail\n");
  1464. return status;
  1465. }
  1466. snd_printdd(KERN_INFO "dsp_dma_setup_common() Read ACTIVE\n");
  1467. }
  1468. active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
  1469. DSPDMAC_ACTIVE_AAR_MASK;
  1470. status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
  1471. if (status < 0) {
  1472. snd_printdd(KERN_ERR "write ACTIVE Reg fail\n");
  1473. return status;
  1474. }
  1475. snd_printdd(KERN_INFO " dsp_dma_setup_common() Write ACTIVE\n");
  1476. status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
  1477. port_map_mask);
  1478. if (status < 0) {
  1479. snd_printdd(KERN_ERR "write AUDCHSEL Reg fail\n");
  1480. return status;
  1481. }
  1482. snd_printdd(KERN_INFO " dsp_dma_setup_common() Write AUDCHSEL\n");
  1483. status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
  1484. DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
  1485. if (status < 0) {
  1486. snd_printdd(KERN_ERR "write IRQCNT Reg fail\n");
  1487. return status;
  1488. }
  1489. snd_printdd(KERN_INFO " dsp_dma_setup_common() Write IRQCNT\n");
  1490. snd_printdd(
  1491. "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
  1492. "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
  1493. chip_addx, dsp_addx, dma_chan,
  1494. port_map_mask, chnl_prop, active);
  1495. snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Complete ------\n");
  1496. return 0;
  1497. }
  1498. /*
  1499. * Setup the DSP DMA per-transfer-specific registers
  1500. */
  1501. static int dsp_dma_setup(struct hda_codec *codec,
  1502. unsigned int chip_addx,
  1503. unsigned int count,
  1504. unsigned int dma_chan)
  1505. {
  1506. int status = 0;
  1507. bool code, yram;
  1508. unsigned int dsp_addx;
  1509. unsigned int addr_field;
  1510. unsigned int incr_field;
  1511. unsigned int base_cnt;
  1512. unsigned int cur_cnt;
  1513. unsigned int dma_cfg = 0;
  1514. unsigned int adr_ofs = 0;
  1515. unsigned int xfr_cnt = 0;
  1516. const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
  1517. DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
  1518. snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Begin ---------\n");
  1519. if (count > max_dma_count) {
  1520. snd_printdd(KERN_ERR "count too big\n");
  1521. return -EINVAL;
  1522. }
  1523. dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
  1524. if (dsp_addx == INVALID_CHIP_ADDRESS) {
  1525. snd_printdd(KERN_ERR "invalid chip addr\n");
  1526. return -ENXIO;
  1527. }
  1528. snd_printdd(KERN_INFO " dsp_dma_setup() start reg pgm\n");
  1529. addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
  1530. incr_field = 0;
  1531. if (!code) {
  1532. addr_field <<= 1;
  1533. if (yram)
  1534. addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
  1535. incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
  1536. }
  1537. dma_cfg = addr_field + incr_field;
  1538. status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
  1539. dma_cfg);
  1540. if (status < 0) {
  1541. snd_printdd(KERN_ERR "write DMACFG Reg fail\n");
  1542. return status;
  1543. }
  1544. snd_printdd(KERN_INFO " dsp_dma_setup() Write DMACFG\n");
  1545. adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
  1546. (code ? 0 : 1));
  1547. status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
  1548. adr_ofs);
  1549. if (status < 0) {
  1550. snd_printdd(KERN_ERR "write DSPADROFS Reg fail\n");
  1551. return status;
  1552. }
  1553. snd_printdd(KERN_INFO " dsp_dma_setup() Write DSPADROFS\n");
  1554. base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
  1555. cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
  1556. xfr_cnt = base_cnt | cur_cnt;
  1557. status = chipio_write(codec,
  1558. DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
  1559. if (status < 0) {
  1560. snd_printdd(KERN_ERR "write XFRCNT Reg fail\n");
  1561. return status;
  1562. }
  1563. snd_printdd(KERN_INFO " dsp_dma_setup() Write XFRCNT\n");
  1564. snd_printdd(
  1565. "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
  1566. "ADROFS=0x%x, XFRCNT=0x%x\n",
  1567. chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
  1568. snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Complete ---------\n");
  1569. return 0;
  1570. }
  1571. /*
  1572. * Start the DSP DMA
  1573. */
  1574. static int dsp_dma_start(struct hda_codec *codec,
  1575. unsigned int dma_chan, bool ovly)
  1576. {
  1577. unsigned int reg = 0;
  1578. int status = 0;
  1579. snd_printdd(KERN_INFO "-- dsp_dma_start() -- Begin ---------\n");
  1580. if (ovly) {
  1581. status = chipio_read(codec,
  1582. DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
  1583. if (status < 0) {
  1584. snd_printdd(KERN_ERR "read CHNLSTART reg fail\n");
  1585. return status;
  1586. }
  1587. snd_printdd(KERN_INFO "-- dsp_dma_start() Read CHNLSTART\n");
  1588. reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
  1589. DSPDMAC_CHNLSTART_DIS_MASK);
  1590. }
  1591. status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
  1592. reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
  1593. if (status < 0) {
  1594. snd_printdd(KERN_ERR "write CHNLSTART reg fail\n");
  1595. return status;
  1596. }
  1597. snd_printdd(KERN_INFO "-- dsp_dma_start() -- Complete ---------\n");
  1598. return status;
  1599. }
  1600. /*
  1601. * Stop the DSP DMA
  1602. */
  1603. static int dsp_dma_stop(struct hda_codec *codec,
  1604. unsigned int dma_chan, bool ovly)
  1605. {
  1606. unsigned int reg = 0;
  1607. int status = 0;
  1608. snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Begin ---------\n");
  1609. if (ovly) {
  1610. status = chipio_read(codec,
  1611. DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
  1612. if (status < 0) {
  1613. snd_printdd(KERN_ERR "read CHNLSTART reg fail\n");
  1614. return status;
  1615. }
  1616. snd_printdd(KERN_INFO "-- dsp_dma_stop() Read CHNLSTART\n");
  1617. reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
  1618. DSPDMAC_CHNLSTART_DIS_MASK);
  1619. }
  1620. status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
  1621. reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
  1622. if (status < 0) {
  1623. snd_printdd(KERN_ERR "write CHNLSTART reg fail\n");
  1624. return status;
  1625. }
  1626. snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Complete ---------\n");
  1627. return status;
  1628. }
  1629. /**
  1630. * Allocate router ports
  1631. *
  1632. * @codec: the HDA codec
  1633. * @num_chans: number of channels in the stream
  1634. * @ports_per_channel: number of ports per channel
  1635. * @start_device: start device
  1636. * @port_map: pointer to the port list to hold the allocated ports
  1637. *
  1638. * Returns zero or a negative error code.
  1639. */
  1640. static int dsp_allocate_router_ports(struct hda_codec *codec,
  1641. unsigned int num_chans,
  1642. unsigned int ports_per_channel,
  1643. unsigned int start_device,
  1644. unsigned int *port_map)
  1645. {
  1646. int status = 0;
  1647. int res;
  1648. u8 val;
  1649. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1650. if (status < 0)
  1651. return status;
  1652. val = start_device << 6;
  1653. val |= (ports_per_channel - 1) << 4;
  1654. val |= num_chans - 1;
  1655. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1656. VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
  1657. val);
  1658. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1659. VENDOR_CHIPIO_PORT_ALLOC_SET,
  1660. MEM_CONNID_DSP);
  1661. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1662. if (status < 0)
  1663. return status;
  1664. res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  1665. VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
  1666. *port_map = res;
  1667. return (res < 0) ? res : 0;
  1668. }
  1669. /*
  1670. * Free router ports
  1671. */
  1672. static int dsp_free_router_ports(struct hda_codec *codec)
  1673. {
  1674. int status = 0;
  1675. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1676. if (status < 0)
  1677. return status;
  1678. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1679. VENDOR_CHIPIO_PORT_FREE_SET,
  1680. MEM_CONNID_DSP);
  1681. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1682. return status;
  1683. }
  1684. /*
  1685. * Allocate DSP ports for the download stream
  1686. */
  1687. static int dsp_allocate_ports(struct hda_codec *codec,
  1688. unsigned int num_chans,
  1689. unsigned int rate_multi, unsigned int *port_map)
  1690. {
  1691. int status;
  1692. snd_printdd(KERN_INFO " dsp_allocate_ports() -- begin\n");
  1693. if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
  1694. snd_printdd(KERN_ERR "bad rate multiple\n");
  1695. return -EINVAL;
  1696. }
  1697. status = dsp_allocate_router_ports(codec, num_chans,
  1698. rate_multi, 0, port_map);
  1699. snd_printdd(KERN_INFO " dsp_allocate_ports() -- complete\n");
  1700. return status;
  1701. }
  1702. static int dsp_allocate_ports_format(struct hda_codec *codec,
  1703. const unsigned short fmt,
  1704. unsigned int *port_map)
  1705. {
  1706. int status;
  1707. unsigned int num_chans;
  1708. unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
  1709. unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
  1710. unsigned int rate_multi = sample_rate_mul / sample_rate_div;
  1711. if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
  1712. snd_printdd(KERN_ERR "bad rate multiple\n");
  1713. return -EINVAL;
  1714. }
  1715. num_chans = get_hdafmt_chs(fmt) + 1;
  1716. status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
  1717. return status;
  1718. }
  1719. /*
  1720. * free DSP ports
  1721. */
  1722. static int dsp_free_ports(struct hda_codec *codec)
  1723. {
  1724. int status;
  1725. snd_printdd(KERN_INFO " dsp_free_ports() -- begin\n");
  1726. status = dsp_free_router_ports(codec);
  1727. if (status < 0) {
  1728. snd_printdd(KERN_ERR "free router ports fail\n");
  1729. return status;
  1730. }
  1731. snd_printdd(KERN_INFO " dsp_free_ports() -- complete\n");
  1732. return status;
  1733. }
  1734. /*
  1735. * HDA DMA engine stuffs for DSP code download
  1736. */
  1737. struct dma_engine {
  1738. struct hda_codec *codec;
  1739. unsigned short m_converter_format;
  1740. struct snd_dma_buffer *dmab;
  1741. unsigned int buf_size;
  1742. };
  1743. enum dma_state {
  1744. DMA_STATE_STOP = 0,
  1745. DMA_STATE_RUN = 1
  1746. };
  1747. static int dma_convert_to_hda_format(
  1748. unsigned int sample_rate,
  1749. unsigned short channels,
  1750. unsigned short *hda_format)
  1751. {
  1752. unsigned int format_val;
  1753. format_val = snd_hda_calc_stream_format(
  1754. sample_rate,
  1755. channels,
  1756. SNDRV_PCM_FORMAT_S32_LE,
  1757. 32, 0);
  1758. if (hda_format)
  1759. *hda_format = (unsigned short)format_val;
  1760. return 0;
  1761. }
  1762. /*
  1763. * Reset DMA for DSP download
  1764. */
  1765. static int dma_reset(struct dma_engine *dma)
  1766. {
  1767. struct hda_codec *codec = dma->codec;
  1768. struct ca0132_spec *spec = codec->spec;
  1769. int status;
  1770. if (dma->dmab->area)
  1771. snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
  1772. status = snd_hda_codec_load_dsp_prepare(codec,
  1773. dma->m_converter_format,
  1774. dma->buf_size,
  1775. dma->dmab);
  1776. if (status < 0)
  1777. return status;
  1778. spec->dsp_stream_id = status;
  1779. return 0;
  1780. }
  1781. static int dma_set_state(struct dma_engine *dma, enum dma_state state)
  1782. {
  1783. bool cmd;
  1784. snd_printdd("dma_set_state state=%d\n", state);
  1785. switch (state) {
  1786. case DMA_STATE_STOP:
  1787. cmd = false;
  1788. break;
  1789. case DMA_STATE_RUN:
  1790. cmd = true;
  1791. break;
  1792. default:
  1793. return 0;
  1794. }
  1795. snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
  1796. return 0;
  1797. }
  1798. static unsigned int dma_get_buffer_size(struct dma_engine *dma)
  1799. {
  1800. return dma->dmab->bytes;
  1801. }
  1802. static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
  1803. {
  1804. return dma->dmab->area;
  1805. }
  1806. static int dma_xfer(struct dma_engine *dma,
  1807. const unsigned int *data,
  1808. unsigned int count)
  1809. {
  1810. memcpy(dma->dmab->area, data, count);
  1811. return 0;
  1812. }
  1813. static void dma_get_converter_format(
  1814. struct dma_engine *dma,
  1815. unsigned short *format)
  1816. {
  1817. if (format)
  1818. *format = dma->m_converter_format;
  1819. }
  1820. static unsigned int dma_get_stream_id(struct dma_engine *dma)
  1821. {
  1822. struct ca0132_spec *spec = dma->codec->spec;
  1823. return spec->dsp_stream_id;
  1824. }
  1825. struct dsp_image_seg {
  1826. u32 magic;
  1827. u32 chip_addr;
  1828. u32 count;
  1829. u32 data[0];
  1830. };
  1831. static const u32 g_magic_value = 0x4c46584d;
  1832. static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
  1833. static bool is_valid(const struct dsp_image_seg *p)
  1834. {
  1835. return p->magic == g_magic_value;
  1836. }
  1837. static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
  1838. {
  1839. return g_chip_addr_magic_value == p->chip_addr;
  1840. }
  1841. static bool is_last(const struct dsp_image_seg *p)
  1842. {
  1843. return p->count == 0;
  1844. }
  1845. static size_t dsp_sizeof(const struct dsp_image_seg *p)
  1846. {
  1847. return sizeof(*p) + p->count*sizeof(u32);
  1848. }
  1849. static const struct dsp_image_seg *get_next_seg_ptr(
  1850. const struct dsp_image_seg *p)
  1851. {
  1852. return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
  1853. }
  1854. /*
  1855. * CA0132 chip DSP transfer stuffs. For DSP download.
  1856. */
  1857. #define INVALID_DMA_CHANNEL (~0U)
  1858. /*
  1859. * Program a list of address/data pairs via the ChipIO widget.
  1860. * The segment data is in the format of successive pairs of words.
  1861. * These are repeated as indicated by the segment's count field.
  1862. */
  1863. static int dspxfr_hci_write(struct hda_codec *codec,
  1864. const struct dsp_image_seg *fls)
  1865. {
  1866. int status;
  1867. const u32 *data;
  1868. unsigned int count;
  1869. if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
  1870. snd_printdd(KERN_ERR "hci_write invalid params\n");
  1871. return -EINVAL;
  1872. }
  1873. count = fls->count;
  1874. data = (u32 *)(fls->data);
  1875. while (count >= 2) {
  1876. status = chipio_write(codec, data[0], data[1]);
  1877. if (status < 0) {
  1878. snd_printdd(KERN_ERR "hci_write chipio failed\n");
  1879. return status;
  1880. }
  1881. count -= 2;
  1882. data += 2;
  1883. }
  1884. return 0;
  1885. }
  1886. /**
  1887. * Write a block of data into DSP code or data RAM using pre-allocated
  1888. * DMA engine.
  1889. *
  1890. * @codec: the HDA codec
  1891. * @fls: pointer to a fast load image
  1892. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  1893. * no relocation
  1894. * @dma_engine: pointer to DMA engine to be used for DSP download
  1895. * @dma_chan: The number of DMA channels used for DSP download
  1896. * @port_map_mask: port mapping
  1897. * @ovly: TRUE if overlay format is required
  1898. *
  1899. * Returns zero or a negative error code.
  1900. */
  1901. static int dspxfr_one_seg(struct hda_codec *codec,
  1902. const struct dsp_image_seg *fls,
  1903. unsigned int reloc,
  1904. struct dma_engine *dma_engine,
  1905. unsigned int dma_chan,
  1906. unsigned int port_map_mask,
  1907. bool ovly)
  1908. {
  1909. int status = 0;
  1910. bool comm_dma_setup_done = false;
  1911. const unsigned int *data;
  1912. unsigned int chip_addx;
  1913. unsigned int words_to_write;
  1914. unsigned int buffer_size_words;
  1915. unsigned char *buffer_addx;
  1916. unsigned short hda_format;
  1917. unsigned int sample_rate_div;
  1918. unsigned int sample_rate_mul;
  1919. unsigned int num_chans;
  1920. unsigned int hda_frame_size_words;
  1921. unsigned int remainder_words;
  1922. const u32 *data_remainder;
  1923. u32 chip_addx_remainder;
  1924. unsigned int run_size_words;
  1925. const struct dsp_image_seg *hci_write = NULL;
  1926. unsigned long timeout;
  1927. bool dma_active;
  1928. if (fls == NULL)
  1929. return -EINVAL;
  1930. if (is_hci_prog_list_seg(fls)) {
  1931. hci_write = fls;
  1932. fls = get_next_seg_ptr(fls);
  1933. }
  1934. if (hci_write && (!fls || is_last(fls))) {
  1935. snd_printdd("hci_write\n");
  1936. return dspxfr_hci_write(codec, hci_write);
  1937. }
  1938. if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
  1939. snd_printdd("Invalid Params\n");
  1940. return -EINVAL;
  1941. }
  1942. data = fls->data;
  1943. chip_addx = fls->chip_addr,
  1944. words_to_write = fls->count;
  1945. if (!words_to_write)
  1946. return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
  1947. if (reloc)
  1948. chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
  1949. if (!UC_RANGE(chip_addx, words_to_write) &&
  1950. !X_RANGE_ALL(chip_addx, words_to_write) &&
  1951. !Y_RANGE_ALL(chip_addx, words_to_write)) {
  1952. snd_printdd("Invalid chip_addx Params\n");
  1953. return -EINVAL;
  1954. }
  1955. buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
  1956. sizeof(u32);
  1957. buffer_addx = dma_get_buffer_addr(dma_engine);
  1958. if (buffer_addx == NULL) {
  1959. snd_printdd(KERN_ERR "dma_engine buffer NULL\n");
  1960. return -EINVAL;
  1961. }
  1962. dma_get_converter_format(dma_engine, &hda_format);
  1963. sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
  1964. sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
  1965. num_chans = get_hdafmt_chs(hda_format) + 1;
  1966. hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
  1967. (num_chans * sample_rate_mul / sample_rate_div));
  1968. buffer_size_words = min(buffer_size_words,
  1969. (unsigned int)(UC_RANGE(chip_addx, 1) ?
  1970. 65536 : 32768));
  1971. buffer_size_words -= buffer_size_words % hda_frame_size_words;
  1972. snd_printdd(
  1973. "chpadr=0x%08x frmsz=%u nchan=%u "
  1974. "rate_mul=%u div=%u bufsz=%u\n",
  1975. chip_addx, hda_frame_size_words, num_chans,
  1976. sample_rate_mul, sample_rate_div, buffer_size_words);
  1977. if ((buffer_addx == NULL) || (hda_frame_size_words == 0) ||
  1978. (buffer_size_words < hda_frame_size_words)) {
  1979. snd_printdd(KERN_ERR "dspxfr_one_seg:failed\n");
  1980. return -EINVAL;
  1981. }
  1982. remainder_words = words_to_write % hda_frame_size_words;
  1983. data_remainder = data;
  1984. chip_addx_remainder = chip_addx;
  1985. data += remainder_words;
  1986. chip_addx += remainder_words*sizeof(u32);
  1987. words_to_write -= remainder_words;
  1988. while (words_to_write != 0) {
  1989. run_size_words = min(buffer_size_words, words_to_write);
  1990. snd_printdd("dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
  1991. words_to_write, run_size_words, remainder_words);
  1992. dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
  1993. if (!comm_dma_setup_done) {
  1994. status = dsp_dma_stop(codec, dma_chan, ovly);
  1995. if (status < 0)
  1996. return status;
  1997. status = dsp_dma_setup_common(codec, chip_addx,
  1998. dma_chan, port_map_mask, ovly);
  1999. if (status < 0)
  2000. return status;
  2001. comm_dma_setup_done = true;
  2002. }
  2003. status = dsp_dma_setup(codec, chip_addx,
  2004. run_size_words, dma_chan);
  2005. if (status < 0)
  2006. return status;
  2007. status = dsp_dma_start(codec, dma_chan, ovly);
  2008. if (status < 0)
  2009. return status;
  2010. if (!dsp_is_dma_active(codec, dma_chan)) {
  2011. snd_printdd(KERN_ERR "dspxfr:DMA did not start\n");
  2012. return -EIO;
  2013. }
  2014. status = dma_set_state(dma_engine, DMA_STATE_RUN);
  2015. if (status < 0)
  2016. return status;
  2017. if (remainder_words != 0) {
  2018. status = chipio_write_multiple(codec,
  2019. chip_addx_remainder,
  2020. data_remainder,
  2021. remainder_words);
  2022. if (status < 0)
  2023. return status;
  2024. remainder_words = 0;
  2025. }
  2026. if (hci_write) {
  2027. status = dspxfr_hci_write(codec, hci_write);
  2028. if (status < 0)
  2029. return status;
  2030. hci_write = NULL;
  2031. }
  2032. timeout = jiffies + msecs_to_jiffies(2000);
  2033. do {
  2034. dma_active = dsp_is_dma_active(codec, dma_chan);
  2035. if (!dma_active)
  2036. break;
  2037. msleep(20);
  2038. } while (time_before(jiffies, timeout));
  2039. if (dma_active)
  2040. break;
  2041. snd_printdd(KERN_INFO "+++++ DMA complete\n");
  2042. dma_set_state(dma_engine, DMA_STATE_STOP);
  2043. status = dma_reset(dma_engine);
  2044. if (status < 0)
  2045. return status;
  2046. data += run_size_words;
  2047. chip_addx += run_size_words*sizeof(u32);
  2048. words_to_write -= run_size_words;
  2049. }
  2050. if (remainder_words != 0) {
  2051. status = chipio_write_multiple(codec, chip_addx_remainder,
  2052. data_remainder, remainder_words);
  2053. }
  2054. return status;
  2055. }
  2056. /**
  2057. * Write the entire DSP image of a DSP code/data overlay to DSP memories
  2058. *
  2059. * @codec: the HDA codec
  2060. * @fls_data: pointer to a fast load image
  2061. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  2062. * no relocation
  2063. * @sample_rate: sampling rate of the stream used for DSP download
  2064. * @number_channels: channels of the stream used for DSP download
  2065. * @ovly: TRUE if overlay format is required
  2066. *
  2067. * Returns zero or a negative error code.
  2068. */
  2069. static int dspxfr_image(struct hda_codec *codec,
  2070. const struct dsp_image_seg *fls_data,
  2071. unsigned int reloc,
  2072. unsigned int sample_rate,
  2073. unsigned short channels,
  2074. bool ovly)
  2075. {
  2076. struct ca0132_spec *spec = codec->spec;
  2077. int status;
  2078. unsigned short hda_format = 0;
  2079. unsigned int response;
  2080. unsigned char stream_id = 0;
  2081. struct dma_engine *dma_engine;
  2082. unsigned int dma_chan;
  2083. unsigned int port_map_mask;
  2084. if (fls_data == NULL)
  2085. return -EINVAL;
  2086. dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
  2087. if (!dma_engine)
  2088. return -ENOMEM;
  2089. dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
  2090. if (!dma_engine->dmab) {
  2091. kfree(dma_engine);
  2092. return -ENOMEM;
  2093. }
  2094. dma_engine->codec = codec;
  2095. dma_convert_to_hda_format(sample_rate, channels, &hda_format);
  2096. dma_engine->m_converter_format = hda_format;
  2097. dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
  2098. DSP_DMA_WRITE_BUFLEN_INIT) * 2;
  2099. dma_chan = ovly ? INVALID_DMA_CHANNEL : 0;
  2100. status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
  2101. hda_format, &response);
  2102. if (status < 0) {
  2103. snd_printdd(KERN_ERR "set converter format fail\n");
  2104. goto exit;
  2105. }
  2106. status = snd_hda_codec_load_dsp_prepare(codec,
  2107. dma_engine->m_converter_format,
  2108. dma_engine->buf_size,
  2109. dma_engine->dmab);
  2110. if (status < 0)
  2111. goto exit;
  2112. spec->dsp_stream_id = status;
  2113. if (ovly) {
  2114. status = dspio_alloc_dma_chan(codec, &dma_chan);
  2115. if (status < 0) {
  2116. snd_printdd(KERN_ERR "alloc dmachan fail\n");
  2117. dma_chan = INVALID_DMA_CHANNEL;
  2118. goto exit;
  2119. }
  2120. }
  2121. port_map_mask = 0;
  2122. status = dsp_allocate_ports_format(codec, hda_format,
  2123. &port_map_mask);
  2124. if (status < 0) {
  2125. snd_printdd(KERN_ERR "alloc ports fail\n");
  2126. goto exit;
  2127. }
  2128. stream_id = dma_get_stream_id(dma_engine);
  2129. status = codec_set_converter_stream_channel(codec,
  2130. WIDGET_CHIP_CTRL, stream_id, 0, &response);
  2131. if (status < 0) {
  2132. snd_printdd(KERN_ERR "set stream chan fail\n");
  2133. goto exit;
  2134. }
  2135. while ((fls_data != NULL) && !is_last(fls_data)) {
  2136. if (!is_valid(fls_data)) {
  2137. snd_printdd(KERN_ERR "FLS check fail\n");
  2138. status = -EINVAL;
  2139. goto exit;
  2140. }
  2141. status = dspxfr_one_seg(codec, fls_data, reloc,
  2142. dma_engine, dma_chan,
  2143. port_map_mask, ovly);
  2144. if (status < 0)
  2145. break;
  2146. if (is_hci_prog_list_seg(fls_data))
  2147. fls_data = get_next_seg_ptr(fls_data);
  2148. if ((fls_data != NULL) && !is_last(fls_data))
  2149. fls_data = get_next_seg_ptr(fls_data);
  2150. }
  2151. if (port_map_mask != 0)
  2152. status = dsp_free_ports(codec);
  2153. if (status < 0)
  2154. goto exit;
  2155. status = codec_set_converter_stream_channel(codec,
  2156. WIDGET_CHIP_CTRL, 0, 0, &response);
  2157. exit:
  2158. if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
  2159. dspio_free_dma_chan(codec, dma_chan);
  2160. if (dma_engine->dmab->area)
  2161. snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
  2162. kfree(dma_engine->dmab);
  2163. kfree(dma_engine);
  2164. return status;
  2165. }
  2166. /*
  2167. * CA0132 DSP download stuffs.
  2168. */
  2169. static void dspload_post_setup(struct hda_codec *codec)
  2170. {
  2171. snd_printdd(KERN_INFO "---- dspload_post_setup ------\n");
  2172. /*set DSP speaker to 2.0 configuration*/
  2173. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
  2174. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
  2175. /*update write pointer*/
  2176. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
  2177. }
  2178. /**
  2179. * Download DSP from a DSP Image Fast Load structure. This structure is a
  2180. * linear, non-constant sized element array of structures, each of which
  2181. * contain the count of the data to be loaded, the data itself, and the
  2182. * corresponding starting chip address of the starting data location.
  2183. *
  2184. * @codec: the HDA codec
  2185. * @fls: pointer to a fast load image
  2186. * @ovly: TRUE if overlay format is required
  2187. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  2188. * no relocation
  2189. * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
  2190. * @router_chans: number of audio router channels to be allocated (0 means use
  2191. * internal defaults; max is 32)
  2192. *
  2193. * Returns zero or a negative error code.
  2194. */
  2195. static int dspload_image(struct hda_codec *codec,
  2196. const struct dsp_image_seg *fls,
  2197. bool ovly,
  2198. unsigned int reloc,
  2199. bool autostart,
  2200. int router_chans)
  2201. {
  2202. int status = 0;
  2203. unsigned int sample_rate;
  2204. unsigned short channels;
  2205. snd_printdd(KERN_INFO "---- dspload_image begin ------\n");
  2206. if (router_chans == 0) {
  2207. if (!ovly)
  2208. router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
  2209. else
  2210. router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
  2211. }
  2212. sample_rate = 48000;
  2213. channels = (unsigned short)router_chans;
  2214. while (channels > 16) {
  2215. sample_rate *= 2;
  2216. channels /= 2;
  2217. }
  2218. do {
  2219. snd_printdd(KERN_INFO "Ready to program DMA\n");
  2220. if (!ovly)
  2221. status = dsp_reset(codec);
  2222. if (status < 0)
  2223. break;
  2224. snd_printdd(KERN_INFO "dsp_reset() complete\n");
  2225. status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
  2226. ovly);
  2227. if (status < 0)
  2228. break;
  2229. snd_printdd(KERN_INFO "dspxfr_image() complete\n");
  2230. if (autostart && !ovly) {
  2231. dspload_post_setup(codec);
  2232. status = dsp_set_run_state(codec);
  2233. }
  2234. snd_printdd(KERN_INFO "LOAD FINISHED\n");
  2235. } while (0);
  2236. return status;
  2237. }
  2238. #ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
  2239. static bool dspload_is_loaded(struct hda_codec *codec)
  2240. {
  2241. unsigned int data = 0;
  2242. int status = 0;
  2243. status = chipio_read(codec, 0x40004, &data);
  2244. if ((status < 0) || (data != 1))
  2245. return false;
  2246. return true;
  2247. }
  2248. #else
  2249. #define dspload_is_loaded(codec) false
  2250. #endif
  2251. static bool dspload_wait_loaded(struct hda_codec *codec)
  2252. {
  2253. unsigned long timeout = jiffies + msecs_to_jiffies(2000);
  2254. do {
  2255. if (dspload_is_loaded(codec)) {
  2256. pr_info("ca0132 DOWNLOAD OK :-) DSP IS RUNNING.\n");
  2257. return true;
  2258. }
  2259. msleep(20);
  2260. } while (time_before(jiffies, timeout));
  2261. pr_err("ca0132 DOWNLOAD FAILED!!! DSP IS NOT RUNNING.\n");
  2262. return false;
  2263. }
  2264. /*
  2265. * PCM stuffs
  2266. */
  2267. static void ca0132_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  2268. u32 stream_tag,
  2269. int channel_id, int format)
  2270. {
  2271. unsigned int oldval, newval;
  2272. if (!nid)
  2273. return;
  2274. snd_printdd(
  2275. "ca0132_setup_stream: NID=0x%x, stream=0x%x, "
  2276. "channel=%d, format=0x%x\n",
  2277. nid, stream_tag, channel_id, format);
  2278. /* update the format-id if changed */
  2279. oldval = snd_hda_codec_read(codec, nid, 0,
  2280. AC_VERB_GET_STREAM_FORMAT,
  2281. 0);
  2282. if (oldval != format) {
  2283. msleep(20);
  2284. snd_hda_codec_write(codec, nid, 0,
  2285. AC_VERB_SET_STREAM_FORMAT,
  2286. format);
  2287. }
  2288. oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
  2289. newval = (stream_tag << 4) | channel_id;
  2290. if (oldval != newval) {
  2291. snd_hda_codec_write(codec, nid, 0,
  2292. AC_VERB_SET_CHANNEL_STREAMID,
  2293. newval);
  2294. }
  2295. }
  2296. static void ca0132_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
  2297. {
  2298. unsigned int val;
  2299. if (!nid)
  2300. return;
  2301. snd_printdd(KERN_INFO "ca0132_cleanup_stream: NID=0x%x\n", nid);
  2302. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
  2303. if (!val)
  2304. return;
  2305. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
  2306. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  2307. }
  2308. /*
  2309. * PCM callbacks
  2310. */
  2311. static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  2312. struct hda_codec *codec,
  2313. unsigned int stream_tag,
  2314. unsigned int format,
  2315. struct snd_pcm_substream *substream)
  2316. {
  2317. struct ca0132_spec *spec = codec->spec;
  2318. ca0132_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
  2319. return 0;
  2320. }
  2321. static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2322. struct hda_codec *codec,
  2323. struct snd_pcm_substream *substream)
  2324. {
  2325. struct ca0132_spec *spec = codec->spec;
  2326. if (spec->dsp_state == DSP_DOWNLOADING)
  2327. return 0;
  2328. /*If Playback effects are on, allow stream some time to flush
  2329. *effects tail*/
  2330. if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
  2331. msleep(50);
  2332. ca0132_cleanup_stream(codec, spec->dacs[0]);
  2333. return 0;
  2334. }
  2335. /*
  2336. * Digital out
  2337. */
  2338. static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  2339. struct hda_codec *codec,
  2340. struct snd_pcm_substream *substream)
  2341. {
  2342. struct ca0132_spec *spec = codec->spec;
  2343. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  2344. }
  2345. static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  2346. struct hda_codec *codec,
  2347. unsigned int stream_tag,
  2348. unsigned int format,
  2349. struct snd_pcm_substream *substream)
  2350. {
  2351. struct ca0132_spec *spec = codec->spec;
  2352. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  2353. stream_tag, format, substream);
  2354. }
  2355. static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2356. struct hda_codec *codec,
  2357. struct snd_pcm_substream *substream)
  2358. {
  2359. struct ca0132_spec *spec = codec->spec;
  2360. return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
  2361. }
  2362. static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  2363. struct hda_codec *codec,
  2364. struct snd_pcm_substream *substream)
  2365. {
  2366. struct ca0132_spec *spec = codec->spec;
  2367. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  2368. }
  2369. /*
  2370. * Analog capture
  2371. */
  2372. static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  2373. struct hda_codec *codec,
  2374. unsigned int stream_tag,
  2375. unsigned int format,
  2376. struct snd_pcm_substream *substream)
  2377. {
  2378. struct ca0132_spec *spec = codec->spec;
  2379. ca0132_setup_stream(codec, spec->adcs[substream->number],
  2380. stream_tag, 0, format);
  2381. return 0;
  2382. }
  2383. static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2384. struct hda_codec *codec,
  2385. struct snd_pcm_substream *substream)
  2386. {
  2387. struct ca0132_spec *spec = codec->spec;
  2388. if (spec->dsp_state == DSP_DOWNLOADING)
  2389. return 0;
  2390. ca0132_cleanup_stream(codec, hinfo->nid);
  2391. return 0;
  2392. }
  2393. /*
  2394. * Controls stuffs.
  2395. */
  2396. /*
  2397. * Mixer controls helpers.
  2398. */
  2399. #define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
  2400. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2401. .name = xname, \
  2402. .subdevice = HDA_SUBDEV_AMP_FLAG, \
  2403. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
  2404. SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
  2405. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
  2406. .info = ca0132_volume_info, \
  2407. .get = ca0132_volume_get, \
  2408. .put = ca0132_volume_put, \
  2409. .tlv = { .c = ca0132_volume_tlv }, \
  2410. .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
  2411. #define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
  2412. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2413. .name = xname, \
  2414. .subdevice = HDA_SUBDEV_AMP_FLAG, \
  2415. .info = snd_hda_mixer_amp_switch_info, \
  2416. .get = ca0132_switch_get, \
  2417. .put = ca0132_switch_put, \
  2418. .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
  2419. /* stereo */
  2420. #define CA0132_CODEC_VOL(xname, nid, dir) \
  2421. CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
  2422. #define CA0132_CODEC_MUTE(xname, nid, dir) \
  2423. CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
  2424. /* The followings are for tuning of products */
  2425. #ifdef ENABLE_TUNING_CONTROLS
  2426. static unsigned int voice_focus_vals_lookup[] = {
  2427. 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000,
  2428. 0x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000,
  2429. 0x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000,
  2430. 0x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000,
  2431. 0x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000,
  2432. 0x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000,
  2433. 0x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000,
  2434. 0x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000,
  2435. 0x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000,
  2436. 0x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000,
  2437. 0x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000,
  2438. 0x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000,
  2439. 0x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000,
  2440. 0x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000,
  2441. 0x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000,
  2442. 0x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000,
  2443. 0x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000,
  2444. 0x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000,
  2445. 0x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000,
  2446. 0x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000,
  2447. 0x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000,
  2448. 0x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000,
  2449. 0x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000,
  2450. 0x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000,
  2451. 0x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000,
  2452. 0x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000,
  2453. 0x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000
  2454. };
  2455. static unsigned int mic_svm_vals_lookup[] = {
  2456. 0x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
  2457. 0x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
  2458. 0x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
  2459. 0x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
  2460. 0x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
  2461. 0x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
  2462. 0x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
  2463. 0x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
  2464. 0x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
  2465. 0x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
  2466. 0x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
  2467. 0x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
  2468. 0x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
  2469. 0x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
  2470. 0x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
  2471. 0x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
  2472. 0x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
  2473. };
  2474. static unsigned int equalizer_vals_lookup[] = {
  2475. 0xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
  2476. 0xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
  2477. 0xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
  2478. 0xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
  2479. 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
  2480. 0x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
  2481. 0x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
  2482. 0x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
  2483. 0x41C00000
  2484. };
  2485. static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid,
  2486. unsigned int *lookup, int idx)
  2487. {
  2488. int i = 0;
  2489. for (i = 0; i < TUNING_CTLS_COUNT; i++)
  2490. if (nid == ca0132_tuning_ctls[i].nid)
  2491. break;
  2492. snd_hda_power_up(codec);
  2493. dspio_set_param(codec, ca0132_tuning_ctls[i].mid,
  2494. ca0132_tuning_ctls[i].req,
  2495. &(lookup[idx]), sizeof(unsigned int));
  2496. snd_hda_power_down(codec);
  2497. return 1;
  2498. }
  2499. static int tuning_ctl_get(struct snd_kcontrol *kcontrol,
  2500. struct snd_ctl_elem_value *ucontrol)
  2501. {
  2502. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2503. struct ca0132_spec *spec = codec->spec;
  2504. hda_nid_t nid = get_amp_nid(kcontrol);
  2505. long *valp = ucontrol->value.integer.value;
  2506. int idx = nid - TUNING_CTL_START_NID;
  2507. *valp = spec->cur_ctl_vals[idx];
  2508. return 0;
  2509. }
  2510. static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol,
  2511. struct snd_ctl_elem_info *uinfo)
  2512. {
  2513. int chs = get_amp_channels(kcontrol);
  2514. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2515. uinfo->count = chs == 3 ? 2 : 1;
  2516. uinfo->value.integer.min = 20;
  2517. uinfo->value.integer.max = 180;
  2518. uinfo->value.integer.step = 1;
  2519. return 0;
  2520. }
  2521. static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol,
  2522. struct snd_ctl_elem_value *ucontrol)
  2523. {
  2524. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2525. struct ca0132_spec *spec = codec->spec;
  2526. hda_nid_t nid = get_amp_nid(kcontrol);
  2527. long *valp = ucontrol->value.integer.value;
  2528. int idx;
  2529. idx = nid - TUNING_CTL_START_NID;
  2530. /* any change? */
  2531. if (spec->cur_ctl_vals[idx] == *valp)
  2532. return 0;
  2533. spec->cur_ctl_vals[idx] = *valp;
  2534. idx = *valp - 20;
  2535. tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx);
  2536. return 1;
  2537. }
  2538. static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol,
  2539. struct snd_ctl_elem_info *uinfo)
  2540. {
  2541. int chs = get_amp_channels(kcontrol);
  2542. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2543. uinfo->count = chs == 3 ? 2 : 1;
  2544. uinfo->value.integer.min = 0;
  2545. uinfo->value.integer.max = 100;
  2546. uinfo->value.integer.step = 1;
  2547. return 0;
  2548. }
  2549. static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol,
  2550. struct snd_ctl_elem_value *ucontrol)
  2551. {
  2552. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2553. struct ca0132_spec *spec = codec->spec;
  2554. hda_nid_t nid = get_amp_nid(kcontrol);
  2555. long *valp = ucontrol->value.integer.value;
  2556. int idx;
  2557. idx = nid - TUNING_CTL_START_NID;
  2558. /* any change? */
  2559. if (spec->cur_ctl_vals[idx] == *valp)
  2560. return 0;
  2561. spec->cur_ctl_vals[idx] = *valp;
  2562. idx = *valp;
  2563. tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx);
  2564. return 0;
  2565. }
  2566. static int equalizer_ctl_info(struct snd_kcontrol *kcontrol,
  2567. struct snd_ctl_elem_info *uinfo)
  2568. {
  2569. int chs = get_amp_channels(kcontrol);
  2570. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2571. uinfo->count = chs == 3 ? 2 : 1;
  2572. uinfo->value.integer.min = 0;
  2573. uinfo->value.integer.max = 48;
  2574. uinfo->value.integer.step = 1;
  2575. return 0;
  2576. }
  2577. static int equalizer_ctl_put(struct snd_kcontrol *kcontrol,
  2578. struct snd_ctl_elem_value *ucontrol)
  2579. {
  2580. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2581. struct ca0132_spec *spec = codec->spec;
  2582. hda_nid_t nid = get_amp_nid(kcontrol);
  2583. long *valp = ucontrol->value.integer.value;
  2584. int idx;
  2585. idx = nid - TUNING_CTL_START_NID;
  2586. /* any change? */
  2587. if (spec->cur_ctl_vals[idx] == *valp)
  2588. return 0;
  2589. spec->cur_ctl_vals[idx] = *valp;
  2590. idx = *valp;
  2591. tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx);
  2592. return 1;
  2593. }
  2594. static const DECLARE_TLV_DB_SCALE(voice_focus_db_scale, 2000, 100, 0);
  2595. static const DECLARE_TLV_DB_SCALE(eq_db_scale, -2400, 100, 0);
  2596. static int add_tuning_control(struct hda_codec *codec,
  2597. hda_nid_t pnid, hda_nid_t nid,
  2598. const char *name, int dir)
  2599. {
  2600. char namestr[44];
  2601. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  2602. struct snd_kcontrol_new knew =
  2603. HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
  2604. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  2605. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  2606. knew.tlv.c = 0;
  2607. knew.tlv.p = 0;
  2608. switch (pnid) {
  2609. case VOICE_FOCUS:
  2610. knew.info = voice_focus_ctl_info;
  2611. knew.get = tuning_ctl_get;
  2612. knew.put = voice_focus_ctl_put;
  2613. knew.tlv.p = voice_focus_db_scale;
  2614. break;
  2615. case MIC_SVM:
  2616. knew.info = mic_svm_ctl_info;
  2617. knew.get = tuning_ctl_get;
  2618. knew.put = mic_svm_ctl_put;
  2619. break;
  2620. case EQUALIZER:
  2621. knew.info = equalizer_ctl_info;
  2622. knew.get = tuning_ctl_get;
  2623. knew.put = equalizer_ctl_put;
  2624. knew.tlv.p = eq_db_scale;
  2625. break;
  2626. default:
  2627. return 0;
  2628. }
  2629. knew.private_value =
  2630. HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
  2631. sprintf(namestr, "%s %s Volume", name, dirstr[dir]);
  2632. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  2633. }
  2634. static int add_tuning_ctls(struct hda_codec *codec)
  2635. {
  2636. int i;
  2637. int err;
  2638. for (i = 0; i < TUNING_CTLS_COUNT; i++) {
  2639. err = add_tuning_control(codec,
  2640. ca0132_tuning_ctls[i].parent_nid,
  2641. ca0132_tuning_ctls[i].nid,
  2642. ca0132_tuning_ctls[i].name,
  2643. ca0132_tuning_ctls[i].direct);
  2644. if (err < 0)
  2645. return err;
  2646. }
  2647. return 0;
  2648. }
  2649. static void ca0132_init_tuning_defaults(struct hda_codec *codec)
  2650. {
  2651. struct ca0132_spec *spec = codec->spec;
  2652. int i;
  2653. /* Wedge Angle defaults to 30. 10 below is 30 - 20. 20 is min. */
  2654. spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10;
  2655. /* SVM level defaults to 0.74. */
  2656. spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74;
  2657. /* EQ defaults to 0dB. */
  2658. for (i = 2; i < TUNING_CTLS_COUNT; i++)
  2659. spec->cur_ctl_vals[i] = 24;
  2660. }
  2661. #endif /*ENABLE_TUNING_CONTROLS*/
  2662. /*
  2663. * Select the active output.
  2664. * If autodetect is enabled, output will be selected based on jack detection.
  2665. * If jack inserted, headphone will be selected, else built-in speakers
  2666. * If autodetect is disabled, output will be selected based on selection.
  2667. */
  2668. static int ca0132_select_out(struct hda_codec *codec)
  2669. {
  2670. struct ca0132_spec *spec = codec->spec;
  2671. unsigned int pin_ctl;
  2672. int jack_present;
  2673. int auto_jack;
  2674. unsigned int tmp;
  2675. int err;
  2676. snd_printdd(KERN_INFO "ca0132_select_out\n");
  2677. snd_hda_power_up(codec);
  2678. auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  2679. if (auto_jack)
  2680. jack_present = snd_hda_jack_detect(codec, spec->out_pins[1]);
  2681. else
  2682. jack_present =
  2683. spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
  2684. if (jack_present)
  2685. spec->cur_out_type = HEADPHONE_OUT;
  2686. else
  2687. spec->cur_out_type = SPEAKER_OUT;
  2688. if (spec->cur_out_type == SPEAKER_OUT) {
  2689. snd_printdd(KERN_INFO "ca0132_select_out speaker\n");
  2690. /*speaker out config*/
  2691. tmp = FLOAT_ONE;
  2692. err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
  2693. if (err < 0)
  2694. goto exit;
  2695. /*enable speaker EQ*/
  2696. tmp = FLOAT_ONE;
  2697. err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
  2698. if (err < 0)
  2699. goto exit;
  2700. /* Setup EAPD */
  2701. snd_hda_codec_write(codec, spec->out_pins[1], 0,
  2702. VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
  2703. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2704. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  2705. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2706. VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
  2707. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2708. AC_VERB_SET_EAPD_BTLENABLE, 0x02);
  2709. /* disable headphone node */
  2710. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
  2711. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2712. snd_hda_set_pin_ctl(codec, spec->out_pins[1],
  2713. pin_ctl & ~PIN_HP);
  2714. /* enable speaker node */
  2715. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  2716. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2717. snd_hda_set_pin_ctl(codec, spec->out_pins[0],
  2718. pin_ctl | PIN_OUT);
  2719. } else {
  2720. snd_printdd(KERN_INFO "ca0132_select_out hp\n");
  2721. /*headphone out config*/
  2722. tmp = FLOAT_ZERO;
  2723. err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
  2724. if (err < 0)
  2725. goto exit;
  2726. /*disable speaker EQ*/
  2727. tmp = FLOAT_ZERO;
  2728. err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
  2729. if (err < 0)
  2730. goto exit;
  2731. /* Setup EAPD */
  2732. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2733. VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
  2734. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2735. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  2736. snd_hda_codec_write(codec, spec->out_pins[1], 0,
  2737. VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
  2738. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2739. AC_VERB_SET_EAPD_BTLENABLE, 0x02);
  2740. /* disable speaker*/
  2741. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  2742. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2743. snd_hda_set_pin_ctl(codec, spec->out_pins[0],
  2744. pin_ctl & ~PIN_HP);
  2745. /* enable headphone*/
  2746. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
  2747. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2748. snd_hda_set_pin_ctl(codec, spec->out_pins[1],
  2749. pin_ctl | PIN_HP);
  2750. }
  2751. exit:
  2752. snd_hda_power_down(codec);
  2753. return err < 0 ? err : 0;
  2754. }
  2755. static void ca0132_set_dmic(struct hda_codec *codec, int enable);
  2756. static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
  2757. static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
  2758. /*
  2759. * Select the active VIP source
  2760. */
  2761. static int ca0132_set_vipsource(struct hda_codec *codec, int val)
  2762. {
  2763. struct ca0132_spec *spec = codec->spec;
  2764. unsigned int tmp;
  2765. if (!dspload_is_loaded(codec))
  2766. return 0;
  2767. /* if CrystalVoice if off, vipsource should be 0 */
  2768. if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
  2769. (val == 0)) {
  2770. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
  2771. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  2772. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  2773. if (spec->cur_mic_type == DIGITAL_MIC)
  2774. tmp = FLOAT_TWO;
  2775. else
  2776. tmp = FLOAT_ONE;
  2777. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  2778. tmp = FLOAT_ZERO;
  2779. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  2780. } else {
  2781. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
  2782. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
  2783. if (spec->cur_mic_type == DIGITAL_MIC)
  2784. tmp = FLOAT_TWO;
  2785. else
  2786. tmp = FLOAT_ONE;
  2787. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  2788. tmp = FLOAT_ONE;
  2789. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  2790. msleep(20);
  2791. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
  2792. }
  2793. return 1;
  2794. }
  2795. /*
  2796. * Select the active microphone.
  2797. * If autodetect is enabled, mic will be selected based on jack detection.
  2798. * If jack inserted, ext.mic will be selected, else built-in mic
  2799. * If autodetect is disabled, mic will be selected based on selection.
  2800. */
  2801. static int ca0132_select_mic(struct hda_codec *codec)
  2802. {
  2803. struct ca0132_spec *spec = codec->spec;
  2804. int jack_present;
  2805. int auto_jack;
  2806. snd_printdd(KERN_INFO "ca0132_select_mic\n");
  2807. snd_hda_power_up(codec);
  2808. auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
  2809. if (auto_jack)
  2810. jack_present = snd_hda_jack_detect(codec, spec->input_pins[0]);
  2811. else
  2812. jack_present =
  2813. spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
  2814. if (jack_present)
  2815. spec->cur_mic_type = LINE_MIC_IN;
  2816. else
  2817. spec->cur_mic_type = DIGITAL_MIC;
  2818. if (spec->cur_mic_type == DIGITAL_MIC) {
  2819. /* enable digital Mic */
  2820. chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
  2821. ca0132_set_dmic(codec, 1);
  2822. ca0132_mic_boost_set(codec, 0);
  2823. /* set voice focus */
  2824. ca0132_effects_set(codec, VOICE_FOCUS,
  2825. spec->effects_switch
  2826. [VOICE_FOCUS - EFFECT_START_NID]);
  2827. } else {
  2828. /* disable digital Mic */
  2829. chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
  2830. ca0132_set_dmic(codec, 0);
  2831. ca0132_mic_boost_set(codec, spec->cur_mic_boost);
  2832. /* disable voice focus */
  2833. ca0132_effects_set(codec, VOICE_FOCUS, 0);
  2834. }
  2835. snd_hda_power_down(codec);
  2836. return 0;
  2837. }
  2838. /*
  2839. * Check if VNODE settings take effect immediately.
  2840. */
  2841. static bool ca0132_is_vnode_effective(struct hda_codec *codec,
  2842. hda_nid_t vnid,
  2843. hda_nid_t *shared_nid)
  2844. {
  2845. struct ca0132_spec *spec = codec->spec;
  2846. hda_nid_t nid;
  2847. switch (vnid) {
  2848. case VNID_SPK:
  2849. nid = spec->shared_out_nid;
  2850. break;
  2851. case VNID_MIC:
  2852. nid = spec->shared_mic_nid;
  2853. break;
  2854. default:
  2855. return false;
  2856. }
  2857. if (shared_nid)
  2858. *shared_nid = nid;
  2859. return true;
  2860. }
  2861. /*
  2862. * The following functions are control change helpers.
  2863. * They return 0 if no changed. Return 1 if changed.
  2864. */
  2865. static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
  2866. {
  2867. struct ca0132_spec *spec = codec->spec;
  2868. unsigned int tmp;
  2869. /* based on CrystalVoice state to enable VoiceFX. */
  2870. if (enable) {
  2871. tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
  2872. FLOAT_ONE : FLOAT_ZERO;
  2873. } else {
  2874. tmp = FLOAT_ZERO;
  2875. }
  2876. dspio_set_uint_param(codec, ca0132_voicefx.mid,
  2877. ca0132_voicefx.reqs[0], tmp);
  2878. return 1;
  2879. }
  2880. /*
  2881. * Set the effects parameters
  2882. */
  2883. static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
  2884. {
  2885. struct ca0132_spec *spec = codec->spec;
  2886. unsigned int on;
  2887. int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  2888. int err = 0;
  2889. int idx = nid - EFFECT_START_NID;
  2890. if ((idx < 0) || (idx >= num_fx))
  2891. return 0; /* no changed */
  2892. /* for out effect, qualify with PE */
  2893. if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
  2894. /* if PE if off, turn off out effects. */
  2895. if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
  2896. val = 0;
  2897. }
  2898. /* for in effect, qualify with CrystalVoice */
  2899. if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
  2900. /* if CrystalVoice if off, turn off in effects. */
  2901. if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
  2902. val = 0;
  2903. /* Voice Focus applies to 2-ch Mic, Digital Mic */
  2904. if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
  2905. val = 0;
  2906. }
  2907. snd_printdd(KERN_INFO "ca0132_effect_set: nid=0x%x, val=%ld\n",
  2908. nid, val);
  2909. on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
  2910. err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
  2911. ca0132_effects[idx].reqs[0], on);
  2912. if (err < 0)
  2913. return 0; /* no changed */
  2914. return 1;
  2915. }
  2916. /*
  2917. * Turn on/off Playback Enhancements
  2918. */
  2919. static int ca0132_pe_switch_set(struct hda_codec *codec)
  2920. {
  2921. struct ca0132_spec *spec = codec->spec;
  2922. hda_nid_t nid;
  2923. int i, ret = 0;
  2924. snd_printdd(KERN_INFO "ca0132_pe_switch_set: val=%ld\n",
  2925. spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
  2926. i = OUT_EFFECT_START_NID - EFFECT_START_NID;
  2927. nid = OUT_EFFECT_START_NID;
  2928. /* PE affects all out effects */
  2929. for (; nid < OUT_EFFECT_END_NID; nid++, i++)
  2930. ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
  2931. return ret;
  2932. }
  2933. /* Check if Mic1 is streaming, if so, stop streaming */
  2934. static int stop_mic1(struct hda_codec *codec)
  2935. {
  2936. struct ca0132_spec *spec = codec->spec;
  2937. unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
  2938. AC_VERB_GET_CONV, 0);
  2939. if (oldval != 0)
  2940. snd_hda_codec_write(codec, spec->adcs[0], 0,
  2941. AC_VERB_SET_CHANNEL_STREAMID,
  2942. 0);
  2943. return oldval;
  2944. }
  2945. /* Resume Mic1 streaming if it was stopped. */
  2946. static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
  2947. {
  2948. struct ca0132_spec *spec = codec->spec;
  2949. /* Restore the previous stream and channel */
  2950. if (oldval != 0)
  2951. snd_hda_codec_write(codec, spec->adcs[0], 0,
  2952. AC_VERB_SET_CHANNEL_STREAMID,
  2953. oldval);
  2954. }
  2955. /*
  2956. * Turn on/off CrystalVoice
  2957. */
  2958. static int ca0132_cvoice_switch_set(struct hda_codec *codec)
  2959. {
  2960. struct ca0132_spec *spec = codec->spec;
  2961. hda_nid_t nid;
  2962. int i, ret = 0;
  2963. unsigned int oldval;
  2964. snd_printdd(KERN_INFO "ca0132_cvoice_switch_set: val=%ld\n",
  2965. spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
  2966. i = IN_EFFECT_START_NID - EFFECT_START_NID;
  2967. nid = IN_EFFECT_START_NID;
  2968. /* CrystalVoice affects all in effects */
  2969. for (; nid < IN_EFFECT_END_NID; nid++, i++)
  2970. ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
  2971. /* including VoiceFX */
  2972. ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
  2973. /* set correct vipsource */
  2974. oldval = stop_mic1(codec);
  2975. ret |= ca0132_set_vipsource(codec, 1);
  2976. resume_mic1(codec, oldval);
  2977. return ret;
  2978. }
  2979. static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
  2980. {
  2981. struct ca0132_spec *spec = codec->spec;
  2982. int ret = 0;
  2983. if (val) /* on */
  2984. ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
  2985. HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
  2986. else /* off */
  2987. ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
  2988. HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
  2989. return ret;
  2990. }
  2991. static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
  2992. struct snd_ctl_elem_value *ucontrol)
  2993. {
  2994. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2995. hda_nid_t nid = get_amp_nid(kcontrol);
  2996. hda_nid_t shared_nid = 0;
  2997. bool effective;
  2998. int ret = 0;
  2999. struct ca0132_spec *spec = codec->spec;
  3000. int auto_jack;
  3001. if (nid == VNID_HP_SEL) {
  3002. auto_jack =
  3003. spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  3004. if (!auto_jack)
  3005. ca0132_select_out(codec);
  3006. return 1;
  3007. }
  3008. if (nid == VNID_AMIC1_SEL) {
  3009. auto_jack =
  3010. spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
  3011. if (!auto_jack)
  3012. ca0132_select_mic(codec);
  3013. return 1;
  3014. }
  3015. if (nid == VNID_HP_ASEL) {
  3016. ca0132_select_out(codec);
  3017. return 1;
  3018. }
  3019. if (nid == VNID_AMIC1_ASEL) {
  3020. ca0132_select_mic(codec);
  3021. return 1;
  3022. }
  3023. /* if effective conditions, then update hw immediately. */
  3024. effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
  3025. if (effective) {
  3026. int dir = get_amp_direction(kcontrol);
  3027. int ch = get_amp_channels(kcontrol);
  3028. unsigned long pval;
  3029. mutex_lock(&codec->control_mutex);
  3030. pval = kcontrol->private_value;
  3031. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
  3032. 0, dir);
  3033. ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  3034. kcontrol->private_value = pval;
  3035. mutex_unlock(&codec->control_mutex);
  3036. }
  3037. return ret;
  3038. }
  3039. /* End of control change helpers. */
  3040. static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
  3041. struct snd_ctl_elem_info *uinfo)
  3042. {
  3043. unsigned int items = sizeof(ca0132_voicefx_presets)
  3044. / sizeof(struct ct_voicefx_preset);
  3045. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  3046. uinfo->count = 1;
  3047. uinfo->value.enumerated.items = items;
  3048. if (uinfo->value.enumerated.item >= items)
  3049. uinfo->value.enumerated.item = items - 1;
  3050. strcpy(uinfo->value.enumerated.name,
  3051. ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
  3052. return 0;
  3053. }
  3054. static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
  3055. struct snd_ctl_elem_value *ucontrol)
  3056. {
  3057. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3058. struct ca0132_spec *spec = codec->spec;
  3059. ucontrol->value.enumerated.item[0] = spec->voicefx_val;
  3060. return 0;
  3061. }
  3062. static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
  3063. struct snd_ctl_elem_value *ucontrol)
  3064. {
  3065. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3066. struct ca0132_spec *spec = codec->spec;
  3067. int i, err = 0;
  3068. int sel = ucontrol->value.enumerated.item[0];
  3069. unsigned int items = sizeof(ca0132_voicefx_presets)
  3070. / sizeof(struct ct_voicefx_preset);
  3071. if (sel >= items)
  3072. return 0;
  3073. snd_printdd(KERN_INFO "ca0132_voicefx_put: sel=%d, preset=%s\n",
  3074. sel, ca0132_voicefx_presets[sel].name);
  3075. /*
  3076. * Idx 0 is default.
  3077. * Default needs to qualify with CrystalVoice state.
  3078. */
  3079. for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
  3080. err = dspio_set_uint_param(codec, ca0132_voicefx.mid,
  3081. ca0132_voicefx.reqs[i],
  3082. ca0132_voicefx_presets[sel].vals[i]);
  3083. if (err < 0)
  3084. break;
  3085. }
  3086. if (err >= 0) {
  3087. spec->voicefx_val = sel;
  3088. /* enable voice fx */
  3089. ca0132_voicefx_set(codec, (sel ? 1 : 0));
  3090. }
  3091. return 1;
  3092. }
  3093. static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
  3094. struct snd_ctl_elem_value *ucontrol)
  3095. {
  3096. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3097. struct ca0132_spec *spec = codec->spec;
  3098. hda_nid_t nid = get_amp_nid(kcontrol);
  3099. int ch = get_amp_channels(kcontrol);
  3100. long *valp = ucontrol->value.integer.value;
  3101. /* vnode */
  3102. if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
  3103. if (ch & 1) {
  3104. *valp = spec->vnode_lswitch[nid - VNODE_START_NID];
  3105. valp++;
  3106. }
  3107. if (ch & 2) {
  3108. *valp = spec->vnode_rswitch[nid - VNODE_START_NID];
  3109. valp++;
  3110. }
  3111. return 0;
  3112. }
  3113. /* effects, include PE and CrystalVoice */
  3114. if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
  3115. *valp = spec->effects_switch[nid - EFFECT_START_NID];
  3116. return 0;
  3117. }
  3118. /* mic boost */
  3119. if (nid == spec->input_pins[0]) {
  3120. *valp = spec->cur_mic_boost;
  3121. return 0;
  3122. }
  3123. return 0;
  3124. }
  3125. static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
  3126. struct snd_ctl_elem_value *ucontrol)
  3127. {
  3128. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3129. struct ca0132_spec *spec = codec->spec;
  3130. hda_nid_t nid = get_amp_nid(kcontrol);
  3131. int ch = get_amp_channels(kcontrol);
  3132. long *valp = ucontrol->value.integer.value;
  3133. int changed = 1;
  3134. snd_printdd(KERN_INFO "ca0132_switch_put: nid=0x%x, val=%ld\n",
  3135. nid, *valp);
  3136. snd_hda_power_up(codec);
  3137. /* vnode */
  3138. if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
  3139. if (ch & 1) {
  3140. spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
  3141. valp++;
  3142. }
  3143. if (ch & 2) {
  3144. spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
  3145. valp++;
  3146. }
  3147. changed = ca0132_vnode_switch_set(kcontrol, ucontrol);
  3148. goto exit;
  3149. }
  3150. /* PE */
  3151. if (nid == PLAY_ENHANCEMENT) {
  3152. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  3153. changed = ca0132_pe_switch_set(codec);
  3154. goto exit;
  3155. }
  3156. /* CrystalVoice */
  3157. if (nid == CRYSTAL_VOICE) {
  3158. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  3159. changed = ca0132_cvoice_switch_set(codec);
  3160. goto exit;
  3161. }
  3162. /* out and in effects */
  3163. if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
  3164. ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
  3165. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  3166. changed = ca0132_effects_set(codec, nid, *valp);
  3167. goto exit;
  3168. }
  3169. /* mic boost */
  3170. if (nid == spec->input_pins[0]) {
  3171. spec->cur_mic_boost = *valp;
  3172. /* Mic boost does not apply to Digital Mic */
  3173. if (spec->cur_mic_type != DIGITAL_MIC)
  3174. changed = ca0132_mic_boost_set(codec, *valp);
  3175. goto exit;
  3176. }
  3177. exit:
  3178. snd_hda_power_down(codec);
  3179. return changed;
  3180. }
  3181. /*
  3182. * Volume related
  3183. */
  3184. static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
  3185. struct snd_ctl_elem_info *uinfo)
  3186. {
  3187. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3188. struct ca0132_spec *spec = codec->spec;
  3189. hda_nid_t nid = get_amp_nid(kcontrol);
  3190. int ch = get_amp_channels(kcontrol);
  3191. int dir = get_amp_direction(kcontrol);
  3192. unsigned long pval;
  3193. int err;
  3194. switch (nid) {
  3195. case VNID_SPK:
  3196. /* follow shared_out info */
  3197. nid = spec->shared_out_nid;
  3198. mutex_lock(&codec->control_mutex);
  3199. pval = kcontrol->private_value;
  3200. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  3201. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  3202. kcontrol->private_value = pval;
  3203. mutex_unlock(&codec->control_mutex);
  3204. break;
  3205. case VNID_MIC:
  3206. /* follow shared_mic info */
  3207. nid = spec->shared_mic_nid;
  3208. mutex_lock(&codec->control_mutex);
  3209. pval = kcontrol->private_value;
  3210. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  3211. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  3212. kcontrol->private_value = pval;
  3213. mutex_unlock(&codec->control_mutex);
  3214. break;
  3215. default:
  3216. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  3217. }
  3218. return err;
  3219. }
  3220. static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
  3221. struct snd_ctl_elem_value *ucontrol)
  3222. {
  3223. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3224. struct ca0132_spec *spec = codec->spec;
  3225. hda_nid_t nid = get_amp_nid(kcontrol);
  3226. int ch = get_amp_channels(kcontrol);
  3227. long *valp = ucontrol->value.integer.value;
  3228. /* store the left and right volume */
  3229. if (ch & 1) {
  3230. *valp = spec->vnode_lvol[nid - VNODE_START_NID];
  3231. valp++;
  3232. }
  3233. if (ch & 2) {
  3234. *valp = spec->vnode_rvol[nid - VNODE_START_NID];
  3235. valp++;
  3236. }
  3237. return 0;
  3238. }
  3239. static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
  3240. struct snd_ctl_elem_value *ucontrol)
  3241. {
  3242. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3243. struct ca0132_spec *spec = codec->spec;
  3244. hda_nid_t nid = get_amp_nid(kcontrol);
  3245. int ch = get_amp_channels(kcontrol);
  3246. long *valp = ucontrol->value.integer.value;
  3247. hda_nid_t shared_nid = 0;
  3248. bool effective;
  3249. int changed = 1;
  3250. /* store the left and right volume */
  3251. if (ch & 1) {
  3252. spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
  3253. valp++;
  3254. }
  3255. if (ch & 2) {
  3256. spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
  3257. valp++;
  3258. }
  3259. /* if effective conditions, then update hw immediately. */
  3260. effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
  3261. if (effective) {
  3262. int dir = get_amp_direction(kcontrol);
  3263. unsigned long pval;
  3264. snd_hda_power_up(codec);
  3265. mutex_lock(&codec->control_mutex);
  3266. pval = kcontrol->private_value;
  3267. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
  3268. 0, dir);
  3269. changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
  3270. kcontrol->private_value = pval;
  3271. mutex_unlock(&codec->control_mutex);
  3272. snd_hda_power_down(codec);
  3273. }
  3274. return changed;
  3275. }
  3276. static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  3277. unsigned int size, unsigned int __user *tlv)
  3278. {
  3279. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3280. struct ca0132_spec *spec = codec->spec;
  3281. hda_nid_t nid = get_amp_nid(kcontrol);
  3282. int ch = get_amp_channels(kcontrol);
  3283. int dir = get_amp_direction(kcontrol);
  3284. unsigned long pval;
  3285. int err;
  3286. switch (nid) {
  3287. case VNID_SPK:
  3288. /* follow shared_out tlv */
  3289. nid = spec->shared_out_nid;
  3290. mutex_lock(&codec->control_mutex);
  3291. pval = kcontrol->private_value;
  3292. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  3293. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  3294. kcontrol->private_value = pval;
  3295. mutex_unlock(&codec->control_mutex);
  3296. break;
  3297. case VNID_MIC:
  3298. /* follow shared_mic tlv */
  3299. nid = spec->shared_mic_nid;
  3300. mutex_lock(&codec->control_mutex);
  3301. pval = kcontrol->private_value;
  3302. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  3303. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  3304. kcontrol->private_value = pval;
  3305. mutex_unlock(&codec->control_mutex);
  3306. break;
  3307. default:
  3308. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  3309. }
  3310. return err;
  3311. }
  3312. static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
  3313. const char *pfx, int dir)
  3314. {
  3315. char namestr[44];
  3316. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  3317. struct snd_kcontrol_new knew =
  3318. CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
  3319. sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
  3320. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  3321. }
  3322. static int add_voicefx(struct hda_codec *codec)
  3323. {
  3324. struct snd_kcontrol_new knew =
  3325. HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
  3326. VOICEFX, 1, 0, HDA_INPUT);
  3327. knew.info = ca0132_voicefx_info;
  3328. knew.get = ca0132_voicefx_get;
  3329. knew.put = ca0132_voicefx_put;
  3330. return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
  3331. }
  3332. /*
  3333. * When changing Node IDs for Mixer Controls below, make sure to update
  3334. * Node IDs in ca0132_config() as well.
  3335. */
  3336. static struct snd_kcontrol_new ca0132_mixer[] = {
  3337. CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
  3338. CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
  3339. CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
  3340. CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
  3341. HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
  3342. HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
  3343. HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
  3344. HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
  3345. CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
  3346. 0x12, 1, HDA_INPUT),
  3347. CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
  3348. VNID_HP_SEL, 1, HDA_OUTPUT),
  3349. CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
  3350. VNID_AMIC1_SEL, 1, HDA_INPUT),
  3351. CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
  3352. VNID_HP_ASEL, 1, HDA_OUTPUT),
  3353. CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
  3354. VNID_AMIC1_ASEL, 1, HDA_INPUT),
  3355. { } /* end */
  3356. };
  3357. static int ca0132_build_controls(struct hda_codec *codec)
  3358. {
  3359. struct ca0132_spec *spec = codec->spec;
  3360. int i, num_fx;
  3361. int err = 0;
  3362. /* Add Mixer controls */
  3363. for (i = 0; i < spec->num_mixers; i++) {
  3364. err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
  3365. if (err < 0)
  3366. return err;
  3367. }
  3368. /* Add in and out effects controls.
  3369. * VoiceFX, PE and CrystalVoice are added separately.
  3370. */
  3371. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  3372. for (i = 0; i < num_fx; i++) {
  3373. err = add_fx_switch(codec, ca0132_effects[i].nid,
  3374. ca0132_effects[i].name,
  3375. ca0132_effects[i].direct);
  3376. if (err < 0)
  3377. return err;
  3378. }
  3379. err = add_fx_switch(codec, PLAY_ENHANCEMENT, "PlayEnhancement", 0);
  3380. if (err < 0)
  3381. return err;
  3382. err = add_fx_switch(codec, CRYSTAL_VOICE, "CrystalVoice", 1);
  3383. if (err < 0)
  3384. return err;
  3385. add_voicefx(codec);
  3386. #ifdef ENABLE_TUNING_CONTROLS
  3387. add_tuning_ctls(codec);
  3388. #endif
  3389. err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
  3390. if (err < 0)
  3391. return err;
  3392. if (spec->dig_out) {
  3393. err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
  3394. spec->dig_out);
  3395. if (err < 0)
  3396. return err;
  3397. err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
  3398. if (err < 0)
  3399. return err;
  3400. /* spec->multiout.share_spdif = 1; */
  3401. }
  3402. if (spec->dig_in) {
  3403. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  3404. if (err < 0)
  3405. return err;
  3406. }
  3407. return 0;
  3408. }
  3409. /*
  3410. * PCM
  3411. */
  3412. static struct hda_pcm_stream ca0132_pcm_analog_playback = {
  3413. .substreams = 1,
  3414. .channels_min = 2,
  3415. .channels_max = 6,
  3416. .ops = {
  3417. .prepare = ca0132_playback_pcm_prepare,
  3418. .cleanup = ca0132_playback_pcm_cleanup
  3419. },
  3420. };
  3421. static struct hda_pcm_stream ca0132_pcm_analog_capture = {
  3422. .substreams = 1,
  3423. .channels_min = 2,
  3424. .channels_max = 2,
  3425. .ops = {
  3426. .prepare = ca0132_capture_pcm_prepare,
  3427. .cleanup = ca0132_capture_pcm_cleanup
  3428. },
  3429. };
  3430. static struct hda_pcm_stream ca0132_pcm_digital_playback = {
  3431. .substreams = 1,
  3432. .channels_min = 2,
  3433. .channels_max = 2,
  3434. .ops = {
  3435. .open = ca0132_dig_playback_pcm_open,
  3436. .close = ca0132_dig_playback_pcm_close,
  3437. .prepare = ca0132_dig_playback_pcm_prepare,
  3438. .cleanup = ca0132_dig_playback_pcm_cleanup
  3439. },
  3440. };
  3441. static struct hda_pcm_stream ca0132_pcm_digital_capture = {
  3442. .substreams = 1,
  3443. .channels_min = 2,
  3444. .channels_max = 2,
  3445. };
  3446. static int ca0132_build_pcms(struct hda_codec *codec)
  3447. {
  3448. struct ca0132_spec *spec = codec->spec;
  3449. struct hda_pcm *info = spec->pcm_rec;
  3450. codec->pcm_info = info;
  3451. codec->num_pcms = 0;
  3452. info->name = "CA0132 Analog";
  3453. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
  3454. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
  3455. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  3456. spec->multiout.max_channels;
  3457. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  3458. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  3459. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
  3460. codec->num_pcms++;
  3461. info++;
  3462. info->name = "CA0132 Analog Mic-In2";
  3463. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  3464. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  3465. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
  3466. codec->num_pcms++;
  3467. info++;
  3468. info->name = "CA0132 What U Hear";
  3469. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  3470. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  3471. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
  3472. codec->num_pcms++;
  3473. if (!spec->dig_out && !spec->dig_in)
  3474. return 0;
  3475. info++;
  3476. info->name = "CA0132 Digital";
  3477. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  3478. if (spec->dig_out) {
  3479. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  3480. ca0132_pcm_digital_playback;
  3481. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
  3482. }
  3483. if (spec->dig_in) {
  3484. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  3485. ca0132_pcm_digital_capture;
  3486. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
  3487. }
  3488. codec->num_pcms++;
  3489. return 0;
  3490. }
  3491. static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
  3492. {
  3493. if (pin) {
  3494. snd_hda_set_pin_ctl(codec, pin, PIN_HP);
  3495. if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
  3496. snd_hda_codec_write(codec, pin, 0,
  3497. AC_VERB_SET_AMP_GAIN_MUTE,
  3498. AMP_OUT_UNMUTE);
  3499. }
  3500. if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
  3501. snd_hda_codec_write(codec, dac, 0,
  3502. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
  3503. }
  3504. static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
  3505. {
  3506. if (pin) {
  3507. snd_hda_set_pin_ctl(codec, pin, PIN_VREF80);
  3508. if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
  3509. snd_hda_codec_write(codec, pin, 0,
  3510. AC_VERB_SET_AMP_GAIN_MUTE,
  3511. AMP_IN_UNMUTE(0));
  3512. }
  3513. if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP)) {
  3514. snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  3515. AMP_IN_UNMUTE(0));
  3516. /* init to 0 dB and unmute. */
  3517. snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
  3518. HDA_AMP_VOLMASK, 0x5a);
  3519. snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
  3520. HDA_AMP_MUTE, 0);
  3521. }
  3522. }
  3523. static void ca0132_init_unsol(struct hda_codec *codec)
  3524. {
  3525. snd_hda_jack_detect_enable(codec, UNSOL_TAG_HP, UNSOL_TAG_HP);
  3526. snd_hda_jack_detect_enable(codec, UNSOL_TAG_AMIC1, UNSOL_TAG_AMIC1);
  3527. }
  3528. static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
  3529. {
  3530. unsigned int caps;
  3531. caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
  3532. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  3533. snd_hda_override_amp_caps(codec, nid, dir, caps);
  3534. }
  3535. /*
  3536. * Switch between Digital built-in mic and analog mic.
  3537. */
  3538. static void ca0132_set_dmic(struct hda_codec *codec, int enable)
  3539. {
  3540. struct ca0132_spec *spec = codec->spec;
  3541. unsigned int tmp;
  3542. u8 val;
  3543. unsigned int oldval;
  3544. snd_printdd(KERN_INFO "ca0132_set_dmic: enable=%d\n", enable);
  3545. oldval = stop_mic1(codec);
  3546. ca0132_set_vipsource(codec, 0);
  3547. if (enable) {
  3548. /* set DMic input as 2-ch */
  3549. tmp = FLOAT_TWO;
  3550. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3551. val = spec->dmic_ctl;
  3552. val |= 0x80;
  3553. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3554. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  3555. if (!(spec->dmic_ctl & 0x20))
  3556. chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
  3557. } else {
  3558. /* set AMic input as mono */
  3559. tmp = FLOAT_ONE;
  3560. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3561. val = spec->dmic_ctl;
  3562. /* clear bit7 and bit5 to disable dmic */
  3563. val &= 0x5f;
  3564. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3565. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  3566. if (!(spec->dmic_ctl & 0x20))
  3567. chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
  3568. }
  3569. ca0132_set_vipsource(codec, 1);
  3570. resume_mic1(codec, oldval);
  3571. }
  3572. /*
  3573. * Initialization for Digital Mic.
  3574. */
  3575. static void ca0132_init_dmic(struct hda_codec *codec)
  3576. {
  3577. struct ca0132_spec *spec = codec->spec;
  3578. u8 val;
  3579. /* Setup Digital Mic here, but don't enable.
  3580. * Enable based on jack detect.
  3581. */
  3582. /* MCLK uses MPIO1, set to enable.
  3583. * Bit 2-0: MPIO select
  3584. * Bit 3: set to disable
  3585. * Bit 7-4: reserved
  3586. */
  3587. val = 0x01;
  3588. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3589. VENDOR_CHIPIO_DMIC_MCLK_SET, val);
  3590. /* Data1 uses MPIO3. Data2 not use
  3591. * Bit 2-0: Data1 MPIO select
  3592. * Bit 3: set disable Data1
  3593. * Bit 6-4: Data2 MPIO select
  3594. * Bit 7: set disable Data2
  3595. */
  3596. val = 0x83;
  3597. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3598. VENDOR_CHIPIO_DMIC_PIN_SET, val);
  3599. /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
  3600. * Bit 3-0: Channel mask
  3601. * Bit 4: set for 48KHz, clear for 32KHz
  3602. * Bit 5: mode
  3603. * Bit 6: set to select Data2, clear for Data1
  3604. * Bit 7: set to enable DMic, clear for AMic
  3605. */
  3606. val = 0x23;
  3607. /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
  3608. spec->dmic_ctl = val;
  3609. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3610. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  3611. }
  3612. /*
  3613. * Initialization for Analog Mic 2
  3614. */
  3615. static void ca0132_init_analog_mic2(struct hda_codec *codec)
  3616. {
  3617. struct ca0132_spec *spec = codec->spec;
  3618. mutex_lock(&spec->chipio_mutex);
  3619. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3620. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
  3621. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3622. VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
  3623. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3624. VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
  3625. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3626. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x2D);
  3627. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3628. VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
  3629. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3630. VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
  3631. mutex_unlock(&spec->chipio_mutex);
  3632. }
  3633. static void ca0132_refresh_widget_caps(struct hda_codec *codec)
  3634. {
  3635. struct ca0132_spec *spec = codec->spec;
  3636. int i;
  3637. hda_nid_t nid;
  3638. snd_printdd(KERN_INFO "ca0132_refresh_widget_caps.\n");
  3639. nid = codec->start_nid;
  3640. for (i = 0; i < codec->num_nodes; i++, nid++)
  3641. codec->wcaps[i] = snd_hda_param_read(codec, nid,
  3642. AC_PAR_AUDIO_WIDGET_CAP);
  3643. for (i = 0; i < spec->multiout.num_dacs; i++)
  3644. refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
  3645. for (i = 0; i < spec->num_outputs; i++)
  3646. refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
  3647. for (i = 0; i < spec->num_inputs; i++) {
  3648. refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
  3649. refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
  3650. }
  3651. }
  3652. /*
  3653. * Setup default parameters for DSP
  3654. */
  3655. static void ca0132_setup_defaults(struct hda_codec *codec)
  3656. {
  3657. unsigned int tmp;
  3658. int num_fx;
  3659. int idx, i;
  3660. if (!dspload_is_loaded(codec))
  3661. return;
  3662. /* out, in effects + voicefx */
  3663. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
  3664. for (idx = 0; idx < num_fx; idx++) {
  3665. for (i = 0; i <= ca0132_effects[idx].params; i++) {
  3666. dspio_set_uint_param(codec, ca0132_effects[idx].mid,
  3667. ca0132_effects[idx].reqs[i],
  3668. ca0132_effects[idx].def_vals[i]);
  3669. }
  3670. }
  3671. /*remove DSP headroom*/
  3672. tmp = FLOAT_ZERO;
  3673. dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
  3674. /*set speaker EQ bypass attenuation*/
  3675. dspio_set_uint_param(codec, 0x8f, 0x01, tmp);
  3676. /* set AMic1 and AMic2 as mono mic */
  3677. tmp = FLOAT_ONE;
  3678. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3679. dspio_set_uint_param(codec, 0x80, 0x01, tmp);
  3680. /* set AMic1 as CrystalVoice input */
  3681. tmp = FLOAT_ONE;
  3682. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  3683. /* set WUH source */
  3684. tmp = FLOAT_TWO;
  3685. dspio_set_uint_param(codec, 0x31, 0x00, tmp);
  3686. }
  3687. /*
  3688. * Initialization of flags in chip
  3689. */
  3690. static void ca0132_init_flags(struct hda_codec *codec)
  3691. {
  3692. chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
  3693. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
  3694. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
  3695. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
  3696. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
  3697. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
  3698. }
  3699. /*
  3700. * Initialization of parameters in chip
  3701. */
  3702. static void ca0132_init_params(struct hda_codec *codec)
  3703. {
  3704. chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
  3705. chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
  3706. }
  3707. static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
  3708. {
  3709. chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
  3710. chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
  3711. chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
  3712. chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
  3713. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
  3714. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
  3715. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  3716. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  3717. chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
  3718. }
  3719. static bool ca0132_download_dsp_images(struct hda_codec *codec)
  3720. {
  3721. bool dsp_loaded = false;
  3722. const struct dsp_image_seg *dsp_os_image;
  3723. const struct firmware *fw_entry;
  3724. if (request_firmware(&fw_entry, EFX_FILE, codec->bus->card->dev) != 0)
  3725. return false;
  3726. dsp_os_image = (struct dsp_image_seg *)(fw_entry->data);
  3727. dspload_image(codec, dsp_os_image, 0, 0, true, 0);
  3728. dsp_loaded = dspload_wait_loaded(codec);
  3729. release_firmware(fw_entry);
  3730. return dsp_loaded;
  3731. }
  3732. static void ca0132_download_dsp(struct hda_codec *codec)
  3733. {
  3734. struct ca0132_spec *spec = codec->spec;
  3735. #ifndef CONFIG_SND_HDA_CODEC_CA0132_DSP
  3736. return; /* NOP */
  3737. #endif
  3738. spec->dsp_state = DSP_DOWNLOAD_INIT;
  3739. if (spec->dsp_state == DSP_DOWNLOAD_INIT) {
  3740. chipio_enable_clocks(codec);
  3741. spec->dsp_state = DSP_DOWNLOADING;
  3742. if (!ca0132_download_dsp_images(codec))
  3743. spec->dsp_state = DSP_DOWNLOAD_FAILED;
  3744. else
  3745. spec->dsp_state = DSP_DOWNLOADED;
  3746. }
  3747. if (spec->dsp_state == DSP_DOWNLOADED)
  3748. ca0132_set_dsp_msr(codec, true);
  3749. }
  3750. static void ca0132_process_dsp_response(struct hda_codec *codec)
  3751. {
  3752. struct ca0132_spec *spec = codec->spec;
  3753. snd_printdd(KERN_INFO "ca0132_process_dsp_response\n");
  3754. if (spec->wait_scp) {
  3755. if (dspio_get_response_data(codec) >= 0)
  3756. spec->wait_scp = 0;
  3757. }
  3758. dspio_clear_response_queue(codec);
  3759. }
  3760. static void ca0132_unsol_event(struct hda_codec *codec, unsigned int res)
  3761. {
  3762. snd_printdd(KERN_INFO "ca0132_unsol_event: 0x%x\n", res);
  3763. if (((res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f) == UNSOL_TAG_DSP) {
  3764. ca0132_process_dsp_response(codec);
  3765. } else {
  3766. res = snd_hda_jack_get_action(codec,
  3767. (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f);
  3768. snd_printdd(KERN_INFO "snd_hda_jack_get_action: 0x%x\n", res);
  3769. switch (res) {
  3770. case UNSOL_TAG_HP:
  3771. ca0132_select_out(codec);
  3772. snd_hda_jack_report_sync(codec);
  3773. break;
  3774. case UNSOL_TAG_AMIC1:
  3775. ca0132_select_mic(codec);
  3776. snd_hda_jack_report_sync(codec);
  3777. break;
  3778. default:
  3779. break;
  3780. }
  3781. }
  3782. }
  3783. /*
  3784. * Verbs tables.
  3785. */
  3786. /* Sends before DSP download. */
  3787. static struct hda_verb ca0132_base_init_verbs[] = {
  3788. /*enable ct extension*/
  3789. {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
  3790. /*enable DSP node unsol, needed for DSP download*/
  3791. {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_DSP},
  3792. {}
  3793. };
  3794. /* Send at exit. */
  3795. static struct hda_verb ca0132_base_exit_verbs[] = {
  3796. /*set afg to D3*/
  3797. {0x01, AC_VERB_SET_POWER_STATE, 0x03},
  3798. /*disable ct extension*/
  3799. {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
  3800. {}
  3801. };
  3802. /* Other verbs tables. Sends after DSP download. */
  3803. static struct hda_verb ca0132_init_verbs0[] = {
  3804. /* chip init verbs */
  3805. {0x15, 0x70D, 0xF0},
  3806. {0x15, 0x70E, 0xFE},
  3807. {0x15, 0x707, 0x75},
  3808. {0x15, 0x707, 0xD3},
  3809. {0x15, 0x707, 0x09},
  3810. {0x15, 0x707, 0x53},
  3811. {0x15, 0x707, 0xD4},
  3812. {0x15, 0x707, 0xEF},
  3813. {0x15, 0x707, 0x75},
  3814. {0x15, 0x707, 0xD3},
  3815. {0x15, 0x707, 0x09},
  3816. {0x15, 0x707, 0x02},
  3817. {0x15, 0x707, 0x37},
  3818. {0x15, 0x707, 0x78},
  3819. {0x15, 0x53C, 0xCE},
  3820. {0x15, 0x575, 0xC9},
  3821. {0x15, 0x53D, 0xCE},
  3822. {0x15, 0x5B7, 0xC9},
  3823. {0x15, 0x70D, 0xE8},
  3824. {0x15, 0x70E, 0xFE},
  3825. {0x15, 0x707, 0x02},
  3826. {0x15, 0x707, 0x68},
  3827. {0x15, 0x707, 0x62},
  3828. {0x15, 0x53A, 0xCE},
  3829. {0x15, 0x546, 0xC9},
  3830. {0x15, 0x53B, 0xCE},
  3831. {0x15, 0x5E8, 0xC9},
  3832. {0x15, 0x717, 0x0D},
  3833. {0x15, 0x718, 0x20},
  3834. {}
  3835. };
  3836. static struct hda_verb ca0132_init_verbs1[] = {
  3837. {0x10, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_HP},
  3838. {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_AMIC1},
  3839. /* config EAPD */
  3840. {0x0b, 0x78D, 0x00},
  3841. /*{0x0b, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
  3842. /*{0x10, 0x78D, 0x02},*/
  3843. /*{0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
  3844. {}
  3845. };
  3846. static void ca0132_init_chip(struct hda_codec *codec)
  3847. {
  3848. struct ca0132_spec *spec = codec->spec;
  3849. int num_fx;
  3850. int i;
  3851. unsigned int on;
  3852. mutex_init(&spec->chipio_mutex);
  3853. spec->cur_out_type = SPEAKER_OUT;
  3854. spec->cur_mic_type = DIGITAL_MIC;
  3855. spec->cur_mic_boost = 0;
  3856. for (i = 0; i < VNODES_COUNT; i++) {
  3857. spec->vnode_lvol[i] = 0x5a;
  3858. spec->vnode_rvol[i] = 0x5a;
  3859. spec->vnode_lswitch[i] = 0;
  3860. spec->vnode_rswitch[i] = 0;
  3861. }
  3862. /*
  3863. * Default states for effects are in ca0132_effects[].
  3864. */
  3865. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  3866. for (i = 0; i < num_fx; i++) {
  3867. on = (unsigned int)ca0132_effects[i].reqs[0];
  3868. spec->effects_switch[i] = on ? 1 : 0;
  3869. }
  3870. spec->voicefx_val = 0;
  3871. spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
  3872. spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
  3873. #ifdef ENABLE_TUNING_CONTROLS
  3874. ca0132_init_tuning_defaults(codec);
  3875. #endif
  3876. }
  3877. static void ca0132_exit_chip(struct hda_codec *codec)
  3878. {
  3879. /* put any chip cleanup stuffs here. */
  3880. if (dspload_is_loaded(codec))
  3881. dsp_reset(codec);
  3882. }
  3883. static int ca0132_init(struct hda_codec *codec)
  3884. {
  3885. struct ca0132_spec *spec = codec->spec;
  3886. struct auto_pin_cfg *cfg = &spec->autocfg;
  3887. int i;
  3888. spec->dsp_state = DSP_DOWNLOAD_INIT;
  3889. spec->curr_chip_addx = INVALID_CHIP_ADDRESS;
  3890. snd_hda_power_up(codec);
  3891. ca0132_init_params(codec);
  3892. ca0132_init_flags(codec);
  3893. snd_hda_sequence_write(codec, spec->base_init_verbs);
  3894. ca0132_download_dsp(codec);
  3895. ca0132_refresh_widget_caps(codec);
  3896. ca0132_setup_defaults(codec);
  3897. ca0132_init_analog_mic2(codec);
  3898. ca0132_init_dmic(codec);
  3899. for (i = 0; i < spec->num_outputs; i++)
  3900. init_output(codec, spec->out_pins[i], spec->dacs[0]);
  3901. init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
  3902. for (i = 0; i < spec->num_inputs; i++)
  3903. init_input(codec, spec->input_pins[i], spec->adcs[i]);
  3904. init_input(codec, cfg->dig_in_pin, spec->dig_in);
  3905. for (i = 0; i < spec->num_init_verbs; i++)
  3906. snd_hda_sequence_write(codec, spec->init_verbs[i]);
  3907. ca0132_init_unsol(codec);
  3908. ca0132_select_out(codec);
  3909. ca0132_select_mic(codec);
  3910. snd_hda_jack_report_sync(codec);
  3911. snd_hda_power_down(codec);
  3912. return 0;
  3913. }
  3914. static void ca0132_free(struct hda_codec *codec)
  3915. {
  3916. struct ca0132_spec *spec = codec->spec;
  3917. snd_hda_power_up(codec);
  3918. snd_hda_sequence_write(codec, spec->base_exit_verbs);
  3919. ca0132_exit_chip(codec);
  3920. snd_hda_power_down(codec);
  3921. kfree(codec->spec);
  3922. }
  3923. static struct hda_codec_ops ca0132_patch_ops = {
  3924. .build_controls = ca0132_build_controls,
  3925. .build_pcms = ca0132_build_pcms,
  3926. .init = ca0132_init,
  3927. .free = ca0132_free,
  3928. .unsol_event = ca0132_unsol_event,
  3929. };
  3930. static void ca0132_config(struct hda_codec *codec)
  3931. {
  3932. struct ca0132_spec *spec = codec->spec;
  3933. struct auto_pin_cfg *cfg = &spec->autocfg;
  3934. spec->dacs[0] = 0x2;
  3935. spec->dacs[1] = 0x3;
  3936. spec->dacs[2] = 0x4;
  3937. spec->multiout.dac_nids = spec->dacs;
  3938. spec->multiout.num_dacs = 3;
  3939. spec->multiout.max_channels = 2;
  3940. spec->num_outputs = 2;
  3941. spec->out_pins[0] = 0x0b; /* speaker out */
  3942. spec->out_pins[1] = 0x10; /* headphone out */
  3943. spec->shared_out_nid = 0x2;
  3944. spec->num_inputs = 3;
  3945. spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
  3946. spec->adcs[1] = 0x8; /* analog mic2 */
  3947. spec->adcs[2] = 0xa; /* what u hear */
  3948. spec->shared_mic_nid = 0x7;
  3949. spec->input_pins[0] = 0x12;
  3950. spec->input_pins[1] = 0x11;
  3951. spec->input_pins[2] = 0x13;
  3952. /* SPDIF I/O */
  3953. spec->dig_out = 0x05;
  3954. spec->multiout.dig_out_nid = spec->dig_out;
  3955. cfg->dig_out_pins[0] = 0x0c;
  3956. cfg->dig_outs = 1;
  3957. cfg->dig_out_type[0] = HDA_PCM_TYPE_SPDIF;
  3958. spec->dig_in = 0x09;
  3959. cfg->dig_in_pin = 0x0e;
  3960. cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
  3961. }
  3962. static int patch_ca0132(struct hda_codec *codec)
  3963. {
  3964. struct ca0132_spec *spec;
  3965. int err;
  3966. snd_printdd("patch_ca0132\n");
  3967. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  3968. if (!spec)
  3969. return -ENOMEM;
  3970. codec->spec = spec;
  3971. spec->num_mixers = 1;
  3972. spec->mixers[0] = ca0132_mixer;
  3973. spec->base_init_verbs = ca0132_base_init_verbs;
  3974. spec->base_exit_verbs = ca0132_base_exit_verbs;
  3975. spec->init_verbs[0] = ca0132_init_verbs0;
  3976. spec->init_verbs[1] = ca0132_init_verbs1;
  3977. spec->num_init_verbs = 2;
  3978. ca0132_init_chip(codec);
  3979. ca0132_config(codec);
  3980. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  3981. if (err < 0)
  3982. return err;
  3983. codec->patch_ops = ca0132_patch_ops;
  3984. return 0;
  3985. }
  3986. /*
  3987. * patch entries
  3988. */
  3989. static struct hda_codec_preset snd_hda_preset_ca0132[] = {
  3990. { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
  3991. {} /* terminator */
  3992. };
  3993. MODULE_ALIAS("snd-hda-codec-id:11020011");
  3994. MODULE_LICENSE("GPL");
  3995. MODULE_DESCRIPTION("Creative Sound Core3D codec");
  3996. static struct hda_codec_preset_list ca0132_list = {
  3997. .preset = snd_hda_preset_ca0132,
  3998. .owner = THIS_MODULE,
  3999. };
  4000. static int __init patch_ca0132_init(void)
  4001. {
  4002. return snd_hda_add_codec_preset(&ca0132_list);
  4003. }
  4004. static void __exit patch_ca0132_exit(void)
  4005. {
  4006. snd_hda_delete_codec_preset(&ca0132_list);
  4007. }
  4008. module_init(patch_ca0132_init)
  4009. module_exit(patch_ca0132_exit)