via82xx.c 79 KB

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