ac97_codec.c 85 KB

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