via82xx.c 75 KB

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