ac97_codec.c 90 KB

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