ac97_codec.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  3. * Universal interface for Audio Codec '97
  4. *
  5. * For more details look to AC '97 component specification revision 2.2
  6. * by Intel Corporation (http://developer.intel.com).
  7. *
  8. *
  9. * This program 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 program 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. */
  24. #include <sound/driver.h>
  25. #include <linux/delay.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/pci.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/mutex.h>
  31. #include <sound/core.h>
  32. #include <sound/pcm.h>
  33. #include <sound/tlv.h>
  34. #include <sound/ac97_codec.h>
  35. #include <sound/asoundef.h>
  36. #include <sound/initval.h>
  37. #include "ac97_id.h"
  38. #include "ac97_patch.c"
  39. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  40. MODULE_DESCRIPTION("Universal interface for Audio Codec '97");
  41. MODULE_LICENSE("GPL");
  42. static int enable_loopback;
  43. module_param(enable_loopback, bool, 0444);
  44. MODULE_PARM_DESC(enable_loopback, "Enable AC97 ADC/DAC Loopback Control");
  45. #ifdef CONFIG_SND_AC97_POWER_SAVE
  46. static int power_save;
  47. module_param(power_save, bool, 0644);
  48. MODULE_PARM_DESC(power_save, "Enable AC97 power-saving control");
  49. #endif
  50. /*
  51. */
  52. struct ac97_codec_id {
  53. unsigned int id;
  54. unsigned int mask;
  55. const char *name;
  56. int (*patch)(struct snd_ac97 *ac97);
  57. int (*mpatch)(struct snd_ac97 *ac97);
  58. unsigned int flags;
  59. };
  60. static const struct ac97_codec_id snd_ac97_codec_id_vendors[] = {
  61. { 0x414b4d00, 0xffffff00, "Asahi Kasei", NULL, NULL },
  62. { 0x41445300, 0xffffff00, "Analog Devices", NULL, NULL },
  63. { 0x414c4300, 0xffffff00, "Realtek", NULL, NULL },
  64. { 0x414c4700, 0xffffff00, "Realtek", NULL, NULL },
  65. { 0x434d4900, 0xffffff00, "C-Media Electronics", NULL, NULL },
  66. { 0x43525900, 0xffffff00, "Cirrus Logic", NULL, NULL },
  67. { 0x43585400, 0xffffff00, "Conexant", NULL, NULL },
  68. { 0x44543000, 0xffffff00, "Diamond Technology", NULL, NULL },
  69. { 0x454d4300, 0xffffff00, "eMicro", NULL, NULL },
  70. { 0x45838300, 0xffffff00, "ESS Technology", NULL, NULL },
  71. { 0x48525300, 0xffffff00, "Intersil", NULL, NULL },
  72. { 0x49434500, 0xffffff00, "ICEnsemble", NULL, NULL },
  73. { 0x49544500, 0xffffff00, "ITE Tech.Inc", NULL, NULL },
  74. { 0x4e534300, 0xffffff00, "National Semiconductor", NULL, NULL },
  75. { 0x50534300, 0xffffff00, "Philips", NULL, NULL },
  76. { 0x53494c00, 0xffffff00, "Silicon Laboratory", NULL, NULL },
  77. { 0x54524100, 0xffffff00, "TriTech", NULL, NULL },
  78. { 0x54584e00, 0xffffff00, "Texas Instruments", NULL, NULL },
  79. { 0x56494100, 0xffffff00, "VIA Technologies", NULL, NULL },
  80. { 0x57454300, 0xffffff00, "Winbond", NULL, NULL },
  81. { 0x574d4c00, 0xffffff00, "Wolfson", NULL, NULL },
  82. { 0x594d4800, 0xffffff00, "Yamaha", NULL, NULL },
  83. { 0x83847600, 0xffffff00, "SigmaTel", NULL, NULL },
  84. { 0, 0, NULL, NULL, NULL }
  85. };
  86. static const struct ac97_codec_id snd_ac97_codec_ids[] = {
  87. { 0x414b4d00, 0xffffffff, "AK4540", NULL, NULL },
  88. { 0x414b4d01, 0xffffffff, "AK4542", NULL, NULL },
  89. { 0x414b4d02, 0xffffffff, "AK4543", NULL, NULL },
  90. { 0x414b4d06, 0xffffffff, "AK4544A", NULL, NULL },
  91. { 0x414b4d07, 0xffffffff, "AK4545", NULL, NULL },
  92. { 0x41445303, 0xffffffff, "AD1819", patch_ad1819, NULL },
  93. { 0x41445340, 0xffffffff, "AD1881", patch_ad1881, NULL },
  94. { 0x41445348, 0xffffffff, "AD1881A", patch_ad1881, NULL },
  95. { 0x41445360, 0xffffffff, "AD1885", patch_ad1885, NULL },
  96. { 0x41445361, 0xffffffff, "AD1886", patch_ad1886, NULL },
  97. { 0x41445362, 0xffffffff, "AD1887", patch_ad1881, NULL },
  98. { 0x41445363, 0xffffffff, "AD1886A", patch_ad1881, NULL },
  99. { 0x41445368, 0xffffffff, "AD1888", patch_ad1888, NULL },
  100. { 0x41445370, 0xffffffff, "AD1980", patch_ad1980, NULL },
  101. { 0x41445372, 0xffffffff, "AD1981A", patch_ad1981a, NULL },
  102. { 0x41445374, 0xffffffff, "AD1981B", patch_ad1981b, NULL },
  103. { 0x41445375, 0xffffffff, "AD1985", patch_ad1985, NULL },
  104. { 0x41445378, 0xffffffff, "AD1986", patch_ad1986, NULL },
  105. { 0x414c4300, 0xffffff00, "ALC100,100P", NULL, NULL },
  106. { 0x414c4710, 0xfffffff0, "ALC200,200P", NULL, NULL },
  107. { 0x414c4721, 0xffffffff, "ALC650D", NULL, NULL }, /* already patched */
  108. { 0x414c4722, 0xffffffff, "ALC650E", NULL, NULL }, /* already patched */
  109. { 0x414c4723, 0xffffffff, "ALC650F", NULL, NULL }, /* already patched */
  110. { 0x414c4720, 0xfffffff0, "ALC650", patch_alc650, NULL },
  111. { 0x414c4760, 0xfffffff0, "ALC655", patch_alc655, NULL },
  112. { 0x414c4781, 0xffffffff, "ALC658D", NULL, NULL }, /* already patched */
  113. { 0x414c4780, 0xfffffff0, "ALC658", patch_alc655, NULL },
  114. { 0x414c4790, 0xfffffff0, "ALC850", patch_alc850, NULL },
  115. { 0x414c4730, 0xffffffff, "ALC101", NULL, NULL },
  116. { 0x414c4740, 0xfffffff0, "ALC202", NULL, NULL },
  117. { 0x414c4750, 0xfffffff0, "ALC250", NULL, NULL },
  118. { 0x414c4770, 0xfffffff0, "ALC203", NULL, NULL },
  119. { 0x434d4941, 0xffffffff, "CMI9738", patch_cm9738, NULL },
  120. { 0x434d4961, 0xffffffff, "CMI9739", patch_cm9739, NULL },
  121. { 0x434d4969, 0xffffffff, "CMI9780", patch_cm9780, NULL },
  122. { 0x434d4978, 0xffffffff, "CMI9761A", patch_cm9761, NULL },
  123. { 0x434d4982, 0xffffffff, "CMI9761B", patch_cm9761, NULL },
  124. { 0x434d4983, 0xffffffff, "CMI9761A+", patch_cm9761, NULL },
  125. { 0x43525900, 0xfffffff8, "CS4297", NULL, NULL },
  126. { 0x43525910, 0xfffffff8, "CS4297A", patch_cirrus_spdif, NULL },
  127. { 0x43525920, 0xfffffff8, "CS4298", patch_cirrus_spdif, NULL },
  128. { 0x43525928, 0xfffffff8, "CS4294", NULL, NULL },
  129. { 0x43525930, 0xfffffff8, "CS4299", patch_cirrus_cs4299, NULL },
  130. { 0x43525948, 0xfffffff8, "CS4201", NULL, NULL },
  131. { 0x43525958, 0xfffffff8, "CS4205", patch_cirrus_spdif, NULL },
  132. { 0x43525960, 0xfffffff8, "CS4291", NULL, NULL },
  133. { 0x43525970, 0xfffffff8, "CS4202", NULL, NULL },
  134. { 0x43585421, 0xffffffff, "HSD11246", NULL, NULL }, // SmartMC II
  135. { 0x43585428, 0xfffffff8, "Cx20468", patch_conexant, NULL }, // SmartAMC fixme: the mask might be different
  136. { 0x43585431, 0xffffffff, "Cx20551", patch_cx20551, NULL },
  137. { 0x44543031, 0xfffffff0, "DT0398", NULL, NULL },
  138. { 0x454d4328, 0xffffffff, "EM28028", NULL, NULL }, // same as TR28028?
  139. { 0x45838308, 0xffffffff, "ESS1988", NULL, NULL },
  140. { 0x48525300, 0xffffff00, "HMP9701", NULL, NULL },
  141. { 0x49434501, 0xffffffff, "ICE1230", NULL, NULL },
  142. { 0x49434511, 0xffffffff, "ICE1232", NULL, NULL }, // alias VIA VT1611A?
  143. { 0x49434514, 0xffffffff, "ICE1232A", NULL, NULL },
  144. { 0x49434551, 0xffffffff, "VT1616", patch_vt1616, NULL },
  145. { 0x49434552, 0xffffffff, "VT1616i", patch_vt1616, NULL }, // VT1616 compatible (chipset integrated)
  146. { 0x49544520, 0xffffffff, "IT2226E", NULL, NULL },
  147. { 0x49544561, 0xffffffff, "IT2646E", patch_it2646, NULL },
  148. { 0x4e534300, 0xffffffff, "LM4540,43,45,46,48", NULL, NULL }, // only guess --jk
  149. { 0x4e534331, 0xffffffff, "LM4549", NULL, NULL },
  150. { 0x4e534350, 0xffffffff, "LM4550", patch_lm4550, NULL }, // volume wrap fix
  151. { 0x50534304, 0xffffffff, "UCB1400", patch_ucb1400, NULL },
  152. { 0x53494c20, 0xffffffe0, "Si3036,8", mpatch_si3036, mpatch_si3036, AC97_MODEM_PATCH },
  153. { 0x54524102, 0xffffffff, "TR28022", NULL, NULL },
  154. { 0x54524106, 0xffffffff, "TR28026", NULL, NULL },
  155. { 0x54524108, 0xffffffff, "TR28028", patch_tritech_tr28028, NULL }, // added by xin jin [07/09/99]
  156. { 0x54524123, 0xffffffff, "TR28602", NULL, NULL }, // only guess --jk [TR28023 = eMicro EM28023 (new CT1297)]
  157. { 0x54584e20, 0xffffffff, "TLC320AD9xC", NULL, NULL },
  158. { 0x56494161, 0xffffffff, "VIA1612A", NULL, NULL }, // modified ICE1232 with S/PDIF
  159. { 0x56494170, 0xffffffff, "VIA1617A", patch_vt1617a, NULL }, // modified VT1616 with S/PDIF
  160. { 0x56494182, 0xffffffff, "VIA1618", NULL, NULL },
  161. { 0x57454301, 0xffffffff, "W83971D", NULL, NULL },
  162. { 0x574d4c00, 0xffffffff, "WM9701A", NULL, NULL },
  163. { 0x574d4C03, 0xffffffff, "WM9703,WM9707,WM9708,WM9717", patch_wolfson03, NULL},
  164. { 0x574d4C04, 0xffffffff, "WM9704M,WM9704Q", patch_wolfson04, NULL},
  165. { 0x574d4C05, 0xffffffff, "WM9705,WM9710", patch_wolfson05, NULL},
  166. { 0x574d4C09, 0xffffffff, "WM9709", NULL, NULL},
  167. { 0x574d4C12, 0xffffffff, "WM9711,WM9712", patch_wolfson11, NULL},
  168. { 0x574d4c13, 0xffffffff, "WM9713,WM9714", patch_wolfson13, NULL, AC97_DEFAULT_POWER_OFF},
  169. { 0x594d4800, 0xffffffff, "YMF743", NULL, NULL },
  170. { 0x594d4802, 0xffffffff, "YMF752", NULL, NULL },
  171. { 0x594d4803, 0xffffffff, "YMF753", patch_yamaha_ymf753, NULL },
  172. { 0x83847600, 0xffffffff, "STAC9700,83,84", patch_sigmatel_stac9700, NULL },
  173. { 0x83847604, 0xffffffff, "STAC9701,3,4,5", NULL, NULL },
  174. { 0x83847605, 0xffffffff, "STAC9704", NULL, NULL },
  175. { 0x83847608, 0xffffffff, "STAC9708,11", patch_sigmatel_stac9708, NULL },
  176. { 0x83847609, 0xffffffff, "STAC9721,23", patch_sigmatel_stac9721, NULL },
  177. { 0x83847644, 0xffffffff, "STAC9744", patch_sigmatel_stac9744, NULL },
  178. { 0x83847650, 0xffffffff, "STAC9750,51", NULL, NULL }, // patch?
  179. { 0x83847652, 0xffffffff, "STAC9752,53", NULL, NULL }, // patch?
  180. { 0x83847656, 0xffffffff, "STAC9756,57", patch_sigmatel_stac9756, NULL },
  181. { 0x83847658, 0xffffffff, "STAC9758,59", patch_sigmatel_stac9758, NULL },
  182. { 0x83847666, 0xffffffff, "STAC9766,67", NULL, NULL }, // patch?
  183. { 0, 0, NULL, NULL, NULL }
  184. };
  185. static void update_power_regs(struct snd_ac97 *ac97);
  186. #ifdef CONFIG_SND_AC97_POWER_SAVE
  187. #define ac97_is_power_save_mode(ac97) \
  188. ((ac97->scaps & AC97_SCAP_POWER_SAVE) && power_save)
  189. #else
  190. #define ac97_is_power_save_mode(ac97) 0
  191. #endif
  192. /*
  193. * I/O routines
  194. */
  195. static int snd_ac97_valid_reg(struct snd_ac97 *ac97, unsigned short reg)
  196. {
  197. /* filter some registers for buggy codecs */
  198. switch (ac97->id) {
  199. case AC97_ID_AK4540:
  200. case AC97_ID_AK4542:
  201. if (reg <= 0x1c || reg == 0x20 || reg == 0x26 || reg >= 0x7c)
  202. return 1;
  203. return 0;
  204. case AC97_ID_AD1819: /* AD1819 */
  205. case AC97_ID_AD1881: /* AD1881 */
  206. case AC97_ID_AD1881A: /* AD1881A */
  207. if (reg >= 0x3a && reg <= 0x6e) /* 0x59 */
  208. return 0;
  209. return 1;
  210. case AC97_ID_AD1885: /* AD1885 */
  211. case AC97_ID_AD1886: /* AD1886 */
  212. case AC97_ID_AD1886A: /* AD1886A - !!verify!! --jk */
  213. case AC97_ID_AD1887: /* AD1887 - !!verify!! --jk */
  214. if (reg == 0x5a)
  215. return 1;
  216. if (reg >= 0x3c && reg <= 0x6e) /* 0x59 */
  217. return 0;
  218. return 1;
  219. case AC97_ID_STAC9700:
  220. case AC97_ID_STAC9704:
  221. case AC97_ID_STAC9705:
  222. case AC97_ID_STAC9708:
  223. case AC97_ID_STAC9721:
  224. case AC97_ID_STAC9744:
  225. case AC97_ID_STAC9756:
  226. if (reg <= 0x3a || reg >= 0x5a)
  227. return 1;
  228. return 0;
  229. }
  230. return 1;
  231. }
  232. /**
  233. * snd_ac97_write - write a value on the given register
  234. * @ac97: the ac97 instance
  235. * @reg: the register to change
  236. * @value: the value to set
  237. *
  238. * Writes a value on the given register. This will invoke the write
  239. * callback directly after the register check.
  240. * This function doesn't change the register cache unlike
  241. * #snd_ca97_write_cache(), so use this only when you don't want to
  242. * reflect the change to the suspend/resume state.
  243. */
  244. void snd_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
  245. {
  246. if (!snd_ac97_valid_reg(ac97, reg))
  247. return;
  248. if ((ac97->id & 0xffffff00) == AC97_ID_ALC100) {
  249. /* Fix H/W bug of ALC100/100P */
  250. if (reg == AC97_MASTER || reg == AC97_HEADPHONE)
  251. ac97->bus->ops->write(ac97, AC97_RESET, 0); /* reset audio codec */
  252. }
  253. ac97->bus->ops->write(ac97, reg, value);
  254. }
  255. EXPORT_SYMBOL(snd_ac97_write);
  256. /**
  257. * snd_ac97_read - read a value from the given register
  258. *
  259. * @ac97: the ac97 instance
  260. * @reg: the register to read
  261. *
  262. * Reads a value from the given register. This will invoke the read
  263. * callback directly after the register check.
  264. *
  265. * Returns the read value.
  266. */
  267. unsigned short snd_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
  268. {
  269. if (!snd_ac97_valid_reg(ac97, reg))
  270. return 0;
  271. return ac97->bus->ops->read(ac97, reg);
  272. }
  273. /* read a register - return the cached value if already read */
  274. static inline unsigned short snd_ac97_read_cache(struct snd_ac97 *ac97, unsigned short reg)
  275. {
  276. if (! test_bit(reg, ac97->reg_accessed)) {
  277. ac97->regs[reg] = ac97->bus->ops->read(ac97, reg);
  278. // set_bit(reg, ac97->reg_accessed);
  279. }
  280. return ac97->regs[reg];
  281. }
  282. EXPORT_SYMBOL(snd_ac97_read);
  283. /**
  284. * snd_ac97_write_cache - write a value on the given register and update the cache
  285. * @ac97: the ac97 instance
  286. * @reg: the register to change
  287. * @value: the value to set
  288. *
  289. * Writes a value on the given register and updates the register
  290. * cache. The cached values are used for the cached-read and the
  291. * suspend/resume.
  292. */
  293. void snd_ac97_write_cache(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
  294. {
  295. if (!snd_ac97_valid_reg(ac97, reg))
  296. return;
  297. mutex_lock(&ac97->reg_mutex);
  298. ac97->regs[reg] = value;
  299. ac97->bus->ops->write(ac97, reg, value);
  300. set_bit(reg, ac97->reg_accessed);
  301. mutex_unlock(&ac97->reg_mutex);
  302. }
  303. EXPORT_SYMBOL(snd_ac97_write_cache);
  304. /**
  305. * snd_ac97_update - update the value on the given register
  306. * @ac97: the ac97 instance
  307. * @reg: the register to change
  308. * @value: the value to set
  309. *
  310. * Compares the value with the register cache and updates the value
  311. * only when the value is changed.
  312. *
  313. * Returns 1 if the value is changed, 0 if no change, or a negative
  314. * code on failure.
  315. */
  316. int snd_ac97_update(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
  317. {
  318. int change;
  319. if (!snd_ac97_valid_reg(ac97, reg))
  320. return -EINVAL;
  321. mutex_lock(&ac97->reg_mutex);
  322. change = ac97->regs[reg] != value;
  323. if (change) {
  324. ac97->regs[reg] = value;
  325. ac97->bus->ops->write(ac97, reg, value);
  326. }
  327. set_bit(reg, ac97->reg_accessed);
  328. mutex_unlock(&ac97->reg_mutex);
  329. return change;
  330. }
  331. EXPORT_SYMBOL(snd_ac97_update);
  332. /**
  333. * snd_ac97_update_bits - update the bits on the given register
  334. * @ac97: the ac97 instance
  335. * @reg: the register to change
  336. * @mask: the bit-mask to change
  337. * @value: the value to set
  338. *
  339. * Updates the masked-bits on the given register only when the value
  340. * is changed.
  341. *
  342. * Returns 1 if the bits are changed, 0 if no change, or a negative
  343. * code on failure.
  344. */
  345. int snd_ac97_update_bits(struct snd_ac97 *ac97, unsigned short reg, unsigned short mask, unsigned short value)
  346. {
  347. int change;
  348. if (!snd_ac97_valid_reg(ac97, reg))
  349. return -EINVAL;
  350. mutex_lock(&ac97->reg_mutex);
  351. change = snd_ac97_update_bits_nolock(ac97, reg, mask, value);
  352. mutex_unlock(&ac97->reg_mutex);
  353. return change;
  354. }
  355. EXPORT_SYMBOL(snd_ac97_update_bits);
  356. /* no lock version - see snd_ac97_updat_bits() */
  357. int snd_ac97_update_bits_nolock(struct snd_ac97 *ac97, unsigned short reg,
  358. unsigned short mask, unsigned short value)
  359. {
  360. int change;
  361. unsigned short old, new;
  362. old = snd_ac97_read_cache(ac97, reg);
  363. new = (old & ~mask) | (value & mask);
  364. change = old != new;
  365. if (change) {
  366. ac97->regs[reg] = new;
  367. ac97->bus->ops->write(ac97, reg, new);
  368. }
  369. set_bit(reg, ac97->reg_accessed);
  370. return change;
  371. }
  372. static int snd_ac97_ad18xx_update_pcm_bits(struct snd_ac97 *ac97, int codec, unsigned short mask, unsigned short value)
  373. {
  374. int change;
  375. unsigned short old, new, cfg;
  376. mutex_lock(&ac97->page_mutex);
  377. old = ac97->spec.ad18xx.pcmreg[codec];
  378. new = (old & ~mask) | (value & mask);
  379. change = old != new;
  380. if (change) {
  381. mutex_lock(&ac97->reg_mutex);
  382. cfg = snd_ac97_read_cache(ac97, AC97_AD_SERIAL_CFG);
  383. ac97->spec.ad18xx.pcmreg[codec] = new;
  384. /* select single codec */
  385. ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG,
  386. (cfg & ~0x7000) |
  387. ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
  388. /* update PCM bits */
  389. ac97->bus->ops->write(ac97, AC97_PCM, new);
  390. /* select all codecs */
  391. ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG,
  392. cfg | 0x7000);
  393. mutex_unlock(&ac97->reg_mutex);
  394. }
  395. mutex_unlock(&ac97->page_mutex);
  396. return change;
  397. }
  398. /*
  399. * Controls
  400. */
  401. static int snd_ac97_info_enum_double(struct snd_kcontrol *kcontrol,
  402. struct snd_ctl_elem_info *uinfo)
  403. {
  404. struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
  405. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  406. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  407. uinfo->value.enumerated.items = e->mask;
  408. if (uinfo->value.enumerated.item > e->mask - 1)
  409. uinfo->value.enumerated.item = e->mask - 1;
  410. strcpy(uinfo->value.enumerated.name, e->texts[uinfo->value.enumerated.item]);
  411. return 0;
  412. }
  413. static int snd_ac97_get_enum_double(struct snd_kcontrol *kcontrol,
  414. struct snd_ctl_elem_value *ucontrol)
  415. {
  416. struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
  417. struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
  418. unsigned short val, bitmask;
  419. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  420. ;
  421. val = snd_ac97_read_cache(ac97, e->reg);
  422. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  423. if (e->shift_l != e->shift_r)
  424. ucontrol->value.enumerated.item[1] = (val >> e->shift_r) & (bitmask - 1);
  425. return 0;
  426. }
  427. static int snd_ac97_put_enum_double(struct snd_kcontrol *kcontrol,
  428. struct snd_ctl_elem_value *ucontrol)
  429. {
  430. struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
  431. struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
  432. unsigned short val;
  433. unsigned short mask, bitmask;
  434. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  435. ;
  436. if (ucontrol->value.enumerated.item[0] > e->mask - 1)
  437. return -EINVAL;
  438. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  439. mask = (bitmask - 1) << e->shift_l;
  440. if (e->shift_l != e->shift_r) {
  441. if (ucontrol->value.enumerated.item[1] > e->mask - 1)
  442. return -EINVAL;
  443. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  444. mask |= (bitmask - 1) << e->shift_r;
  445. }
  446. return snd_ac97_update_bits(ac97, e->reg, mask, val);
  447. }
  448. /* save/restore ac97 v2.3 paging */
  449. static int snd_ac97_page_save(struct snd_ac97 *ac97, int reg, struct snd_kcontrol *kcontrol)
  450. {
  451. int page_save = -1;
  452. if ((kcontrol->private_value & (1<<25)) &&
  453. (ac97->ext_id & AC97_EI_REV_MASK) >= AC97_EI_REV_23 &&
  454. (reg >= 0x60 && reg < 0x70)) {
  455. unsigned short page = (kcontrol->private_value >> 26) & 0x0f;
  456. mutex_lock(&ac97->page_mutex); /* lock paging */
  457. page_save = snd_ac97_read(ac97, AC97_INT_PAGING) & AC97_PAGE_MASK;
  458. snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page);
  459. }
  460. return page_save;
  461. }
  462. static void snd_ac97_page_restore(struct snd_ac97 *ac97, int page_save)
  463. {
  464. if (page_save >= 0) {
  465. snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page_save);
  466. mutex_unlock(&ac97->page_mutex); /* unlock paging */
  467. }
  468. }
  469. /* volume and switch controls */
  470. static int snd_ac97_info_volsw(struct snd_kcontrol *kcontrol,
  471. struct snd_ctl_elem_info *uinfo)
  472. {
  473. int mask = (kcontrol->private_value >> 16) & 0xff;
  474. int shift = (kcontrol->private_value >> 8) & 0x0f;
  475. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  476. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  477. uinfo->count = shift == rshift ? 1 : 2;
  478. uinfo->value.integer.min = 0;
  479. uinfo->value.integer.max = mask;
  480. return 0;
  481. }
  482. static int snd_ac97_get_volsw(struct snd_kcontrol *kcontrol,
  483. struct snd_ctl_elem_value *ucontrol)
  484. {
  485. struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
  486. int reg = kcontrol->private_value & 0xff;
  487. int shift = (kcontrol->private_value >> 8) & 0x0f;
  488. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  489. int mask = (kcontrol->private_value >> 16) & 0xff;
  490. int invert = (kcontrol->private_value >> 24) & 0x01;
  491. int page_save;
  492. page_save = snd_ac97_page_save(ac97, reg, kcontrol);
  493. ucontrol->value.integer.value[0] = (snd_ac97_read_cache(ac97, reg) >> shift) & mask;
  494. if (shift != rshift)
  495. ucontrol->value.integer.value[1] = (snd_ac97_read_cache(ac97, reg) >> rshift) & mask;
  496. if (invert) {
  497. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  498. if (shift != rshift)
  499. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  500. }
  501. snd_ac97_page_restore(ac97, page_save);
  502. return 0;
  503. }
  504. static int snd_ac97_put_volsw(struct snd_kcontrol *kcontrol,
  505. struct snd_ctl_elem_value *ucontrol)
  506. {
  507. struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
  508. int reg = kcontrol->private_value & 0xff;
  509. int shift = (kcontrol->private_value >> 8) & 0x0f;
  510. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  511. int mask = (kcontrol->private_value >> 16) & 0xff;
  512. int invert = (kcontrol->private_value >> 24) & 0x01;
  513. int err, page_save;
  514. unsigned short val, val2, val_mask;
  515. page_save = snd_ac97_page_save(ac97, reg, kcontrol);
  516. val = (ucontrol->value.integer.value[0] & mask);
  517. if (invert)
  518. val = mask - val;
  519. val_mask = mask << shift;
  520. val = val << shift;
  521. if (shift != rshift) {
  522. val2 = (ucontrol->value.integer.value[1] & mask);
  523. if (invert)
  524. val2 = mask - val2;
  525. val_mask |= mask << rshift;
  526. val |= val2 << rshift;
  527. }
  528. err = snd_ac97_update_bits(ac97, reg, val_mask, val);
  529. snd_ac97_page_restore(ac97, page_save);
  530. #ifdef CONFIG_SND_AC97_POWER_SAVE
  531. /* check analog mixer power-down */
  532. if ((val_mask & 0x8000) &&
  533. (kcontrol->private_value & (1<<30))) {
  534. if (val & 0x8000)
  535. ac97->power_up &= ~(1 << (reg>>1));
  536. else
  537. ac97->power_up |= 1 << (reg>>1);
  538. update_power_regs(ac97);
  539. }
  540. #endif
  541. return err;
  542. }
  543. static const struct snd_kcontrol_new snd_ac97_controls_master_mono[2] = {
  544. AC97_SINGLE("Master Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
  545. AC97_SINGLE("Master Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1)
  546. };
  547. static const struct snd_kcontrol_new snd_ac97_controls_tone[2] = {
  548. AC97_SINGLE("Tone Control - Bass", AC97_MASTER_TONE, 8, 15, 1),
  549. AC97_SINGLE("Tone Control - Treble", AC97_MASTER_TONE, 0, 15, 1)
  550. };
  551. static const struct snd_kcontrol_new snd_ac97_controls_pc_beep[2] = {
  552. AC97_SINGLE("PC Speaker Playback Switch", AC97_PC_BEEP, 15, 1, 1),
  553. AC97_SINGLE("PC Speaker Playback Volume", AC97_PC_BEEP, 1, 15, 1)
  554. };
  555. static const struct snd_kcontrol_new snd_ac97_controls_mic_boost =
  556. AC97_SINGLE("Mic Boost (+20dB)", AC97_MIC, 6, 1, 0);
  557. static const char* std_rec_sel[] = {"Mic", "CD", "Video", "Aux", "Line", "Mix", "Mix Mono", "Phone"};
  558. static const char* std_3d_path[] = {"pre 3D", "post 3D"};
  559. static const char* std_mix[] = {"Mix", "Mic"};
  560. static const char* std_mic[] = {"Mic1", "Mic2"};
  561. static const struct ac97_enum std_enum[] = {
  562. AC97_ENUM_DOUBLE(AC97_REC_SEL, 8, 0, 8, std_rec_sel),
  563. AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, std_3d_path),
  564. AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 9, 2, std_mix),
  565. AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 8, 2, std_mic),
  566. };
  567. static const struct snd_kcontrol_new snd_ac97_control_capture_src =
  568. AC97_ENUM("Capture Source", std_enum[0]);
  569. static const struct snd_kcontrol_new snd_ac97_control_capture_vol =
  570. AC97_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0);
  571. static const struct snd_kcontrol_new snd_ac97_controls_mic_capture[2] = {
  572. AC97_SINGLE("Mic Capture Switch", AC97_REC_GAIN_MIC, 15, 1, 1),
  573. AC97_SINGLE("Mic Capture Volume", AC97_REC_GAIN_MIC, 0, 15, 0)
  574. };
  575. enum {
  576. AC97_GENERAL_PCM_OUT = 0,
  577. AC97_GENERAL_STEREO_ENHANCEMENT,
  578. AC97_GENERAL_3D,
  579. AC97_GENERAL_LOUDNESS,
  580. AC97_GENERAL_MONO,
  581. AC97_GENERAL_MIC,
  582. AC97_GENERAL_LOOPBACK
  583. };
  584. static const struct snd_kcontrol_new snd_ac97_controls_general[7] = {
  585. AC97_ENUM("PCM Out Path & Mute", std_enum[1]),
  586. AC97_SINGLE("Simulated Stereo Enhancement", AC97_GENERAL_PURPOSE, 14, 1, 0),
  587. AC97_SINGLE("3D Control - Switch", AC97_GENERAL_PURPOSE, 13, 1, 0),
  588. AC97_SINGLE("Loudness (bass boost)", AC97_GENERAL_PURPOSE, 12, 1, 0),
  589. AC97_ENUM("Mono Output Select", std_enum[2]),
  590. AC97_ENUM("Mic Select", std_enum[3]),
  591. AC97_SINGLE("ADC/DAC Loopback", AC97_GENERAL_PURPOSE, 7, 1, 0)
  592. };
  593. static const struct snd_kcontrol_new snd_ac97_controls_3d[2] = {
  594. AC97_SINGLE("3D Control - Center", AC97_3D_CONTROL, 8, 15, 0),
  595. AC97_SINGLE("3D Control - Depth", AC97_3D_CONTROL, 0, 15, 0)
  596. };
  597. static const struct snd_kcontrol_new snd_ac97_controls_center[2] = {
  598. AC97_SINGLE("Center Playback Switch", AC97_CENTER_LFE_MASTER, 7, 1, 1),
  599. AC97_SINGLE("Center Playback Volume", AC97_CENTER_LFE_MASTER, 0, 31, 1)
  600. };
  601. static const struct snd_kcontrol_new snd_ac97_controls_lfe[2] = {
  602. AC97_SINGLE("LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 1, 1),
  603. AC97_SINGLE("LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 31, 1)
  604. };
  605. static const struct snd_kcontrol_new snd_ac97_control_eapd =
  606. AC97_SINGLE("External Amplifier", AC97_POWERDOWN, 15, 1, 1);
  607. static const struct snd_kcontrol_new snd_ac97_controls_modem_switches[2] = {
  608. AC97_SINGLE("Off-hook Switch", AC97_GPIO_STATUS, 0, 1, 0),
  609. AC97_SINGLE("Caller ID Switch", AC97_GPIO_STATUS, 2, 1, 0)
  610. };
  611. /* change the existing EAPD control as inverted */
  612. static void set_inv_eapd(struct snd_ac97 *ac97, struct snd_kcontrol *kctl)
  613. {
  614. kctl->private_value = AC97_SINGLE_VALUE(AC97_POWERDOWN, 15, 1, 0);
  615. snd_ac97_update_bits(ac97, AC97_POWERDOWN, (1<<15), (1<<15)); /* EAPD up */
  616. ac97->scaps |= AC97_SCAP_INV_EAPD;
  617. }
  618. static int snd_ac97_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  619. {
  620. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  621. uinfo->count = 1;
  622. return 0;
  623. }
  624. static int snd_ac97_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  625. {
  626. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  627. IEC958_AES0_NONAUDIO |
  628. IEC958_AES0_CON_EMPHASIS_5015 |
  629. IEC958_AES0_CON_NOT_COPYRIGHT;
  630. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  631. IEC958_AES1_CON_ORIGINAL;
  632. ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
  633. return 0;
  634. }
  635. static int snd_ac97_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  636. {
  637. /* FIXME: AC'97 spec doesn't say which bits are used for what */
  638. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  639. IEC958_AES0_NONAUDIO |
  640. IEC958_AES0_PRO_FS |
  641. IEC958_AES0_PRO_EMPHASIS_5015;
  642. return 0;
  643. }
  644. static int snd_ac97_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  645. {
  646. struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
  647. mutex_lock(&ac97->reg_mutex);
  648. ucontrol->value.iec958.status[0] = ac97->spdif_status & 0xff;
  649. ucontrol->value.iec958.status[1] = (ac97->spdif_status >> 8) & 0xff;
  650. ucontrol->value.iec958.status[2] = (ac97->spdif_status >> 16) & 0xff;
  651. ucontrol->value.iec958.status[3] = (ac97->spdif_status >> 24) & 0xff;
  652. mutex_unlock(&ac97->reg_mutex);
  653. return 0;
  654. }
  655. static int snd_ac97_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  656. {
  657. struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
  658. unsigned int new = 0;
  659. unsigned short val = 0;
  660. int change;
  661. new = val = ucontrol->value.iec958.status[0] & (IEC958_AES0_PROFESSIONAL|IEC958_AES0_NONAUDIO);
  662. if (ucontrol->value.iec958.status[0] & IEC958_AES0_PROFESSIONAL) {
  663. new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_PRO_FS|IEC958_AES0_PRO_EMPHASIS_5015);
  664. switch (new & IEC958_AES0_PRO_FS) {
  665. case IEC958_AES0_PRO_FS_44100: val |= 0<<12; break;
  666. case IEC958_AES0_PRO_FS_48000: val |= 2<<12; break;
  667. case IEC958_AES0_PRO_FS_32000: val |= 3<<12; break;
  668. default: val |= 1<<12; break;
  669. }
  670. if ((new & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
  671. val |= 1<<3;
  672. } else {
  673. new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT);
  674. new |= ((ucontrol->value.iec958.status[1] & (IEC958_AES1_CON_CATEGORY|IEC958_AES1_CON_ORIGINAL)) << 8);
  675. new |= ((ucontrol->value.iec958.status[3] & IEC958_AES3_CON_FS) << 24);
  676. if ((new & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
  677. val |= 1<<3;
  678. if (!(new & IEC958_AES0_CON_NOT_COPYRIGHT))
  679. val |= 1<<2;
  680. val |= ((new >> 8) & 0xff) << 4; // category + original
  681. switch ((new >> 24) & 0xff) {
  682. case IEC958_AES3_CON_FS_44100: val |= 0<<12; break;
  683. case IEC958_AES3_CON_FS_48000: val |= 2<<12; break;
  684. case IEC958_AES3_CON_FS_32000: val |= 3<<12; break;
  685. default: val |= 1<<12; break;
  686. }
  687. }
  688. mutex_lock(&ac97->reg_mutex);
  689. change = ac97->spdif_status != new;
  690. ac97->spdif_status = new;
  691. if (ac97->flags & AC97_CS_SPDIF) {
  692. int x = (val >> 12) & 0x03;
  693. switch (x) {
  694. case 0: x = 1; break; // 44.1
  695. case 2: x = 0; break; // 48.0
  696. default: x = 0; break; // illegal.
  697. }
  698. change |= snd_ac97_update_bits_nolock(ac97, AC97_CSR_SPDIF, 0x3fff, ((val & 0xcfff) | (x << 12)));
  699. } else if (ac97->flags & AC97_CX_SPDIF) {
  700. int v;
  701. v = new & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT) ? 0 : AC97_CXR_COPYRGT;
  702. v |= new & IEC958_AES0_NONAUDIO ? AC97_CXR_SPDIF_AC3 : AC97_CXR_SPDIF_PCM;
  703. change |= snd_ac97_update_bits_nolock(ac97, AC97_CXR_AUDIO_MISC,
  704. AC97_CXR_SPDIF_MASK | AC97_CXR_COPYRGT,
  705. v);
  706. } else {
  707. unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
  708. snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
  709. change |= snd_ac97_update_bits_nolock(ac97, AC97_SPDIF, 0x3fff, val);
  710. if (extst & AC97_EA_SPDIF) {
  711. snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
  712. }
  713. }
  714. mutex_unlock(&ac97->reg_mutex);
  715. return change;
  716. }
  717. static int snd_ac97_put_spsa(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  718. {
  719. struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
  720. int reg = kcontrol->private_value & 0xff;
  721. int shift = (kcontrol->private_value >> 8) & 0xff;
  722. int mask = (kcontrol->private_value >> 16) & 0xff;
  723. // int invert = (kcontrol->private_value >> 24) & 0xff;
  724. unsigned short value, old, new;
  725. int change;
  726. value = (ucontrol->value.integer.value[0] & mask);
  727. mutex_lock(&ac97->reg_mutex);
  728. mask <<= shift;
  729. value <<= shift;
  730. old = snd_ac97_read_cache(ac97, reg);
  731. new = (old & ~mask) | value;
  732. change = old != new;
  733. if (change) {
  734. unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
  735. snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
  736. change = snd_ac97_update_bits_nolock(ac97, reg, mask, value);
  737. if (extst & AC97_EA_SPDIF)
  738. snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
  739. }
  740. mutex_unlock(&ac97->reg_mutex);
  741. return change;
  742. }
  743. static const struct snd_kcontrol_new snd_ac97_controls_spdif[5] = {
  744. {
  745. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  746. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  747. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  748. .info = snd_ac97_spdif_mask_info,
  749. .get = snd_ac97_spdif_cmask_get,
  750. },
  751. {
  752. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  753. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  754. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  755. .info = snd_ac97_spdif_mask_info,
  756. .get = snd_ac97_spdif_pmask_get,
  757. },
  758. {
  759. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  760. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  761. .info = snd_ac97_spdif_mask_info,
  762. .get = snd_ac97_spdif_default_get,
  763. .put = snd_ac97_spdif_default_put,
  764. },
  765. AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),AC97_EXTENDED_STATUS, 2, 1, 0),
  766. {
  767. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  768. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "AC97-SPSA",
  769. .info = snd_ac97_info_volsw,
  770. .get = snd_ac97_get_volsw,
  771. .put = snd_ac97_put_spsa,
  772. .private_value = AC97_SINGLE_VALUE(AC97_EXTENDED_STATUS, 4, 3, 0)
  773. },
  774. };
  775. #define AD18XX_PCM_BITS(xname, codec, lshift, rshift, mask) \
  776. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_bits, \
  777. .get = snd_ac97_ad18xx_pcm_get_bits, .put = snd_ac97_ad18xx_pcm_put_bits, \
  778. .private_value = (codec) | ((lshift) << 8) | ((rshift) << 12) | ((mask) << 16) }
  779. static int snd_ac97_ad18xx_pcm_info_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  780. {
  781. struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
  782. int mask = (kcontrol->private_value >> 16) & 0x0f;
  783. int lshift = (kcontrol->private_value >> 8) & 0x0f;
  784. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  785. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  786. if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
  787. uinfo->count = 2;
  788. else
  789. uinfo->count = 1;
  790. uinfo->value.integer.min = 0;
  791. uinfo->value.integer.max = mask;
  792. return 0;
  793. }
  794. static int snd_ac97_ad18xx_pcm_get_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  795. {
  796. struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
  797. int codec = kcontrol->private_value & 3;
  798. int lshift = (kcontrol->private_value >> 8) & 0x0f;
  799. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  800. int mask = (kcontrol->private_value >> 16) & 0xff;
  801. ucontrol->value.integer.value[0] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> lshift) & mask);
  802. if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
  803. ucontrol->value.integer.value[1] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> rshift) & mask);
  804. return 0;
  805. }
  806. static int snd_ac97_ad18xx_pcm_put_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  807. {
  808. struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
  809. int codec = kcontrol->private_value & 3;
  810. int lshift = (kcontrol->private_value >> 8) & 0x0f;
  811. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  812. int mask = (kcontrol->private_value >> 16) & 0xff;
  813. unsigned short val, valmask;
  814. val = (mask - (ucontrol->value.integer.value[0] & mask)) << lshift;
  815. valmask = mask << lshift;
  816. if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES)) {
  817. val |= (mask - (ucontrol->value.integer.value[1] & mask)) << rshift;
  818. valmask |= mask << rshift;
  819. }
  820. return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, valmask, val);
  821. }
  822. #define AD18XX_PCM_VOLUME(xname, codec) \
  823. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_volume, \
  824. .get = snd_ac97_ad18xx_pcm_get_volume, .put = snd_ac97_ad18xx_pcm_put_volume, \
  825. .private_value = codec }
  826. static int snd_ac97_ad18xx_pcm_info_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  827. {
  828. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  829. uinfo->count = 2;
  830. uinfo->value.integer.min = 0;
  831. uinfo->value.integer.max = 31;
  832. return 0;
  833. }
  834. static int snd_ac97_ad18xx_pcm_get_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  835. {
  836. struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
  837. int codec = kcontrol->private_value & 3;
  838. mutex_lock(&ac97->page_mutex);
  839. ucontrol->value.integer.value[0] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 0) & 31);
  840. ucontrol->value.integer.value[1] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 8) & 31);
  841. mutex_unlock(&ac97->page_mutex);
  842. return 0;
  843. }
  844. static int snd_ac97_ad18xx_pcm_put_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  845. {
  846. struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
  847. int codec = kcontrol->private_value & 3;
  848. unsigned short val1, val2;
  849. val1 = 31 - (ucontrol->value.integer.value[0] & 31);
  850. val2 = 31 - (ucontrol->value.integer.value[1] & 31);
  851. return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, 0x1f1f, (val1 << 8) | val2);
  852. }
  853. static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_pcm[2] = {
  854. AD18XX_PCM_BITS("PCM Playback Switch", 0, 15, 7, 1),
  855. AD18XX_PCM_VOLUME("PCM Playback Volume", 0)
  856. };
  857. static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_surround[2] = {
  858. AD18XX_PCM_BITS("Surround Playback Switch", 1, 15, 7, 1),
  859. AD18XX_PCM_VOLUME("Surround Playback Volume", 1)
  860. };
  861. static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_center[2] = {
  862. AD18XX_PCM_BITS("Center Playback Switch", 2, 15, 15, 1),
  863. AD18XX_PCM_BITS("Center Playback Volume", 2, 8, 8, 31)
  864. };
  865. static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_lfe[2] = {
  866. AD18XX_PCM_BITS("LFE Playback Switch", 2, 7, 7, 1),
  867. AD18XX_PCM_BITS("LFE Playback Volume", 2, 0, 0, 31)
  868. };
  869. /*
  870. *
  871. */
  872. static void snd_ac97_powerdown(struct snd_ac97 *ac97);
  873. static int snd_ac97_bus_free(struct snd_ac97_bus *bus)
  874. {
  875. if (bus) {
  876. snd_ac97_bus_proc_done(bus);
  877. kfree(bus->pcms);
  878. if (bus->private_free)
  879. bus->private_free(bus);
  880. kfree(bus);
  881. }
  882. return 0;
  883. }
  884. static int snd_ac97_bus_dev_free(struct snd_device *device)
  885. {
  886. struct snd_ac97_bus *bus = device->device_data;
  887. return snd_ac97_bus_free(bus);
  888. }
  889. static int snd_ac97_free(struct snd_ac97 *ac97)
  890. {
  891. if (ac97) {
  892. #ifdef CONFIG_SND_AC97_POWER_SAVE
  893. cancel_delayed_work(&ac97->power_work);
  894. flush_scheduled_work();
  895. #endif
  896. snd_ac97_proc_done(ac97);
  897. if (ac97->bus)
  898. ac97->bus->codec[ac97->num] = NULL;
  899. if (ac97->private_free)
  900. ac97->private_free(ac97);
  901. kfree(ac97);
  902. }
  903. return 0;
  904. }
  905. static int snd_ac97_dev_free(struct snd_device *device)
  906. {
  907. struct snd_ac97 *ac97 = device->device_data;
  908. snd_ac97_powerdown(ac97); /* for avoiding click noises during shut down */
  909. return snd_ac97_free(ac97);
  910. }
  911. static int snd_ac97_try_volume_mix(struct snd_ac97 * ac97, int reg)
  912. {
  913. unsigned short val, mask = 0x8000;
  914. if (! snd_ac97_valid_reg(ac97, reg))
  915. return 0;
  916. switch (reg) {
  917. case AC97_MASTER_TONE:
  918. return ac97->caps & 0x04 ? 1 : 0;
  919. case AC97_HEADPHONE:
  920. return ac97->caps & 0x10 ? 1 : 0;
  921. case AC97_REC_GAIN_MIC:
  922. return ac97->caps & 0x01 ? 1 : 0;
  923. case AC97_3D_CONTROL:
  924. if (ac97->caps & 0x7c00) {
  925. val = snd_ac97_read(ac97, reg);
  926. /* if nonzero - fixed and we can't set it */
  927. return val == 0;
  928. }
  929. return 0;
  930. case AC97_CENTER_LFE_MASTER: /* center */
  931. if ((ac97->ext_id & AC97_EI_CDAC) == 0)
  932. return 0;
  933. break;
  934. case AC97_CENTER_LFE_MASTER+1: /* lfe */
  935. if ((ac97->ext_id & AC97_EI_LDAC) == 0)
  936. return 0;
  937. reg = AC97_CENTER_LFE_MASTER;
  938. mask = 0x0080;
  939. break;
  940. case AC97_SURROUND_MASTER:
  941. if ((ac97->ext_id & AC97_EI_SDAC) == 0)
  942. return 0;
  943. break;
  944. }
  945. val = snd_ac97_read(ac97, reg);
  946. if (!(val & mask)) {
  947. /* nothing seems to be here - mute flag is not set */
  948. /* try another test */
  949. snd_ac97_write_cache(ac97, reg, val | mask);
  950. val = snd_ac97_read(ac97, reg);
  951. val = snd_ac97_read(ac97, reg);
  952. if (!(val & mask))
  953. return 0; /* nothing here */
  954. }
  955. return 1; /* success, useable */
  956. }
  957. static void check_volume_resolution(struct snd_ac97 *ac97, int reg, unsigned char *lo_max, unsigned char *hi_max)
  958. {
  959. unsigned short cbit[3] = { 0x20, 0x10, 0x01 };
  960. unsigned char max[3] = { 63, 31, 15 };
  961. int i;
  962. /* first look up the static resolution table */
  963. if (ac97->res_table) {
  964. const struct snd_ac97_res_table *tbl;
  965. for (tbl = ac97->res_table; tbl->reg; tbl++) {
  966. if (tbl->reg == reg) {
  967. *lo_max = tbl->bits & 0xff;
  968. *hi_max = (tbl->bits >> 8) & 0xff;
  969. return;
  970. }
  971. }
  972. }
  973. *lo_max = *hi_max = 0;
  974. for (i = 0 ; i < ARRAY_SIZE(cbit); i++) {
  975. unsigned short val;
  976. snd_ac97_write(ac97, reg, 0x8080 | cbit[i] | (cbit[i] << 8));
  977. /* Do the read twice due to buffers on some ac97 codecs.
  978. * e.g. The STAC9704 returns exactly what you wrote to the register
  979. * if you read it immediately. This causes the detect routine to fail.
  980. */
  981. val = snd_ac97_read(ac97, reg);
  982. val = snd_ac97_read(ac97, reg);
  983. if (! *lo_max && (val & 0x7f) == cbit[i])
  984. *lo_max = max[i];
  985. if (! *hi_max && ((val >> 8) & 0x7f) == cbit[i])
  986. *hi_max = max[i];
  987. if (*lo_max && *hi_max)
  988. break;
  989. }
  990. }
  991. static int snd_ac97_try_bit(struct snd_ac97 * ac97, int reg, int bit)
  992. {
  993. unsigned short mask, val, orig, res;
  994. mask = 1 << bit;
  995. orig = snd_ac97_read(ac97, reg);
  996. val = orig ^ mask;
  997. snd_ac97_write(ac97, reg, val);
  998. res = snd_ac97_read(ac97, reg);
  999. snd_ac97_write_cache(ac97, reg, orig);
  1000. return res == val;
  1001. }
  1002. /* check the volume resolution of center/lfe */
  1003. static void snd_ac97_change_volume_params2(struct snd_ac97 * ac97, int reg, int shift, unsigned char *max)
  1004. {
  1005. unsigned short val, val1;
  1006. *max = 63;
  1007. val = 0x8080 | (0x20 << shift);
  1008. snd_ac97_write(ac97, reg, val);
  1009. val1 = snd_ac97_read(ac97, reg);
  1010. if (val != val1) {
  1011. *max = 31;
  1012. }
  1013. /* reset volume to zero */
  1014. snd_ac97_write_cache(ac97, reg, 0x8080);
  1015. }
  1016. static inline int printable(unsigned int x)
  1017. {
  1018. x &= 0xff;
  1019. if (x < ' ' || x >= 0x71) {
  1020. if (x <= 0x89)
  1021. return x - 0x71 + 'A';
  1022. return '?';
  1023. }
  1024. return x;
  1025. }
  1026. static struct snd_kcontrol *snd_ac97_cnew(const struct snd_kcontrol_new *_template,
  1027. struct snd_ac97 * ac97)
  1028. {
  1029. struct snd_kcontrol_new template;
  1030. memcpy(&template, _template, sizeof(template));
  1031. template.index = ac97->num;
  1032. return snd_ctl_new1(&template, ac97);
  1033. }
  1034. /*
  1035. * create mute switch(es) for normal stereo controls
  1036. */
  1037. static int snd_ac97_cmute_new_stereo(struct snd_card *card, char *name, int reg,
  1038. int check_stereo, int check_amix,
  1039. struct snd_ac97 *ac97)
  1040. {
  1041. struct snd_kcontrol *kctl;
  1042. int err;
  1043. unsigned short val, val1, mute_mask;
  1044. if (! snd_ac97_valid_reg(ac97, reg))
  1045. return 0;
  1046. mute_mask = 0x8000;
  1047. val = snd_ac97_read(ac97, reg);
  1048. if (check_stereo || (ac97->flags & AC97_STEREO_MUTES)) {
  1049. /* check whether both mute bits work */
  1050. val1 = val | 0x8080;
  1051. snd_ac97_write(ac97, reg, val1);
  1052. if (val1 == snd_ac97_read(ac97, reg))
  1053. mute_mask = 0x8080;
  1054. }
  1055. if (mute_mask == 0x8080) {
  1056. struct snd_kcontrol_new tmp = AC97_DOUBLE(name, reg, 15, 7, 1, 1);
  1057. if (check_amix)
  1058. tmp.private_value |= (1 << 30);
  1059. tmp.index = ac97->num;
  1060. kctl = snd_ctl_new1(&tmp, ac97);
  1061. } else {
  1062. struct snd_kcontrol_new tmp = AC97_SINGLE(name, reg, 15, 1, 1);
  1063. if (check_amix)
  1064. tmp.private_value |= (1 << 30);
  1065. tmp.index = ac97->num;
  1066. kctl = snd_ctl_new1(&tmp, ac97);
  1067. }
  1068. err = snd_ctl_add(card, kctl);
  1069. if (err < 0)
  1070. return err;
  1071. /* mute as default */
  1072. snd_ac97_write_cache(ac97, reg, val | mute_mask);
  1073. return 0;
  1074. }
  1075. /*
  1076. * set dB information
  1077. */
  1078. static const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0);
  1079. static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
  1080. static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
  1081. static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
  1082. static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
  1083. static const unsigned int *find_db_scale(unsigned int maxval)
  1084. {
  1085. switch (maxval) {
  1086. case 0x0f: return db_scale_4bit;
  1087. case 0x1f: return db_scale_5bit;
  1088. case 0x3f: return db_scale_6bit;
  1089. }
  1090. return NULL;
  1091. }
  1092. static void set_tlv_db_scale(struct snd_kcontrol *kctl, const unsigned int *tlv)
  1093. {
  1094. kctl->tlv.p = tlv;
  1095. if (tlv)
  1096. kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  1097. }
  1098. /*
  1099. * create a volume for normal stereo/mono controls
  1100. */
  1101. static int snd_ac97_cvol_new(struct snd_card *card, char *name, int reg, unsigned int lo_max,
  1102. unsigned int hi_max, struct snd_ac97 *ac97)
  1103. {
  1104. int err;
  1105. struct snd_kcontrol *kctl;
  1106. if (! snd_ac97_valid_reg(ac97, reg))
  1107. return 0;
  1108. if (hi_max) {
  1109. /* invert */
  1110. struct snd_kcontrol_new tmp = AC97_DOUBLE(name, reg, 8, 0, lo_max, 1);
  1111. tmp.index = ac97->num;
  1112. kctl = snd_ctl_new1(&tmp, ac97);
  1113. } else {
  1114. /* invert */
  1115. struct snd_kcontrol_new tmp = AC97_SINGLE(name, reg, 0, lo_max, 1);
  1116. tmp.index = ac97->num;
  1117. kctl = snd_ctl_new1(&tmp, ac97);
  1118. }
  1119. if (reg >= AC97_PHONE && reg <= AC97_PCM)
  1120. set_tlv_db_scale(kctl, db_scale_5bit_12db_max);
  1121. else
  1122. set_tlv_db_scale(kctl, find_db_scale(lo_max));
  1123. err = snd_ctl_add(card, kctl);
  1124. if (err < 0)
  1125. return err;
  1126. snd_ac97_write_cache(ac97, reg,
  1127. (snd_ac97_read(ac97, reg) & 0x8080) |
  1128. lo_max | (hi_max << 8));
  1129. return 0;
  1130. }
  1131. /*
  1132. * create a mute-switch and a volume for normal stereo/mono controls
  1133. */
  1134. static int snd_ac97_cmix_new_stereo(struct snd_card *card, const char *pfx,
  1135. int reg, int check_stereo, int check_amix,
  1136. struct snd_ac97 *ac97)
  1137. {
  1138. int err;
  1139. char name[44];
  1140. unsigned char lo_max, hi_max;
  1141. if (! snd_ac97_valid_reg(ac97, reg))
  1142. return 0;
  1143. if (snd_ac97_try_bit(ac97, reg, 15)) {
  1144. sprintf(name, "%s Switch", pfx);
  1145. if ((err = snd_ac97_cmute_new_stereo(card, name, reg,
  1146. check_stereo, check_amix,
  1147. ac97)) < 0)
  1148. return err;
  1149. }
  1150. check_volume_resolution(ac97, reg, &lo_max, &hi_max);
  1151. if (lo_max) {
  1152. sprintf(name, "%s Volume", pfx);
  1153. if ((err = snd_ac97_cvol_new(card, name, reg, lo_max, hi_max, ac97)) < 0)
  1154. return err;
  1155. }
  1156. return 0;
  1157. }
  1158. #define snd_ac97_cmix_new(card, pfx, reg, acheck, ac97) \
  1159. snd_ac97_cmix_new_stereo(card, pfx, reg, 0, acheck, ac97)
  1160. #define snd_ac97_cmute_new(card, name, reg, acheck, ac97) \
  1161. snd_ac97_cmute_new_stereo(card, name, reg, 0, acheck, ac97)
  1162. static unsigned int snd_ac97_determine_spdif_rates(struct snd_ac97 *ac97);
  1163. static int snd_ac97_mixer_build(struct snd_ac97 * ac97)
  1164. {
  1165. struct snd_card *card = ac97->bus->card;
  1166. struct snd_kcontrol *kctl;
  1167. int err;
  1168. unsigned int idx;
  1169. unsigned char max;
  1170. /* build master controls */
  1171. /* AD claims to remove this control from AD1887, although spec v2.2 does not allow this */
  1172. if (snd_ac97_try_volume_mix(ac97, AC97_MASTER)) {
  1173. if (ac97->flags & AC97_HAS_NO_MASTER_VOL)
  1174. err = snd_ac97_cmute_new(card, "Master Playback Switch",
  1175. AC97_MASTER, 0, ac97);
  1176. else
  1177. err = snd_ac97_cmix_new(card, "Master Playback",
  1178. AC97_MASTER, 0, ac97);
  1179. if (err < 0)
  1180. return err;
  1181. }
  1182. ac97->regs[AC97_CENTER_LFE_MASTER] = 0x8080;
  1183. /* build center controls */
  1184. if ((snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER))
  1185. && !(ac97->flags & AC97_AD_MULTI)) {
  1186. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_center[0], ac97))) < 0)
  1187. return err;
  1188. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_center[1], ac97))) < 0)
  1189. return err;
  1190. snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 0, &max);
  1191. kctl->private_value &= ~(0xff << 16);
  1192. kctl->private_value |= (int)max << 16;
  1193. set_tlv_db_scale(kctl, find_db_scale(max));
  1194. snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max);
  1195. }
  1196. /* build LFE controls */
  1197. if ((snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER+1))
  1198. && !(ac97->flags & AC97_AD_MULTI)) {
  1199. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_lfe[0], ac97))) < 0)
  1200. return err;
  1201. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_lfe[1], ac97))) < 0)
  1202. return err;
  1203. snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 8, &max);
  1204. kctl->private_value &= ~(0xff << 16);
  1205. kctl->private_value |= (int)max << 16;
  1206. set_tlv_db_scale(kctl, find_db_scale(max));
  1207. snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max << 8);
  1208. }
  1209. /* build surround controls */
  1210. if ((snd_ac97_try_volume_mix(ac97, AC97_SURROUND_MASTER))
  1211. && !(ac97->flags & AC97_AD_MULTI)) {
  1212. /* Surround Master (0x38) is with stereo mutes */
  1213. if ((err = snd_ac97_cmix_new_stereo(card, "Surround Playback",
  1214. AC97_SURROUND_MASTER, 1, 0,
  1215. ac97)) < 0)
  1216. return err;
  1217. }
  1218. /* build headphone controls */
  1219. if (snd_ac97_try_volume_mix(ac97, AC97_HEADPHONE)) {
  1220. if ((err = snd_ac97_cmix_new(card, "Headphone Playback",
  1221. AC97_HEADPHONE, 0, ac97)) < 0)
  1222. return err;
  1223. }
  1224. /* build master mono controls */
  1225. if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_MONO)) {
  1226. if ((err = snd_ac97_cmix_new(card, "Master Mono Playback",
  1227. AC97_MASTER_MONO, 0, ac97)) < 0)
  1228. return err;
  1229. }
  1230. /* build master tone controls */
  1231. if (!(ac97->flags & AC97_HAS_NO_TONE)) {
  1232. if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_TONE)) {
  1233. for (idx = 0; idx < 2; idx++) {
  1234. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_tone[idx], ac97))) < 0)
  1235. return err;
  1236. if (ac97->id == AC97_ID_YMF753) {
  1237. kctl->private_value &= ~(0xff << 16);
  1238. kctl->private_value |= 7 << 16;
  1239. }
  1240. }
  1241. snd_ac97_write_cache(ac97, AC97_MASTER_TONE, 0x0f0f);
  1242. }
  1243. }
  1244. /* build PC Speaker controls */
  1245. if (!(ac97->flags & AC97_HAS_NO_PC_BEEP) &&
  1246. ((ac97->flags & AC97_HAS_PC_BEEP) ||
  1247. snd_ac97_try_volume_mix(ac97, AC97_PC_BEEP))) {
  1248. for (idx = 0; idx < 2; idx++)
  1249. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_pc_beep[idx], ac97))) < 0)
  1250. return err;
  1251. set_tlv_db_scale(kctl, db_scale_4bit);
  1252. snd_ac97_write_cache(ac97, AC97_PC_BEEP,
  1253. snd_ac97_read(ac97, AC97_PC_BEEP) | 0x801e);
  1254. }
  1255. /* build Phone controls */
  1256. if (!(ac97->flags & AC97_HAS_NO_PHONE)) {
  1257. if (snd_ac97_try_volume_mix(ac97, AC97_PHONE)) {
  1258. if ((err = snd_ac97_cmix_new(card, "Phone Playback",
  1259. AC97_PHONE, 1, ac97)) < 0)
  1260. return err;
  1261. }
  1262. }
  1263. /* build MIC controls */
  1264. if (!(ac97->flags & AC97_HAS_NO_MIC)) {
  1265. if (snd_ac97_try_volume_mix(ac97, AC97_MIC)) {
  1266. if ((err = snd_ac97_cmix_new(card, "Mic Playback",
  1267. AC97_MIC, 1, ac97)) < 0)
  1268. return err;
  1269. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_mic_boost, ac97))) < 0)
  1270. return err;
  1271. }
  1272. }
  1273. /* build Line controls */
  1274. if (snd_ac97_try_volume_mix(ac97, AC97_LINE)) {
  1275. if ((err = snd_ac97_cmix_new(card, "Line Playback",
  1276. AC97_LINE, 1, ac97)) < 0)
  1277. return err;
  1278. }
  1279. /* build CD controls */
  1280. if (!(ac97->flags & AC97_HAS_NO_CD)) {
  1281. if (snd_ac97_try_volume_mix(ac97, AC97_CD)) {
  1282. if ((err = snd_ac97_cmix_new(card, "CD Playback",
  1283. AC97_CD, 1, ac97)) < 0)
  1284. return err;
  1285. }
  1286. }
  1287. /* build Video controls */
  1288. if (!(ac97->flags & AC97_HAS_NO_VIDEO)) {
  1289. if (snd_ac97_try_volume_mix(ac97, AC97_VIDEO)) {
  1290. if ((err = snd_ac97_cmix_new(card, "Video Playback",
  1291. AC97_VIDEO, 1, ac97)) < 0)
  1292. return err;
  1293. }
  1294. }
  1295. /* build Aux controls */
  1296. if (!(ac97->flags & AC97_HAS_NO_AUX)) {
  1297. if (snd_ac97_try_volume_mix(ac97, AC97_AUX)) {
  1298. if ((err = snd_ac97_cmix_new(card, "Aux Playback",
  1299. AC97_AUX, 1, ac97)) < 0)
  1300. return err;
  1301. }
  1302. }
  1303. /* build PCM controls */
  1304. if (ac97->flags & AC97_AD_MULTI) {
  1305. unsigned short init_val;
  1306. if (ac97->flags & AC97_STEREO_MUTES)
  1307. init_val = 0x9f9f;
  1308. else
  1309. init_val = 0x9f1f;
  1310. for (idx = 0; idx < 2; idx++)
  1311. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_pcm[idx], ac97))) < 0)
  1312. return err;
  1313. set_tlv_db_scale(kctl, db_scale_5bit);
  1314. ac97->spec.ad18xx.pcmreg[0] = init_val;
  1315. if (ac97->scaps & AC97_SCAP_SURROUND_DAC) {
  1316. for (idx = 0; idx < 2; idx++)
  1317. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_surround[idx], ac97))) < 0)
  1318. return err;
  1319. set_tlv_db_scale(kctl, db_scale_5bit);
  1320. ac97->spec.ad18xx.pcmreg[1] = init_val;
  1321. }
  1322. if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC) {
  1323. for (idx = 0; idx < 2; idx++)
  1324. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_center[idx], ac97))) < 0)
  1325. return err;
  1326. set_tlv_db_scale(kctl, db_scale_5bit);
  1327. for (idx = 0; idx < 2; idx++)
  1328. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_lfe[idx], ac97))) < 0)
  1329. return err;
  1330. set_tlv_db_scale(kctl, db_scale_5bit);
  1331. ac97->spec.ad18xx.pcmreg[2] = init_val;
  1332. }
  1333. snd_ac97_write_cache(ac97, AC97_PCM, init_val);
  1334. } else {
  1335. if (!(ac97->flags & AC97_HAS_NO_STD_PCM)) {
  1336. if (ac97->flags & AC97_HAS_NO_PCM_VOL)
  1337. err = snd_ac97_cmute_new(card,
  1338. "PCM Playback Switch",
  1339. AC97_PCM, 0, ac97);
  1340. else
  1341. err = snd_ac97_cmix_new(card, "PCM Playback",
  1342. AC97_PCM, 0, ac97);
  1343. if (err < 0)
  1344. return err;
  1345. }
  1346. }
  1347. /* build Capture controls */
  1348. if (!(ac97->flags & AC97_HAS_NO_REC_GAIN)) {
  1349. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_control_capture_src, ac97))) < 0)
  1350. return err;
  1351. if (snd_ac97_try_bit(ac97, AC97_REC_GAIN, 15)) {
  1352. err = snd_ac97_cmute_new(card, "Capture Switch",
  1353. AC97_REC_GAIN, 0, ac97);
  1354. if (err < 0)
  1355. return err;
  1356. }
  1357. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_control_capture_vol, ac97))) < 0)
  1358. return err;
  1359. set_tlv_db_scale(kctl, db_scale_rec_gain);
  1360. snd_ac97_write_cache(ac97, AC97_REC_SEL, 0x0000);
  1361. snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x0000);
  1362. }
  1363. /* build MIC Capture controls */
  1364. if (snd_ac97_try_volume_mix(ac97, AC97_REC_GAIN_MIC)) {
  1365. for (idx = 0; idx < 2; idx++)
  1366. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_mic_capture[idx], ac97))) < 0)
  1367. return err;
  1368. set_tlv_db_scale(kctl, db_scale_rec_gain);
  1369. snd_ac97_write_cache(ac97, AC97_REC_GAIN_MIC, 0x0000);
  1370. }
  1371. /* build PCM out path & mute control */
  1372. if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 15)) {
  1373. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_PCM_OUT], ac97))) < 0)
  1374. return err;
  1375. }
  1376. /* build Simulated Stereo Enhancement control */
  1377. if (ac97->caps & 0x0008) {
  1378. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_STEREO_ENHANCEMENT], ac97))) < 0)
  1379. return err;
  1380. }
  1381. /* build 3D Stereo Enhancement control */
  1382. if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 13)) {
  1383. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_3D], ac97))) < 0)
  1384. return err;
  1385. }
  1386. /* build Loudness control */
  1387. if (ac97->caps & 0x0020) {
  1388. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOUDNESS], ac97))) < 0)
  1389. return err;
  1390. }
  1391. /* build Mono output select control */
  1392. if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 9)) {
  1393. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MONO], ac97))) < 0)
  1394. return err;
  1395. }
  1396. /* build Mic select control */
  1397. if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 8)) {
  1398. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MIC], ac97))) < 0)
  1399. return err;
  1400. }
  1401. /* build ADC/DAC loopback control */
  1402. if (enable_loopback && snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 7)) {
  1403. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOOPBACK], ac97))) < 0)
  1404. return err;
  1405. }
  1406. snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, ~AC97_GP_DRSS_MASK, 0x0000);
  1407. /* build 3D controls */
  1408. if (ac97->build_ops->build_3d) {
  1409. ac97->build_ops->build_3d(ac97);
  1410. } else {
  1411. if (snd_ac97_try_volume_mix(ac97, AC97_3D_CONTROL)) {
  1412. unsigned short val;
  1413. val = 0x0707;
  1414. snd_ac97_write(ac97, AC97_3D_CONTROL, val);
  1415. val = snd_ac97_read(ac97, AC97_3D_CONTROL);
  1416. val = val == 0x0606;
  1417. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
  1418. return err;
  1419. if (val)
  1420. kctl->private_value = AC97_3D_CONTROL | (9 << 8) | (7 << 16);
  1421. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[1], ac97))) < 0)
  1422. return err;
  1423. if (val)
  1424. kctl->private_value = AC97_3D_CONTROL | (1 << 8) | (7 << 16);
  1425. snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
  1426. }
  1427. }
  1428. /* build S/PDIF controls */
  1429. /* Hack for ASUS P5P800-VM, which does not indicate S/PDIF capability */
  1430. if (ac97->subsystem_vendor == 0x1043 &&
  1431. ac97->subsystem_device == 0x810f)
  1432. ac97->ext_id |= AC97_EI_SPDIF;
  1433. if ((ac97->ext_id & AC97_EI_SPDIF) && !(ac97->scaps & AC97_SCAP_NO_SPDIF)) {
  1434. if (ac97->build_ops->build_spdif) {
  1435. if ((err = ac97->build_ops->build_spdif(ac97)) < 0)
  1436. return err;
  1437. } else {
  1438. for (idx = 0; idx < 5; idx++)
  1439. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_spdif[idx], ac97))) < 0)
  1440. return err;
  1441. if (ac97->build_ops->build_post_spdif) {
  1442. if ((err = ac97->build_ops->build_post_spdif(ac97)) < 0)
  1443. return err;
  1444. }
  1445. /* set default PCM S/PDIF params */
  1446. /* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */
  1447. snd_ac97_write_cache(ac97, AC97_SPDIF, 0x2a20);
  1448. ac97->rates[AC97_RATES_SPDIF] = snd_ac97_determine_spdif_rates(ac97);
  1449. }
  1450. ac97->spdif_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
  1451. }
  1452. /* build chip specific controls */
  1453. if (ac97->build_ops->build_specific)
  1454. if ((err = ac97->build_ops->build_specific(ac97)) < 0)
  1455. return err;
  1456. if (snd_ac97_try_bit(ac97, AC97_POWERDOWN, 15)) {
  1457. kctl = snd_ac97_cnew(&snd_ac97_control_eapd, ac97);
  1458. if (! kctl)
  1459. return -ENOMEM;
  1460. if (ac97->scaps & AC97_SCAP_INV_EAPD)
  1461. set_inv_eapd(ac97, kctl);
  1462. if ((err = snd_ctl_add(card, kctl)) < 0)
  1463. return err;
  1464. }
  1465. return 0;
  1466. }
  1467. static int snd_ac97_modem_build(struct snd_card *card, struct snd_ac97 * ac97)
  1468. {
  1469. int err, idx;
  1470. //printk("AC97_GPIO_CFG = %x\n",snd_ac97_read(ac97,AC97_GPIO_CFG));
  1471. snd_ac97_write(ac97, AC97_GPIO_CFG, 0xffff & ~(AC97_GPIO_LINE1_OH));
  1472. snd_ac97_write(ac97, AC97_GPIO_POLARITY, 0xffff & ~(AC97_GPIO_LINE1_OH));
  1473. snd_ac97_write(ac97, AC97_GPIO_STICKY, 0xffff);
  1474. snd_ac97_write(ac97, AC97_GPIO_WAKEUP, 0x0);
  1475. snd_ac97_write(ac97, AC97_MISC_AFE, 0x0);
  1476. /* build modem switches */
  1477. for (idx = 0; idx < ARRAY_SIZE(snd_ac97_controls_modem_switches); idx++)
  1478. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ac97_controls_modem_switches[idx], ac97))) < 0)
  1479. return err;
  1480. /* build chip specific controls */
  1481. if (ac97->build_ops->build_specific)
  1482. if ((err = ac97->build_ops->build_specific(ac97)) < 0)
  1483. return err;
  1484. return 0;
  1485. }
  1486. static int snd_ac97_test_rate(struct snd_ac97 *ac97, int reg, int shadow_reg, int rate)
  1487. {
  1488. unsigned short val;
  1489. unsigned int tmp;
  1490. tmp = ((unsigned int)rate * ac97->bus->clock) / 48000;
  1491. snd_ac97_write_cache(ac97, reg, tmp & 0xffff);
  1492. if (shadow_reg)
  1493. snd_ac97_write_cache(ac97, shadow_reg, tmp & 0xffff);
  1494. val = snd_ac97_read(ac97, reg);
  1495. return val == (tmp & 0xffff);
  1496. }
  1497. static void snd_ac97_determine_rates(struct snd_ac97 *ac97, int reg, int shadow_reg, unsigned int *r_result)
  1498. {
  1499. unsigned int result = 0;
  1500. unsigned short saved;
  1501. if (ac97->bus->no_vra) {
  1502. *r_result = SNDRV_PCM_RATE_48000;
  1503. if ((ac97->flags & AC97_DOUBLE_RATE) &&
  1504. reg == AC97_PCM_FRONT_DAC_RATE)
  1505. *r_result |= SNDRV_PCM_RATE_96000;
  1506. return;
  1507. }
  1508. saved = snd_ac97_read(ac97, reg);
  1509. if ((ac97->ext_id & AC97_EI_DRA) && reg == AC97_PCM_FRONT_DAC_RATE)
  1510. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
  1511. AC97_EA_DRA, 0);
  1512. /* test a non-standard rate */
  1513. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11000))
  1514. result |= SNDRV_PCM_RATE_CONTINUOUS;
  1515. /* let's try to obtain standard rates */
  1516. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 8000))
  1517. result |= SNDRV_PCM_RATE_8000;
  1518. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11025))
  1519. result |= SNDRV_PCM_RATE_11025;
  1520. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 16000))
  1521. result |= SNDRV_PCM_RATE_16000;
  1522. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 22050))
  1523. result |= SNDRV_PCM_RATE_22050;
  1524. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 32000))
  1525. result |= SNDRV_PCM_RATE_32000;
  1526. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 44100))
  1527. result |= SNDRV_PCM_RATE_44100;
  1528. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 48000))
  1529. result |= SNDRV_PCM_RATE_48000;
  1530. if ((ac97->flags & AC97_DOUBLE_RATE) &&
  1531. reg == AC97_PCM_FRONT_DAC_RATE) {
  1532. /* test standard double rates */
  1533. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
  1534. AC97_EA_DRA, AC97_EA_DRA);
  1535. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 64000 / 2))
  1536. result |= SNDRV_PCM_RATE_64000;
  1537. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 88200 / 2))
  1538. result |= SNDRV_PCM_RATE_88200;
  1539. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 96000 / 2))
  1540. result |= SNDRV_PCM_RATE_96000;
  1541. /* some codecs don't support variable double rates */
  1542. if (!snd_ac97_test_rate(ac97, reg, shadow_reg, 76100 / 2))
  1543. result &= ~SNDRV_PCM_RATE_CONTINUOUS;
  1544. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
  1545. AC97_EA_DRA, 0);
  1546. }
  1547. /* restore the default value */
  1548. snd_ac97_write_cache(ac97, reg, saved);
  1549. if (shadow_reg)
  1550. snd_ac97_write_cache(ac97, shadow_reg, saved);
  1551. *r_result = result;
  1552. }
  1553. /* check AC97_SPDIF register to accept which sample rates */
  1554. static unsigned int snd_ac97_determine_spdif_rates(struct snd_ac97 *ac97)
  1555. {
  1556. unsigned int result = 0;
  1557. int i;
  1558. static unsigned short ctl_bits[] = {
  1559. AC97_SC_SPSR_44K, AC97_SC_SPSR_32K, AC97_SC_SPSR_48K
  1560. };
  1561. static unsigned int rate_bits[] = {
  1562. SNDRV_PCM_RATE_44100, SNDRV_PCM_RATE_32000, SNDRV_PCM_RATE_48000
  1563. };
  1564. for (i = 0; i < (int)ARRAY_SIZE(ctl_bits); i++) {
  1565. snd_ac97_update_bits(ac97, AC97_SPDIF, AC97_SC_SPSR_MASK, ctl_bits[i]);
  1566. if ((snd_ac97_read(ac97, AC97_SPDIF) & AC97_SC_SPSR_MASK) == ctl_bits[i])
  1567. result |= rate_bits[i];
  1568. }
  1569. return result;
  1570. }
  1571. /* look for the codec id table matching with the given id */
  1572. static const struct ac97_codec_id *look_for_codec_id(const struct ac97_codec_id *table,
  1573. unsigned int id)
  1574. {
  1575. const struct ac97_codec_id *pid;
  1576. for (pid = table; pid->id; pid++)
  1577. if (pid->id == (id & pid->mask))
  1578. return pid;
  1579. return NULL;
  1580. }
  1581. void snd_ac97_get_name(struct snd_ac97 *ac97, unsigned int id, char *name, int modem)
  1582. {
  1583. const struct ac97_codec_id *pid;
  1584. sprintf(name, "0x%x %c%c%c", id,
  1585. printable(id >> 24),
  1586. printable(id >> 16),
  1587. printable(id >> 8));
  1588. pid = look_for_codec_id(snd_ac97_codec_id_vendors, id);
  1589. if (! pid)
  1590. return;
  1591. strcpy(name, pid->name);
  1592. if (ac97 && pid->patch) {
  1593. if ((modem && (pid->flags & AC97_MODEM_PATCH)) ||
  1594. (! modem && ! (pid->flags & AC97_MODEM_PATCH)))
  1595. pid->patch(ac97);
  1596. }
  1597. pid = look_for_codec_id(snd_ac97_codec_ids, id);
  1598. if (pid) {
  1599. strcat(name, " ");
  1600. strcat(name, pid->name);
  1601. if (pid->mask != 0xffffffff)
  1602. sprintf(name + strlen(name), " rev %d", id & ~pid->mask);
  1603. if (ac97 && pid->patch) {
  1604. if ((modem && (pid->flags & AC97_MODEM_PATCH)) ||
  1605. (! modem && ! (pid->flags & AC97_MODEM_PATCH)))
  1606. pid->patch(ac97);
  1607. }
  1608. } else
  1609. sprintf(name + strlen(name), " id %x", id & 0xff);
  1610. }
  1611. /**
  1612. * snd_ac97_get_short_name - retrieve codec name
  1613. * @ac97: the codec instance
  1614. *
  1615. * Returns the short identifying name of the codec.
  1616. */
  1617. const char *snd_ac97_get_short_name(struct snd_ac97 *ac97)
  1618. {
  1619. const struct ac97_codec_id *pid;
  1620. for (pid = snd_ac97_codec_ids; pid->id; pid++)
  1621. if (pid->id == (ac97->id & pid->mask))
  1622. return pid->name;
  1623. return "unknown codec";
  1624. }
  1625. EXPORT_SYMBOL(snd_ac97_get_short_name);
  1626. /* wait for a while until registers are accessible after RESET
  1627. * return 0 if ok, negative not ready
  1628. */
  1629. static int ac97_reset_wait(struct snd_ac97 *ac97, int timeout, int with_modem)
  1630. {
  1631. unsigned long end_time;
  1632. unsigned short val;
  1633. end_time = jiffies + timeout;
  1634. do {
  1635. /* use preliminary reads to settle the communication */
  1636. snd_ac97_read(ac97, AC97_RESET);
  1637. snd_ac97_read(ac97, AC97_VENDOR_ID1);
  1638. snd_ac97_read(ac97, AC97_VENDOR_ID2);
  1639. /* modem? */
  1640. if (with_modem) {
  1641. val = snd_ac97_read(ac97, AC97_EXTENDED_MID);
  1642. if (val != 0xffff && (val & 1) != 0)
  1643. return 0;
  1644. }
  1645. if (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) {
  1646. /* probably only Xbox issue - all registers are read as zero */
  1647. val = snd_ac97_read(ac97, AC97_VENDOR_ID1);
  1648. if (val != 0 && val != 0xffff)
  1649. return 0;
  1650. } else {
  1651. /* because the PCM or MASTER volume registers can be modified,
  1652. * the REC_GAIN register is used for tests
  1653. */
  1654. /* test if we can write to the record gain volume register */
  1655. snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a05);
  1656. if ((snd_ac97_read(ac97, AC97_REC_GAIN) & 0x7fff) == 0x0a05)
  1657. return 0;
  1658. }
  1659. schedule_timeout_uninterruptible(1);
  1660. } while (time_after_eq(end_time, jiffies));
  1661. return -ENODEV;
  1662. }
  1663. /**
  1664. * snd_ac97_bus - create an AC97 bus component
  1665. * @card: the card instance
  1666. * @num: the bus number
  1667. * @ops: the bus callbacks table
  1668. * @private_data: private data pointer for the new instance
  1669. * @rbus: the pointer to store the new AC97 bus instance.
  1670. *
  1671. * Creates an AC97 bus component. An struct snd_ac97_bus instance is newly
  1672. * allocated and initialized.
  1673. *
  1674. * The ops table must include valid callbacks (at least read and
  1675. * write). The other callbacks, wait and reset, are not mandatory.
  1676. *
  1677. * The clock is set to 48000. If another clock is needed, set
  1678. * (*rbus)->clock manually.
  1679. *
  1680. * The AC97 bus instance is registered as a low-level device, so you don't
  1681. * have to release it manually.
  1682. *
  1683. * Returns zero if successful, or a negative error code on failure.
  1684. */
  1685. int snd_ac97_bus(struct snd_card *card, int num, struct snd_ac97_bus_ops *ops,
  1686. void *private_data, struct snd_ac97_bus **rbus)
  1687. {
  1688. int err;
  1689. struct snd_ac97_bus *bus;
  1690. static struct snd_device_ops dev_ops = {
  1691. .dev_free = snd_ac97_bus_dev_free,
  1692. };
  1693. snd_assert(card != NULL, return -EINVAL);
  1694. snd_assert(rbus != NULL, return -EINVAL);
  1695. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  1696. if (bus == NULL)
  1697. return -ENOMEM;
  1698. bus->card = card;
  1699. bus->num = num;
  1700. bus->ops = ops;
  1701. bus->private_data = private_data;
  1702. bus->clock = 48000;
  1703. spin_lock_init(&bus->bus_lock);
  1704. snd_ac97_bus_proc_init(bus);
  1705. if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
  1706. snd_ac97_bus_free(bus);
  1707. return err;
  1708. }
  1709. *rbus = bus;
  1710. return 0;
  1711. }
  1712. EXPORT_SYMBOL(snd_ac97_bus);
  1713. /* stop no dev release warning */
  1714. static void ac97_device_release(struct device * dev)
  1715. {
  1716. }
  1717. /* register ac97 codec to bus */
  1718. static int snd_ac97_dev_register(struct snd_device *device)
  1719. {
  1720. struct snd_ac97 *ac97 = device->device_data;
  1721. int err;
  1722. ac97->dev.bus = &ac97_bus_type;
  1723. ac97->dev.parent = ac97->bus->card->dev;
  1724. ac97->dev.release = ac97_device_release;
  1725. snprintf(ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
  1726. ac97->bus->card->number, ac97->num,
  1727. snd_ac97_get_short_name(ac97));
  1728. if ((err = device_register(&ac97->dev)) < 0) {
  1729. snd_printk(KERN_ERR "Can't register ac97 bus\n");
  1730. ac97->dev.bus = NULL;
  1731. return err;
  1732. }
  1733. return 0;
  1734. }
  1735. /* disconnect ac97 codec */
  1736. static int snd_ac97_dev_disconnect(struct snd_device *device)
  1737. {
  1738. struct snd_ac97 *ac97 = device->device_data;
  1739. if (ac97->dev.bus)
  1740. device_unregister(&ac97->dev);
  1741. return 0;
  1742. }
  1743. /* build_ops to do nothing */
  1744. static struct snd_ac97_build_ops null_build_ops;
  1745. #ifdef CONFIG_SND_AC97_POWER_SAVE
  1746. static void do_update_power(struct work_struct *work)
  1747. {
  1748. update_power_regs(
  1749. container_of(work, struct snd_ac97, power_work.work));
  1750. }
  1751. #endif
  1752. /**
  1753. * snd_ac97_mixer - create an Codec97 component
  1754. * @bus: the AC97 bus which codec is attached to
  1755. * @template: the template of ac97, including index, callbacks and
  1756. * the private data.
  1757. * @rac97: the pointer to store the new ac97 instance.
  1758. *
  1759. * Creates an Codec97 component. An struct snd_ac97 instance is newly
  1760. * allocated and initialized from the template. The codec
  1761. * is then initialized by the standard procedure.
  1762. *
  1763. * The template must include the codec number (num) and address (addr),
  1764. * and the private data (private_data).
  1765. *
  1766. * The ac97 instance is registered as a low-level device, so you don't
  1767. * have to release it manually.
  1768. *
  1769. * Returns zero if successful, or a negative error code on failure.
  1770. */
  1771. int snd_ac97_mixer(struct snd_ac97_bus *bus, struct snd_ac97_template *template, struct snd_ac97 **rac97)
  1772. {
  1773. int err;
  1774. struct snd_ac97 *ac97;
  1775. struct snd_card *card;
  1776. char name[64];
  1777. unsigned long end_time;
  1778. unsigned int reg;
  1779. const struct ac97_codec_id *pid;
  1780. static struct snd_device_ops ops = {
  1781. .dev_free = snd_ac97_dev_free,
  1782. .dev_register = snd_ac97_dev_register,
  1783. .dev_disconnect = snd_ac97_dev_disconnect,
  1784. };
  1785. snd_assert(rac97 != NULL, return -EINVAL);
  1786. *rac97 = NULL;
  1787. snd_assert(bus != NULL && template != NULL, return -EINVAL);
  1788. snd_assert(template->num < 4 && bus->codec[template->num] == NULL, return -EINVAL);
  1789. card = bus->card;
  1790. ac97 = kzalloc(sizeof(*ac97), GFP_KERNEL);
  1791. if (ac97 == NULL)
  1792. return -ENOMEM;
  1793. ac97->private_data = template->private_data;
  1794. ac97->private_free = template->private_free;
  1795. ac97->bus = bus;
  1796. ac97->pci = template->pci;
  1797. ac97->num = template->num;
  1798. ac97->addr = template->addr;
  1799. ac97->scaps = template->scaps;
  1800. ac97->res_table = template->res_table;
  1801. bus->codec[ac97->num] = ac97;
  1802. mutex_init(&ac97->reg_mutex);
  1803. mutex_init(&ac97->page_mutex);
  1804. #ifdef CONFIG_SND_AC97_POWER_SAVE
  1805. INIT_DELAYED_WORK(&ac97->power_work, do_update_power);
  1806. #endif
  1807. #ifdef CONFIG_PCI
  1808. if (ac97->pci) {
  1809. pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_VENDOR_ID, &ac97->subsystem_vendor);
  1810. pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_ID, &ac97->subsystem_device);
  1811. }
  1812. #endif
  1813. if (bus->ops->reset) {
  1814. bus->ops->reset(ac97);
  1815. goto __access_ok;
  1816. }
  1817. ac97->id = snd_ac97_read(ac97, AC97_VENDOR_ID1) << 16;
  1818. ac97->id |= snd_ac97_read(ac97, AC97_VENDOR_ID2);
  1819. if (ac97->id && ac97->id != (unsigned int)-1) {
  1820. pid = look_for_codec_id(snd_ac97_codec_ids, ac97->id);
  1821. if (pid && (pid->flags & AC97_DEFAULT_POWER_OFF))
  1822. goto __access_ok;
  1823. }
  1824. /* reset to defaults */
  1825. if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO))
  1826. snd_ac97_write(ac97, AC97_RESET, 0);
  1827. if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM))
  1828. snd_ac97_write(ac97, AC97_EXTENDED_MID, 0);
  1829. if (bus->ops->wait)
  1830. bus->ops->wait(ac97);
  1831. else {
  1832. udelay(50);
  1833. if (ac97->scaps & AC97_SCAP_SKIP_AUDIO)
  1834. err = ac97_reset_wait(ac97, HZ/2, 1);
  1835. else {
  1836. err = ac97_reset_wait(ac97, HZ/2, 0);
  1837. if (err < 0)
  1838. err = ac97_reset_wait(ac97, HZ/2, 1);
  1839. }
  1840. if (err < 0) {
  1841. snd_printk(KERN_WARNING "AC'97 %d does not respond - RESET\n", ac97->num);
  1842. /* proceed anyway - it's often non-critical */
  1843. }
  1844. }
  1845. __access_ok:
  1846. ac97->id = snd_ac97_read(ac97, AC97_VENDOR_ID1) << 16;
  1847. ac97->id |= snd_ac97_read(ac97, AC97_VENDOR_ID2);
  1848. if (! (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) &&
  1849. (ac97->id == 0x00000000 || ac97->id == 0xffffffff)) {
  1850. snd_printk(KERN_ERR "AC'97 %d access is not valid [0x%x], removing mixer.\n", ac97->num, ac97->id);
  1851. snd_ac97_free(ac97);
  1852. return -EIO;
  1853. }
  1854. pid = look_for_codec_id(snd_ac97_codec_ids, ac97->id);
  1855. if (pid)
  1856. ac97->flags |= pid->flags;
  1857. /* test for AC'97 */
  1858. if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO) && !(ac97->scaps & AC97_SCAP_AUDIO)) {
  1859. /* test if we can write to the record gain volume register */
  1860. snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a06);
  1861. if (((err = snd_ac97_read(ac97, AC97_REC_GAIN)) & 0x7fff) == 0x0a06)
  1862. ac97->scaps |= AC97_SCAP_AUDIO;
  1863. }
  1864. if (ac97->scaps & AC97_SCAP_AUDIO) {
  1865. ac97->caps = snd_ac97_read(ac97, AC97_RESET);
  1866. ac97->ext_id = snd_ac97_read(ac97, AC97_EXTENDED_ID);
  1867. if (ac97->ext_id == 0xffff) /* invalid combination */
  1868. ac97->ext_id = 0;
  1869. }
  1870. /* test for MC'97 */
  1871. if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM) && !(ac97->scaps & AC97_SCAP_MODEM)) {
  1872. ac97->ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID);
  1873. if (ac97->ext_mid == 0xffff) /* invalid combination */
  1874. ac97->ext_mid = 0;
  1875. if (ac97->ext_mid & 1)
  1876. ac97->scaps |= AC97_SCAP_MODEM;
  1877. }
  1878. if (!ac97_is_audio(ac97) && !ac97_is_modem(ac97)) {
  1879. if (!(ac97->scaps & (AC97_SCAP_SKIP_AUDIO|AC97_SCAP_SKIP_MODEM)))
  1880. snd_printk(KERN_ERR "AC'97 %d access error (not audio or modem codec)\n", ac97->num);
  1881. snd_ac97_free(ac97);
  1882. return -EACCES;
  1883. }
  1884. if (bus->ops->reset) // FIXME: always skipping?
  1885. goto __ready_ok;
  1886. /* FIXME: add powerdown control */
  1887. if (ac97_is_audio(ac97)) {
  1888. /* nothing should be in powerdown mode */
  1889. snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0);
  1890. if (! (ac97->flags & AC97_DEFAULT_POWER_OFF)) {
  1891. snd_ac97_write_cache(ac97, AC97_RESET, 0); /* reset to defaults */
  1892. udelay(100);
  1893. snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0);
  1894. }
  1895. /* nothing should be in powerdown mode */
  1896. snd_ac97_write_cache(ac97, AC97_GENERAL_PURPOSE, 0);
  1897. end_time = jiffies + (HZ / 10);
  1898. do {
  1899. if ((snd_ac97_read(ac97, AC97_POWERDOWN) & 0x0f) == 0x0f)
  1900. goto __ready_ok;
  1901. schedule_timeout_uninterruptible(1);
  1902. } while (time_after_eq(end_time, jiffies));
  1903. snd_printk(KERN_WARNING "AC'97 %d analog subsections not ready\n", ac97->num);
  1904. }
  1905. /* FIXME: add powerdown control */
  1906. if (ac97_is_modem(ac97)) {
  1907. unsigned char tmp;
  1908. /* nothing should be in powerdown mode */
  1909. /* note: it's important to set the rate at first */
  1910. tmp = AC97_MEA_GPIO;
  1911. if (ac97->ext_mid & AC97_MEI_LINE1) {
  1912. snd_ac97_write_cache(ac97, AC97_LINE1_RATE, 8000);
  1913. tmp |= AC97_MEA_ADC1 | AC97_MEA_DAC1;
  1914. }
  1915. if (ac97->ext_mid & AC97_MEI_LINE2) {
  1916. snd_ac97_write_cache(ac97, AC97_LINE2_RATE, 8000);
  1917. tmp |= AC97_MEA_ADC2 | AC97_MEA_DAC2;
  1918. }
  1919. if (ac97->ext_mid & AC97_MEI_HANDSET) {
  1920. snd_ac97_write_cache(ac97, AC97_HANDSET_RATE, 8000);
  1921. tmp |= AC97_MEA_HADC | AC97_MEA_HDAC;
  1922. }
  1923. snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0);
  1924. udelay(100);
  1925. /* nothing should be in powerdown mode */
  1926. snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0);
  1927. end_time = jiffies + (HZ / 10);
  1928. do {
  1929. if ((snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS) & tmp) == tmp)
  1930. goto __ready_ok;
  1931. schedule_timeout_uninterruptible(1);
  1932. } while (time_after_eq(end_time, jiffies));
  1933. snd_printk(KERN_WARNING "MC'97 %d converters and GPIO not ready (0x%x)\n", ac97->num, snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS));
  1934. }
  1935. __ready_ok:
  1936. if (ac97_is_audio(ac97))
  1937. ac97->addr = (ac97->ext_id & AC97_EI_ADDR_MASK) >> AC97_EI_ADDR_SHIFT;
  1938. else
  1939. ac97->addr = (ac97->ext_mid & AC97_MEI_ADDR_MASK) >> AC97_MEI_ADDR_SHIFT;
  1940. if (ac97->ext_id & 0x01c9) { /* L/R, MIC, SDAC, LDAC VRA support */
  1941. reg = snd_ac97_read(ac97, AC97_EXTENDED_STATUS);
  1942. reg |= ac97->ext_id & 0x01c0; /* LDAC/SDAC/CDAC */
  1943. if (! bus->no_vra)
  1944. reg |= ac97->ext_id & 0x0009; /* VRA/VRM */
  1945. snd_ac97_write_cache(ac97, AC97_EXTENDED_STATUS, reg);
  1946. }
  1947. if ((ac97->ext_id & AC97_EI_DRA) && bus->dra) {
  1948. /* Intel controllers require double rate data to be put in
  1949. * slots 7+8, so let's hope the codec supports it. */
  1950. snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, AC97_GP_DRSS_MASK, AC97_GP_DRSS_78);
  1951. if ((snd_ac97_read(ac97, AC97_GENERAL_PURPOSE) & AC97_GP_DRSS_MASK) == AC97_GP_DRSS_78)
  1952. ac97->flags |= AC97_DOUBLE_RATE;
  1953. /* restore to slots 10/11 to avoid the confliction with surrounds */
  1954. snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, AC97_GP_DRSS_MASK, 0);
  1955. }
  1956. if (ac97->ext_id & AC97_EI_VRA) { /* VRA support */
  1957. snd_ac97_determine_rates(ac97, AC97_PCM_FRONT_DAC_RATE, 0, &ac97->rates[AC97_RATES_FRONT_DAC]);
  1958. snd_ac97_determine_rates(ac97, AC97_PCM_LR_ADC_RATE, 0, &ac97->rates[AC97_RATES_ADC]);
  1959. } else {
  1960. ac97->rates[AC97_RATES_FRONT_DAC] = SNDRV_PCM_RATE_48000;
  1961. if (ac97->flags & AC97_DOUBLE_RATE)
  1962. ac97->rates[AC97_RATES_FRONT_DAC] |= SNDRV_PCM_RATE_96000;
  1963. ac97->rates[AC97_RATES_ADC] = SNDRV_PCM_RATE_48000;
  1964. }
  1965. if (ac97->ext_id & AC97_EI_SPDIF) {
  1966. /* codec specific code (patch) should override these values */
  1967. ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_32000;
  1968. }
  1969. if (ac97->ext_id & AC97_EI_VRM) { /* MIC VRA support */
  1970. snd_ac97_determine_rates(ac97, AC97_PCM_MIC_ADC_RATE, 0, &ac97->rates[AC97_RATES_MIC_ADC]);
  1971. } else {
  1972. ac97->rates[AC97_RATES_MIC_ADC] = SNDRV_PCM_RATE_48000;
  1973. }
  1974. if (ac97->ext_id & AC97_EI_SDAC) { /* SDAC support */
  1975. snd_ac97_determine_rates(ac97, AC97_PCM_SURR_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_SURR_DAC]);
  1976. ac97->scaps |= AC97_SCAP_SURROUND_DAC;
  1977. }
  1978. if (ac97->ext_id & AC97_EI_LDAC) { /* LDAC support */
  1979. snd_ac97_determine_rates(ac97, AC97_PCM_LFE_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_LFE_DAC]);
  1980. ac97->scaps |= AC97_SCAP_CENTER_LFE_DAC;
  1981. }
  1982. /* additional initializations */
  1983. if (bus->ops->init)
  1984. bus->ops->init(ac97);
  1985. snd_ac97_get_name(ac97, ac97->id, name, !ac97_is_audio(ac97));
  1986. snd_ac97_get_name(NULL, ac97->id, name, !ac97_is_audio(ac97)); // ac97->id might be changed in the special setup code
  1987. if (! ac97->build_ops)
  1988. ac97->build_ops = &null_build_ops;
  1989. if (ac97_is_audio(ac97)) {
  1990. char comp[16];
  1991. if (card->mixername[0] == '\0') {
  1992. strcpy(card->mixername, name);
  1993. } else {
  1994. if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) {
  1995. strcat(card->mixername, ",");
  1996. strcat(card->mixername, name);
  1997. }
  1998. }
  1999. sprintf(comp, "AC97a:%08x", ac97->id);
  2000. if ((err = snd_component_add(card, comp)) < 0) {
  2001. snd_ac97_free(ac97);
  2002. return err;
  2003. }
  2004. if (snd_ac97_mixer_build(ac97) < 0) {
  2005. snd_ac97_free(ac97);
  2006. return -ENOMEM;
  2007. }
  2008. }
  2009. if (ac97_is_modem(ac97)) {
  2010. char comp[16];
  2011. if (card->mixername[0] == '\0') {
  2012. strcpy(card->mixername, name);
  2013. } else {
  2014. if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) {
  2015. strcat(card->mixername, ",");
  2016. strcat(card->mixername, name);
  2017. }
  2018. }
  2019. sprintf(comp, "AC97m:%08x", ac97->id);
  2020. if ((err = snd_component_add(card, comp)) < 0) {
  2021. snd_ac97_free(ac97);
  2022. return err;
  2023. }
  2024. if (snd_ac97_modem_build(card, ac97) < 0) {
  2025. snd_ac97_free(ac97);
  2026. return -ENOMEM;
  2027. }
  2028. }
  2029. if (ac97_is_audio(ac97))
  2030. update_power_regs(ac97);
  2031. snd_ac97_proc_init(ac97);
  2032. if ((err = snd_device_new(card, SNDRV_DEV_CODEC, ac97, &ops)) < 0) {
  2033. snd_ac97_free(ac97);
  2034. return err;
  2035. }
  2036. *rac97 = ac97;
  2037. return 0;
  2038. }
  2039. EXPORT_SYMBOL(snd_ac97_mixer);
  2040. /*
  2041. * Power down the chip.
  2042. *
  2043. * MASTER and HEADPHONE registers are muted but the register cache values
  2044. * are not changed, so that the values can be restored in snd_ac97_resume().
  2045. */
  2046. static void snd_ac97_powerdown(struct snd_ac97 *ac97)
  2047. {
  2048. unsigned short power;
  2049. if (ac97_is_audio(ac97)) {
  2050. /* some codecs have stereo mute bits */
  2051. snd_ac97_write(ac97, AC97_MASTER, 0x9f9f);
  2052. snd_ac97_write(ac97, AC97_HEADPHONE, 0x9f9f);
  2053. }
  2054. /* surround, CLFE, mic powerdown */
  2055. power = ac97->regs[AC97_EXTENDED_STATUS];
  2056. if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
  2057. power |= AC97_EA_PRJ;
  2058. if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
  2059. power |= AC97_EA_PRI | AC97_EA_PRK;
  2060. power |= AC97_EA_PRL;
  2061. snd_ac97_write(ac97, AC97_EXTENDED_STATUS, power);
  2062. /* powerdown external amplifier */
  2063. if (ac97->scaps & AC97_SCAP_INV_EAPD)
  2064. power = ac97->regs[AC97_POWERDOWN] & ~AC97_PD_EAPD;
  2065. else if (! (ac97->scaps & AC97_SCAP_EAPD_LED))
  2066. power = ac97->regs[AC97_POWERDOWN] | AC97_PD_EAPD;
  2067. power |= AC97_PD_PR6; /* Headphone amplifier powerdown */
  2068. power |= AC97_PD_PR0 | AC97_PD_PR1; /* ADC & DAC powerdown */
  2069. snd_ac97_write(ac97, AC97_POWERDOWN, power);
  2070. udelay(100);
  2071. power |= AC97_PD_PR2 | AC97_PD_PR3; /* Analog Mixer powerdown */
  2072. snd_ac97_write(ac97, AC97_POWERDOWN, power);
  2073. if (ac97_is_power_save_mode(ac97)) {
  2074. udelay(100);
  2075. /* AC-link powerdown, internal Clk disable */
  2076. /* FIXME: this may cause click noises on some boards */
  2077. power |= AC97_PD_PR4 | AC97_PD_PR5;
  2078. snd_ac97_write(ac97, AC97_POWERDOWN, power);
  2079. }
  2080. }
  2081. struct ac97_power_reg {
  2082. unsigned short reg;
  2083. unsigned short power_reg;
  2084. unsigned short mask;
  2085. };
  2086. enum { PWIDX_ADC, PWIDX_FRONT, PWIDX_CLFE, PWIDX_SURR, PWIDX_MIC, PWIDX_SIZE };
  2087. static struct ac97_power_reg power_regs[PWIDX_SIZE] = {
  2088. [PWIDX_ADC] = { AC97_PCM_LR_ADC_RATE, AC97_POWERDOWN, AC97_PD_PR0},
  2089. [PWIDX_FRONT] = { AC97_PCM_FRONT_DAC_RATE, AC97_POWERDOWN, AC97_PD_PR1},
  2090. [PWIDX_CLFE] = { AC97_PCM_LFE_DAC_RATE, AC97_EXTENDED_STATUS,
  2091. AC97_EA_PRI | AC97_EA_PRK},
  2092. [PWIDX_SURR] = { AC97_PCM_SURR_DAC_RATE, AC97_EXTENDED_STATUS,
  2093. AC97_EA_PRJ},
  2094. [PWIDX_MIC] = { AC97_PCM_MIC_ADC_RATE, AC97_EXTENDED_STATUS,
  2095. AC97_EA_PRL},
  2096. };
  2097. #ifdef CONFIG_SND_AC97_POWER_SAVE
  2098. /**
  2099. * snd_ac97_update_power - update the powerdown register
  2100. * @ac97: the codec instance
  2101. * @reg: the rate register, e.g. AC97_PCM_FRONT_DAC_RATE
  2102. * @powerup: non-zero when power up the part
  2103. *
  2104. * Update the AC97 powerdown register bits of the given part.
  2105. */
  2106. int snd_ac97_update_power(struct snd_ac97 *ac97, int reg, int powerup)
  2107. {
  2108. int i;
  2109. if (! ac97)
  2110. return 0;
  2111. if (reg) {
  2112. /* SPDIF requires DAC power, too */
  2113. if (reg == AC97_SPDIF)
  2114. reg = AC97_PCM_FRONT_DAC_RATE;
  2115. for (i = 0; i < PWIDX_SIZE; i++) {
  2116. if (power_regs[i].reg == reg) {
  2117. if (powerup)
  2118. ac97->power_up |= (1 << i);
  2119. else
  2120. ac97->power_up &= ~(1 << i);
  2121. break;
  2122. }
  2123. }
  2124. }
  2125. if (ac97_is_power_save_mode(ac97) && !powerup)
  2126. /* adjust power-down bits after two seconds delay
  2127. * (for avoiding loud click noises for many (OSS) apps
  2128. * that open/close frequently)
  2129. */
  2130. schedule_delayed_work(&ac97->power_work, HZ*2);
  2131. else {
  2132. cancel_delayed_work(&ac97->power_work);
  2133. update_power_regs(ac97);
  2134. }
  2135. return 0;
  2136. }
  2137. EXPORT_SYMBOL(snd_ac97_update_power);
  2138. #endif /* CONFIG_SND_AC97_POWER_SAVE */
  2139. static void update_power_regs(struct snd_ac97 *ac97)
  2140. {
  2141. unsigned int power_up, bits;
  2142. int i;
  2143. power_up = (1 << PWIDX_FRONT) | (1 << PWIDX_ADC);
  2144. power_up |= (1 << PWIDX_MIC);
  2145. if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
  2146. power_up |= (1 << PWIDX_SURR);
  2147. if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
  2148. power_up |= (1 << PWIDX_CLFE);
  2149. #ifdef CONFIG_SND_AC97_POWER_SAVE
  2150. if (ac97_is_power_save_mode(ac97))
  2151. power_up = ac97->power_up;
  2152. #endif
  2153. if (power_up) {
  2154. if (ac97->regs[AC97_POWERDOWN] & AC97_PD_PR2) {
  2155. /* needs power-up analog mix and vref */
  2156. snd_ac97_update_bits(ac97, AC97_POWERDOWN,
  2157. AC97_PD_PR3, 0);
  2158. msleep(1);
  2159. snd_ac97_update_bits(ac97, AC97_POWERDOWN,
  2160. AC97_PD_PR2, 0);
  2161. }
  2162. }
  2163. for (i = 0; i < PWIDX_SIZE; i++) {
  2164. if (power_up & (1 << i))
  2165. bits = 0;
  2166. else
  2167. bits = power_regs[i].mask;
  2168. snd_ac97_update_bits(ac97, power_regs[i].power_reg,
  2169. power_regs[i].mask, bits);
  2170. }
  2171. if (! power_up) {
  2172. if (! (ac97->regs[AC97_POWERDOWN] & AC97_PD_PR2)) {
  2173. /* power down analog mix and vref */
  2174. snd_ac97_update_bits(ac97, AC97_POWERDOWN,
  2175. AC97_PD_PR2, AC97_PD_PR2);
  2176. snd_ac97_update_bits(ac97, AC97_POWERDOWN,
  2177. AC97_PD_PR3, AC97_PD_PR3);
  2178. }
  2179. }
  2180. }
  2181. #ifdef CONFIG_PM
  2182. /**
  2183. * snd_ac97_suspend - General suspend function for AC97 codec
  2184. * @ac97: the ac97 instance
  2185. *
  2186. * Suspends the codec, power down the chip.
  2187. */
  2188. void snd_ac97_suspend(struct snd_ac97 *ac97)
  2189. {
  2190. if (! ac97)
  2191. return;
  2192. if (ac97->build_ops->suspend)
  2193. ac97->build_ops->suspend(ac97);
  2194. #ifdef CONFIG_SND_AC97_POWER_SAVE
  2195. cancel_delayed_work(&ac97->power_work);
  2196. flush_scheduled_work();
  2197. #endif
  2198. snd_ac97_powerdown(ac97);
  2199. }
  2200. EXPORT_SYMBOL(snd_ac97_suspend);
  2201. /*
  2202. * restore ac97 status
  2203. */
  2204. void snd_ac97_restore_status(struct snd_ac97 *ac97)
  2205. {
  2206. int i;
  2207. for (i = 2; i < 0x7c ; i += 2) {
  2208. if (i == AC97_POWERDOWN || i == AC97_EXTENDED_ID)
  2209. continue;
  2210. /* restore only accessible registers
  2211. * some chip (e.g. nm256) may hang up when unsupported registers
  2212. * are accessed..!
  2213. */
  2214. if (test_bit(i, ac97->reg_accessed)) {
  2215. snd_ac97_write(ac97, i, ac97->regs[i]);
  2216. snd_ac97_read(ac97, i);
  2217. }
  2218. }
  2219. }
  2220. /*
  2221. * restore IEC958 status
  2222. */
  2223. void snd_ac97_restore_iec958(struct snd_ac97 *ac97)
  2224. {
  2225. if (ac97->ext_id & AC97_EI_SPDIF) {
  2226. if (ac97->regs[AC97_EXTENDED_STATUS] & AC97_EA_SPDIF) {
  2227. /* reset spdif status */
  2228. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
  2229. snd_ac97_write(ac97, AC97_EXTENDED_STATUS, ac97->regs[AC97_EXTENDED_STATUS]);
  2230. if (ac97->flags & AC97_CS_SPDIF)
  2231. snd_ac97_write(ac97, AC97_CSR_SPDIF, ac97->regs[AC97_CSR_SPDIF]);
  2232. else
  2233. snd_ac97_write(ac97, AC97_SPDIF, ac97->regs[AC97_SPDIF]);
  2234. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
  2235. }
  2236. }
  2237. }
  2238. /**
  2239. * snd_ac97_resume - General resume function for AC97 codec
  2240. * @ac97: the ac97 instance
  2241. *
  2242. * Do the standard resume procedure, power up and restoring the
  2243. * old register values.
  2244. */
  2245. void snd_ac97_resume(struct snd_ac97 *ac97)
  2246. {
  2247. unsigned long end_time;
  2248. if (! ac97)
  2249. return;
  2250. if (ac97->bus->ops->reset) {
  2251. ac97->bus->ops->reset(ac97);
  2252. goto __reset_ready;
  2253. }
  2254. snd_ac97_write(ac97, AC97_POWERDOWN, 0);
  2255. if (! (ac97->flags & AC97_DEFAULT_POWER_OFF)) {
  2256. snd_ac97_write(ac97, AC97_RESET, 0);
  2257. udelay(100);
  2258. snd_ac97_write(ac97, AC97_POWERDOWN, 0);
  2259. }
  2260. snd_ac97_write(ac97, AC97_GENERAL_PURPOSE, 0);
  2261. snd_ac97_write(ac97, AC97_POWERDOWN, ac97->regs[AC97_POWERDOWN]);
  2262. if (ac97_is_audio(ac97)) {
  2263. ac97->bus->ops->write(ac97, AC97_MASTER, 0x8101);
  2264. end_time = jiffies + msecs_to_jiffies(100);
  2265. do {
  2266. if (snd_ac97_read(ac97, AC97_MASTER) == 0x8101)
  2267. break;
  2268. schedule_timeout_uninterruptible(1);
  2269. } while (time_after_eq(end_time, jiffies));
  2270. /* FIXME: extra delay */
  2271. ac97->bus->ops->write(ac97, AC97_MASTER, 0x8000);
  2272. if (snd_ac97_read(ac97, AC97_MASTER) != 0x8000)
  2273. msleep(250);
  2274. } else {
  2275. end_time = jiffies + msecs_to_jiffies(100);
  2276. do {
  2277. unsigned short val = snd_ac97_read(ac97, AC97_EXTENDED_MID);
  2278. if (val != 0xffff && (val & 1) != 0)
  2279. break;
  2280. schedule_timeout_uninterruptible(1);
  2281. } while (time_after_eq(end_time, jiffies));
  2282. }
  2283. __reset_ready:
  2284. if (ac97->bus->ops->init)
  2285. ac97->bus->ops->init(ac97);
  2286. if (ac97->build_ops->resume)
  2287. ac97->build_ops->resume(ac97);
  2288. else {
  2289. snd_ac97_restore_status(ac97);
  2290. snd_ac97_restore_iec958(ac97);
  2291. }
  2292. }
  2293. EXPORT_SYMBOL(snd_ac97_resume);
  2294. #endif
  2295. /*
  2296. * Hardware tuning
  2297. */
  2298. static void set_ctl_name(char *dst, const char *src, const char *suffix)
  2299. {
  2300. if (suffix)
  2301. sprintf(dst, "%s %s", src, suffix);
  2302. else
  2303. strcpy(dst, src);
  2304. }
  2305. /* remove the control with the given name and optional suffix */
  2306. static int snd_ac97_remove_ctl(struct snd_ac97 *ac97, const char *name,
  2307. const char *suffix)
  2308. {
  2309. struct snd_ctl_elem_id id;
  2310. memset(&id, 0, sizeof(id));
  2311. set_ctl_name(id.name, name, suffix);
  2312. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  2313. return snd_ctl_remove_id(ac97->bus->card, &id);
  2314. }
  2315. static struct snd_kcontrol *ctl_find(struct snd_ac97 *ac97, const char *name, const char *suffix)
  2316. {
  2317. struct snd_ctl_elem_id sid;
  2318. memset(&sid, 0, sizeof(sid));
  2319. set_ctl_name(sid.name, name, suffix);
  2320. sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  2321. return snd_ctl_find_id(ac97->bus->card, &sid);
  2322. }
  2323. /* rename the control with the given name and optional suffix */
  2324. static int snd_ac97_rename_ctl(struct snd_ac97 *ac97, const char *src,
  2325. const char *dst, const char *suffix)
  2326. {
  2327. struct snd_kcontrol *kctl = ctl_find(ac97, src, suffix);
  2328. if (kctl) {
  2329. set_ctl_name(kctl->id.name, dst, suffix);
  2330. return 0;
  2331. }
  2332. return -ENOENT;
  2333. }
  2334. /* rename both Volume and Switch controls - don't check the return value */
  2335. static void snd_ac97_rename_vol_ctl(struct snd_ac97 *ac97, const char *src,
  2336. const char *dst)
  2337. {
  2338. snd_ac97_rename_ctl(ac97, src, dst, "Switch");
  2339. snd_ac97_rename_ctl(ac97, src, dst, "Volume");
  2340. }
  2341. /* swap controls */
  2342. static int snd_ac97_swap_ctl(struct snd_ac97 *ac97, const char *s1,
  2343. const char *s2, const char *suffix)
  2344. {
  2345. struct snd_kcontrol *kctl1, *kctl2;
  2346. kctl1 = ctl_find(ac97, s1, suffix);
  2347. kctl2 = ctl_find(ac97, s2, suffix);
  2348. if (kctl1 && kctl2) {
  2349. set_ctl_name(kctl1->id.name, s2, suffix);
  2350. set_ctl_name(kctl2->id.name, s1, suffix);
  2351. return 0;
  2352. }
  2353. return -ENOENT;
  2354. }
  2355. #if 1
  2356. /* bind hp and master controls instead of using only hp control */
  2357. static int bind_hp_volsw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2358. {
  2359. int err = snd_ac97_put_volsw(kcontrol, ucontrol);
  2360. if (err > 0) {
  2361. unsigned long priv_saved = kcontrol->private_value;
  2362. kcontrol->private_value = (kcontrol->private_value & ~0xff) | AC97_HEADPHONE;
  2363. snd_ac97_put_volsw(kcontrol, ucontrol);
  2364. kcontrol->private_value = priv_saved;
  2365. }
  2366. return err;
  2367. }
  2368. /* ac97 tune: bind Master and Headphone controls */
  2369. static int tune_hp_only(struct snd_ac97 *ac97)
  2370. {
  2371. struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
  2372. struct snd_kcontrol *mvol = ctl_find(ac97, "Master Playback Volume", NULL);
  2373. if (! msw || ! mvol)
  2374. return -ENOENT;
  2375. msw->put = bind_hp_volsw_put;
  2376. mvol->put = bind_hp_volsw_put;
  2377. snd_ac97_remove_ctl(ac97, "Headphone Playback", "Switch");
  2378. snd_ac97_remove_ctl(ac97, "Headphone Playback", "Volume");
  2379. return 0;
  2380. }
  2381. #else
  2382. /* ac97 tune: use Headphone control as master */
  2383. static int tune_hp_only(struct snd_ac97 *ac97)
  2384. {
  2385. if (ctl_find(ac97, "Headphone Playback Switch", NULL) == NULL)
  2386. return -ENOENT;
  2387. snd_ac97_remove_ctl(ac97, "Master Playback", "Switch");
  2388. snd_ac97_remove_ctl(ac97, "Master Playback", "Volume");
  2389. snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
  2390. return 0;
  2391. }
  2392. #endif
  2393. /* ac97 tune: swap Headphone and Master controls */
  2394. static int tune_swap_hp(struct snd_ac97 *ac97)
  2395. {
  2396. if (ctl_find(ac97, "Headphone Playback Switch", NULL) == NULL)
  2397. return -ENOENT;
  2398. snd_ac97_rename_vol_ctl(ac97, "Master Playback", "Line-Out Playback");
  2399. snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
  2400. return 0;
  2401. }
  2402. /* ac97 tune: swap Surround and Master controls */
  2403. static int tune_swap_surround(struct snd_ac97 *ac97)
  2404. {
  2405. if (snd_ac97_swap_ctl(ac97, "Master Playback", "Surround Playback", "Switch") ||
  2406. snd_ac97_swap_ctl(ac97, "Master Playback", "Surround Playback", "Volume"))
  2407. return -ENOENT;
  2408. return 0;
  2409. }
  2410. /* ac97 tune: set up mic sharing for AD codecs */
  2411. static int tune_ad_sharing(struct snd_ac97 *ac97)
  2412. {
  2413. unsigned short scfg;
  2414. if ((ac97->id & 0xffffff00) != 0x41445300) {
  2415. snd_printk(KERN_ERR "ac97_quirk AD_SHARING is only for AD codecs\n");
  2416. return -EINVAL;
  2417. }
  2418. /* Turn on OMS bit to route microphone to back panel */
  2419. scfg = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
  2420. snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, scfg | 0x0200);
  2421. return 0;
  2422. }
  2423. static const struct snd_kcontrol_new snd_ac97_alc_jack_detect =
  2424. AC97_SINGLE("Jack Detect", AC97_ALC650_CLOCK, 5, 1, 0);
  2425. /* ac97 tune: set up ALC jack-select */
  2426. static int tune_alc_jack(struct snd_ac97 *ac97)
  2427. {
  2428. if ((ac97->id & 0xffffff00) != 0x414c4700) {
  2429. snd_printk(KERN_ERR "ac97_quirk ALC_JACK is only for Realtek codecs\n");
  2430. return -EINVAL;
  2431. }
  2432. snd_ac97_update_bits(ac97, 0x7a, 0x20, 0x20); /* select jack detect function */
  2433. snd_ac97_update_bits(ac97, 0x7a, 0x01, 0x01); /* Line-out auto mute */
  2434. if (ac97->id == AC97_ID_ALC658D)
  2435. snd_ac97_update_bits(ac97, 0x74, 0x0800, 0x0800);
  2436. return snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&snd_ac97_alc_jack_detect, ac97));
  2437. }
  2438. /* ac97 tune: inversed EAPD bit */
  2439. static int tune_inv_eapd(struct snd_ac97 *ac97)
  2440. {
  2441. struct snd_kcontrol *kctl = ctl_find(ac97, "External Amplifier", NULL);
  2442. if (! kctl)
  2443. return -ENOENT;
  2444. set_inv_eapd(ac97, kctl);
  2445. return 0;
  2446. }
  2447. static int master_mute_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2448. {
  2449. int err = snd_ac97_put_volsw(kcontrol, ucontrol);
  2450. if (err > 0) {
  2451. struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
  2452. int shift = (kcontrol->private_value >> 8) & 0x0f;
  2453. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  2454. unsigned short mask;
  2455. if (shift != rshift)
  2456. mask = 0x8080;
  2457. else
  2458. mask = 0x8000;
  2459. snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000,
  2460. (ac97->regs[AC97_MASTER] & mask) == mask ?
  2461. 0x8000 : 0);
  2462. }
  2463. return err;
  2464. }
  2465. /* ac97 tune: EAPD controls mute LED bound with the master mute */
  2466. static int tune_mute_led(struct snd_ac97 *ac97)
  2467. {
  2468. struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
  2469. if (! msw)
  2470. return -ENOENT;
  2471. msw->put = master_mute_sw_put;
  2472. snd_ac97_remove_ctl(ac97, "External Amplifier", NULL);
  2473. snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000, 0x8000); /* mute LED on */
  2474. ac97->scaps |= AC97_SCAP_EAPD_LED;
  2475. return 0;
  2476. }
  2477. static int hp_master_mute_sw_put(struct snd_kcontrol *kcontrol,
  2478. struct snd_ctl_elem_value *ucontrol)
  2479. {
  2480. int err = bind_hp_volsw_put(kcontrol, ucontrol);
  2481. if (err > 0) {
  2482. struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
  2483. int shift = (kcontrol->private_value >> 8) & 0x0f;
  2484. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  2485. unsigned short mask;
  2486. if (shift != rshift)
  2487. mask = 0x8080;
  2488. else
  2489. mask = 0x8000;
  2490. snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000,
  2491. (ac97->regs[AC97_MASTER] & mask) == mask ?
  2492. 0x8000 : 0);
  2493. }
  2494. return err;
  2495. }
  2496. static int tune_hp_mute_led(struct snd_ac97 *ac97)
  2497. {
  2498. struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
  2499. struct snd_kcontrol *mvol = ctl_find(ac97, "Master Playback Volume", NULL);
  2500. if (! msw || ! mvol)
  2501. return -ENOENT;
  2502. msw->put = hp_master_mute_sw_put;
  2503. mvol->put = bind_hp_volsw_put;
  2504. snd_ac97_remove_ctl(ac97, "External Amplifier", NULL);
  2505. snd_ac97_remove_ctl(ac97, "Headphone Playback", "Switch");
  2506. snd_ac97_remove_ctl(ac97, "Headphone Playback", "Volume");
  2507. snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000, 0x8000); /* mute LED on */
  2508. return 0;
  2509. }
  2510. struct quirk_table {
  2511. const char *name;
  2512. int (*func)(struct snd_ac97 *);
  2513. };
  2514. static struct quirk_table applicable_quirks[] = {
  2515. { "none", NULL },
  2516. { "hp_only", tune_hp_only },
  2517. { "swap_hp", tune_swap_hp },
  2518. { "swap_surround", tune_swap_surround },
  2519. { "ad_sharing", tune_ad_sharing },
  2520. { "alc_jack", tune_alc_jack },
  2521. { "inv_eapd", tune_inv_eapd },
  2522. { "mute_led", tune_mute_led },
  2523. { "hp_mute_led", tune_hp_mute_led },
  2524. };
  2525. /* apply the quirk with the given type */
  2526. static int apply_quirk(struct snd_ac97 *ac97, int type)
  2527. {
  2528. if (type <= 0)
  2529. return 0;
  2530. else if (type >= ARRAY_SIZE(applicable_quirks))
  2531. return -EINVAL;
  2532. if (applicable_quirks[type].func)
  2533. return applicable_quirks[type].func(ac97);
  2534. return 0;
  2535. }
  2536. /* apply the quirk with the given name */
  2537. static int apply_quirk_str(struct snd_ac97 *ac97, const char *typestr)
  2538. {
  2539. int i;
  2540. struct quirk_table *q;
  2541. for (i = 0; i < ARRAY_SIZE(applicable_quirks); i++) {
  2542. q = &applicable_quirks[i];
  2543. if (q->name && ! strcmp(typestr, q->name))
  2544. return apply_quirk(ac97, i);
  2545. }
  2546. /* for compatibility, accept the numbers, too */
  2547. if (*typestr >= '0' && *typestr <= '9')
  2548. return apply_quirk(ac97, (int)simple_strtoul(typestr, NULL, 10));
  2549. return -EINVAL;
  2550. }
  2551. /**
  2552. * snd_ac97_tune_hardware - tune up the hardware
  2553. * @ac97: the ac97 instance
  2554. * @quirk: quirk list
  2555. * @override: explicit quirk value (overrides the list if non-NULL)
  2556. *
  2557. * Do some workaround for each pci device, such as renaming of the
  2558. * headphone (true line-out) control as "Master".
  2559. * The quirk-list must be terminated with a zero-filled entry.
  2560. *
  2561. * Returns zero if successful, or a negative error code on failure.
  2562. */
  2563. int snd_ac97_tune_hardware(struct snd_ac97 *ac97, struct ac97_quirk *quirk, const char *override)
  2564. {
  2565. int result;
  2566. /* quirk overriden? */
  2567. if (override && strcmp(override, "-1") && strcmp(override, "default")) {
  2568. result = apply_quirk_str(ac97, override);
  2569. if (result < 0)
  2570. snd_printk(KERN_ERR "applying quirk type %s failed (%d)\n", override, result);
  2571. return result;
  2572. }
  2573. if (! quirk)
  2574. return -EINVAL;
  2575. for (; quirk->subvendor; quirk++) {
  2576. if (quirk->subvendor != ac97->subsystem_vendor)
  2577. continue;
  2578. if ((! quirk->mask && quirk->subdevice == ac97->subsystem_device) ||
  2579. quirk->subdevice == (quirk->mask & ac97->subsystem_device)) {
  2580. if (quirk->codec_id && quirk->codec_id != ac97->id)
  2581. continue;
  2582. snd_printdd("ac97 quirk for %s (%04x:%04x)\n", quirk->name, ac97->subsystem_vendor, ac97->subsystem_device);
  2583. result = apply_quirk(ac97, quirk->type);
  2584. if (result < 0)
  2585. snd_printk(KERN_ERR "applying quirk type %d for %s failed (%d)\n", quirk->type, quirk->name, result);
  2586. return result;
  2587. }
  2588. }
  2589. return 0;
  2590. }
  2591. EXPORT_SYMBOL(snd_ac97_tune_hardware);
  2592. /*
  2593. * INIT part
  2594. */
  2595. static int __init alsa_ac97_init(void)
  2596. {
  2597. return 0;
  2598. }
  2599. static void __exit alsa_ac97_exit(void)
  2600. {
  2601. }
  2602. module_init(alsa_ac97_init)
  2603. module_exit(alsa_ac97_exit)