ac97_codec.c 85 KB

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