via82xx.c 71 KB

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