via82xx.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409
  1. /*
  2. * ALSA driver for VIA VT82xx (South Bridge)
  3. *
  4. * VT82C686A/B/C, VT8233A/C, VT8235
  5. *
  6. * Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
  7. * Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
  8. * 2002 Takashi Iwai <tiwai@suse.de>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. /*
  26. * Changes:
  27. *
  28. * Dec. 19, 2002 Takashi Iwai <tiwai@suse.de>
  29. * - use the DSX channels for the first pcm playback.
  30. * (on VIA8233, 8233C and 8235 only)
  31. * this will allow you play simultaneously up to 4 streams.
  32. * multi-channel playback is assigned to the second device
  33. * on these chips.
  34. * - support the secondary capture (on VIA8233/C,8235)
  35. * - SPDIF support
  36. * the DSX3 channel can be used for SPDIF output.
  37. * on VIA8233A, this channel is assigned to the second pcm
  38. * playback.
  39. * the card config of alsa-lib will assign the correct
  40. * device for applications.
  41. * - clean up the code, separate low-level initialization
  42. * routines for each chipset.
  43. */
  44. #include <sound/driver.h>
  45. #include <asm/io.h>
  46. #include <linux/delay.h>
  47. #include <linux/interrupt.h>
  48. #include <linux/init.h>
  49. #include <linux/pci.h>
  50. #include <linux/slab.h>
  51. #include <linux/gameport.h>
  52. #include <linux/moduleparam.h>
  53. #include <sound/core.h>
  54. #include <sound/pcm.h>
  55. #include <sound/pcm_params.h>
  56. #include <sound/info.h>
  57. #include <sound/ac97_codec.h>
  58. #include <sound/mpu401.h>
  59. #include <sound/initval.h>
  60. #if 0
  61. #define POINTER_DEBUG
  62. #endif
  63. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  64. MODULE_DESCRIPTION("VIA VT82xx audio");
  65. MODULE_LICENSE("GPL");
  66. MODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C,pci},{VIA,VT8233A/C,8235}}");
  67. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  68. #define SUPPORT_JOYSTICK 1
  69. #endif
  70. static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
  71. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  72. static long mpu_port;
  73. #ifdef SUPPORT_JOYSTICK
  74. static int joystick;
  75. #endif
  76. static int ac97_clock = 48000;
  77. static char *ac97_quirk;
  78. static int dxs_support;
  79. module_param(index, int, 0444);
  80. MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
  81. module_param(id, charp, 0444);
  82. MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
  83. module_param(mpu_port, long, 0444);
  84. MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
  85. #ifdef SUPPORT_JOYSTICK
  86. module_param(joystick, bool, 0444);
  87. MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
  88. #endif
  89. module_param(ac97_clock, int, 0444);
  90. MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
  91. module_param(ac97_quirk, charp, 0444);
  92. MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
  93. module_param(dxs_support, int, 0444);
  94. MODULE_PARM_DESC(dxs_support, "Support for DXS channels (0 = auto, 1 = enable, 2 = disable, 3 = 48k only, 4 = no VRA, 5 = enable any sample rate)");
  95. /* just for backward compatibility */
  96. static int enable;
  97. module_param(enable, int, 0444);
  98. /* revision numbers for via686 */
  99. #define VIA_REV_686_A 0x10
  100. #define VIA_REV_686_B 0x11
  101. #define VIA_REV_686_C 0x12
  102. #define VIA_REV_686_D 0x13
  103. #define VIA_REV_686_E 0x14
  104. #define VIA_REV_686_H 0x20
  105. /* revision numbers for via8233 */
  106. #define VIA_REV_PRE_8233 0x10 /* not in market */
  107. #define VIA_REV_8233C 0x20 /* 2 rec, 4 pb, 1 multi-pb */
  108. #define VIA_REV_8233 0x30 /* 2 rec, 4 pb, 1 multi-pb, spdif */
  109. #define VIA_REV_8233A 0x40 /* 1 rec, 1 multi-pb, spdf */
  110. #define VIA_REV_8235 0x50 /* 2 rec, 4 pb, 1 multi-pb, spdif */
  111. #define VIA_REV_8237 0x60
  112. /*
  113. * Direct registers
  114. */
  115. #define VIAREG(via, x) ((via)->port + VIA_REG_##x)
  116. #define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
  117. /* common offsets */
  118. #define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
  119. #define VIA_REG_STAT_ACTIVE 0x80 /* RO */
  120. #define VIA_REG_STAT_PAUSED 0x40 /* RO */
  121. #define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
  122. #define VIA_REG_STAT_STOPPED 0x04 /* RWC */
  123. #define VIA_REG_STAT_EOL 0x02 /* RWC */
  124. #define VIA_REG_STAT_FLAG 0x01 /* RWC */
  125. #define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
  126. #define VIA_REG_CTRL_START 0x80 /* WO */
  127. #define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
  128. #define VIA_REG_CTRL_AUTOSTART 0x20
  129. #define VIA_REG_CTRL_PAUSE 0x08 /* RW */
  130. #define VIA_REG_CTRL_INT_STOP 0x04
  131. #define VIA_REG_CTRL_INT_EOL 0x02
  132. #define VIA_REG_CTRL_INT_FLAG 0x01
  133. #define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
  134. #define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
  135. #define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
  136. #define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
  137. #define VIA_REG_TYPE_16BIT 0x20 /* RW */
  138. #define VIA_REG_TYPE_STEREO 0x10 /* RW */
  139. #define VIA_REG_TYPE_INT_LLINE 0x00
  140. #define VIA_REG_TYPE_INT_LSAMPLE 0x04
  141. #define VIA_REG_TYPE_INT_LESSONE 0x08
  142. #define VIA_REG_TYPE_INT_MASK 0x0c
  143. #define VIA_REG_TYPE_INT_EOL 0x02
  144. #define VIA_REG_TYPE_INT_FLAG 0x01
  145. #define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
  146. #define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
  147. #define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
  148. #define VIA8233_REG_TYPE_16BIT 0x00200000 /* RW */
  149. #define VIA8233_REG_TYPE_STEREO 0x00100000 /* RW */
  150. #define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
  151. #define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
  152. #define DEFINE_VIA_REGSET(name,val) \
  153. enum {\
  154. VIA_REG_##name##_STATUS = (val),\
  155. VIA_REG_##name##_CONTROL = (val) + 0x01,\
  156. VIA_REG_##name##_TYPE = (val) + 0x02,\
  157. VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
  158. VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
  159. VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
  160. VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
  161. }
  162. /* playback block */
  163. DEFINE_VIA_REGSET(PLAYBACK, 0x00);
  164. DEFINE_VIA_REGSET(CAPTURE, 0x10);
  165. DEFINE_VIA_REGSET(FM, 0x20);
  166. /* AC'97 */
  167. #define VIA_REG_AC97 0x80 /* dword */
  168. #define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
  169. #define VIA_REG_AC97_CODEC_ID_SHIFT 30
  170. #define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
  171. #define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
  172. #define VIA_REG_AC97_SECONDARY_VALID (1<<27)
  173. #define VIA_REG_AC97_PRIMARY_VALID (1<<25)
  174. #define VIA_REG_AC97_BUSY (1<<24)
  175. #define VIA_REG_AC97_READ (1<<23)
  176. #define VIA_REG_AC97_CMD_SHIFT 16
  177. #define VIA_REG_AC97_CMD_MASK 0x7e
  178. #define VIA_REG_AC97_DATA_SHIFT 0
  179. #define VIA_REG_AC97_DATA_MASK 0xffff
  180. #define VIA_REG_SGD_SHADOW 0x84 /* dword */
  181. /* via686 */
  182. #define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
  183. #define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
  184. #define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
  185. #define VIA_REG_SGD_STAT_PB_EOL (1<<4)
  186. #define VIA_REG_SGD_STAT_CP_EOL (1<<5)
  187. #define VIA_REG_SGD_STAT_FM_EOL (1<<6)
  188. #define VIA_REG_SGD_STAT_PB_STOP (1<<8)
  189. #define VIA_REG_SGD_STAT_CP_STOP (1<<9)
  190. #define VIA_REG_SGD_STAT_FM_STOP (1<<10)
  191. #define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
  192. #define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
  193. #define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
  194. /* via8233 */
  195. #define VIA8233_REG_SGD_STAT_FLAG (1<<0)
  196. #define VIA8233_REG_SGD_STAT_EOL (1<<1)
  197. #define VIA8233_REG_SGD_STAT_STOP (1<<2)
  198. #define VIA8233_REG_SGD_STAT_ACTIVE (1<<3)
  199. #define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
  200. #define VIA8233_REG_SGD_CHAN_SDX 0
  201. #define VIA8233_REG_SGD_CHAN_MULTI 4
  202. #define VIA8233_REG_SGD_CHAN_REC 6
  203. #define VIA8233_REG_SGD_CHAN_REC1 7
  204. #define VIA_REG_GPI_STATUS 0x88
  205. #define VIA_REG_GPI_INTR 0x8c
  206. /* multi-channel and capture registers for via8233 */
  207. DEFINE_VIA_REGSET(MULTPLAY, 0x40);
  208. DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
  209. /* via8233-specific registers */
  210. #define VIA_REG_OFS_PLAYBACK_VOLUME_L 0x02 /* byte */
  211. #define VIA_REG_OFS_PLAYBACK_VOLUME_R 0x03 /* byte */
  212. #define VIA_REG_OFS_MULTPLAY_FORMAT 0x02 /* byte - format and channels */
  213. #define VIA_REG_MULTPLAY_FMT_8BIT 0x00
  214. #define VIA_REG_MULTPLAY_FMT_16BIT 0x80
  215. #define VIA_REG_MULTPLAY_FMT_CH_MASK 0x70 /* # channels << 4 (valid = 1,2,4,6) */
  216. #define VIA_REG_OFS_CAPTURE_FIFO 0x02 /* byte - bit 6 = fifo enable */
  217. #define VIA_REG_CAPTURE_FIFO_ENABLE 0x40
  218. #define VIA_DXS_MAX_VOLUME 31 /* max. volume (attenuation) of reg 0x32/33 */
  219. #define VIA_REG_CAPTURE_CHANNEL 0x63 /* byte - input select */
  220. #define VIA_REG_CAPTURE_CHANNEL_MIC 0x4
  221. #define VIA_REG_CAPTURE_CHANNEL_LINE 0
  222. #define VIA_REG_CAPTURE_SELECT_CODEC 0x03 /* recording source codec (0 = primary) */
  223. #define VIA_TBL_BIT_FLAG 0x40000000
  224. #define VIA_TBL_BIT_EOL 0x80000000
  225. /* pci space */
  226. #define VIA_ACLINK_STAT 0x40
  227. #define VIA_ACLINK_C11_READY 0x20
  228. #define VIA_ACLINK_C10_READY 0x10
  229. #define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
  230. #define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
  231. #define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
  232. #define VIA_ACLINK_CTRL 0x41
  233. #define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
  234. #define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
  235. #define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
  236. #define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
  237. #define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
  238. #define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
  239. #define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
  240. #define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
  241. #define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
  242. VIA_ACLINK_CTRL_RESET|\
  243. VIA_ACLINK_CTRL_PCM|\
  244. VIA_ACLINK_CTRL_VRA)
  245. #define VIA_FUNC_ENABLE 0x42
  246. #define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
  247. #define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
  248. #define VIA_FUNC_RX2C_WRITE 0x20
  249. #define VIA_FUNC_SB_FIFO_EMPTY 0x10
  250. #define VIA_FUNC_ENABLE_GAME 0x08
  251. #define VIA_FUNC_ENABLE_FM 0x04
  252. #define VIA_FUNC_ENABLE_MIDI 0x02
  253. #define VIA_FUNC_ENABLE_SB 0x01
  254. #define VIA_PNP_CONTROL 0x43
  255. #define VIA_FM_NMI_CTRL 0x48
  256. #define VIA8233_VOLCHG_CTRL 0x48
  257. #define VIA8233_SPDIF_CTRL 0x49
  258. #define VIA8233_SPDIF_DX3 0x08
  259. #define VIA8233_SPDIF_SLOT_MASK 0x03
  260. #define VIA8233_SPDIF_SLOT_1011 0x00
  261. #define VIA8233_SPDIF_SLOT_34 0x01
  262. #define VIA8233_SPDIF_SLOT_78 0x02
  263. #define VIA8233_SPDIF_SLOT_69 0x03
  264. /*
  265. */
  266. #define VIA_DXS_AUTO 0
  267. #define VIA_DXS_ENABLE 1
  268. #define VIA_DXS_DISABLE 2
  269. #define VIA_DXS_48K 3
  270. #define VIA_DXS_NO_VRA 4
  271. #define VIA_DXS_SRC 5
  272. /*
  273. */
  274. typedef struct _snd_via82xx via82xx_t;
  275. typedef struct via_dev viadev_t;
  276. /*
  277. * pcm stream
  278. */
  279. struct snd_via_sg_table {
  280. unsigned int offset;
  281. unsigned int size;
  282. } ;
  283. #define VIA_TABLE_SIZE 255
  284. struct via_dev {
  285. unsigned int reg_offset;
  286. unsigned long port;
  287. int direction; /* playback = 0, capture = 1 */
  288. snd_pcm_substream_t *substream;
  289. int running;
  290. unsigned int tbl_entries; /* # descriptors */
  291. struct snd_dma_buffer table;
  292. struct snd_via_sg_table *idx_table;
  293. /* for recovery from the unexpected pointer */
  294. unsigned int lastpos;
  295. unsigned int fragsize;
  296. unsigned int bufsize;
  297. unsigned int bufsize2;
  298. };
  299. enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
  300. enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
  301. #define VIA_MAX_DEVS 7 /* 4 playback, 1 multi, 2 capture */
  302. struct via_rate_lock {
  303. spinlock_t lock;
  304. int rate;
  305. int used;
  306. };
  307. struct _snd_via82xx {
  308. int irq;
  309. unsigned long port;
  310. struct resource *mpu_res;
  311. int chip_type;
  312. unsigned char revision;
  313. unsigned char old_legacy;
  314. unsigned char old_legacy_cfg;
  315. #ifdef CONFIG_PM
  316. unsigned char legacy_saved;
  317. unsigned char legacy_cfg_saved;
  318. unsigned char spdif_ctrl_saved;
  319. unsigned char capture_src_saved[2];
  320. unsigned int mpu_port_saved;
  321. #endif
  322. unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
  323. unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
  324. unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
  325. struct pci_dev *pci;
  326. snd_card_t *card;
  327. unsigned int num_devs;
  328. unsigned int playback_devno, multi_devno, capture_devno;
  329. viadev_t devs[VIA_MAX_DEVS];
  330. struct via_rate_lock rates[2]; /* playback and capture */
  331. unsigned int dxs_fixed: 1; /* DXS channel accepts only 48kHz */
  332. unsigned int no_vra: 1; /* no need to set VRA on DXS channels */
  333. unsigned int dxs_src: 1; /* use full SRC capabilities of DXS */
  334. unsigned int spdif_on: 1; /* only spdif rates work to external DACs */
  335. snd_pcm_t *pcms[2];
  336. snd_rawmidi_t *rmidi;
  337. ac97_bus_t *ac97_bus;
  338. ac97_t *ac97;
  339. unsigned int ac97_clock;
  340. unsigned int ac97_secondary; /* secondary AC'97 codec is present */
  341. spinlock_t reg_lock;
  342. snd_info_entry_t *proc_entry;
  343. #ifdef SUPPORT_JOYSTICK
  344. struct gameport *gameport;
  345. #endif
  346. };
  347. static struct pci_device_id snd_via82xx_ids[] = {
  348. { 0x1106, 0x3058, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA686, }, /* 686A */
  349. { 0x1106, 0x3059, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA8233, }, /* VT8233 */
  350. { 0, }
  351. };
  352. MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
  353. /*
  354. */
  355. /*
  356. * allocate and initialize the descriptor buffers
  357. * periods = number of periods
  358. * fragsize = period size in bytes
  359. */
  360. static int build_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
  361. struct pci_dev *pci,
  362. unsigned int periods, unsigned int fragsize)
  363. {
  364. unsigned int i, idx, ofs, rest;
  365. via82xx_t *chip = snd_pcm_substream_chip(substream);
  366. struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
  367. if (dev->table.area == NULL) {
  368. /* the start of each lists must be aligned to 8 bytes,
  369. * but the kernel pages are much bigger, so we don't care
  370. */
  371. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  372. PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
  373. &dev->table) < 0)
  374. return -ENOMEM;
  375. }
  376. if (! dev->idx_table) {
  377. dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
  378. if (! dev->idx_table)
  379. return -ENOMEM;
  380. }
  381. /* fill the entries */
  382. idx = 0;
  383. ofs = 0;
  384. for (i = 0; i < periods; i++) {
  385. rest = fragsize;
  386. /* fill descriptors for a period.
  387. * a period can be split to several descriptors if it's
  388. * over page boundary.
  389. */
  390. do {
  391. unsigned int r;
  392. unsigned int flag;
  393. if (idx >= VIA_TABLE_SIZE) {
  394. snd_printk(KERN_ERR "via82xx: too much table size!\n");
  395. return -EINVAL;
  396. }
  397. ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
  398. r = PAGE_SIZE - (ofs % PAGE_SIZE);
  399. if (rest < r)
  400. r = rest;
  401. rest -= r;
  402. if (! rest) {
  403. if (i == periods - 1)
  404. flag = VIA_TBL_BIT_EOL; /* buffer boundary */
  405. else
  406. flag = VIA_TBL_BIT_FLAG; /* period boundary */
  407. } else
  408. flag = 0; /* period continues to the next */
  409. // printk("via: tbl %d: at %d size %d (rest %d)\n", idx, ofs, r, rest);
  410. ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
  411. dev->idx_table[idx].offset = ofs;
  412. dev->idx_table[idx].size = r;
  413. ofs += r;
  414. idx++;
  415. } while (rest > 0);
  416. }
  417. dev->tbl_entries = idx;
  418. dev->bufsize = periods * fragsize;
  419. dev->bufsize2 = dev->bufsize / 2;
  420. dev->fragsize = fragsize;
  421. return 0;
  422. }
  423. static int clean_via_table(viadev_t *dev, snd_pcm_substream_t *substream,
  424. struct pci_dev *pci)
  425. {
  426. if (dev->table.area) {
  427. snd_dma_free_pages(&dev->table);
  428. dev->table.area = NULL;
  429. }
  430. kfree(dev->idx_table);
  431. dev->idx_table = NULL;
  432. return 0;
  433. }
  434. /*
  435. * Basic I/O
  436. */
  437. static inline unsigned int snd_via82xx_codec_xread(via82xx_t *chip)
  438. {
  439. return inl(VIAREG(chip, AC97));
  440. }
  441. static inline void snd_via82xx_codec_xwrite(via82xx_t *chip, unsigned int val)
  442. {
  443. outl(val, VIAREG(chip, AC97));
  444. }
  445. static int snd_via82xx_codec_ready(via82xx_t *chip, int secondary)
  446. {
  447. unsigned int timeout = 1000; /* 1ms */
  448. unsigned int val;
  449. while (timeout-- > 0) {
  450. udelay(1);
  451. if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
  452. return val & 0xffff;
  453. }
  454. snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_via82xx_codec_xread(chip));
  455. return -EIO;
  456. }
  457. static int snd_via82xx_codec_valid(via82xx_t *chip, int secondary)
  458. {
  459. unsigned int timeout = 1000; /* 1ms */
  460. unsigned int val, val1;
  461. unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
  462. VIA_REG_AC97_SECONDARY_VALID;
  463. while (timeout-- > 0) {
  464. val = snd_via82xx_codec_xread(chip);
  465. val1 = val & (VIA_REG_AC97_BUSY | stat);
  466. if (val1 == stat)
  467. return val & 0xffff;
  468. udelay(1);
  469. }
  470. return -EIO;
  471. }
  472. static void snd_via82xx_codec_wait(ac97_t *ac97)
  473. {
  474. via82xx_t *chip = ac97->private_data;
  475. int err;
  476. err = snd_via82xx_codec_ready(chip, ac97->num);
  477. /* here we need to wait fairly for long time.. */
  478. msleep(500);
  479. }
  480. static void snd_via82xx_codec_write(ac97_t *ac97,
  481. unsigned short reg,
  482. unsigned short val)
  483. {
  484. via82xx_t *chip = ac97->private_data;
  485. unsigned int xval;
  486. xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
  487. xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
  488. xval |= reg << VIA_REG_AC97_CMD_SHIFT;
  489. xval |= val << VIA_REG_AC97_DATA_SHIFT;
  490. snd_via82xx_codec_xwrite(chip, xval);
  491. snd_via82xx_codec_ready(chip, ac97->num);
  492. }
  493. static unsigned short snd_via82xx_codec_read(ac97_t *ac97, unsigned short reg)
  494. {
  495. via82xx_t *chip = ac97->private_data;
  496. unsigned int xval, val = 0xffff;
  497. int again = 0;
  498. xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
  499. xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
  500. xval |= VIA_REG_AC97_READ;
  501. xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
  502. while (1) {
  503. if (again++ > 3) {
  504. snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n", ac97->num, snd_via82xx_codec_xread(chip));
  505. return 0xffff;
  506. }
  507. snd_via82xx_codec_xwrite(chip, xval);
  508. udelay (20);
  509. if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
  510. udelay(25);
  511. val = snd_via82xx_codec_xread(chip);
  512. break;
  513. }
  514. }
  515. return val & 0xffff;
  516. }
  517. static void snd_via82xx_channel_reset(via82xx_t *chip, viadev_t *viadev)
  518. {
  519. outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
  520. VIADEV_REG(viadev, OFFSET_CONTROL));
  521. inb(VIADEV_REG(viadev, OFFSET_CONTROL));
  522. udelay(50);
  523. /* disable interrupts */
  524. outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
  525. /* clear interrupts */
  526. outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
  527. outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
  528. // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
  529. viadev->lastpos = 0;
  530. }
  531. /*
  532. * Interrupt handler
  533. */
  534. static irqreturn_t snd_via82xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  535. {
  536. via82xx_t *chip = dev_id;
  537. unsigned int status;
  538. unsigned int i;
  539. status = inl(VIAREG(chip, SGD_SHADOW));
  540. if (! (status & chip->intr_mask)) {
  541. if (chip->rmidi)
  542. /* check mpu401 interrupt */
  543. return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
  544. return IRQ_NONE;
  545. }
  546. /* check status for each stream */
  547. spin_lock(&chip->reg_lock);
  548. for (i = 0; i < chip->num_devs; i++) {
  549. viadev_t *viadev = &chip->devs[i];
  550. unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
  551. c_status &= (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED);
  552. if (! c_status)
  553. continue;
  554. if (viadev->substream && viadev->running) {
  555. spin_unlock(&chip->reg_lock);
  556. snd_pcm_period_elapsed(viadev->substream);
  557. spin_lock(&chip->reg_lock);
  558. }
  559. outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
  560. }
  561. spin_unlock(&chip->reg_lock);
  562. return IRQ_HANDLED;
  563. }
  564. /*
  565. * PCM callbacks
  566. */
  567. /*
  568. * trigger callback
  569. */
  570. static int snd_via82xx_pcm_trigger(snd_pcm_substream_t * substream, int cmd)
  571. {
  572. via82xx_t *chip = snd_pcm_substream_chip(substream);
  573. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  574. unsigned char val;
  575. if (chip->chip_type != TYPE_VIA686)
  576. val = VIA_REG_CTRL_INT;
  577. else
  578. val = 0;
  579. switch (cmd) {
  580. case SNDRV_PCM_TRIGGER_START:
  581. case SNDRV_PCM_TRIGGER_RESUME:
  582. val |= VIA_REG_CTRL_START;
  583. viadev->running = 1;
  584. break;
  585. case SNDRV_PCM_TRIGGER_STOP:
  586. case SNDRV_PCM_TRIGGER_SUSPEND:
  587. val = VIA_REG_CTRL_TERMINATE;
  588. viadev->running = 0;
  589. break;
  590. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  591. val |= VIA_REG_CTRL_PAUSE;
  592. viadev->running = 0;
  593. break;
  594. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  595. viadev->running = 1;
  596. break;
  597. default:
  598. return -EINVAL;
  599. }
  600. outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
  601. if (cmd == SNDRV_PCM_TRIGGER_STOP)
  602. snd_via82xx_channel_reset(chip, viadev);
  603. return 0;
  604. }
  605. /*
  606. * pointer callbacks
  607. */
  608. /*
  609. * calculate the linear position at the given sg-buffer index and the rest count
  610. */
  611. #define check_invalid_pos(viadev,pos) \
  612. ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 || viadev->lastpos < viadev->bufsize2))
  613. static inline unsigned int calc_linear_pos(viadev_t *viadev, unsigned int idx, unsigned int count)
  614. {
  615. unsigned int size, base, res;
  616. size = viadev->idx_table[idx].size;
  617. base = viadev->idx_table[idx].offset;
  618. res = base + size - count;
  619. /* check the validity of the calculated position */
  620. if (size < count) {
  621. snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n", (int)size, (int)count);
  622. res = viadev->lastpos;
  623. } else {
  624. if (! count) {
  625. /* Some mobos report count = 0 on the DMA boundary,
  626. * i.e. count = size indeed.
  627. * Let's check whether this step is above the expected size.
  628. */
  629. int delta = res - viadev->lastpos;
  630. if (delta < 0)
  631. delta += viadev->bufsize;
  632. if ((unsigned int)delta > viadev->fragsize)
  633. res = base;
  634. }
  635. if (check_invalid_pos(viadev, res)) {
  636. #ifdef POINTER_DEBUG
  637. printk(KERN_DEBUG "fail: idx = %i/%i, lastpos = 0x%x, bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, count = 0x%x\n", idx, viadev->tbl_entries, viadev->lastpos, viadev->bufsize2, viadev->idx_table[idx].offset, viadev->idx_table[idx].size, count);
  638. #endif
  639. /* count register returns full size when end of buffer is reached */
  640. res = base + size;
  641. if (check_invalid_pos(viadev, res)) {
  642. snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), using last valid pointer\n");
  643. res = viadev->lastpos;
  644. }
  645. }
  646. }
  647. viadev->lastpos = res; /* remember the last position */
  648. if (res >= viadev->bufsize)
  649. res -= viadev->bufsize;
  650. return res;
  651. }
  652. /*
  653. * get the current pointer on via686
  654. */
  655. static snd_pcm_uframes_t snd_via686_pcm_pointer(snd_pcm_substream_t *substream)
  656. {
  657. via82xx_t *chip = snd_pcm_substream_chip(substream);
  658. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  659. unsigned int idx, ptr, count, res;
  660. snd_assert(viadev->tbl_entries, return 0);
  661. if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
  662. return 0;
  663. spin_lock(&chip->reg_lock);
  664. count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
  665. /* The via686a does not have the current index register,
  666. * so we need to calculate the index from CURR_PTR.
  667. */
  668. ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
  669. if (ptr <= (unsigned int)viadev->table.addr)
  670. idx = 0;
  671. else /* CURR_PTR holds the address + 8 */
  672. idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
  673. res = calc_linear_pos(viadev, idx, count);
  674. spin_unlock(&chip->reg_lock);
  675. return bytes_to_frames(substream->runtime, res);
  676. }
  677. /*
  678. * get the current pointer on via823x
  679. */
  680. static snd_pcm_uframes_t snd_via8233_pcm_pointer(snd_pcm_substream_t *substream)
  681. {
  682. via82xx_t *chip = snd_pcm_substream_chip(substream);
  683. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  684. unsigned int idx, count, res;
  685. int timeout = 5000;
  686. snd_assert(viadev->tbl_entries, return 0);
  687. if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
  688. return 0;
  689. spin_lock(&chip->reg_lock);
  690. do {
  691. count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
  692. /* some mobos read 0 count */
  693. if ((count & 0xffffff) || ! viadev->running)
  694. break;
  695. } while (--timeout);
  696. if (! timeout)
  697. snd_printd(KERN_ERR "zero position is read\n");
  698. idx = count >> 24;
  699. if (idx >= viadev->tbl_entries) {
  700. #ifdef POINTER_DEBUG
  701. printk("fail: invalid idx = %i/%i\n", idx, viadev->tbl_entries);
  702. #endif
  703. res = viadev->lastpos;
  704. } else {
  705. count &= 0xffffff;
  706. res = calc_linear_pos(viadev, idx, count);
  707. }
  708. spin_unlock(&chip->reg_lock);
  709. return bytes_to_frames(substream->runtime, res);
  710. }
  711. /*
  712. * hw_params callback:
  713. * allocate the buffer and build up the buffer description table
  714. */
  715. static int snd_via82xx_hw_params(snd_pcm_substream_t * substream,
  716. snd_pcm_hw_params_t * hw_params)
  717. {
  718. via82xx_t *chip = snd_pcm_substream_chip(substream);
  719. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  720. int err;
  721. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  722. if (err < 0)
  723. return err;
  724. err = build_via_table(viadev, substream, chip->pci,
  725. params_periods(hw_params),
  726. params_period_bytes(hw_params));
  727. if (err < 0)
  728. return err;
  729. return 0;
  730. }
  731. /*
  732. * hw_free callback:
  733. * clean up the buffer description table and release the buffer
  734. */
  735. static int snd_via82xx_hw_free(snd_pcm_substream_t * substream)
  736. {
  737. via82xx_t *chip = snd_pcm_substream_chip(substream);
  738. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  739. clean_via_table(viadev, substream, chip->pci);
  740. snd_pcm_lib_free_pages(substream);
  741. return 0;
  742. }
  743. /*
  744. * set up the table pointer
  745. */
  746. static void snd_via82xx_set_table_ptr(via82xx_t *chip, viadev_t *viadev)
  747. {
  748. snd_via82xx_codec_ready(chip, 0);
  749. outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
  750. udelay(20);
  751. snd_via82xx_codec_ready(chip, 0);
  752. }
  753. /*
  754. * prepare callback for playback and capture on via686
  755. */
  756. static void via686_setup_format(via82xx_t *chip, viadev_t *viadev, snd_pcm_runtime_t *runtime)
  757. {
  758. snd_via82xx_channel_reset(chip, viadev);
  759. /* this must be set after channel_reset */
  760. snd_via82xx_set_table_ptr(chip, viadev);
  761. outb(VIA_REG_TYPE_AUTOSTART |
  762. (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
  763. (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
  764. ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
  765. VIA_REG_TYPE_INT_EOL |
  766. VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
  767. }
  768. static int snd_via686_playback_prepare(snd_pcm_substream_t *substream)
  769. {
  770. via82xx_t *chip = snd_pcm_substream_chip(substream);
  771. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  772. snd_pcm_runtime_t *runtime = substream->runtime;
  773. snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
  774. snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
  775. via686_setup_format(chip, viadev, runtime);
  776. return 0;
  777. }
  778. static int snd_via686_capture_prepare(snd_pcm_substream_t *substream)
  779. {
  780. via82xx_t *chip = snd_pcm_substream_chip(substream);
  781. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  782. snd_pcm_runtime_t *runtime = substream->runtime;
  783. snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
  784. via686_setup_format(chip, viadev, runtime);
  785. return 0;
  786. }
  787. /*
  788. * lock the current rate
  789. */
  790. static int via_lock_rate(struct via_rate_lock *rec, int rate)
  791. {
  792. int changed = 0;
  793. spin_lock_irq(&rec->lock);
  794. if (rec->rate != rate) {
  795. if (rec->rate && rec->used > 1) /* already set */
  796. changed = -EINVAL;
  797. else {
  798. rec->rate = rate;
  799. changed = 1;
  800. }
  801. }
  802. spin_unlock_irq(&rec->lock);
  803. return changed;
  804. }
  805. /*
  806. * prepare callback for DSX playback on via823x
  807. */
  808. static int snd_via8233_playback_prepare(snd_pcm_substream_t *substream)
  809. {
  810. via82xx_t *chip = snd_pcm_substream_chip(substream);
  811. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  812. snd_pcm_runtime_t *runtime = substream->runtime;
  813. int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
  814. int rate_changed;
  815. u32 rbits;
  816. if ((rate_changed = via_lock_rate(&chip->rates[0], ac97_rate)) < 0)
  817. return rate_changed;
  818. if (rate_changed)
  819. snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
  820. chip->no_vra ? 48000 : runtime->rate);
  821. if (chip->spdif_on && viadev->reg_offset == 0x30)
  822. snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
  823. if (runtime->rate == 48000)
  824. rbits = 0xfffff;
  825. else
  826. rbits = (0x100000 / 48000) * runtime->rate + ((0x100000 % 48000) * runtime->rate) / 48000;
  827. snd_assert((rbits & ~0xfffff) == 0, return -EINVAL);
  828. snd_via82xx_channel_reset(chip, viadev);
  829. snd_via82xx_set_table_ptr(chip, viadev);
  830. outb(chip->playback_volume[viadev->reg_offset / 0x10][0], VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
  831. outb(chip->playback_volume[viadev->reg_offset / 0x10][1], VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
  832. outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
  833. (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
  834. rbits | /* rate */
  835. 0xff000000, /* STOP index is never reached */
  836. VIADEV_REG(viadev, OFFSET_STOP_IDX));
  837. udelay(20);
  838. snd_via82xx_codec_ready(chip, 0);
  839. return 0;
  840. }
  841. /*
  842. * prepare callback for multi-channel playback on via823x
  843. */
  844. static int snd_via8233_multi_prepare(snd_pcm_substream_t *substream)
  845. {
  846. via82xx_t *chip = snd_pcm_substream_chip(substream);
  847. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  848. snd_pcm_runtime_t *runtime = substream->runtime;
  849. unsigned int slots;
  850. int fmt;
  851. if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
  852. return -EINVAL;
  853. snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
  854. snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
  855. snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
  856. snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
  857. snd_via82xx_channel_reset(chip, viadev);
  858. snd_via82xx_set_table_ptr(chip, viadev);
  859. fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ? VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
  860. fmt |= runtime->channels << 4;
  861. outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
  862. #if 0
  863. if (chip->revision == VIA_REV_8233A)
  864. slots = 0;
  865. else
  866. #endif
  867. {
  868. /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
  869. /* corresponding to FL, FR, RL, RR, C, LFE ?? */
  870. switch (runtime->channels) {
  871. case 1: slots = (1<<0) | (1<<4); break;
  872. case 2: slots = (1<<0) | (2<<4); break;
  873. case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
  874. case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
  875. case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
  876. case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
  877. default: slots = 0; break;
  878. }
  879. }
  880. /* STOP index is never reached */
  881. outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
  882. udelay(20);
  883. snd_via82xx_codec_ready(chip, 0);
  884. return 0;
  885. }
  886. /*
  887. * prepare callback for capture on via823x
  888. */
  889. static int snd_via8233_capture_prepare(snd_pcm_substream_t *substream)
  890. {
  891. via82xx_t *chip = snd_pcm_substream_chip(substream);
  892. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  893. snd_pcm_runtime_t *runtime = substream->runtime;
  894. if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
  895. return -EINVAL;
  896. snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
  897. snd_via82xx_channel_reset(chip, viadev);
  898. snd_via82xx_set_table_ptr(chip, viadev);
  899. outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
  900. outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
  901. (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
  902. 0xff000000, /* STOP index is never reached */
  903. VIADEV_REG(viadev, OFFSET_STOP_IDX));
  904. udelay(20);
  905. snd_via82xx_codec_ready(chip, 0);
  906. return 0;
  907. }
  908. /*
  909. * pcm hardware definition, identical for both playback and capture
  910. */
  911. static snd_pcm_hardware_t snd_via82xx_hw =
  912. {
  913. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  914. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  915. SNDRV_PCM_INFO_MMAP_VALID |
  916. /* SNDRV_PCM_INFO_RESUME | */
  917. SNDRV_PCM_INFO_PAUSE),
  918. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  919. .rates = SNDRV_PCM_RATE_48000,
  920. .rate_min = 48000,
  921. .rate_max = 48000,
  922. .channels_min = 1,
  923. .channels_max = 2,
  924. .buffer_bytes_max = 128 * 1024,
  925. .period_bytes_min = 32,
  926. .period_bytes_max = 128 * 1024,
  927. .periods_min = 2,
  928. .periods_max = VIA_TABLE_SIZE / 2,
  929. .fifo_size = 0,
  930. };
  931. /*
  932. * open callback skeleton
  933. */
  934. static int snd_via82xx_pcm_open(via82xx_t *chip, viadev_t *viadev, snd_pcm_substream_t * substream)
  935. {
  936. snd_pcm_runtime_t *runtime = substream->runtime;
  937. int err;
  938. struct via_rate_lock *ratep;
  939. runtime->hw = snd_via82xx_hw;
  940. /* set the hw rate condition */
  941. ratep = &chip->rates[viadev->direction];
  942. spin_lock_irq(&ratep->lock);
  943. ratep->used++;
  944. if (chip->spdif_on && viadev->reg_offset == 0x30) {
  945. /* DXS#3 and spdif is on */
  946. runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
  947. snd_pcm_limit_hw_rates(runtime);
  948. } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
  949. /* fixed DXS playback rate */
  950. runtime->hw.rates = SNDRV_PCM_RATE_48000;
  951. runtime->hw.rate_min = runtime->hw.rate_max = 48000;
  952. } else if (chip->dxs_src && viadev->reg_offset < 0x40) {
  953. /* use full SRC capabilities of DXS */
  954. runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
  955. SNDRV_PCM_RATE_8000_48000);
  956. runtime->hw.rate_min = 8000;
  957. runtime->hw.rate_max = 48000;
  958. } else if (! ratep->rate) {
  959. int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
  960. runtime->hw.rates = chip->ac97->rates[idx];
  961. snd_pcm_limit_hw_rates(runtime);
  962. } else {
  963. /* a fixed rate */
  964. runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
  965. runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
  966. }
  967. spin_unlock_irq(&ratep->lock);
  968. /* we may remove following constaint when we modify table entries
  969. in interrupt */
  970. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  971. return err;
  972. runtime->private_data = viadev;
  973. viadev->substream = substream;
  974. return 0;
  975. }
  976. /*
  977. * open callback for playback on via686 and via823x DSX
  978. */
  979. static int snd_via82xx_playback_open(snd_pcm_substream_t * substream)
  980. {
  981. via82xx_t *chip = snd_pcm_substream_chip(substream);
  982. viadev_t *viadev = &chip->devs[chip->playback_devno + substream->number];
  983. int err;
  984. if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
  985. return err;
  986. return 0;
  987. }
  988. /*
  989. * open callback for playback on via823x multi-channel
  990. */
  991. static int snd_via8233_multi_open(snd_pcm_substream_t * substream)
  992. {
  993. via82xx_t *chip = snd_pcm_substream_chip(substream);
  994. viadev_t *viadev = &chip->devs[chip->multi_devno];
  995. int err;
  996. /* channels constraint for VIA8233A
  997. * 3 and 5 channels are not supported
  998. */
  999. static unsigned int channels[] = {
  1000. 1, 2, 4, 6
  1001. };
  1002. static snd_pcm_hw_constraint_list_t hw_constraints_channels = {
  1003. .count = ARRAY_SIZE(channels),
  1004. .list = channels,
  1005. .mask = 0,
  1006. };
  1007. if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
  1008. return err;
  1009. substream->runtime->hw.channels_max = 6;
  1010. if (chip->revision == VIA_REV_8233A)
  1011. snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels);
  1012. return 0;
  1013. }
  1014. /*
  1015. * open callback for capture on via686 and via823x
  1016. */
  1017. static int snd_via82xx_capture_open(snd_pcm_substream_t * substream)
  1018. {
  1019. via82xx_t *chip = snd_pcm_substream_chip(substream);
  1020. viadev_t *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
  1021. return snd_via82xx_pcm_open(chip, viadev, substream);
  1022. }
  1023. /*
  1024. * close callback
  1025. */
  1026. static int snd_via82xx_pcm_close(snd_pcm_substream_t * substream)
  1027. {
  1028. via82xx_t *chip = snd_pcm_substream_chip(substream);
  1029. viadev_t *viadev = (viadev_t *)substream->runtime->private_data;
  1030. struct via_rate_lock *ratep;
  1031. /* release the rate lock */
  1032. ratep = &chip->rates[viadev->direction];
  1033. spin_lock_irq(&ratep->lock);
  1034. ratep->used--;
  1035. if (! ratep->used)
  1036. ratep->rate = 0;
  1037. spin_unlock_irq(&ratep->lock);
  1038. viadev->substream = NULL;
  1039. return 0;
  1040. }
  1041. /* via686 playback callbacks */
  1042. static snd_pcm_ops_t snd_via686_playback_ops = {
  1043. .open = snd_via82xx_playback_open,
  1044. .close = snd_via82xx_pcm_close,
  1045. .ioctl = snd_pcm_lib_ioctl,
  1046. .hw_params = snd_via82xx_hw_params,
  1047. .hw_free = snd_via82xx_hw_free,
  1048. .prepare = snd_via686_playback_prepare,
  1049. .trigger = snd_via82xx_pcm_trigger,
  1050. .pointer = snd_via686_pcm_pointer,
  1051. .page = snd_pcm_sgbuf_ops_page,
  1052. };
  1053. /* via686 capture callbacks */
  1054. static snd_pcm_ops_t snd_via686_capture_ops = {
  1055. .open = snd_via82xx_capture_open,
  1056. .close = snd_via82xx_pcm_close,
  1057. .ioctl = snd_pcm_lib_ioctl,
  1058. .hw_params = snd_via82xx_hw_params,
  1059. .hw_free = snd_via82xx_hw_free,
  1060. .prepare = snd_via686_capture_prepare,
  1061. .trigger = snd_via82xx_pcm_trigger,
  1062. .pointer = snd_via686_pcm_pointer,
  1063. .page = snd_pcm_sgbuf_ops_page,
  1064. };
  1065. /* via823x DSX playback callbacks */
  1066. static snd_pcm_ops_t snd_via8233_playback_ops = {
  1067. .open = snd_via82xx_playback_open,
  1068. .close = snd_via82xx_pcm_close,
  1069. .ioctl = snd_pcm_lib_ioctl,
  1070. .hw_params = snd_via82xx_hw_params,
  1071. .hw_free = snd_via82xx_hw_free,
  1072. .prepare = snd_via8233_playback_prepare,
  1073. .trigger = snd_via82xx_pcm_trigger,
  1074. .pointer = snd_via8233_pcm_pointer,
  1075. .page = snd_pcm_sgbuf_ops_page,
  1076. };
  1077. /* via823x multi-channel playback callbacks */
  1078. static snd_pcm_ops_t snd_via8233_multi_ops = {
  1079. .open = snd_via8233_multi_open,
  1080. .close = snd_via82xx_pcm_close,
  1081. .ioctl = snd_pcm_lib_ioctl,
  1082. .hw_params = snd_via82xx_hw_params,
  1083. .hw_free = snd_via82xx_hw_free,
  1084. .prepare = snd_via8233_multi_prepare,
  1085. .trigger = snd_via82xx_pcm_trigger,
  1086. .pointer = snd_via8233_pcm_pointer,
  1087. .page = snd_pcm_sgbuf_ops_page,
  1088. };
  1089. /* via823x capture callbacks */
  1090. static snd_pcm_ops_t snd_via8233_capture_ops = {
  1091. .open = snd_via82xx_capture_open,
  1092. .close = snd_via82xx_pcm_close,
  1093. .ioctl = snd_pcm_lib_ioctl,
  1094. .hw_params = snd_via82xx_hw_params,
  1095. .hw_free = snd_via82xx_hw_free,
  1096. .prepare = snd_via8233_capture_prepare,
  1097. .trigger = snd_via82xx_pcm_trigger,
  1098. .pointer = snd_via8233_pcm_pointer,
  1099. .page = snd_pcm_sgbuf_ops_page,
  1100. };
  1101. static void init_viadev(via82xx_t *chip, int idx, unsigned int reg_offset, int direction)
  1102. {
  1103. chip->devs[idx].reg_offset = reg_offset;
  1104. chip->devs[idx].direction = direction;
  1105. chip->devs[idx].port = chip->port + reg_offset;
  1106. }
  1107. /*
  1108. * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
  1109. */
  1110. static int __devinit snd_via8233_pcm_new(via82xx_t *chip)
  1111. {
  1112. snd_pcm_t *pcm;
  1113. int i, err;
  1114. chip->playback_devno = 0; /* x 4 */
  1115. chip->multi_devno = 4; /* x 1 */
  1116. chip->capture_devno = 5; /* x 2 */
  1117. chip->num_devs = 7;
  1118. chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
  1119. /* PCM #0: 4 DSX playbacks and 1 capture */
  1120. err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
  1121. if (err < 0)
  1122. return err;
  1123. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
  1124. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
  1125. pcm->private_data = chip;
  1126. strcpy(pcm->name, chip->card->shortname);
  1127. chip->pcms[0] = pcm;
  1128. /* set up playbacks */
  1129. for (i = 0; i < 4; i++)
  1130. init_viadev(chip, i, 0x10 * i, 0);
  1131. /* capture */
  1132. init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 1);
  1133. if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1134. snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
  1135. return err;
  1136. /* PCM #1: multi-channel playback and 2nd capture */
  1137. err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
  1138. if (err < 0)
  1139. return err;
  1140. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
  1141. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
  1142. pcm->private_data = chip;
  1143. strcpy(pcm->name, chip->card->shortname);
  1144. chip->pcms[1] = pcm;
  1145. /* set up playback */
  1146. init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 0);
  1147. /* set up capture */
  1148. init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 1);
  1149. if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1150. snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
  1151. return err;
  1152. return 0;
  1153. }
  1154. /*
  1155. * create pcm instances for VIA8233A
  1156. */
  1157. static int __devinit snd_via8233a_pcm_new(via82xx_t *chip)
  1158. {
  1159. snd_pcm_t *pcm;
  1160. int err;
  1161. chip->multi_devno = 0;
  1162. chip->playback_devno = 1;
  1163. chip->capture_devno = 2;
  1164. chip->num_devs = 3;
  1165. chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
  1166. /* PCM #0: multi-channel playback and capture */
  1167. err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
  1168. if (err < 0)
  1169. return err;
  1170. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
  1171. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
  1172. pcm->private_data = chip;
  1173. strcpy(pcm->name, chip->card->shortname);
  1174. chip->pcms[0] = pcm;
  1175. /* set up playback */
  1176. init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 0);
  1177. /* capture */
  1178. init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 1);
  1179. if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1180. snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
  1181. return err;
  1182. /* SPDIF supported? */
  1183. if (! ac97_can_spdif(chip->ac97))
  1184. return 0;
  1185. /* PCM #1: DXS3 playback (for spdif) */
  1186. err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
  1187. if (err < 0)
  1188. return err;
  1189. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
  1190. pcm->private_data = chip;
  1191. strcpy(pcm->name, chip->card->shortname);
  1192. chip->pcms[1] = pcm;
  1193. /* set up playback */
  1194. init_viadev(chip, chip->playback_devno, 0x30, 0);
  1195. if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1196. snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
  1197. return err;
  1198. return 0;
  1199. }
  1200. /*
  1201. * create a pcm instance for via686a/b
  1202. */
  1203. static int __devinit snd_via686_pcm_new(via82xx_t *chip)
  1204. {
  1205. snd_pcm_t *pcm;
  1206. int err;
  1207. chip->playback_devno = 0;
  1208. chip->capture_devno = 1;
  1209. chip->num_devs = 2;
  1210. chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
  1211. err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
  1212. if (err < 0)
  1213. return err;
  1214. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
  1215. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
  1216. pcm->private_data = chip;
  1217. strcpy(pcm->name, chip->card->shortname);
  1218. chip->pcms[0] = pcm;
  1219. init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0);
  1220. init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 1);
  1221. if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1222. snd_dma_pci_data(chip->pci), 64*1024, 128*1024)) < 0)
  1223. return err;
  1224. return 0;
  1225. }
  1226. /*
  1227. * Mixer part
  1228. */
  1229. static int snd_via8233_capture_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  1230. {
  1231. /* formerly they were "Line" and "Mic", but it looks like that they
  1232. * have nothing to do with the actual physical connections...
  1233. */
  1234. static char *texts[2] = {
  1235. "Input1", "Input2"
  1236. };
  1237. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1238. uinfo->count = 1;
  1239. uinfo->value.enumerated.items = 2;
  1240. if (uinfo->value.enumerated.item >= 2)
  1241. uinfo->value.enumerated.item = 1;
  1242. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  1243. return 0;
  1244. }
  1245. static int snd_via8233_capture_source_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1246. {
  1247. via82xx_t *chip = snd_kcontrol_chip(kcontrol);
  1248. unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
  1249. ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
  1250. return 0;
  1251. }
  1252. static int snd_via8233_capture_source_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1253. {
  1254. via82xx_t *chip = snd_kcontrol_chip(kcontrol);
  1255. unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
  1256. u8 val, oval;
  1257. spin_lock_irq(&chip->reg_lock);
  1258. oval = inb(port);
  1259. val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
  1260. if (ucontrol->value.enumerated.item[0])
  1261. val |= VIA_REG_CAPTURE_CHANNEL_MIC;
  1262. if (val != oval)
  1263. outb(val, port);
  1264. spin_unlock_irq(&chip->reg_lock);
  1265. return val != oval;
  1266. }
  1267. static snd_kcontrol_new_t snd_via8233_capture_source __devinitdata = {
  1268. .name = "Input Source Select",
  1269. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1270. .info = snd_via8233_capture_source_info,
  1271. .get = snd_via8233_capture_source_get,
  1272. .put = snd_via8233_capture_source_put,
  1273. };
  1274. static int snd_via8233_dxs3_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  1275. {
  1276. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1277. uinfo->count = 1;
  1278. uinfo->value.integer.min = 0;
  1279. uinfo->value.integer.max = 1;
  1280. return 0;
  1281. }
  1282. static int snd_via8233_dxs3_spdif_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1283. {
  1284. via82xx_t *chip = snd_kcontrol_chip(kcontrol);
  1285. u8 val;
  1286. pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
  1287. ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
  1288. return 0;
  1289. }
  1290. static int snd_via8233_dxs3_spdif_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1291. {
  1292. via82xx_t *chip = snd_kcontrol_chip(kcontrol);
  1293. u8 val, oval;
  1294. pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
  1295. val = oval & ~VIA8233_SPDIF_DX3;
  1296. if (ucontrol->value.integer.value[0])
  1297. val |= VIA8233_SPDIF_DX3;
  1298. /* save the spdif flag for rate filtering */
  1299. chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
  1300. if (val != oval) {
  1301. pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
  1302. return 1;
  1303. }
  1304. return 0;
  1305. }
  1306. static snd_kcontrol_new_t snd_via8233_dxs3_spdif_control __devinitdata = {
  1307. .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
  1308. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1309. .info = snd_via8233_dxs3_spdif_info,
  1310. .get = snd_via8233_dxs3_spdif_get,
  1311. .put = snd_via8233_dxs3_spdif_put,
  1312. };
  1313. static int snd_via8233_dxs_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  1314. {
  1315. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1316. uinfo->count = 2;
  1317. uinfo->value.integer.min = 0;
  1318. uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
  1319. return 0;
  1320. }
  1321. static int snd_via8233_dxs_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1322. {
  1323. via82xx_t *chip = snd_kcontrol_chip(kcontrol);
  1324. unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
  1325. ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
  1326. ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
  1327. return 0;
  1328. }
  1329. static int snd_via8233_pcmdxs_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1330. {
  1331. via82xx_t *chip = snd_kcontrol_chip(kcontrol);
  1332. ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
  1333. ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
  1334. return 0;
  1335. }
  1336. static int snd_via8233_dxs_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1337. {
  1338. via82xx_t *chip = snd_kcontrol_chip(kcontrol);
  1339. unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
  1340. unsigned long port = chip->port + 0x10 * idx;
  1341. unsigned char val;
  1342. int i, change = 0;
  1343. for (i = 0; i < 2; i++) {
  1344. val = ucontrol->value.integer.value[i];
  1345. if (val > VIA_DXS_MAX_VOLUME)
  1346. val = VIA_DXS_MAX_VOLUME;
  1347. val = VIA_DXS_MAX_VOLUME - val;
  1348. change |= val != chip->playback_volume[idx][i];
  1349. if (change) {
  1350. chip->playback_volume[idx][i] = val;
  1351. outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
  1352. }
  1353. }
  1354. return change;
  1355. }
  1356. static int snd_via8233_pcmdxs_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1357. {
  1358. via82xx_t *chip = snd_kcontrol_chip(kcontrol);
  1359. unsigned int idx;
  1360. unsigned char val;
  1361. int i, change = 0;
  1362. for (i = 0; i < 2; i++) {
  1363. val = ucontrol->value.integer.value[i];
  1364. if (val > VIA_DXS_MAX_VOLUME)
  1365. val = VIA_DXS_MAX_VOLUME;
  1366. val = VIA_DXS_MAX_VOLUME - val;
  1367. if (val != chip->playback_volume_c[i]) {
  1368. change = 1;
  1369. chip->playback_volume_c[i] = val;
  1370. for (idx = 0; idx < 4; idx++) {
  1371. unsigned long port = chip->port + 0x10 * idx;
  1372. chip->playback_volume[idx][i] = val;
  1373. outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
  1374. }
  1375. }
  1376. }
  1377. return change;
  1378. }
  1379. static snd_kcontrol_new_t snd_via8233_pcmdxs_volume_control __devinitdata = {
  1380. .name = "PCM Playback Volume",
  1381. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1382. .info = snd_via8233_dxs_volume_info,
  1383. .get = snd_via8233_pcmdxs_volume_get,
  1384. .put = snd_via8233_pcmdxs_volume_put,
  1385. };
  1386. static snd_kcontrol_new_t snd_via8233_dxs_volume_control __devinitdata = {
  1387. .name = "VIA DXS Playback Volume",
  1388. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1389. .count = 4,
  1390. .info = snd_via8233_dxs_volume_info,
  1391. .get = snd_via8233_dxs_volume_get,
  1392. .put = snd_via8233_dxs_volume_put,
  1393. };
  1394. /*
  1395. */
  1396. static void snd_via82xx_mixer_free_ac97_bus(ac97_bus_t *bus)
  1397. {
  1398. via82xx_t *chip = bus->private_data;
  1399. chip->ac97_bus = NULL;
  1400. }
  1401. static void snd_via82xx_mixer_free_ac97(ac97_t *ac97)
  1402. {
  1403. via82xx_t *chip = ac97->private_data;
  1404. chip->ac97 = NULL;
  1405. }
  1406. static struct ac97_quirk ac97_quirks[] = {
  1407. {
  1408. .subvendor = 0x1106,
  1409. .subdevice = 0x4161,
  1410. .codec_id = 0x56494161, /* VT1612A */
  1411. .name = "Soltek SL-75DRV5",
  1412. .type = AC97_TUNE_NONE
  1413. },
  1414. { /* FIXME: which codec? */
  1415. .subvendor = 0x1106,
  1416. .subdevice = 0x4161,
  1417. .name = "ASRock K7VT2",
  1418. .type = AC97_TUNE_HP_ONLY
  1419. },
  1420. {
  1421. .subvendor = 0x1019,
  1422. .subdevice = 0x0a81,
  1423. .name = "ECS K7VTA3",
  1424. .type = AC97_TUNE_HP_ONLY
  1425. },
  1426. {
  1427. .subvendor = 0x1019,
  1428. .subdevice = 0x0a85,
  1429. .name = "ECS L7VMM2",
  1430. .type = AC97_TUNE_HP_ONLY
  1431. },
  1432. {
  1433. .subvendor = 0x1849,
  1434. .subdevice = 0x3059,
  1435. .name = "ASRock K7VM2",
  1436. .type = AC97_TUNE_HP_ONLY /* VT1616 */
  1437. },
  1438. {
  1439. .subvendor = 0x14cd,
  1440. .subdevice = 0x7002,
  1441. .name = "Unknown",
  1442. .type = AC97_TUNE_ALC_JACK
  1443. },
  1444. {
  1445. .subvendor = 0x1071,
  1446. .subdevice = 0x8590,
  1447. .name = "Mitac Mobo",
  1448. .type = AC97_TUNE_ALC_JACK
  1449. },
  1450. {
  1451. .subvendor = 0x161f,
  1452. .subdevice = 0x202b,
  1453. .name = "Arima Notebook",
  1454. .type = AC97_TUNE_HP_ONLY,
  1455. },
  1456. { } /* terminator */
  1457. };
  1458. static int __devinit snd_via82xx_mixer_new(via82xx_t *chip, const char *quirk_override)
  1459. {
  1460. ac97_template_t ac97;
  1461. int err;
  1462. static ac97_bus_ops_t ops = {
  1463. .write = snd_via82xx_codec_write,
  1464. .read = snd_via82xx_codec_read,
  1465. .wait = snd_via82xx_codec_wait,
  1466. };
  1467. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
  1468. return err;
  1469. chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
  1470. chip->ac97_bus->clock = chip->ac97_clock;
  1471. memset(&ac97, 0, sizeof(ac97));
  1472. ac97.private_data = chip;
  1473. ac97.private_free = snd_via82xx_mixer_free_ac97;
  1474. ac97.pci = chip->pci;
  1475. ac97.scaps = AC97_SCAP_SKIP_MODEM;
  1476. if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
  1477. return err;
  1478. snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
  1479. if (chip->chip_type != TYPE_VIA686) {
  1480. /* use slot 10/11 */
  1481. snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
  1482. }
  1483. return 0;
  1484. }
  1485. #ifdef SUPPORT_JOYSTICK
  1486. #define JOYSTICK_ADDR 0x200
  1487. static int __devinit snd_via686_create_gameport(via82xx_t *chip, unsigned char *legacy)
  1488. {
  1489. struct gameport *gp;
  1490. struct resource *r;
  1491. if (!joystick)
  1492. return -ENODEV;
  1493. r = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport");
  1494. if (!r) {
  1495. printk(KERN_WARNING "via82xx: cannot reserve joystick port 0x%#x\n", JOYSTICK_ADDR);
  1496. return -EBUSY;
  1497. }
  1498. chip->gameport = gp = gameport_allocate_port();
  1499. if (!gp) {
  1500. printk(KERN_ERR "via82xx: cannot allocate memory for gameport\n");
  1501. release_and_free_resource(r);
  1502. return -ENOMEM;
  1503. }
  1504. gameport_set_name(gp, "VIA686 Gameport");
  1505. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  1506. gameport_set_dev_parent(gp, &chip->pci->dev);
  1507. gp->io = JOYSTICK_ADDR;
  1508. gameport_set_port_data(gp, r);
  1509. /* Enable legacy joystick port */
  1510. *legacy |= VIA_FUNC_ENABLE_GAME;
  1511. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
  1512. gameport_register_port(chip->gameport);
  1513. return 0;
  1514. }
  1515. static void snd_via686_free_gameport(via82xx_t *chip)
  1516. {
  1517. if (chip->gameport) {
  1518. struct resource *r = gameport_get_port_data(chip->gameport);
  1519. gameport_unregister_port(chip->gameport);
  1520. chip->gameport = NULL;
  1521. release_and_free_resource(r);
  1522. }
  1523. }
  1524. #else
  1525. static inline int snd_via686_create_gameport(via82xx_t *chip, unsigned char *legacy)
  1526. {
  1527. return -ENOSYS;
  1528. }
  1529. static inline void snd_via686_free_gameport(via82xx_t *chip) { }
  1530. #endif
  1531. /*
  1532. *
  1533. */
  1534. static int __devinit snd_via8233_init_misc(via82xx_t *chip)
  1535. {
  1536. int i, err, caps;
  1537. unsigned char val;
  1538. caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
  1539. for (i = 0; i < caps; i++) {
  1540. snd_via8233_capture_source.index = i;
  1541. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
  1542. if (err < 0)
  1543. return err;
  1544. }
  1545. if (ac97_can_spdif(chip->ac97)) {
  1546. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
  1547. if (err < 0)
  1548. return err;
  1549. }
  1550. if (chip->chip_type != TYPE_VIA8233A) {
  1551. /* when no h/w PCM volume control is found, use DXS volume control
  1552. * as the PCM vol control
  1553. */
  1554. snd_ctl_elem_id_t sid;
  1555. memset(&sid, 0, sizeof(sid));
  1556. strcpy(sid.name, "PCM Playback Volume");
  1557. sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1558. if (! snd_ctl_find_id(chip->card, &sid)) {
  1559. snd_printd(KERN_INFO "Using DXS as PCM Playback\n");
  1560. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
  1561. if (err < 0)
  1562. return err;
  1563. }
  1564. else /* Using DXS when PCM emulation is enabled is really weird */
  1565. {
  1566. /* Standalone DXS controls */
  1567. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs_volume_control, chip));
  1568. if (err < 0)
  1569. return err;
  1570. }
  1571. }
  1572. /* select spdif data slot 10/11 */
  1573. pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
  1574. val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
  1575. val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
  1576. pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
  1577. return 0;
  1578. }
  1579. static int __devinit snd_via686_init_misc(via82xx_t *chip)
  1580. {
  1581. unsigned char legacy, legacy_cfg;
  1582. int rev_h = 0;
  1583. legacy = chip->old_legacy;
  1584. legacy_cfg = chip->old_legacy_cfg;
  1585. legacy |= VIA_FUNC_MIDI_IRQMASK; /* FIXME: correct? (disable MIDI) */
  1586. legacy &= ~VIA_FUNC_ENABLE_GAME; /* disable joystick */
  1587. if (chip->revision >= VIA_REV_686_H) {
  1588. rev_h = 1;
  1589. if (mpu_port >= 0x200) { /* force MIDI */
  1590. mpu_port &= 0xfffc;
  1591. pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
  1592. #ifdef CONFIG_PM
  1593. chip->mpu_port_saved = mpu_port;
  1594. #endif
  1595. } else {
  1596. mpu_port = pci_resource_start(chip->pci, 2);
  1597. }
  1598. } else {
  1599. switch (mpu_port) { /* force MIDI */
  1600. case 0x300:
  1601. case 0x310:
  1602. case 0x320:
  1603. case 0x330:
  1604. legacy_cfg &= ~(3 << 2);
  1605. legacy_cfg |= (mpu_port & 0x0030) >> 2;
  1606. break;
  1607. default: /* no, use BIOS settings */
  1608. if (legacy & VIA_FUNC_ENABLE_MIDI)
  1609. mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
  1610. break;
  1611. }
  1612. }
  1613. if (mpu_port >= 0x200 &&
  1614. (chip->mpu_res = request_region(mpu_port, 2, "VIA82xx MPU401"))
  1615. != NULL) {
  1616. if (rev_h)
  1617. legacy |= VIA_FUNC_MIDI_PNP; /* enable PCI I/O 2 */
  1618. legacy |= VIA_FUNC_ENABLE_MIDI;
  1619. } else {
  1620. if (rev_h)
  1621. legacy &= ~VIA_FUNC_MIDI_PNP; /* disable PCI I/O 2 */
  1622. legacy &= ~VIA_FUNC_ENABLE_MIDI;
  1623. mpu_port = 0;
  1624. }
  1625. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
  1626. pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
  1627. if (chip->mpu_res) {
  1628. if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
  1629. mpu_port, 1,
  1630. chip->irq, 0, &chip->rmidi) < 0) {
  1631. printk(KERN_WARNING "unable to initialize MPU-401"
  1632. " at 0x%lx, skipping\n", mpu_port);
  1633. legacy &= ~VIA_FUNC_ENABLE_MIDI;
  1634. } else {
  1635. legacy &= ~VIA_FUNC_MIDI_IRQMASK; /* enable MIDI interrupt */
  1636. }
  1637. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
  1638. }
  1639. snd_via686_create_gameport(chip, &legacy);
  1640. #ifdef CONFIG_PM
  1641. chip->legacy_saved = legacy;
  1642. chip->legacy_cfg_saved = legacy_cfg;
  1643. #endif
  1644. return 0;
  1645. }
  1646. /*
  1647. * proc interface
  1648. */
  1649. static void snd_via82xx_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
  1650. {
  1651. via82xx_t *chip = entry->private_data;
  1652. int i;
  1653. snd_iprintf(buffer, "%s\n\n", chip->card->longname);
  1654. for (i = 0; i < 0xa0; i += 4) {
  1655. snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
  1656. }
  1657. }
  1658. static void __devinit snd_via82xx_proc_init(via82xx_t *chip)
  1659. {
  1660. snd_info_entry_t *entry;
  1661. if (! snd_card_proc_new(chip->card, "via82xx", &entry))
  1662. snd_info_set_text_ops(entry, chip, 1024, snd_via82xx_proc_read);
  1663. }
  1664. /*
  1665. *
  1666. */
  1667. static int snd_via82xx_chip_init(via82xx_t *chip)
  1668. {
  1669. unsigned int val;
  1670. unsigned long end_time;
  1671. unsigned char pval;
  1672. #if 0 /* broken on K7M? */
  1673. if (chip->chip_type == TYPE_VIA686)
  1674. /* disable all legacy ports */
  1675. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
  1676. #endif
  1677. pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
  1678. if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
  1679. /* deassert ACLink reset, force SYNC */
  1680. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
  1681. VIA_ACLINK_CTRL_ENABLE |
  1682. VIA_ACLINK_CTRL_RESET |
  1683. VIA_ACLINK_CTRL_SYNC);
  1684. udelay(100);
  1685. #if 1 /* FIXME: should we do full reset here for all chip models? */
  1686. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
  1687. udelay(100);
  1688. #else
  1689. /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
  1690. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
  1691. VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
  1692. udelay(2);
  1693. #endif
  1694. /* ACLink on, deassert ACLink reset, VSR, SGD data out */
  1695. /* note - FM data out has trouble with non VRA codecs !! */
  1696. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
  1697. udelay(100);
  1698. }
  1699. /* Make sure VRA is enabled, in case we didn't do a
  1700. * complete codec reset, above */
  1701. pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
  1702. if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
  1703. /* ACLink on, deassert ACLink reset, VSR, SGD data out */
  1704. /* note - FM data out has trouble with non VRA codecs !! */
  1705. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
  1706. udelay(100);
  1707. }
  1708. /* wait until codec ready */
  1709. end_time = jiffies + msecs_to_jiffies(750);
  1710. do {
  1711. pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
  1712. if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
  1713. break;
  1714. set_current_state(TASK_UNINTERRUPTIBLE);
  1715. schedule_timeout(1);
  1716. } while (time_before(jiffies, end_time));
  1717. if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
  1718. snd_printk("AC'97 codec is not ready [0x%x]\n", val);
  1719. #if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
  1720. snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
  1721. VIA_REG_AC97_SECONDARY_VALID |
  1722. (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
  1723. end_time = jiffies + msecs_to_jiffies(750);
  1724. snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
  1725. VIA_REG_AC97_SECONDARY_VALID |
  1726. (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
  1727. do {
  1728. if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
  1729. chip->ac97_secondary = 1;
  1730. goto __ac97_ok2;
  1731. }
  1732. set_current_state(TASK_INTERRUPTIBLE);
  1733. schedule_timeout(1);
  1734. } while (time_before(jiffies, end_time));
  1735. /* This is ok, the most of motherboards have only one codec */
  1736. __ac97_ok2:
  1737. #endif
  1738. if (chip->chip_type == TYPE_VIA686) {
  1739. /* route FM trap to IRQ, disable FM trap */
  1740. pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
  1741. /* disable all GPI interrupts */
  1742. outl(0, VIAREG(chip, GPI_INTR));
  1743. }
  1744. if (chip->chip_type != TYPE_VIA686) {
  1745. /* Workaround for Award BIOS bug:
  1746. * DXS channels don't work properly with VRA if MC97 is disabled.
  1747. */
  1748. struct pci_dev *pci;
  1749. pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
  1750. if (pci) {
  1751. unsigned char data;
  1752. pci_read_config_byte(pci, 0x44, &data);
  1753. pci_write_config_byte(pci, 0x44, data | 0x40);
  1754. pci_dev_put(pci);
  1755. }
  1756. }
  1757. if (chip->chip_type != TYPE_VIA8233A) {
  1758. int i, idx;
  1759. for (idx = 0; idx < 4; idx++) {
  1760. unsigned long port = chip->port + 0x10 * idx;
  1761. for (i = 0; i < 2; i++) {
  1762. chip->playback_volume[idx][i]=chip->playback_volume_c[i];
  1763. outb(chip->playback_volume_c[i], port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
  1764. }
  1765. }
  1766. }
  1767. return 0;
  1768. }
  1769. #ifdef CONFIG_PM
  1770. /*
  1771. * power management
  1772. */
  1773. static int snd_via82xx_suspend(snd_card_t *card, pm_message_t state)
  1774. {
  1775. via82xx_t *chip = card->pm_private_data;
  1776. int i;
  1777. for (i = 0; i < 2; i++)
  1778. if (chip->pcms[i])
  1779. snd_pcm_suspend_all(chip->pcms[i]);
  1780. for (i = 0; i < chip->num_devs; i++)
  1781. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  1782. synchronize_irq(chip->irq);
  1783. snd_ac97_suspend(chip->ac97);
  1784. /* save misc values */
  1785. if (chip->chip_type != TYPE_VIA686) {
  1786. pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
  1787. chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
  1788. chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
  1789. }
  1790. pci_set_power_state(chip->pci, 3);
  1791. pci_disable_device(chip->pci);
  1792. return 0;
  1793. }
  1794. static int snd_via82xx_resume(snd_card_t *card)
  1795. {
  1796. via82xx_t *chip = card->pm_private_data;
  1797. int i;
  1798. pci_enable_device(chip->pci);
  1799. pci_set_power_state(chip->pci, 0);
  1800. snd_via82xx_chip_init(chip);
  1801. if (chip->chip_type == TYPE_VIA686) {
  1802. if (chip->mpu_port_saved)
  1803. pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
  1804. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
  1805. pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
  1806. } else {
  1807. pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
  1808. outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
  1809. outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
  1810. }
  1811. snd_ac97_resume(chip->ac97);
  1812. for (i = 0; i < chip->num_devs; i++)
  1813. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  1814. return 0;
  1815. }
  1816. #endif /* CONFIG_PM */
  1817. static int snd_via82xx_free(via82xx_t *chip)
  1818. {
  1819. unsigned int i;
  1820. if (chip->irq < 0)
  1821. goto __end_hw;
  1822. /* disable interrupts */
  1823. for (i = 0; i < chip->num_devs; i++)
  1824. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  1825. synchronize_irq(chip->irq);
  1826. __end_hw:
  1827. if (chip->irq >= 0)
  1828. free_irq(chip->irq, (void *)chip);
  1829. release_and_free_resource(chip->mpu_res);
  1830. pci_release_regions(chip->pci);
  1831. if (chip->chip_type == TYPE_VIA686) {
  1832. snd_via686_free_gameport(chip);
  1833. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
  1834. pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
  1835. }
  1836. pci_disable_device(chip->pci);
  1837. kfree(chip);
  1838. return 0;
  1839. }
  1840. static int snd_via82xx_dev_free(snd_device_t *device)
  1841. {
  1842. via82xx_t *chip = device->device_data;
  1843. return snd_via82xx_free(chip);
  1844. }
  1845. static int __devinit snd_via82xx_create(snd_card_t * card,
  1846. struct pci_dev *pci,
  1847. int chip_type,
  1848. int revision,
  1849. unsigned int ac97_clock,
  1850. via82xx_t ** r_via)
  1851. {
  1852. via82xx_t *chip;
  1853. int err;
  1854. static snd_device_ops_t ops = {
  1855. .dev_free = snd_via82xx_dev_free,
  1856. };
  1857. if ((err = pci_enable_device(pci)) < 0)
  1858. return err;
  1859. if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
  1860. pci_disable_device(pci);
  1861. return -ENOMEM;
  1862. }
  1863. chip->chip_type = chip_type;
  1864. chip->revision = revision;
  1865. spin_lock_init(&chip->reg_lock);
  1866. spin_lock_init(&chip->rates[0].lock);
  1867. spin_lock_init(&chip->rates[1].lock);
  1868. chip->card = card;
  1869. chip->pci = pci;
  1870. chip->irq = -1;
  1871. pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
  1872. pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
  1873. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
  1874. chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
  1875. if ((err = pci_request_regions(pci, card->driver)) < 0) {
  1876. kfree(chip);
  1877. pci_disable_device(pci);
  1878. return err;
  1879. }
  1880. chip->port = pci_resource_start(pci, 0);
  1881. if (request_irq(pci->irq, snd_via82xx_interrupt, SA_INTERRUPT|SA_SHIRQ,
  1882. card->driver, (void *)chip)) {
  1883. snd_printk("unable to grab IRQ %d\n", pci->irq);
  1884. snd_via82xx_free(chip);
  1885. return -EBUSY;
  1886. }
  1887. chip->irq = pci->irq;
  1888. if (ac97_clock >= 8000 && ac97_clock <= 48000)
  1889. chip->ac97_clock = ac97_clock;
  1890. synchronize_irq(chip->irq);
  1891. if ((err = snd_via82xx_chip_init(chip)) < 0) {
  1892. snd_via82xx_free(chip);
  1893. return err;
  1894. }
  1895. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1896. snd_via82xx_free(chip);
  1897. return err;
  1898. }
  1899. /* The 8233 ac97 controller does not implement the master bit
  1900. * in the pci command register. IMHO this is a violation of the PCI spec.
  1901. * We call pci_set_master here because it does not hurt. */
  1902. pci_set_master(pci);
  1903. snd_card_set_dev(card, &pci->dev);
  1904. *r_via = chip;
  1905. return 0;
  1906. }
  1907. struct via823x_info {
  1908. int revision;
  1909. char *name;
  1910. int type;
  1911. };
  1912. static struct via823x_info via823x_cards[] __devinitdata = {
  1913. { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
  1914. { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
  1915. { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
  1916. { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
  1917. { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
  1918. { VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
  1919. };
  1920. /*
  1921. * auto detection of DXS channel supports.
  1922. */
  1923. struct dxs_whitelist {
  1924. unsigned short subvendor;
  1925. unsigned short subdevice;
  1926. unsigned short mask;
  1927. short action; /* new dxs_support value */
  1928. };
  1929. static int __devinit check_dxs_list(struct pci_dev *pci)
  1930. {
  1931. static struct dxs_whitelist whitelist[] = {
  1932. { .subvendor = 0x1005, .subdevice = 0x4710, .action = VIA_DXS_ENABLE }, /* Avance Logic Mobo */
  1933. { .subvendor = 0x1019, .subdevice = 0x0996, .action = VIA_DXS_48K },
  1934. { .subvendor = 0x1019, .subdevice = 0x0a81, .action = VIA_DXS_NO_VRA }, /* ECS K7VTA3 v8.0 */
  1935. { .subvendor = 0x1019, .subdevice = 0x0a85, .action = VIA_DXS_NO_VRA }, /* ECS L7VMM2 */
  1936. { .subvendor = 0x1019, .subdevice = 0xa101, .action = VIA_DXS_SRC },
  1937. { .subvendor = 0x1025, .subdevice = 0x0033, .action = VIA_DXS_NO_VRA }, /* Acer Inspire 1353LM */
  1938. { .subvendor = 0x1025, .subdevice = 0x0046, .action = VIA_DXS_SRC }, /* Acer Aspire 1524 WLMi */
  1939. { .subvendor = 0x1043, .subdevice = 0x8095, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8X (FIXME: possibly VIA_DXS_ENABLE?)*/
  1940. { .subvendor = 0x1043, .subdevice = 0x80a1, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8-X */
  1941. { .subvendor = 0x1043, .subdevice = 0x80b0, .action = VIA_DXS_NO_VRA }, /* ASUS A7V600 & K8V*/
  1942. { .subvendor = 0x1043, .subdevice = 0x810d, .action = VIA_DXS_SRC }, /* ASUS */
  1943. { .subvendor = 0x1043, .subdevice = 0x812a, .action = VIA_DXS_SRC }, /* ASUS A8V Deluxe */
  1944. { .subvendor = 0x1071, .subdevice = 0x8375, .action = VIA_DXS_NO_VRA }, /* Vobis/Yakumo/Mitac notebook */
  1945. { .subvendor = 0x1071, .subdevice = 0x8399, .action = VIA_DXS_NO_VRA }, /* Umax AB 595T (VIA K8N800A - VT8237) */
  1946. { .subvendor = 0x10cf, .subdevice = 0x118e, .action = VIA_DXS_ENABLE }, /* FSC laptop */
  1947. { .subvendor = 0x1106, .subdevice = 0x4161, .action = VIA_DXS_NO_VRA }, /* ASRock K7VT2 */
  1948. { .subvendor = 0x1106, .subdevice = 0x4552, .action = VIA_DXS_NO_VRA }, /* QDI Kudoz 7X/600-6AL */
  1949. { .subvendor = 0x1106, .subdevice = 0xaa01, .action = VIA_DXS_NO_VRA }, /* EPIA MII */
  1950. { .subvendor = 0x1106, .subdevice = 0xc001, .action = VIA_DXS_SRC }, /* Insight P4-ITX */
  1951. { .subvendor = 0x1297, .subdevice = 0xa232, .action = VIA_DXS_ENABLE }, /* Shuttle ?? */
  1952. { .subvendor = 0x1297, .subdevice = 0xc160, .action = VIA_DXS_ENABLE }, /* Shuttle SK41G */
  1953. { .subvendor = 0x1458, .subdevice = 0xa002, .action = VIA_DXS_ENABLE }, /* Gigabyte GA-7VAXP */
  1954. { .subvendor = 0x1462, .subdevice = 0x0080, .action = VIA_DXS_SRC }, /* MSI K8T Neo-FIS2R */
  1955. { .subvendor = 0x1462, .subdevice = 0x0430, .action = VIA_DXS_SRC }, /* MSI 7142 (K8MM-V) */
  1956. { .subvendor = 0x1462, .subdevice = 0x3800, .action = VIA_DXS_ENABLE }, /* MSI KT266 */
  1957. { .subvendor = 0x1462, .subdevice = 0x5901, .action = VIA_DXS_NO_VRA }, /* MSI KT6 Delta-SR */
  1958. { .subvendor = 0x1462, .subdevice = 0x7023, .action = VIA_DXS_NO_VRA }, /* MSI K8T Neo2-FI */
  1959. { .subvendor = 0x1462, .subdevice = 0x7120, .action = VIA_DXS_ENABLE }, /* MSI KT4V */
  1960. { .subvendor = 0x1462, .subdevice = 0x7142, .action = VIA_DXS_ENABLE }, /* MSI K8MM-V */
  1961. { .subvendor = 0x147b, .subdevice = 0x1401, .action = VIA_DXS_ENABLE }, /* ABIT KD7(-RAID) */
  1962. { .subvendor = 0x147b, .subdevice = 0x1411, .action = VIA_DXS_ENABLE }, /* ABIT VA-20 */
  1963. { .subvendor = 0x147b, .subdevice = 0x1413, .action = VIA_DXS_ENABLE }, /* ABIT KV8 Pro */
  1964. { .subvendor = 0x147b, .subdevice = 0x1415, .action = VIA_DXS_NO_VRA }, /* Abit AV8 */
  1965. { .subvendor = 0x14ff, .subdevice = 0x0403, .action = VIA_DXS_ENABLE }, /* Twinhead mobo */
  1966. { .subvendor = 0x14ff, .subdevice = 0x0408, .action = VIA_DXS_SRC }, /* Twinhead laptop */
  1967. { .subvendor = 0x1558, .subdevice = 0x4701, .action = VIA_DXS_SRC }, /* Clevo D470 */
  1968. { .subvendor = 0x1584, .subdevice = 0x8120, .action = VIA_DXS_ENABLE }, /* Gericom/Targa/Vobis/Uniwill laptop */
  1969. { .subvendor = 0x1584, .subdevice = 0x8123, .action = VIA_DXS_NO_VRA }, /* Uniwill (Targa Visionary XP-210) */
  1970. { .subvendor = 0x161f, .subdevice = 0x202b, .action = VIA_DXS_NO_VRA }, /* Amira Note book */
  1971. { .subvendor = 0x161f, .subdevice = 0x2032, .action = VIA_DXS_48K }, /* m680x machines */
  1972. { .subvendor = 0x1631, .subdevice = 0xe004, .action = VIA_DXS_ENABLE }, /* Easy Note 3174, Packard Bell */
  1973. { .subvendor = 0x1695, .subdevice = 0x3005, .action = VIA_DXS_ENABLE }, /* EPoX EP-8K9A */
  1974. { .subvendor = 0x1849, .subdevice = 0x3059, .action = VIA_DXS_NO_VRA }, /* ASRock K7VM2 */
  1975. { .subvendor = 0x1919, .subdevice = 0x200a, .action = VIA_DXS_NO_VRA }, /* Soltek SL-K8Tpro-939 */
  1976. { .subvendor = 0x4005, .subdevice = 0x4710, .action = VIA_DXS_SRC }, /* MSI K7T266 Pro2 (MS-6380 V2.0) BIOS 3.7 */
  1977. { } /* terminator */
  1978. };
  1979. struct dxs_whitelist *w;
  1980. unsigned short subsystem_vendor;
  1981. unsigned short subsystem_device;
  1982. pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
  1983. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
  1984. for (w = whitelist; w->subvendor; w++) {
  1985. if (w->subvendor != subsystem_vendor)
  1986. continue;
  1987. if (w->mask) {
  1988. if ((w->mask & subsystem_device) == w->subdevice)
  1989. return w->action;
  1990. } else {
  1991. if (subsystem_device == w->subdevice)
  1992. return w->action;
  1993. }
  1994. }
  1995. /*
  1996. * not detected, try 48k rate only to be sure.
  1997. */
  1998. printk(KERN_INFO "via82xx: Assuming DXS channels with 48k fixed sample rate.\n");
  1999. printk(KERN_INFO " Please try dxs_support=5 option\n");
  2000. printk(KERN_INFO " and report if it works on your machine.\n");
  2001. printk(KERN_INFO " For more details, read ALSA-Configuration.txt.\n");
  2002. return VIA_DXS_48K;
  2003. };
  2004. static int __devinit snd_via82xx_probe(struct pci_dev *pci,
  2005. const struct pci_device_id *pci_id)
  2006. {
  2007. snd_card_t *card;
  2008. via82xx_t *chip;
  2009. unsigned char revision;
  2010. int chip_type = 0, card_type;
  2011. unsigned int i;
  2012. int err;
  2013. card = snd_card_new(index, id, THIS_MODULE, 0);
  2014. if (card == NULL)
  2015. return -ENOMEM;
  2016. card_type = pci_id->driver_data;
  2017. pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
  2018. switch (card_type) {
  2019. case TYPE_CARD_VIA686:
  2020. strcpy(card->driver, "VIA686A");
  2021. sprintf(card->shortname, "VIA 82C686A/B rev%x", revision);
  2022. chip_type = TYPE_VIA686;
  2023. break;
  2024. case TYPE_CARD_VIA8233:
  2025. chip_type = TYPE_VIA8233;
  2026. sprintf(card->shortname, "VIA 823x rev%x", revision);
  2027. for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
  2028. if (revision == via823x_cards[i].revision) {
  2029. chip_type = via823x_cards[i].type;
  2030. strcpy(card->shortname, via823x_cards[i].name);
  2031. break;
  2032. }
  2033. }
  2034. if (chip_type != TYPE_VIA8233A) {
  2035. if (dxs_support == VIA_DXS_AUTO)
  2036. dxs_support = check_dxs_list(pci);
  2037. /* force to use VIA8233 or 8233A model according to
  2038. * dxs_support module option
  2039. */
  2040. if (dxs_support == VIA_DXS_DISABLE)
  2041. chip_type = TYPE_VIA8233A;
  2042. else
  2043. chip_type = TYPE_VIA8233;
  2044. }
  2045. if (chip_type == TYPE_VIA8233A)
  2046. strcpy(card->driver, "VIA8233A");
  2047. else if (revision >= VIA_REV_8237)
  2048. strcpy(card->driver, "VIA8237"); /* no slog assignment */
  2049. else
  2050. strcpy(card->driver, "VIA8233");
  2051. break;
  2052. default:
  2053. snd_printk(KERN_ERR "invalid card type %d\n", card_type);
  2054. err = -EINVAL;
  2055. goto __error;
  2056. }
  2057. if ((err = snd_via82xx_create(card, pci, chip_type, revision,
  2058. ac97_clock, &chip)) < 0)
  2059. goto __error;
  2060. if ((err = snd_via82xx_mixer_new(chip, ac97_quirk)) < 0)
  2061. goto __error;
  2062. if (chip_type == TYPE_VIA686) {
  2063. if ((err = snd_via686_pcm_new(chip)) < 0 ||
  2064. (err = snd_via686_init_misc(chip)) < 0)
  2065. goto __error;
  2066. } else {
  2067. if (chip_type == TYPE_VIA8233A) {
  2068. if ((err = snd_via8233a_pcm_new(chip)) < 0)
  2069. goto __error;
  2070. // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
  2071. } else {
  2072. if ((err = snd_via8233_pcm_new(chip)) < 0)
  2073. goto __error;
  2074. if (dxs_support == VIA_DXS_48K)
  2075. chip->dxs_fixed = 1;
  2076. else if (dxs_support == VIA_DXS_NO_VRA)
  2077. chip->no_vra = 1;
  2078. else if (dxs_support == VIA_DXS_SRC) {
  2079. chip->no_vra = 1;
  2080. chip->dxs_src = 1;
  2081. }
  2082. }
  2083. if ((err = snd_via8233_init_misc(chip)) < 0)
  2084. goto __error;
  2085. }
  2086. snd_card_set_pm_callback(card, snd_via82xx_suspend, snd_via82xx_resume, chip);
  2087. /* disable interrupts */
  2088. for (i = 0; i < chip->num_devs; i++)
  2089. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  2090. snprintf(card->longname, sizeof(card->longname),
  2091. "%s with %s at %#lx, irq %d", card->shortname,
  2092. snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
  2093. snd_via82xx_proc_init(chip);
  2094. if ((err = snd_card_register(card)) < 0) {
  2095. snd_card_free(card);
  2096. return err;
  2097. }
  2098. pci_set_drvdata(pci, card);
  2099. return 0;
  2100. __error:
  2101. snd_card_free(card);
  2102. return err;
  2103. }
  2104. static void __devexit snd_via82xx_remove(struct pci_dev *pci)
  2105. {
  2106. snd_card_free(pci_get_drvdata(pci));
  2107. pci_set_drvdata(pci, NULL);
  2108. }
  2109. static struct pci_driver driver = {
  2110. .name = "VIA 82xx Audio",
  2111. .owner = THIS_MODULE,
  2112. .id_table = snd_via82xx_ids,
  2113. .probe = snd_via82xx_probe,
  2114. .remove = __devexit_p(snd_via82xx_remove),
  2115. SND_PCI_PM_CALLBACKS
  2116. };
  2117. static int __init alsa_card_via82xx_init(void)
  2118. {
  2119. return pci_register_driver(&driver);
  2120. }
  2121. static void __exit alsa_card_via82xx_exit(void)
  2122. {
  2123. pci_unregister_driver(&driver);
  2124. }
  2125. module_init(alsa_card_via82xx_init)
  2126. module_exit(alsa_card_via82xx_exit)