ac97_codec.c 85 KB

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