patch_ca0132.c 122 KB

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