ac97_codec.c 85 KB

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