ac97_codec.c 84 KB

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