ac97_codec.c 91 KB

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