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. MODULE_FIRMWARE(EFX_FILE);
  66. static char *dirstr[2] = { "Playback", "Capture" };
  67. enum {
  68. SPEAKER_OUT,
  69. HEADPHONE_OUT
  70. };
  71. enum {
  72. DIGITAL_MIC,
  73. LINE_MIC_IN
  74. };
  75. enum {
  76. #define VNODE_START_NID 0x80
  77. VNID_SPK = VNODE_START_NID, /* Speaker vnid */
  78. VNID_MIC,
  79. VNID_HP_SEL,
  80. VNID_AMIC1_SEL,
  81. VNID_HP_ASEL,
  82. VNID_AMIC1_ASEL,
  83. VNODE_END_NID,
  84. #define VNODES_COUNT (VNODE_END_NID - VNODE_START_NID)
  85. #define EFFECT_START_NID 0x90
  86. #define OUT_EFFECT_START_NID EFFECT_START_NID
  87. SURROUND = OUT_EFFECT_START_NID,
  88. CRYSTALIZER,
  89. DIALOG_PLUS,
  90. SMART_VOLUME,
  91. X_BASS,
  92. EQUALIZER,
  93. OUT_EFFECT_END_NID,
  94. #define OUT_EFFECTS_COUNT (OUT_EFFECT_END_NID - OUT_EFFECT_START_NID)
  95. #define IN_EFFECT_START_NID OUT_EFFECT_END_NID
  96. ECHO_CANCELLATION = IN_EFFECT_START_NID,
  97. VOICE_FOCUS,
  98. MIC_SVM,
  99. NOISE_REDUCTION,
  100. IN_EFFECT_END_NID,
  101. #define IN_EFFECTS_COUNT (IN_EFFECT_END_NID - IN_EFFECT_START_NID)
  102. VOICEFX = IN_EFFECT_END_NID,
  103. PLAY_ENHANCEMENT,
  104. CRYSTAL_VOICE,
  105. EFFECT_END_NID
  106. #define EFFECTS_COUNT (EFFECT_END_NID - EFFECT_START_NID)
  107. };
  108. /* Effects values size*/
  109. #define EFFECT_VALS_MAX_COUNT 12
  110. struct ct_effect {
  111. char name[44];
  112. hda_nid_t nid;
  113. int mid; /*effect module ID*/
  114. int reqs[EFFECT_VALS_MAX_COUNT]; /*effect module request*/
  115. int direct; /* 0:output; 1:input*/
  116. int params; /* number of default non-on/off params */
  117. /*effect default values, 1st is on/off. */
  118. unsigned int def_vals[EFFECT_VALS_MAX_COUNT];
  119. };
  120. #define EFX_DIR_OUT 0
  121. #define EFX_DIR_IN 1
  122. static struct ct_effect ca0132_effects[EFFECTS_COUNT] = {
  123. { .name = "Surround",
  124. .nid = SURROUND,
  125. .mid = 0x96,
  126. .reqs = {0, 1},
  127. .direct = EFX_DIR_OUT,
  128. .params = 1,
  129. .def_vals = {0x3F800000, 0x3F2B851F}
  130. },
  131. { .name = "Crystalizer",
  132. .nid = CRYSTALIZER,
  133. .mid = 0x96,
  134. .reqs = {7, 8},
  135. .direct = EFX_DIR_OUT,
  136. .params = 1,
  137. .def_vals = {0x3F800000, 0x3F266666}
  138. },
  139. { .name = "Dialog Plus",
  140. .nid = DIALOG_PLUS,
  141. .mid = 0x96,
  142. .reqs = {2, 3},
  143. .direct = EFX_DIR_OUT,
  144. .params = 1,
  145. .def_vals = {0x00000000, 0x3F000000}
  146. },
  147. { .name = "Smart Volume",
  148. .nid = SMART_VOLUME,
  149. .mid = 0x96,
  150. .reqs = {4, 5, 6},
  151. .direct = EFX_DIR_OUT,
  152. .params = 2,
  153. .def_vals = {0x3F800000, 0x3F3D70A4, 0x00000000}
  154. },
  155. { .name = "X-Bass",
  156. .nid = X_BASS,
  157. .mid = 0x96,
  158. .reqs = {24, 23, 25},
  159. .direct = EFX_DIR_OUT,
  160. .params = 2,
  161. .def_vals = {0x3F800000, 0x42A00000, 0x3F000000}
  162. },
  163. { .name = "Equalizer",
  164. .nid = EQUALIZER,
  165. .mid = 0x96,
  166. .reqs = {9, 10, 11, 12, 13, 14,
  167. 15, 16, 17, 18, 19, 20},
  168. .direct = EFX_DIR_OUT,
  169. .params = 11,
  170. .def_vals = {0x00000000, 0x00000000, 0x00000000, 0x00000000,
  171. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  172. 0x00000000, 0x00000000, 0x00000000, 0x00000000}
  173. },
  174. { .name = "Echo Cancellation",
  175. .nid = ECHO_CANCELLATION,
  176. .mid = 0x95,
  177. .reqs = {0, 1, 2, 3},
  178. .direct = EFX_DIR_IN,
  179. .params = 3,
  180. .def_vals = {0x00000000, 0x3F3A9692, 0x00000000, 0x00000000}
  181. },
  182. { .name = "Voice Focus",
  183. .nid = VOICE_FOCUS,
  184. .mid = 0x95,
  185. .reqs = {6, 7, 8, 9},
  186. .direct = EFX_DIR_IN,
  187. .params = 3,
  188. .def_vals = {0x3F800000, 0x3D7DF3B6, 0x41F00000, 0x41F00000}
  189. },
  190. { .name = "Mic SVM",
  191. .nid = MIC_SVM,
  192. .mid = 0x95,
  193. .reqs = {44, 45},
  194. .direct = EFX_DIR_IN,
  195. .params = 1,
  196. .def_vals = {0x00000000, 0x3F3D70A4}
  197. },
  198. { .name = "Noise Reduction",
  199. .nid = NOISE_REDUCTION,
  200. .mid = 0x95,
  201. .reqs = {4, 5},
  202. .direct = EFX_DIR_IN,
  203. .params = 1,
  204. .def_vals = {0x3F800000, 0x3F000000}
  205. },
  206. { .name = "VoiceFX",
  207. .nid = VOICEFX,
  208. .mid = 0x95,
  209. .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18},
  210. .direct = EFX_DIR_IN,
  211. .params = 8,
  212. .def_vals = {0x00000000, 0x43C80000, 0x44AF0000, 0x44FA0000,
  213. 0x3F800000, 0x3F800000, 0x3F800000, 0x00000000,
  214. 0x00000000}
  215. }
  216. };
  217. /* Tuning controls */
  218. #ifdef ENABLE_TUNING_CONTROLS
  219. enum {
  220. #define TUNING_CTL_START_NID 0xC0
  221. WEDGE_ANGLE = TUNING_CTL_START_NID,
  222. SVM_LEVEL,
  223. EQUALIZER_BAND_0,
  224. EQUALIZER_BAND_1,
  225. EQUALIZER_BAND_2,
  226. EQUALIZER_BAND_3,
  227. EQUALIZER_BAND_4,
  228. EQUALIZER_BAND_5,
  229. EQUALIZER_BAND_6,
  230. EQUALIZER_BAND_7,
  231. EQUALIZER_BAND_8,
  232. EQUALIZER_BAND_9,
  233. TUNING_CTL_END_NID
  234. #define TUNING_CTLS_COUNT (TUNING_CTL_END_NID - TUNING_CTL_START_NID)
  235. };
  236. struct ct_tuning_ctl {
  237. char name[44];
  238. hda_nid_t parent_nid;
  239. hda_nid_t nid;
  240. int mid; /*effect module ID*/
  241. int req; /*effect module request*/
  242. int direct; /* 0:output; 1:input*/
  243. unsigned int def_val;/*effect default values*/
  244. };
  245. static struct ct_tuning_ctl ca0132_tuning_ctls[] = {
  246. { .name = "Wedge Angle",
  247. .parent_nid = VOICE_FOCUS,
  248. .nid = WEDGE_ANGLE,
  249. .mid = 0x95,
  250. .req = 8,
  251. .direct = EFX_DIR_IN,
  252. .def_val = 0x41F00000
  253. },
  254. { .name = "SVM Level",
  255. .parent_nid = MIC_SVM,
  256. .nid = SVM_LEVEL,
  257. .mid = 0x95,
  258. .req = 45,
  259. .direct = EFX_DIR_IN,
  260. .def_val = 0x3F3D70A4
  261. },
  262. { .name = "EQ Band0",
  263. .parent_nid = EQUALIZER,
  264. .nid = EQUALIZER_BAND_0,
  265. .mid = 0x96,
  266. .req = 11,
  267. .direct = EFX_DIR_OUT,
  268. .def_val = 0x00000000
  269. },
  270. { .name = "EQ Band1",
  271. .parent_nid = EQUALIZER,
  272. .nid = EQUALIZER_BAND_1,
  273. .mid = 0x96,
  274. .req = 12,
  275. .direct = EFX_DIR_OUT,
  276. .def_val = 0x00000000
  277. },
  278. { .name = "EQ Band2",
  279. .parent_nid = EQUALIZER,
  280. .nid = EQUALIZER_BAND_2,
  281. .mid = 0x96,
  282. .req = 13,
  283. .direct = EFX_DIR_OUT,
  284. .def_val = 0x00000000
  285. },
  286. { .name = "EQ Band3",
  287. .parent_nid = EQUALIZER,
  288. .nid = EQUALIZER_BAND_3,
  289. .mid = 0x96,
  290. .req = 14,
  291. .direct = EFX_DIR_OUT,
  292. .def_val = 0x00000000
  293. },
  294. { .name = "EQ Band4",
  295. .parent_nid = EQUALIZER,
  296. .nid = EQUALIZER_BAND_4,
  297. .mid = 0x96,
  298. .req = 15,
  299. .direct = EFX_DIR_OUT,
  300. .def_val = 0x00000000
  301. },
  302. { .name = "EQ Band5",
  303. .parent_nid = EQUALIZER,
  304. .nid = EQUALIZER_BAND_5,
  305. .mid = 0x96,
  306. .req = 16,
  307. .direct = EFX_DIR_OUT,
  308. .def_val = 0x00000000
  309. },
  310. { .name = "EQ Band6",
  311. .parent_nid = EQUALIZER,
  312. .nid = EQUALIZER_BAND_6,
  313. .mid = 0x96,
  314. .req = 17,
  315. .direct = EFX_DIR_OUT,
  316. .def_val = 0x00000000
  317. },
  318. { .name = "EQ Band7",
  319. .parent_nid = EQUALIZER,
  320. .nid = EQUALIZER_BAND_7,
  321. .mid = 0x96,
  322. .req = 18,
  323. .direct = EFX_DIR_OUT,
  324. .def_val = 0x00000000
  325. },
  326. { .name = "EQ Band8",
  327. .parent_nid = EQUALIZER,
  328. .nid = EQUALIZER_BAND_8,
  329. .mid = 0x96,
  330. .req = 19,
  331. .direct = EFX_DIR_OUT,
  332. .def_val = 0x00000000
  333. },
  334. { .name = "EQ Band9",
  335. .parent_nid = EQUALIZER,
  336. .nid = EQUALIZER_BAND_9,
  337. .mid = 0x96,
  338. .req = 20,
  339. .direct = EFX_DIR_OUT,
  340. .def_val = 0x00000000
  341. }
  342. };
  343. #endif
  344. /* Voice FX Presets */
  345. #define VOICEFX_MAX_PARAM_COUNT 9
  346. struct ct_voicefx {
  347. char *name;
  348. hda_nid_t nid;
  349. int mid;
  350. int reqs[VOICEFX_MAX_PARAM_COUNT]; /*effect module request*/
  351. };
  352. struct ct_voicefx_preset {
  353. char *name; /*preset name*/
  354. unsigned int vals[VOICEFX_MAX_PARAM_COUNT];
  355. };
  356. struct ct_voicefx ca0132_voicefx = {
  357. .name = "VoiceFX Capture Switch",
  358. .nid = VOICEFX,
  359. .mid = 0x95,
  360. .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18}
  361. };
  362. struct ct_voicefx_preset ca0132_voicefx_presets[] = {
  363. { .name = "Neutral",
  364. .vals = { 0x00000000, 0x43C80000, 0x44AF0000,
  365. 0x44FA0000, 0x3F800000, 0x3F800000,
  366. 0x3F800000, 0x00000000, 0x00000000 }
  367. },
  368. { .name = "Female2Male",
  369. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  370. 0x44FA0000, 0x3F19999A, 0x3F866666,
  371. 0x3F800000, 0x00000000, 0x00000000 }
  372. },
  373. { .name = "Male2Female",
  374. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  375. 0x450AC000, 0x4017AE14, 0x3F6B851F,
  376. 0x3F800000, 0x00000000, 0x00000000 }
  377. },
  378. { .name = "ScrappyKid",
  379. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  380. 0x44FA0000, 0x40400000, 0x3F28F5C3,
  381. 0x3F800000, 0x00000000, 0x00000000 }
  382. },
  383. { .name = "Elderly",
  384. .vals = { 0x3F800000, 0x44324000, 0x44BB8000,
  385. 0x44E10000, 0x3FB33333, 0x3FB9999A,
  386. 0x3F800000, 0x3E3A2E43, 0x00000000 }
  387. },
  388. { .name = "Orc",
  389. .vals = { 0x3F800000, 0x43EA0000, 0x44A52000,
  390. 0x45098000, 0x3F266666, 0x3FC00000,
  391. 0x3F800000, 0x00000000, 0x00000000 }
  392. },
  393. { .name = "Elf",
  394. .vals = { 0x3F800000, 0x43C70000, 0x44AE6000,
  395. 0x45193000, 0x3F8E147B, 0x3F75C28F,
  396. 0x3F800000, 0x00000000, 0x00000000 }
  397. },
  398. { .name = "Dwarf",
  399. .vals = { 0x3F800000, 0x43930000, 0x44BEE000,
  400. 0x45007000, 0x3F451EB8, 0x3F7851EC,
  401. 0x3F800000, 0x00000000, 0x00000000 }
  402. },
  403. { .name = "AlienBrute",
  404. .vals = { 0x3F800000, 0x43BFC5AC, 0x44B28FDF,
  405. 0x451F6000, 0x3F266666, 0x3FA7D945,
  406. 0x3F800000, 0x3CF5C28F, 0x00000000 }
  407. },
  408. { .name = "Robot",
  409. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  410. 0x44FA0000, 0x3FB2718B, 0x3F800000,
  411. 0xBC07010E, 0x00000000, 0x00000000 }
  412. },
  413. { .name = "Marine",
  414. .vals = { 0x3F800000, 0x43C20000, 0x44906000,
  415. 0x44E70000, 0x3F4CCCCD, 0x3F8A3D71,
  416. 0x3F0A3D71, 0x00000000, 0x00000000 }
  417. },
  418. { .name = "Emo",
  419. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  420. 0x44FA0000, 0x3F800000, 0x3F800000,
  421. 0x3E4CCCCD, 0x00000000, 0x00000000 }
  422. },
  423. { .name = "DeepVoice",
  424. .vals = { 0x3F800000, 0x43A9C5AC, 0x44AA4FDF,
  425. 0x44FFC000, 0x3EDBB56F, 0x3F99C4CA,
  426. 0x3F800000, 0x00000000, 0x00000000 }
  427. },
  428. { .name = "Munchkin",
  429. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  430. 0x44FA0000, 0x3F800000, 0x3F1A043C,
  431. 0x3F800000, 0x00000000, 0x00000000 }
  432. }
  433. };
  434. enum hda_cmd_vendor_io {
  435. /* for DspIO node */
  436. VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
  437. VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
  438. VENDOR_DSPIO_STATUS = 0xF01,
  439. VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
  440. VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
  441. VENDOR_DSPIO_DSP_INIT = 0x703,
  442. VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
  443. VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
  444. /* for ChipIO node */
  445. VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
  446. VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
  447. VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
  448. VENDOR_CHIPIO_DATA_LOW = 0x300,
  449. VENDOR_CHIPIO_DATA_HIGH = 0x400,
  450. VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
  451. VENDOR_CHIPIO_STATUS = 0xF01,
  452. VENDOR_CHIPIO_HIC_POST_READ = 0x702,
  453. VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
  454. VENDOR_CHIPIO_8051_DATA_WRITE = 0x707,
  455. VENDOR_CHIPIO_8051_DATA_READ = 0xF07,
  456. VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
  457. VENDOR_CHIPIO_CT_EXTENSIONS_GET = 0xF0A,
  458. VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
  459. VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
  460. VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
  461. VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
  462. VENDOR_CHIPIO_FLAG_SET = 0x70F,
  463. VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
  464. VENDOR_CHIPIO_PARAM_SET = 0x710,
  465. VENDOR_CHIPIO_PARAM_GET = 0xF10,
  466. VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
  467. VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
  468. VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
  469. VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
  470. VENDOR_CHIPIO_PARAM_EX_ID_GET = 0xF17,
  471. VENDOR_CHIPIO_PARAM_EX_ID_SET = 0x717,
  472. VENDOR_CHIPIO_PARAM_EX_VALUE_GET = 0xF18,
  473. VENDOR_CHIPIO_PARAM_EX_VALUE_SET = 0x718,
  474. VENDOR_CHIPIO_DMIC_CTL_SET = 0x788,
  475. VENDOR_CHIPIO_DMIC_CTL_GET = 0xF88,
  476. VENDOR_CHIPIO_DMIC_PIN_SET = 0x789,
  477. VENDOR_CHIPIO_DMIC_PIN_GET = 0xF89,
  478. VENDOR_CHIPIO_DMIC_MCLK_SET = 0x78A,
  479. VENDOR_CHIPIO_DMIC_MCLK_GET = 0xF8A,
  480. VENDOR_CHIPIO_EAPD_SEL_SET = 0x78D
  481. };
  482. /*
  483. * Control flag IDs
  484. */
  485. enum control_flag_id {
  486. /* Connection manager stream setup is bypassed/enabled */
  487. CONTROL_FLAG_C_MGR = 0,
  488. /* DSP DMA is bypassed/enabled */
  489. CONTROL_FLAG_DMA = 1,
  490. /* 8051 'idle' mode is disabled/enabled */
  491. CONTROL_FLAG_IDLE_ENABLE = 2,
  492. /* Tracker for the SPDIF-in path is bypassed/enabled */
  493. CONTROL_FLAG_TRACKER = 3,
  494. /* DigitalOut to Spdif2Out connection is disabled/enabled */
  495. CONTROL_FLAG_SPDIF2OUT = 4,
  496. /* Digital Microphone is disabled/enabled */
  497. CONTROL_FLAG_DMIC = 5,
  498. /* ADC_B rate is 48 kHz/96 kHz */
  499. CONTROL_FLAG_ADC_B_96KHZ = 6,
  500. /* ADC_C rate is 48 kHz/96 kHz */
  501. CONTROL_FLAG_ADC_C_96KHZ = 7,
  502. /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
  503. CONTROL_FLAG_DAC_96KHZ = 8,
  504. /* DSP rate is 48 kHz/96 kHz */
  505. CONTROL_FLAG_DSP_96KHZ = 9,
  506. /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
  507. CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
  508. /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
  509. CONTROL_FLAG_SRC_RATE_96KHZ = 11,
  510. /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
  511. CONTROL_FLAG_DECODE_LOOP = 12,
  512. /* De-emphasis filter on DAC-1 disabled/enabled */
  513. CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
  514. /* De-emphasis filter on DAC-2 disabled/enabled */
  515. CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
  516. /* De-emphasis filter on DAC-3 disabled/enabled */
  517. CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
  518. /* High-pass filter on ADC_B disabled/enabled */
  519. CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
  520. /* High-pass filter on ADC_C disabled/enabled */
  521. CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
  522. /* Common mode on Port_A disabled/enabled */
  523. CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
  524. /* Common mode on Port_D disabled/enabled */
  525. CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
  526. /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
  527. CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
  528. /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
  529. CONTROL_FLAG_PORT_D_10KOHM_LOAD = 21,
  530. /* ASI rate is 48kHz/96kHz */
  531. CONTROL_FLAG_ASI_96KHZ = 22,
  532. /* DAC power settings able to control attached ports no/yes */
  533. CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
  534. /* Clock Stop OK reporting is disabled/enabled */
  535. CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
  536. /* Number of control flags */
  537. CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
  538. };
  539. /*
  540. * Control parameter IDs
  541. */
  542. enum control_param_id {
  543. /* 0: None, 1: Mic1In*/
  544. CONTROL_PARAM_VIP_SOURCE = 1,
  545. /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
  546. CONTROL_PARAM_SPDIF1_SOURCE = 2,
  547. /* Port A output stage gain setting to use when 16 Ohm output
  548. * impedance is selected*/
  549. CONTROL_PARAM_PORTA_160OHM_GAIN = 8,
  550. /* Port D output stage gain setting to use when 16 Ohm output
  551. * impedance is selected*/
  552. CONTROL_PARAM_PORTD_160OHM_GAIN = 10,
  553. /* Stream Control */
  554. /* Select stream with the given ID */
  555. CONTROL_PARAM_STREAM_ID = 24,
  556. /* Source connection point for the selected stream */
  557. CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
  558. /* Destination connection point for the selected stream */
  559. CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
  560. /* Number of audio channels in the selected stream */
  561. CONTROL_PARAM_STREAMS_CHANNELS = 27,
  562. /*Enable control for the selected stream */
  563. CONTROL_PARAM_STREAM_CONTROL = 28,
  564. /* Connection Point Control */
  565. /* Select connection point with the given ID */
  566. CONTROL_PARAM_CONN_POINT_ID = 29,
  567. /* Connection point sample rate */
  568. CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
  569. /* Node Control */
  570. /* Select HDA node with the given ID */
  571. CONTROL_PARAM_NODE_ID = 31
  572. };
  573. /*
  574. * Dsp Io Status codes
  575. */
  576. enum hda_vendor_status_dspio {
  577. /* Success */
  578. VENDOR_STATUS_DSPIO_OK = 0x00,
  579. /* Busy, unable to accept new command, the host must retry */
  580. VENDOR_STATUS_DSPIO_BUSY = 0x01,
  581. /* SCP command queue is full */
  582. VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
  583. /* SCP response queue is empty */
  584. VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
  585. };
  586. /*
  587. * Chip Io Status codes
  588. */
  589. enum hda_vendor_status_chipio {
  590. /* Success */
  591. VENDOR_STATUS_CHIPIO_OK = 0x00,
  592. /* Busy, unable to accept new command, the host must retry */
  593. VENDOR_STATUS_CHIPIO_BUSY = 0x01
  594. };
  595. /*
  596. * CA0132 sample rate
  597. */
  598. enum ca0132_sample_rate {
  599. SR_6_000 = 0x00,
  600. SR_8_000 = 0x01,
  601. SR_9_600 = 0x02,
  602. SR_11_025 = 0x03,
  603. SR_16_000 = 0x04,
  604. SR_22_050 = 0x05,
  605. SR_24_000 = 0x06,
  606. SR_32_000 = 0x07,
  607. SR_44_100 = 0x08,
  608. SR_48_000 = 0x09,
  609. SR_88_200 = 0x0A,
  610. SR_96_000 = 0x0B,
  611. SR_144_000 = 0x0C,
  612. SR_176_400 = 0x0D,
  613. SR_192_000 = 0x0E,
  614. SR_384_000 = 0x0F,
  615. SR_COUNT = 0x10,
  616. SR_RATE_UNKNOWN = 0x1F
  617. };
  618. enum dsp_download_state {
  619. DSP_DOWNLOAD_FAILED = -1,
  620. DSP_DOWNLOAD_INIT = 0,
  621. DSP_DOWNLOADING = 1,
  622. DSP_DOWNLOADED = 2
  623. };
  624. /* retrieve parameters from hda format */
  625. #define get_hdafmt_chs(fmt) (fmt & 0xf)
  626. #define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
  627. #define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
  628. #define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
  629. /*
  630. * CA0132 specific
  631. */
  632. struct ca0132_spec {
  633. struct snd_kcontrol_new *mixers[5];
  634. unsigned int num_mixers;
  635. const struct hda_verb *base_init_verbs;
  636. const struct hda_verb *base_exit_verbs;
  637. const struct hda_verb *init_verbs[5];
  638. unsigned int num_init_verbs; /* exclude base init verbs */
  639. struct auto_pin_cfg autocfg;
  640. /* Nodes configurations */
  641. struct hda_multi_out multiout;
  642. hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
  643. hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
  644. unsigned int num_outputs;
  645. hda_nid_t input_pins[AUTO_PIN_LAST];
  646. hda_nid_t adcs[AUTO_PIN_LAST];
  647. hda_nid_t dig_out;
  648. hda_nid_t dig_in;
  649. unsigned int num_inputs;
  650. hda_nid_t shared_mic_nid;
  651. hda_nid_t shared_out_nid;
  652. struct hda_pcm pcm_rec[5]; /* PCM information */
  653. /* chip access */
  654. struct mutex chipio_mutex; /* chip access mutex */
  655. u32 curr_chip_addx;
  656. /* DSP download related */
  657. enum dsp_download_state dsp_state;
  658. unsigned int dsp_stream_id;
  659. unsigned int wait_scp;
  660. unsigned int wait_scp_header;
  661. unsigned int wait_num_data;
  662. unsigned int scp_resp_header;
  663. unsigned int scp_resp_data[4];
  664. unsigned int scp_resp_count;
  665. /* mixer and effects related */
  666. unsigned char dmic_ctl;
  667. int cur_out_type;
  668. int cur_mic_type;
  669. long vnode_lvol[VNODES_COUNT];
  670. long vnode_rvol[VNODES_COUNT];
  671. long vnode_lswitch[VNODES_COUNT];
  672. long vnode_rswitch[VNODES_COUNT];
  673. long effects_switch[EFFECTS_COUNT];
  674. long voicefx_val;
  675. long cur_mic_boost;
  676. #ifdef ENABLE_TUNING_CONTROLS
  677. long cur_ctl_vals[TUNING_CTLS_COUNT];
  678. #endif
  679. };
  680. /*
  681. * CA0132 codec access
  682. */
  683. unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
  684. unsigned int verb, unsigned int parm, unsigned int *res)
  685. {
  686. unsigned int response;
  687. response = snd_hda_codec_read(codec, nid, 0, verb, parm);
  688. *res = response;
  689. return ((response == -1) ? -1 : 0);
  690. }
  691. static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
  692. unsigned short converter_format, unsigned int *res)
  693. {
  694. return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
  695. converter_format & 0xffff, res);
  696. }
  697. static int codec_set_converter_stream_channel(struct hda_codec *codec,
  698. hda_nid_t nid, unsigned char stream,
  699. unsigned char channel, unsigned int *res)
  700. {
  701. unsigned char converter_stream_channel = 0;
  702. converter_stream_channel = (stream << 4) | (channel & 0x0f);
  703. return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
  704. converter_stream_channel, res);
  705. }
  706. /* Chip access helper function */
  707. static int chipio_send(struct hda_codec *codec,
  708. unsigned int reg,
  709. unsigned int data)
  710. {
  711. unsigned int res;
  712. int retry = 50;
  713. /* send bits of data specified by reg */
  714. do {
  715. res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  716. reg, data);
  717. if (res == VENDOR_STATUS_CHIPIO_OK)
  718. return 0;
  719. } while (--retry);
  720. return -EIO;
  721. }
  722. /*
  723. * Write chip address through the vendor widget -- NOT protected by the Mutex!
  724. */
  725. static int chipio_write_address(struct hda_codec *codec,
  726. unsigned int chip_addx)
  727. {
  728. struct ca0132_spec *spec = codec->spec;
  729. int res;
  730. if (spec->curr_chip_addx == chip_addx)
  731. return 0;
  732. /* send low 16 bits of the address */
  733. res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
  734. chip_addx & 0xffff);
  735. if (res != -EIO) {
  736. /* send high 16 bits of the address */
  737. res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
  738. chip_addx >> 16);
  739. }
  740. spec->curr_chip_addx = (res < 0) ? ~0UL : chip_addx;
  741. return res;
  742. }
  743. /*
  744. * Write data through the vendor widget -- NOT protected by the Mutex!
  745. */
  746. static int chipio_write_data(struct hda_codec *codec, unsigned int data)
  747. {
  748. struct ca0132_spec *spec = codec->spec;
  749. int res;
  750. /* send low 16 bits of the data */
  751. res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
  752. if (res != -EIO) {
  753. /* send high 16 bits of the data */
  754. res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
  755. data >> 16);
  756. }
  757. /*If no error encountered, automatically increment the address
  758. as per chip behaviour*/
  759. spec->curr_chip_addx = (res != -EIO) ?
  760. (spec->curr_chip_addx + 4) : ~0UL;
  761. return res;
  762. }
  763. /*
  764. * Write multiple data through the vendor widget -- NOT protected by the Mutex!
  765. */
  766. static int chipio_write_data_multiple(struct hda_codec *codec,
  767. const u32 *data,
  768. unsigned int count)
  769. {
  770. int status = 0;
  771. if (data == NULL) {
  772. snd_printdd(KERN_ERR "chipio_write_data null ptr");
  773. return -EINVAL;
  774. }
  775. while ((count-- != 0) && (status == 0))
  776. status = chipio_write_data(codec, *data++);
  777. return status;
  778. }
  779. /*
  780. * Read data through the vendor widget -- NOT protected by the Mutex!
  781. */
  782. static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
  783. {
  784. struct ca0132_spec *spec = codec->spec;
  785. int res;
  786. /* post read */
  787. res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
  788. if (res != -EIO) {
  789. /* read status */
  790. res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  791. }
  792. if (res != -EIO) {
  793. /* read data */
  794. *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  795. VENDOR_CHIPIO_HIC_READ_DATA,
  796. 0);
  797. }
  798. /*If no error encountered, automatically increment the address
  799. as per chip behaviour*/
  800. spec->curr_chip_addx = (res != -EIO) ?
  801. (spec->curr_chip_addx + 4) : ~0UL;
  802. return res;
  803. }
  804. /*
  805. * Write given value to the given address through the chip I/O widget.
  806. * protected by the Mutex
  807. */
  808. static int chipio_write(struct hda_codec *codec,
  809. unsigned int chip_addx, const unsigned int data)
  810. {
  811. struct ca0132_spec *spec = codec->spec;
  812. int err;
  813. mutex_lock(&spec->chipio_mutex);
  814. /* write the address, and if successful proceed to write data */
  815. err = chipio_write_address(codec, chip_addx);
  816. if (err < 0)
  817. goto exit;
  818. err = chipio_write_data(codec, data);
  819. if (err < 0)
  820. goto exit;
  821. exit:
  822. mutex_unlock(&spec->chipio_mutex);
  823. return err;
  824. }
  825. /*
  826. * Write multiple values to the given address through the chip I/O widget.
  827. * protected by the Mutex
  828. */
  829. static int chipio_write_multiple(struct hda_codec *codec,
  830. u32 chip_addx,
  831. const u32 *data,
  832. unsigned int count)
  833. {
  834. struct ca0132_spec *spec = codec->spec;
  835. int status;
  836. mutex_lock(&spec->chipio_mutex);
  837. status = chipio_write_address(codec, chip_addx);
  838. if (status < 0)
  839. goto error;
  840. status = chipio_write_data_multiple(codec, data, count);
  841. error:
  842. mutex_unlock(&spec->chipio_mutex);
  843. return status;
  844. }
  845. /*
  846. * Read the given address through the chip I/O widget
  847. * protected by the Mutex
  848. */
  849. static int chipio_read(struct hda_codec *codec,
  850. unsigned int chip_addx, unsigned int *data)
  851. {
  852. struct ca0132_spec *spec = codec->spec;
  853. int err;
  854. mutex_lock(&spec->chipio_mutex);
  855. /* write the address, and if successful proceed to write data */
  856. err = chipio_write_address(codec, chip_addx);
  857. if (err < 0)
  858. goto exit;
  859. err = chipio_read_data(codec, data);
  860. if (err < 0)
  861. goto exit;
  862. exit:
  863. mutex_unlock(&spec->chipio_mutex);
  864. return err;
  865. }
  866. /*
  867. * Set chip control flags through the chip I/O widget.
  868. */
  869. static void chipio_set_control_flag(struct hda_codec *codec,
  870. enum control_flag_id flag_id,
  871. bool flag_state)
  872. {
  873. unsigned int val;
  874. unsigned int flag_bit;
  875. flag_bit = (flag_state ? 1 : 0);
  876. val = (flag_bit << 7) | (flag_id);
  877. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  878. VENDOR_CHIPIO_FLAG_SET, val);
  879. }
  880. /*
  881. * Set chip parameters through the chip I/O widget.
  882. */
  883. static void chipio_set_control_param(struct hda_codec *codec,
  884. enum control_param_id param_id, int param_val)
  885. {
  886. struct ca0132_spec *spec = codec->spec;
  887. int val;
  888. if ((param_id < 32) && (param_val < 8)) {
  889. val = (param_val << 5) | (param_id);
  890. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  891. VENDOR_CHIPIO_PARAM_SET, val);
  892. } else {
  893. mutex_lock(&spec->chipio_mutex);
  894. if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
  895. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  896. VENDOR_CHIPIO_PARAM_EX_ID_SET,
  897. param_id);
  898. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  899. VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
  900. param_val);
  901. }
  902. mutex_unlock(&spec->chipio_mutex);
  903. }
  904. }
  905. /*
  906. * Set sampling rate of the connection point.
  907. */
  908. static void chipio_set_conn_rate(struct hda_codec *codec,
  909. int connid, enum ca0132_sample_rate rate)
  910. {
  911. chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
  912. chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
  913. rate);
  914. }
  915. /*
  916. * Enable clocks.
  917. */
  918. static void chipio_enable_clocks(struct hda_codec *codec)
  919. {
  920. struct ca0132_spec *spec = codec->spec;
  921. mutex_lock(&spec->chipio_mutex);
  922. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  923. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
  924. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  925. VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
  926. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  927. VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
  928. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  929. VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
  930. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  931. VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
  932. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  933. VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
  934. mutex_unlock(&spec->chipio_mutex);
  935. }
  936. /*
  937. * CA0132 DSP IO stuffs
  938. */
  939. static int dspio_send(struct hda_codec *codec, unsigned int reg,
  940. unsigned int data)
  941. {
  942. int res;
  943. int retry = 50;
  944. /* send bits of data specified by reg to dsp */
  945. do {
  946. res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
  947. if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
  948. return res;
  949. } while (--retry);
  950. return -EIO;
  951. }
  952. /*
  953. * Wait for DSP to be ready for commands
  954. */
  955. static void dspio_write_wait(struct hda_codec *codec)
  956. {
  957. int status;
  958. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  959. do {
  960. status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  961. VENDOR_DSPIO_STATUS, 0);
  962. if ((status == VENDOR_STATUS_DSPIO_OK) ||
  963. (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
  964. break;
  965. msleep(1);
  966. } while (time_before(jiffies, timeout));
  967. }
  968. /*
  969. * Write SCP data to DSP
  970. */
  971. static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
  972. {
  973. struct ca0132_spec *spec = codec->spec;
  974. int status;
  975. dspio_write_wait(codec);
  976. mutex_lock(&spec->chipio_mutex);
  977. status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
  978. scp_data & 0xffff);
  979. if (status < 0)
  980. goto error;
  981. status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
  982. scp_data >> 16);
  983. if (status < 0)
  984. goto error;
  985. /* OK, now check if the write itself has executed*/
  986. status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  987. VENDOR_DSPIO_STATUS, 0);
  988. error:
  989. mutex_unlock(&spec->chipio_mutex);
  990. return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
  991. -EIO : 0;
  992. }
  993. /*
  994. * Write multiple SCP data to DSP
  995. */
  996. static int dspio_write_multiple(struct hda_codec *codec,
  997. unsigned int *buffer, unsigned int size)
  998. {
  999. int status = 0;
  1000. unsigned int count;
  1001. if ((buffer == NULL))
  1002. return -EINVAL;
  1003. count = 0;
  1004. while (count < size) {
  1005. status = dspio_write(codec, *buffer++);
  1006. if (status != 0)
  1007. break;
  1008. count++;
  1009. }
  1010. return status;
  1011. }
  1012. static int dspio_read(struct hda_codec *codec, unsigned int *data)
  1013. {
  1014. int status;
  1015. status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
  1016. if (status == -EIO)
  1017. return status;
  1018. status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
  1019. if (status == -EIO ||
  1020. status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY)
  1021. return -EIO;
  1022. *data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  1023. VENDOR_DSPIO_SCP_READ_DATA, 0);
  1024. return 0;
  1025. }
  1026. static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
  1027. unsigned int *buf_size, unsigned int size_count)
  1028. {
  1029. int status = 0;
  1030. unsigned int size = *buf_size;
  1031. unsigned int count;
  1032. unsigned int skip_count;
  1033. unsigned int dummy;
  1034. if ((buffer == NULL))
  1035. return -1;
  1036. count = 0;
  1037. while (count < size && count < size_count) {
  1038. status = dspio_read(codec, buffer++);
  1039. if (status != 0)
  1040. break;
  1041. count++;
  1042. }
  1043. skip_count = count;
  1044. if (status == 0) {
  1045. while (skip_count < size) {
  1046. status = dspio_read(codec, &dummy);
  1047. if (status != 0)
  1048. break;
  1049. skip_count++;
  1050. }
  1051. }
  1052. *buf_size = count;
  1053. return status;
  1054. }
  1055. /*
  1056. * Construct the SCP header using corresponding fields
  1057. */
  1058. static inline unsigned int
  1059. make_scp_header(unsigned int target_id, unsigned int source_id,
  1060. unsigned int get_flag, unsigned int req,
  1061. unsigned int device_flag, unsigned int resp_flag,
  1062. unsigned int error_flag, unsigned int data_size)
  1063. {
  1064. unsigned int header = 0;
  1065. header = (data_size & 0x1f) << 27;
  1066. header |= (error_flag & 0x01) << 26;
  1067. header |= (resp_flag & 0x01) << 25;
  1068. header |= (device_flag & 0x01) << 24;
  1069. header |= (req & 0x7f) << 17;
  1070. header |= (get_flag & 0x01) << 16;
  1071. header |= (source_id & 0xff) << 8;
  1072. header |= target_id & 0xff;
  1073. return header;
  1074. }
  1075. /*
  1076. * Extract corresponding fields from SCP header
  1077. */
  1078. static inline void
  1079. extract_scp_header(unsigned int header,
  1080. unsigned int *target_id, unsigned int *source_id,
  1081. unsigned int *get_flag, unsigned int *req,
  1082. unsigned int *device_flag, unsigned int *resp_flag,
  1083. unsigned int *error_flag, unsigned int *data_size)
  1084. {
  1085. if (data_size)
  1086. *data_size = (header >> 27) & 0x1f;
  1087. if (error_flag)
  1088. *error_flag = (header >> 26) & 0x01;
  1089. if (resp_flag)
  1090. *resp_flag = (header >> 25) & 0x01;
  1091. if (device_flag)
  1092. *device_flag = (header >> 24) & 0x01;
  1093. if (req)
  1094. *req = (header >> 17) & 0x7f;
  1095. if (get_flag)
  1096. *get_flag = (header >> 16) & 0x01;
  1097. if (source_id)
  1098. *source_id = (header >> 8) & 0xff;
  1099. if (target_id)
  1100. *target_id = header & 0xff;
  1101. }
  1102. #define SCP_MAX_DATA_WORDS (16)
  1103. /* Structure to contain any SCP message */
  1104. struct scp_msg {
  1105. unsigned int hdr;
  1106. unsigned int data[SCP_MAX_DATA_WORDS];
  1107. };
  1108. static void dspio_clear_response_queue(struct hda_codec *codec)
  1109. {
  1110. unsigned int dummy = 0;
  1111. int status = -1;
  1112. /* clear all from the response queue */
  1113. do {
  1114. status = dspio_read(codec, &dummy);
  1115. } while (status == 0);
  1116. }
  1117. static int dspio_get_response_data(struct hda_codec *codec)
  1118. {
  1119. struct ca0132_spec *spec = codec->spec;
  1120. unsigned int data = 0;
  1121. unsigned int count;
  1122. if (dspio_read(codec, &data) < 0)
  1123. return -EIO;
  1124. if ((data & 0x00ffffff) == spec->wait_scp_header) {
  1125. spec->scp_resp_header = data;
  1126. spec->scp_resp_count = data >> 27;
  1127. count = spec->wait_num_data;
  1128. dspio_read_multiple(codec, spec->scp_resp_data,
  1129. &spec->scp_resp_count, count);
  1130. return 0;
  1131. }
  1132. return -EIO;
  1133. }
  1134. /*
  1135. * Send SCP message to DSP
  1136. */
  1137. static int dspio_send_scp_message(struct hda_codec *codec,
  1138. unsigned char *send_buf,
  1139. unsigned int send_buf_size,
  1140. unsigned char *return_buf,
  1141. unsigned int return_buf_size,
  1142. unsigned int *bytes_returned)
  1143. {
  1144. struct ca0132_spec *spec = codec->spec;
  1145. int retry;
  1146. int status = -1;
  1147. unsigned int scp_send_size = 0;
  1148. unsigned int total_size;
  1149. bool waiting_for_resp = false;
  1150. unsigned int header;
  1151. struct scp_msg *ret_msg;
  1152. unsigned int resp_src_id, resp_target_id;
  1153. unsigned int data_size, src_id, target_id, get_flag, device_flag;
  1154. if (bytes_returned)
  1155. *bytes_returned = 0;
  1156. /* get scp header from buffer */
  1157. header = *((unsigned int *)send_buf);
  1158. extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
  1159. &device_flag, NULL, NULL, &data_size);
  1160. scp_send_size = data_size + 1;
  1161. total_size = (scp_send_size * 4);
  1162. if (send_buf_size < total_size)
  1163. return -EINVAL;
  1164. if (get_flag || device_flag) {
  1165. if (!return_buf || return_buf_size < 4 || !bytes_returned)
  1166. return -EINVAL;
  1167. spec->wait_scp_header = *((unsigned int *)send_buf);
  1168. /* swap source id with target id */
  1169. resp_target_id = src_id;
  1170. resp_src_id = target_id;
  1171. spec->wait_scp_header &= 0xffff0000;
  1172. spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
  1173. spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
  1174. spec->wait_scp = 1;
  1175. waiting_for_resp = true;
  1176. }
  1177. status = dspio_write_multiple(codec, (unsigned int *)send_buf,
  1178. scp_send_size);
  1179. if (status < 0) {
  1180. spec->wait_scp = 0;
  1181. return status;
  1182. }
  1183. if (waiting_for_resp) {
  1184. memset(return_buf, 0, return_buf_size);
  1185. retry = 50;
  1186. do {
  1187. msleep(20);
  1188. } while (spec->wait_scp && (--retry != 0));
  1189. waiting_for_resp = false;
  1190. if (retry != 0) {
  1191. ret_msg = (struct scp_msg *)return_buf;
  1192. memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
  1193. memcpy(&ret_msg->data, spec->scp_resp_data,
  1194. spec->wait_num_data);
  1195. *bytes_returned = (spec->scp_resp_count + 1) * 4;
  1196. status = 0;
  1197. } else {
  1198. status = -EIO;
  1199. }
  1200. spec->wait_scp = 0;
  1201. }
  1202. return status;
  1203. }
  1204. /**
  1205. * Prepare and send the SCP message to DSP
  1206. * @codec: the HDA codec
  1207. * @mod_id: ID of the DSP module to send the command
  1208. * @req: ID of request to send to the DSP module
  1209. * @dir: SET or GET
  1210. * @data: pointer to the data to send with the request, request specific
  1211. * @len: length of the data, in bytes
  1212. * @reply: point to the buffer to hold data returned for a reply
  1213. * @reply_len: length of the reply buffer returned from GET
  1214. *
  1215. * Returns zero or a negative error code.
  1216. */
  1217. static int dspio_scp(struct hda_codec *codec,
  1218. int mod_id, int req, int dir, void *data, unsigned int len,
  1219. void *reply, unsigned int *reply_len)
  1220. {
  1221. int status = 0;
  1222. struct scp_msg scp_send, scp_reply;
  1223. unsigned int ret_bytes, send_size, ret_size;
  1224. unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
  1225. unsigned int reply_data_size;
  1226. memset(&scp_send, 0, sizeof(scp_send));
  1227. memset(&scp_reply, 0, sizeof(scp_reply));
  1228. if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
  1229. return -EINVAL;
  1230. if (dir == SCP_GET && reply == NULL) {
  1231. snd_printdd(KERN_ERR "dspio_scp get but has no buffer");
  1232. return -EINVAL;
  1233. }
  1234. if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
  1235. snd_printdd(KERN_ERR "dspio_scp bad resp buf len parms");
  1236. return -EINVAL;
  1237. }
  1238. scp_send.hdr = make_scp_header(mod_id, 0x20, (dir == SCP_GET), req,
  1239. 0, 0, 0, len/sizeof(unsigned int));
  1240. if (data != NULL && len > 0) {
  1241. len = min((unsigned int)(sizeof(scp_send.data)), len);
  1242. memcpy(scp_send.data, data, len);
  1243. }
  1244. ret_bytes = 0;
  1245. send_size = sizeof(unsigned int) + len;
  1246. status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
  1247. send_size, (unsigned char *)&scp_reply,
  1248. sizeof(scp_reply), &ret_bytes);
  1249. if (status < 0) {
  1250. snd_printdd(KERN_ERR "dspio_scp: send scp msg failed");
  1251. return status;
  1252. }
  1253. /* extract send and reply headers members */
  1254. extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
  1255. NULL, NULL, NULL, NULL, NULL);
  1256. extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
  1257. &reply_resp_flag, &reply_error_flag,
  1258. &reply_data_size);
  1259. if (!send_get_flag)
  1260. return 0;
  1261. if (reply_resp_flag && !reply_error_flag) {
  1262. ret_size = (ret_bytes - sizeof(scp_reply.hdr))
  1263. / sizeof(unsigned int);
  1264. if (*reply_len < ret_size*sizeof(unsigned int)) {
  1265. snd_printdd(KERN_ERR "reply too long for buf");
  1266. return -EINVAL;
  1267. } else if (ret_size != reply_data_size) {
  1268. snd_printdd(KERN_ERR "RetLen and HdrLen .NE.");
  1269. return -EINVAL;
  1270. } else {
  1271. *reply_len = ret_size*sizeof(unsigned int);
  1272. memcpy(reply, scp_reply.data, *reply_len);
  1273. }
  1274. } else {
  1275. snd_printdd(KERN_ERR "reply ill-formed or errflag set");
  1276. return -EIO;
  1277. }
  1278. return status;
  1279. }
  1280. /*
  1281. * Set DSP parameters
  1282. */
  1283. static int dspio_set_param(struct hda_codec *codec, int mod_id,
  1284. int req, void *data, unsigned int len)
  1285. {
  1286. return dspio_scp(codec, mod_id, req, SCP_SET, data, len, NULL, NULL);
  1287. }
  1288. static int dspio_set_uint_param(struct hda_codec *codec, int mod_id,
  1289. int req, unsigned int data)
  1290. {
  1291. return dspio_set_param(codec, mod_id, req, &data, sizeof(unsigned int));
  1292. }
  1293. /*
  1294. * Allocate a DSP DMA channel via an SCP message
  1295. */
  1296. static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
  1297. {
  1298. int status = 0;
  1299. unsigned int size = sizeof(dma_chan);
  1300. snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- begin");
  1301. status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
  1302. SCP_GET, NULL, 0, dma_chan, &size);
  1303. if (status < 0) {
  1304. snd_printdd(KERN_INFO "dspio_alloc_dma_chan: SCP Failed");
  1305. return status;
  1306. }
  1307. if ((*dma_chan + 1) == 0) {
  1308. snd_printdd(KERN_INFO "no free dma channels to allocate");
  1309. return -EBUSY;
  1310. }
  1311. snd_printdd("dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
  1312. snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- complete");
  1313. return status;
  1314. }
  1315. /*
  1316. * Free a DSP DMA via an SCP message
  1317. */
  1318. static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
  1319. {
  1320. int status = 0;
  1321. unsigned int dummy = 0;
  1322. snd_printdd(KERN_INFO " dspio_free_dma_chan() -- begin");
  1323. snd_printdd("dspio_free_dma_chan: chan=%d\n", dma_chan);
  1324. status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
  1325. SCP_SET, &dma_chan, sizeof(dma_chan), NULL, &dummy);
  1326. if (status < 0) {
  1327. snd_printdd(KERN_INFO "dspio_free_dma_chan: SCP Failed");
  1328. return status;
  1329. }
  1330. snd_printdd(KERN_INFO " dspio_free_dma_chan() -- complete");
  1331. return status;
  1332. }
  1333. /*
  1334. * (Re)start the DSP
  1335. */
  1336. static int dsp_set_run_state(struct hda_codec *codec)
  1337. {
  1338. unsigned int dbg_ctrl_reg;
  1339. unsigned int halt_state;
  1340. int err;
  1341. err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
  1342. if (err < 0)
  1343. return err;
  1344. halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
  1345. DSP_DBGCNTL_STATE_LOBIT;
  1346. if (halt_state != 0) {
  1347. dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
  1348. DSP_DBGCNTL_SS_MASK);
  1349. err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
  1350. dbg_ctrl_reg);
  1351. if (err < 0)
  1352. return err;
  1353. dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
  1354. DSP_DBGCNTL_EXEC_MASK;
  1355. err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
  1356. dbg_ctrl_reg);
  1357. if (err < 0)
  1358. return err;
  1359. }
  1360. return 0;
  1361. }
  1362. /*
  1363. * Reset the DSP
  1364. */
  1365. static int dsp_reset(struct hda_codec *codec)
  1366. {
  1367. unsigned int res;
  1368. int retry = 20;
  1369. snd_printdd("dsp_reset\n");
  1370. do {
  1371. res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
  1372. retry--;
  1373. } while (res == -EIO && retry);
  1374. if (!retry) {
  1375. snd_printdd("dsp_reset timeout\n");
  1376. return -EIO;
  1377. }
  1378. return 0;
  1379. }
  1380. /*
  1381. * Convert chip address to DSP address
  1382. */
  1383. static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
  1384. bool *code, bool *yram)
  1385. {
  1386. *code = *yram = false;
  1387. if (UC_RANGE(chip_addx, 1)) {
  1388. *code = true;
  1389. return UC_OFF(chip_addx);
  1390. } else if (X_RANGE_ALL(chip_addx, 1)) {
  1391. return X_OFF(chip_addx);
  1392. } else if (Y_RANGE_ALL(chip_addx, 1)) {
  1393. *yram = true;
  1394. return Y_OFF(chip_addx);
  1395. }
  1396. return (unsigned int)INVALID_CHIP_ADDRESS;
  1397. }
  1398. /*
  1399. * Check if the DSP DMA is active
  1400. */
  1401. static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
  1402. {
  1403. unsigned int dma_chnlstart_reg;
  1404. chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
  1405. return ((dma_chnlstart_reg & (1 <<
  1406. (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
  1407. }
  1408. static int dsp_dma_setup_common(struct hda_codec *codec,
  1409. unsigned int chip_addx,
  1410. unsigned int dma_chan,
  1411. unsigned int port_map_mask,
  1412. bool ovly)
  1413. {
  1414. int status = 0;
  1415. unsigned int chnl_prop;
  1416. unsigned int dsp_addx;
  1417. unsigned int active;
  1418. bool code, yram;
  1419. snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Begin ---------");
  1420. if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
  1421. snd_printdd(KERN_ERR "dma chan num invalid");
  1422. return -EINVAL;
  1423. }
  1424. if (dsp_is_dma_active(codec, dma_chan)) {
  1425. snd_printdd(KERN_ERR "dma already active");
  1426. return -EBUSY;
  1427. }
  1428. dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
  1429. if (dsp_addx == INVALID_CHIP_ADDRESS) {
  1430. snd_printdd(KERN_ERR "invalid chip addr");
  1431. return -ENXIO;
  1432. }
  1433. chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
  1434. active = 0;
  1435. snd_printdd(KERN_INFO " dsp_dma_setup_common() start reg pgm");
  1436. if (ovly) {
  1437. status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
  1438. &chnl_prop);
  1439. if (status < 0) {
  1440. snd_printdd(KERN_ERR "read CHNLPROP Reg fail");
  1441. return status;
  1442. }
  1443. snd_printdd(KERN_INFO "dsp_dma_setup_common() Read CHNLPROP");
  1444. }
  1445. if (!code)
  1446. chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
  1447. else
  1448. chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
  1449. chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
  1450. status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
  1451. if (status < 0) {
  1452. snd_printdd(KERN_ERR "write CHNLPROP Reg fail");
  1453. return status;
  1454. }
  1455. snd_printdd(KERN_INFO " dsp_dma_setup_common() Write CHNLPROP");
  1456. if (ovly) {
  1457. status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
  1458. &active);
  1459. if (status < 0) {
  1460. snd_printdd(KERN_ERR "read ACTIVE Reg fail");
  1461. return status;
  1462. }
  1463. snd_printdd(KERN_INFO "dsp_dma_setup_common() Read ACTIVE");
  1464. }
  1465. active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
  1466. DSPDMAC_ACTIVE_AAR_MASK;
  1467. status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
  1468. if (status < 0) {
  1469. snd_printdd(KERN_ERR "write ACTIVE Reg fail");
  1470. return status;
  1471. }
  1472. snd_printdd(KERN_INFO " dsp_dma_setup_common() Write ACTIVE");
  1473. status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
  1474. port_map_mask);
  1475. if (status < 0) {
  1476. snd_printdd(KERN_ERR "write AUDCHSEL Reg fail");
  1477. return status;
  1478. }
  1479. snd_printdd(KERN_INFO " dsp_dma_setup_common() Write AUDCHSEL");
  1480. status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
  1481. DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
  1482. if (status < 0) {
  1483. snd_printdd(KERN_ERR "write IRQCNT Reg fail");
  1484. return status;
  1485. }
  1486. snd_printdd(KERN_INFO " dsp_dma_setup_common() Write IRQCNT");
  1487. snd_printdd(
  1488. "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
  1489. "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
  1490. chip_addx, dsp_addx, dma_chan,
  1491. port_map_mask, chnl_prop, active);
  1492. snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Complete ------");
  1493. return 0;
  1494. }
  1495. /*
  1496. * Setup the DSP DMA per-transfer-specific registers
  1497. */
  1498. static int dsp_dma_setup(struct hda_codec *codec,
  1499. unsigned int chip_addx,
  1500. unsigned int count,
  1501. unsigned int dma_chan)
  1502. {
  1503. int status = 0;
  1504. bool code, yram;
  1505. unsigned int dsp_addx;
  1506. unsigned int addr_field;
  1507. unsigned int incr_field;
  1508. unsigned int base_cnt;
  1509. unsigned int cur_cnt;
  1510. unsigned int dma_cfg = 0;
  1511. unsigned int adr_ofs = 0;
  1512. unsigned int xfr_cnt = 0;
  1513. const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
  1514. DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
  1515. snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Begin ---------");
  1516. if (count > max_dma_count) {
  1517. snd_printdd(KERN_ERR "count too big");
  1518. return -EINVAL;
  1519. }
  1520. dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
  1521. if (dsp_addx == INVALID_CHIP_ADDRESS) {
  1522. snd_printdd(KERN_ERR "invalid chip addr");
  1523. return -ENXIO;
  1524. }
  1525. snd_printdd(KERN_INFO " dsp_dma_setup() start reg pgm");
  1526. addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
  1527. incr_field = 0;
  1528. if (!code) {
  1529. addr_field <<= 1;
  1530. if (yram)
  1531. addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
  1532. incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
  1533. }
  1534. dma_cfg = addr_field + incr_field;
  1535. status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
  1536. dma_cfg);
  1537. if (status < 0) {
  1538. snd_printdd(KERN_ERR "write DMACFG Reg fail");
  1539. return status;
  1540. }
  1541. snd_printdd(KERN_INFO " dsp_dma_setup() Write DMACFG");
  1542. adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
  1543. (code ? 0 : 1));
  1544. status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
  1545. adr_ofs);
  1546. if (status < 0) {
  1547. snd_printdd(KERN_ERR "write DSPADROFS Reg fail");
  1548. return status;
  1549. }
  1550. snd_printdd(KERN_INFO " dsp_dma_setup() Write DSPADROFS");
  1551. base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
  1552. cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
  1553. xfr_cnt = base_cnt | cur_cnt;
  1554. status = chipio_write(codec,
  1555. DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
  1556. if (status < 0) {
  1557. snd_printdd(KERN_ERR "write XFRCNT Reg fail");
  1558. return status;
  1559. }
  1560. snd_printdd(KERN_INFO " dsp_dma_setup() Write XFRCNT");
  1561. snd_printdd(
  1562. "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
  1563. "ADROFS=0x%x, XFRCNT=0x%x\n",
  1564. chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
  1565. snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Complete ---------");
  1566. return 0;
  1567. }
  1568. /*
  1569. * Start the DSP DMA
  1570. */
  1571. static int dsp_dma_start(struct hda_codec *codec,
  1572. unsigned int dma_chan, bool ovly)
  1573. {
  1574. unsigned int reg = 0;
  1575. int status = 0;
  1576. snd_printdd(KERN_INFO "-- dsp_dma_start() -- Begin ---------");
  1577. if (ovly) {
  1578. status = chipio_read(codec,
  1579. DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
  1580. if (status < 0) {
  1581. snd_printdd(KERN_ERR "read CHNLSTART reg fail");
  1582. return status;
  1583. }
  1584. snd_printdd(KERN_INFO "-- dsp_dma_start() Read CHNLSTART");
  1585. reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
  1586. DSPDMAC_CHNLSTART_DIS_MASK);
  1587. }
  1588. status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
  1589. reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
  1590. if (status < 0) {
  1591. snd_printdd(KERN_ERR "write CHNLSTART reg fail");
  1592. return status;
  1593. }
  1594. snd_printdd(KERN_INFO "-- dsp_dma_start() -- Complete ---------");
  1595. return status;
  1596. }
  1597. /*
  1598. * Stop the DSP DMA
  1599. */
  1600. static int dsp_dma_stop(struct hda_codec *codec,
  1601. unsigned int dma_chan, bool ovly)
  1602. {
  1603. unsigned int reg = 0;
  1604. int status = 0;
  1605. snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Begin ---------");
  1606. if (ovly) {
  1607. status = chipio_read(codec,
  1608. DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
  1609. if (status < 0) {
  1610. snd_printdd(KERN_ERR "read CHNLSTART reg fail");
  1611. return status;
  1612. }
  1613. snd_printdd(KERN_INFO "-- dsp_dma_stop() Read CHNLSTART");
  1614. reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
  1615. DSPDMAC_CHNLSTART_DIS_MASK);
  1616. }
  1617. status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
  1618. reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
  1619. if (status < 0) {
  1620. snd_printdd(KERN_ERR "write CHNLSTART reg fail");
  1621. return status;
  1622. }
  1623. snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Complete ---------");
  1624. return status;
  1625. }
  1626. /**
  1627. * Allocate router ports
  1628. *
  1629. * @codec: the HDA codec
  1630. * @num_chans: number of channels in the stream
  1631. * @ports_per_channel: number of ports per channel
  1632. * @start_device: start device
  1633. * @port_map: pointer to the port list to hold the allocated ports
  1634. *
  1635. * Returns zero or a negative error code.
  1636. */
  1637. static int dsp_allocate_router_ports(struct hda_codec *codec,
  1638. unsigned int num_chans,
  1639. unsigned int ports_per_channel,
  1640. unsigned int start_device,
  1641. unsigned int *port_map)
  1642. {
  1643. int status = 0;
  1644. int res;
  1645. u8 val;
  1646. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1647. if (status < 0)
  1648. return status;
  1649. val = start_device << 6;
  1650. val |= (ports_per_channel - 1) << 4;
  1651. val |= num_chans - 1;
  1652. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1653. VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
  1654. val);
  1655. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1656. VENDOR_CHIPIO_PORT_ALLOC_SET,
  1657. MEM_CONNID_DSP);
  1658. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1659. if (status < 0)
  1660. return status;
  1661. res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  1662. VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
  1663. *port_map = res;
  1664. return (res < 0) ? res : 0;
  1665. }
  1666. /*
  1667. * Free router ports
  1668. */
  1669. static int dsp_free_router_ports(struct hda_codec *codec)
  1670. {
  1671. int status = 0;
  1672. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1673. if (status < 0)
  1674. return status;
  1675. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1676. VENDOR_CHIPIO_PORT_FREE_SET,
  1677. MEM_CONNID_DSP);
  1678. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1679. return status;
  1680. }
  1681. /*
  1682. * Allocate DSP ports for the download stream
  1683. */
  1684. static int dsp_allocate_ports(struct hda_codec *codec,
  1685. unsigned int num_chans,
  1686. unsigned int rate_multi, unsigned int *port_map)
  1687. {
  1688. int status;
  1689. snd_printdd(KERN_INFO " dsp_allocate_ports() -- begin");
  1690. if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
  1691. snd_printdd(KERN_ERR "bad rate multiple");
  1692. return -EINVAL;
  1693. }
  1694. status = dsp_allocate_router_ports(codec, num_chans,
  1695. rate_multi, 0, port_map);
  1696. snd_printdd(KERN_INFO " dsp_allocate_ports() -- complete");
  1697. return status;
  1698. }
  1699. static int dsp_allocate_ports_format(struct hda_codec *codec,
  1700. const unsigned short fmt,
  1701. unsigned int *port_map)
  1702. {
  1703. int status;
  1704. unsigned int num_chans;
  1705. unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
  1706. unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
  1707. unsigned int rate_multi = sample_rate_mul / sample_rate_div;
  1708. if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
  1709. snd_printdd(KERN_ERR "bad rate multiple");
  1710. return -EINVAL;
  1711. }
  1712. num_chans = get_hdafmt_chs(fmt) + 1;
  1713. status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
  1714. return status;
  1715. }
  1716. /*
  1717. * free DSP ports
  1718. */
  1719. static int dsp_free_ports(struct hda_codec *codec)
  1720. {
  1721. int status;
  1722. snd_printdd(KERN_INFO " dsp_free_ports() -- begin");
  1723. status = dsp_free_router_ports(codec);
  1724. if (status < 0) {
  1725. snd_printdd(KERN_ERR "free router ports fail");
  1726. return status;
  1727. }
  1728. snd_printdd(KERN_INFO " dsp_free_ports() -- complete");
  1729. return status;
  1730. }
  1731. /*
  1732. * HDA DMA engine stuffs for DSP code download
  1733. */
  1734. struct dma_engine {
  1735. struct hda_codec *codec;
  1736. unsigned short m_converter_format;
  1737. struct snd_dma_buffer *dmab;
  1738. unsigned int buf_size;
  1739. };
  1740. enum dma_state {
  1741. DMA_STATE_STOP = 0,
  1742. DMA_STATE_RUN = 1
  1743. };
  1744. static int dma_convert_to_hda_format(
  1745. unsigned int sample_rate,
  1746. unsigned short channels,
  1747. unsigned short *hda_format)
  1748. {
  1749. unsigned int format_val;
  1750. format_val = snd_hda_calc_stream_format(
  1751. sample_rate,
  1752. channels,
  1753. SNDRV_PCM_FORMAT_S32_LE,
  1754. 32, 0);
  1755. if (hda_format)
  1756. *hda_format = (unsigned short)format_val;
  1757. return 0;
  1758. }
  1759. /*
  1760. * Reset DMA for DSP download
  1761. */
  1762. static int dma_reset(struct dma_engine *dma)
  1763. {
  1764. struct hda_codec *codec = dma->codec;
  1765. struct ca0132_spec *spec = codec->spec;
  1766. int status;
  1767. if (dma->dmab)
  1768. snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
  1769. status = snd_hda_codec_load_dsp_prepare(codec,
  1770. dma->m_converter_format,
  1771. dma->buf_size,
  1772. dma->dmab);
  1773. if (status < 0)
  1774. return status;
  1775. spec->dsp_stream_id = status;
  1776. return 0;
  1777. }
  1778. static int dma_set_state(struct dma_engine *dma, enum dma_state state)
  1779. {
  1780. bool cmd;
  1781. snd_printdd("dma_set_state state=%d\n", state);
  1782. switch (state) {
  1783. case DMA_STATE_STOP:
  1784. cmd = false;
  1785. break;
  1786. case DMA_STATE_RUN:
  1787. cmd = true;
  1788. break;
  1789. default:
  1790. return 0;
  1791. }
  1792. snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
  1793. return 0;
  1794. }
  1795. static unsigned int dma_get_buffer_size(struct dma_engine *dma)
  1796. {
  1797. return dma->dmab->bytes;
  1798. }
  1799. static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
  1800. {
  1801. return dma->dmab->area;
  1802. }
  1803. static int dma_xfer(struct dma_engine *dma,
  1804. const unsigned int *data,
  1805. unsigned int count)
  1806. {
  1807. memcpy(dma->dmab->area, data, count);
  1808. return 0;
  1809. }
  1810. static void dma_get_converter_format(
  1811. struct dma_engine *dma,
  1812. unsigned short *format)
  1813. {
  1814. if (format)
  1815. *format = dma->m_converter_format;
  1816. }
  1817. static unsigned int dma_get_stream_id(struct dma_engine *dma)
  1818. {
  1819. struct ca0132_spec *spec = dma->codec->spec;
  1820. return spec->dsp_stream_id;
  1821. }
  1822. struct dsp_image_seg {
  1823. u32 magic;
  1824. u32 chip_addr;
  1825. u32 count;
  1826. u32 data[0];
  1827. };
  1828. static const u32 g_magic_value = 0x4c46584d;
  1829. static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
  1830. static bool is_valid(const struct dsp_image_seg *p)
  1831. {
  1832. return p->magic == g_magic_value;
  1833. }
  1834. static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
  1835. {
  1836. return g_chip_addr_magic_value == p->chip_addr;
  1837. }
  1838. static bool is_last(const struct dsp_image_seg *p)
  1839. {
  1840. return p->count == 0;
  1841. }
  1842. static size_t dsp_sizeof(const struct dsp_image_seg *p)
  1843. {
  1844. return sizeof(*p) + p->count*sizeof(u32);
  1845. }
  1846. static const struct dsp_image_seg *get_next_seg_ptr(
  1847. const struct dsp_image_seg *p)
  1848. {
  1849. return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
  1850. }
  1851. /*
  1852. * CA0132 chip DSP transfer stuffs. For DSP download.
  1853. */
  1854. #define INVALID_DMA_CHANNEL (~0UL)
  1855. /*
  1856. * Program a list of address/data pairs via the ChipIO widget.
  1857. * The segment data is in the format of successive pairs of words.
  1858. * These are repeated as indicated by the segment's count field.
  1859. */
  1860. static int dspxfr_hci_write(struct hda_codec *codec,
  1861. const struct dsp_image_seg *fls)
  1862. {
  1863. int status;
  1864. const u32 *data;
  1865. unsigned int count;
  1866. if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
  1867. snd_printdd(KERN_ERR "hci_write invalid params");
  1868. return -EINVAL;
  1869. }
  1870. count = fls->count;
  1871. data = (u32 *)(fls->data);
  1872. while (count >= 2) {
  1873. status = chipio_write(codec, data[0], data[1]);
  1874. if (status < 0) {
  1875. snd_printdd(KERN_ERR "hci_write chipio failed");
  1876. return status;
  1877. }
  1878. count -= 2;
  1879. data += 2;
  1880. }
  1881. return 0;
  1882. }
  1883. /**
  1884. * Write a block of data into DSP code or data RAM using pre-allocated
  1885. * DMA engine.
  1886. *
  1887. * @codec: the HDA codec
  1888. * @fls: pointer to a fast load image
  1889. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  1890. * no relocation
  1891. * @dma_engine: pointer to DMA engine to be used for DSP download
  1892. * @dma_chan: The number of DMA channels used for DSP download
  1893. * @port_map_mask: port mapping
  1894. * @ovly: TRUE if overlay format is required
  1895. *
  1896. * Returns zero or a negative error code.
  1897. */
  1898. static int dspxfr_one_seg(struct hda_codec *codec,
  1899. const struct dsp_image_seg *fls,
  1900. unsigned int reloc,
  1901. struct dma_engine *dma_engine,
  1902. unsigned int dma_chan,
  1903. unsigned int port_map_mask,
  1904. bool ovly)
  1905. {
  1906. int status = 0;
  1907. bool comm_dma_setup_done = false;
  1908. const unsigned int *data;
  1909. unsigned int chip_addx;
  1910. unsigned int words_to_write;
  1911. unsigned int buffer_size_words;
  1912. unsigned char *buffer_addx;
  1913. unsigned short hda_format;
  1914. unsigned int sample_rate_div;
  1915. unsigned int sample_rate_mul;
  1916. unsigned int num_chans;
  1917. unsigned int hda_frame_size_words;
  1918. unsigned int remainder_words;
  1919. const u32 *data_remainder;
  1920. u32 chip_addx_remainder;
  1921. unsigned int run_size_words;
  1922. const struct dsp_image_seg *hci_write = NULL;
  1923. int retry;
  1924. if (fls == NULL)
  1925. return -EINVAL;
  1926. if (is_hci_prog_list_seg(fls)) {
  1927. hci_write = fls;
  1928. fls = get_next_seg_ptr(fls);
  1929. }
  1930. if (hci_write && (!fls || is_last(fls))) {
  1931. snd_printdd("hci_write\n");
  1932. return dspxfr_hci_write(codec, hci_write);
  1933. }
  1934. if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
  1935. snd_printdd("Invalid Params\n");
  1936. return -EINVAL;
  1937. }
  1938. data = fls->data;
  1939. chip_addx = fls->chip_addr,
  1940. words_to_write = fls->count;
  1941. if (!words_to_write)
  1942. return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
  1943. if (reloc)
  1944. chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
  1945. if (!UC_RANGE(chip_addx, words_to_write) &&
  1946. !X_RANGE_ALL(chip_addx, words_to_write) &&
  1947. !Y_RANGE_ALL(chip_addx, words_to_write)) {
  1948. snd_printdd("Invalid chip_addx Params\n");
  1949. return -EINVAL;
  1950. }
  1951. buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
  1952. sizeof(u32);
  1953. buffer_addx = dma_get_buffer_addr(dma_engine);
  1954. if (buffer_addx == NULL) {
  1955. snd_printdd(KERN_ERR "dma_engine buffer NULL\n");
  1956. return -EINVAL;
  1957. }
  1958. dma_get_converter_format(dma_engine, &hda_format);
  1959. sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
  1960. sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
  1961. num_chans = get_hdafmt_chs(hda_format) + 1;
  1962. hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
  1963. (num_chans * sample_rate_mul / sample_rate_div));
  1964. buffer_size_words = min(buffer_size_words,
  1965. (unsigned int)(UC_RANGE(chip_addx, 1) ?
  1966. 65536 : 32768));
  1967. buffer_size_words -= buffer_size_words % hda_frame_size_words;
  1968. snd_printdd(
  1969. "chpadr=0x%08x frmsz=%u nchan=%u "
  1970. "rate_mul=%u div=%u bufsz=%u\n",
  1971. chip_addx, hda_frame_size_words, num_chans,
  1972. sample_rate_mul, sample_rate_div, buffer_size_words);
  1973. if ((buffer_addx == NULL) || (hda_frame_size_words == 0) ||
  1974. (buffer_size_words < hda_frame_size_words)) {
  1975. snd_printdd(KERN_ERR "dspxfr_one_seg:failed\n");
  1976. return -EINVAL;
  1977. }
  1978. remainder_words = words_to_write % hda_frame_size_words;
  1979. data_remainder = data;
  1980. chip_addx_remainder = chip_addx;
  1981. data += remainder_words;
  1982. chip_addx += remainder_words*sizeof(u32);
  1983. words_to_write -= remainder_words;
  1984. while (words_to_write != 0) {
  1985. run_size_words = min(buffer_size_words, words_to_write);
  1986. snd_printdd("dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
  1987. words_to_write, run_size_words, remainder_words);
  1988. dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
  1989. if (!comm_dma_setup_done) {
  1990. status = dsp_dma_stop(codec, dma_chan, ovly);
  1991. if (status < 0)
  1992. return -EIO;
  1993. status = dsp_dma_setup_common(codec, chip_addx,
  1994. dma_chan, port_map_mask, ovly);
  1995. if (status < 0)
  1996. return status;
  1997. comm_dma_setup_done = true;
  1998. }
  1999. status = dsp_dma_setup(codec, chip_addx,
  2000. run_size_words, dma_chan);
  2001. if (status < 0)
  2002. return status;
  2003. status = dsp_dma_start(codec, dma_chan, ovly);
  2004. if (status < 0)
  2005. return status;
  2006. if (!dsp_is_dma_active(codec, dma_chan)) {
  2007. snd_printdd(KERN_ERR "dspxfr:DMA did not start");
  2008. return -EIO;
  2009. }
  2010. status = dma_set_state(dma_engine, DMA_STATE_RUN);
  2011. if (status < 0)
  2012. return status;
  2013. if (remainder_words != 0) {
  2014. status = chipio_write_multiple(codec,
  2015. chip_addx_remainder,
  2016. data_remainder,
  2017. remainder_words);
  2018. remainder_words = 0;
  2019. }
  2020. if (hci_write) {
  2021. status = dspxfr_hci_write(codec, hci_write);
  2022. hci_write = NULL;
  2023. }
  2024. retry = 5000;
  2025. while (dsp_is_dma_active(codec, dma_chan)) {
  2026. if (--retry <= 0)
  2027. break;
  2028. }
  2029. snd_printdd(KERN_INFO "+++++ DMA complete");
  2030. dma_set_state(dma_engine, DMA_STATE_STOP);
  2031. dma_reset(dma_engine);
  2032. if (status < 0)
  2033. return status;
  2034. data += run_size_words;
  2035. chip_addx += run_size_words*sizeof(u32);
  2036. words_to_write -= run_size_words;
  2037. }
  2038. if (remainder_words != 0) {
  2039. status = chipio_write_multiple(codec, chip_addx_remainder,
  2040. data_remainder, remainder_words);
  2041. }
  2042. return status;
  2043. }
  2044. /**
  2045. * Write the entire DSP image of a DSP code/data overlay to DSP memories
  2046. *
  2047. * @codec: the HDA codec
  2048. * @fls_data: pointer to a fast load image
  2049. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  2050. * no relocation
  2051. * @sample_rate: sampling rate of the stream used for DSP download
  2052. * @number_channels: channels of the stream used for DSP download
  2053. * @ovly: TRUE if overlay format is required
  2054. *
  2055. * Returns zero or a negative error code.
  2056. */
  2057. static int dspxfr_image(struct hda_codec *codec,
  2058. const struct dsp_image_seg *fls_data,
  2059. unsigned int reloc,
  2060. unsigned int sample_rate,
  2061. unsigned short channels,
  2062. bool ovly)
  2063. {
  2064. struct ca0132_spec *spec = codec->spec;
  2065. int status;
  2066. unsigned short hda_format = 0;
  2067. unsigned int response;
  2068. unsigned char stream_id = 0;
  2069. struct dma_engine *dma_engine;
  2070. unsigned int dma_chan;
  2071. unsigned int port_map_mask;
  2072. if (fls_data == NULL)
  2073. return -EINVAL;
  2074. dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
  2075. if (!dma_engine) {
  2076. status = -ENOMEM;
  2077. goto exit;
  2078. }
  2079. dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
  2080. if (!dma_engine->dmab) {
  2081. status = -ENOMEM;
  2082. goto exit;
  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 = 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 = (unsigned int)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_DSP_LOADER
  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)