ac97_codec.c 91 KB

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