ac97_codec.c 85 KB

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