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. { 0x414c4781, 0xffffffff, "ALC658D", NULL, NULL }, /* already patched */
  107. { 0x414c4780, 0xfffffff0, "ALC658", patch_alc655, NULL },
  108. { 0x414c4790, 0xfffffff0, "ALC850", patch_alc850, NULL },
  109. { 0x414c4730, 0xffffffff, "ALC101", NULL, NULL },
  110. { 0x414c4740, 0xfffffff0, "ALC202", NULL, NULL },
  111. { 0x414c4750, 0xfffffff0, "ALC250", NULL, NULL },
  112. { 0x414c4770, 0xfffffff0, "ALC203", NULL, NULL },
  113. { 0x434d4941, 0xffffffff, "CMI9738", patch_cm9738, NULL },
  114. { 0x434d4961, 0xffffffff, "CMI9739", patch_cm9739, NULL },
  115. { 0x434d4969, 0xffffffff, "CMI9780", patch_cm9780, NULL },
  116. { 0x434d4978, 0xffffffff, "CMI9761", patch_cm9761, NULL },
  117. { 0x434d4982, 0xffffffff, "CMI9761", patch_cm9761, NULL },
  118. { 0x434d4983, 0xffffffff, "CMI9761", patch_cm9761, NULL },
  119. { 0x43525900, 0xfffffff8, "CS4297", NULL, NULL },
  120. { 0x43525910, 0xfffffff8, "CS4297A", patch_cirrus_spdif, NULL },
  121. { 0x43525920, 0xfffffff8, "CS4298", patch_cirrus_spdif, NULL },
  122. { 0x43525928, 0xfffffff8, "CS4294", NULL, NULL },
  123. { 0x43525930, 0xfffffff8, "CS4299", patch_cirrus_cs4299, NULL },
  124. { 0x43525948, 0xfffffff8, "CS4201", NULL, NULL },
  125. { 0x43525958, 0xfffffff8, "CS4205", patch_cirrus_spdif, NULL },
  126. { 0x43525960, 0xfffffff8, "CS4291", NULL, NULL },
  127. { 0x43525970, 0xfffffff8, "CS4202", NULL, NULL },
  128. { 0x43585421, 0xffffffff, "HSD11246", NULL, NULL }, // SmartMC II
  129. { 0x43585428, 0xfffffff8, "Cx20468", patch_conexant, NULL }, // SmartAMC fixme: the mask might be different
  130. { 0x44543031, 0xfffffff0, "DT0398", NULL, NULL },
  131. { 0x454d4328, 0xffffffff, "28028", NULL, NULL }, // same as TR28028?
  132. { 0x45838308, 0xffffffff, "ESS1988", NULL, NULL },
  133. { 0x48525300, 0xffffff00, "HMP9701", NULL, NULL },
  134. { 0x49434501, 0xffffffff, "ICE1230", NULL, NULL },
  135. { 0x49434511, 0xffffffff, "ICE1232", NULL, NULL }, // alias VIA VT1611A?
  136. { 0x49434514, 0xffffffff, "ICE1232A", NULL, NULL },
  137. { 0x49434551, 0xffffffff, "VT1616", patch_vt1616, NULL },
  138. { 0x49434552, 0xffffffff, "VT1616i", patch_vt1616, NULL }, // VT1616 compatible (chipset integrated)
  139. { 0x49544520, 0xffffffff, "IT2226E", NULL, NULL },
  140. { 0x49544561, 0xffffffff, "IT2646E", patch_it2646, NULL },
  141. { 0x4e534300, 0xffffffff, "LM4540,43,45,46,48", NULL, NULL }, // only guess --jk
  142. { 0x4e534331, 0xffffffff, "LM4549", NULL, NULL },
  143. { 0x4e534350, 0xffffffff, "LM4550", NULL, NULL },
  144. { 0x50534304, 0xffffffff, "UCB1400", NULL, NULL },
  145. { 0x53494c20, 0xffffffe0, "Si3036,8", mpatch_si3036, mpatch_si3036, AC97_MODEM_PATCH },
  146. { 0x54524102, 0xffffffff, "TR28022", NULL, NULL },
  147. { 0x54524106, 0xffffffff, "TR28026", NULL, NULL },
  148. { 0x54524108, 0xffffffff, "TR28028", patch_tritech_tr28028, NULL }, // added by xin jin [07/09/99]
  149. { 0x54524123, 0xffffffff, "TR28602", NULL, NULL }, // only guess --jk [TR28023 = eMicro EM28023 (new CT1297)]
  150. { 0x54584e20, 0xffffffff, "TLC320AD9xC", NULL, NULL },
  151. { 0x56494161, 0xffffffff, "VIA1612A", NULL, NULL }, // modified ICE1232 with S/PDIF
  152. { 0x56494170, 0xffffffff, "VIA1617A", patch_vt1617a, NULL }, // modified VT1616 with S/PDIF
  153. { 0x57454301, 0xffffffff, "W83971D", NULL, NULL },
  154. { 0x574d4c00, 0xffffffff, "WM9701A", NULL, NULL },
  155. { 0x574d4C03, 0xffffffff, "WM9703,WM9707,WM9708,WM9717", patch_wolfson03, NULL},
  156. { 0x574d4C04, 0xffffffff, "WM9704M,WM9704Q", patch_wolfson04, NULL},
  157. { 0x574d4C05, 0xffffffff, "WM9705,WM9710", patch_wolfson05, NULL},
  158. { 0x574d4C09, 0xffffffff, "WM9709", NULL, NULL},
  159. { 0x574d4C12, 0xffffffff, "WM9711,WM9712", patch_wolfson11, NULL},
  160. { 0x574d4c13, 0xffffffff, "WM9713,WM9714", patch_wolfson13, NULL, AC97_DEFAULT_POWER_OFF},
  161. { 0x594d4800, 0xffffffff, "YMF743", NULL, NULL },
  162. { 0x594d4802, 0xffffffff, "YMF752", NULL, NULL },
  163. { 0x594d4803, 0xffffffff, "YMF753", patch_yamaha_ymf753, NULL },
  164. { 0x83847600, 0xffffffff, "STAC9700,83,84", patch_sigmatel_stac9700, NULL },
  165. { 0x83847604, 0xffffffff, "STAC9701,3,4,5", NULL, NULL },
  166. { 0x83847605, 0xffffffff, "STAC9704", NULL, NULL },
  167. { 0x83847608, 0xffffffff, "STAC9708,11", patch_sigmatel_stac9708, NULL },
  168. { 0x83847609, 0xffffffff, "STAC9721,23", patch_sigmatel_stac9721, NULL },
  169. { 0x83847644, 0xffffffff, "STAC9744", patch_sigmatel_stac9744, NULL },
  170. { 0x83847650, 0xffffffff, "STAC9750,51", NULL, NULL }, // patch?
  171. { 0x83847652, 0xffffffff, "STAC9752,53", NULL, NULL }, // patch?
  172. { 0x83847656, 0xffffffff, "STAC9756,57", patch_sigmatel_stac9756, NULL },
  173. { 0x83847658, 0xffffffff, "STAC9758,59", patch_sigmatel_stac9758, NULL },
  174. { 0x83847666, 0xffffffff, "STAC9766,67", NULL, NULL }, // patch?
  175. { 0, 0, NULL, NULL, NULL }
  176. };
  177. const char *snd_ac97_stereo_enhancements[] =
  178. {
  179. /* 0 */ "No 3D Stereo Enhancement",
  180. /* 1 */ "Analog Devices Phat Stereo",
  181. /* 2 */ "Creative Stereo Enhancement",
  182. /* 3 */ "National Semi 3D Stereo Enhancement",
  183. /* 4 */ "YAMAHA Ymersion",
  184. /* 5 */ "BBE 3D Stereo Enhancement",
  185. /* 6 */ "Crystal Semi 3D Stereo Enhancement",
  186. /* 7 */ "Qsound QXpander",
  187. /* 8 */ "Spatializer 3D Stereo Enhancement",
  188. /* 9 */ "SRS 3D Stereo Enhancement",
  189. /* 10 */ "Platform Tech 3D Stereo Enhancement",
  190. /* 11 */ "AKM 3D Audio",
  191. /* 12 */ "Aureal Stereo Enhancement",
  192. /* 13 */ "Aztech 3D Enhancement",
  193. /* 14 */ "Binaura 3D Audio Enhancement",
  194. /* 15 */ "ESS Technology Stereo Enhancement",
  195. /* 16 */ "Harman International VMAx",
  196. /* 17 */ "Nvidea/IC Ensemble/KS Waves 3D Stereo Enhancement",
  197. /* 18 */ "Philips Incredible Sound",
  198. /* 19 */ "Texas Instruments 3D Stereo Enhancement",
  199. /* 20 */ "VLSI Technology 3D Stereo Enhancement",
  200. /* 21 */ "TriTech 3D Stereo Enhancement",
  201. /* 22 */ "Realtek 3D Stereo Enhancement",
  202. /* 23 */ "Samsung 3D Stereo Enhancement",
  203. /* 24 */ "Wolfson Microelectronics 3D Enhancement",
  204. /* 25 */ "Delta Integration 3D Enhancement",
  205. /* 26 */ "SigmaTel 3D Enhancement",
  206. /* 27 */ "IC Ensemble/KS Waves",
  207. /* 28 */ "Rockwell 3D Stereo Enhancement",
  208. /* 29 */ "Reserved 29",
  209. /* 30 */ "Reserved 30",
  210. /* 31 */ "Reserved 31"
  211. };
  212. /*
  213. * Shared AC97 controllers (ICH, ATIIXP...)
  214. */
  215. static DECLARE_MUTEX(shared_codec_mutex);
  216. static ac97_t *shared_codec[AC97_SHARED_TYPES][4];
  217. /*
  218. * I/O routines
  219. */
  220. static int snd_ac97_valid_reg(ac97_t *ac97, unsigned short reg)
  221. {
  222. if (ac97->limited_regs && ! test_bit(reg, ac97->reg_accessed))
  223. return 0;
  224. /* filter some registers for buggy codecs */
  225. switch (ac97->id) {
  226. case AC97_ID_AK4540:
  227. case AC97_ID_AK4542:
  228. if (reg <= 0x1c || reg == 0x20 || reg == 0x26 || reg >= 0x7c)
  229. return 1;
  230. return 0;
  231. case AC97_ID_AD1819: /* AD1819 */
  232. case AC97_ID_AD1881: /* AD1881 */
  233. case AC97_ID_AD1881A: /* AD1881A */
  234. if (reg >= 0x3a && reg <= 0x6e) /* 0x59 */
  235. return 0;
  236. return 1;
  237. case AC97_ID_AD1885: /* AD1885 */
  238. case AC97_ID_AD1886: /* AD1886 */
  239. case AC97_ID_AD1886A: /* AD1886A - !!verify!! --jk */
  240. case AC97_ID_AD1887: /* AD1887 - !!verify!! --jk */
  241. if (reg == 0x5a)
  242. return 1;
  243. if (reg >= 0x3c && reg <= 0x6e) /* 0x59 */
  244. return 0;
  245. return 1;
  246. case AC97_ID_STAC9700:
  247. case AC97_ID_STAC9704:
  248. case AC97_ID_STAC9705:
  249. case AC97_ID_STAC9708:
  250. case AC97_ID_STAC9721:
  251. case AC97_ID_STAC9744:
  252. case AC97_ID_STAC9756:
  253. if (reg <= 0x3a || reg >= 0x5a)
  254. return 1;
  255. return 0;
  256. }
  257. return 1;
  258. }
  259. /**
  260. * snd_ac97_write - write a value on the given register
  261. * @ac97: the ac97 instance
  262. * @reg: the register to change
  263. * @value: the value to set
  264. *
  265. * Writes a value on the given register. This will invoke the write
  266. * callback directly after the register check.
  267. * This function doesn't change the register cache unlike
  268. * #snd_ca97_write_cache(), so use this only when you don't want to
  269. * reflect the change to the suspend/resume state.
  270. */
  271. void snd_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short value)
  272. {
  273. if (!snd_ac97_valid_reg(ac97, reg))
  274. return;
  275. if ((ac97->id & 0xffffff00) == AC97_ID_ALC100) {
  276. /* Fix H/W bug of ALC100/100P */
  277. if (reg == AC97_MASTER || reg == AC97_HEADPHONE)
  278. ac97->bus->ops->write(ac97, AC97_RESET, 0); /* reset audio codec */
  279. }
  280. ac97->bus->ops->write(ac97, reg, value);
  281. }
  282. /**
  283. * snd_ac97_read - read a value from the given register
  284. *
  285. * @ac97: the ac97 instance
  286. * @reg: the register to read
  287. *
  288. * Reads a value from the given register. This will invoke the read
  289. * callback directly after the register check.
  290. *
  291. * Returns the read value.
  292. */
  293. unsigned short snd_ac97_read(ac97_t *ac97, unsigned short reg)
  294. {
  295. if (!snd_ac97_valid_reg(ac97, reg))
  296. return 0;
  297. return ac97->bus->ops->read(ac97, reg);
  298. }
  299. /* read a register - return the cached value if already read */
  300. static inline unsigned short snd_ac97_read_cache(ac97_t *ac97, unsigned short reg)
  301. {
  302. if (! test_bit(reg, ac97->reg_accessed)) {
  303. ac97->regs[reg] = ac97->bus->ops->read(ac97, reg);
  304. // set_bit(reg, ac97->reg_accessed);
  305. }
  306. return ac97->regs[reg];
  307. }
  308. /**
  309. * snd_ac97_write_cache - write a value on the given register and update the cache
  310. * @ac97: the ac97 instance
  311. * @reg: the register to change
  312. * @value: the value to set
  313. *
  314. * Writes a value on the given register and updates the register
  315. * cache. The cached values are used for the cached-read and the
  316. * suspend/resume.
  317. */
  318. void snd_ac97_write_cache(ac97_t *ac97, unsigned short reg, unsigned short value)
  319. {
  320. if (!snd_ac97_valid_reg(ac97, reg))
  321. return;
  322. down(&ac97->reg_mutex);
  323. ac97->regs[reg] = value;
  324. ac97->bus->ops->write(ac97, reg, value);
  325. set_bit(reg, ac97->reg_accessed);
  326. up(&ac97->reg_mutex);
  327. }
  328. /**
  329. * snd_ac97_update - update the value on the given register
  330. * @ac97: the ac97 instance
  331. * @reg: the register to change
  332. * @value: the value to set
  333. *
  334. * Compares the value with the register cache and updates the value
  335. * only when the value is changed.
  336. *
  337. * Returns 1 if the value is changed, 0 if no change, or a negative
  338. * code on failure.
  339. */
  340. int snd_ac97_update(ac97_t *ac97, unsigned short reg, unsigned short value)
  341. {
  342. int change;
  343. if (!snd_ac97_valid_reg(ac97, reg))
  344. return -EINVAL;
  345. down(&ac97->reg_mutex);
  346. change = ac97->regs[reg] != value;
  347. if (change) {
  348. ac97->regs[reg] = value;
  349. ac97->bus->ops->write(ac97, reg, value);
  350. }
  351. set_bit(reg, ac97->reg_accessed);
  352. up(&ac97->reg_mutex);
  353. return change;
  354. }
  355. /**
  356. * snd_ac97_update_bits - update the bits on the given register
  357. * @ac97: the ac97 instance
  358. * @reg: the register to change
  359. * @mask: the bit-mask to change
  360. * @value: the value to set
  361. *
  362. * Updates the masked-bits on the given register only when the value
  363. * is changed.
  364. *
  365. * Returns 1 if the bits are changed, 0 if no change, or a negative
  366. * code on failure.
  367. */
  368. int snd_ac97_update_bits(ac97_t *ac97, unsigned short reg, unsigned short mask, unsigned short value)
  369. {
  370. int change;
  371. if (!snd_ac97_valid_reg(ac97, reg))
  372. return -EINVAL;
  373. down(&ac97->reg_mutex);
  374. change = snd_ac97_update_bits_nolock(ac97, reg, mask, value);
  375. up(&ac97->reg_mutex);
  376. return change;
  377. }
  378. /* no lock version - see snd_ac97_updat_bits() */
  379. int snd_ac97_update_bits_nolock(ac97_t *ac97, unsigned short reg,
  380. unsigned short mask, unsigned short value)
  381. {
  382. int change;
  383. unsigned short old, new;
  384. old = snd_ac97_read_cache(ac97, reg);
  385. new = (old & ~mask) | value;
  386. change = old != new;
  387. if (change) {
  388. ac97->regs[reg] = new;
  389. ac97->bus->ops->write(ac97, reg, new);
  390. }
  391. set_bit(reg, ac97->reg_accessed);
  392. return change;
  393. }
  394. static int snd_ac97_ad18xx_update_pcm_bits(ac97_t *ac97, int codec, unsigned short mask, unsigned short value)
  395. {
  396. int change;
  397. unsigned short old, new, cfg;
  398. down(&ac97->page_mutex);
  399. old = ac97->spec.ad18xx.pcmreg[codec];
  400. new = (old & ~mask) | value;
  401. change = old != new;
  402. if (change) {
  403. down(&ac97->reg_mutex);
  404. cfg = snd_ac97_read_cache(ac97, AC97_AD_SERIAL_CFG);
  405. ac97->spec.ad18xx.pcmreg[codec] = new;
  406. /* select single codec */
  407. ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG,
  408. (cfg & ~0x7000) |
  409. ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
  410. /* update PCM bits */
  411. ac97->bus->ops->write(ac97, AC97_PCM, new);
  412. /* select all codecs */
  413. ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG,
  414. cfg | 0x7000);
  415. up(&ac97->reg_mutex);
  416. }
  417. up(&ac97->page_mutex);
  418. return change;
  419. }
  420. /*
  421. * Controls
  422. */
  423. int snd_ac97_info_enum_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  424. {
  425. struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
  426. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  427. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  428. uinfo->value.enumerated.items = e->mask;
  429. if (uinfo->value.enumerated.item > e->mask - 1)
  430. uinfo->value.enumerated.item = e->mask - 1;
  431. strcpy(uinfo->value.enumerated.name, e->texts[uinfo->value.enumerated.item]);
  432. return 0;
  433. }
  434. int snd_ac97_get_enum_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  435. {
  436. ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
  437. struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
  438. unsigned short val, bitmask;
  439. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  440. ;
  441. val = snd_ac97_read_cache(ac97, e->reg);
  442. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  443. if (e->shift_l != e->shift_r)
  444. ucontrol->value.enumerated.item[1] = (val >> e->shift_r) & (bitmask - 1);
  445. return 0;
  446. }
  447. int snd_ac97_put_enum_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  448. {
  449. ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
  450. struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
  451. unsigned short val;
  452. unsigned short mask, bitmask;
  453. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  454. ;
  455. if (ucontrol->value.enumerated.item[0] > e->mask - 1)
  456. return -EINVAL;
  457. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  458. mask = (bitmask - 1) << e->shift_l;
  459. if (e->shift_l != e->shift_r) {
  460. if (ucontrol->value.enumerated.item[1] > e->mask - 1)
  461. return -EINVAL;
  462. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  463. mask |= (bitmask - 1) << e->shift_r;
  464. }
  465. return snd_ac97_update_bits(ac97, e->reg, mask, val);
  466. }
  467. /* save/restore ac97 v2.3 paging */
  468. static int snd_ac97_page_save(ac97_t *ac97, int reg, snd_kcontrol_t *kcontrol)
  469. {
  470. int page_save = -1;
  471. if ((kcontrol->private_value & (1<<25)) &&
  472. (ac97->ext_id & AC97_EI_REV_MASK) >= AC97_EI_REV_23 &&
  473. (reg >= 0x60 && reg < 0x70)) {
  474. unsigned short page = (kcontrol->private_value >> 26) & 0x0f;
  475. down(&ac97->page_mutex); /* lock paging */
  476. page_save = snd_ac97_read(ac97, AC97_INT_PAGING) & AC97_PAGE_MASK;
  477. snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page);
  478. }
  479. return page_save;
  480. }
  481. static void snd_ac97_page_restore(ac97_t *ac97, int page_save)
  482. {
  483. if (page_save >= 0) {
  484. snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page_save);
  485. up(&ac97->page_mutex); /* unlock paging */
  486. }
  487. }
  488. /* volume and switch controls */
  489. int snd_ac97_info_volsw(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  490. {
  491. int mask = (kcontrol->private_value >> 16) & 0xff;
  492. int shift = (kcontrol->private_value >> 8) & 0x0f;
  493. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  494. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  495. uinfo->count = shift == rshift ? 1 : 2;
  496. uinfo->value.integer.min = 0;
  497. uinfo->value.integer.max = mask;
  498. return 0;
  499. }
  500. int snd_ac97_get_volsw(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  501. {
  502. ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
  503. int reg = kcontrol->private_value & 0xff;
  504. int shift = (kcontrol->private_value >> 8) & 0x0f;
  505. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  506. int mask = (kcontrol->private_value >> 16) & 0xff;
  507. int invert = (kcontrol->private_value >> 24) & 0x01;
  508. int page_save;
  509. page_save = snd_ac97_page_save(ac97, reg, kcontrol);
  510. ucontrol->value.integer.value[0] = (snd_ac97_read_cache(ac97, reg) >> shift) & mask;
  511. if (shift != rshift)
  512. ucontrol->value.integer.value[1] = (snd_ac97_read_cache(ac97, reg) >> rshift) & mask;
  513. if (invert) {
  514. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  515. if (shift != rshift)
  516. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  517. }
  518. snd_ac97_page_restore(ac97, page_save);
  519. return 0;
  520. }
  521. int snd_ac97_put_volsw(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  522. {
  523. ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
  524. int reg = kcontrol->private_value & 0xff;
  525. int shift = (kcontrol->private_value >> 8) & 0x0f;
  526. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  527. int mask = (kcontrol->private_value >> 16) & 0xff;
  528. int invert = (kcontrol->private_value >> 24) & 0x01;
  529. int err, page_save;
  530. unsigned short val, val2, val_mask;
  531. page_save = snd_ac97_page_save(ac97, reg, kcontrol);
  532. val = (ucontrol->value.integer.value[0] & mask);
  533. if (invert)
  534. val = mask - val;
  535. val_mask = mask << shift;
  536. val = val << shift;
  537. if (shift != rshift) {
  538. val2 = (ucontrol->value.integer.value[1] & mask);
  539. if (invert)
  540. val2 = mask - val2;
  541. val_mask |= mask << rshift;
  542. val |= val2 << rshift;
  543. }
  544. err = snd_ac97_update_bits(ac97, reg, val_mask, val);
  545. snd_ac97_page_restore(ac97, page_save);
  546. return err;
  547. }
  548. static const snd_kcontrol_new_t snd_ac97_controls_master_mono[2] = {
  549. AC97_SINGLE("Master Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
  550. AC97_SINGLE("Master Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1)
  551. };
  552. static const snd_kcontrol_new_t snd_ac97_controls_tone[2] = {
  553. AC97_SINGLE("Tone Control - Bass", AC97_MASTER_TONE, 8, 15, 1),
  554. AC97_SINGLE("Tone Control - Treble", AC97_MASTER_TONE, 0, 15, 1)
  555. };
  556. static const snd_kcontrol_new_t snd_ac97_controls_pc_beep[2] = {
  557. AC97_SINGLE("PC Speaker Playback Switch", AC97_PC_BEEP, 15, 1, 1),
  558. AC97_SINGLE("PC Speaker Playback Volume", AC97_PC_BEEP, 1, 15, 1)
  559. };
  560. static const snd_kcontrol_new_t snd_ac97_controls_mic_boost =
  561. AC97_SINGLE("Mic Boost (+20dB)", AC97_MIC, 6, 1, 0);
  562. static const char* std_rec_sel[] = {"Mic", "CD", "Video", "Aux", "Line", "Mix", "Mix Mono", "Phone"};
  563. static const char* std_3d_path[] = {"pre 3D", "post 3D"};
  564. static const char* std_mix[] = {"Mix", "Mic"};
  565. static const char* std_mic[] = {"Mic1", "Mic2"};
  566. static const struct ac97_enum std_enum[] = {
  567. AC97_ENUM_DOUBLE(AC97_REC_SEL, 8, 0, 8, std_rec_sel),
  568. AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, std_3d_path),
  569. AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 9, 2, std_mix),
  570. AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 8, 2, std_mic),
  571. };
  572. static const snd_kcontrol_new_t snd_ac97_control_capture_src =
  573. AC97_ENUM("Capture Source", std_enum[0]);
  574. static const snd_kcontrol_new_t snd_ac97_control_capture_vol =
  575. AC97_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0);
  576. static const snd_kcontrol_new_t snd_ac97_controls_mic_capture[2] = {
  577. AC97_SINGLE("Mic Capture Switch", AC97_REC_GAIN_MIC, 15, 1, 1),
  578. AC97_SINGLE("Mic Capture Volume", AC97_REC_GAIN_MIC, 0, 15, 0)
  579. };
  580. typedef enum {
  581. AC97_GENERAL_PCM_OUT = 0,
  582. AC97_GENERAL_STEREO_ENHANCEMENT,
  583. AC97_GENERAL_3D,
  584. AC97_GENERAL_LOUDNESS,
  585. AC97_GENERAL_MONO,
  586. AC97_GENERAL_MIC,
  587. AC97_GENERAL_LOOPBACK
  588. } ac97_general_index_t;
  589. static const snd_kcontrol_new_t snd_ac97_controls_general[7] = {
  590. AC97_ENUM("PCM Out Path & Mute", std_enum[1]),
  591. AC97_SINGLE("Simulated Stereo Enhancement", AC97_GENERAL_PURPOSE, 14, 1, 0),
  592. AC97_SINGLE("3D Control - Switch", AC97_GENERAL_PURPOSE, 13, 1, 0),
  593. AC97_SINGLE("Loudness (bass boost)", AC97_GENERAL_PURPOSE, 12, 1, 0),
  594. AC97_ENUM("Mono Output Select", std_enum[2]),
  595. AC97_ENUM("Mic Select", std_enum[3]),
  596. AC97_SINGLE("ADC/DAC Loopback", AC97_GENERAL_PURPOSE, 7, 1, 0)
  597. };
  598. const snd_kcontrol_new_t snd_ac97_controls_3d[2] = {
  599. AC97_SINGLE("3D Control - Center", AC97_3D_CONTROL, 8, 15, 0),
  600. AC97_SINGLE("3D Control - Depth", AC97_3D_CONTROL, 0, 15, 0)
  601. };
  602. static const snd_kcontrol_new_t snd_ac97_controls_center[2] = {
  603. AC97_SINGLE("Center Playback Switch", AC97_CENTER_LFE_MASTER, 7, 1, 1),
  604. AC97_SINGLE("Center Playback Volume", AC97_CENTER_LFE_MASTER, 0, 31, 1)
  605. };
  606. static const snd_kcontrol_new_t snd_ac97_controls_lfe[2] = {
  607. AC97_SINGLE("LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 1, 1),
  608. AC97_SINGLE("LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 31, 1)
  609. };
  610. static const snd_kcontrol_new_t snd_ac97_control_eapd =
  611. AC97_SINGLE("External Amplifier", AC97_POWERDOWN, 15, 1, 1);
  612. static const snd_kcontrol_new_t snd_ac97_controls_modem_switches[2] = {
  613. AC97_SINGLE("Off-hook Switch", AC97_GPIO_STATUS, 0, 1, 0),
  614. AC97_SINGLE("Caller ID Switch", AC97_GPIO_STATUS, 2, 1, 0)
  615. };
  616. /* change the existing EAPD control as inverted */
  617. static void set_inv_eapd(ac97_t *ac97, snd_kcontrol_t *kctl)
  618. {
  619. kctl->private_value = AC97_SINGLE_VALUE(AC97_POWERDOWN, 15, 1, 0);
  620. snd_ac97_update_bits(ac97, AC97_POWERDOWN, (1<<15), (1<<15)); /* EAPD up */
  621. ac97->scaps |= AC97_SCAP_INV_EAPD;
  622. }
  623. static int snd_ac97_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  624. {
  625. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  626. uinfo->count = 1;
  627. return 0;
  628. }
  629. static int snd_ac97_spdif_cmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
  630. {
  631. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  632. IEC958_AES0_NONAUDIO |
  633. IEC958_AES0_CON_EMPHASIS_5015 |
  634. IEC958_AES0_CON_NOT_COPYRIGHT;
  635. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  636. IEC958_AES1_CON_ORIGINAL;
  637. ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
  638. return 0;
  639. }
  640. static int snd_ac97_spdif_pmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
  641. {
  642. /* FIXME: AC'97 spec doesn't say which bits are used for what */
  643. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  644. IEC958_AES0_NONAUDIO |
  645. IEC958_AES0_PRO_FS |
  646. IEC958_AES0_PRO_EMPHASIS_5015;
  647. return 0;
  648. }
  649. static int snd_ac97_spdif_default_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
  650. {
  651. ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
  652. down(&ac97->reg_mutex);
  653. ucontrol->value.iec958.status[0] = ac97->spdif_status & 0xff;
  654. ucontrol->value.iec958.status[1] = (ac97->spdif_status >> 8) & 0xff;
  655. ucontrol->value.iec958.status[2] = (ac97->spdif_status >> 16) & 0xff;
  656. ucontrol->value.iec958.status[3] = (ac97->spdif_status >> 24) & 0xff;
  657. up(&ac97->reg_mutex);
  658. return 0;
  659. }
  660. static int snd_ac97_spdif_default_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
  661. {
  662. ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
  663. unsigned int new = 0;
  664. unsigned short val = 0;
  665. int change;
  666. new = val = ucontrol->value.iec958.status[0] & (IEC958_AES0_PROFESSIONAL|IEC958_AES0_NONAUDIO);
  667. if (ucontrol->value.iec958.status[0] & IEC958_AES0_PROFESSIONAL) {
  668. new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_PRO_FS|IEC958_AES0_PRO_EMPHASIS_5015);
  669. switch (new & IEC958_AES0_PRO_FS) {
  670. case IEC958_AES0_PRO_FS_44100: val |= 0<<12; break;
  671. case IEC958_AES0_PRO_FS_48000: val |= 2<<12; break;
  672. case IEC958_AES0_PRO_FS_32000: val |= 3<<12; break;
  673. default: val |= 1<<12; break;
  674. }
  675. if ((new & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
  676. val |= 1<<3;
  677. } else {
  678. new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT);
  679. new |= ((ucontrol->value.iec958.status[1] & (IEC958_AES1_CON_CATEGORY|IEC958_AES1_CON_ORIGINAL)) << 8);
  680. new |= ((ucontrol->value.iec958.status[3] & IEC958_AES3_CON_FS) << 24);
  681. if ((new & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
  682. val |= 1<<3;
  683. if (!(new & IEC958_AES0_CON_NOT_COPYRIGHT))
  684. val |= 1<<2;
  685. val |= ((new >> 8) & 0xff) << 4; // category + original
  686. switch ((new >> 24) & 0xff) {
  687. case IEC958_AES3_CON_FS_44100: val |= 0<<12; break;
  688. case IEC958_AES3_CON_FS_48000: val |= 2<<12; break;
  689. case IEC958_AES3_CON_FS_32000: val |= 3<<12; break;
  690. default: val |= 1<<12; break;
  691. }
  692. }
  693. down(&ac97->reg_mutex);
  694. change = ac97->spdif_status != new;
  695. ac97->spdif_status = new;
  696. if (ac97->flags & AC97_CS_SPDIF) {
  697. int x = (val >> 12) & 0x03;
  698. switch (x) {
  699. case 0: x = 1; break; // 44.1
  700. case 2: x = 0; break; // 48.0
  701. default: x = 0; break; // illegal.
  702. }
  703. change |= snd_ac97_update_bits_nolock(ac97, AC97_CSR_SPDIF, 0x3fff, ((val & 0xcfff) | (x << 12)));
  704. } else if (ac97->flags & AC97_CX_SPDIF) {
  705. int v;
  706. v = new & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT) ? 0 : AC97_CXR_COPYRGT;
  707. v |= new & IEC958_AES0_NONAUDIO ? AC97_CXR_SPDIF_AC3 : AC97_CXR_SPDIF_PCM;
  708. change |= snd_ac97_update_bits_nolock(ac97, AC97_CXR_AUDIO_MISC,
  709. AC97_CXR_SPDIF_MASK | AC97_CXR_COPYRGT,
  710. v);
  711. } else {
  712. unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
  713. snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
  714. change |= snd_ac97_update_bits_nolock(ac97, AC97_SPDIF, 0x3fff, val);
  715. if (extst & AC97_EA_SPDIF) {
  716. snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
  717. }
  718. }
  719. up(&ac97->reg_mutex);
  720. return change;
  721. }
  722. static int snd_ac97_put_spsa(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  723. {
  724. ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
  725. int reg = kcontrol->private_value & 0xff;
  726. int shift = (kcontrol->private_value >> 8) & 0xff;
  727. int mask = (kcontrol->private_value >> 16) & 0xff;
  728. // int invert = (kcontrol->private_value >> 24) & 0xff;
  729. unsigned short value, old, new;
  730. int change;
  731. value = (ucontrol->value.integer.value[0] & mask);
  732. down(&ac97->reg_mutex);
  733. mask <<= shift;
  734. value <<= shift;
  735. old = snd_ac97_read_cache(ac97, reg);
  736. new = (old & ~mask) | value;
  737. change = old != new;
  738. if (change) {
  739. unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
  740. snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
  741. change = snd_ac97_update_bits_nolock(ac97, reg, mask, value);
  742. if (extst & AC97_EA_SPDIF)
  743. snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
  744. }
  745. up(&ac97->reg_mutex);
  746. return change;
  747. }
  748. const snd_kcontrol_new_t snd_ac97_controls_spdif[5] = {
  749. {
  750. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  751. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  752. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  753. .info = snd_ac97_spdif_mask_info,
  754. .get = snd_ac97_spdif_cmask_get,
  755. },
  756. {
  757. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  758. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  759. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  760. .info = snd_ac97_spdif_mask_info,
  761. .get = snd_ac97_spdif_pmask_get,
  762. },
  763. {
  764. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  765. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  766. .info = snd_ac97_spdif_mask_info,
  767. .get = snd_ac97_spdif_default_get,
  768. .put = snd_ac97_spdif_default_put,
  769. },
  770. AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),AC97_EXTENDED_STATUS, 2, 1, 0),
  771. {
  772. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  773. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "AC97-SPSA",
  774. .info = snd_ac97_info_volsw,
  775. .get = snd_ac97_get_volsw,
  776. .put = snd_ac97_put_spsa,
  777. .private_value = AC97_SINGLE_VALUE(AC97_EXTENDED_STATUS, 4, 3, 0)
  778. },
  779. };
  780. #define AD18XX_PCM_BITS(xname, codec, lshift, rshift, mask) \
  781. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_bits, \
  782. .get = snd_ac97_ad18xx_pcm_get_bits, .put = snd_ac97_ad18xx_pcm_put_bits, \
  783. .private_value = (codec) | ((lshift) << 8) | ((rshift) << 12) | ((mask) << 16) }
  784. static int snd_ac97_ad18xx_pcm_info_bits(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  785. {
  786. ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
  787. int mask = (kcontrol->private_value >> 16) & 0x0f;
  788. int lshift = (kcontrol->private_value >> 8) & 0x0f;
  789. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  790. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  791. if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
  792. uinfo->count = 2;
  793. else
  794. uinfo->count = 1;
  795. uinfo->value.integer.min = 0;
  796. uinfo->value.integer.max = mask;
  797. return 0;
  798. }
  799. static int snd_ac97_ad18xx_pcm_get_bits(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  800. {
  801. ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
  802. int codec = kcontrol->private_value & 3;
  803. int lshift = (kcontrol->private_value >> 8) & 0x0f;
  804. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  805. int mask = (kcontrol->private_value >> 16) & 0xff;
  806. ucontrol->value.integer.value[0] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> lshift) & mask);
  807. if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
  808. ucontrol->value.integer.value[1] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> rshift) & mask);
  809. return 0;
  810. }
  811. static int snd_ac97_ad18xx_pcm_put_bits(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  812. {
  813. ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
  814. int codec = kcontrol->private_value & 3;
  815. int lshift = (kcontrol->private_value >> 8) & 0x0f;
  816. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  817. int mask = (kcontrol->private_value >> 16) & 0xff;
  818. unsigned short val, valmask;
  819. val = (mask - (ucontrol->value.integer.value[0] & mask)) << lshift;
  820. valmask = mask << lshift;
  821. if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES)) {
  822. val |= (mask - (ucontrol->value.integer.value[1] & mask)) << rshift;
  823. valmask |= mask << rshift;
  824. }
  825. return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, valmask, val);
  826. }
  827. #define AD18XX_PCM_VOLUME(xname, codec) \
  828. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_volume, \
  829. .get = snd_ac97_ad18xx_pcm_get_volume, .put = snd_ac97_ad18xx_pcm_put_volume, \
  830. .private_value = codec }
  831. static int snd_ac97_ad18xx_pcm_info_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  832. {
  833. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  834. uinfo->count = 2;
  835. uinfo->value.integer.min = 0;
  836. uinfo->value.integer.max = 31;
  837. return 0;
  838. }
  839. static int snd_ac97_ad18xx_pcm_get_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  840. {
  841. ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
  842. int codec = kcontrol->private_value & 3;
  843. down(&ac97->page_mutex);
  844. ucontrol->value.integer.value[0] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 0) & 31);
  845. ucontrol->value.integer.value[1] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 8) & 31);
  846. up(&ac97->page_mutex);
  847. return 0;
  848. }
  849. static int snd_ac97_ad18xx_pcm_put_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  850. {
  851. ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
  852. int codec = kcontrol->private_value & 3;
  853. unsigned short val1, val2;
  854. val1 = 31 - (ucontrol->value.integer.value[0] & 31);
  855. val2 = 31 - (ucontrol->value.integer.value[1] & 31);
  856. return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, 0x1f1f, (val1 << 8) | val2);
  857. }
  858. static const snd_kcontrol_new_t snd_ac97_controls_ad18xx_pcm[2] = {
  859. AD18XX_PCM_BITS("PCM Playback Switch", 0, 15, 7, 1),
  860. AD18XX_PCM_VOLUME("PCM Playback Volume", 0)
  861. };
  862. static const snd_kcontrol_new_t snd_ac97_controls_ad18xx_surround[2] = {
  863. AD18XX_PCM_BITS("Surround Playback Switch", 1, 15, 7, 1),
  864. AD18XX_PCM_VOLUME("Surround Playback Volume", 1)
  865. };
  866. static const snd_kcontrol_new_t snd_ac97_controls_ad18xx_center[2] = {
  867. AD18XX_PCM_BITS("Center Playback Switch", 2, 15, 15, 1),
  868. AD18XX_PCM_BITS("Center Playback Volume", 2, 8, 8, 31)
  869. };
  870. static const snd_kcontrol_new_t snd_ac97_controls_ad18xx_lfe[2] = {
  871. AD18XX_PCM_BITS("LFE Playback Switch", 2, 7, 7, 1),
  872. AD18XX_PCM_BITS("LFE Playback Volume", 2, 0, 0, 31)
  873. };
  874. /*
  875. *
  876. */
  877. static void snd_ac97_powerdown(ac97_t *ac97);
  878. static int snd_ac97_bus_free(ac97_bus_t *bus)
  879. {
  880. if (bus) {
  881. snd_ac97_bus_proc_done(bus);
  882. kfree(bus->pcms);
  883. if (bus->private_free)
  884. bus->private_free(bus);
  885. kfree(bus);
  886. }
  887. return 0;
  888. }
  889. static int snd_ac97_bus_dev_free(snd_device_t *device)
  890. {
  891. ac97_bus_t *bus = device->device_data;
  892. return snd_ac97_bus_free(bus);
  893. }
  894. static int snd_ac97_free(ac97_t *ac97)
  895. {
  896. if (ac97) {
  897. snd_ac97_proc_done(ac97);
  898. if (ac97->bus) {
  899. ac97->bus->codec[ac97->num] = NULL;
  900. if (ac97->bus->shared_type) {
  901. down(&shared_codec_mutex);
  902. shared_codec[ac97->bus->shared_type-1][ac97->num] = NULL;
  903. up(&shared_codec_mutex);
  904. }
  905. }
  906. if (ac97->private_free)
  907. ac97->private_free(ac97);
  908. kfree(ac97);
  909. }
  910. return 0;
  911. }
  912. static int snd_ac97_dev_free(snd_device_t *device)
  913. {
  914. ac97_t *ac97 = device->device_data;
  915. snd_ac97_powerdown(ac97); /* for avoiding click noises during shut down */
  916. return snd_ac97_free(ac97);
  917. }
  918. static int snd_ac97_try_volume_mix(ac97_t * ac97, int reg)
  919. {
  920. unsigned short val, mask = 0x8000;
  921. if (! snd_ac97_valid_reg(ac97, reg))
  922. return 0;
  923. switch (reg) {
  924. case AC97_MASTER_TONE:
  925. return ac97->caps & 0x04 ? 1 : 0;
  926. case AC97_HEADPHONE:
  927. return ac97->caps & 0x10 ? 1 : 0;
  928. case AC97_REC_GAIN_MIC:
  929. return ac97->caps & 0x01 ? 1 : 0;
  930. case AC97_3D_CONTROL:
  931. if (ac97->caps & 0x7c00) {
  932. val = snd_ac97_read(ac97, reg);
  933. /* if nonzero - fixed and we can't set it */
  934. return val == 0;
  935. }
  936. return 0;
  937. case AC97_CENTER_LFE_MASTER: /* center */
  938. if ((ac97->ext_id & AC97_EI_CDAC) == 0)
  939. return 0;
  940. break;
  941. case AC97_CENTER_LFE_MASTER+1: /* lfe */
  942. if ((ac97->ext_id & AC97_EI_LDAC) == 0)
  943. return 0;
  944. reg = AC97_CENTER_LFE_MASTER;
  945. mask = 0x0080;
  946. break;
  947. case AC97_SURROUND_MASTER:
  948. if ((ac97->ext_id & AC97_EI_SDAC) == 0)
  949. return 0;
  950. break;
  951. }
  952. if (ac97->limited_regs && test_bit(reg, ac97->reg_accessed))
  953. return 1; /* allow without check */
  954. val = snd_ac97_read(ac97, reg);
  955. if (!(val & mask)) {
  956. /* nothing seems to be here - mute flag is not set */
  957. /* try another test */
  958. snd_ac97_write_cache(ac97, reg, val | mask);
  959. val = snd_ac97_read(ac97, reg);
  960. if (!(val & mask))
  961. return 0; /* nothing here */
  962. }
  963. return 1; /* success, useable */
  964. }
  965. static void check_volume_resolution(ac97_t *ac97, int reg, unsigned char *lo_max, unsigned char *hi_max)
  966. {
  967. unsigned short cbit[3] = { 0x20, 0x10, 0x01 };
  968. unsigned char max[3] = { 63, 31, 15 };
  969. int i;
  970. *lo_max = *hi_max = 0;
  971. for (i = 0 ; i < ARRAY_SIZE(cbit); i++) {
  972. unsigned short val;
  973. snd_ac97_write(ac97, reg, 0x8080 | cbit[i] | (cbit[i] << 8));
  974. /* Do the read twice due to buffers on some ac97 codecs.
  975. * e.g. The STAC9704 returns exactly what you wrote the the register
  976. * if you read it immediately. This causes the detect routine to fail.
  977. */
  978. val = snd_ac97_read(ac97, reg);
  979. val = snd_ac97_read(ac97, reg);
  980. if (! *lo_max && (val & 0x7f) == cbit[i])
  981. *lo_max = max[i];
  982. if (! *hi_max && ((val >> 8) & 0x7f) == cbit[i])
  983. *hi_max = max[i];
  984. if (*lo_max && *hi_max)
  985. break;
  986. }
  987. }
  988. int snd_ac97_try_bit(ac97_t * ac97, int reg, int bit)
  989. {
  990. unsigned short mask, val, orig, res;
  991. mask = 1 << bit;
  992. orig = snd_ac97_read(ac97, reg);
  993. val = orig ^ mask;
  994. snd_ac97_write(ac97, reg, val);
  995. res = snd_ac97_read(ac97, reg);
  996. snd_ac97_write_cache(ac97, reg, orig);
  997. return res == val;
  998. }
  999. /* check the volume resolution of center/lfe */
  1000. static void snd_ac97_change_volume_params2(ac97_t * ac97, int reg, int shift, unsigned char *max)
  1001. {
  1002. unsigned short val, val1;
  1003. *max = 63;
  1004. val = 0x8080 | (0x20 << shift);
  1005. snd_ac97_write(ac97, reg, val);
  1006. val1 = snd_ac97_read(ac97, reg);
  1007. if (val != val1) {
  1008. *max = 31;
  1009. }
  1010. /* reset volume to zero */
  1011. snd_ac97_write_cache(ac97, reg, 0x8080);
  1012. }
  1013. static inline int printable(unsigned int x)
  1014. {
  1015. x &= 0xff;
  1016. if (x < ' ' || x >= 0x71) {
  1017. if (x <= 0x89)
  1018. return x - 0x71 + 'A';
  1019. return '?';
  1020. }
  1021. return x;
  1022. }
  1023. snd_kcontrol_t *snd_ac97_cnew(const snd_kcontrol_new_t *_template, ac97_t * ac97)
  1024. {
  1025. snd_kcontrol_new_t template;
  1026. memcpy(&template, _template, sizeof(template));
  1027. snd_runtime_check(!template.index, return NULL);
  1028. template.index = ac97->num;
  1029. return snd_ctl_new1(&template, ac97);
  1030. }
  1031. /*
  1032. * create mute switch(es) for normal stereo controls
  1033. */
  1034. static int snd_ac97_cmute_new_stereo(snd_card_t *card, char *name, int reg, int check_stereo, ac97_t *ac97)
  1035. {
  1036. snd_kcontrol_t *kctl;
  1037. int err;
  1038. unsigned short val, val1, mute_mask;
  1039. if (! snd_ac97_valid_reg(ac97, reg))
  1040. return 0;
  1041. mute_mask = 0x8000;
  1042. val = snd_ac97_read(ac97, reg);
  1043. if (check_stereo || (ac97->flags & AC97_STEREO_MUTES)) {
  1044. /* check whether both mute bits work */
  1045. val1 = val | 0x8080;
  1046. snd_ac97_write(ac97, reg, val1);
  1047. if (val1 == snd_ac97_read(ac97, reg))
  1048. mute_mask = 0x8080;
  1049. }
  1050. if (mute_mask == 0x8080) {
  1051. snd_kcontrol_new_t tmp = AC97_DOUBLE(name, reg, 15, 7, 1, 1);
  1052. tmp.index = ac97->num;
  1053. kctl = snd_ctl_new1(&tmp, ac97);
  1054. } else {
  1055. snd_kcontrol_new_t tmp = AC97_SINGLE(name, reg, 15, 1, 1);
  1056. tmp.index = ac97->num;
  1057. kctl = snd_ctl_new1(&tmp, ac97);
  1058. }
  1059. err = snd_ctl_add(card, kctl);
  1060. if (err < 0)
  1061. return err;
  1062. /* mute as default */
  1063. snd_ac97_write_cache(ac97, reg, val | mute_mask);
  1064. return 0;
  1065. }
  1066. /*
  1067. * create a volume for normal stereo/mono controls
  1068. */
  1069. static int snd_ac97_cvol_new(snd_card_t *card, char *name, int reg, unsigned int lo_max,
  1070. unsigned int hi_max, ac97_t *ac97)
  1071. {
  1072. int err;
  1073. snd_kcontrol_t *kctl;
  1074. if (! snd_ac97_valid_reg(ac97, reg))
  1075. return 0;
  1076. if (hi_max) {
  1077. /* invert */
  1078. snd_kcontrol_new_t tmp = AC97_DOUBLE(name, reg, 8, 0, lo_max, 1);
  1079. tmp.index = ac97->num;
  1080. kctl = snd_ctl_new1(&tmp, ac97);
  1081. } else {
  1082. /* invert */
  1083. snd_kcontrol_new_t tmp = AC97_SINGLE(name, reg, 0, lo_max, 1);
  1084. tmp.index = ac97->num;
  1085. kctl = snd_ctl_new1(&tmp, ac97);
  1086. }
  1087. err = snd_ctl_add(card, kctl);
  1088. if (err < 0)
  1089. return err;
  1090. snd_ac97_write_cache(ac97, reg,
  1091. (snd_ac97_read(ac97, reg) & 0x8080) |
  1092. lo_max | (hi_max << 8));
  1093. return 0;
  1094. }
  1095. /*
  1096. * create a mute-switch and a volume for normal stereo/mono controls
  1097. */
  1098. static int snd_ac97_cmix_new_stereo(snd_card_t *card, const char *pfx, int reg, int check_stereo, ac97_t *ac97)
  1099. {
  1100. int err;
  1101. char name[44];
  1102. unsigned char lo_max, hi_max;
  1103. if (! snd_ac97_valid_reg(ac97, reg))
  1104. return 0;
  1105. if (snd_ac97_try_bit(ac97, reg, 15)) {
  1106. sprintf(name, "%s Switch", pfx);
  1107. if ((err = snd_ac97_cmute_new_stereo(card, name, reg, check_stereo, ac97)) < 0)
  1108. return err;
  1109. }
  1110. check_volume_resolution(ac97, reg, &lo_max, &hi_max);
  1111. if (lo_max) {
  1112. sprintf(name, "%s Volume", pfx);
  1113. if ((err = snd_ac97_cvol_new(card, name, reg, lo_max, hi_max, ac97)) < 0)
  1114. return err;
  1115. }
  1116. return 0;
  1117. }
  1118. #define snd_ac97_cmix_new(card, pfx, reg, ac97) snd_ac97_cmix_new_stereo(card, pfx, reg, 0, ac97)
  1119. #define snd_ac97_cmute_new(card, name, reg, ac97) snd_ac97_cmute_new_stereo(card, name, reg, 0, ac97)
  1120. static unsigned int snd_ac97_determine_spdif_rates(ac97_t *ac97);
  1121. static int snd_ac97_mixer_build(ac97_t * ac97)
  1122. {
  1123. snd_card_t *card = ac97->bus->card;
  1124. snd_kcontrol_t *kctl;
  1125. int err;
  1126. unsigned int idx;
  1127. unsigned char max;
  1128. /* build master controls */
  1129. /* AD claims to remove this control from AD1887, although spec v2.2 does not allow this */
  1130. if (snd_ac97_try_volume_mix(ac97, AC97_MASTER)) {
  1131. if (ac97->flags & AC97_HAS_NO_MASTER_VOL)
  1132. err = snd_ac97_cmute_new(card, "Master Playback Switch", AC97_MASTER, ac97);
  1133. else
  1134. err = snd_ac97_cmix_new(card, "Master Playback", AC97_MASTER, ac97);
  1135. if (err < 0)
  1136. return err;
  1137. }
  1138. ac97->regs[AC97_CENTER_LFE_MASTER] = 0x8080;
  1139. /* build center controls */
  1140. if (snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER)) {
  1141. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_center[0], ac97))) < 0)
  1142. return err;
  1143. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_center[1], ac97))) < 0)
  1144. return err;
  1145. snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 0, &max);
  1146. kctl->private_value &= ~(0xff << 16);
  1147. kctl->private_value |= (int)max << 16;
  1148. snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max);
  1149. }
  1150. /* build LFE controls */
  1151. if (snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER+1)) {
  1152. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_lfe[0], ac97))) < 0)
  1153. return err;
  1154. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_lfe[1], ac97))) < 0)
  1155. return err;
  1156. snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 8, &max);
  1157. kctl->private_value &= ~(0xff << 16);
  1158. kctl->private_value |= (int)max << 16;
  1159. snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max << 8);
  1160. }
  1161. /* build surround controls */
  1162. if (snd_ac97_try_volume_mix(ac97, AC97_SURROUND_MASTER)) {
  1163. /* Surround Master (0x38) is with stereo mutes */
  1164. if ((err = snd_ac97_cmix_new_stereo(card, "Surround Playback", AC97_SURROUND_MASTER, 1, ac97)) < 0)
  1165. return err;
  1166. }
  1167. /* build headphone controls */
  1168. if (snd_ac97_try_volume_mix(ac97, AC97_HEADPHONE)) {
  1169. if ((err = snd_ac97_cmix_new(card, "Headphone Playback", AC97_HEADPHONE, ac97)) < 0)
  1170. return err;
  1171. }
  1172. /* build master mono controls */
  1173. if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_MONO)) {
  1174. if ((err = snd_ac97_cmix_new(card, "Master Mono Playback", AC97_MASTER_MONO, ac97)) < 0)
  1175. return err;
  1176. }
  1177. /* build master tone controls */
  1178. if (!(ac97->flags & AC97_HAS_NO_TONE)) {
  1179. if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_TONE)) {
  1180. for (idx = 0; idx < 2; idx++) {
  1181. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_tone[idx], ac97))) < 0)
  1182. return err;
  1183. if (ac97->id == AC97_ID_YMF753) {
  1184. kctl->private_value &= ~(0xff << 16);
  1185. kctl->private_value |= 7 << 16;
  1186. }
  1187. }
  1188. snd_ac97_write_cache(ac97, AC97_MASTER_TONE, 0x0f0f);
  1189. }
  1190. }
  1191. /* build PC Speaker controls */
  1192. if (!(ac97->flags & AC97_HAS_NO_PC_BEEP) &&
  1193. ((ac97->flags & AC97_HAS_PC_BEEP) ||
  1194. snd_ac97_try_volume_mix(ac97, AC97_PC_BEEP))) {
  1195. for (idx = 0; idx < 2; idx++)
  1196. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_pc_beep[idx], ac97))) < 0)
  1197. return err;
  1198. snd_ac97_write_cache(ac97, AC97_PC_BEEP,
  1199. snd_ac97_read(ac97, AC97_PC_BEEP) | 0x801e);
  1200. }
  1201. /* build Phone controls */
  1202. if (!(ac97->flags & AC97_HAS_NO_PHONE)) {
  1203. if (snd_ac97_try_volume_mix(ac97, AC97_PHONE)) {
  1204. if ((err = snd_ac97_cmix_new(card, "Phone Playback", AC97_PHONE, ac97)) < 0)
  1205. return err;
  1206. }
  1207. }
  1208. /* build MIC controls */
  1209. if (!(ac97->flags & AC97_HAS_NO_MIC)) {
  1210. if (snd_ac97_try_volume_mix(ac97, AC97_MIC)) {
  1211. if ((err = snd_ac97_cmix_new(card, "Mic Playback", AC97_MIC, ac97)) < 0)
  1212. return err;
  1213. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_mic_boost, ac97))) < 0)
  1214. return err;
  1215. }
  1216. }
  1217. /* build Line controls */
  1218. if (snd_ac97_try_volume_mix(ac97, AC97_LINE)) {
  1219. if ((err = snd_ac97_cmix_new(card, "Line Playback", AC97_LINE, ac97)) < 0)
  1220. return err;
  1221. }
  1222. /* build CD controls */
  1223. if (!(ac97->flags & AC97_HAS_NO_CD)) {
  1224. if (snd_ac97_try_volume_mix(ac97, AC97_CD)) {
  1225. if ((err = snd_ac97_cmix_new(card, "CD Playback", AC97_CD, ac97)) < 0)
  1226. return err;
  1227. }
  1228. }
  1229. /* build Video controls */
  1230. if (!(ac97->flags & AC97_HAS_NO_VIDEO)) {
  1231. if (snd_ac97_try_volume_mix(ac97, AC97_VIDEO)) {
  1232. if ((err = snd_ac97_cmix_new(card, "Video Playback", AC97_VIDEO, ac97)) < 0)
  1233. return err;
  1234. }
  1235. }
  1236. /* build Aux controls */
  1237. if (snd_ac97_try_volume_mix(ac97, AC97_AUX)) {
  1238. if ((err = snd_ac97_cmix_new(card, "Aux Playback", AC97_AUX, ac97)) < 0)
  1239. return err;
  1240. }
  1241. /* build PCM controls */
  1242. if (ac97->flags & AC97_AD_MULTI) {
  1243. unsigned short init_val;
  1244. if (ac97->flags & AC97_STEREO_MUTES)
  1245. init_val = 0x9f9f;
  1246. else
  1247. init_val = 0x9f1f;
  1248. for (idx = 0; idx < 2; idx++)
  1249. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_pcm[idx], ac97))) < 0)
  1250. return err;
  1251. ac97->spec.ad18xx.pcmreg[0] = init_val;
  1252. if (ac97->scaps & AC97_SCAP_SURROUND_DAC) {
  1253. for (idx = 0; idx < 2; idx++)
  1254. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_surround[idx], ac97))) < 0)
  1255. return err;
  1256. ac97->spec.ad18xx.pcmreg[1] = init_val;
  1257. }
  1258. if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC) {
  1259. for (idx = 0; idx < 2; idx++)
  1260. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_center[idx], ac97))) < 0)
  1261. return err;
  1262. for (idx = 0; idx < 2; idx++)
  1263. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_lfe[idx], ac97))) < 0)
  1264. return err;
  1265. ac97->spec.ad18xx.pcmreg[2] = init_val;
  1266. }
  1267. snd_ac97_write_cache(ac97, AC97_PCM, init_val);
  1268. } else {
  1269. if (!(ac97->flags & AC97_HAS_NO_STD_PCM)) {
  1270. if (ac97->flags & AC97_HAS_NO_PCM_VOL)
  1271. err = snd_ac97_cmute_new(card, "PCM Playback Switch", AC97_PCM, ac97);
  1272. else
  1273. err = snd_ac97_cmix_new(card, "PCM Playback", AC97_PCM, ac97);
  1274. if (err < 0)
  1275. return err;
  1276. }
  1277. }
  1278. /* build Capture controls */
  1279. if (!(ac97->flags & AC97_HAS_NO_REC_GAIN)) {
  1280. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_control_capture_src, ac97))) < 0)
  1281. return err;
  1282. if (snd_ac97_try_bit(ac97, AC97_REC_GAIN, 15)) {
  1283. if ((err = snd_ac97_cmute_new(card, "Capture Switch", AC97_REC_GAIN, ac97)) < 0)
  1284. return err;
  1285. }
  1286. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_control_capture_vol, ac97))) < 0)
  1287. return err;
  1288. snd_ac97_write_cache(ac97, AC97_REC_SEL, 0x0000);
  1289. snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x0000);
  1290. }
  1291. /* build MIC Capture controls */
  1292. if (snd_ac97_try_volume_mix(ac97, AC97_REC_GAIN_MIC)) {
  1293. for (idx = 0; idx < 2; idx++)
  1294. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_mic_capture[idx], ac97))) < 0)
  1295. return err;
  1296. snd_ac97_write_cache(ac97, AC97_REC_GAIN_MIC, 0x0000);
  1297. }
  1298. /* build PCM out path & mute control */
  1299. if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 15)) {
  1300. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_PCM_OUT], ac97))) < 0)
  1301. return err;
  1302. }
  1303. /* build Simulated Stereo Enhancement control */
  1304. if (ac97->caps & 0x0008) {
  1305. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_STEREO_ENHANCEMENT], ac97))) < 0)
  1306. return err;
  1307. }
  1308. /* build 3D Stereo Enhancement control */
  1309. if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 13)) {
  1310. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_3D], ac97))) < 0)
  1311. return err;
  1312. }
  1313. /* build Loudness control */
  1314. if (ac97->caps & 0x0020) {
  1315. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOUDNESS], ac97))) < 0)
  1316. return err;
  1317. }
  1318. /* build Mono output select control */
  1319. if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 9)) {
  1320. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MONO], ac97))) < 0)
  1321. return err;
  1322. }
  1323. /* build Mic select control */
  1324. if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 8)) {
  1325. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MIC], ac97))) < 0)
  1326. return err;
  1327. }
  1328. /* build ADC/DAC loopback control */
  1329. if (enable_loopback && snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 7)) {
  1330. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOOPBACK], ac97))) < 0)
  1331. return err;
  1332. }
  1333. snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, ~AC97_GP_DRSS_MASK, 0x0000);
  1334. /* build 3D controls */
  1335. if (ac97->build_ops->build_3d) {
  1336. ac97->build_ops->build_3d(ac97);
  1337. } else {
  1338. if (snd_ac97_try_volume_mix(ac97, AC97_3D_CONTROL)) {
  1339. unsigned short val;
  1340. val = 0x0707;
  1341. snd_ac97_write(ac97, AC97_3D_CONTROL, val);
  1342. val = snd_ac97_read(ac97, AC97_3D_CONTROL);
  1343. val = val == 0x0606;
  1344. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
  1345. return err;
  1346. if (val)
  1347. kctl->private_value = AC97_3D_CONTROL | (9 << 8) | (7 << 16);
  1348. if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[1], ac97))) < 0)
  1349. return err;
  1350. if (val)
  1351. kctl->private_value = AC97_3D_CONTROL | (1 << 8) | (7 << 16);
  1352. snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
  1353. }
  1354. }
  1355. /* build S/PDIF controls */
  1356. if ((ac97->ext_id & AC97_EI_SPDIF) && !(ac97->scaps & AC97_SCAP_NO_SPDIF)) {
  1357. if (ac97->build_ops->build_spdif) {
  1358. if ((err = ac97->build_ops->build_spdif(ac97)) < 0)
  1359. return err;
  1360. } else {
  1361. for (idx = 0; idx < 5; idx++)
  1362. if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_spdif[idx], ac97))) < 0)
  1363. return err;
  1364. if (ac97->build_ops->build_post_spdif) {
  1365. if ((err = ac97->build_ops->build_post_spdif(ac97)) < 0)
  1366. return err;
  1367. }
  1368. /* set default PCM S/PDIF params */
  1369. /* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */
  1370. snd_ac97_write_cache(ac97, AC97_SPDIF, 0x2a20);
  1371. ac97->rates[AC97_RATES_SPDIF] = snd_ac97_determine_spdif_rates(ac97);
  1372. }
  1373. ac97->spdif_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
  1374. }
  1375. /* build chip specific controls */
  1376. if (ac97->build_ops->build_specific)
  1377. if ((err = ac97->build_ops->build_specific(ac97)) < 0)
  1378. return err;
  1379. if (snd_ac97_try_bit(ac97, AC97_POWERDOWN, 15)) {
  1380. kctl = snd_ac97_cnew(&snd_ac97_control_eapd, ac97);
  1381. if (! kctl)
  1382. return -ENOMEM;
  1383. if (ac97->scaps & AC97_SCAP_INV_EAPD)
  1384. set_inv_eapd(ac97, kctl);
  1385. if ((err = snd_ctl_add(card, kctl)) < 0)
  1386. return err;
  1387. }
  1388. return 0;
  1389. }
  1390. static int snd_ac97_modem_build(snd_card_t * card, ac97_t * ac97)
  1391. {
  1392. int err, idx;
  1393. //printk("AC97_GPIO_CFG = %x\n",snd_ac97_read(ac97,AC97_GPIO_CFG));
  1394. snd_ac97_write(ac97, AC97_GPIO_CFG, 0xffff & ~(AC97_GPIO_LINE1_OH));
  1395. snd_ac97_write(ac97, AC97_GPIO_POLARITY, 0xffff & ~(AC97_GPIO_LINE1_OH));
  1396. snd_ac97_write(ac97, AC97_GPIO_STICKY, 0xffff);
  1397. snd_ac97_write(ac97, AC97_GPIO_WAKEUP, 0x0);
  1398. snd_ac97_write(ac97, AC97_MISC_AFE, 0x0);
  1399. /* build modem switches */
  1400. for (idx = 0; idx < ARRAY_SIZE(snd_ac97_controls_modem_switches); idx++)
  1401. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ac97_controls_modem_switches[idx], ac97))) < 0)
  1402. return err;
  1403. /* build chip specific controls */
  1404. if (ac97->build_ops->build_specific)
  1405. if ((err = ac97->build_ops->build_specific(ac97)) < 0)
  1406. return err;
  1407. return 0;
  1408. }
  1409. static int snd_ac97_test_rate(ac97_t *ac97, int reg, int shadow_reg, int rate)
  1410. {
  1411. unsigned short val;
  1412. unsigned int tmp;
  1413. tmp = ((unsigned int)rate * ac97->bus->clock) / 48000;
  1414. snd_ac97_write_cache(ac97, reg, tmp & 0xffff);
  1415. if (shadow_reg)
  1416. snd_ac97_write_cache(ac97, shadow_reg, tmp & 0xffff);
  1417. val = snd_ac97_read(ac97, reg);
  1418. return val == (tmp & 0xffff);
  1419. }
  1420. static void snd_ac97_determine_rates(ac97_t *ac97, int reg, int shadow_reg, unsigned int *r_result)
  1421. {
  1422. unsigned int result = 0;
  1423. unsigned short saved;
  1424. if (ac97->bus->no_vra) {
  1425. *r_result = SNDRV_PCM_RATE_48000;
  1426. if ((ac97->flags & AC97_DOUBLE_RATE) &&
  1427. reg == AC97_PCM_FRONT_DAC_RATE)
  1428. *r_result |= SNDRV_PCM_RATE_96000;
  1429. return;
  1430. }
  1431. saved = snd_ac97_read(ac97, reg);
  1432. if ((ac97->ext_id & AC97_EI_DRA) && reg == AC97_PCM_FRONT_DAC_RATE)
  1433. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
  1434. AC97_EA_DRA, 0);
  1435. /* test a non-standard rate */
  1436. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11000))
  1437. result |= SNDRV_PCM_RATE_CONTINUOUS;
  1438. /* let's try to obtain standard rates */
  1439. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 8000))
  1440. result |= SNDRV_PCM_RATE_8000;
  1441. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11025))
  1442. result |= SNDRV_PCM_RATE_11025;
  1443. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 16000))
  1444. result |= SNDRV_PCM_RATE_16000;
  1445. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 22050))
  1446. result |= SNDRV_PCM_RATE_22050;
  1447. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 32000))
  1448. result |= SNDRV_PCM_RATE_32000;
  1449. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 44100))
  1450. result |= SNDRV_PCM_RATE_44100;
  1451. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 48000))
  1452. result |= SNDRV_PCM_RATE_48000;
  1453. if ((ac97->flags & AC97_DOUBLE_RATE) &&
  1454. reg == AC97_PCM_FRONT_DAC_RATE) {
  1455. /* test standard double rates */
  1456. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
  1457. AC97_EA_DRA, AC97_EA_DRA);
  1458. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 64000 / 2))
  1459. result |= SNDRV_PCM_RATE_64000;
  1460. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 88200 / 2))
  1461. result |= SNDRV_PCM_RATE_88200;
  1462. if (snd_ac97_test_rate(ac97, reg, shadow_reg, 96000 / 2))
  1463. result |= SNDRV_PCM_RATE_96000;
  1464. /* some codecs don't support variable double rates */
  1465. if (!snd_ac97_test_rate(ac97, reg, shadow_reg, 76100 / 2))
  1466. result &= ~SNDRV_PCM_RATE_CONTINUOUS;
  1467. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
  1468. AC97_EA_DRA, 0);
  1469. }
  1470. /* restore the default value */
  1471. snd_ac97_write_cache(ac97, reg, saved);
  1472. if (shadow_reg)
  1473. snd_ac97_write_cache(ac97, shadow_reg, saved);
  1474. *r_result = result;
  1475. }
  1476. /* check AC97_SPDIF register to accept which sample rates */
  1477. static unsigned int snd_ac97_determine_spdif_rates(ac97_t *ac97)
  1478. {
  1479. unsigned int result = 0;
  1480. int i;
  1481. static unsigned short ctl_bits[] = {
  1482. AC97_SC_SPSR_44K, AC97_SC_SPSR_32K, AC97_SC_SPSR_48K
  1483. };
  1484. static unsigned int rate_bits[] = {
  1485. SNDRV_PCM_RATE_44100, SNDRV_PCM_RATE_32000, SNDRV_PCM_RATE_48000
  1486. };
  1487. for (i = 0; i < (int)ARRAY_SIZE(ctl_bits); i++) {
  1488. snd_ac97_update_bits(ac97, AC97_SPDIF, AC97_SC_SPSR_MASK, ctl_bits[i]);
  1489. if ((snd_ac97_read(ac97, AC97_SPDIF) & AC97_SC_SPSR_MASK) == ctl_bits[i])
  1490. result |= rate_bits[i];
  1491. }
  1492. return result;
  1493. }
  1494. /* look for the codec id table matching with the given id */
  1495. static const ac97_codec_id_t *look_for_codec_id(const ac97_codec_id_t *table,
  1496. unsigned int id)
  1497. {
  1498. const ac97_codec_id_t *pid;
  1499. for (pid = table; pid->id; pid++)
  1500. if (pid->id == (id & pid->mask))
  1501. return pid;
  1502. return NULL;
  1503. }
  1504. void snd_ac97_get_name(ac97_t *ac97, unsigned int id, char *name, int modem)
  1505. {
  1506. const ac97_codec_id_t *pid;
  1507. sprintf(name, "0x%x %c%c%c", id,
  1508. printable(id >> 24),
  1509. printable(id >> 16),
  1510. printable(id >> 8));
  1511. pid = look_for_codec_id(snd_ac97_codec_id_vendors, id);
  1512. if (! pid)
  1513. return;
  1514. strcpy(name, pid->name);
  1515. if (ac97 && pid->patch) {
  1516. if ((modem && (pid->flags & AC97_MODEM_PATCH)) ||
  1517. (! modem && ! (pid->flags & AC97_MODEM_PATCH)))
  1518. pid->patch(ac97);
  1519. }
  1520. pid = look_for_codec_id(snd_ac97_codec_ids, id);
  1521. if (pid) {
  1522. strcat(name, " ");
  1523. strcat(name, pid->name);
  1524. if (pid->mask != 0xffffffff)
  1525. sprintf(name + strlen(name), " rev %d", id & ~pid->mask);
  1526. if (ac97 && pid->patch) {
  1527. if ((modem && (pid->flags & AC97_MODEM_PATCH)) ||
  1528. (! modem && ! (pid->flags & AC97_MODEM_PATCH)))
  1529. pid->patch(ac97);
  1530. }
  1531. } else
  1532. sprintf(name + strlen(name), " id %x", id & 0xff);
  1533. }
  1534. /**
  1535. * snd_ac97_get_short_name - retrieve codec name
  1536. * @ac97: the codec instance
  1537. *
  1538. * Returns the short identifying name of the codec.
  1539. */
  1540. const char *snd_ac97_get_short_name(ac97_t *ac97)
  1541. {
  1542. const ac97_codec_id_t *pid;
  1543. for (pid = snd_ac97_codec_ids; pid->id; pid++)
  1544. if (pid->id == (ac97->id & pid->mask))
  1545. return pid->name;
  1546. return "unknown codec";
  1547. }
  1548. /* wait for a while until registers are accessible after RESET
  1549. * return 0 if ok, negative not ready
  1550. */
  1551. static int ac97_reset_wait(ac97_t *ac97, int timeout, int with_modem)
  1552. {
  1553. unsigned long end_time;
  1554. unsigned short val;
  1555. end_time = jiffies + timeout;
  1556. do {
  1557. /* use preliminary reads to settle the communication */
  1558. snd_ac97_read(ac97, AC97_RESET);
  1559. snd_ac97_read(ac97, AC97_VENDOR_ID1);
  1560. snd_ac97_read(ac97, AC97_VENDOR_ID2);
  1561. /* modem? */
  1562. if (with_modem) {
  1563. val = snd_ac97_read(ac97, AC97_EXTENDED_MID);
  1564. if (val != 0xffff && (val & 1) != 0)
  1565. return 0;
  1566. }
  1567. if (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) {
  1568. /* probably only Xbox issue - all registers are read as zero */
  1569. val = snd_ac97_read(ac97, AC97_VENDOR_ID1);
  1570. if (val != 0 && val != 0xffff)
  1571. return 0;
  1572. } else {
  1573. /* because the PCM or MASTER volume registers can be modified,
  1574. * the REC_GAIN register is used for tests
  1575. */
  1576. /* test if we can write to the record gain volume register */
  1577. snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a05);
  1578. if ((snd_ac97_read(ac97, AC97_REC_GAIN) & 0x7fff) == 0x0a05)
  1579. return 0;
  1580. }
  1581. set_current_state(TASK_UNINTERRUPTIBLE);
  1582. schedule_timeout(1);
  1583. } while (time_after_eq(end_time, jiffies));
  1584. return -ENODEV;
  1585. }
  1586. /**
  1587. * snd_ac97_bus - create an AC97 bus component
  1588. * @card: the card instance
  1589. * @num: the bus number
  1590. * @ops: the bus callbacks table
  1591. * @private_data: private data pointer for the new instance
  1592. * @rbus: the pointer to store the new AC97 bus instance.
  1593. *
  1594. * Creates an AC97 bus component. An ac97_bus_t instance is newly
  1595. * allocated and initialized.
  1596. *
  1597. * The ops table must include valid callbacks (at least read and
  1598. * write). The other callbacks, wait and reset, are not mandatory.
  1599. *
  1600. * The clock is set to 48000. If another clock is needed, set
  1601. * (*rbus)->clock manually.
  1602. *
  1603. * The AC97 bus instance is registered as a low-level device, so you don't
  1604. * have to release it manually.
  1605. *
  1606. * Returns zero if successful, or a negative error code on failure.
  1607. */
  1608. int snd_ac97_bus(snd_card_t *card, int num, ac97_bus_ops_t *ops,
  1609. void *private_data, ac97_bus_t **rbus)
  1610. {
  1611. int err;
  1612. ac97_bus_t *bus;
  1613. static snd_device_ops_t dev_ops = {
  1614. .dev_free = snd_ac97_bus_dev_free,
  1615. };
  1616. snd_assert(card != NULL, return -EINVAL);
  1617. snd_assert(rbus != NULL, return -EINVAL);
  1618. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  1619. if (bus == NULL)
  1620. return -ENOMEM;
  1621. bus->card = card;
  1622. bus->num = num;
  1623. bus->ops = ops;
  1624. bus->private_data = private_data;
  1625. bus->clock = 48000;
  1626. spin_lock_init(&bus->bus_lock);
  1627. snd_ac97_bus_proc_init(bus);
  1628. if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
  1629. snd_ac97_bus_free(bus);
  1630. return err;
  1631. }
  1632. *rbus = bus;
  1633. return 0;
  1634. }
  1635. /* stop no dev release warning */
  1636. static void ac97_device_release(struct device * dev)
  1637. {
  1638. }
  1639. /* register ac97 codec to bus */
  1640. static int snd_ac97_dev_register(snd_device_t *device)
  1641. {
  1642. ac97_t *ac97 = device->device_data;
  1643. int err;
  1644. ac97->dev.bus = &ac97_bus_type;
  1645. ac97->dev.parent = ac97->bus->card->dev;
  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 = kzalloc(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)