ac97_codec.c 85 KB

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