via82xx.c 70 KB

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