via82xx.c 75 KB

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