ac97_codec.c 94 KB

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