via82xx.c 71 KB

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