ac97_codec.c 91 KB

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