ac97_codec.c 91 KB

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