patch_ca0132.c 124 KB

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