via82xx.c 77 KB

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