patch_ca0132.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914
  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. static int _add_switch(struct hda_codec *codec, hda_nid_t nid, const char *pfx,
  653. int chan, int dir)
  654. {
  655. char namestr[44];
  656. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  657. struct snd_kcontrol_new knew =
  658. HDA_CODEC_MUTE_MONO(namestr, nid, chan, 0, type);
  659. if ((query_amp_caps(codec, nid, type) & AC_AMPCAP_MUTE) == 0) {
  660. snd_printdd("Skipping '%s %s Switch' (no mute on node 0x%x)\n", pfx, dirstr[dir], nid);
  661. return 0;
  662. }
  663. sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
  664. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  665. }
  666. static int _add_volume(struct hda_codec *codec, hda_nid_t nid, const char *pfx,
  667. int chan, int dir)
  668. {
  669. char namestr[44];
  670. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  671. struct snd_kcontrol_new knew =
  672. HDA_CODEC_VOLUME_MONO(namestr, nid, chan, 0, type);
  673. if ((query_amp_caps(codec, nid, type) & AC_AMPCAP_NUM_STEPS) == 0) {
  674. snd_printdd("Skipping '%s %s Volume' (no amp on node 0x%x)\n", pfx, dirstr[dir], nid);
  675. return 0;
  676. }
  677. sprintf(namestr, "%s %s Volume", pfx, dirstr[dir]);
  678. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  679. }
  680. #define add_out_switch(codec, nid, pfx) _add_switch(codec, nid, pfx, 3, 0)
  681. #define add_out_volume(codec, nid, pfx) _add_volume(codec, nid, pfx, 3, 0)
  682. #define add_in_switch(codec, nid, pfx) _add_switch(codec, nid, pfx, 3, 1)
  683. #define add_in_volume(codec, nid, pfx) _add_volume(codec, nid, pfx, 3, 1)
  684. #define add_mono_switch(codec, nid, pfx, chan) \
  685. _add_switch(codec, nid, pfx, chan, 0)
  686. #define add_mono_volume(codec, nid, pfx, chan) \
  687. _add_volume(codec, nid, pfx, chan, 0)
  688. #define add_in_mono_switch(codec, nid, pfx, chan) \
  689. _add_switch(codec, nid, pfx, chan, 1)
  690. #define add_in_mono_volume(codec, nid, pfx, chan) \
  691. _add_volume(codec, nid, pfx, chan, 1)
  692. enum dsp_download_state {
  693. DSP_DOWNLOAD_FAILED = -1,
  694. DSP_DOWNLOAD_INIT = 0,
  695. DSP_DOWNLOADING = 1,
  696. DSP_DOWNLOADED = 2
  697. };
  698. /* retrieve parameters from hda format */
  699. #define get_hdafmt_chs(fmt) (fmt & 0xf)
  700. #define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
  701. #define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
  702. #define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
  703. /*
  704. * CA0132 specific
  705. */
  706. struct ca0132_spec {
  707. const struct hda_verb *base_init_verbs;
  708. const struct hda_verb *base_exit_verbs;
  709. const struct hda_verb *init_verbs[5];
  710. unsigned int num_init_verbs; /* exclude base init verbs */
  711. struct auto_pin_cfg autocfg;
  712. /* Nodes configurations */
  713. struct hda_multi_out multiout;
  714. hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
  715. hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
  716. hda_nid_t hp_dac;
  717. unsigned int num_outputs;
  718. hda_nid_t input_pins[AUTO_PIN_LAST];
  719. hda_nid_t adcs[AUTO_PIN_LAST];
  720. hda_nid_t dig_out;
  721. hda_nid_t dig_in;
  722. unsigned int num_inputs;
  723. long curr_hp_switch;
  724. long curr_hp_volume[2];
  725. long curr_speaker_switch;
  726. const char *input_labels[AUTO_PIN_LAST];
  727. struct hda_pcm pcm_rec[5]; /* PCM information */
  728. /* chip access */
  729. struct mutex chipio_mutex; /* chip access mutex */
  730. u32 curr_chip_addx;
  731. /* DSP download related */
  732. enum dsp_download_state dsp_state;
  733. unsigned int dsp_stream_id;
  734. unsigned int wait_scp;
  735. unsigned int wait_scp_header;
  736. unsigned int wait_num_data;
  737. unsigned int scp_resp_header;
  738. unsigned int scp_resp_data[4];
  739. unsigned int scp_resp_count;
  740. /* mixer and effects related */
  741. unsigned char dmic_ctl;
  742. int cur_out_type;
  743. int cur_mic_type;
  744. long vnode_lvol[VNODES_COUNT];
  745. long vnode_rvol[VNODES_COUNT];
  746. long vnode_lswitch[VNODES_COUNT];
  747. long vnode_rswitch[VNODES_COUNT];
  748. long effects_switch[EFFECTS_COUNT];
  749. long voicefx_val;
  750. long cur_mic_boost;
  751. };
  752. /*
  753. * CA0132 codec access
  754. */
  755. unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
  756. unsigned int verb, unsigned int parm, unsigned int *res)
  757. {
  758. unsigned int response;
  759. response = snd_hda_codec_read(codec, nid, 0, verb, parm);
  760. *res = response;
  761. return ((response == -1) ? -1 : 0);
  762. }
  763. static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
  764. unsigned short converter_format, unsigned int *res)
  765. {
  766. return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
  767. converter_format & 0xffff, res);
  768. }
  769. static int codec_set_converter_stream_channel(struct hda_codec *codec,
  770. hda_nid_t nid, unsigned char stream,
  771. unsigned char channel, unsigned int *res)
  772. {
  773. unsigned char converter_stream_channel = 0;
  774. converter_stream_channel = (stream << 4) | (channel & 0x0f);
  775. return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
  776. converter_stream_channel, res);
  777. }
  778. /* Chip access helper function */
  779. static int chipio_send(struct hda_codec *codec,
  780. unsigned int reg,
  781. unsigned int data)
  782. {
  783. unsigned int res;
  784. int retry = 50;
  785. /* send bits of data specified by reg */
  786. do {
  787. res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  788. reg, data);
  789. if (res == VENDOR_STATUS_CHIPIO_OK)
  790. return 0;
  791. } while (--retry);
  792. return -EIO;
  793. }
  794. /*
  795. * Write chip address through the vendor widget -- NOT protected by the Mutex!
  796. */
  797. static int chipio_write_address(struct hda_codec *codec,
  798. unsigned int chip_addx)
  799. {
  800. struct ca0132_spec *spec = codec->spec;
  801. int res;
  802. if (spec->curr_chip_addx == chip_addx)
  803. return 0;
  804. /* send low 16 bits of the address */
  805. res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
  806. chip_addx & 0xffff);
  807. if (res != -EIO) {
  808. /* send high 16 bits of the address */
  809. res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
  810. chip_addx >> 16);
  811. }
  812. spec->curr_chip_addx = (res < 0) ? ~0UL : chip_addx;
  813. return res;
  814. }
  815. /*
  816. * Write data through the vendor widget -- NOT protected by the Mutex!
  817. */
  818. static int chipio_write_data(struct hda_codec *codec, unsigned int data)
  819. {
  820. struct ca0132_spec *spec = codec->spec;
  821. int res;
  822. /* send low 16 bits of the data */
  823. res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
  824. if (res != -EIO) {
  825. /* send high 16 bits of the data */
  826. res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
  827. data >> 16);
  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 multiple data through the vendor widget -- NOT protected by the Mutex!
  837. */
  838. static int chipio_write_data_multiple(struct hda_codec *codec,
  839. const u32 *data,
  840. unsigned int count)
  841. {
  842. int status = 0;
  843. if (data == NULL) {
  844. snd_printdd(KERN_ERR "chipio_write_data null ptr");
  845. return -EINVAL;
  846. }
  847. while ((count-- != 0) && (status == 0))
  848. status = chipio_write_data(codec, *data++);
  849. return status;
  850. }
  851. /*
  852. * Read data through the vendor widget -- NOT protected by the Mutex!
  853. */
  854. static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
  855. {
  856. struct ca0132_spec *spec = codec->spec;
  857. int res;
  858. /* post read */
  859. res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
  860. if (res != -EIO) {
  861. /* read status */
  862. res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  863. }
  864. if (res != -EIO) {
  865. /* read data */
  866. *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  867. VENDOR_CHIPIO_HIC_READ_DATA,
  868. 0);
  869. }
  870. /*If no error encountered, automatically increment the address
  871. as per chip behaviour*/
  872. spec->curr_chip_addx = (res != -EIO) ?
  873. (spec->curr_chip_addx + 4) : ~0UL;
  874. return res;
  875. }
  876. /*
  877. * Write given value to the given address through the chip I/O widget.
  878. * protected by the Mutex
  879. */
  880. static int chipio_write(struct hda_codec *codec,
  881. unsigned int chip_addx, const 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_write_data(codec, data);
  891. if (err < 0)
  892. goto exit;
  893. exit:
  894. mutex_unlock(&spec->chipio_mutex);
  895. return err;
  896. }
  897. /*
  898. * Write multiple values to the given address through the chip I/O widget.
  899. * protected by the Mutex
  900. */
  901. static int chipio_write_multiple(struct hda_codec *codec,
  902. u32 chip_addx,
  903. const u32 *data,
  904. unsigned int count)
  905. {
  906. struct ca0132_spec *spec = codec->spec;
  907. int status;
  908. mutex_lock(&spec->chipio_mutex);
  909. status = chipio_write_address(codec, chip_addx);
  910. if (status < 0)
  911. goto error;
  912. status = chipio_write_data_multiple(codec, data, count);
  913. error:
  914. mutex_unlock(&spec->chipio_mutex);
  915. return status;
  916. }
  917. /*
  918. * Read the given address through the chip I/O widget
  919. * protected by the Mutex
  920. */
  921. static int chipio_read(struct hda_codec *codec,
  922. unsigned int chip_addx, unsigned int *data)
  923. {
  924. struct ca0132_spec *spec = codec->spec;
  925. int err;
  926. mutex_lock(&spec->chipio_mutex);
  927. /* write the address, and if successful proceed to write data */
  928. err = chipio_write_address(codec, chip_addx);
  929. if (err < 0)
  930. goto exit;
  931. err = chipio_read_data(codec, data);
  932. if (err < 0)
  933. goto exit;
  934. exit:
  935. mutex_unlock(&spec->chipio_mutex);
  936. return err;
  937. }
  938. /*
  939. * Set chip control flags through the chip I/O widget.
  940. */
  941. static void chipio_set_control_flag(struct hda_codec *codec,
  942. enum control_flag_id flag_id,
  943. bool flag_state)
  944. {
  945. unsigned int val;
  946. unsigned int flag_bit;
  947. flag_bit = (flag_state ? 1 : 0);
  948. val = (flag_bit << 7) | (flag_id);
  949. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  950. VENDOR_CHIPIO_FLAG_SET, val);
  951. }
  952. /*
  953. * Set chip parameters through the chip I/O widget.
  954. */
  955. static void chipio_set_control_param(struct hda_codec *codec,
  956. enum control_param_id param_id, int param_val)
  957. {
  958. struct ca0132_spec *spec = codec->spec;
  959. int val;
  960. if ((param_id < 32) && (param_val < 8)) {
  961. val = (param_val << 5) | (param_id);
  962. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  963. VENDOR_CHIPIO_PARAM_SET, val);
  964. } else {
  965. mutex_lock(&spec->chipio_mutex);
  966. if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
  967. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  968. VENDOR_CHIPIO_PARAM_EX_ID_SET,
  969. param_id);
  970. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  971. VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
  972. param_val);
  973. }
  974. mutex_unlock(&spec->chipio_mutex);
  975. }
  976. }
  977. /*
  978. * Set sampling rate of the connection point.
  979. */
  980. static void chipio_set_conn_rate(struct hda_codec *codec,
  981. int connid, enum ca0132_sample_rate rate)
  982. {
  983. chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
  984. chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
  985. rate);
  986. }
  987. /*
  988. * Enable clocks.
  989. */
  990. static void chipio_enable_clocks(struct hda_codec *codec)
  991. {
  992. struct ca0132_spec *spec = codec->spec;
  993. mutex_lock(&spec->chipio_mutex);
  994. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  995. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
  996. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  997. VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
  998. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  999. VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
  1000. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1001. VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
  1002. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1003. VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
  1004. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1005. VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
  1006. mutex_unlock(&spec->chipio_mutex);
  1007. }
  1008. /*
  1009. * CA0132 DSP IO stuffs
  1010. */
  1011. static int dspio_send(struct hda_codec *codec, unsigned int reg,
  1012. unsigned int data)
  1013. {
  1014. unsigned int res;
  1015. int retry = 50;
  1016. /* send bits of data specified by reg to dsp */
  1017. do {
  1018. res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
  1019. if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
  1020. return res;
  1021. } while (--retry);
  1022. return -EIO;
  1023. }
  1024. /*
  1025. * Wait for DSP to be ready for commands
  1026. */
  1027. static void dspio_write_wait(struct hda_codec *codec)
  1028. {
  1029. int status;
  1030. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  1031. do {
  1032. status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  1033. VENDOR_DSPIO_STATUS, 0);
  1034. if ((status == VENDOR_STATUS_DSPIO_OK) ||
  1035. (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
  1036. break;
  1037. msleep(1);
  1038. } while (time_before(jiffies, timeout));
  1039. }
  1040. /*
  1041. * Write SCP data to DSP
  1042. */
  1043. static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
  1044. {
  1045. struct ca0132_spec *spec = codec->spec;
  1046. int status;
  1047. dspio_write_wait(codec);
  1048. mutex_lock(&spec->chipio_mutex);
  1049. status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
  1050. scp_data & 0xffff);
  1051. if (status < 0)
  1052. goto error;
  1053. status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
  1054. scp_data >> 16);
  1055. if (status < 0)
  1056. goto error;
  1057. /* OK, now check if the write itself has executed*/
  1058. status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  1059. VENDOR_DSPIO_STATUS, 0);
  1060. error:
  1061. mutex_unlock(&spec->chipio_mutex);
  1062. return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
  1063. -EIO : 0;
  1064. }
  1065. /*
  1066. * Write multiple SCP data to DSP
  1067. */
  1068. static int dspio_write_multiple(struct hda_codec *codec,
  1069. unsigned int *buffer, unsigned int size)
  1070. {
  1071. int status = 0;
  1072. unsigned int count;
  1073. if ((buffer == NULL))
  1074. return -EINVAL;
  1075. count = 0;
  1076. while (count < size) {
  1077. status = dspio_write(codec, *buffer++);
  1078. if (status != 0)
  1079. break;
  1080. count++;
  1081. }
  1082. return status;
  1083. }
  1084. /*
  1085. * Construct the SCP header using corresponding fields
  1086. */
  1087. static inline unsigned int
  1088. make_scp_header(unsigned int target_id, unsigned int source_id,
  1089. unsigned int get_flag, unsigned int req,
  1090. unsigned int device_flag, unsigned int resp_flag,
  1091. unsigned int error_flag, unsigned int data_size)
  1092. {
  1093. unsigned int header = 0;
  1094. header = (data_size & 0x1f) << 27;
  1095. header |= (error_flag & 0x01) << 26;
  1096. header |= (resp_flag & 0x01) << 25;
  1097. header |= (device_flag & 0x01) << 24;
  1098. header |= (req & 0x7f) << 17;
  1099. header |= (get_flag & 0x01) << 16;
  1100. header |= (source_id & 0xff) << 8;
  1101. header |= target_id & 0xff;
  1102. return header;
  1103. }
  1104. /*
  1105. * Extract corresponding fields from SCP header
  1106. */
  1107. static inline void
  1108. extract_scp_header(unsigned int header,
  1109. unsigned int *target_id, unsigned int *source_id,
  1110. unsigned int *get_flag, unsigned int *req,
  1111. unsigned int *device_flag, unsigned int *resp_flag,
  1112. unsigned int *error_flag, unsigned int *data_size)
  1113. {
  1114. if (data_size)
  1115. *data_size = (header >> 27) & 0x1f;
  1116. if (error_flag)
  1117. *error_flag = (header >> 26) & 0x01;
  1118. if (resp_flag)
  1119. *resp_flag = (header >> 25) & 0x01;
  1120. if (device_flag)
  1121. *device_flag = (header >> 24) & 0x01;
  1122. if (req)
  1123. *req = (header >> 17) & 0x7f;
  1124. if (get_flag)
  1125. *get_flag = (header >> 16) & 0x01;
  1126. if (source_id)
  1127. *source_id = (header >> 8) & 0xff;
  1128. if (target_id)
  1129. *target_id = header & 0xff;
  1130. }
  1131. #define SCP_MAX_DATA_WORDS (16)
  1132. /* Structure to contain any SCP message */
  1133. struct scp_msg {
  1134. unsigned int hdr;
  1135. unsigned int data[SCP_MAX_DATA_WORDS];
  1136. };
  1137. /*
  1138. * Send SCP message to DSP
  1139. */
  1140. static int dspio_send_scp_message(struct hda_codec *codec,
  1141. unsigned char *send_buf,
  1142. unsigned int send_buf_size,
  1143. unsigned char *return_buf,
  1144. unsigned int return_buf_size,
  1145. unsigned int *bytes_returned)
  1146. {
  1147. struct ca0132_spec *spec = codec->spec;
  1148. int retry;
  1149. int status = -1;
  1150. unsigned int scp_send_size = 0;
  1151. unsigned int total_size;
  1152. bool waiting_for_resp = false;
  1153. unsigned int header;
  1154. struct scp_msg *ret_msg;
  1155. unsigned int resp_src_id, resp_target_id;
  1156. unsigned int data_size, src_id, target_id, get_flag, device_flag;
  1157. if (bytes_returned)
  1158. *bytes_returned = 0;
  1159. /* get scp header from buffer */
  1160. header = *((unsigned int *)send_buf);
  1161. extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
  1162. &device_flag, NULL, NULL, &data_size);
  1163. scp_send_size = data_size + 1;
  1164. total_size = (scp_send_size * 4);
  1165. if (send_buf_size < total_size)
  1166. return -EINVAL;
  1167. if (get_flag || device_flag) {
  1168. if (!return_buf || return_buf_size < 4 || !bytes_returned)
  1169. return -EINVAL;
  1170. spec->wait_scp_header = *((unsigned int *)send_buf);
  1171. /* swap source id with target id */
  1172. resp_target_id = src_id;
  1173. resp_src_id = target_id;
  1174. spec->wait_scp_header &= 0xffff0000;
  1175. spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
  1176. spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
  1177. spec->wait_scp = 1;
  1178. waiting_for_resp = true;
  1179. }
  1180. status = dspio_write_multiple(codec, (unsigned int *)send_buf,
  1181. scp_send_size);
  1182. if (status < 0) {
  1183. spec->wait_scp = 0;
  1184. return status;
  1185. }
  1186. if (waiting_for_resp) {
  1187. memset(return_buf, 0, return_buf_size);
  1188. retry = 50;
  1189. do {
  1190. msleep(20);
  1191. } while (spec->wait_scp && (--retry != 0));
  1192. waiting_for_resp = false;
  1193. if (retry != 0) {
  1194. ret_msg = (struct scp_msg *)return_buf;
  1195. memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
  1196. memcpy(&ret_msg->data, spec->scp_resp_data,
  1197. spec->wait_num_data);
  1198. *bytes_returned = (spec->scp_resp_count + 1) * 4;
  1199. status = 0;
  1200. } else {
  1201. status = -EIO;
  1202. }
  1203. spec->wait_scp = 0;
  1204. }
  1205. return status;
  1206. }
  1207. /**
  1208. * Prepare and send the SCP message to DSP
  1209. * @codec: the HDA codec
  1210. * @mod_id: ID of the DSP module to send the command
  1211. * @req: ID of request to send to the DSP module
  1212. * @dir: SET or GET
  1213. * @data: pointer to the data to send with the request, request specific
  1214. * @len: length of the data, in bytes
  1215. * @reply: point to the buffer to hold data returned for a reply
  1216. * @reply_len: length of the reply buffer returned from GET
  1217. *
  1218. * Returns zero or a negative error code.
  1219. */
  1220. static int dspio_scp(struct hda_codec *codec,
  1221. int mod_id, int req, int dir, void *data, unsigned int len,
  1222. void *reply, unsigned int *reply_len)
  1223. {
  1224. int status = 0;
  1225. struct scp_msg scp_send, scp_reply;
  1226. unsigned int ret_bytes, send_size, ret_size;
  1227. unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
  1228. unsigned int reply_data_size;
  1229. memset(&scp_send, 0, sizeof(scp_send));
  1230. memset(&scp_reply, 0, sizeof(scp_reply));
  1231. if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
  1232. return -EINVAL;
  1233. if (dir == SCP_GET && reply == NULL) {
  1234. snd_printdd(KERN_ERR "dspio_scp get but has no buffer");
  1235. return -EINVAL;
  1236. }
  1237. if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
  1238. snd_printdd(KERN_ERR "dspio_scp bad resp buf len parms");
  1239. return -EINVAL;
  1240. }
  1241. scp_send.hdr = make_scp_header(mod_id, 0x20, (dir == SCP_GET), req,
  1242. 0, 0, 0, len/sizeof(unsigned int));
  1243. if (data != NULL && len > 0) {
  1244. len = min((unsigned int)(sizeof(scp_send.data)), len);
  1245. memcpy(scp_send.data, data, len);
  1246. }
  1247. ret_bytes = 0;
  1248. send_size = sizeof(unsigned int) + len;
  1249. status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
  1250. send_size, (unsigned char *)&scp_reply,
  1251. sizeof(scp_reply), &ret_bytes);
  1252. if (status < 0) {
  1253. snd_printdd(KERN_ERR "dspio_scp: send scp msg failed");
  1254. return status;
  1255. }
  1256. /* extract send and reply headers members */
  1257. extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
  1258. NULL, NULL, NULL, NULL, NULL);
  1259. extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
  1260. &reply_resp_flag, &reply_error_flag,
  1261. &reply_data_size);
  1262. if (!send_get_flag)
  1263. return 0;
  1264. if (reply_resp_flag && !reply_error_flag) {
  1265. ret_size = (ret_bytes - sizeof(scp_reply.hdr))
  1266. / sizeof(unsigned int);
  1267. if (*reply_len < ret_size*sizeof(unsigned int)) {
  1268. snd_printdd(KERN_ERR "reply too long for buf");
  1269. return -EINVAL;
  1270. } else if (ret_size != reply_data_size) {
  1271. snd_printdd(KERN_ERR "RetLen and HdrLen .NE.");
  1272. return -EINVAL;
  1273. } else {
  1274. *reply_len = ret_size*sizeof(unsigned int);
  1275. memcpy(reply, scp_reply.data, *reply_len);
  1276. }
  1277. } else {
  1278. snd_printdd(KERN_ERR "reply ill-formed or errflag set");
  1279. return -EIO;
  1280. }
  1281. return status;
  1282. }
  1283. /*
  1284. * Set DSP parameters
  1285. */
  1286. static int dspio_set_param(struct hda_codec *codec, int mod_id,
  1287. int req, void *data, unsigned int len)
  1288. {
  1289. return dspio_scp(codec, mod_id, req, SCP_SET, data, len, NULL, NULL);
  1290. }
  1291. static int dspio_set_uint_param(struct hda_codec *codec, int mod_id,
  1292. int req, unsigned int data)
  1293. {
  1294. return dspio_set_param(codec, mod_id, req, &data, sizeof(unsigned int));
  1295. }
  1296. /*
  1297. * Allocate a DSP DMA channel via an SCP message
  1298. */
  1299. static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
  1300. {
  1301. int status = 0;
  1302. unsigned int size = sizeof(dma_chan);
  1303. snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- begin");
  1304. status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
  1305. SCP_GET, NULL, 0, dma_chan, &size);
  1306. if (status < 0) {
  1307. snd_printdd(KERN_INFO "dspio_alloc_dma_chan: SCP Failed");
  1308. return status;
  1309. }
  1310. if ((*dma_chan + 1) == 0) {
  1311. snd_printdd(KERN_INFO "no free dma channels to allocate");
  1312. return -EBUSY;
  1313. }
  1314. snd_printdd("dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
  1315. snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- complete");
  1316. return status;
  1317. }
  1318. /*
  1319. * Free a DSP DMA via an SCP message
  1320. */
  1321. static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
  1322. {
  1323. int status = 0;
  1324. unsigned int dummy = 0;
  1325. snd_printdd(KERN_INFO " dspio_free_dma_chan() -- begin");
  1326. snd_printdd("dspio_free_dma_chan: chan=%d\n", dma_chan);
  1327. status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
  1328. SCP_SET, &dma_chan, sizeof(dma_chan), NULL, &dummy);
  1329. if (status < 0) {
  1330. snd_printdd(KERN_INFO "dspio_free_dma_chan: SCP Failed");
  1331. return status;
  1332. }
  1333. snd_printdd(KERN_INFO " dspio_free_dma_chan() -- complete");
  1334. return status;
  1335. }
  1336. /*
  1337. * (Re)start the DSP
  1338. */
  1339. static int dsp_set_run_state(struct hda_codec *codec)
  1340. {
  1341. unsigned int dbg_ctrl_reg;
  1342. unsigned int halt_state;
  1343. int err;
  1344. err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
  1345. if (err < 0)
  1346. return err;
  1347. halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
  1348. DSP_DBGCNTL_STATE_LOBIT;
  1349. if (halt_state != 0) {
  1350. dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
  1351. DSP_DBGCNTL_SS_MASK);
  1352. err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
  1353. dbg_ctrl_reg);
  1354. if (err < 0)
  1355. return err;
  1356. dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
  1357. DSP_DBGCNTL_EXEC_MASK;
  1358. err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
  1359. dbg_ctrl_reg);
  1360. if (err < 0)
  1361. return err;
  1362. }
  1363. return 0;
  1364. }
  1365. /*
  1366. * Reset the DSP
  1367. */
  1368. static int dsp_reset(struct hda_codec *codec)
  1369. {
  1370. unsigned int res;
  1371. int retry = 20;
  1372. snd_printdd("dsp_reset\n");
  1373. do {
  1374. res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
  1375. retry--;
  1376. } while (res == -EIO && retry);
  1377. if (!retry) {
  1378. snd_printdd("dsp_reset timeout\n");
  1379. return -EIO;
  1380. }
  1381. return 0;
  1382. }
  1383. /*
  1384. * Convert chip address to DSP address
  1385. */
  1386. static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
  1387. bool *code, bool *yram)
  1388. {
  1389. *code = *yram = false;
  1390. if (UC_RANGE(chip_addx, 1)) {
  1391. *code = true;
  1392. return UC_OFF(chip_addx);
  1393. } else if (X_RANGE_ALL(chip_addx, 1)) {
  1394. return X_OFF(chip_addx);
  1395. } else if (Y_RANGE_ALL(chip_addx, 1)) {
  1396. *yram = true;
  1397. return Y_OFF(chip_addx);
  1398. }
  1399. return (unsigned int)INVALID_CHIP_ADDRESS;
  1400. }
  1401. /*
  1402. * Check if the DSP DMA is active
  1403. */
  1404. static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
  1405. {
  1406. unsigned int dma_chnlstart_reg;
  1407. chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
  1408. return ((dma_chnlstart_reg & (1 <<
  1409. (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
  1410. }
  1411. static int dsp_dma_setup_common(struct hda_codec *codec,
  1412. unsigned int chip_addx,
  1413. unsigned int dma_chan,
  1414. unsigned int port_map_mask,
  1415. bool ovly)
  1416. {
  1417. int status = 0;
  1418. unsigned int chnl_prop;
  1419. unsigned int dsp_addx;
  1420. unsigned int active;
  1421. bool code, yram;
  1422. snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Begin ---------");
  1423. if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
  1424. snd_printdd(KERN_ERR "dma chan num invalid");
  1425. return -EINVAL;
  1426. }
  1427. if (dsp_is_dma_active(codec, dma_chan)) {
  1428. snd_printdd(KERN_ERR "dma already active");
  1429. return -EBUSY;
  1430. }
  1431. dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
  1432. if (dsp_addx == INVALID_CHIP_ADDRESS) {
  1433. snd_printdd(KERN_ERR "invalid chip addr");
  1434. return -ENXIO;
  1435. }
  1436. chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
  1437. active = 0;
  1438. snd_printdd(KERN_INFO " dsp_dma_setup_common() start reg pgm");
  1439. if (ovly) {
  1440. status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
  1441. &chnl_prop);
  1442. if (status < 0) {
  1443. snd_printdd(KERN_ERR "read CHNLPROP Reg fail");
  1444. return status;
  1445. }
  1446. snd_printdd(KERN_INFO "dsp_dma_setup_common() Read CHNLPROP");
  1447. }
  1448. if (!code)
  1449. chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
  1450. else
  1451. chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
  1452. chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
  1453. status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
  1454. if (status < 0) {
  1455. snd_printdd(KERN_ERR "write CHNLPROP Reg fail");
  1456. return status;
  1457. }
  1458. snd_printdd(KERN_INFO " dsp_dma_setup_common() Write CHNLPROP");
  1459. if (ovly) {
  1460. status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
  1461. &active);
  1462. if (status < 0) {
  1463. snd_printdd(KERN_ERR "read ACTIVE Reg fail");
  1464. return status;
  1465. }
  1466. snd_printdd(KERN_INFO "dsp_dma_setup_common() Read ACTIVE");
  1467. }
  1468. active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
  1469. DSPDMAC_ACTIVE_AAR_MASK;
  1470. status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
  1471. if (status < 0) {
  1472. snd_printdd(KERN_ERR "write ACTIVE Reg fail");
  1473. return status;
  1474. }
  1475. snd_printdd(KERN_INFO " dsp_dma_setup_common() Write ACTIVE");
  1476. status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
  1477. port_map_mask);
  1478. if (status < 0) {
  1479. snd_printdd(KERN_ERR "write AUDCHSEL Reg fail");
  1480. return status;
  1481. }
  1482. snd_printdd(KERN_INFO " dsp_dma_setup_common() Write AUDCHSEL");
  1483. status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
  1484. DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
  1485. if (status < 0) {
  1486. snd_printdd(KERN_ERR "write IRQCNT Reg fail");
  1487. return status;
  1488. }
  1489. snd_printdd(KERN_INFO " dsp_dma_setup_common() Write IRQCNT");
  1490. snd_printdd(
  1491. "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
  1492. "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
  1493. chip_addx, dsp_addx, dma_chan,
  1494. port_map_mask, chnl_prop, active);
  1495. snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Complete ------");
  1496. return 0;
  1497. }
  1498. /*
  1499. * Setup the DSP DMA per-transfer-specific registers
  1500. */
  1501. static int dsp_dma_setup(struct hda_codec *codec,
  1502. unsigned int chip_addx,
  1503. unsigned int count,
  1504. unsigned int dma_chan)
  1505. {
  1506. int status = 0;
  1507. bool code, yram;
  1508. unsigned int dsp_addx;
  1509. unsigned int addr_field;
  1510. unsigned int incr_field;
  1511. unsigned int base_cnt;
  1512. unsigned int cur_cnt;
  1513. unsigned int dma_cfg = 0;
  1514. unsigned int adr_ofs = 0;
  1515. unsigned int xfr_cnt = 0;
  1516. const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
  1517. DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
  1518. snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Begin ---------");
  1519. if (count > max_dma_count) {
  1520. snd_printdd(KERN_ERR "count too big");
  1521. return -EINVAL;
  1522. }
  1523. dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
  1524. if (dsp_addx == INVALID_CHIP_ADDRESS) {
  1525. snd_printdd(KERN_ERR "invalid chip addr");
  1526. return -ENXIO;
  1527. }
  1528. snd_printdd(KERN_INFO " dsp_dma_setup() start reg pgm");
  1529. addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
  1530. incr_field = 0;
  1531. if (!code) {
  1532. addr_field <<= 1;
  1533. if (yram)
  1534. addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
  1535. incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
  1536. }
  1537. dma_cfg = addr_field + incr_field;
  1538. status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
  1539. dma_cfg);
  1540. if (status < 0) {
  1541. snd_printdd(KERN_ERR "write DMACFG Reg fail");
  1542. return status;
  1543. }
  1544. snd_printdd(KERN_INFO " dsp_dma_setup() Write DMACFG");
  1545. adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
  1546. (code ? 0 : 1));
  1547. status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
  1548. adr_ofs);
  1549. if (status < 0) {
  1550. snd_printdd(KERN_ERR "write DSPADROFS Reg fail");
  1551. return status;
  1552. }
  1553. snd_printdd(KERN_INFO " dsp_dma_setup() Write DSPADROFS");
  1554. base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
  1555. cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
  1556. xfr_cnt = base_cnt | cur_cnt;
  1557. status = chipio_write(codec,
  1558. DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
  1559. if (status < 0) {
  1560. snd_printdd(KERN_ERR "write XFRCNT Reg fail");
  1561. return status;
  1562. }
  1563. snd_printdd(KERN_INFO " dsp_dma_setup() Write XFRCNT");
  1564. snd_printdd(
  1565. "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
  1566. "ADROFS=0x%x, XFRCNT=0x%x\n",
  1567. chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
  1568. snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Complete ---------");
  1569. return 0;
  1570. }
  1571. /*
  1572. * Start the DSP DMA
  1573. */
  1574. static int dsp_dma_start(struct hda_codec *codec,
  1575. unsigned int dma_chan, bool ovly)
  1576. {
  1577. unsigned int reg = 0;
  1578. int status = 0;
  1579. snd_printdd(KERN_INFO "-- dsp_dma_start() -- Begin ---------");
  1580. if (ovly) {
  1581. status = chipio_read(codec,
  1582. DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
  1583. if (status < 0) {
  1584. snd_printdd(KERN_ERR "read CHNLSTART reg fail");
  1585. return status;
  1586. }
  1587. snd_printdd(KERN_INFO "-- dsp_dma_start() Read CHNLSTART");
  1588. reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
  1589. DSPDMAC_CHNLSTART_DIS_MASK);
  1590. }
  1591. status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
  1592. reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
  1593. if (status < 0) {
  1594. snd_printdd(KERN_ERR "write CHNLSTART reg fail");
  1595. return status;
  1596. }
  1597. snd_printdd(KERN_INFO "-- dsp_dma_start() -- Complete ---------");
  1598. return status;
  1599. }
  1600. /*
  1601. * Stop the DSP DMA
  1602. */
  1603. static int dsp_dma_stop(struct hda_codec *codec,
  1604. unsigned int dma_chan, bool ovly)
  1605. {
  1606. unsigned int reg = 0;
  1607. int status = 0;
  1608. snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Begin ---------");
  1609. if (ovly) {
  1610. status = chipio_read(codec,
  1611. DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
  1612. if (status < 0) {
  1613. snd_printdd(KERN_ERR "read CHNLSTART reg fail");
  1614. return status;
  1615. }
  1616. snd_printdd(KERN_INFO "-- dsp_dma_stop() Read CHNLSTART");
  1617. reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
  1618. DSPDMAC_CHNLSTART_DIS_MASK);
  1619. }
  1620. status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
  1621. reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
  1622. if (status < 0) {
  1623. snd_printdd(KERN_ERR "write CHNLSTART reg fail");
  1624. return status;
  1625. }
  1626. snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Complete ---------");
  1627. return status;
  1628. }
  1629. /**
  1630. * Allocate router ports
  1631. *
  1632. * @codec: the HDA codec
  1633. * @num_chans: number of channels in the stream
  1634. * @ports_per_channel: number of ports per channel
  1635. * @start_device: start device
  1636. * @port_map: pointer to the port list to hold the allocated ports
  1637. *
  1638. * Returns zero or a negative error code.
  1639. */
  1640. static int dsp_allocate_router_ports(struct hda_codec *codec,
  1641. unsigned int num_chans,
  1642. unsigned int ports_per_channel,
  1643. unsigned int start_device,
  1644. unsigned int *port_map)
  1645. {
  1646. int status = 0;
  1647. int res;
  1648. u8 val;
  1649. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1650. if (status < 0)
  1651. return status;
  1652. val = start_device << 6;
  1653. val |= (ports_per_channel - 1) << 4;
  1654. val |= num_chans - 1;
  1655. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1656. VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
  1657. val);
  1658. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1659. VENDOR_CHIPIO_PORT_ALLOC_SET,
  1660. MEM_CONNID_DSP);
  1661. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1662. if (status < 0)
  1663. return status;
  1664. res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  1665. VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
  1666. *port_map = res;
  1667. return (res < 0) ? res : 0;
  1668. }
  1669. /*
  1670. * Free router ports
  1671. */
  1672. static int dsp_free_router_ports(struct hda_codec *codec)
  1673. {
  1674. int status = 0;
  1675. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1676. if (status < 0)
  1677. return status;
  1678. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1679. VENDOR_CHIPIO_PORT_FREE_SET,
  1680. MEM_CONNID_DSP);
  1681. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1682. return status;
  1683. }
  1684. /*
  1685. * Allocate DSP ports for the download stream
  1686. */
  1687. static int dsp_allocate_ports(struct hda_codec *codec,
  1688. unsigned int num_chans,
  1689. unsigned int rate_multi, unsigned int *port_map)
  1690. {
  1691. int status;
  1692. snd_printdd(KERN_INFO " dsp_allocate_ports() -- begin");
  1693. if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
  1694. snd_printdd(KERN_ERR "bad rate multiple");
  1695. return -EINVAL;
  1696. }
  1697. status = dsp_allocate_router_ports(codec, num_chans,
  1698. rate_multi, 0, port_map);
  1699. snd_printdd(KERN_INFO " dsp_allocate_ports() -- complete");
  1700. return status;
  1701. }
  1702. static int dsp_allocate_ports_format(struct hda_codec *codec,
  1703. const unsigned short fmt,
  1704. unsigned int *port_map)
  1705. {
  1706. int status;
  1707. unsigned int num_chans;
  1708. unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
  1709. unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
  1710. unsigned int rate_multi = sample_rate_mul / sample_rate_div;
  1711. if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
  1712. snd_printdd(KERN_ERR "bad rate multiple");
  1713. return -EINVAL;
  1714. }
  1715. num_chans = get_hdafmt_chs(fmt) + 1;
  1716. status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
  1717. return status;
  1718. }
  1719. /*
  1720. * free DSP ports
  1721. */
  1722. static int dsp_free_ports(struct hda_codec *codec)
  1723. {
  1724. int status;
  1725. snd_printdd(KERN_INFO " dsp_free_ports() -- begin");
  1726. status = dsp_free_router_ports(codec);
  1727. if (status < 0) {
  1728. snd_printdd(KERN_ERR "free router ports fail");
  1729. return status;
  1730. }
  1731. snd_printdd(KERN_INFO " dsp_free_ports() -- complete");
  1732. return status;
  1733. }
  1734. /*
  1735. * HDA DMA engine stuffs for DSP code download
  1736. */
  1737. struct dma_engine {
  1738. struct hda_codec *codec;
  1739. unsigned short m_converter_format;
  1740. struct snd_dma_buffer *dmab;
  1741. unsigned int buf_size;
  1742. };
  1743. enum dma_state {
  1744. DMA_STATE_STOP = 0,
  1745. DMA_STATE_RUN = 1
  1746. };
  1747. static int dma_convert_to_hda_format(
  1748. unsigned int sample_rate,
  1749. unsigned short channels,
  1750. unsigned short *hda_format)
  1751. {
  1752. unsigned int format_val;
  1753. format_val = snd_hda_calc_stream_format(
  1754. sample_rate,
  1755. channels,
  1756. SNDRV_PCM_FORMAT_S32_LE,
  1757. 32, 0);
  1758. if (hda_format)
  1759. *hda_format = (unsigned short)format_val;
  1760. return 0;
  1761. }
  1762. /*
  1763. * Reset DMA for DSP download
  1764. */
  1765. static int dma_reset(struct dma_engine *dma)
  1766. {
  1767. struct hda_codec *codec = dma->codec;
  1768. struct ca0132_spec *spec = codec->spec;
  1769. int status;
  1770. if (dma->dmab)
  1771. snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
  1772. status = snd_hda_codec_load_dsp_prepare(codec,
  1773. dma->m_converter_format,
  1774. dma->buf_size,
  1775. dma->dmab);
  1776. if (status < 0)
  1777. return status;
  1778. spec->dsp_stream_id = status;
  1779. return 0;
  1780. }
  1781. static int dma_set_state(struct dma_engine *dma, enum dma_state state)
  1782. {
  1783. bool cmd;
  1784. snd_printdd("dma_set_state state=%d\n", state);
  1785. switch (state) {
  1786. case DMA_STATE_STOP:
  1787. cmd = false;
  1788. break;
  1789. case DMA_STATE_RUN:
  1790. cmd = true;
  1791. break;
  1792. default:
  1793. return 0;
  1794. }
  1795. snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
  1796. return 0;
  1797. }
  1798. static unsigned int dma_get_buffer_size(struct dma_engine *dma)
  1799. {
  1800. return dma->dmab->bytes;
  1801. }
  1802. static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
  1803. {
  1804. return dma->dmab->area;
  1805. }
  1806. static int dma_xfer(struct dma_engine *dma,
  1807. const unsigned int *data,
  1808. unsigned int count)
  1809. {
  1810. memcpy(dma->dmab->area, data, count);
  1811. return 0;
  1812. }
  1813. static void dma_get_converter_format(
  1814. struct dma_engine *dma,
  1815. unsigned short *format)
  1816. {
  1817. if (format)
  1818. *format = dma->m_converter_format;
  1819. }
  1820. static unsigned int dma_get_stream_id(struct dma_engine *dma)
  1821. {
  1822. struct ca0132_spec *spec = dma->codec->spec;
  1823. return spec->dsp_stream_id;
  1824. }
  1825. struct dsp_image_seg {
  1826. u32 magic;
  1827. u32 chip_addr;
  1828. u32 count;
  1829. u32 data[0];
  1830. };
  1831. static const u32 g_magic_value = 0x4c46584d;
  1832. static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
  1833. static bool is_valid(const struct dsp_image_seg *p)
  1834. {
  1835. return p->magic == g_magic_value;
  1836. }
  1837. static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
  1838. {
  1839. return g_chip_addr_magic_value == p->chip_addr;
  1840. }
  1841. static bool is_last(const struct dsp_image_seg *p)
  1842. {
  1843. return p->count == 0;
  1844. }
  1845. static size_t dsp_sizeof(const struct dsp_image_seg *p)
  1846. {
  1847. return sizeof(*p) + p->count*sizeof(u32);
  1848. }
  1849. static const struct dsp_image_seg *get_next_seg_ptr(
  1850. const struct dsp_image_seg *p)
  1851. {
  1852. return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
  1853. }
  1854. /*
  1855. * CA0132 chip DSP transfer stuffs. For DSP download.
  1856. */
  1857. #define INVALID_DMA_CHANNEL (~0UL)
  1858. /*
  1859. * Program a list of address/data pairs via the ChipIO widget.
  1860. * The segment data is in the format of successive pairs of words.
  1861. * These are repeated as indicated by the segment's count field.
  1862. */
  1863. static int dspxfr_hci_write(struct hda_codec *codec,
  1864. const struct dsp_image_seg *fls)
  1865. {
  1866. int status;
  1867. const u32 *data;
  1868. unsigned int count;
  1869. if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
  1870. snd_printdd(KERN_ERR "hci_write invalid params");
  1871. return -EINVAL;
  1872. }
  1873. count = fls->count;
  1874. data = (u32 *)(fls->data);
  1875. while (count >= 2) {
  1876. status = chipio_write(codec, data[0], data[1]);
  1877. if (status < 0) {
  1878. snd_printdd(KERN_ERR "hci_write chipio failed");
  1879. return status;
  1880. }
  1881. count -= 2;
  1882. data += 2;
  1883. }
  1884. return 0;
  1885. }
  1886. /**
  1887. * Write a block of data into DSP code or data RAM using pre-allocated
  1888. * DMA engine.
  1889. *
  1890. * @codec: the HDA codec
  1891. * @fls: pointer to a fast load image
  1892. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  1893. * no relocation
  1894. * @dma_engine: pointer to DMA engine to be used for DSP download
  1895. * @dma_chan: The number of DMA channels used for DSP download
  1896. * @port_map_mask: port mapping
  1897. * @ovly: TRUE if overlay format is required
  1898. *
  1899. * Returns zero or a negative error code.
  1900. */
  1901. static int dspxfr_one_seg(struct hda_codec *codec,
  1902. const struct dsp_image_seg *fls,
  1903. unsigned int reloc,
  1904. struct dma_engine *dma_engine,
  1905. unsigned int dma_chan,
  1906. unsigned int port_map_mask,
  1907. bool ovly)
  1908. {
  1909. int status;
  1910. bool comm_dma_setup_done = false;
  1911. const unsigned int *data;
  1912. unsigned int chip_addx;
  1913. unsigned int words_to_write;
  1914. unsigned int buffer_size_words;
  1915. unsigned char *buffer_addx;
  1916. unsigned short hda_format;
  1917. unsigned int sample_rate_div;
  1918. unsigned int sample_rate_mul;
  1919. unsigned int num_chans;
  1920. unsigned int hda_frame_size_words;
  1921. unsigned int remainder_words;
  1922. const u32 *data_remainder;
  1923. u32 chip_addx_remainder;
  1924. unsigned int run_size_words;
  1925. const struct dsp_image_seg *hci_write = NULL;
  1926. int retry;
  1927. if (fls == NULL)
  1928. return -EINVAL;
  1929. if (is_hci_prog_list_seg(fls)) {
  1930. hci_write = fls;
  1931. fls = get_next_seg_ptr(fls);
  1932. }
  1933. if (hci_write && (!fls || is_last(fls))) {
  1934. snd_printdd("hci_write\n");
  1935. return dspxfr_hci_write(codec, hci_write);
  1936. }
  1937. if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
  1938. snd_printdd("Invalid Params\n");
  1939. return -EINVAL;
  1940. }
  1941. data = fls->data;
  1942. chip_addx = fls->chip_addr,
  1943. words_to_write = fls->count;
  1944. if (!words_to_write)
  1945. return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
  1946. if (reloc)
  1947. chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
  1948. if (!UC_RANGE(chip_addx, words_to_write) &&
  1949. !X_RANGE_ALL(chip_addx, words_to_write) &&
  1950. !Y_RANGE_ALL(chip_addx, words_to_write)) {
  1951. snd_printdd("Invalid chip_addx Params\n");
  1952. return -EINVAL;
  1953. }
  1954. buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
  1955. sizeof(u32);
  1956. buffer_addx = dma_get_buffer_addr(dma_engine);
  1957. if (buffer_addx == NULL) {
  1958. snd_printdd(KERN_ERR "dma_engine buffer NULL\n");
  1959. return -EINVAL;
  1960. }
  1961. dma_get_converter_format(dma_engine, &hda_format);
  1962. sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
  1963. sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
  1964. num_chans = get_hdafmt_chs(hda_format) + 1;
  1965. hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
  1966. (num_chans * sample_rate_mul / sample_rate_div));
  1967. buffer_size_words = min(buffer_size_words,
  1968. (unsigned int)(UC_RANGE(chip_addx, 1) ?
  1969. 65536 : 32768));
  1970. buffer_size_words -= buffer_size_words % hda_frame_size_words;
  1971. snd_printdd(
  1972. "chpadr=0x%08x frmsz=%u nchan=%u "
  1973. "rate_mul=%u div=%u bufsz=%u\n",
  1974. chip_addx, hda_frame_size_words, num_chans,
  1975. sample_rate_mul, sample_rate_div, buffer_size_words);
  1976. if ((buffer_addx == NULL) || (hda_frame_size_words == 0) ||
  1977. (buffer_size_words < hda_frame_size_words)) {
  1978. snd_printdd(KERN_ERR "dspxfr_one_seg:failed\n");
  1979. return -EINVAL;
  1980. }
  1981. remainder_words = words_to_write % hda_frame_size_words;
  1982. data_remainder = data;
  1983. chip_addx_remainder = chip_addx;
  1984. data += remainder_words;
  1985. chip_addx += remainder_words*sizeof(u32);
  1986. words_to_write -= remainder_words;
  1987. while (words_to_write != 0) {
  1988. run_size_words = min(buffer_size_words, words_to_write);
  1989. snd_printdd("dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
  1990. words_to_write, run_size_words, remainder_words);
  1991. dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
  1992. if (!comm_dma_setup_done) {
  1993. status = dsp_dma_stop(codec, dma_chan, ovly);
  1994. if (status < 0)
  1995. return -EIO;
  1996. status = dsp_dma_setup_common(codec, chip_addx,
  1997. dma_chan, port_map_mask, ovly);
  1998. if (status < 0)
  1999. return status;
  2000. comm_dma_setup_done = true;
  2001. }
  2002. status = dsp_dma_setup(codec, chip_addx,
  2003. run_size_words, dma_chan);
  2004. if (status < 0)
  2005. return status;
  2006. status = dsp_dma_start(codec, dma_chan, ovly);
  2007. if (status < 0)
  2008. return status;
  2009. if (!dsp_is_dma_active(codec, dma_chan)) {
  2010. snd_printdd(KERN_ERR "dspxfr:DMA did not start");
  2011. return -EIO;
  2012. }
  2013. status = dma_set_state(dma_engine, DMA_STATE_RUN);
  2014. if (status < 0)
  2015. return status;
  2016. if (remainder_words != 0) {
  2017. status = chipio_write_multiple(codec,
  2018. chip_addx_remainder,
  2019. data_remainder,
  2020. remainder_words);
  2021. remainder_words = 0;
  2022. }
  2023. if (hci_write) {
  2024. status = dspxfr_hci_write(codec, hci_write);
  2025. hci_write = NULL;
  2026. }
  2027. retry = 5000;
  2028. while (dsp_is_dma_active(codec, dma_chan)) {
  2029. if (--retry <= 0)
  2030. break;
  2031. }
  2032. snd_printdd(KERN_INFO "+++++ DMA complete");
  2033. dma_set_state(dma_engine, DMA_STATE_STOP);
  2034. dma_reset(dma_engine);
  2035. if (status < 0)
  2036. return status;
  2037. data += run_size_words;
  2038. chip_addx += run_size_words*sizeof(u32);
  2039. words_to_write -= run_size_words;
  2040. }
  2041. if (remainder_words != 0) {
  2042. status = chipio_write_multiple(codec, chip_addx_remainder,
  2043. data_remainder, remainder_words);
  2044. }
  2045. return status;
  2046. }
  2047. /**
  2048. * Write the entire DSP image of a DSP code/data overlay to DSP memories
  2049. *
  2050. * @codec: the HDA codec
  2051. * @fls_data: pointer to a fast load image
  2052. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  2053. * no relocation
  2054. * @sample_rate: sampling rate of the stream used for DSP download
  2055. * @number_channels: channels of the stream used for DSP download
  2056. * @ovly: TRUE if overlay format is required
  2057. *
  2058. * Returns zero or a negative error code.
  2059. */
  2060. static int dspxfr_image(struct hda_codec *codec,
  2061. const struct dsp_image_seg *fls_data,
  2062. unsigned int reloc,
  2063. unsigned int sample_rate,
  2064. unsigned short channels,
  2065. bool ovly)
  2066. {
  2067. struct ca0132_spec *spec = codec->spec;
  2068. int status;
  2069. unsigned short hda_format = 0;
  2070. unsigned int response;
  2071. unsigned char stream_id = 0;
  2072. struct dma_engine *dma_engine;
  2073. unsigned int dma_chan;
  2074. unsigned int port_map_mask;
  2075. if (fls_data == NULL)
  2076. return -EINVAL;
  2077. dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
  2078. if (!dma_engine)
  2079. return -ENOMEM;
  2080. dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
  2081. if (!dma_engine->dmab) {
  2082. status = -ENOMEM;
  2083. goto exit;
  2084. }
  2085. dma_engine->codec = codec;
  2086. dma_convert_to_hda_format(sample_rate, channels, &hda_format);
  2087. dma_engine->m_converter_format = hda_format;
  2088. dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
  2089. DSP_DMA_WRITE_BUFLEN_INIT) * 2;
  2090. dma_chan = 0;
  2091. status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
  2092. hda_format, &response);
  2093. if (status < 0) {
  2094. snd_printdd(KERN_ERR "set converter format fail");
  2095. goto exit;
  2096. }
  2097. status = snd_hda_codec_load_dsp_prepare(codec,
  2098. dma_engine->m_converter_format,
  2099. dma_engine->buf_size,
  2100. dma_engine->dmab);
  2101. if (status < 0)
  2102. goto exit;
  2103. spec->dsp_stream_id = status;
  2104. if (ovly) {
  2105. status = dspio_alloc_dma_chan(codec, &dma_chan);
  2106. if (status < 0) {
  2107. snd_printdd(KERN_ERR "alloc dmachan fail");
  2108. dma_chan = (unsigned int)INVALID_DMA_CHANNEL;
  2109. goto exit;
  2110. }
  2111. }
  2112. port_map_mask = 0;
  2113. status = dsp_allocate_ports_format(codec, hda_format,
  2114. &port_map_mask);
  2115. if (status < 0) {
  2116. snd_printdd(KERN_ERR "alloc ports fail");
  2117. goto exit;
  2118. }
  2119. stream_id = dma_get_stream_id(dma_engine);
  2120. status = codec_set_converter_stream_channel(codec,
  2121. WIDGET_CHIP_CTRL, stream_id, 0, &response);
  2122. if (status < 0) {
  2123. snd_printdd(KERN_ERR "set stream chan fail");
  2124. goto exit;
  2125. }
  2126. while ((fls_data != NULL) && !is_last(fls_data)) {
  2127. if (!is_valid(fls_data)) {
  2128. snd_printdd(KERN_ERR "FLS check fail");
  2129. status = -EINVAL;
  2130. goto exit;
  2131. }
  2132. status = dspxfr_one_seg(codec, fls_data, reloc,
  2133. dma_engine, dma_chan,
  2134. port_map_mask, ovly);
  2135. if (status < 0)
  2136. break;
  2137. if (is_hci_prog_list_seg(fls_data))
  2138. fls_data = get_next_seg_ptr(fls_data);
  2139. if ((fls_data != NULL) && !is_last(fls_data))
  2140. fls_data = get_next_seg_ptr(fls_data);
  2141. }
  2142. if (port_map_mask != 0)
  2143. status = dsp_free_ports(codec);
  2144. if (status < 0)
  2145. goto exit;
  2146. status = codec_set_converter_stream_channel(codec,
  2147. WIDGET_CHIP_CTRL, 0, 0, &response);
  2148. exit:
  2149. if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
  2150. dspio_free_dma_chan(codec, dma_chan);
  2151. if (dma_engine->dmab)
  2152. snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
  2153. kfree(dma_engine->dmab);
  2154. kfree(dma_engine);
  2155. return status;
  2156. }
  2157. /*
  2158. * CA0132 DSP download stuffs.
  2159. */
  2160. static void dspload_post_setup(struct hda_codec *codec)
  2161. {
  2162. snd_printdd(KERN_INFO "---- dspload_post_setup ------");
  2163. /*set DSP speaker to 2.0 configuration*/
  2164. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
  2165. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
  2166. /*update write pointer*/
  2167. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
  2168. }
  2169. /**
  2170. * Download DSP from a DSP Image Fast Load structure. This structure is a
  2171. * linear, non-constant sized element array of structures, each of which
  2172. * contain the count of the data to be loaded, the data itself, and the
  2173. * corresponding starting chip address of the starting data location.
  2174. *
  2175. * @codec: the HDA codec
  2176. * @fls: pointer to a fast load image
  2177. * @ovly: TRUE if overlay format is required
  2178. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  2179. * no relocation
  2180. * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
  2181. * @router_chans: number of audio router channels to be allocated (0 means use
  2182. * internal defaults; max is 32)
  2183. *
  2184. * Returns zero or a negative error code.
  2185. */
  2186. static int dspload_image(struct hda_codec *codec,
  2187. const struct dsp_image_seg *fls,
  2188. bool ovly,
  2189. unsigned int reloc,
  2190. bool autostart,
  2191. int router_chans)
  2192. {
  2193. int status = 0;
  2194. unsigned int sample_rate;
  2195. unsigned short channels;
  2196. snd_printdd(KERN_INFO "---- dspload_image begin ------");
  2197. if (router_chans == 0) {
  2198. if (!ovly)
  2199. router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
  2200. else
  2201. router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
  2202. }
  2203. sample_rate = 48000;
  2204. channels = (unsigned short)router_chans;
  2205. while (channels > 16) {
  2206. sample_rate *= 2;
  2207. channels /= 2;
  2208. }
  2209. do {
  2210. snd_printdd(KERN_INFO "Ready to program DMA");
  2211. if (!ovly)
  2212. status = dsp_reset(codec);
  2213. if (status < 0)
  2214. break;
  2215. snd_printdd(KERN_INFO "dsp_reset() complete");
  2216. status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
  2217. ovly);
  2218. if (status < 0)
  2219. break;
  2220. snd_printdd(KERN_INFO "dspxfr_image() complete");
  2221. if (autostart && !ovly) {
  2222. dspload_post_setup(codec);
  2223. status = dsp_set_run_state(codec);
  2224. }
  2225. snd_printdd(KERN_INFO "LOAD FINISHED");
  2226. } while (0);
  2227. return status;
  2228. }
  2229. static const struct firmware *fw_efx;
  2230. static int request_firmware_cached(const struct firmware **firmware_p,
  2231. const char *name, struct device *device)
  2232. {
  2233. if (*firmware_p)
  2234. return 0; /* already loaded */
  2235. return request_firmware(firmware_p, name, device);
  2236. }
  2237. static void release_cached_firmware(void)
  2238. {
  2239. if (fw_efx) {
  2240. release_firmware(fw_efx);
  2241. fw_efx = NULL;
  2242. }
  2243. }
  2244. static bool dspload_is_loaded(struct hda_codec *codec)
  2245. {
  2246. unsigned int data = 0;
  2247. int status = 0;
  2248. status = chipio_read(codec, 0x40004, &data);
  2249. if ((status < 0) || (data != 1))
  2250. return false;
  2251. return true;
  2252. }
  2253. static bool dspload_wait_loaded(struct hda_codec *codec)
  2254. {
  2255. int retry = 100;
  2256. do {
  2257. msleep(20);
  2258. if (dspload_is_loaded(codec)) {
  2259. pr_info("ca0132 DOWNLOAD OK :-) DSP IS RUNNING.\n");
  2260. return true;
  2261. }
  2262. } while (--retry);
  2263. pr_err("ca0132 DOWNLOAD FAILED!!! DSP IS NOT RUNNING.\n");
  2264. return false;
  2265. }
  2266. /*
  2267. * Controls stuffs.
  2268. */
  2269. /*
  2270. * Mixer controls helpers.
  2271. */
  2272. #define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
  2273. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2274. .name = xname, \
  2275. .subdevice = HDA_SUBDEV_AMP_FLAG, \
  2276. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
  2277. SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
  2278. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
  2279. .info = ca0132_volume_info, \
  2280. .get = ca0132_volume_get, \
  2281. .put = ca0132_volume_put, \
  2282. .tlv = { .c = ca0132_volume_tlv }, \
  2283. .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
  2284. #define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
  2285. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2286. .name = xname, \
  2287. .subdevice = HDA_SUBDEV_AMP_FLAG, \
  2288. .info = snd_hda_mixer_amp_switch_info, \
  2289. .get = ca0132_switch_get, \
  2290. .put = ca0132_switch_put, \
  2291. .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
  2292. /* stereo */
  2293. #define CA0132_CODEC_VOL(xname, nid, dir) \
  2294. CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
  2295. #define CA0132_CODEC_MUTE(xname, nid, dir) \
  2296. CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
  2297. /*
  2298. * PCM callbacks
  2299. */
  2300. static int ca0132_playback_pcm_open(struct hda_pcm_stream *hinfo,
  2301. struct hda_codec *codec,
  2302. struct snd_pcm_substream *substream)
  2303. {
  2304. struct ca0132_spec *spec = codec->spec;
  2305. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
  2306. hinfo);
  2307. }
  2308. static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  2309. struct hda_codec *codec,
  2310. unsigned int stream_tag,
  2311. unsigned int format,
  2312. struct snd_pcm_substream *substream)
  2313. {
  2314. struct ca0132_spec *spec = codec->spec;
  2315. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
  2316. stream_tag, format, substream);
  2317. }
  2318. static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2319. struct hda_codec *codec,
  2320. struct snd_pcm_substream *substream)
  2321. {
  2322. struct ca0132_spec *spec = codec->spec;
  2323. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  2324. }
  2325. /*
  2326. * Digital out
  2327. */
  2328. static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  2329. struct hda_codec *codec,
  2330. struct snd_pcm_substream *substream)
  2331. {
  2332. struct ca0132_spec *spec = codec->spec;
  2333. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  2334. }
  2335. static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  2336. struct hda_codec *codec,
  2337. unsigned int stream_tag,
  2338. unsigned int format,
  2339. struct snd_pcm_substream *substream)
  2340. {
  2341. struct ca0132_spec *spec = codec->spec;
  2342. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  2343. stream_tag, format, substream);
  2344. }
  2345. static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2346. struct hda_codec *codec,
  2347. struct snd_pcm_substream *substream)
  2348. {
  2349. struct ca0132_spec *spec = codec->spec;
  2350. return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
  2351. }
  2352. static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  2353. struct hda_codec *codec,
  2354. struct snd_pcm_substream *substream)
  2355. {
  2356. struct ca0132_spec *spec = codec->spec;
  2357. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  2358. }
  2359. /*
  2360. * Select the active output.
  2361. * If autodetect is enabled, output will be selected based on jack detection.
  2362. * If jack inserted, headphone will be selected, else built-in speakers
  2363. * If autodetect is disabled, output will be selected based on selection.
  2364. */
  2365. static int ca0132_select_out(struct hda_codec *codec)
  2366. {
  2367. struct ca0132_spec *spec = codec->spec;
  2368. unsigned int pin_ctl;
  2369. int jack_present;
  2370. int auto_jack;
  2371. unsigned int tmp;
  2372. int err;
  2373. snd_printdd(KERN_INFO "ca0132_select_out\n");
  2374. snd_hda_power_up(codec);
  2375. auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  2376. if (auto_jack)
  2377. jack_present = snd_hda_jack_detect(codec, spec->out_pins[1]);
  2378. else
  2379. jack_present =
  2380. spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
  2381. if (jack_present)
  2382. spec->cur_out_type = HEADPHONE_OUT;
  2383. else
  2384. spec->cur_out_type = SPEAKER_OUT;
  2385. if (spec->cur_out_type == SPEAKER_OUT) {
  2386. snd_printdd(KERN_INFO "ca0132_select_out speaker\n");
  2387. /*speaker out config*/
  2388. tmp = FLOAT_ONE;
  2389. err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
  2390. if (err < 0)
  2391. goto exit;
  2392. /*enable speaker EQ*/
  2393. tmp = FLOAT_ONE;
  2394. err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
  2395. if (err < 0)
  2396. goto exit;
  2397. /* Setup EAPD */
  2398. snd_hda_codec_write(codec, spec->out_pins[1], 0,
  2399. VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
  2400. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2401. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  2402. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2403. VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
  2404. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2405. AC_VERB_SET_EAPD_BTLENABLE, 0x02);
  2406. /* disable headphone node */
  2407. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
  2408. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2409. snd_hda_codec_write(codec, spec->out_pins[1], 0,
  2410. AC_VERB_SET_PIN_WIDGET_CONTROL,
  2411. pin_ctl & 0xBF);
  2412. /* enable speaker node */
  2413. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  2414. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2415. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2416. AC_VERB_SET_PIN_WIDGET_CONTROL,
  2417. pin_ctl | 0x40);
  2418. } else {
  2419. snd_printdd(KERN_INFO "ca0132_select_out hp\n");
  2420. /*headphone out config*/
  2421. tmp = FLOAT_ZERO;
  2422. err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
  2423. if (err < 0)
  2424. goto exit;
  2425. /*disable speaker EQ*/
  2426. tmp = FLOAT_ZERO;
  2427. err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
  2428. if (err < 0)
  2429. goto exit;
  2430. /* Setup EAPD */
  2431. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2432. VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
  2433. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2434. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  2435. snd_hda_codec_write(codec, spec->out_pins[1], 0,
  2436. VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
  2437. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2438. AC_VERB_SET_EAPD_BTLENABLE, 0x02);
  2439. /* disable speaker*/
  2440. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  2441. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2442. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2443. AC_VERB_SET_PIN_WIDGET_CONTROL,
  2444. pin_ctl & 0xBF);
  2445. /* enable headphone*/
  2446. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
  2447. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2448. snd_hda_codec_write(codec, spec->out_pins[1], 0,
  2449. AC_VERB_SET_PIN_WIDGET_CONTROL,
  2450. pin_ctl | 0x40);
  2451. }
  2452. exit:
  2453. snd_hda_power_down(codec);
  2454. return err < 0 ? err : 0;
  2455. }
  2456. static void ca0132_set_dmic(struct hda_codec *codec, int enable);
  2457. static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
  2458. static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
  2459. /*
  2460. * Select the active VIP source
  2461. */
  2462. static int ca0132_set_vipsource(struct hda_codec *codec, int val)
  2463. {
  2464. struct ca0132_spec *spec = codec->spec;
  2465. unsigned int tmp;
  2466. if (!dspload_is_loaded(codec))
  2467. return 0;
  2468. /* if CrystalVoice if off, vipsource should be 0 */
  2469. if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
  2470. (val == 0)) {
  2471. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
  2472. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  2473. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  2474. if (spec->cur_mic_type == DIGITAL_MIC)
  2475. tmp = FLOAT_TWO;
  2476. else
  2477. tmp = FLOAT_ONE;
  2478. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  2479. tmp = FLOAT_ZERO;
  2480. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  2481. } else {
  2482. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
  2483. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
  2484. if (spec->cur_mic_type == DIGITAL_MIC)
  2485. tmp = FLOAT_TWO;
  2486. else
  2487. tmp = FLOAT_ONE;
  2488. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  2489. tmp = FLOAT_ONE;
  2490. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  2491. msleep(20);
  2492. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
  2493. }
  2494. return 1;
  2495. }
  2496. /*
  2497. * Select the active microphone.
  2498. * If autodetect is enabled, mic will be selected based on jack detection.
  2499. * If jack inserted, ext.mic will be selected, else built-in mic
  2500. * If autodetect is disabled, mic will be selected based on selection.
  2501. */
  2502. static int ca0132_select_mic(struct hda_codec *codec)
  2503. {
  2504. struct ca0132_spec *spec = codec->spec;
  2505. int jack_present;
  2506. int auto_jack;
  2507. snd_printdd(KERN_INFO "ca0132_select_mic\n");
  2508. snd_hda_power_up(codec);
  2509. auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
  2510. if (auto_jack)
  2511. jack_present = snd_hda_jack_detect(codec, spec->input_pins[0]);
  2512. else
  2513. jack_present =
  2514. spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
  2515. if (jack_present)
  2516. spec->cur_mic_type = LINE_MIC_IN;
  2517. else
  2518. spec->cur_mic_type = DIGITAL_MIC;
  2519. if (spec->cur_mic_type == DIGITAL_MIC) {
  2520. /* enable digital Mic */
  2521. chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
  2522. ca0132_set_dmic(codec, 1);
  2523. ca0132_mic_boost_set(codec, 0);
  2524. /* set voice focus */
  2525. ca0132_effects_set(codec, VOICE_FOCUS,
  2526. spec->effects_switch
  2527. [VOICE_FOCUS - EFFECT_START_NID]);
  2528. } else {
  2529. /* disable digital Mic */
  2530. chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
  2531. ca0132_set_dmic(codec, 0);
  2532. ca0132_mic_boost_set(codec, spec->cur_mic_boost);
  2533. /* disable voice focus */
  2534. ca0132_effects_set(codec, VOICE_FOCUS, 0);
  2535. }
  2536. snd_hda_power_down(codec);
  2537. return 0;
  2538. }
  2539. /*
  2540. * Set the effects parameters
  2541. */
  2542. static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
  2543. {
  2544. struct ca0132_spec *spec = codec->spec;
  2545. unsigned int on;
  2546. int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  2547. int err = 0;
  2548. int idx = nid - EFFECT_START_NID;
  2549. if ((idx < 0) || (idx >= num_fx))
  2550. return 0; /* no changed */
  2551. /* for out effect, qualify with PE */
  2552. if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
  2553. /* if PE if off, turn off out effects. */
  2554. if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
  2555. val = 0;
  2556. }
  2557. /* for in effect, qualify with CrystalVoice */
  2558. if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
  2559. /* if CrystalVoice if off, turn off in effects. */
  2560. if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
  2561. val = 0;
  2562. /* Voice Focus applies to 2-ch Mic, Digital Mic */
  2563. if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
  2564. val = 0;
  2565. }
  2566. snd_printdd(KERN_INFO, "ca0132_effect_set: nid=0x%x, val=%ld\n",
  2567. nid, val);
  2568. on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
  2569. err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
  2570. ca0132_effects[idx].reqs[0], on);
  2571. if (err < 0)
  2572. return 0; /* no changed */
  2573. return 1;
  2574. }
  2575. /* Check if Mic1 is streaming, if so, stop streaming */
  2576. static int stop_mic1(struct hda_codec *codec)
  2577. {
  2578. struct ca0132_spec *spec = codec->spec;
  2579. unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
  2580. AC_VERB_GET_CONV, 0);
  2581. if (oldval != 0)
  2582. snd_hda_codec_write(codec, spec->adcs[0], 0,
  2583. AC_VERB_SET_CHANNEL_STREAMID,
  2584. 0);
  2585. return oldval;
  2586. }
  2587. /* Resume Mic1 streaming if it was stopped. */
  2588. static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
  2589. {
  2590. struct ca0132_spec *spec = codec->spec;
  2591. /* Restore the previous stream and channel */
  2592. if (oldval != 0)
  2593. snd_hda_codec_write(codec, spec->adcs[0], 0,
  2594. AC_VERB_SET_CHANNEL_STREAMID,
  2595. oldval);
  2596. }
  2597. /*
  2598. * Set Mic Boost
  2599. */
  2600. static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
  2601. {
  2602. struct ca0132_spec *spec = codec->spec;
  2603. int ret = 0;
  2604. if (val) /* on */
  2605. ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
  2606. HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
  2607. else /* off */
  2608. ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
  2609. HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
  2610. return ret;
  2611. }
  2612. /*
  2613. */
  2614. static struct hda_pcm_stream ca0132_pcm_analog_playback = {
  2615. .substreams = 1,
  2616. .channels_min = 2,
  2617. .channels_max = 2,
  2618. .ops = {
  2619. .open = ca0132_playback_pcm_open,
  2620. .prepare = ca0132_playback_pcm_prepare,
  2621. .cleanup = ca0132_playback_pcm_cleanup
  2622. },
  2623. };
  2624. static struct hda_pcm_stream ca0132_pcm_analog_capture = {
  2625. .substreams = 1,
  2626. .channels_min = 2,
  2627. .channels_max = 2,
  2628. };
  2629. static struct hda_pcm_stream ca0132_pcm_digital_playback = {
  2630. .substreams = 1,
  2631. .channels_min = 2,
  2632. .channels_max = 2,
  2633. .ops = {
  2634. .open = ca0132_dig_playback_pcm_open,
  2635. .close = ca0132_dig_playback_pcm_close,
  2636. .prepare = ca0132_dig_playback_pcm_prepare,
  2637. .cleanup = ca0132_dig_playback_pcm_cleanup
  2638. },
  2639. };
  2640. static struct hda_pcm_stream ca0132_pcm_digital_capture = {
  2641. .substreams = 1,
  2642. .channels_min = 2,
  2643. .channels_max = 2,
  2644. };
  2645. static int ca0132_build_pcms(struct hda_codec *codec)
  2646. {
  2647. struct ca0132_spec *spec = codec->spec;
  2648. struct hda_pcm *info = spec->pcm_rec;
  2649. codec->pcm_info = info;
  2650. codec->num_pcms = 0;
  2651. info->name = "CA0132 Analog";
  2652. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
  2653. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
  2654. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  2655. spec->multiout.max_channels;
  2656. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  2657. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_inputs;
  2658. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
  2659. codec->num_pcms++;
  2660. if (!spec->dig_out && !spec->dig_in)
  2661. return 0;
  2662. info++;
  2663. info->name = "CA0132 Digital";
  2664. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  2665. if (spec->dig_out) {
  2666. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  2667. ca0132_pcm_digital_playback;
  2668. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
  2669. }
  2670. if (spec->dig_in) {
  2671. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  2672. ca0132_pcm_digital_capture;
  2673. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
  2674. }
  2675. codec->num_pcms++;
  2676. return 0;
  2677. }
  2678. #define REG_CODEC_MUTE 0x18b014
  2679. #define REG_CODEC_HP_VOL_L 0x18b070
  2680. #define REG_CODEC_HP_VOL_R 0x18b074
  2681. static int ca0132_hp_switch_get(struct snd_kcontrol *kcontrol,
  2682. struct snd_ctl_elem_value *ucontrol)
  2683. {
  2684. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2685. struct ca0132_spec *spec = codec->spec;
  2686. long *valp = ucontrol->value.integer.value;
  2687. *valp = spec->curr_hp_switch;
  2688. return 0;
  2689. }
  2690. static int ca0132_hp_switch_put(struct snd_kcontrol *kcontrol,
  2691. struct snd_ctl_elem_value *ucontrol)
  2692. {
  2693. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2694. struct ca0132_spec *spec = codec->spec;
  2695. long *valp = ucontrol->value.integer.value;
  2696. unsigned int data;
  2697. int err;
  2698. /* any change? */
  2699. if (spec->curr_hp_switch == *valp)
  2700. return 0;
  2701. snd_hda_power_up(codec);
  2702. err = chipio_read(codec, REG_CODEC_MUTE, &data);
  2703. if (err < 0)
  2704. goto exit;
  2705. /* *valp 0 is mute, 1 is unmute */
  2706. data = (data & 0x7f) | (*valp ? 0 : 0x80);
  2707. err = chipio_write(codec, REG_CODEC_MUTE, data);
  2708. if (err < 0)
  2709. goto exit;
  2710. spec->curr_hp_switch = *valp;
  2711. exit:
  2712. snd_hda_power_down(codec);
  2713. return err < 0 ? err : 1;
  2714. }
  2715. static int ca0132_speaker_switch_get(struct snd_kcontrol *kcontrol,
  2716. struct snd_ctl_elem_value *ucontrol)
  2717. {
  2718. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2719. struct ca0132_spec *spec = codec->spec;
  2720. long *valp = ucontrol->value.integer.value;
  2721. *valp = spec->curr_speaker_switch;
  2722. return 0;
  2723. }
  2724. static int ca0132_speaker_switch_put(struct snd_kcontrol *kcontrol,
  2725. struct snd_ctl_elem_value *ucontrol)
  2726. {
  2727. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2728. struct ca0132_spec *spec = codec->spec;
  2729. long *valp = ucontrol->value.integer.value;
  2730. unsigned int data;
  2731. int err;
  2732. /* any change? */
  2733. if (spec->curr_speaker_switch == *valp)
  2734. return 0;
  2735. snd_hda_power_up(codec);
  2736. err = chipio_read(codec, REG_CODEC_MUTE, &data);
  2737. if (err < 0)
  2738. goto exit;
  2739. /* *valp 0 is mute, 1 is unmute */
  2740. data = (data & 0xef) | (*valp ? 0 : 0x10);
  2741. err = chipio_write(codec, REG_CODEC_MUTE, data);
  2742. if (err < 0)
  2743. goto exit;
  2744. spec->curr_speaker_switch = *valp;
  2745. exit:
  2746. snd_hda_power_down(codec);
  2747. return err < 0 ? err : 1;
  2748. }
  2749. static int ca0132_hp_volume_get(struct snd_kcontrol *kcontrol,
  2750. struct snd_ctl_elem_value *ucontrol)
  2751. {
  2752. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2753. struct ca0132_spec *spec = codec->spec;
  2754. long *valp = ucontrol->value.integer.value;
  2755. *valp++ = spec->curr_hp_volume[0];
  2756. *valp = spec->curr_hp_volume[1];
  2757. return 0;
  2758. }
  2759. static int ca0132_hp_volume_put(struct snd_kcontrol *kcontrol,
  2760. struct snd_ctl_elem_value *ucontrol)
  2761. {
  2762. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2763. struct ca0132_spec *spec = codec->spec;
  2764. long *valp = ucontrol->value.integer.value;
  2765. long left_vol, right_vol;
  2766. unsigned int data;
  2767. int val;
  2768. int err;
  2769. left_vol = *valp++;
  2770. right_vol = *valp;
  2771. /* any change? */
  2772. if ((spec->curr_hp_volume[0] == left_vol) &&
  2773. (spec->curr_hp_volume[1] == right_vol))
  2774. return 0;
  2775. snd_hda_power_up(codec);
  2776. err = chipio_read(codec, REG_CODEC_HP_VOL_L, &data);
  2777. if (err < 0)
  2778. goto exit;
  2779. val = 31 - left_vol;
  2780. data = (data & 0xe0) | val;
  2781. err = chipio_write(codec, REG_CODEC_HP_VOL_L, data);
  2782. if (err < 0)
  2783. goto exit;
  2784. val = 31 - right_vol;
  2785. data = (data & 0xe0) | val;
  2786. err = chipio_write(codec, REG_CODEC_HP_VOL_R, data);
  2787. if (err < 0)
  2788. goto exit;
  2789. spec->curr_hp_volume[0] = left_vol;
  2790. spec->curr_hp_volume[1] = right_vol;
  2791. exit:
  2792. snd_hda_power_down(codec);
  2793. return err < 0 ? err : 1;
  2794. }
  2795. static int add_hp_switch(struct hda_codec *codec, hda_nid_t nid)
  2796. {
  2797. struct snd_kcontrol_new knew =
  2798. HDA_CODEC_MUTE_MONO("Headphone Playback Switch",
  2799. nid, 1, 0, HDA_OUTPUT);
  2800. knew.get = ca0132_hp_switch_get;
  2801. knew.put = ca0132_hp_switch_put;
  2802. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  2803. }
  2804. static int add_hp_volume(struct hda_codec *codec, hda_nid_t nid)
  2805. {
  2806. struct snd_kcontrol_new knew =
  2807. HDA_CODEC_VOLUME_MONO("Headphone Playback Volume",
  2808. nid, 3, 0, HDA_OUTPUT);
  2809. knew.get = ca0132_hp_volume_get;
  2810. knew.put = ca0132_hp_volume_put;
  2811. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  2812. }
  2813. static int add_speaker_switch(struct hda_codec *codec, hda_nid_t nid)
  2814. {
  2815. struct snd_kcontrol_new knew =
  2816. HDA_CODEC_MUTE_MONO("Speaker Playback Switch",
  2817. nid, 1, 0, HDA_OUTPUT);
  2818. knew.get = ca0132_speaker_switch_get;
  2819. knew.put = ca0132_speaker_switch_put;
  2820. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  2821. }
  2822. static void ca0132_fix_hp_caps(struct hda_codec *codec)
  2823. {
  2824. struct ca0132_spec *spec = codec->spec;
  2825. struct auto_pin_cfg *cfg = &spec->autocfg;
  2826. unsigned int caps;
  2827. /* set mute-capable, 1db step, 32 steps, ofs 6 */
  2828. caps = 0x80031f06;
  2829. snd_hda_override_amp_caps(codec, cfg->hp_pins[0], HDA_OUTPUT, caps);
  2830. }
  2831. static int ca0132_build_controls(struct hda_codec *codec)
  2832. {
  2833. struct ca0132_spec *spec = codec->spec;
  2834. struct auto_pin_cfg *cfg = &spec->autocfg;
  2835. int i, err;
  2836. if (spec->multiout.num_dacs) {
  2837. err = add_speaker_switch(codec, spec->out_pins[0]);
  2838. if (err < 0)
  2839. return err;
  2840. }
  2841. if (cfg->hp_outs) {
  2842. ca0132_fix_hp_caps(codec);
  2843. err = add_hp_switch(codec, cfg->hp_pins[0]);
  2844. if (err < 0)
  2845. return err;
  2846. err = add_hp_volume(codec, cfg->hp_pins[0]);
  2847. if (err < 0)
  2848. return err;
  2849. }
  2850. for (i = 0; i < spec->num_inputs; i++) {
  2851. const char *label = spec->input_labels[i];
  2852. err = add_in_switch(codec, spec->adcs[i], label);
  2853. if (err < 0)
  2854. return err;
  2855. err = add_in_volume(codec, spec->adcs[i], label);
  2856. if (err < 0)
  2857. return err;
  2858. if (cfg->inputs[i].type == AUTO_PIN_MIC) {
  2859. /* add Mic-Boost */
  2860. err = add_in_mono_volume(codec, spec->input_pins[i],
  2861. "Mic Boost", 1);
  2862. if (err < 0)
  2863. return err;
  2864. }
  2865. }
  2866. if (spec->dig_out) {
  2867. err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
  2868. spec->dig_out);
  2869. if (err < 0)
  2870. return err;
  2871. err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
  2872. if (err < 0)
  2873. return err;
  2874. /* spec->multiout.share_spdif = 1; */
  2875. }
  2876. if (spec->dig_in) {
  2877. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  2878. if (err < 0)
  2879. return err;
  2880. }
  2881. return 0;
  2882. }
  2883. static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
  2884. {
  2885. unsigned int caps;
  2886. caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
  2887. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  2888. snd_hda_override_amp_caps(codec, nid, dir, caps);
  2889. }
  2890. /*
  2891. * Switch between Digital built-in mic and analog mic.
  2892. */
  2893. static void ca0132_set_dmic(struct hda_codec *codec, int enable)
  2894. {
  2895. struct ca0132_spec *spec = codec->spec;
  2896. unsigned int tmp;
  2897. u8 val;
  2898. unsigned int oldval;
  2899. snd_printdd(KERN_INFO "ca0132_set_dmic: enable=%d\n", enable);
  2900. oldval = stop_mic1(codec);
  2901. ca0132_set_vipsource(codec, 0);
  2902. if (enable) {
  2903. /* set DMic input as 2-ch */
  2904. tmp = FLOAT_TWO;
  2905. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  2906. val = spec->dmic_ctl;
  2907. val |= 0x80;
  2908. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  2909. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  2910. if (!(spec->dmic_ctl & 0x20))
  2911. chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
  2912. } else {
  2913. /* set AMic input as mono */
  2914. tmp = FLOAT_ONE;
  2915. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  2916. val = spec->dmic_ctl;
  2917. /* clear bit7 and bit5 to disable dmic */
  2918. val &= 0x5f;
  2919. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  2920. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  2921. if (!(spec->dmic_ctl & 0x20))
  2922. chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
  2923. }
  2924. ca0132_set_vipsource(codec, 1);
  2925. resume_mic1(codec, oldval);
  2926. }
  2927. /*
  2928. * Initialization for Digital Mic.
  2929. */
  2930. static void ca0132_init_dmic(struct hda_codec *codec)
  2931. {
  2932. struct ca0132_spec *spec = codec->spec;
  2933. u8 val;
  2934. /* Setup Digital Mic here, but don't enable.
  2935. * Enable based on jack detect.
  2936. */
  2937. /* MCLK uses MPIO1, set to enable.
  2938. * Bit 2-0: MPIO select
  2939. * Bit 3: set to disable
  2940. * Bit 7-4: reserved
  2941. */
  2942. val = 0x01;
  2943. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  2944. VENDOR_CHIPIO_DMIC_MCLK_SET, val);
  2945. /* Data1 uses MPIO3. Data2 not use
  2946. * Bit 2-0: Data1 MPIO select
  2947. * Bit 3: set disable Data1
  2948. * Bit 6-4: Data2 MPIO select
  2949. * Bit 7: set disable Data2
  2950. */
  2951. val = 0x83;
  2952. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  2953. VENDOR_CHIPIO_DMIC_PIN_SET, val);
  2954. /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
  2955. * Bit 3-0: Channel mask
  2956. * Bit 4: set for 48KHz, clear for 32KHz
  2957. * Bit 5: mode
  2958. * Bit 6: set to select Data2, clear for Data1
  2959. * Bit 7: set to enable DMic, clear for AMic
  2960. */
  2961. val = 0x23;
  2962. /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
  2963. spec->dmic_ctl = val;
  2964. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  2965. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  2966. }
  2967. /*
  2968. * Initialization for Analog Mic 2
  2969. */
  2970. static void ca0132_init_analog_mic2(struct hda_codec *codec)
  2971. {
  2972. struct ca0132_spec *spec = codec->spec;
  2973. mutex_lock(&spec->chipio_mutex);
  2974. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  2975. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
  2976. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  2977. VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
  2978. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  2979. VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
  2980. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  2981. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x2D);
  2982. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  2983. VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
  2984. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  2985. VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
  2986. mutex_unlock(&spec->chipio_mutex);
  2987. }
  2988. static void ca0132_refresh_widget_caps(struct hda_codec *codec)
  2989. {
  2990. struct ca0132_spec *spec = codec->spec;
  2991. int i;
  2992. hda_nid_t nid;
  2993. snd_printdd(KERN_INFO "ca0132_refresh_widget_caps.\n");
  2994. nid = codec->start_nid;
  2995. for (i = 0; i < codec->num_nodes; i++, nid++)
  2996. codec->wcaps[i] = snd_hda_param_read(codec, nid,
  2997. AC_PAR_AUDIO_WIDGET_CAP);
  2998. for (i = 0; i < spec->multiout.num_dacs; i++)
  2999. refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
  3000. for (i = 0; i < spec->num_outputs; i++)
  3001. refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
  3002. for (i = 0; i < spec->num_inputs; i++) {
  3003. refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
  3004. refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
  3005. }
  3006. }
  3007. /*
  3008. * Setup default parameters for DSP
  3009. */
  3010. static void ca0132_setup_defaults(struct hda_codec *codec)
  3011. {
  3012. unsigned int tmp;
  3013. int num_fx;
  3014. int idx, i;
  3015. if (!dspload_is_loaded(codec))
  3016. return;
  3017. /* out, in effects + voicefx */
  3018. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
  3019. for (idx = 0; idx < num_fx; idx++) {
  3020. for (i = 0; i <= ca0132_effects[idx].params; i++) {
  3021. dspio_set_uint_param(codec, ca0132_effects[idx].mid,
  3022. ca0132_effects[idx].reqs[i],
  3023. ca0132_effects[idx].def_vals[i]);
  3024. }
  3025. }
  3026. /*remove DSP headroom*/
  3027. tmp = FLOAT_ZERO;
  3028. dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
  3029. /*set speaker EQ bypass attenuation*/
  3030. dspio_set_uint_param(codec, 0x8f, 0x01, tmp);
  3031. /* set AMic1 and AMic2 as mono mic */
  3032. tmp = FLOAT_ONE;
  3033. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3034. dspio_set_uint_param(codec, 0x80, 0x01, tmp);
  3035. /* set AMic1 as CrystalVoice input */
  3036. tmp = FLOAT_ONE;
  3037. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  3038. /* set WUH source */
  3039. tmp = FLOAT_TWO;
  3040. dspio_set_uint_param(codec, 0x31, 0x00, tmp);
  3041. }
  3042. /*
  3043. * Initialization of flags in chip
  3044. */
  3045. static void ca0132_init_flags(struct hda_codec *codec)
  3046. {
  3047. chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
  3048. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
  3049. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
  3050. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
  3051. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
  3052. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
  3053. }
  3054. /*
  3055. * Initialization of parameters in chip
  3056. */
  3057. static void ca0132_init_params(struct hda_codec *codec)
  3058. {
  3059. chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
  3060. chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
  3061. }
  3062. static void ca0132_set_ct_ext(struct hda_codec *codec, int enable)
  3063. {
  3064. /* Set Creative extension */
  3065. snd_printdd("SET CREATIVE EXTENSION\n");
  3066. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3067. VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE,
  3068. enable);
  3069. msleep(20);
  3070. }
  3071. static void ca0132_config(struct hda_codec *codec)
  3072. {
  3073. struct ca0132_spec *spec = codec->spec;
  3074. struct auto_pin_cfg *cfg = &spec->autocfg;
  3075. codec->no_sticky_stream = 1;
  3076. /* line-outs */
  3077. cfg->line_outs = 1;
  3078. cfg->line_out_pins[0] = 0x0b; /* front */
  3079. cfg->line_out_type = AUTO_PIN_LINE_OUT;
  3080. spec->dacs[0] = 0x02;
  3081. spec->out_pins[0] = 0x0b;
  3082. spec->multiout.dac_nids = spec->dacs;
  3083. spec->multiout.num_dacs = 1;
  3084. spec->multiout.max_channels = 2;
  3085. /* headphone */
  3086. cfg->hp_outs = 1;
  3087. cfg->hp_pins[0] = 0x0f;
  3088. spec->hp_dac = 0;
  3089. spec->multiout.hp_nid = 0;
  3090. /* inputs */
  3091. cfg->num_inputs = 2; /* Mic-in and line-in */
  3092. cfg->inputs[0].pin = 0x12;
  3093. cfg->inputs[0].type = AUTO_PIN_MIC;
  3094. cfg->inputs[1].pin = 0x11;
  3095. cfg->inputs[1].type = AUTO_PIN_LINE_IN;
  3096. /* Mic-in */
  3097. spec->input_pins[0] = 0x12;
  3098. spec->input_labels[0] = "Mic";
  3099. spec->adcs[0] = 0x07;
  3100. /* Line-In */
  3101. spec->input_pins[1] = 0x11;
  3102. spec->input_labels[1] = "Line";
  3103. spec->adcs[1] = 0x08;
  3104. spec->num_inputs = 2;
  3105. /* SPDIF I/O */
  3106. spec->dig_out = 0x05;
  3107. spec->multiout.dig_out_nid = spec->dig_out;
  3108. cfg->dig_out_pins[0] = 0x0c;
  3109. cfg->dig_outs = 1;
  3110. cfg->dig_out_type[0] = HDA_PCM_TYPE_SPDIF;
  3111. spec->dig_in = 0x09;
  3112. cfg->dig_in_pin = 0x0e;
  3113. cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
  3114. }
  3115. /*
  3116. * Verbs tables.
  3117. */
  3118. /* Sends before DSP download. */
  3119. static struct hda_verb ca0132_base_init_verbs[] = {
  3120. /*enable ct extension*/
  3121. {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
  3122. /*enable DSP node unsol, needed for DSP download*/
  3123. {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_DSP},
  3124. {}
  3125. };
  3126. /* Send at exit. */
  3127. static struct hda_verb ca0132_base_exit_verbs[] = {
  3128. /*set afg to D3*/
  3129. {0x01, AC_VERB_SET_POWER_STATE, 0x03},
  3130. /*disable ct extension*/
  3131. {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
  3132. {}
  3133. };
  3134. /* Other verbs tables. Sends after DSP download. */
  3135. static struct hda_verb ca0132_init_verbs0[] = {
  3136. /* chip init verbs */
  3137. {0x15, 0x70D, 0xF0},
  3138. {0x15, 0x70E, 0xFE},
  3139. {0x15, 0x707, 0x75},
  3140. {0x15, 0x707, 0xD3},
  3141. {0x15, 0x707, 0x09},
  3142. {0x15, 0x707, 0x53},
  3143. {0x15, 0x707, 0xD4},
  3144. {0x15, 0x707, 0xEF},
  3145. {0x15, 0x707, 0x75},
  3146. {0x15, 0x707, 0xD3},
  3147. {0x15, 0x707, 0x09},
  3148. {0x15, 0x707, 0x02},
  3149. {0x15, 0x707, 0x37},
  3150. {0x15, 0x707, 0x78},
  3151. {0x15, 0x53C, 0xCE},
  3152. {0x15, 0x575, 0xC9},
  3153. {0x15, 0x53D, 0xCE},
  3154. {0x15, 0x5B7, 0xC9},
  3155. {0x15, 0x70D, 0xE8},
  3156. {0x15, 0x70E, 0xFE},
  3157. {0x15, 0x707, 0x02},
  3158. {0x15, 0x707, 0x68},
  3159. {0x15, 0x707, 0x62},
  3160. {0x15, 0x53A, 0xCE},
  3161. {0x15, 0x546, 0xC9},
  3162. {0x15, 0x53B, 0xCE},
  3163. {0x15, 0x5E8, 0xC9},
  3164. {0x15, 0x717, 0x0D},
  3165. {0x15, 0x718, 0x20},
  3166. {}
  3167. };
  3168. static struct hda_verb ca0132_init_verbs1[] = {
  3169. {0x10, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_HP},
  3170. {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_AMIC1},
  3171. /* config EAPD */
  3172. {0x0b, 0x78D, 0x00},
  3173. /*{0x0b, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
  3174. /*{0x10, 0x78D, 0x02},*/
  3175. /*{0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
  3176. {}
  3177. };
  3178. static void ca0132_init_chip(struct hda_codec *codec)
  3179. {
  3180. struct ca0132_spec *spec = codec->spec;
  3181. int num_fx;
  3182. int i;
  3183. unsigned int on;
  3184. mutex_init(&spec->chipio_mutex);
  3185. spec->cur_out_type = SPEAKER_OUT;
  3186. spec->cur_mic_type = DIGITAL_MIC;
  3187. spec->cur_mic_boost = 0;
  3188. for (i = 0; i < VNODES_COUNT; i++) {
  3189. spec->vnode_lvol[i] = 0x5a;
  3190. spec->vnode_rvol[i] = 0x5a;
  3191. spec->vnode_lswitch[i] = 0;
  3192. spec->vnode_rswitch[i] = 0;
  3193. }
  3194. /*
  3195. * Default states for effects are in ca0132_effects[].
  3196. */
  3197. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  3198. for (i = 0; i < num_fx; i++) {
  3199. on = (unsigned int)ca0132_effects[i].reqs[0];
  3200. spec->effects_switch[i] = on ? 1 : 0;
  3201. }
  3202. spec->voicefx_val = 0;
  3203. spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
  3204. spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
  3205. }
  3206. static void ca0132_exit_chip(struct hda_codec *codec)
  3207. {
  3208. /* put any chip cleanup stuffs here. */
  3209. if (dspload_is_loaded(codec))
  3210. dsp_reset(codec);
  3211. }
  3212. static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
  3213. {
  3214. chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
  3215. chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
  3216. chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
  3217. chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
  3218. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
  3219. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
  3220. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
  3221. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
  3222. chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
  3223. }
  3224. static bool ca0132_download_dsp_images(struct hda_codec *codec)
  3225. {
  3226. bool dsp_loaded = false;
  3227. const struct dsp_image_seg *dsp_os_image;
  3228. if (request_firmware_cached(&fw_efx, EFX_FILE,
  3229. codec->bus->card->dev) != 0)
  3230. return false;
  3231. dsp_os_image = (struct dsp_image_seg *)(fw_efx->data);
  3232. dspload_image(codec, dsp_os_image, 0, 0, true, 0);
  3233. dsp_loaded = dspload_wait_loaded(codec);
  3234. return dsp_loaded;
  3235. }
  3236. static void ca0132_download_dsp(struct hda_codec *codec)
  3237. {
  3238. struct ca0132_spec *spec = codec->spec;
  3239. spec->dsp_state = DSP_DOWNLOAD_INIT;
  3240. if (spec->dsp_state == DSP_DOWNLOAD_INIT) {
  3241. chipio_enable_clocks(codec);
  3242. spec->dsp_state = DSP_DOWNLOADING;
  3243. if (!ca0132_download_dsp_images(codec))
  3244. spec->dsp_state = DSP_DOWNLOAD_FAILED;
  3245. else
  3246. spec->dsp_state = DSP_DOWNLOADED;
  3247. }
  3248. if (spec->dsp_state == DSP_DOWNLOADED)
  3249. ca0132_set_dsp_msr(codec, true);
  3250. }
  3251. static int ca0132_init(struct hda_codec *codec)
  3252. {
  3253. struct ca0132_spec *spec = codec->spec;
  3254. struct auto_pin_cfg *cfg = &spec->autocfg;
  3255. int i;
  3256. spec->dsp_state = DSP_DOWNLOAD_INIT;
  3257. spec->curr_chip_addx = (unsigned int)INVALID_CHIP_ADDRESS;
  3258. snd_hda_power_up(codec);
  3259. ca0132_init_params(codec);
  3260. ca0132_init_flags(codec);
  3261. snd_hda_sequence_write(codec, spec->base_init_verbs);
  3262. #ifdef CONFIG_SND_HDA_DSP_LOADER
  3263. ca0132_download_dsp(codec);
  3264. #endif
  3265. ca0132_refresh_widget_caps(codec);
  3266. ca0132_setup_defaults(codec);
  3267. ca0132_init_analog_mic2(codec);
  3268. ca0132_init_dmic(codec);
  3269. for (i = 0; i < spec->num_outputs; i++)
  3270. init_output(codec, spec->out_pins[i], spec->dacs[0]);
  3271. init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
  3272. for (i = 0; i < spec->num_inputs; i++)
  3273. init_input(codec, spec->input_pins[i], spec->adcs[i]);
  3274. init_input(codec, cfg->dig_in_pin, spec->dig_in);
  3275. for (i = 0; i < spec->num_init_verbs; i++)
  3276. snd_hda_sequence_write(codec, spec->init_verbs[i]);
  3277. ca0132_select_out(codec);
  3278. ca0132_select_mic(codec);
  3279. snd_hda_power_down(codec);
  3280. return 0;
  3281. }
  3282. static void ca0132_free(struct hda_codec *codec)
  3283. {
  3284. struct ca0132_spec *spec = codec->spec;
  3285. snd_hda_power_up(codec);
  3286. snd_hda_sequence_write(codec, spec->base_exit_verbs);
  3287. ca0132_exit_chip(codec);
  3288. snd_hda_power_down(codec);
  3289. kfree(codec->spec);
  3290. }
  3291. static struct hda_codec_ops ca0132_patch_ops = {
  3292. .build_controls = ca0132_build_controls,
  3293. .build_pcms = ca0132_build_pcms,
  3294. .init = ca0132_init,
  3295. .free = ca0132_free,
  3296. };
  3297. static int patch_ca0132(struct hda_codec *codec)
  3298. {
  3299. struct ca0132_spec *spec;
  3300. snd_printdd("patch_ca0132\n");
  3301. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  3302. if (!spec)
  3303. return -ENOMEM;
  3304. codec->spec = spec;
  3305. spec->base_init_verbs = ca0132_base_init_verbs;
  3306. spec->base_exit_verbs = ca0132_base_exit_verbs;
  3307. spec->init_verbs[0] = ca0132_init_verbs0;
  3308. spec->init_verbs[1] = ca0132_init_verbs1;
  3309. spec->num_init_verbs = 2;
  3310. ca0132_init_chip(codec);
  3311. ca0132_config(codec);
  3312. codec->patch_ops = ca0132_patch_ops;
  3313. return 0;
  3314. }
  3315. /*
  3316. * patch entries
  3317. */
  3318. static struct hda_codec_preset snd_hda_preset_ca0132[] = {
  3319. { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
  3320. {} /* terminator */
  3321. };
  3322. MODULE_ALIAS("snd-hda-codec-id:11020011");
  3323. MODULE_LICENSE("GPL");
  3324. MODULE_DESCRIPTION("Creative CA0132, CA0132 HD-audio codec");
  3325. static struct hda_codec_preset_list ca0132_list = {
  3326. .preset = snd_hda_preset_ca0132,
  3327. .owner = THIS_MODULE,
  3328. };
  3329. static int __init patch_ca0132_init(void)
  3330. {
  3331. return snd_hda_add_codec_preset(&ca0132_list);
  3332. }
  3333. static void __exit patch_ca0132_exit(void)
  3334. {
  3335. release_cached_firmware();
  3336. snd_hda_delete_codec_preset(&ca0132_list);
  3337. }
  3338. module_init(patch_ca0132_init)
  3339. module_exit(patch_ca0132_exit)