patch_ca0132.c 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708
  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. if (hda_frame_size_words == 0) {
  1969. snd_printdd(KERN_ERR "frmsz zero\n");
  1970. return -EINVAL;
  1971. }
  1972. buffer_size_words = min(buffer_size_words,
  1973. (unsigned int)(UC_RANGE(chip_addx, 1) ?
  1974. 65536 : 32768));
  1975. buffer_size_words -= buffer_size_words % hda_frame_size_words;
  1976. snd_printdd(
  1977. "chpadr=0x%08x frmsz=%u nchan=%u "
  1978. "rate_mul=%u div=%u bufsz=%u\n",
  1979. chip_addx, hda_frame_size_words, num_chans,
  1980. sample_rate_mul, sample_rate_div, buffer_size_words);
  1981. if (buffer_size_words < hda_frame_size_words) {
  1982. snd_printdd(KERN_ERR "dspxfr_one_seg:failed\n");
  1983. return -EINVAL;
  1984. }
  1985. remainder_words = words_to_write % hda_frame_size_words;
  1986. data_remainder = data;
  1987. chip_addx_remainder = chip_addx;
  1988. data += remainder_words;
  1989. chip_addx += remainder_words*sizeof(u32);
  1990. words_to_write -= remainder_words;
  1991. while (words_to_write != 0) {
  1992. run_size_words = min(buffer_size_words, words_to_write);
  1993. snd_printdd("dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
  1994. words_to_write, run_size_words, remainder_words);
  1995. dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
  1996. if (!comm_dma_setup_done) {
  1997. status = dsp_dma_stop(codec, dma_chan, ovly);
  1998. if (status < 0)
  1999. return status;
  2000. status = dsp_dma_setup_common(codec, chip_addx,
  2001. dma_chan, port_map_mask, ovly);
  2002. if (status < 0)
  2003. return status;
  2004. comm_dma_setup_done = true;
  2005. }
  2006. status = dsp_dma_setup(codec, chip_addx,
  2007. run_size_words, dma_chan);
  2008. if (status < 0)
  2009. return status;
  2010. status = dsp_dma_start(codec, dma_chan, ovly);
  2011. if (status < 0)
  2012. return status;
  2013. if (!dsp_is_dma_active(codec, dma_chan)) {
  2014. snd_printdd(KERN_ERR "dspxfr:DMA did not start\n");
  2015. return -EIO;
  2016. }
  2017. status = dma_set_state(dma_engine, DMA_STATE_RUN);
  2018. if (status < 0)
  2019. return status;
  2020. if (remainder_words != 0) {
  2021. status = chipio_write_multiple(codec,
  2022. chip_addx_remainder,
  2023. data_remainder,
  2024. remainder_words);
  2025. if (status < 0)
  2026. return status;
  2027. remainder_words = 0;
  2028. }
  2029. if (hci_write) {
  2030. status = dspxfr_hci_write(codec, hci_write);
  2031. if (status < 0)
  2032. return status;
  2033. hci_write = NULL;
  2034. }
  2035. timeout = jiffies + msecs_to_jiffies(2000);
  2036. do {
  2037. dma_active = dsp_is_dma_active(codec, dma_chan);
  2038. if (!dma_active)
  2039. break;
  2040. msleep(20);
  2041. } while (time_before(jiffies, timeout));
  2042. if (dma_active)
  2043. break;
  2044. snd_printdd(KERN_INFO "+++++ DMA complete\n");
  2045. dma_set_state(dma_engine, DMA_STATE_STOP);
  2046. status = dma_reset(dma_engine);
  2047. if (status < 0)
  2048. return status;
  2049. data += run_size_words;
  2050. chip_addx += run_size_words*sizeof(u32);
  2051. words_to_write -= run_size_words;
  2052. }
  2053. if (remainder_words != 0) {
  2054. status = chipio_write_multiple(codec, chip_addx_remainder,
  2055. data_remainder, remainder_words);
  2056. }
  2057. return status;
  2058. }
  2059. /**
  2060. * Write the entire DSP image of a DSP code/data overlay to DSP memories
  2061. *
  2062. * @codec: the HDA codec
  2063. * @fls_data: pointer to a fast load image
  2064. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  2065. * no relocation
  2066. * @sample_rate: sampling rate of the stream used for DSP download
  2067. * @number_channels: channels of the stream used for DSP download
  2068. * @ovly: TRUE if overlay format is required
  2069. *
  2070. * Returns zero or a negative error code.
  2071. */
  2072. static int dspxfr_image(struct hda_codec *codec,
  2073. const struct dsp_image_seg *fls_data,
  2074. unsigned int reloc,
  2075. unsigned int sample_rate,
  2076. unsigned short channels,
  2077. bool ovly)
  2078. {
  2079. struct ca0132_spec *spec = codec->spec;
  2080. int status;
  2081. unsigned short hda_format = 0;
  2082. unsigned int response;
  2083. unsigned char stream_id = 0;
  2084. struct dma_engine *dma_engine;
  2085. unsigned int dma_chan;
  2086. unsigned int port_map_mask;
  2087. if (fls_data == NULL)
  2088. return -EINVAL;
  2089. dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
  2090. if (!dma_engine)
  2091. return -ENOMEM;
  2092. dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
  2093. if (!dma_engine->dmab) {
  2094. kfree(dma_engine);
  2095. return -ENOMEM;
  2096. }
  2097. dma_engine->codec = codec;
  2098. dma_convert_to_hda_format(sample_rate, channels, &hda_format);
  2099. dma_engine->m_converter_format = hda_format;
  2100. dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
  2101. DSP_DMA_WRITE_BUFLEN_INIT) * 2;
  2102. dma_chan = ovly ? INVALID_DMA_CHANNEL : 0;
  2103. status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
  2104. hda_format, &response);
  2105. if (status < 0) {
  2106. snd_printdd(KERN_ERR "set converter format fail\n");
  2107. goto exit;
  2108. }
  2109. status = snd_hda_codec_load_dsp_prepare(codec,
  2110. dma_engine->m_converter_format,
  2111. dma_engine->buf_size,
  2112. dma_engine->dmab);
  2113. if (status < 0)
  2114. goto exit;
  2115. spec->dsp_stream_id = status;
  2116. if (ovly) {
  2117. status = dspio_alloc_dma_chan(codec, &dma_chan);
  2118. if (status < 0) {
  2119. snd_printdd(KERN_ERR "alloc dmachan fail\n");
  2120. dma_chan = INVALID_DMA_CHANNEL;
  2121. goto exit;
  2122. }
  2123. }
  2124. port_map_mask = 0;
  2125. status = dsp_allocate_ports_format(codec, hda_format,
  2126. &port_map_mask);
  2127. if (status < 0) {
  2128. snd_printdd(KERN_ERR "alloc ports fail\n");
  2129. goto exit;
  2130. }
  2131. stream_id = dma_get_stream_id(dma_engine);
  2132. status = codec_set_converter_stream_channel(codec,
  2133. WIDGET_CHIP_CTRL, stream_id, 0, &response);
  2134. if (status < 0) {
  2135. snd_printdd(KERN_ERR "set stream chan fail\n");
  2136. goto exit;
  2137. }
  2138. while ((fls_data != NULL) && !is_last(fls_data)) {
  2139. if (!is_valid(fls_data)) {
  2140. snd_printdd(KERN_ERR "FLS check fail\n");
  2141. status = -EINVAL;
  2142. goto exit;
  2143. }
  2144. status = dspxfr_one_seg(codec, fls_data, reloc,
  2145. dma_engine, dma_chan,
  2146. port_map_mask, ovly);
  2147. if (status < 0)
  2148. break;
  2149. if (is_hci_prog_list_seg(fls_data))
  2150. fls_data = get_next_seg_ptr(fls_data);
  2151. if ((fls_data != NULL) && !is_last(fls_data))
  2152. fls_data = get_next_seg_ptr(fls_data);
  2153. }
  2154. if (port_map_mask != 0)
  2155. status = dsp_free_ports(codec);
  2156. if (status < 0)
  2157. goto exit;
  2158. status = codec_set_converter_stream_channel(codec,
  2159. WIDGET_CHIP_CTRL, 0, 0, &response);
  2160. exit:
  2161. if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
  2162. dspio_free_dma_chan(codec, dma_chan);
  2163. if (dma_engine->dmab->area)
  2164. snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
  2165. kfree(dma_engine->dmab);
  2166. kfree(dma_engine);
  2167. return status;
  2168. }
  2169. /*
  2170. * CA0132 DSP download stuffs.
  2171. */
  2172. static void dspload_post_setup(struct hda_codec *codec)
  2173. {
  2174. snd_printdd(KERN_INFO "---- dspload_post_setup ------\n");
  2175. /*set DSP speaker to 2.0 configuration*/
  2176. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
  2177. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
  2178. /*update write pointer*/
  2179. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
  2180. }
  2181. /**
  2182. * Download DSP from a DSP Image Fast Load structure. This structure is a
  2183. * linear, non-constant sized element array of structures, each of which
  2184. * contain the count of the data to be loaded, the data itself, and the
  2185. * corresponding starting chip address of the starting data location.
  2186. *
  2187. * @codec: the HDA codec
  2188. * @fls: pointer to a fast load image
  2189. * @ovly: TRUE if overlay format is required
  2190. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  2191. * no relocation
  2192. * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
  2193. * @router_chans: number of audio router channels to be allocated (0 means use
  2194. * internal defaults; max is 32)
  2195. *
  2196. * Returns zero or a negative error code.
  2197. */
  2198. static int dspload_image(struct hda_codec *codec,
  2199. const struct dsp_image_seg *fls,
  2200. bool ovly,
  2201. unsigned int reloc,
  2202. bool autostart,
  2203. int router_chans)
  2204. {
  2205. int status = 0;
  2206. unsigned int sample_rate;
  2207. unsigned short channels;
  2208. snd_printdd(KERN_INFO "---- dspload_image begin ------\n");
  2209. if (router_chans == 0) {
  2210. if (!ovly)
  2211. router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
  2212. else
  2213. router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
  2214. }
  2215. sample_rate = 48000;
  2216. channels = (unsigned short)router_chans;
  2217. while (channels > 16) {
  2218. sample_rate *= 2;
  2219. channels /= 2;
  2220. }
  2221. do {
  2222. snd_printdd(KERN_INFO "Ready to program DMA\n");
  2223. if (!ovly)
  2224. status = dsp_reset(codec);
  2225. if (status < 0)
  2226. break;
  2227. snd_printdd(KERN_INFO "dsp_reset() complete\n");
  2228. status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
  2229. ovly);
  2230. if (status < 0)
  2231. break;
  2232. snd_printdd(KERN_INFO "dspxfr_image() complete\n");
  2233. if (autostart && !ovly) {
  2234. dspload_post_setup(codec);
  2235. status = dsp_set_run_state(codec);
  2236. }
  2237. snd_printdd(KERN_INFO "LOAD FINISHED\n");
  2238. } while (0);
  2239. return status;
  2240. }
  2241. #ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
  2242. static bool dspload_is_loaded(struct hda_codec *codec)
  2243. {
  2244. unsigned int data = 0;
  2245. int status = 0;
  2246. status = chipio_read(codec, 0x40004, &data);
  2247. if ((status < 0) || (data != 1))
  2248. return false;
  2249. return true;
  2250. }
  2251. #else
  2252. #define dspload_is_loaded(codec) false
  2253. #endif
  2254. static bool dspload_wait_loaded(struct hda_codec *codec)
  2255. {
  2256. unsigned long timeout = jiffies + msecs_to_jiffies(2000);
  2257. do {
  2258. if (dspload_is_loaded(codec)) {
  2259. pr_info("ca0132 DOWNLOAD OK :-) DSP IS RUNNING.\n");
  2260. return true;
  2261. }
  2262. msleep(20);
  2263. } while (time_before(jiffies, timeout));
  2264. pr_err("ca0132 DOWNLOAD FAILED!!! DSP IS NOT RUNNING.\n");
  2265. return false;
  2266. }
  2267. /*
  2268. * PCM stuffs
  2269. */
  2270. static void ca0132_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  2271. u32 stream_tag,
  2272. int channel_id, int format)
  2273. {
  2274. unsigned int oldval, newval;
  2275. if (!nid)
  2276. return;
  2277. snd_printdd(
  2278. "ca0132_setup_stream: NID=0x%x, stream=0x%x, "
  2279. "channel=%d, format=0x%x\n",
  2280. nid, stream_tag, channel_id, format);
  2281. /* update the format-id if changed */
  2282. oldval = snd_hda_codec_read(codec, nid, 0,
  2283. AC_VERB_GET_STREAM_FORMAT,
  2284. 0);
  2285. if (oldval != format) {
  2286. msleep(20);
  2287. snd_hda_codec_write(codec, nid, 0,
  2288. AC_VERB_SET_STREAM_FORMAT,
  2289. format);
  2290. }
  2291. oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
  2292. newval = (stream_tag << 4) | channel_id;
  2293. if (oldval != newval) {
  2294. snd_hda_codec_write(codec, nid, 0,
  2295. AC_VERB_SET_CHANNEL_STREAMID,
  2296. newval);
  2297. }
  2298. }
  2299. static void ca0132_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
  2300. {
  2301. unsigned int val;
  2302. if (!nid)
  2303. return;
  2304. snd_printdd(KERN_INFO "ca0132_cleanup_stream: NID=0x%x\n", nid);
  2305. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
  2306. if (!val)
  2307. return;
  2308. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
  2309. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  2310. }
  2311. /*
  2312. * PCM callbacks
  2313. */
  2314. static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  2315. struct hda_codec *codec,
  2316. unsigned int stream_tag,
  2317. unsigned int format,
  2318. struct snd_pcm_substream *substream)
  2319. {
  2320. struct ca0132_spec *spec = codec->spec;
  2321. ca0132_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
  2322. return 0;
  2323. }
  2324. static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2325. struct hda_codec *codec,
  2326. struct snd_pcm_substream *substream)
  2327. {
  2328. struct ca0132_spec *spec = codec->spec;
  2329. if (spec->dsp_state == DSP_DOWNLOADING)
  2330. return 0;
  2331. /*If Playback effects are on, allow stream some time to flush
  2332. *effects tail*/
  2333. if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
  2334. msleep(50);
  2335. ca0132_cleanup_stream(codec, spec->dacs[0]);
  2336. return 0;
  2337. }
  2338. /*
  2339. * Digital out
  2340. */
  2341. static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  2342. struct hda_codec *codec,
  2343. struct snd_pcm_substream *substream)
  2344. {
  2345. struct ca0132_spec *spec = codec->spec;
  2346. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  2347. }
  2348. static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  2349. struct hda_codec *codec,
  2350. unsigned int stream_tag,
  2351. unsigned int format,
  2352. struct snd_pcm_substream *substream)
  2353. {
  2354. struct ca0132_spec *spec = codec->spec;
  2355. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  2356. stream_tag, format, substream);
  2357. }
  2358. static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2359. struct hda_codec *codec,
  2360. struct snd_pcm_substream *substream)
  2361. {
  2362. struct ca0132_spec *spec = codec->spec;
  2363. return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
  2364. }
  2365. static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  2366. struct hda_codec *codec,
  2367. struct snd_pcm_substream *substream)
  2368. {
  2369. struct ca0132_spec *spec = codec->spec;
  2370. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  2371. }
  2372. /*
  2373. * Analog capture
  2374. */
  2375. static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  2376. struct hda_codec *codec,
  2377. unsigned int stream_tag,
  2378. unsigned int format,
  2379. struct snd_pcm_substream *substream)
  2380. {
  2381. struct ca0132_spec *spec = codec->spec;
  2382. ca0132_setup_stream(codec, spec->adcs[substream->number],
  2383. stream_tag, 0, format);
  2384. return 0;
  2385. }
  2386. static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2387. struct hda_codec *codec,
  2388. struct snd_pcm_substream *substream)
  2389. {
  2390. struct ca0132_spec *spec = codec->spec;
  2391. if (spec->dsp_state == DSP_DOWNLOADING)
  2392. return 0;
  2393. ca0132_cleanup_stream(codec, hinfo->nid);
  2394. return 0;
  2395. }
  2396. /*
  2397. * Controls stuffs.
  2398. */
  2399. /*
  2400. * Mixer controls helpers.
  2401. */
  2402. #define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
  2403. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2404. .name = xname, \
  2405. .subdevice = HDA_SUBDEV_AMP_FLAG, \
  2406. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
  2407. SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
  2408. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
  2409. .info = ca0132_volume_info, \
  2410. .get = ca0132_volume_get, \
  2411. .put = ca0132_volume_put, \
  2412. .tlv = { .c = ca0132_volume_tlv }, \
  2413. .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
  2414. #define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
  2415. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2416. .name = xname, \
  2417. .subdevice = HDA_SUBDEV_AMP_FLAG, \
  2418. .info = snd_hda_mixer_amp_switch_info, \
  2419. .get = ca0132_switch_get, \
  2420. .put = ca0132_switch_put, \
  2421. .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
  2422. /* stereo */
  2423. #define CA0132_CODEC_VOL(xname, nid, dir) \
  2424. CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
  2425. #define CA0132_CODEC_MUTE(xname, nid, dir) \
  2426. CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
  2427. /* The followings are for tuning of products */
  2428. #ifdef ENABLE_TUNING_CONTROLS
  2429. static unsigned int voice_focus_vals_lookup[] = {
  2430. 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000,
  2431. 0x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000,
  2432. 0x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000,
  2433. 0x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000,
  2434. 0x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000,
  2435. 0x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000,
  2436. 0x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000,
  2437. 0x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000,
  2438. 0x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000,
  2439. 0x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000,
  2440. 0x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000,
  2441. 0x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000,
  2442. 0x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000,
  2443. 0x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000,
  2444. 0x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000,
  2445. 0x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000,
  2446. 0x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000,
  2447. 0x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000,
  2448. 0x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000,
  2449. 0x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000,
  2450. 0x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000,
  2451. 0x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000,
  2452. 0x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000,
  2453. 0x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000,
  2454. 0x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000,
  2455. 0x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000,
  2456. 0x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000
  2457. };
  2458. static unsigned int mic_svm_vals_lookup[] = {
  2459. 0x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
  2460. 0x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
  2461. 0x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
  2462. 0x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
  2463. 0x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
  2464. 0x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
  2465. 0x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
  2466. 0x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
  2467. 0x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
  2468. 0x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
  2469. 0x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
  2470. 0x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
  2471. 0x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
  2472. 0x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
  2473. 0x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
  2474. 0x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
  2475. 0x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
  2476. };
  2477. static unsigned int equalizer_vals_lookup[] = {
  2478. 0xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
  2479. 0xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
  2480. 0xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
  2481. 0xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
  2482. 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
  2483. 0x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
  2484. 0x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
  2485. 0x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
  2486. 0x41C00000
  2487. };
  2488. static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid,
  2489. unsigned int *lookup, int idx)
  2490. {
  2491. int i = 0;
  2492. for (i = 0; i < TUNING_CTLS_COUNT; i++)
  2493. if (nid == ca0132_tuning_ctls[i].nid)
  2494. break;
  2495. snd_hda_power_up(codec);
  2496. dspio_set_param(codec, ca0132_tuning_ctls[i].mid,
  2497. ca0132_tuning_ctls[i].req,
  2498. &(lookup[idx]), sizeof(unsigned int));
  2499. snd_hda_power_down(codec);
  2500. return 1;
  2501. }
  2502. static int tuning_ctl_get(struct snd_kcontrol *kcontrol,
  2503. struct snd_ctl_elem_value *ucontrol)
  2504. {
  2505. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2506. struct ca0132_spec *spec = codec->spec;
  2507. hda_nid_t nid = get_amp_nid(kcontrol);
  2508. long *valp = ucontrol->value.integer.value;
  2509. int idx = nid - TUNING_CTL_START_NID;
  2510. *valp = spec->cur_ctl_vals[idx];
  2511. return 0;
  2512. }
  2513. static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol,
  2514. struct snd_ctl_elem_info *uinfo)
  2515. {
  2516. int chs = get_amp_channels(kcontrol);
  2517. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2518. uinfo->count = chs == 3 ? 2 : 1;
  2519. uinfo->value.integer.min = 20;
  2520. uinfo->value.integer.max = 180;
  2521. uinfo->value.integer.step = 1;
  2522. return 0;
  2523. }
  2524. static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol,
  2525. struct snd_ctl_elem_value *ucontrol)
  2526. {
  2527. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2528. struct ca0132_spec *spec = codec->spec;
  2529. hda_nid_t nid = get_amp_nid(kcontrol);
  2530. long *valp = ucontrol->value.integer.value;
  2531. int idx;
  2532. idx = nid - TUNING_CTL_START_NID;
  2533. /* any change? */
  2534. if (spec->cur_ctl_vals[idx] == *valp)
  2535. return 0;
  2536. spec->cur_ctl_vals[idx] = *valp;
  2537. idx = *valp - 20;
  2538. tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx);
  2539. return 1;
  2540. }
  2541. static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol,
  2542. struct snd_ctl_elem_info *uinfo)
  2543. {
  2544. int chs = get_amp_channels(kcontrol);
  2545. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2546. uinfo->count = chs == 3 ? 2 : 1;
  2547. uinfo->value.integer.min = 0;
  2548. uinfo->value.integer.max = 100;
  2549. uinfo->value.integer.step = 1;
  2550. return 0;
  2551. }
  2552. static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol,
  2553. struct snd_ctl_elem_value *ucontrol)
  2554. {
  2555. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2556. struct ca0132_spec *spec = codec->spec;
  2557. hda_nid_t nid = get_amp_nid(kcontrol);
  2558. long *valp = ucontrol->value.integer.value;
  2559. int idx;
  2560. idx = nid - TUNING_CTL_START_NID;
  2561. /* any change? */
  2562. if (spec->cur_ctl_vals[idx] == *valp)
  2563. return 0;
  2564. spec->cur_ctl_vals[idx] = *valp;
  2565. idx = *valp;
  2566. tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx);
  2567. return 0;
  2568. }
  2569. static int equalizer_ctl_info(struct snd_kcontrol *kcontrol,
  2570. struct snd_ctl_elem_info *uinfo)
  2571. {
  2572. int chs = get_amp_channels(kcontrol);
  2573. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2574. uinfo->count = chs == 3 ? 2 : 1;
  2575. uinfo->value.integer.min = 0;
  2576. uinfo->value.integer.max = 48;
  2577. uinfo->value.integer.step = 1;
  2578. return 0;
  2579. }
  2580. static int equalizer_ctl_put(struct snd_kcontrol *kcontrol,
  2581. struct snd_ctl_elem_value *ucontrol)
  2582. {
  2583. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2584. struct ca0132_spec *spec = codec->spec;
  2585. hda_nid_t nid = get_amp_nid(kcontrol);
  2586. long *valp = ucontrol->value.integer.value;
  2587. int idx;
  2588. idx = nid - TUNING_CTL_START_NID;
  2589. /* any change? */
  2590. if (spec->cur_ctl_vals[idx] == *valp)
  2591. return 0;
  2592. spec->cur_ctl_vals[idx] = *valp;
  2593. idx = *valp;
  2594. tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx);
  2595. return 1;
  2596. }
  2597. static const DECLARE_TLV_DB_SCALE(voice_focus_db_scale, 2000, 100, 0);
  2598. static const DECLARE_TLV_DB_SCALE(eq_db_scale, -2400, 100, 0);
  2599. static int add_tuning_control(struct hda_codec *codec,
  2600. hda_nid_t pnid, hda_nid_t nid,
  2601. const char *name, int dir)
  2602. {
  2603. char namestr[44];
  2604. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  2605. struct snd_kcontrol_new knew =
  2606. HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
  2607. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  2608. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  2609. knew.tlv.c = 0;
  2610. knew.tlv.p = 0;
  2611. switch (pnid) {
  2612. case VOICE_FOCUS:
  2613. knew.info = voice_focus_ctl_info;
  2614. knew.get = tuning_ctl_get;
  2615. knew.put = voice_focus_ctl_put;
  2616. knew.tlv.p = voice_focus_db_scale;
  2617. break;
  2618. case MIC_SVM:
  2619. knew.info = mic_svm_ctl_info;
  2620. knew.get = tuning_ctl_get;
  2621. knew.put = mic_svm_ctl_put;
  2622. break;
  2623. case EQUALIZER:
  2624. knew.info = equalizer_ctl_info;
  2625. knew.get = tuning_ctl_get;
  2626. knew.put = equalizer_ctl_put;
  2627. knew.tlv.p = eq_db_scale;
  2628. break;
  2629. default:
  2630. return 0;
  2631. }
  2632. knew.private_value =
  2633. HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
  2634. sprintf(namestr, "%s %s Volume", name, dirstr[dir]);
  2635. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  2636. }
  2637. static int add_tuning_ctls(struct hda_codec *codec)
  2638. {
  2639. int i;
  2640. int err;
  2641. for (i = 0; i < TUNING_CTLS_COUNT; i++) {
  2642. err = add_tuning_control(codec,
  2643. ca0132_tuning_ctls[i].parent_nid,
  2644. ca0132_tuning_ctls[i].nid,
  2645. ca0132_tuning_ctls[i].name,
  2646. ca0132_tuning_ctls[i].direct);
  2647. if (err < 0)
  2648. return err;
  2649. }
  2650. return 0;
  2651. }
  2652. static void ca0132_init_tuning_defaults(struct hda_codec *codec)
  2653. {
  2654. struct ca0132_spec *spec = codec->spec;
  2655. int i;
  2656. /* Wedge Angle defaults to 30. 10 below is 30 - 20. 20 is min. */
  2657. spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10;
  2658. /* SVM level defaults to 0.74. */
  2659. spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74;
  2660. /* EQ defaults to 0dB. */
  2661. for (i = 2; i < TUNING_CTLS_COUNT; i++)
  2662. spec->cur_ctl_vals[i] = 24;
  2663. }
  2664. #endif /*ENABLE_TUNING_CONTROLS*/
  2665. /*
  2666. * Select the active output.
  2667. * If autodetect is enabled, output will be selected based on jack detection.
  2668. * If jack inserted, headphone will be selected, else built-in speakers
  2669. * If autodetect is disabled, output will be selected based on selection.
  2670. */
  2671. static int ca0132_select_out(struct hda_codec *codec)
  2672. {
  2673. struct ca0132_spec *spec = codec->spec;
  2674. unsigned int pin_ctl;
  2675. int jack_present;
  2676. int auto_jack;
  2677. unsigned int tmp;
  2678. int err;
  2679. snd_printdd(KERN_INFO "ca0132_select_out\n");
  2680. snd_hda_power_up(codec);
  2681. auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  2682. if (auto_jack)
  2683. jack_present = snd_hda_jack_detect(codec, spec->out_pins[1]);
  2684. else
  2685. jack_present =
  2686. spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
  2687. if (jack_present)
  2688. spec->cur_out_type = HEADPHONE_OUT;
  2689. else
  2690. spec->cur_out_type = SPEAKER_OUT;
  2691. if (spec->cur_out_type == SPEAKER_OUT) {
  2692. snd_printdd(KERN_INFO "ca0132_select_out speaker\n");
  2693. /*speaker out config*/
  2694. tmp = FLOAT_ONE;
  2695. err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
  2696. if (err < 0)
  2697. goto exit;
  2698. /*enable speaker EQ*/
  2699. tmp = FLOAT_ONE;
  2700. err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
  2701. if (err < 0)
  2702. goto exit;
  2703. /* Setup EAPD */
  2704. snd_hda_codec_write(codec, spec->out_pins[1], 0,
  2705. VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
  2706. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2707. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  2708. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2709. VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
  2710. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2711. AC_VERB_SET_EAPD_BTLENABLE, 0x02);
  2712. /* disable headphone node */
  2713. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
  2714. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2715. snd_hda_set_pin_ctl(codec, spec->out_pins[1],
  2716. pin_ctl & ~PIN_HP);
  2717. /* enable speaker node */
  2718. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  2719. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2720. snd_hda_set_pin_ctl(codec, spec->out_pins[0],
  2721. pin_ctl | PIN_OUT);
  2722. } else {
  2723. snd_printdd(KERN_INFO "ca0132_select_out hp\n");
  2724. /*headphone out config*/
  2725. tmp = FLOAT_ZERO;
  2726. err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
  2727. if (err < 0)
  2728. goto exit;
  2729. /*disable speaker EQ*/
  2730. tmp = FLOAT_ZERO;
  2731. err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
  2732. if (err < 0)
  2733. goto exit;
  2734. /* Setup EAPD */
  2735. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2736. VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
  2737. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2738. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  2739. snd_hda_codec_write(codec, spec->out_pins[1], 0,
  2740. VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
  2741. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2742. AC_VERB_SET_EAPD_BTLENABLE, 0x02);
  2743. /* disable speaker*/
  2744. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  2745. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2746. snd_hda_set_pin_ctl(codec, spec->out_pins[0],
  2747. pin_ctl & ~PIN_HP);
  2748. /* enable headphone*/
  2749. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
  2750. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2751. snd_hda_set_pin_ctl(codec, spec->out_pins[1],
  2752. pin_ctl | PIN_HP);
  2753. }
  2754. exit:
  2755. snd_hda_power_down(codec);
  2756. return err < 0 ? err : 0;
  2757. }
  2758. static void ca0132_set_dmic(struct hda_codec *codec, int enable);
  2759. static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
  2760. static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
  2761. /*
  2762. * Select the active VIP source
  2763. */
  2764. static int ca0132_set_vipsource(struct hda_codec *codec, int val)
  2765. {
  2766. struct ca0132_spec *spec = codec->spec;
  2767. unsigned int tmp;
  2768. if (spec->dsp_state != DSP_DOWNLOADED)
  2769. return 0;
  2770. /* if CrystalVoice if off, vipsource should be 0 */
  2771. if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
  2772. (val == 0)) {
  2773. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
  2774. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  2775. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  2776. if (spec->cur_mic_type == DIGITAL_MIC)
  2777. tmp = FLOAT_TWO;
  2778. else
  2779. tmp = FLOAT_ONE;
  2780. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  2781. tmp = FLOAT_ZERO;
  2782. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  2783. } else {
  2784. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
  2785. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
  2786. if (spec->cur_mic_type == DIGITAL_MIC)
  2787. tmp = FLOAT_TWO;
  2788. else
  2789. tmp = FLOAT_ONE;
  2790. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  2791. tmp = FLOAT_ONE;
  2792. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  2793. msleep(20);
  2794. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
  2795. }
  2796. return 1;
  2797. }
  2798. /*
  2799. * Select the active microphone.
  2800. * If autodetect is enabled, mic will be selected based on jack detection.
  2801. * If jack inserted, ext.mic will be selected, else built-in mic
  2802. * If autodetect is disabled, mic will be selected based on selection.
  2803. */
  2804. static int ca0132_select_mic(struct hda_codec *codec)
  2805. {
  2806. struct ca0132_spec *spec = codec->spec;
  2807. int jack_present;
  2808. int auto_jack;
  2809. snd_printdd(KERN_INFO "ca0132_select_mic\n");
  2810. snd_hda_power_up(codec);
  2811. auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
  2812. if (auto_jack)
  2813. jack_present = snd_hda_jack_detect(codec, spec->input_pins[0]);
  2814. else
  2815. jack_present =
  2816. spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
  2817. if (jack_present)
  2818. spec->cur_mic_type = LINE_MIC_IN;
  2819. else
  2820. spec->cur_mic_type = DIGITAL_MIC;
  2821. if (spec->cur_mic_type == DIGITAL_MIC) {
  2822. /* enable digital Mic */
  2823. chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
  2824. ca0132_set_dmic(codec, 1);
  2825. ca0132_mic_boost_set(codec, 0);
  2826. /* set voice focus */
  2827. ca0132_effects_set(codec, VOICE_FOCUS,
  2828. spec->effects_switch
  2829. [VOICE_FOCUS - EFFECT_START_NID]);
  2830. } else {
  2831. /* disable digital Mic */
  2832. chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
  2833. ca0132_set_dmic(codec, 0);
  2834. ca0132_mic_boost_set(codec, spec->cur_mic_boost);
  2835. /* disable voice focus */
  2836. ca0132_effects_set(codec, VOICE_FOCUS, 0);
  2837. }
  2838. snd_hda_power_down(codec);
  2839. return 0;
  2840. }
  2841. /*
  2842. * Check if VNODE settings take effect immediately.
  2843. */
  2844. static bool ca0132_is_vnode_effective(struct hda_codec *codec,
  2845. hda_nid_t vnid,
  2846. hda_nid_t *shared_nid)
  2847. {
  2848. struct ca0132_spec *spec = codec->spec;
  2849. hda_nid_t nid;
  2850. switch (vnid) {
  2851. case VNID_SPK:
  2852. nid = spec->shared_out_nid;
  2853. break;
  2854. case VNID_MIC:
  2855. nid = spec->shared_mic_nid;
  2856. break;
  2857. default:
  2858. return false;
  2859. }
  2860. if (shared_nid)
  2861. *shared_nid = nid;
  2862. return true;
  2863. }
  2864. /*
  2865. * The following functions are control change helpers.
  2866. * They return 0 if no changed. Return 1 if changed.
  2867. */
  2868. static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
  2869. {
  2870. struct ca0132_spec *spec = codec->spec;
  2871. unsigned int tmp;
  2872. /* based on CrystalVoice state to enable VoiceFX. */
  2873. if (enable) {
  2874. tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
  2875. FLOAT_ONE : FLOAT_ZERO;
  2876. } else {
  2877. tmp = FLOAT_ZERO;
  2878. }
  2879. dspio_set_uint_param(codec, ca0132_voicefx.mid,
  2880. ca0132_voicefx.reqs[0], tmp);
  2881. return 1;
  2882. }
  2883. /*
  2884. * Set the effects parameters
  2885. */
  2886. static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
  2887. {
  2888. struct ca0132_spec *spec = codec->spec;
  2889. unsigned int on;
  2890. int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  2891. int err = 0;
  2892. int idx = nid - EFFECT_START_NID;
  2893. if ((idx < 0) || (idx >= num_fx))
  2894. return 0; /* no changed */
  2895. /* for out effect, qualify with PE */
  2896. if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
  2897. /* if PE if off, turn off out effects. */
  2898. if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
  2899. val = 0;
  2900. }
  2901. /* for in effect, qualify with CrystalVoice */
  2902. if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
  2903. /* if CrystalVoice if off, turn off in effects. */
  2904. if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
  2905. val = 0;
  2906. /* Voice Focus applies to 2-ch Mic, Digital Mic */
  2907. if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
  2908. val = 0;
  2909. }
  2910. snd_printdd(KERN_INFO "ca0132_effect_set: nid=0x%x, val=%ld\n",
  2911. nid, val);
  2912. on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
  2913. err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
  2914. ca0132_effects[idx].reqs[0], on);
  2915. if (err < 0)
  2916. return 0; /* no changed */
  2917. return 1;
  2918. }
  2919. /*
  2920. * Turn on/off Playback Enhancements
  2921. */
  2922. static int ca0132_pe_switch_set(struct hda_codec *codec)
  2923. {
  2924. struct ca0132_spec *spec = codec->spec;
  2925. hda_nid_t nid;
  2926. int i, ret = 0;
  2927. snd_printdd(KERN_INFO "ca0132_pe_switch_set: val=%ld\n",
  2928. spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
  2929. i = OUT_EFFECT_START_NID - EFFECT_START_NID;
  2930. nid = OUT_EFFECT_START_NID;
  2931. /* PE affects all out effects */
  2932. for (; nid < OUT_EFFECT_END_NID; nid++, i++)
  2933. ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
  2934. return ret;
  2935. }
  2936. /* Check if Mic1 is streaming, if so, stop streaming */
  2937. static int stop_mic1(struct hda_codec *codec)
  2938. {
  2939. struct ca0132_spec *spec = codec->spec;
  2940. unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
  2941. AC_VERB_GET_CONV, 0);
  2942. if (oldval != 0)
  2943. snd_hda_codec_write(codec, spec->adcs[0], 0,
  2944. AC_VERB_SET_CHANNEL_STREAMID,
  2945. 0);
  2946. return oldval;
  2947. }
  2948. /* Resume Mic1 streaming if it was stopped. */
  2949. static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
  2950. {
  2951. struct ca0132_spec *spec = codec->spec;
  2952. /* Restore the previous stream and channel */
  2953. if (oldval != 0)
  2954. snd_hda_codec_write(codec, spec->adcs[0], 0,
  2955. AC_VERB_SET_CHANNEL_STREAMID,
  2956. oldval);
  2957. }
  2958. /*
  2959. * Turn on/off CrystalVoice
  2960. */
  2961. static int ca0132_cvoice_switch_set(struct hda_codec *codec)
  2962. {
  2963. struct ca0132_spec *spec = codec->spec;
  2964. hda_nid_t nid;
  2965. int i, ret = 0;
  2966. unsigned int oldval;
  2967. snd_printdd(KERN_INFO "ca0132_cvoice_switch_set: val=%ld\n",
  2968. spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
  2969. i = IN_EFFECT_START_NID - EFFECT_START_NID;
  2970. nid = IN_EFFECT_START_NID;
  2971. /* CrystalVoice affects all in effects */
  2972. for (; nid < IN_EFFECT_END_NID; nid++, i++)
  2973. ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
  2974. /* including VoiceFX */
  2975. ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
  2976. /* set correct vipsource */
  2977. oldval = stop_mic1(codec);
  2978. ret |= ca0132_set_vipsource(codec, 1);
  2979. resume_mic1(codec, oldval);
  2980. return ret;
  2981. }
  2982. static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
  2983. {
  2984. struct ca0132_spec *spec = codec->spec;
  2985. int ret = 0;
  2986. if (val) /* on */
  2987. ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
  2988. HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
  2989. else /* off */
  2990. ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
  2991. HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
  2992. return ret;
  2993. }
  2994. static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
  2995. struct snd_ctl_elem_value *ucontrol)
  2996. {
  2997. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2998. hda_nid_t nid = get_amp_nid(kcontrol);
  2999. hda_nid_t shared_nid = 0;
  3000. bool effective;
  3001. int ret = 0;
  3002. struct ca0132_spec *spec = codec->spec;
  3003. int auto_jack;
  3004. if (nid == VNID_HP_SEL) {
  3005. auto_jack =
  3006. spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  3007. if (!auto_jack)
  3008. ca0132_select_out(codec);
  3009. return 1;
  3010. }
  3011. if (nid == VNID_AMIC1_SEL) {
  3012. auto_jack =
  3013. spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
  3014. if (!auto_jack)
  3015. ca0132_select_mic(codec);
  3016. return 1;
  3017. }
  3018. if (nid == VNID_HP_ASEL) {
  3019. ca0132_select_out(codec);
  3020. return 1;
  3021. }
  3022. if (nid == VNID_AMIC1_ASEL) {
  3023. ca0132_select_mic(codec);
  3024. return 1;
  3025. }
  3026. /* if effective conditions, then update hw immediately. */
  3027. effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
  3028. if (effective) {
  3029. int dir = get_amp_direction(kcontrol);
  3030. int ch = get_amp_channels(kcontrol);
  3031. unsigned long pval;
  3032. mutex_lock(&codec->control_mutex);
  3033. pval = kcontrol->private_value;
  3034. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
  3035. 0, dir);
  3036. ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  3037. kcontrol->private_value = pval;
  3038. mutex_unlock(&codec->control_mutex);
  3039. }
  3040. return ret;
  3041. }
  3042. /* End of control change helpers. */
  3043. static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
  3044. struct snd_ctl_elem_info *uinfo)
  3045. {
  3046. unsigned int items = sizeof(ca0132_voicefx_presets)
  3047. / sizeof(struct ct_voicefx_preset);
  3048. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  3049. uinfo->count = 1;
  3050. uinfo->value.enumerated.items = items;
  3051. if (uinfo->value.enumerated.item >= items)
  3052. uinfo->value.enumerated.item = items - 1;
  3053. strcpy(uinfo->value.enumerated.name,
  3054. ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
  3055. return 0;
  3056. }
  3057. static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
  3058. struct snd_ctl_elem_value *ucontrol)
  3059. {
  3060. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3061. struct ca0132_spec *spec = codec->spec;
  3062. ucontrol->value.enumerated.item[0] = spec->voicefx_val;
  3063. return 0;
  3064. }
  3065. static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
  3066. struct snd_ctl_elem_value *ucontrol)
  3067. {
  3068. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3069. struct ca0132_spec *spec = codec->spec;
  3070. int i, err = 0;
  3071. int sel = ucontrol->value.enumerated.item[0];
  3072. unsigned int items = sizeof(ca0132_voicefx_presets)
  3073. / sizeof(struct ct_voicefx_preset);
  3074. if (sel >= items)
  3075. return 0;
  3076. snd_printdd(KERN_INFO "ca0132_voicefx_put: sel=%d, preset=%s\n",
  3077. sel, ca0132_voicefx_presets[sel].name);
  3078. /*
  3079. * Idx 0 is default.
  3080. * Default needs to qualify with CrystalVoice state.
  3081. */
  3082. for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
  3083. err = dspio_set_uint_param(codec, ca0132_voicefx.mid,
  3084. ca0132_voicefx.reqs[i],
  3085. ca0132_voicefx_presets[sel].vals[i]);
  3086. if (err < 0)
  3087. break;
  3088. }
  3089. if (err >= 0) {
  3090. spec->voicefx_val = sel;
  3091. /* enable voice fx */
  3092. ca0132_voicefx_set(codec, (sel ? 1 : 0));
  3093. }
  3094. return 1;
  3095. }
  3096. static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
  3097. struct snd_ctl_elem_value *ucontrol)
  3098. {
  3099. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3100. struct ca0132_spec *spec = codec->spec;
  3101. hda_nid_t nid = get_amp_nid(kcontrol);
  3102. int ch = get_amp_channels(kcontrol);
  3103. long *valp = ucontrol->value.integer.value;
  3104. /* vnode */
  3105. if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
  3106. if (ch & 1) {
  3107. *valp = spec->vnode_lswitch[nid - VNODE_START_NID];
  3108. valp++;
  3109. }
  3110. if (ch & 2) {
  3111. *valp = spec->vnode_rswitch[nid - VNODE_START_NID];
  3112. valp++;
  3113. }
  3114. return 0;
  3115. }
  3116. /* effects, include PE and CrystalVoice */
  3117. if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
  3118. *valp = spec->effects_switch[nid - EFFECT_START_NID];
  3119. return 0;
  3120. }
  3121. /* mic boost */
  3122. if (nid == spec->input_pins[0]) {
  3123. *valp = spec->cur_mic_boost;
  3124. return 0;
  3125. }
  3126. return 0;
  3127. }
  3128. static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
  3129. struct snd_ctl_elem_value *ucontrol)
  3130. {
  3131. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3132. struct ca0132_spec *spec = codec->spec;
  3133. hda_nid_t nid = get_amp_nid(kcontrol);
  3134. int ch = get_amp_channels(kcontrol);
  3135. long *valp = ucontrol->value.integer.value;
  3136. int changed = 1;
  3137. snd_printdd(KERN_INFO "ca0132_switch_put: nid=0x%x, val=%ld\n",
  3138. nid, *valp);
  3139. snd_hda_power_up(codec);
  3140. /* vnode */
  3141. if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
  3142. if (ch & 1) {
  3143. spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
  3144. valp++;
  3145. }
  3146. if (ch & 2) {
  3147. spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
  3148. valp++;
  3149. }
  3150. changed = ca0132_vnode_switch_set(kcontrol, ucontrol);
  3151. goto exit;
  3152. }
  3153. /* PE */
  3154. if (nid == PLAY_ENHANCEMENT) {
  3155. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  3156. changed = ca0132_pe_switch_set(codec);
  3157. goto exit;
  3158. }
  3159. /* CrystalVoice */
  3160. if (nid == CRYSTAL_VOICE) {
  3161. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  3162. changed = ca0132_cvoice_switch_set(codec);
  3163. goto exit;
  3164. }
  3165. /* out and in effects */
  3166. if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
  3167. ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
  3168. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  3169. changed = ca0132_effects_set(codec, nid, *valp);
  3170. goto exit;
  3171. }
  3172. /* mic boost */
  3173. if (nid == spec->input_pins[0]) {
  3174. spec->cur_mic_boost = *valp;
  3175. /* Mic boost does not apply to Digital Mic */
  3176. if (spec->cur_mic_type != DIGITAL_MIC)
  3177. changed = ca0132_mic_boost_set(codec, *valp);
  3178. goto exit;
  3179. }
  3180. exit:
  3181. snd_hda_power_down(codec);
  3182. return changed;
  3183. }
  3184. /*
  3185. * Volume related
  3186. */
  3187. static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
  3188. struct snd_ctl_elem_info *uinfo)
  3189. {
  3190. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3191. struct ca0132_spec *spec = codec->spec;
  3192. hda_nid_t nid = get_amp_nid(kcontrol);
  3193. int ch = get_amp_channels(kcontrol);
  3194. int dir = get_amp_direction(kcontrol);
  3195. unsigned long pval;
  3196. int err;
  3197. switch (nid) {
  3198. case VNID_SPK:
  3199. /* follow shared_out info */
  3200. nid = spec->shared_out_nid;
  3201. mutex_lock(&codec->control_mutex);
  3202. pval = kcontrol->private_value;
  3203. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  3204. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  3205. kcontrol->private_value = pval;
  3206. mutex_unlock(&codec->control_mutex);
  3207. break;
  3208. case VNID_MIC:
  3209. /* follow shared_mic info */
  3210. nid = spec->shared_mic_nid;
  3211. mutex_lock(&codec->control_mutex);
  3212. pval = kcontrol->private_value;
  3213. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  3214. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  3215. kcontrol->private_value = pval;
  3216. mutex_unlock(&codec->control_mutex);
  3217. break;
  3218. default:
  3219. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  3220. }
  3221. return err;
  3222. }
  3223. static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
  3224. struct snd_ctl_elem_value *ucontrol)
  3225. {
  3226. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3227. struct ca0132_spec *spec = codec->spec;
  3228. hda_nid_t nid = get_amp_nid(kcontrol);
  3229. int ch = get_amp_channels(kcontrol);
  3230. long *valp = ucontrol->value.integer.value;
  3231. /* store the left and right volume */
  3232. if (ch & 1) {
  3233. *valp = spec->vnode_lvol[nid - VNODE_START_NID];
  3234. valp++;
  3235. }
  3236. if (ch & 2) {
  3237. *valp = spec->vnode_rvol[nid - VNODE_START_NID];
  3238. valp++;
  3239. }
  3240. return 0;
  3241. }
  3242. static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
  3243. struct snd_ctl_elem_value *ucontrol)
  3244. {
  3245. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3246. struct ca0132_spec *spec = codec->spec;
  3247. hda_nid_t nid = get_amp_nid(kcontrol);
  3248. int ch = get_amp_channels(kcontrol);
  3249. long *valp = ucontrol->value.integer.value;
  3250. hda_nid_t shared_nid = 0;
  3251. bool effective;
  3252. int changed = 1;
  3253. /* store the left and right volume */
  3254. if (ch & 1) {
  3255. spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
  3256. valp++;
  3257. }
  3258. if (ch & 2) {
  3259. spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
  3260. valp++;
  3261. }
  3262. /* if effective conditions, then update hw immediately. */
  3263. effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
  3264. if (effective) {
  3265. int dir = get_amp_direction(kcontrol);
  3266. unsigned long pval;
  3267. snd_hda_power_up(codec);
  3268. mutex_lock(&codec->control_mutex);
  3269. pval = kcontrol->private_value;
  3270. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
  3271. 0, dir);
  3272. changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
  3273. kcontrol->private_value = pval;
  3274. mutex_unlock(&codec->control_mutex);
  3275. snd_hda_power_down(codec);
  3276. }
  3277. return changed;
  3278. }
  3279. static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  3280. unsigned int size, unsigned int __user *tlv)
  3281. {
  3282. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3283. struct ca0132_spec *spec = codec->spec;
  3284. hda_nid_t nid = get_amp_nid(kcontrol);
  3285. int ch = get_amp_channels(kcontrol);
  3286. int dir = get_amp_direction(kcontrol);
  3287. unsigned long pval;
  3288. int err;
  3289. switch (nid) {
  3290. case VNID_SPK:
  3291. /* follow shared_out tlv */
  3292. nid = spec->shared_out_nid;
  3293. mutex_lock(&codec->control_mutex);
  3294. pval = kcontrol->private_value;
  3295. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  3296. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  3297. kcontrol->private_value = pval;
  3298. mutex_unlock(&codec->control_mutex);
  3299. break;
  3300. case VNID_MIC:
  3301. /* follow shared_mic tlv */
  3302. nid = spec->shared_mic_nid;
  3303. mutex_lock(&codec->control_mutex);
  3304. pval = kcontrol->private_value;
  3305. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  3306. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  3307. kcontrol->private_value = pval;
  3308. mutex_unlock(&codec->control_mutex);
  3309. break;
  3310. default:
  3311. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  3312. }
  3313. return err;
  3314. }
  3315. static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
  3316. const char *pfx, int dir)
  3317. {
  3318. char namestr[44];
  3319. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  3320. struct snd_kcontrol_new knew =
  3321. CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
  3322. sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
  3323. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  3324. }
  3325. static int add_voicefx(struct hda_codec *codec)
  3326. {
  3327. struct snd_kcontrol_new knew =
  3328. HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
  3329. VOICEFX, 1, 0, HDA_INPUT);
  3330. knew.info = ca0132_voicefx_info;
  3331. knew.get = ca0132_voicefx_get;
  3332. knew.put = ca0132_voicefx_put;
  3333. return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
  3334. }
  3335. /*
  3336. * When changing Node IDs for Mixer Controls below, make sure to update
  3337. * Node IDs in ca0132_config() as well.
  3338. */
  3339. static struct snd_kcontrol_new ca0132_mixer[] = {
  3340. CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
  3341. CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
  3342. CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
  3343. CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
  3344. HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
  3345. HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
  3346. HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
  3347. HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
  3348. CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
  3349. 0x12, 1, HDA_INPUT),
  3350. CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
  3351. VNID_HP_SEL, 1, HDA_OUTPUT),
  3352. CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
  3353. VNID_AMIC1_SEL, 1, HDA_INPUT),
  3354. CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
  3355. VNID_HP_ASEL, 1, HDA_OUTPUT),
  3356. CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
  3357. VNID_AMIC1_ASEL, 1, HDA_INPUT),
  3358. { } /* end */
  3359. };
  3360. static int ca0132_build_controls(struct hda_codec *codec)
  3361. {
  3362. struct ca0132_spec *spec = codec->spec;
  3363. int i, num_fx;
  3364. int err = 0;
  3365. /* Add Mixer controls */
  3366. for (i = 0; i < spec->num_mixers; i++) {
  3367. err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
  3368. if (err < 0)
  3369. return err;
  3370. }
  3371. /* Add in and out effects controls.
  3372. * VoiceFX, PE and CrystalVoice are added separately.
  3373. */
  3374. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  3375. for (i = 0; i < num_fx; i++) {
  3376. err = add_fx_switch(codec, ca0132_effects[i].nid,
  3377. ca0132_effects[i].name,
  3378. ca0132_effects[i].direct);
  3379. if (err < 0)
  3380. return err;
  3381. }
  3382. err = add_fx_switch(codec, PLAY_ENHANCEMENT, "PlayEnhancement", 0);
  3383. if (err < 0)
  3384. return err;
  3385. err = add_fx_switch(codec, CRYSTAL_VOICE, "CrystalVoice", 1);
  3386. if (err < 0)
  3387. return err;
  3388. add_voicefx(codec);
  3389. #ifdef ENABLE_TUNING_CONTROLS
  3390. add_tuning_ctls(codec);
  3391. #endif
  3392. err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
  3393. if (err < 0)
  3394. return err;
  3395. if (spec->dig_out) {
  3396. err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
  3397. spec->dig_out);
  3398. if (err < 0)
  3399. return err;
  3400. err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
  3401. if (err < 0)
  3402. return err;
  3403. /* spec->multiout.share_spdif = 1; */
  3404. }
  3405. if (spec->dig_in) {
  3406. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  3407. if (err < 0)
  3408. return err;
  3409. }
  3410. return 0;
  3411. }
  3412. /*
  3413. * PCM
  3414. */
  3415. static struct hda_pcm_stream ca0132_pcm_analog_playback = {
  3416. .substreams = 1,
  3417. .channels_min = 2,
  3418. .channels_max = 6,
  3419. .ops = {
  3420. .prepare = ca0132_playback_pcm_prepare,
  3421. .cleanup = ca0132_playback_pcm_cleanup
  3422. },
  3423. };
  3424. static struct hda_pcm_stream ca0132_pcm_analog_capture = {
  3425. .substreams = 1,
  3426. .channels_min = 2,
  3427. .channels_max = 2,
  3428. .ops = {
  3429. .prepare = ca0132_capture_pcm_prepare,
  3430. .cleanup = ca0132_capture_pcm_cleanup
  3431. },
  3432. };
  3433. static struct hda_pcm_stream ca0132_pcm_digital_playback = {
  3434. .substreams = 1,
  3435. .channels_min = 2,
  3436. .channels_max = 2,
  3437. .ops = {
  3438. .open = ca0132_dig_playback_pcm_open,
  3439. .close = ca0132_dig_playback_pcm_close,
  3440. .prepare = ca0132_dig_playback_pcm_prepare,
  3441. .cleanup = ca0132_dig_playback_pcm_cleanup
  3442. },
  3443. };
  3444. static struct hda_pcm_stream ca0132_pcm_digital_capture = {
  3445. .substreams = 1,
  3446. .channels_min = 2,
  3447. .channels_max = 2,
  3448. };
  3449. static int ca0132_build_pcms(struct hda_codec *codec)
  3450. {
  3451. struct ca0132_spec *spec = codec->spec;
  3452. struct hda_pcm *info = spec->pcm_rec;
  3453. codec->pcm_info = info;
  3454. codec->num_pcms = 0;
  3455. info->name = "CA0132 Analog";
  3456. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
  3457. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
  3458. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  3459. spec->multiout.max_channels;
  3460. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  3461. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  3462. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
  3463. codec->num_pcms++;
  3464. info++;
  3465. info->name = "CA0132 Analog Mic-In2";
  3466. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  3467. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  3468. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
  3469. codec->num_pcms++;
  3470. info++;
  3471. info->name = "CA0132 What U Hear";
  3472. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  3473. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  3474. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
  3475. codec->num_pcms++;
  3476. if (!spec->dig_out && !spec->dig_in)
  3477. return 0;
  3478. info++;
  3479. info->name = "CA0132 Digital";
  3480. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  3481. if (spec->dig_out) {
  3482. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  3483. ca0132_pcm_digital_playback;
  3484. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
  3485. }
  3486. if (spec->dig_in) {
  3487. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  3488. ca0132_pcm_digital_capture;
  3489. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
  3490. }
  3491. codec->num_pcms++;
  3492. return 0;
  3493. }
  3494. static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
  3495. {
  3496. if (pin) {
  3497. snd_hda_set_pin_ctl(codec, pin, PIN_HP);
  3498. if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
  3499. snd_hda_codec_write(codec, pin, 0,
  3500. AC_VERB_SET_AMP_GAIN_MUTE,
  3501. AMP_OUT_UNMUTE);
  3502. }
  3503. if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
  3504. snd_hda_codec_write(codec, dac, 0,
  3505. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
  3506. }
  3507. static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
  3508. {
  3509. if (pin) {
  3510. snd_hda_set_pin_ctl(codec, pin, PIN_VREF80);
  3511. if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
  3512. snd_hda_codec_write(codec, pin, 0,
  3513. AC_VERB_SET_AMP_GAIN_MUTE,
  3514. AMP_IN_UNMUTE(0));
  3515. }
  3516. if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP)) {
  3517. snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  3518. AMP_IN_UNMUTE(0));
  3519. /* init to 0 dB and unmute. */
  3520. snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
  3521. HDA_AMP_VOLMASK, 0x5a);
  3522. snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
  3523. HDA_AMP_MUTE, 0);
  3524. }
  3525. }
  3526. static void ca0132_init_unsol(struct hda_codec *codec)
  3527. {
  3528. snd_hda_jack_detect_enable(codec, UNSOL_TAG_HP, UNSOL_TAG_HP);
  3529. snd_hda_jack_detect_enable(codec, UNSOL_TAG_AMIC1, UNSOL_TAG_AMIC1);
  3530. }
  3531. static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
  3532. {
  3533. unsigned int caps;
  3534. caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
  3535. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  3536. snd_hda_override_amp_caps(codec, nid, dir, caps);
  3537. }
  3538. /*
  3539. * Switch between Digital built-in mic and analog mic.
  3540. */
  3541. static void ca0132_set_dmic(struct hda_codec *codec, int enable)
  3542. {
  3543. struct ca0132_spec *spec = codec->spec;
  3544. unsigned int tmp;
  3545. u8 val;
  3546. unsigned int oldval;
  3547. snd_printdd(KERN_INFO "ca0132_set_dmic: enable=%d\n", enable);
  3548. oldval = stop_mic1(codec);
  3549. ca0132_set_vipsource(codec, 0);
  3550. if (enable) {
  3551. /* set DMic input as 2-ch */
  3552. tmp = FLOAT_TWO;
  3553. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3554. val = spec->dmic_ctl;
  3555. val |= 0x80;
  3556. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3557. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  3558. if (!(spec->dmic_ctl & 0x20))
  3559. chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
  3560. } else {
  3561. /* set AMic input as mono */
  3562. tmp = FLOAT_ONE;
  3563. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3564. val = spec->dmic_ctl;
  3565. /* clear bit7 and bit5 to disable dmic */
  3566. val &= 0x5f;
  3567. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3568. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  3569. if (!(spec->dmic_ctl & 0x20))
  3570. chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
  3571. }
  3572. ca0132_set_vipsource(codec, 1);
  3573. resume_mic1(codec, oldval);
  3574. }
  3575. /*
  3576. * Initialization for Digital Mic.
  3577. */
  3578. static void ca0132_init_dmic(struct hda_codec *codec)
  3579. {
  3580. struct ca0132_spec *spec = codec->spec;
  3581. u8 val;
  3582. /* Setup Digital Mic here, but don't enable.
  3583. * Enable based on jack detect.
  3584. */
  3585. /* MCLK uses MPIO1, set to enable.
  3586. * Bit 2-0: MPIO select
  3587. * Bit 3: set to disable
  3588. * Bit 7-4: reserved
  3589. */
  3590. val = 0x01;
  3591. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3592. VENDOR_CHIPIO_DMIC_MCLK_SET, val);
  3593. /* Data1 uses MPIO3. Data2 not use
  3594. * Bit 2-0: Data1 MPIO select
  3595. * Bit 3: set disable Data1
  3596. * Bit 6-4: Data2 MPIO select
  3597. * Bit 7: set disable Data2
  3598. */
  3599. val = 0x83;
  3600. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3601. VENDOR_CHIPIO_DMIC_PIN_SET, val);
  3602. /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
  3603. * Bit 3-0: Channel mask
  3604. * Bit 4: set for 48KHz, clear for 32KHz
  3605. * Bit 5: mode
  3606. * Bit 6: set to select Data2, clear for Data1
  3607. * Bit 7: set to enable DMic, clear for AMic
  3608. */
  3609. val = 0x23;
  3610. /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
  3611. spec->dmic_ctl = val;
  3612. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3613. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  3614. }
  3615. /*
  3616. * Initialization for Analog Mic 2
  3617. */
  3618. static void ca0132_init_analog_mic2(struct hda_codec *codec)
  3619. {
  3620. struct ca0132_spec *spec = codec->spec;
  3621. mutex_lock(&spec->chipio_mutex);
  3622. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3623. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
  3624. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3625. VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
  3626. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3627. VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
  3628. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3629. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x2D);
  3630. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3631. VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
  3632. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3633. VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
  3634. mutex_unlock(&spec->chipio_mutex);
  3635. }
  3636. static void ca0132_refresh_widget_caps(struct hda_codec *codec)
  3637. {
  3638. struct ca0132_spec *spec = codec->spec;
  3639. int i;
  3640. hda_nid_t nid;
  3641. snd_printdd(KERN_INFO "ca0132_refresh_widget_caps.\n");
  3642. nid = codec->start_nid;
  3643. for (i = 0; i < codec->num_nodes; i++, nid++)
  3644. codec->wcaps[i] = snd_hda_param_read(codec, nid,
  3645. AC_PAR_AUDIO_WIDGET_CAP);
  3646. for (i = 0; i < spec->multiout.num_dacs; i++)
  3647. refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
  3648. for (i = 0; i < spec->num_outputs; i++)
  3649. refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
  3650. for (i = 0; i < spec->num_inputs; i++) {
  3651. refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
  3652. refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
  3653. }
  3654. }
  3655. /*
  3656. * Setup default parameters for DSP
  3657. */
  3658. static void ca0132_setup_defaults(struct hda_codec *codec)
  3659. {
  3660. struct ca0132_spec *spec = codec->spec;
  3661. unsigned int tmp;
  3662. int num_fx;
  3663. int idx, i;
  3664. if (spec->dsp_state != DSP_DOWNLOADED)
  3665. return;
  3666. /* out, in effects + voicefx */
  3667. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
  3668. for (idx = 0; idx < num_fx; idx++) {
  3669. for (i = 0; i <= ca0132_effects[idx].params; i++) {
  3670. dspio_set_uint_param(codec, ca0132_effects[idx].mid,
  3671. ca0132_effects[idx].reqs[i],
  3672. ca0132_effects[idx].def_vals[i]);
  3673. }
  3674. }
  3675. /*remove DSP headroom*/
  3676. tmp = FLOAT_ZERO;
  3677. dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
  3678. /*set speaker EQ bypass attenuation*/
  3679. dspio_set_uint_param(codec, 0x8f, 0x01, tmp);
  3680. /* set AMic1 and AMic2 as mono mic */
  3681. tmp = FLOAT_ONE;
  3682. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3683. dspio_set_uint_param(codec, 0x80, 0x01, tmp);
  3684. /* set AMic1 as CrystalVoice input */
  3685. tmp = FLOAT_ONE;
  3686. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  3687. /* set WUH source */
  3688. tmp = FLOAT_TWO;
  3689. dspio_set_uint_param(codec, 0x31, 0x00, tmp);
  3690. }
  3691. /*
  3692. * Initialization of flags in chip
  3693. */
  3694. static void ca0132_init_flags(struct hda_codec *codec)
  3695. {
  3696. chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
  3697. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
  3698. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
  3699. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
  3700. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
  3701. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
  3702. }
  3703. /*
  3704. * Initialization of parameters in chip
  3705. */
  3706. static void ca0132_init_params(struct hda_codec *codec)
  3707. {
  3708. chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
  3709. chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
  3710. }
  3711. static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
  3712. {
  3713. chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
  3714. chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
  3715. chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
  3716. chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
  3717. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
  3718. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
  3719. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  3720. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  3721. chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
  3722. }
  3723. static bool ca0132_download_dsp_images(struct hda_codec *codec)
  3724. {
  3725. bool dsp_loaded = false;
  3726. const struct dsp_image_seg *dsp_os_image;
  3727. const struct firmware *fw_entry;
  3728. if (request_firmware(&fw_entry, EFX_FILE, codec->bus->card->dev) != 0)
  3729. return false;
  3730. dsp_os_image = (struct dsp_image_seg *)(fw_entry->data);
  3731. if (dspload_image(codec, dsp_os_image, 0, 0, true, 0)) {
  3732. pr_err("ca0132 dspload_image failed.\n");
  3733. goto exit_download;
  3734. }
  3735. dsp_loaded = dspload_wait_loaded(codec);
  3736. exit_download:
  3737. release_firmware(fw_entry);
  3738. return dsp_loaded;
  3739. }
  3740. static void ca0132_download_dsp(struct hda_codec *codec)
  3741. {
  3742. struct ca0132_spec *spec = codec->spec;
  3743. #ifndef CONFIG_SND_HDA_CODEC_CA0132_DSP
  3744. return; /* NOP */
  3745. #endif
  3746. chipio_enable_clocks(codec);
  3747. spec->dsp_state = DSP_DOWNLOADING;
  3748. if (!ca0132_download_dsp_images(codec))
  3749. spec->dsp_state = DSP_DOWNLOAD_FAILED;
  3750. else
  3751. spec->dsp_state = DSP_DOWNLOADED;
  3752. if (spec->dsp_state == DSP_DOWNLOADED)
  3753. ca0132_set_dsp_msr(codec, true);
  3754. }
  3755. static void ca0132_process_dsp_response(struct hda_codec *codec)
  3756. {
  3757. struct ca0132_spec *spec = codec->spec;
  3758. snd_printdd(KERN_INFO "ca0132_process_dsp_response\n");
  3759. if (spec->wait_scp) {
  3760. if (dspio_get_response_data(codec) >= 0)
  3761. spec->wait_scp = 0;
  3762. }
  3763. dspio_clear_response_queue(codec);
  3764. }
  3765. static void ca0132_unsol_event(struct hda_codec *codec, unsigned int res)
  3766. {
  3767. snd_printdd(KERN_INFO "ca0132_unsol_event: 0x%x\n", res);
  3768. if (((res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f) == UNSOL_TAG_DSP) {
  3769. ca0132_process_dsp_response(codec);
  3770. } else {
  3771. res = snd_hda_jack_get_action(codec,
  3772. (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f);
  3773. snd_printdd(KERN_INFO "snd_hda_jack_get_action: 0x%x\n", res);
  3774. switch (res) {
  3775. case UNSOL_TAG_HP:
  3776. ca0132_select_out(codec);
  3777. snd_hda_jack_report_sync(codec);
  3778. break;
  3779. case UNSOL_TAG_AMIC1:
  3780. ca0132_select_mic(codec);
  3781. snd_hda_jack_report_sync(codec);
  3782. break;
  3783. default:
  3784. break;
  3785. }
  3786. }
  3787. }
  3788. /*
  3789. * Verbs tables.
  3790. */
  3791. /* Sends before DSP download. */
  3792. static struct hda_verb ca0132_base_init_verbs[] = {
  3793. /*enable ct extension*/
  3794. {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
  3795. /*enable DSP node unsol, needed for DSP download*/
  3796. {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_DSP},
  3797. {}
  3798. };
  3799. /* Send at exit. */
  3800. static struct hda_verb ca0132_base_exit_verbs[] = {
  3801. /*set afg to D3*/
  3802. {0x01, AC_VERB_SET_POWER_STATE, 0x03},
  3803. /*disable ct extension*/
  3804. {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
  3805. {}
  3806. };
  3807. /* Other verbs tables. Sends after DSP download. */
  3808. static struct hda_verb ca0132_init_verbs0[] = {
  3809. /* chip init verbs */
  3810. {0x15, 0x70D, 0xF0},
  3811. {0x15, 0x70E, 0xFE},
  3812. {0x15, 0x707, 0x75},
  3813. {0x15, 0x707, 0xD3},
  3814. {0x15, 0x707, 0x09},
  3815. {0x15, 0x707, 0x53},
  3816. {0x15, 0x707, 0xD4},
  3817. {0x15, 0x707, 0xEF},
  3818. {0x15, 0x707, 0x75},
  3819. {0x15, 0x707, 0xD3},
  3820. {0x15, 0x707, 0x09},
  3821. {0x15, 0x707, 0x02},
  3822. {0x15, 0x707, 0x37},
  3823. {0x15, 0x707, 0x78},
  3824. {0x15, 0x53C, 0xCE},
  3825. {0x15, 0x575, 0xC9},
  3826. {0x15, 0x53D, 0xCE},
  3827. {0x15, 0x5B7, 0xC9},
  3828. {0x15, 0x70D, 0xE8},
  3829. {0x15, 0x70E, 0xFE},
  3830. {0x15, 0x707, 0x02},
  3831. {0x15, 0x707, 0x68},
  3832. {0x15, 0x707, 0x62},
  3833. {0x15, 0x53A, 0xCE},
  3834. {0x15, 0x546, 0xC9},
  3835. {0x15, 0x53B, 0xCE},
  3836. {0x15, 0x5E8, 0xC9},
  3837. {0x15, 0x717, 0x0D},
  3838. {0x15, 0x718, 0x20},
  3839. {}
  3840. };
  3841. static struct hda_verb ca0132_init_verbs1[] = {
  3842. {0x10, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_HP},
  3843. {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_AMIC1},
  3844. /* config EAPD */
  3845. {0x0b, 0x78D, 0x00},
  3846. /*{0x0b, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
  3847. /*{0x10, 0x78D, 0x02},*/
  3848. /*{0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
  3849. {}
  3850. };
  3851. static void ca0132_init_chip(struct hda_codec *codec)
  3852. {
  3853. struct ca0132_spec *spec = codec->spec;
  3854. int num_fx;
  3855. int i;
  3856. unsigned int on;
  3857. mutex_init(&spec->chipio_mutex);
  3858. spec->cur_out_type = SPEAKER_OUT;
  3859. spec->cur_mic_type = DIGITAL_MIC;
  3860. spec->cur_mic_boost = 0;
  3861. for (i = 0; i < VNODES_COUNT; i++) {
  3862. spec->vnode_lvol[i] = 0x5a;
  3863. spec->vnode_rvol[i] = 0x5a;
  3864. spec->vnode_lswitch[i] = 0;
  3865. spec->vnode_rswitch[i] = 0;
  3866. }
  3867. /*
  3868. * Default states for effects are in ca0132_effects[].
  3869. */
  3870. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  3871. for (i = 0; i < num_fx; i++) {
  3872. on = (unsigned int)ca0132_effects[i].reqs[0];
  3873. spec->effects_switch[i] = on ? 1 : 0;
  3874. }
  3875. spec->voicefx_val = 0;
  3876. spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
  3877. spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
  3878. #ifdef ENABLE_TUNING_CONTROLS
  3879. ca0132_init_tuning_defaults(codec);
  3880. #endif
  3881. }
  3882. static void ca0132_exit_chip(struct hda_codec *codec)
  3883. {
  3884. /* put any chip cleanup stuffs here. */
  3885. if (dspload_is_loaded(codec))
  3886. dsp_reset(codec);
  3887. }
  3888. static int ca0132_init(struct hda_codec *codec)
  3889. {
  3890. struct ca0132_spec *spec = codec->spec;
  3891. struct auto_pin_cfg *cfg = &spec->autocfg;
  3892. int i;
  3893. spec->dsp_state = DSP_DOWNLOAD_INIT;
  3894. spec->curr_chip_addx = INVALID_CHIP_ADDRESS;
  3895. snd_hda_power_up(codec);
  3896. ca0132_init_params(codec);
  3897. ca0132_init_flags(codec);
  3898. snd_hda_sequence_write(codec, spec->base_init_verbs);
  3899. ca0132_download_dsp(codec);
  3900. ca0132_refresh_widget_caps(codec);
  3901. ca0132_setup_defaults(codec);
  3902. ca0132_init_analog_mic2(codec);
  3903. ca0132_init_dmic(codec);
  3904. for (i = 0; i < spec->num_outputs; i++)
  3905. init_output(codec, spec->out_pins[i], spec->dacs[0]);
  3906. init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
  3907. for (i = 0; i < spec->num_inputs; i++)
  3908. init_input(codec, spec->input_pins[i], spec->adcs[i]);
  3909. init_input(codec, cfg->dig_in_pin, spec->dig_in);
  3910. for (i = 0; i < spec->num_init_verbs; i++)
  3911. snd_hda_sequence_write(codec, spec->init_verbs[i]);
  3912. ca0132_init_unsol(codec);
  3913. ca0132_select_out(codec);
  3914. ca0132_select_mic(codec);
  3915. snd_hda_jack_report_sync(codec);
  3916. snd_hda_power_down(codec);
  3917. return 0;
  3918. }
  3919. static void ca0132_free(struct hda_codec *codec)
  3920. {
  3921. struct ca0132_spec *spec = codec->spec;
  3922. snd_hda_power_up(codec);
  3923. snd_hda_sequence_write(codec, spec->base_exit_verbs);
  3924. ca0132_exit_chip(codec);
  3925. snd_hda_power_down(codec);
  3926. kfree(codec->spec);
  3927. }
  3928. static struct hda_codec_ops ca0132_patch_ops = {
  3929. .build_controls = ca0132_build_controls,
  3930. .build_pcms = ca0132_build_pcms,
  3931. .init = ca0132_init,
  3932. .free = ca0132_free,
  3933. .unsol_event = ca0132_unsol_event,
  3934. };
  3935. static void ca0132_config(struct hda_codec *codec)
  3936. {
  3937. struct ca0132_spec *spec = codec->spec;
  3938. struct auto_pin_cfg *cfg = &spec->autocfg;
  3939. spec->dacs[0] = 0x2;
  3940. spec->dacs[1] = 0x3;
  3941. spec->dacs[2] = 0x4;
  3942. spec->multiout.dac_nids = spec->dacs;
  3943. spec->multiout.num_dacs = 3;
  3944. spec->multiout.max_channels = 2;
  3945. spec->num_outputs = 2;
  3946. spec->out_pins[0] = 0x0b; /* speaker out */
  3947. spec->out_pins[1] = 0x10; /* headphone out */
  3948. spec->shared_out_nid = 0x2;
  3949. spec->num_inputs = 3;
  3950. spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
  3951. spec->adcs[1] = 0x8; /* analog mic2 */
  3952. spec->adcs[2] = 0xa; /* what u hear */
  3953. spec->shared_mic_nid = 0x7;
  3954. spec->input_pins[0] = 0x12;
  3955. spec->input_pins[1] = 0x11;
  3956. spec->input_pins[2] = 0x13;
  3957. /* SPDIF I/O */
  3958. spec->dig_out = 0x05;
  3959. spec->multiout.dig_out_nid = spec->dig_out;
  3960. cfg->dig_out_pins[0] = 0x0c;
  3961. cfg->dig_outs = 1;
  3962. cfg->dig_out_type[0] = HDA_PCM_TYPE_SPDIF;
  3963. spec->dig_in = 0x09;
  3964. cfg->dig_in_pin = 0x0e;
  3965. cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
  3966. }
  3967. static int patch_ca0132(struct hda_codec *codec)
  3968. {
  3969. struct ca0132_spec *spec;
  3970. int err;
  3971. snd_printdd("patch_ca0132\n");
  3972. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  3973. if (!spec)
  3974. return -ENOMEM;
  3975. codec->spec = spec;
  3976. spec->num_mixers = 1;
  3977. spec->mixers[0] = ca0132_mixer;
  3978. spec->base_init_verbs = ca0132_base_init_verbs;
  3979. spec->base_exit_verbs = ca0132_base_exit_verbs;
  3980. spec->init_verbs[0] = ca0132_init_verbs0;
  3981. spec->init_verbs[1] = ca0132_init_verbs1;
  3982. spec->num_init_verbs = 2;
  3983. ca0132_init_chip(codec);
  3984. ca0132_config(codec);
  3985. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  3986. if (err < 0)
  3987. return err;
  3988. codec->patch_ops = ca0132_patch_ops;
  3989. return 0;
  3990. }
  3991. /*
  3992. * patch entries
  3993. */
  3994. static struct hda_codec_preset snd_hda_preset_ca0132[] = {
  3995. { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
  3996. {} /* terminator */
  3997. };
  3998. MODULE_ALIAS("snd-hda-codec-id:11020011");
  3999. MODULE_LICENSE("GPL");
  4000. MODULE_DESCRIPTION("Creative Sound Core3D codec");
  4001. static struct hda_codec_preset_list ca0132_list = {
  4002. .preset = snd_hda_preset_ca0132,
  4003. .owner = THIS_MODULE,
  4004. };
  4005. static int __init patch_ca0132_init(void)
  4006. {
  4007. return snd_hda_add_codec_preset(&ca0132_list);
  4008. }
  4009. static void __exit patch_ca0132_exit(void)
  4010. {
  4011. snd_hda_delete_codec_preset(&ca0132_list);
  4012. }
  4013. module_init(patch_ca0132_init)
  4014. module_exit(patch_ca0132_exit)