patch_ca0132.c 124 KB

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