patch_ca0132.c 112 KB

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