via82xx.c 77 KB

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