via82xx.c 75 KB

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