ac97_codec.c 85 KB

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