via82xx.c 77 KB

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