patch_ca0132.c 120 KB

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