ac97_codec.c 94 KB

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